VertexArrayObject.js 2.7 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.24/esri/copyright.txt for details.
  4. */
  5. import t from"../../core/Logger.js";import{isSome as e,disposeMaybe as i}from"../../core/maybe.js";import{ResourceType as r}from"./enums.js";import{bindVertexBufferLayout as s,unbindVertexBufferLayout as n}from"./Util.js";const o=t.getLogger("esri.views.webgl.VertexArrayObject");class f{constructor(t,e,i,s,n=null){this._context=t,this._locations=e,this._layout=i,this._buffers=s,this._indexBuffer=n,this._glName=null,this._initialized=!1,t.instanceCounter.increment(r.VertexArrayObject,this)}get glName(){return this._glName}get context(){return this._context}get vertexBuffers(){return this._buffers}get indexBuffer(){return this._indexBuffer}get size(){return Object.keys(this._buffers).reduce(((t,e)=>t+this._buffers[e].size),e(this._indexBuffer)?this._indexBuffer.size:0)}get layout(){return this._layout}get locations(){return this._locations}dispose(t=!0){if(!this._context)return void((this._glName||t&&Object.getOwnPropertyNames(this._buffers).length>0)&&o.warn("Leaked WebGL VAO"));if(this._glName){const t=this._context?.capabilities?.vao;t?(t.deleteVertexArray(this._glName),this._glName=null):o.warn("Leaked WebGL VAO")}if(this._context.getBoundVAO()===this&&this._context.bindVAO(null),t){for(const t in this._buffers)this._buffers[t].dispose(),delete this._buffers[t];this._indexBuffer=i(this._indexBuffer)}this._context.instanceCounter.decrement(r.VertexArrayObject,this),this._context=null}initialize(){if(this._initialized)return;const t=this._context.capabilities.vao;if(t){const e=t.createVertexArray();t.bindVertexArray(e),this._bindLayout(),t.bindVertexArray(null),this._glName=e}this._initialized=!0}bind(){this.initialize();const t=this._context.capabilities.vao;t?t.bindVertexArray(this.glName):(this._context.bindVAO(null),this._bindLayout())}_bindLayout(){const{_buffers:t,_layout:i,_indexBuffer:r}=this;t||o.error("Vertex buffer dictionary is empty!");const n=this._context.gl;for(const e in t){const r=t[e];r||o.error("Vertex buffer is uninitialized!");const n=i[e];n||o.error("Vertex element descriptor is empty!"),s(this._context,this._locations,r,n)}if(e(r)){!!this._context.capabilities.vao?n.bindBuffer(n.ELEMENT_ARRAY_BUFFER,r.glName):this._context.bindBuffer(r)}}unbind(){this.initialize();const t=this._context.capabilities.vao;t?t.bindVertexArray(null):this._unbindLayout()}_unbindLayout(){const{_buffers:t,_layout:i}=this;t||o.error("Vertex buffer dictionary is empty!");for(const e in t){const r=t[e];r||o.error("Vertex buffer is uninitialized!");const s=i[e];n(this._context,this._locations,r,s)}e(this._indexBuffer)&&this._context.unbindBuffer(this._indexBuffer.bufferType)}}export{f as VertexArrayObject};