/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../../chunks/_rollupPluginBabelHelpers","../../../../../core/maybe","../BufferPool","./FreeList","../../../../webgl/BufferObject","../../../../webgl/enums"],(function(t,i,e,r,s,n,u){"use strict";const h=1.25,a=32767,f=a<<16|a;let p=function(){function t(t,i,e){const s=r.TypedBuffer.create(i*e*Uint32Array.BYTES_PER_ELEMENT);this.size=i,this.strideInt=e,this.bufferType=t,this.dirty={start:1/0,end:0},this._gpu=null,this._cpu=s,this.clear()}var a=t.prototype;return a.invalidate=function(){this._invalidateTriangleBuffer(),e.applySome(this._gpu,(t=>t.dispose())),this._gpu=null},a._invalidateTriangleBuffer=function(){e.applySome(this._gpuComputeTriangles,(t=>t.dispose())),this._gpuComputeTriangles=null},a.destroy=function(){e.applySome(this._gpu,(t=>t.dispose())),e.applySome(this._gpuComputeTriangles,(t=>t.dispose())),e.applySome(this._cpu,(t=>t.destroy())),e.applySome(this._cpu2,(t=>t.destroy()))},a.clear=function(){this.dirty.start=1/0,this.dirty.end=0,this.freeList=new s.FreeList({start:0,end:this._cpu.length/this.strideInt}),this.fillPointer=0},a.ensure=function(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)*h),r=e*this.strideInt;this._cpu.expand(r*Uint32Array.BYTES_PER_ELEMENT),this.freeList.free(i,e-i)}},a.set=function(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,this.dirty.end))},a.getGPUBuffer=function(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 e.isNone(this._gpuComputeTriangles)&&(this._gpuComputeTriangles=this._createComputeBuffer(t)),this._gpuComputeTriangles}return e.isNone(this._gpu)&&(this._gpu=this._createBuffer(t)),this._gpu},a.getCPUBuffer=function(){if(!this._cpu2){const t=this._cpu.slice();this._cpu2=t}return this._cpu2.length!==this._cpu.length&&this._cpu2.expand(this._cpu.length*this._cpu.array.BYTES_PER_ELEMENT),this._cpu2.set(this._cpu),this._cpu2},a.maxAvailableSpace=function(){return this.freeList.maxAvailableSpace()},a.insert=function(t,i,r,s){const n=r*this.strideInt;if(!n)return 0;const u=i*this.strideInt*Uint32Array.BYTES_PER_ELEMENT,h=new Uint32Array(t,u,n),a=e.unwrapOrThrow(this.freeList.firstFit(r),"First fit region must be defined")*this.strideInt,f=n,p=a/this.strideInt-i;if(0!==s)for(let e=0;e<h.length;e++)h[e]+=s;return this._cpu.array.set(h,a),this.dirty.start=Math.min(this.dirty.start,a),this.dirty.end=Math.max(this.dirty.end,a+f),this.fillPointer=Math.max(this.fillPointer,a+f),p},a.free=function(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]=f;this.dirty.start=Math.min(this.dirty.start,r),this.dirty.end=Math.max(this.dirty.end,s),this.freeList.free(t,i)},a.upload=function(){if(this.dirty.end){if(this._invalidateTriangleBuffer(),e.isNone(this._gpu))return this.dirty.start=1/0,void(this.dirty.end=0);this._gpu.setSubDataFromView(this._cpu.array,this.dirty.start,this.dirty.start,this.dirty.end),this.dirty.start=1/0,this.dirty.end=0}},a._createBuffer=function(t){const i=u.Usage.DYNAMIC_DRAW;return"index"===this.bufferType?n.BufferObject.createIndex(t,i,this._cpu.array):n.BufferObject.createVertex(t,i,this._cpu.array)},a._createComputeBuffer=function(t){const i=u.Usage.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 n.BufferObject.createIndex(t,i,e)},i._createClass(t,[{key:"elementSize",get:function(){return this._cpu.length/this.strideInt}},{key:"invalidated",get:function(){return this.bufferSize&&!this._gpu}},{key:"invalidatedComputeBuffer",get:function(){return this.bufferSize&&!this._gpuComputeTriangles}},{key:"bufferSize",get:function(){return this._cpu.length/this.strideInt}}]),t}();t.Buffer=p,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
