typegpu 0.3.0-alpha.5 → 0.3.0-alpha.6
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-3A2SULTE.cjs +4 -0
- package/chunk-3A2SULTE.cjs.map +1 -0
- package/chunk-U7YXBTRR.js +4 -0
- package/chunk-U7YXBTRR.js.map +1 -0
- package/data/index.cjs +1 -1
- package/data/index.d.cts +2 -2
- package/data/index.d.ts +2 -2
- package/data/index.js +1 -1
- package/index.cjs +9 -11
- package/index.cjs.map +1 -1
- package/index.d.cts +92 -29
- package/index.d.ts +92 -29
- package/index.js +9 -11
- package/index.js.map +1 -1
- package/package.json +9 -3
- package/{utilityTypes-CRzYf73j.d.cts → utilityTypes-D53GCmPw.d.cts} +1 -1
- package/{utilityTypes-CRzYf73j.d.ts → utilityTypes-D53GCmPw.d.ts} +1 -1
- package/chunk-CXTZVSMH.cjs +0 -2
- package/chunk-CXTZVSMH.cjs.map +0 -1
- package/chunk-R4KCT3GE.js +0 -2
- package/chunk-R4KCT3GE.js.map +0 -1
package/index.js
CHANGED
@@ -1,19 +1,17 @@
|
|
1
|
-
import{A as
|
2
|
-
`}function
|
1
|
+
import{A as S,B as F,C as u,E as I,Ea as R,F as nt,Fa as X,G as Y,H,Ha as b,I as we,J as at,K as De,L as Se,M as f,Ma as it,N as ot,Na as st,O as _,P as v,Sa as ut,Ta as pt,Ua as lt,a as L,b as E,c as s,d as G,e as Qe,f as Ze,q as be,r as W,s as J,t as O,u as et,v as re,w as ne,x as tt,y as rt,z as he}from"./chunk-U7YXBTRR.js";var V=class{constructor(){s(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++}`}},k=class{constructor(){s(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 dt(e,t){throw new Error(`Failed to handle ${e} at ${t}`)}var kt=["bool","f32","f16","i32","u32","vec2f","vec3f","vec4f","vec2h","vec3h","vec4h","vec2i","vec3i","vec4i","vec2u","vec3u","vec4u","mat2x2f","mat3x3f","mat4x4f"];function Mt(e){return kt.includes(e.type)}function $t(e,[t,r]){return` ${st(r)}${t}: ${e.resolve(r)},
|
2
|
+
`}function Wt(e,t){let r=e.names.makeUnique(t.label);return e.addDeclaration(`
|
3
3
|
struct ${r} {
|
4
|
-
${Object.entries(t.propTypes).map(n
|
5
|
-
`),r}function Ot(e,t){let r=e.resolve(t.elementType);return t.elementCount===0?`array<${r}>`:`array<${r}, ${t.elementCount}>`}function
|
6
|
-
${
|
7
|
-
`)}`);this.cause=r;this.trace=n;Object.setPrototypeOf(this,e.prototype)}appendToTrace(r){let n=[r,...this.trace];return new e(this.cause,n)}},ae=class e extends Error{constructor(r){super(`Missing value for '${r}'`);this.slot=r;Object.setPrototypeOf(this,e.prototype)}},j=class e extends Error{constructor(t){var r;super(`Buffer '${(r=t.label)!=null?r:"<unnamed>"}' is not bindable as a uniform. Use .$usage('uniform') to allow it.`),Object.setPrototypeOf(this,e.prototype)}};var z=class e extends Error{constructor(t){super(`Bind group was not provided for '${t!=null?t:"<unnamed>"}' layout.`),Object.setPrototypeOf(this,e.prototype)}};function*pt(e){let t=0;for(;;)e.has(t)||(yield t),t++}var U=Symbol("Unknown data type");function K(e){return!!e&&(typeof e=="object"||typeof e=="function")&&"resolve"in e}function lt(e){return typeof e=="number"||typeof e=="boolean"||typeof e=="string"||K(e)}function dt(e){return!!e&&typeof e=="object"&&"getMappedRange"in e&&"mapAsync"in e}function mt(e){return e.__brand==="TgpuSlot"}function x(e,t){return lt(t.value)||L(t.value)?e.resolve(t.value):String(t.value)}function Wt(e){throw new Error(`'${JSON.stringify(e)}' was not handled by the WGSL generator.`)}function gt(e,t){return t?{value:"true",dataType:we}:{value:"false",dataType:we}}function ft(e,t){return`${e.indent()}{
|
8
|
-
${t.block.map(r=>_e(e,r)).join(`
|
4
|
+
${Object.entries(t.propTypes).map(n=>$t(e,n)).join("")}}
|
5
|
+
`),r}function Ot(e,t){let r=e.resolve(t.elementType);return t.elementCount===0?`array<${r}>`:`array<${r}, ${t.elementCount}>`}function Ue(e,t){if(Mt(t))return t.type;if(t.type==="struct")return Wt(e,t);if(t.type==="array")return Ot(e,t);if(t.type==="atomic")return`atomic<${Ue(e,t.inner)}>`;if(t.type==="decorated")return e.resolve(t.inner);dt(t,"resolveData")}function N(e){return(e==null?void 0:e.resourceType)==="slot"}function ae(e){return(e==null?void 0:e.resourceType)==="derived"}function*mt(e){let t=0;for(;;)e.has(t)||(yield t),t++}var D=Symbol("Unknown data type");function Fe(e){return!!e&&(typeof e=="object"||typeof e=="function")&&"resolve"in e}function Q(e){return typeof e=="number"||typeof e=="boolean"||typeof e=="string"||Fe(e)||N(e)||ae(e)}function Tt(e){return!!e&&typeof e=="object"&&"getMappedRange"in e&&"mapAsync"in e}function h(e,t){return Q(t.value)||G(t.value)?e.resolve(t.value):String(t.value)}function Nt(e){throw new Error(`'${JSON.stringify(e)}' was not handled by the WGSL generator.`)}function gt(e,t){return t?{value:"true",dataType:he}:{value:"false",dataType:he}}function yt(e,t){return`${e.indent()}{
|
6
|
+
${t.block.map(r=>ve(e,r)).join(`
|
9
7
|
`)}
|
10
|
-
${e.dedent()}}`}function
|
8
|
+
${e.dedent()}}`}function _e(e,t){return e.getById(t)}function U(e,t){if(typeof t=="string")return _e(e,t);if(typeof t=="boolean")return gt(e,t);if("x2"in t){let[r,n,a]=t.x2,o=h(e,U(e,r)),i=h(e,U(e,a));return{value:`${o} ${n} ${i}`,dataType:D}}if("."in t){let[r,n]=t["."],a=U(e,r),o=h(e,U(e,n));if(typeof a.value=="string")return{value:`${a.value}.${o}`,dataType:D};if(Q(a.value))return o==="value"?{value:h(e,a),dataType:D}:{value:a.value[o],dataType:D};if(typeof a.value=="object")return{value:a.value[o],dataType:D};throw new Error(`Cannot access member ${o} of ${a.value}`)}if("[]"in t){let[r,n]=t["[]"],a=h(e,U(e,r)),o=h(e,U(e,n));return{value:`${a}[${o}]`,dataType:D}}if("num"in t)return{value:t.num,dataType:D};if("call"in t){let n=U(e,t.call).value,o=t.args.map(p=>U(e,p)).map(p=>h(e,p));return typeof n=="string"?{value:`${n}(${o.join(", ")})`,dataType:D}:{value:n(...o),dataType:D}}Nt(t)}function ve(e,t){if(typeof t=="string")return`${e.pre}${h(e,_e(e,t))};`;if(typeof t=="boolean")return`${e.pre}${h(e,gt(e,t))};`;if("return"in t)return t.return===null?`${e.pre}return;`:`${e.pre}return ${h(e,U(e,t.return))};`;if("if"in t){let r=h(e,U(e,t.if));e.indent();let n=ve(e,t.do);e.dedent(),e.indent();let a=t.else?ve(e,t.else):void 0;return e.dedent(),a?`${e.pre}if (${r})
|
11
9
|
${n}
|
12
10
|
${e.pre}else
|
13
11
|
${a}`:`${e.pre}if (${r})
|
14
|
-
${n}`}if("let"in t||"const"in t){let r=x(e,Fe(e,"let"in t?t.let:t.const)),n=t.eq?S(e,t.eq):void 0;if(!n)throw new Error("Cannot create variable without an initial value.");return`${e.pre}var ${r} = ${x(e,n)};`}return"block"in t?ft(e,t):`${e.pre}${x(e,S(e,t))};`}function Ae(e,t){return ft(e,t)}import{BufferReader as ve,BufferWriter as Ee}from"typed-binary";function Nt(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 T=Nt;function M(e,t){t>=0?e.writeByte(t&127):e.writeByte(t&127|128)}function $(e){let t=e.readByte();return t&128?(t&127)-128:t&127}var h={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=R(t);T(e,n);for(let[a,o]of Object.entries(t.propTypes))T(e,R(o)),q(e,o,r[a]);T(e,n)},array(e,t,r){if(t.elementCount===0)throw new Error("Cannot write using a runtime-sized schema.");let n=R(t);T(e,n);let a=e.currentByteOffset;for(let o=0;o<Math.min(t.elementCount,r.length);o++)T(e,n),q(e,t.elementType,r[o]);e.seekTo(a+w(t))},atomic(e,t,r){var n;(n=h[t.inner.type])==null||n.call(h,e,t,r)},decorated(e,t,r){var o,i;let n=Z(t);T(e,n);let a=e.currentByteOffset;(i=h[(o=t.inner)==null?void 0:o.type])==null||i.call(h,e,t.inner,r),e.seekTo(a+w(t))},uint8(e,t,r){e.writeByte(r)},uint8x2(e,t,r){e.writeByte(r.x),e.writeByte(r.y)},uint8x4(e,t,r){e.writeByte(r.x),e.writeByte(r.y),e.writeByte(r.z),e.writeByte(r.w)},sint8(e,t,r){M(e,r)},sint8x2(e,t,r){M(e,r.x),M(e,r.y)},sint8x4(e,t,r){M(e,r.x),M(e,r.y),M(e,r.z),M(e,r.w)},unorm8(e,t,r){e.writeByte(Math.floor(r*255))},unorm8x2(e,t,r){e.writeByte(Math.floor(r.x*255)),e.writeByte(Math.floor(r.y*255))},unorm8x4(e,t,r){e.writeByte(Math.floor(r.x*255)),e.writeByte(Math.floor(r.y*255)),e.writeByte(Math.floor(r.z*255)),e.writeByte(Math.floor(r.w*255))},snorm8(e,t,r){e.writeByte(Math.floor(r*127+128))},snorm8x2(e,t,r){e.writeByte(Math.floor(r.x*127+128)),e.writeByte(Math.floor(r.y*127+128))},snorm8x4(e,t,r){e.writeByte(Math.floor(r.x*127+128)),e.writeByte(Math.floor(r.y*127+128)),e.writeByte(Math.floor(r.z*127+128)),e.writeByte(Math.floor(r.w*127+128))},uint16(e,t,r){let n=new ArrayBuffer(2),a=new DataView(n),o=e.endianness==="little";a.setUint16(0,r,o),e.writeSlice(new Uint8Array(n))},uint16x2(e,t,r){let n=new ArrayBuffer(4),a=new DataView(n),o=e.endianness==="little";a.setUint16(0,r.x,o),a.setUint16(2,r.y,o),e.writeSlice(new Uint8Array(n))},uint16x4(e,t,r){let n=new ArrayBuffer(8),a=new DataView(n),o=e.endianness==="little";a.setUint16(0,r.x,o),a.setUint16(2,r.y,o),a.setUint16(4,r.z,o),a.setUint16(6,r.w,o),e.writeSlice(new Uint8Array(n))},sint16(e,t,r){let n=new ArrayBuffer(2),a=new DataView(n),o=e.endianness==="little";a.setInt16(0,r,o),e.writeSlice(new Uint8Array(n))},sint16x2(e,t,r){let n=new ArrayBuffer(4),a=new DataView(n),o=e.endianness==="little";a.setInt16(0,r.x,o),a.setInt16(2,r.y,o),e.writeSlice(new Uint8Array(n))},sint16x4(e,t,r){let n=new ArrayBuffer(8),a=new DataView(n),o=e.endianness==="little";a.setInt16(0,r.x,o),a.setInt16(2,r.y,o),a.setInt16(4,r.z,o),a.setInt16(6,r.w,o),e.writeSlice(new Uint8Array(n))},unorm16(e,t,r){let n=new ArrayBuffer(2),a=new DataView(n),o=e.endianness==="little";a.setUint16(0,Math.floor(r*65535),o),e.writeSlice(new Uint8Array(n))},unorm16x2(e,t,r){let n=new ArrayBuffer(4),a=new DataView(n),o=e.endianness==="little";a.setUint16(0,Math.floor(r.x*65535),o),a.setUint16(2,Math.floor(r.y*65535),o),e.writeSlice(new Uint8Array(n))},unorm16x4(e,t,r){let n=new ArrayBuffer(8),a=new DataView(n),o=e.endianness==="little";a.setUint16(0,Math.floor(r.x*65535),o),a.setUint16(2,Math.floor(r.y*65535),o),a.setUint16(4,Math.floor(r.z*65535),o),a.setUint16(6,Math.floor(r.w*65535),o),e.writeSlice(new Uint8Array(n))},snorm16(e,t,r){let n=new ArrayBuffer(2),a=new DataView(n),o=e.endianness==="little";a.setUint16(0,Math.floor(r*32767+32768),o),e.writeSlice(new Uint8Array(n))},snorm16x2(e,t,r){let n=new ArrayBuffer(4),a=new DataView(n),o=e.endianness==="little";a.setUint16(0,Math.floor(r.x*32767+32768),o),a.setUint16(2,Math.floor(r.y*32767+32768),o),e.writeSlice(new Uint8Array(n))},snorm16x4(e,t,r){let n=new ArrayBuffer(8),a=new DataView(n),o=e.endianness==="little";a.setUint16(0,Math.floor(r.x*32767+32768),o),a.setUint16(2,Math.floor(r.y*32767+32768),o),a.setUint16(4,Math.floor(r.z*32767+32768),o),a.setUint16(6,Math.floor(r.w*32767+32768),o),e.writeSlice(new Uint8Array(n))},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.writeByte(Math.floor(r.z*255)),e.writeByte(Math.floor(r.y*255)),e.writeByte(Math.floor(r.x*255)),e.writeByte(Math.floor(r.w*255))},"loose-array"(e,t,r){var o,i;let n=R(t);T(e,n);let a=e.currentByteOffset;for(let p=0;p<Math.min(t.elementCount,r.length);p++)T(e,n),(i=h[(o=t.elementType)==null?void 0:o.type])==null||i.call(h,e,t.elementType,r[p]);e.seekTo(a+w(t))},"loose-struct"(e,t,r){var n;for(let[a,o]of Object.entries(t.propTypes))(n=h[o.type])==null||n.call(h,e,o,r[a])},"loose-decorated"(e,t,r){var i;let n=Z(t);T(e,n);let a=e.currentByteOffset,o=h[(i=t.inner)==null?void 0:i.type];return o==null||o(e,t.inner,r),e.seekTo(a+w(t)),r}};function q(e,t,r){let n=h[t.type];if(!n)throw new Error(`Cannot write data of type '${t.type}'.`);n(e,t,r)}var jt={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 he(e.readFloat32(),e.readFloat32(),e.readFloat32())},vec4f(e){return y(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32())},vec2h(e){return et(e.readFloat16(),e.readFloat16())},vec3h(e){return tt(e.readFloat16(),e.readFloat16(),e.readFloat16())},vec4h(e){return rt(e.readFloat16(),e.readFloat16(),e.readFloat16(),e.readFloat16())},vec2i(e){return X(e.readInt32(),e.readInt32())},vec3i(e){return De(e.readInt32(),e.readInt32(),e.readInt32())},vec4i(e){return F(e.readInt32(),e.readInt32(),e.readInt32(),e.readInt32())},vec2u(e){return Q(e.readUint32(),e.readUint32())},vec3u(e){return Ue(e.readUint32(),e.readUint32(),e.readUint32())},vec4u(e){return _(e.readUint32(),e.readUint32(),e.readUint32(),e.readUint32())},mat2x2f(e){return ot(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32())},mat3x3f(e){let t=()=>{let r=e.readFloat32();return e.readFloat32(),r};return it(e.readFloat32(),e.readFloat32(),t(),e.readFloat32(),e.readFloat32(),t(),e.readFloat32(),e.readFloat32(),t())},mat4x4f(e){return st(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=R(t);T(e,r);let n={};for(let[a,o]of Object.entries(t.propTypes))T(e,R(o)),n[a]=A(e,o);return T(e,r),n},array(e,t){if(t.elementCount===0)throw new Error("Cannot read using a runtime-sized schema.");let r=R(t),n=[];for(let a=0;a<t.elementCount;a++){T(e,r);let o=t.elementType,i=A(e,o);n.push(i)}return T(e,r),n},atomic(e,t){return A(e,t.inner)},decorated(e,t){let r=Z(t);T(e,r);let n=e.currentByteOffset,a=A(e,t.inner);return e.seekTo(n+w(t)),a},uint8:e=>e.readByte(),uint8x2:e=>Q(e.readByte(),e.readByte()),uint8x4:e=>_(e.readByte(),e.readByte(),e.readByte(),e.readByte()),sint8:e=>$(e),sint8x2:e=>X($(e),$(e)),sint8x4:e=>F($(e),$(e),$(e),$(e)),unorm8:e=>e.readByte()/255,unorm8x2:e=>G(e.readByte()/255,e.readByte()/255),unorm8x4:e=>y(e.readByte()/255,e.readByte()/255,e.readByte()/255,e.readByte()/255),snorm8:e=>(e.readByte()-128)/127,snorm8x2:e=>G((e.readByte()-128)/127,(e.readByte()-128)/127),snorm8x4:e=>y((e.readByte()-128)/127,(e.readByte()-128)/127,(e.readByte()-128)/127,(e.readByte()-128)/127),uint16(e){let t=new ArrayBuffer(2);e.readSlice(new Uint8Array(t),0,2);let r=new DataView(t),n=e.endianness==="little";return r.getUint16(0,n)},uint16x2(e){let t=new ArrayBuffer(4);e.readSlice(new Uint8Array(t),0,4);let r=new DataView(t),n=e.endianness==="little";return Q(r.getUint16(0,n),r.getUint16(2,n))},uint16x4(e){let t=new ArrayBuffer(8);e.readSlice(new Uint8Array(t),0,8);let r=new DataView(t),n=e.endianness==="little";return _(r.getUint16(0,n),r.getUint16(2,n),r.getUint16(4,n),r.getUint16(6,n))},sint16(e){let t=new ArrayBuffer(2);e.readSlice(new Uint8Array(t),0,2);let r=new DataView(t),n=e.endianness==="little";return r.getInt16(0,n)},sint16x2(e){let t=new ArrayBuffer(4);e.readSlice(new Uint8Array(t),0,4);let r=new DataView(t),n=e.endianness==="little";return X(r.getInt16(0,n),r.getInt16(2,n))},sint16x4(e){let t=new ArrayBuffer(8);e.readSlice(new Uint8Array(t),0,8);let r=new DataView(t),n=e.endianness==="little";return F(r.getInt16(0,n),r.getInt16(2,n),r.getInt16(4,n),r.getInt16(6,n))},unorm16(e){let t=new ArrayBuffer(2);e.readSlice(new Uint8Array(t),0,2);let r=new DataView(t),n=e.endianness==="little";return r.getUint16(0,n)/65535},unorm16x2(e){let t=new ArrayBuffer(4);e.readSlice(new Uint8Array(t),0,4);let r=new DataView(t),n=e.endianness==="little";return G(r.getUint16(0,n)/65535,r.getUint16(2,n)/65535)},unorm16x4(e){let t=new ArrayBuffer(8);e.readSlice(new Uint8Array(t),0,8);let r=new DataView(t),n=e.endianness==="little";return y(r.getUint16(0,n)/65535,r.getUint16(2,n)/65535,r.getUint16(4,n)/65535,r.getUint16(6,n)/65535)},snorm16(e){let t=new ArrayBuffer(2);e.readSlice(new Uint8Array(t),0,2);let r=new DataView(t),n=e.endianness==="little";return r.getInt16(0,n)/32767},snorm16x2(e){let t=new ArrayBuffer(4);e.readSlice(new Uint8Array(t),0,4);let r=new DataView(t),n=e.endianness==="little";return G((r.getUint16(0,n)-32768)/32767,(r.getUint16(2,n)-32768)/32767)},snorm16x4(e){let t=new ArrayBuffer(8);e.readSlice(new Uint8Array(t),0,8);let r=new DataView(t),n=e.endianness==="little";return y((r.getUint16(0,n)-32768)/32767,(r.getUint16(2,n)-32768)/32767,(r.getUint16(4,n)-32768)/32767,(r.getUint16(6,n)-32768)/32767)},float16(e){return e.readFloat16()},float16x2:e=>G(e.readFloat16(),e.readFloat16()),float16x4:e=>y(e.readFloat16(),e.readFloat16(),e.readFloat16(),e.readFloat16()),float32:e=>e.readFloat32(),float32x2:e=>G(e.readFloat32(),e.readFloat32()),float32x3:e=>he(e.readFloat32(),e.readFloat32(),e.readFloat32()),float32x4:e=>y(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32()),uint32:e=>e.readUint32(),uint32x2:e=>Q(e.readUint32(),e.readUint32()),uint32x3:e=>Ue(e.readUint32(),e.readUint32(),e.readUint32()),uint32x4:e=>_(e.readUint32(),e.readUint32(),e.readUint32(),e.readUint32()),sint32:e=>e.readInt32(),sint32x2:e=>X(e.readInt32(),e.readInt32()),sint32x3:e=>De(e.readInt32(),e.readInt32(),e.readInt32()),sint32x4:e=>F(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 y(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 y(n,r,t,a)},"loose-struct"(e,t){let r={};for(let[n,a]of Object.entries(t.propTypes))r[n]=A(e,a);return r},"loose-array"(e,t){let r=R(t),n=[];for(let a=0;a<t.elementCount;a++)T(e,r),n.push(A(e,t.elementType));return T(e,r),n},"loose-decorated"(e,t){T(e,Z(t));let r=e.currentByteOffset,n=A(e,t.inner);return e.seekTo(r+w(t)),n}};function A(e,t){let r=jt[t.type];if(!r)throw new Error(`Cannot read data of type '${t.type}'.`);return r(e,t)}function Tt(e,t,r){return L(t)?new oe(e,t,r):new oe(e,t,r,["storage","uniform"])}function J(e){return e.resourceType==="buffer"}function Re(e){return!!e.usableAsUniform}function zt(e){return!!e.usableAsVertex}var oe=class{constructor(t,r,n,a){this._group=t;this.dataType=r;this.initialOrBuffer=n;this._disallowedUsages=a;s(this,"resourceType","buffer");s(this,"flags",GPUBufferUsage.COPY_DST|GPUBufferUsage.COPY_SRC);s(this,"_buffer",null);s(this,"_ownBuffer");s(this,"_destroyed",!1);s(this,"_label");s(this,"initial");s(this,"usableAsUniform",!1);s(this,"usableAsStorage",!1);s(this,"usableAsVertex",!1);dt(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:w(this.dataType),usage:this.flags,mappedAtCreation:!!this.initial,label:(r=this.label)!=null?r:"<unnamed>"}),this.initial)){let n=new Ee(this._buffer.getMappedRange());q(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} 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){return this.flags|=t,this}write(t){let r=this.buffer,n=this._group.device;if(r.mapState==="mapped"){let i=r.getMappedRange();q(new Ee(i),this.dataType,t);return}let a=w(this.dataType);this._group.flush();let o=new ArrayBuffer(a);q(new Ee(o),this.dataType,t),n.queue.writeBuffer(r,0,o,0,a)}copyFrom(t){if(this.buffer.mapState==="mapped")throw new Error("Cannot copy to a mapped buffer.");let r=w(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 A(new ve(i),this.dataType)}if(t.usage&GPUBufferUsage.MAP_READ){await t.mapAsync(GPUMapMode.READ);let i=t.getMappedRange(),p=A(new ve(i),this.dataType);return t.unmap(),p}let n=r.createBuffer({size:w(this.dataType),usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ}),a=r.createCommandEncoder();a.copyBufferToBuffer(t,0,n,0,w(this.dataType)),r.queue.submit([a.finish()]),await r.queue.onSubmittedWorkDone(),await n.mapAsync(GPUMapMode.READ,0,w(this.dataType));let o=A(new ve(n.getMappedRange()),this.dataType);return n.unmap(),n.destroy(),o}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 se(e){return!!(e!=null&&e.usableAsStorage)}var ie=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)}};var Kt={uniform:"uniform",mutable:"storage, read_write",readonly:"storage, read"};var te=class{constructor(t,r,n){this.usage=t;this.dataType=r;this._membership=n;s(this,"~repr");s(this,"resourceType","buffer-usage")}get label(){return this._membership.key}resolve(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout),a=Kt[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(!Ze())throw new Error("Cannot access buffer's value directly in JS.");return this}};function qt(e){return(e==null?void 0:e.resourceType)==="sampler"}function Jt(e){return(e==null?void 0:e.resourceType)==="sampler-comparison"}var ue=class{constructor(t){this._membership=t;s(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}},pe=class{constructor(t){this._membership=t;s(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}};var le=class{constructor(t){this._membership=t;s(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}};var yt={r8unorm:u,r8snorm:u,r8uint:D,r8sint:B,r16uint:D,r16sint:B,r16float:u,rg8unorm:u,rg8snorm:u,rg8uint:D,rg8sint:B,r32uint:D,r32sint:B,r32float:u,rg16uint:D,rg16sint:B,rg16float:u,rgba8unorm:u,"rgba8unorm-srgb":u,rgba8snorm:u,rgba8uint:D,rgba8sint:B,bgra8unorm:u,"bgra8unorm-srgb":u,rgb9e5ufloat:u,rgb10a2uint:D,rgb10a2unorm:u,rg11b10ufloat:u,rg32uint:D,rg32sint:B,rg32float:u,rgba16uint:D,rgba16sint:B,rgba16float:u,rgba32uint:D,rgba32sint:B,rgba32float:u,stencil8:u,depth16unorm:u,depth24plus:u,"depth24plus-stencil8":u,depth32float:u,"depth32float-stencil8":u,"bc1-rgba-unorm":u,"bc1-rgba-unorm-srgb":u,"bc2-rgba-unorm":u,"bc2-rgba-unorm-srgb":u,"bc3-rgba-unorm":u,"bc3-rgba-unorm-srgb":u,"bc4-r-unorm":u,"bc4-r-snorm":u,"bc5-rg-unorm":u,"bc5-rg-snorm":u,"bc6h-rgb-ufloat":u,"bc6h-rgb-float":u,"bc7-rgba-unorm":u,"bc7-rgba-unorm-srgb":u,"etc2-rgb8unorm":u,"etc2-rgb8unorm-srgb":u,"etc2-rgb8a1unorm":u,"etc2-rgb8a1unorm-srgb":u,"etc2-rgba8unorm":u,"etc2-rgba8unorm-srgb":u,"eac-r11unorm":u,"eac-r11snorm":u,"eac-rg11unorm":u,"eac-rg11snorm":u,"astc-4x4-unorm":u,"astc-4x4-unorm-srgb":u,"astc-5x4-unorm":u,"astc-5x4-unorm-srgb":u,"astc-5x5-unorm":u,"astc-5x5-unorm-srgb":u,"astc-6x5-unorm":u,"astc-6x5-unorm-srgb":u,"astc-6x6-unorm":u,"astc-6x6-unorm-srgb":u,"astc-8x5-unorm":u,"astc-8x5-unorm-srgb":u,"astc-8x6-unorm":u,"astc-8x6-unorm-srgb":u,"astc-8x8-unorm":u,"astc-8x8-unorm-srgb":u,"astc-10x5-unorm":u,"astc-10x5-unorm-srgb":u,"astc-10x6-unorm":u,"astc-10x6-unorm-srgb":u,"astc-10x8-unorm":u,"astc-10x8-unorm-srgb":u,"astc-10x10-unorm":u,"astc-10x10-unorm-srgb":u,"astc-12x10-unorm":u,"astc-12x10-unorm-srgb":u,"astc-12x12-unorm":u,"astc-12x12-unorm-srgb":u},re={rgba8unorm:y,rgba8snorm:y,rgba8uint:_,rgba8sint:F,rgba16uint:_,rgba16sint:F,rgba16float:y,r32uint:_,r32sint:F,r32float:y,rg32uint:_,rg32sint:F,rg32float:y,rgba32uint:_,rgba32sint:F,rgba32float:y,bgra8unorm:y},ct={f32:"float",u32:"uint",i32:"sint"},xt={float:u,"unfilterable-float":u,uint:D,sint:B,depth:u};function bt(e,t){return new Pe(e,t)}function I(e){return(e==null?void 0:e.resourceType)==="texture"}function Ie(e){return(e==null?void 0:e.resourceType)==="texture-storage-view"}function Ve(e){return(e==null?void 0:e.resourceType)==="texture-sampled-view"}var wt={mutable:"read_write",readonly:"read",writeonly:"write"},Pe=class{constructor(t,r){this.props=t;this._branch=r;s(this,"resourceType","texture");s(this,"usableAsSampled",!1);s(this,"usableAsStorage",!1);s(this,"usableAsRender",!1);s(this,"_destroyed",!1);s(this,"_label");s(this,"_flags",GPUTextureUsage.COPY_DST|GPUTextureUsage.COPY_SRC);s(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}_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=re[n];return ee(!!a,`Unsupported storage texture format: ${n}`),new Le(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(!re[r])throw new Error(`Unsupported storage texture format: ${r}`);return new Ge(t,this)}destroy(){var t;this._destroyed||(this._destroyed=!0,(t=this._texture)==null||t.destroy())}},ge={"1d":"1d","2d":"2d","2d-array":"2d_array",cube:"cube","cube-array":"cube_array","3d":"3d"},Le=class{constructor(t,r,n){this.access=r;this._texture=n;s(this,"resourceType","texture-storage-view");s(this,"texelDataType");s(this,"dimension");s(this,"_view");s(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=re[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_${ge[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${a}) var ${r}: ${o}<${this._format}, ${wt[this.access]}>;`),r}},de=class{constructor(t,r,n,a){this._format=t;this.dimension=r;this.access=n;this._membership=a;s(this,"resourceType","texture-storage-view");s(this,"texelDataType");this.texelDataType=re[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_${ge[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: ${a}<${this._format}, ${wt[this.access]}>;`),r}},Ge=class{constructor(t,r){this._props=t;this._texture=r;s(this,"resourceType","texture-sampled-view");s(this,"channelDataType");s(this,"dimension");s(this,"_format");s(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=yt[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(E({label:`${(t=this.label)!=null?t:"<unnamed>"} - View`},this._props))),this._view}resolve(t){var p;let r=t.names.makeUnique(this.label),n=((p=this._texture.props.sampleCount)!=null?p:1)>1,{group:a,binding:o}=t.allocateFixedEntry({texture:ct[this.channelDataType.type],viewDimension:this.dimension,multisampled:n},this),i=n?"texture_multisampled_2d":`texture_${ge[this.dimension]}`;return t.addDeclaration(`@group(${a}) @binding(${o}) var ${r}: ${i}<${t.resolve(this.channelDataType)}>;`),r}},me=class{constructor(t,r,n,a){this.dimension=r;this._multisampled=n;this._membership=a;s(this,"resourceType","texture-sampled-view");s(this,"channelDataType");this.channelDataType=xt[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_${ge[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: ${a}<${t.resolve(this.channelDataType)}>;`),r}};function Ce(e){return!!(e!=null&&e.usableAsSampled)}function Yt(e){return!!(e!=null&&e.usableAsRender)}var fe=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 Te(e){return new Me(e)}function ye(e){return!!e&&e.resourceType==="bind-group-layout"}function Dt(e){return!!e&&e.resourceType==="bind-group"}var ke=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)}},ht=["compute"],Y=["compute","vertex","fragment"],Me=class{constructor(t){this.entries=t;s(this,"_label");s(this,"_index");s(this,"resourceType","bind-group-layout");s(this,"bound",{});var n,a,o,i,p;let r=0;for(let[g,l]of Object.entries(t)){if(l===null){r++;continue}let d={idx:r,key:g,layout:this};if("uniform"in l&&(this.bound[g]=new te("uniform",l.uniform,d)),"storage"in l){let f="type"in l.storage?l.storage:l.storage(0);this.bound[g]=new te((n=l.access)!=null?n:"readonly",f,d)}"texture"in l&&(this.bound[g]=new me(l.texture,(a=l.viewDimension)!=null?a:"2d",(o=l.multisampled)!=null?o:!1,d)),"storageTexture"in l&&(this.bound[g]=new de(l.storageTexture,(i=l.viewDimension)!=null?i:"2d",(p=l.access)!=null?p:"writeonly",d)),"externalTexture"in l&&(this.bound[g]=new le(d)),"sampler"in l&&(l.sampler==="comparison"?this.bound[g]=new pe(d):this.bound[g]=new ue(d)),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 g,l,d,f;if(a===null)return null;let i=a.visibility,p={binding:o,visibility:0};if("uniform"in a)i=i!=null?i:Y,p.buffer={type:"uniform"};else if("storage"in a)i=i!=null?i:a.access==="mutable"?ht:Y,p.buffer={type:a.access==="mutable"?"storage":"read-only-storage"};else if("sampler"in a)i=i!=null?i:Y,p.sampler={type:a.sampler};else if("texture"in a)i=i!=null?i:Y,p.texture={sampleType:a.texture,viewDimension:(g=a.viewDimension)!=null?g:"2d",multisampled:(l=a.multisampled)!=null?l:!1};else if("storageTexture"in a){let m=(d=a.access)!=null?d:"writeonly";i=i!=null?i:m==="readonly"?Y:ht,p.storageTexture={format:a.storageTexture,access:{mutable:"read-write",readonly:"read-only",writeonly:"write-only"}[m],viewDimension:(f=a.viewDimension)!=null?f:"2d"}}else"externalTexture"in a&&(i=i!=null?i:Y,p.externalTexture={});return i!=null&&i.includes("compute")&&(p.visibility|=GPUShaderStage.COMPUTE),i!=null&&i.includes("vertex")&&(p.visibility|=GPUShaderStage.VERTEX),i!=null&&i.includes("fragment")&&(p.visibility|=GPUShaderStage.FRAGMENT),p}).filter(a=>a!==null)})}populate(t){return new O(this,t)}},O=class{constructor(t,r){this.layout=t;this.entries=r;s(this,"resourceType","bind-group");for(let n of Object.keys(t.entries))if(t.entries[n]!==null&&!(n in r))throw new ke(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 g;if(o===null)return null;let p=this.entries[a];if(p===void 0)throw new Error(`'${a}' is a resource required to populate bind group layout '${(g=this.layout.label)!=null?g:"<unnamed>"}'.`);if("uniform"in o){let l;if(J(p)){if(!Re(p))throw new j(p);l={buffer:t.unwrap(p)}}else l={buffer:p};return{binding:i,resource:l}}if("storage"in o){let l;if(J(p)){if(!se(p))throw new j(p);l={buffer:t.unwrap(p)}}else l={buffer:p};return{binding:i,resource:l}}if("texture"in o){let l;if(I(p)){if(!Ce(p))throw new fe(p);l=t.unwrap(p.asSampled())}else l=p;return{binding:i,resource:l}}if("storageTexture"in o){let l;if(I(p)){if(!se(p))throw new ie(p);o.access==="readonly"?l=t.unwrap(p.asReadonly()):o.access==="mutable"?l=t.unwrap(p.asMutable()):l=t.unwrap(p.asReadonly())}else l=p;return{binding:i,resource:l}}if("externalTexture"in o||"sampler"in o)return{binding:i,resource:p};throw new Error(`Malformed bind group entry: ${p} (${JSON.stringify(p)})`)}).filter(a=>a!==null)})}};var Ut="#CATCHALL#",Oe=class{constructor(){s(this,"_stack",[]);s(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})}pushSlotBindings(t){this._stack.push({type:"slotBinding",bindingMap:new WeakMap(t)})}pushFunctionScope(t,r,n){this._stack.push({type:"functionScope",args:t,returnType:r,externalMap:n})}pop(){let t=this._stack.pop();(t==null?void 0:t.type)==="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:U}: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}}}}},ce=[""," "," "," "," "," "," "," "," "],$e=ce.length-1,We=class{constructor(){s(this,"identLevel",0)}get pre(){var t;return(t=ce[this.identLevel])!=null?t:ce[$e].repeat(this.identLevel/$e)+ce[this.identLevel%$e]}indent(){let t=this.pre;return this.identLevel++,t}dedent(){return this.identLevel--,this.pre}},Ne=class{constructor(t){s(this,"_memoizedResolves",new WeakMap);s(this,"_indentController",new We);s(this,"_jitTranspiler");s(this,"_itemStateStack",new Oe);s(this,"_declarations",[]);s(this,"bindGroupLayoutsToPlaceholderMap",new Map);s(this,"_nextFreeLayoutPlaceholderIdx",0);s(this,"fixedBindings",[]);s(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){var r;return(r=this._itemStateStack.getResourceById(t))!=null?r:{value:t,dataType:U}}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);let r=Ae(this,t.body);this._itemStateStack.pop();let n=t.args.map(a=>`${a.value}: ${this.resolve(a.dataType)}`).join(", ");return{head:t.returnType!==void 0?`(${n}) -> ${this.resolve(t.returnType)}`:`(${n})`,body:r}}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:Ut,binding:n}}readSlot(t){let r=this._itemStateStack.readSlot(t);if(r===void 0)throw new ae(t);return r}unwrap(t){let r=t;for(;mt(r);)r=this.readSlot(r);return r}_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(([g,l])=>this._itemStateStack.readSlot(g)===l))return i.result;let a=L(t)?Se(this,t):t.resolve(this),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 N?a.appendToTrace(t):new N(a,[t])}finally{this._itemStateStack.pop()}}resolve(t,r=[]){if(typeof t=="string"||typeof t=="number"||typeof t=="boolean")return String(t);let n=!1;r.length>0&&(n=!0,this._itemStateStack.pushSlotBindings(r));try{if(this._itemStateStack.itemDepth===0){let a=Qe(()=>this._getOrInstantiate(t));return`${[...this._declarations].join(`
|
12
|
+
${n}`}if("let"in t||"const"in t){let r=h(e,_e(e,"let"in t?t.let:t.const)),n=t.eq?U(e,t.eq):void 0;if(!n)throw new Error("Cannot create variable without an initial value.");return`${e.pre}var ${r} = ${h(e,n)};`}return"block"in t?yt(e,t):`${e.pre}${h(e,U(e,t))};`}function Be(e,t){return yt(e,t)}import{BufferReader as Ae,BufferWriter as Le}from"typed-binary";function jt(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 y=jt;var w={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=R(t);y(e,n);for(let[a,o]of Object.entries(t.propTypes))y(e,R(o)),j(e,o,r[a]);y(e,n)},array(e,t,r){if(t.elementCount===0)throw new Error("Cannot write using a runtime-sized schema.");let n=R(t);y(e,n);let a=e.currentByteOffset;for(let o=0;o<Math.min(t.elementCount,r.length);o++)y(e,n),j(e,t.elementType,r[o]);e.seekTo(a+b(t))},atomic(e,t,r){var n;(n=w[t.inner.type])==null||n.call(w,e,t,r)},decorated(e,t,r){var o,i;let n=X(t);y(e,n);let a=e.currentByteOffset;(i=w[(o=t.inner)==null?void 0:o.type])==null||i.call(w,e,t.inner,r),e.seekTo(a+b(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)},"loose-array"(e,t,r){var o,i;let n=R(t);y(e,n);let a=e.currentByteOffset;for(let p=0;p<Math.min(t.elementCount,r.length);p++)y(e,n),(i=w[(o=t.elementType)==null?void 0:o.type])==null||i.call(w,e,t.elementType,r[p]);e.seekTo(a+b(t))},"loose-struct"(e,t,r){var n;for(let[a,o]of Object.entries(t.propTypes))(n=w[o.type])==null||n.call(w,e,o,r[a])},"loose-decorated"(e,t,r){var i;let n=X(t);y(e,n);let a=e.currentByteOffset,o=w[(i=t.inner)==null?void 0:i.type];return o==null||o(e,t.inner,r),e.seekTo(a+b(t)),r}};function j(e,t,r){let n=w[t.type];if(!n)throw new Error(`Cannot write data of type '${t.type}'.`);n(e,t,r)}var M={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 I(e.readFloat32(),e.readFloat32())},vec3f(e){return we(e.readFloat32(),e.readFloat32(),e.readFloat32())},vec4f(e){return f(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32())},vec2h(e){return nt(e.readFloat16(),e.readFloat16())},vec3h(e){return at(e.readFloat16(),e.readFloat16(),e.readFloat16())},vec4h(e){return ot(e.readFloat16(),e.readFloat16(),e.readFloat16(),e.readFloat16())},vec2i(e){return Y(e.readInt32(),e.readInt32())},vec3i(e){return De(e.readInt32(),e.readInt32(),e.readInt32())},vec4i(e){return _(e.readInt32(),e.readInt32(),e.readInt32(),e.readInt32())},vec2u(e){return H(e.readUint32(),e.readUint32())},vec3u(e){return Se(e.readUint32(),e.readUint32(),e.readUint32())},vec4u(e){return v(e.readUint32(),e.readUint32(),e.readUint32(),e.readUint32())},mat2x2f(e){return ut(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32())},mat3x3f(e){let t=()=>{let r=e.readFloat32();return e.readFloat32(),r};return pt(e.readFloat32(),e.readFloat32(),t(),e.readFloat32(),e.readFloat32(),t(),e.readFloat32(),e.readFloat32(),t())},mat4x4f(e){return lt(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=R(t);y(e,r);let n={};for(let[a,o]of Object.entries(t.propTypes))y(e,R(o)),n[a]=B(e,o);return y(e,r),n},array(e,t){if(t.elementCount===0)throw new Error("Cannot read using a runtime-sized schema.");let r=R(t),n=[];for(let a=0;a<t.elementCount;a++){y(e,r);let o=t.elementType,i=B(e,o);n.push(i)}return y(e,r),n},atomic(e,t){return B(e,t.inner)},decorated(e,t){let r=X(t);y(e,r);let n=e.currentByteOffset,a=B(e,t.inner);return e.seekTo(n+b(t)),a},uint8:e=>e.readUint8(),uint8x2:e=>H(e.readUint8(),e.readUint8()),uint8x4:e=>v(e.readUint8(),e.readUint8(),e.readUint8(),e.readUint8()),sint8:e=>e.readInt8(),sint8x2:e=>Y(e.readInt8(),e.readInt8()),sint8x4:e=>_(e.readInt8(),e.readInt8(),e.readInt8(),e.readInt8()),unorm8:e=>e.readUint8()/255,unorm8x2:e=>I(e.readUint8()/255,e.readUint8()/255),unorm8x4:e=>f(e.readUint8()/255,e.readUint8()/255,e.readUint8()/255,e.readUint8()/255),snorm8:e=>(e.readUint8()-128)/127,snorm8x2:e=>I((e.readUint8()-128)/127,(e.readUint8()-128)/127),snorm8x4:e=>f((e.readUint8()-128)/127,(e.readUint8()-128)/127,(e.readUint8()-128)/127,(e.readUint8()-128)/127),uint16:e=>e.readUint16(),uint16x2:e=>H(e.readUint16(),e.readUint16()),uint16x4:e=>v(e.readUint16(),e.readUint16(),e.readUint16(),e.readUint16()),sint16:e=>e.readInt16(),sint16x2:e=>Y(e.readInt16(),e.readInt16()),sint16x4:e=>_(e.readInt16(),e.readInt16(),e.readInt16(),e.readInt16()),unorm16:e=>e.readUint16()/65535,unorm16x2:e=>I(e.readUint16()/65535,e.readUint16()/65535),unorm16x4:e=>f(e.readUint16()/65535,e.readUint16()/65535,e.readUint16()/65535,e.readUint16()/65535),snorm16:e=>(e.readUint16()-32768)/32767,snorm16x2:e=>I(M.snorm16(e),M.snorm16(e)),snorm16x4:e=>f(M.snorm16(e),M.snorm16(e),M.snorm16(e),M.snorm16(e)),float16(e){return e.readFloat16()},float16x2:e=>I(e.readFloat16(),e.readFloat16()),float16x4:e=>f(e.readFloat16(),e.readFloat16(),e.readFloat16(),e.readFloat16()),float32:e=>e.readFloat32(),float32x2:e=>I(e.readFloat32(),e.readFloat32()),float32x3:e=>we(e.readFloat32(),e.readFloat32(),e.readFloat32()),float32x4:e=>f(e.readFloat32(),e.readFloat32(),e.readFloat32(),e.readFloat32()),uint32:e=>e.readUint32(),uint32x2:e=>H(e.readUint32(),e.readUint32()),uint32x3:e=>Se(e.readUint32(),e.readUint32(),e.readUint32()),uint32x4:e=>v(e.readUint32(),e.readUint32(),e.readUint32(),e.readUint32()),sint32:e=>e.readInt32(),sint32x2:e=>Y(e.readInt32(),e.readInt32()),sint32x3:e=>De(e.readInt32(),e.readInt32(),e.readInt32()),sint32x4:e=>_(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 f(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 f(n,r,t,a)},"loose-struct"(e,t){let r={};for(let[n,a]of Object.entries(t.propTypes))r[n]=B(e,a);return r},"loose-array"(e,t){let r=R(t),n=[];for(let a=0;a<t.elementCount;a++)y(e,r),n.push(B(e,t.elementType));return y(e,r),n},"loose-decorated"(e,t){y(e,X(t));let r=e.currentByteOffset,n=B(e,t.inner);return e.seekTo(r+b(t)),n}};function B(e,t){let r=M[t.type];if(!r)throw new Error(`Cannot read data of type '${t.type}'.`);return r(e,t)}function ft(e,t,r){return G(t)?new oe(e,t,r):new oe(e,t,r,["storage","uniform"])}function z(e){return e.resourceType==="buffer"}function Re(e){return!!e.usableAsUniform}function zt(e){return!!e.usableAsVertex}var oe=class{constructor(t,r,n,a){this._group=t;this.dataType=r;this.initialOrBuffer=n;this._disallowedUsages=a;s(this,"resourceType","buffer");s(this,"flags",GPUBufferUsage.COPY_DST|GPUBufferUsage.COPY_SRC);s(this,"_buffer",null);s(this,"_ownBuffer");s(this,"_destroyed",!1);s(this,"_label");s(this,"initial");s(this,"usableAsUniform",!1);s(this,"usableAsStorage",!1);s(this,"usableAsVertex",!1);Tt(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:b(this.dataType),usage:this.flags,mappedAtCreation:!!this.initial,label:(r=this.label)!=null?r:"<unnamed>"}),this.initial)){let n=new Le(this._buffer.getMappedRange());j(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} 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){return this.flags|=t,this}write(t){let r=this.buffer,n=this._group.device;if(r.mapState==="mapped"){let i=r.getMappedRange();j(new Le(i),this.dataType,t);return}let a=b(this.dataType);this._group.flush();let o=new ArrayBuffer(a);j(new Le(o),this.dataType,t),n.queue.writeBuffer(r,0,o,0,a)}copyFrom(t){if(this.buffer.mapState==="mapped")throw new Error("Cannot copy to a mapped buffer.");let r=b(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 B(new Ae(i),this.dataType)}if(t.usage&GPUBufferUsage.MAP_READ){await t.mapAsync(GPUMapMode.READ);let i=t.getMappedRange(),p=B(new Ae(i),this.dataType);return t.unmap(),p}let n=r.createBuffer({size:b(this.dataType),usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ}),a=r.createCommandEncoder();a.copyBufferToBuffer(t,0,n,0,b(this.dataType)),r.queue.submit([a.finish()]),await r.queue.onSubmittedWorkDone(),await n.mapAsync(GPUMapMode.READ,0,b(this.dataType));let o=B(new Ae(n.getMappedRange()),this.dataType);return n.unmap(),n.destroy(),o}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 se(e){return!!(e!=null&&e.usableAsStorage)}var ie=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)}};var Kt={uniform:"uniform",mutable:"storage, read_write",readonly:"storage, read"};var Z=class{constructor(t,r,n){this.usage=t;this.dataType=r;this._membership=n;s(this,"~repr");s(this,"resourceType","buffer-usage")}get label(){return this._membership.key}resolve(t){let r=t.names.makeUnique(this.label),n=t.allocateLayoutEntry(this._membership.layout),a=Kt[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(!rt())throw new Error("Cannot access buffer's value directly in JS.");return this}};function qt(e){return(e==null?void 0:e.resourceType)==="sampler"}function Jt(e){return(e==null?void 0:e.resourceType)==="sampler-comparison"}var ue=class{constructor(t){this._membership=t;s(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}},pe=class{constructor(t){this._membership=t;s(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}};var le=class{constructor(t){this._membership=t;s(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}};var ct={r8unorm:u,r8snorm:u,r8uint:S,r8sint:F,r16uint:S,r16sint:F,r16float:u,rg8unorm:u,rg8snorm:u,rg8uint:S,rg8sint:F,r32uint:S,r32sint:F,r32float:u,rg16uint:S,rg16sint:F,rg16float:u,rgba8unorm:u,"rgba8unorm-srgb":u,rgba8snorm:u,rgba8uint:S,rgba8sint:F,bgra8unorm:u,"bgra8unorm-srgb":u,rgb9e5ufloat:u,rgb10a2uint:S,rgb10a2unorm:u,rg11b10ufloat:u,rg32uint:S,rg32sint:F,rg32float:u,rgba16uint:S,rgba16sint:F,rgba16float:u,rgba32uint:S,rgba32sint:F,rgba32float:u,stencil8:u,depth16unorm:u,depth24plus:u,"depth24plus-stencil8":u,depth32float:u,"depth32float-stencil8":u,"bc1-rgba-unorm":u,"bc1-rgba-unorm-srgb":u,"bc2-rgba-unorm":u,"bc2-rgba-unorm-srgb":u,"bc3-rgba-unorm":u,"bc3-rgba-unorm-srgb":u,"bc4-r-unorm":u,"bc4-r-snorm":u,"bc5-rg-unorm":u,"bc5-rg-snorm":u,"bc6h-rgb-ufloat":u,"bc6h-rgb-float":u,"bc7-rgba-unorm":u,"bc7-rgba-unorm-srgb":u,"etc2-rgb8unorm":u,"etc2-rgb8unorm-srgb":u,"etc2-rgb8a1unorm":u,"etc2-rgb8a1unorm-srgb":u,"etc2-rgba8unorm":u,"etc2-rgba8unorm-srgb":u,"eac-r11unorm":u,"eac-r11snorm":u,"eac-rg11unorm":u,"eac-rg11snorm":u,"astc-4x4-unorm":u,"astc-4x4-unorm-srgb":u,"astc-5x4-unorm":u,"astc-5x4-unorm-srgb":u,"astc-5x5-unorm":u,"astc-5x5-unorm-srgb":u,"astc-6x5-unorm":u,"astc-6x5-unorm-srgb":u,"astc-6x6-unorm":u,"astc-6x6-unorm-srgb":u,"astc-8x5-unorm":u,"astc-8x5-unorm-srgb":u,"astc-8x6-unorm":u,"astc-8x6-unorm-srgb":u,"astc-8x8-unorm":u,"astc-8x8-unorm-srgb":u,"astc-10x5-unorm":u,"astc-10x5-unorm-srgb":u,"astc-10x6-unorm":u,"astc-10x6-unorm-srgb":u,"astc-10x8-unorm":u,"astc-10x8-unorm-srgb":u,"astc-10x10-unorm":u,"astc-10x10-unorm-srgb":u,"astc-12x10-unorm":u,"astc-12x10-unorm-srgb":u,"astc-12x12-unorm":u,"astc-12x12-unorm-srgb":u},ee={rgba8unorm:f,rgba8snorm:f,rgba8uint:v,rgba8sint:_,rgba16uint:v,rgba16sint:_,rgba16float:f,r32uint:v,r32sint:_,r32float:f,rg32uint:v,rg32sint:_,rg32float:f,rgba32uint:v,rgba32sint:_,rgba32float:f,bgra8unorm:f},xt={f32:"float",u32:"uint",i32:"sint"},bt={float:u,"unfilterable-float":u,uint:S,sint:F,depth:u};function ht(e,t){return new Pe(e,t)}function C(e){return(e==null?void 0:e.resourceType)==="texture"}function Ie(e){return(e==null?void 0:e.resourceType)==="texture-storage-view"}function Ce(e){return(e==null?void 0:e.resourceType)==="texture-sampled-view"}var wt={mutable:"read_write",readonly:"read",writeonly:"write"},Pe=class{constructor(t,r){this.props=t;this._branch=r;s(this,"resourceType","texture");s(this,"usableAsSampled",!1);s(this,"usableAsStorage",!1);s(this,"usableAsRender",!1);s(this,"_destroyed",!1);s(this,"_label");s(this,"_flags",GPUTextureUsage.COPY_DST|GPUTextureUsage.COPY_SRC);s(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}_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=ee[n];return J(!!a,`Unsupported storage texture format: ${n}`),new Ee(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(!ee[r])throw new Error(`Unsupported storage texture format: ${r}`);return new Ge(t,this)}destroy(){var t;this._destroyed||(this._destroyed=!0,(t=this._texture)==null||t.destroy())}},Te={"1d":"1d","2d":"2d","2d-array":"2d_array",cube:"cube","cube-array":"cube_array","3d":"3d"},Ee=class{constructor(t,r,n){this.access=r;this._texture=n;s(this,"resourceType","texture-storage-view");s(this,"texelDataType");s(this,"dimension");s(this,"_view");s(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=ee[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_${Te[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${a}) var ${r}: ${o}<${this._format}, ${wt[this.access]}>;`),r}},de=class{constructor(t,r,n,a){this._format=t;this.dimension=r;this.access=n;this._membership=a;s(this,"resourceType","texture-storage-view");s(this,"texelDataType");this.texelDataType=ee[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_${Te[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: ${a}<${this._format}, ${wt[this.access]}>;`),r}},Ge=class{constructor(t,r){this._props=t;this._texture=r;s(this,"resourceType","texture-sampled-view");s(this,"channelDataType");s(this,"dimension");s(this,"_format");s(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=ct[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(L({label:`${(t=this.label)!=null?t:"<unnamed>"} - View`},this._props))),this._view}resolve(t){var p;let r=t.names.makeUnique(this.label),n=((p=this._texture.props.sampleCount)!=null?p:1)>1,{group:a,binding:o}=t.allocateFixedEntry({texture:xt[this.channelDataType.type],viewDimension:this.dimension,multisampled:n},this),i=n?"texture_multisampled_2d":`texture_${Te[this.dimension]}`;return t.addDeclaration(`@group(${a}) @binding(${o}) var ${r}: ${i}<${t.resolve(this.channelDataType)}>;`),r}},me=class{constructor(t,r,n,a){this.dimension=r;this._multisampled=n;this._membership=a;s(this,"resourceType","texture-sampled-view");s(this,"channelDataType");this.channelDataType=bt[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_${Te[this.dimension]}`;return t.addDeclaration(`@group(${n}) @binding(${this._membership.idx}) var ${r}: ${a}<${t.resolve(this.channelDataType)}>;`),r}};function Ve(e){return!!(e!=null&&e.usableAsSampled)}function Yt(e){return!!(e!=null&&e.usableAsRender)}var ge=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 ye(e){return new Me(e)}function fe(e){return!!e&&e.resourceType==="bind-group-layout"}function St(e){return!!e&&e.resourceType==="bind-group"}var ke=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)}},Dt=["compute"],K=["compute","vertex","fragment"],Me=class{constructor(t){this.entries=t;s(this,"_label");s(this,"_index");s(this,"resourceType","bind-group-layout");s(this,"bound",{});var n,a,o,i,p;let r=0;for(let[d,l]of Object.entries(t)){if(l===null){r++;continue}let m={idx:r,key:d,layout:this};if("uniform"in l&&(this.bound[d]=new Z("uniform",l.uniform,m)),"storage"in l){let g="type"in l.storage?l.storage:l.storage(0);this.bound[d]=new Z((n=l.access)!=null?n:"readonly",g,m)}"texture"in l&&(this.bound[d]=new me(l.texture,(a=l.viewDimension)!=null?a:"2d",(o=l.multisampled)!=null?o:!1,m)),"storageTexture"in l&&(this.bound[d]=new de(l.storageTexture,(i=l.viewDimension)!=null?i:"2d",(p=l.access)!=null?p:"writeonly",m)),"externalTexture"in l&&(this.bound[d]=new le(m)),"sampler"in l&&(l.sampler==="comparison"?this.bound[d]=new pe(m):this.bound[d]=new ue(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 d,l,m,g;if(a===null)return null;let i=a.visibility,p={binding:o,visibility:0};if("uniform"in a)i=i!=null?i:K,p.buffer={type:"uniform"};else if("storage"in a)i=i!=null?i:a.access==="mutable"?Dt:K,p.buffer={type:a.access==="mutable"?"storage":"read-only-storage"};else if("sampler"in a)i=i!=null?i:K,p.sampler={type:a.sampler};else if("texture"in a)i=i!=null?i:K,p.texture={sampleType:a.texture,viewDimension:(d=a.viewDimension)!=null?d:"2d",multisampled:(l=a.multisampled)!=null?l:!1};else if("storageTexture"in a){let T=(m=a.access)!=null?m:"writeonly";i=i!=null?i:T==="readonly"?K:Dt,p.storageTexture={format:a.storageTexture,access:{mutable:"read-write",readonly:"read-only",writeonly:"write-only"}[T],viewDimension:(g=a.viewDimension)!=null?g:"2d"}}else"externalTexture"in a&&(i=i!=null?i:K,p.externalTexture={});return i!=null&&i.includes("compute")&&(p.visibility|=GPUShaderStage.COMPUTE),i!=null&&i.includes("vertex")&&(p.visibility|=GPUShaderStage.VERTEX),i!=null&&i.includes("fragment")&&(p.visibility|=GPUShaderStage.FRAGMENT),p}).filter(a=>a!==null)})}populate(t){return new $(this,t)}},$=class{constructor(t,r){this.layout=t;this.entries=r;s(this,"resourceType","bind-group");for(let n of Object.keys(t.entries))if(t.entries[n]!==null&&!(n in r))throw new ke(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 d;if(o===null)return null;let p=this.entries[a];if(p===void 0)throw new Error(`'${a}' is a resource required to populate bind group layout '${(d=this.layout.label)!=null?d:"<unnamed>"}'.`);if("uniform"in o){let l;if(z(p)){if(!Re(p))throw new re(p);l={buffer:t.unwrap(p)}}else l={buffer:p};return{binding:i,resource:l}}if("storage"in o){let l;if(z(p)){if(!se(p))throw new re(p);l={buffer:t.unwrap(p)}}else l={buffer:p};return{binding:i,resource:l}}if("texture"in o){let l;if(C(p)){if(!Ve(p))throw new ge(p);l=t.unwrap(p.asSampled())}else l=p;return{binding:i,resource:l}}if("storageTexture"in o){let l;if(C(p)){if(!se(p))throw new ie(p);o.access==="readonly"?l=t.unwrap(p.asReadonly()):o.access==="mutable"?l=t.unwrap(p.asMutable()):l=t.unwrap(p.asReadonly())}else l=p;return{binding:i,resource:l}}if("externalTexture"in o||"sampler"in o)return{binding:i,resource:p};throw new Error(`Malformed bind group entry: ${p} (${JSON.stringify(p)})`)}).filter(a=>a!==null)})}};var Ut="#CATCHALL#",We=class{constructor(){s(this,"_stack",[]);s(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})}pushSlotBindings(t){this._stack.push({type:"slotBinding",bindingMap:new WeakMap(t)})}pushFunctionScope(t,r,n){this._stack.push({type:"functionScope",args:t,returnType:r,externalMap:n})}pop(){let t=this._stack.pop();(t==null?void 0:t.type)==="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:D}: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}}}}},ce=[""," "," "," "," "," "," "," "," "],$e=ce.length-1,Oe=class{constructor(){s(this,"identLevel",0)}get pre(){var t;return(t=ce[this.identLevel])!=null?t:ce[$e].repeat(this.identLevel/$e)+ce[this.identLevel%$e]}indent(){let t=this.pre;return this.identLevel++,t}dedent(){return this.identLevel--,this.pre}},Ne=class{constructor(t){s(this,"_memoizedResolves",new WeakMap);s(this,"_memoizedDerived",new WeakMap);s(this,"_indentController",new Oe);s(this,"_jitTranspiler");s(this,"_itemStateStack",new We);s(this,"_declarations",[]);s(this,"bindGroupLayoutsToPlaceholderMap",new Map);s(this,"_nextFreeLayoutPlaceholderIdx",0);s(this,"fixedBindings",[]);s(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){var r;return(r=this._itemStateStack.getResourceById(t))!=null?r:{value:t,dataType:D}}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);let r=Be(this,t.body);this._itemStateStack.pop();let n=t.args.map(a=>`${a.value}: ${this.resolve(a.dataType)}`).join(", ");return{head:t.returnType!==void 0?`(${n}) -> ${this.resolve(t.returnType)}`:`(${n})`,body:r}}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:Ut,binding:n}}readSlot(t){let r=this._itemStateStack.readSlot(t);if(r===void 0)throw new et(t);return r}withSlots(t,r){this._itemStateStack.pushSlotBindings(t);try{return r()}finally{this._itemStateStack.pop()}}unwrap(t){let r=t;for(;;)if(N(r))r=this.readSlot(r);else if(ae(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(([d,l])=>d.areEqual(this._itemStateStack.readSlot(d),l)))return i.result;let a=t["~compute"](),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 O?a.appendToTrace(t):new O(a,[t])}finally{this._itemStateStack.pop()}}_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(([d,l])=>d.areEqual(this._itemStateStack.readSlot(d),l)))return i.result;let a=G(t)?Ue(this,t):t.resolve(this),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 O?a.appendToTrace(t):new O(a,[t])}finally{this._itemStateStack.pop()}}resolve(t){let r=this.unwrap(t);if(typeof r=="string"||typeof r=="number"||typeof r=="boolean")return String(r);if(this._itemStateStack.itemDepth===0){let n=tt(this,()=>this._getOrInstantiate(r));return`${[...this._declarations].join(`
|
15
13
|
|
16
|
-
`)}${
|
14
|
+
`)}${n}`}return this._getOrInstantiate(r)}resolveValue(t,r){if(Q(t))return this.resolve(t);if(Qe(r))return`array(${t.map(n=>this.resolveValue(n,r.elementType))})`;if(Ze(r))return`${this.resolve(r)}(${Object.entries(r.propTypes).map(([n,a])=>this.resolveValue(t[n],a))})`;throw new Error(`Value ${t} of schema ${r} is not resolvable to WGSL`)}};function q(e,t){var g;let r=new Ne(t),n=r.resolve(e),a=r.bindGroupLayoutsToPlaceholderMap,o=[],i=new Set([...a.keys()].map(T=>T.index).filter(T=>T!==void 0)),p=mt(i),d=r.fixedBindings.map((T,c)=>[String(c),T.layoutEntry]),l=()=>{let T=p.next().value,c=ye(Object.fromEntries(d));return o[T]=c,n=n.replaceAll(Ut,String(T)),[T,new $(c,Object.fromEntries(r.fixedBindings.map((x,A)=>[String(A),x.resource])))]},m=d.length>0?l():null;for(let[T,c]of a.entries()){let x=(g=T.index)!=null?g:p.next().value;o[x]=T,n=n.replaceAll(c,String(x))}return{code:n,bindGroupLayouts:o,catchall:m}}function Ft(e){return!!e&&(typeof e=="object"||typeof e=="function")&&"$name"in e}function _t(e,t){for(let[r,n]of Object.entries(t))e[r]=n,Ft(n)&&(!("label"in n)||n.label===void 0)&&n.$name(r)}function vt(e,t,r){return Object.entries(t).reduce((n,[a,o])=>{let i=Fe(o)||G(o)||N(o)?e.resolve(o):String(o);return n.replaceAll(new RegExp(`(?<![\\w_])${a}(?![\\w_])`,"g"),i)},r)}function Bt(e){let{input:t,extraDependencies:r,names:n,jitTranspiler:a}=e,o={};_t(o,r!=null?r:{});let i=(Array.isArray(t)?t:[t]).filter(m=>typeof m=="string"),p=(Array.isArray(t)?t:[t]).filter(m=>typeof m!="string"),d={resolve(m){let g=i.join(`
|
17
15
|
|
18
|
-
`);for(let
|
16
|
+
`);for(let T of p)m.resolve(T);return vt(m,o,g)}};Object.defineProperty(d,"toString",{value:()=>"<root>"});let{code:l}=q(d,{names:n==="strict"?new k:new V,jitTranspiler:a});return l}var te=class{constructor(t){this._make=t;s(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 At(e,t,r){return new je(new ze(e,t,r),{})}function Lt(e){return(e==null?void 0:e.resourceType)==="compute-pipeline"}var je=class e{constructor(t,r){this._core=t;this._priors=r;s(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){var i;let a=this._core.unwrap(),o=this._core.branch.commandEncoder.beginComputePass({label:(i=this._core.label)!=null?i:"<unnamed>"});o.setPipeline(a.pipeline),a.bindGroupLayouts.forEach((p,d)=>{var l;if(a.catchall&&d===a.catchall[0])o.setBindGroup(d,this._core.branch.unwrap(a.catchall[1]));else{let m=(l=this._priors.bindGroupLayoutMap)==null?void 0:l.get(p);if(m===void 0)throw new ne(p.label);o.setBindGroup(d,this._core.branch.unwrap(m))}}),o.dispatchWorkgroups(t,r,n),o.end()}$name(t){return this._core.label=t,this}},ze=class{constructor(t,r,n){this.branch=t;this._slotBindings=r;this._entryFn=n;s(this,"label");s(this,"_memo")}unwrap(){var t,r,n,a,o;if(this._memo===void 0){let i=this.branch.device,{code:p,bindGroupLayouts:d,catchall:l}=q({resolve:m=>(m.withSlots(this._slotBindings,()=>{m.resolve(this._entryFn)}),"")},{names:this.branch.nameRegistry,jitTranspiler:this.branch.jitTranspiler});l!==null&&((r=d[l[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:d.map(m=>this.branch.unwrap(m))}),compute:{module:i.createShaderModule({label:`${(o=this.label)!=null?o:"<unnamed>"} - Shader`,code:p})}}),bindGroupLayouts:d,catchall:l}}return this._memo}};function Ht(e){return typeof(e==null?void 0:e.format)=="string"}function Rt(e,t){var i,p;let r=[];if(W(e)){if(!Ht(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=be(e))!=null?i:0}]}]}}let n=[],a=new WeakMap,o=0;for(let[d,l]of Object.entries(e)){if(it(l))continue;let m=t[d];if(!m)throw new Error(`An attribute by the name of '${d}' was not provided to the shader.`);let g=m._layout,T=a.get(g);T||(r.push(g),T=[],n.push({arrayStride:g.stride,stepMode:g.stepMode,attributes:T}),a.set(g,T)),o=(p=be(l))!=null?p:o,T.push({format:m.format,offset:m.offset,shaderLocation:o++})}return{usedVertexLayouts:r,bufferDefinitions:n}}function Pt(e){return(e==null?void 0:e.resourceType)==="vertex-layout"}function Xt(e){return typeof(e==null?void 0:e.loadOp)=="string"}function Et(e,t){if(W(e)){if(!Xt(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 Qt(e){return typeof(e==null?void 0:e.format)=="string"}function Gt(e,t){if(W(e)){if(!Qt(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 It(e){return new Ke(new qe(e),{})}var Ke=class e{constructor(t,r){this._core=t;this._priors=r;s(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(fe(t))return new e(this._core,E(L({},this._priors),{bindGroupLayoutMap:new Map([...(n=this._priors.bindGroupLayoutMap)!=null?n:[],[t,r]])}));if(Pt(t))return new e(this._core,E(L({},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,E(L({},this._priors),{colorAttachment:t}))}draw(t,r,n,a){var m,g;let o=this._core.unwrap(),{branch:i,fragmentFn:p}=this._core.options,d=Et(p.shell.returnType,(m=this._priors.colorAttachment)!=null?m:{}).map(T=>C(T.view)?E(L({},T),{view:i.unwrap(T.view).createView()}):T),l=i.commandEncoder.beginRenderPass({label:(g=this._core.label)!=null?g:"<unnamed>",colorAttachments:d});l.setPipeline(o.pipeline),o.bindGroupLayouts.forEach((T,c)=>{var x;if(o.catchall&&c===o.catchall[0])l.setBindGroup(c,i.unwrap(o.catchall[1]));else{let A=(x=this._priors.bindGroupLayoutMap)==null?void 0:x.get(T);if(A===void 0)throw new ne(T.label);l.setBindGroup(c,i.unwrap(A))}}),this._core.usedVertexLayouts.forEach((T,c)=>{var A,P;let x=(A=this._priors.vertexLayoutMap)==null?void 0:A.get(T);if(!x)throw new Error(`Missing vertex buffer for layout '${(P=T.label)!=null?P:"<unnamed>"}'. Please provide it using pipeline.with(layout, buffer).(...)`);l.setVertexBuffer(c,i.unwrap(x))}),l.draw(t,r,n,a),l.end()}},qe=class{constructor(t){this.options=t;s(this,"label");s(this,"usedVertexLayouts");s(this,"_memo");s(this,"_vertexBufferLayouts");s(this,"_targets");let r=Rt(t.vertexFn.shell.argTypes[0],t.vertexAttribs);this._vertexBufferLayouts=r.bufferDefinitions,this.usedVertexLayouts=r.usedVertexLayouts,this._targets=Gt(t.fragmentFn.shell.returnType,t.targets)}unwrap(){var t,r,n,a,o;if(this._memo===void 0){let{branch:i,vertexFn:p,fragmentFn:d,slotBindings:l,primitiveState:m}=this.options,{code:g,bindGroupLayouts:T,catchall:c}=q({resolve:P=>(P.withSlots(l,()=>{P.resolve(p),P.resolve(d)}),"")},{names:i.nameRegistry,jitTranspiler:i.jitTranspiler});c!==null&&((r=T[c[0]])==null||r.$name(`${(t=this.label)!=null?t:"<unnamed>"} - Automatic Bind Group & Layout`));let x=i.device,A=x.createShaderModule({label:`${(n=this.label)!=null?n:"<unnamed>"} - Shader`,code:g});this._memo={pipeline:x.createRenderPipeline({label:(a=this.label)!=null?a:"<unnamed>",layout:x.createPipelineLayout({label:`${(o=this.label)!=null?o:"<unnamed>"} - Pipeline Layout`,bindGroupLayouts:T.map(P=>i.unwrap(P))}),vertex:{module:A,buffers:this._vertexBufferLayouts},fragment:{module:A,targets:this._targets},primitive:m!=null?m:{}}),bindGroupLayouts:T,catchall:c}}return this._memo}};var Je=class e{constructor(t,r){this._getRoot=t;this._slotBindings=r}with(t,r){return new e(this._getRoot,[...this._slotBindings,[t,r]])}withCompute(t){return new Ye(this._getRoot(),this._slotBindings,t)}withVertex(t,r){return new He({branch:this._getRoot(),primitiveState:void 0,slotBindings:this._slotBindings,vertexFn:t,vertexAttribs:r})}},Ye=class{constructor(t,r,n){this._root=t;this._slotBindings=r;this._entryFn=n}createPipeline(){return At(this._root,this._slotBindings,this._entryFn)}},He=class{constructor(t){this._options=t}withFragment(t,r,n){return J(typeof t!="string","Just type mismatch validation"),J(typeof r!="string","Just type mismatch validation"),new Xe(E(L({},this._options),{fragmentFn:t,targets:r}))}},Xe=class e{constructor(t){this._options=t}withPrimitive(t){return new e(E(L({},this._options),{primitiveState:t}))}createPipeline(){return It(this._options)}},xe=class extends Je{constructor(r,n,a,o){super(()=>this,[]);this.device=r;this.nameRegistry=n;this.jitTranspiler=a;this._ownDevice=o;s(this,"_disposables",[]);s(this,"_unwrappedBindGroupLayouts",new te(r=>r.unwrap(this)));s(this,"_unwrappedBindGroups",new te(r=>r.unwrap(this)));s(this,"_commandEncoder",null)}get commandEncoder(){return this._commandEncoder||(this._commandEncoder=this.device.createCommandEncoder()),this._commandEncoder}createBuffer(r,n){let a=ft(this,r,n);return this._disposables.push(a),a}createTexture(r){let n=ht(r,this);return this._disposables.push(n),n}createBindGroup(r,n){return new $(r,n)}destroy(){for(let r of this._disposables)r.destroy();this._ownDevice&&this.device.destroy()}unwrap(r){if(Lt(r))return r.rawPipeline;if(fe(r))return this._unwrappedBindGroupLayouts.getOrMake(r);if(St(r))return this._unwrappedBindGroups.getOrMake(r);if(z(r))return r.buffer;if(C(r)||Ie(r)||Ce(r))return r.unwrap();throw new Error(`Unknown resource type: ${r}`)}flush(){this._commandEncoder&&(this.device.queue.submit([this._commandEncoder.finish()]),this._commandEncoder=null)}};async function Ct(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 xe(await o.requestDevice(r),n==="random"?new V:new k,a,!0)}function Vt(e){let{device:t,unstable_names:r="random",unstable_jitTranspiler:n}=e!=null?e:{};return new xe(t,r==="random"?new V:new k,n,!1)}var Zt={Uniform:"uniform",Storage:"storage",Vertex:"vertex",bindGroupLayout:ye,init:Ct,initFromDevice:Vt,resolve:Bt},pa=Zt;export{re as NotUniformError,O as ResolutionError,pa as default,z as isBuffer,Jt as isComparisonSampler,Ce as isSampledTextureView,qt as isSampler,Ie as isStorageTextureView,C as isTexture,Yt as isUsableAsRender,Ve as isUsableAsSampled,se as isUsableAsStorage,Re as isUsableAsUniform,zt as isUsableAsVertex,Zt as tgpu};
|
19
17
|
//# sourceMappingURL=index.js.map
|