@margoengine/shader 0.0.0-stage → 1.0.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.
Files changed (92) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1919 -2
  4. package/dist/attribute.d.ts +28 -0
  5. package/dist/black-hole.d.ts +246 -0
  6. package/dist/black-hole.js +445 -0
  7. package/dist/clock-BLGNexlt.js +52 -0
  8. package/dist/clock.d.ts +42 -0
  9. package/dist/compute-CXGG9qvI.js +360 -0
  10. package/dist/compute-texture.d.ts +100 -0
  11. package/dist/compute-texture.js +131 -0
  12. package/dist/compute-volume.d.ts +57 -0
  13. package/dist/compute-volume.js +130 -0
  14. package/dist/compute.d.ts +200 -0
  15. package/dist/control-CFjkGPBk.js +201 -0
  16. package/dist/control.d.ts +93 -0
  17. package/dist/deform.d.ts +111 -0
  18. package/dist/deform.js +89 -0
  19. package/dist/display-BrJM_Qkd.js +5 -0
  20. package/dist/editor-model.d.ts +192 -0
  21. package/dist/editor-pass.d.ts +71 -0
  22. package/dist/editor-pass.js +216 -0
  23. package/dist/editor.d.ts +150 -0
  24. package/dist/editor.js +1071 -0
  25. package/dist/effects.d.ts +150 -0
  26. package/dist/effects.js +87 -0
  27. package/dist/errors-CsQwshHA.js +7 -0
  28. package/dist/glsl/emit.d.ts +43 -0
  29. package/dist/glsl/host.d.ts +55 -0
  30. package/dist/glsl/material.d.ts +173 -0
  31. package/dist/glsl/parser.d.ts +76 -0
  32. package/dist/glsl/preprocess.d.ts +9 -0
  33. package/dist/glsl/presets.d.ts +33 -0
  34. package/dist/glsl/program.d.ts +210 -0
  35. package/dist/glsl/scene.d.ts +9 -0
  36. package/dist/glsl-material.d.ts +82 -0
  37. package/dist/glsl-material.js +288 -0
  38. package/dist/glsl-pass.d.ts +93 -0
  39. package/dist/glsl-pass.js +186 -0
  40. package/dist/glsl.d.ts +94 -0
  41. package/dist/glsl.js +170 -0
  42. package/dist/gpu-DQJk2xAv.js +55 -0
  43. package/dist/graph.d.ts +82 -0
  44. package/dist/graph.js +365 -0
  45. package/dist/index.d.ts +8 -0
  46. package/dist/index.js +103 -0
  47. package/dist/lighting-JU9yQPfM.js +77 -0
  48. package/dist/lighting.d.ts +62 -0
  49. package/dist/lighting.js +1 -0
  50. package/dist/material-C6bpqlnE.js +1184 -0
  51. package/dist/material-CZ8V1FeA.js +299 -0
  52. package/dist/material.d.ts +79 -0
  53. package/dist/node-B6yoIC9V.js +590 -0
  54. package/dist/node.d.ts +433 -0
  55. package/dist/noise.d.ts +38 -0
  56. package/dist/noise.js +107 -0
  57. package/dist/own-stack-D1XnXrrz.js +11 -0
  58. package/dist/own-stack.d.ts +12 -0
  59. package/dist/pass.d.ts +109 -0
  60. package/dist/pass.js +239 -0
  61. package/dist/post-DLpZ110L.js +113 -0
  62. package/dist/post.d.ts +69 -0
  63. package/dist/preprocess-CWGWlBWQ.js +1195 -0
  64. package/dist/raw-0AYej3Ak.js +1500 -0
  65. package/dist/raw.d.ts +744 -0
  66. package/dist/raw.js +1 -0
  67. package/dist/readback.d.ts +42 -0
  68. package/dist/readback.js +85 -0
  69. package/dist/scope.d.ts +21 -0
  70. package/dist/serialization.d.ts +11 -0
  71. package/dist/serialization.js +192 -0
  72. package/dist/shadertoy/sound.d.ts +48 -0
  73. package/dist/shadertoy.d.ts +249 -0
  74. package/dist/shadertoy.js +909 -0
  75. package/dist/shared/caster.d.ts +159 -0
  76. package/dist/shared/fog.d.ts +33 -0
  77. package/dist/shared/fractal.d.ts +9 -0
  78. package/dist/shared/msaa.d.ts +30 -0
  79. package/dist/shared/stack.d.ts +22 -0
  80. package/dist/sound-CF_dzP41.js +78 -0
  81. package/dist/storage-format-DWaiSxmq.js +25 -0
  82. package/dist/storage-format.d.ts +7 -0
  83. package/dist/subgroups.d.ts +97 -0
  84. package/dist/subgroups.js +95 -0
  85. package/dist/texture-BawKO_h9.js +62 -0
  86. package/dist/texture-array.d.ts +7 -0
  87. package/dist/texture-array.js +11 -0
  88. package/dist/texture.d.ts +16 -0
  89. package/dist/utils.d.ts +106 -0
  90. package/dist/utils.js +90 -0
  91. package/dist/view-rect-Bm1ac-TK.js +16 -0
  92. package/package.json +149 -4
@@ -0,0 +1,216 @@
1
+ import { Lt as e, Mt as t, Rt as n, Zt as r, cn as i, ln as a } from "./node-B6yoIC9V.js";
2
+ import { g as o, i as s, n as c, o as l, t as u } from "./compute-CXGG9qvI.js";
3
+ import { n as d, r as f, t as p } from "./post-DLpZ110L.js";
4
+ import { compileSurfaceGraph as m, parseSurfaceGraph as h, serializeSurfaceGraph as g } from "./graph.js";
5
+ import { NodeEditor as _ } from "./editor.js";
6
+ import { ComputeTexture as v } from "./compute-texture.js";
7
+ import { ComputeVolume as y } from "./compute-volume.js";
8
+ import { MargoError as b } from "@margoengine/math/errors";
9
+ //#region src/editor-pass.ts
10
+ var x = () => ({
11
+ version: 1,
12
+ kind: "post",
13
+ graph: {
14
+ version: 1,
15
+ nodes: [{
16
+ id: "screen",
17
+ op: "color",
18
+ inputs: []
19
+ }],
20
+ outputs: { color: "screen" },
21
+ fallback: {
22
+ color: [
23
+ 0,
24
+ 0,
25
+ 0
26
+ ],
27
+ emissive: [
28
+ 0,
29
+ 0,
30
+ 0
31
+ ],
32
+ roughness: .5,
33
+ metalness: 0,
34
+ opacity: 1
35
+ }
36
+ }
37
+ }), S = [
38
+ "position",
39
+ "worldPosition",
40
+ "normal",
41
+ "worldNormal",
42
+ "color",
43
+ "uv"
44
+ ], C = (e) => typeof e == "string" && /^[A-Za-z][A-Za-z0-9_-]{0,63}$/.test(e), w = (e) => {
45
+ if (typeof e != "string" || new TextEncoder().encode(e).length > 131072) throw RangeError("Use a pass graph JSON document of at most 131072 bytes");
46
+ };
47
+ function T(e) {
48
+ w(e);
49
+ let t = JSON.parse(e);
50
+ if (!t || t.version !== 1 || t.kind !== "post" && t.kind !== "compute") throw TypeError("Use a version 1 post or compute pass graph");
51
+ if (t.colorSpace !== void 0 && t.colorSpace !== "hdr" && t.colorSpace !== "ldr") throw TypeError("Use hdr or ldr colorSpace");
52
+ let n = h(g(t.graph));
53
+ if (Object.keys(n.outputs).some((e) => e !== "color" && e !== "opacity")) throw TypeError("Pass graphs use color and opacity outputs only; remove material slots");
54
+ let r = {};
55
+ if (t.layout !== void 0) {
56
+ if (!t.layout || typeof t.layout != "object" || Array.isArray(t.layout) || Object.keys(t.layout).length > 129) throw TypeError("Use at most 129 layout positions");
57
+ for (let [e, i] of Object.entries(t.layout)) {
58
+ if (e !== "output" && !n.nodes.some((t) => t.id === e) || !Array.isArray(i) || i.length !== 2 || !i.every((e) => typeof e == "number" && Number.isFinite(e) && Math.abs(e) <= 1e6)) throw TypeError("Use finite layout positions for existing nodes, within one million pixels");
59
+ r[e] = [i[0], i[1]];
60
+ }
61
+ }
62
+ if (t.kind === "post") {
63
+ if (t.compute !== void 0) throw TypeError("Remove compute settings from a post graph");
64
+ return {
65
+ version: 1,
66
+ kind: "post",
67
+ graph: n,
68
+ layout: r,
69
+ colorSpace: t.colorSpace ?? "ldr"
70
+ };
71
+ }
72
+ let i = t.compute;
73
+ if (!i || !Array.isArray(i.size) || i.size.length !== 3 || !i.size.every((e) => Number.isInteger(e) && e > 0 && e <= 65535) || i.size.reduce((e, t) => e * t, 1) > 4294967295 || !C(i.target) || i.source !== void 0 && !C(i.source)) throw TypeError("Use a compute size of three positive integers up to 65535, at most 4294967295 invocations, and named target/source resources");
74
+ return {
75
+ version: 1,
76
+ kind: "compute",
77
+ graph: n,
78
+ layout: r,
79
+ compute: {
80
+ size: [...i.size],
81
+ target: i.target,
82
+ ...i.source ? { source: i.source } : {}
83
+ }
84
+ };
85
+ }
86
+ function E(e) {
87
+ return JSON.stringify(T(JSON.stringify(e)));
88
+ }
89
+ var D = (e, t, n) => {
90
+ let r = Object.hasOwn(e, t) ? e[t] : void 0;
91
+ if (!(r instanceof c || r instanceof v || r instanceof y)) throw TypeError(`Provide the pass graph resource '${t}'`);
92
+ if (r instanceof c ? r.type !== "vec4f" || r.count < n : /[us]int$/.test(r.format) || r.pixels < n) throw TypeError(`Use a float texture or vec4f buffer '${t}' large enough for ${n} invocations`);
93
+ return r;
94
+ }, O = (e, t) => e instanceof c ? e.element(t) : e.load(e.coord(t));
95
+ function k(h, g = {}) {
96
+ let _ = T(E(h));
97
+ if (_.kind === "post") {
98
+ let o = m(_.graph, {
99
+ position: i(e, 0),
100
+ worldPosition: i(t, 1),
101
+ normal: i(f()),
102
+ worldNormal: i(r),
103
+ color: d().rgb,
104
+ uv: i(n, 0)
105
+ });
106
+ return new p(a(o.colorNode?.rgb ?? i(0), o.opacityNode ?? d().a), { kind: _.colorSpace });
107
+ }
108
+ let v = _.compute, y = v.size.reduce((e, t) => e * t, 1), b = D(g, v.target, y), x = v.source ? D(g, v.source, y) : void 0;
109
+ return new u(v.size, (e) => {
110
+ let t = m(_.graph, {
111
+ position: s.toFloat(),
112
+ worldPosition: o.toFloat(),
113
+ normal: l.toFloat(),
114
+ worldNormal: i(r),
115
+ color: x ? O(x, e).rgb : i(0),
116
+ uv: s.toFloat().add(.5).div(i(...v.size))
117
+ }), n = a(t.colorNode?.rgb ?? i(0), t.opacityNode ?? (x ? O(x, e).a : 1));
118
+ return b instanceof c ? b.store(e, n) : b.store(b.coord(e), n);
119
+ });
120
+ }
121
+ var A = (e) => {
122
+ let t = e === "post" ? [
123
+ "Pixel position",
124
+ "Resolution",
125
+ "Scene depth",
126
+ "Time",
127
+ "Screen colour",
128
+ "Screen UV"
129
+ ] : [
130
+ "Invocation position",
131
+ "Workgroup position",
132
+ "Local position",
133
+ "Time",
134
+ "Source value",
135
+ "Normalized position"
136
+ ];
137
+ return S.map((e, n) => ({
138
+ op: e,
139
+ label: t[n],
140
+ group: "Input",
141
+ description: t[n] + " as a three-component value."
142
+ }));
143
+ }, j = class {
144
+ constructor(e, t = {}) {
145
+ if (this.parent = e, this.options = t, this.listeners = /* @__PURE__ */ new Set(), this.stopped = !1, this.doc = T(E(t.document ?? x())), this.resources = t.resources ?? {}, this.mount(), t.signal) {
146
+ let e = () => this.dispose();
147
+ t.signal.addEventListener("abort", e, { once: !0 }), this.abort = () => t.signal.removeEventListener("abort", e), t.signal.aborted && this.dispose();
148
+ }
149
+ }
150
+ live() {
151
+ if (this.stopped) throw new b("disposed", "Create a new PassNodeEditor after disposal");
152
+ }
153
+ mount() {
154
+ let e = this.doc;
155
+ k(e, this.resources).dispose(), this.view = new _(this.parent, {
156
+ graph: e.graph,
157
+ layout: e.layout,
158
+ readOnly: this.options.readOnly,
159
+ presentation: {
160
+ title: e.kind === "post" ? "Post output" : "Compute output",
161
+ fallback: !1,
162
+ operations: A(e.kind),
163
+ outputs: [{
164
+ slot: "color",
165
+ label: "Colour",
166
+ type: "vec3f"
167
+ }, {
168
+ slot: "opacity",
169
+ label: "Alpha",
170
+ type: "f32"
171
+ }],
172
+ code: (t) => {
173
+ let n = k({
174
+ ...e,
175
+ graph: t
176
+ }, this.resources);
177
+ return n instanceof p ? n.code() : n.code;
178
+ }
179
+ }
180
+ }), this.stop = this.view.onGraph(() => {
181
+ for (let e of this.listeners) e(this.document);
182
+ });
183
+ }
184
+ get editor() {
185
+ return this.live(), this.view;
186
+ }
187
+ get document() {
188
+ return this.live(), T(E({
189
+ ...this.doc,
190
+ graph: this.view.graph,
191
+ layout: this.view.layout
192
+ }));
193
+ }
194
+ save() {
195
+ return E(this.document);
196
+ }
197
+ load(e) {
198
+ if (this.live(), this.options.readOnly) throw new b("state", "Create an editable PassNodeEditor to load another document");
199
+ let t = T(typeof e == "string" ? e : E(e));
200
+ k(t, this.resources).dispose(), this.stop?.(), this.view.dispose(), this.doc = t, this.mount();
201
+ }
202
+ onGraph(e) {
203
+ return this.live(), this.listeners.add(e), e(this.document), () => this.listeners.delete(e);
204
+ }
205
+ compile() {
206
+ return k(this.document, this.resources);
207
+ }
208
+ dispose() {
209
+ this.stopped || (this.stopped = !0, this.abort?.(), this.stop?.(), this.view.dispose(), this.listeners.clear());
210
+ }
211
+ [Symbol.dispose ?? Symbol()]() {
212
+ this.dispose();
213
+ }
214
+ };
215
+ //#endregion
216
+ export { j as PassNodeEditor, k as compilePassGraph, T as parsePassGraph, E as serializePassGraph };
@@ -0,0 +1,150 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import { type SurfaceGraph, type SurfaceOperation } from './graph.js';
3
+ import { type EditorOperation, GraphModel, OUTPUT_SLOTS, type EditorStatus, type GraphLayout } from './editor-model.js';
4
+ export * from './editor-model.js';
5
+ /** Options of {@link NodeEditor}. */
6
+ export interface NodeEditorOptions {
7
+ /** A valid portable graph to start from; default an empty graph. */
8
+ graph?: SurfaceGraph;
9
+ /** Node positions saved with the graph (`editor.layout`); nodes without one are arranged. */
10
+ layout?: GraphLayout;
11
+ /** Show the graph but refuse edits: no palette, no dragging, no typing. Default false. */
12
+ readOnly?: boolean;
13
+ /** Disposes the editor when aborted. */
14
+ signal?: AbortSignal;
15
+ /** Optional host labels, visible outputs and code preview, for non-material graph editors. */
16
+ presentation?: NodeEditorPresentation;
17
+ }
18
+ /** Presentation of the same typed graph model in another shader host. */
19
+ export interface NodeEditorPresentation {
20
+ /** Palette and node descriptions; omitted operations keep their surface labels. */
21
+ operations?: readonly EditorOperation[];
22
+ /** Ordered output sockets, default all material slots. */
23
+ outputs?: readonly (typeof OUTPUT_SLOTS)[number][];
24
+ /** Output panel title, default Material output. */
25
+ title?: string;
26
+ /** Show fallback material fields, default true. */
27
+ fallback?: boolean;
28
+ /** Generated code for the preview button; default material fragment code. */
29
+ code?(graph: SurfaceGraph): string;
30
+ }
31
+ /**
32
+ * A visual editor for the portable surface graph (`SurfaceGraph`): drag nodes from a palette, wire outputs to inputs,
33
+ * type constants, and watch the graph validate as you edit. It edits a {@link GraphModel}; {@link NodeEditor.onGraph}
34
+ * reports each valid graph so a material can be rebuilt (hot reload). Styled in a shadow root, light and dark, with no
35
+ * global side effects. Mouse, touch and keyboard all work: sockets are buttons (click an output, then click an input),
36
+ * `Shift+A` or double click opens the node palette, `Delete`, `Ctrl+Z`, `Ctrl+Y`, `Ctrl+D`, `Ctrl+A`, `F` and the arrow
37
+ * keys do what they do in other node editors.
38
+ * @throws TypeError or RangeError when `options.graph` is invalid; MargoError `'disposed'` from methods after `dispose()`.
39
+ */
40
+ export declare class NodeEditor {
41
+ /** The editor's root element, already appended to the parent you passed. */
42
+ readonly element: HTMLElement;
43
+ /** The editable graph; change it directly or through the user's gestures. */
44
+ readonly model: GraphModel;
45
+ readonly readOnly: boolean;
46
+ private readonly doc;
47
+ private readonly root;
48
+ private readonly viewport;
49
+ private readonly world;
50
+ private readonly wires;
51
+ private readonly palette;
52
+ private readonly search;
53
+ private readonly list;
54
+ private readonly code;
55
+ private readonly statusText;
56
+ private readonly buttons;
57
+ private readonly nodes;
58
+ private readonly selected;
59
+ private readonly graphListeners;
60
+ private readonly changeListeners;
61
+ private readonly unsubscribe;
62
+ private readonly abort?;
63
+ private pan;
64
+ private gesture?;
65
+ private armed?;
66
+ private lastJSON;
67
+ private matches;
68
+ private choice;
69
+ private addAt;
70
+ private lastPoint?;
71
+ private pendingWire?;
72
+ private flash;
73
+ private disposed;
74
+ private readonly presentation;
75
+ private readonly operations;
76
+ private readonly slots;
77
+ /**
78
+ * @param parent Element to append the editor to; the editor fills it, so give the parent a height.
79
+ */
80
+ constructor(parent: HTMLElement, options?: NodeEditorOptions);
81
+ private make;
82
+ private live;
83
+ /** The current graph, validated. Throws like {@link GraphModel.toGraph} while the graph is invalid or incomplete. */
84
+ get graph(): SurfaceGraph;
85
+ /** Whether the graph validates, or the first thing to fix. */
86
+ get status(): EditorStatus;
87
+ /** Ids of the selected nodes. */
88
+ get selection(): readonly string[];
89
+ /** Node positions, to save beside the graph. */
90
+ get layout(): GraphLayout;
91
+ /**
92
+ * Calls `listener` with every new valid graph (once on subscribe if the graph is valid now, then after each edit that
93
+ * changes it; moves and invalid states do not call it). Rebuild a material from it for hot reload. Returns a function
94
+ * that stops the calls.
95
+ */
96
+ onGraph(listener: (graph: SurfaceGraph) => void): () => void;
97
+ /** Calls `listener` with the status after every edit. Returns a function that stops the calls. */
98
+ onChange(listener: (status: EditorStatus) => void): () => void;
99
+ /** Replaces the graph being edited (one undo step). */
100
+ load(graph: SurfaceGraph, layout?: GraphLayout): void;
101
+ /** Selects nodes by id (unknown ids are ignored). */
102
+ select(ids: Iterable<string>): void;
103
+ /** Zooms and pans so every node and the output panel are visible. */
104
+ fit(): void;
105
+ /** Adds a node of `op` where the palette would put it, selects it and returns its id. */
106
+ add(op: SurfaceOperation, at?: readonly [number, number]): string;
107
+ /** Removes the editor from the page and stops every listener. Safe to call twice. */
108
+ dispose(): void;
109
+ [Symbol.dispose]: () => void;
110
+ private applyPan;
111
+ private point;
112
+ private render;
113
+ /** Selector of the socket that has keyboard focus, so a redraw can give it back (the redraw replaces the buttons). */
114
+ private focusedSocket;
115
+ private layoutOnly;
116
+ private place;
117
+ private socket;
118
+ private field;
119
+ private drawNode;
120
+ private option;
121
+ private usedBy;
122
+ private drawOutput;
123
+ private curve;
124
+ private drawWires;
125
+ private paintSelection;
126
+ private say;
127
+ /** The input socket or output-panel slot whose centre is within reach of a point, if any. */
128
+ private targetAt;
129
+ private connectTo;
130
+ private sourceOf;
131
+ private socketTarget;
132
+ private wire;
133
+ /** The middle of the visible canvas in editor pixels, stepped down and right for each node so new nodes do not stack. */
134
+ private centre;
135
+ private down;
136
+ private moved;
137
+ private up;
138
+ private cancel;
139
+ private suppressNext;
140
+ private suppress;
141
+ private clicked;
142
+ private wheel;
143
+ private key;
144
+ private openPalette;
145
+ private closePalette;
146
+ private filter;
147
+ private paintChoice;
148
+ private choose;
149
+ private showCode;
150
+ }