@margoengine/dof 0.1.0 → 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/index.js CHANGED
@@ -1,6 +1,6 @@
1
- // @margo/core-built 0.1.0
1
+ // @margo/core-built 1.0.0
2
2
  import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
- const n=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
3
+ const n=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
4
  import { t as e } from "./errors-B1DmBK5g.js";
5
5
  import { t } from "./projection-CS2tLkwf.js";
6
6
  import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
@@ -70,17 +70,20 @@ function p(e) {
70
70
  function m(e, t) {
71
71
  return f(e) === "webgl2" ? t() : void 0;
72
72
  }
73
+ function h(e) {
74
+ return e ? e.then(() => void 0, () => void 0) : Promise.resolve();
75
+ }
73
76
  //#endregion
74
77
  //#region ../shared/msaa.js
75
- var h = "@vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {return vec4f(array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3))[i],0,1);}", g = (e, t, n) => e.createBindGroup({
78
+ var g = "@vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {return vec4f(array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3))[i],0,1);}", _ = (e, t, n) => e.createBindGroup({
76
79
  layout: t.getBindGroupLayout(0),
77
80
  entries: [{
78
81
  binding: 0,
79
82
  resource: n
80
83
  }]
81
- }), _ = Symbol.for("margoengine.optional.msaa"), v = h + "\n@group(0) @binding(0) var source:texture_depth_multisampled_2d;\n@fragment fn fragment(@builtin(position) p:vec4f)->@builtin(frag_depth) f32 {\n let q=vec2i(p.xy);\n return max(max(textureLoad(source,q,0),textureLoad(source,q,1)),max(textureLoad(source,q,2),textureLoad(source,q,3)));\n}";
82
- function y(e, t, n, r, i, a = !1) {
83
- let o = (e) => v.replace(/return max[^;]+;/, `return textureLoad(source,q,${e});`), s = (n, r) => {
84
+ }), v = Symbol.for("margoengine.optional.msaa"), y = g + "\n@group(0) @binding(0) var source:texture_depth_multisampled_2d;\n@fragment fn fragment(@builtin(position) p:vec4f)->@builtin(frag_depth) f32 {\n let q=vec2i(p.xy);\n return max(max(textureLoad(source,q,0),textureLoad(source,q,1)),max(textureLoad(source,q,2),textureLoad(source,q,3)));\n}";
85
+ function b(e, t, n, r, i, a = !1) {
86
+ let o = (e) => y.replace(/return max[^;]+;/, `return textureLoad(source,q,${e});`), s = (n, r) => {
84
87
  let a = e.createShaderModule({ code: n }), o = e.createRenderPipeline({
85
88
  layout: "auto",
86
89
  vertex: {
@@ -101,7 +104,7 @@ function y(e, t, n, r, i, a = !1) {
101
104
  });
102
105
  return {
103
106
  pipeline: o,
104
- group: g(e, o, t)
107
+ group: _(e, o, t)
105
108
  };
106
109
  };
107
110
  return {
@@ -113,7 +116,7 @@ function y(e, t, n, r, i, a = !1) {
113
116
  ].map((e) => s(o(e), {
114
117
  count: r,
115
118
  mask: 1 << e
116
- })) : [s(o("i32(sample)").replace("p:vec4f)", "p:vec4f,@builtin(sample_index) sample:u32)"), { count: r })] : [s(v, { count: r })],
119
+ })) : [s(o("i32(sample)").replace("p:vec4f)", "p:vec4f,@builtin(sample_index) sample:u32)"), { count: r })] : [s(y, { count: r })],
117
120
  descriptor: {
118
121
  colorAttachments: [],
119
122
  depthStencilAttachment: {
@@ -126,7 +129,22 @@ function y(e, t, n, r, i, a = !1) {
126
129
  }
127
130
  };
128
131
  }
129
- var b = class {
132
+ function x(e, t, n, r, i, a) {
133
+ let s = e.pipelines.get(r);
134
+ s || e.pipelines.set(r, s = /* @__PURE__ */ new Map());
135
+ let c = s.get(i);
136
+ if (!c) {
137
+ let t = e.samples === 1;
138
+ s.set(i, c = b(e.device, e.depthSource, n, r, i, t));
139
+ }
140
+ c.descriptor.depthStencilAttachment.view = n, c.descriptor.depthStencilAttachment.depthLoadOp = a?.view ? "load" : "clear";
141
+ let l = t.beginRenderPass(c.descriptor);
142
+ a && o(l, a);
143
+ let u = c.draws;
144
+ for (let e = 0; e < u.length; e++) l.setPipeline(u[e].pipeline), l.setBindGroup(0, u[e].group), l.draw(3);
145
+ l.end();
146
+ }
147
+ var ee = class {
130
148
  constructor(e, t, n, r = 4) {
131
149
  if (r !== 1 && r !== 4) throw RangeError("Scene samples must be 1 or 4");
132
150
  this.samples = r, this.device = e, this.resolvedColor = t, this.resolvedDepth = n, this.textures = [], this.pipelines = /* @__PURE__ */ new Map();
@@ -168,7 +186,7 @@ var b = class {
168
186
  depth: e.depth,
169
187
  format: e.format,
170
188
  descriptor: e.descriptor,
171
- owner: x(e)
189
+ owner: S(e)
172
190
  }), e.color = this.color, e.depth = this.depth, e.format = this.resolvedColor.format, this.baseDescriptor !== e.descriptor && (this.baseDescriptor = e.descriptor, this.descriptor = {
173
191
  ...e.descriptor,
174
192
  depthStencil: {
@@ -176,47 +194,39 @@ var b = class {
176
194
  format: this.resolvedDepth.format
177
195
  },
178
196
  multisample: { count: this.samples }
179
- }), e.descriptor = this.descriptor, e[_] = this, this.retired = !1;
197
+ }), e.descriptor = this.descriptor, e[v] = this, this.retired = !1, this.resized = e.width !== void 0 && (e.width !== this.resolvedDepth.width || e.height !== this.resolvedDepth.height);
180
198
  }
181
199
  copyDepth(e, t, n, r = this.resolvedDepth.format) {
182
- let i = this.pipelines.get(n);
183
- i || this.pipelines.set(n, i = /* @__PURE__ */ new Map());
184
- let a = i.get(r);
185
- a || (this.depthSource ??= this.resolvedDepth.createView({ aspect: "depth-only" }), i.set(r, a = y(this.device, this.depthSource, t, n, r, this.samples === 1))), a.descriptor.depthStencilAttachment.view = t, a.descriptor.depthStencilAttachment.depthLoadOp = this.viewContext?.view ? "load" : "clear";
186
- let s = e.beginRenderPass(a.descriptor);
187
- this.viewContext && o(s, this.viewContext);
188
- let c = a.draws;
189
- for (let e = 0; e < c.length; e++) s.setPipeline(c[e].pipeline), s.setBindGroup(0, c[e].group), s.draw(3);
190
- s.end();
200
+ this.depthSource ??= this.resolvedDepth.createView({ aspect: "depth-only" }), x(this, e, t, n, r, this.viewContext);
191
201
  }
192
202
  resolve(e, t = !1, n = !0) {
193
- if (this.samples !== 1) {
203
+ if (this.samples === 1) t && this.give(e);
204
+ else {
194
205
  if (n && this.copyDepth(e, this.resolvedDepthView, 1), t) {
195
206
  let t = this.viewContext?.view ? "store" : "discard";
196
- this.finalResolution.colorAttachments[0].storeOp = t, this.finalResolution.depthStencilAttachment.depthStoreOp = t, this.original && this.copyDepth(e, this.original.depth, this.original.descriptor.multisample?.count ?? 1, this.original.descriptor.depthStencil.format), this.retired = !0;
207
+ this.finalResolution.colorAttachments[0].storeOp = t, this.finalResolution.depthStencilAttachment.depthStoreOp = t, this.give(e);
197
208
  }
198
209
  e.beginRenderPass(t ? this.finalResolution : this.resolution).end();
199
210
  }
200
211
  }
212
+ give(e) {
213
+ let t = this.original;
214
+ this.retired || !t || this.resized || (this.copyDepth(e, t.depth, t.descriptor.multisample?.count ?? 1, t.descriptor.depthStencil.format), this.retired = !0);
215
+ }
201
216
  restore(e) {
202
217
  let t = this.original;
203
- this.samples === 1 ? (Object.assign(e, {
218
+ this.give(e.encoder), Object.assign(e, {
204
219
  color: t.color,
205
220
  depth: t.depth,
206
221
  format: t.format,
207
222
  descriptor: t.descriptor
208
- }), e[_] = t.owner) : (this.retired || this.copyDepth(e.encoder, t.depth, t.descriptor.multisample?.count ?? 1, t.descriptor.depthStencil.format), Object.assign(e, {
209
- color: t.color,
210
- depth: t.depth,
211
- format: t.format,
212
- descriptor: t.descriptor
213
- }), e[_] = t.owner);
223
+ }), e[v] = t.owner;
214
224
  }
215
225
  present(e) {
216
226
  this.resolve(e.encoder, !0), this.restore(e);
217
227
  let t = e.descriptor.multisample?.count ?? 1;
218
228
  if (!this.presentation || this.presentationFormat !== e.format || this.presentationSamples !== t) {
219
- let n = this.device.createShaderModule({ code: h + "@group(0) @binding(0) var source:texture_2d<f32>;@fragment fn fragment(@builtin(position) p:vec4f)->@location(0) vec4f{return textureLoad(source,vec2i(p.xy),0);}" });
229
+ let n = this.device.createShaderModule({ code: g + "@group(0) @binding(0) var source:texture_2d<f32>;@fragment fn fragment(@builtin(position) p:vec4f)->@location(0) vec4f{return textureLoad(source,vec2i(p.xy),0);}" });
220
230
  this.presentation = this.device.createRenderPipeline({
221
231
  layout: "auto",
222
232
  vertex: {
@@ -229,7 +239,7 @@ var b = class {
229
239
  targets: [{ format: e.format }]
230
240
  },
231
241
  multisample: e.descriptor.multisample
232
- }), this.presentationGroup = g(this.device, this.presentation, this.resolvedColor.createView()), this.presentationFormat = e.format, this.presentationSamples = t;
242
+ }), this.presentationGroup = _(this.device, this.presentation, this.resolvedColor.createView()), this.presentationFormat = e.format, this.presentationSamples = t;
233
243
  }
234
244
  let n = this.presentationPass ??= { colorAttachments: [{
235
245
  view: e.color,
@@ -245,13 +255,13 @@ var b = class {
245
255
  this.textures = [], this.pipelines.clear();
246
256
  }
247
257
  };
248
- function x(e) {
249
- let t = e[_];
258
+ function S(e) {
259
+ let t = e[v];
250
260
  return t?.color === e.color && t.depth === e.depth ? t : void 0;
251
261
  }
252
262
  //#endregion
253
263
  //#region ../shared/gpu.js
254
- function S(e, t, n) {
264
+ function C(e, t, n) {
255
265
  return e.createBindGroup({
256
266
  layout: t.getBindGroupLayout(0),
257
267
  entries: n.map((e, t) => ({
@@ -260,7 +270,7 @@ function S(e, t, n) {
260
270
  }))
261
271
  });
262
272
  }
263
- function C(e, t, n, r = {}) {
273
+ function te(e, t, n, r = {}) {
264
274
  let i = e.createShaderModule({ code: t });
265
275
  return e.createRenderPipeline({
266
276
  layout: "auto",
@@ -285,11 +295,41 @@ function w(e, t, n = "main") {
285
295
  }
286
296
  });
287
297
  }
288
- var T = "fn pbrNeutral(input:vec3f)->vec3f {let x=min(input.r,min(input.g,input.b));let offset=select(0.04,x-6.25*x*x,x<0.08);var v=input-offset;let peak=max(v.r,max(v.g,v.b));if(peak<0.76){return v;}let newPeak=1.0-0.24*0.24/(peak+0.24-0.76);v*=newPeak/peak;return mix(v,vec3f(newPeak),1.0-1.0/(0.15*(peak-newPeak)+1.0));}\nfn toDisplay(c:vec3f)->vec3f {let x=pbrNeutral(max(c,vec3f(0)));return select(x*12.92,1.055*pow(x,vec3f(1.0/2.4))-0.055,x>=vec3f(0.0031308));}\n", E = /let mapped=pbrNeutral\([\s\S]*?return vec4f\([^\n]+\);/, D = (e) => e.replace(E, "return vec4f(linear,1);"), O = (e) => `${e} requires the standard core lighting shader; remove the plugin that replaced core's shader`, k = "struct Params {lens:vec4f,frame:vec4f,ortho:vec4f}\n@group(0) @binding(0) var<uniform> p:Params;", A = "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}", j = `${k}
298
+ //#endregion
299
+ //#region ../shared/output-key.js
300
+ var T = class {
301
+ format = "";
302
+ samples = -1;
303
+ hdr = !1;
304
+ straight = !1;
305
+ extra = void 0;
306
+ #e = "";
307
+ #t = -1;
308
+ #n = !1;
309
+ #r = !1;
310
+ #i = void 0;
311
+ changed(e, t, n) {
312
+ return this.format === e.format && this.samples === e.multisample.count && this.hdr === !!e.hdr && this.straight === t && this.extra === n ? !1 : (this.#e = e.format, this.#t = e.multisample.count, this.#n = !!e.hdr, this.#r = t, this.#i = n, !0);
313
+ }
314
+ done() {
315
+ this.format = this.#e, this.samples = this.#t, this.hdr = this.#n, this.straight = this.#r, this.extra = this.#i;
316
+ }
317
+ reset() {
318
+ this.format = "";
319
+ }
320
+ }, E = Symbol.for("margoengine.premultiplied-canvas"), D = (e) => !!e?.canvas?.[E], O = /vec4f\(toDisplay\(((?:[^()]|\((?:[^()]|\([^()]*\))*\))*)\),([\w.]+)\)/g;
321
+ function k(e) {
322
+ let t = 0, n = e.replace(O, (e, n, r) => (t++, `vec4f(toDisplayStraight(${n},${r}),${r})`));
323
+ if (!t && !e.includes("toDisplayStraight(")) throw RangeError("display shader has no vec4f(toDisplay(colour),alpha) output to convert; write the display output in that form or call toDisplayStraight(colour,alpha) itself");
324
+ return `${n}
325
+ fn toDisplayStraight(c:vec3f,a:f32)->vec3f {if(a>0.0&&a<1.0){return toDisplay(c/a)*a;}return toDisplay(c);}
326
+ `;
327
+ }
328
+ var A = (e, t) => D(e) && t.includes("fn toDisplay(") ? k(t) : t, j = "fn pbrNeutral(input:vec3f)->vec3f {let x=min(input.r,min(input.g,input.b));let offset=select(0.04,x-6.25*x*x,x<0.08);var v=input-offset;let peak=max(v.r,max(v.g,v.b));if(peak<0.76){return v;}let newPeak=1.0-0.24*0.24/(peak+0.24-0.76);v*=newPeak/peak;return mix(v,vec3f(newPeak),1.0-1.0/(0.15*(peak-newPeak)+1.0));}\nfn toDisplay(c:vec3f)->vec3f {let x=pbrNeutral(max(c,vec3f(0)));return select(x*12.92,1.055*pow(x,vec3f(1.0/2.4))-0.055,x>=vec3f(0.0031308));}\n", M = /let mapped=pbrNeutral\([\s\S]*?return vec4f\([^\n]+\);/, N = (e) => e.replace(M, "return vec4f(linear,1);"), P = (e) => `${e} requires the standard core lighting shader; remove the plugin that replaced core's shader`, F = "struct Params {lens:vec4f,frame:vec4f,ortho:vec4f}\n@group(0) @binding(0) var<uniform> p:Params;", I = "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}", L = `${F}
289
329
  @group(0) @binding(1) var depth:texture_depth_2d;
290
330
  @group(0) @binding(2) var coc:texture_storage_2d<rg32float,write>;
291
331
  @group(0) @binding(3) var tiles:texture_storage_2d<r32float,write>;
292
- ${A}
332
+ ${I}
293
333
  @compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u,@builtin(workgroup_id) tile:vec3u,@builtin(local_invocation_index) local:u32) {
294
334
  var near=0.0;
295
335
  if(all(id.xy<textureDimensions(coc))){
@@ -301,7 +341,7 @@ ${A}
301
341
  }
302
342
  let m=groupMax(near,local);
303
343
  if(local==0u){textureStore(tiles,vec2i(tile.xy),vec4f(m,0,0,0));}
304
- }`, M = (e) => `var frontColor=vec3f(0);var frontWeight=0.0;var farTotal=farWeight;
344
+ }`, R = (e) => `var frontColor=vec3f(0);var frontWeight=0.0;var farTotal=farWeight;
305
345
  for(var i=0u;i<${e}u;i++){
306
346
  let farPixel=clamp(pixel+vec2i(round(spiral(i,${e}.0)*center.x)),vec2i(0),limit);
307
347
  let farCoC=textureLoad(coc,farPixel,0).xy;let farW=1.0;farTotal+=farW;
@@ -311,17 +351,25 @@ ${A}
311
351
  frontColor+=textureLoad(scene,farPixel,0).rgb*w;frontWeight+=w;
312
352
  }
313
353
  }
314
- if(frontWeight>0.0){farColor=mix(farColor/farWeight,frontColor/frontWeight,min(frontWeight/farTotal,1.0));farWeight=1.0;}`, N = "@group(0) @binding(1) var scene:texture_2d<f32>;\n@group(0) @binding(2) var coc:texture_2d<f32>;\nfn spiral(i:u32,n:f32)->vec2f {\n let angle=f32(i)*2.39996323;\n return sqrt((f32(i)+0.5)/n)*vec2f(cos(angle),sin(angle));\n}\nfn covers(o:vec2f,radius:f32,pixel:vec2i)->f32 {return select(0.0,1.0,length(o)<=radius);}", P = `${k}
315
- ${N}
354
+ if(frontWeight>0.0){farColor=mix(farColor/farWeight,frontColor/frontWeight,min(frontWeight/farTotal,1.0));farWeight=1.0;}`, z = "@group(0) @binding(1) var scene:texture_2d<f32>;\n@group(0) @binding(2) var coc:texture_2d<f32>;\nfn spiral(i:u32,n:f32)->vec2f {\n let angle=f32(i)*2.39996323;\n return sqrt((f32(i)+0.5)/n)*vec2f(cos(angle),sin(angle));\n}\nfn covers(o:vec2f,radius:f32,pixel:vec2i)->f32 {return select(0.0,1.0,length(o)<=radius);}", B = `${F}
355
+ ${z}
316
356
  @group(0) @binding(3) var coarse:texture_storage_2d<rgba16float,write>;
317
357
  @compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u) {
318
358
  if(any(id.xy>=textureDimensions(coarse))){return;}
319
359
  let pixel=vec2i(id.xy)*2;let limit=vec2i(textureDimensions(coc))-1;let center=textureLoad(coc,pixel,0).xy;
320
360
  var farColor=textureLoad(scene,pixel,0).rgb;var farWeight=1.0;
321
361
  let wide=center.x>3.19&&p.frame.w!=0.0;
322
- if(wide){${M(64)}}
362
+ if(wide){${R(64)}}
323
363
  textureStore(coarse,id.xy,vec4f(farColor/farWeight,select(0.0,center.y,wide)));
324
- }`, F = `${k}
364
+ }`;
365
+ function V(e, t, n) {
366
+ return Math.min(128, e * t / n);
367
+ }
368
+ function H(e) {
369
+ let t = Math.max(Math.log2(e * .2215567 / 1.7320508), 0);
370
+ return Math.min(5, Math.max(3, Math.ceil(t)));
371
+ }
372
+ var U = `${F}
325
373
  @group(0) @binding(1) var scene:texture_2d<f32>;
326
374
  @group(0) @binding(2) var coc:texture_2d<f32>;
327
375
  @group(0) @binding(3) var nearTaps:texture_storage_2d<rgba16float,write>;
@@ -334,8 +382,8 @@ ${N}
334
382
  textureStore(nearTaps,at,vec4f(color*g,g));
335
383
  textureStore(nearShapes,at,vec4f(-s.x*g,s.y*g,0,0));
336
384
  textureStore(nearScene,at,vec4f(color,1));
337
- }`, I = "@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}", L = (e, t) => `var ${e}=textureSampleLevel(${t},filtered,at,low);if(blend>0.0){${e}=mix(${e},textureSampleLevel(${t},filtered,at,low+1.0),blend);}`, R = `${k}
338
- ${N}
385
+ }`, W = "@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}", G = (e, t) => `var ${e}=textureSampleLevel(${t},filtered,at,low);if(blend>0.0){${e}=mix(${e},textureSampleLevel(${t},filtered,at,low+1.0),blend);}`, K = `${F}
386
+ ${z}
339
387
  @group(0) @binding(3) var result:texture_storage_2d<rgba16float,write>;
340
388
  @group(0) @binding(4) var tiles:texture_2d<f32>;
341
389
  @group(0) @binding(5) var coarse:texture_2d<f32>;
@@ -343,12 +391,12 @@ ${N}
343
391
  @group(0) @binding(7) var nearTaps:texture_2d<f32>;
344
392
  @group(0) @binding(8) var nearShapes:texture_2d<f32>;
345
393
  @group(0) @binding(9) var nearScene:texture_2d<f32>;
346
- ${A}
394
+ ${I}
347
395
  fn disk(i:u32)->vec2f {return spiral(i,64.0);}
348
396
  const tapArea=1.0;
349
397
  fn nearLod(radius:f32)->f32 {return max(log2(radius*0.2215567*sqrt(tapArea)/1.7320508),0.0);}
350
398
  @compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u,@builtin(workgroup_id) tile:vec3u,@builtin(local_invocation_index) local:u32) {
351
- let count=vec2i(textureDimensions(tiles));let footprint=1.5*exp2(min(ceil(nearLod(p.lens.w)),3.0));let reach=i32(ceil((p.lens.w+1.0+footprint)/8.0));let span=2*reach+1;
399
+ let count=vec2i(textureDimensions(tiles));let footprint=1.5*exp2(min(ceil(nearLod(p.lens.w)),5.0));let reach=i32(ceil((p.lens.w+1.0+footprint)/8.0));let span=2*reach+1;
352
400
  var mine=0.0;
353
401
  for(var t=i32(local);t<span*span;t+=64){
354
402
  let o=vec2i(t%span,t/span)-reach;let at=vec2i(tile.xy)+o;
@@ -372,18 +420,18 @@ fn nearLod(radius:f32)->f32 {return max(log2(radius*0.2215567*sqrt(tapArea)/1.73
372
420
  let s=textureLoad(coarse,min(q+o,last),0);
373
421
  if(w>0.0&&abs(s.w-center.y)<=0.05*center.y){up+=vec4f(s.rgb,1.0)*w;}
374
422
  }
375
- if(up.w>0.999){farColor=up.rgb;farWeight=up.w;}else{${M(64)}}
376
- }else if(center.x>2.26){${M(32)}}else if(center.x>0.5){${M(16)}}
377
- let reachTaps=select(0u,64u,nearMax>0.5);let R=min(p.lens.w,nearMax+1.0);let lod=min(nearLod(R),3.0);
423
+ if(up.w>0.999){farColor=up.rgb;farWeight=up.w;}else{${R(64)}}
424
+ }else if(center.x>2.26){${R(32)}}else if(center.x>0.5){${R(16)}}
425
+ let reachTaps=select(0u,64u,nearMax>0.5);let R=min(p.lens.w,nearMax+1.0);let lod=min(nearLod(R),5.0);
378
426
  let low=floor(lod);let blend=lod-low;
379
427
  let nearSize=vec2f(textureDimensions(nearTaps));
380
428
  for(var i=0u;i<reachTaps;i++){
381
429
  let nearTap=disk(i)*R;let nearPixel=pixel+vec2i(round(nearTap));
382
430
  if(any(nearPixel<vec2i(0))||any(nearPixel>limit)){continue;}
383
- let at=(vec2f(pixel)+0.5+nearTap)/nearSize;${L("source", "nearTaps")}
431
+ let at=(vec2f(pixel)+0.5+nearTap)/nearSize;${G("source", "nearTaps")}
384
432
  var share=0.0;var tint=vec3f(0);
385
433
  if(source.w>0.0){
386
- ${L("nearForm", "nearShapes")}let form=nearForm.xy/source.w;let radius=form.x;tint=source.rgb/source.w;
434
+ ${G("nearForm", "nearShapes")}let form=nearForm.xy/source.w;let radius=form.x;tint=source.rgb/source.w;
387
435
  if(form.y<=center.y+max(0.001,center.y*0.01)){
388
436
  share=min(source.w*max(radius*radius,1.0),1.0);
389
437
  let inside=clamp(radius-length(nearTap)+0.5,0.0,1.0);
@@ -392,7 +440,7 @@ fn nearLod(radius:f32)->f32 {return max(log2(radius*0.2215567*sqrt(tapArea)/1.73
392
440
  }
393
441
  }
394
442
  if(center.x< -0.5&&share<1.0&&covers(nearTap,-center.x,pixel)>0.0){
395
- ${L("behind", "nearScene")}backColor+=max(behind.rgb-tint*share,vec3f(0));backWeight+=1.0-share;
443
+ ${G("behind", "nearScene")}backColor+=max(behind.rgb-tint*share,vec3f(0));backWeight+=1.0-share;
396
444
  }
397
445
  }
398
446
  if(center.x< -0.5){
@@ -402,20 +450,20 @@ fn nearLod(radius:f32)->f32 {return max(log2(radius*0.2215567*sqrt(tapArea)/1.73
402
450
  let coverage=clamp(nearWeight/64.0,0.0,1.0);
403
451
  let color=mix(farColor/farWeight,nearColor/max(nearWeight,0.0001),coverage);
404
452
  textureStore(result,pixel,vec4f(color,sharp.a));
405
- }`, z = `
453
+ }`, q = `
406
454
  @group(0) @binding(0) var scene:texture_2d<f32>;
407
455
  @vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {
408
456
  let xy=array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3));return vec4f(xy[i],0,1);
409
457
  }
410
- ${T}
458
+ ${j}
411
459
  @fragment fn fragment(@builtin(position) pos:vec4f)->@location(0) vec4f {
412
460
  let c=textureLoad(scene,vec2i(pos.xy),0);return vec4f(toDisplay(c.rgb),c.a);
413
- }`, B = `@group(0) @binding(0) var scene:texture_2d<f32>;
461
+ }`, J = `@group(0) @binding(0) var scene:texture_2d<f32>;
414
462
  @group(0) @binding(1) var coc:texture_2d<f32>;
415
463
  @group(0) @binding(2) var halfScene:texture_storage_2d<rgba16float,write>;
416
464
  @group(0) @binding(3) var halfCoc:texture_storage_2d<rg32float,write>;
417
465
  @group(0) @binding(4) var tiles:texture_storage_2d<r32float,write>;
418
- ${A}
466
+ ${I}
419
467
  @compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u,@builtin(workgroup_id) tile:vec3u,@builtin(local_invocation_index) local:u32) {
420
468
  var near=0.0;
421
469
  if(all(id.xy<textureDimensions(halfCoc))){
@@ -428,8 +476,8 @@ ${A}
428
476
  }
429
477
  let m=groupMax(near,local);
430
478
  if(local==0u){textureStore(tiles,vec2i(tile.xy),vec4f(m,0,0,0));}
431
- }`, V = "@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}";
432
- function H(e) {
479
+ }`, Y = "@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}";
480
+ function X(e) {
433
481
  let t = (e) => {
434
482
  let t = e.split("f32(i)*2.39996323");
435
483
  if (t.length < 2) throw new r("unsupported", "DOF temporal needs the gather kernels of its own version");
@@ -443,16 +491,138 @@ function H(e) {
443
491
  };
444
492
  }
445
493
  //#endregion
494
+ //#region src/alpha.ts
495
+ function Z(e, t) {
496
+ return t.reduce((e, [t, n, i]) => {
497
+ let a = e.split(t);
498
+ if (i === void 0 ? a.length < 2 : a.length !== i + 1) throw new r("unsupported", "DOF on a transparent canvas needs the gather kernels of its own version");
499
+ return a.join(n);
500
+ }, e);
501
+ }
502
+ var Q = [["var frontColor=vec3f(0)", "var frontColor=vec4f(0)"], ["textureLoad(scene,farPixel,0).rgb", "textureLoad(scene,farPixel,0)"]];
503
+ function ne(e, t) {
504
+ let n = Z(e.nearShader, [[
505
+ "vec4f(-s.x*g,s.y*g,0,0)",
506
+ "vec4f(-s.x*g,s.y*g,textureLoad(scene,at,0).a*g,0)",
507
+ 1
508
+ ], [
509
+ "textureStore(nearScene,at,vec4f(color,1));",
510
+ "textureStore(nearScene,at,textureLoad(scene,at,0));",
511
+ 1
512
+ ]]), r = Z(e.coarseShader, [
513
+ ...Q,
514
+ [
515
+ "@compute",
516
+ "@group(0) @binding(4) var coarseAlpha:texture_storage_2d<r32float,write>;\n@compute",
517
+ 1
518
+ ],
519
+ [
520
+ "var farColor=textureLoad(scene,pixel,0).rgb;",
521
+ "var farColor=textureLoad(scene,pixel,0);",
522
+ 1
523
+ ],
524
+ [
525
+ "textureStore(coarse,id.xy,vec4f(farColor/farWeight,select(0.0,center.y,wide)));",
526
+ "let tone=farColor/farWeight;textureStore(coarse,id.xy,vec4f(tone.rgb,select(0.0,center.y,wide)));textureStore(coarseAlpha,id.xy,vec4f(tone.a));",
527
+ 1
528
+ ]
529
+ ]), i = "@group(0) @binding(10) var coarseAlpha:texture_2d<f32>;\n" + (t ? "@group(0) @binding(11) var resultAlpha:texture_storage_2d<r32float,write>;\n" : ""), a = Z(e.bokehShader, [
530
+ ...Q,
531
+ [
532
+ "fn disk(i:u32)",
533
+ i + "fn disk(i:u32)",
534
+ 1
535
+ ],
536
+ [
537
+ "var farColor=sharp.rgb;",
538
+ "var farColor=sharp;",
539
+ 1
540
+ ],
541
+ [
542
+ "var nearColor=vec3f(0);",
543
+ "var nearColor=vec4f(0);",
544
+ 1
545
+ ],
546
+ [
547
+ "var backColor=vec3f(0);",
548
+ "var backColor=vec4f(0);",
549
+ 1
550
+ ],
551
+ [
552
+ "var up=vec4f(0);",
553
+ "var up=vec4f(0);var upAlpha=0.0;",
554
+ 1
555
+ ],
556
+ [
557
+ "up+=vec4f(s.rgb,1.0)*w;",
558
+ "up+=vec4f(s.rgb,1.0)*w;upAlpha+=textureLoad(coarseAlpha,min(q+o,last),0).r*w;",
559
+ 1
560
+ ],
561
+ [
562
+ "farColor=up.rgb;",
563
+ "farColor=vec4f(up.rgb,upAlpha);",
564
+ 1
565
+ ],
566
+ [
567
+ "var tint=vec3f(0);",
568
+ "var tint=vec4f(0);",
569
+ 1
570
+ ],
571
+ [
572
+ "tint=source.rgb/source.w;",
573
+ "tint=vec4f(source.rgb,nearForm.z)/source.w;",
574
+ 1
575
+ ],
576
+ [
577
+ "backColor+=max(behind.rgb-tint*share,vec3f(0));",
578
+ "backColor+=max(behind-tint*share,vec4f(0));",
579
+ 1
580
+ ],
581
+ [
582
+ "nearColor+=sharp.rgb*weight;",
583
+ "nearColor+=sharp*weight;",
584
+ 1
585
+ ]
586
+ ]), o = t ? Z(a, [[
587
+ "textureStore(result,pixel,vec4f(color,coverage));",
588
+ "textureStore(result,pixel,vec4f(color.rgb,coverage));textureStore(resultAlpha,pixel,vec4f(color.a));",
589
+ 1
590
+ ], [
591
+ "textureStore(result,pixel,vec4f(sharp.rgb,0));",
592
+ "textureStore(result,pixel,vec4f(sharp.rgb,0));textureStore(resultAlpha,pixel,vec4f(sharp.a));",
593
+ 1
594
+ ]]) : Z(a, [[
595
+ "textureStore(result,pixel,vec4f(color,sharp.a));",
596
+ "textureStore(result,pixel,color);",
597
+ 1
598
+ ]]), s = t ? Z(e.joinShader, [[
599
+ "@compute",
600
+ "@group(0) @binding(4) var blurredAlpha:texture_2d<f32>;\n@compute",
601
+ 1
602
+ ], [
603
+ "textureStore(result,pixel,vec4f(mix(sharp.rgb,b.rgb,w),sharp.a));",
604
+ "let a=mix(mix(textureLoad(blurredAlpha,clamp(q,vec2i(0),last),0).r,textureLoad(blurredAlpha,clamp(q+vec2i(1,0),vec2i(0),last),0).r,f.x),\n mix(textureLoad(blurredAlpha,clamp(q+vec2i(0,1),vec2i(0),last),0).r,textureLoad(blurredAlpha,clamp(q+vec2i(1,1),vec2i(0),last),0).r,f.x),f.y);\n textureStore(result,pixel,mix(sharp,vec4f(b.rgb,a),w));",
605
+ 1
606
+ ]]) : e.joinShader;
607
+ return {
608
+ ...e,
609
+ nearShader: n,
610
+ coarseShader: r,
611
+ bokehShader: o,
612
+ joinShader: s
613
+ };
614
+ }
615
+ //#endregion
446
616
  //#region src/index.ts
447
- var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
617
+ var $ = (...e) => e.filter((e) => e !== void 0), re = class e {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
448
618
  constructor(e = {}) {
449
- this.options = e, this.name = "dof", this.nearViews = [], this.nearBase = [], this.nearLevels = [], this.waiting = !1, this.groups = [], this.values = /* @__PURE__ */ new Float32Array(12), this.halfValues = /* @__PURE__ */ new Float32Array(12), this.turn = 0, this.shaders = /* @__PURE__ */ new Map(), this.attachment = {
619
+ this.options = e, this.name = "dof", this.nearViews = [], this.nearBase = [], this.nearLevels = [], this.waiting = !1, this.routeReady = Promise.resolve(), this.groups = [], this.values = /* @__PURE__ */ new Float32Array(12), this.halfValues = /* @__PURE__ */ new Float32Array(12), this.turn = 0, this.alpha = !1, this.shaders = /* @__PURE__ */ new Map(), this.attachment = {
450
620
  view: void 0,
451
621
  loadOp: "clear",
452
622
  storeOp: "store"
453
- }, this.presentation = { colorAttachments: [this.attachment] }, this.disposed = !1, this.key = "", this.validate();
623
+ }, this.presentation = { colorAttachments: [this.attachment] }, this.disposed = !1, this.key = new T(), this.validate();
454
624
  }
455
- static attach(t, r = {}) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
625
+ static attach(t, r = {}) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
456
626
  let i = new e(r);
457
627
  return n(t).use(i), i;
458
628
  }
@@ -481,7 +651,7 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
481
651
  }
482
652
  validate() {
483
653
  let e = this.options, t = e.filmGauge, n = t === void 0 ? e.focalLength ?? .05 : 0, r = e.focalDistance ?? 5, i = e.aperture ?? 2.8, a = e.maxRadius ?? 16;
484
- if (!Number.isFinite(Math.fround(n)) || t === void 0 && Math.fround(n) <= 0 || !Number.isFinite(Math.fround(r)) || Math.fround(r) <= Math.fround(n) || !Number.isFinite(Math.fround(i)) || Math.fround(i) <= 0 || !Number.isFinite(a) || a < 1 || a > 32 || e.enabled !== void 0 && typeof e.enabled != "boolean" || e.temporal !== void 0 && typeof e.temporal != "boolean") throw RangeError("Invalid DOF options; use focalLength > 0, focalDistance > focalLength, aperture > 0, maxRadius from 1 to 32 and a boolean enabled and temporal");
654
+ if (!Number.isFinite(Math.fround(n)) || t === void 0 && Math.fround(n) <= 0 || !Number.isFinite(Math.fround(r)) || Math.fround(r) <= Math.fround(n) || !Number.isFinite(Math.fround(i)) || Math.fround(i) <= 0 || !Number.isFinite(a) || a < 1 || a > 32 || e.referenceHeight !== void 0 && !(Number.isFinite(e.referenceHeight) && e.referenceHeight > 0) || e.enabled !== void 0 && typeof e.enabled != "boolean" || e.temporal !== void 0 && typeof e.temporal != "boolean") throw RangeError("Invalid DOF options; use focalLength > 0, focalDistance > focalLength, aperture > 0, maxRadius from 1 to 32, referenceHeight > 0 and a boolean enabled and temporal");
485
655
  if (t !== void 0 && !(Number.isFinite(t) && t > 0) || e.sensorFit !== void 0 && ![
486
656
  "auto",
487
657
  "horizontal",
@@ -494,36 +664,48 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
494
664
  this.engine = e, this.uniform = e.gpu.createBuffer({
495
665
  size: 48,
496
666
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
497
- });
498
- let t = {
499
- cocShader: j,
500
- coarseShader: P,
501
- bokehShader: R,
502
- groupMax: A
503
- }, n = this.options.lens?.shaders(t) ?? t, i = this.options.temporal === !0, a = i ? H(n) : n;
504
- i && (this.halfUniform = e.gpu.createBuffer({
667
+ }), this.build(D(e));
668
+ let t = this.uniform, n = m(e.gpu, () => import("./compat-CkmQH9nh.js"));
669
+ this.waiting = !!n, n?.then(({ dofCompatPipelines: e }) => {
670
+ this.uniform === t && (this.waiting = !1, this.compatPipelines = e, this.useCompat(), this.releaseTextures());
671
+ }, (e) => {
672
+ throw this.waiting = !1, e;
673
+ }), this.routeReady = h(n);
674
+ }
675
+ build(e) {
676
+ let t = this.engine.gpu, n = {
677
+ cocShader: L,
678
+ coarseShader: B,
679
+ bokehShader: K,
680
+ groupMax: I
681
+ }, r = this.options.lens?.shaders(n) ?? n, i = this.options.temporal === !0, a = {
682
+ ...i ? X(r) : r,
683
+ nearShader: U,
684
+ joinShader: Y
685
+ }, o = e ? ne(a, i) : a;
686
+ this.alpha = e, this.kernels = o, i && (this.halfUniform ??= t.createBuffer({
505
687
  size: 48,
506
688
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
507
- }), this.halvePipeline = w(e.gpu, B), this.joinPipeline = w(e.gpu, V)), this.cocPipeline = e.gpu.createComputePipeline({
689
+ }), this.halvePipeline = w(t, J), this.joinPipeline = w(t, o.joinShader)), this.cocPipeline = t.createComputePipeline({
508
690
  layout: "auto",
509
691
  compute: {
510
- module: e.gpu.createShaderModule({ code: a.cocShader }),
692
+ module: t.createShaderModule({ code: o.cocShader }),
511
693
  entryPoint: "main"
512
694
  }
513
- }), this.bokehPipeline = w(e.gpu, a.bokehShader), this.coarsePipeline = w(e.gpu, a.coarseShader), this.nearPipeline = w(e.gpu, F), this.nearDownPipeline = w(e.gpu, I), this.nearSampler = e.gpu.createSampler({
695
+ }), this.bokehPipeline = w(t, o.bokehShader), this.coarsePipeline = w(t, o.coarseShader), this.nearPipeline = w(t, o.nearShader), this.nearDownPipeline = w(t, W), this.nearSampler = t.createSampler({
514
696
  magFilter: "linear",
515
697
  minFilter: "linear",
516
698
  mipmapFilter: "linear"
517
- });
518
- let o = this.uniform, s = m(e.gpu, () => import("./compat-CkmQH9nh.js"));
519
- this.waiting = !!s, s?.then(({ dofCompatPipelines: t }) => {
520
- if (this.uniform !== o) return;
521
- this.waiting = !1;
522
- let n = t(e.gpu, a, i ? B : void 0);
523
- ({coc: this.cocPipeline, tiles: this.tilesPipeline, bokeh: this.bokehPipeline} = n), i && (this.halvePipeline = n.halve), this.releaseTextures();
524
- }, (e) => {
525
- throw this.waiting = !1, e;
526
- });
699
+ }), this.tilesPipeline = void 0, this.useCompat();
700
+ }
701
+ useCompat() {
702
+ let e = this.compatPipelines;
703
+ if (!e || !this.kernels) return;
704
+ let t = !!this.halfUniform, n = e(this.engine.gpu, this.kernels, t ? J : void 0);
705
+ ({coc: this.cocPipeline, tiles: this.tilesPipeline, bokeh: this.bokehPipeline} = n), t && (this.halvePipeline = n.halve);
706
+ }
707
+ get ready() {
708
+ return this.routeReady;
527
709
  }
528
710
  get paused() {
529
711
  return this.waiting && !this.host;
@@ -537,13 +719,13 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
537
719
  }
538
720
  }), !this.host && (e.add({
539
721
  name: "dof:coc",
540
- order: 20,
722
+ order: 29.8,
541
723
  execute: (e) => {
542
724
  e.backend === "webgpu" && !this.paused && this.circles(e);
543
725
  }
544
726
  }), e.add({
545
727
  name: "dof:bokeh",
546
- order: 21,
728
+ order: 29.9,
547
729
  execute: (e) => {
548
730
  e.backend === "webgpu" && !this.paused && this.blur(e);
549
731
  }
@@ -561,24 +743,26 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
561
743
  }
562
744
  capture(e) {
563
745
  this.validate();
564
- let n = this.options, i = this.values, [o, s] = a(e);
565
- if (i[0] = n.focalDistance ?? 5, i[1] = n.filmGauge === void 0 ? n.focalLength ?? .05 : this.lens(n.filmGauge, e.camera.fov, o / s), !(Math.fround(i[0]) > Math.fround(i[1]))) throw RangeError(`DOF focalDistance must lie beyond the focal length the camera gives, ${i[1]}`);
566
- i[2] = n.aperture ?? 2.8, i[3] = n.maxRadius ?? 16, i[4] = e.camera.near, i[5] = e.camera.far, i[6] = i[1] * s / (4 * i[2] * (i[0] - i[1]) * Math.tan(e.camera.fov * Math.PI / 360)), i[7] = +this.enabled, i[8] = 0, i[11] = +(!!this.tilesPipeline && (e.descriptor.multisample?.count ?? 4) > 1);
567
- let c = t(e.camera.matrix, e.camera.position);
568
- if (c && (i[4] = c.near, i[5] = c.far, i[6] = i[1] * s * c.sy * i[0] / (4 * i[2] * (i[0] - i[1])), i[8] = 1), n.lens?.write(i, e.camera, o / s), !Number.isFinite(i[6])) throw RangeError("DOF lens scale exceeds float32; use a larger aperture or a focalDistance further from focalLength");
746
+ let n = D(this.engine);
747
+ n !== this.alpha && (this.releaseTextures(), this.build(n));
748
+ let i = this.options, o = this.values, [s, c] = a(e);
749
+ if (o[0] = i.focalDistance ?? 5, o[1] = i.filmGauge === void 0 ? i.focalLength ?? .05 : this.lens(i.filmGauge, e.camera.fov, s / c), !(Math.fround(o[0]) > Math.fround(o[1]))) throw RangeError(`DOF focalDistance must lie beyond the focal length the camera gives, ${o[1]}`);
750
+ o[2] = i.aperture ?? 2.8, o[3] = V(i.maxRadius ?? 16, c, i.referenceHeight ?? 1080), o[4] = e.camera.near, o[5] = e.camera.far, o[6] = o[1] * c / (4 * o[2] * (o[0] - o[1]) * Math.tan(e.camera.fov * Math.PI / 360)), o[7] = +this.enabled, o[8] = 0, o[11] = +(!!this.tilesPipeline && (e.descriptor.multisample?.count ?? 4) > 1);
751
+ let l = t(e.camera.matrix, e.camera.position);
752
+ if (l && (o[4] = l.near, o[5] = l.far, o[6] = o[1] * c * l.sy * o[0] / (4 * o[2] * (o[0] - o[1])), o[8] = 1), i.lens?.write(o, e.camera, s / c), !Number.isFinite(o[6])) throw RangeError("DOF lens scale exceeds float32; use a larger aperture or a focalDistance further from focalLength");
569
753
  if (!this.host) {
570
754
  if (this.engine.hasPlugin("ssao") || this.engine.hasPlugin("post-processing") || this.engine.hasPlugin("bloom")) throw new r("state", "DOF cannot share scene capture with another post effect; put both effects in one PostStack");
571
755
  this.resize(e), this.originalColor = e.color, this.originalDepth = e.depth;
572
756
  let t = this.shaders.get(e.shader);
573
757
  if (t === void 0) {
574
- if (t = D(e.shader), t === e.shader) throw new r("unsupported", O("DOF"));
758
+ if (t = N(e.shader), t === e.shader) throw new r("unsupported", P("DOF"));
575
759
  this.shaders.set(e.shader, t);
576
760
  }
577
761
  e.shader = t, this.captureTarget.begin(e);
578
762
  }
579
- if (e.device.queue.writeBuffer(this.uniform, 0, i), this.halfUniform) {
763
+ if (e.device.queue.writeBuffer(this.uniform, 0, o), this.halfUniform) {
580
764
  let t = this.halfValues;
581
- t.set(i), t[3] = i[3] / 2, t[11] = this.turn++ * 2.39996323 % (2 * Math.PI), e.device.queue.writeBuffer(this.halfUniform, 0, t);
765
+ t.set(o), t[3] = o[3] / 2, t[11] = this.turn++ * 2.39996323 % (2 * Math.PI), e.device.queue.writeBuffer(this.halfUniform, 0, t);
582
766
  }
583
767
  }
584
768
  resize(e) {
@@ -594,8 +778,8 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
594
778
  size: [e.width, e.height],
595
779
  format: t,
596
780
  usage: n | GPUTextureUsage.RENDER_ATTACHMENT
597
- }), this.targets(e.device, e.width, e.height), this.captureTarget = new b(e.device, this.scene, this.depth, e.descriptor.multisample?.count ?? 4), this.sceneView = this.scene.createView(), this.depthView = this.depth.createView();
598
- let r = this.cocView, i = this.outputView, a = this.tileView, o = this.coarseView, s = e.device.createShaderModule({ code: z });
781
+ }), this.targets(e.device, e.width, e.height), this.captureTarget = new ee(e.device, this.scene, this.depth, e.descriptor.multisample?.count ?? 4), this.sceneView = this.scene.createView(), this.depthView = this.depth.createView();
782
+ let r = this.cocView, i = this.outputView, a = this.tileView, o = this.coarseView, s = e.device.createShaderModule({ code: A(this.engine, q) });
599
783
  this.presentPipeline = e.device.createRenderPipeline({
600
784
  layout: "auto",
601
785
  vertex: {
@@ -644,6 +828,10 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
644
828
  ...this.nearViews.map((e, t) => ({
645
829
  binding: 7 + t,
646
830
  resource: e
831
+ })),
832
+ ...$(this.coarseAlphaView, this.blurredAlphaView).map((e, t) => ({
833
+ binding: 10 + t,
834
+ resource: e
647
835
  }))
648
836
  ]
649
837
  }),
@@ -654,17 +842,18 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
654
842
  resource: i
655
843
  }]
656
844
  }),
657
- this.halfUniform ? void 0 : S(e.device, this.coarsePipeline, [
845
+ this.halfUniform ? void 0 : C(e.device, this.coarsePipeline, [
658
846
  { buffer: this.uniform },
659
847
  this.sceneView,
660
848
  r,
661
- o
849
+ o,
850
+ ...$(this.coarseAlphaView)
662
851
  ])
663
852
  ], this.halfUniform || (this.groups[8] = this.nearGroup(e.device, { buffer: this.uniform }, this.sceneView, r)), this.halfUniform && this.halfGroups(e.device, this.sceneView);
664
853
  }
665
854
  halfGroups(e, t) {
666
855
  let n = { buffer: this.halfUniform }, r = this.halfScene.createView(), i = this.halfCoc.createView(), a = this.halfTiles.createView(), o = this.blurred.createView();
667
- this.groups[1] = S(e, this.bokehPipeline, [
856
+ this.groups[1] = C(e, this.bokehPipeline, [
668
857
  n,
669
858
  r,
670
859
  i,
@@ -672,27 +861,30 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
672
861
  a,
673
862
  this.coarseView,
674
863
  this.nearSampler,
675
- ...this.nearViews
676
- ]), this.groups[3] = S(e, this.coarsePipeline, [
864
+ ...this.nearViews,
865
+ ...$(this.coarseAlphaView, this.blurredAlphaView)
866
+ ]), this.groups[3] = C(e, this.coarsePipeline, [
677
867
  n,
678
868
  r,
679
869
  i,
680
- this.coarseView
681
- ]), this.groups[8] = this.nearGroup(e, n, r, i), this.groups[5] = S(e, this.halvePipeline, [
870
+ this.coarseView,
871
+ ...$(this.coarseAlphaView)
872
+ ]), this.groups[8] = this.nearGroup(e, n, r, i), this.groups[5] = C(e, this.halvePipeline, [
682
873
  t,
683
874
  this.cocView,
684
875
  r,
685
876
  i,
686
877
  ...this.tilesPipeline ? [] : [a]
687
- ]), this.groups[6] = S(e, this.joinPipeline, [
878
+ ]), this.groups[6] = C(e, this.joinPipeline, [
688
879
  t,
689
880
  this.cocView,
690
881
  o,
691
- this.outputView
692
- ]), this.tilesPipeline && (this.groups[7] = S(e, this.tilesPipeline, [i, a]));
882
+ this.outputView,
883
+ ...$(this.blurredAlphaView)
884
+ ]), this.tilesPipeline && (this.groups[7] = C(e, this.tilesPipeline, [i, a]));
693
885
  }
694
886
  nearGroup(e, t, n, r) {
695
- return S(e, this.nearPipeline, [
887
+ return C(e, this.nearPipeline, [
696
888
  t,
697
889
  n,
698
890
  r,
@@ -700,7 +892,7 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
700
892
  ]);
701
893
  }
702
894
  cocGroup(e, t, n = !1) {
703
- return this.tilesPipeline ? (this.tilesGroup = S(e, this.tilesPipeline, [this.cocView, this.tileView]), S(e, this.cocPipeline, [
895
+ return this.tilesPipeline ? (this.tilesGroup = C(e, this.tilesPipeline, [this.cocView, this.tileView]), C(e, this.cocPipeline, [
704
896
  { buffer: this.uniform },
705
897
  t.createView({ aspect: "depth-only" }),
706
898
  this.cocView
@@ -724,7 +916,7 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
724
916
  resource: this.tileView
725
917
  }
726
918
  ]
727
- }) : S(e, this.cocPipeline, [
919
+ }) : C(e, this.cocPipeline, [
728
920
  { buffer: this.uniform },
729
921
  t.createView({ aspect: "depth-only" }),
730
922
  this.cocView,
@@ -736,7 +928,7 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
736
928
  }
737
929
  blur(e) {
738
930
  let t = this.halvePipeline ? 2 : 1;
739
- this.dispatch(e, this.nearPipeline, this.groups[8], 8 * t), this.nearLevels.forEach((n, r) => this.dispatch(e, this.nearDownPipeline, n, 16 * t << r)), this.dispatch(e, this.coarsePipeline, this.groups[3], 16 * t), this.dispatch(e, this.bokehPipeline, this.groups[1], 8 * t), this.joinPipeline && this.dispatch(e, this.joinPipeline, this.groups[6]);
931
+ this.dispatch(e, this.nearPipeline, this.groups[8], 8 * t), this.nearLevels.slice(0, H(this.values[3])).forEach((n, r) => this.dispatch(e, this.nearDownPipeline, n, 16 * t << r)), this.dispatch(e, this.coarsePipeline, this.groups[3], 16 * t), this.dispatch(e, this.bokehPipeline, this.groups[1], 8 * t), this.joinPipeline && this.dispatch(e, this.joinPipeline, this.groups[6]);
740
932
  }
741
933
  dispatch(e, t, n, r = 8) {
742
934
  t === this.cocPipeline && this.captureTarget?.resolve(e.encoder, !0);
@@ -745,13 +937,13 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
745
937
  }
746
938
  present(e) {
747
939
  let t = this.paused;
748
- t && (this.captureTarget.resolve(e.encoder, !0), this.groups[4] ??= S(e.device, this.presentPipeline, [this.sceneView])), this.attachment.view = this.originalColor, this.attachment.loadOp = s(e, "clear");
940
+ t && (this.captureTarget.resolve(e.encoder, !0), this.groups[4] ??= C(e.device, this.presentPipeline, [this.sceneView])), this.attachment.view = this.originalColor, this.attachment.loadOp = s(e, "clear");
749
941
  let n = e.encoder.beginRenderPass(this.presentation);
750
942
  n.setPipeline(this.presentPipeline), n.setBindGroup(0, this.groups[t ? 4 : 2]), o(n, e), n.draw(3), n.end(), this.captureTarget.restore(e);
751
943
  }
752
944
  stage(e, t, n) {
753
- let r = e.device, i = this.host.depthTexture, a = n.format + n.multisample.count;
754
- if ((this.coc?.width !== e.width || this.coc.height !== e.height) && (this.releaseTextures(), this.targets(r, e.width, e.height)), this.sceneDepth !== i && (this.sceneDepth = i, this.groups[0] = this.cocGroup(r, i)), this.input !== t && this.halfUniform ? (this.input = t, this.halfGroups(r, t)) : this.input !== t && (this.input = t, this.groups[1] = S(r, this.bokehPipeline, [
945
+ let r = e.device, i = this.host.depthTexture;
946
+ if ((this.coc?.width !== e.width || this.coc.height !== e.height) && (this.releaseTextures(), this.targets(r, e.width, e.height)), this.sceneDepth !== i && (this.sceneDepth = i, this.groups[0] = this.cocGroup(r, i)), this.input !== t && this.halfUniform ? (this.input = t, this.halfGroups(r, t)) : this.input !== t && (this.input = t, this.groups[1] = C(r, this.bokehPipeline, [
755
947
  { buffer: this.uniform },
756
948
  t,
757
949
  this.cocView,
@@ -759,16 +951,22 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
759
951
  this.tileView,
760
952
  this.coarseView,
761
953
  this.nearSampler,
762
- ...this.nearViews
763
- ]), this.groups[3] = S(r, this.coarsePipeline, [
954
+ ...this.nearViews,
955
+ ...$(this.coarseAlphaView, this.blurredAlphaView)
956
+ ]), this.groups[3] = C(r, this.coarsePipeline, [
764
957
  { buffer: this.uniform },
765
958
  t,
766
959
  this.cocView,
767
- this.coarseView
960
+ this.coarseView,
961
+ ...$(this.coarseAlphaView)
768
962
  ]), this.groups[8] = this.nearGroup(r, { buffer: this.uniform }, t, this.cocView)), this.circles(e), this.blur(e), n.hdr) return this.outputView;
769
- this.key !== a && (this.key = a, this.presentPipeline = C(r, z, n.format, { multisample: n.multisample }), this.groups[2] = S(r, this.presentPipeline, [this.outputView])), this.attachment.view = n.view, this.attachment.loadOp = s(e, "clear");
770
- let c = e.encoder.beginRenderPass(this.presentation);
771
- c.setPipeline(this.presentPipeline), c.setBindGroup(0, this.groups[2]), o(c, e), c.draw(3), c.end();
963
+ if (this.key.changed(n, D(this.engine))) {
964
+ let e = A(this.engine, q);
965
+ this.presentPipeline = te(r, e, n.format, { multisample: n.multisample }), this.groups[2] = C(r, this.presentPipeline, [this.outputView]), this.key.done();
966
+ }
967
+ this.attachment.view = n.view, this.attachment.loadOp = s(e, "clear");
968
+ let a = e.encoder.beginRenderPass(this.presentation);
969
+ a.setPipeline(this.presentPipeline), a.setBindGroup(0, this.groups[2]), o(a, e), a.draw(3), a.end();
772
970
  }
773
971
  targets(e, t, n) {
774
972
  let r = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING;
@@ -786,7 +984,7 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
786
984
  usage: r
787
985
  });
788
986
  let i = this.halfUniform ? 2 : 1, a = Math.ceil(t / i), o = Math.ceil(n / i);
789
- this.coarse = e.createTexture({
987
+ if (this.coarse = e.createTexture({
790
988
  size: [Math.ceil(a / 2), Math.ceil(o / 2)],
791
989
  format: "rgba16float",
792
990
  usage: r
@@ -806,8 +1004,15 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
806
1004
  size: [a, o],
807
1005
  format: "rgba16float",
808
1006
  usage: r
809
- }));
810
- let s = Math.min(4, Math.floor(Math.log2(Math.max(a, o))) + 1), c = () => e.createTexture({
1007
+ })), this.alpha) {
1008
+ let t = (t) => e.createTexture({
1009
+ size: t,
1010
+ format: "r32float",
1011
+ usage: r
1012
+ });
1013
+ this.coarseAlpha = t([Math.ceil(a / 2), Math.ceil(o / 2)]), this.coarseAlphaView = this.coarseAlpha.createView(), this.halfUniform && (this.blurredAlpha = t([a, o]), this.blurredAlphaView = this.blurredAlpha.createView());
1014
+ }
1015
+ let s = Math.min(6, Math.floor(Math.log2(Math.max(a, o))) + 1), c = () => e.createTexture({
811
1016
  size: [a, o],
812
1017
  format: "rgba16float",
813
1018
  usage: r,
@@ -825,15 +1030,15 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
825
1030
  this.nearLevels = [];
826
1031
  for (let t = 1; t < s; t++) {
827
1032
  let n = u(t);
828
- this.nearLevels.push(S(e, this.nearDownPipeline, [...d, ...n])), d = n;
1033
+ this.nearLevels.push(C(e, this.nearDownPipeline, [...d, ...n])), d = n;
829
1034
  }
830
1035
  this.cocView = this.coc.createView(), this.outputView = this.output.createView(), this.tileView = this.tiles.createView(), this.coarseView = this.coarse.createView();
831
1036
  }
832
1037
  releaseTextures() {
833
- this.captureTarget?.dispose(), this.captureTarget = void 0, this.scene?.destroy(), this.depth?.destroy(), this.coc?.destroy(), this.output?.destroy(), this.tiles?.destroy(), this.coarse?.destroy(), this.halfScene?.destroy(), this.halfCoc?.destroy(), this.halfTiles?.destroy(), this.blurred?.destroy(), this.nearTaps?.destroy(), this.nearShapes?.destroy(), this.nearScene?.destroy(), this.nearTaps = this.nearShapes = this.nearScene = void 0, this.nearViews = this.nearBase = [], this.nearLevels = [], this.scene = this.depth = this.coc = this.output = this.tiles = this.coarse = void 0, this.halfScene = this.halfCoc = this.halfTiles = this.blurred = void 0, this.sceneView = this.depthView = void 0, this.groups = [], this.presentPipeline = void 0, this.input = this.sceneDepth = this.cocView = this.outputView = this.tileView = this.coarseView = void 0, this.key = "";
1038
+ this.captureTarget?.dispose(), this.captureTarget = void 0, this.scene?.destroy(), this.depth?.destroy(), this.coc?.destroy(), this.output?.destroy(), this.tiles?.destroy(), this.coarse?.destroy(), this.halfScene?.destroy(), this.halfCoc?.destroy(), this.halfTiles?.destroy(), this.blurred?.destroy(), this.nearTaps?.destroy(), this.nearShapes?.destroy(), this.nearScene?.destroy(), this.coarseAlpha?.destroy(), this.blurredAlpha?.destroy(), this.coarseAlpha = this.blurredAlpha = this.coarseAlphaView = this.blurredAlphaView = void 0, this.nearTaps = this.nearShapes = this.nearScene = void 0, this.nearViews = this.nearBase = [], this.nearLevels = [], this.scene = this.depth = this.coc = this.output = this.tiles = this.coarse = void 0, this.halfScene = this.halfCoc = this.halfTiles = this.blurred = void 0, this.sceneView = this.depthView = void 0, this.groups = [], this.presentPipeline = void 0, this.input = this.sceneDepth = this.cocView = this.outputView = this.tileView = this.coarseView = void 0, this.key.reset();
834
1039
  }
835
1040
  release() {
836
- this.releaseTextures(), this.uniform?.destroy(), this.halfUniform?.destroy(), this.uniform = this.halfUniform = void 0, this.halvePipeline = this.joinPipeline = void 0, this.cocPipeline = this.bokehPipeline = this.coarsePipeline = this.tilesPipeline = void 0, this.nearPipeline = this.nearDownPipeline = void 0, this.nearSampler = void 0, this.originalColor = this.originalDepth = void 0, this.shaders.clear();
1041
+ this.releaseTextures(), this.uniform?.destroy(), this.halfUniform?.destroy(), this.uniform = this.halfUniform = void 0, this.halvePipeline = this.joinPipeline = void 0, this.cocPipeline = this.bokehPipeline = this.coarsePipeline = this.tilesPipeline = void 0, this.nearPipeline = this.nearDownPipeline = void 0, this.kernels = this.compatPipelines = void 0, this.nearSampler = void 0, this.originalColor = this.originalDepth = void 0, this.shaders.clear();
837
1042
  }
838
1043
  onDeviceRestored(e) {
839
1044
  if (!this.disposed) {
@@ -856,4 +1061,4 @@ var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
856
1061
  }
857
1062
  };
858
1063
  //#endregion
859
- export { U as DOF };
1064
+ export { re as DOF };