VertexArrayObject.js 2.8 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import t from"../../core/Logger.js";import{isSome as e,disposeMaybe as i,nullifyNonNullableForDispose as r}from"../../core/maybe.js";import{ResourceType as s}from"./enums.js";import{bindVertexBufferLayout as n,unbindVertexBufferLayout as o}from"./Util.js";const f=t.getLogger("esri.views.webgl.VertexArrayObject");class a{constructor(t,e,i,r,n=null){this._context=t,this._locations=e,this._layout=i,this._buffers=r,this._indexBuffer=n,this._glName=null,this._initialized=!1,t.instanceCounter.increment(s.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)&&f.warn("Leaked WebGL VAO"));if(this._glName){const t=this._context?.capabilities?.vao;t?(t.deleteVertexArray(this._glName),this._glName=null):f.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(s.VertexArrayObject,this),this._context=r(this._context)}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||f.error("Vertex buffer dictionary is empty!");const s=this._context.gl;for(const e in t){const r=t[e];r||f.error("Vertex buffer is uninitialized!");const s=i[e];s||f.error("Vertex element descriptor is empty!"),n(this._context,this._locations,r,s)}if(e(r)){!!this._context.capabilities.vao?s.bindBuffer(s.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||f.error("Vertex buffer dictionary is empty!");for(const e in t){const r=t[e];r||f.error("Vertex buffer is uninitialized!");const s=i[e];o(this._context,this._locations,r,s)}e(this._indexBuffer)&&this._context.unbindBuffer(this._indexBuffer.bufferType)}}export{a as VertexArrayObject};