typegpu 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/chunk-3JTP2ATR.cjs.map +1 -1
- package/chunk-3QXCKMEJ.cjs.map +1 -1
- package/chunk-QRLTUOBJ.cjs.map +1 -1
- package/data/index.cjs.map +1 -1
- package/index.cjs +10 -10
- package/index.cjs.map +1 -1
- package/index.js +10 -10
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/std/index.cjs.map +1 -1
package/index.js
CHANGED
@@ -1,26 +1,26 @@
|
|
1
|
-
import{$ as v,a as Cr,aa as W,b as Er,c as Xe,ca as h,d as Ze,e as X,f as Vr,g as Z,h as k,i as Pt,j as $,k as Le,oa as Gr,ra as $r,ta as et,ua as tt}from"./chunk-VOFHV4Z6.js";import{a as b,b as j,c as K,d as Rt,e as se,f as P,h as _r,i as q,j as Pr,k as Qe,l as Lr}from"./chunk-KVOFUY5A.js";import{A as S,B as _e,C,D as E,E as Ke,F as qe,G as Je,I as He,J as _t,K as he,L as I,M as B,N as d,O as Pe,P as vr,Q as Ur,R as Ar,S as Ir,T as Rr,U as Br,V as be,W as Q,X as _,a as A,b as L,c as p,d as Ue,da as Ye,e as ce,f as Ft,g as vt,h as Ut,i as oe,j as Ae,k as Ie,l as Dr,m as z,n as At,o as It,p as ie,q as Fr,r as Bt,s as G,t as Re,u as H,v as Y,w as ue,x as Be,y as ye,z as xe}from"./chunk-DR55IHDD.js";var Mn=["vec2f","vec2h","vec2i","vec2u","vec3f","vec3h","vec3i","vec3u","vec4f","vec4h","vec4i","vec4u","struct"],Nn={f:{1:d,2:G,3:ue,4:S},h:{1:Pe,2:Re,3:Be,4:_e},i:{1:B,2:H,3:ye,4:C},u:{1:I,2:Y,3:xe,4:E}},Or={vec2f:G,vec2h:Re,vec2i:H,vec2u:Y,vec3f:ue,vec3h:Be,vec3i:ye,vec3u:xe,vec4f:S,vec4h:_e,vec4i:C,vec4u:E,mat2x2f:Ke,mat3x3f:qe,mat4x4f:Je},kr={vec2f:d,vec2h:Pe,vec2i:B,vec2u:I,vec3f:d,vec3h:Pe,vec3i:B,vec3u:I,vec4f:d,vec4h:Pe,vec4i:B,vec4u:I,mat2x2f:G,mat3x3f:ue,mat4x4f:S};function Ce(e,t){var o;if(typeof e=="string"||typeof e=="number"||typeof e=="boolean")return P;if(K(e)||j(e)){let i=z();if(!i)throw new Error("Resolution context not found when unwrapping slot or derived");let s=i.unwrap(e);return we(s)}let r=e;for(Lr(r)&&(r=r[b].dataType);Ye(r);)r=r.inner;let n="kind"in r?r.kind:r.type;if(n==="struct")return(o=r.propTypes[t])!=null?o:P;let a=t.length;if(Mn.includes(n)&&a>=1&&a<=4){let i=n[4],s=Nn[i][a];if(s)return s}return be(r)?r:P}function Wr(e){if(be(e)){if(Q(e))return e.elementType;if(e.type in kr)return kr[e.type]}return P}function we(e){var t,r;if(K(e)||j(e))return we(e.value);if(typeof e=="string")return P;if(typeof e=="number")return(r=(t=Lt(String(e)))==null?void 0:t.dataType)!=null?r:P;if(typeof e=="boolean")return he;if("kind"in e){let n=e.kind;if(n in Or)return Or[n]}return be(e)?e:P}function Lt(e){if(/^0x[0-9a-f]+$/i.test(e))return{value:e,dataType:He};if(/^0b[01]+$/i.test(e))return{value:`${Number.parseInt(e.slice(2),2)}`,dataType:He};if(/^-?(?:\d+\.\d*|\d*\.\d+)$/i.test(e))return{value:e,dataType:_t};if(/^-?\d+(?:\.\d+)?e-?\d+$/i.test(e))return{value:e,dataType:_t};if(/^-?\d+$/i.test(e))return{value:e,dataType:He}}var M={get(e,t){if(t in e)return Reflect.get(e,t);if(t!=="~providing")return t==="toString"||t===Symbol.toStringTag||t===Symbol.toPrimitive?()=>e.toString():new Proxy({"~resolve":r=>`${r.resolve(e)}.${String(t)}`,toString:()=>{var r;return`.value(...).${String(t)}:${(r=e.label)!=null?r:"<unnamed>"}`},[b]:{dataType:Ce(e[b].dataType,String(t))}},M)}};function Ee(e){let t=e;for(;j(t)||K(t)||se(t)||Qe(t);)t=t.value;return t}function Nr(e,t){return new Ct(e,t)}var Mr;Mr=b;var Ct=class{constructor(t,r){this.dataType=t;this._value=r;p(this,"_label");p(this,Mr);this[b]={dataType:t}}get label(){return this._label}$name(t){return this._label=t,this}"~resolve"(t){let r=t.names.makeUnique(this._label),n=t.resolveValue(this._value,this.dataType);return t.addDeclaration(`const ${r} = ${n};`),r}toString(){var t;return`const:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){return ie()?new Proxy({"~resolve":t=>t.resolve(this),toString:()=>{var t;return`.value:${(t=this.label)!=null?t:"<unnamed>"}`},[b]:{dataType:this.dataType}},M):this._value}};function N(e){return!!e&&(typeof e=="object"||typeof e=="function")&&"$name"in e}function pe(e,t){for(let[r,n]of Object.entries(t))e[r]=n,N(n)&&(!("label"in n)||n.label===void 0)&&n.$name(r)}function zr(e,t,r){let n=[...e.matchAll(new RegExp(":\\s*(?<arg>.*?)\\s*[,)]","g"))].map(a=>a?a[1]:void 0);r(Object.fromEntries(t.flatMap((a,o)=>{let i=n?n[o]:void 0;return _(a)&&i!==void 0?[[i,a]]:[]})))}function Se(e,t,r){var o;let n=e.match(new RegExp("->(?<output>.*?){","s")),a=n?(o=n[1])==null?void 0:o.trim():void 0;_(t)&&a&&!/\s/g.test(a)&&r({[a]:t})}function zn(e){return new RegExp(`(?<![\\w_.])${e.replaceAll(".","\\.")}(?![\\w_])`,"g")}function le(e,t,r){return Object.entries(t).reduce((n,[a,o])=>{var i;return q(o)||Xe(o)?n.replaceAll(zn(a),e.resolve(o)):o!==null&&typeof o=="object"?((i=[...r.matchAll(new RegExp(`${a.replaceAll(".","\\.")}\\.(?<prop>.*?)(?![\\w_])`,"g"))].map(l=>l[1]))!=null?i:[]).reduce((l,u)=>u&&u in o?le(e,{[`${a}.${u}`]:o[u]},l):l,n):n},r)}function jr(e){return new Et(e)}var Et=class{constructor(t){this.declaration=t;p(this,"externalsToApply",[])}$uses(t){return this.externalsToApply.push(t),this}"~resolve"(t){let r={};for(let a of this.externalsToApply)pe(r,a);let n=le(t,r,this.declaration);return t.addDeclaration(n),""}toString(){return`declare: ${this.declaration}`}};var Kr=new WeakMap;function qr(e){return Kr.get(e)}function Jr(e,t,r){return Kr.set(e,{ast:t,externals:r}),e}function ee(e,t){let r=[];return typeof t=="string"&&(zr(t,e.argTypes,n=>r.push(n)),Se(t,e.returnType,n=>r.push(n))),{label:void 0,applyExternals(n){r.push(n)},resolve(n,a=""){var s;let o={};for(let l of r)pe(o,l);let i=n.names.makeUnique(this.label);if(typeof t=="string"){let l=le(n,o,t.trim());n.addDeclaration(`${a}fn ${i}${l}`)}else{let l=qr(t);if(l!=null&&l.externals){let y=Object.fromEntries(Object.entries(l.externals).filter(([T])=>!(T in o)));pe(o,y)}let u=(s=l==null?void 0:l.ast)!=null?s:n.transpileFn(String(t)),m=u.externalNames.filter(y=>!(y in o));if(m.length>0)throw new Ut(this.label,m);let f=u.argNames.map((y,T)=>({value:y,dataType:e.argTypes[T]})),{head:g,body:w}=n.fnToWgsl({args:f,returnType:e.returnType,body:u.body,externalMap:o});n.addDeclaration(`${a}fn ${i}${n.resolve(g)}${n.resolve(w)}`)}return i}}}function Hr(e){let t=0;return Object.fromEntries(Object.entries(e).map(([r,n])=>{if(et(n))return[r,n];let a=Z(n);return a!==void 0?(t=a+1,[r,n]):[r,Gr(n,{type:"@location",value:t++})]}))}function rt(e){return k(e)?Z(e)!==void 0?e:$r(0,e):Pt(Hr(e))}function De(e){return Pt(Hr(e))}function Yr(e){var t,r,n,a;return{argTypes:[De((t=e.in)!=null?t:{})],returnType:void 0,workgroupSize:[(r=e.workgroupSize[0])!=null?r:1,(n=e.workgroupSize[1])!=null?n:1,(a=e.workgroupSize[2])!=null?a:1],does(o){return jn(this,e.workgroupSize,o)}}}function jn(e,t,r){let n=ee(e,r),a=e.argTypes[0];return{shell:e,get label(){return n.label},$uses(o){return n.applyExternals(o),this},$name(o){return n.label=o,N(a)&&a.$name(`${o}_Input`),this},"~resolve"(o){return n.resolve(o,`@compute @workgroup_size(${t.join(", ")}) `)},toString(){var o;return`computeFn:${(o=this.label)!=null?o:"<unnamed>"}`}}}function Qr(e,t){return{argTypes:e,returnType:t,does(r){return qn(this,r)}}}function Vt(e){return(e==null?void 0:e.resourceType)==="function"}function Kn([e,t]){var r;return`${(r=e.label)!=null?r:"<unnamed>"}=${t}`}function qn(e,t){let r=ee(e,t),n={shell:e,resourceType:"function",$uses(i){return r.applyExternals(i),this},$name(i){return r.label=i,this},with(i,s){return Xr(o,[[se(i)?i.slot:i,s]])},"~resolve"(i){return r.resolve(i)}},a=Bt((...i)=>{if(typeof t=="string")throw new Error("Cannot execute on the CPU functions constructed with raw WGSL");return t(...i)},(...i)=>{var s;return{value:new nt(o,i.map(l=>l.value)),dataType:(s=e.returnType)!=null?s:P}});Object.defineProperty(a,b,{value:{implementation:t}});let o=Object.assign(a,n);return Object.defineProperty(o,"label",{get:()=>r.label}),Object.defineProperty(o,"toString",{value:()=>{var i;return`fn:${(i=r.label)!=null?i:"<unnamed>"}`}}),o}function Xr(e,t){let r={resourceType:"function",shell:e.shell,"~providing":{inner:e,pairs:t},$uses(o){return e.$uses(o),this},$name(o){return e.$name(o),this},with(o,i){return Xr(a,[...t,[se(o)?o.slot:o,i]])}},n=Bt((...o)=>e(...o),(...o)=>{var i;return{value:new nt(a,o.map(s=>s.value)),dataType:(i=e.shell.returnType)!=null?i:P}}),a=Object.assign(n,r);return Object.defineProperty(a,"label",{get:()=>e.label}),Object.defineProperty(a,"toString",{value(){var i;return`fn:${(i=e.label)!=null?i:"<unnamed>"}[${t.map(Kn).join(", ")}]`}}),Object.defineProperty(a,b,{value:{implementation:e[b].implementation}}),a}var nt=class{constructor(t,r){this._fn=t;this._params=r}get label(){return this._fn.label}"~resolve"(t){return t.resolve(`${t.resolve(this._fn)}(${this._params.map(r=>t.resolve(r)).join(", ")})`)}toString(){var t;return`call:${(t=this.label)!=null?t:"<unnamed>"}`}};function Zr(e){var t;return{argTypes:[De((t=e.in)!=null?t:{})],targets:e.out,returnType:rt(e.out),does(r){return Jn(this,r)}}}function Jn(e,t){let r=ee(e,t),n=e.returnType,a=e.argTypes[0];return typeof t=="string"&&Se(t,n,i=>r.applyExternals(i)),{shell:e,outputType:n,get label(){return r.label},$uses(i){return r.applyExternals(i),this},$name(i){return r.label=i,N(n)&&n.$name(`${i}_Output`),N(a)&&a.$name(`${i}_Input`),this},"~resolve"(i){if(typeof t=="string")return r.resolve(i,"@fragment ");let s=i;if(s.callStack===void 0)throw new Error("Cannot resolve a TGSL function outside of a generation context");try{return s.callStack.push(n),r.resolve(i,"@fragment ")}finally{s.callStack.pop()}},toString(){var i;return`fragmentFn:${(i=this.label)!=null?i:"<unnamed>"}`}}}function en(e){var t,r;return{attributes:[(t=e.in)!=null?t:{}],returnType:rt(e.out),argTypes:[De((r=e.in)!=null?r:{})],does(n){return Hn(this,n)}}}function Hn(e,t){let r=ee(e,t),n=e.returnType,a=e.argTypes[0];return typeof t=="string"&&Se(t,n,o=>r.applyExternals(o)),{shell:e,outputType:n,inputType:a,get label(){return r.label},$uses(o){return r.applyExternals(o),this},$name(o){return r.label=o,N(n)&&n.$name(`${o}_Output`),N(a)&&a.$name(`${o}_Input`),this},"~resolve"(o){if(typeof t=="string")return r.resolve(o,"@vertex ");let i=o;if(i.callStack===void 0)throw new Error("Cannot resolve a TGSL function outside of a generation context");try{return i.callStack.push(n),r.resolve(o,"@vertex ")}finally{i.callStack.pop()}},toString(){var o;return`vertexFn:${(o=this.label)!=null?o:"<unnamed>"}`}}}var te=class{constructor(){p(this,"lastUniqueId",0)}makeUnique(t){let r;return t?(r=t.replaceAll(/\s/g,"_"),r=r.replaceAll(/[^\w\d]/g,"")):r="item",`${r}_${this.lastUniqueId++}`}},re=class{constructor(){p(this,"_usedNames",new Set)}makeUnique(t){if(t===void 0)throw new Error("Unnamed item found when using a strict name registry");let r=0,n=t;for(;this._usedNames.has(n);)r++,n=`${t}_${r}`;return this._usedNames.add(n),n}};function tn(e,t){throw new Error(`Failed to handle ${e} at ${t}`)}function Ve(e){return typeof(e==null?void 0:e.format)=="string"}function rn(e,t){var i,s;let r=[];if(k(e)){if(!Ve(t))throw new Error("Shader expected a single attribute, not a record of attributes to be passed in.");return r.push(t._layout),{usedVertexLayouts:r,bufferDefinitions:[{arrayStride:t._layout.stride,stepMode:t._layout.stepMode,attributes:[{format:t.format,offset:t.offset,shaderLocation:(i=Z(e))!=null?i:0}]}]}}let n=[],a=new WeakMap,o=0;for(let[l,u]of Object.entries(e)){if(et(u))continue;let m=t[l];if(!m)throw new Error(`An attribute by the name of '${l}' was not provided to the shader.`);let f=m._layout,g=a.get(f);g||(r.push(f),g=[],n.push({arrayStride:f.stride,stepMode:f.stepMode,attributes:g}),a.set(f,g)),o=(s=Z(u))!=null?s:o,g.push({format:m.format,offset:m.offset,shaderLocation:o++})}return{usedVertexLayouts:r,bufferDefinitions:n}}var Yn=["bool","f32","f16","i32","u32","vec2f","vec3f","vec4f","vec2h","vec3h","vec4h","vec2i","vec3i","vec4i","vec2u","vec3u","vec4u","mat2x2f","mat3x3f","mat4x4f"];function Qn(e){return Yn.includes(e.type)}function Gt(e,[t,r]){return` ${tt(r)}${t}: ${e.resolve(r)},
|
2
|
-
`}function
|
1
|
+
import{$ as v,a as Er,aa as W,b as Vr,c as Xe,ca as h,d as Ze,e as X,f as Gr,g as Z,h as k,i as Pt,j as $,k as Le,oa as $r,ra as Or,ta as et,ua as tt}from"./chunk-VOFHV4Z6.js";import{a as b,b as j,c as K,d as Rt,e as se,f as P,h as Pr,i as q,j as Lr,k as Qe,l as Cr}from"./chunk-KVOFUY5A.js";import{A as S,B as _e,C,D as E,E as Ke,F as qe,G as Je,I as He,J as _t,K as he,L as I,M as B,N as d,O as Pe,P as vr,Q as Ur,R as Ar,S as Ir,T as Rr,U as Br,V as be,W as Q,X as _,Z as _r,a as A,b as L,c as p,d as Ue,da as Ye,e as ce,f as Ft,g as vt,h as Ut,i as oe,j as Ae,k as Ie,l as Dr,m as z,n as At,o as It,p as ie,q as Fr,r as Bt,s as G,t as Re,u as H,v as Y,w as ue,x as Be,y as ye,z as xe}from"./chunk-DR55IHDD.js";var Nn=["vec2f","vec2h","vec2i","vec2u","vec3f","vec3h","vec3i","vec3u","vec4f","vec4h","vec4i","vec4u","struct"],zn={f:{1:d,2:G,3:ue,4:S},h:{1:Pe,2:Re,3:Be,4:_e},i:{1:B,2:H,3:ye,4:C},u:{1:I,2:Y,3:xe,4:E}},kr={vec2f:G,vec2h:Re,vec2i:H,vec2u:Y,vec3f:ue,vec3h:Be,vec3i:ye,vec3u:xe,vec4f:S,vec4h:_e,vec4i:C,vec4u:E,mat2x2f:Ke,mat3x3f:qe,mat4x4f:Je},Wr={vec2f:d,vec2h:Pe,vec2i:B,vec2u:I,vec3f:d,vec3h:Pe,vec3i:B,vec3u:I,vec4f:d,vec4h:Pe,vec4i:B,vec4u:I,mat2x2f:G,mat3x3f:ue,mat4x4f:S};function Ce(e,t){var o;if(typeof e=="string"||typeof e=="number"||typeof e=="boolean")return P;if(K(e)||j(e)){let i=z();if(!i)throw new Error("Resolution context not found when unwrapping slot or derived");let s=i.unwrap(e);return we(s)}let r=e;for(Cr(r)&&(r=r[b].dataType);Ye(r);)r=r.inner;let n="kind"in r?r.kind:r.type;if(n==="struct")return(o=r.propTypes[t])!=null?o:P;let a=t.length;if(Nn.includes(n)&&a>=1&&a<=4){let i=n[4],s=zn[i][a];if(s)return s}return be(r)?r:P}function Mr(e){if(be(e)){if(Q(e))return e.elementType;if(e.type in Wr)return Wr[e.type]}return P}function we(e){var t,r;if(K(e)||j(e))return we(e.value);if(typeof e=="string")return P;if(typeof e=="number")return(r=(t=Lt(String(e)))==null?void 0:t.dataType)!=null?r:P;if(typeof e=="boolean")return he;if("kind"in e){let n=e.kind;if(n in kr)return kr[n]}return be(e)?e:P}function Lt(e){if(/^0x[0-9a-f]+$/i.test(e))return{value:e,dataType:He};if(/^0b[01]+$/i.test(e))return{value:`${Number.parseInt(e.slice(2),2)}`,dataType:He};if(/^-?(?:\d+\.\d*|\d*\.\d+)$/i.test(e))return{value:e,dataType:_t};if(/^-?\d+(?:\.\d+)?e-?\d+$/i.test(e))return{value:e,dataType:_t};if(/^-?\d+$/i.test(e))return{value:e,dataType:He}}var M={get(e,t){if(t in e)return Reflect.get(e,t);if(t!=="~providing")return t==="toString"||t===Symbol.toStringTag||t===Symbol.toPrimitive?()=>e.toString():new Proxy({"~resolve":r=>`${r.resolve(e)}.${String(t)}`,toString:()=>{var r;return`.value(...).${String(t)}:${(r=e.label)!=null?r:"<unnamed>"}`},[b]:{dataType:Ce(e[b].dataType,String(t))}},M)}};function Ee(e){let t=e;for(;j(t)||K(t)||se(t)||Qe(t);)t=t.value;return t}function zr(e,t){return new Ct(e,t)}var Nr;Nr=b;var Ct=class{constructor(t,r){this.dataType=t;this._value=r;p(this,"_label");p(this,Nr);this[b]={dataType:t}}get label(){return this._label}$name(t){return this._label=t,this}"~resolve"(t){let r=t.names.makeUnique(this._label),n=t.resolveValue(this._value,this.dataType);return t.addDeclaration(`const ${r} = ${n};`),r}toString(){var t;return`const:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){return ie()?new Proxy({"~resolve":t=>t.resolve(this),toString:()=>{var t;return`.value:${(t=this.label)!=null?t:"<unnamed>"}`},[b]:{dataType:this.dataType}},M):this._value}};function N(e){return!!e&&(typeof e=="object"||typeof e=="function")&&"$name"in e}function pe(e,t){for(let[r,n]of Object.entries(t))e[r]=n,N(n)&&(!("label"in n)||n.label===void 0)&&n.$name(r)}function jr(e,t,r){let n=[...e.matchAll(new RegExp(":\\s*(?<arg>.*?)\\s*[,)]","g"))].map(a=>a?a[1]:void 0);r(Object.fromEntries(t.flatMap((a,o)=>{let i=n?n[o]:void 0;return _(a)&&i!==void 0?[[i,a]]:[]})))}function Se(e,t,r){var o;let n=e.match(new RegExp("->(?<output>.*?){","s")),a=n?(o=n[1])==null?void 0:o.trim():void 0;_(t)&&a&&!/\s/g.test(a)&&r({[a]:t})}function jn(e){return new RegExp(`(?<![\\w_.])${e.replaceAll(".","\\.")}(?![\\w_])`,"g")}function le(e,t,r){return Object.entries(t).reduce((n,[a,o])=>{var i;return q(o)||Xe(o)?n.replaceAll(jn(a),e.resolve(o)):o!==null&&typeof o=="object"?((i=[...r.matchAll(new RegExp(`${a.replaceAll(".","\\.")}\\.(?<prop>.*?)(?![\\w_])`,"g"))].map(l=>l[1]))!=null?i:[]).reduce((l,u)=>u&&u in o?le(e,{[`${a}.${u}`]:o[u]},l):l,n):n},r)}function Kr(e){return new Et(e)}var Et=class{constructor(t){this.declaration=t;p(this,"externalsToApply",[])}$uses(t){return this.externalsToApply.push(t),this}"~resolve"(t){let r={};for(let a of this.externalsToApply)pe(r,a);let n=le(t,r,this.declaration);return t.addDeclaration(n),""}toString(){return`declare: ${this.declaration}`}};var qr=new WeakMap;function Jr(e){return qr.get(e)}function Hr(e,t,r){return qr.set(e,{ast:t,externals:r}),e}function ee(e,t){let r=[];return typeof t=="string"&&(jr(t,e.argTypes,n=>r.push(n)),Se(t,e.returnType,n=>r.push(n))),{label:void 0,applyExternals(n){r.push(n)},resolve(n,a=""){var s;let o={};for(let l of r)pe(o,l);let i=n.names.makeUnique(this.label);if(typeof t=="string"){let l=le(n,o,t.trim());n.addDeclaration(`${a}fn ${i}${l}`)}else{let l=Jr(t);if(l!=null&&l.externals){let y=Object.fromEntries(Object.entries(l.externals).filter(([T])=>!(T in o)));pe(o,y)}let u=(s=l==null?void 0:l.ast)!=null?s:n.transpileFn(String(t)),m=u.externalNames.filter(y=>!(y in o));if(m.length>0)throw new Ut(this.label,m);let f=u.argNames.map((y,T)=>({value:y,dataType:e.argTypes[T]})),{head:g,body:w}=n.fnToWgsl({args:f,returnType:e.returnType,body:u.body,externalMap:o});n.addDeclaration(`${a}fn ${i}${n.resolve(g)}${n.resolve(w)}`)}return i}}}function Yr(e){let t=0;return Object.fromEntries(Object.entries(e).map(([r,n])=>{if(et(n))return[r,n];let a=Z(n);return a!==void 0?(t=a+1,[r,n]):[r,$r(n,{type:"@location",value:t++})]}))}function rt(e){return k(e)?Z(e)!==void 0?e:Or(0,e):Pt(Yr(e))}function De(e){return Pt(Yr(e))}function Qr(e){var t,r,n,a;return{argTypes:[De((t=e.in)!=null?t:{})],returnType:void 0,workgroupSize:[(r=e.workgroupSize[0])!=null?r:1,(n=e.workgroupSize[1])!=null?n:1,(a=e.workgroupSize[2])!=null?a:1],does(o){return Kn(this,e.workgroupSize,o)}}}function Kn(e,t,r){let n=ee(e,r),a=e.argTypes[0];return{shell:e,get label(){return n.label},$uses(o){return n.applyExternals(o),this},$name(o){return n.label=o,N(a)&&a.$name(`${o}_Input`),this},"~resolve"(o){return n.resolve(o,`@compute @workgroup_size(${t.join(", ")}) `)},toString(){var o;return`computeFn:${(o=this.label)!=null?o:"<unnamed>"}`}}}function Xr(e,t){return{argTypes:e,returnType:t,does(r){return Jn(this,r)}}}function Vt(e){return(e==null?void 0:e.resourceType)==="function"}function qn([e,t]){var r;return`${(r=e.label)!=null?r:"<unnamed>"}=${t}`}function Jn(e,t){let r=ee(e,t),n={shell:e,resourceType:"function",$uses(i){return r.applyExternals(i),this},$name(i){return r.label=i,this},with(i,s){return Zr(o,[[se(i)?i.slot:i,s]])},"~resolve"(i){return r.resolve(i)}},a=Bt((...i)=>{if(typeof t=="string")throw new Error("Cannot execute on the CPU functions constructed with raw WGSL");return t(...i)},(...i)=>{var s;return{value:new nt(o,i.map(l=>l.value)),dataType:(s=e.returnType)!=null?s:P}});Object.defineProperty(a,b,{value:{implementation:t}});let o=Object.assign(a,n);return Object.defineProperty(o,"label",{get:()=>r.label}),Object.defineProperty(o,"toString",{value:()=>{var i;return`fn:${(i=r.label)!=null?i:"<unnamed>"}`}}),o}function Zr(e,t){let r={resourceType:"function",shell:e.shell,"~providing":{inner:e,pairs:t},$uses(o){return e.$uses(o),this},$name(o){return e.$name(o),this},with(o,i){return Zr(a,[...t,[se(o)?o.slot:o,i]])}},n=Bt((...o)=>e(...o),(...o)=>{var i;return{value:new nt(a,o.map(s=>s.value)),dataType:(i=e.shell.returnType)!=null?i:P}}),a=Object.assign(n,r);return Object.defineProperty(a,"label",{get:()=>e.label}),Object.defineProperty(a,"toString",{value(){var i;return`fn:${(i=e.label)!=null?i:"<unnamed>"}[${t.map(qn).join(", ")}]`}}),Object.defineProperty(a,b,{value:{implementation:e[b].implementation}}),a}var nt=class{constructor(t,r){this._fn=t;this._params=r}get label(){return this._fn.label}"~resolve"(t){return t.resolve(`${t.resolve(this._fn)}(${this._params.map(r=>t.resolve(r)).join(", ")})`)}toString(){var t;return`call:${(t=this.label)!=null?t:"<unnamed>"}`}};function en(e){var t;return{argTypes:[De((t=e.in)!=null?t:{})],targets:e.out,returnType:rt(e.out),does(r){return Hn(this,r)}}}function Hn(e,t){let r=ee(e,t),n=e.returnType,a=e.argTypes[0];return typeof t=="string"&&Se(t,n,i=>r.applyExternals(i)),{shell:e,outputType:n,get label(){return r.label},$uses(i){return r.applyExternals(i),this},$name(i){return r.label=i,N(n)&&n.$name(`${i}_Output`),N(a)&&a.$name(`${i}_Input`),this},"~resolve"(i){if(typeof t=="string")return r.resolve(i,"@fragment ");let s=i;if(s.callStack===void 0)throw new Error("Cannot resolve a TGSL function outside of a generation context");try{return s.callStack.push(n),r.resolve(i,"@fragment ")}finally{s.callStack.pop()}},toString(){var i;return`fragmentFn:${(i=this.label)!=null?i:"<unnamed>"}`}}}function tn(e){var t,r;return{attributes:[(t=e.in)!=null?t:{}],returnType:rt(e.out),argTypes:[De((r=e.in)!=null?r:{})],does(n){return Yn(this,n)}}}function Yn(e,t){let r=ee(e,t),n=e.returnType,a=e.argTypes[0];return typeof t=="string"&&Se(t,n,o=>r.applyExternals(o)),{shell:e,outputType:n,inputType:a,get label(){return r.label},$uses(o){return r.applyExternals(o),this},$name(o){return r.label=o,N(n)&&n.$name(`${o}_Output`),N(a)&&a.$name(`${o}_Input`),this},"~resolve"(o){if(typeof t=="string")return r.resolve(o,"@vertex ");let i=o;if(i.callStack===void 0)throw new Error("Cannot resolve a TGSL function outside of a generation context");try{return i.callStack.push(n),r.resolve(o,"@vertex ")}finally{i.callStack.pop()}},toString(){var o;return`vertexFn:${(o=this.label)!=null?o:"<unnamed>"}`}}}var te=class{constructor(){p(this,"lastUniqueId",0)}makeUnique(t){let r;return t?(r=t.replaceAll(/\s/g,"_"),r=r.replaceAll(/[^\w\d]/g,"")):r="item",`${r}_${this.lastUniqueId++}`}},re=class{constructor(){p(this,"_usedNames",new Set)}makeUnique(t){if(t===void 0)throw new Error("Unnamed item found when using a strict name registry");let r=0,n=t;for(;this._usedNames.has(n);)r++,n=`${t}_${r}`;return this._usedNames.add(n),n}};function rn(e,t){throw new Error(`Failed to handle ${e} at ${t}`)}function Ve(e){return typeof(e==null?void 0:e.format)=="string"}function nn(e,t){var i,s;let r=[];if(k(e)){if(!Ve(t))throw new Error("Shader expected a single attribute, not a record of attributes to be passed in.");return r.push(t._layout),{usedVertexLayouts:r,bufferDefinitions:[{arrayStride:t._layout.stride,stepMode:t._layout.stepMode,attributes:[{format:t.format,offset:t.offset,shaderLocation:(i=Z(e))!=null?i:0}]}]}}let n=[],a=new WeakMap,o=0;for(let[l,u]of Object.entries(e)){if(et(u))continue;let m=t[l];if(!m)throw new Error(`An attribute by the name of '${l}' was not provided to the shader.`);let f=m._layout,g=a.get(f);g||(r.push(f),g=[],n.push({arrayStride:f.stride,stepMode:f.stepMode,attributes:g}),a.set(f,g)),o=(s=Z(u))!=null?s:o,g.push({format:m.format,offset:m.offset,shaderLocation:o++})}return{usedVertexLayouts:r,bufferDefinitions:n}}var Qn=["bool","f32","f16","i32","u32","vec2f","vec3f","vec4f","vec2h","vec3h","vec4h","vec2i","vec3i","vec4i","vec2u","vec3u","vec4u","mat2x2f","mat3x3f","mat4x4f"];function Xn(e){return Qn.includes(e.type)}function Gt(e,[t,r]){return` ${tt(r)}${t}: ${e.resolve(r)},
|
2
|
+
`}function Zn(e,t){let r=e.names.makeUnique(t.label);return e.addDeclaration(`
|
3
3
|
struct ${r} {
|
4
4
|
${Object.entries(t.propTypes).map(n=>Gt(e,n)).join("")}}
|
5
|
-
`),r}function
|
5
|
+
`),r}function ea(e,t){let r=e.names.makeUnique(t.label);return e.addDeclaration(`
|
6
6
|
struct ${r} {
|
7
7
|
${Object.entries(t.propTypes).map(n=>Ve(n[1])?Gt(e,[n[0],Le[n[1].format]]):Gt(e,n)).join("")}
|
8
8
|
}
|
9
|
-
`),r}function
|
9
|
+
`),r}function ta(e,t){let r=e.resolve(t.elementType);return t.elementCount===0?`array<${r}>`:`array<${r}, ${t.elementCount}>`}function ra(e,t){let r=e.resolve(Ve(t.elementType)?Le[t.elementType.format]:t.elementType);return t.elementCount===0?`array<${r}>`:`array<${r}, ${t.elementCount}>`}function $t(e,t){if(Xe(t))return t.type==="unstruct"?ea(e,t):t.type==="disarray"?ra(e,t):t.type==="loose-decorated"?e.resolve(Ve(t.inner)?Le[t.inner.format]:t.inner):e.resolve(Le[t.type]);if(Xn(t))return t.type;if(t.type==="struct")return Zn(e,t);if(t.type==="array")return ta(e,t);if(t.type==="atomic")return`atomic<${$t(e,t.inner)}>`;if(t.type==="decorated")return e.resolve(t.inner);if(t.type==="ptr")return t.addressSpace==="storage"?`ptr<storage, ${e.resolve(t.inner)}, ${t.access==="read-write"?"read_write":t.access}>`:`ptr<${t.addressSpace}, ${e.resolve(t.inner)}>`;if(t.type==="abstractInt"||t.type==="abstractFloat")throw new Error("Abstract types have no concrete representation in WGSL");rn(t,"resolveData")}var aa=["==","!=","<","<=",">",">=","<<",">>","+","-","*","/","%","|","^","&","&&","||"],oa=["&&","||","==","!=","<","<=",">",">="];function an(e,t,r){return r?oa.includes(t)?he:t==="="?r:e:t==="!"||t==="~"?he:e}function U(e,t){return q(t.value)?e.resolve(t.value):String(t.value)}function ia(e){throw new Error(`'${JSON.stringify(e)}' was not handled by the WGSL generator.`)}function on(e,t){return{value:t?"true":"false",dataType:he}}function sn(e,t){e.pushBlockScope();try{return`${e.indent()}{
|
10
10
|
${t.b.map(r=>de(e,r)).join(`
|
11
11
|
`)}
|
12
|
-
${e.dedent()}}`}finally{e.popBlockScope()}}function
|
12
|
+
${e.dedent()}}`}finally{e.popBlockScope()}}function sa(e,t,r){return e.defineVariable(t,r)}function Ot(e,t){let r=e.getById(t);if(!r)throw new Error(`Identifier ${t} not found`);return r}function R(e,t){if(typeof t=="string")return Ot(e,t);if(typeof t=="boolean")return on(e,t);if("x"in t){let[r,n,a]=t.x,o=R(e,r),i=R(e,a),s=U(e,o),l=U(e,i),u=an(o.dataType,n,i.dataType);return{value:aa.includes(n)?`(${s} ${n} ${l})`:`${s} ${n} ${l}`,dataType:u}}if("p"in t){let[r,n]=t.p,a=R(e,n);return{value:`${U(e,a)}${r}`,dataType:a.dataType}}if("u"in t){let[r,n]=t.u,a=R(e,n),o=U(e,a),i=an(a.dataType,r);return{value:`${r}${o}`,dataType:i}}if("a"in t){let[r,n]=t.a,a=R(e,r);if(typeof a.value=="string")return{value:`${a.value}.${n}`,dataType:Ce(a.dataType,n)};let o=a.value[n];if(q(a.value))return{value:o,dataType:Ce(a.value,n)};if(typeof a.value=="object"){let i=q(o)?we(o):P;return{value:o,dataType:i}}throw new Error(`Cannot access member ${n} of ${a.value}`)}if("i"in t){let[r,n]=t.i,a=R(e,r),o=R(e,n),i=U(e,a),s=U(e,o);return{value:`${i}[${s}]`,dataType:Mr(a.dataType)}}if("n"in t){let r=Lt(t.n);if(!r)throw new Error(`Invalid numeric literal ${t.n}`);return r}if("f"in t){let[r,n]=t.f,a=R(e,r),o=a.value;e.callStack.push(o);let s=n.map(u=>R(e,u)).map(u=>({value:U(e,u),dataType:u.dataType})),l=s.map(u=>u.value);return e.callStack.pop(),typeof o=="string"?{value:`${o}(${l.join(", ")})`,dataType:a.dataType}:_(o)?{value:`${e.resolve(o)}(${l.join(", ")})`,dataType:a.dataType}:o(...s)}if("o"in t){let r=t.o,n=e.callStack[e.callStack.length-1],a=o=>o.map(i=>{let s=R(e,i);return U(e,s)}).join(", ");if(_(n)){let i=Object.keys(n.propTypes).map(s=>{let l=r[s];if(l===void 0)throw new Error(`Missing property ${s} in object literal for struct ${n}`);return l});return{value:a(i),dataType:n}}return{value:a(Object.values(r)),dataType:P}}if("s"in t)throw new Error("Cannot use string literals in TGSL.");ia(t)}function de(e,t){if(typeof t=="string")return`${e.pre}${U(e,Ot(e,t))};`;if(typeof t=="boolean")return`${e.pre}${U(e,on(e,t))};`;if("r"in t){if(_(e.callStack[e.callStack.length-1])&&t.r!==null&&typeof t.r=="object"&&"o"in t.r){let r=U(e,R(e,t.r)),n=e.resolve(e.callStack[e.callStack.length-1]);return`${e.pre}return ${n}(${r});`}return t.r===null?`${e.pre}return;`:`${e.pre}return ${U(e,R(e,t.r))};`}if("q"in t){let[r,n,a]=t.q,o=U(e,R(e,r));e.indent();let i=de(e,n);e.dedent(),e.indent();let s=a?de(e,a):void 0;return e.dedent(),s?`${e.pre}if (${o})
|
13
13
|
${i}
|
14
14
|
${e.pre}else
|
15
15
|
${s}`:`${e.pre}if (${o})
|
16
|
-
${i}`}if("l"in t||"c"in t){let[r,n]="l"in t?t.l:t.c,a=n?R(e,n):void 0;if(!a||!n)throw new Error("Cannot create variable without an initial value.");
|
16
|
+
${i}`}if("l"in t||"c"in t){let[r,n]="l"in t?t.l:t.c,a=n?R(e,n):void 0;if(!a||!n)throw new Error("Cannot create variable without an initial value.");sa(e,r,a.dataType);let o=U(e,Ot(e,r));if(typeof n=="object"&&"o"in n&&_(e.callStack[e.callStack.length-1])){let i=e.resolve(e.callStack[e.callStack.length-1]);return`${e.pre}var ${o} = ${i}(${U(e,a)});`}return`${e.pre}var ${o} = ${U(e,a)};`}if("b"in t){e.pushBlockScope();try{return sn(e,t)}finally{e.popBlockScope()}}if("j"in t){let[r,n,a,o]=t.j,i=r?de(e,r):void 0,s=i?i.slice(0,-1):"",l=n?R(e,n):void 0,u=l?U(e,l):"",m=a?de(e,a):void 0,f=m?m.slice(0,-1):"";e.indent();let g=de(e,o);return e.dedent(),`${e.pre}for (${s}; ${u}; ${f})
|
17
17
|
${g}`}if("w"in t){let[r,n]=t.w,a=U(e,R(e,r));e.indent();let o=de(e,n);return e.dedent(),`${e.pre}while (${a})
|
18
|
-
${o}`}return"k"in t?`${e.pre}continue;`:"d"in t?`${e.pre}break;`:`${e.pre}${U(e,R(e,t))};`}function kt(e,t){return
|
18
|
+
${o}`}return"k"in t?`${e.pre}continue;`:"d"in t?`${e.pre}break;`:`${e.pre}${U(e,R(e,t))};`}function kt(e,t){return sn(e,t)}import{BufferReader as Jt,BufferWriter as Ht}from"typed-binary";import{getSystemEndianness as da}from"typed-binary";import{Measurer as pa}from"typed-binary";function ua(e,t){let r="size"in e?e.size:e.currentByteOffset,n=t-1,a=r&n;"skipBytes"in e?e.skipBytes(t-a&n):e.add(t-a&n)}var D=ua;var un=new WeakMap;function at(e){let t=un.get(e);if(t)return t;let r=new pa,n={},a;for(let o in e.propTypes){let i=e.propTypes[o];if(i===void 0)throw new Error(`Property ${o} is undefined in struct`);let s=r.size;D(r,X(e)?W(i):v(i)),a&&(a.padding=r.size-s);let l=h(i);n[o]={offset:r.size,size:l},a=n[o],r.add(l)}return a&&(a.padding=$(h(e),v(e))-r.size),un.set(e,n),n}var ot=(()=>{try{return new Function("return true"),!0}catch(e){return!1}})(),Wt=new WeakMap,Mt={u32:"u32",vec2u:"u32",vec3u:"u32",vec4u:"u32",i32:"i32",vec2i:"i32",vec3i:"i32",vec4i:"i32",f32:"f32",vec2f:"f32",vec3f:"f32",vec4f:"f32",vec2h:"f32",vec3h:"f32",vec4h:"f32",mat2x2f:"f32",mat3x3f:"f32",mat4x4f:"f32"},Nt={u32:"setUint32",i32:"setInt32",f32:"setFloat32"};function zt(e,t,r){if(_(e)||X(e)){let a=at(e),o=Object.entries(a).sort((s,l)=>s[1].offset-l[1].offset),i="";for(let[s,l]of o){let u=e.propTypes[s];u&&(i+=zt(u,`(${t} + ${l.offset})`,`${r}.${s}`))}return i}if(Q(e)||Ze(e)){let a=e,o=$(h(a.elementType),v(a.elementType)),i="";return i+=`for (let i = 0; i < ${a.elementCount}; i++) {
|
19
19
|
`,i+=zt(a.elementType,`(${t} + i * ${o})`,`${r}[i]`),i+=`}
|
20
20
|
`,i}if(Ar(e)){let a=Mt[e.type],o="",i=Nt[a],s=["x","y","z","w"],l=vr(e)?2:Ur(e)?3:4;for(let u=0;u<l;u++)o+=`output.${i}((${t} + ${u*4}), ${r}.${s[u]}, littleEndian);
|
21
21
|
`;return o}if(Br(e)){let a=Mt[e.type],o=Nt[a],i=Ir(e)?2:Rr(e)?3:4,s=i*i,l=$(i*4,8),u="";for(let m=0;m<s;m++){let f=Math.floor(m/i),g=m%i,w=f*l+g*4;u+=`output.${o}((${t} + ${w}), ${r}.columns[${f}].${["x","y","z","w"][g]}, littleEndian);
|
22
|
-
`}return u}let n=Mt[e.type];return`output.${Nt[n]}(${t}, ${r}, littleEndian);
|
23
|
-
`}function it(e){if(Wt.has(e))return Wt.get(e);let t=zt(e,"offset","value"),r=new Function("output","offset","value","littleEndian=true",t);return Wt.set(e,r),r}var V={bool(e,t,r){e.writeBool(r)},f32(e,t,r){e.writeFloat32(r)},f16(e,t,r){e.writeFloat16(r)},i32(e,t,r){e.writeInt32(r)},u32(e,t,r){e.writeUint32(r)},vec2f(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y)},vec2h(e,t,r){e.writeFloat16(r.x),e.writeFloat16(r.y)},vec2i(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y)},vec2u(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y)},vec3f(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y),e.writeFloat32(r.z)},vec3h(e,t,r){e.writeFloat16(r.x),e.writeFloat16(r.y),e.writeFloat16(r.z)},vec3i(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y),e.writeInt32(r.z)},vec3u(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y),e.writeUint32(r.z)},vec4f(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y),e.writeFloat32(r.z),e.writeFloat32(r.w)},vec4h(e,t,r){e.writeFloat16(r.x),e.writeFloat16(r.y),e.writeFloat16(r.z),e.writeFloat16(r.w)},vec4i(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y),e.writeInt32(r.z),e.writeInt32(r.w)},vec4u(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y),e.writeUint32(r.z),e.writeUint32(r.w)},mat2x2f(e,t,r){for(let n=0;n<r.length;++n)e.writeFloat32(r[n])},mat3x3f(e,t,r){for(let n=0;n<r.length;++n)e.writeFloat32(r[n])},mat4x4f(e,t,r){for(let n=0;n<r.length;++n)e.writeFloat32(r[n])},struct(e,t,r){let n=v(t);D(e,n);for(let[a,o]of Object.entries(t.propTypes))D(e,v(o)),ne(e,o,r[a]);D(e,n)},array(e,t,r){if(t.elementCount===0)throw new Error("Cannot write using a runtime-sized schema.");let n=v(t);D(e,n);let a=e.currentByteOffset;for(let o=0;o<Math.min(t.elementCount,r.length);o++)D(e,n),ne(e,t.elementType,r[o]);e.seekTo(a+h(t))},ptr(){throw new Error("Pointers are not host-shareable")},atomic(e,t,r){var n;(n=V[t.inner.type])==null||n.call(V,e,t,r)},decorated(e,t,r){var o,i;let n=W(t);D(e,n);let a=e.currentByteOffset;(i=V[(o=t.inner)==null?void 0:o.type])==null||i.call(V,e,t.inner,r),e.seekTo(a+h(t))},uint8(e,t,r){e.writeUint8(r)},uint8x2(e,t,r){e.writeUint8(r.x),e.writeUint8(r.y)},uint8x4(e,t,r){e.writeUint8(r.x),e.writeUint8(r.y),e.writeUint8(r.z),e.writeUint8(r.w)},sint8(e,t,r){e.writeInt8(r)},sint8x2(e,t,r){e.writeInt8(r.x),e.writeInt8(r.y)},sint8x4(e,t,r){e.writeInt8(r.x),e.writeInt8(r.y),e.writeInt8(r.z),e.writeInt8(r.w)},unorm8(e,t,r){e.writeUint8(r*255)},unorm8x2(e,t,r){e.writeUint8(r.x*255),e.writeUint8(r.y*255)},unorm8x4(e,t,r){e.writeUint8(r.x*255),e.writeUint8(r.y*255),e.writeUint8(r.z*255),e.writeUint8(r.w*255)},snorm8(e,t,r){e.writeUint8(r*127+128)},snorm8x2(e,t,r){e.writeUint8(r.x*127+128),e.writeUint8(r.y*127+128)},snorm8x4(e,t,r){e.writeUint8(r.x*127+128),e.writeUint8(r.y*127+128),e.writeUint8(r.z*127+128),e.writeUint8(r.w*127+128)},uint16(e,t,r){e.writeUint16(r)},uint16x2(e,t,r){e.writeUint16(r.x),e.writeUint16(r.y)},uint16x4(e,t,r){e.writeUint16(r.x),e.writeUint16(r.y),e.writeUint16(r.z),e.writeUint16(r.w)},sint16(e,t,r){e.writeInt16(r)},sint16x2(e,t,r){e.writeInt16(r.x),e.writeInt16(r.y)},sint16x4(e,t,r){e.writeInt16(r.x),e.writeInt16(r.y),e.writeInt16(r.z),e.writeInt16(r.w)},unorm16(e,t,r){e.writeUint16(r*65535)},unorm16x2(e,t,r){e.writeUint16(r.x*65535),e.writeUint16(r.y*65535)},unorm16x4(e,t,r){e.writeUint16(r.x*65535),e.writeUint16(r.y*65535),e.writeUint16(r.z*65535),e.writeUint16(r.w*65535)},snorm16(e,t,r){e.writeUint16(r*32767+32768)},snorm16x2(e,t,r){e.writeUint16(r.x*32767+32768),e.writeUint16(r.y*32767+32768)},snorm16x4(e,t,r){e.writeUint16(r.x*32767+32768),e.writeUint16(r.y*32767+32768),e.writeUint16(r.z*32767+32768),e.writeUint16(r.w*32767+32768)},float16(e,t,r){e.writeFloat16(r)},float16x2(e,t,r){e.writeFloat16(r.x),e.writeFloat16(r.y)},float16x4(e,t,r){e.writeFloat16(r.x),e.writeFloat16(r.y),e.writeFloat16(r.z),e.writeFloat16(r.w)},float32(e,t,r){e.writeFloat32(r)},float32x2(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y)},float32x3(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y),e.writeFloat32(r.z)},float32x4(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y),e.writeFloat32(r.z),e.writeFloat32(r.w)},uint32(e,t,r){e.writeUint32(r)},uint32x2(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y)},uint32x3(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y),e.writeUint32(r.z)},uint32x4(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y),e.writeUint32(r.z),e.writeUint32(r.w)},sint32(e,t,r){e.writeInt32(r)},sint32x2(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y)},sint32x3(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y),e.writeInt32(r.z)},sint32x4(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y),e.writeInt32(r.z),e.writeInt32(r.w)},"unorm10-10-10-2"(e,t,r){let n=0;n|=(r.x*1023&1023)<<22,n|=(r.x*1023&1023)<<12,n|=(r.y*1023&1023)<<2,n|=r.z*3&3,e.writeUint32(n)},"unorm8x4-bgra"(e,t,r){e.writeUint8(r.z*255),e.writeUint8(r.y*255),e.writeUint8(r.x*255),e.writeUint8(r.w*255)},disarray(e,t,r){var o,i;let n=v(t);D(e,n);let a=e.currentByteOffset;for(let s=0;s<Math.min(t.elementCount,r.length);s++)D(e,n),(i=V[(o=t.elementType)==null?void 0:o.type])==null||i.call(V,e,t.elementType,r[s]);e.seekTo(a+h(t))},unstruct(e,t,r){var n;for(let[a,o]of Object.entries(t.propTypes))(n=V[o.type])==null||n.call(V,e,o,r[a])},"loose-decorated"(e,t,r){var i;let n=W(t);D(e,n);let a=e.currentByteOffset,o=V[(i=t.inner)==null?void 0:i.type];return o==null||o(e,t.inner,r),e.seekTo(a+h(t)),r}};function ne(e,t,r){let n=V[t.type];if(!n)throw new Error(`Cannot write data of type '${t.type}'.`);n(e,t,r)}var me={bool(e){return e.readBool()},f32(e){return e.readFloat32()},f16(e){return e.readFloat16()},i32(e){return e.readInt32()},u32(e){return e.readUint32()},vec2f(e){return G(e.readFloat32(),e.readFloat32())},vec3f(e){return ue(e.readFloat32(),e.readFloat32(),e.readFloat32())},vec4f(e){return S(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32())},vec2h(e){return Re(e.readFloat16(),e.readFloat16())},vec3h(e){return Be(e.readFloat16(),e.readFloat16(),e.readFloat16())},vec4h(e){return _e(e.readFloat16(),e.readFloat16(),e.readFloat16(),e.readFloat16())},vec2i(e){return H(e.readInt32(),e.readInt32())},vec3i(e){return ye(e.readInt32(),e.readInt32(),e.readInt32())},vec4i(e){return C(e.readInt32(),e.readInt32(),e.readInt32(),e.readInt32())},vec2u(e){return Y(e.readUint32(),e.readUint32())},vec3u(e){return xe(e.readUint32(),e.readUint32(),e.readUint32())},vec4u(e){return E(e.readUint32(),e.readUint32(),e.readUint32(),e.readUint32())},mat2x2f(e){return Ke(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32())},mat3x3f(e){let t=()=>{let r=e.readFloat32();return e.readFloat32(),r};return qe(e.readFloat32(),e.readFloat32(),t(),e.readFloat32(),e.readFloat32(),t(),e.readFloat32(),e.readFloat32(),t())},mat4x4f(e){return Je(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32())},struct(e,t){let r=v(t);D(e,r);let n={};for(let[a,o]of Object.entries(t.propTypes))D(e,v(o)),n[a]=O(e,o);return D(e,r),n},array(e,t){if(t.elementCount===0)throw new Error("Cannot read using a runtime-sized schema.");let r=v(t),n=[];for(let a=0;a<t.elementCount;a++){D(e,r);let o=t.elementType,i=O(e,o);n.push(i)}return D(e,r),n},ptr(){throw new Error("Pointers are not host-shareable")},atomic(e,t){return O(e,t.inner)},decorated(e,t){let r=W(t);D(e,r);let n=e.currentByteOffset,a=O(e,t.inner);return e.seekTo(n+h(t)),a},uint8:e=>e.readUint8(),uint8x2:e=>Y(e.readUint8(),e.readUint8()),uint8x4:e=>E(e.readUint8(),e.readUint8(),e.readUint8(),e.readUint8()),sint8:e=>e.readInt8(),sint8x2:e=>H(e.readInt8(),e.readInt8()),sint8x4:e=>C(e.readInt8(),e.readInt8(),e.readInt8(),e.readInt8()),unorm8:e=>e.readUint8()/255,unorm8x2:e=>G(e.readUint8()/255,e.readUint8()/255),unorm8x4:e=>S(e.readUint8()/255,e.readUint8()/255,e.readUint8()/255,e.readUint8()/255),snorm8:e=>(e.readUint8()-128)/127,snorm8x2:e=>G((e.readUint8()-128)/127,(e.readUint8()-128)/127),snorm8x4:e=>S((e.readUint8()-128)/127,(e.readUint8()-128)/127,(e.readUint8()-128)/127,(e.readUint8()-128)/127),uint16:e=>e.readUint16(),uint16x2:e=>Y(e.readUint16(),e.readUint16()),uint16x4:e=>E(e.readUint16(),e.readUint16(),e.readUint16(),e.readUint16()),sint16:e=>e.readInt16(),sint16x2:e=>H(e.readInt16(),e.readInt16()),sint16x4:e=>C(e.readInt16(),e.readInt16(),e.readInt16(),e.readInt16()),unorm16:e=>e.readUint16()/65535,unorm16x2:e=>G(e.readUint16()/65535,e.readUint16()/65535),unorm16x4:e=>S(e.readUint16()/65535,e.readUint16()/65535,e.readUint16()/65535,e.readUint16()/65535),snorm16:e=>(e.readUint16()-32768)/32767,snorm16x2:e=>G(me.snorm16(e),me.snorm16(e)),snorm16x4:e=>S(me.snorm16(e),me.snorm16(e),me.snorm16(e),me.snorm16(e)),float16(e){return e.readFloat16()},float16x2:e=>G(e.readFloat16(),e.readFloat16()),float16x4:e=>S(e.readFloat16(),e.readFloat16(),e.readFloat16(),e.readFloat16()),float32:e=>e.readFloat32(),float32x2:e=>G(e.readFloat32(),e.readFloat32()),float32x3:e=>ue(e.readFloat32(),e.readFloat32(),e.readFloat32()),float32x4:e=>S(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32()),uint32:e=>e.readUint32(),uint32x2:e=>Y(e.readUint32(),e.readUint32()),uint32x3:e=>xe(e.readUint32(),e.readUint32(),e.readUint32()),uint32x4:e=>E(e.readUint32(),e.readUint32(),e.readUint32(),e.readUint32()),sint32:e=>e.readInt32(),sint32x2:e=>H(e.readInt32(),e.readInt32()),sint32x3:e=>ye(e.readInt32(),e.readInt32(),e.readInt32()),sint32x4:e=>C(e.readInt32(),e.readInt32(),e.readInt32(),e.readInt32()),"unorm10-10-10-2"(e){let t=e.readUint32(),r=(t>>22)/1023,n=(t>>12&1023)/1023,a=(t>>2&1023)/1023,o=(t&3)/3;return S(r,n,a,o)},"unorm8x4-bgra"(e){let t=e.readByte()/255,r=e.readByte()/255,n=e.readByte()/255,a=e.readByte()/255;return S(n,r,t,a)},unstruct(e,t){let r={};for(let[n,a]of Object.entries(t.propTypes))r[n]=O(e,a);return r},disarray(e,t){let r=v(t),n=[];for(let a=0;a<t.elementCount;a++)D(e,r),n.push(O(e,t.elementType));return D(e,r),n},"loose-decorated"(e,t){D(e,W(t));let r=e.currentByteOffset,n=O(e,t.inner);return e.seekTo(r+h(t)),n}};function O(e,t){let r=me[t.type];if(!r)throw new Error(`Cannot read data of type '${t.type}'.`);return r(e,t)}import{BufferWriter as pa}from"typed-binary";function un(e,t){var u;let r=h(e);if(r===0||t===void 0||t===null)return[];let n=new ArrayBuffer(r),a=new pa(n),o=[];function i(m,f,g,w){var y;if(f!=null){if(_(m)||X(m)){let T=at(m);for(let[F,c]of Object.entries(T)){let x=m.propTypes[F];if(!x)continue;let Te=f[F];Te!==void 0&&i(x,Te,g+c.offset,(y=c.padding)!=null?y:w)}return}if(Q(m)||Ze(m)){let T=m,F=$(h(T.elementType),v(T.elementType));if(!Array.isArray(f))throw new Error("Partial value for array must be an array");let c=f;c.sort((x,Te)=>x.idx-Te.idx);for(let{idx:x,value:Te}of c)i(T.elementType,Te,g+x*F,F-h(T.elementType))}else{let T=h(m);a.seekTo(g),ne(a,m,f),o.push({start:g,end:g+T,padding:w})}}}if(i(e,t,0),o.length===0)return[];let s=[],l=o[0];for(let m=1;m<o.length;m++){let f=o[m];if(!f||!l)throw new Error("Internal error: missing segment");f.start===l.end+((u=l.padding)!=null?u:0)?(l.end=f.end,l.padding=f.padding):(s.push({data:new Uint8Array(n,l.start,l.end-l.start)}),l=f)}if(!l)throw new Error("Internal error: missing segment");return s.push({data:new Uint8Array(n,l.start,l.end-l.start)}),s}function fe(e){return!!(e!=null&&e.usableAsStorage)}var Ge=class e extends Error{constructor(t){var r;super(`Resource '${(r=t.label)!=null?r:"<unnamed>"}' cannot be bound as 'storage'. Use .$usage('storage') to allow it.`),Object.setPrototypeOf(this,e.prototype)}};function st(e){return!!e.usableAsUniform}var gn={uniform:"uniform",mutable:"storage, read_write",readonly:"storage, read"},mn;mn=b;var $e=class{constructor(t,r){this.usage=t;this.buffer=r;p(this,"~repr");p(this,"resourceType","buffer-usage");p(this,mn);this[b]={dataType:r.dataType}}get label(){return this.buffer.label}$name(t){return this.buffer.$name(t),this}"~resolve"(t){let r=t.names.makeUnique(this.label),{group:n,binding:a}=t.allocateFixedEntry(this.usage==="uniform"?{uniform:this.buffer.dataType}:{storage:this.buffer.dataType,access:this.usage},this.buffer),o=gn[this.usage];return t.addDeclaration(`@group(${n}) @binding(${a}) var<${o}> ${r}: ${t.resolve(this.buffer.dataType)};`),r}write(t){this.buffer.write(t)}toString(){var t;return`${this.usage}:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){if(!ie())throw new Error("Cannot access buffer's value directly in JS.");return new Proxy({"~resolve":t=>t.resolve(this),toString:()=>{var t;return`.value:${(t=this.label)!=null?t:"<unnamed>"}`},[b]:{dataType:this.buffer.dataType}},M)}},fn;fn=b;var Oe=class{constructor(t,r,n){this.usage=t;this.dataType=r;this._membership=n;p(this,"~repr");p(this,"resourceType","buffer-usage");p(this,fn);this[b]={dataType:r}}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout),a=gn[this.usage];return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var<${a}> ${r}: ${t.resolve(this.dataType)};`),r}toString(){var t;return`${this.usage}:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){if(!ie())throw new Error("Cannot access buffer's value directly in JS.");return new Proxy({"~resolve":t=>t.resolve(this),toString:()=>{var t;return`.value:${(t=this.label)!=null?t:"<unnamed>"}`},[b]:{dataType:this.dataType}},M)}},pn=new WeakMap;function jt(e){if(!fe(e))throw new Error(`Cannot pass ${e} to asMutable, as it is not allowed to be used as storage. To allow it, call .$usage('storage') when creating the buffer.`);let t=pn.get(e);return t||(t=new $e("mutable",e),pn.set(e,t)),t}var ln=new WeakMap;function Kt(e){if(!fe(e))throw new Error(`Cannot pass ${e} to asReadonly, as it is not allowed to be used as storage. To allow it, call .$usage('storage') when creating the buffer.`);let t=ln.get(e);return t||(t=new $e("readonly",e),ln.set(e,t)),t}var dn=new WeakMap;function qt(e){if(!st(e))throw new Error(`Cannot pass ${e} to asUniform, as it is not allowed to be used as a uniform. To allow it, call .$usage('uniform') when creating the buffer.`);let t=dn.get(e);return t||(t=new $e("uniform",e),dn.set(e,t)),t}var Yt={uniform:qt,mutable:jt,readonly:Kt};function cn(e,t,r){return be(t)?new ut(e,t,r):new ut(e,t,r,["storage","uniform"])}function ae(e){return e.resourceType==="buffer"}function da(e){return!!e.usableAsVertex}var Tn=la(),ut=class{constructor(t,r,n,a){this._group=t;this.dataType=r;this.initialOrBuffer=n;this._disallowedUsages=a;p(this,"resourceType","buffer");p(this,"flags",GPUBufferUsage.COPY_DST|GPUBufferUsage.COPY_SRC);p(this,"_buffer",null);p(this,"_ownBuffer");p(this,"_destroyed",!1);p(this,"_hostBuffer");p(this,"_label");p(this,"initial");p(this,"usableAsUniform",!1);p(this,"usableAsStorage",!1);p(this,"usableAsVertex",!1);Pr(n)?(this._ownBuffer=!1,this._buffer=n):(this._ownBuffer=!0,this.initial=n)}get label(){return this._label}get buffer(){var r;let t=this._group.device;if(this._destroyed)throw new Error("This buffer has been destroyed");if(!this._buffer&&(this._buffer=t.createBuffer({size:h(this.dataType),usage:this.flags,mappedAtCreation:!!this.initial,label:(r=this.label)!=null?r:"<unnamed>"}),this.initial)){let n=new Ht(this._buffer.getMappedRange());ne(n,this.dataType,this.initial),this._buffer.unmap()}return this._buffer}get destroyed(){return this._destroyed}$name(t){return this._label=t,this._buffer&&(this._buffer.label=t),this}$usage(...t){var r;for(let n of t){if((r=this._disallowedUsages)!=null&&r.includes(n))throw new Error(`Buffer of type ${this.dataType.type} cannot be used as ${n}`);this.flags|=n==="uniform"?GPUBufferUsage.UNIFORM:0,this.flags|=n==="storage"?GPUBufferUsage.STORAGE:0,this.flags|=n==="vertex"?GPUBufferUsage.VERTEX:0,this.usableAsUniform=this.usableAsUniform||n==="uniform",this.usableAsStorage=this.usableAsStorage||n==="storage",this.usableAsVertex=this.usableAsVertex||n==="vertex"}return this}$addFlags(t){if(!this._ownBuffer)throw new Error("Cannot add flags to a buffer that is not managed by TypeGPU.");return t&GPUBufferUsage.MAP_READ?(this.flags=GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ,this):t&GPUBufferUsage.MAP_WRITE?(this.flags=GPUBufferUsage.COPY_SRC|GPUBufferUsage.MAP_WRITE,this):(this.flags|=t,this)}compileWriter(){if(ot)it(this.dataType);else throw new Error("This environment does not allow eval")}write(t){let r=this.buffer,n=this._group.device;if(r.mapState==="mapped"){let o=r.getMappedRange();if(ot){it(this.dataType)(new DataView(o),0,t,Tn==="little");return}ne(new Ht(o),this.dataType,t);return}let a=h(this.dataType);this._hostBuffer||(this._hostBuffer=new ArrayBuffer(a)),this._group.flush(),ot?it(this.dataType)(new DataView(this._hostBuffer),0,t,Tn==="little"):ne(new Ht(this._hostBuffer),this.dataType,t),n.queue.writeBuffer(r,0,this._hostBuffer,0,a)}writePartial(t){let r=this.buffer,n=this._group.device,a=un(this.dataType,t);if(r.mapState==="mapped"){let o=r.getMappedRange(),i=new Uint8Array(o);for(let s of a)i.set(s.data,s.data.byteOffset)}else for(let o of a)n.queue.writeBuffer(r,o.data.byteOffset,o.data,0,o.data.byteLength)}copyFrom(t){if(this.buffer.mapState==="mapped")throw new Error("Cannot copy to a mapped buffer.");let r=h(this.dataType);this._group.commandEncoder.copyBufferToBuffer(t.buffer,0,this.buffer,0,r)}async read(){this._group.flush();let t=this.buffer,r=this._group.device;if(t.mapState==="mapped"){let i=t.getMappedRange();return O(new Jt(i),this.dataType)}if(t.usage&GPUBufferUsage.MAP_READ){await t.mapAsync(GPUMapMode.READ);let i=t.getMappedRange(),s=O(new Jt(i),this.dataType);return t.unmap(),s}let n=r.createBuffer({size:h(this.dataType),usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ}),a=r.createCommandEncoder();a.copyBufferToBuffer(t,0,n,0,h(this.dataType)),r.queue.submit([a.finish()]),await r.queue.onSubmittedWorkDone(),await n.mapAsync(GPUMapMode.READ,0,h(this.dataType));let o=O(new Jt(n.getMappedRange()),this.dataType);return n.unmap(),n.destroy(),o}as(t){var r;return(r=Yt[t])==null?void 0:r.call(Yt,this)}destroy(){var t;this._destroyed||(this._destroyed=!0,this._ownBuffer&&((t=this._buffer)==null||t.destroy()))}toString(){var t;return`buffer:${(t=this._label)!=null?t:"<unnamed>"}`}};function yn(e){return new Qt(e)}function xn(e){return new Xt(e)}function ke(e){return(e==null?void 0:e.resourceType)==="sampler"}function We(e){return(e==null?void 0:e.resourceType)==="sampler-comparison"}var pt=class{constructor(t){this._membership=t;p(this,"resourceType","sampler")}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout);return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: sampler;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},lt=class{constructor(t){this._membership=t;p(this,"resourceType","sampler-comparison")}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout);return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: sampler_comparison;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},Qt=class{constructor(t){this._props=t;p(this,"resourceType","sampler");p(this,"_label");p(this,"_filtering");p(this,"_sampler",null);this._filtering=t.minFilter==="linear"||t.magFilter==="linear"||t.mipmapFilter==="linear"}unwrap(t){var r;return this._sampler||(this._sampler=t.device.createSampler(L(A({},this._props),{label:(r=this._label)!=null?r:"<unnamed>"}))),this._sampler}get label(){return this._label}$name(t){return this._label=t,this}"~resolve"(t){let r=t.names.makeUnique(this._label),{group:n,binding:a}=t.allocateFixedEntry({sampler:this._filtering?"filtering":"non-filtering"},this);return t.addDeclaration(`@group(${n}) @binding(${a}) var ${r}: sampler;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},Xt=class{constructor(t){this._props=t;p(this,"resourceType","sampler-comparison");p(this,"_label");p(this,"_sampler",null)}unwrap(t){var r;return this._sampler||(this._sampler=t.device.createSampler(L(A({},this._props),{label:(r=this._label)!=null?r:"<unnamed>"}))),this._sampler}get label(){return this._label}$name(t){return this._label=t,this}"~resolve"(t){let r=t.names.makeUnique(this.label),{group:n,binding:a}=t.allocateFixedEntry({sampler:"comparison"},this);return t.addDeclaration(`@group(${n}) @binding(${a}) var ${r}: sampler_comparison;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}};var dt=class{constructor(t){this._membership=t;p(this,"resourceType","external-texture")}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout);return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: texture_external;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}};var hn={r8unorm:d,r8snorm:d,r8uint:I,r8sint:B,r16uint:I,r16sint:B,r16float:d,rg8unorm:d,rg8snorm:d,rg8uint:I,rg8sint:B,r32uint:I,r32sint:B,r32float:d,rg16uint:I,rg16sint:B,rg16float:d,rgba8unorm:d,"rgba8unorm-srgb":d,rgba8snorm:d,rgba8uint:I,rgba8sint:B,bgra8unorm:d,"bgra8unorm-srgb":d,rgb9e5ufloat:d,rgb10a2uint:I,rgb10a2unorm:d,rg11b10ufloat:d,rg32uint:I,rg32sint:B,rg32float:d,rgba16uint:I,rgba16sint:B,rgba16float:d,rgba32uint:I,rgba32sint:B,rgba32float:d,stencil8:d,depth16unorm:d,depth24plus:d,"depth24plus-stencil8":d,depth32float:d,"depth32float-stencil8":d,"bc1-rgba-unorm":d,"bc1-rgba-unorm-srgb":d,"bc2-rgba-unorm":d,"bc2-rgba-unorm-srgb":d,"bc3-rgba-unorm":d,"bc3-rgba-unorm-srgb":d,"bc4-r-unorm":d,"bc4-r-snorm":d,"bc5-rg-unorm":d,"bc5-rg-snorm":d,"bc6h-rgb-ufloat":d,"bc6h-rgb-float":d,"bc7-rgba-unorm":d,"bc7-rgba-unorm-srgb":d,"etc2-rgb8unorm":d,"etc2-rgb8unorm-srgb":d,"etc2-rgb8a1unorm":d,"etc2-rgb8a1unorm-srgb":d,"etc2-rgba8unorm":d,"etc2-rgba8unorm-srgb":d,"eac-r11unorm":d,"eac-r11snorm":d,"eac-rg11unorm":d,"eac-rg11snorm":d,"astc-4x4-unorm":d,"astc-4x4-unorm-srgb":d,"astc-5x4-unorm":d,"astc-5x4-unorm-srgb":d,"astc-5x5-unorm":d,"astc-5x5-unorm-srgb":d,"astc-6x5-unorm":d,"astc-6x5-unorm-srgb":d,"astc-6x6-unorm":d,"astc-6x6-unorm-srgb":d,"astc-8x5-unorm":d,"astc-8x5-unorm-srgb":d,"astc-8x6-unorm":d,"astc-8x6-unorm-srgb":d,"astc-8x8-unorm":d,"astc-8x8-unorm-srgb":d,"astc-10x5-unorm":d,"astc-10x5-unorm-srgb":d,"astc-10x6-unorm":d,"astc-10x6-unorm-srgb":d,"astc-10x8-unorm":d,"astc-10x8-unorm-srgb":d,"astc-10x10-unorm":d,"astc-10x10-unorm-srgb":d,"astc-12x10-unorm":d,"astc-12x10-unorm-srgb":d,"astc-12x12-unorm":d,"astc-12x12-unorm-srgb":d},Me={rgba8unorm:S,rgba8snorm:S,rgba8uint:E,rgba8sint:C,rgba16uint:E,rgba16sint:C,rgba16float:S,r32uint:E,r32sint:C,r32float:S,rg32uint:E,rg32sint:C,rg32float:S,rgba32uint:E,rgba32sint:C,rgba32float:S,bgra8unorm:S},bn={f32:"float",u32:"uint",i32:"sint"},wn={float:d,"unfilterable-float":d,uint:I,sint:B,depth:d};function Sn(e,t){return new Zt(e,t)}function J(e){return(e==null?void 0:e.resourceType)==="texture"}function Ne(e){return(e==null?void 0:e.resourceType)==="texture-storage-view"}function ze(e){return(e==null?void 0:e.resourceType)==="texture-sampled-view"}var Dn={mutable:"read_write",readonly:"read",writeonly:"write"},Zt=class{constructor(t,r){this.props=t;this._branch=r;p(this,"resourceType","texture");p(this,"usableAsSampled",!1);p(this,"usableAsStorage",!1);p(this,"usableAsRender",!1);p(this,"_destroyed",!1);p(this,"_label");p(this,"_flags",GPUTextureUsage.COPY_DST|GPUTextureUsage.COPY_SRC);p(this,"_texture",null)}get label(){return this._label}$name(t){return this._label=t,this}unwrap(){var t,r,n,a,o;if(this._destroyed)throw new Error("This texture has been destroyed");return this._texture||(this._texture=this._branch.device.createTexture({label:(t=this._label)!=null?t:"<unnamed>",format:this.props.format,size:this.props.size,usage:this._flags,dimension:(r=this.props.dimension)!=null?r:"2d",viewFormats:(n=this.props.viewFormats)!=null?n:[],mipLevelCount:(a=this.props.mipLevelCount)!=null?a:1,sampleCount:(o=this.props.sampleCount)!=null?o:1})),this._texture}$usage(...t){let r=t.includes("storage"),n=t.includes("sampled"),a=t.includes("render");return this._flags|=n?GPUTextureUsage.TEXTURE_BINDING:0,this._flags|=r?GPUTextureUsage.STORAGE_BINDING:0,this._flags|=a?GPUTextureUsage.RENDER_ATTACHMENT:0,this.usableAsStorage||(this.usableAsStorage=r),this.usableAsSampled||(this.usableAsSampled=n),this.usableAsRender||(this.usableAsRender=a),this}createView(t,r){if(t==="sampled")return this._asSampled(r);let n=r;switch(t){case"mutable":return this._asMutable(n);case"readonly":return this._asReadonly(n);case"writeonly":return this._asWriteonly(n)}}_asStorage(t,r){var o;if(!this.usableAsStorage)throw new Error("Unusable as storage");let n=(o=t==null?void 0:t.format)!=null?o:this.props.format,a=Me[n];return Ue(!!a,`Unsupported storage texture format: ${n}`),new er(t!=null?t:{},r,this)}_asReadonly(t){return this._asStorage(t,"readonly")}_asWriteonly(t){return this._asStorage(t,"writeonly")}_asMutable(t){return this._asStorage(t,"mutable")}_asSampled(t){var a;if(!this.usableAsSampled)throw new Error("Unusable as sampled");let r=(a=t==null?void 0:t.format)!=null?a:this.props.format;if(!Me[r])throw new Error(`Unsupported storage texture format: ${r}`);return new tr(t,this)}destroy(){var t;this._destroyed||(this._destroyed=!0,(t=this._texture)==null||t.destroy())}},gt={"1d":"1d","2d":"2d","2d-array":"2d_array",cube:"cube","cube-array":"cube_array","3d":"3d"},er=class{constructor(t,r,n){this.access=r;this._texture=n;p(this,"resourceType","texture-storage-view");p(this,"texelDataType");p(this,"dimension");p(this,"_view");p(this,"_format");var a,o,i;this.dimension=(o=(a=t==null?void 0:t.dimension)!=null?a:n.props.dimension)!=null?o:"2d",this._format=(i=t==null?void 0:t.format)!=null?i:n.props.format,this.texelDataType=Me[this._format]}get label(){return this._texture.label}$name(t){return this._texture.$name(t),this}unwrap(){var t;return this._view||(this._view=this._texture.unwrap().createView({label:`${(t=this.label)!=null?t:"<unnamed>"} - View`,format:this._format,dimension:this.dimension})),this._view}"~resolve"(t){let r=t.names.makeUnique(this.label),{group:n,binding:a}=t.allocateFixedEntry({storageTexture:this._format,access:this.access,viewDimension:this.dimension},this),o=`texture_storage_${gt[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${a}) var ${r}: ${o}<${this._format}, ${Dn[this.access]}>;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},mt=class{constructor(t,r,n,a){this._format=t;this.dimension=r;this.access=n;this._membership=a;p(this,"resourceType","texture-storage-view");p(this,"texelDataType");this.texelDataType=Me[this._format]}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout),a=`texture_storage_${gt[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: ${a}<${this._format}, ${Dn[this.access]}>;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},tr=class{constructor(t,r){this._props=t;this._texture=r;p(this,"resourceType","texture-sampled-view");p(this,"channelDataType");p(this,"dimension");p(this,"_format");p(this,"_view");var n,a,o;this.dimension=(a=(n=t==null?void 0:t.dimension)!=null?n:r.props.dimension)!=null?a:"2d",this._format=(o=t==null?void 0:t.format)!=null?o:r.props.format,this.channelDataType=hn[this._format]}get label(){return this._texture.label}$name(t){return this._texture.$name(t),this}unwrap(){var t;return this._view||(this._view=this._texture.unwrap().createView(A({label:`${(t=this.label)!=null?t:"<unnamed>"} - View`},this._props))),this._view}"~resolve"(t){var s;let r=t.names.makeUnique(this.label),n=((s=this._texture.props.sampleCount)!=null?s:1)>1,{group:a,binding:o}=t.allocateFixedEntry({texture:bn[this.channelDataType.type],viewDimension:this.dimension,multisampled:n},this),i=n?"texture_multisampled_2d":`texture_${gt[this.dimension]}`;return t.addDeclaration(`@group(${a}) @binding(${o}) var ${r}: ${i}<${t.resolve(this.channelDataType)}>;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},ft=class{constructor(t,r,n,a){this.dimension=r;this._multisampled=n;this._membership=a;p(this,"resourceType","texture-sampled-view");p(this,"channelDataType");this.channelDataType=wn[t]}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout),a=this._multisampled?"texture_multisampled_2d":`texture_${gt[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: ${a}<${t.resolve(this.channelDataType)}>;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}};function rr(e){return!!(e!=null&&e.usableAsSampled)}function ma(e){return!!(e!=null&&e.usableAsRender)}var Tt=class e extends Error{constructor(t){var r;super(`Resource '${(r=t.label)!=null?r:"<unnamed>"}' cannot be bound as 'sampled'. Use .$usage('sampled') to allow it.`),Object.setPrototypeOf(this,e.prototype)}};function ct(e){return new ar(e)}function yt(e){return!!e&&e.resourceType==="bind-group-layout"}function or(e){return!!e&&e.resourceType==="bind-group"}var nr=class e extends Error{constructor(t,r){super(`Bind group '${t!=null?t:"<unnamed>"}' is missing a required binding '${r}'`),Object.setPrototypeOf(this,e.prototype)}},Fn=["compute"],Fe=["compute","vertex","fragment"],ar=class{constructor(t){this.entries=t;p(this,"_label");p(this,"_index");p(this,"resourceType","bind-group-layout");p(this,"bound",{});var n,a,o,i,s;let r=0;for(let[l,u]of Object.entries(t)){if(u===null){r++;continue}let m={idx:r,key:l,layout:this};if("uniform"in u&&(this.bound[l]=new Oe("uniform",u.uniform,m)),"storage"in u){let f="type"in u.storage?u.storage:u.storage(0);this.bound[l]=new Oe((n=u.access)!=null?n:"readonly",f,m)}"texture"in u&&(this.bound[l]=new ft(u.texture,(a=u.viewDimension)!=null?a:"2d",(o=u.multisampled)!=null?o:!1,m)),"storageTexture"in u&&(this.bound[l]=new mt(u.storageTexture,(i=u.viewDimension)!=null?i:"2d",(s=u.access)!=null?s:"writeonly",m)),"externalTexture"in u&&(this.bound[l]=new dt(m)),"sampler"in u&&(u.sampler==="comparison"?this.bound[l]=new lt(m):this.bound[l]=new pt(m)),r++}}get label(){return this._label}get index(){return this._index}$name(t){return this._label=t,this}$idx(t){return this._index=t,this}unwrap(t){var n;return t.device.createBindGroupLayout({label:(n=this.label)!=null?n:"<unnamed>",entries:Object.values(this.entries).map((a,o)=>{var l,u,m,f;if(a===null)return null;let i=a.visibility,s={binding:o,visibility:0};if("uniform"in a)i=i!=null?i:Fe,s.buffer={type:"uniform"};else if("storage"in a)i=i!=null?i:a.access==="mutable"?Fn:Fe,s.buffer={type:a.access==="mutable"?"storage":"read-only-storage"};else if("sampler"in a)i=i!=null?i:Fe,s.sampler={type:a.sampler};else if("texture"in a)i=i!=null?i:Fe,s.texture={sampleType:a.texture,viewDimension:(l=a.viewDimension)!=null?l:"2d",multisampled:(u=a.multisampled)!=null?u:!1};else if("storageTexture"in a){let g=(m=a.access)!=null?m:"writeonly";i=i!=null?i:g==="readonly"?Fe:Fn,s.storageTexture={format:a.storageTexture,access:{mutable:"read-write",readonly:"read-only",writeonly:"write-only"}[g],viewDimension:(f=a.viewDimension)!=null?f:"2d"}}else"externalTexture"in a&&(i=i!=null?i:Fe,s.externalTexture={});return i!=null&&i.includes("compute")&&(s.visibility|=GPUShaderStage.COMPUTE),i!=null&&i.includes("vertex")&&(s.visibility|=GPUShaderStage.VERTEX),i!=null&&i.includes("fragment")&&(s.visibility|=GPUShaderStage.FRAGMENT),s}).filter(a=>a!==null)})}populate(t){return new ge(this,t)}},ge=class{constructor(t,r){this.layout=t;this.entries=r;p(this,"resourceType","bind-group");for(let n of Object.keys(t.entries))if(t.entries[n]!==null&&!(n in r))throw new nr(t.label,n)}unwrap(t){var n;return t.device.createBindGroup({label:(n=this.layout.label)!=null?n:"<unnamed>",layout:t.unwrap(this.layout),entries:Object.entries(this.layout.entries).map(([a,o],i)=>{var l;if(o===null)return null;let s=this.entries[a];if(s===void 0)throw new Error(`'${a}' is a resource required to populate bind group layout '${(l=this.layout.label)!=null?l:"<unnamed>"}'.`);if("uniform"in o){let u;if(ae(s)){if(!st(s))throw new vt(s);u={buffer:t.unwrap(s)}}else u={buffer:s};return{binding:i,resource:u}}if("storage"in o){let u;if(ae(s)){if(!fe(s))throw new Ge(s);u={buffer:t.unwrap(s)}}else u={buffer:s};return{binding:i,resource:u}}if("texture"in o){let u;if(J(s)){if(!rr(s))throw new Tt(s);u=t.unwrap(s.createView("sampled"))}else ze(s)?u=t.unwrap(s):u=s;return{binding:i,resource:u}}if("storageTexture"in o){let u;if(J(s)){if(!fe(s))throw new Ge(s);o.access==="readonly"?u=t.unwrap(s.createView("readonly")):o.access==="mutable"?u=t.unwrap(s.createView("mutable")):u=t.unwrap(s.createView("writeonly"))}else Ne(s)?u=t.unwrap(s):u=s;return{binding:i,resource:u}}if("sampler"in o)return ke(s)||We(s)?{binding:i,resource:t.unwrap(s)}:{binding:i,resource:s};if("externalTexture"in o)return{binding:i,resource:s};throw new Error(`Malformed bind group entry: ${s} (${JSON.stringify(s)})`)}).filter(a=>a!==null)})}};var Un="#CATCHALL#",sr=class{constructor(){p(this,"_stack",[]);p(this,"_itemDepth",0)}get itemDepth(){return this._itemDepth}get topItem(){let t=this._stack[this._stack.length-1];if(!t||t.type!=="item")throw new Error("Internal error, expected item layer to be on top.");return t}pushItem(){this._itemDepth++,this._stack.push({type:"item",usedSlots:new Set})}popItem(){this.pop("item")}pushSlotBindings(t){this._stack.push({type:"slotBinding",bindingMap:new WeakMap(t)})}popSlotBindings(){this.pop("slotBinding")}pushFunctionScope(t,r,n){this._stack.push({type:"functionScope",args:t,returnType:r,externalMap:n})}popFunctionScope(){this.pop("functionScope")}pushBlockScope(){this._stack.push({type:"blockScope",declarations:new Map})}popBlockScope(){this.pop("blockScope")}pop(t){let r=this._stack[this._stack.length-1];if(!r||t&&r.type!==t)throw new Error(`Internal error, expected a ${t} layer to be on top.`);this._stack.pop(),t==="item"&&this._itemDepth--}readSlot(t){for(let r=this._stack.length-1;r>=0;--r){let n=this._stack[r];if((n==null?void 0:n.type)==="item")n.usedSlots.add(t);else if((n==null?void 0:n.type)==="slotBinding"){let a=n.bindingMap.get(t);if(a!==void 0)return a}else if(!((n==null?void 0:n.type)==="functionScope"||(n==null?void 0:n.type)==="blockScope"))throw new Error("Unknown layer type.")}return t.defaultValue}getResourceById(t){for(let r=this._stack.length-1;r>=0;--r){let n=this._stack[r];if((n==null?void 0:n.type)==="functionScope"){let a=n.args.find(i=>i.value===t);if(a!==void 0)return a;let o=n.externalMap[t];return o!==void 0?{value:o,dataType:q(o)?we(o):P}:void 0}if((n==null?void 0:n.type)==="blockScope"){let a=n.declarations.get(t);if(a!==void 0)return{value:t,dataType:a}}}}defineBlockVariable(t,r){for(let n=this._stack.length-1;n>=0;--n){let a=this._stack[n];if((a==null?void 0:a.type)==="blockScope")return a.declarations.set(t,r),{value:t,dataType:r}}throw new Error("No block scope found to define a variable in.")}},xt=[""," "," "," "," "," "," "," "," "],ir=xt.length-1,ur=class{constructor(){p(this,"identLevel",0)}get pre(){var t;return(t=xt[this.identLevel])!=null?t:xt[ir].repeat(this.identLevel/ir)+xt[this.identLevel%ir]}indent(){let t=this.pre;return this.identLevel++,t}dedent(){return this.identLevel--,this.pre}},vn;vn=b;var pr=class{constructor(t){p(this,"_memoizedResolves",new WeakMap);p(this,"_memoizedDerived",new WeakMap);p(this,"_indentController",new ur);p(this,"_jitTranspiler");p(this,"_itemStateStack",new sr);p(this,"_declarations",[]);p(this,vn,{itemStateStack:this._itemStateStack});p(this,"bindGroupLayoutsToPlaceholderMap",new Map);p(this,"_nextFreeLayoutPlaceholderIdx",0);p(this,"fixedBindings",[]);p(this,"callStack",[]);p(this,"names");this.names=t.names,this._jitTranspiler=t.jitTranspiler}get pre(){return this._indentController.pre}indent(){return this._indentController.indent()}dedent(){return this._indentController.dedent()}getById(t){let r=this._itemStateStack.getResourceById(t);return r===void 0?null:r}defineVariable(t,r){return this._itemStateStack.defineBlockVariable(t,r)}pushBlockScope(){this._itemStateStack.pushBlockScope()}popBlockScope(){this._itemStateStack.popBlockScope()}transpileFn(t){if(!this._jitTranspiler)throw new Error("Tried to execute a tgpu.fn function without providing a JIT transpiler, or transpiling at build time.");return this._jitTranspiler.transpileFn(t)}fnToWgsl(t){this._itemStateStack.pushFunctionScope(t.args,t.returnType,t.externalMap);try{let r=kt(this,t.body),n=t.args.map(a=>`${a.value}: ${this.resolve(a.dataType)}`).join(", ");return{head:t.returnType!==void 0?`(${n}) -> ${tt(t.returnType)} ${this.resolve(t.returnType)}`:`(${n})`,body:r}}finally{this._itemStateStack.popFunctionScope()}}addDeclaration(t){this._declarations.push(t)}allocateLayoutEntry(t){let r=this.bindGroupLayoutsToPlaceholderMap,n=r.get(t);return n||(n=`#BIND_GROUP_LAYOUT_${this._nextFreeLayoutPlaceholderIdx++}#`,r.set(t,n)),n}allocateFixedEntry(t,r){let n=this.fixedBindings.length;return this.fixedBindings.push({layoutEntry:t,resource:r}),{group:Un,binding:n}}readSlot(t){let r=this._itemStateStack.readSlot(t);if(r===void 0)throw new Ft(t);return r}withSlots(t,r){this._itemStateStack.pushSlotBindings(t);try{return r()}finally{this._itemStateStack.popSlotBindings()}}unwrap(t){if(Rt(t))return this.withSlots(t["~providing"].pairs,()=>this.unwrap(t["~providing"].inner));let r=t;for(;;)if(j(r))r=this.readSlot(r);else if(K(r))r=this._getOrCompute(r);else break;return r}_getOrCompute(t){var n;let r=(n=this._memoizedDerived.get(t))!=null?n:[];this._itemStateStack.pushItem();try{for(let i of r)if([...i.slotToValueMap.entries()].every(([l,u])=>l.areEqual(this._itemStateStack.readSlot(l),u)))return i.result;At(Ie.CPU);let a;try{a=t["~compute"]()}finally{It(Ie.CPU)}let o=new Map;for(let i of this._itemStateStack.topItem.usedSlots)o.set(i,this._itemStateStack.readSlot(i));return r.push({slotToValueMap:o,result:a}),this._memoizedDerived.set(t,r),a}catch(a){throw a instanceof ce?a.appendToTrace(t):new ce(a,[t])}finally{this._itemStateStack.popItem()}}_getOrInstantiate(t){var n;let r=(n=this._memoizedResolves.get(t))!=null?n:[];this._itemStateStack.pushItem();try{for(let i of r)if([...i.slotToValueMap.entries()].every(([l,u])=>l.areEqual(this._itemStateStack.readSlot(l),u)))return i.result;let a;k(t)?a=$t(this,t):K(t)||j(t)?a=this.resolve(this.unwrap(t)):_r(t)?a=t["~resolve"](this):a=this.resolveValue(t);let o=new Map;for(let i of this._itemStateStack.topItem.usedSlots)o.set(i,this._itemStateStack.readSlot(i));return r.push({slotToValueMap:o,result:a}),this._memoizedResolves.set(t,r),a}catch(a){throw a instanceof ce?a.appendToTrace(t):new ce(a,[t])}finally{this._itemStateStack.popItem()}}resolve(t){if(Rt(t))return this.withSlots(t["~providing"].pairs,()=>this.resolve(t["~providing"].inner));if(t&&typeof t=="object"||typeof t=="function"){if(this._itemStateStack.itemDepth===0)try{At(Ie.GPU);let r=Dr(this,()=>this._getOrInstantiate(t));return`${[...this._declarations].join(`
|
22
|
+
`}return u}let n=Mt[_r(e)?e.inner.type:e.type];return`output.${Nt[n]}(${t}, ${r}, littleEndian);
|
23
|
+
`}function it(e){if(Wt.has(e))return Wt.get(e);let t=zt(e,"offset","value"),r=new Function("output","offset","value","littleEndian=true",t);return Wt.set(e,r),r}var V={bool(e,t,r){e.writeBool(r)},f32(e,t,r){e.writeFloat32(r)},f16(e,t,r){e.writeFloat16(r)},i32(e,t,r){e.writeInt32(r)},u32(e,t,r){e.writeUint32(r)},vec2f(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y)},vec2h(e,t,r){e.writeFloat16(r.x),e.writeFloat16(r.y)},vec2i(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y)},vec2u(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y)},vec3f(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y),e.writeFloat32(r.z)},vec3h(e,t,r){e.writeFloat16(r.x),e.writeFloat16(r.y),e.writeFloat16(r.z)},vec3i(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y),e.writeInt32(r.z)},vec3u(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y),e.writeUint32(r.z)},vec4f(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y),e.writeFloat32(r.z),e.writeFloat32(r.w)},vec4h(e,t,r){e.writeFloat16(r.x),e.writeFloat16(r.y),e.writeFloat16(r.z),e.writeFloat16(r.w)},vec4i(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y),e.writeInt32(r.z),e.writeInt32(r.w)},vec4u(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y),e.writeUint32(r.z),e.writeUint32(r.w)},mat2x2f(e,t,r){for(let n=0;n<r.length;++n)e.writeFloat32(r[n])},mat3x3f(e,t,r){for(let n=0;n<r.length;++n)e.writeFloat32(r[n])},mat4x4f(e,t,r){for(let n=0;n<r.length;++n)e.writeFloat32(r[n])},struct(e,t,r){let n=v(t);D(e,n);for(let[a,o]of Object.entries(t.propTypes))D(e,v(o)),ne(e,o,r[a]);D(e,n)},array(e,t,r){if(t.elementCount===0)throw new Error("Cannot write using a runtime-sized schema.");let n=v(t);D(e,n);let a=e.currentByteOffset;for(let o=0;o<Math.min(t.elementCount,r.length);o++)D(e,n),ne(e,t.elementType,r[o]);e.seekTo(a+h(t))},ptr(){throw new Error("Pointers are not host-shareable")},atomic(e,t,r){var n;(n=V[t.inner.type])==null||n.call(V,e,t,r)},decorated(e,t,r){var o,i;let n=W(t);D(e,n);let a=e.currentByteOffset;(i=V[(o=t.inner)==null?void 0:o.type])==null||i.call(V,e,t.inner,r),e.seekTo(a+h(t))},uint8(e,t,r){e.writeUint8(r)},uint8x2(e,t,r){e.writeUint8(r.x),e.writeUint8(r.y)},uint8x4(e,t,r){e.writeUint8(r.x),e.writeUint8(r.y),e.writeUint8(r.z),e.writeUint8(r.w)},sint8(e,t,r){e.writeInt8(r)},sint8x2(e,t,r){e.writeInt8(r.x),e.writeInt8(r.y)},sint8x4(e,t,r){e.writeInt8(r.x),e.writeInt8(r.y),e.writeInt8(r.z),e.writeInt8(r.w)},unorm8(e,t,r){e.writeUint8(r*255)},unorm8x2(e,t,r){e.writeUint8(r.x*255),e.writeUint8(r.y*255)},unorm8x4(e,t,r){e.writeUint8(r.x*255),e.writeUint8(r.y*255),e.writeUint8(r.z*255),e.writeUint8(r.w*255)},snorm8(e,t,r){e.writeUint8(r*127+128)},snorm8x2(e,t,r){e.writeUint8(r.x*127+128),e.writeUint8(r.y*127+128)},snorm8x4(e,t,r){e.writeUint8(r.x*127+128),e.writeUint8(r.y*127+128),e.writeUint8(r.z*127+128),e.writeUint8(r.w*127+128)},uint16(e,t,r){e.writeUint16(r)},uint16x2(e,t,r){e.writeUint16(r.x),e.writeUint16(r.y)},uint16x4(e,t,r){e.writeUint16(r.x),e.writeUint16(r.y),e.writeUint16(r.z),e.writeUint16(r.w)},sint16(e,t,r){e.writeInt16(r)},sint16x2(e,t,r){e.writeInt16(r.x),e.writeInt16(r.y)},sint16x4(e,t,r){e.writeInt16(r.x),e.writeInt16(r.y),e.writeInt16(r.z),e.writeInt16(r.w)},unorm16(e,t,r){e.writeUint16(r*65535)},unorm16x2(e,t,r){e.writeUint16(r.x*65535),e.writeUint16(r.y*65535)},unorm16x4(e,t,r){e.writeUint16(r.x*65535),e.writeUint16(r.y*65535),e.writeUint16(r.z*65535),e.writeUint16(r.w*65535)},snorm16(e,t,r){e.writeUint16(r*32767+32768)},snorm16x2(e,t,r){e.writeUint16(r.x*32767+32768),e.writeUint16(r.y*32767+32768)},snorm16x4(e,t,r){e.writeUint16(r.x*32767+32768),e.writeUint16(r.y*32767+32768),e.writeUint16(r.z*32767+32768),e.writeUint16(r.w*32767+32768)},float16(e,t,r){e.writeFloat16(r)},float16x2(e,t,r){e.writeFloat16(r.x),e.writeFloat16(r.y)},float16x4(e,t,r){e.writeFloat16(r.x),e.writeFloat16(r.y),e.writeFloat16(r.z),e.writeFloat16(r.w)},float32(e,t,r){e.writeFloat32(r)},float32x2(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y)},float32x3(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y),e.writeFloat32(r.z)},float32x4(e,t,r){e.writeFloat32(r.x),e.writeFloat32(r.y),e.writeFloat32(r.z),e.writeFloat32(r.w)},uint32(e,t,r){e.writeUint32(r)},uint32x2(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y)},uint32x3(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y),e.writeUint32(r.z)},uint32x4(e,t,r){e.writeUint32(r.x),e.writeUint32(r.y),e.writeUint32(r.z),e.writeUint32(r.w)},sint32(e,t,r){e.writeInt32(r)},sint32x2(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y)},sint32x3(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y),e.writeInt32(r.z)},sint32x4(e,t,r){e.writeInt32(r.x),e.writeInt32(r.y),e.writeInt32(r.z),e.writeInt32(r.w)},"unorm10-10-10-2"(e,t,r){let n=0;n|=(r.x*1023&1023)<<22,n|=(r.x*1023&1023)<<12,n|=(r.y*1023&1023)<<2,n|=r.z*3&3,e.writeUint32(n)},"unorm8x4-bgra"(e,t,r){e.writeUint8(r.z*255),e.writeUint8(r.y*255),e.writeUint8(r.x*255),e.writeUint8(r.w*255)},disarray(e,t,r){var o,i;let n=v(t);D(e,n);let a=e.currentByteOffset;for(let s=0;s<Math.min(t.elementCount,r.length);s++)D(e,n),(i=V[(o=t.elementType)==null?void 0:o.type])==null||i.call(V,e,t.elementType,r[s]);e.seekTo(a+h(t))},unstruct(e,t,r){var n;for(let[a,o]of Object.entries(t.propTypes))(n=V[o.type])==null||n.call(V,e,o,r[a])},"loose-decorated"(e,t,r){var i;let n=W(t);D(e,n);let a=e.currentByteOffset,o=V[(i=t.inner)==null?void 0:i.type];return o==null||o(e,t.inner,r),e.seekTo(a+h(t)),r}};function ne(e,t,r){let n=V[t.type];if(!n)throw new Error(`Cannot write data of type '${t.type}'.`);n(e,t,r)}var me={bool(e){return e.readBool()},f32(e){return e.readFloat32()},f16(e){return e.readFloat16()},i32(e){return e.readInt32()},u32(e){return e.readUint32()},vec2f(e){return G(e.readFloat32(),e.readFloat32())},vec3f(e){return ue(e.readFloat32(),e.readFloat32(),e.readFloat32())},vec4f(e){return S(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32())},vec2h(e){return Re(e.readFloat16(),e.readFloat16())},vec3h(e){return Be(e.readFloat16(),e.readFloat16(),e.readFloat16())},vec4h(e){return _e(e.readFloat16(),e.readFloat16(),e.readFloat16(),e.readFloat16())},vec2i(e){return H(e.readInt32(),e.readInt32())},vec3i(e){return ye(e.readInt32(),e.readInt32(),e.readInt32())},vec4i(e){return C(e.readInt32(),e.readInt32(),e.readInt32(),e.readInt32())},vec2u(e){return Y(e.readUint32(),e.readUint32())},vec3u(e){return xe(e.readUint32(),e.readUint32(),e.readUint32())},vec4u(e){return E(e.readUint32(),e.readUint32(),e.readUint32(),e.readUint32())},mat2x2f(e){return Ke(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32())},mat3x3f(e){let t=()=>{let r=e.readFloat32();return e.readFloat32(),r};return qe(e.readFloat32(),e.readFloat32(),t(),e.readFloat32(),e.readFloat32(),t(),e.readFloat32(),e.readFloat32(),t())},mat4x4f(e){return Je(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32())},struct(e,t){let r=v(t);D(e,r);let n={};for(let[a,o]of Object.entries(t.propTypes))D(e,v(o)),n[a]=O(e,o);return D(e,r),n},array(e,t){if(t.elementCount===0)throw new Error("Cannot read using a runtime-sized schema.");let r=v(t),n=[];for(let a=0;a<t.elementCount;a++){D(e,r);let o=t.elementType,i=O(e,o);n.push(i)}return D(e,r),n},ptr(){throw new Error("Pointers are not host-shareable")},atomic(e,t){return O(e,t.inner)},decorated(e,t){let r=W(t);D(e,r);let n=e.currentByteOffset,a=O(e,t.inner);return e.seekTo(n+h(t)),a},uint8:e=>e.readUint8(),uint8x2:e=>Y(e.readUint8(),e.readUint8()),uint8x4:e=>E(e.readUint8(),e.readUint8(),e.readUint8(),e.readUint8()),sint8:e=>e.readInt8(),sint8x2:e=>H(e.readInt8(),e.readInt8()),sint8x4:e=>C(e.readInt8(),e.readInt8(),e.readInt8(),e.readInt8()),unorm8:e=>e.readUint8()/255,unorm8x2:e=>G(e.readUint8()/255,e.readUint8()/255),unorm8x4:e=>S(e.readUint8()/255,e.readUint8()/255,e.readUint8()/255,e.readUint8()/255),snorm8:e=>(e.readUint8()-128)/127,snorm8x2:e=>G((e.readUint8()-128)/127,(e.readUint8()-128)/127),snorm8x4:e=>S((e.readUint8()-128)/127,(e.readUint8()-128)/127,(e.readUint8()-128)/127,(e.readUint8()-128)/127),uint16:e=>e.readUint16(),uint16x2:e=>Y(e.readUint16(),e.readUint16()),uint16x4:e=>E(e.readUint16(),e.readUint16(),e.readUint16(),e.readUint16()),sint16:e=>e.readInt16(),sint16x2:e=>H(e.readInt16(),e.readInt16()),sint16x4:e=>C(e.readInt16(),e.readInt16(),e.readInt16(),e.readInt16()),unorm16:e=>e.readUint16()/65535,unorm16x2:e=>G(e.readUint16()/65535,e.readUint16()/65535),unorm16x4:e=>S(e.readUint16()/65535,e.readUint16()/65535,e.readUint16()/65535,e.readUint16()/65535),snorm16:e=>(e.readUint16()-32768)/32767,snorm16x2:e=>G(me.snorm16(e),me.snorm16(e)),snorm16x4:e=>S(me.snorm16(e),me.snorm16(e),me.snorm16(e),me.snorm16(e)),float16(e){return e.readFloat16()},float16x2:e=>G(e.readFloat16(),e.readFloat16()),float16x4:e=>S(e.readFloat16(),e.readFloat16(),e.readFloat16(),e.readFloat16()),float32:e=>e.readFloat32(),float32x2:e=>G(e.readFloat32(),e.readFloat32()),float32x3:e=>ue(e.readFloat32(),e.readFloat32(),e.readFloat32()),float32x4:e=>S(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32()),uint32:e=>e.readUint32(),uint32x2:e=>Y(e.readUint32(),e.readUint32()),uint32x3:e=>xe(e.readUint32(),e.readUint32(),e.readUint32()),uint32x4:e=>E(e.readUint32(),e.readUint32(),e.readUint32(),e.readUint32()),sint32:e=>e.readInt32(),sint32x2:e=>H(e.readInt32(),e.readInt32()),sint32x3:e=>ye(e.readInt32(),e.readInt32(),e.readInt32()),sint32x4:e=>C(e.readInt32(),e.readInt32(),e.readInt32(),e.readInt32()),"unorm10-10-10-2"(e){let t=e.readUint32(),r=(t>>22)/1023,n=(t>>12&1023)/1023,a=(t>>2&1023)/1023,o=(t&3)/3;return S(r,n,a,o)},"unorm8x4-bgra"(e){let t=e.readByte()/255,r=e.readByte()/255,n=e.readByte()/255,a=e.readByte()/255;return S(n,r,t,a)},unstruct(e,t){let r={};for(let[n,a]of Object.entries(t.propTypes))r[n]=O(e,a);return r},disarray(e,t){let r=v(t),n=[];for(let a=0;a<t.elementCount;a++)D(e,r),n.push(O(e,t.elementType));return D(e,r),n},"loose-decorated"(e,t){D(e,W(t));let r=e.currentByteOffset,n=O(e,t.inner);return e.seekTo(r+h(t)),n}};function O(e,t){let r=me[t.type];if(!r)throw new Error(`Cannot read data of type '${t.type}'.`);return r(e,t)}import{BufferWriter as la}from"typed-binary";function pn(e,t){var u;let r=h(e);if(r===0||t===void 0||t===null)return[];let n=new ArrayBuffer(r),a=new la(n),o=[];function i(m,f,g,w){var y;if(f!=null){if(_(m)||X(m)){let T=at(m);for(let[F,c]of Object.entries(T)){let x=m.propTypes[F];if(!x)continue;let Te=f[F];Te!==void 0&&i(x,Te,g+c.offset,(y=c.padding)!=null?y:w)}return}if(Q(m)||Ze(m)){let T=m,F=$(h(T.elementType),v(T.elementType));if(!Array.isArray(f))throw new Error("Partial value for array must be an array");let c=f;c.sort((x,Te)=>x.idx-Te.idx);for(let{idx:x,value:Te}of c)i(T.elementType,Te,g+x*F,F-h(T.elementType))}else{let T=h(m);a.seekTo(g),ne(a,m,f),o.push({start:g,end:g+T,padding:w})}}}if(i(e,t,0),o.length===0)return[];let s=[],l=o[0];for(let m=1;m<o.length;m++){let f=o[m];if(!f||!l)throw new Error("Internal error: missing segment");f.start===l.end+((u=l.padding)!=null?u:0)?(l.end=f.end,l.padding=f.padding):(s.push({data:new Uint8Array(n,l.start,l.end-l.start)}),l=f)}if(!l)throw new Error("Internal error: missing segment");return s.push({data:new Uint8Array(n,l.start,l.end-l.start)}),s}function fe(e){return!!(e!=null&&e.usableAsStorage)}var Ge=class e extends Error{constructor(t){var r;super(`Resource '${(r=t.label)!=null?r:"<unnamed>"}' cannot be bound as 'storage'. Use .$usage('storage') to allow it.`),Object.setPrototypeOf(this,e.prototype)}};function st(e){return!!e.usableAsUniform}var Tn={uniform:"uniform",mutable:"storage, read_write",readonly:"storage, read"},fn;fn=b;var $e=class{constructor(t,r){this.usage=t;this.buffer=r;p(this,"~repr");p(this,"resourceType","buffer-usage");p(this,fn);this[b]={dataType:r.dataType}}get label(){return this.buffer.label}$name(t){return this.buffer.$name(t),this}"~resolve"(t){let r=t.names.makeUnique(this.label),{group:n,binding:a}=t.allocateFixedEntry(this.usage==="uniform"?{uniform:this.buffer.dataType}:{storage:this.buffer.dataType,access:this.usage},this.buffer),o=Tn[this.usage];return t.addDeclaration(`@group(${n}) @binding(${a}) var<${o}> ${r}: ${t.resolve(this.buffer.dataType)};`),r}write(t){this.buffer.write(t)}toString(){var t;return`${this.usage}:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){if(!ie())throw new Error("Cannot access buffer's value directly in JS.");return new Proxy({"~resolve":t=>t.resolve(this),toString:()=>{var t;return`.value:${(t=this.label)!=null?t:"<unnamed>"}`},[b]:{dataType:this.buffer.dataType}},M)}},gn;gn=b;var Oe=class{constructor(t,r,n){this.usage=t;this.dataType=r;this._membership=n;p(this,"~repr");p(this,"resourceType","buffer-usage");p(this,gn);this[b]={dataType:r}}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout),a=Tn[this.usage];return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var<${a}> ${r}: ${t.resolve(this.dataType)};`),r}toString(){var t;return`${this.usage}:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){if(!ie())throw new Error("Cannot access buffer's value directly in JS.");return new Proxy({"~resolve":t=>t.resolve(this),toString:()=>{var t;return`.value:${(t=this.label)!=null?t:"<unnamed>"}`},[b]:{dataType:this.dataType}},M)}},ln=new WeakMap;function jt(e){if(!fe(e))throw new Error(`Cannot pass ${e} to asMutable, as it is not allowed to be used as storage. To allow it, call .$usage('storage') when creating the buffer.`);let t=ln.get(e);return t||(t=new $e("mutable",e),ln.set(e,t)),t}var dn=new WeakMap;function Kt(e){if(!fe(e))throw new Error(`Cannot pass ${e} to asReadonly, as it is not allowed to be used as storage. To allow it, call .$usage('storage') when creating the buffer.`);let t=dn.get(e);return t||(t=new $e("readonly",e),dn.set(e,t)),t}var mn=new WeakMap;function qt(e){if(!st(e))throw new Error(`Cannot pass ${e} to asUniform, as it is not allowed to be used as a uniform. To allow it, call .$usage('uniform') when creating the buffer.`);let t=mn.get(e);return t||(t=new $e("uniform",e),mn.set(e,t)),t}var Yt={uniform:qt,mutable:jt,readonly:Kt};function yn(e,t,r){return be(t)?new ut(e,t,r):new ut(e,t,r,["storage","uniform"])}function ae(e){return e.resourceType==="buffer"}function ma(e){return!!e.usableAsVertex}var cn=da(),ut=class{constructor(t,r,n,a){this._group=t;this.dataType=r;this.initialOrBuffer=n;this._disallowedUsages=a;p(this,"resourceType","buffer");p(this,"flags",GPUBufferUsage.COPY_DST|GPUBufferUsage.COPY_SRC);p(this,"_buffer",null);p(this,"_ownBuffer");p(this,"_destroyed",!1);p(this,"_hostBuffer");p(this,"_label");p(this,"initial");p(this,"usableAsUniform",!1);p(this,"usableAsStorage",!1);p(this,"usableAsVertex",!1);Lr(n)?(this._ownBuffer=!1,this._buffer=n):(this._ownBuffer=!0,this.initial=n)}get label(){return this._label}get buffer(){var r;let t=this._group.device;if(this._destroyed)throw new Error("This buffer has been destroyed");if(!this._buffer&&(this._buffer=t.createBuffer({size:h(this.dataType),usage:this.flags,mappedAtCreation:!!this.initial,label:(r=this.label)!=null?r:"<unnamed>"}),this.initial)){let n=new Ht(this._buffer.getMappedRange());ne(n,this.dataType,this.initial),this._buffer.unmap()}return this._buffer}get destroyed(){return this._destroyed}$name(t){return this._label=t,this._buffer&&(this._buffer.label=t),this}$usage(...t){var r;for(let n of t){if((r=this._disallowedUsages)!=null&&r.includes(n))throw new Error(`Buffer of type ${this.dataType.type} cannot be used as ${n}`);this.flags|=n==="uniform"?GPUBufferUsage.UNIFORM:0,this.flags|=n==="storage"?GPUBufferUsage.STORAGE:0,this.flags|=n==="vertex"?GPUBufferUsage.VERTEX:0,this.usableAsUniform=this.usableAsUniform||n==="uniform",this.usableAsStorage=this.usableAsStorage||n==="storage",this.usableAsVertex=this.usableAsVertex||n==="vertex"}return this}$addFlags(t){if(!this._ownBuffer)throw new Error("Cannot add flags to a buffer that is not managed by TypeGPU.");return t&GPUBufferUsage.MAP_READ?(this.flags=GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ,this):t&GPUBufferUsage.MAP_WRITE?(this.flags=GPUBufferUsage.COPY_SRC|GPUBufferUsage.MAP_WRITE,this):(this.flags|=t,this)}compileWriter(){if(ot)it(this.dataType);else throw new Error("This environment does not allow eval")}write(t){let r=this.buffer,n=this._group.device;if(r.mapState==="mapped"){let o=r.getMappedRange();if(ot){it(this.dataType)(new DataView(o),0,t,cn==="little");return}ne(new Ht(o),this.dataType,t);return}let a=h(this.dataType);this._hostBuffer||(this._hostBuffer=new ArrayBuffer(a)),this._group.flush(),ot?it(this.dataType)(new DataView(this._hostBuffer),0,t,cn==="little"):ne(new Ht(this._hostBuffer),this.dataType,t),n.queue.writeBuffer(r,0,this._hostBuffer,0,a)}writePartial(t){let r=this.buffer,n=this._group.device,a=pn(this.dataType,t);if(r.mapState==="mapped"){let o=r.getMappedRange(),i=new Uint8Array(o);for(let s of a)i.set(s.data,s.data.byteOffset)}else for(let o of a)n.queue.writeBuffer(r,o.data.byteOffset,o.data,0,o.data.byteLength)}copyFrom(t){if(this.buffer.mapState==="mapped")throw new Error("Cannot copy to a mapped buffer.");let r=h(this.dataType);this._group.commandEncoder.copyBufferToBuffer(t.buffer,0,this.buffer,0,r)}async read(){this._group.flush();let t=this.buffer,r=this._group.device;if(t.mapState==="mapped"){let i=t.getMappedRange();return O(new Jt(i),this.dataType)}if(t.usage&GPUBufferUsage.MAP_READ){await t.mapAsync(GPUMapMode.READ);let i=t.getMappedRange(),s=O(new Jt(i),this.dataType);return t.unmap(),s}let n=r.createBuffer({size:h(this.dataType),usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ}),a=r.createCommandEncoder();a.copyBufferToBuffer(t,0,n,0,h(this.dataType)),r.queue.submit([a.finish()]),await r.queue.onSubmittedWorkDone(),await n.mapAsync(GPUMapMode.READ,0,h(this.dataType));let o=O(new Jt(n.getMappedRange()),this.dataType);return n.unmap(),n.destroy(),o}as(t){var r;return(r=Yt[t])==null?void 0:r.call(Yt,this)}destroy(){var t;this._destroyed||(this._destroyed=!0,this._ownBuffer&&((t=this._buffer)==null||t.destroy()))}toString(){var t;return`buffer:${(t=this._label)!=null?t:"<unnamed>"}`}};function xn(e){return new Qt(e)}function hn(e){return new Xt(e)}function ke(e){return(e==null?void 0:e.resourceType)==="sampler"}function We(e){return(e==null?void 0:e.resourceType)==="sampler-comparison"}var pt=class{constructor(t){this._membership=t;p(this,"resourceType","sampler")}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout);return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: sampler;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},lt=class{constructor(t){this._membership=t;p(this,"resourceType","sampler-comparison")}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout);return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: sampler_comparison;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},Qt=class{constructor(t){this._props=t;p(this,"resourceType","sampler");p(this,"_label");p(this,"_filtering");p(this,"_sampler",null);this._filtering=t.minFilter==="linear"||t.magFilter==="linear"||t.mipmapFilter==="linear"}unwrap(t){var r;return this._sampler||(this._sampler=t.device.createSampler(L(A({},this._props),{label:(r=this._label)!=null?r:"<unnamed>"}))),this._sampler}get label(){return this._label}$name(t){return this._label=t,this}"~resolve"(t){let r=t.names.makeUnique(this._label),{group:n,binding:a}=t.allocateFixedEntry({sampler:this._filtering?"filtering":"non-filtering"},this);return t.addDeclaration(`@group(${n}) @binding(${a}) var ${r}: sampler;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},Xt=class{constructor(t){this._props=t;p(this,"resourceType","sampler-comparison");p(this,"_label");p(this,"_sampler",null)}unwrap(t){var r;return this._sampler||(this._sampler=t.device.createSampler(L(A({},this._props),{label:(r=this._label)!=null?r:"<unnamed>"}))),this._sampler}get label(){return this._label}$name(t){return this._label=t,this}"~resolve"(t){let r=t.names.makeUnique(this.label),{group:n,binding:a}=t.allocateFixedEntry({sampler:"comparison"},this);return t.addDeclaration(`@group(${n}) @binding(${a}) var ${r}: sampler_comparison;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}};var dt=class{constructor(t){this._membership=t;p(this,"resourceType","external-texture")}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout);return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: texture_external;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}};var bn={r8unorm:d,r8snorm:d,r8uint:I,r8sint:B,r16uint:I,r16sint:B,r16float:d,rg8unorm:d,rg8snorm:d,rg8uint:I,rg8sint:B,r32uint:I,r32sint:B,r32float:d,rg16uint:I,rg16sint:B,rg16float:d,rgba8unorm:d,"rgba8unorm-srgb":d,rgba8snorm:d,rgba8uint:I,rgba8sint:B,bgra8unorm:d,"bgra8unorm-srgb":d,rgb9e5ufloat:d,rgb10a2uint:I,rgb10a2unorm:d,rg11b10ufloat:d,rg32uint:I,rg32sint:B,rg32float:d,rgba16uint:I,rgba16sint:B,rgba16float:d,rgba32uint:I,rgba32sint:B,rgba32float:d,stencil8:d,depth16unorm:d,depth24plus:d,"depth24plus-stencil8":d,depth32float:d,"depth32float-stencil8":d,"bc1-rgba-unorm":d,"bc1-rgba-unorm-srgb":d,"bc2-rgba-unorm":d,"bc2-rgba-unorm-srgb":d,"bc3-rgba-unorm":d,"bc3-rgba-unorm-srgb":d,"bc4-r-unorm":d,"bc4-r-snorm":d,"bc5-rg-unorm":d,"bc5-rg-snorm":d,"bc6h-rgb-ufloat":d,"bc6h-rgb-float":d,"bc7-rgba-unorm":d,"bc7-rgba-unorm-srgb":d,"etc2-rgb8unorm":d,"etc2-rgb8unorm-srgb":d,"etc2-rgb8a1unorm":d,"etc2-rgb8a1unorm-srgb":d,"etc2-rgba8unorm":d,"etc2-rgba8unorm-srgb":d,"eac-r11unorm":d,"eac-r11snorm":d,"eac-rg11unorm":d,"eac-rg11snorm":d,"astc-4x4-unorm":d,"astc-4x4-unorm-srgb":d,"astc-5x4-unorm":d,"astc-5x4-unorm-srgb":d,"astc-5x5-unorm":d,"astc-5x5-unorm-srgb":d,"astc-6x5-unorm":d,"astc-6x5-unorm-srgb":d,"astc-6x6-unorm":d,"astc-6x6-unorm-srgb":d,"astc-8x5-unorm":d,"astc-8x5-unorm-srgb":d,"astc-8x6-unorm":d,"astc-8x6-unorm-srgb":d,"astc-8x8-unorm":d,"astc-8x8-unorm-srgb":d,"astc-10x5-unorm":d,"astc-10x5-unorm-srgb":d,"astc-10x6-unorm":d,"astc-10x6-unorm-srgb":d,"astc-10x8-unorm":d,"astc-10x8-unorm-srgb":d,"astc-10x10-unorm":d,"astc-10x10-unorm-srgb":d,"astc-12x10-unorm":d,"astc-12x10-unorm-srgb":d,"astc-12x12-unorm":d,"astc-12x12-unorm-srgb":d},Me={rgba8unorm:S,rgba8snorm:S,rgba8uint:E,rgba8sint:C,rgba16uint:E,rgba16sint:C,rgba16float:S,r32uint:E,r32sint:C,r32float:S,rg32uint:E,rg32sint:C,rg32float:S,rgba32uint:E,rgba32sint:C,rgba32float:S,bgra8unorm:S},wn={f32:"float",u32:"uint",i32:"sint"},Sn={float:d,"unfilterable-float":d,uint:I,sint:B,depth:d};function Dn(e,t){return new Zt(e,t)}function J(e){return(e==null?void 0:e.resourceType)==="texture"}function Ne(e){return(e==null?void 0:e.resourceType)==="texture-storage-view"}function ze(e){return(e==null?void 0:e.resourceType)==="texture-sampled-view"}var Fn={mutable:"read_write",readonly:"read",writeonly:"write"},Zt=class{constructor(t,r){this.props=t;this._branch=r;p(this,"resourceType","texture");p(this,"usableAsSampled",!1);p(this,"usableAsStorage",!1);p(this,"usableAsRender",!1);p(this,"_destroyed",!1);p(this,"_label");p(this,"_flags",GPUTextureUsage.COPY_DST|GPUTextureUsage.COPY_SRC);p(this,"_texture",null)}get label(){return this._label}$name(t){return this._label=t,this}unwrap(){var t,r,n,a,o;if(this._destroyed)throw new Error("This texture has been destroyed");return this._texture||(this._texture=this._branch.device.createTexture({label:(t=this._label)!=null?t:"<unnamed>",format:this.props.format,size:this.props.size,usage:this._flags,dimension:(r=this.props.dimension)!=null?r:"2d",viewFormats:(n=this.props.viewFormats)!=null?n:[],mipLevelCount:(a=this.props.mipLevelCount)!=null?a:1,sampleCount:(o=this.props.sampleCount)!=null?o:1})),this._texture}$usage(...t){let r=t.includes("storage"),n=t.includes("sampled"),a=t.includes("render");return this._flags|=n?GPUTextureUsage.TEXTURE_BINDING:0,this._flags|=r?GPUTextureUsage.STORAGE_BINDING:0,this._flags|=a?GPUTextureUsage.RENDER_ATTACHMENT:0,this.usableAsStorage||(this.usableAsStorage=r),this.usableAsSampled||(this.usableAsSampled=n),this.usableAsRender||(this.usableAsRender=a),this}createView(t,r){if(t==="sampled")return this._asSampled(r);let n=r;switch(t){case"mutable":return this._asMutable(n);case"readonly":return this._asReadonly(n);case"writeonly":return this._asWriteonly(n)}}_asStorage(t,r){var o;if(!this.usableAsStorage)throw new Error("Unusable as storage");let n=(o=t==null?void 0:t.format)!=null?o:this.props.format,a=Me[n];return Ue(!!a,`Unsupported storage texture format: ${n}`),new er(t!=null?t:{},r,this)}_asReadonly(t){return this._asStorage(t,"readonly")}_asWriteonly(t){return this._asStorage(t,"writeonly")}_asMutable(t){return this._asStorage(t,"mutable")}_asSampled(t){var a;if(!this.usableAsSampled)throw new Error("Unusable as sampled");let r=(a=t==null?void 0:t.format)!=null?a:this.props.format;if(!Me[r])throw new Error(`Unsupported storage texture format: ${r}`);return new tr(t,this)}destroy(){var t;this._destroyed||(this._destroyed=!0,(t=this._texture)==null||t.destroy())}},gt={"1d":"1d","2d":"2d","2d-array":"2d_array",cube:"cube","cube-array":"cube_array","3d":"3d"},er=class{constructor(t,r,n){this.access=r;this._texture=n;p(this,"resourceType","texture-storage-view");p(this,"texelDataType");p(this,"dimension");p(this,"_view");p(this,"_format");var a,o,i;this.dimension=(o=(a=t==null?void 0:t.dimension)!=null?a:n.props.dimension)!=null?o:"2d",this._format=(i=t==null?void 0:t.format)!=null?i:n.props.format,this.texelDataType=Me[this._format]}get label(){return this._texture.label}$name(t){return this._texture.$name(t),this}unwrap(){var t;return this._view||(this._view=this._texture.unwrap().createView({label:`${(t=this.label)!=null?t:"<unnamed>"} - View`,format:this._format,dimension:this.dimension})),this._view}"~resolve"(t){let r=t.names.makeUnique(this.label),{group:n,binding:a}=t.allocateFixedEntry({storageTexture:this._format,access:this.access,viewDimension:this.dimension},this),o=`texture_storage_${gt[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${a}) var ${r}: ${o}<${this._format}, ${Fn[this.access]}>;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},mt=class{constructor(t,r,n,a){this._format=t;this.dimension=r;this.access=n;this._membership=a;p(this,"resourceType","texture-storage-view");p(this,"texelDataType");this.texelDataType=Me[this._format]}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout),a=`texture_storage_${gt[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: ${a}<${this._format}, ${Fn[this.access]}>;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},tr=class{constructor(t,r){this._props=t;this._texture=r;p(this,"resourceType","texture-sampled-view");p(this,"channelDataType");p(this,"dimension");p(this,"_format");p(this,"_view");var n,a,o;this.dimension=(a=(n=t==null?void 0:t.dimension)!=null?n:r.props.dimension)!=null?a:"2d",this._format=(o=t==null?void 0:t.format)!=null?o:r.props.format,this.channelDataType=bn[this._format]}get label(){return this._texture.label}$name(t){return this._texture.$name(t),this}unwrap(){var t;return this._view||(this._view=this._texture.unwrap().createView(A({label:`${(t=this.label)!=null?t:"<unnamed>"} - View`},this._props))),this._view}"~resolve"(t){var s;let r=t.names.makeUnique(this.label),n=((s=this._texture.props.sampleCount)!=null?s:1)>1,{group:a,binding:o}=t.allocateFixedEntry({texture:wn[this.channelDataType.type],viewDimension:this.dimension,multisampled:n},this),i=n?"texture_multisampled_2d":`texture_${gt[this.dimension]}`;return t.addDeclaration(`@group(${a}) @binding(${o}) var ${r}: ${i}<${t.resolve(this.channelDataType)}>;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}},ft=class{constructor(t,r,n,a){this.dimension=r;this._multisampled=n;this._membership=a;p(this,"resourceType","texture-sampled-view");p(this,"channelDataType");this.channelDataType=Sn[t]}get label(){return this._membership.key}"~resolve"(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout),a=this._multisampled?"texture_multisampled_2d":`texture_${gt[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: ${a}<${t.resolve(this.channelDataType)}>;`),r}toString(){var t;return`${this.resourceType}:${(t=this.label)!=null?t:"<unnamed>"}`}};function rr(e){return!!(e!=null&&e.usableAsSampled)}function fa(e){return!!(e!=null&&e.usableAsRender)}var Tt=class e extends Error{constructor(t){var r;super(`Resource '${(r=t.label)!=null?r:"<unnamed>"}' cannot be bound as 'sampled'. Use .$usage('sampled') to allow it.`),Object.setPrototypeOf(this,e.prototype)}};function ct(e){return new ar(e)}function yt(e){return!!e&&e.resourceType==="bind-group-layout"}function or(e){return!!e&&e.resourceType==="bind-group"}var nr=class e extends Error{constructor(t,r){super(`Bind group '${t!=null?t:"<unnamed>"}' is missing a required binding '${r}'`),Object.setPrototypeOf(this,e.prototype)}},vn=["compute"],Fe=["compute","vertex","fragment"],ar=class{constructor(t){this.entries=t;p(this,"_label");p(this,"_index");p(this,"resourceType","bind-group-layout");p(this,"bound",{});var n,a,o,i,s;let r=0;for(let[l,u]of Object.entries(t)){if(u===null){r++;continue}let m={idx:r,key:l,layout:this};if("uniform"in u&&(this.bound[l]=new Oe("uniform",u.uniform,m)),"storage"in u){let f="type"in u.storage?u.storage:u.storage(0);this.bound[l]=new Oe((n=u.access)!=null?n:"readonly",f,m)}"texture"in u&&(this.bound[l]=new ft(u.texture,(a=u.viewDimension)!=null?a:"2d",(o=u.multisampled)!=null?o:!1,m)),"storageTexture"in u&&(this.bound[l]=new mt(u.storageTexture,(i=u.viewDimension)!=null?i:"2d",(s=u.access)!=null?s:"writeonly",m)),"externalTexture"in u&&(this.bound[l]=new dt(m)),"sampler"in u&&(u.sampler==="comparison"?this.bound[l]=new lt(m):this.bound[l]=new pt(m)),r++}}get label(){return this._label}get index(){return this._index}$name(t){return this._label=t,this}$idx(t){return this._index=t,this}unwrap(t){var n;return t.device.createBindGroupLayout({label:(n=this.label)!=null?n:"<unnamed>",entries:Object.values(this.entries).map((a,o)=>{var l,u,m,f;if(a===null)return null;let i=a.visibility,s={binding:o,visibility:0};if("uniform"in a)i=i!=null?i:Fe,s.buffer={type:"uniform"};else if("storage"in a)i=i!=null?i:a.access==="mutable"?vn:Fe,s.buffer={type:a.access==="mutable"?"storage":"read-only-storage"};else if("sampler"in a)i=i!=null?i:Fe,s.sampler={type:a.sampler};else if("texture"in a)i=i!=null?i:Fe,s.texture={sampleType:a.texture,viewDimension:(l=a.viewDimension)!=null?l:"2d",multisampled:(u=a.multisampled)!=null?u:!1};else if("storageTexture"in a){let g=(m=a.access)!=null?m:"writeonly";i=i!=null?i:g==="readonly"?Fe:vn,s.storageTexture={format:a.storageTexture,access:{mutable:"read-write",readonly:"read-only",writeonly:"write-only"}[g],viewDimension:(f=a.viewDimension)!=null?f:"2d"}}else"externalTexture"in a&&(i=i!=null?i:Fe,s.externalTexture={});return i!=null&&i.includes("compute")&&(s.visibility|=GPUShaderStage.COMPUTE),i!=null&&i.includes("vertex")&&(s.visibility|=GPUShaderStage.VERTEX),i!=null&&i.includes("fragment")&&(s.visibility|=GPUShaderStage.FRAGMENT),s}).filter(a=>a!==null)})}populate(t){return new ge(this,t)}},ge=class{constructor(t,r){this.layout=t;this.entries=r;p(this,"resourceType","bind-group");for(let n of Object.keys(t.entries))if(t.entries[n]!==null&&!(n in r))throw new nr(t.label,n)}unwrap(t){var n;return t.device.createBindGroup({label:(n=this.layout.label)!=null?n:"<unnamed>",layout:t.unwrap(this.layout),entries:Object.entries(this.layout.entries).map(([a,o],i)=>{var l;if(o===null)return null;let s=this.entries[a];if(s===void 0)throw new Error(`'${a}' is a resource required to populate bind group layout '${(l=this.layout.label)!=null?l:"<unnamed>"}'.`);if("uniform"in o){let u;if(ae(s)){if(!st(s))throw new vt(s);u={buffer:t.unwrap(s)}}else u={buffer:s};return{binding:i,resource:u}}if("storage"in o){let u;if(ae(s)){if(!fe(s))throw new Ge(s);u={buffer:t.unwrap(s)}}else u={buffer:s};return{binding:i,resource:u}}if("texture"in o){let u;if(J(s)){if(!rr(s))throw new Tt(s);u=t.unwrap(s.createView("sampled"))}else ze(s)?u=t.unwrap(s):u=s;return{binding:i,resource:u}}if("storageTexture"in o){let u;if(J(s)){if(!fe(s))throw new Ge(s);o.access==="readonly"?u=t.unwrap(s.createView("readonly")):o.access==="mutable"?u=t.unwrap(s.createView("mutable")):u=t.unwrap(s.createView("writeonly"))}else Ne(s)?u=t.unwrap(s):u=s;return{binding:i,resource:u}}if("sampler"in o)return ke(s)||We(s)?{binding:i,resource:t.unwrap(s)}:{binding:i,resource:s};if("externalTexture"in o)return{binding:i,resource:s};throw new Error(`Malformed bind group entry: ${s} (${JSON.stringify(s)})`)}).filter(a=>a!==null)})}};var An="#CATCHALL#",sr=class{constructor(){p(this,"_stack",[]);p(this,"_itemDepth",0)}get itemDepth(){return this._itemDepth}get topItem(){let t=this._stack[this._stack.length-1];if(!t||t.type!=="item")throw new Error("Internal error, expected item layer to be on top.");return t}pushItem(){this._itemDepth++,this._stack.push({type:"item",usedSlots:new Set})}popItem(){this.pop("item")}pushSlotBindings(t){this._stack.push({type:"slotBinding",bindingMap:new WeakMap(t)})}popSlotBindings(){this.pop("slotBinding")}pushFunctionScope(t,r,n){this._stack.push({type:"functionScope",args:t,returnType:r,externalMap:n})}popFunctionScope(){this.pop("functionScope")}pushBlockScope(){this._stack.push({type:"blockScope",declarations:new Map})}popBlockScope(){this.pop("blockScope")}pop(t){let r=this._stack[this._stack.length-1];if(!r||t&&r.type!==t)throw new Error(`Internal error, expected a ${t} layer to be on top.`);this._stack.pop(),t==="item"&&this._itemDepth--}readSlot(t){for(let r=this._stack.length-1;r>=0;--r){let n=this._stack[r];if((n==null?void 0:n.type)==="item")n.usedSlots.add(t);else if((n==null?void 0:n.type)==="slotBinding"){let a=n.bindingMap.get(t);if(a!==void 0)return a}else if(!((n==null?void 0:n.type)==="functionScope"||(n==null?void 0:n.type)==="blockScope"))throw new Error("Unknown layer type.")}return t.defaultValue}getResourceById(t){for(let r=this._stack.length-1;r>=0;--r){let n=this._stack[r];if((n==null?void 0:n.type)==="functionScope"){let a=n.args.find(i=>i.value===t);if(a!==void 0)return a;let o=n.externalMap[t];return o!==void 0?{value:o,dataType:q(o)?we(o):P}:void 0}if((n==null?void 0:n.type)==="blockScope"){let a=n.declarations.get(t);if(a!==void 0)return{value:t,dataType:a}}}}defineBlockVariable(t,r){for(let n=this._stack.length-1;n>=0;--n){let a=this._stack[n];if((a==null?void 0:a.type)==="blockScope")return a.declarations.set(t,r),{value:t,dataType:r}}throw new Error("No block scope found to define a variable in.")}},xt=[""," "," "," "," "," "," "," "," "],ir=xt.length-1,ur=class{constructor(){p(this,"identLevel",0)}get pre(){var t;return(t=xt[this.identLevel])!=null?t:xt[ir].repeat(this.identLevel/ir)+xt[this.identLevel%ir]}indent(){let t=this.pre;return this.identLevel++,t}dedent(){return this.identLevel--,this.pre}},Un;Un=b;var pr=class{constructor(t){p(this,"_memoizedResolves",new WeakMap);p(this,"_memoizedDerived",new WeakMap);p(this,"_indentController",new ur);p(this,"_jitTranspiler");p(this,"_itemStateStack",new sr);p(this,"_declarations",[]);p(this,Un,{itemStateStack:this._itemStateStack});p(this,"bindGroupLayoutsToPlaceholderMap",new Map);p(this,"_nextFreeLayoutPlaceholderIdx",0);p(this,"fixedBindings",[]);p(this,"callStack",[]);p(this,"names");this.names=t.names,this._jitTranspiler=t.jitTranspiler}get pre(){return this._indentController.pre}indent(){return this._indentController.indent()}dedent(){return this._indentController.dedent()}getById(t){let r=this._itemStateStack.getResourceById(t);return r===void 0?null:r}defineVariable(t,r){return this._itemStateStack.defineBlockVariable(t,r)}pushBlockScope(){this._itemStateStack.pushBlockScope()}popBlockScope(){this._itemStateStack.popBlockScope()}transpileFn(t){if(!this._jitTranspiler)throw new Error("Tried to execute a tgpu.fn function without providing a JIT transpiler, or transpiling at build time.");return this._jitTranspiler.transpileFn(t)}fnToWgsl(t){this._itemStateStack.pushFunctionScope(t.args,t.returnType,t.externalMap);try{let r=kt(this,t.body),n=t.args.map(a=>`${a.value}: ${this.resolve(a.dataType)}`).join(", ");return{head:t.returnType!==void 0?`(${n}) -> ${tt(t.returnType)} ${this.resolve(t.returnType)}`:`(${n})`,body:r}}finally{this._itemStateStack.popFunctionScope()}}addDeclaration(t){this._declarations.push(t)}allocateLayoutEntry(t){let r=this.bindGroupLayoutsToPlaceholderMap,n=r.get(t);return n||(n=`#BIND_GROUP_LAYOUT_${this._nextFreeLayoutPlaceholderIdx++}#`,r.set(t,n)),n}allocateFixedEntry(t,r){let n=this.fixedBindings.length;return this.fixedBindings.push({layoutEntry:t,resource:r}),{group:An,binding:n}}readSlot(t){let r=this._itemStateStack.readSlot(t);if(r===void 0)throw new Ft(t);return r}withSlots(t,r){this._itemStateStack.pushSlotBindings(t);try{return r()}finally{this._itemStateStack.popSlotBindings()}}unwrap(t){if(Rt(t))return this.withSlots(t["~providing"].pairs,()=>this.unwrap(t["~providing"].inner));let r=t;for(;;)if(j(r))r=this.readSlot(r);else if(K(r))r=this._getOrCompute(r);else break;return r}_getOrCompute(t){var n;let r=(n=this._memoizedDerived.get(t))!=null?n:[];this._itemStateStack.pushItem();try{for(let i of r)if([...i.slotToValueMap.entries()].every(([l,u])=>l.areEqual(this._itemStateStack.readSlot(l),u)))return i.result;At(Ie.CPU);let a;try{a=t["~compute"]()}finally{It(Ie.CPU)}let o=new Map;for(let i of this._itemStateStack.topItem.usedSlots)o.set(i,this._itemStateStack.readSlot(i));return r.push({slotToValueMap:o,result:a}),this._memoizedDerived.set(t,r),a}catch(a){throw a instanceof ce?a.appendToTrace(t):new ce(a,[t])}finally{this._itemStateStack.popItem()}}_getOrInstantiate(t){var n;let r=(n=this._memoizedResolves.get(t))!=null?n:[];this._itemStateStack.pushItem();try{for(let i of r)if([...i.slotToValueMap.entries()].every(([l,u])=>l.areEqual(this._itemStateStack.readSlot(l),u)))return i.result;let a;k(t)?a=$t(this,t):K(t)||j(t)?a=this.resolve(this.unwrap(t)):Pr(t)?a=t["~resolve"](this):a=this.resolveValue(t);let o=new Map;for(let i of this._itemStateStack.topItem.usedSlots)o.set(i,this._itemStateStack.readSlot(i));return r.push({slotToValueMap:o,result:a}),this._memoizedResolves.set(t,r),a}catch(a){throw a instanceof ce?a.appendToTrace(t):new ce(a,[t])}finally{this._itemStateStack.popItem()}}resolve(t){if(Rt(t))return this.withSlots(t["~providing"].pairs,()=>this.resolve(t["~providing"].inner));if(t&&typeof t=="object"||typeof t=="function"){if(this._itemStateStack.itemDepth===0)try{At(Ie.GPU);let r=Dr(this,()=>this._getOrInstantiate(t));return`${[...this._declarations].join(`
|
24
24
|
|
25
|
-
`)}${r}`}finally{It(Ie.GPU)}return this._getOrInstantiate(t)}return String(t)}resolveValue(t,r){if(q(t))return this.resolve(t);if(r&&Q(r))return`array(${t.map(n=>this.resolveValue(n,r.elementType))})`;if(Array.isArray(t))return`array(${t.map(n=>this.resolveValue(n))})`;if(r&&_(r))return`${this.resolve(r)}(${Object.entries(r.propTypes).map(([n,a])=>this.resolveValue(t[n],a))})`;throw new Error(`Value ${t} (as json: ${JSON.stringify(t)}) of schema ${r} is not resolvable to WGSL`)}};function ve(e,t){var f;let r=new pr(t),n=r.resolve(e),a=r.bindGroupLayoutsToPlaceholderMap,o=[],i=new Set([...a.keys()].map(g=>g.index).filter(g=>g!==void 0)),s=Fr(i),l=r.fixedBindings.map((g,w)=>[String(w),g.layoutEntry]),u=()=>{let g=s.next().value,w=ct(Object.fromEntries(l));return o[g]=w,n=n.replaceAll(Un,String(g)),[g,new ge(w,Object.fromEntries(r.fixedBindings.map((y,T)=>[String(T),y.resource])))]},m=l.length>0?u():null;for(let[g,w]of a.entries()){let y=(f=g.index)!=null?f:s.next().value;o[y]=g,n=n.replaceAll(w,String(y))}return{code:n,bindGroupLayouts:o,catchall:m}}function An(e){let{externals:t,template:r,names:n,unstable_jitTranspiler:a}=e,o={};pe(o,t!=null?t:{});let i={"~resolve"(l){return le(l,o,r!=null?r:"")},toString:()=>"<root>"},{code:s}=ve(i,{names:n==="strict"?new re:new te,jitTranspiler:a});return s}var je=class{constructor(t){this._make=t;p(this,"_map",new WeakMap)}getOrMake(t,...r){if(this._map.has(t))return this._map.get(t);let n=this._make(t,...r);return this._map.set(t,n),n}};function In(e,t,r){return new lr(new dr(e,t,r),{})}function Rn(e){return(e==null?void 0:e.resourceType)==="compute-pipeline"}var lr=class e{constructor(t,r){this._core=t;this._priors=r;p(this,"resourceType","compute-pipeline")}get label(){return this._core.label}get rawPipeline(){return this._core.unwrap().pipeline}with(t,r){var n;return new e(this._core,{bindGroupLayoutMap:new Map([...(n=this._priors.bindGroupLayoutMap)!=null?n:[],[t,r]])})}dispatchWorkgroups(t,r,n){let a=this._core.unwrap(),{branch:o,label:i}=this._core,s=o.commandEncoder.beginComputePass({label:i!=null?i:"<unnamed>"});s.setPipeline(a.pipeline);let l=new Set(a.bindGroupLayouts);if(a.bindGroupLayouts.forEach((u,m)=>{var f;if(a.catchall&&m===a.catchall[0])s.setBindGroup(m,o.unwrap(a.catchall[1])),l.delete(u);else{let g=(f=this._priors.bindGroupLayoutMap)==null?void 0:f.get(u);g!==void 0&&(l.delete(u),s.setBindGroup(m,o.unwrap(g)))}}),l.size>0)throw new oe(l);s.dispatchWorkgroups(t,r,n),s.end()}$name(t){return this._core.label=t,this}},dr=class{constructor(t,r,n){this.branch=t;this._slotBindings=r;this._entryFn=n;p(this,"label");p(this,"_memo")}unwrap(){var t,r,n,a,o;if(this._memo===void 0){let i=this.branch.device,{code:s,bindGroupLayouts:l,catchall:u}=ve({"~resolve":m=>(m.withSlots(this._slotBindings,()=>{m.resolve(this._entryFn)}),""),toString:()=>{var m;return`computePipeline:${(m=this.label)!=null?m:"<unnamed>"}`}},{names:this.branch.nameRegistry,jitTranspiler:this.branch.jitTranspiler});u!==null&&((r=l[u[0]])==null||r.$name(`${(t=this.label)!=null?t:"<unnamed>"} - Automatic Bind Group & Layout`)),this._memo={pipeline:i.createComputePipeline({label:(n=this.label)!=null?n:"<unnamed>",layout:i.createPipelineLayout({label:`${(a=this.label)!=null?a:"<unnamed>"} - Pipeline Layout`,bindGroupLayouts:l.map(m=>this.branch.unwrap(m))}),compute:{module:i.createShaderModule({label:`${(o=this.label)!=null?o:"<unnamed>"} - Shader`,code:s})}}),bindGroupLayouts:l,catchall:u}}return this._memo}};function gr(e,t="vertex"){return new fr(e,t)}function bt(e){return(e==null?void 0:e.resourceType)==="vertex-layout"}var mr=Symbol("defaultAttribEntry");function ht(e,t,r,n,a){if(Ye(t)||Vr(t)){let o=Z(t);return o!==void 0&&(n[a!=null?a:mr]=o),ht(e,t.inner,$(r,W(t)),n)}if(_(t)){let o=r;return Object.fromEntries(Object.entries(t.propTypes).map(([i,s])=>{o=$(o,v(s));let l=[i,ht(e,s,o,n,i)];return o+=h(s),l}))}if(X(t)){let o=r;return Object.fromEntries(Object.entries(t.propTypes).map(([i,s])=>{o=$(o,W(s));let l=[i,ht(e,s,o,n,i)];return o+=h(s),l}))}if("type"in t&&typeof t.type=="string"){if(Cr.includes(t.type))return{_layout:e,format:t.type,offset:r};let o=Er[t.type];if(o)return{_layout:e,format:o,offset:r}}throw new Error(`Unsupported data used in vertex layout: ${String(t)}`)}var fr=class{constructor(t,r){this.schemaForCount=t;this.stepMode=r;p(this,"resourceType","vertex-layout");p(this,"stride");p(this,"attrib");p(this,"_customLocationMap",{});p(this,"_label");let n=t(0);this.stride=$(h(n.elementType),v(n)),this.attrib=ht(this,n.elementType,0,this._customLocationMap)}get label(){return this._label}get vertexLayout(){if(this._customLocationMap[mr]!==void 0){if(typeof this.attrib.format!="string"||typeof this.attrib.offset!="number")throw new Error("Single attribute vertex layouts must have a format and offset.");return{arrayStride:this.stride,stepMode:this.stepMode,attributes:[{format:this.attrib.format,offset:this.attrib.offset,shaderLocation:this._customLocationMap[mr]}]}}if(!Object.keys(this.attrib).every(r=>this._customLocationMap[r]!==void 0))throw new Error("All attributes must have custom locations in order to unwrap a vertex layout.");return{arrayStride:this.stride,stepMode:this.stepMode,attributes:[...Object.entries(this.attrib).map(([r,n])=>({format:n.format,offset:n.offset,shaderLocation:this._customLocationMap[r]}))]}}$name(t){return this._label=t,this}};function fa(e){return typeof(e==null?void 0:e.loadOp)=="string"}function Bn(e,t){if(k(e)){if(!fa(t))throw new Error("Expected a single color attachment, not a record.");return[t]}let r=[];for(let n of Object.keys(e)){let a=t[n];if(!a)throw new Error(`A color attachment by the name of '${n}' was not provided to the shader.`);r.push(a)}return r}function ga(e){return typeof(e==null?void 0:e.format)=="string"}function _n(e,t){if(k(e)){if(!ga(t))throw new Error("Expected a single color target configuration, not a record.");return[t]}let r=[];for(let n of Object.keys(e)){let a=t[n];if(!a)throw new Error(`A color target by the name of '${n}' was not provided to the shader.`);r.push(a)}return r}function Pn(e){return new Tr(new cr(e),{})}function Ln(e){return(e==null?void 0:e.resourceType)==="render-pipeline"}var Tr=class e{constructor(t,r){this.core=t;this.priors=r;p(this,"resourceType","render-pipeline")}get label(){return this.core.label}$name(t){return this.core.label=t,this}with(t,r){var n,a;if(yt(t))return new e(this.core,L(A({},this.priors),{bindGroupLayoutMap:new Map([...(n=this.priors.bindGroupLayoutMap)!=null?n:[],[t,r]])}));if(bt(t))return new e(this.core,L(A({},this.priors),{vertexLayoutMap:new Map([...(a=this.priors.vertexLayoutMap)!=null?a:[],[t,r]])}));throw new Error("Unsupported value passed into .with()")}withColorAttachment(t){return new e(this.core,L(A({},this.priors),{colorAttachment:t}))}withDepthStencilAttachment(t){return new e(this.core,L(A({},this.priors),{depthStencilAttachment:t}))}draw(t,r,n,a){var y;let o=this.core.unwrap(),{branch:i,fragmentFn:s}=this.core.options,u={colorAttachments:Bn(s.shell.targets,(y=this.priors.colorAttachment)!=null?y:{}).map(T=>J(T.view)?L(A({},T),{view:i.unwrap(T.view).createView()}):T)};if(this.core.label!==void 0&&(u.label=this.core.label),this.priors.depthStencilAttachment!==void 0){let T=this.priors.depthStencilAttachment;J(T.view)?u.depthStencilAttachment=L(A({},T),{view:i.unwrap(T.view).createView()}):u.depthStencilAttachment=T}let m=i.commandEncoder.beginRenderPass(u);m.setPipeline(o.pipeline);let f=new Set(o.bindGroupLayouts);o.bindGroupLayouts.forEach((T,F)=>{var c;if(o.catchall&&F===o.catchall[0])m.setBindGroup(F,i.unwrap(o.catchall[1])),f.delete(T);else{let x=(c=this.priors.bindGroupLayoutMap)==null?void 0:c.get(T);x!==void 0&&(f.delete(T),m.setBindGroup(F,i.unwrap(x)))}});let g=new Set(this.core.usedVertexLayouts);if(this.core.usedVertexLayouts.forEach((T,F)=>{var x;let c=(x=this.priors.vertexLayoutMap)==null?void 0:x.get(T);c&&(g.delete(T),m.setVertexBuffer(F,i.unwrap(c)))}),f.size>0)throw new oe(f);if(g.size>0)throw new Ae(g);m.draw(t,r,n,a),m.end(),i.flush()}},cr=class{constructor(t){this.options=t;p(this,"label");p(this,"usedVertexLayouts");p(this,"_memo");p(this,"_vertexBufferLayouts");p(this,"_targets");let r=rn(t.vertexFn.shell.attributes[0],t.vertexAttribs);this._vertexBufferLayouts=r.bufferDefinitions,this.usedVertexLayouts=r.usedVertexLayouts,this._targets=_n(t.fragmentFn.shell.targets,t.targets)}unwrap(){var t,r,n,a;if(this._memo===void 0){let{branch:o,vertexFn:i,fragmentFn:s,slotBindings:l,primitiveState:u,depthStencilState:m,multisampleState:f}=this.options,{code:g,bindGroupLayouts:w,catchall:y}=ve({"~resolve":x=>(x.withSlots(l,()=>{x.resolve(i),x.resolve(s)}),""),toString:()=>{var x;return`renderPipeline:${(x=this.label)!=null?x:"<unnamed>"}`}},{names:o.nameRegistry,jitTranspiler:o.jitTranspiler});y!==null&&((r=w[y[0]])==null||r.$name(`${(t=this.label)!=null?t:"<unnamed>"} - Automatic Bind Group & Layout`));let T=o.device,F=T.createShaderModule({label:`${(n=this.label)!=null?n:"<unnamed>"} - Shader`,code:g}),c={layout:T.createPipelineLayout({label:`${(a=this.label)!=null?a:"<unnamed>"} - Pipeline Layout`,bindGroupLayouts:w.map(x=>o.unwrap(x))}),vertex:{module:F,buffers:this._vertexBufferLayouts},fragment:{module:F,targets:this._targets}};this.label!==void 0&&(c.label=this.label),u&&(c.primitive=u),m&&(c.depthStencil=m),f&&(c.multisample=f),this._memo={pipeline:T.createRenderPipeline(c),bindGroupLayouts:w,catchall:y}}return this._memo}};var yr=class e{constructor(t,r){this._getRoot=t;this._slotBindings=r}with(t,r){return new e(this._getRoot,[...this._slotBindings,[se(t)?t.slot:t,r]])}withCompute(t){return new xr(this._getRoot(),this._slotBindings,t)}withVertex(t,r){return new hr({branch:this._getRoot(),primitiveState:void 0,depthStencilState:void 0,slotBindings:this._slotBindings,vertexFn:t,vertexAttribs:r,multisampleState:void 0})}},xr=class{constructor(t,r,n){this._root=t;this._slotBindings=r;this._entryFn=n}createPipeline(){return In(this._root,this._slotBindings,this._entryFn)}},hr=class{constructor(t){this._options=t}withFragment(t,r,n){return Ue(typeof t!="string","Just type mismatch validation"),Ue(typeof r!="string","Just type mismatch validation"),new br(L(A({},this._options),{fragmentFn:t,targets:r}))}},br=class e{constructor(t){this._options=t}withPrimitive(t){return new e(L(A({},this._options),{primitiveState:t}))}withDepthStencil(t){return new e(L(A({},this._options),{depthStencilState:t}))}withMultisample(t){return new e(L(A({},this._options),{multisampleState:t}))}createPipeline(){return Pn(this._options)}},wt=class extends yr{constructor(r,n,a,o){super(()=>this,[]);this.device=r;this.nameRegistry=n;this.jitTranspiler=a;this._ownDevice=o;p(this,"~unstable");p(this,"_disposables",[]);p(this,"_unwrappedBindGroupLayouts",new je(r=>r.unwrap(this)));p(this,"_unwrappedBindGroups",new je(r=>r.unwrap(this)));p(this,"_commandEncoder",null);this["~unstable"]=this}get commandEncoder(){return this._commandEncoder||(this._commandEncoder=this.device.createCommandEncoder()),this._commandEncoder}createBuffer(r,n){let a=cn(this,r,n);return this._disposables.push(a),a}createUniform(r,n){return this.createBuffer(r,n).$usage("uniform").as("uniform")}createMutable(r,n){return this.createBuffer(r,n).$usage("storage").as("mutable")}createReadonly(r,n){return this.createBuffer(r,n).$usage("storage").as("readonly")}createBindGroup(r,n){return new ge(r,n)}destroy(){for(let r of this._disposables)r.destroy();this._ownDevice&&this.device.destroy()}createTexture(r){let n=Sn(r,this);return this._disposables.push(n),n}unwrap(r){if(Rn(r))return r.rawPipeline;if(Ln(r))return r.core.unwrap().pipeline;if(yt(r))return this._unwrappedBindGroupLayouts.getOrMake(r);if(or(r))return this._unwrappedBindGroups.getOrMake(r);if(ae(r))return r.buffer;if(J(r)||Ne(r)||ze(r))return r.unwrap();if(bt(r))return r.vertexLayout;if(ke(r)){if("unwrap"in r)return r.unwrap(this);throw new Error("Cannot unwrap laid-out sampler.")}if(We(r)){if("unwrap"in r)return r.unwrap(this);throw new Error("Cannot unwrap laid-out comparison sampler.")}throw new Error(`Unknown resource type: ${r}`)}beginRenderPass(r,n){let a=this.commandEncoder.beginRenderPass(r),o=new Map,i=new Map,s,l=()=>{if(!s)throw new Error("Cannot draw without a call to pass.setPipeline");let{core:u,priors:m}=s,f=u.unwrap();a.setPipeline(f.pipeline);let g=new Set(f.bindGroupLayouts);f.bindGroupLayouts.forEach((y,T)=>{var F,c;if(f.catchall&&T===f.catchall[0])a.setBindGroup(T,this.unwrap(f.catchall[1])),g.delete(y);else{let x=(c=(F=m.bindGroupLayoutMap)==null?void 0:F.get(y))!=null?c:o.get(y);x!==void 0&&(g.delete(y),or(x)?a.setBindGroup(T,this.unwrap(x)):a.setBindGroup(T,x))}});let w=new Set;if(u.usedVertexLayouts.forEach((y,T)=>{var x;let F=(x=m.vertexLayoutMap)==null?void 0:x.get(y),c=F?{buffer:F,offset:void 0,size:void 0}:i.get(y);!c||!c.buffer?w.add(y):ae(c.buffer)?a.setVertexBuffer(T,this.unwrap(c.buffer),c.offset,c.size):a.setVertexBuffer(T,c.buffer,c.offset,c.size)}),g.size>0)throw new oe(g);if(w.size>0)throw new Ae(w)};n({setViewport(...u){a.setViewport(...u)},setScissorRect(...u){a.setScissorRect(...u)},setBlendConstant(...u){a.setBlendConstant(...u)},setStencilReference(...u){a.setStencilReference(...u)},beginOcclusionQuery(...u){a.beginOcclusionQuery(...u)},endOcclusionQuery(...u){a.endOcclusionQuery(...u)},executeBundles(...u){a.executeBundles(...u)},setPipeline(u){s=u},setIndexBuffer:(u,m,f,g)=>{ae(u)?a.setIndexBuffer(this.unwrap(u),m,f,g):a.setIndexBuffer(u,m,f,g)},setVertexBuffer(u,m,f,g){i.set(u,{buffer:m,offset:f,size:g})},setBindGroup(u,m){o.set(u,m)},draw(u,m,f,g){l(),a.draw(u,m,f,g)},drawIndexed(...u){l(),a.drawIndexed(...u)},drawIndirect(...u){l(),a.drawIndirect(...u)},drawIndexedIndirect(...u){l(),a.drawIndexedIndirect(...u)}}),a.end()}flush(){this._commandEncoder&&(this.device.queue.submit([this._commandEncoder.finish()]),this._commandEncoder=null)}};async function Cn(e){let{adapter:t,device:r,unstable_names:n="random",unstable_jitTranspiler:a}=e!=null?e:{};if(!navigator.gpu)throw new Error("WebGPU is not supported by this browser.");let o=await navigator.gpu.requestAdapter(t);if(!o)throw new Error("Could not find a compatible GPU");return new wt(await o.requestDevice(r),n==="random"?new te:new re,a,!0)}function En(e){let{device:t,unstable_names:r="random",unstable_jitTranspiler:n}=e!=null?e:{};return new wt(t,r==="random"?new te:new re,n,!1)}function St(e){return new wr(e)}var wr=class{constructor(t=void 0){this.defaultValue=t;p(this,"resourceType","slot");p(this,"label");p(this,"~repr")}$name(t){return this.label=t,this}areEqual(t,r){return Object.is(t,r)}toString(){var t;return`slot:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){let t=z();if(!t)throw new Error("Cannot access tgpu.slot's value outside of resolution.");return Ee(t.unwrap(this))}};function Vn(e,t){return new Sr(e,t)}var Sr=class{constructor(t,r=void 0){this.schema=t;this.defaultValue=r;p(this,"resourceType","accessor");p(this,"~repr");p(this,"label");p(this,"slot");this.slot=St(r)}$name(t){return this.label=t,this.slot.$name(t),this}toString(){var t;return`accessor:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){if(!z())throw new Error("Cannot access tgpu.accessor's value outside of resolution.");return new Proxy({"~resolve":r=>r.resolve(this),toString:()=>{var r;return`.value:${(r=this.label)!=null?r:"<unnamed>"}`},[b]:{dataType:this.schema}},M)}"~resolve"(t){let r=t.unwrap(this.slot);return Qe(r)?t.resolve(r):Vt(r)?`${t.resolve(r)}()`:t.resolveValue(r,this.schema)}};function Gn(e){return ca(e)}function Ta([e,t]){var r;return`${(r=e.label)!=null?r:"<unnamed>"}=${t}`}function ca(e){return{resourceType:"derived","~compute":e,"~repr":void 0,get value(){let r=z();if(!r)throw new Error("Cannot access tgpu.derived's value outside of resolution.");return Ee(r.unwrap(this))},with(r,n){return $n(this,[[r,n]])},toString(){return"derived"}}}function $n(e,t){return{resourceType:"derived","~repr":void 0,"~compute"(){throw new Error("'~compute' should never be read on bound derived items.")},"~providing":{inner:e,pairs:t},get value(){let n=z();if(!n)throw new Error("Cannot access tgpu.derived's value outside of resolution.");return Ee(n.unwrap(this))},with(n,a){return $n(e,[...t,[n,a]])},toString(){return`derived[${t.map(Ta).join(", ")}]`}}}function On(e,t){return new Dt("private",e,t)}function kn(e){return new Dt("workgroup",e)}var Dt=class{constructor(t,r,n){this.scope=t;this._dataType=r;this._initialValue=n;p(this,"_label")}$name(t){return this._label=t,this}"~resolve"(t){let r=t.names.makeUnique(this._label);return this._initialValue?t.addDeclaration(`var<${this.scope}> ${r}: ${t.resolve(this._dataType)} = ${t.resolveValue(this._initialValue,this._dataType)};`):t.addDeclaration(`var<${this.scope}> ${r}: ${t.resolve(this._dataType)};`),r}get label(){return this._label}toString(){var t;return`var:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){if(!ie())throw new Error("Cannot access tgpu.var's value directly in JS.");return new Proxy({"~resolve":t=>t.resolve(this),toString:()=>{var t;return`.value:${(t=this.label)!=null?t:"<unnamed>"}`},[b]:{dataType:this._dataType}},M)}};var Wn={bindGroupLayout:ct,vertexLayout:gr,init:Cn,initFromDevice:En,resolve:An,"~unstable":{fn:Qr,fragmentFn:Zr,vertexFn:en,computeFn:Yr,vertexLayout:gr,derived:Gn,slot:St,accessor:Vn,privateVar:On,workgroupVar:kn,const:Nr,declare:jr,sampler:yn,comparisonSampler:xn}},Nu=Wn;Object.assign(Wn,{__assignAst:Jr});export{oe as MissingBindGroupsError,Ut as MissingLinksError,Ft as MissingSlotValueError,Ae as MissingVertexBuffersError,vt as NotUniformError,te as RandomNameRegistry,ce as ResolutionError,re as StrictNameRegistry,Nu as default,ae as isBuffer,We as isComparisonSampler,K as isDerived,ze as isSampledTextureView,ke as isSampler,j as isSlot,Ne as isStorageTextureView,J as isTexture,Vt as isTgpuFn,ma as isUsableAsRender,rr as isUsableAsSampled,fe as isUsableAsStorage,st as isUsableAsUniform,da as isUsableAsVertex,Wn as tgpu,jt as unstable_asMutable,Kt as unstable_asReadonly,qt as unstable_asUniform};
|
25
|
+
`)}${r}`}finally{It(Ie.GPU)}return this._getOrInstantiate(t)}return String(t)}resolveValue(t,r){if(q(t))return this.resolve(t);if(r&&Q(r))return`array(${t.map(n=>this.resolveValue(n,r.elementType))})`;if(Array.isArray(t))return`array(${t.map(n=>this.resolveValue(n))})`;if(r&&_(r))return`${this.resolve(r)}(${Object.entries(r.propTypes).map(([n,a])=>this.resolveValue(t[n],a))})`;throw new Error(`Value ${t} (as json: ${JSON.stringify(t)}) of schema ${r} is not resolvable to WGSL`)}};function ve(e,t){var f;let r=new pr(t),n=r.resolve(e),a=r.bindGroupLayoutsToPlaceholderMap,o=[],i=new Set([...a.keys()].map(g=>g.index).filter(g=>g!==void 0)),s=Fr(i),l=r.fixedBindings.map((g,w)=>[String(w),g.layoutEntry]),u=()=>{let g=s.next().value,w=ct(Object.fromEntries(l));return o[g]=w,n=n.replaceAll(An,String(g)),[g,new ge(w,Object.fromEntries(r.fixedBindings.map((y,T)=>[String(T),y.resource])))]},m=l.length>0?u():null;for(let[g,w]of a.entries()){let y=(f=g.index)!=null?f:s.next().value;o[y]=g,n=n.replaceAll(w,String(y))}return{code:n,bindGroupLayouts:o,catchall:m}}function In(e){let{externals:t,template:r,names:n,unstable_jitTranspiler:a}=e,o={};pe(o,t!=null?t:{});let i={"~resolve"(l){return le(l,o,r!=null?r:"")},toString:()=>"<root>"},{code:s}=ve(i,{names:n==="strict"?new re:new te,jitTranspiler:a});return s}var je=class{constructor(t){this._make=t;p(this,"_map",new WeakMap)}getOrMake(t,...r){if(this._map.has(t))return this._map.get(t);let n=this._make(t,...r);return this._map.set(t,n),n}};function Rn(e,t,r){return new lr(new dr(e,t,r),{})}function Bn(e){return(e==null?void 0:e.resourceType)==="compute-pipeline"}var lr=class e{constructor(t,r){this._core=t;this._priors=r;p(this,"resourceType","compute-pipeline")}get label(){return this._core.label}get rawPipeline(){return this._core.unwrap().pipeline}with(t,r){var n;return new e(this._core,{bindGroupLayoutMap:new Map([...(n=this._priors.bindGroupLayoutMap)!=null?n:[],[t,r]])})}dispatchWorkgroups(t,r,n){let a=this._core.unwrap(),{branch:o,label:i}=this._core,s=o.commandEncoder.beginComputePass({label:i!=null?i:"<unnamed>"});s.setPipeline(a.pipeline);let l=new Set(a.bindGroupLayouts);if(a.bindGroupLayouts.forEach((u,m)=>{var f;if(a.catchall&&m===a.catchall[0])s.setBindGroup(m,o.unwrap(a.catchall[1])),l.delete(u);else{let g=(f=this._priors.bindGroupLayoutMap)==null?void 0:f.get(u);g!==void 0&&(l.delete(u),s.setBindGroup(m,o.unwrap(g)))}}),l.size>0)throw new oe(l);s.dispatchWorkgroups(t,r,n),s.end()}$name(t){return this._core.label=t,this}},dr=class{constructor(t,r,n){this.branch=t;this._slotBindings=r;this._entryFn=n;p(this,"label");p(this,"_memo")}unwrap(){var t,r,n,a,o;if(this._memo===void 0){let i=this.branch.device,{code:s,bindGroupLayouts:l,catchall:u}=ve({"~resolve":m=>(m.withSlots(this._slotBindings,()=>{m.resolve(this._entryFn)}),""),toString:()=>{var m;return`computePipeline:${(m=this.label)!=null?m:"<unnamed>"}`}},{names:this.branch.nameRegistry,jitTranspiler:this.branch.jitTranspiler});u!==null&&((r=l[u[0]])==null||r.$name(`${(t=this.label)!=null?t:"<unnamed>"} - Automatic Bind Group & Layout`)),this._memo={pipeline:i.createComputePipeline({label:(n=this.label)!=null?n:"<unnamed>",layout:i.createPipelineLayout({label:`${(a=this.label)!=null?a:"<unnamed>"} - Pipeline Layout`,bindGroupLayouts:l.map(m=>this.branch.unwrap(m))}),compute:{module:i.createShaderModule({label:`${(o=this.label)!=null?o:"<unnamed>"} - Shader`,code:s})}}),bindGroupLayouts:l,catchall:u}}return this._memo}};function gr(e,t="vertex"){return new fr(e,t)}function bt(e){return(e==null?void 0:e.resourceType)==="vertex-layout"}var mr=Symbol("defaultAttribEntry");function ht(e,t,r,n,a){if(Ye(t)||Gr(t)){let o=Z(t);return o!==void 0&&(n[a!=null?a:mr]=o),ht(e,t.inner,$(r,W(t)),n)}if(_(t)){let o=r;return Object.fromEntries(Object.entries(t.propTypes).map(([i,s])=>{o=$(o,v(s));let l=[i,ht(e,s,o,n,i)];return o+=h(s),l}))}if(X(t)){let o=r;return Object.fromEntries(Object.entries(t.propTypes).map(([i,s])=>{o=$(o,W(s));let l=[i,ht(e,s,o,n,i)];return o+=h(s),l}))}if("type"in t&&typeof t.type=="string"){if(Er.includes(t.type))return{_layout:e,format:t.type,offset:r};let o=Vr[t.type];if(o)return{_layout:e,format:o,offset:r}}throw new Error(`Unsupported data used in vertex layout: ${String(t)}`)}var fr=class{constructor(t,r){this.schemaForCount=t;this.stepMode=r;p(this,"resourceType","vertex-layout");p(this,"stride");p(this,"attrib");p(this,"_customLocationMap",{});p(this,"_label");let n=t(0);this.stride=$(h(n.elementType),v(n)),this.attrib=ht(this,n.elementType,0,this._customLocationMap)}get label(){return this._label}get vertexLayout(){if(this._customLocationMap[mr]!==void 0){if(typeof this.attrib.format!="string"||typeof this.attrib.offset!="number")throw new Error("Single attribute vertex layouts must have a format and offset.");return{arrayStride:this.stride,stepMode:this.stepMode,attributes:[{format:this.attrib.format,offset:this.attrib.offset,shaderLocation:this._customLocationMap[mr]}]}}if(!Object.keys(this.attrib).every(r=>this._customLocationMap[r]!==void 0))throw new Error("All attributes must have custom locations in order to unwrap a vertex layout.");return{arrayStride:this.stride,stepMode:this.stepMode,attributes:[...Object.entries(this.attrib).map(([r,n])=>({format:n.format,offset:n.offset,shaderLocation:this._customLocationMap[r]}))]}}$name(t){return this._label=t,this}};function ga(e){return typeof(e==null?void 0:e.loadOp)=="string"}function _n(e,t){if(k(e)){if(!ga(t))throw new Error("Expected a single color attachment, not a record.");return[t]}let r=[];for(let n of Object.keys(e)){let a=t[n];if(!a)throw new Error(`A color attachment by the name of '${n}' was not provided to the shader.`);r.push(a)}return r}function Ta(e){return typeof(e==null?void 0:e.format)=="string"}function Pn(e,t){if(k(e)){if(!Ta(t))throw new Error("Expected a single color target configuration, not a record.");return[t]}let r=[];for(let n of Object.keys(e)){let a=t[n];if(!a)throw new Error(`A color target by the name of '${n}' was not provided to the shader.`);r.push(a)}return r}function Ln(e){return new Tr(new cr(e),{})}function Cn(e){return(e==null?void 0:e.resourceType)==="render-pipeline"}var Tr=class e{constructor(t,r){this.core=t;this.priors=r;p(this,"resourceType","render-pipeline")}get label(){return this.core.label}$name(t){return this.core.label=t,this}with(t,r){var n,a;if(yt(t))return new e(this.core,L(A({},this.priors),{bindGroupLayoutMap:new Map([...(n=this.priors.bindGroupLayoutMap)!=null?n:[],[t,r]])}));if(bt(t))return new e(this.core,L(A({},this.priors),{vertexLayoutMap:new Map([...(a=this.priors.vertexLayoutMap)!=null?a:[],[t,r]])}));throw new Error("Unsupported value passed into .with()")}withColorAttachment(t){return new e(this.core,L(A({},this.priors),{colorAttachment:t}))}withDepthStencilAttachment(t){return new e(this.core,L(A({},this.priors),{depthStencilAttachment:t}))}draw(t,r,n,a){var y;let o=this.core.unwrap(),{branch:i,fragmentFn:s}=this.core.options,u={colorAttachments:_n(s.shell.targets,(y=this.priors.colorAttachment)!=null?y:{}).map(T=>J(T.view)?L(A({},T),{view:i.unwrap(T.view).createView()}):T)};if(this.core.label!==void 0&&(u.label=this.core.label),this.priors.depthStencilAttachment!==void 0){let T=this.priors.depthStencilAttachment;J(T.view)?u.depthStencilAttachment=L(A({},T),{view:i.unwrap(T.view).createView()}):u.depthStencilAttachment=T}let m=i.commandEncoder.beginRenderPass(u);m.setPipeline(o.pipeline);let f=new Set(o.bindGroupLayouts);o.bindGroupLayouts.forEach((T,F)=>{var c;if(o.catchall&&F===o.catchall[0])m.setBindGroup(F,i.unwrap(o.catchall[1])),f.delete(T);else{let x=(c=this.priors.bindGroupLayoutMap)==null?void 0:c.get(T);x!==void 0&&(f.delete(T),m.setBindGroup(F,i.unwrap(x)))}});let g=new Set(this.core.usedVertexLayouts);if(this.core.usedVertexLayouts.forEach((T,F)=>{var x;let c=(x=this.priors.vertexLayoutMap)==null?void 0:x.get(T);c&&(g.delete(T),m.setVertexBuffer(F,i.unwrap(c)))}),f.size>0)throw new oe(f);if(g.size>0)throw new Ae(g);m.draw(t,r,n,a),m.end(),i.flush()}},cr=class{constructor(t){this.options=t;p(this,"label");p(this,"usedVertexLayouts");p(this,"_memo");p(this,"_vertexBufferLayouts");p(this,"_targets");let r=nn(t.vertexFn.shell.attributes[0],t.vertexAttribs);this._vertexBufferLayouts=r.bufferDefinitions,this.usedVertexLayouts=r.usedVertexLayouts,this._targets=Pn(t.fragmentFn.shell.targets,t.targets)}unwrap(){var t,r,n,a;if(this._memo===void 0){let{branch:o,vertexFn:i,fragmentFn:s,slotBindings:l,primitiveState:u,depthStencilState:m,multisampleState:f}=this.options,{code:g,bindGroupLayouts:w,catchall:y}=ve({"~resolve":x=>(x.withSlots(l,()=>{x.resolve(i),x.resolve(s)}),""),toString:()=>{var x;return`renderPipeline:${(x=this.label)!=null?x:"<unnamed>"}`}},{names:o.nameRegistry,jitTranspiler:o.jitTranspiler});y!==null&&((r=w[y[0]])==null||r.$name(`${(t=this.label)!=null?t:"<unnamed>"} - Automatic Bind Group & Layout`));let T=o.device,F=T.createShaderModule({label:`${(n=this.label)!=null?n:"<unnamed>"} - Shader`,code:g}),c={layout:T.createPipelineLayout({label:`${(a=this.label)!=null?a:"<unnamed>"} - Pipeline Layout`,bindGroupLayouts:w.map(x=>o.unwrap(x))}),vertex:{module:F,buffers:this._vertexBufferLayouts},fragment:{module:F,targets:this._targets}};this.label!==void 0&&(c.label=this.label),u&&(c.primitive=u),m&&(c.depthStencil=m),f&&(c.multisample=f),this._memo={pipeline:T.createRenderPipeline(c),bindGroupLayouts:w,catchall:y}}return this._memo}};var yr=class e{constructor(t,r){this._getRoot=t;this._slotBindings=r}with(t,r){return new e(this._getRoot,[...this._slotBindings,[se(t)?t.slot:t,r]])}withCompute(t){return new xr(this._getRoot(),this._slotBindings,t)}withVertex(t,r){return new hr({branch:this._getRoot(),primitiveState:void 0,depthStencilState:void 0,slotBindings:this._slotBindings,vertexFn:t,vertexAttribs:r,multisampleState:void 0})}},xr=class{constructor(t,r,n){this._root=t;this._slotBindings=r;this._entryFn=n}createPipeline(){return Rn(this._root,this._slotBindings,this._entryFn)}},hr=class{constructor(t){this._options=t}withFragment(t,r,n){return Ue(typeof t!="string","Just type mismatch validation"),Ue(typeof r!="string","Just type mismatch validation"),new br(L(A({},this._options),{fragmentFn:t,targets:r}))}},br=class e{constructor(t){this._options=t}withPrimitive(t){return new e(L(A({},this._options),{primitiveState:t}))}withDepthStencil(t){return new e(L(A({},this._options),{depthStencilState:t}))}withMultisample(t){return new e(L(A({},this._options),{multisampleState:t}))}createPipeline(){return Ln(this._options)}},wt=class extends yr{constructor(r,n,a,o){super(()=>this,[]);this.device=r;this.nameRegistry=n;this.jitTranspiler=a;this._ownDevice=o;p(this,"~unstable");p(this,"_disposables",[]);p(this,"_unwrappedBindGroupLayouts",new je(r=>r.unwrap(this)));p(this,"_unwrappedBindGroups",new je(r=>r.unwrap(this)));p(this,"_commandEncoder",null);this["~unstable"]=this}get commandEncoder(){return this._commandEncoder||(this._commandEncoder=this.device.createCommandEncoder()),this._commandEncoder}createBuffer(r,n){let a=yn(this,r,n);return this._disposables.push(a),a}createUniform(r,n){return this.createBuffer(r,n).$usage("uniform").as("uniform")}createMutable(r,n){return this.createBuffer(r,n).$usage("storage").as("mutable")}createReadonly(r,n){return this.createBuffer(r,n).$usage("storage").as("readonly")}createBindGroup(r,n){return new ge(r,n)}destroy(){for(let r of this._disposables)r.destroy();this._ownDevice&&this.device.destroy()}createTexture(r){let n=Dn(r,this);return this._disposables.push(n),n}unwrap(r){if(Bn(r))return r.rawPipeline;if(Cn(r))return r.core.unwrap().pipeline;if(yt(r))return this._unwrappedBindGroupLayouts.getOrMake(r);if(or(r))return this._unwrappedBindGroups.getOrMake(r);if(ae(r))return r.buffer;if(J(r)||Ne(r)||ze(r))return r.unwrap();if(bt(r))return r.vertexLayout;if(ke(r)){if("unwrap"in r)return r.unwrap(this);throw new Error("Cannot unwrap laid-out sampler.")}if(We(r)){if("unwrap"in r)return r.unwrap(this);throw new Error("Cannot unwrap laid-out comparison sampler.")}throw new Error(`Unknown resource type: ${r}`)}beginRenderPass(r,n){let a=this.commandEncoder.beginRenderPass(r),o=new Map,i=new Map,s,l=()=>{if(!s)throw new Error("Cannot draw without a call to pass.setPipeline");let{core:u,priors:m}=s,f=u.unwrap();a.setPipeline(f.pipeline);let g=new Set(f.bindGroupLayouts);f.bindGroupLayouts.forEach((y,T)=>{var F,c;if(f.catchall&&T===f.catchall[0])a.setBindGroup(T,this.unwrap(f.catchall[1])),g.delete(y);else{let x=(c=(F=m.bindGroupLayoutMap)==null?void 0:F.get(y))!=null?c:o.get(y);x!==void 0&&(g.delete(y),or(x)?a.setBindGroup(T,this.unwrap(x)):a.setBindGroup(T,x))}});let w=new Set;if(u.usedVertexLayouts.forEach((y,T)=>{var x;let F=(x=m.vertexLayoutMap)==null?void 0:x.get(y),c=F?{buffer:F,offset:void 0,size:void 0}:i.get(y);!c||!c.buffer?w.add(y):ae(c.buffer)?a.setVertexBuffer(T,this.unwrap(c.buffer),c.offset,c.size):a.setVertexBuffer(T,c.buffer,c.offset,c.size)}),g.size>0)throw new oe(g);if(w.size>0)throw new Ae(w)};n({setViewport(...u){a.setViewport(...u)},setScissorRect(...u){a.setScissorRect(...u)},setBlendConstant(...u){a.setBlendConstant(...u)},setStencilReference(...u){a.setStencilReference(...u)},beginOcclusionQuery(...u){a.beginOcclusionQuery(...u)},endOcclusionQuery(...u){a.endOcclusionQuery(...u)},executeBundles(...u){a.executeBundles(...u)},setPipeline(u){s=u},setIndexBuffer:(u,m,f,g)=>{ae(u)?a.setIndexBuffer(this.unwrap(u),m,f,g):a.setIndexBuffer(u,m,f,g)},setVertexBuffer(u,m,f,g){i.set(u,{buffer:m,offset:f,size:g})},setBindGroup(u,m){o.set(u,m)},draw(u,m,f,g){l(),a.draw(u,m,f,g)},drawIndexed(...u){l(),a.drawIndexed(...u)},drawIndirect(...u){l(),a.drawIndirect(...u)},drawIndexedIndirect(...u){l(),a.drawIndexedIndirect(...u)}}),a.end()}flush(){this._commandEncoder&&(this.device.queue.submit([this._commandEncoder.finish()]),this._commandEncoder=null)}};async function En(e){let{adapter:t,device:r,unstable_names:n="random",unstable_jitTranspiler:a}=e!=null?e:{};if(!navigator.gpu)throw new Error("WebGPU is not supported by this browser.");let o=await navigator.gpu.requestAdapter(t);if(!o)throw new Error("Could not find a compatible GPU");return new wt(await o.requestDevice(r),n==="random"?new te:new re,a,!0)}function Vn(e){let{device:t,unstable_names:r="random",unstable_jitTranspiler:n}=e!=null?e:{};return new wt(t,r==="random"?new te:new re,n,!1)}function St(e){return new wr(e)}var wr=class{constructor(t=void 0){this.defaultValue=t;p(this,"resourceType","slot");p(this,"label");p(this,"~repr")}$name(t){return this.label=t,this}areEqual(t,r){return Object.is(t,r)}toString(){var t;return`slot:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){let t=z();if(!t)throw new Error("Cannot access tgpu.slot's value outside of resolution.");return Ee(t.unwrap(this))}};function Gn(e,t){return new Sr(e,t)}var Sr=class{constructor(t,r=void 0){this.schema=t;this.defaultValue=r;p(this,"resourceType","accessor");p(this,"~repr");p(this,"label");p(this,"slot");this.slot=St(r)}$name(t){return this.label=t,this.slot.$name(t),this}toString(){var t;return`accessor:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){if(!z())throw new Error("Cannot access tgpu.accessor's value outside of resolution.");return new Proxy({"~resolve":r=>r.resolve(this),toString:()=>{var r;return`.value:${(r=this.label)!=null?r:"<unnamed>"}`},[b]:{dataType:this.schema}},M)}"~resolve"(t){let r=t.unwrap(this.slot);return Qe(r)?t.resolve(r):Vt(r)?`${t.resolve(r)}()`:t.resolveValue(r,this.schema)}};function $n(e){return ya(e)}function ca([e,t]){var r;return`${(r=e.label)!=null?r:"<unnamed>"}=${t}`}function ya(e){return{resourceType:"derived","~compute":e,"~repr":void 0,get value(){let r=z();if(!r)throw new Error("Cannot access tgpu.derived's value outside of resolution.");return Ee(r.unwrap(this))},with(r,n){return On(this,[[r,n]])},toString(){return"derived"}}}function On(e,t){return{resourceType:"derived","~repr":void 0,"~compute"(){throw new Error("'~compute' should never be read on bound derived items.")},"~providing":{inner:e,pairs:t},get value(){let n=z();if(!n)throw new Error("Cannot access tgpu.derived's value outside of resolution.");return Ee(n.unwrap(this))},with(n,a){return On(e,[...t,[n,a]])},toString(){return`derived[${t.map(ca).join(", ")}]`}}}function kn(e,t){return new Dt("private",e,t)}function Wn(e){return new Dt("workgroup",e)}var Dt=class{constructor(t,r,n){this.scope=t;this._dataType=r;this._initialValue=n;p(this,"_label")}$name(t){return this._label=t,this}"~resolve"(t){let r=t.names.makeUnique(this._label);return this._initialValue?t.addDeclaration(`var<${this.scope}> ${r}: ${t.resolve(this._dataType)} = ${t.resolveValue(this._initialValue,this._dataType)};`):t.addDeclaration(`var<${this.scope}> ${r}: ${t.resolve(this._dataType)};`),r}get label(){return this._label}toString(){var t;return`var:${(t=this.label)!=null?t:"<unnamed>"}`}get value(){if(!ie())throw new Error("Cannot access tgpu.var's value directly in JS.");return new Proxy({"~resolve":t=>t.resolve(this),toString:()=>{var t;return`.value:${(t=this.label)!=null?t:"<unnamed>"}`},[b]:{dataType:this._dataType}},M)}};var Mn={bindGroupLayout:ct,vertexLayout:gr,init:En,initFromDevice:Vn,resolve:In,"~unstable":{fn:Xr,fragmentFn:en,vertexFn:tn,computeFn:Qr,vertexLayout:gr,derived:$n,slot:St,accessor:Gn,privateVar:kn,workgroupVar:Wn,const:zr,declare:Kr,sampler:xn,comparisonSampler:hn}},zu=Mn;Object.assign(Mn,{__assignAst:Hr});export{oe as MissingBindGroupsError,Ut as MissingLinksError,Ft as MissingSlotValueError,Ae as MissingVertexBuffersError,vt as NotUniformError,te as RandomNameRegistry,ce as ResolutionError,re as StrictNameRegistry,zu as default,ae as isBuffer,We as isComparisonSampler,K as isDerived,ze as isSampledTextureView,ke as isSampler,j as isSlot,Ne as isStorageTextureView,J as isTexture,Vt as isTgpuFn,fa as isUsableAsRender,rr as isUsableAsSampled,fe as isUsableAsStorage,st as isUsableAsUniform,ma as isUsableAsVertex,Mn as tgpu,jt as unstable_asMutable,Kt as unstable_asReadonly,qt as unstable_asUniform};
|
26
26
|
//# sourceMappingURL=index.js.map
|