@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,150 @@
|
|
|
1
|
+
import { NodePass, type NodePassOptions } from './post.js';
|
|
2
|
+
import { type UniformNode } from './node.js';
|
|
3
|
+
/**
|
|
4
|
+
* A ready-made PostStack effect: a {@link NodePass} whose live parameters are named f32 uniforms. Assign
|
|
5
|
+
* `effect.uniforms.<name>.value` at any time; that only re-uploads the value, it never rebuilds a shader or pipeline.
|
|
6
|
+
*/
|
|
7
|
+
export type ScreenEffect<U extends Record<string, UniformNode<'f32'>>> = NodePass & {
|
|
8
|
+
readonly uniforms: U;
|
|
9
|
+
};
|
|
10
|
+
/** Options for {@link vignette}. */
|
|
11
|
+
export interface VignetteOptions extends NodePassOptions {
|
|
12
|
+
/** How much the corners darken: rgb is multiplied by `1 - darkness` there. Default 0.8. */
|
|
13
|
+
darkness?: number;
|
|
14
|
+
/** Distance from the centre where darkening starts, 0 at the centre and 1 at the corners. Default 0.4. */
|
|
15
|
+
radius?: number;
|
|
16
|
+
/** Width of the smooth ramp from no darkening to full darkening, same units as `radius`. Default 0.6. */
|
|
17
|
+
softness?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Darkens towards the screen corners. Distance from the centre is measured in UV units scaled so the corners are 1
|
|
21
|
+
* (not aspect-corrected, so the falloff is an ellipse on non-square screens); pixels inside `radius` are unchanged and
|
|
22
|
+
* the darkening rises with a smoothstep over `softness`. The result is clamped to 0..1.
|
|
23
|
+
* The parameterisation and its defaults are a Margo design choice.
|
|
24
|
+
* @throws RangeError when a given option is not a finite number.
|
|
25
|
+
*/
|
|
26
|
+
export declare function vignette(options?: VignetteOptions): ScreenEffect<{
|
|
27
|
+
darkness: UniformNode<'f32'>;
|
|
28
|
+
radius: UniformNode<'f32'>;
|
|
29
|
+
softness: UniformNode<'f32'>;
|
|
30
|
+
}>;
|
|
31
|
+
/** Options for {@link rgbShift}. */
|
|
32
|
+
export interface RGBShiftOptions extends NodePassOptions {
|
|
33
|
+
/** Offset of the red and blue images in UV units. Default 0.002: one five-hundredth of the screen, a fringe of about 4 px at 1920 wide (2 px at 960), visible as lens fringe without doubling edges. */
|
|
34
|
+
amount?: number;
|
|
35
|
+
/** Direction of the offset in radians; 0 moves red right and blue left, positive turns towards screen-down. Default 0. */
|
|
36
|
+
angle?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Colour fringe: red is read `amount` UV units behind along `angle` and blue the same distance ahead, so the red
|
|
40
|
+
* image moves along the direction and the blue image against it; green and alpha are the pixel's own.
|
|
41
|
+
* @throws RangeError when a given option is not a finite number.
|
|
42
|
+
*/
|
|
43
|
+
export declare function rgbShift(options?: RGBShiftOptions): ScreenEffect<{
|
|
44
|
+
amount: UniformNode<'f32'>;
|
|
45
|
+
angle: UniformNode<'f32'>;
|
|
46
|
+
}>;
|
|
47
|
+
/** Options for {@link chromaticAberration}. */
|
|
48
|
+
export interface ChromaticAberrationOptions extends NodePassOptions {
|
|
49
|
+
/** Red is scaled about the centre by `1 - strength` and blue by `1 + strength`. Default 0.01. */
|
|
50
|
+
strength?: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Lateral chromatic aberration: red and blue are read from the image scaled about the screen centre by
|
|
54
|
+
* `1 - strength` and `1 + strength`, so the fringes grow towards the edges and the centre stays sharp.
|
|
55
|
+
* @throws RangeError when `strength` is given and is not a finite number.
|
|
56
|
+
*/
|
|
57
|
+
export declare function chromaticAberration(options?: ChromaticAberrationOptions): ScreenEffect<{
|
|
58
|
+
strength: UniformNode<'f32'>;
|
|
59
|
+
}>;
|
|
60
|
+
/** Options for {@link pixelate}. */
|
|
61
|
+
export interface PixelateOptions extends NodePassOptions {
|
|
62
|
+
/** Block size in pixels, floored, at least 1. Default 8. */
|
|
63
|
+
size?: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Block mosaic: square blocks of `size` pixels from the top left corner, each filled with the exact input pixel at
|
|
67
|
+
* its centre (offset `floor(size / 2)`, kept on screen for partial blocks at the right and bottom).
|
|
68
|
+
* @throws RangeError when `size` is given and is not a finite number.
|
|
69
|
+
*/
|
|
70
|
+
export declare function pixelate(options?: PixelateOptions): ScreenEffect<{
|
|
71
|
+
size: UniformNode<'f32'>;
|
|
72
|
+
}>;
|
|
73
|
+
/** Options for {@link halftone}. */
|
|
74
|
+
export interface HalftoneOptions extends NodePassOptions {
|
|
75
|
+
/** Cell size in pixels. Default 8. */
|
|
76
|
+
size?: number;
|
|
77
|
+
/** Red screen angle in radians; green is 60 degrees on and blue 30 degrees on. Default 15 degrees (about 0.26). */
|
|
78
|
+
angle?: number;
|
|
79
|
+
/** One screen on the luma instead of three colour screens; read at construction. Default false. */
|
|
80
|
+
greyscale?: boolean;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Print-style colour halftone: one rotated dot screen per channel (red at `angle`, green at `angle + 60` degrees,
|
|
84
|
+
* blue at `angle + 30` degrees: the conventional 15/75/45 degree screen set by default). Each cell reads the input once
|
|
85
|
+
* at its centre and draws one dot whose area follows that channel, giving coloured dots on black. `greyscale` uses a
|
|
86
|
+
* single screen on the Rec. 709 luma for all three channels.
|
|
87
|
+
* @throws RangeError when a given numeric option is not a finite number.
|
|
88
|
+
*/
|
|
89
|
+
export declare function halftone(options?: HalftoneOptions): ScreenEffect<{
|
|
90
|
+
size: UniformNode<'f32'>;
|
|
91
|
+
angle: UniformNode<'f32'>;
|
|
92
|
+
}>;
|
|
93
|
+
/** Options for {@link dotScreen}. */
|
|
94
|
+
export interface DotScreenOptions extends NodePassOptions {
|
|
95
|
+
/** Cell size in pixels. Default 6. */
|
|
96
|
+
size?: number;
|
|
97
|
+
/** Screen angle in radians. Default pi/4. */
|
|
98
|
+
angle?: number;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Newspaper look: black dots on white from one rotated screen, each dot's area following the darkness (1 - Rec. 709
|
|
102
|
+
* luma) of the pixel's own input colour.
|
|
103
|
+
* @throws RangeError when a given option is not a finite number.
|
|
104
|
+
*/
|
|
105
|
+
export declare function dotScreen(options?: DotScreenOptions): ScreenEffect<{
|
|
106
|
+
size: UniformNode<'f32'>;
|
|
107
|
+
angle: UniformNode<'f32'>;
|
|
108
|
+
}>;
|
|
109
|
+
/** Options for {@link sobel}. */
|
|
110
|
+
export interface SobelOptions extends NodePassOptions {
|
|
111
|
+
/** Gradient magnitude scale before clamping to 0..1. Default 1. */
|
|
112
|
+
strength?: number;
|
|
113
|
+
/** 0 shows white edges on black; 1 draws black edges over the image. Default 0. */
|
|
114
|
+
overlay?: number;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Edge detection with the Sobel operator (3x3, weights 1, 2, 1) on Rec. 709 luma, reading the eight neighbours
|
|
118
|
+
* exactly at their texel centres. `edge = clamp(sqrt(gx^2 + gy^2) * strength, 0, 1)`; the output mixes the grey edge
|
|
119
|
+
* image with the input darkened by the edge, by `overlay`.
|
|
120
|
+
* @throws RangeError when a given option is not a finite number.
|
|
121
|
+
*/
|
|
122
|
+
export declare function sobel(options?: SobelOptions): ScreenEffect<{
|
|
123
|
+
strength: UniformNode<'f32'>;
|
|
124
|
+
overlay: UniformNode<'f32'>;
|
|
125
|
+
}>;
|
|
126
|
+
/** Options for {@link sepia}. */
|
|
127
|
+
export interface SepiaOptions extends NodePassOptions {
|
|
128
|
+
/** Blend from the input (0) to full sepia (1); not clamped, so larger values extrapolate. Default 1. */
|
|
129
|
+
amount?: number;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Sepia tone with the matrix of the W3C Filter Effects Module Level 1 `sepia()` function, blended with the input by
|
|
133
|
+
* `amount`. Not clamped in the shader; an LDR target clamps on write.
|
|
134
|
+
* @throws RangeError when `amount` is given and is not a finite number.
|
|
135
|
+
*/
|
|
136
|
+
export declare function sepia(options?: SepiaOptions): ScreenEffect<{
|
|
137
|
+
amount: UniformNode<'f32'>;
|
|
138
|
+
}>;
|
|
139
|
+
/** Options for {@link posterize}. */
|
|
140
|
+
export interface PosterizeOptions extends NodePassOptions {
|
|
141
|
+
/** Levels per channel, floored, at least 2. Default 4. */
|
|
142
|
+
levels?: number;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Reduces each channel to `levels` evenly spaced values from 0 to 1, rounding half up.
|
|
146
|
+
* @throws RangeError when `levels` is given and is not a finite number.
|
|
147
|
+
*/
|
|
148
|
+
export declare function posterize(options?: PosterizeOptions): ScreenEffect<{
|
|
149
|
+
levels: UniformNode<'f32'>;
|
|
150
|
+
}>;
|
package/dist/effects.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { D as e, Lt as t, Mt as n, Rt as r, Ut as i, Wt as a, cn as o, ct as s, en as c, ft as l, ln as u, sn as d, ut as f, zt as p } from "./node-DzKv9CF2.js";
|
|
2
|
+
import { n as m, t as h } from "./post-DC62ZleD.js";
|
|
3
|
+
//#region src/effects.ts
|
|
4
|
+
var g = 0;
|
|
5
|
+
function _(e, t, n) {
|
|
6
|
+
let r = e[t];
|
|
7
|
+
if (r !== void 0 && (typeof r != "number" || !Number.isFinite(r))) throw RangeError(`Screen effect ${t} must be a finite number`);
|
|
8
|
+
return c(r ?? n, { name: t });
|
|
9
|
+
}
|
|
10
|
+
function v(e, t, n, r) {
|
|
11
|
+
let i = new h(u(n, m().a), {
|
|
12
|
+
kind: t.kind,
|
|
13
|
+
name: t.name ?? `shader:${e}:${g++}`
|
|
14
|
+
});
|
|
15
|
+
return Object.assign(i, { uniforms: r });
|
|
16
|
+
}
|
|
17
|
+
var y = (e) => e.dot(o(.2126, .7152, .0722)), b = (e) => m(e.div(n)), x = (e, t, n) => d(t.mul(e.x).sub(n.mul(e.y)), n.mul(e.x).add(t.mul(e.y))), S = (e) => {
|
|
18
|
+
let t = a(e.oneMinus()), n = t.mul(t.mul(t.mul(t.mul(.6786358).sub(.4090116)).add(.1827032)).sub(.5127241)).add(.7074851);
|
|
19
|
+
return p(e.greaterThan(Math.PI / 4), n.min(Math.SQRT1_2), a(e.div(Math.PI)));
|
|
20
|
+
};
|
|
21
|
+
function C(e, n, r) {
|
|
22
|
+
let i = n.cos(), a = n.sin(), o = x(t, i, a.negate()), s = o.div(e).floor().add(.5).mul(e), c = S(r(x(s, i, a)).saturate()).mul(e);
|
|
23
|
+
return c.sub(o.distance(s)).add(.5).saturate().mul(c.mul(2).saturate());
|
|
24
|
+
}
|
|
25
|
+
function w(e = {}) {
|
|
26
|
+
let t = _(e, "darkness", .8), n = _(e, "radius", .4), a = _(e, "softness", .6), o = r.sub(.5).length().mul(Math.SQRT2), s = i(n, n.add(f(a, 1e-5)), o);
|
|
27
|
+
return v("vignette", e, m().rgb.mul(s.mul(t).oneMinus()).saturate(), {
|
|
28
|
+
darkness: t,
|
|
29
|
+
radius: n,
|
|
30
|
+
softness: a
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function T(e = {}) {
|
|
34
|
+
let t = _(e, "amount", .002), n = _(e, "angle", 0), i = d(n.cos(), n.sin()).mul(t);
|
|
35
|
+
return v("rgbShift", e, o(m(r.sub(i)).r, m().g, m(r.add(i)).b), {
|
|
36
|
+
amount: t,
|
|
37
|
+
angle: n
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function E(e = {}) {
|
|
41
|
+
let t = _(e, "strength", .01), n = r.sub(.5);
|
|
42
|
+
return v("chromaticAberration", e, o(m(n.mul(t.oneMinus()).add(.5)).r, m().g, m(n.mul(t.add(1)).add(.5)).b), { strength: t });
|
|
43
|
+
}
|
|
44
|
+
function D(r = {}) {
|
|
45
|
+
let i = _(r, "size", 8), a = f(i.floor(), 1), o = t.div(a).floor().mul(a);
|
|
46
|
+
return v("pixelate", r, b(e(o.add(a.mul(.5).floor()).add(.5), d(.5), n.sub(.5))).rgb, { size: i });
|
|
47
|
+
}
|
|
48
|
+
function O(e = {}) {
|
|
49
|
+
let t = _(e, "size", 8), n = _(e, "angle", Math.PI / 12);
|
|
50
|
+
return v("halftone", e, e.greyscale ? o(C(t, n, (e) => y(b(e).rgb))) : o(C(t, n, (e) => b(e).r), C(t, n.add(Math.PI / 3), (e) => b(e).g), C(t, n.add(Math.PI / 6), (e) => b(e).b)), {
|
|
51
|
+
size: t,
|
|
52
|
+
angle: n
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function k(e = {}) {
|
|
56
|
+
let t = _(e, "size", 6), n = _(e, "angle", Math.PI / 4), r = y(m().rgb).oneMinus();
|
|
57
|
+
return v("dotScreen", e, o(C(t, n, () => r).oneMinus()), {
|
|
58
|
+
size: t,
|
|
59
|
+
angle: n
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function A(e = {}) {
|
|
63
|
+
let n = _(e, "strength", 1), r = _(e, "overlay", 0), i = (e, n) => y(b(t.add(d(e, n))).rgb), [s, c, u, f, p, h, g, x] = [
|
|
64
|
+
i(-1, -1),
|
|
65
|
+
i(0, -1),
|
|
66
|
+
i(1, -1),
|
|
67
|
+
i(-1, 0),
|
|
68
|
+
i(1, 0),
|
|
69
|
+
i(-1, 1),
|
|
70
|
+
i(0, 1),
|
|
71
|
+
i(1, 1)
|
|
72
|
+
], S = u.add(p.mul(2)).add(x).sub(s.add(f.mul(2)).add(h)), C = h.add(g.mul(2)).add(x).sub(s.add(c.mul(2)).add(u)), w = a(S.mul(S).add(C.mul(C))).mul(n).saturate();
|
|
73
|
+
return v("sobel", e, l(o(w), m().rgb.mul(w.oneMinus()), r), {
|
|
74
|
+
strength: n,
|
|
75
|
+
overlay: r
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function j(e = {}) {
|
|
79
|
+
let t = _(e, "amount", 1), n = s(.393, .349, .272, .769, .686, .534, .189, .168, .131), r = m().rgb;
|
|
80
|
+
return v("sepia", e, l(r, n.mul(r), t), { amount: t });
|
|
81
|
+
}
|
|
82
|
+
function M(e = {}) {
|
|
83
|
+
let t = _(e, "levels", 4), n = f(t.floor(), 2).sub(1);
|
|
84
|
+
return v("posterize", e, m().rgb.mul(n).add(.5).floor().div(n), { levels: t });
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
export { E as chromaticAberration, k as dotScreen, O as halftone, D as pixelate, M as posterize, T as rgbShift, j as sepia, A as sobel, w as vignette };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { GLSLHost } from './host.js';
|
|
2
|
+
import type { Program } from './parser.js';
|
|
3
|
+
/** Typed GLSL expression and control-flow lowering to the custom transition WGSL contract. */
|
|
4
|
+
export declare class GLSLEmitter {
|
|
5
|
+
private program;
|
|
6
|
+
private host;
|
|
7
|
+
private version;
|
|
8
|
+
private scopes;
|
|
9
|
+
private serial;
|
|
10
|
+
private active?;
|
|
11
|
+
private functions;
|
|
12
|
+
private names;
|
|
13
|
+
private helpers;
|
|
14
|
+
private equalityNames;
|
|
15
|
+
private uniforms;
|
|
16
|
+
private flipY;
|
|
17
|
+
constructor(program: Program, host: GLSLHost, version?: 100 | 300);
|
|
18
|
+
private fail;
|
|
19
|
+
private temp;
|
|
20
|
+
private equality;
|
|
21
|
+
private find;
|
|
22
|
+
private define;
|
|
23
|
+
private convert;
|
|
24
|
+
private join;
|
|
25
|
+
private literal;
|
|
26
|
+
private assign;
|
|
27
|
+
private expr;
|
|
28
|
+
private arith;
|
|
29
|
+
private call;
|
|
30
|
+
private picked;
|
|
31
|
+
private lookup;
|
|
32
|
+
private texel;
|
|
33
|
+
private inverse;
|
|
34
|
+
private declarations;
|
|
35
|
+
private arrayCount;
|
|
36
|
+
private components;
|
|
37
|
+
private parameter;
|
|
38
|
+
private returnValue;
|
|
39
|
+
private body;
|
|
40
|
+
private statement;
|
|
41
|
+
private switch;
|
|
42
|
+
emit(): string;
|
|
43
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** A scalar or flattened vector, column-major matrix or fixed array uniform value. */
|
|
2
|
+
export type GLSLUniformValue = number | boolean | readonly (number | boolean)[];
|
|
3
|
+
/** A name the host provides to translated GLSL: the WGSL expression that reads it and its GLSL type. */
|
|
4
|
+
export interface GLSLHostName {
|
|
5
|
+
readonly code: string;
|
|
6
|
+
readonly type: string;
|
|
7
|
+
}
|
|
8
|
+
/** The level, bias, gradients or texel offset of a GLSL texture lookup, as WGSL expressions. */
|
|
9
|
+
export interface GLSLSampleOptions {
|
|
10
|
+
readonly lod?: string;
|
|
11
|
+
readonly bias?: string;
|
|
12
|
+
readonly ddx?: string;
|
|
13
|
+
readonly ddy?: string;
|
|
14
|
+
readonly offset?: string;
|
|
15
|
+
/** The depth reference of a shadow sampler's lookup (GLSL ES 3.00 8.8): the lookup compares it with the texel. */
|
|
16
|
+
readonly compare?: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* What a host (a package that gives translated GLSL its surroundings) supplies to the GLSL front end:
|
|
20
|
+
* the uniform values it freezes into constants, the coordinate convention, the names it provides,
|
|
21
|
+
* the colour readers it maps to WGSL samplers and the WGSL that wraps the entry function.
|
|
22
|
+
*/
|
|
23
|
+
export interface GLSLHost {
|
|
24
|
+
/** Uniform values snapshotted into WGSL constants. */
|
|
25
|
+
readonly uniforms: Readonly<Record<string, GLSLUniformValue>>;
|
|
26
|
+
/** GLSL UVs use a bottom-left origin; true flips y at every sample and at the entry. */
|
|
27
|
+
readonly flipY: boolean;
|
|
28
|
+
/** Names the host defines (a global with one of these names is not declared again, except gl_FragCoord). */
|
|
29
|
+
readonly names: Readonly<Record<string, GLSLHostName>>;
|
|
30
|
+
/** Maps a colour reader such as getFromColor to the WGSL texture it samples. */
|
|
31
|
+
readonly colorReaders: Readonly<Record<string, string>>;
|
|
32
|
+
/** WGSL placed before the user's functions (the storage behind gl_FragCoord). */
|
|
33
|
+
readonly prologue: string;
|
|
34
|
+
/** WGSL for the entry function that calls the translated one, given its WGSL name. */
|
|
35
|
+
epilogue(entry: string): string;
|
|
36
|
+
/** The GLSL function the host expects: its name, return type and parameter types. */
|
|
37
|
+
readonly entry: {
|
|
38
|
+
readonly name: string;
|
|
39
|
+
readonly type: string;
|
|
40
|
+
readonly params: readonly string[];
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* WGSL that samples `texture` (a value of the GLSL sampler type `kind`) at `coords`, already y flipped when the
|
|
44
|
+
* host flips. Without it a lookup reads the host's own `sample(texture, uv)`, the nearest texel of a 2D image.
|
|
45
|
+
*/
|
|
46
|
+
readonly sample?: (texture: string, kind: string, coords: string, options: GLSLSampleOptions) => string;
|
|
47
|
+
/** The WGSL sampler paired with texture `code` when a sampler is passed to a function. Default `code` + "s". */
|
|
48
|
+
readonly samplerOf?: (code: string) => string;
|
|
49
|
+
/** When set, a sampler argument also passes this u32 WGSL value (the lookup mode of texture `code`) after its sampler. */
|
|
50
|
+
readonly samplerMode?: (code: string) => string;
|
|
51
|
+
/** Window y points up, as GLSL's does, so dFdy is the negated WGSL dpdy. Default `flipY`. */
|
|
52
|
+
readonly yUp?: boolean;
|
|
53
|
+
/** Message for a missing entry function. */
|
|
54
|
+
readonly missingEntry: string;
|
|
55
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { RawMesh, RawShaderMaterial, type RawMeshOptions, type RawShaderMaterialOptions, type RawTextureInput } from '../raw.js';
|
|
2
|
+
import { type GLSLLinked, type GLSLSceneLinker, type GLSLProgramSource, type GLSLProgramWGSL } from './program.js';
|
|
3
|
+
/**
|
|
4
|
+
* A uniform's value: a number or boolean, the numbers of a vector or column-major matrix (an array, a typed array or
|
|
5
|
+
* an `{x, y, z, w}` object), a `'#rrggbb'` colour for a vec3, or for a sampler a `Texture` of `@margoengine/materials`,
|
|
6
|
+
* a GPUTexture or a GPUTextureView (null reads opaque white; zero for an integer sampler), `(device) => GPUTexture | GPUTextureView`
|
|
7
|
+
* (made once per device, so it is back after a device restore; a plain texture is lost with its device, reads white and is
|
|
8
|
+
* reported once through `engine.onError`, #borrowed-lost), or `{texture, sampler}` to
|
|
9
|
+
* give a GPUTexture its sampler (a shadow sampler's `compare` defaults to `'less-equal'`, GL's LEQUAL). A struct or
|
|
10
|
+
* block takes an object of its fields; an array a list of its elements (numbers of scalar and vector elements may also
|
|
11
|
+
* run on in one flat list); a sampler array a list of textures.
|
|
12
|
+
*/
|
|
13
|
+
export type ShaderUniformValue = number | boolean | string | ArrayLike<number | boolean> | {
|
|
14
|
+
readonly x: number;
|
|
15
|
+
readonly y: number;
|
|
16
|
+
readonly z?: number;
|
|
17
|
+
readonly w?: number;
|
|
18
|
+
} | RawTextureInput | {
|
|
19
|
+
readonly texture: RawTextureInput | null;
|
|
20
|
+
readonly sampler?: GPUSamplerDescriptor;
|
|
21
|
+
} | readonly ShaderUniformValue[] | {
|
|
22
|
+
readonly [field: string]: ShaderUniformValue;
|
|
23
|
+
} | null;
|
|
24
|
+
/** A live uniform in the `{value}` form web engines use: assign `value` any time. */
|
|
25
|
+
export interface ShaderUniform {
|
|
26
|
+
value: ShaderUniformValue;
|
|
27
|
+
}
|
|
28
|
+
/** Which faces draw, by GL's counter-clockwise front: `'front'`, `'back'` or `'double'`. */
|
|
29
|
+
export type ShaderSide = 'front' | 'back' | 'double';
|
|
30
|
+
/** A depth comparison in GL's sense (nearer is less), mapped onto Margo's reversed Z. */
|
|
31
|
+
export type ShaderDepthFunc = GPUCompareFunction;
|
|
32
|
+
/** Options for `new ShaderMaterial(options)`. Render state fields stay live on the material, as on RawShaderMaterial. */
|
|
33
|
+
export interface ShaderMaterialOptions extends Omit<GLSLProgramSource, 'uniformNames'>, Omit<RawShaderMaterialOptions, 'vertexShader' | 'fragmentShader' | 'vertexEntry' | 'fragmentEntry' | 'uniforms' | 'textures' | 'storage' | 'groups' | 'gbufferEntry' | 'velocityEntry' | 'blending' | 'depthCompare'> {
|
|
34
|
+
/**
|
|
35
|
+
* Live values by GLSL name; the types come from the GLSL. A uniform the program declares and this omits starts at
|
|
36
|
+
* zero, unless it is a built-in of `builtins`: the engine fills those, and a name given here is yours instead.
|
|
37
|
+
*/
|
|
38
|
+
uniforms?: Record<string, ShaderUniform>;
|
|
39
|
+
/** Faces to draw. Default `'front'`. Give this or `cull`, not both. */
|
|
40
|
+
side?: ShaderSide;
|
|
41
|
+
/**
|
|
42
|
+
* A preset (`'none'`, `'normal'`, `'additive'`, `'subtractive'`, `'multiply'`, `'premultiplied'`), a GPUBlendState,
|
|
43
|
+
* or `'custom'`, built from `blendSrc`, `blendDst`, `blendEquation` and their alpha forms. Default `'normal'`.
|
|
44
|
+
*/
|
|
45
|
+
blending?: RawShaderMaterialOptions['blending'] | 'custom';
|
|
46
|
+
/** Source factor of `'custom'` blending. Default `'src-alpha'`. */
|
|
47
|
+
blendSrc?: GPUBlendFactor;
|
|
48
|
+
/** Destination factor of `'custom'` blending. Default `'one-minus-src-alpha'`. */
|
|
49
|
+
blendDst?: GPUBlendFactor;
|
|
50
|
+
/** Operation of `'custom'` blending. Default `'add'`. */
|
|
51
|
+
blendEquation?: GPUBlendOperation;
|
|
52
|
+
/** Alpha source factor of `'custom'` blending. Default `blendSrc`. */
|
|
53
|
+
blendSrcAlpha?: GPUBlendFactor;
|
|
54
|
+
/** Alpha destination factor of `'custom'` blending. Default `blendDst`. */
|
|
55
|
+
blendDstAlpha?: GPUBlendFactor;
|
|
56
|
+
/** Alpha operation of `'custom'` blending. Default `blendEquation`. */
|
|
57
|
+
blendEquationAlpha?: GPUBlendOperation;
|
|
58
|
+
/** The depth test in GL's sense (`'less'` passes nearer fragments). Default `'less-equal'`. */
|
|
59
|
+
depthFunc?: ShaderDepthFunc;
|
|
60
|
+
/** Offset depth by `polygonOffsetFactor` times the slope plus `polygonOffsetUnits` steps, as GL's polygon offset. Default false. */
|
|
61
|
+
polygonOffset?: boolean;
|
|
62
|
+
/** Default 0; positive moves away from the camera, as in GL. */
|
|
63
|
+
polygonOffsetFactor?: number;
|
|
64
|
+
/** Default 0; positive moves away from the camera, as in GL. */
|
|
65
|
+
polygonOffsetUnits?: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @internal What ShaderMaterial adds to the program core: the scene part of linking, and a {value} for each scene
|
|
69
|
+
* uniform the program reads (read through `holder.material`, the material once made).
|
|
70
|
+
*/
|
|
71
|
+
export interface GLSLMaterialScene {
|
|
72
|
+
readonly link: GLSLSceneLinker;
|
|
73
|
+
/** Replaces the scene uniforms in `values` with the ones `linked` reads, unless the caller gave that name. */
|
|
74
|
+
uniforms(linked: GLSLLinked, values: Record<string, ShaderUniform>, holder: {
|
|
75
|
+
material?: GLSLMaterial;
|
|
76
|
+
}): void;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A material from a whole GLSL program, as a web engine's `RawShaderMaterial` takes one: GLSL ES 1.00 or 3.00 vertex and
|
|
80
|
+
* fragment stages with `void main()`, live `uniforms` by GLSL name, `defines` and the render state of
|
|
81
|
+
* RawShaderMaterial. Draw it with a RawMesh in a RawShaders plugin; attributes match by name. The GLSL is translated
|
|
82
|
+
* here, once per source and defines (syntax and link errors throw here, with `stage`, `line` and `column`), and
|
|
83
|
+
* `material.code(stage, mesh)` shows the WGSL each stage becomes. Use ShaderMaterial of shader/glsl-material, which is this
|
|
84
|
+
* with the scene options `lights`, `fog` and `velocity`; ShaderPass and Shadertoy draw with this core alone.
|
|
85
|
+
*/
|
|
86
|
+
export declare class GLSLMaterial extends RawShaderMaterial<ShaderUniform> {
|
|
87
|
+
/** True on every ShaderMaterial, for code that tells materials apart without importing the class. */
|
|
88
|
+
readonly isShaderMaterial = true;
|
|
89
|
+
/** Live values by GLSL name: `material.uniforms.time.value = 1`. */
|
|
90
|
+
readonly uniforms: Record<string, ShaderUniform>;
|
|
91
|
+
private linked;
|
|
92
|
+
private source;
|
|
93
|
+
/** @param scene The scene options' code (ShaderMaterial passes it). */
|
|
94
|
+
constructor(options: ShaderMaterialOptions, scene?: GLSLMaterialScene);
|
|
95
|
+
private offset;
|
|
96
|
+
private applyOffset;
|
|
97
|
+
/** Faces drawn: `'front'` (cull back), `'back'` (cull front) or `'double'`. Live. */
|
|
98
|
+
get side(): ShaderSide;
|
|
99
|
+
set side(side: ShaderSide);
|
|
100
|
+
/** The depth test in GL's sense; reads and writes `depthCompare` (reversed Z). Live. */
|
|
101
|
+
get depthFunc(): ShaderDepthFunc;
|
|
102
|
+
set depthFunc(f: ShaderDepthFunc);
|
|
103
|
+
/** GL's polygon offset; sets `depthBias` and `depthBiasSlopeScale`. Live. */
|
|
104
|
+
get polygonOffset(): boolean;
|
|
105
|
+
set polygonOffset(on: boolean);
|
|
106
|
+
get polygonOffsetFactor(): number;
|
|
107
|
+
set polygonOffsetFactor(v: number);
|
|
108
|
+
get polygonOffsetUnits(): number;
|
|
109
|
+
set polygonOffsetUnits(v: number);
|
|
110
|
+
/** The linked program's tables: uniforms, textures, attributes (with what each holds), varyings and the convention. */
|
|
111
|
+
get program(): GLSLProgramWGSL;
|
|
112
|
+
private holder;
|
|
113
|
+
private scene?;
|
|
114
|
+
private static parts;
|
|
115
|
+
private publish;
|
|
116
|
+
/** The GLSL of the vertex stage; assigning translates it again. */
|
|
117
|
+
get vertexShader(): string;
|
|
118
|
+
set vertexShader(code: string);
|
|
119
|
+
/** The GLSL of the fragment stage; assigning translates it again. */
|
|
120
|
+
get fragmentShader(): string;
|
|
121
|
+
set fragmentShader(code: string);
|
|
122
|
+
/** The macros defined before both stages. Assigning a new object translates again; after editing it, set `needsUpdate`. */
|
|
123
|
+
get defines(): Record<string, string | number | boolean>;
|
|
124
|
+
set defines(defines: Record<string, string | number | boolean>);
|
|
125
|
+
/** Setting true translates the program again with the current `defines`. */
|
|
126
|
+
set needsUpdate(value: boolean);
|
|
127
|
+
/** The WGSL of `stage` for `mesh`: the prelude, the translated GLSL and the entry for the mesh's attributes. */
|
|
128
|
+
code(stage: 'vertex' | 'fragment', mesh?: RawMesh): string;
|
|
129
|
+
private relink;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Geometry for {@link shaderMesh}: plain arrays by stream (a `GeometryData` of `@margoengine/geometry` is one), or a
|
|
133
|
+
* core `Geometry` (its interleaved `vertices` of position and normal, and `indices`).
|
|
134
|
+
*/
|
|
135
|
+
export interface ShaderGeometry {
|
|
136
|
+
/** xyz per vertex. */
|
|
137
|
+
readonly positions?: ArrayLike<number>;
|
|
138
|
+
/** xyz per vertex. */
|
|
139
|
+
readonly normals?: ArrayLike<number>;
|
|
140
|
+
/** uv per vertex. */
|
|
141
|
+
readonly uvs?: ArrayLike<number>;
|
|
142
|
+
/** A second uv per vertex. */
|
|
143
|
+
readonly uvs1?: ArrayLike<number>;
|
|
144
|
+
/** rgb or rgba per vertex. */
|
|
145
|
+
readonly colors?: ArrayLike<number>;
|
|
146
|
+
/** xyzw per vertex (w the bitangent sign). */
|
|
147
|
+
readonly tangents?: ArrayLike<number>;
|
|
148
|
+
/** A core Geometry's position and normal per vertex, six numbers each. */
|
|
149
|
+
readonly vertices?: ArrayLike<number>;
|
|
150
|
+
readonly indices?: ArrayLike<number>;
|
|
151
|
+
}
|
|
152
|
+
/** Options for {@link shaderMesh}: RawMesh options plus the streams the geometry lacks, by GLSL attribute name. */
|
|
153
|
+
export interface ShaderMeshOptions extends Omit<RawMeshOptions, 'material' | 'attributes' | 'index'> {
|
|
154
|
+
/** Per-vertex data by GLSL attribute name, sized by its GLSL type (a mat4 takes 16 numbers a vertex). */
|
|
155
|
+
attributes?: Record<string, ArrayLike<number>>;
|
|
156
|
+
/**
|
|
157
|
+
* Per-instance data by GLSL attribute name or by what it holds (`instanceMatrix`: 16 numbers an instance, column
|
|
158
|
+
* major; `instanceColor`: 3). The instance count defaults to the number of elements.
|
|
159
|
+
*/
|
|
160
|
+
instances?: Record<string, ArrayLike<number>>;
|
|
161
|
+
/** Triangle indices; default the geometry's. */
|
|
162
|
+
index?: RawMeshOptions['index'];
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* A RawMesh for `material` from `geometry`: each attribute the program declares takes the geometry stream it holds
|
|
166
|
+
* (by built-in name, by the material's `attributes` map, or by its own name: position, normal, uv, uv1, color,
|
|
167
|
+
* tangent), or the data in `options.attributes` and `options.instances` under its GLSL name. One the mesh lacks reads
|
|
168
|
+
* its default value (`defaultAttributeValues`). A matrix attribute is split into one vector attribute per column.
|
|
169
|
+
* The mesh is culled by the sphere around its positions, except under `builtins: false` (the program then places
|
|
170
|
+
* points with its own matrices, so `frustumCulled` defaults to false).
|
|
171
|
+
* @throws TypeError or RangeError for data of the wrong length or a stream count that differs between attributes.
|
|
172
|
+
*/
|
|
173
|
+
export declare function shaderMesh<M extends GLSLMaterial>(geometry: ShaderGeometry, material: M, options?: ShaderMeshOptions): RawMesh<M>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/** Parsed expression with explicit operator precedence. */
|
|
2
|
+
export interface Expression {
|
|
3
|
+
op: string;
|
|
4
|
+
value: string;
|
|
5
|
+
args: Expression[];
|
|
6
|
+
}
|
|
7
|
+
/** Typed variable or parameter declaration. */
|
|
8
|
+
export interface Declaration {
|
|
9
|
+
name: string;
|
|
10
|
+
type: string;
|
|
11
|
+
qualifier: string;
|
|
12
|
+
init?: Expression;
|
|
13
|
+
}
|
|
14
|
+
/** Structured statement before WGSL control-flow lowering. */
|
|
15
|
+
export interface Statement {
|
|
16
|
+
kind: string;
|
|
17
|
+
expr?: Expression;
|
|
18
|
+
decls?: Declaration[];
|
|
19
|
+
body?: Statement[];
|
|
20
|
+
other?: Statement[];
|
|
21
|
+
init?: Statement;
|
|
22
|
+
update?: Expression;
|
|
23
|
+
/** A switch's clauses in source order: their folded integer labels (null for default) and statements. */
|
|
24
|
+
cases?: {
|
|
25
|
+
labels: (number | null)[];
|
|
26
|
+
body: Statement[];
|
|
27
|
+
}[];
|
|
28
|
+
}
|
|
29
|
+
/** Function definition or prototype. */
|
|
30
|
+
export interface FunctionDeclaration {
|
|
31
|
+
name: string;
|
|
32
|
+
type: string;
|
|
33
|
+
params: Declaration[];
|
|
34
|
+
body?: Statement[];
|
|
35
|
+
}
|
|
36
|
+
/** One parsed transition translation unit. */
|
|
37
|
+
export interface Program {
|
|
38
|
+
globals: Declaration[];
|
|
39
|
+
functions: FunctionDeclaration[];
|
|
40
|
+
structs: Map<string, Declaration[]>;
|
|
41
|
+
}
|
|
42
|
+
/** Tokenizes and parses GLSL declarations, expressions and structured control flow. No GPU work. */
|
|
43
|
+
export declare class GLSLParser {
|
|
44
|
+
private readonly source;
|
|
45
|
+
private readonly subject;
|
|
46
|
+
private tokens;
|
|
47
|
+
private offsets;
|
|
48
|
+
private i;
|
|
49
|
+
private depth;
|
|
50
|
+
private readonly known;
|
|
51
|
+
private readonly globals;
|
|
52
|
+
private readonly constants;
|
|
53
|
+
/**
|
|
54
|
+
* `subject` names the source in messages ("transition" by default); a syntax error carries the `line` and `column`
|
|
55
|
+
* (1-based) of its token in `source`.
|
|
56
|
+
*/
|
|
57
|
+
constructor(source: string, subject?: string);
|
|
58
|
+
private peek;
|
|
59
|
+
private take;
|
|
60
|
+
private match;
|
|
61
|
+
private expect;
|
|
62
|
+
private fail;
|
|
63
|
+
private identifier;
|
|
64
|
+
private qualified;
|
|
65
|
+
private array;
|
|
66
|
+
private fold;
|
|
67
|
+
private number;
|
|
68
|
+
private declarations;
|
|
69
|
+
private uniformBlock;
|
|
70
|
+
parse(): Program;
|
|
71
|
+
private block;
|
|
72
|
+
private statement;
|
|
73
|
+
private simple;
|
|
74
|
+
private constructs;
|
|
75
|
+
private expression;
|
|
76
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** What preprocessing found: the GLSL ES version the source declares (100 when it declares none). */
|
|
2
|
+
export interface GLSLSourceInfo {
|
|
3
|
+
version: 100 | 300;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Minimal C-style GLSL preprocessing, including function macros and conditional compilation. Accepts `#version 100`
|
|
7
|
+
* and `#version 300 es` (GLSL ES 3.00 3.3); the version found is written to `info`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function preprocessGLSL(source: string, info?: GLSLSourceInfo): string;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** What an attribute holds, for `shaderMesh`: a geometry stream or an instance stream. */
|
|
2
|
+
export type GLSLAttributeSemantic = 'position' | 'normal' | 'uv' | 'uv1' | 'color' | 'tangent' | 'instanceMatrix' | 'instanceColor';
|
|
3
|
+
/** A convention's name, `'auto'` (the one the program's names show) or false (none). */
|
|
4
|
+
export type GLSLBuiltins = 'modelViewMatrix' | 'worldViewProjection' | 'matrix_model' | 'auto' | false;
|
|
5
|
+
/** @internal A uniform the engine supplies: its GLSL type, the WGSL that reads it and the stages that may use it undeclared. */
|
|
6
|
+
export interface GLSLPresetUniform {
|
|
7
|
+
readonly glsl: string;
|
|
8
|
+
readonly code: string;
|
|
9
|
+
readonly implicit?: readonly ('vertex' | 'fragment')[];
|
|
10
|
+
readonly helpers?: readonly string[];
|
|
11
|
+
}
|
|
12
|
+
/** @internal An attribute a convention names: its GLSL type, what it holds and its value when the mesh lacks it. */
|
|
13
|
+
export interface GLSLPresetAttribute {
|
|
14
|
+
readonly glsl: string;
|
|
15
|
+
readonly semantic: GLSLAttributeSemantic;
|
|
16
|
+
readonly implicit?: boolean;
|
|
17
|
+
readonly default?: readonly number[];
|
|
18
|
+
}
|
|
19
|
+
/** @internal */
|
|
20
|
+
export interface GLSLPreset {
|
|
21
|
+
readonly uniforms: Readonly<Record<string, GLSLPresetUniform>>;
|
|
22
|
+
readonly attributes: Readonly<Record<string, GLSLPresetAttribute>>;
|
|
23
|
+
}
|
|
24
|
+
/** @internal */
|
|
25
|
+
export declare const semantics: readonly GLSLAttributeSemantic[];
|
|
26
|
+
/** @internal */
|
|
27
|
+
export declare const presetNames: readonly ['modelViewMatrix', 'worldViewProjection', 'matrix_model'];
|
|
28
|
+
/** @internal */
|
|
29
|
+
export declare const glProjection = "fn margoGLProjection()->mat4x4f{var p=camera.projection;for(var c=0;c<4;c++){p[c].z=p[c].w-2.0*p[c].z;}return p;}\n";
|
|
30
|
+
/** @internal */
|
|
31
|
+
export declare const viewInverse: string;
|
|
32
|
+
/** @internal Each convention's names; `gl` false keeps Margo's projection (WebGPU clip space). */
|
|
33
|
+
export declare const presets: Record<(typeof presetNames)[number], (gl: boolean) => GLSLPreset>;
|