@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 3.41 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{isArrayLike as t}from"../../core/arrayUtils.js";import has from"../../core/has.js";import e from"../../core/Logger.js";import{isUint8Array as s,isUint16Array as i,isUint32Array as r}from"../../core/typedArrayUtil.js";import{checkWebGLError as n}from"./checkWebGLError.js";import{ResourceType as u,DataType as a}from"./enums.js";const f=()=>e.getLogger("esri.views.webgl.BufferObject"),h=null,c=!!has("esri-tests-disable-gpu-memory-measurements");class o{static createIndex(t,e,s){return new o(t,34963,e,s)}static createUniform(t,e,s){return new o(t,35345,e,s)}static createPixelPack(t,e=35041,s){const i=new o(t,35051,e);return s&&i.setSize(s),i}static createPixelUnpack(t,e=35040,s){return new o(t,35052,e,s)}static createTransformFeedback(t,e=35044,s){const i=new o(t,35982,e);return i.setSize(s),i}constructor(t,e,s,i){this._context=t,this.bufferType=e,this.usage=s,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),i&&this.setData(i)}get glName(){return this._glName}get size(){return this._size}get indexType(){return this._indexType}get sizeBytes(){if(34963===this.bufferType){if(this._indexType===a.UNSIGNED_INT)return 4*this._size;if(this._indexType===a.UNSIGNED_SHORT)return 2*this._size}return this._size}get usedMemory(){return c?0:this.sizeBytes}get _isVAOAware(){return 34963===this.bufferType||34962===this.bufferType}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&&f().warn("Leaked WebGL buffer object")}setSize(t,e=null){if(34963===this.bufferType&&null!=e)switch(this._indexType=e,e){case a.UNSIGNED_SHORT:t*=2;break;case a.UNSIGNED_INT:t*=4}this._setBufferData(t)}setData(t){if(!t)return;let e=t.byteLength;34963===this.bufferType&&(s(t)?this._indexType=a.UNSIGNED_BYTE:i(t)?(e/=2,this._indexType=a.UNSIGNED_SHORT):r(t)&&(e/=4,this._indexType=a.UNSIGNED_INT)),this._setBufferData(e,t)}_setBufferData(t,e=null){this._size=t;const s=this._context.getBoundVAO();this._isVAOAware&&this._context.bindVAO(null),this._context.bindBuffer(this);const i=this._context.gl;null!=e?i.bufferData(this.bufferType,e,this.usage):i.bufferData(this.bufferType,t,this.usage),n(i),this._isVAOAware&&this._context.bindVAO(s)}setSubData(t,e,s,i){if(!t)return;const r=this._context.getBoundVAO();this._isVAOAware&&this._context.bindVAO(null),this._context.bindBuffer(this);const{gl:u}=this._context;u.bufferSubData(this.bufferType,e*t.BYTES_PER_ELEMENT,t,s,i-s),n(u),this._isVAOAware&&this._context.bindVAO(r)}getSubData(t,e=0,s,i){if(s<0||i<0)return;const r=_(t)?t.BYTES_PER_ELEMENT:1;if(r*((s??0)+(i??0))>t.byteLength)return;e+r*(i??0)>this.usedMemory&&f().warn("Potential problem getting subdata: requested data exceeds buffer size!");const n=this._context.gl;35982===this.bufferType?(this._context.bindBuffer(this,35982),n.getBufferSubData(35982,e,t,s,i),this._context.unbindBuffer(35982)):(this._context.bindBuffer(this,36662),n.getBufferSubData(36662,e,t,s,i),this._context.unbindBuffer(36662))}async getSubDataAsync(t,e=0,s,i){await this._context.clientWaitAsync(),this.getSubData(t,e,s,i)}}function _(e){return t(e)}export{o as BufferObject,h as tracer};