UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 4.19 kB
import{UNIFORMTYPE_FLOAT as t,UNIFORMTYPE_VEC2 as s,UNIFORMTYPE_VEC3 as o,UNIFORMTYPE_VEC4 as e,UNIFORMTYPE_INT as n,UNIFORMTYPE_IVEC2 as i,UNIFORMTYPE_IVEC3 as r,UNIFORMTYPE_IVEC4 as a,UNIFORMTYPE_MAT2 as f,UNIFORMTYPE_MAT3 as c,UNIFORMTYPE_FLOATARRAY as l,UNIFORMTYPE_VEC2ARRAY as g,UNIFORMTYPE_VEC3ARRAY as h,UNIFORMTYPE_UINT as u,UNIFORMTYPE_UVEC2 as m,UNIFORMTYPE_UVEC3 as d,UNIFORMTYPE_UVEC4 as p,UNIFORMTYPE_INTARRAY as y,UNIFORMTYPE_BOOLARRAY as v,UNIFORMTYPE_UINTARRAY as F,UNIFORMTYPE_IVEC2ARRAY as U,UNIFORMTYPE_BVEC2ARRAY as b,UNIFORMTYPE_UVEC2ARRAY as I,UNIFORMTYPE_IVEC3ARRAY as S,UNIFORMTYPE_BVEC3ARRAY as w,UNIFORMTYPE_UVEC3ARRAY as B}from"./constants.js";import{DynamicBufferAllocation as z}from"./dynamic-buffers.js";const A=[];A[t]=function(t,s,o){t.storageFloat32[o]=s},A[s]=(t,s,o)=>{const e=t.storageFloat32;e[o]=s[0],e[o+1]=s[1]},A[o]=(t,s,o)=>{const e=t.storageFloat32;e[o]=s[0],e[o+1]=s[1],e[o+2]=s[2]},A[e]=(t,s,o)=>{const e=t.storageFloat32;e[o]=s[0],e[o+1]=s[1],e[o+2]=s[2],e[o+3]=s[3]},A[n]=function(t,s,o){t.storageInt32[o]=s},A[i]=function(t,s,o){const e=t.storageInt32;e[o]=s[0],e[o+1]=s[1]},A[r]=function(t,s,o){const e=t.storageInt32;e[o]=s[0],e[o+1]=s[1],e[o+2]=s[2]},A[a]=function(t,s,o){const e=t.storageInt32;e[o]=s[0],e[o+1]=s[1],e[o+2]=s[2],e[o+3]=s[3]},A[f]=(t,s,o)=>{const e=t.storageFloat32;e[o]=s[0],e[o+1]=s[1],e[o+4]=s[2],e[o+5]=s[3],e[o+8]=s[4],e[o+9]=s[5]},A[c]=(t,s,o)=>{const e=t.storageFloat32;e[o]=s[0],e[o+1]=s[1],e[o+2]=s[2],e[o+4]=s[3],e[o+5]=s[4],e[o+6]=s[5],e[o+8]=s[6],e[o+9]=s[7],e[o+10]=s[8]},A[l]=function(t,s,o,e){const n=t.storageFloat32;for(let t=0;t<e;t++)n[o+4*t]=s[t]},A[g]=(t,s,o,e)=>{const n=t.storageFloat32;for(let t=0;t<e;t++)n[o+4*t]=s[2*t],n[o+4*t+1]=s[2*t+1]},A[h]=(t,s,o,e)=>{const n=t.storageFloat32;for(let t=0;t<e;t++)n[o+4*t]=s[3*t],n[o+4*t+1]=s[3*t+1],n[o+4*t+2]=s[3*t+2]},A[u]=(t,s,o,e)=>{t.storageUint32[o]=s},A[m]=(t,s,o,e)=>{const n=t.storageUint32;n[o]=s[0],n[o+1]=s[1]},A[d]=(t,s,o,e)=>{const n=t.storageUint32;n[o]=s[0],n[o+1]=s[1],n[o+2]=s[2]},A[p]=(t,s,o,e)=>{const n=t.storageUint32;n[o]=s[0],n[o+1]=s[1],n[o+2]=s[2],n[o+3]=s[3]},A[y]=function(t,s,o,e){const n=t.storageInt32;for(let t=0;t<e;t++)n[o+4*t]=s[t]},A[v]=A[y],A[F]=function(t,s,o,e){const n=t.storageUint32;for(let t=0;t<e;t++)n[o+4*t]=s[t]},A[U]=(t,s,o,e)=>{const n=t.storageInt32;for(let t=0;t<e;t++)n[o+4*t]=s[2*t],n[o+4*t+1]=s[2*t+1]},A[b]=A[U],A[I]=(t,s,o,e)=>{const n=t.storageUint32;for(let t=0;t<e;t++)n[o+4*t]=s[2*t],n[o+4*t+1]=s[2*t+1]},A[S]=(t,s,o,e)=>{const n=t.storageInt32;for(let t=0;t<e;t++)n[o+4*t]=s[3*t],n[o+4*t+1]=s[3*t+1],n[o+4*t+2]=s[3*t+2]},A[w]=A[S],A[B]=(t,s,o,e)=>{const n=t.storageUint32;for(let t=0;t<e;t++)n[o+4*t]=s[3*t],n[o+4*t+1]=s[3*t+1],n[o+4*t+2]=s[3*t+2]};class x{constructor(t,s,o=!0){if(this.device=void 0,this.persistent=void 0,this.allocation=void 0,this.storageFloat32=void 0,this.storageInt32=void 0,this.storageUint32=void 0,this.renderVersionDirty=0,this.device=t,this.format=s,this.persistent=o,o){this.impl=t.createUniformBufferImpl(this);const o=new ArrayBuffer(s.byteSize);this.assignStorage(new Int32Array(o)),t._vram.ub+=this.format.byteSize}else this.allocation=new z}destroy(){if(this.persistent){const t=this.device;this.impl.destroy(t),t._vram.ub-=this.format.byteSize}}get offset(){return this.persistent?0:this.allocation.offset}assignStorage(t){this.storageInt32=t,this.storageUint32=new Uint32Array(t.buffer,t.byteOffset,t.byteLength/4),this.storageFloat32=new Float32Array(t.buffer,t.byteOffset,t.byteLength/4)}loseContext(){var t;null==(t=this.impl)||t.loseContext()}setUniform(t){const s=t.offset,o=t.scopeId.value;if(null!=o){const e=A[t.updateType];e?e(this,o,s,t.count):this.storageFloat32.set(o,s)}}set(t){const s=this.format.map.get(t);s&&this.setUniform(s)}update(){const t=this.persistent;if(!t){const t=this.allocation,s=t.gpuBuffer;this.device.dynamicBuffers.alloc(t,this.format.byteSize),this.assignStorage(t.storage),s!==t.gpuBuffer&&(this.renderVersionDirty=this.device.renderVersion)}const s=this.format.uniforms;for(let t=0;t<s.length;t++)this.setUniform(s[t]);t?this.impl.unlock(this):(this.storageFloat32=null,this.storageInt32=null)}}export{x as UniformBuffer};