@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
package/dist/pass.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
2
|
+
import type { Engine, EnginePlugin, FrameGraph, PassContext } from '@margoengine/core/extension';
|
|
3
|
+
import { NodeMaterial, type NodeMaterialOptions } from './material.js';
|
|
4
|
+
import { type Node, type NodeInput } from './node.js';
|
|
5
|
+
import { type BindableTexture } from './texture.js';
|
|
6
|
+
type Context = Extract<PassContext, {
|
|
7
|
+
backend: 'webgpu';
|
|
8
|
+
}>;
|
|
9
|
+
/** Options for {@link PassMaterial}: a NodeMaterial's, plus what it writes to each output of a {@link ScenePass}. */
|
|
10
|
+
export interface PassMaterialOptions extends NodeMaterialOptions {
|
|
11
|
+
/** A `vec4f` node per output name. Outputs left out read as transparent black; the name `color` is the lit colour. */
|
|
12
|
+
outputs?: Record<string, Node<'vec4f'>>;
|
|
13
|
+
}
|
|
14
|
+
/** A NodeMaterial with extra outputs. It draws as usual in the frame; a {@link ScenePass} it is added to receives `outputs`. */
|
|
15
|
+
export declare class PassMaterial extends NodeMaterial {
|
|
16
|
+
private readonly lines;
|
|
17
|
+
private readonly declared;
|
|
18
|
+
private readonly patchedOutputs;
|
|
19
|
+
constructor(options?: PassMaterialOptions);
|
|
20
|
+
shader(source: string, group: number): string;
|
|
21
|
+
}
|
|
22
|
+
/** What gltf/pbr's `PBRCapture` needs; the model side of it. */
|
|
23
|
+
interface CaptureHook {
|
|
24
|
+
declarations(code: string): string;
|
|
25
|
+
readonly targets: readonly GPUColorTargetState[];
|
|
26
|
+
begin(): GPURenderPassEncoder;
|
|
27
|
+
}
|
|
28
|
+
/** A model a {@link ScenePass} draws: a gltf/pbr model or a mesh from `createMesh`. */
|
|
29
|
+
export interface ScenePassModel {
|
|
30
|
+
setCapture(hook: CaptureHook): () => void;
|
|
31
|
+
onDispose(callback: () => void): () => void;
|
|
32
|
+
draw(context: Context, mode: 'capture', color: GPUTextureView, depth: GPUTextureView): void;
|
|
33
|
+
}
|
|
34
|
+
/** Options for {@link ScenePass.attach}. */
|
|
35
|
+
export interface ScenePassOptions {
|
|
36
|
+
/** A texture format per output name, for example `{glow: 'rgba16float', mask: 'rgba8unorm'}`. The name `color` holds the lit colour. */
|
|
37
|
+
outputs: Record<string, GPUTextureFormat>;
|
|
38
|
+
/** Frame graph order: below 1 runs before the colour pass, so surfaces drawn in it can sample this pass. Default 0.5. */
|
|
39
|
+
order?: number;
|
|
40
|
+
}
|
|
41
|
+
/** Draws its models into textures of their own before the frame's colour pass. Read them with `texture(pass.texture(name), screenUV)`. */
|
|
42
|
+
export declare class ScenePass implements EnginePlugin {
|
|
43
|
+
readonly name: string;
|
|
44
|
+
readonly order: number;
|
|
45
|
+
private readonly names;
|
|
46
|
+
private readonly formats;
|
|
47
|
+
private readonly hook;
|
|
48
|
+
private readonly models;
|
|
49
|
+
private readonly views;
|
|
50
|
+
private readonly bound;
|
|
51
|
+
private readonly tails;
|
|
52
|
+
private readonly previous;
|
|
53
|
+
private textures;
|
|
54
|
+
private depth?;
|
|
55
|
+
private depthView?;
|
|
56
|
+
private sampler?;
|
|
57
|
+
private device?;
|
|
58
|
+
private width;
|
|
59
|
+
private height;
|
|
60
|
+
private depthFormat;
|
|
61
|
+
private context?;
|
|
62
|
+
private pass?;
|
|
63
|
+
private last?;
|
|
64
|
+
private disposed;
|
|
65
|
+
private engine?;
|
|
66
|
+
/** Makes the pass and adds it to `engine`. */
|
|
67
|
+
static attach(engine: EngineKernel, options: ScenePassOptions): ScenePass;
|
|
68
|
+
constructor({ outputs, order }: ScenePassOptions);
|
|
69
|
+
/** Module-scope WGSL for the capture shader of `code`: a variable per output a material did not declare, and the outputs. */
|
|
70
|
+
private tail;
|
|
71
|
+
private begin;
|
|
72
|
+
/** Draws `model` in this pass from the next frame, beside any SSR, SSGI or other ScenePass that captures it. Returns a remover. */
|
|
73
|
+
add(model: ScenePassModel): () => void;
|
|
74
|
+
/** Output `name` as a texture for `texture()` in a NodeMaterial or NodePass. Linear, the size of the drawing buffer. */
|
|
75
|
+
texture(name: string): BindableTexture;
|
|
76
|
+
/**
|
|
77
|
+
* How far a surface moved on screen since the last frame, in UV units: camera motion from the matrices of both frames.
|
|
78
|
+
* A moving mesh gives where it was as `previous`, a world position. Write it to an output with `vec4(pass.velocity(), 0, 1)`.
|
|
79
|
+
*/
|
|
80
|
+
velocity(previous?: NodeInput<'vec3f'>): Node<'vec2f'>;
|
|
81
|
+
setup(engine: Engine): void;
|
|
82
|
+
registerPasses(graph: FrameGraph): void;
|
|
83
|
+
private resize;
|
|
84
|
+
private render;
|
|
85
|
+
private release;
|
|
86
|
+
/** Frees the textures and lets go of the models; `engine.removePlugin(pass.name)` calls it. */
|
|
87
|
+
dispose(): void;
|
|
88
|
+
[Symbol.dispose](): void;
|
|
89
|
+
}
|
|
90
|
+
/** What {@link sceneColor} returns. */
|
|
91
|
+
export interface SceneColor {
|
|
92
|
+
/** The frame as it stands, in the format it is drawn in, for `texture()`. Display-encoded without an HDR PostStack. */
|
|
93
|
+
readonly texture: BindableTexture;
|
|
94
|
+
/** The frame at `coord` (default the pixel itself) in linear light, whatever the target encodes. */
|
|
95
|
+
read(coord?: NodeInput<'vec2f'>): Node<'vec3f'>;
|
|
96
|
+
/** Lets go of the shared snapshot; the frame stops paying for it with the last holder. */
|
|
97
|
+
dispose(): void;
|
|
98
|
+
[Symbol.dispose](): void;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The finished frame behind a surface: core meshes, sky, water, fog and every plugin drawn so far, resolved from MSAA, in the
|
|
102
|
+
* snapshot `@margoengine/materials` transmission uses. It is taken after the opaque phase, so a surface reading it is
|
|
103
|
+
* `transparent: true`, which draws after the snapshot; other blended surfaces are not in it.
|
|
104
|
+
* @example new NodeMaterial({ transparent: true, toneMapped: false, emissiveNode: sceneColor(engine).read(screenUV.add(wobble)) })
|
|
105
|
+
* @throws MargoError on an engine without WebGPU.
|
|
106
|
+
*/
|
|
107
|
+
export declare function sceneColor(engine: EngineKernel): SceneColor;
|
|
108
|
+
export {};
|
package/dist/pass.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { Et as e, K as t, Ot as n, Rt as r, T as i, en as a, ln as o, mn as s, sn as c, zt as l } from "./node-DzKv9CF2.js";
|
|
5
|
+
import { n as u } from "./texture-D6vy0bam.js";
|
|
6
|
+
import { n as d, t as f } from "./material-REzUrXtu.js";
|
|
7
|
+
import { t as p } from "./view-rect-Bm1ac-TK.js";
|
|
8
|
+
import { MargoError as m } from "@margoengine/math/errors";
|
|
9
|
+
import { acquireOpaqueColor as h } from "@margoengine/materials";
|
|
10
|
+
//#region ../shared/capture.js
|
|
11
|
+
var g = Symbol.for("margoengine.capture"), _ = (e) => e.current ?? e.users.values().next().value;
|
|
12
|
+
function v(e, t) {
|
|
13
|
+
let n = e[g];
|
|
14
|
+
if (n) n.users.add(t);
|
|
15
|
+
else {
|
|
16
|
+
let r = { users: /* @__PURE__ */ new Set() };
|
|
17
|
+
n = r, e[g] = r, r.users.add(t);
|
|
18
|
+
try {
|
|
19
|
+
r.restore = e.setCapture({
|
|
20
|
+
get targets() {
|
|
21
|
+
return _(r).targets;
|
|
22
|
+
},
|
|
23
|
+
declarations: (e) => _(r).declarations(e),
|
|
24
|
+
begin: () => _(r).begin()
|
|
25
|
+
});
|
|
26
|
+
} catch (t) {
|
|
27
|
+
throw delete e[g], t;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
let r = n;
|
|
31
|
+
return () => {
|
|
32
|
+
r.users.delete(t) && !r.users.size && (delete e[g], r.restore?.());
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function y(e, t, n) {
|
|
36
|
+
let r = e.get(t);
|
|
37
|
+
if (!r) {
|
|
38
|
+
let i = v(t, n), a = t.onDispose(() => e.delete(t));
|
|
39
|
+
r = () => {
|
|
40
|
+
e.get(t) === r && (e.delete(t), a(), i());
|
|
41
|
+
}, e.set(t, r);
|
|
42
|
+
}
|
|
43
|
+
return r;
|
|
44
|
+
}
|
|
45
|
+
function b(e, t, n) {
|
|
46
|
+
let r = e[g];
|
|
47
|
+
if (r?.users.has(t)) {
|
|
48
|
+
r.current = t;
|
|
49
|
+
try {
|
|
50
|
+
e.draw(n, "capture", n.color, n.depth);
|
|
51
|
+
} finally {
|
|
52
|
+
r.current = void 0;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/pass.ts
|
|
58
|
+
var x = (e) => "margoOut" + e, S = (e) => `var<private> ${x(e)}:vec4f;\n`, C = (e) => {
|
|
59
|
+
if (!/^[A-Za-z][A-Za-z0-9]*$/.test(e)) throw TypeError(`Output name "${e}" must be letters and digits, starting with a letter`);
|
|
60
|
+
}, w = class extends f {
|
|
61
|
+
constructor(e = {}) {
|
|
62
|
+
let { outputs: t = {}, ...n } = e, r = Object.keys(t);
|
|
63
|
+
r.forEach((e) => {
|
|
64
|
+
if (C(e), e === "color") throw TypeError("The color output is the lit colour; pick another name for a node");
|
|
65
|
+
i(t[e], "vec4f", `output ${e}`);
|
|
66
|
+
});
|
|
67
|
+
let a = "";
|
|
68
|
+
super(n, (e) => {
|
|
69
|
+
let n = e.stage(d, Object.values(t));
|
|
70
|
+
a = [...n.lines, ...n.values.map((e, t) => `${x(r[t])}=${e};`)].join("\n ");
|
|
71
|
+
}), this.patchedOutputs = /* @__PURE__ */ new Map(), this.lines = a, this.declared = r.map(S).join("");
|
|
72
|
+
}
|
|
73
|
+
shader(e, t) {
|
|
74
|
+
let n = super.shader(e, t);
|
|
75
|
+
if (!this.lines) return n;
|
|
76
|
+
let r = this.patchedOutputs.get(n);
|
|
77
|
+
if (r === void 0) {
|
|
78
|
+
let e = "\n return s;";
|
|
79
|
+
if (n.split(e).length !== 2) throw new m("unsupported", "PassMaterial requires the standard gltf/pbr shader; use matching @margoengine/gltf/pbr and shader versions");
|
|
80
|
+
r = this.declared + n.replace(e, () => `\n {\n ${this.lines}\n }${e}`), this.patchedOutputs.set(n, r);
|
|
81
|
+
}
|
|
82
|
+
return r;
|
|
83
|
+
}
|
|
84
|
+
}, T = 0, E = class t {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
85
|
+
static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
86
|
+
let r = new t(n);
|
|
87
|
+
return e.use(r), r;
|
|
88
|
+
}
|
|
89
|
+
constructor({ outputs: e, order: t = .5 }) {
|
|
90
|
+
if (this.name = "shader:scene:" + T++, this.models = /* @__PURE__ */ new Map(), this.views = /* @__PURE__ */ new Map(), this.bound = /* @__PURE__ */ new Map(), this.tails = /* @__PURE__ */ new Map(), this.previous = a(Array(16).fill(0), { type: "mat4x4f" }), this.textures = [], this.width = 0, this.height = 0, this.depthFormat = "", this.disposed = !1, this.names = Object.keys(e ?? {}), this.formats = Object.values(e ?? {}), !this.names.length) throw TypeError("ScenePass needs at least one output: {glow: \"rgba16float\"}");
|
|
91
|
+
this.names.forEach(C);
|
|
92
|
+
for (let e of this.formats) if (/32float|int$|depth|stencil/.test(e)) throw TypeError(`ScenePass output format ${e} must be a filterable colour format such as rgba16float`);
|
|
93
|
+
if (!Number.isFinite(t) || t <= 0) throw RangeError("ScenePass order must be a number > 0");
|
|
94
|
+
this.order = t, this.hook = {
|
|
95
|
+
targets: this.formats.map((e) => ({ format: e })),
|
|
96
|
+
declarations: (e) => this.tail(e),
|
|
97
|
+
begin: () => this.begin()
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
tail(e) {
|
|
101
|
+
let t = this.tails.get(e);
|
|
102
|
+
if (t === void 0) {
|
|
103
|
+
let n = this.names.includes("color");
|
|
104
|
+
t = this.names.filter((t) => t !== "color" && !e.includes(S(t))).map(S).join("") + (n ? "var<private> margoBeauty:vec4f;\n" : "") + `struct MargoCapture {${this.names.map((e, t) => `@location(${t}) o${t}:vec4f`).join(",")}}\nfn margoCapture(s:Surface,v:vec3f,k:vec3f)->MargoCapture {return MargoCapture(${this.names.map((e) => e === "color" ? "margoBeauty" : x(e)).join(",")});}\n`, this.tails.size >= 64 && this.tails.clear(), this.tails.set(e, t);
|
|
105
|
+
}
|
|
106
|
+
return t;
|
|
107
|
+
}
|
|
108
|
+
begin() {
|
|
109
|
+
if (this.pass) return this.pass;
|
|
110
|
+
let e = this.context, t = !e.view?.index, n = this.depthFormat.includes("stencil") ? {
|
|
111
|
+
stencilClearValue: 0,
|
|
112
|
+
stencilLoadOp: "clear",
|
|
113
|
+
stencilStoreOp: "store"
|
|
114
|
+
} : {}, r = this.pass = e.encoder.beginRenderPass({
|
|
115
|
+
label: this.name,
|
|
116
|
+
colorAttachments: this.names.map((e) => ({
|
|
117
|
+
view: this.views.get(e),
|
|
118
|
+
clearValue: [
|
|
119
|
+
0,
|
|
120
|
+
0,
|
|
121
|
+
0,
|
|
122
|
+
0
|
|
123
|
+
],
|
|
124
|
+
loadOp: t ? "clear" : "load",
|
|
125
|
+
storeOp: "store"
|
|
126
|
+
})),
|
|
127
|
+
depthStencilAttachment: {
|
|
128
|
+
view: this.depthView,
|
|
129
|
+
depthClearValue: 0,
|
|
130
|
+
depthLoadOp: t ? "clear" : "load",
|
|
131
|
+
depthStoreOp: "store",
|
|
132
|
+
...n
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
return p(r, e), r;
|
|
136
|
+
}
|
|
137
|
+
add(e) {
|
|
138
|
+
if (this.disposed) throw new m("disposed", "ScenePass is disposed; create a new ScenePass.");
|
|
139
|
+
return y(this.models, e, this.hook);
|
|
140
|
+
}
|
|
141
|
+
texture(e) {
|
|
142
|
+
if (!this.names.includes(e)) throw RangeError(`ScenePass has no output "${e}"; it has ${this.names.join(", ")}`);
|
|
143
|
+
let t = this.bound.get(e);
|
|
144
|
+
return t || (t = { bind: () => this.views.has(e) ? [this.views.get(e), this.sampler] : void 0 }, this.bound.set(e, t)), t;
|
|
145
|
+
}
|
|
146
|
+
velocity(t = e) {
|
|
147
|
+
let r = this.previous.mul(o(i(t, "vec3f", "previous position"), 1)), a = n.mul(s).mul(o(e, 1));
|
|
148
|
+
return a.xy.div(a.w).sub(r.xy.div(r.w)).mul(c(.5, -.5));
|
|
149
|
+
}
|
|
150
|
+
setup(e) {
|
|
151
|
+
if (this.disposed) throw new m("disposed", "ScenePass is disposed; create a new ScenePass.");
|
|
152
|
+
if (!e.gpu) throw new m("unsupported", "ScenePass requires WebGPU; see https://margoengine.com/errors#webgpu");
|
|
153
|
+
this.engine = e;
|
|
154
|
+
}
|
|
155
|
+
registerPasses(e) {
|
|
156
|
+
e.add({
|
|
157
|
+
name: this.name,
|
|
158
|
+
order: this.order,
|
|
159
|
+
execute: (e) => {
|
|
160
|
+
e.backend === "webgpu" && !this.disposed && this.render(e);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
resize(e) {
|
|
165
|
+
let t = e.descriptor.depthStencil.format;
|
|
166
|
+
if (this.device === e.device && this.width === e.width && this.height === e.height && this.depthFormat === t) return;
|
|
167
|
+
this.release(), this.device = e.device, this.width = e.width, this.height = e.height, this.depthFormat = t;
|
|
168
|
+
let n = GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING;
|
|
169
|
+
this.textures = this.formats.map((t, r) => e.device.createTexture({
|
|
170
|
+
label: `${this.name}:${this.names[r]}`,
|
|
171
|
+
size: [e.width, e.height],
|
|
172
|
+
format: t,
|
|
173
|
+
usage: n
|
|
174
|
+
})), this.textures.forEach((e, t) => this.views.set(this.names[t], e.createView())), this.depth = e.device.createTexture({
|
|
175
|
+
label: this.name + ":depth",
|
|
176
|
+
size: [e.width, e.height],
|
|
177
|
+
format: t,
|
|
178
|
+
usage: n
|
|
179
|
+
}), this.depthView = this.depth.createView(), this.sampler ??= e.device.createSampler({
|
|
180
|
+
minFilter: "linear",
|
|
181
|
+
magFilter: "linear"
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
render(e) {
|
|
185
|
+
this.resize(e), this.previous.value = this.last ?? Array.from(e.camera.matrix), this.last = Array.from(e.camera.matrix);
|
|
186
|
+
let t = Object.create(e, {
|
|
187
|
+
descriptor: { value: {
|
|
188
|
+
...e.descriptor,
|
|
189
|
+
multisample: { count: 1 }
|
|
190
|
+
} },
|
|
191
|
+
format: { value: "rgba16float" },
|
|
192
|
+
view: { value: e.view && { ...e.view } }
|
|
193
|
+
});
|
|
194
|
+
this.context = t;
|
|
195
|
+
let n = this.hook.begin();
|
|
196
|
+
try {
|
|
197
|
+
for (let e of this.models.keys()) b(e, this.hook, t);
|
|
198
|
+
} finally {
|
|
199
|
+
n.end(), this.pass = this.context = void 0;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
release() {
|
|
203
|
+
this.textures.forEach((e) => e.destroy()), this.depth?.destroy(), this.textures = [], this.depth = void 0, this.views.clear(), this.sampler = void 0;
|
|
204
|
+
}
|
|
205
|
+
dispose() {
|
|
206
|
+
if (this.disposed) return;
|
|
207
|
+
this.disposed = !0;
|
|
208
|
+
let e = this.engine;
|
|
209
|
+
this.engine = void 0;
|
|
210
|
+
for (let e of [...this.models.values()]) e();
|
|
211
|
+
this.release(), e?.sceneOf(this)?.removePlugin(this.name);
|
|
212
|
+
}
|
|
213
|
+
[Symbol.dispose]() {
|
|
214
|
+
this.dispose();
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
function D(e) {
|
|
218
|
+
let n = h(e, () => {}), a = /* @__PURE__ */ new WeakMap(), o = { bind(e) {
|
|
219
|
+
let t = a.get(e);
|
|
220
|
+
return t || a.set(e, t = e.createSampler({
|
|
221
|
+
minFilter: "linear",
|
|
222
|
+
magFilter: "linear"
|
|
223
|
+
})), [n.view, t];
|
|
224
|
+
} }, s = () => n.release();
|
|
225
|
+
return {
|
|
226
|
+
texture: o,
|
|
227
|
+
read(e = r) {
|
|
228
|
+
let n = u(o, i(e, "vec2f", "sceneColor coordinate")).rgb;
|
|
229
|
+
return l(t.greaterThan(.5), n, l(n.lessThanEqual(.04045), n.div(12.92), n.add(.055).div(1.055).pow(2.4)));
|
|
230
|
+
},
|
|
231
|
+
dispose: s,
|
|
232
|
+
[Symbol.dispose]: s
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
//#endregion
|
|
236
|
+
export { w as PassMaterial, E as ScenePass, D as sceneColor };
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { Pt as e, T as t, l as n, o as r } from "./node-DzKv9CF2.js";
|
|
5
|
+
import { t as i } from "./clock-BLGNexlt.js";
|
|
6
|
+
import { n as a, t as o } from "./view-rect-Bm1ac-TK.js";
|
|
7
|
+
import { n as s, r as c } from "./gpu-DQ5Jmggz.js";
|
|
8
|
+
import { t as l } from "./display-BrJM_Qkd.js";
|
|
9
|
+
import { t as u } from "./own-stack-D1XnXrrz.js";
|
|
10
|
+
import { MargoError as d } from "@margoengine/math/errors";
|
|
11
|
+
//#region src/post.ts
|
|
12
|
+
var f = (e, t) => ({
|
|
13
|
+
prefix: "nd" + t[0],
|
|
14
|
+
bindings: 1,
|
|
15
|
+
declare: (t, n, r) => `@group(${n}) @binding(${r}) var ${t}:${e};`,
|
|
16
|
+
entries: (e, n) => [n[t]]
|
|
17
|
+
}), p = /*@__PURE__*/ f("texture_2d<f32>", "input"), m = /*@__PURE__*/ f("sampler", "sampler"), h = /*@__PURE__*/ f("texture_depth_2d", "depth"), g = {
|
|
18
|
+
kind: "post",
|
|
19
|
+
builtins: {
|
|
20
|
+
uv: "v.uv",
|
|
21
|
+
screenUV: "v.uv",
|
|
22
|
+
screenCoordinate: "v.position.xy"
|
|
23
|
+
}
|
|
24
|
+
}, _ = (e) => {
|
|
25
|
+
if (e.stage.kind !== "post") throw TypeError("inputColor() and inputDepth() are only available in NodePass nodes");
|
|
26
|
+
return `vec2i(${e.local("v.position.xy")})`;
|
|
27
|
+
};
|
|
28
|
+
function v(e) {
|
|
29
|
+
return new r("vec4f", "ic", e === void 0 ? [] : [t(e, "vec2f", "inputColor uv")], (e, t) => {
|
|
30
|
+
let n = _(e), r = e.resource(p);
|
|
31
|
+
return t.args.length ? `textureSampleLevel(${r}, ${e.resource(m)}, ${e.expr(t.args[0])}, 0.0)` : `textureLoad(${r}, ${n}, 0)`;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
function y(e) {
|
|
35
|
+
return new r("f32", "id", e === void 0 ? [] : [t(e, "vec2f", "inputDepth uv")], (e, t) => {
|
|
36
|
+
let n = _(e), r = e.resource(h);
|
|
37
|
+
return `textureLoad(${r}, ${t.args.length ? `vec2i(${e.wrap(t.args[0])} * vec2f(textureDimensions(${r})))` : n}, 0)`;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
var b = 0, x = class t {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
41
|
+
static async attach(e, n, r = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
42
|
+
let { signal: i, ...a } = r;
|
|
43
|
+
i?.throwIfAborted();
|
|
44
|
+
let o = new t(n, a);
|
|
45
|
+
return o.stack = await u(e, o, i), o;
|
|
46
|
+
}
|
|
47
|
+
constructor(e, { kind: t = "ldr", name: i = "shader:pass:" + b++ } = {}) {
|
|
48
|
+
if (this.node = e, this.program = new n(0), this.disposed = !1, this.encoded = !1, this.attachment = {
|
|
49
|
+
view: void 0,
|
|
50
|
+
loadOp: "clear",
|
|
51
|
+
storeOp: "store"
|
|
52
|
+
}, this.descriptor = { colorAttachments: [this.attachment] }, this.last = [], !(e instanceof r) || e.type !== "vec3f" && e.type !== "vec4f") throw TypeError("NodePass needs a vec3f or vec4f node");
|
|
53
|
+
if (t !== "hdr" && t !== "ldr") throw TypeError("NodePass kind must be 'hdr' or 'ldr'");
|
|
54
|
+
this.name = i, this.kind = t;
|
|
55
|
+
let a = this.program.stage(g, [e]);
|
|
56
|
+
this.body = a.lines.map((e) => " " + e + "\n").join(""), this.value = e.type === "vec3f" ? `vec4f(${a.values[0]}, 1.0)` : a.values[0];
|
|
57
|
+
}
|
|
58
|
+
code(e = !1) {
|
|
59
|
+
let t = e ? `let c = ${this.value};\n let x = pbrNeutral(max(c.rgb, vec3f(0.0)));\n return vec4f(select(x * 12.92, 1.055 * pow(x, vec3f(1.0 / 2.4)) - 0.055, x >= vec3f(0.0031308)), c.a);` : `return ${this.value};`;
|
|
60
|
+
return `${s}\n${this.program.declare(0)}\n${e ? l : ""}@fragment fn fragment(v:Vertex)->@location(0) vec4f {\n${this.body} ${t}\n}\n`;
|
|
61
|
+
}
|
|
62
|
+
joinStack(e) {
|
|
63
|
+
if (this.host || this.disposed) throw new d(this.disposed ? "disposed" : "attached", `${this.name} is already attached; create a new NodePass for another PostStack`);
|
|
64
|
+
return this.host = e, {
|
|
65
|
+
kind: this.kind,
|
|
66
|
+
render: (e, t, n) => this.render(e, t, n)
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
setup(e) {
|
|
70
|
+
if (!this.host || this.disposed) throw new d("state", "NodePass runs inside a PostStack: PostStack.attach(engine, [pass])");
|
|
71
|
+
if (!e.gpu) throw new d("unsupported", "NodePass requires WebGPU; use a WebGPU engine");
|
|
72
|
+
this.engine = e, i.hold(e, this);
|
|
73
|
+
}
|
|
74
|
+
registerPasses() {}
|
|
75
|
+
render(t, n, r) {
|
|
76
|
+
let s = t.device, l = this.kind === "hdr" && !r.hdr;
|
|
77
|
+
(!this.pipeline || this.format !== r.format || this.samples !== r.multisample.count || this.encoded !== l) && (this.format = r.format, this.samples = r.multisample.count, this.encoded = l, this.pipeline = c(s, this.code(l), r.format, { multisample: r.multisample }), this.group = void 0), this.sampler ??= s.createSampler({
|
|
78
|
+
magFilter: "linear",
|
|
79
|
+
minFilter: "linear"
|
|
80
|
+
});
|
|
81
|
+
let u = this.host.depthTexture;
|
|
82
|
+
u !== this.depthTexture && (this.depthTexture = u, this.depthView = u?.createView({ aspect: "depth-only" }));
|
|
83
|
+
let d = this.program.entries(s, {
|
|
84
|
+
width: t.width,
|
|
85
|
+
height: t.height,
|
|
86
|
+
time: i.find(this.engine)?.time ?? 0,
|
|
87
|
+
delta: t.delta,
|
|
88
|
+
input: n,
|
|
89
|
+
sampler: this.sampler,
|
|
90
|
+
depth: this.depthView
|
|
91
|
+
});
|
|
92
|
+
(!this.group || !e(d, this.last)) && (this.last = d, this.group = d.length ? s.createBindGroup({
|
|
93
|
+
layout: this.pipeline.getBindGroupLayout(0),
|
|
94
|
+
entries: d
|
|
95
|
+
}) : void 0), this.attachment.view = r.view, this.attachment.loadOp = a(t);
|
|
96
|
+
let f = t.encoder.beginRenderPass(this.descriptor);
|
|
97
|
+
o(f, t), f.setPipeline(this.pipeline), this.group && f.setBindGroup(0, this.group), f.draw(3), f.end();
|
|
98
|
+
}
|
|
99
|
+
onDeviceRestored() {
|
|
100
|
+
this.format = void 0, this.samples = void 0, this.encoded = !1, this.sampler = this.group = this.pipeline = this.depthTexture = this.depthView = void 0;
|
|
101
|
+
}
|
|
102
|
+
dispose() {
|
|
103
|
+
if (this.disposed) return;
|
|
104
|
+
this.disposed = !0;
|
|
105
|
+
let e = this.engine, t = this.stack;
|
|
106
|
+
this.engine = this.stack = void 0, t?.removeEffect(this), i.release(e, this), e?.sceneOf(this)?.removePlugin(this.name), this.onDeviceRestored();
|
|
107
|
+
}
|
|
108
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
109
|
+
this.dispose();
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
//#endregion
|
|
113
|
+
export { v as n, y as r, x as t };
|
package/dist/post.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import type { Engine, EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
import { Node, Program, type NodeInput } from './node.js';
|
|
5
|
+
/** The effect's input colour at this pixel, or bilinear at `coord` (0-1, origin top left). Linear HDR for `kind: 'hdr'`, display-encoded for `'ldr'`. */
|
|
6
|
+
export declare function inputColor(coord?: NodeInput<'vec2f'>): Node<'vec4f'>;
|
|
7
|
+
/** Raw scene depth at this pixel or at `coord` (reversed Z: 1 near, 0 far or sky). */
|
|
8
|
+
export declare function inputDepth(coord?: NodeInput<'vec2f'>): Node<'f32'>;
|
|
9
|
+
/** Options for `NodePass.attach(engine, node, options)`. */
|
|
10
|
+
export interface NodePassAttachOptions extends NodePassOptions {
|
|
11
|
+
/** Aborts the attach while `@margoengine/post` loads; the pass is then disposed and nothing attaches. */
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
}
|
|
14
|
+
/** Options for `new NodePass(node, options)`. */
|
|
15
|
+
export interface NodePassOptions {
|
|
16
|
+
/**
|
|
17
|
+
* `'ldr'` (default) runs after tonemapping on display colour; `'hdr'` runs on linear light before it (and, when last,
|
|
18
|
+
* applies the display curve, Khronos PBR Neutral, then the sRGB encoding).
|
|
19
|
+
*/
|
|
20
|
+
kind?: 'hdr' | 'ldr';
|
|
21
|
+
/** Plugin and debug name. Default `'shader:pass:<n>'` (n counts passes). */
|
|
22
|
+
name?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A full-screen effect written as one node, run by `@margoengine/post`'s PostStack: `PostStack.attach(engine, [new Tonemap(), pass])`,
|
|
26
|
+
* or alone in a stack of its own with `await NodePass.attach(engine, node)`.
|
|
27
|
+
*/
|
|
28
|
+
export declare class NodePass implements EnginePlugin {
|
|
29
|
+
readonly node: Node<'vec3f' | 'vec4f'>;
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly kind: 'hdr' | 'ldr';
|
|
32
|
+
readonly program: Program;
|
|
33
|
+
private readonly body;
|
|
34
|
+
private readonly value;
|
|
35
|
+
private host?;
|
|
36
|
+
private engine?;
|
|
37
|
+
private stack?;
|
|
38
|
+
private disposed;
|
|
39
|
+
private format?;
|
|
40
|
+
private samples?;
|
|
41
|
+
private encoded;
|
|
42
|
+
private readonly attachment;
|
|
43
|
+
private readonly descriptor;
|
|
44
|
+
private pipeline?;
|
|
45
|
+
private group?;
|
|
46
|
+
private last;
|
|
47
|
+
private sampler?;
|
|
48
|
+
private depthTexture?;
|
|
49
|
+
private depthView?;
|
|
50
|
+
/**
|
|
51
|
+
* Adds a new NodePass to the PostStack of the engine's scene, after the last effect of its kind, or runs it in a new
|
|
52
|
+
* PostStack when the scene has none (loads `@margoengine/post` when called). Disposing the pass removes it from that
|
|
53
|
+
* stack, and a stack made for it goes with it. `PostStack.join(engine, new NodePass(node), index)` places it yourself.
|
|
54
|
+
* @throws MargoError `'unsupported'` on a WebGL engine; the stack's errors (a repeated name, an HDR pass on the LDR
|
|
55
|
+
* tier); the signal's reason when aborted before the pass attaches.
|
|
56
|
+
*/
|
|
57
|
+
static attach(engine: EngineKernel, node: Node<'vec3f' | 'vec4f'>, options?: NodePassAttachOptions): Promise<NodePass>;
|
|
58
|
+
constructor(node: Node<'vec3f' | 'vec4f'>, { kind, name }?: NodePassOptions);
|
|
59
|
+
/** The pass WGSL; `encode` adds the display curve (PBR Neutral) and sRGB encoding (an HDR pass that ends the chain). */
|
|
60
|
+
code(encode?: boolean): string;
|
|
61
|
+
private joinStack;
|
|
62
|
+
setup(engine: Engine): void;
|
|
63
|
+
registerPasses(): void;
|
|
64
|
+
private render;
|
|
65
|
+
onDeviceRestored(): void;
|
|
66
|
+
/** Removes the pass, from the PostStack {@link NodePass.attach} joined too; safe to call twice. */
|
|
67
|
+
dispose(): void;
|
|
68
|
+
[Symbol.dispose]: () => void;
|
|
69
|
+
}
|