glre 0.30.0 → 0.32.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/index.js CHANGED
@@ -1,70 +1,36 @@
1
- import{durable as H,event as at}from"reev";import{createFrame as ut,createQueue as ct}from"refr";import{nested as Y}from"reev";var g={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)},$e=(e,t)=>e.forEach(t),mt=(e,...t)=>{$e(e,r=>r(...t))},dt=(e="",t="_",r="/")=>e.split(t).join(r),xt=(e=".pdf")=>e.split(".").pop()?.toLowerCase()??"",gt=(e=0)=>`${e}`.split(".")[1]?.length??0,bt=(e=0)=>`${e}`.split(".")[0]?.length-(e<0?1:0),vt=(e=0,t=-2)=>(t*=-1,t=Math.pow(10,t),e*=t,e=Math.round(e),e/=t,e);var S=["bool","uint","int","float","bvec2","ivec2","uvec2","vec2","bvec3","ivec3","uvec3","vec3","bvec4","ivec4","uvec4","vec4","color","mat2","mat3","mat4"],U=["toBool","toUint","toInt","toFloat","toBvec2","toIvec2","toUvec2","toVec2","toBvec3","toIvec3","toUvec3","toVec3","toBvec4","toIvec4","toUvec4","toVec4","toColor","toMat2","toMat3","toMat4"],A={add:"+",sub:"-",mul:"*",div:"/",mod:"%",equal:"==",notEqual:"!=",lessThan:"<",lessThanEqual:"<=",greaterThan:">",greaterThanEqual:">=",and:"&&",or:"||",bitAnd:"&",bitOr:"|",bitXor:"^",shiftLeft:"<<",shiftRight:">>"},Z=Object.keys(A),$=["dot","distance","length","lengthSq","determinant","luminance"],B=["all","any"],D=["abs","sign","floor","ceil","round","fract","trunc","sin","cos","tan","asin","acos","atan","exp","exp2","log","log2","sqrt","inverseSqrt","normalize","oneMinus","saturate","negate","reciprocal","dFdx","dFdy","fwidth"],M=["cross"],V=["reflect","refract"],z=["min","max","mix","clamp","step","smoothstep"],W=["texture","textureLod","textureSize","cubeTexture"],Be=["atan2","degrees","faceforward","bitcast","cbrt","difference","equals","pow","pow2","pow3","pow4","radians","transformDirection"],Q=[...$,...B,...D,...M,...V,...z,...W,...Be],J={float:"f32",int:"i32",uint:"u32",bool:"bool",vec2:"vec2f",vec3:"vec3f",vec4:"vec4f",mat2:"mat2x2f",mat3:"mat3x3f",mat4:"mat4x4f",ivec2:"vec2i",ivec3:"vec3i",ivec4:"vec4i",uvec2:"vec2u",uvec3:"vec3u",uvec4:"vec4u",bvec2:"vec2<bool>",bvec3:"vec3<bool>",bvec4:"vec4<bool>"},q={1:"float",2:"vec2",3:"vec3",4:"vec4",9:"mat3",16:"mat4"},ee={position:"vec4",vertex_index:"uint",instance_index:"uint",front_facing:"bool",frag_depth:"float",sample_index:"uint",sample_mask:"uint",point_coord:"vec2",positionLocal:"vec3",positionWorld:"vec3",positionView:"vec3",normalLocal:"vec3",normalWorld:"vec3",normalView:"vec3",screenCoordinate:"vec2",screenUV:"vec2",gl_FragCoord:"vec4",gl_VertexID:"uint",gl_InstanceID:"uint",gl_FrontFacing:"bool",gl_FragDepth:"float",gl_SampleID:"uint",gl_SampleMask:"uint",gl_PointCoord:"vec2",normal:"vec3",uv:"vec2",color:"vec4"},te=["equal","notEqual","lessThan","lessThanEqual","greaterThan","greaterThanEqual"],re=["and","or"],oe={position:"gl_FragCoord",vertex_index:"gl_VertexID",instance_index:"gl_InstanceID",front_facing:"gl_FrontFacing",frag_depth:"gl_FragDepth",sample_index:"gl_SampleID",sample_mask:"gl_SampleMask",point_coord:"gl_PointCoord",uv:"gl_FragCoord.xy"};var k=e=>g.str(e)&&/^[xyzwrgbastpq]{1,4}$/.test(e),ne=e=>Z.includes(e),se=e=>Q.includes(e),ie=e=>U.includes(e),P=e=>!e||typeof e!="object"?!1:e.isProxy;var De=0,ae=()=>`i${De++}`,L=(e,t)=>e.filter(r=>!g.und(r)&&!g.nul(r)).map(r=>u(r,t)).join(", "),R=(e,t)=>g.str(e)?t?.isWebGL?e:J[e]:"",ue=e=>A[e]||e,ce=e=>oe[e],pe=e=>{let t=U.indexOf(e);return t!==-1?S[t]:"float"},fe=e=>Array.from(e.headers).map(([,t])=>t).join(`
2
- `),le=(e,t)=>{let{id:r,children:n=[],layout:o}=e,[s,a,...i]=n,m=o?.type&&o?.type!=="auto"?o?.type:a?y(a,t):"void",b=[],c=[];if(o?.inputs)for(let d of o.inputs)b.push([d.name,d.type]);else for(let d=0;d<i.length;d++)b.push([`p${d}`,y(i[d],t)]);let p="";if(t?.isWebGL){for(let[d,_]of b)c.push(`${_} ${d}`);p+=`${m} ${r}(${c}) {
3
- `}else{for(let[d,_]of b)c.push(`${d}: ${R(_,t)}`);p+=`fn ${r}(${c}) -> ${R(m,t)} {
4
- `}let T=u(s,t);return T&&(p+=T+`
5
- `),a&&(p+=`return ${u(a,t)};`),p+=`
6
- }`,p},Me=`
7
- #version 300 es
8
- precision mediump float;
9
- out vec4 fragColor;
10
- `.trim(),Ve=`
11
- @fragment
12
- fn main(@builtin(position) position: vec4f) -> @location(0) vec4f {
13
- `.trim(),ze=(e,t,r=!0)=>{let n="";return r&&(n+=Me),t&&(n+=`
14
- `+t+`
15
- `),r?n+=`void main() {
16
- fragColor = ${e};`:(n+=Ve+`
17
- `,n+=` return ${e};`),n+=`
18
- }`,n},We=(e,t,r=!0)=>"",w=(e,t={})=>{let r=u(e,t),n=fe(t),o=ze(r,n,t.isWebGL);return console.log(`// \u2193\u2193\u2193 generated \u2193\u2193\u2193
19
-
20
- ${o}
21
-
22
- `),o},O=(e,t)=>{let r=u(e,t),n=fe(t);return We(r,n,t.isWebGL)};var qe=(e,t)=>e.reduce((r,n)=>{let o=y(n,t),s=S.indexOf(r);return S.indexOf(o)>s?o:r},"float"),ke=e=>ee[e],Ye=(e,t,r)=>{let n=t.length>0?y(t[0],r):"float";return V.includes(e)?n:$.includes(e)?"float":B.includes(e)?"bool":D.includes(e)?n:M.includes(e)?"vec3":W.includes(e)?"vec4":z.includes(e)?qe(t,r):n},me=(e,t,r)=>{if(te.includes(r)||re.includes(r))return"bool";if(e===t||e.includes("vec")&&!t.includes("vec"))return e;if(t.includes("vec")&&!e.includes("vec"))return t;let n=S.indexOf(e),o=S.indexOf(t);return n>=o?e:t},je=e=>g.bol(e)?"bool":g.num(e)?"float":g.arr(e)&&q[e.length]||"float",He=e=>q[e],Ke=(e,t)=>{let{type:r,props:n}=e,{id:o,children:s=[],inferFrom:a}=n,[i,m,b]=s;return a?y(a,t):r==="conversion"?i:r==="operator"?me(y(m,t),y(b,t),i):r==="function"?Ye(i,s.slice(1),t):r==="swizzle"?He(i.length):r==="ternary"?me(y(m,t),y(b,t),"add"):r==="builtin"?ke(o):r==="define"?n.layout?.type&&n.layout?.type!=="auto"?n.layout.type:m?y(m,t):"void":y(i,t)},y=(e,t)=>{if(t||(t={}),!P(e))return je(e);if(t.infers||(t.infers=new WeakMap),t.infers.has(e))return t.infers.get(e);let r=Ke(e,t);return t.infers.set(e,r),r};var u=(e,t)=>{if(t||(t={}),t.headers||(t.headers=new Map),g.str(e))return e;if(g.num(e)){let c=`${e}`;return c.includes(".")?c:c+".0"}if(g.bol(e))return e?"true":"false";if(!e)return"";let{type:r,props:n}=e,{id:o="",children:s=[]}=n,[a,i,m]=s,b="";if(r==="attribute"){if(t.headers.has(o))return o;b=`${y(e,t)} ${o}`}if(r==="uniform"){if(t.headers.has(o))return o;t.binding||(t.binding=0);let c=y(e,t);b=t.isWebGL?`uniform ${c} ${o};`:`@group(0) @binding(${t.binding++}) var<uniform> ${o}: ${R(c,t)};`}if(r==="constant"){if(t.headers.has(o))return o;let c=y(e,t),p=u(a,t);b=t.isWebGL?`const ${c} ${o} = ${p};`:`const ${o}: ${R(c,t)} = ${p};`}if(r==="varying"){if(t.headers.has(o))return o;b=`${y(e,t)} ${o}`}if(b)return t.headers.set(o,b),t.onMount?.(o),o;if(r==="variable")return o;if(r==="swizzle")return`${u(i,t)}.${u(a,t)}`;if(r==="ternary")return`(${u(a,t)} ? ${u(i,t)} : ${u(m,t)})`;if(r==="builtin")return t?.isWebGL?ce(o):o;if(r==="conversion")return`${R(a,t)}(${L(s.slice(1),t)})`;if(r==="operator")return a==="not"||a==="bitNot"?`!${u(i,t)}`:`(${u(i,t)} ${ue(a)} ${u(m,t)})`;if(r==="function")return a==="negate"?`(-${L(s.slice(1),t)})`:`${a}(${L(s.slice(1),t)})`;if(r==="scope")return s.map(c=>u(c,t)).join(`
23
- `);if(r==="assign")return`${u(a,t)} = ${u(i,t)};`;if(r==="loop")return t.isWebGL?`for (int i = 0; i < ${a}; i += 1) {
24
- ${u(i,t)}
25
- }`:`for (var i: i32 = 0; i < ${a}; i++) {
26
- ${u(i,t)}
27
- }`;if(r==="define"){let c=s.slice(2),p=`${o}(${c.map(T=>u(T,t))})`;return t.headers.has(o)||t.headers.set(o,le(n,t)),p}if(r==="if"){let c=`if (${u(a,t)}) {
28
- ${u(i,t)}
29
- }`;for(let p=2;p<s.length;p+=2){let T=p>=s.length-1;c+=T?` else {
30
- ${u(s[p],t)}
31
- }`:` else if (${u(s[p],t)}) {
32
- ${u(s[p+1],t)}
33
- }`}return c}if(r==="switch"){let c=`switch (${u(a,t)}) {
34
- `;for(let p=1;p<s.length;p+=2)p>=s.length-1&&s.length%2===0?c+=`default:
35
- ${u(s[p],t)}
1
+ import{durable as fe,event as Pt}from"reev";import{createFrame as St,createQueue as wt}from"refr";import{nested as pe}from"reev";var l={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)},lt=(e,t)=>e.forEach(t),It=(e,...t)=>{lt(e,r=>r(...t))},Ut=(e="",t="_",r="/")=>e.split(t).join(r),Ft=(e=".pdf")=>e.split(".").pop()?.toLowerCase()??"",At=(e=0)=>`${e}`.split(".")[1]?.length??0,Dt=(e=0)=>`${e}`.split(".")[0]?.length-(e<0?1:0),Bt=(e=0,t=-2)=>(t*=-1,t=Math.pow(10,t),e*=t,e=Math.round(e),e/=t,e);var R=["bool","uint","int","float","bvec2","ivec2","uvec2","vec2","bvec3","ivec3","uvec3","vec3","bvec4","ivec4","uvec4","vec4","color","mat2","mat3","mat4","texture","sampler2D"],Y=["toBool","toUint","toInt","toFloat","toBvec2","toIvec2","toUvec2","toVec2","toBvec3","toIvec3","toUvec3","toVec3","toBvec4","toIvec4","toUvec4","toVec4","toColor","toMat2","toMat3","toMat4"],H={add:"+",sub:"-",mul:"*",div:"/",mod:"%",equal:"==",notEqual:"!=",lessThan:"<",lessThanEqual:"<=",greaterThan:">",greaterThanEqual:">=",and:"&&",or:"||",bitAnd:"&",bitOr:"|",bitXor:"^",shiftLeft:"<<",shiftRight:">>"},xe=Object.keys(H),j=["dot","distance","length","lengthSq","determinant","luminance"],K=["all","any"],Z=["abs","sign","floor","ceil","round","fract","trunc","sin","cos","tan","asin","acos","atan","exp","exp2","log","log2","sqrt","inverseSqrt","normalize","oneMinus","saturate","negate","reciprocal","dFdx","dFdy","fwidth"],Q=["cross"],J=["reflect","refract"],ee=["min","max","mix","clamp","step","smoothstep"],te=["texture","textureLod","textureSize","cubeTexture"],mt=["atan2","degrees","faceforward","bitcast","cbrt","difference","equals","pow","pow2","pow3","pow4","radians","transformDirection"],de=[...j,...K,...Z,...Q,...J,...ee,...te,...mt],ge={float:"f32",int:"i32",uint:"u32",bool:"bool",vec2:"vec2f",vec3:"vec3f",vec4:"vec4f",mat2:"mat2x2f",mat3:"mat3x3f",mat4:"mat4x4f",ivec2:"vec2i",ivec3:"vec3i",ivec4:"vec4i",uvec2:"vec2u",uvec3:"vec3u",uvec4:"vec4u",bvec2:"vec2<bool>",bvec3:"vec3<bool>",bvec4:"vec4<bool>"},re={1:"float",2:"vec2",3:"vec3",4:"vec4",9:"mat3",16:"mat4"},be={position:"vec4",vertex_index:"uint",instance_index:"uint",front_facing:"bool",frag_depth:"float",sample_index:"uint",sample_mask:"uint",point_coord:"vec2",positionLocal:"vec3",positionWorld:"vec3",positionView:"vec3",normalLocal:"vec3",normalWorld:"vec3",normalView:"vec3",screenCoordinate:"vec2",screenUV:"vec2",gl_FragCoord:"vec4",gl_VertexID:"uint",gl_InstanceID:"uint",gl_FrontFacing:"bool",gl_FragDepth:"float",gl_SampleID:"uint",gl_SampleMask:"uint",gl_PointCoord:"vec2",normal:"vec3",uv:"vec2",color:"vec4"},ve=["equal","notEqual","lessThan","lessThanEqual","greaterThan","greaterThanEqual"],he=["and","or"],Xe={position:"gl_FragCoord",vertex_index:"gl_VertexID",instance_index:"gl_InstanceID",front_facing:"gl_FrontFacing",frag_depth:"gl_FragDepth",sample_index:"gl_SampleID",sample_mask:"gl_SampleMask",point_coord:"gl_PointCoord",uv:"gl_FragCoord.xy"};var oe=e=>l.str(e)&&/^[xyzwrgbastpq]{1,4}$/.test(e),ye=e=>xe.includes(e),Te=e=>de.includes(e),_e=e=>Y.includes(e),Ee=e=>!e||typeof e!="object"?!1:e.isProxy,Ce=e=>!(!e||e==="auto"),Ne=e=>{let t=(e>>16&255)/255,r=(e>>8&255)/255,n=(e&255)/255;return[t,r,n]},xt=0,w=()=>`i${xt++}`,O=(e,t)=>e.filter(r=>!l.und(r)&&!l.nul(r)).map(r=>c(r,t)).join(", "),S=(e,t)=>l.str(e)?t?.isWebGL?e:ge[e]:"",Pe=e=>H[e]||e,Se=e=>Xe[e],we=e=>{let t=Y.indexOf(e);return t!==-1?R[t]:"float"};var dt=(e,t)=>e.reduce((r,n)=>{let o=X(n,t),i=R.indexOf(r);return R.indexOf(o)>i?o:r},"float"),gt=e=>be[e],bt=(e,t,r)=>{let n=t.length>0?X(t[0],r):"float";return J.includes(e)?n:j.includes(e)?"float":K.includes(e)?"bool":Z.includes(e)?n:Q.includes(e)?"vec3":te.includes(e)?"vec4":ee.includes(e)?dt(t,r):n},Le=(e,t,r)=>{if(ve.includes(r)||he.includes(r))return"bool";if(e===t||e.includes("vec")&&!t.includes("vec"))return e;if(t.includes("vec")&&!e.includes("vec"))return t;let n=R.indexOf(e),o=R.indexOf(t);return n>=o?e:t},vt=e=>l.bol(e)?"bool":l.num(e)?"float":l.arr(e)&&re[e.length]||"float",ht=e=>re[e],Xt=(e,t)=>{if(e.length===0)return"void";let r=X(e[0],t);for(let n of e.slice(1))if(r!==X(n))throw new Error("glre node system error: defined scope return mismatch");return r},yt=(e,t)=>{let{type:r,props:n}=e,{id:o,children:i=[],layout:s,inferFrom:a}=n,[p,f,b]=i;return r==="conversion"?p:r==="operator"?Le(X(f,t),X(b,t),p):r==="function"?bt(p,i.slice(1),t):r==="swizzle"?ht(p.length):r==="ternary"?Le(X(f,t),X(b,t),"add"):r==="builtin"?gt(o):r==="define"&&Ce(s?.type)?s?.type:a?Xt(a,t):X(p,t)},X=(e,t)=>{if(t||(t={}),!Ee(e))return vt(e);if(t.infers||(t.infers=new WeakMap),t.infers.has(e))return t.infers.get(e);let r=yt(e,t);return t.infers.set(e,r),r};var Re=(e,t,r,n)=>{if(e.isWebGL)return`texture(${O(n?[t,r,n]:[t,r],e)})`;let o=c(t,e),i=[o,o+"Sampler",c(r,e)];return n?(i.push(c(n,e)),`textureSampleLevel(${i})`):`textureSample(${i})`},Oe=(e,t,r,n)=>{let o=`if (${c(t,e)}) {
2
+ ${c(r,e)}
3
+ }`;for(let i=2;i<n.length;i+=2){let s=i>=n.length-1;o+=s?` else {
4
+ ${c(n[i],e)}
5
+ }`:` else if (${c(n[i],e)}) {
6
+ ${c(n[i+1],e)}
7
+ }`}return o},Ge=(e,t,r)=>{let n=`switch (${c(t,e)}) {
8
+ `;for(let o=1;o<r.length;o+=2)o>=r.length-1&&r.length%2===0?n+=`default:
9
+ ${c(r[o],e)}
36
10
  break;
37
- `:p+1<s.length&&(c+=`case ${u(s[p],t)}:
38
- ${u(s[p+1],t)}
11
+ `:o+1<r.length&&(n+=`case ${c(r[o],e)}:
12
+ ${c(r[o+1],e)}
39
13
  break;
40
- `);return c+="}",c}if(r==="declare"){let c=y(a,t),p=i?.props?.id;if(t.isWebGL)return`${c} ${p} = ${u(a,t)};`;let T=R(c);return`var ${p}: ${T} = ${u(a,t)};`}return u(a,t)};var G=null;var de=e=>{G&&(G.props.children||(G.props.children=[]),G.props.children.push(e))},xe=(e,t)=>{t||(t=ae());let r=N("variable",{id:t,inferFrom:e}),n=N("declare",null,e,r);return de(n),r},ge=(e,t)=>{let r=N("assign",null,e,t);return de(r),e};var Ze=(e,t)=>{if(t==="string")return u(e)},N=(e,t,...r)=>{t||(t={}),r.length&&(t.children=r);let n=(a,i)=>{if(i==="type")return e;if(i==="props")return t;if(i==="toVar")return xe.bind(null,s);if(i==="assign")return ge.bind(null,s);if(i==="isProxy")return!0;if(i==="toString")return u.bind(null,s);if(i===Symbol.toPrimitive)return Ze.bind(null,s);if(k(i))return be(i,s);if(ne(i))return(...m)=>Qe(i,s,...m);if(se(i))return(...m)=>ve(i,s,...m);if(ie(i))return()=>C(pe(i),s)},o=(a,i,m)=>k(i)?(be(i,s).assign(m),!0):!1,s=new Proxy({},{get:n,set:o});return s};var I=(e,t)=>N("uniform",{id:t},e);var X=e=>N("builtin",{id:e}),be=(e,t)=>N("swizzle",null,e,t),Qe=(e,...t)=>N("operator",null,e,...t),ve=(e,...t)=>N("function",null,e,...t),C=(e,...t)=>N("conversion",null,e,...t);var Yt=X("position"),jt=X("vertex_index"),Ht=X("instance_index"),Kt=X("front_facing"),Zt=X("frag_depth"),Qt=X("sample_index"),Jt=X("sample_mask"),er=X("point_coord"),tr=X("normalLocal"),rr=X("normalWorld"),or=X("normalView"),nr=X("position"),sr=X("positionWorld"),ir=X("positionView"),ar=X("screenCoordinate"),ur=X("screenUV"),Je=e=>C("float",e);var ye=(e,t)=>C("vec2",e,t);var cr=I(ye(1280,800),"iResolution"),pr=I(ye(0,0),"iMouse"),fr=I(Je(0),"iTime");var et=`
41
- #version 300 es
42
- void main() {
43
- float x = float(gl_VertexID % 2) * 4.0 - 1.0;
44
- float y = float(gl_VertexID / 2) * 4.0 - 1.0;
45
- gl_Position = vec4(x, y, 0.0, 1.0);
46
- }
47
- `,tt=`
14
+ `);return n+="}",n},$e=(e,t,r)=>{let n=X(t,e),o=r?.props?.id;if(e.isWebGL)return`${n} ${o} = ${c(t,e)};`;let i=S(n);return`var ${o}: ${i} = ${c(t,e)};`},Ie=(e,t,r)=>{let{id:n,children:o=[],layout:i}=t,[s,...a]=o,p=[],f=[];if(i?.inputs)for(let g of i.inputs)p.push([g.name,g.type]);else for(let g=0;g<a.length;g++)p.push([`p${g}`,X(a[g],e)]);let b=[];if(e?.isWebGL){for(let[g,_]of p)f.push(`${_} ${g}`);b.push(`${r} ${n}(${f}) {`)}else{for(let[g,_]of p)f.push(`${g}: ${S(_,e)}`);b.push(`fn ${n}(${f}) -> ${S(r,e)} {`)}let x=c(s,e);return x&&b.push(x),b.push("}"),b.join(`
15
+ `)},Ue=(e,t,r)=>e.isWebGL?`${r} ${t};`:`@location(${e.vertVaryings.size}) ${t}: ${S(r,e)}`,Fe=(e,t,r)=>{let n=r==="sampler2D"||r==="texture";if(e.isWebGL)return n?`uniform sampler2D ${t};`:`uniform ${r} ${t};`;if(n){let{group:a=1,binding:p=0}=e.webgpu?.textures.map.get(t)||{};return`@group(${a}) @binding(${p}) var ${t}Sampler: sampler;
16
+ @group(${a}) @binding(${p+1}) var ${t}: texture_2d<f32>;`}let{group:o=0,binding:i=0}=e.webgpu?.uniforms.map.get(t)||{},s=S(r,e);return`@group(${o}) @binding(${i}) var<uniform> ${t}: ${s};`},Ae=(e,t,r)=>{if(e.isWebGL)return`${r} ${t};`;let{location:n=0}=e.webgpu?.attribs.map.get(t)||{},o=S(r,e);return`@location(${n}) ${t}: ${o}`},De=(e,t,r,n)=>e.isWebGL?`const ${r} ${t} = ${n};`:`const ${t}: ${S(r,e)} = ${n};`;var c=(e,t)=>{if(t||(t={}),t.headers||(t.headers=new Map),t.vertVaryings||(t.vertVaryings=new Map),t.fragInputs||(t.fragInputs=new Map),t.vertInputs||(t.vertInputs=new Map),t.vertOutputs||(t.vertOutputs=new Map,t.isWebGL||(t.fragInputs.set("position","@builtin(position) position: vec4f"),t.vertOutputs.set("position","@builtin(position) position: vec4f"))),l.str(e))return e;if(l.num(e)){let x=`${e}`;return x.includes(".")?x:x+".0"}if(l.bol(e))return e?"true":"false";if(!e)return"";let{type:r,props:n}=e,{id:o="",children:i=[]}=n,[s,a,p,f]=i;if(r==="variable")return o;if(r==="swizzle")return`${c(a,t)}.${c(s,t)}`;if(r==="ternary")return t.isWebGL?`(${c(s,t)} ? ${c(a,t)} : ${c(p,t)})`:`select(${c(s,t)}, ${c(a,t)}, ${c(p,t)})`;if(r==="conversion")return`${S(s,t)}(${O(i.slice(1),t)})`;if(r==="operator")return s==="not"||s==="bitNot"?`!${c(a,t)}`:`(${c(a,t)} ${Pe(s)} ${c(p,t)})`;if(r==="function")return s==="negate"?`(-${O(i.slice(1),t)})`:s==="texture"?Re(t,a,p,f):`${s}(${O(i.slice(1),t)})`;if(r==="scope")return i.map(x=>c(x,t)).join(`
17
+ `);if(r==="assign")return`${c(s,t)} = ${c(a,t)};`;if(r==="return")return`return ${c(s,t)};`;if(r==="loop")return t.isWebGL?`for (int i = 0; i < ${s}; i += 1) {
18
+ ${c(a,t)}
19
+ }`:`for (var i: i32 = 0; i < ${s}; i++) {
20
+ ${c(a,t)}
21
+ }`;if(r==="if")return Oe(t,s,a,i);if(r==="switch")return Ge(t,s,i);if(r==="declare")return $e(t,s,a);if(r==="define"){let x=`${o}(${O(i.slice(1),t)})`;return t.headers.has(o)||t.headers.set(o,Ie(t,n,X(e,t))),x}if(r==="varying"){if(t.vertOutputs.has(o))return t.isWebGL?`${o}`:`out.${o}`;let x=Ue(t,o,X(e,t));return t.fragInputs.set(o,x),t.vertOutputs.set(o,x),t.vertVaryings.set(o,c(s,t)),t.isWebGL?`${o}`:`out.${o}`}if(r==="builtin"){if(t.isWebGL)return Se(o);if(o==="position")return"out.position";let x=`@builtin(${o}) ${o}: ${S(X(e,t),t)}`;return t.isFrag?t.fragInputs.set(o,x):t.vertInputs.set(o,x),`in.${o}`}if(r==="attribute")return t.vertInputs.set(o,Ae(t,o,X(e,t))),t.isWebGL?`${o}`:`in.${o}`;if(t.headers.has(o))return o;let b="";return r==="uniform"&&(b=Fe(t,o,X(e,t))),r==="constant"&&(b=De(t,o,X(e,t),c(s,t))),b?(t.headers.set(o,b),o):c(s,t)};var G=null,U=null,F=e=>{if(!G||(G.props.children||(G.props.children=[]),G.props.children.push(e),e.type!=="return"||!U))return;let{props:t}=U;t.inferFrom||(t.inferFrom=[]),t.inferFrom.push(e)},Be=(e,t)=>{t||(t=w());let r=m("variable",{id:t,inferFrom:[e]}),n=m("declare",null,e,r);return F(n),r},Me=(e,t)=>{let r=m("assign",null,e,t);return F(r),e},Tt=e=>{let t=m("return",{inferFrom:[e]},e);return F(t),t},$=(e,t,r=U)=>{let n=G,o=U;G=e,U=r,n&&(e.props.parent=n);let i=t();i&&Tt(i),G=n,U=o},pr=(e,t)=>{let r=m("scope");$(r,t);let n=m("if",null,e,r);F(n);let o=()=>({ElseIf:(i,s)=>{let a=m("scope");return $(a,s),n.props.children.push(i,a),o()},Else:i=>{let s=m("scope");$(s,i),n.props.children.push(s)}});return o()},cr=(e,t)=>{let r=m("scope");$(r,()=>t({i:m("variable",{id:"i",inferFrom:[I(0)]})}));let n=m("loop",null,e,r);return F(n),n},fr=e=>{let t=m("switch",null,e);F(t);let r=()=>({Case:(...n)=>o=>{let i=m("scope");$(i,o);for(let s of n)t.props.children.push(s,i);return r()},Default:n=>{let o=m("scope");$(o,n),t.props.children.push(o)}});return r()},lr=e=>{let t,r=(...n)=>{let o=t?.name||w(),i=m("scope"),s=[],a=[];if(t?.inputs)for(let f of t.inputs)a.push({id:f.name,inferFrom:[d(f.type)]});else for(let f=0;f<n.length;f++)a.push({id:`p${f}`,inferFrom:[n[f]]});for(let f of a)s.push(m("variable",f));let p=m("define",{id:o,layout:t},i,...n);return $(i,()=>e(s),p),p};return r.setLayout=n=>(t=n,r),r};var _t=(e,t)=>{if(t==="string")return c(e)},m=(e,t,...r)=>{t||(t={}),r.length&&(t.children=r);let n=(s,a)=>{if(a==="type")return e;if(a==="props")return t;if(a==="toVar")return Be.bind(null,i);if(a==="assign")return Me.bind(null,i);if(a==="isProxy")return!0;if(a==="toString")return c.bind(null,i);if(a===Symbol.toPrimitive)return _t.bind(null,i);if(oe(a))return Ve(a,i);if(ye(a))return(...p)=>Et(a,i,...p);if(Te(a))return(...p)=>u(a,i,...p);if(_e(a))return()=>d(we(a),i)},o=(s,a,p)=>oe(a)?(Ve(a,i).assign(p),!0):!1,i=new Proxy({},{get:n,set:o});return i},vr=(e,t=w())=>m("attribute",{id:t},e),hr=(e,t=w())=>m("constant",{id:t},e),B=(e,t=w())=>m("uniform",{id:t},e),Xr=(e=w())=>m("variable",{id:e}),T=(e=w())=>m("builtin",{id:e}),yr=(e,t=w())=>m("varying",{id:t,inferFrom:[e]},e),Ve=(e,t)=>m("swizzle",null,e,t),Et=(e,...t)=>m("operator",null,e,...t),u=(e,...t)=>m("function",null,e,...t),d=(e,...t)=>m("conversion",null,e,...t),Tr=(e,t,r)=>m("ternary",null,e,t,r);var Ct=`
48
22
  #version 300 es
49
23
  precision mediump float;
50
- uniform vec2 iResolution;
51
24
  out vec4 fragColor;
52
- void main() {
53
- fragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
54
- }
55
- `,Xe=(e,t,r)=>{let n=e.createShader(r);if(!n)throw new Error("Failed to create shader");if(e.shaderSource(n,t.trim()),e.compileShader(n),e.getShaderParameter(n,e.COMPILE_STATUS))return n;let o=e.getShaderInfoLog(n);e.deleteShader(n),console.warn(`Could not compile shader: ${o}`)},_e=(e,t=et,r=tt,n=()=>{})=>{P(r)&&(r=w(r,{isWebGL:!0})),P(t)&&(t=O(r,{isWebGL:!0}));let o=e.createProgram(),s=Xe(e,t,e.VERTEX_SHADER),a=Xe(e,r,e.FRAGMENT_SHADER);if(!s||!a)return n();if(e.attachShader(o,s),e.attachShader(o,a),e.linkProgram(o),e.getProgramParameter(o,e.LINK_STATUS))return o;let i=e.getProgramInfoLog(o);e.deleteProgram(o),n(),console.warn(`Could not link pg: ${i}`)},Te=(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},he=(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},Ee=(e,t,r)=>{r&&(e=Math.max(...r)+1);let n=t.length/e;return Math.floor(n)},Ne=(e,t,r,n,o)=>{e.bindBuffer(e.ARRAY_BUFFER,n),e.enableVertexAttribArray(r),e.vertexAttribPointer(r,t,e.FLOAT,!1,0,0),o&&e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,o)},Pe=(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)};var Se=async e=>{let t=e.el.getContext("webgl2"),r=_e(t,e.vs,e.fs,()=>void(e.isLoop=!1));t.useProgram(r);let n=0,o=Y(d=>t.getUniformLocation(r,d)),s=Y(d=>t.getAttribLocation(r,d)),a=Y(()=>n++);return{webgl:{context:t,program:r},render:()=>{t.clear(t.COLOR_BUFFER_BIT),t.viewport(0,0,...e.size),t.drawArrays(t.TRIANGLES,0,3)},clean:()=>t.deleteProgram(r),_attribute:(d="",_,h)=>{let E=s(d,!0),v=Te(t,_),f=he(t,h),l=Ee(e.count,_,h);Ne(t,l,E,v,f)},_uniform:(d,_)=>{let h=o(d);if(g.num(_))return t.uniform1f(h,_);let E=_.length;if(E<=4)return t[`uniform${E}fv`](h,_);E=Math.sqrt(E)<<0,t[`uniformMatrix${E}fv`](h,!1,_)},_texture:(d,_)=>{let h=new Image;Object.assign(h,{src:_,crossOrigin:"anonymous"}),h.decode().then(()=>{let E=o(d),v=a(d);Pe(t,h,E,v)})}}};import{nested as j}from"reev";var rt=`
56
- @vertex
57
- fn main(@builtin(vertex_index) vertex_index: u32) -> @builtin(position) vec4f {
58
- let x = f32(vertex_index % 2u) * 4.0 - 1.0;
59
- let y = f32(vertex_index / 2u) * 4.0 - 1.0;
60
- return vec4f(x, y, 0.0, 1.0);
61
- }
62
- `,ot=`
63
- @group(0) @binding(0) var<uniform> iResolution: vec2f;
64
-
65
- @fragment
66
- fn main(@builtin(position) position: vec4f) -> @location(0) vec4f {
67
- return vec4f(position.xy / iResolution, 0.0, 1.0);
68
- }
69
- `,Re=async e=>{let t=navigator.gpu,r=t.getPreferredCanvasFormat(),o=await(await t.requestAdapter()).requestDevice();return e.configure({device:o,format:r,alphaMode:"opaque"}),{device:o,format:r}},Ce=(e,t,r,n,o=rt,s=ot)=>{P(o)&&(o=O(o,{isWebGL:!1})),P(s)&&(s=w(s,{isWebGL:!1}));let a=e.createPipelineLayout({bindGroupLayouts:n});return e.createRenderPipeline({vertex:{module:e.createShaderModule({code:o.trim()}),entryPoint:"main",buffers:r},fragment:{module:e.createShaderModule({code:s.trim()}),entryPoint:"main",targets:[{format:t}]},layout:a,primitive:{topology:"triangle-list"}})},Le=(e,t)=>{let r=[],n=[];t.forEach((a,i)=>{if(!a)return;let m="buffer"in a,b=a instanceof GPUTextureView,c=a instanceof GPUSampler;if(m)r.push({binding:i,visibility:3,buffer:{type:"uniform"}});else if(b)r.push({binding:i,visibility:2,texture:{}});else if(c)r.push({binding:i,visibility:2,sampler:{}});else return;n.push({binding:i,resource:a})});let o=e.createBindGroupLayout({entries:r}),s=e.createBindGroup({layout:o,entries:n});return{layout:o,bindGroup:s}},we=e=>({colorAttachments:[{view:e.getCurrentTexture().createView(),clearValue:{r:0,g:0,b:0,a:1},loadOp:"clear",storeOp:"store"}]}),nt=e=>Math.ceil(e/256)*256,Oe=(e,t)=>{let r=new Float32Array(t),n=e.createBuffer({size:r.byteLength,usage:40});return{array:r,buffer:n}},Ge=(e,t)=>{let r=new Float32Array(t),n=nt(r.byteLength),o=e.createBuffer({size:n,usage:72});return{array:r,buffer:o}},Ie=(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}},st=(e,t)=>e/t,it=e=>e===2?"float32x2":e===3?"float32x3":e===4?"float32x4":"float32",Fe=(e,t,r=6)=>{let n=st(t,r);return{arrayStride:n*4,attributes:[{shaderLocation:e,offset:0,format:it(n)}]}};var Ue=async e=>{let t=e.el.getContext("webgpu"),{device:r,format:n}=await Re(t),o={device:r,context:t,resources:[[],[]],loadingImg:0,needsUpdate:!0},s=[],a=[],i=[],m=j((v,f)=>{let{array:l,buffer:x}=Oe(r,f);return a.push(x),i.push(Fe(i.length,l.length,e.count)),o.needsUpdate=!0,{array:l,buffer:x}}),b=j((v,f)=>{let{array:l,buffer:x}=Ge(r,f);return o.resources[0].push({buffer:x}),o.needsUpdate=!0,{array:l,buffer:x}}),c=j((v,{width:f,height:l})=>{let{texture:x,sampler:F}=Ie(r,f,l);return o.resources[1].push(F,x.createView()),o.needsUpdate=!0,{texture:x,width:f,height:l}}),p=()=>{let v=[];s.length=0,o.resources.forEach(f=>{if(!f.length)return;let{layout:l,bindGroup:x}=Le(r,f);v.push(l),s.push(x)}),o.pipeline=Ce(r,n,i,v,e.vs,e.fs)};return{webgpu:o,render:()=>{if(o.loadingImg)return;o.needsUpdate&&p(),o.needsUpdate=!1;let v=r.createCommandEncoder(),f=v.beginRenderPass(we(t));f.setPipeline(o.pipeline),s.forEach((l,x)=>f.setBindGroup(x,l)),a.forEach((l,x)=>f.setVertexBuffer(x,l)),f.draw(e.count,1,0,0),f.end(),r.queue.submit([v.finish()])},clean:()=>{},_attribute:(v="",f)=>{let{array:l,buffer:x}=m(v,f);r.queue.writeBuffer(x,0,l)},_uniform:(v,f)=>{g.num(f)&&(f=[f]);let{array:l,buffer:x}=b(v,f);l.set(f),r.queue.writeBuffer(x,0,l)},_texture:(v,f)=>{o.loadingImg++;let l=Object.assign(new Image,{src:f,crossOrigin:"anonymous"});l.decode().then(()=>{let{texture:x,width:F,height:Ae}=c(v,l);r.queue.copyExternalImageToTexture({source:l},{texture:x},{width:F,height:Ae}),o.loadingImg--})}}};var Ar=e=>g.obj(e)?"isGL"in e:!1,pt=()=>typeof window>"u",ft=()=>pt()?!1:"gpu"in navigator,K=performance.now(),lt=e=>{let t=at({isNative:!1,isWebGL:!0,isLoop:!0,isGL:!0,size:[0,0],mouse:[0,0],count:6,webgl:{},webgpu:{}});return t.queue=ct(),t.frame=ut(),t.attribute=H((r,n,o)=>t.queue(()=>t._attribute?.(r,n,o))),t.texture=H((r,n)=>t.queue(()=>t._texture?.(r,n))),t.uniform=H((r,n,o)=>t.queue(()=>t._uniform?.(r,n,o))),t.uniform({iResolution:t.size,iMouse:[0,0],iTime:K}),t("mount",async()=>{t.vs=t.vs||t.vert||t.vertex,t.fs=t.fs||t.frag||t.fragment,ft()||(t.isWebGL=!0),t.isWebGL?t(await Se(t)):t(await Ue(t)),t.resize(),t.frame(()=>(t.loop(),t.queue.flush(),t.render(),t.isLoop)),!t.isNative&&(window.addEventListener("resize",t.resize),t.el.addEventListener("mousemove",t.mousemove))}),t("clean",()=>{t.frame.stop(),t.frame.clean(t.render),!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,a]=t.size,{top:i,left:m}=t.el.getBoundingClientRect();t.mouse[0]=(n-i-s/2)/(s/2),t.mouse[1]=-(o-m-a/2)/(a/2),t.uniform("iMouse",t.mouse)}),t("loop",()=>{K=performance.now()/1e3,t.uniform("iTime",K)}),t(e)},$r=lt;export{nt as alignTo256,Ne as createAttrib,Le as createBindGroup,Fe as createBufferLayout,we as createDescriptor,Re as createDevice,lt as createGL,he as createIbo,Ce as createPipeline,_e as createProgram,Pe as createTexture,Ie as createTextureSampler,Ge as createUniformBuffer,Te as createVbo,Oe as createVertexBuffer,$r as default,tt as defaultFragmentGLSL,et as defaultVertexGLSL,bt as dig,$e as each,xt as ext,gt as fig,mt as flush,Ee as getStride,g as is,Ar as isGL,pt as isServer,ft as isWebGPUSupported,dt as replace,vt as sig,Se as webgl,Ue as webgpu};
25
+ `.trim(),We=(e,t)=>{let r=c(e,t);return[Array.from(t.headers.values()).join(`
26
+ `),r]},ne=(e,t)=>`struct ${e} {
27
+ ${Array.from(t.values()).join(`,
28
+ `)}
29
+ }`,M=(e,t)=>{t.headers?.clear(),t.isFrag=!1;let[r,n]=We(e,t),o=[];if(t.isWebGL){o.push("#version 300 es");for(let s of t.vertInputs.values())o.push(`in ${s}`);for(let s of t.vertOutputs.values())o.push(`out ${s}`);o.push(r),o.push("void main() {"),o.push(` gl_Position = ${n};`);for(let[s,a]of t.vertVaryings.entries())o.push(` ${s} = ${a};`)}else{t.vertInputs?.size&&o.push(ne("In",t.vertInputs)),t.vertOutputs?.size&&o.push(ne("Out",t.vertOutputs)),o.push(r),o.push("@vertex"),o.push(`fn main(${t.vertInputs?.size?"in: In":""}) -> Out {`),o.push(" var out: Out;"),o.push(` out.position = ${n};`);for(let[s,a]of t.vertVaryings.entries())o.push(` out.${s} = ${a};`);o.push(" return out;")}o.push("}");let i=o.filter(Boolean).join(`
30
+ `);return console.log(`\u2193\u2193\u2193generated\u2193\u2193\u2193
31
+ ${i}`),i},V=(e,t)=>{t.headers?.clear(),t.isFrag=!0;let[r,n]=We(e,t),o=[];if(t.isWebGL){o.push(Ct);for(let s of t.fragInputs.values())o.push(`in ${s}`);o.push(r),o.push(`void main() {
32
+ fragColor = ${n};`)}else t.fragInputs?.size&&o.push(ne("Out",t.fragInputs)),o.push(r),o.push(`@fragment
33
+ fn main(out: Out) -> @location(0) vec4f {`),o.push(` return ${n};`);o.push("}");let i=o.filter(Boolean).join(`
34
+ `);return console.log(`\u2193\u2193\u2193generated\u2193\u2193\u2193
35
+ ${i}`),i},se=T("position"),ie=T("vertex_index"),Sr=T("instance_index"),wr=T("front_facing"),Lr=T("frag_depth"),Rr=T("sample_index"),Or=T("sample_mask"),Gr=T("point_coord"),$r=T("normalLocal"),Ir=T("normalWorld"),Ur=T("normalView"),Fr=T("position"),Ar=T("positionWorld"),Dr=T("positionView"),Br=T("screenCoordinate"),Mr=T("screenUV"),z=e=>d("float",e),I=e=>d("int",e),Vr=e=>d("uint",e),zr=e=>d("bool",e),qe=(e,t)=>d("vec2",e,t),ze=(e,t,r)=>d("vec3",e,t,r),ae=(e,t,r,n)=>d("vec4",e,t,r,n),Wr=(...e)=>d("mat2",...e),qr=(...e)=>d("mat3",...e),kr=(...e)=>d("mat4",...e),Yr=(e,t)=>d("ivec2",e,t),Hr=(e,t,r)=>d("ivec3",e,t,r),jr=(e,t,r,n)=>d("ivec4",e,t,r,n),Kr=(e,t)=>d("uvec2",e,t),Zr=(e,t,r)=>d("uvec3",e,t,r),Qr=(e,t,r,n)=>d("uvec4",e,t,r,n),Jr=(e,t)=>d("bvec2",e,t),eo=(e,t,r)=>d("bvec3",e,t,r),to=(e,t,r,n)=>d("bvec4",e,t,r,n),ro=()=>d("texture"),oo=()=>d("sampler2D"),no=(e,t,r)=>l.num(e)&&l.und(t)&&l.und(r)?ze(...Ne(e)):ze(e,t,r),ue=B(qe(1280,800),"iResolution"),so=B(qe(0,0),"iMouse"),io=B(z(0),"iTime"),ao=()=>se.xy.div(ue),uo=(e,t,r)=>u("texture",e,t,r),po=(e,t,r)=>u("cubeTexture",e,t,r),co=(e,t)=>u("textureSize",e,t),fo=e=>u("abs",e),lo=e=>u("acos",e),mo=e=>u("all",e),xo=e=>u("any",e),go=e=>u("asin",e),bo=(e,t)=>t!==void 0?u("atan",e,t):u("atan",e),vo=(e,t)=>u("atan",e,t),ho=(e,t)=>u("bitcast",e,t),Xo=e=>u("cbrt",e),yo=e=>u("ceil",e),To=(e,t,r)=>u("clamp",e,t,r),_o=e=>u("cos",e),Eo=(e,t)=>u("cross",e,t),Co=e=>u("dFdx",e),No=e=>u("dFdy",e),Po=e=>u("degrees",e),So=(e,t)=>u("difference",e,t),wo=(e,t)=>u("distance",e,t),Lo=(e,t)=>u("dot",e,t),Ro=(e,t)=>u("equals",e,t),Oo=e=>u("exp",e),Go=e=>u("exp2",e),$o=(e,t,r)=>u("faceforward",e,t,r),Io=e=>u("floor",e),ke=e=>u("fract",e),Uo=e=>u("fwidth",e),Fo=e=>u("inverseSqrt",e),Ao=e=>u("length",e),Do=e=>u("lengthSq",e),Bo=e=>u("log",e),Mo=e=>u("log2",e),Vo=(e,t)=>u("max",e,t),zo=(e,t)=>u("min",e,t),Wo=(e,t,r)=>u("mix",e,t,r),qo=e=>u("negate",e),ko=e=>u("normalize",e),Yo=e=>u("oneMinus",e),Ho=(e,t)=>u("pow",e,t),jo=e=>u("pow2",e),Ko=e=>u("pow3",e),Zo=e=>u("pow4",e),Qo=e=>u("radians",e),Jo=e=>u("reciprocal",e),en=(e,t)=>u("reflect",e,t),tn=(e,t,r)=>u("refract",e,t,r),rn=e=>u("round",e),on=e=>u("saturate",e),nn=e=>u("sign",e),sn=e=>u("sin",e),an=(e,t,r)=>u("smoothstep",e,t,r),un=e=>u("sqrt",e),pn=(e,t)=>u("step",e,t),cn=e=>u("tan",e),fn=(e,t)=>u("transformDirection",e,t),ln=e=>u("trunc",e);var Ye=(e,t,r)=>{let n=e.createShader(r);if(!n)throw new Error("Failed to create shader");if(e.shaderSource(n,t.trim()),e.compileShader(n),e.getShaderParameter(n,e.COMPILE_STATUS))return n;let o=e.getShaderInfoLog(n);e.deleteShader(n),console.warn(`Could not compile shader: ${o}`)},He=(e,t,r,n=()=>{},o)=>{let i={isWebGL:!0,gl:o};l.str(r)||(r=V(r,i)),l.str(t)||(t=M(t,i));let s=e.createProgram(),a=Ye(e,t,e.VERTEX_SHADER),p=Ye(e,r,e.FRAGMENT_SHADER);if(!a||!p)return n();if(e.attachShader(s,a),e.attachShader(s,p),e.linkProgram(s),e.getProgramParameter(s,e.LINK_STATUS))return s;let f=e.getProgramInfoLog(s);e.deleteProgram(s),n(),console.warn(`Could not link pg: ${f}`)},je=(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},Ke=(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},Ze=(e,t,r)=>{r&&(e=Math.max(...r)+1);let n=t.length/e;return Math.floor(n)},Qe=(e,t,r,n,o)=>{e.bindBuffer(e.ARRAY_BUFFER,n),e.enableVertexAttribArray(r),e.vertexAttribPointer(r,t,e.FLOAT,!1,0,0),o&&e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,o)},Je=(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)};var et=async e=>{let t=e.el.getContext("webgl2"),r=He(t,e.vs,e.fs,()=>void(e.isLoop=!1),e);t.useProgram(r);let n=0,o=pe(g=>t.getUniformLocation(r,g)),i=pe(g=>t.getAttribLocation(r,g)),s=pe(()=>n++);return{render:()=>{t.clear(t.COLOR_BUFFER_BIT),t.viewport(0,0,...e.size),t.drawArrays(t.TRIANGLES,0,3)},clean:()=>t.deleteProgram(r),_attribute:(g="",_,E)=>{let N=i(g,!0),A=je(t,_),W=Ke(t,E),q=Ze(e.count,_,E);Qe(t,q,N,A,W)},_uniform:(g,_)=>{let E=o(g);if(l.num(_))return t.uniform1f(E,_);let N=_.length;if(N<=4)return t[`uniform${N}fv`](E,_);N=Math.sqrt(N)<<0,t[`uniformMatrix${N}fv`](E,!1,_)},_texture:(g,_)=>{let E=new Image;Object.assign(E,{src:_,crossOrigin:"anonymous"}),E.decode().then(()=>{let N=o(g),A=s(g);Je(t,E,N,A)})},webgl:{context:t,program:r}}};import{nested as ce}from"reev";var tt=async e=>{let t=navigator.gpu,r=t.getPreferredCanvasFormat(),o=await(await t.requestAdapter()).requestDevice();return e.configure({device:o,format:r,alphaMode:"opaque"}),{device:o,format:r}},rt=()=>{let e=0,t=0,r=0;return{uniform:()=>{let n=Math.floor(e/12),o=e%12;return e++,{group:n,binding:o}},texture:()=>{let o=Math.floor(e/12)+1+Math.floor(t/6),i=t%6*2;return t++,{group:o,binding:i}},attrib:()=>{let n=r;return r++,{location:n}}}},ot=(e,t,r,n,o,i,s)=>{let a={isWebGL:!1,webgpu:o};l.str(s)||(s=V(s,a)),l.str(i)||(i=M(i,a));let p=e.createPipelineLayout({bindGroupLayouts:n});return e.createRenderPipeline({vertex:{module:e.createShaderModule({code:i.trim()}),entryPoint:"main",buffers:r},fragment:{module:e.createShaderModule({code:s.trim()}),entryPoint:"main",targets:[{format:t}]},layout:p,primitive:{topology:"triangle-list"},depthStencil:{depthWriteEnabled:!0,depthCompare:"less",format:"depth24plus"}})},nt=(e,t)=>{let r=new Float32Array(t),n=Math.ceil(r.byteLength/256)*256,o=e.createBuffer({size:n,usage:72});return{array:r,buffer:o}},st=(e,t)=>{let r=new Float32Array(t),n=e.createBuffer({size:r.byteLength,usage:40});return{array:r,buffer:n}},it=(e,t,r)=>{let n=new Map,o=(s=0)=>(n.has(s)||n.set(s,{entries0:[],entries1:[]}),n.get(s));for(let{binding:s,buffer:a,group:p}of t.values()){let{entries0:f,entries1:b}=o(p);f.push({binding:s,visibility:3,buffer:{type:"uniform"}}),b.push({binding:s,resource:{buffer:a}})}for(let{binding:s,group:a,sampler:p,texture:f}of r.values()){let{entries0:b,entries1:x}=o(a);b.push({binding:s,visibility:2,sampler:{}}),b.push({binding:s+1,visibility:2,texture:{}}),x.push({binding:s,resource:p}),x.push({binding:s+1,resource:f.createView()})}let i={bindGroups:[],bindGroupLayouts:[]};for(let[s,{entries0:a,entries1:p}]of n)i.bindGroupLayouts[s]=e.createBindGroupLayout({entries:a}),i.bindGroups[s]=e.createBindGroup({layout:i.bindGroupLayouts[s],entries:p});return i},at=(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"}}),ut=(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}},pt=(e,t,r)=>e.createTexture({size:[t,r],format:"depth24plus",usage:GPUTextureUsage.RENDER_ATTACHMENT}),Nt=e=>e===2?"float32x2":e===3?"float32x3":e===4?"float32x4":"float32",ct=e=>{let t=[],r=[];for(let[,{buffer:n,location:o,stride:i}]of e)t[o]=n,r[o]={arrayStride:i*4,attributes:[{shaderLocation:o,offset:0,format:Nt(i)}]};return{vertexBuffers:t,bufferLayouts:r}};var ft=async e=>{let t=e.el.getContext("webgpu"),{device:r,format:n}=await tt(t),o=rt(),i=0,s=!0,a,p=y=>{},f=ce((y,v)=>{s=!0;let{group:h,binding:C}=o.uniform(),{array:L,buffer:P}=nt(r,v);return{array:L,buffer:P,binding:C,group:h}}),b=ce((y,{width:v,height:h})=>{s=!0;let{group:C,binding:L}=o.texture(),{texture:P,sampler:D}=ut(r,v,h);return{texture:P,sampler:D,binding:L,group:C}}),x=ce((y,v)=>{s=!0;let h=v.length/e.count,{location:C}=o.attrib(),{array:L,buffer:P}=st(r,v);return{array:L,buffer:P,location:C,stride:h}}),g=()=>{let{vertexBuffers:y,bufferLayouts:v}=ct(x.map),{bindGroups:h,bindGroupLayouts:C}=it(r,f.map,b.map),L=ot(r,n,v,C,me,e.vs,e.fs);p=P=>{P.setPipeline(L),h.forEach((D,k)=>P.setBindGroup(k,D)),y.forEach((D,k)=>P.setVertexBuffer(k,D)),P.draw(e.count,1,0,0),P.end()}},_=()=>{if(i)return;s&&g(),s=!1;let y=r.createCommandEncoder();p(y.beginRenderPass(at(t,a))),r.queue.submit([y.finish()])},E=()=>{let y=e.el;a?.destroy(),a=pt(r,y.width,y.height)},N=()=>{a?.destroy()},A=(y="",v)=>{let{array:h,buffer:C}=x(y,v);h.set(v),r.queue.writeBuffer(C,0,h)},W=(y,v)=>{l.num(v)&&(v=[v]);let{array:h,buffer:C}=f(y,v);h.set(v),r.queue.writeBuffer(C,0,h)},q=(y,v)=>{i++;let h=Object.assign(new Image,{src:v,crossOrigin:"anonymous"});h.decode().then(()=>{let C=b(y,h);r.queue.copyExternalImageToTexture({source:h},{texture:C.texture},{width:h.width,height:h.height}),i--})};E();let me={device:r,uniforms:f,textures:b,attribs:x};return{webgpu:me,render:_,resize:E,clean:N,_attribute:A,_uniform:W,_texture:q}};var Bn=e=>l.obj(e)?"isGL"in e:!1,Lt=()=>typeof window>"u",Rt=()=>Lt()?!1:"gpu"in navigator,le=performance.now(),Ot=()=>ae(ke(se.xy.div(ue)),0,1),Gt=()=>ae(z(I(ie).mod(I(2))).mul(4).sub(1),z(I(ie).div(I(2))).mul(4).sub(1),0,1),$t=e=>{let t=Pt({isNative:!1,isWebGL:!0,isLoop:!0,isGL:!0,size:[0,0],mouse:[0,0],count:6,webgl:{},webgpu:{}});return t.queue=wt(),t.frame=St(),t.attribute=fe((r,n,o)=>t.queue(()=>t._attribute?.(r,n,o)),t),t.uniform=fe((r,n,o)=>t.queue(()=>t._uniform?.(r,n,o)),t),t.texture=fe((r,n)=>t.queue(()=>t._texture?.(r,n)),t),t.uniform({iResolution:t.size,iMouse:[0,0],iTime:le}),t("mount",async()=>{t.vs=t.vs||t.vert||t.vertex||Gt(),t.fs=t.fs||t.frag||t.fragment||Ot(),Rt()||(t.isWebGL=!0),t.isWebGL?t(await et(t)):t(await ft(t)),t.resize(),t.frame(()=>(t.loop(),t.queue.flush(),t.render(),t.isLoop)),!t.isNative&&(window.addEventListener("resize",t.resize),t.el.addEventListener("mousemove",t.mousemove))}),t("clean",()=>{t.frame.stop(),t.frame.clean(t.render),!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[i,s]=t.size,{top:a,left:p}=t.el.getBoundingClientRect();t.mouse[0]=(n-a-i/2)/(i/2),t.mouse[1]=-(o-p-s/2)/(s/2),t.uniform("iMouse",t.mouse)}),t("loop",()=>{le=performance.now()/1e3,t.uniform("iTime",le)}),t(e)},Mn=$t;export{lr as Fn,pr as If,cr as Loop,Tt as Return,fr as Switch,fo as abs,lo as acos,mo as all,xo as any,go as asin,Me as assign,bo as atan,vo as atan2,vr as attribute,ho as bitcast,zr as bool,T as builtin,Jr as bvec2,eo as bvec3,to as bvec4,Xo as cbrt,yo as ceil,To as clamp,c as code,no as color,hr as constant,d as conversion,we as conversionToConstant,_o as cos,Qe as createAttrib,st as createAttribBuffer,it as createBindGroup,rt as createBindings,pt as createDepthTexture,at as createDescriptor,tt as createDevice,$t as createGL,Ke as createIbo,ot as createPipeline,He as createProgram,Je as createTexture,ut as createTextureSampler,nt as createUniformBuffer,je as createVbo,ct as createVertexBuffers,Eo as cross,po as cubeTexture,Co as dFdx,No as dFdy,Mn as default,Po as degrees,So as difference,Dt as dig,wo as distance,Lo as dot,lt as each,Ro as equals,Oo as exp,Go as exp2,Ft as ext,$o as faceforward,At as fig,z as float,Io as floor,It as flush,S as formatConversions,ke as fract,Lr as fragDepth,V as fragment,wr as frontFacing,u as function_,Uo as fwidth,Se as getBluiltin,w as getId,Pe as getOperator,Ze as getStride,Ne as hex2rgb,so as iMouse,ue as iResolution,io as iTime,Sr as instanceIndex,I as int,Fo as inverseSqrt,l as is,Ce as isConstantsType,_e as isConversion,Te as isFunction,Bn as isGL,Ee as isNodeProxy,ye as isOperator,Lt as isServer,oe as isSwizzle,Rt as isWebGPUSupported,Yr as ivec2,Hr as ivec3,jr as ivec4,O as joins,Ao as length,Do as lengthSq,Bo as log,Mo as log2,Wr as mat2,qr as mat3,kr as mat4,Vo as max,zo as min,Wo as mix,qo as negate,m as node,$r as normalLocal,Ur as normalView,Ir as normalWorld,ko as normalize,Yo as oneMinus,Et as operator,Gr as pointCoord,se as position,Fr as positionLocal,Dr as positionView,Ar as positionWorld,Ho as pow,jo as pow2,Ko as pow3,Zo as pow4,Qo as radians,Jo as reciprocal,en as reflect,tn as refract,Ut as replace,rn as round,Rr as sampleIndex,Or as sampleMask,oo as sampler2D,on as saturate,Br as screenCoordinate,Mr as screenUV,Tr as select,Bt as sig,nn as sign,sn as sin,an as smoothstep,un as sqrt,pn as step,Ve as swizzle,cn as tan,uo as texture,ro as texture2D,co as textureSize,Be as toVar,fn as transformDirection,ln as trunc,Vr as uint,B as uniform,ao as uv,Kr as uvec2,Zr as uvec3,Qr as uvec4,Xr as variable,qe as vec2,ze as vec3,ae as vec4,M as vertex,ie as vertexIndex,yr as vertexStage,et as webgl,ft as webgpu};
70
36
  //# sourceMappingURL=index.js.map