glre 0.37.0 → 0.39.0
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/dist/addons.cjs +2 -0
- package/dist/addons.cjs.map +1 -0
- package/dist/addons.d.ts +457 -0
- package/dist/addons.js +2 -0
- package/dist/addons.js.map +1 -0
- package/dist/index.cjs +31 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +170 -1791
- package/dist/index.js +31 -40
- package/dist/index.js.map +1 -1
- package/dist/native.cjs +1 -45
- package/dist/native.cjs.map +1 -1
- package/dist/native.d.ts +511 -11
- package/dist/native.js +1 -45
- package/dist/native.js.map +1 -1
- package/dist/node.cjs +40 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.ts +614 -0
- package/dist/node.js +40 -0
- package/dist/node.js.map +1 -0
- package/dist/react.cjs +1 -45
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.ts +506 -4
- package/dist/react.js +1 -45
- package/dist/react.js.map +1 -1
- package/dist/solid.cjs +1 -45
- package/dist/solid.cjs.map +1 -1
- package/dist/solid.d.ts +506 -4
- package/dist/solid.js +1 -45
- package/dist/solid.js.map +1 -1
- package/package.json +56 -3
- package/src/addons/index.ts +6 -0
- package/src/index.ts +6 -22
- package/src/node/{core.ts → build.ts} +3 -7
- package/src/node/create.ts +73 -0
- package/src/node/index.ts +63 -47
- package/src/node/scope.ts +65 -50
- package/src/node/types.ts +222 -164
- package/src/node/utils/const.ts +64 -3
- package/src/node/utils/index.ts +8 -5
- package/src/node/utils/infer.ts +23 -35
- package/src/node/utils/parse.ts +15 -18
- package/src/node/utils/utils.ts +13 -12
- package/src/types.ts +5 -7
- package/src/utils/pipeline.ts +3 -3
- package/src/utils/program.ts +7 -2
- package/src/{webgl.ts → utils/webgl.ts} +24 -14
- package/src/{webgpu.ts → utils/webgpu.ts} +28 -7
- package/src/node/node.ts +0 -64
package/dist/index.js
CHANGED
|
@@ -1,46 +1,37 @@
|
|
|
1
|
-
import{durable as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
${
|
|
5
|
-
}`:` else if (${f(n[s],e)}) {
|
|
6
|
-
${f(n[s+1],e)}
|
|
7
|
-
}`}return o},Qe=(e,t,r)=>{let n=`switch (${f(t,e)}) {
|
|
8
|
-
`;for(let o=1;o<r.length;o+=2)o>=r.length-1&&r.length%2===0?n+=`default:
|
|
9
|
-
${f(r[o],e)}
|
|
10
|
-
break;
|
|
11
|
-
`:o+1<r.length&&(n+=`case ${f(r[o],e)}:
|
|
12
|
-
${f(r[o+1],e)}
|
|
13
|
-
break;
|
|
14
|
-
`);return n+="}",n},Je=(e,t,r)=>{let n=E(t,e),o=r?.props?.id;if(e.isWebGL)return`${n} ${o} = ${f(t,e)};`;let s=N(n);return`var ${o}: ${s} = ${f(t,e)};`},et=(e,t,r={})=>{let n=[];for(let s in r){let i=r[s],p=E(i,e);e.isWebGL&&Y(e,t,p),n.push(e.isWebGL?`${p} ${s};`:`${s}: ${N(p,e)},`)}let o=n.join(`
|
|
15
|
-
`);return`struct ${t} {
|
|
16
|
-
${o}
|
|
17
|
-
};`},tt=(e,t,r="",n,o)=>{if(e.isWebGL)if(o){let s=[];for(let i in n)s.push(o[i]);return`${t} ${r} = ${t}(${S(s,e)});`}else return`${t} ${r};`;else if(o){let s=[];for(let i in n)s.push(o[i]);return`var ${r}: ${t} = ${t}(${S(s,e)});`}else return`var ${r}: ${t};`},rt=(e,t,r)=>{let{id:n,children:o=[],layout:s}=t,[i,...p]=o,a=[],u=[];if(s?.inputs)for(let T of s.inputs)a.push([T.name,T.type]);else for(let T=0;T<p.length;T++)a.push([`p${T}`,E(p[T],e)]);let l=[];if(e?.isWebGL){for(let[T,d]of a)Y(e,n,d),u.push(`${d} ${T}`);Y(e,n,r),l.push(`${r} ${n}(${u}) {`)}else{for(let[d,g]of a)u.push(`${d}: ${N(g,e)}`);r==="void"?l.push(`fn ${n}(${u}) {`):l.push(`fn ${n}(${u}) -> ${N(r,e)} {`)}let y=f(i,e);return y&&l.push(y),l.push("}"),l.join(`
|
|
18
|
-
`)},ot=(e,t,r)=>e.isWebGL?`${r} ${t};`:`@location(${e.code?.vertVaryings?.size||0}) ${t}: ${N(r,e)}`,nt=(e,t,r)=>{if(e.isWebGL)return`${r} ${t};`;let{location:n=0}=e.gl?.webgpu?.attribs.map.get(t)||{},o=N(r,e);return`@location(${n}) ${t}: ${o}`},st=(e,t,r)=>{let n=r==="sampler2D"||r==="texture";if(e.isWebGL)return n?`uniform sampler2D ${t};`:`uniform ${r} ${t};`;if(n){let{group:p=1,binding:a=0}=e.gl?.webgpu?.textures.map.get(t)||{};return`@group(${p}) @binding(${a}) var ${t}Sampler: sampler;
|
|
19
|
-
@group(${p}) @binding(${a+1}) var ${t}: texture_2d<f32>;`}let{group:o=0,binding:s=0}=e.gl?.webgpu?.uniforms.map.get(t)||{},i=N(r,e);return`@group(${o}) @binding(${s}) var<uniform> ${t}: ${i};`},it=(e,t,r)=>{if(e.isWebGL){let i=`uniform sampler2D ${t};`;if(e.label!=="compute")return i;let p=e.units?.(t);return`${i}
|
|
20
|
-
layout(location = ${p}) out vec4 _${t};`}let{group:n=0,binding:o=0}=e.gl?.webgpu?.storages.map.get(t)||{},s=N(r,e);return`@group(${n}) @binding(${o}) var<storage, read_write> ${t}: array<${s}>;`},at=(e,t,r,n)=>e.isWebGL?`const ${r} ${t} = ${n};`:`const ${t}: ${N(r,e)} = ${n};`;var f=(e,t)=>{if(t||(t={}),Ve(t),x.arr(e))return S(e,t);if(x.str(e))return e;if(x.num(e)){let d=`${e}`;return d.includes(".")?d:d+".0"}if(x.bol(e))return e?"true":"false";if(!e)return"";let{type:r,props:n={}}=e,{id:o="i",children:s=[],fields:i,initialValues:p}=n,[a,u,l,y]=s;if(r==="variable")return o;if(r==="member")return`${f(a,t)}.${f(u,t)}`;if(r==="element")return`${f(a,t)}[${f(u,t)}]`;if(r==="gather")return t.isWebGL?He(t,a,u,e):`${f(a,t)}[${f(u,t)}]`;if(r==="scatter"){let[d,g]=a.props.children??[];return t.isWebGL?je(t,d,u):`${f(d,t)}[${f(g,t)}] = ${f(u,t)};`}if(r==="ternary")return t.isWebGL?`(${f(l,t)} ? ${f(a,t)} : ${f(u,t)})`:`select(${f(a,t)}, ${f(u,t)}, ${f(l,t)})`;if(r==="conversion")return`${N(a,t)}(${S(s.slice(1),t)})`;if(r==="operator")return a==="not"||a==="bitNot"?`!${f(u,t)}`:`(${f(u,t)} ${We(a)} ${f(l,t)})`;if(r==="function")return a==="negate"?`(-${f(u,t)})`:a==="oneMinus"?`(1.0-${f(u,t)})`:a==="texture"?Ke(t,u,l,y):a==="atan2"&&t.isWebGL?`atan(${f(u,t)}, ${f(l,t)})`:`${a}(${S(s.slice(1),t)})`;if(r==="scope")return s.map(d=>f(d,t)).join(`
|
|
21
|
-
`);if(r==="assign")return`${f(a,t)} = ${f(u,t)};`;if(r==="return")return`return ${f(a,t)};`;if(r==="loop")return t.isWebGL?`for (int ${o} = 0; ${o} < ${f(a,t)}; ${o} += 1) {
|
|
22
|
-
${f(u,t)}
|
|
23
|
-
}`:`for (var ${o}: i32 = 0; ${o} < ${f(a,t)}; ${o}++) {
|
|
24
|
-
${f(u,t)}
|
|
25
|
-
}`;if(r==="if")return Ze(t,a,u,s);if(r==="switch")return Qe(t,a,s);if(r==="declare")return Je(t,a,u);if(r==="define")return t.code?.headers.has(o)||t.code?.headers.set(o,rt(t,n,E(e,t))),`${o}(${S(s.slice(1),t)})`;if(r==="struct")return t.code?.headers.has(o)||t.code?.headers.set(o,et(t,o,i)),tt(t,o,a.props.id,i,p);if(r==="varying"){if(t.code?.vertOutputs.has(o))return t.isWebGL?`${o}`:`out.${o}`;let d=ot(t,o,E(e,t));return t.code?.fragInputs.set(o,d),t.code?.vertOutputs.set(o,d),t.code?.vertVaryings.set(o,f(a,t)),t.isWebGL?`${o}`:`out.${o}`}if(r==="builtin"){if(t.isWebGL)return Be(t,o);if(o==="position")return"out.position";let d=`@builtin(${o}) ${o}: ${N(E(e,t),t)}`;return t.label==="compute"?t.code?.computeInputs.set(o,d):t.label==="frag"?t.code?.fragInputs.set(o,d):t.label==="vert"&&t.code?.vertInputs.set(o,d),`in.${o}`}if(r==="attribute"){let d=de(t,o,!0);return xe(a,d),e.listeners.add(d),t.code?.vertInputs.set(o,nt(t,o,E(e,t))),t.isWebGL?`${o}`:`in.${o}`}if(t.code?.headers.has(o))return o;let T="";if(r==="uniform"){let d=E(e,t),g=de(t,o,!1,d==="texture");xe(a,g),e.listeners.add(g),T=st(t,o,d)}return r==="storage"&&(T=it(t,o,E(e,t))),r==="constant"&&(T=at(t,o,E(e,t),f(a,t))),T?(t.code?.headers.set(o,T),o):f(a,t)};var F=null,O=null,I=e=>{if(!F||(F.props.children||(F.props.children=[]),F.props.children.push(e),e.type!=="return"||!O))return;let{props:t}=O;t.inferFrom||(t.inferFrom=[]),t.inferFrom.push(e)},ut=(e,t)=>{t||(t=C());let r=m("variable",{id:t,inferFrom:[e]}),n=m("declare",null,e,r);return I(n),r},pt=(e,t=!1,r)=>{let n=m(t?"scatter":"assign",null,e,r);return I(n),e},St=e=>{let t=m("return",{inferFrom:[e]},e);return I(t),t},Gr=(e,t=C())=>(r={},n=C())=>{let o=m("variable",{id:n,inferFrom:[t]}),s=m("struct",{id:t,fields:e,initialValues:r},o);return I(s),o},A=(e,t,r=O)=>{let[n,o]=[F,O];[F,O]=[e,r];let s=t();s&&St(s),[F,O]=[n,o]},Lr=(e,t)=>{let r=m("scope");A(r,t);let n=m("if",null,e,r);I(n);let o=()=>({ElseIf:(s,i)=>{let p=m("scope");return A(p,i),n.props.children.push(s,p),o()},Else:s=>{let i=m("scope");A(i,s),n.props.children.push(i)}});return o()},Nr=(e,t)=>{let r=m("scope"),n=C();A(r,()=>t({i:m("variable",{id:n,inferFrom:[b("int",0)]})}));let o=m("loop",{id:n},e,r);return I(o),o},Rr=e=>{let t=m("switch",null,e);I(t);let r=()=>({Case:(...n)=>o=>{let s=m("scope");A(s,o);for(let i of n)t.props.children.push(i,s);return r()},Default:n=>{let o=m("scope");A(o,n),t.props.children.push(o)}});return r()},wr=(e,t=C())=>{let r,n=(...o)=>{let s=r?.name||t,i=m("scope"),p=[],a=[];if(r?.inputs)for(let l of r.inputs)a.push({id:l.name,inferFrom:[b(l.type)]});else for(let l=0;l<o.length;l++)a.push({id:`p${l}`,inferFrom:[o[l]]});for(let l of a)p.push(m("variable",l));let u=m("define",{id:s,layout:r},i,...o);return A(i,()=>e(p),u),u};return n.setLayout=o=>(r=o,n),n};var Ut=(e,t)=>{if(t==="string")return f(e,null)},m=(e,t,...r)=>{t||(t={}),r.length&&(t.children=r);let n=new Set,o=(p,a)=>{if(a==="type")return e;if(a==="props")return t;if(a==="toVar")return ut.bind(null,i);if(a==="isProxy")return!0;if(a==="toString")return f.bind(null,i);if(a===Symbol.toPrimitive)return Ut.bind(null,i);if(a==="listeners")return n;if(a==="attribute")return(u=C())=>Ft(i,u);if(a==="constant")return(u=C())=>At(i,u);if(a==="uniform")return(u=C())=>W(i,u);if(a==="variable")return(u=C())=>It(u);if(a==="builtin")return(u=C())=>_(u);if(a==="vertexStage")return(u=C())=>Ot(i,u);if(a==="element")return u=>e==="storage"?Dt(i,u):ft(i,u);if(a==="member")return u=>Te(i,u);if(a==="assign")return pt.bind(null,i,i.type==="gather");if(Ie(a))return(...u)=>Mt(a,i,...u);if(Oe(a))return(...u)=>c(a,i,...u);if(De(a))return()=>b(ke(a),i);if(x.str(a))return ze(a)?ft(i,a):Te(i,a)},s=(p,a,u)=>(a==="value"&&n.forEach(l=>l(u)),x.str(a)&&Te(i,a).assign(u),!0),i=new Proxy({},{get:o,set:s});return i},Ft=(e,t=C())=>m("attribute",{id:t},e),At=(e,t=C())=>m("constant",{id:t},e),W=(e,t=C())=>m("uniform",{id:t},e),Ir=(e,t=C())=>m("storage",{id:t},e),It=(e=C())=>m("variable",{id:e}),_=(e=C())=>m("builtin",{id:e}),Ot=(e,t=C())=>m("varying",{id:t,inferFrom:[e]},e),Te=(e,t)=>m("member",null,e,t),ft=(e,t)=>m("element",null,e,t),Dt=(e,t)=>m("gather",null,e,t),Or=(e,t)=>m("scatter",null,e,t),Dr=(e,t,r)=>m("ternary",null,e,t,r),Mt=(e,...t)=>m("operator",null,e,...t),c=(e,...t)=>m("function",null,e,...t),b=(e,...t)=>m("conversion",null,e,...t);var Bt=`
|
|
1
|
+
import{durable as B,event as de}from"reev";import{createFrame as Ee,createQueue as ge}from"refr";var _={arr:Array.isArray,bol:e=>typeof e=="boolean",str:e=>typeof e=="string",num:e=>typeof e=="number",int:e=>Number.isInteger(e),fun:e=>typeof e=="function",und:e=>typeof e>"u",nul:e=>e===null,set:e=>e instanceof Set,map:e=>e instanceof Map,obj:e=>!!e&&e.constructor.name==="Object",nan:e=>typeof e=="number"&&Number.isNaN(e)};var W=e=>e instanceof Float32Array,F=(e,t,r)=>{e.loading++;let n=new Image;Object.assign(n,{src:t,crossOrigin:"anonymous"}),n.decode().then(()=>{r(n),e.loading--})};import{nested as A}from"reev";var N=(e,t,r,n=console.warn)=>{let o=e.createShader(r);if(!o)return n("Failed to create shader");if(e.shaderSource(o,t.trim()),e.compileShader(o),e.getShaderParameter(o,e.COMPILE_STATUS))return o;let a=e.getShaderInfoLog(o);e.deleteShader(o),n(`Could not compile shader: ${a}
|
|
2
|
+
|
|
3
|
+
\u2193\u2193\u2193generated\u2193\u2193\u2193
|
|
4
|
+
${t}`)},C=(e,t,r,n)=>{let o=e.createProgram(),a=N(e,t,e.FRAGMENT_SHADER,n.error),s=N(e,r,e.VERTEX_SHADER,n.error);if(!a||!s)return;if(e.attachShader(o,a),e.attachShader(o,s),e.linkProgram(o),e.getProgramParameter(o,e.LINK_STATUS))return o;let i=e.getProgramInfoLog(o);e.deleteProgram(o),n.error(`Could not link program: ${i}`)},oe=(e,t)=>{let r=e.createBuffer();return e.bindBuffer(e.ARRAY_BUFFER,r),e.bufferData(e.ARRAY_BUFFER,new Float32Array(t),e.STATIC_DRAW),e.bindBuffer(e.ARRAY_BUFFER,null),r},ae=(e,t)=>{let r=e.createBuffer();return e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,r),e.bufferData(e.ELEMENT_ARRAY_BUFFER,new Int16Array(t),e.STATIC_DRAW),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,null),r},se=(e,t,r)=>{r&&(e=Math.max(...r)+1);let n=t.length/e;return Math.floor(n)},O=(e,t,r,n,o)=>{let a=oe(e,n),s=ae(e,o),i=se(r,n,o);e.bindBuffer(e.ARRAY_BUFFER,a),e.enableVertexAttribArray(t),e.vertexAttribPointer(t,i,e.FLOAT,!1,0,0),s&&e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,s)},I=(e,t,r)=>{if(_.num(r))return e.uniform1f(t,r);let n=r.length;if(n<=4)return e[`uniform${n}fv`](t,r);n=Math.sqrt(n)<<0,e[`uniformMatrix${n}fv`](t,!1,r)},V=(e,t,r,n)=>{let o=e.createTexture();e.bindTexture(e.TEXTURE_2D,o),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,t),e.generateMipmap(e.TEXTURE_2D),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),e.uniform1i(r,n),e.activeTexture(e.TEXTURE0+n),e.bindTexture(e.TEXTURE_2D,o)},k=(e,t,r,n,o,a,s)=>{let i=r*r,f=t.length/i;for(let u=0;u<i;u++)for(let m=0;m<Math.min(f,4);m++)s[4*u+m]=t[u*f+m]||0;e.activeTexture(e.TEXTURE0+a),e.bindTexture(e.TEXTURE_2D,n.texture),e.texImage2D(e.TEXTURE_2D,0,e.RGBA32F,r,r,0,e.RGBA,e.FLOAT,s),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,o.texture),e.texImage2D(e.TEXTURE_2D,0,e.RGBA32F,r,r,0,e.RGBA,e.FLOAT,s),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)},q=(e,t)=>{for(let{ping:r,pong:n}of t)e.deleteTexture(r.texture),e.deleteTexture(n.texture),e.deleteFramebuffer(r.buffer),e.deleteFramebuffer(n.buffer)},z=(e,t,r,n,o,a)=>{e.activeTexture(e.TEXTURE0+o),e.bindTexture(e.TEXTURE_2D,t.texture),e.uniform1i(n,o),a===0&&e.bindFramebuffer(e.FRAMEBUFFER,r.buffer);let s=e.COLOR_ATTACHMENT0+a;return e.framebufferTexture2D(e.FRAMEBUFFER,s,e.TEXTURE_2D,r.texture,0),s};var ie=`
|
|
26
5
|
#version 300 es
|
|
27
6
|
precision mediump float;
|
|
28
7
|
out vec4 fragColor;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}`,j=(e,t)=>{if(x.str(e))return e.trim();t.code?.headers?.clear(),t.label="frag";let[r,n]=ge(e,t),o=[];if(t.isWebGL){o.push(Bt);for(let i of t.code?.fragInputs?.values()||[])o.push(`in ${i}`);o.push(r),o.push(`void main() {
|
|
35
|
-
fragColor = ${n};`)}else t.code?.fragInputs?.size&&o.push(H("Out",t.code.fragInputs)),o.push(r),o.push(`@fragment
|
|
36
|
-
fn main(out: Out) -> @location(0) vec4f {`),o.push(` return ${n};`);return o.push("}"),o.filter(Boolean).join(`
|
|
37
|
-
`).trim()},K=(e,t)=>{if(x.str(e))return e.trim();t.code?.headers?.clear(),t.label="vert";let[r,n]=ge(e,t),o=[];if(t.isWebGL){o.push("#version 300 es");for(let i of t.code?.vertInputs?.values()||[])o.push(`in ${i}`);for(let i of t.code?.vertOutputs?.values()||[])o.push(`out ${i}`);o.push(r),o.push("void main() {"),o.push(` gl_Position = ${n};`);for(let[i,p]of t.code?.vertVaryings?.entries()||[])o.push(` ${i} = ${p};`)}else{t.code?.vertInputs?.size&&o.push(H("In",t.code.vertInputs)),t.code?.vertOutputs?.size&&o.push(H("Out",t.code.vertOutputs)),o.push(r),o.push("@vertex"),o.push(`fn main(${t.code?.vertInputs?.size?"in: In":""}) -> Out {`),o.push(" var out: Out;"),o.push(` out.position = ${n};`);for(let[i,p]of t.code?.vertVaryings?.entries()||[])o.push(` out.${i} = ${p};`);o.push(" return out;")}return o.push("}"),o.filter(Boolean).join(`
|
|
38
|
-
`).trim()},Z=(e,t)=>{if(x.str(e))return e.trim();t.code?.headers?.clear(),t.label="compute";let[r,n]=ge(e,t),o=[];return t.isWebGL?(o.push("#version 300 es"),o.push("precision mediump float;"),o.push(r),o.push("void main() {"),o.push(` ${n};`),o.push("}")):(t.code?.computeInputs?.size&&o.push(H("In",t.code.computeInputs)),o.push(r),o.push("@compute @workgroup_size(32)"),o.push(`fn main(${t.code?.computeInputs?.size?"in: In":""}) {`),o.push(` ${n};`),o.push("}")),o.filter(Boolean).join(`
|
|
39
|
-
`).trim()};var be=_("position"),ve=_("vertex_index"),Yr=_("instance_index"),Hr=_("front_facing"),jr=_("frag_depth"),Kr=_("sample_index"),Zr=_("sample_mask"),Qr=_("point_coord"),Jr=_("global_invocation_id"),eo=_("position"),to=_("positionWorld"),ro=_("positionView"),oo=_("normalLocal"),no=_("normalWorld"),so=_("normalView"),io=_("screenCoordinate"),ao=_("screenUV"),Q=e=>b("float",e),k=e=>b("int",e),uo=e=>b("uint",e),po=e=>b("bool",e),lt=(e,t)=>b("vec2",e,t),ct=(e,t,r)=>b("vec3",e,t,r),he=(e,t,r,n)=>b("vec4",e,t,r,n),fo=(...e)=>b("mat2",...e),co=(...e)=>b("mat3",...e),lo=(...e)=>b("mat4",...e),xo=(e,t)=>b("ivec2",e,t),mo=(e,t,r)=>b("ivec3",e,t,r),To=(e,t,r,n)=>b("ivec4",e,t,r,n),go=(e,t)=>b("uvec2",e,t),bo=(e,t,r)=>b("uvec3",e,t,r),vo=(e,t,r,n)=>b("uvec4",e,t,r,n),ho=(e,t)=>b("bvec2",e,t),yo=(e,t,r)=>b("bvec3",e,t,r),Eo=(e,t,r,n)=>b("bvec4",e,t,r,n),Xo=e=>b("texture",e),Co=()=>b("sampler2D"),_o=(e,t,r)=>x.num(e)&&x.und(t)&&x.und(r)?ct(...Me(e)):ct(e,t,r),ye=W(lt(),"iResolution"),Po=W(lt(),"iMouse"),$o=W(Q(),"iTime"),Go=be.xy.div(ye),Lo=e=>c("all",e),No=e=>c("any",e),Ro=e=>c("length",e),wo=e=>c("lengthSq",e),So=(e,t)=>c("distance",e,t),Uo=(e,t)=>c("dot",e,t),Fo=(e,t)=>c("cross",e,t),Ao=(e,t,r)=>c("cubeTexture",e,t,r),Io=(e,t,r)=>c("texture",e,t,r),Oo=(e,t,r)=>c("texelFetch",e,t,r),Do=(e,t,r)=>c("textureLod",e,t,r),Mo=e=>c("abs",e),Bo=e=>c("acos",e),Wo=e=>c("acosh",e),ko=e=>c("asin",e),Vo=e=>c("asinh",e),zo=e=>c("atan",e),qo=e=>c("atanh",e),Yo=e=>c("ceil",e),Ho=e=>c("cos",e),jo=e=>c("cosh",e),Ko=e=>c("dFdx",e),Zo=e=>c("dFdy",e),Qo=e=>c("degrees",e),Jo=e=>c("exp",e),en=e=>c("exp2",e),tn=e=>c("floor",e),dt=e=>c("fract",e),rn=e=>c("fwidth",e),on=e=>c("inverseSqrt",e),nn=e=>c("log",e),sn=e=>c("log2",e),an=e=>c("negate",e),un=e=>c("normalize",e),pn=e=>c("oneMinus",e),fn=e=>c("radians",e),cn=e=>c("reciprocal",e),ln=e=>c("round",e),dn=e=>c("saturate",e),xn=e=>c("sign",e),mn=e=>c("sin",e),Tn=e=>c("sinh",e),gn=e=>c("sqrt",e),bn=e=>c("tan",e),vn=e=>c("tanh",e),hn=e=>c("trunc",e),yn=(e,t)=>c("atan2",e,t),En=(e,t,r)=>c("clamp",e,t,r),Xn=(e,t)=>c("max",e,t),Cn=(e,t)=>c("min",e,t),_n=(e,t,r)=>c("mix",e,t,r),Pn=(e,t)=>c("pow",e,t),$n=(e,t)=>c("reflect",e,t),Gn=(e,t,r)=>c("refract",e,t,r),Ln=(e,t,r)=>c("smoothstep",e,t,r),Nn=(e,t)=>c("step",e,t),Rn=(e,t)=>e.sub(e.div(t).toFloat().floor().mul(t));var kt=`
|
|
8
|
+
uniform vec2 iResolution;
|
|
9
|
+
void main() {
|
|
10
|
+
fragColor = vec4(fract((gl_FragCoord.xy / iResolution)), 0.0, 1.0);
|
|
11
|
+
}
|
|
12
|
+
`,K=`
|
|
40
13
|
#version 300 es
|
|
41
14
|
void main() {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}`.trim(),Vt=(e,t)=>{if(!e.cs)return null;t.getExtension("EXT_color_buffer_float");let r=0,n=0,o=D(()=>r++),s={isWebGL:!0,gl:e,units:o},i=ne(t,Z(e.cs,s),kt,e),p=Math.ceil(Math.sqrt(e.particles)),a=D(g=>t.getUniformLocation(i,g)),u=D(g=>{let v=new Float32Array(p*p*4),P={texture:t.createTexture(),buffer:t.createFramebuffer()},R={texture:t.createTexture(),buffer:t.createFramebuffer()};return{ping:P,pong:R,array:v,loc:a(g),unit:o(g)}});return{render:()=>{t.useProgram(i);let g=u.map.values().map(({ping:v,pong:P,loc:R,unit:U},M)=>{let[V,re]=n%2?[v,P]:[P,v];return Le(t,V,re,R,U,M)});t.drawBuffers(g),t.drawArrays(t.TRIANGLES,0,3),t.bindFramebuffer(t.FRAMEBUFFER,null),n++},clean:()=>{t.deleteProgram(i),Ge(t,u.map.values())},_uniform:(g,v)=>{t.useProgram(i),se(t,a(g),v)},_storage:(g,v)=>{let{ping:P,pong:R,unit:U,array:M}=u(g);$e(t,v,p,P,R,U,M)},storages:u}},xt=async e=>{let t={isWebGL:!0,gl:e},r=e.el.getContext("webgl2"),n=Vt(e,r),o=ne(r,j(e.fs,t),K(e.vs,t),e);r.useProgram(o);let s=0,i=D(()=>s++),p=D(v=>r.getAttribLocation(o,v)),a=D(v=>r.getUniformLocation(o,v)),u=(v="",P,R)=>{let U=p(v,!0);_e(r,U,e.count,P,R)},l=(v,P)=>{r.useProgram(o),se(r,a(v),P),n?._uniform(v,P)},y=(v,P)=>{r.useProgram(o),z(e,P,R=>{Pe(r,R,a(v),i(v))})},T=()=>{n?.clean(),r.deleteProgram(o),r.getExtension("WEBGL_lose_context")?.loseContext()},d=()=>{n?.render(),r.useProgram(o),r.viewport(0,0,...e.size),r.drawArrays(r.TRIANGLES,0,e.count),r.bindFramebuffer(r.FRAMEBUFFER,null)};return{webgl:{context:r,program:o,storages:n?.storages},render:d,clean:T,_attribute:u,_uniform:l,_texture:y,_storage:n?._storage}};import{nested as ee}from"reev";var mt=async(e,t=console.log)=>{let r=navigator.gpu,n=r.getPreferredCanvasFormat(),s=await(await r.requestAdapter()).requestDevice();return s.onuncapturederror=i=>t(i.error.message),e.configure({device:s,format:n,alphaMode:"opaque"}),{device:s,format:n}},Tt=()=>{let e=0,t=0,r=0,n=0;return{uniform:()=>{let o=Math.floor(e/12),s=e%12;return e++,{group:o,binding:s}},texture:()=>{let s=Math.floor(e/12)+1+Math.floor(t/6),i=t%6*2;return t++,{group:s,binding:i}},storage:()=>{let s=Math.floor(e/12)+Math.floor(t/6)+2+Math.floor(r/12),i=r%12;return r++,{group:s,binding:i}},attrib:()=>{let o=n;return n++,{location:o}}}},zt=e=>e===2?"float32x2":e===3?"float32x3":e===4?"float32x4":"float32",gt=e=>{let t=[],r=[];for(let{buffer:n,location:o,stride:s}of e)t[o]=n,r[o]={arrayStride:s*4,attributes:[{shaderLocation:o,offset:0,format:zt(s)}]};return{vertexBuffers:t,bufferLayouts:r}},bt=(e,t,r,n=[])=>{let o=new Map,s={bindGroups:[],bindGroupLayouts:[]},i=(p,a,u)=>{o.has(p)||o.set(p,{layouts:[],bindings:[]});let{layouts:l,bindings:y}=o.get(p);l.push(a),y.push(u)};for(let{binding:p,buffer:a,group:u}of t)i(u,{binding:p,visibility:7,buffer:{type:"uniform"}},{binding:p,resource:{buffer:a}});for(let{binding:p,buffer:a,group:u}of n)i(u,{binding:p,visibility:6,buffer:{type:"storage"}},{binding:p,resource:{buffer:a}});for(let{binding:p,group:a,sampler:u,view:l}of r)i(a,{binding:p,visibility:2,sampler:{}},{binding:p,resource:u}),i(a,{binding:p+1,visibility:2,texture:{}},{binding:p+1,resource:l});for(let[p,{layouts:a,bindings:u}]of o)s.bindGroupLayouts[p]=e.createBindGroupLayout({entries:a}),s.bindGroups[p]=e.createBindGroup({layout:s.bindGroupLayouts[p],entries:u});return s},vt=(e,t,r,n,o,s)=>e.createRenderPipeline({vertex:{module:e.createShaderModule({label:"vert",code:o}),entryPoint:"main",buffers:r},fragment:{module:e.createShaderModule({label:"frag",code:s}),entryPoint:"main",targets:[{format:t}]},layout:e.createPipelineLayout({bindGroupLayouts:n}),primitive:{topology:"triangle-list"},depthStencil:{depthWriteEnabled:!0,depthCompare:"less",format:"depth24plus"}}),ht=(e,t,r)=>e.createComputePipeline({compute:{module:e.createShaderModule({label:"compute",code:r}),entryPoint:"main"},layout:e.createPipelineLayout({bindGroupLayouts:t})}),qt=e=>e==="uniform"?72:e==="attrib"?40:140,J=(e,t,r)=>{Xe(t)||(t=new Float32Array(t));let n=qt(r),o=r==="uniform"?Math.ceil(t.byteLength/256)*256:t.byteLength,s=e.createBuffer({size:o,usage:n});return{array:t,buffer:s}},yt=(e,t)=>({colorAttachments:[{view:e.getCurrentTexture().createView(),clearValue:{r:0,g:0,b:0,a:1},loadOp:"clear",storeOp:"store"}],depthStencilAttachment:{view:t.createView(),depthClearValue:1,depthLoadOp:"clear",depthStoreOp:"store"}}),Et=(e,t=1280,r=800)=>{let n=e.createTexture({size:[t,r],format:"rgba8unorm",usage:22}),o=e.createSampler({magFilter:"linear",minFilter:"linear"});return{texture:n,sampler:o}},Xt=(e,t,r)=>e.createTexture({size:[t,r],format:"depth24plus",usage:GPUTextureUsage.RENDER_ATTACHMENT});var Yt=32,Ht=(e,t,r)=>{let n=u=>{},o=ee((u,l)=>{let{array:y,buffer:T}=J(t,l,"storage"),{binding:d,group:g}=r.storage();return{array:y,buffer:T,binding:d,group:g}});return{storages:o,_storage:(u,l)=>{let{array:y,buffer:T}=o(u,l);t.queue.writeBuffer(T,0,y)},update:(u,l,y)=>{let T=ht(t,l,y);n=d=>{d.setPipeline(T),u.forEach((v,P)=>d.setBindGroup(P,v));let g=Math.ceil(e.particles/Yt);d.dispatchWorkgroups(g,1,1),d.end()}},render:u=>{n(u)},clean:()=>{for(let{buffer:u}of o.map.values())u.destroy()}}},Ct=async e=>{let t=e.el.getContext("webgpu"),{device:r,format:n}=await mt(t,e.error),o=Tt(),s=Ht(e,r,o),i,p,a,u=h=>{},l=!0,y,T=ee((h,X)=>{l=!0;let $=X.length/e.count,{location:G}=o.attrib(),{array:w,buffer:L}=J(r,X,"attrib");return{array:w,buffer:L,location:G,stride:$}}),d=ee((h,X)=>{l=!0;let{binding:$,group:G}=o.uniform(),{array:w,buffer:L}=J(r,X,"uniform");return{array:w,buffer:L,binding:$,group:G}}),g=ee((h,X=0,$=0)=>{l=!0;let{binding:G,group:w}=o.texture(),{texture:L,sampler:B}=Et(r,X,$);return{texture:L,sampler:B,binding:G,group:w,view:L.createView()}}),v=(h="",X)=>{let{array:$,buffer:G}=T(h,X);r.queue.writeBuffer(G,0,$)},P=(h,X)=>{x.num(X)&&(X=[X]);let{array:$,buffer:G}=d(h,X);$.set(X),r.queue.writeBuffer(G,0,$)},R=(h,X)=>{z(e,X,$=>{let{width:G,height:w}=$,{texture:L}=g(h,G,w);r.queue.copyExternalImageToTexture({source:$},{texture:L},{width:G,height:w})})},U=()=>{let{vertexBuffers:h,bufferLayouts:X}=gt(T.map.values()),{bindGroups:$,bindGroupLayouts:G}=bt(r,d.map.values(),g.map.values(),s.storages.map.values()),w=vt(r,n,X,G,a,i);u=L=>{L.setPipeline(w),$.forEach((B,oe)=>L.setBindGroup(oe,B)),h.forEach((B,oe)=>L.setVertexBuffer(oe,B)),L.draw(e.count,1,0,0),L.end()},e.cs&&s.update($,G,p)},M=()=>{if(!i||!a){let X={isWebGL:!1,gl:e};i=j(e.fs,X),a=K(e.vs,X),p=Z(e.cs,X)}if(e.loading)return;l&&U(),l=!1;let h=r.createCommandEncoder();e.cs&&s.render(h.beginComputePass()),u(h.beginRenderPass(yt(t,y))),r.queue.submit([h.finish()])},V=()=>{let h=e.el;y?.destroy(),y=Xt(r,h.width,h.height)},re=()=>{r.destroy(),y?.destroy();for(let{texture:h}of g.map.values())h.destroy();for(let{buffer:h}of d.map.values())h.destroy();for(let{buffer:h}of T.map.values())h.destroy();s.clean()};return V(),{webgpu:{device:r,uniforms:d,textures:g,attribs:T,storages:s.storages},render:M,resize:V,clean:re,_attribute:v,_uniform:P,_texture:R,_storage:s._storage}};var ts=e=>x.obj(e)?"isGL"in e:!1,Qt=()=>typeof window>"u",Jt=()=>Qt()?!1:"gpu"in navigator,Ee=performance.now(),er=()=>he(dt(be.xy.div(ye)),0,1),tr=()=>he(Q(k(ve).mod(k(2))).mul(4).sub(1),Q(k(ve).div(k(2))).mul(4).sub(1),0,1),rr=e=>{let t=jt({isNative:!1,isWebGL:!0,isError:!1,isLoop:!0,isGL:!0,size:[0,0],mouse:[0,0],count:6,particles:1024,webgl:{},webgpu:{},loading:0,error(){t.isError=!0,t.isLoop=!1,t.clean(),console.warn("GLRE Error:",...arguments)}});return t.queue=Zt(),t.frame=Kt(),t.attribute=te((r,n,o)=>t.queue(()=>t._attribute?.(r,n,o)),t),t.storage=te((r,n)=>t.queue(()=>t._storage?.(r,n)),t),t.uniform=te((r,n)=>t.queue(()=>t._uniform?.(r,n)),t),t.texture=te((r,n)=>t.queue(()=>t._texture?.(r,n)),t),t.uniform({iResolution:t.size,iMouse:[0,0],iTime:Ee}),t("mount",async()=>{t.vs=t.vs||t.vert||t.vertex||tr(),t.fs=t.fs||t.frag||t.fragment||er(),t.cs=t.cs||t.comp||t.compute,Jt()||(t.isWebGL=!0),t.isWebGL?t(await xt(t)):t(await Ct(t)),!t.isError&&(t.resize(),t.frame(()=>(t.loop(),t.queue.flush(),t.loading?!0:(t.render(),t.isLoop))),!t.isNative&&(window.addEventListener("resize",t.resize),t.el.addEventListener("mousemove",t.mousemove)))}),t("clean",()=>{t.frame.stop(),!t.isNative&&(window.removeEventListener("resize",t.resize),t.el.removeEventListener("mousemove",t.mousemove))}),t("resize",()=>{let r=t.width||window.innerWidth,n=t.height||window.innerHeight;t.size[0]=t.el.width=r,t.size[1]=t.el.height=n,t.uniform("iResolution",t.size)}),t("mousemove",(r,n=r.clientX,o=r.clientY)=>{let[s,i]=t.size,{top:p,left:a}=t.el.getBoundingClientRect();t.mouse[0]=(n-p-s/2)/(s/2),t.mouse[1]=-(o-a-i/2)/(i/2),t.uniform("iMouse",t.mouse)}),t("loop",()=>{Ee=performance.now()/1e3,t.uniform("iTime",Ee)}),t(e)},rs=rr;export{wr as Fn,Lr as If,Nr as Loop,St as Return,Rr as Switch,Mo as abs,Bo as acos,Wo as acosh,Lo as all,No as any,ko as asin,Vo as asinh,pt as assign,zo as atan,yn as atan2,qo as atanh,Ft as attribute,po as bool,_ as builtin,ho as bvec2,yo as bvec3,Eo as bvec4,Yo as ceil,En as clamp,_o as color,Z as compute,At as constant,b as conversion,Ho as cos,jo as cosh,rr as createGL,Fo as cross,Ao as cubeTexture,Ko as dFdx,Zo as dFdy,rs as default,Qo as degrees,So as distance,Uo as dot,ft as element,Jo as exp,en as exp2,Q as float,tn as floor,dt as fract,jr as fragDepth,j as fragment,Hr as frontFacing,c as function_,rn as fwidth,Dt as gather,Po as iMouse,ye as iResolution,$o as iTime,Jr as id,Yr as instanceIndex,k as int,on as inverseSqrt,ts as isGL,Qt as isServer,Jt as isWebGPUSupported,xo as ivec2,mo as ivec3,To as ivec4,Ro as length,wo as lengthSq,nn as log,sn as log2,fo as mat2,co as mat3,lo as mat4,Xn as max,Te as member,Cn as min,_n as mix,Rn as mod,an as negate,m as node,oo as normalLocal,so as normalView,no as normalWorld,un as normalize,pn as oneMinus,Mt as operator,Qr as pointCoord,be as position,eo as positionLocal,ro as positionView,to as positionWorld,Pn as pow,fn as radians,cn as reciprocal,$n as reflect,Gn as refract,ln as round,Kr as sampleIndex,Zr as sampleMask,Co as sampler2D,dn as saturate,Or as scatter,io as screenCoordinate,ao as screenUV,Dr as select,xn as sign,mn as sin,Tn as sinh,Ln as smoothstep,gn as sqrt,Nn as step,Ir as storage,Gr as struct,bn as tan,vn as tanh,Oo as texelFetch,Io as texture,Xo as texture2D,Do as textureLod,ut as toVar,hn as trunc,uo as uint,W as uniform,Go as uv,go as uvec2,bo as uvec3,vo as uvec4,It as variable,lt as vec2,ct as vec3,he as vec4,K as vertex,ve as vertexIndex,Ot as vertexStage,xt as webgl,Ct as webgpu};
|
|
15
|
+
float x = float(gl_VertexID % 2) * 4.0 - 1.0;
|
|
16
|
+
float y = float(gl_VertexID / 2) * 4.0 - 1.0;
|
|
17
|
+
gl_Position = vec4(x, y, 0.0, 1.0);
|
|
18
|
+
}`,ue=(e,t)=>{if(!e.cs)return null;t.getExtension("EXT_color_buffer_float");let r=0,n=0,o=A(()=>r++),a=_.str(e.cs)?e.cs:e.cs.compute({isWebGL:!0,gl:e,units:o}),s=C(t,a,K,e),i=Math.ceil(Math.sqrt(e.particles)),f=A(b=>t.getUniformLocation(s,b)),u=A(b=>{let x=new Float32Array(i*i*4),L={texture:t.createTexture(),buffer:t.createFramebuffer()},l={texture:t.createTexture(),buffer:t.createFramebuffer()};return{ping:L,pong:l,array:x,loc:f(b),unit:o(b)}});return{render:()=>{t.useProgram(s);let b=u.map.values().map(({ping:x,pong:L,loc:l,unit:y},U)=>{let[P,S]=n%2?[x,L]:[L,x];return z(t,P,S,l,y,U)});t.drawBuffers(b),t.drawArrays(t.TRIANGLES,0,3),t.bindFramebuffer(t.FRAMEBUFFER,null),n++},clean:()=>{t.deleteProgram(s),q(t,u.map.values())},_uniform:(b,x)=>{t.useProgram(s),I(t,f(b),x)},_storage:(b,x)=>{let{ping:L,pong:l,unit:y,array:U}=u(b);k(t,x,i,L,l,y,U)},storages:u}},Y=async e=>{let t={isWebGL:!0,gl:e},r=e.el.getContext("webgl2"),n=ue(e,r),o=e.fs?_.str(e.fs)?e.fs:e.fs.fragment(t):ie,a=e.vs?_.str(e.vs)?e.vs:e.vs.vertex(t):K,s=C(r,o,a,e);r.useProgram(s);let i=0,f=A(()=>i++),u=A(l=>r.getAttribLocation(s,l)),m=A(l=>r.getUniformLocation(s,l)),E=(l="",y,U)=>{let P=u(l,!0);O(r,P,e.count,y,U)},R=(l,y)=>{r.useProgram(s),I(r,m(l),y),n?._uniform(l,y)},T=(l,y)=>{r.useProgram(s),F(e,y,U=>{V(r,U,m(l),f(l))})},b=()=>{n?.clean(),r.deleteProgram(s),r.getExtension("WEBGL_lose_context")?.loseContext()},x=()=>{n?.render(),r.useProgram(s),r.viewport(0,0,...e.size),r.drawArrays(r.TRIANGLES,0,e.count),r.bindFramebuffer(r.FRAMEBUFFER,null)};return{webgl:{context:r,program:s,storages:n?.storages},render:x,clean:b,_attribute:E,_uniform:R,_texture:T,_storage:n?._storage}};import{nested as D}from"reev";var H=async(e,t=console.log)=>{let r=navigator.gpu,n=r.getPreferredCanvasFormat(),a=await(await r.requestAdapter()).requestDevice();return a.onuncapturederror=s=>t(s.error.message),e.configure({device:a,format:n,alphaMode:"opaque"}),{device:a,format:n}},j=()=>{let e=0,t=0,r=0,n=0;return{uniform:()=>{let o=Math.floor(e/12),a=e%12;return e++,{group:o,binding:a}},texture:()=>{let a=Math.floor(e/12)+1+Math.floor(t/6),s=t%6*2;return t++,{group:a,binding:s}},storage:()=>{let a=Math.floor(e/12)+Math.floor(t/6)+2+Math.floor(r/12),s=r%12;return r++,{group:a,binding:s}},attrib:()=>{let o=n;return n++,{location:o}}}},fe=e=>e===2?"float32x2":e===3?"float32x3":e===4?"float32x4":"float32",$=e=>{let t=[],r=[];for(let{buffer:n,location:o,stride:a}of e)t[o]=n,r[o]={arrayStride:a*4,attributes:[{shaderLocation:o,offset:0,format:fe(a)}]};return{vertexBuffers:t,bufferLayouts:r}},Q=(e,t,r,n=[])=>{let o=new Map,a={bindGroups:[],bindGroupLayouts:[]},s=(i,f,u)=>{o.has(i)||o.set(i,{layouts:[],bindings:[]});let{layouts:m,bindings:E}=o.get(i);m.push(f),E.push(u)};for(let{binding:i,buffer:f,group:u}of t)s(u,{binding:i,visibility:7,buffer:{type:"uniform"}},{binding:i,resource:{buffer:f}});for(let{binding:i,buffer:f,group:u}of n)s(u,{binding:i,visibility:6,buffer:{type:"storage"}},{binding:i,resource:{buffer:f}});for(let{binding:i,group:f,sampler:u,view:m}of r)s(f,{binding:i,visibility:2,sampler:{}},{binding:i,resource:u}),s(f,{binding:i+1,visibility:2,texture:{}},{binding:i+1,resource:m});for(let[i,{layouts:f,bindings:u}]of o)a.bindGroupLayouts[i]=e.createBindGroupLayout({entries:f}),a.bindGroups[i]=e.createBindGroup({layout:a.bindGroupLayouts[i],entries:u});return a},Z=(e,t,r,n,o,a)=>e.createRenderPipeline({vertex:{module:e.createShaderModule({label:"vert",code:o.trim()}),entryPoint:"main",buffers:r},fragment:{module:e.createShaderModule({label:"frag",code:a.trim()}),entryPoint:"main",targets:[{format:t}]},layout:e.createPipelineLayout({bindGroupLayouts:n}),primitive:{topology:"triangle-list"},depthStencil:{depthWriteEnabled:!0,depthCompare:"less",format:"depth24plus"}}),J=(e,t,r)=>e.createComputePipeline({compute:{module:e.createShaderModule({label:"compute",code:r.trim()}),entryPoint:"main"},layout:e.createPipelineLayout({bindGroupLayouts:t})}),me=e=>e==="uniform"?72:e==="attrib"?40:140,w=(e,t,r)=>{W(t)||(t=new Float32Array(t));let n=me(r),o=r==="uniform"?Math.ceil(t.byteLength/256)*256:t.byteLength,a=e.createBuffer({size:o,usage:n});return{array:t,buffer:a}},ee=(e,t)=>({colorAttachments:[{view:e.getCurrentTexture().createView(),clearValue:{r:0,g:0,b:0,a:1},loadOp:"clear",storeOp:"store"}],depthStencilAttachment:{view:t.createView(),depthClearValue:1,depthLoadOp:"clear",depthStoreOp:"store"}}),te=(e,t=1280,r=800)=>{let n=e.createTexture({size:[t,r],format:"rgba8unorm",usage:22}),o=e.createSampler({magFilter:"linear",minFilter:"linear"});return{texture:n,sampler:o}},re=(e,t,r)=>e.createTexture({size:[t,r],format:"depth24plus",usage:GPUTextureUsage.RENDER_ATTACHMENT});var pe=32,ce=`
|
|
19
|
+
struct In { @builtin(vertex_index) vertex_index: u32 }
|
|
20
|
+
struct Out { @builtin(position) position: vec4f }
|
|
21
|
+
@vertex
|
|
22
|
+
fn main(in: In) -> Out {
|
|
23
|
+
var out: Out;
|
|
24
|
+
var x = f32(in.vertex_index % 2) * 4.0 - 1.0;
|
|
25
|
+
var y = f32(in.vertex_index / 2) * 4.0 - 1.0;
|
|
26
|
+
out.position = vec4f(x, y, 0.0, 1.0);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
`.trim(),be=`
|
|
30
|
+
struct Out { @builtin(position) position: vec4f }
|
|
31
|
+
@group(0) @binding(0) var<uniform> iResolution: vec2f;
|
|
32
|
+
@fragment
|
|
33
|
+
fn main(out: Out) -> @location(0) vec4f {
|
|
34
|
+
return vec4f(fract((out.position.xy / iResolution)), 0.0, 1.0);
|
|
35
|
+
}
|
|
36
|
+
`,le=(e,t,r)=>{let n=u=>{},o=D((u,m)=>{let{array:E,buffer:R}=w(t,m,"storage"),{binding:T,group:b}=r.storage();return{array:E,buffer:R,binding:T,group:b}});return{storages:o,_storage:(u,m)=>{let{array:E,buffer:R}=o(u,m);t.queue.writeBuffer(R,0,E)},update:(u,m,E)=>{let R=J(t,m,E);n=T=>{T.setPipeline(R),u.forEach((x,L)=>T.setBindGroup(L,x));let b=Math.ceil(e.particles/pe);T.dispatchWorkgroups(b,1,1),T.end()}},render:u=>{n(u)},clean:()=>{for(let{buffer:u}of o.map.values())u.destroy()}}},ne=async e=>{let t=e.el.getContext("webgpu"),{device:r,format:n}=await H(t,e.error),o=j(),a=le(e,r,o),s,i,f,u=p=>{},m=!0,E,R=D((p,c)=>{m=!0;let d=c.length/e.count,{location:g}=o.attrib(),{array:h,buffer:G}=w(r,c,"attrib");return{array:h,buffer:G,location:g,stride:d}}),T=D((p,c)=>{m=!0;let{binding:d,group:g}=o.uniform(),{array:h,buffer:G}=w(r,c,"uniform");return{array:h,buffer:G,binding:d,group:g}}),b=D((p,c=0,d=0)=>{m=!0;let{binding:g,group:h}=o.texture(),{texture:G,sampler:v}=te(r,c,d);return{texture:G,sampler:v,binding:g,group:h,view:G.createView()}}),x=(p="",c)=>{let{array:d,buffer:g}=R(p,c);r.queue.writeBuffer(g,0,d)},L=(p,c)=>{_.num(c)&&(c=[c]);let{array:d,buffer:g}=T(p,c);d.set(c),r.queue.writeBuffer(g,0,d)},l=(p,c)=>{F(e,c,d=>{let{width:g,height:h}=d,{texture:G}=b(p,g,h);r.queue.copyExternalImageToTexture({source:d},{texture:G},{width:g,height:h})})},y=()=>{let{vertexBuffers:p,bufferLayouts:c}=$(R.map.values()),{bindGroups:d,bindGroupLayouts:g}=Q(r,T.map.values(),b.map.values(),a.storages.map.values()),h=Z(r,n,c,g,f,s);u=G=>{G.setPipeline(h),d.forEach((v,M)=>G.setBindGroup(M,v)),p.forEach((v,M)=>G.setVertexBuffer(M,v)),G.draw(e.count,1,0,0),G.end()},e.cs&&a.update(d,g,i)},U=()=>{if(!s||!f){let c={isWebGL:!1,gl:e};s=e.fs?_.str(e.fs)?e.fs:e.fs.fragment(c):be,f=e.vs?_.str(e.vs)?e.vs:e.vs.vertex(c):ce,i=e.cs?_.str(e.cs)?e.cs:e.cs.compute(c):""}if(e.loading)return;m&&y(),m=!1;let p=r.createCommandEncoder();e.cs&&a.render(p.beginComputePass()),u(p.beginRenderPass(ee(t,E))),r.queue.submit([p.finish()])},P=()=>{let p=e.el;E?.destroy(),E=re(r,p.width,p.height)},S=()=>{r.destroy(),E?.destroy();for(let{texture:p}of b.map.values())p.destroy();for(let{buffer:p}of T.map.values())p.destroy();for(let{buffer:p}of R.map.values())p.destroy();a.clean()};return P(),{webgpu:{device:r,uniforms:T,textures:b,attribs:R,storages:a.storages},render:U,resize:P,clean:S,_attribute:x,_uniform:L,_texture:l,_storage:a._storage}};var Ne=e=>_.obj(e)?"isGL"in e:!1,Te=()=>typeof window>"u",xe=()=>Te()?!1:"gpu"in navigator,X=performance.now(),Ge=e=>{let t=de({isNative:!1,isWebGL:!0,isError:!1,isLoop:!0,isGL:!0,size:[0,0],mouse:[0,0],count:6,particles:1024,webgl:{},webgpu:{},loading:0,error(){t.isError=!0,t.isLoop=!1,t.clean(),console.warn("GLRE Error:",...arguments)}});return t.queue=ge(),t.frame=Ee(),t.attribute=B((r,n,o)=>t.queue(()=>t._attribute?.(r,n,o)),t),t.storage=B((r,n)=>t.queue(()=>t._storage?.(r,n)),t),t.uniform=B((r,n)=>t.queue(()=>t._uniform?.(r,n)),t),t.texture=B((r,n)=>t.queue(()=>t._texture?.(r,n)),t),t.uniform({iResolution:t.size,iMouse:[0,0],iTime:X}),t("mount",async()=>{xe()||(t.isWebGL=!0),t.vs=t.vs||t.vert||t.vertex,t.fs=t.fs||t.frag||t.fragment,t.cs=t.cs||t.comp||t.compute,t.isWebGL?t(await Y(t)):t(await ne(t)),!t.isError&&(t.resize(),t.frame(()=>(t.loop(),t.queue.flush(),t.loading?!0:(t.render(),t.isLoop))),!t.isNative&&(window.addEventListener("resize",t.resize),t.el.addEventListener("mousemove",t.mousemove)))}),t("clean",()=>{t.frame.stop(),!t.isNative&&(window.removeEventListener("resize",t.resize),t.el.removeEventListener("mousemove",t.mousemove))}),t("resize",()=>{let r=t.width||window.innerWidth,n=t.height||window.innerHeight;t.size[0]=t.el.width=r,t.size[1]=t.el.height=n,t.uniform("iResolution",t.size)}),t("mousemove",(r,n=r.clientX,o=r.clientY)=>{let[a,s]=t.size,{top:i,left:f}=t.el.getBoundingClientRect();t.mouse[0]=(n-i-a/2)/(a/2),t.mouse[1]=-(o-f-s/2)/(s/2),t.uniform("iMouse",t.mouse)}),t("loop",()=>{X=performance.now()/1e3,t.uniform("iTime",X)}),t(e)},Oe=Ge;export{Ge as createGL,Oe as default,Ne as isGL,Te as isServer,xe as isWebGPUSupported};
|
|
46
37
|
//# sourceMappingURL=index.js.map
|