UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 3.78 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{isArrayLike as t}from"../../core/arrayUtils.js";import"../../core/has.js";import e from"../../core/Logger.js";import{isUint8Array as i,isUint16Array as s,isUint32Array as r}from"../../core/typedArrayUtil.js";import{checkWebGLError as n}from"./checkWebGLError.js";import{BufferType as _,Usage as a,ResourceType as u,DataType as f}from"./enums.js";const h=()=>e.getLogger("esri.views.webgl.BufferObject"),c=null;class E{static createIndex(t,e,i){return new E(t,_.ELEMENT_ARRAY_BUFFER,e,i)}static createVertex(t,e,i){return new E(t,_.ARRAY_BUFFER,e,i)}static createUniform(t,e,i){return new E(t,_.UNIFORM_BUFFER,e,i)}static createPixelPack(t,e=a.STREAM_READ,i){const s=new E(t,_.PIXEL_PACK_BUFFER,e);return i&&s.setSize(i),s}static createPixelUnpack(t,e=a.STREAM_DRAW,i){return new E(t,_.PIXEL_UNPACK_BUFFER,e,i)}static createTransformFeedback(t,e=a.STATIC_DRAW,i){const s=new E(t,_.TRANSFORM_FEEDBACK_BUFFER,e);return s.setSize(i),s}constructor(t,e,i,s){this._context=t,this.bufferType=e,this.usage=i,this._glName=null,this._size=-1,this._indexType=void 0,t.instanceCounter.increment(u.BufferObject,this),this._glName=this._context.gl.createBuffer(),n(this._context.gl),s&&this.setData(s)}get glName(){return this._glName}get size(){return this._size}get indexType(){return this._indexType}get usedMemory(){if(this.bufferType===_.ELEMENT_ARRAY_BUFFER){if(this._indexType===f.UNSIGNED_INT)return 4*this._size;if(this._indexType===f.UNSIGNED_SHORT)return 2*this._size}return this._size}get _isVAOAware(){return this.bufferType===_.ELEMENT_ARRAY_BUFFER||this.bufferType===_.ARRAY_BUFFER}dispose(){if(this._context?.gl){if(this._glName){this._context.gl.deleteBuffer(this._glName),this._glName=null}this._context.instanceCounter.decrement(u.BufferObject,this),this._context=null}else this._glName&&h().warn("Leaked WebGL buffer object")}setSize(t,e=null){if(this.bufferType===_.ELEMENT_ARRAY_BUFFER&&null!=e)switch(this._indexType=e,e){case f.UNSIGNED_SHORT:t*=2;break;case f.UNSIGNED_INT:t*=4}this._setBufferData(t)}setData(t){if(!t)return;let e=t.byteLength;this.bufferType===_.ELEMENT_ARRAY_BUFFER&&(i(t)?this._indexType=f.UNSIGNED_BYTE:s(t)?(e/=2,this._indexType=f.UNSIGNED_SHORT):r(t)&&(e/=4,this._indexType=f.UNSIGNED_INT)),this._setBufferData(e,t)}_setBufferData(t,e=null){this._size=t;const i=this._context.getBoundVAO();this._isVAOAware&&this._context.bindVAO(null),this._context.bindBuffer(this);const s=this._context.gl;null!=e?s.bufferData(this.bufferType,e,this.usage):s.bufferData(this.bufferType,t,this.usage),n(s),this._isVAOAware&&this._context.bindVAO(i)}setSubData(t,e,i,s){if(!t)return;const r=this._context.getBoundVAO();this._isVAOAware&&this._context.bindVAO(null),this._context.bindBuffer(this);const{gl:_}=this._context;_.bufferSubData(this.bufferType,e*t.BYTES_PER_ELEMENT,t,i,s-i),n(_),this._isVAOAware&&this._context.bindVAO(r)}getSubData(t,e=0,i,s){if(i<0||s<0)return;const r=o(t)?t.BYTES_PER_ELEMENT:1;if(r*((i??0)+(s??0))>t.byteLength)return;e+r*(s??0)>this.usedMemory&&h().warn("Potential problem getting subdata: requested data exceeds buffer size!");const n=this._context.gl;this.bufferType===_.TRANSFORM_FEEDBACK_BUFFER?(this._context.bindBuffer(this,_.TRANSFORM_FEEDBACK_BUFFER),n.getBufferSubData(_.TRANSFORM_FEEDBACK_BUFFER,e,t,i,s),this._context.unbindBuffer(_.TRANSFORM_FEEDBACK_BUFFER)):(this._context.bindBuffer(this,_.COPY_READ_BUFFER),n.getBufferSubData(_.COPY_READ_BUFFER,e,t,i,s),this._context.unbindBuffer(_.COPY_READ_BUFFER))}async getSubDataAsync(t,e=0,i,s){await this._context.clientWaitAsync(),this.getSubData(t,e,i,s)}}function o(e){return t(e)}export{E as BufferObject,c as tracer};