/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{hasNativeFloat16Array as e}from"../../../../geometry/support/float16.js";import{BufferViewVec2f16 as t,BufferViewVec2f as i,BufferViewVec2f64 as r,BufferViewVec3f16 as s,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 F,BufferViewUint8 as y,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 k,BufferViewInt32 as q,BufferViewVec2i32 as D,BufferViewVec3i32 as G,BufferViewVec4i32 as H}from"../../../../geometry/support/buffer/BufferView.js";import{elementTypeSize as I}from"../../../../geometry/support/buffer/types.js";import{glLayout as J}from"./glUtil.js";import{assert as K}from"../../webgl-engine/lib/Util.js";import{fromLayout as N}from"../../../webgl/VertexAttributeLocations.js";class O{constructor(e,t){this.layout=e,this.buffer="number"==typeof t?new ArrayBuffer(t*e.stride):t;for(const i of e.fields.keys()){const t=e.fields.get(i);this[i]=new t.constructor(this.buffer,t.offset,this.stride)}}get stride(){return this.layout.stride}get count(){return this.buffer.byteLength/this.stride}get byteLength(){return this.buffer.byteLength}getField(e,t){const i=this[e];return i&&i.elementCount===t.ElementCount&&i.elementType===t.ElementType?i:null}slice(e,t){return new O(this.layout,this.buffer.slice(e*this.stride,t*this.stride))}copyFrom(e,t=0,i=0,r=e.count){const s=this.stride;if(s%4==0){const n=new Uint32Array(e.buffer,t*s,r*s/4);new Uint32Array(this.buffer,i*s,r*s/4).set(n)}else{const n=new Uint8Array(e.buffer,t*s,r*s);new Uint8Array(this.buffer,i*s,r*s).set(n)}return this}get usedMemory(){return this.byteLength}dispose(){}}class P{constructor(e){this._stride=0,this._fields=new Map,e&&(this._stride=e.stride,e.fields.forEach(e=>this._fields.set(e[0],{...e[1],constructor:X(e[1].constructor)})))}freeze(){return this}get locations(){return N(J(this))}vec2f16(r,s){return this._appendField(r,e?t:i,s),this}vec2f(e,t){return this._appendField(e,i,t),this}vec2f64(e,t){return this._appendField(e,r,t),this}vec3f16(t,i){return this._appendField(t,e?s:n,i),this}vec3f(e,t){return this._appendField(e,n,t),this}vec3f64(e,t){return this._appendField(e,h,t),this}vec4f16(t,i){return this._appendField(t,e?d:u,i),this}vec4f(e,t){return this._appendField(e,u,t),this}vec4f64(e,t){return this._appendField(e,f,t),this}mat3f(e,t){return this._appendField(e,o,t),this}mat3f64(e,t){return this._appendField(e,p,t),this}mat4f(e,t){return this._appendField(e,l,t),this}mat4f64(e,t){return this._appendField(e,a,t),this}vec4u8(e,t){return this._appendField(e,c,t),this}f16(t,i){return this._appendField(t,e?_:m,i),this}f32(e,t){return this._appendField(e,m,t),this}f64(e,t){return this._appendField(e,F,t),this}u8(e,t){return this._appendField(e,y,t),this}u16(e,t){return this._appendField(e,g,t),this}i8(e,t){return this._appendField(e,b,t),this}vec2i8(e,t){return this._appendField(e,w,t),this}vec2i16(e,t){return this._appendField(e,v,t),this}vec2u8(e,t){return this._appendField(e,E,t),this}vec2u16(e,t){return this._appendField(e,A,t),this}vec4u16(e,t){return this._appendField(e,M,t),this}vec4i16(e,t){return this._appendField(e,T,t),this}u32(e,t){return this._appendField(e,j,t),this}_appendField(e,t,i){this._fields.has(e)&&K(!1,`${e} already added to vertex buffer layout`);const r=t.ElementCount*I(t.ElementType),s=this._stride;this._fields.set(e,{constructor:t,size:r,offset:s,optional:i}),this._alignFields()}_alignFields(){let e=0,t=1;this._fields.forEach(i=>{const r=I(i.constructor.ElementType);e=Math.floor((e+r-1)/r)*r,i.offset=e,e+=i.size,t=Math.max(t,r)}),e=Math.floor((e+t-1)/t)*t,this._stride=e}createBuffer(e){return new O(this,e)}createView(e){return new O(this,e)}clone(){const e=new P;return e._stride=this._stride,e._fields=new Map,this._fields.forEach((t,i)=>e._fields.set(i,t)),e.BufferType=this.BufferType,e}get stride(){return this._stride}get fields(){return this._fields}}function Q(){return new P}class R{constructor(e){this.fields=new Array,e.fields.forEach((e,t)=>{const i={...e,constructor:W(e.constructor)};this.fields.push([t,i])}),this.stride=e.stride}}const S=[m,i,n,u,o,l,F,r,h,f,p,a,y,E,U,c,g,A,B,M,j,L,x,C,b,w,z,V,$,v,k,T,q,D,G,H];function W(e){return`${e.ElementType}_${e.ElementCount}`}function X(e){return Y.get(e)}const Y=new Map;S.forEach(e=>Y.set(W(e),e));export{O as InterleavedBuffer,P as InterleavedLayout,R as PackedLayout,Q as newLayout};
