UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 2.34 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import e from"../../core/Logger.js";import{disposeMaybe as t}from"../../core/maybe.js";import{baseTypedArrayMemory as i}from"../../core/memoryEstimations.js";import{ResourceType as r}from"./enums.js";import{bindVertexBufferLayout as s}from"./Util.js";const n=()=>e.getLogger("esri.views.webgl.VertexArrayObject");let o=class{constructor(e,t,i,r,s=null){this._context=e,this._locations=t,this._layout=i,this._buffers=r,this._indexBuffer=s,this._glName=null,this._initialized=!1}get glName(){return this._glName}get context(){return this._context}get vertexBuffers(){return this._buffers}get indexBuffer(){return this._indexBuffer}get byteSize(){return Array.from(this._buffers.values()).reduce(((e,t)=>e+t.usedMemory),null!=this._indexBuffer?this._indexBuffer.usedMemory:0)}get layout(){return this._layout}get locations(){return this._locations}get usedMemory(){return this.byteSize+(this._buffers.size+(this._indexBuffer?1:0))*i}get cachedMemory(){return this.usedMemory}dispose(){this._context?(this._context.getBoundVAO()===this&&this._context.bindVAO(null),this._buffers.forEach((e=>e.dispose())),this._buffers.clear(),this._indexBuffer=t(this._indexBuffer),this.disposeVAOOnly()):(this._glName||this._buffers.size>0)&&n().warn("Leaked WebGL VAO")}disposeVAOOnly(){this._glName&&(this._context.gl.deleteVertexArray(this._glName),this._glName=null,this._context.instanceCounter.decrement(r.VertexArrayObject,this)),this._context=null}initialize(){if(this._initialized)return;const{gl:e}=this._context,t=e.createVertexArray();e.bindVertexArray(t),this._bindLayout(),e.bindVertexArray(null),this._glName=t,this._context.instanceCounter.increment(r.VertexArrayObject,this),this._initialized=!0}bind(){this.initialize(),this._context.gl.bindVertexArray(this.glName)}_bindLayout(){const{_buffers:e,_layout:t,_indexBuffer:i}=this;e||n().error("Vertex buffer dictionary is empty!");const r=this._context.gl;this._buffers.forEach(((e,i)=>{const r=t.get(i);r?s(this._context,this._locations,e,r):n().error("Vertex element descriptor is empty!")})),null!=i&&r.bindBuffer(r.ELEMENT_ARRAY_BUFFER,i.glName)}unbind(){this.initialize(),this._context.gl.bindVertexArray(null)}};export{o as VertexArrayObject};