@margoengine/shader 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1771 -3
- package/dist/black-hole.d.ts +246 -0
- package/dist/black-hole.js +445 -0
- package/dist/clock-BLGNexlt.js +52 -0
- package/dist/clock.d.ts +42 -0
- package/dist/compute-D-BghbXj.js +354 -0
- package/dist/compute-texture.d.ts +100 -0
- package/dist/compute-texture.js +131 -0
- package/dist/compute-volume.d.ts +57 -0
- package/dist/compute-volume.js +130 -0
- package/dist/compute.d.ts +194 -0
- package/dist/control-DvjfV7R0.js +201 -0
- package/dist/control.d.ts +93 -0
- package/dist/deform.d.ts +111 -0
- package/dist/deform.js +89 -0
- package/dist/display-BrJM_Qkd.js +5 -0
- package/dist/editor-model.d.ts +192 -0
- package/dist/editor-pass.d.ts +71 -0
- package/dist/editor-pass.js +216 -0
- package/dist/editor.d.ts +150 -0
- package/dist/editor.js +1071 -0
- package/dist/effects.d.ts +150 -0
- package/dist/effects.js +87 -0
- package/dist/errors-CsQwshHA.js +7 -0
- package/dist/glsl/emit.d.ts +43 -0
- package/dist/glsl/host.d.ts +55 -0
- package/dist/glsl/material.d.ts +173 -0
- package/dist/glsl/parser.d.ts +76 -0
- package/dist/glsl/preprocess.d.ts +9 -0
- package/dist/glsl/presets.d.ts +33 -0
- package/dist/glsl/program.d.ts +210 -0
- package/dist/glsl/scene.d.ts +9 -0
- package/dist/glsl-material.d.ts +82 -0
- package/dist/glsl-material.js +288 -0
- package/dist/glsl-pass.d.ts +93 -0
- package/dist/glsl-pass.js +186 -0
- package/dist/glsl.d.ts +94 -0
- package/dist/glsl.js +170 -0
- package/dist/gpu-DQ5Jmggz.js +31 -0
- package/dist/graph.d.ts +82 -0
- package/dist/graph.js +365 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/lighting-Dx8_RsBz.js +77 -0
- package/dist/lighting.d.ts +62 -0
- package/dist/lighting.js +1 -0
- package/dist/material-CU7p-Q8u.js +1184 -0
- package/dist/material-REzUrXtu.js +275 -0
- package/dist/material.d.ts +74 -0
- package/dist/node-DzKv9CF2.js +590 -0
- package/dist/node.d.ts +427 -0
- package/dist/noise.d.ts +38 -0
- package/dist/noise.js +107 -0
- package/dist/own-stack-D1XnXrrz.js +11 -0
- package/dist/own-stack.d.ts +12 -0
- package/dist/pass.d.ts +108 -0
- package/dist/pass.js +236 -0
- package/dist/post-DC62ZleD.js +113 -0
- package/dist/post.d.ts +69 -0
- package/dist/preprocess-CWGWlBWQ.js +1195 -0
- package/dist/raw-D0W8U3Qa.js +1351 -0
- package/dist/raw.d.ts +634 -0
- package/dist/raw.js +1 -0
- package/dist/readback.d.ts +42 -0
- package/dist/readback.js +84 -0
- package/dist/scope.d.ts +21 -0
- package/dist/serialization.d.ts +11 -0
- package/dist/serialization.js +192 -0
- package/dist/shadertoy/sound.d.ts +48 -0
- package/dist/shadertoy.d.ts +249 -0
- package/dist/shadertoy.js +909 -0
- package/dist/shared/caster.d.ts +152 -0
- package/dist/shared/fog.d.ts +33 -0
- package/dist/shared/fractal.d.ts +9 -0
- package/dist/shared/msaa.d.ts +25 -0
- package/dist/shared/stack.d.ts +22 -0
- package/dist/sound-CF_dzP41.js +78 -0
- package/dist/storage-format-DWaiSxmq.js +25 -0
- package/dist/storage-format.d.ts +7 -0
- package/dist/subgroups.d.ts +97 -0
- package/dist/subgroups.js +95 -0
- package/dist/texture-D6vy0bam.js +62 -0
- package/dist/texture-array.d.ts +7 -0
- package/dist/texture-array.js +11 -0
- package/dist/texture.d.ts +16 -0
- package/dist/utils.d.ts +106 -0
- package/dist/utils.js +90 -0
- package/dist/view-rect-Bm1ac-TK.js +16 -0
- package/package.json +151 -6
|
@@ -0,0 +1,909 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { t as e } from "./errors-CsQwshHA.js";
|
|
5
|
+
import { a as t, c as n, l as r, o as i, s as a } from "./raw-D0W8U3Qa.js";
|
|
6
|
+
import { t as o } from "./material-CU7p-Q8u.js";
|
|
7
|
+
import { ShaderPass as s } from "./glsl-pass.js";
|
|
8
|
+
import { MargoError as c } from "@margoengine/math/errors";
|
|
9
|
+
//#region ../shared/copy-external.js
|
|
10
|
+
var l = /* @__PURE__ */ new WeakSet(), u = /* @__PURE__ */ new WeakMap();
|
|
11
|
+
function d(e) {
|
|
12
|
+
return !!e && typeof e == "object" && ("videoWidth" in e || "displayWidth" in e);
|
|
13
|
+
}
|
|
14
|
+
var f = (e) => [e.videoWidth ?? e.displayWidth ?? e.naturalWidth ?? e.width ?? 0, e.videoHeight ?? e.displayHeight ?? e.naturalHeight ?? e.height ?? 0];
|
|
15
|
+
function p(e, t, n, r) {
|
|
16
|
+
let i = e.queue, a = t.source, o = d(a);
|
|
17
|
+
if (o && !l.has(i)) try {
|
|
18
|
+
return i.copyExternalImageToTexture(t, n, r);
|
|
19
|
+
} catch (e) {
|
|
20
|
+
if (!(e instanceof TypeError)) throw e;
|
|
21
|
+
l.add(i);
|
|
22
|
+
}
|
|
23
|
+
if (!o) return i.copyExternalImageToTexture(t, n, r);
|
|
24
|
+
let [s, c] = f(a), p = u.get(a);
|
|
25
|
+
if (!p || p.w !== s || p.h !== c) {
|
|
26
|
+
let e = typeof OffscreenCanvas == "function" ? new OffscreenCanvas(s, c) : document.createElement("canvas");
|
|
27
|
+
e.width = s, e.height = c, u.set(a, p = {
|
|
28
|
+
canvas: e,
|
|
29
|
+
w: s,
|
|
30
|
+
h: c
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
p.canvas.getContext("2d").drawImage(a, 0, 0), i.copyExternalImageToTexture({
|
|
34
|
+
...t,
|
|
35
|
+
source: p.canvas
|
|
36
|
+
}, n, r);
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/shadertoy.ts
|
|
40
|
+
var m = [
|
|
41
|
+
"iChannel0",
|
|
42
|
+
"iChannel1",
|
|
43
|
+
"iChannel2",
|
|
44
|
+
"iChannel3"
|
|
45
|
+
], h = [
|
|
46
|
+
"A",
|
|
47
|
+
"B",
|
|
48
|
+
"C",
|
|
49
|
+
"D"
|
|
50
|
+
], g = {
|
|
51
|
+
"2d": "sampler2D",
|
|
52
|
+
cube: "samplerCube",
|
|
53
|
+
"3d": "sampler3D"
|
|
54
|
+
}, _ = {
|
|
55
|
+
keyboard: [
|
|
56
|
+
256,
|
|
57
|
+
3,
|
|
58
|
+
1
|
|
59
|
+
],
|
|
60
|
+
audio: [
|
|
61
|
+
512,
|
|
62
|
+
2,
|
|
63
|
+
1
|
|
64
|
+
]
|
|
65
|
+
}, v = (e, t) => Math.floor(Math.log2(Math.max(e, t))) + 1;
|
|
66
|
+
function y(e, t) {
|
|
67
|
+
let n = [
|
|
68
|
+
0,
|
|
69
|
+
1,
|
|
70
|
+
2,
|
|
71
|
+
3
|
|
72
|
+
].map((t) => [
|
|
73
|
+
0,
|
|
74
|
+
1,
|
|
75
|
+
2,
|
|
76
|
+
3,
|
|
77
|
+
4,
|
|
78
|
+
5,
|
|
79
|
+
6,
|
|
80
|
+
7
|
|
81
|
+
].map((n) => n < 4 ? e[n * 4 + t] : +(n - 4 === t)));
|
|
82
|
+
for (let e = 0; e < 4; e++) {
|
|
83
|
+
let t = e;
|
|
84
|
+
for (let r = e + 1; r < 4; r++) Math.abs(n[r][e]) > Math.abs(n[t][e]) && (t = r);
|
|
85
|
+
let r = n[t], i = r[e];
|
|
86
|
+
n[t] = n[e], n[e] = r;
|
|
87
|
+
for (let e = 0; e < 8; e++) r[e] = r[e] / i;
|
|
88
|
+
for (let t of n) if (t !== r) for (let n = 0, i = t[e]; n < 8; n++) t[n] = t[n] - i * r[n];
|
|
89
|
+
}
|
|
90
|
+
for (let e = 0; e < 4; e++) for (let r = 0; r < 4; r++) t[r * 4 + e] = n[e][4 + r];
|
|
91
|
+
}
|
|
92
|
+
var b = class {
|
|
93
|
+
constructor() {
|
|
94
|
+
this.made = /* @__PURE__ */ new Map(), this.flat = { bind: (e) => [this.view(e, "2d"), void 0] };
|
|
95
|
+
}
|
|
96
|
+
view(e, t) {
|
|
97
|
+
return w(this.texture(e, t), t);
|
|
98
|
+
}
|
|
99
|
+
texture(e, t) {
|
|
100
|
+
let n = this.made.get(e);
|
|
101
|
+
n || this.made.set(e, n = {});
|
|
102
|
+
let r = n[t];
|
|
103
|
+
if (!r) {
|
|
104
|
+
let i = t === "cube" ? 6 : 1;
|
|
105
|
+
r = n[t] = e.createTexture({
|
|
106
|
+
label: "shader:shadertoy:blank",
|
|
107
|
+
size: [
|
|
108
|
+
1,
|
|
109
|
+
1,
|
|
110
|
+
i
|
|
111
|
+
],
|
|
112
|
+
dimension: t === "3d" ? "3d" : "2d",
|
|
113
|
+
format: "rgba8unorm",
|
|
114
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
|
|
115
|
+
}), e.queue.writeTexture({ texture: r }, new Uint8Array(i * 4).fill(255).map((e, t) => t % 4 == 3 ? e : 0), {
|
|
116
|
+
bytesPerRow: 4,
|
|
117
|
+
rowsPerImage: 1
|
|
118
|
+
}, [
|
|
119
|
+
1,
|
|
120
|
+
1,
|
|
121
|
+
i
|
|
122
|
+
]);
|
|
123
|
+
}
|
|
124
|
+
return r;
|
|
125
|
+
}
|
|
126
|
+
of(e) {
|
|
127
|
+
return e === "2d" ? this.flat : this.device ? this.texture(this.device, e) : null;
|
|
128
|
+
}
|
|
129
|
+
destroy() {
|
|
130
|
+
for (let e of this.made.values()) for (let t of Object.values(e)) t?.destroy();
|
|
131
|
+
this.made.clear();
|
|
132
|
+
}
|
|
133
|
+
}, x = "@group(0) @binding(0) var src:texture_2d<f32>;\n@vertex fn v(@builtin(vertex_index) i:u32)->@builtin(position) vec4f{return vec4f(f32(i&1u)*4.0-1.0,f32(i>>1u)*4.0-1.0,0.0,1.0);}\n@fragment fn f(@builtin(position) p:vec4f)->@location(0) vec4f{let n=vec2i(textureDimensions(src))-1;let i=vec2i(p.xy)*2;\nreturn (textureLoad(src,min(i,n),0)+textureLoad(src,min(i+vec2i(1,0),n),0)+textureLoad(src,min(i+vec2i(0,1),n),0)+textureLoad(src,min(i+vec2i(1,1),n),0))*0.25;}", S = class {
|
|
134
|
+
constructor(e) {
|
|
135
|
+
this.device = e, this.pipelines = /* @__PURE__ */ new Map(), this.groups = /* @__PURE__ */ new WeakMap();
|
|
136
|
+
}
|
|
137
|
+
make(e, t) {
|
|
138
|
+
let n = this.device, r = t.format;
|
|
139
|
+
this.layout ??= n.createBindGroupLayout({ entries: [{
|
|
140
|
+
binding: 0,
|
|
141
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
142
|
+
texture: { sampleType: "unfilterable-float" }
|
|
143
|
+
}] });
|
|
144
|
+
let i = this.pipelines.get(r);
|
|
145
|
+
if (!i) {
|
|
146
|
+
let e = n.createShaderModule({
|
|
147
|
+
label: "shader:shadertoy:mips",
|
|
148
|
+
code: x
|
|
149
|
+
});
|
|
150
|
+
i = n.createRenderPipeline({
|
|
151
|
+
label: "shader:shadertoy:mips",
|
|
152
|
+
layout: n.createPipelineLayout({ bindGroupLayouts: [this.layout] }),
|
|
153
|
+
vertex: {
|
|
154
|
+
module: e,
|
|
155
|
+
entryPoint: "v"
|
|
156
|
+
},
|
|
157
|
+
fragment: {
|
|
158
|
+
module: e,
|
|
159
|
+
entryPoint: "f",
|
|
160
|
+
targets: [{ format: r }]
|
|
161
|
+
}
|
|
162
|
+
}), this.pipelines.set(r, i);
|
|
163
|
+
}
|
|
164
|
+
let a = this.groups.get(t);
|
|
165
|
+
if (!a) {
|
|
166
|
+
a = [];
|
|
167
|
+
for (let e = 1; e < t.mipLevelCount; e++) a.push({
|
|
168
|
+
group: n.createBindGroup({
|
|
169
|
+
layout: this.layout,
|
|
170
|
+
entries: [{
|
|
171
|
+
binding: 0,
|
|
172
|
+
resource: t.createView({
|
|
173
|
+
baseMipLevel: e - 1,
|
|
174
|
+
mipLevelCount: 1
|
|
175
|
+
})
|
|
176
|
+
}]
|
|
177
|
+
}),
|
|
178
|
+
view: t.createView({
|
|
179
|
+
baseMipLevel: e,
|
|
180
|
+
mipLevelCount: 1
|
|
181
|
+
})
|
|
182
|
+
});
|
|
183
|
+
this.groups.set(t, a);
|
|
184
|
+
}
|
|
185
|
+
for (let t of a) {
|
|
186
|
+
let n = e.beginRenderPass({ colorAttachments: [{
|
|
187
|
+
view: t.view,
|
|
188
|
+
loadOp: "clear",
|
|
189
|
+
storeOp: "store"
|
|
190
|
+
}] });
|
|
191
|
+
n.setPipeline(i), n.setBindGroup(0, t.group), n.draw(3), n.end();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}, C = /* @__PURE__ */ new WeakMap();
|
|
195
|
+
function w(e, t) {
|
|
196
|
+
if (typeof e.createView != "function") return e;
|
|
197
|
+
let n = C.get(e);
|
|
198
|
+
return n || C.set(e, n = {}), n[t] ??= e.createView({ dimension: t });
|
|
199
|
+
}
|
|
200
|
+
function T(e, t) {
|
|
201
|
+
let n = e;
|
|
202
|
+
return !n || typeof n != "object" || typeof n.createView != "function" || typeof n.dimension != "string" ? !0 : n.dimension === "3d" == (t === "3d") ? t !== "cube" || typeof n.depthOrArrayLayers != "number" || n.depthOrArrayLayers === 6 : !1;
|
|
203
|
+
}
|
|
204
|
+
var E = (e, t) => `Shadertoy ${e} is a ${t === "2d" ? "sampler2D" : g[t]} channel; give it ${t === "3d" ? "a 3D texture" : t === "cube" ? "a 2D texture of six layers" : "a 2D texture"}`, D = (e) => !!e && typeof e == "object" && typeof e.bind == "function", O = (e) => h.includes(e);
|
|
205
|
+
function k(e) {
|
|
206
|
+
if (e === "input") return "input";
|
|
207
|
+
if (e === "keyboard") return "keyboard";
|
|
208
|
+
if (e === "cubeA") return "cubemap";
|
|
209
|
+
if (O(e)) return "buffer";
|
|
210
|
+
if (e && typeof e == "object") {
|
|
211
|
+
if ("getByteFrequencyData" in e) return "audio";
|
|
212
|
+
if (!("createView" in e) && !("bind" in e) && ("videoWidth" in e || "width" in e)) return "media";
|
|
213
|
+
}
|
|
214
|
+
return "texture";
|
|
215
|
+
}
|
|
216
|
+
function A(e, n, r, i) {
|
|
217
|
+
let o = a(e) && "texture" in e ? e : { texture: e ?? null }, s = o.texture, c = k(s), l = c === "texture" && s && typeof s == "object" && "createView" in s ? s : void 0, u = c === "cubemap" ? "cube" : "2d", d = o.type ?? (c === "cubemap" ? "cube" : l?.dimension === "3d" ? "3d" : "2d");
|
|
218
|
+
if (!(d in g)) throw TypeError(`Shadertoy ${n} type must be '2d', 'cube' or '3d'`);
|
|
219
|
+
if (c === "texture" && !T(s, d)) throw TypeError(E(n, d));
|
|
220
|
+
if (c !== "texture" && d !== u) throw TypeError(`Shadertoy ${n}: ${c === "cubemap" ? "Cube A" : c === "buffer" ? "a buffer" : c === "input" ? "the stack's colour" : "this channel"} is a ${u} channel; leave type out or use '${u}'`);
|
|
221
|
+
if (c === "buffer" && !i.includes(s)) throw TypeError(`Shadertoy ${n} reads Buffer ${s}, which this toy does not have; add buffer${s}`);
|
|
222
|
+
if (c === "cubemap" && !i.includes("cubeA")) throw TypeError(`Shadertoy ${n} reads Cube A, which this toy does not have; add cubeA`);
|
|
223
|
+
let f = o.filter ?? "linear", p = o.wrap ?? "clamp", m = p === "repeat" ? "repeat" : "clamp-to-edge", h = f === "nearest" ? "nearest" : "linear";
|
|
224
|
+
if (f !== "nearest" && f !== "linear" && f !== "mipmap") throw TypeError(`Shadertoy ${n} filter must be 'nearest', 'linear' or 'mipmap'`);
|
|
225
|
+
if (p !== "clamp" && p !== "repeat") throw TypeError(`Shadertoy ${n} wrap must be 'clamp' or 'repeat'`);
|
|
226
|
+
let v = o.resolution ?? _[c], y = {
|
|
227
|
+
magFilter: h,
|
|
228
|
+
minFilter: h,
|
|
229
|
+
addressModeU: m,
|
|
230
|
+
addressModeV: m,
|
|
231
|
+
addressModeW: m
|
|
232
|
+
};
|
|
233
|
+
f === "mipmap" ? y.mipmapFilter = "linear" : y.lodMaxClamp = 0;
|
|
234
|
+
let b = c === "texture" ? t(s, `Shadertoy ${n}`, r) : void 0;
|
|
235
|
+
return {
|
|
236
|
+
kind: c,
|
|
237
|
+
texture: s,
|
|
238
|
+
type: d,
|
|
239
|
+
filter: f,
|
|
240
|
+
wrap: p,
|
|
241
|
+
sampler: y,
|
|
242
|
+
resolution: v,
|
|
243
|
+
...b && { slot: b }
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
var j = 3, M = {
|
|
247
|
+
screen: "mainImage(shadertoyOut,gl_FragCoord.xy)",
|
|
248
|
+
mesh: "mainImage(shadertoyOut,shadertoyUv*iResolution.xy)",
|
|
249
|
+
vrscreen: "vec4 shadertoyNear=shadertoyEye*vec4(gl_FragCoord.xy/iResolution.xy*2.0-1.0,1.0,1.0);mainVR(shadertoyOut,gl_FragCoord.xy,shadertoyEyePosition,normalize(shadertoyNear.xyz/shadertoyNear.w-shadertoyEyePosition))",
|
|
250
|
+
vrmesh: "mainVR(shadertoyOut,shadertoyUv*iResolution.xy,shadertoyRayOri,normalize(shadertoyRay))",
|
|
251
|
+
cube: "float shadertoyS=iResolution.x;vec2 shadertoyA=gl_FragCoord.xy,shadertoyP=mod(shadertoyA,shadertoyS),shadertoyT=shadertoyP/shadertoyS*2.0-1.0;int shadertoyF=int(shadertoyA.x/shadertoyS)+3*int(shadertoyA.y/shadertoyS);vec3 shadertoyD=vec3(-shadertoyT.x,-shadertoyT.y,-1.0);if(shadertoyF==0)shadertoyD=vec3(1.0,-shadertoyT.y,-shadertoyT.x);else if(shadertoyF==1)shadertoyD=vec3(-1.0,-shadertoyT.y,shadertoyT.x);else if(shadertoyF==2)shadertoyD=vec3(shadertoyT.x,1.0,shadertoyT.y);else if(shadertoyF==3)shadertoyD=vec3(shadertoyT.x,-1.0,-shadertoyT.y);else if(shadertoyF==4)shadertoyD=vec3(shadertoyT.x,-shadertoyT.y,1.0);mainCubemap(shadertoyOut,shadertoyP,vec3(0.0),normalize(shadertoyD))",
|
|
252
|
+
sound: "int shadertoyN=shadertoySoundBase+int(gl_FragCoord.y)*512+int(gl_FragCoord.x);shadertoyOut=vec4(clamp(mainSound("
|
|
253
|
+
}, N = (e) => /\bmainSound\s*\(\s*(?:(?:in|const|highp|mediump|lowp)\s+)*int\b/.test(e);
|
|
254
|
+
function P(e, t, n, r, i = !1) {
|
|
255
|
+
let a = t.map((e, t) => `uniform ${g[e.type]} iChannel${t};`).join(""), o = n === "sound" ? `${M.sound}${N(e) ? "shadertoyN," : ""}float(shadertoyN)/iSampleRate),-1.0,1.0),0.0,1.0)` : n === "cube" ? M.cube : i && n !== "buffer" ? M[`vr${n}`] : M[n === "mesh" ? "mesh" : "screen"];
|
|
256
|
+
return "#version 300 es\nprecision highp float;precision highp int;precision highp sampler3D;uniform vec3 iResolution;uniform float iTime;uniform float iTimeDelta;uniform float iFrameRate;uniform int iFrame;uniform float iChannelTime[4];uniform vec3 iChannelResolution[4];uniform vec4 iMouse;uniform vec4 iDate;uniform float iSampleRate;" + a + (n === "sound" ? "uniform int shadertoySoundBase;" : "") + (i && n === "screen" ? "uniform mat4 shadertoyEye;uniform vec3 shadertoyEyePosition;" : "") + (i && n === "mesh" ? "in vec3 shadertoyRayOri;in vec3 shadertoyRay;" : "") + (n === "mesh" ? "in vec2 shadertoyUv;" : "") + "out vec4 shadertoyColor;\n#define iGlobalTime iTime\n" + (r ? r + "\n" : "") + e + `\nvoid main(){vec4 shadertoyOut=vec4(0.0,0.0,0.0,1.0);${o};shadertoyColor=${n === "screen" || n === "mesh" ? "vec4(shadertoyOut.rgb,1.0)" : "shadertoyOut"};}\n`;
|
|
257
|
+
}
|
|
258
|
+
var F = (e) => `#version 300 es\nin vec3 position;in vec2 uv;out vec2 shadertoyUv;${e ? "out vec3 shadertoyRayOri;out vec3 shadertoyRay;" : ""}\nvoid main(){shadertoyUv=uv;${e ? "shadertoyRayOri=cameraPosition;shadertoyRay=(modelMatrix*vec4(position,1.0)).xyz-cameraPosition;" : ""}gl_Position=projectionMatrix*modelViewMatrix*vec4(position,1.0);}\n`, I = (e) => Object.fromEntries(e.flatMap((e, t) => e.kind === "buffer" ? [[m[t], {
|
|
259
|
+
filter: e.filter,
|
|
260
|
+
wrap: e.wrap
|
|
261
|
+
}]] : [])), L = (e) => ({
|
|
262
|
+
get value() {
|
|
263
|
+
return e();
|
|
264
|
+
},
|
|
265
|
+
set value(e) {
|
|
266
|
+
throw TypeError("Shadertoy inputs are read only here; set toy.time, toy.frame, toy.mouse or toy.date instead");
|
|
267
|
+
}
|
|
268
|
+
}), R = () => ({
|
|
269
|
+
view: void 0,
|
|
270
|
+
format: "rgba32float",
|
|
271
|
+
multisample: { count: 1 },
|
|
272
|
+
hdr: !1
|
|
273
|
+
}), z = class extends s {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
274
|
+
constructor(e, t, n, r, i, a, o, s = !1, c) {
|
|
275
|
+
super({
|
|
276
|
+
fragmentShader: t,
|
|
277
|
+
uniforms: i
|
|
278
|
+
}, a), this.toy = e, this.size = n, this.blanks = r, this.chain = o, this.buffer = s, this.rays = c;
|
|
279
|
+
}
|
|
280
|
+
get time() {
|
|
281
|
+
return this.toy.time;
|
|
282
|
+
}
|
|
283
|
+
advanceTime(e) {
|
|
284
|
+
this.toy.tick(e, this);
|
|
285
|
+
}
|
|
286
|
+
onPreUpdate(e) {
|
|
287
|
+
this.advanceTime(e);
|
|
288
|
+
}
|
|
289
|
+
encoding(e) {
|
|
290
|
+
return this.buffer ? 1 : super.encoding(e);
|
|
291
|
+
}
|
|
292
|
+
render(e, t, n) {
|
|
293
|
+
this.buffer !== "fixed" && (this.size[0] = e.width, this.size[1] = e.height), this.rays && (y(e.camera.matrix, this.rays.eye), this.rays.position = e.camera.position), this.blanks.device = e.device, this.chain && (this.toy.refresh(e.device, e.encoder), this.chain.run(e, t)), super.render(e, t, n);
|
|
294
|
+
}
|
|
295
|
+
setup(e) {
|
|
296
|
+
super.setup(e), this.toy.adopt(e);
|
|
297
|
+
}
|
|
298
|
+
onDeviceRestored() {
|
|
299
|
+
this.blanks.destroy(), this.chain?.release(), super.onDeviceRestored();
|
|
300
|
+
}
|
|
301
|
+
dispose() {
|
|
302
|
+
this.blanks.destroy(), this.chain?.dispose(), super.dispose();
|
|
303
|
+
}
|
|
304
|
+
drawInto(e, t, n) {
|
|
305
|
+
this.render(e, t, n);
|
|
306
|
+
}
|
|
307
|
+
}, B = class {
|
|
308
|
+
constructor(e, t) {
|
|
309
|
+
this.toy = e, this.fixed = t, this.size = [
|
|
310
|
+
0,
|
|
311
|
+
0,
|
|
312
|
+
1
|
|
313
|
+
], this.passes = [], this.held = /* @__PURE__ */ new Map(), this.out = R(), this.half = {
|
|
314
|
+
view: void 0,
|
|
315
|
+
format: "rgba16float",
|
|
316
|
+
multisample: { count: 1 },
|
|
317
|
+
hdr: !1
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
view(e) {
|
|
321
|
+
return { bind: () => {
|
|
322
|
+
let t = this.held.get(e);
|
|
323
|
+
return t && [t.views[t.front], void 0];
|
|
324
|
+
} };
|
|
325
|
+
}
|
|
326
|
+
run(e, t) {
|
|
327
|
+
let [n, r] = this.fixed ?? [e.width, e.height];
|
|
328
|
+
if (this.device !== e.device || this.size[0] !== n || this.size[1] !== r) {
|
|
329
|
+
this.release(), this.device = e.device, this.size[0] = n, this.size[1] = r;
|
|
330
|
+
for (let { letter: t, mips: n } of this.passes) this.held.set(t, this.make(e.device, t, n));
|
|
331
|
+
this.cube && this.makeCube(e.device, this.cube);
|
|
332
|
+
}
|
|
333
|
+
let i = this.toy.step();
|
|
334
|
+
if (i === this.step) return;
|
|
335
|
+
this.step = i, this.frame?.base !== e && (this.frame = {
|
|
336
|
+
base: e,
|
|
337
|
+
context: Object.create(e, {
|
|
338
|
+
width: { value: n },
|
|
339
|
+
height: { value: r },
|
|
340
|
+
view: { value: void 0 }
|
|
341
|
+
})
|
|
342
|
+
});
|
|
343
|
+
for (let { letter: n, pass: r, mips: i } of this.passes) {
|
|
344
|
+
let a = this.held.get(n);
|
|
345
|
+
this.out.view = a.targets[1 - a.front], r.drawInto(this.frame.context, t, this.out), a.front = 1 - a.front, i && (this.mips ??= new S(e.device)).make(e.encoder, a.textures[a.front]);
|
|
346
|
+
}
|
|
347
|
+
let a = this.cube;
|
|
348
|
+
if (a) {
|
|
349
|
+
let n = a.size;
|
|
350
|
+
this.half.view = a.target, a.pass.drawInto(Object.create(e, {
|
|
351
|
+
width: { value: 3 * n },
|
|
352
|
+
height: { value: 2 * n },
|
|
353
|
+
view: { value: void 0 }
|
|
354
|
+
}), t, this.half), a.mips && (this.mips ??= new S(e.device)).make(e.encoder, a.atlas);
|
|
355
|
+
for (let t = 0; t < a.texture.mipLevelCount; t++) for (let r = 0, i = n >> t; r < 6; r++) e.encoder.copyTextureToTexture({
|
|
356
|
+
texture: a.atlas,
|
|
357
|
+
mipLevel: t,
|
|
358
|
+
origin: [
|
|
359
|
+
r % 3 * i,
|
|
360
|
+
Math.floor(r / 3) * i,
|
|
361
|
+
0
|
|
362
|
+
]
|
|
363
|
+
}, {
|
|
364
|
+
texture: a.texture,
|
|
365
|
+
mipLevel: t,
|
|
366
|
+
origin: [
|
|
367
|
+
0,
|
|
368
|
+
0,
|
|
369
|
+
r
|
|
370
|
+
]
|
|
371
|
+
}, [
|
|
372
|
+
i,
|
|
373
|
+
i,
|
|
374
|
+
1
|
|
375
|
+
]);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
makeCube(e, t) {
|
|
379
|
+
let n = t.size, r = t.mips ? v(n, n) : 1;
|
|
380
|
+
t.atlas = e.createTexture({
|
|
381
|
+
label: "shader:shadertoy:cubeA:atlas",
|
|
382
|
+
size: [3 * n, 2 * n],
|
|
383
|
+
mipLevelCount: r,
|
|
384
|
+
format: "rgba16float",
|
|
385
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC
|
|
386
|
+
}), t.target = t.atlas.createView({
|
|
387
|
+
baseMipLevel: 0,
|
|
388
|
+
mipLevelCount: 1
|
|
389
|
+
}), t.texture = e.createTexture({
|
|
390
|
+
label: "shader:shadertoy:cubeA",
|
|
391
|
+
size: [
|
|
392
|
+
n,
|
|
393
|
+
n,
|
|
394
|
+
6
|
|
395
|
+
],
|
|
396
|
+
mipLevelCount: r,
|
|
397
|
+
format: "rgba16float",
|
|
398
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
|
|
399
|
+
}), t.view = t.texture.createView({ dimension: "cube" });
|
|
400
|
+
}
|
|
401
|
+
make(e, t, n) {
|
|
402
|
+
let [r, i] = this.size, a = [0, 1].map(() => e.createTexture({
|
|
403
|
+
label: `shader:shadertoy:buffer${t}`,
|
|
404
|
+
size: [r, i],
|
|
405
|
+
mipLevelCount: n ? v(r, i) : 1,
|
|
406
|
+
format: "rgba32float",
|
|
407
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC
|
|
408
|
+
}));
|
|
409
|
+
return {
|
|
410
|
+
textures: a,
|
|
411
|
+
views: a.map((e) => e.createView()),
|
|
412
|
+
targets: a.map((e) => e.createView({
|
|
413
|
+
baseMipLevel: 0,
|
|
414
|
+
mipLevelCount: 1
|
|
415
|
+
})),
|
|
416
|
+
front: 0
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
release() {
|
|
420
|
+
for (let e of this.held.values()) for (let t of e.textures) t.destroy();
|
|
421
|
+
this.held.clear();
|
|
422
|
+
let e = this.cube;
|
|
423
|
+
e && (e.atlas?.destroy(), e.texture?.destroy(), e.atlas = e.target = e.texture = e.view = void 0), this.device = this.mips = this.step = void 0;
|
|
424
|
+
}
|
|
425
|
+
ready() {
|
|
426
|
+
return Promise.all([...this.passes.map((e) => e.pass), this.cube?.pass].map((e) => e?.ready()));
|
|
427
|
+
}
|
|
428
|
+
dispose() {
|
|
429
|
+
this.release();
|
|
430
|
+
for (let e of this.passes) e.pass.dispose();
|
|
431
|
+
this.cube?.pass.dispose();
|
|
432
|
+
}
|
|
433
|
+
}, V = class extends o {
|
|
434
|
+
constructor(e, t) {
|
|
435
|
+
super(t), this.blanks = e;
|
|
436
|
+
}
|
|
437
|
+
dispose() {
|
|
438
|
+
this.blanks.destroy(), super.dispose();
|
|
439
|
+
}
|
|
440
|
+
}, H = 0, U = class t {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
441
|
+
static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
442
|
+
let r = new t(n);
|
|
443
|
+
return e.use(r), r;
|
|
444
|
+
}
|
|
445
|
+
constructor(e) {
|
|
446
|
+
this.time = 0, this.frame = 0, this.timeDelta = 0, this.frameRate = 60, this.mouse = [
|
|
447
|
+
0,
|
|
448
|
+
0,
|
|
449
|
+
0,
|
|
450
|
+
0
|
|
451
|
+
], this.jobs = /* @__PURE__ */ new Set(), this.today = [
|
|
452
|
+
0,
|
|
453
|
+
0,
|
|
454
|
+
0,
|
|
455
|
+
0
|
|
456
|
+
], this.paused = !1, this.started = !1, this.ticks = 0, this.drawn = -1, this.disposed = !1, this.materials = 0, this.targets = /* @__PURE__ */ new Map(), this.output = {
|
|
457
|
+
view: void 0,
|
|
458
|
+
format: "rgba8unorm",
|
|
459
|
+
multisample: { count: 1 },
|
|
460
|
+
hdr: !1
|
|
461
|
+
}, this.made = [], this.feeds = /* @__PURE__ */ new Map(), this.keys = {
|
|
462
|
+
down: /* @__PURE__ */ new Uint8Array(256),
|
|
463
|
+
pressed: /* @__PURE__ */ new Uint8Array(256),
|
|
464
|
+
toggled: /* @__PURE__ */ new Uint8Array(256)
|
|
465
|
+
}, this.onKey = (e) => {
|
|
466
|
+
let t = e.keyCode;
|
|
467
|
+
t >= 0 && t < 256 && (e.type === "keyup" ? this.keys.down[t] = 0 : this.keys.down[t] || (this.keys.down[t] = this.keys.pressed[t] = 255, this.keys.toggled[t] ^= 255));
|
|
468
|
+
}, this.view = { bind: (e) => {
|
|
469
|
+
let t = this.target(e);
|
|
470
|
+
return [t.view, t.sampler];
|
|
471
|
+
} };
|
|
472
|
+
let t = typeof e == "string" ? { image: { source: e } } : e, n = t?.image?.source;
|
|
473
|
+
if (typeof n != "string" || !/\bmainImage\b/.test(n)) throw TypeError("Shadertoy needs the Image source with void mainImage(out vec4 fragColor, in vec2 fragCoord)");
|
|
474
|
+
let [r, i] = t.resolution ?? [512, 512];
|
|
475
|
+
if (!Number.isInteger(r) || !Number.isInteger(i) || r < 1 || i < 1 || r > 8192 || i > 8192) throw RangeError("Shadertoy resolution must be two integers from 1 to 8192");
|
|
476
|
+
if (t.common !== void 0 && typeof t.common != "string") throw TypeError("Shadertoy common must be GLSL text");
|
|
477
|
+
this.source = n, this.common = t.common ?? "", this.resolution = [r, i], this.sampleRate = t.sampleRate ?? 44100, this.name = t.name ?? "shader:shadertoy:" + H++;
|
|
478
|
+
let a = h.filter((e) => t[`buffer${e}`] !== void 0), o = [...a];
|
|
479
|
+
for (let e of a) if (typeof t[`buffer${e}`]?.source != "string" || !/\bmainImage\b/.test(t[`buffer${e}`].source)) throw TypeError(`Shadertoy buffer${e} needs a source with void mainImage(out vec4 fragColor, in vec2 fragCoord)`);
|
|
480
|
+
let s = t.cubeA, c = this.cubeSize = s?.size ?? 1024;
|
|
481
|
+
if (s !== void 0) {
|
|
482
|
+
if (typeof s?.source != "string" || !/\bmainCubemap\b/.test(s.source)) throw TypeError("Shadertoy cubeA needs a source with void mainCubemap(out vec4 fragColor, in vec2 fragCoord, in vec3 rayOri, in vec3 rayDir)");
|
|
483
|
+
if (!Number.isInteger(c) || c < 1 || c > 2048 || c & c - 1) throw RangeError("Shadertoy cubeA size must be a power of two from 1 to 2048");
|
|
484
|
+
o.push("cubeA");
|
|
485
|
+
}
|
|
486
|
+
if (t.sound !== void 0 && (typeof t.sound?.source != "string" || !/\bmainSound\b/.test(t.sound.source))) throw TypeError("Shadertoy sound needs a source with vec2 mainSound(int samp, float time)");
|
|
487
|
+
if (this.vr = t.vr === !0, this.vr && !/\bmainVR\b/.test(n)) throw TypeError("Shadertoy vr: true needs void mainVR(out vec4 fragColor, in vec2 fragCoord, in vec3 fragRayOri, in vec3 fragRayDir) in the Image source");
|
|
488
|
+
let l = (e, t) => ({
|
|
489
|
+
tab: e,
|
|
490
|
+
source: t.source,
|
|
491
|
+
channels: m.map((n, r) => A(t[n], `${e === "image" ? "" : e + " "}${n}`, `toy.setChannel(${r}, texture${e === "image" ? "" : `, '${e}'`})`, o))
|
|
492
|
+
});
|
|
493
|
+
this.tabs = [
|
|
494
|
+
...h.filter((e) => o.includes(e)).map((e) => l(`buffer${e}`, t[`buffer${e}`])),
|
|
495
|
+
...s ? [l("cubeA", s)] : [],
|
|
496
|
+
l("image", t.image)
|
|
497
|
+
];
|
|
498
|
+
let u = this.tabs.at(-1).channels;
|
|
499
|
+
if (this.fragmentShader = P(n, u, "screen", this.common), this.vr && (this.vrShader = P(n, u, "screen", this.common, !0)), this.meshShaders = {
|
|
500
|
+
vertexShader: F(this.vr),
|
|
501
|
+
fragmentShader: P(n, u, "mesh", this.common, this.vr)
|
|
502
|
+
}, this.buffers = Object.fromEntries(this.tabs.filter((e) => e.tab.startsWith("buffer")).map((e) => [e.tab.slice(6), P(e.source, e.channels, "buffer", this.common)])), s && (this.cubeShader = P(s.source, this.tabs.at(-2).channels, "cube", this.common)), t.sound) {
|
|
503
|
+
let e = this.soundTab = l("sound", t.sound), n = e.channels.findIndex((e) => e.kind !== "texture");
|
|
504
|
+
if (n >= 0) throw TypeError(`Shadertoy sound iChannel${n} reads textures only; give it a texture or null`);
|
|
505
|
+
this.soundShader = P(e.source, e.channels, "sound", this.common);
|
|
506
|
+
}
|
|
507
|
+
this.tabs.some((e) => e.channels.some((e) => e.kind === "keyboard")) && (this.keyTarget = t.keyboard ?? globalThis, this.keyTarget.addEventListener?.("keydown", this.onKey), this.keyTarget.addEventListener?.("keyup", this.onKey));
|
|
508
|
+
}
|
|
509
|
+
get date() {
|
|
510
|
+
if (this.dateSet) return this.dateSet;
|
|
511
|
+
let e = /* @__PURE__ */ new Date();
|
|
512
|
+
return this.today[0] = e.getFullYear(), this.today[1] = e.getMonth(), this.today[2] = e.getDate(), this.today[3] = e.getHours() * 3600 + e.getMinutes() * 60 + e.getSeconds() + e.getMilliseconds() / 1e3, this.today;
|
|
513
|
+
}
|
|
514
|
+
set date(e) {
|
|
515
|
+
if (e !== void 0 && (e.length !== 4 || !Array.from(e).every(Number.isFinite))) throw TypeError("Shadertoy date must be [year, month, day, seconds] or undefined for the clock");
|
|
516
|
+
this.dateSet = e && [...e];
|
|
517
|
+
}
|
|
518
|
+
pause() {
|
|
519
|
+
this.paused = !0;
|
|
520
|
+
}
|
|
521
|
+
play() {
|
|
522
|
+
this.paused = !1;
|
|
523
|
+
}
|
|
524
|
+
reset() {
|
|
525
|
+
this.time = this.frame = this.timeDelta = 0, this.started = !1;
|
|
526
|
+
}
|
|
527
|
+
tick(e, t) {
|
|
528
|
+
(this.ticker ??= t) === t && (this.ticks++, !this.paused && (this.started && (this.time += e, this.frame++), this.started = !0, this.timeDelta = e, e > 0 && (this.frameRate = 1 / e)));
|
|
529
|
+
}
|
|
530
|
+
step() {
|
|
531
|
+
return this.paused ? -1 - this.frame : this.ticks;
|
|
532
|
+
}
|
|
533
|
+
refresh(e, t) {
|
|
534
|
+
let n = this.step();
|
|
535
|
+
if (this.refreshed !== n || this.feedDevice !== e) {
|
|
536
|
+
this.refreshed = n, this.feedDevice !== e && (this.releaseFeeds(), this.feedDevice = e);
|
|
537
|
+
for (let n of this.tabs) for (let r of n.channels) {
|
|
538
|
+
if (r.kind !== "keyboard" && r.kind !== "media" && r.kind !== "audio") continue;
|
|
539
|
+
let n = this.feeds.get(r);
|
|
540
|
+
n || this.feeds.set(r, n = {}), this.fill(e, r, n) && r.filter === "mipmap" && (this.mips ??= new S(e)).make(t, n.texture);
|
|
541
|
+
}
|
|
542
|
+
this.keys.pressed.fill(0);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
fill(e, t, n) {
|
|
546
|
+
let [r, i] = _[t.kind] ?? [0, 0];
|
|
547
|
+
if (t.kind === "media") {
|
|
548
|
+
let e = t.texture;
|
|
549
|
+
if ("readyState" in e && e.readyState < 2 || (r = e.videoWidth ?? e.width, i = e.videoHeight ?? e.height, !r || !i) || n.copied === e && !("readyState" in e) && !("getContext" in e)) return !1;
|
|
550
|
+
}
|
|
551
|
+
if ((!n.texture || n.texture.width !== r || n.texture.height !== i) && (n.texture?.destroy(), n.texture = e.createTexture({
|
|
552
|
+
label: "shader:shadertoy:" + t.kind,
|
|
553
|
+
size: [r, i],
|
|
554
|
+
mipLevelCount: t.filter === "mipmap" ? v(r, i) : 1,
|
|
555
|
+
format: "rgba8unorm",
|
|
556
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT
|
|
557
|
+
}), n.view = n.texture.createView()), t.kind === "media") p(e, { source: t.texture }, { texture: n.texture }, [r, i]), n.copied = t.texture;
|
|
558
|
+
else {
|
|
559
|
+
let a = new Uint8Array(r * i * 4);
|
|
560
|
+
(t.kind === "keyboard" ? [
|
|
561
|
+
this.keys.toggled,
|
|
562
|
+
this.keys.pressed,
|
|
563
|
+
this.keys.down
|
|
564
|
+
] : (() => {
|
|
565
|
+
let e = t.texture, n = /* @__PURE__ */ new Uint8Array(512), r = /* @__PURE__ */ new Uint8Array(512);
|
|
566
|
+
return e.getByteTimeDomainData(n), e.getByteFrequencyData(r), [n, r];
|
|
567
|
+
})()).forEach((e, t) => e.forEach((e, n) => a.set([
|
|
568
|
+
e,
|
|
569
|
+
0,
|
|
570
|
+
0,
|
|
571
|
+
255
|
|
572
|
+
], (t * r + n) * 4))), e.queue.writeTexture({ texture: n.texture }, a, {
|
|
573
|
+
bytesPerRow: r * 4,
|
|
574
|
+
rowsPerImage: i
|
|
575
|
+
}, [r, i]);
|
|
576
|
+
}
|
|
577
|
+
return !0;
|
|
578
|
+
}
|
|
579
|
+
releaseFeeds() {
|
|
580
|
+
for (let e of this.feeds.values()) e.texture?.destroy();
|
|
581
|
+
this.feeds.clear(), this.mips = this.feedDevice = void 0;
|
|
582
|
+
}
|
|
583
|
+
uniforms(e, t, n, r) {
|
|
584
|
+
let i = Array(12).fill(0), a = [
|
|
585
|
+
0,
|
|
586
|
+
0,
|
|
587
|
+
0,
|
|
588
|
+
0
|
|
589
|
+
], o = {
|
|
590
|
+
iResolution: L(() => e),
|
|
591
|
+
iTime: L(() => this.time),
|
|
592
|
+
iTimeDelta: L(() => this.timeDelta),
|
|
593
|
+
iFrameRate: L(() => this.frameRate),
|
|
594
|
+
iFrame: L(() => this.frame),
|
|
595
|
+
iChannelTime: L(() => (n.forEach((e, t) => {
|
|
596
|
+
let n = e.texture;
|
|
597
|
+
a[t] = (e.kind === "media" ? n.currentTime : e.kind === "audio" ? n.context?.currentTime : 0) ?? 0;
|
|
598
|
+
}), a)),
|
|
599
|
+
iChannelResolution: L(() => (n.forEach((n, a) => {
|
|
600
|
+
let o = n.texture, s = n.kind === "input" || n.kind === "buffer" ? r?.size ?? e : n.kind === "cubemap" ? [
|
|
601
|
+
this.cubeSize,
|
|
602
|
+
this.cubeSize,
|
|
603
|
+
1
|
|
604
|
+
] : n.kind === "media" && !n.resolution ? [o.videoWidth ?? o.width, o.videoHeight ?? o.height] : n.kind === "texture" && !n.resolution ? this.sizeOf(n, t.device) : n.resolution ?? [
|
|
605
|
+
0,
|
|
606
|
+
0,
|
|
607
|
+
0
|
|
608
|
+
];
|
|
609
|
+
i[a * 3] = s[0] ?? 0, i[a * 3 + 1] = s[1] ?? 0, i[a * 3 + 2] = s[2] ?? 1;
|
|
610
|
+
}), i)),
|
|
611
|
+
iMouse: L(() => this.mouse),
|
|
612
|
+
iDate: L(() => this.date),
|
|
613
|
+
iSampleRate: L(() => this.sampleRate)
|
|
614
|
+
};
|
|
615
|
+
return n.forEach((e, n) => {
|
|
616
|
+
if (e.kind === "input") return;
|
|
617
|
+
let i = { bind: (n, r) => {
|
|
618
|
+
let i = this.feeds.get(e)?.view;
|
|
619
|
+
return i ? [i, void 0] : t.flat.bind(n, r);
|
|
620
|
+
} }, a = () => {
|
|
621
|
+
let n = t.device, r = e.slot.value, i = n ? this.lent(e, n) : typeof r == "function" ? void 0 : r;
|
|
622
|
+
return i ? D(i) ? i : w(i, e.type) : n ? t.view(n, e.type) : t.of(e.type);
|
|
623
|
+
}, s = e.kind === "buffer" ? r.view(e.texture) : i, c = {
|
|
624
|
+
get texture() {
|
|
625
|
+
return (e.kind === "cubemap" ? r.cube?.view : e.kind === "texture" ? a() : s) ?? t.of(e.type);
|
|
626
|
+
},
|
|
627
|
+
sampler: e.sampler
|
|
628
|
+
};
|
|
629
|
+
o[m[n]] = L(() => c);
|
|
630
|
+
}), o;
|
|
631
|
+
}
|
|
632
|
+
lent(t, r) {
|
|
633
|
+
let i = t.slot, a = i.value;
|
|
634
|
+
if (D(a)) return a;
|
|
635
|
+
let o = n(i, r, this.engine ?? this.host);
|
|
636
|
+
if (o && !T(o, t.type)) t.misfit !== o && (t.misfit = o, (this.engine ?? this.host)?.onError(e("failed", E(i.name.slice(10), t.type))));
|
|
637
|
+
else return o;
|
|
638
|
+
}
|
|
639
|
+
sizeOf(e, t) {
|
|
640
|
+
let n = e.slot.value, r = t ? this.lent(e, t) : typeof n == "function" ? void 0 : n;
|
|
641
|
+
return typeof r?.width == "number" && typeof r.height == "number" ? [
|
|
642
|
+
r.width,
|
|
643
|
+
r.height,
|
|
644
|
+
e.type === "3d" ? r.depthOrArrayLayers ?? 1 : 1
|
|
645
|
+
] : [
|
|
646
|
+
0,
|
|
647
|
+
0,
|
|
648
|
+
0
|
|
649
|
+
];
|
|
650
|
+
}
|
|
651
|
+
adopt(e) {
|
|
652
|
+
this.host = e, this.fixSlots(e.gpu);
|
|
653
|
+
}
|
|
654
|
+
fixSlots(e) {
|
|
655
|
+
if (e) for (let t of [...this.tabs, ...this.soundTab ? [this.soundTab] : []]) for (let n of t.channels) n.slot && i(n.slot, e);
|
|
656
|
+
}
|
|
657
|
+
setChannel(e, t, n = "image") {
|
|
658
|
+
if (this.disposed) throw new c("disposed", `${this.name} is disposed; create a new Shadertoy`);
|
|
659
|
+
let i = n === "sound" ? this.soundTab : this.tabs.find((e) => e.tab === n), a = i?.channels[e];
|
|
660
|
+
if (!i) throw TypeError(`Shadertoy has no ${n} tab; give new Shadertoy({${n}: ...}) one`);
|
|
661
|
+
if (!a) throw RangeError("Shadertoy setChannel index must be 0, 1, 2 or 3");
|
|
662
|
+
let o = `${n === "image" ? "" : n + " "}iChannel${e}`, s = k(t ?? null);
|
|
663
|
+
if (a.kind === "texture" && s === "texture") {
|
|
664
|
+
if (!T(t, a.type)) throw TypeError(E(o, a.type));
|
|
665
|
+
r(a.slot, t, (this.engine ?? this.host)?.gpu), a.texture = t ?? null, a.misfit = void 0;
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
if ((a.kind === "media" || a.kind === "audio") && s === a.kind) {
|
|
669
|
+
a.texture = t;
|
|
670
|
+
let e = this.feeds.get(a);
|
|
671
|
+
e && (e.copied = void 0);
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
let l = {
|
|
675
|
+
texture: "a texture",
|
|
676
|
+
media: "a video or image",
|
|
677
|
+
audio: "an AnalyserNode",
|
|
678
|
+
input: "the stack's colour",
|
|
679
|
+
buffer: `Buffer ${String(a.texture)}`,
|
|
680
|
+
cubemap: "Cube A",
|
|
681
|
+
keyboard: "the keyboard"
|
|
682
|
+
}[a.kind];
|
|
683
|
+
throw TypeError(a.kind === "texture" ? `Shadertoy ${o} reads a texture; setChannel gives it a texture, a function or null` : a.kind === "media" || a.kind === "audio" ? `Shadertoy ${o} reads ${l}; setChannel gives it ${l}` : `Shadertoy ${o} reads ${l}, which the toy draws itself; make a new Shadertoy to read something else`);
|
|
684
|
+
}
|
|
685
|
+
refuseInput(e) {
|
|
686
|
+
for (let t of this.tabs) {
|
|
687
|
+
let n = t.channels.findIndex((e) => e.kind === "input");
|
|
688
|
+
if (n >= 0) throw new c("failed", `Shadertoy ${t.tab} iChannel${n} reads the stack's colour, which only toy.pass() has; give ${e} a texture`);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
program(e, t) {
|
|
692
|
+
try {
|
|
693
|
+
return t();
|
|
694
|
+
} catch (t) {
|
|
695
|
+
let n = t, r = this.common ? this.common.split("\n").length : 0;
|
|
696
|
+
throw typeof n?.line == "number" && n.stage === "fragment" && n.line > j && (n.tab = n.line > j + r ? e.tab : "common", n.line -= n.line > j + r ? j + r : j), t;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
chain(e) {
|
|
700
|
+
if (this.tabs.length < 2) return;
|
|
701
|
+
let t = new B(this, e);
|
|
702
|
+
for (let e of this.tabs.slice(0, -1)) {
|
|
703
|
+
let n = e.tab.slice(6), r = new b(), i = e.channels.findIndex((e) => e.kind === "input"), a = this.tabs.some((t) => t.channels.some((t) => t.filter === "mipmap" && (e.tab === "cubeA" ? t.kind === "cubemap" : t.kind === "buffer" && t.texture === n)));
|
|
704
|
+
if (e.tab === "cubeA") {
|
|
705
|
+
let n = [
|
|
706
|
+
this.cubeSize,
|
|
707
|
+
this.cubeSize,
|
|
708
|
+
1
|
|
709
|
+
];
|
|
710
|
+
t.cube = {
|
|
711
|
+
pass: this.program(e, () => new z(this, this.cubeShader, n, r, this.uniforms(n, r, e.channels, t), {
|
|
712
|
+
builtins: !1,
|
|
713
|
+
input: i < 0 ? null : m[i],
|
|
714
|
+
unfilterable: I(e.channels),
|
|
715
|
+
name: `${this.name}:cubeA`,
|
|
716
|
+
clipSpace: "webgpu"
|
|
717
|
+
}, void 0, "fixed")),
|
|
718
|
+
size: this.cubeSize,
|
|
719
|
+
mips: a
|
|
720
|
+
};
|
|
721
|
+
continue;
|
|
722
|
+
}
|
|
723
|
+
let o = this.program(e, () => new z(this, this.buffers[n], t.size, r, this.uniforms(t.size, r, e.channels, t), {
|
|
724
|
+
builtins: !1,
|
|
725
|
+
input: i < 0 ? null : m[i],
|
|
726
|
+
unfilterable: I(e.channels),
|
|
727
|
+
name: `${this.name}:buffer${n}`
|
|
728
|
+
}, void 0, !0));
|
|
729
|
+
t.passes.push({
|
|
730
|
+
letter: n,
|
|
731
|
+
pass: o,
|
|
732
|
+
mips: a
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
return t;
|
|
736
|
+
}
|
|
737
|
+
imageTab() {
|
|
738
|
+
return this.tabs.at(-1);
|
|
739
|
+
}
|
|
740
|
+
pass(e = {}) {
|
|
741
|
+
let t = this.imageTab(), n = t.channels.findIndex((e) => e.kind === "input"), r = [
|
|
742
|
+
0,
|
|
743
|
+
0,
|
|
744
|
+
1
|
|
745
|
+
], i = new b(), a = this.chain(), o = this.vr ? {
|
|
746
|
+
eye: Array(16).fill(0),
|
|
747
|
+
position: [
|
|
748
|
+
0,
|
|
749
|
+
0,
|
|
750
|
+
0
|
|
751
|
+
]
|
|
752
|
+
} : void 0, s = this.program(t, () => new z(this, this.vrShader ?? this.fragmentShader, r, i, {
|
|
753
|
+
...this.uniforms(r, i, t.channels, a),
|
|
754
|
+
...o && {
|
|
755
|
+
shadertoyEye: L(() => o.eye),
|
|
756
|
+
shadertoyEyePosition: L(() => o.position)
|
|
757
|
+
}
|
|
758
|
+
}, {
|
|
759
|
+
...e,
|
|
760
|
+
builtins: !1,
|
|
761
|
+
input: n < 0 ? null : m[n],
|
|
762
|
+
unfilterable: I(t.channels)
|
|
763
|
+
}, a ?? this.feedless(), !1, o));
|
|
764
|
+
return this.made.push(s), s;
|
|
765
|
+
}
|
|
766
|
+
feedless() {
|
|
767
|
+
return this.tabs.some((e) => e.channels.some((e) => e.kind === "keyboard" || e.kind === "media" || e.kind === "audio")) ? new B(this) : void 0;
|
|
768
|
+
}
|
|
769
|
+
material(e = {}) {
|
|
770
|
+
this.refuseInput("toy.material()");
|
|
771
|
+
let t = this.imageTab(), n = new b();
|
|
772
|
+
Object.defineProperty(n, "device", {
|
|
773
|
+
get: () => this.engine?.gpu,
|
|
774
|
+
set() {}
|
|
775
|
+
}), this.own ??= this.chain(this.resolution);
|
|
776
|
+
let r = this.program(t, () => new V(n, {
|
|
777
|
+
...e,
|
|
778
|
+
...this.meshShaders,
|
|
779
|
+
uniforms: this.uniforms([...this.resolution, 1], n, t.channels, this.own),
|
|
780
|
+
builtins: "modelViewMatrix",
|
|
781
|
+
unfilterable: I(t.channels)
|
|
782
|
+
}));
|
|
783
|
+
return this.materials++, this.made.push(r), r;
|
|
784
|
+
}
|
|
785
|
+
get texture() {
|
|
786
|
+
if (this.refuseInput("toy.texture"), !this.image) {
|
|
787
|
+
let e = this.imageTab(), t = new b();
|
|
788
|
+
this.own ??= this.chain(this.resolution), this.image = this.program(e, () => new z(this, this.fragmentShader, [
|
|
789
|
+
0,
|
|
790
|
+
0,
|
|
791
|
+
1
|
|
792
|
+
], t, this.uniforms([...this.resolution, 1], t, e.channels, this.own), {
|
|
793
|
+
builtins: !1,
|
|
794
|
+
input: null,
|
|
795
|
+
unfilterable: I(e.channels),
|
|
796
|
+
name: this.name + ":image"
|
|
797
|
+
}));
|
|
798
|
+
}
|
|
799
|
+
return this.view;
|
|
800
|
+
}
|
|
801
|
+
target(e) {
|
|
802
|
+
let t = this.targets.get(e);
|
|
803
|
+
if (!t) {
|
|
804
|
+
let [n, r] = this.resolution, i = e.createTexture({
|
|
805
|
+
label: this.name + ":texture",
|
|
806
|
+
size: [n, r],
|
|
807
|
+
format: "rgba8unorm",
|
|
808
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC
|
|
809
|
+
});
|
|
810
|
+
this.targets.set(e, t = {
|
|
811
|
+
texture: i,
|
|
812
|
+
view: i.createView(),
|
|
813
|
+
sampler: e.createSampler({
|
|
814
|
+
magFilter: "linear",
|
|
815
|
+
minFilter: "linear"
|
|
816
|
+
})
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
return t;
|
|
820
|
+
}
|
|
821
|
+
setup(e) {
|
|
822
|
+
if (this.disposed) throw new c("disposed", `${this.name} is disposed; create a new Shadertoy`);
|
|
823
|
+
if (!e.gpu) throw new c("unsupported", "Shadertoy requires WebGPU; use a WebGPU engine");
|
|
824
|
+
this.engine = e, this.fixSlots(e.gpu);
|
|
825
|
+
}
|
|
826
|
+
advanceTime(e) {
|
|
827
|
+
this.tick(e, this);
|
|
828
|
+
}
|
|
829
|
+
onPreUpdate(e) {
|
|
830
|
+
this.advanceTime(e);
|
|
831
|
+
}
|
|
832
|
+
registerPasses(e) {
|
|
833
|
+
e.add({
|
|
834
|
+
name: this.name + ":texture",
|
|
835
|
+
order: -50,
|
|
836
|
+
execute: (e) => this.draw(e)
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
async sound(e = {}) {
|
|
840
|
+
let t = this.soundTab, { seconds: n = 180, signal: r } = e;
|
|
841
|
+
if (!t) throw new c("failed", "Shadertoy has no Sound tab; give new Shadertoy({image, sound: {source}}) a source with vec2 mainSound(int samp, float time)");
|
|
842
|
+
if (!(n > 0 && n <= 600)) throw RangeError("Shadertoy sound seconds must be above 0 and at most 600");
|
|
843
|
+
if (!this.engine) throw new c("state", "toy.sound() renders while the toy is attached; call engine.use(toy) first");
|
|
844
|
+
r?.throwIfAborted();
|
|
845
|
+
let { SoundJob: i, EDGE: a } = await import("./sound-CF_dzP41.js"), o = { base: 0 }, s = [
|
|
846
|
+
a,
|
|
847
|
+
a,
|
|
848
|
+
1
|
|
849
|
+
], l = new b(), u = this.program(t, () => new z(this, this.soundShader, s, l, {
|
|
850
|
+
...this.uniforms(s, l, t.channels),
|
|
851
|
+
shadertoySoundBase: L(() => o.base)
|
|
852
|
+
}, {
|
|
853
|
+
builtins: !1,
|
|
854
|
+
input: null,
|
|
855
|
+
name: this.name + ":sound",
|
|
856
|
+
clipSpace: "webgpu"
|
|
857
|
+
}, void 0, "fixed")), d = new i(Math.ceil(n * this.sampleRate), this.sampleRate, (e, t, n) => {
|
|
858
|
+
o.base = t, u.drawInto(e, void 0, n);
|
|
859
|
+
}, r);
|
|
860
|
+
this.jobs.add(d);
|
|
861
|
+
try {
|
|
862
|
+
return await d.done;
|
|
863
|
+
} finally {
|
|
864
|
+
this.jobs.delete(d), u.dispose();
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
draw(e) {
|
|
868
|
+
for (let t of this.jobs) t.frame(e);
|
|
869
|
+
if (!this.image && !this.materials || this.drawn === this.ticks) return;
|
|
870
|
+
this.drawn = this.ticks;
|
|
871
|
+
let [t, n] = this.resolution;
|
|
872
|
+
if (this.frameContext?.base !== e && (this.frameContext = {
|
|
873
|
+
base: e,
|
|
874
|
+
context: Object.create(e, {
|
|
875
|
+
width: { value: t },
|
|
876
|
+
height: { value: n },
|
|
877
|
+
view: { value: void 0 }
|
|
878
|
+
})
|
|
879
|
+
}), this.refresh(e.device, e.encoder), this.own?.run(this.frameContext.context, void 0), !this.image) return;
|
|
880
|
+
let r = this.target(e.device);
|
|
881
|
+
this.output.view = r.view, this.image.drawInto(this.frameContext.context, r.view, this.output);
|
|
882
|
+
}
|
|
883
|
+
async ready() {
|
|
884
|
+
await Promise.all([
|
|
885
|
+
this.image,
|
|
886
|
+
...this.made,
|
|
887
|
+
this.own
|
|
888
|
+
].map((e) => e?.ready?.()));
|
|
889
|
+
}
|
|
890
|
+
onDeviceRestored() {
|
|
891
|
+
for (let e of this.targets.values()) e.texture.destroy();
|
|
892
|
+
this.targets.clear(), this.releaseFeeds(), this.own?.release();
|
|
893
|
+
}
|
|
894
|
+
dispose() {
|
|
895
|
+
if (this.disposed) return;
|
|
896
|
+
this.disposed = !0;
|
|
897
|
+
for (let e of this.jobs) e.stop(new c("disposed", `${this.name} was disposed during toy.sound(); create a new Shadertoy`));
|
|
898
|
+
for (let e of this.targets.values()) e.texture.destroy();
|
|
899
|
+
this.targets.clear(), this.releaseFeeds(), this.keyTarget?.removeEventListener?.("keydown", this.onKey), this.keyTarget?.removeEventListener?.("keyup", this.onKey), this.image?.dispose(), this.own?.dispose();
|
|
900
|
+
for (let e of this.made.splice(0)) e.dispose();
|
|
901
|
+
let e = this.engine;
|
|
902
|
+
this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name);
|
|
903
|
+
}
|
|
904
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
905
|
+
this.dispose();
|
|
906
|
+
}
|
|
907
|
+
};
|
|
908
|
+
//#endregion
|
|
909
|
+
export { U as Shadertoy };
|