UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 1.92 kB
import t from"../../core/Logger.js";import{STD140_LAYOUT as e}from"./storageLayouts.js";function a(t,e){return t+(e-t%e)%e}const r=(()=>{const t=new ArrayBuffer(2),e=new Uint8Array(t),a=new Uint16Array(t);return e[0]=170,e[1]=187,48042===a[0]})(),n=4,i=t.getLogger("esri.views.webgl.UniformBufferLayout");class s{constructor(t){this._layout=new Map,this.byteLength=this._buildLayout(t),this.array=new ArrayBuffer(this.byteLength),this.arrayView32=new Float32Array(this.array),this._dataView=new DataView(this.array)}setValues(t,e){for(const a in t)this.setValue(a,t[a],e)}setValue(t,a,r){if(!this._layout.has(t))return void i.warn("Trying to set an unrecognized layout array member!");let s=this.byteLength/n,o=0;const{type:h,offset:y,arrayLength:u}=this._layout.get(t),{numberType:l,elementLength:f,elementCount:g,arrayStride:m}=e[h],w="number"!=typeof a,c=u*g,_=c*f;if((w?"byteLength"in a?a.byteLength/n:a.length:1)!==_&&i.warn("Attempting to set layout array member value with the wrong length!"),w)for(let e=0;e<c;e++)for(let t=0;t<f;t++){const r=y+e*m+t*n,i=e*f+t;if(this._setData(r,a[i],l)){const t=r/n;s=Math.min(s,t),o=Math.max(o,t+1)}}else if(this._setData(y,a,l)){const t=y/n;s=Math.min(s,t),o=Math.max(o,t+1)}r&&(r.from=Math.min(s,r.from),r.to=Math.max(o,r.to))}_setData(t,e,a){switch(a){case"Int32":if(e!==this._dataView.getInt32(t,r))return this._dataView.setInt32(t,e,r),!0;break;case"Uint32":if(e!==this._dataView.getUint32(t,r))return this._dataView.setUint32(t,e,r),!0;break;case"Float32":if(e!==this._dataView.getFloat32(t,r))return this._dataView.setFloat32(t,e,r),!0}return!1}_buildLayout(t){let r=0;for(const{name:i,type:s,count:o}of t){const{elementLength:t,elementCount:h,elementAlignment:y,arrayStride:u,arrayAlignment:l}=e[s],f=o??1,g=f*h,m=g>1,w=m?l:y,c=m?g*u:t*n;r=a(r,w),this._layout.set(i,{type:s,offset:r,arrayLength:f}),r+=c,m&&(r=a(r,w))}return a(r,4*n)}}export{s as UniformBufferLayout};