UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 5.57 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import"../../../../../core/has.js";import{assertIsSome as t}from"../../../../../core/maybe.js";import{toUint32 as i,toFloat32 as r}from"../number.js";import{PooledUint32Array as e}from"../PooledUint32Array.js";import{FreeList as s}from"./FreeList.js";import{BufferObject as n}from"../../../../webgl/BufferObject.js";import{Usage as h}from"../../../../webgl/enums.js";const a=1.25,d=32767,u=d<<16|d;class o{constructor(t,i,r,s){this._pool=s;const n=e.create(i*r*Uint32Array.BYTES_PER_ELEMENT,this._pool);this.size=i,this.strideInt=r,this.bufferType=t,this.dirty={start:1/0,end:0},this.memoryStats={bytesUsed:0,bytesReserved:i*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 s({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,r=Math.round((i+t)*a),e=r*this.strideInt;this._cpu.expand(e*Uint32Array.BYTES_PER_ELEMENT),this.freeList.free(i,r-i),this.memoryStats.bytesReserved+=(r-i)*this.strideInt*Uint32Array.BYTES_PER_ELEMENT}}setU32(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))}setF32(t,r){this.setU32(t,i(r))}setF32Range(t,r,e){const s=i(e);this._cpu.array.fill(s,t,r),this.dirty.start=Math.min(t,this.dirty.start),this.dirty.end=Math.max(r,this.dirty.end)}getF32(t){return r(this._cpu.array[t])}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,r,e,s){const n=e*this.strideInt;if(!n)return 0;const h=r*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,a=new Uint32Array(i,h,n),d=this.freeList.firstFit(e);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+=e*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,d}copyFrom(i,r,e,s,n){const h=e*this.strideInt;if(!h)return 0;const a=r*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,d=i._cpu.getUint32View(a,h),u=this.freeList.firstFit(e);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+=e*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,u}free(t,i,r){const e=t*this.strideInt,s=(t+i)*this.strideInt;if(!0===r)for(let n=t;n!==t+i;n++)this._cpu.array[n*this.strideInt]=u;this.dirty.start=Math.min(this.dirty.start,e),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,i){if(0===i.length)return;const r=this.byteSize,s=t*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,n=r>s,h=this._cpu,a=e.create(s,this._pool);n||a.array.set(this._cpu.getUint32View(0,this.intSize));for(const e of i)if(n||e.srcFrom!==e.dstFrom||0!==e.mutate){this.dirty.start=Math.min(this.dirty.start,e.dstFrom*this.strideInt),this.dirty.end=Math.max(this.dirty.end,(e.dstFrom+e.count)*this.strideInt);for(let t=0;t<e.count;t++){const i=(e.dstFrom+t)*this.strideInt,r=(e.srcFrom+t)*this.strideInt;for(let t=0;t<this.strideInt;t++)a.array[i+t]=h.array[r+t]+e.mutate}}this._cpu.destroy(),this._cpu=a,n&&this.invalidate(),this.freeList.clear(),this.memoryStats.bytesUsed=this.memoryStats.bytesReserved=s}_createBuffer(t){const i=h.DYNAMIC_DRAW;return"index"===this.bufferType?n.createIndex(t,i,this._cpu.array):n.createVertex(t,i,this._cpu.array)}_createComputeBuffer(t){const i=h.DYNAMIC_DRAW,r=new Uint32Array(this.fillPointer/3);for(let e=0;e<this.fillPointer;e+=3)r[e/3]=this._cpu.array[e];return n.createIndex(t,i,r)}}export{o as MappedBuffer};