@margoengine/dof 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.
@@ -0,0 +1,39 @@
1
+ import { n as e } from "./errors-B1DmBK5g.js";
2
+ //#region src/compat.ts
3
+ var t = (t, n) => n.reduce((t, [n, r]) => {
4
+ if (t.split(n).length !== 2) throw e("unsupported", "DOF on WebGL2 needs the shader its compatibility route was written for");
5
+ return t.replace(n, r);
6
+ }, t), n = ",@builtin(workgroup_id) tile:vec3u,@builtin(local_invocation_index) local:u32", r = ({ cocShader: e, groupMax: r }) => t(e, [
7
+ ["@group(0) @binding(3) var tiles:texture_storage_2d<r32float,write>;\n", ""],
8
+ [r, ""],
9
+ [n, ""],
10
+ ["\n let m=groupMax(near,local);\n if(local==0u){textureStore(tiles,vec2i(tile.xy),vec4f(m,0,0,0));}", ""],
11
+ ["let d=textureLoad(depth,vec2i(id.xy),0);", "let at=vec2i(id.xy);var d=textureLoad(depth,at,0);\n if(p.ortho.w>0.5){let last=vec2i(textureDimensions(depth))-1;for(var k=0;k<4;k++){let o=select(vec2i(0,k*2-1),vec2i(k*2-5,0),k>1);d=max(d,textureLoad(depth,clamp(at+o,vec2i(0),last),0));}}"]
12
+ ]), i = "@group(0) @binding(0) var coc:texture_2d<f32>;\n@group(0) @binding(1) var tiles:texture_storage_2d<r32float,write>;\n@compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u) {\n if(any(id.xy>=textureDimensions(tiles))){return;}\n let size=textureDimensions(coc);var m=0.0;\n for(var i=0u;i<64u;i++){\n let at=id.xy*8u+vec2u(i%8u,i/8u);var near=0.0;\n if(all(at<size)){near=-textureLoad(coc,vec2i(at),0).x;}\n m=select(max(m,near),near,i==0u);\n }\n textureStore(tiles,vec2i(id.xy),vec4f(m,0,0,0));\n}", a = ({ bokehShader: e, groupMax: r }) => t(e, [
13
+ [r, ""],
14
+ [n, ""],
15
+ ["var mine=0.0;\n for(var t=i32(local);t<span*span;t+=64){", "var mine=0.0;let tile=id.xy/8u;\n for(var t=0;t<span*span;t++){"],
16
+ ["let nearMax=groupMax(mine,local);", "let nearMax=mine;"]
17
+ ]), o = (e, r) => t(e, [
18
+ ["@group(0) @binding(4) var tiles:texture_storage_2d<r32float,write>;\n", ""],
19
+ [r, ""],
20
+ [n, ""],
21
+ ["\n let m=groupMax(near,local);\n if(local==0u){textureStore(tiles,vec2i(tile.xy),vec4f(m,0,0,0));}", ""]
22
+ ]);
23
+ function s(e, t, n) {
24
+ let s = (t) => e.createComputePipeline({
25
+ layout: "auto",
26
+ compute: {
27
+ module: e.createShaderModule({ code: t }),
28
+ entryPoint: "main"
29
+ }
30
+ });
31
+ return {
32
+ coc: s(r(t)),
33
+ tiles: s(i),
34
+ bokeh: s(a(t)),
35
+ ...n && { halve: s(o(n, t.groupMax)) }
36
+ };
37
+ }
38
+ //#endregion
39
+ export { a as bokehAloneShader, r as cocPixelShader, s as dofCompatPipelines, i as tilesShader };
@@ -0,0 +1,22 @@
1
+ /** DOF's WebGPU kernels (./shaders.js), handed in so this chunk shares no module with the package's entry. */
2
+ export interface DOFShaders {
3
+ readonly cocShader: string;
4
+ readonly bokehShader: string;
5
+ readonly groupMax: string;
6
+ }
7
+ /** The CoC alone: every pixel stores its own circle of confusion; the tile maxima come from {@link tilesShader}. */
8
+ export declare const cocPixelShader: ({ cocShader, groupMax }: DOFShaders) => string;
9
+ /**
10
+ * One invocation per 8x8 tile: the largest near-field radius (-CoC) of its pixels, 0 for pixels past the edge, as the
11
+ * WebGPU route's workgroup reduction gives it.
12
+ */
13
+ export declare const tilesShader = "@group(0) @binding(0) var coc:texture_2d<f32>;\n@group(0) @binding(1) var tiles:texture_storage_2d<r32float,write>;\n@compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u) {\n if(any(id.xy>=textureDimensions(tiles))){return;}\n let size=textureDimensions(coc);var m=0.0;\n for(var i=0u;i<64u;i++){\n let at=id.xy*8u+vec2u(i%8u,i/8u);var near=0.0;\n if(all(at<size)){near=-textureLoad(coc,vec2i(at),0).x;}\n m=select(max(m,near),near,i==0u);\n }\n textureStore(tiles,vec2i(id.xy),vec4f(m,0,0,0));\n}";
14
+ /** The bokeh gather with each invocation reading its own neighbouring tiles' near maximum. */
15
+ export declare const bokehAloneShader: ({ bokehShader, groupMax }: DOFShaders) => string;
16
+ /** The three pipelines of the route, compiled on `device`. */
17
+ export declare function dofCompatPipelines(device: GPUDevice, shaders: DOFShaders, halve?: string): {
18
+ coc: GPUComputePipeline;
19
+ tiles: GPUComputePipeline;
20
+ bokeh: GPUComputePipeline;
21
+ halve?: GPUComputePipeline;
22
+ };
@@ -0,0 +1,7 @@
1
+ //#region ../shared/errors.js
2
+ var e = (e, t) => Object.assign(Error(t), {
3
+ name: "MargoError",
4
+ code: e
5
+ }), t = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, n = (n, r, i = n) => e(n, t(r, i));
6
+ //#endregion
7
+ export { e as n, n as t };
@@ -0,0 +1,146 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { EngineKernel } from '@margoengine/core';
3
+ import { type Engine, type EnginePlugin, type FrameGraph } from '@margoengine/core/extension';
4
+ import type { DOFLens } from './lens.js';
5
+ /** Lens settings, read from `dof.options` every frame. */
6
+ export interface DOFOptions {
7
+ /** Scene units, measured along the camera's viewing axis. Default 5. */
8
+ focalDistance?: number;
9
+ /** Same units as focalDistance; default 0.05 for a metre-based scene. Not used while `filmGauge` is set. */
10
+ focalLength?: number;
11
+ /**
12
+ * Film or sensor size in millimetres along the side `sensorFit` names: 36 for full frame, 23.6 for APS-C, 17.3 for
13
+ * Micro Four Thirds. When set, the focal length follows the camera's field of view every frame, as a real lens on
14
+ * that sensor would have it, in metres; a zoom (a narrower `camera.fov`) lengthens it and deepens the blur. Default:
15
+ * unset, `focalLength` is used.
16
+ */
17
+ filmGauge?: number;
18
+ /** The side `filmGauge` spans: `'auto'` the longer side of the view, `'horizontal'` or `'vertical'`. Default `'auto'`. */
19
+ sensorFit?: 'auto' | 'horizontal' | 'vertical';
20
+ /** Lens f-number, default 2.8. */
21
+ aperture?: number;
22
+ /** Maximum disk radius in pixels, 1-32. Default 16. */
23
+ maxRadius?: number;
24
+ /** False skips the blur and presents the scene sharp. Default true; also settable as `dof.enabled`. */
25
+ enabled?: boolean;
26
+ /** Tilt-shift and polygon bokeh, from `@margoengine/dof/lens`. Read when the effect attaches. Default: none. */
27
+ lens?: DOFLens;
28
+ /**
29
+ * Temporal DOF: the bokeh is gathered at half resolution, a quarter of the pixels, and its taps turn by the golden
30
+ * angle every frame, so TAA attached after DOF averages the turns in its history into smooth bokeh. In-focus pixels
31
+ * stay at full resolution. Native WebGPU and the full WebGL2 compatibility layer. Read when the effect attaches.
32
+ * Default false.
33
+ */
34
+ temporal?: boolean;
35
+ }
36
+ /**
37
+ * Depth of field: thin-lens circle of confusion from reversed-Z depth, near and far bokeh gathered separately; reads
38
+ * `dof.options` each frame. `DOF.attach(engine, options)` alone, or inside a `PostStack` to combine it with SSAO,
39
+ * Bloom, Tonemap or FXAA. Requires native WebGPU or at least the half-float WebGL2 compatibility tier.
40
+ * @throws RangeError when an option is out of range or the lens scale overflows float32; TypeError when `enabled` is
41
+ * not a boolean.
42
+ * @throws MargoError `'unsupported'` on a WebGL engine or when another plugin replaced core's lighting shader,
43
+ * `'disposed'` after `dispose()`, `'attached'` when attached twice, `'state'` when SSAO, Bloom or PostProcessing owns
44
+ * the scene capture.
45
+ */
46
+ export declare class DOF implements EnginePlugin {
47
+ readonly options: DOFOptions;
48
+ readonly name = "dof";
49
+ private engine?;
50
+ private uniform?;
51
+ private cocPipeline?;
52
+ private bokehPipeline?;
53
+ private coarsePipeline?;
54
+ private nearPipeline?;
55
+ private nearDownPipeline?;
56
+ private nearSampler?;
57
+ private nearTaps?;
58
+ private nearShapes?;
59
+ private nearScene?;
60
+ private nearViews;
61
+ private nearBase;
62
+ private nearLevels;
63
+ private tilesPipeline?;
64
+ private tilesGroup?;
65
+ private waiting;
66
+ private presentPipeline?;
67
+ private scene?;
68
+ private depth?;
69
+ private coc?;
70
+ private tiles?;
71
+ private coarse?;
72
+ private output?;
73
+ private sceneView?;
74
+ private depthView?;
75
+ private groups;
76
+ private originalColor?;
77
+ private originalDepth?;
78
+ private format?;
79
+ private depthFormat?;
80
+ private readonly values;
81
+ private readonly halfValues;
82
+ private halfUniform?;
83
+ private halvePipeline?;
84
+ private joinPipeline?;
85
+ private halfScene?;
86
+ private halfCoc?;
87
+ private halfTiles?;
88
+ private blurred?;
89
+ private turn;
90
+ private readonly shaders;
91
+ private readonly attachment;
92
+ private readonly presentation;
93
+ private captureTarget?;
94
+ private disposed;
95
+ private host?;
96
+ private input?;
97
+ private sceneDepth?;
98
+ private cocView?;
99
+ private tileView?;
100
+ private coarseView?;
101
+ private outputView?;
102
+ private key;
103
+ constructor(options?: DOFOptions);
104
+ static attach(instance: EngineKernel, options?: DOFOptions): DOF;
105
+ get depthTexture(): GPUTexture | undefined;
106
+ /** The blurred linear HDR image; while DOF alone waits for its WebGL2 route, the sharp captured scene. */
107
+ get outputTexture(): GPUTexture | undefined;
108
+ /**
109
+ * The focal length of the last frame, in the units of `focalDistance`: `focalLength`, or with `filmGauge` the one
110
+ * the camera's field of view gives on that sensor. Before the first frame, `focalLength` (default 0.05).
111
+ */
112
+ get focalLength(): number;
113
+ get enabled(): boolean;
114
+ set enabled(value: boolean);
115
+ private joinStack;
116
+ private validate;
117
+ setup(engine: Engine): void;
118
+ /** True while DOF, used alone, waits for its WebGL2 route (it presents the captured scene unblurred then). */
119
+ private get paused();
120
+ registerPasses(graph: FrameGraph): void;
121
+ private lens;
122
+ private capture;
123
+ private resize;
124
+ /** Temporal: the gather's groups over the half textures (1, 3) and the halve and join groups over `input` (5, 6). */
125
+ private halfGroups;
126
+ /** The near chain's level 0 bind group over the gather's scene and CoC (full or, temporal, half size). */
127
+ private nearGroup;
128
+ /** The CoC pass's bind group; on the WebGL2 route also the tile pass's (`tilesGroup`). */
129
+ private cocGroup;
130
+ /** CoC and 8x8 tile maxima: one kernel on WebGPU, two on the WebGL2 route. */
131
+ private circles;
132
+ private blur;
133
+ private dispatch;
134
+ private present;
135
+ /** Stacked: blur `input` by the shared depth into outputTexture, handed on as linear light or presented as the display output. */
136
+ private stage;
137
+ /** CoC (radius, view depth), the blurred output, 8x8-tile near-radius maxima and 2x2-block wide far gathers. */
138
+ private targets;
139
+ private releaseTextures;
140
+ private release;
141
+ onDeviceRestored(engine: Engine): void;
142
+ dispose(): void;
143
+ [Symbol.dispose]: () => void;
144
+ /** @deprecated Use dispose(); kept as an alias until 1.0. */
145
+ destroy(): void;
146
+ }