UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 5.3 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import"../../../../../core/has.js";import{assertIsSome as t}from"../../../../../core/maybe.js";import{PooledUint32Array as i}from"../PooledUint32Array.js";import{FreeList as e}from"./FreeList.js";import{BufferObject as r}from"../../../../webgl/BufferObject.js";import{Usage as s}from"../../../../webgl/enums.js";const n=1.25,h=32767,a=h<<16|h;class d{constructor(t,e,r,s){this._pool=s;const n=i.create(e*r*Uint32Array.BYTES_PER_ELEMENT,this._pool);this.size=e,this.strideInt=r,this.bufferType=t,this.dirty={start:1/0,end:0},this.memoryStats={bytesUsed:0,bytesReserved:e*r*Uint32Array.BYTES_PER_ELEMENT},this._gpu=null,this._cpu=n,this.clear()}get elementSize(){return this._cpu.length/this.strideInt}get intSize(){return this.fillPointer*this.strideInt}get byteSize(){return this.intSize*Uint32Array.BYTES_PER_ELEMENT}get invalidated(){return this.bufferSize>0&&!this._gpu}get invalidatedComputeBuffer(){return this.bufferSize>0&&!this._gpuComputeTriangles}invalidate(){this._invalidateTriangleBuffer(),this._gpu?.dispose(),this._gpu=null}_invalidateTriangleBuffer(){this._gpuComputeTriangles?.dispose(),this._gpuComputeTriangles=null}destroy(){this._gpu?.dispose(),this._gpuComputeTriangles?.dispose(),this._cpu?.destroy()}clear(){this.dirty.start=1/0,this.dirty.end=0,this.freeList=new e({start:0,end:this._cpu.length/this.strideInt}),this.fillPointer=0}ensure(t){if(this.maxAvailableSpace()>=t)return;if(t*this.strideInt>this._cpu.length-this.fillPointer){this.invalidate();const i=this._cpu.length/this.strideInt,e=Math.round((i+t)*n),r=e*this.strideInt;this._cpu.expand(r*Uint32Array.BYTES_PER_ELEMENT),this.freeList.free(i,e-i),this.memoryStats.bytesReserved+=(e-i)*this.strideInt*Uint32Array.BYTES_PER_ELEMENT}}set(t,i){this._cpu.array[t]!==i&&(this._cpu.array[t]=i,this.dirty.start=Math.min(t,this.dirty.start),this.dirty.end=Math.max(t+1,this.dirty.end))}getGPUBuffer(t,i=!1){if(!this.bufferSize)return null;if(i){if("index"!==this.bufferType)throw new Error("Tired to get triangle buffer, but target is not an index buffer");return null==this._gpuComputeTriangles&&(this._gpuComputeTriangles=this._createComputeBuffer(t)),this._gpuComputeTriangles}return null==this._gpu&&(this._gpu=this._createBuffer(t)),this._gpu}getView(t,i){return this._cpu.getUint32View(t,i/Uint32Array.BYTES_PER_ELEMENT)}get bufferSize(){return this._cpu.length/this.strideInt}maxAvailableSpace(){return this.freeList.maxAvailableSpace()}insert(i,e,r,s){const n=r*this.strideInt;if(!n)return 0;const h=e*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,a=new Uint32Array(i,h,n),d=this.freeList.firstFit(r);t(d,"First fit region must be defined");const u=d*this.strideInt,o=n;if(this._cpu.array.set(a,u),0!==s)for(let t=0;t<a.length;t++)this._cpu.array[t+u]+=s;return this.dirty.start=Math.min(this.dirty.start,u),this.dirty.end=Math.max(this.dirty.end,u+o),this.fillPointer=Math.max(this.fillPointer,u+o),this.memoryStats.bytesUsed+=r*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,d}copyFrom(i,e,r,s,n){const h=r*this.strideInt;if(!h)return 0;const a=e*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,d=i._cpu.getUint32View(a,h),u=this.freeList.firstFit(r);t(u,"First fit region must be defined");const o=u*this.strideInt,f=h;if(this._cpu.array.set(d,o),0!==s)for(let t=0;t<h;t++)this._cpu.array[o+t*this.strideInt+n]+=s;return this.dirty.start=Math.min(this.dirty.start,o),this.dirty.end=Math.max(this.dirty.end,o+f),this.fillPointer=Math.max(this.fillPointer,o+f),this.memoryStats.bytesUsed+=r*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,u}free(t,i,e){const r=t*this.strideInt,s=(t+i)*this.strideInt;if(!0===e)for(let n=t;n!==t+i;n++)this._cpu.array[n*this.strideInt]=a;this.dirty.start=Math.min(this.dirty.start,r),this.dirty.end=Math.max(this.dirty.end,s),this.freeList.free(t,i),this.memoryStats.bytesUsed-=i*this.strideInt*Uint32Array.BYTES_PER_ELEMENT}upload(){if(this.dirty.end){if(this._invalidateTriangleBuffer(),null==this._gpu)return this.dirty.start=1/0,void(this.dirty.end=0);this._gpu.setSubData(this._cpu.array,this.dirty.start,this.dirty.start,this.dirty.end),this.dirty.start=1/0,this.dirty.end=0}}reshuffle(t,e){if(0===e.length)return;const r=this.byteSize,s=t*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,n=r>s,h=this._cpu,a=i.create(s,this._pool);n||a.array.set(this._cpu.getUint32View(0,this.intSize));for(const i of e)if(n||i.srcFrom!==i.dstFrom||0!==i.mutate){this.dirty.start=Math.min(this.dirty.start,i.dstFrom*this.strideInt),this.dirty.end=Math.max(this.dirty.end,(i.dstFrom+i.count)*this.strideInt);for(let t=0;t<i.count;t++){const e=(i.dstFrom+t)*this.strideInt,r=(i.srcFrom+t)*this.strideInt;for(let t=0;t<this.strideInt;t++)a.array[e+t]=h.array[r+t]+i.mutate}}this._cpu.destroy(),this._cpu=a,n&&this.invalidate(),this.freeList.clear(),this.memoryStats.bytesUsed=this.memoryStats.bytesReserved=s}_createBuffer(t){const i=s.DYNAMIC_DRAW;return"index"===this.bufferType?r.createIndex(t,i,this._cpu.array):r.createVertex(t,i,this._cpu.array)}_createComputeBuffer(t){const i=s.DYNAMIC_DRAW,e=new Uint32Array(this.fillPointer/3);for(let r=0;r<this.fillPointer;r+=3)e[r/3]=this._cpu.array[r];return r.createIndex(t,i,e)}}export{d as MappedBuffer};