/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{hasNativeFloat16Array as t}from"../../../../geometry/support/float16.js";import{BufferViewVec2f16 as e,BufferViewVec2f as i,BufferViewVec2f64 as s,BufferViewVec3f16 as r,BufferViewVec3f as n,BufferViewVec3f64 as h,BufferViewVec4f16 as d,BufferViewVec4f as u,BufferViewVec4f64 as f,BufferViewMat3f as o,BufferViewMat3f64 as p,BufferViewMat4f as l,BufferViewMat4f64 as a,BufferViewVec4u8 as c,BufferViewFloat16 as _,BufferViewFloat as m,BufferViewFloat64 as y,BufferViewUint8 as F,BufferViewUint16 as g,BufferViewInt8 as b,BufferViewVec2i8 as w,BufferViewVec2i16 as v,BufferViewVec2u8 as E,BufferViewVec2u16 as A,BufferViewVec4u16 as M,BufferViewVec4i16 as T,BufferViewUint32 as j,BufferViewVec3u8 as U,BufferViewVec3u16 as B,BufferViewVec2u32 as L,BufferViewVec3u32 as x,BufferViewVec4u32 as C,BufferViewVec3i8 as z,BufferViewVec4i8 as V,BufferViewInt16 as $,BufferViewVec3i16 as O,BufferViewInt32 as k,BufferViewVec2i32 as q,BufferViewVec3i32 as D,BufferViewVec4i32 as G}from"../../../../geometry/support/buffer/BufferView.js";import{elementTypeSize as H}from"../../../../geometry/support/buffer/types.js";import{glLayout as I}from"./glUtil.js";import{assert as J}from"../../webgl-engine/lib/Util.js";import{fromLayout as K}from"../../../webgl/VertexAttributeLocations.js";class N{constructor(t,e,i=0){this.layout=t,this.byteOffset=i,this.buffer="number"==typeof e?new ArrayBuffer(e*t.stride):e;for(const s of t.fields.keys()){const e=t.fields.get(s);this[s]=new e.constructor(this.buffer,i+e.offset,this.stride)}}get stride(){return this.layout.stride}get count(){return this.byteLength/this.stride}get byteLength(){return this.buffer.byteLength-this.byteOffset}getField(t,e){const i=this[t];return i&&i.elementCount===e.ElementCount&&i.elementType===e.ElementType?i:null}slice(t,e){return new N(this.layout,this.buffer.slice(t*this.stride,e*this.stride))}copyFrom(t,e=0,i=0,s=t.count){const r=this.stride;if(r%4==0){const n=new Uint32Array(t.buffer,e*r,s*r/4);new Uint32Array(this.buffer,i*r,s*r/4).set(n)}else{const n=new Uint8Array(t.buffer,e*r,s*r);new Uint8Array(this.buffer,i*r,s*r).set(n)}return this}get usedMemory(){return this.byteLength}dispose(){}}class P{constructor(t){this._stride=0,this._fields=new Map,t&&(this._stride=t.stride,t.fields.forEach(t=>this._fields.set(t[0],{...t[1],constructor:X(t[1].constructor)})))}freeze(){return this}get locations(){return K(I(this))}vec2f16(s,r){return this._appendField(s,t?e:i,r),this}vec2f(t,e){return this._appendField(t,i,e),this}vec2f64(t,e){return this._appendField(t,s,e),this}vec3f16(e,i){return this._appendField(e,t?r:n,i),this}vec3f(t,e){return this._appendField(t,n,e),this}vec3f64(t,e){return this._appendField(t,h,e),this}vec4f16(e,i){return this._appendField(e,t?d:u,i),this}vec4f(t,e){return this._appendField(t,u,e),this}vec4f64(t,e){return this._appendField(t,f,e),this}mat3f(t,e){return this._appendField(t,o,e),this}mat3f64(t,e){return this._appendField(t,p,e),this}mat4f(t,e){return this._appendField(t,l,e),this}mat4f64(t,e){return this._appendField(t,a,e),this}vec4u8(t,e){return this._appendField(t,c,e),this}f16(e,i){return this._appendField(e,t?_:m,i),this}f32(t,e){return this._appendField(t,m,e),this}f64(t,e){return this._appendField(t,y,e),this}u8(t,e){return this._appendField(t,F,e),this}u16(t,e){return this._appendField(t,g,e),this}i8(t,e){return this._appendField(t,b,e),this}vec2i8(t,e){return this._appendField(t,w,e),this}vec2i16(t,e){return this._appendField(t,v,e),this}vec2u8(t,e){return this._appendField(t,E,e),this}vec2u16(t,e){return this._appendField(t,A,e),this}vec4u16(t,e){return this._appendField(t,M,e),this}vec4i16(t,e){return this._appendField(t,T,e),this}u32(t,e){return this._appendField(t,j,e),this}_appendField(t,e,i){this._fields.has(t)&&J(!1,`${t} already added to vertex buffer layout`);const s=e.ElementCount*H(e.ElementType),r=this._stride;this._fields.set(t,{constructor:e,size:s,offset:r,optional:i}),this._alignFields()}_alignFields(){let t=0,e=1;this._fields.forEach(i=>{const s=H(i.constructor.ElementType);t=Math.floor((t+s-1)/s)*s,i.offset=t,t+=i.size,e=Math.max(e,s)}),t=Math.floor((t+e-1)/e)*e,this._stride=t}createBuffer(t){return new N(this,t)}createView(t,e){return new N(this,t,e)}clone(){const t=new P;return t._stride=this._stride,t._fields=new Map,this._fields.forEach((e,i)=>t._fields.set(i,e)),t.BufferType=this.BufferType,t}get stride(){return this._stride}get fields(){return this._fields}}function Q(){return new P}class R{constructor(t){this.fields=new Array,t.fields.forEach((t,e)=>{const i={...t,constructor:W(t.constructor)};this.fields.push([e,i])}),this.stride=t.stride}}const S=[m,i,n,u,o,l,y,s,h,f,p,a,F,E,U,c,g,A,B,M,j,L,x,C,b,w,z,V,$,v,O,T,k,q,D,G];function W(t){return`${t.ElementType}_${t.ElementCount}`}function X(t){return Y.get(t)}const Y=new Map;S.forEach(t=>Y.set(W(t),t));export{N as InterleavedBuffer,P as InterleavedLayout,R as PackedLayout,Q as newLayout};
