BufferObject.js 4.9 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{isArrayLike as e}from"../../core/arrayUtils.js";import t from"../../core/Logger.js";import{isSome as r}from"../../core/maybe.js";import{isUint16Array as i,isUint32Array as s}from"../../core/typedArrayUtil.js";import{checkWebGLError as n}from"./checkWebGLError.js";import{ContextType as o}from"./context-util.js";import{BufferType as f,ResourceType as a,Usage as u,DataType as h}from"./enums.js";const _=t.getLogger("esri.views.webgl.BufferObject");function b(t){return e(t)}class c{constructor(e,t,r,i){this._context=e,this.bufferType=t,this.usage=r,this._glName=null,this._size=-1,this._indexType=void 0,e.instanceCounter.increment(a.BufferObject,this),this._glName=this._context.gl.createBuffer(),n(this._context.gl),i&&this.setData(i)}static createIndex(e,t,r){return new c(e,f.ELEMENT_ARRAY_BUFFER,t,r)}static createVertex(e,t,r){return new c(e,f.ARRAY_BUFFER,t,r)}static createUniform(e,t,r){if(e.type!==o.WEBGL2)throw new Error("Uniform buffers are supported in WebGL2 only!");return new c(e,f.UNIFORM_BUFFER,t,r)}static createPixelPack(e,t=u.STREAM_READ,r){if(e.type!==o.WEBGL2)throw new Error("Pixel pack buffers are supported in WebGL2 only!");const i=new c(e,f.PIXEL_PACK_BUFFER,t);return r&&i.setSize(r),i}static createPixelUnpack(e,t=u.STREAM_DRAW,r){if(e.type!==o.WEBGL2)throw new Error("Pixel unpack buffers are supported in WebGL2 only!");return new c(e,f.PIXEL_UNPACK_BUFFER,t,r)}get glName(){return this._glName}get size(){return this._size}get indexType(){return this._indexType}get byteSize(){return this.bufferType===f.ELEMENT_ARRAY_BUFFER?this._indexType===h.UNSIGNED_INT?4*this._size:2*this._size:this._size}get _isVAOAware(){return this.bufferType===f.ELEMENT_ARRAY_BUFFER||this.bufferType===f.ARRAY_BUFFER}dispose(){if(this._context?.gl){if(this._glName){this._context.gl.deleteBuffer(this._glName),this._glName=null}this._context.instanceCounter.decrement(a.BufferObject,this),this._context=null}else this._glName&&_.warn("Leaked WebGL buffer object")}setSize(e,t=null){if(e<=0&&_.error("Buffer size needs to be positive!"),this.bufferType===f.ELEMENT_ARRAY_BUFFER&&r(t))switch(this._indexType=t,t){case h.UNSIGNED_SHORT:e*=2;break;case h.UNSIGNED_INT:e*=4}this._setBufferData(e)}setData(e){if(!e)return;let t=e.byteLength;this.bufferType===f.ELEMENT_ARRAY_BUFFER&&(i(e)&&(t/=2,this._indexType=h.UNSIGNED_SHORT),s(e)&&(t/=4,this._indexType=h.UNSIGNED_INT)),this._setBufferData(t,e)}_setBufferData(e,t=null){this._size=e;const i=this._context.getBoundVAO();this._isVAOAware&&this._context.bindVAO(null),this._context.bindBuffer(this);const s=this._context.gl;r(t)?s.bufferData(this.bufferType,t,this.usage):s.bufferData(this.bufferType,e,this.usage),n(s),this._isVAOAware&&this._context.bindVAO(i)}setSubData(e,t=0,r=0,o=e.byteLength){if(!e)return;(t<0||t>=this._size)&&_.error("offset is out of range!");let a=t,u=r,h=o,b=e.byteLength;this.bufferType===f.ELEMENT_ARRAY_BUFFER&&(i(e)?(b/=2,a*=2,u*=2,h*=2):s(e)&&(b/=4,a*=4,u*=4,h*=4)),void 0===o&&(o=b-1),r>=o&&_.error("end must be bigger than start!"),t+r-o>this._size&&_.error("An attempt to write beyond the end of the buffer!");const c=this._context.getBoundVAO();this._isVAOAware&&this._context.bindVAO(null),this._context.bindBuffer(this);const E=this._context.gl,l=ArrayBuffer.isView(e)?e.buffer:e,d=0===u&&h===e.byteLength?l:l.slice(u,h);E.bufferSubData(this.bufferType,a,d),n(E),this._isVAOAware&&this._context.bindVAO(c)}setSubDataFromView(e,t,r,i){if(!e)return;(t<0||t>=this._size)&&_.error("offset is out of range!"),r>=i&&_.error("end must be bigger than start!"),t+r-i>this._size&&_.error("An attempt to write beyond the end of the buffer!");const s=this._context.getBoundVAO();this._isVAOAware&&this._context.bindVAO(null),this._context.bindBuffer(this);const f=this._context.gl;if(this._context.type===o.WEBGL2)f.bufferSubData(this.bufferType,t*e.BYTES_PER_ELEMENT,e,r,i-r);else{const s=0===r&&i===e.length?e:e.subarray(r,i);f.bufferSubData(this.bufferType,t*e.BYTES_PER_ELEMENT,s)}n(f),this._isVAOAware&&this._context.bindVAO(s)}getSubData(e,t=0,r,i){if(this._context.type!==o.WEBGL2)return void _.error("Get buffer subdata is supported in WebGL2 only!");if(r<0||i<0)return void _.error("Problem getting subdata: offset and length were less than zero!");const s=b(e)?e.BYTES_PER_ELEMENT:1;if(s*((r??0)+(i??0))>e.byteLength)return void _.error("Problem getting subdata: offset and length exceeded destination size!");t+s*(i??0)>this.byteSize&&_.warn("Potential problem getting subdata: requested data exceeds buffer size!");const n=this._context.gl;this._context.bindBuffer(this,f.COPY_READ_BUFFER),n.getBufferSubData(f.COPY_READ_BUFFER,t,e,r,i),this._context.unbindBuffer(f.COPY_READ_BUFFER)}async getSubDataAsync(e,t=0,r,i){this._context.type===o.WEBGL2?(await this._context.clientWaitAsync(),this.getSubData(e,t,r,i)):_.error("Get buffer subdata is supported in WebGL2 only!")}}export{c as BufferObject};