@margoengine/dof 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.
package/dist/lens.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ /** `DOFLens` settings. */
2
+ export interface DOFLensOptions {
3
+ /** Aperture blades, 3 to 16: the bokeh becomes this polygon. Fixed at construction. Default: round. */
4
+ blades?: number;
5
+ /** Turn of the bokeh polygon in radians. Fixed at construction. Default 0. */
6
+ bladeRotation?: number;
7
+ /** Tilt of the focal plane in radians, under 1.5 either way; 0 keeps it facing the camera. Default 0. */
8
+ tilt?: number;
9
+ /** Screen direction `[right, up]` in which a positive tilt moves the focal plane further away. Default `[0, 1]`. */
10
+ tiltDirection?: readonly [number, number];
11
+ /**
12
+ * Custom bokeh shape (hearts, stars, dirty glass): light passes where the image is bright, as luminance times
13
+ * alpha, partly where it is grey. The image's inscribed circle covers the blur disk, its top towards the top of the
14
+ * screen. Fixed at construction; not with `blades`. Default: none.
15
+ */
16
+ aperture?: DOFApertureImage;
17
+ /**
18
+ * Optical vignetting: towards the frame's edges the bokeh is clipped by a second circle, the lens barrel, into a
19
+ * cat's eye whose long side runs around the frame. The clipping circle's offset at the frame corners, in bokeh radii,
20
+ * 0 to 1.5; it grows linearly from 0 at the centre. Fixed at construction; not with `aperture`. Default 0.
21
+ */
22
+ catEye?: number;
23
+ /**
24
+ * Anamorphic squeeze: the bokeh is an oval this many times taller than wide (1.33, 1.5 and 2 are common anamorphic
25
+ * lenses), 1 to 4. Fixed at construction; not with `aperture`. Default 1, round.
26
+ */
27
+ anamorphic?: number;
28
+ }
29
+ /** The shaders a {@link DOFLens} rewrites. */
30
+ export interface DOFLensShaders {
31
+ readonly cocShader: string;
32
+ readonly coarseShader: string;
33
+ readonly bokehShader: string;
34
+ readonly groupMax: string;
35
+ }
36
+ /** An aperture image for {@link DOFLensOptions.aperture}: RGBA bytes row by row, top row first, as `ImageData` holds them. */
37
+ export interface DOFApertureImage {
38
+ readonly width: number;
39
+ readonly height: number;
40
+ readonly data: ArrayLike<number>;
41
+ }
42
+ /**
43
+ * Lens shape for `DOF`: a tilted focal plane (tilt-shift), and polygon bokeh from aperture blades or any bokeh shape
44
+ * from an aperture image. Pass one as
45
+ * `DOF.attach(engine, {lens})`. `tilt` and `tiltDirection` are read every frame.
46
+ * @throws RangeError when an option is out of range.
47
+ */
48
+ export declare class DOFLens {
49
+ /** See {@link DOFLensOptions.tilt}. */
50
+ tilt: number;
51
+ /** See {@link DOFLensOptions.tiltDirection}. */
52
+ tiltDirection: readonly [number, number];
53
+ private readonly blades?;
54
+ private readonly bladeRotation;
55
+ private readonly mask?;
56
+ private readonly catEye;
57
+ private readonly anamorphic;
58
+ constructor({ blades, bladeRotation, tilt, tiltDirection, aperture, catEye, anamorphic, }?: DOFLensOptions);
59
+ private check;
60
+ /** The DOF kernels rewritten for this lens. Called by `DOF` when it builds its pipelines. */
61
+ shaders(source: DOFLensShaders): DOFLensShaders;
62
+ /** Writes the tilt per clip unit into the free uniform slots 9 and 10. Called by `DOF` every frame. */
63
+ write(values: Float32Array, camera: {
64
+ fov: number;
65
+ matrix: Float32Array;
66
+ position: ArrayLike<number>;
67
+ }, aspect: number): void;
68
+ }
package/dist/lens.js ADDED
@@ -0,0 +1,156 @@
1
+ import { t as e } from "./projection-CS2tLkwf.js";
2
+ import { MargoError as t } from "@margoengine/math/errors";
3
+ //#region src/lens.ts
4
+ var n = "let radius=clamp(p.frame.z*select(1.0-p.lens.x/z,(z-p.lens.x)/p.lens.x,ortho),-p.lens.w,p.lens.w)*p.frame.w;", r = "let n=(vec2f(id.xy)+0.5)/vec2f(textureDimensions(coc))*vec2f(2,-2)+vec2f(-1,1);let t=dot(p.ortho.yz,n);\n let focus=max(select(p.lens.x/max(1.0-t,1e-4),p.lens.x+t,ortho),p.lens.y*1.001);\n let radius=clamp(select(p.frame.z*(p.lens.x-p.lens.y)/(focus-p.lens.y)*(1.0-focus/z),p.frame.z*(z-focus)/p.lens.x,ortho),-p.lens.w,p.lens.w)*p.frame.w;", i = 32, a = 4096, o = 2.39996323;
5
+ function s(e) {
6
+ let { width: t, height: n, data: r } = e ?? {};
7
+ if (!Number.isInteger(t) || !Number.isInteger(n) || t < 1 || n < 1 || !r || r.length < t * n * 4) throw RangeError("Invalid DOFLens aperture; use RGBA bytes of width x height pixels, such as ImageData");
8
+ let a = (e, i) => {
9
+ let a = (Math.min(n - 1, Math.max(0, i)) * t + Math.min(t - 1, Math.max(0, e))) * 4;
10
+ return (.2126 * r[a] + .7152 * r[a + 1] + .0722 * r[a + 2]) * r[a + 3] / 65025;
11
+ }, o = (e, r) => {
12
+ let i = e * t - .5, o = r * n - .5, s = Math.floor(i), c = Math.floor(o), l = i - s, u = o - c;
13
+ return (a(s, c) * (1 - l) + a(s + 1, c) * l) * (1 - u) + (a(s, c + 1) * (1 - l) + a(s + 1, c + 1) * l) * u;
14
+ }, s = /* @__PURE__ */ new Float32Array(1024), c = 0;
15
+ for (let e = 0; e < i; e++) for (let t = 0; t < i; t++) {
16
+ let n = (t + .5) / i * 2 - 1, r = (e + .5) / i * 2 - 1;
17
+ if (n * n + r * r > 1) continue;
18
+ let a = 0;
19
+ for (let n = 0; n < 8; n++) for (let r = 0; r < 8; r++) a += o((t + (r + .5) / 8) / i, (e + (n + .5) / 8) / i);
20
+ let l = a / 64;
21
+ if (!Number.isFinite(l)) throw RangeError("Invalid DOFLens aperture; its bytes must be numbers from 0 to 255");
22
+ s[e * i + t] = l, c = Math.max(c, l);
23
+ }
24
+ if (!(c > 0)) throw RangeError("Invalid DOFLens aperture; the image lets no light through its inscribed circle");
25
+ for (let e = 0; e < s.length; e++) s[e] /= c;
26
+ return s;
27
+ }
28
+ function c(e, t, n) {
29
+ let r = (t * .5 + .5) * i - .5, a = (n * .5 + .5) * i - .5, o = Math.floor(r), s = Math.floor(a), c = r - o, l = a - s, u = (t, n) => t < 0 || n < 0 || t >= i || n >= i ? 0 : e[n * i + t];
30
+ return (u(o, s) * (1 - c) + u(o + 1, s) * c) * (1 - l) + (u(o, s + 1) * (1 - c) + u(o + 1, s + 1) * c) * l;
31
+ }
32
+ function l(e, t) {
33
+ let n = [];
34
+ for (let t = 0; t < a; t++) {
35
+ let r = Math.sqrt((t + .5) / a), i = t * o, s = r * Math.cos(i), l = r * Math.sin(i), u = c(e, -s, -l);
36
+ u > 1 / 64 && n.push([
37
+ s,
38
+ l,
39
+ u
40
+ ]);
41
+ }
42
+ if (!n.length) throw RangeError("Invalid DOFLens aperture; its open area is too small to sample");
43
+ let r = new Float32Array(t * 3), i = new Float64Array(n.length).fill(Infinity), s = 0, l = 0;
44
+ for (let [e, t] of n) s += e / n.length, l += t / n.length;
45
+ let u = 0;
46
+ for (let e = 0; e < n.length; e++) Math.hypot(n[e][0] - s, n[e][1] - l) < Math.hypot(n[u][0] - s, n[u][1] - l) && (u = e);
47
+ for (let e = 0; e < t; e++) {
48
+ let [t, a, o] = n[u];
49
+ r.set([
50
+ t,
51
+ a,
52
+ o
53
+ ], e * 3);
54
+ let s = 0;
55
+ for (let e = 0; e < n.length; e++) i[e] = Math.min(i[e], Math.hypot(n[e][0] - t, n[e][1] - a)), i[e] > i[s] && (s = e);
56
+ u = s;
57
+ }
58
+ return r;
59
+ }
60
+ var u = "fn covers(o:vec2f,radius:f32,pixel:vec2i)->f32 {return select(0.0,1.0,length(o)<=radius);}";
61
+ function d(e, n, r, i) {
62
+ if (e.split(n).length !== i + 1) throw new t("unsupported", "DOFLens needs the DOF shaders of its own version; update both together");
63
+ return e.split(n).join(r);
64
+ }
65
+ var f = (e) => Array.from(e, (e) => `${+e.toFixed(5)}`).join(",");
66
+ function p(e, t) {
67
+ let n = 0, r = 0;
68
+ for (let e = 0; e < 4096; e++) {
69
+ let i = Math.sqrt((e + .5) / 4096), a = e * o;
70
+ n += c(t, i * Math.cos(a), i * Math.sin(a)), r++;
71
+ }
72
+ n /= r;
73
+ let a = [
74
+ 16,
75
+ 32,
76
+ 64
77
+ ].map((e) => `var<private> taps${e}:array<vec3f,${e}>=array<vec3f,${e}>(${l(t, e).reduce((e, t, n, r) => n % 3 ? e : [...e, `vec3f(${f(r.subarray(n, n + 3))})`], [])});`).join("\n"), s = `var<private> mask:array<f32,1024>=array<f32,1024>(${f(t)});
78
+ ${a}
79
+ fn tap(i:u32,n:f32)->vec3f {if(n<20.0){return taps16[i];}if(n<40.0){return taps32[i];}return taps64[i];}
80
+ fn cell(c:vec2i)->f32 {if(any(c<vec2i(0))||any(c>=vec2i(${i}))){return 0.0;}return mask[c.y*${i}+c.x];}
81
+ fn aperture(v:vec2f)->f32 {
82
+ let g=(v*0.5+0.5)*${i}.0-0.5;let c=vec2i(floor(g));let f=g-floor(g);
83
+ return mix(mix(cell(c),cell(c+vec2i(1,0)),f.x),mix(cell(c+vec2i(0,1)),cell(c+vec2i(1,1)),f.x),f.y);
84
+ }
85
+ fn spiral(i:u32,n:f32)->vec2f {`, p = (e, t) => d(t.reduce((e, t) => d(e, `let farPixel=clamp(pixel+vec2i(round(spiral(i,${t}.0)*center.x)),vec2i(0),limit);`, `let farTap=tap(i,${t}.0);let farPixel=clamp(pixel+vec2i(round(farTap.xy*center.x)),vec2i(0),limit);`, 1), d(e, "fn spiral(i:u32,n:f32)->vec2f {", s, 1)), "{farColor+=textureLoad(scene,farPixel,0).rgb;farWeight+=1.0;}", "{farColor+=textureLoad(scene,farPixel,0).rgb*farTap.z;farWeight+=farTap.z;}", t.length), m = (e, t) => d(d(p(e, t), "let farW=1.0;", "let farW=farTap.z;", t.length), u, `${u.slice(0, -2)}*aperture(-o/radius)/${n.toFixed(6)};}`, 1);
86
+ return {
87
+ ...e,
88
+ coarseShader: m(e.coarseShader, [64]),
89
+ bokehShader: d(m(e.bokehShader, [
90
+ 64,
91
+ 32,
92
+ 16
93
+ ]), "let weight=R*R*source.w*inside;", `let weight=R*R*source.w*inside*aperture(-nearTap/radius)/${n.toFixed(6)};`, 1)
94
+ };
95
+ }
96
+ function m(e, t, n) {
97
+ if (!t && n === 1) return e;
98
+ let r = n.toFixed(6), i = `fn barrel(pixel:vec2i)->vec2f {let size=vec2f(textureDimensions(coc));return ${t.toFixed(6)}*(vec2f(pixel)+0.5-size*0.5)/(0.5*length(size));}
99
+ fn eye(v:vec2f,pixel:vec2i)->f32 {return select(0.0,1.0,length(v-barrel(pixel))<=1.0);}
100
+ fn lensArea(pixel:vec2i)->f32 {let d=min(length(barrel(pixel)),1.99);return (2.0*acos(d*0.5)-0.5*d*sqrt(4.0-d*d))/3.14159265;}
101
+ fn spiral(i:u32,n:f32)->vec2f {`, a = (e, t) => d(t.reduce((e, t) => d(e, `let farPixel=clamp(pixel+vec2i(round(spiral(i,${t}.0)*center.x)),vec2i(0),limit);`, `let farTap=spiral(i,${t}.0);let farPixel=clamp(pixel+vec2i(round(farTap*center.x)),vec2i(0),limit);`, 1), d(d(e, "fn spiral(i:u32,n:f32)->vec2f {", i, 1), "vec2f(cos(angle),sin(angle))", `vec2f(cos(angle)/${r},sin(angle))`, 1)), "{farColor+=textureLoad(scene,farPixel,0).rgb;farWeight+=1.0;}", "{let w=eye(-farTap,pixel);farColor+=textureLoad(scene,farPixel,0).rgb*w;farWeight+=w;}", t.length), o = (e, t) => d(d(d(a(e, t), "let farW=1.0;", "let farW=eye(-farTap,pixel);", t.length), "length(o)<=", `length(o*vec2f(${r},1.0))<=`, 1), "radius:f32,pixel:vec2i)->f32 {return ", "radius:f32,pixel:vec2i)->f32 {return eye(-o/radius,pixel)/lensArea(pixel)*", 1).replace("radius*shape(atan2(o.y,o.x))", `radius*shape(atan2(o.y,o.x*${r}))`), s = o(e.bokehShader, [
102
+ 64,
103
+ 32,
104
+ 16
105
+ ]);
106
+ return s = d(s, "fn disk(i:u32)->vec2f {return spiral(i,64.0);}", `fn disk(i:u32)->vec2f {let s=spiral(i,64.0);return vec2f(s.x*${r},s.y);}`, 1), s = s.split("radius*shape(f32(i)*2.39996323)").join(`radius*shape(atan2(nearTap.y,nearTap.x*${r}))`), s = d(s, "-length(nearTap)+0.5", `-length(nearTap*vec2f(${r},1.0))+0.5`, 1), s = d(s, "let weight=R*R*source.w*inside;", `let weight=R*R*source.w*inside*${r}*eye(-nearTap/radius,pixel)/lensArea(pixel);`, 1), {
107
+ ...e,
108
+ coarseShader: o(e.coarseShader, [64]),
109
+ bokehShader: s
110
+ };
111
+ }
112
+ function h(e, n) {
113
+ return n.reduce((e, [n, r]) => {
114
+ if (e.split(n).length !== 2) throw new t("unsupported", "DOFLens needs the DOF shaders of its own version; update both together");
115
+ return e.replace(n, r);
116
+ }, e);
117
+ }
118
+ var g = class {
119
+ constructor({ blades: e, bladeRotation: t = 0, tilt: n = 0, tiltDirection: r = [0, 1], aperture: i, catEye: a = 0, anamorphic: o = 1 } = {}) {
120
+ if (e !== void 0 && (!Number.isInteger(e) || e < 3 || e > 16)) throw RangeError("Invalid DOFLens blades; use a whole number from 3 to 16");
121
+ if (i !== void 0 && e !== void 0) throw RangeError("Invalid DOFLens; give blades or an aperture image, not both");
122
+ if (!(a >= 0 && a <= 1.5)) throw RangeError("Invalid DOFLens catEye; use bokeh radii from 0 to 1.5");
123
+ if (!(o >= 1 && o <= 4)) throw RangeError("Invalid DOFLens anamorphic; use a squeeze from 1 to 4");
124
+ if (i !== void 0 && (a || o !== 1)) throw RangeError("Invalid DOFLens; catEye and anamorphic do not combine with an aperture image");
125
+ if (this.catEye = a, this.anamorphic = o, i !== void 0 && (this.mask = s(i)), !Number.isFinite(t)) throw RangeError("Invalid DOFLens bladeRotation; use finite radians");
126
+ this.blades = e, this.bladeRotation = t, this.tilt = n, this.tiltDirection = r, this.check();
127
+ }
128
+ check() {
129
+ let [e, t] = this.tiltDirection;
130
+ if (!(Math.abs(this.tilt) < 1.5) || !Number.isFinite(e) || !Number.isFinite(t) || !(e || t)) throw RangeError("Invalid DOFLens tilt; use radians under 1.5 either way and a non-zero tiltDirection");
131
+ }
132
+ shaders(e) {
133
+ let t = {
134
+ ...e,
135
+ cocShader: h(e.cocShader, [[n, r]])
136
+ };
137
+ if (this.mask) return p(t, this.mask);
138
+ let i = this.blades;
139
+ if (i === void 0) return m(t, this.catEye, this.anamorphic);
140
+ let a = 2 * Math.PI / i, o = Math.cos(Math.PI / i), s = [["fn spiral(i:u32,n:f32)->vec2f {", `fn shape(a:f32)->f32 {let m=a-${this.bladeRotation};return ${o}/cos(m-${a}*floor(m/${a})-${a / 2});}
141
+ fn spiral(i:u32,n:f32)->vec2f {`], ["return sqrt((f32(i)+0.5)/n)*vec2f", "return sqrt((f32(i)+0.5)/n)*shape(angle)*vec2f"]];
142
+ return t.coarseShader = h(e.coarseShader, s), t.bokehShader = h(e.bokehShader, [
143
+ ...s,
144
+ ["const tapArea=1.0;", `const tapArea=${i / 2 * Math.sin(a) / Math.PI};`],
145
+ ["clamp(radius-length(nearTap)+0.5", "clamp(radius*shape(f32(i)*2.39996323)-length(nearTap)+0.5"],
146
+ [u, u.replace("length(o)<=radius", "length(o)<=radius*shape(atan2(o.y,o.x))")]
147
+ ]), t.coarseShader = h(t.coarseShader, [[u, u.replace("length(o)<=radius", "length(o)<=radius*shape(atan2(o.y,o.x))")]]), m(t, this.catEye, this.anamorphic);
148
+ }
149
+ write(t, n, r) {
150
+ this.check();
151
+ let [i, a] = this.tiltDirection, o = Math.tan(this.tilt) / Math.hypot(i, a), s = e(n.matrix, n.position), c = Math.tan(n.fov * Math.PI / 360);
152
+ t[9] = o * i * (s ? 1 / s.sx : c * r), t[10] = o * a * (s ? 1 / s.sy : c);
153
+ }
154
+ };
155
+ //#endregion
156
+ export { g as DOFLens };
@@ -0,0 +1,16 @@
1
+ //#region ../shared/projection.js
2
+ function e(e, t) {
3
+ if (!e || e[3] || e[7] || e[11]) return null;
4
+ let n = (n) => e[n] * t[0] + e[4 + n] * t[1] + e[8 + n] * t[2] + e[12 + n], r = Math.hypot(e[2], e[6], e[10]), i = n(2);
5
+ return {
6
+ sx: Math.hypot(e[0], e[4], e[8]),
7
+ sy: Math.hypot(e[1], e[5], e[9]),
8
+ sz: r,
9
+ ox: n(0),
10
+ oy: n(1),
11
+ near: (i - 1) / r,
12
+ far: i / r
13
+ };
14
+ }
15
+ //#endregion
16
+ export { e as t };
@@ -0,0 +1,29 @@
1
+ export declare const groupMax = "var<workgroup> lanes:array<f32,64>;\nvar<workgroup> rows:array<f32,8>;\nfn groupMax(v:f32,local:u32)->f32 {\n lanes[local]=v;workgroupBarrier();\n if(local<8u){var m=lanes[local*8u];for(var i=1u;i<8u;i++){m=max(m,lanes[local*8u+i]);}rows[local]=m;}\n workgroupBarrier();\n var m=rows[0];for(var i=1u;i<8u;i++){m=max(m,rows[i]);}return m;\n}";
2
+ export declare const cocShader = "struct Params {lens:vec4f,frame:vec4f,ortho:vec4f}\n@group(0) @binding(0) var<uniform> p:Params;\n@group(0) @binding(1) var depth:texture_depth_2d;\n@group(0) @binding(2) var coc:texture_storage_2d<rg32float,write>;\n@group(0) @binding(3) var tiles:texture_storage_2d<r32float,write>;\nvar<workgroup> lanes:array<f32,64>;\nvar<workgroup> rows:array<f32,8>;\nfn groupMax(v:f32,local:u32)->f32 {\n lanes[local]=v;workgroupBarrier();\n if(local<8u){var m=lanes[local*8u];for(var i=1u;i<8u;i++){m=max(m,lanes[local*8u+i]);}rows[local]=m;}\n workgroupBarrier();\n var m=rows[0];for(var i=1u;i<8u;i++){m=max(m,rows[i]);}return m;\n}\n@compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u,@builtin(workgroup_id) tile:vec3u,@builtin(local_invocation_index) local:u32) {\n var near=0.0;\n if(all(id.xy<textureDimensions(coc))){\n let d=textureLoad(depth,vec2i(id.xy),0);\n let ortho=p.ortho.x>0.5;\n let z=select(p.frame.x*p.frame.y/(p.frame.x+d*(p.frame.y-p.frame.x)),mix(p.frame.y,p.frame.x,d),ortho);\n let radius=clamp(p.frame.z*select(1.0-p.lens.x/z,(z-p.lens.x)/p.lens.x,ortho),-p.lens.w,p.lens.w)*p.frame.w;\n textureStore(coc,vec2i(id.xy),vec4f(radius,z,0,0));near=-radius;\n }\n let m=groupMax(near,local);\n if(local==0u){textureStore(tiles,vec2i(tile.xy),vec4f(m,0,0,0));}\n}";
3
+ export declare const coarseShader: string;
4
+ export declare const NEAR_LEVELS = 6;
5
+ /** The largest disk radius in pixels the near chain's levels serve: a scaled cap stops here. */
6
+ export declare const DOF_MAX_DISK = 128;
7
+ /** The view height in pixels at which `DOFOptions.maxRadius` holds unless `referenceHeight` says otherwise. */
8
+ export declare const DOF_REFERENCE_HEIGHT = 1080;
9
+ /**
10
+ * The disk cap in pixels for a view `height` pixels tall: `maxRadius` at `reference`, scaled by the height so it is one
11
+ * share of the frame at any canvas size, and at most DOF_MAX_DISK.
12
+ */
13
+ export declare function capRadius(maxRadius: number, height: number, reference: number): number;
14
+ /**
15
+ * How many near-chain levels above level 0 a gather capped at `cap` pixels reads: the bokeh kernel's ceil(nearLod) for
16
+ * a round disk (the widest tap spacing; a polygon's is narrower), never under the three it always had, at most five.
17
+ */
18
+ export declare function nearLevelsFor(cap: number): number;
19
+ export declare const nearShader = "struct Params {lens:vec4f,frame:vec4f,ortho:vec4f}\n@group(0) @binding(0) var<uniform> p:Params;\n@group(0) @binding(1) var scene:texture_2d<f32>;\n@group(0) @binding(2) var coc:texture_2d<f32>;\n@group(0) @binding(3) var nearTaps:texture_storage_2d<rgba16float,write>;\n@group(0) @binding(4) var nearShapes:texture_storage_2d<rgba16float,write>;\n@group(0) @binding(5) var nearScene:texture_storage_2d<rgba16float,write>;\n@compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u) {\n if(any(id.xy>=textureDimensions(nearTaps))||p.frame.w==0.0){return;}\n let at=vec2i(id.xy);let s=textureLoad(coc,at,0).xy;let color=textureLoad(scene,at,0).rgb;\n let g=select(0.0,1.0/max(s.x*s.x,1.0),s.x< -0.5);\n textureStore(nearTaps,at,vec4f(color*g,g));\n textureStore(nearShapes,at,vec4f(-s.x*g,s.y*g,0,0));\n textureStore(nearScene,at,vec4f(color,1));\n}";
20
+ export declare const nearDownShader = "@group(0) @binding(0) var tapsBelow:texture_2d<f32>;\n@group(0) @binding(1) var shapesBelow:texture_2d<f32>;\n@group(0) @binding(2) var sceneBelow:texture_2d<f32>;\n@group(0) @binding(3) var nearTaps:texture_storage_2d<rgba16float,write>;\n@group(0) @binding(4) var nearShapes:texture_storage_2d<rgba16float,write>;\n@group(0) @binding(5) var nearScene:texture_storage_2d<rgba16float,write>;\n@compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u) {\n if(any(id.xy>=textureDimensions(nearTaps))){return;}\n let last=vec2i(textureDimensions(tapsBelow))-1;var taps=vec4f(0);var shapes=vec4f(0);var plain=vec4f(0);\n for(var k=0;k<4;k++){\n let at=min(vec2i(id.xy)*2+vec2i(k%2,k/2),last);\n taps+=textureLoad(tapsBelow,at,0);shapes+=textureLoad(shapesBelow,at,0);plain+=textureLoad(sceneBelow,at,0);\n }\n textureStore(nearTaps,vec2i(id.xy),taps*0.25);\n textureStore(nearShapes,vec2i(id.xy),shapes*0.25);\n textureStore(nearScene,vec2i(id.xy),plain*0.25);\n}";
21
+ export declare const bokehShader: string;
22
+ export declare const presentShader: string;
23
+ export declare const halveShader = "@group(0) @binding(0) var scene:texture_2d<f32>;\n@group(0) @binding(1) var coc:texture_2d<f32>;\n@group(0) @binding(2) var halfScene:texture_storage_2d<rgba16float,write>;\n@group(0) @binding(3) var halfCoc:texture_storage_2d<rg32float,write>;\n@group(0) @binding(4) var tiles:texture_storage_2d<r32float,write>;\nvar<workgroup> lanes:array<f32,64>;\nvar<workgroup> rows:array<f32,8>;\nfn groupMax(v:f32,local:u32)->f32 {\n lanes[local]=v;workgroupBarrier();\n if(local<8u){var m=lanes[local*8u];for(var i=1u;i<8u;i++){m=max(m,lanes[local*8u+i]);}rows[local]=m;}\n workgroupBarrier();\n var m=rows[0];for(var i=1u;i<8u;i++){m=max(m,rows[i]);}return m;\n}\n@compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u,@builtin(workgroup_id) tile:vec3u,@builtin(local_invocation_index) local:u32) {\n var near=0.0;\n if(all(id.xy<textureDimensions(halfCoc))){\n let last=vec2i(textureDimensions(coc))-1;var color=vec4f(0);var c=vec2f(0,3.4e38);\n for(var k=0;k<4;k++){\n let at=min(vec2i(id.xy)*2+vec2i(k%2,k/2),last);color+=textureLoad(scene,at,0);\n let s=textureLoad(coc,at,0).xy;if(s.y<c.y){c=s;}\n }\n textureStore(halfScene,vec2i(id.xy),color*0.25);textureStore(halfCoc,vec2i(id.xy),vec4f(c.x*0.5,c.y,0,0));near=-c.x*0.5;\n }\n let m=groupMax(near,local);\n if(local==0u){textureStore(tiles,vec2i(tile.xy),vec4f(m,0,0,0));}\n}";
24
+ export declare const joinShader = "@group(0) @binding(0) var scene:texture_2d<f32>;\n@group(0) @binding(1) var coc:texture_2d<f32>;\n@group(0) @binding(2) var blurred:texture_2d<f32>;\n@group(0) @binding(3) var result:texture_storage_2d<rgba16float,write>;\n@compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u) {\n if(any(id.xy>=textureDimensions(result))){return;}\n let pixel=vec2i(id.xy);let sharp=textureLoad(scene,pixel,0);\n let g=(vec2f(pixel)+0.5)*0.5-0.5;let q=vec2i(floor(g));let f=g-floor(g);let last=vec2i(textureDimensions(blurred))-1;\n let b=mix(mix(textureLoad(blurred,clamp(q,vec2i(0),last),0),textureLoad(blurred,clamp(q+vec2i(1,0),vec2i(0),last),0),f.x),\n mix(textureLoad(blurred,clamp(q+vec2i(0,1),vec2i(0),last),0),textureLoad(blurred,clamp(q+vec2i(1,1),vec2i(0),last),0),f.x),f.y);\n let w=max(smoothstep(0.5,1.5,abs(textureLoad(coc,pixel,0).x)),b.a);\n textureStore(result,pixel,vec4f(mix(sharp.rgb,b.rgb,w),sharp.a));\n}";
25
+ /** The gather kernels for the half image: each frame turns the taps by `p.ortho.w`, and alpha holds near coverage. */
26
+ export declare function halfShaders<T extends {
27
+ coarseShader: string;
28
+ bokehShader: string;
29
+ }>(source: T): T;
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "@margoengine/dof",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Optional two-pass WebGPU depth of field with circular bokeh.",
5
+ "keywords": [
6
+ "margoengine",
7
+ "webgpu",
8
+ "3d",
9
+ "dof"
10
+ ],
11
+ "homepage": "https://margoengine.com",
12
+ "bugs": {
13
+ "url": "https://github.com/scottcampbelldata/margoengine/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/scottcampbelldata/margoengine.git",
18
+ "directory": "packages/dof"
19
+ },
20
+ "license": "Apache-2.0",
21
+ "author": "Scott Campbell",
22
+ "type": "module",
23
+ "sideEffects": false,
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js"
28
+ },
29
+ "./lens": {
30
+ "types": "./dist/lens.d.ts",
31
+ "import": "./dist/lens.js"
32
+ },
33
+ "./autofocus": {
34
+ "types": "./dist/autofocus.d.ts",
35
+ "import": "./dist/autofocus.js"
36
+ }
37
+ },
38
+ "files": [
39
+ "dist",
40
+ "LICENSE",
41
+ "NOTICE"
42
+ ],
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "dependencies": {
47
+ "@margoengine/math": "^1.0.0"
48
+ },
49
+ "peerDependencies": {
50
+ "@margoengine/core": "^1.0.0"
51
+ },
52
+ "scripts": {
53
+ "build": "node ../../scripts/build-postfx.js dof",
54
+ "prepack": "npm run build",
55
+ "test": "node --test tests/*.test.js ../../examples/package-benchmarks/proofs/package-guard.mjs",
56
+ "dev": "vite --config vite.config.js --open /examples/index.html",
57
+ "build:demo": "vite build --config vite.config.js",
58
+ "test:gpu": "cd ../.. && node packages/dof/tests/gpu/lens-check.mjs && node packages/dof/tests/gpu/compat-check.mjs && node packages/dof/tests/gpu/alpha-check.mjs && node packages/dof/tests/gpu/alpha-check.mjs --compat",
59
+ "test:gpu:tiers": "cd ../.. && node packages/webgl2-compat/test/tier-packages-browser.mjs --only=dof%20HDR",
60
+ "test:gpu:restore-tier": "cd ../.. && node packages/dof/tests/gpu/restore-tier-check.mjs",
61
+ "test:gpu:postfx": "cd ../.. && npm run modern:test:postfx:browser"
62
+ }
63
+ }