/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{reallocShrinkThreshold as t,reallocGrowthFactor as i,reallocShrinkFactor as e}from"../../../../../core/arrayUtils.js";import{BufferViewVec4f as s,BufferViewVec4u8 as a}from"../../../../../geometry/support/buffer/BufferView.js";import{newLayout as n}from"../../../support/buffer/InterleavedLayout.js";import{assert as r}from"../Util.js";import{BackedBufferObject as h}from"./BackedBufferObject.js";import{addOptionalLayoutFields as d,initialCapacity as _}from"./InstanceData.js";class c{constructor(t){this.model=t.instanceModel,this.modelNormal=t.instanceModelNormal,this.modelOriginHi=t.instanceModelOriginHi,this.modelOriginLo=t.instanceModelOriginLo,this.featureAttribute=t.getField("instanceFeatureAttribute",s),this.color=t.getField("instanceColor",a),this.olidColor=t.getField("instanceOlidColor",a)}}class o{constructor(t,i){this._rctx=t,this._layout=i,this._headIndex=0,this._tailIndex=0,this._firstIndex=null,this._captureFirstIndex=!0,this._updating=!1,this._prevHeadIndex=0,this._resized=!1,this._capacity=1}destroy(){this._buffer&&this._buffer.destroy()}get buffer(){return this._buffer.buffer}get view(){return this._view}get capacity(){return this._capacity}get size(){const t=this._headIndex,i=this._tailIndex;return t>=i?t-i:t+this._capacity-i}get isEmpty(){return this._headIndex===this._tailIndex}get isFull(){return this._tailIndex===(this._headIndex+1)%this._capacity}get headIndex(){return this._headIndex}get tailIndex(){return this._tailIndex}get firstIndex(){return this._firstIndex}get usedMemory(){return this._buffer?.usedMemory??0}reset(){this._headIndex=0,this._tailIndex=0,this._firstIndex=null}startUpdateCycle(){this._captureFirstIndex=!0}beginUpdate(){r(!this._updating,"already updating"),this._updating=!0,this._prevHeadIndex=this._headIndex}endUpdate(){r(this._updating,"not updating"),this.size<t*this.capacity&&this._shrink(),this._resized?(this._buffer.transferAll(),this._resized=!1):this._transferRange(this._prevHeadIndex,this._headIndex),this._updating=!1}allocateHead(){r(this._updating,"not updating"),this.isFull&&this._grow();const t=this.headIndex;return this._captureFirstIndex&&(this._firstIndex=t,this._captureFirstIndex=!1),this._incrementHead(),r(this._headIndex!==this._tailIndex,"invalid pointers"),t}freeTail(){r(this._updating,"not updating"),r(this.size>0,"invalid size");const t=this._tailIndex===this._firstIndex;this._incrementTail(),t&&(this._firstIndex=this._tailIndex)}_grow(){const t=Math.max(_,Math.floor(this._capacity*i));this._resize(t)}_shrink(){const t=Math.max(_,Math.floor(this._capacity*e));this._resize(t)}_resize(t){if(r(this._updating,"not updating"),t===this._capacity)return;const i=new h(this._rctx,this._layout,t);if(this._buffer){this._firstIndex&&(this._firstIndex=(this._firstIndex+this._capacity-this._tailIndex)%this._capacity);const t=this.size,e=this._compactInstances(i);r(e===t,"invalid compaction"),this._buffer.destroy(),this._tailIndex=0,this._headIndex=e,this._prevHeadIndex=0}this._resized=!0,this._capacity=t,this._buffer=i,this._view=new c(this._layout.createView(this._buffer.array))}_compactInstances(t){const i=this._headIndex,e=this._tailIndex;return e<i?(this._buffer.copyRange(e,i,t),i-e):e>i?(this._buffer.copyRange(e,this._capacity,t),i>0&&this._buffer.copyRange(0,i,t,this._capacity-e),i+(this._capacity-e)):0}_incrementHead(t=1){this._headIndex=(this._headIndex+t)%this._capacity}_incrementTail(t=1){this._tailIndex=(this._tailIndex+t)%this._capacity}_transferRange(t,i){t<i?this._buffer.transferRange(t,i):t>i&&(i>0&&this._buffer.transferRange(0,i),this._buffer.transferRange(t,this._capacity))}}const f=n().vec3f("instanceModelOriginHi").vec3f("instanceModelOriginLo").mat3f("instanceModel").mat3f("instanceModelNormal");function u(t){return d(f.clone(),t)}export{o as RenderInstanceData,c as RenderInstanceDataView,u as getRenderInstanceDataLayout};
