@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +235 -3
- package/dist/autofocus.d.ts +61 -0
- package/dist/autofocus.js +118 -0
- package/dist/compat-CkmQH9nh.js +39 -0
- package/dist/compat.d.ts +22 -0
- package/dist/errors-B1DmBK5g.js +7 -0
- package/dist/index.d.ts +146 -0
- package/dist/index.js +859 -0
- package/dist/lens.d.ts +68 -0
- package/dist/lens.js +156 -0
- package/dist/projection-CS2tLkwf.js +16 -0
- package/dist/shaders.d.ts +15 -0
- package/package.json +63 -6
package/dist/index.js
ADDED
|
@@ -0,0 +1,859 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
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);
|
|
4
|
+
import { t as e } from "./errors-B1DmBK5g.js";
|
|
5
|
+
import { t } from "./projection-CS2tLkwf.js";
|
|
6
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
import { MargoError as r } from "@margoengine/math/errors";
|
|
8
|
+
//#region ../shared/view-rect.js
|
|
9
|
+
function i(e) {
|
|
10
|
+
return e?.view?.viewport;
|
|
11
|
+
}
|
|
12
|
+
function a(e) {
|
|
13
|
+
let t = i(e);
|
|
14
|
+
return t ? [t[2], t[3]] : [e.width, e.height];
|
|
15
|
+
}
|
|
16
|
+
function o(e, t) {
|
|
17
|
+
let n = i(t);
|
|
18
|
+
if (!n) return;
|
|
19
|
+
e.setViewport(n[0], n[1], n[2], n[3], 0, 1);
|
|
20
|
+
let r = t.view.scissor ?? n;
|
|
21
|
+
e.setScissorRect(r[0], r[1], r[2], r[3]);
|
|
22
|
+
}
|
|
23
|
+
function s(e, t = "clear") {
|
|
24
|
+
return e?.view?.index ? "load" : t;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region ../shared/frame-resources.js
|
|
28
|
+
var c = (globalThis[Symbol.for("margoengine.frame-resources")] ??= { devices: /* @__PURE__ */ new WeakMap() }).devices;
|
|
29
|
+
function l(e) {
|
|
30
|
+
let t = c.get(e);
|
|
31
|
+
return t || (c.set(e, t = {
|
|
32
|
+
frame: 0,
|
|
33
|
+
open: !1,
|
|
34
|
+
lost: !1,
|
|
35
|
+
views: /* @__PURE__ */ new Set(),
|
|
36
|
+
drawing: 0,
|
|
37
|
+
retired: [],
|
|
38
|
+
written: /* @__PURE__ */ new WeakMap()
|
|
39
|
+
}), e.lost?.then?.(() => {
|
|
40
|
+
t.lost = !0;
|
|
41
|
+
let e = t.retired;
|
|
42
|
+
t.retired = [];
|
|
43
|
+
for (let t of e) t.destroy();
|
|
44
|
+
})), t;
|
|
45
|
+
}
|
|
46
|
+
function u(e, t) {
|
|
47
|
+
if (!t) return;
|
|
48
|
+
let n = l(e), r = [t, ...n.written.get(t)?.extras ?? []];
|
|
49
|
+
if (n.written.delete(t), !n.lost && n.open && (n.views.size > 1 || n.drawing > 0)) n.retired.push(...r);
|
|
50
|
+
else for (let e of r) e.destroy();
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region ../shared/compat.js
|
|
54
|
+
function d(t, n, r = "full") {
|
|
55
|
+
let i = {
|
|
56
|
+
ldr: 0,
|
|
57
|
+
"half-float": 1,
|
|
58
|
+
full: 2
|
|
59
|
+
};
|
|
60
|
+
if (!Object.hasOwn(i, r)) throw TypeError("minimum must be full, half-float or ldr");
|
|
61
|
+
let a = t?.adapter?.capabilities?.tier;
|
|
62
|
+
if (a && i[a] < i[r]) throw e("unsupported", `${n} requires the ${r} compatibility tier; this device is ${a}`, "compatibility-layer");
|
|
63
|
+
}
|
|
64
|
+
function f(e) {
|
|
65
|
+
return e?.adapterInfo?.vendor === "margo-webgl2" ? "webgl2" : "native";
|
|
66
|
+
}
|
|
67
|
+
function p(e) {
|
|
68
|
+
return f(e) === "native" && e?.features?.has("core-features-and-limits") === !1;
|
|
69
|
+
}
|
|
70
|
+
function m(e, t) {
|
|
71
|
+
return f(e) === "webgl2" ? t() : void 0;
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
//#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({
|
|
76
|
+
layout: t.getBindGroupLayout(0),
|
|
77
|
+
entries: [{
|
|
78
|
+
binding: 0,
|
|
79
|
+
resource: n
|
|
80
|
+
}]
|
|
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
|
+
let a = e.createShaderModule({ code: n }), o = e.createRenderPipeline({
|
|
85
|
+
layout: "auto",
|
|
86
|
+
vertex: {
|
|
87
|
+
module: a,
|
|
88
|
+
entryPoint: "vertex"
|
|
89
|
+
},
|
|
90
|
+
fragment: {
|
|
91
|
+
module: a,
|
|
92
|
+
entryPoint: "fragment",
|
|
93
|
+
targets: []
|
|
94
|
+
},
|
|
95
|
+
depthStencil: {
|
|
96
|
+
format: i,
|
|
97
|
+
depthWriteEnabled: !0,
|
|
98
|
+
depthCompare: "always"
|
|
99
|
+
},
|
|
100
|
+
multisample: r
|
|
101
|
+
});
|
|
102
|
+
return {
|
|
103
|
+
pipeline: o,
|
|
104
|
+
group: g(e, o, t)
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
draws: a ? [s(o(0).replace("_multisampled", ""), { count: r })] : r === 4 ? p(e) ? [
|
|
109
|
+
0,
|
|
110
|
+
1,
|
|
111
|
+
2,
|
|
112
|
+
3
|
|
113
|
+
].map((e) => s(o(e), {
|
|
114
|
+
count: r,
|
|
115
|
+
mask: 1 << e
|
|
116
|
+
})) : [s(o("i32(sample)").replace("p:vec4f)", "p:vec4f,@builtin(sample_index) sample:u32)"), { count: r })] : [s(v, { count: r })],
|
|
117
|
+
descriptor: {
|
|
118
|
+
colorAttachments: [],
|
|
119
|
+
depthStencilAttachment: {
|
|
120
|
+
view: n,
|
|
121
|
+
depthLoadOp: "clear",
|
|
122
|
+
depthClearValue: 0,
|
|
123
|
+
depthStoreOp: "store",
|
|
124
|
+
stencilReadOnly: !0
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
var b = class {
|
|
130
|
+
constructor(e, t, n, r = 4) {
|
|
131
|
+
if (r !== 1 && r !== 4) throw RangeError("Scene samples must be 1 or 4");
|
|
132
|
+
this.samples = r, this.device = e, this.resolvedColor = t, this.resolvedDepth = n, this.textures = [], this.pipelines = /* @__PURE__ */ new Map();
|
|
133
|
+
try {
|
|
134
|
+
if (r === 1) {
|
|
135
|
+
this.color = t.createView(), this.depth = n.createView();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
for (let r of [t, n]) this.textures.push(e.createTexture({
|
|
139
|
+
size: [r.width, r.height],
|
|
140
|
+
format: r.format,
|
|
141
|
+
sampleCount: 4,
|
|
142
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING
|
|
143
|
+
}));
|
|
144
|
+
this.color = this.textures[0].createView(), this.depth = this.textures[1].createView(), this.resolution = { colorAttachments: [{
|
|
145
|
+
view: this.color,
|
|
146
|
+
resolveTarget: t.createView(),
|
|
147
|
+
loadOp: "load",
|
|
148
|
+
storeOp: "store"
|
|
149
|
+
}] }, this.finalResolution = {
|
|
150
|
+
colorAttachments: [{
|
|
151
|
+
...this.resolution.colorAttachments[0],
|
|
152
|
+
storeOp: "discard"
|
|
153
|
+
}],
|
|
154
|
+
depthStencilAttachment: {
|
|
155
|
+
view: this.depth,
|
|
156
|
+
depthLoadOp: "load",
|
|
157
|
+
depthStoreOp: "discard",
|
|
158
|
+
stencilReadOnly: !0
|
|
159
|
+
}
|
|
160
|
+
}, this.depthSource = this.textures[1].createView({ aspect: "depth-only" }), this.resolvedDepthView = n.createView();
|
|
161
|
+
} catch (e) {
|
|
162
|
+
throw this.dispose(), e;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
begin(e) {
|
|
166
|
+
(this.viewContext ??= {}).view = e.view, Object.assign(this.original ??= {}, {
|
|
167
|
+
color: e.color,
|
|
168
|
+
depth: e.depth,
|
|
169
|
+
format: e.format,
|
|
170
|
+
descriptor: e.descriptor,
|
|
171
|
+
owner: x(e)
|
|
172
|
+
}), e.color = this.color, e.depth = this.depth, e.format = this.resolvedColor.format, this.baseDescriptor !== e.descriptor && (this.baseDescriptor = e.descriptor, this.descriptor = {
|
|
173
|
+
...e.descriptor,
|
|
174
|
+
depthStencil: {
|
|
175
|
+
...e.descriptor.depthStencil,
|
|
176
|
+
format: this.resolvedDepth.format
|
|
177
|
+
},
|
|
178
|
+
multisample: { count: this.samples }
|
|
179
|
+
}), e.descriptor = this.descriptor, e[_] = this, this.retired = !1;
|
|
180
|
+
}
|
|
181
|
+
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();
|
|
191
|
+
}
|
|
192
|
+
resolve(e, t = !1, n = !0) {
|
|
193
|
+
if (this.samples !== 1) {
|
|
194
|
+
if (n && this.copyDepth(e, this.resolvedDepthView, 1), t) {
|
|
195
|
+
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;
|
|
197
|
+
}
|
|
198
|
+
e.beginRenderPass(t ? this.finalResolution : this.resolution).end();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
restore(e) {
|
|
202
|
+
let t = this.original;
|
|
203
|
+
this.samples === 1 ? (Object.assign(e, {
|
|
204
|
+
color: t.color,
|
|
205
|
+
depth: t.depth,
|
|
206
|
+
format: t.format,
|
|
207
|
+
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);
|
|
214
|
+
}
|
|
215
|
+
present(e) {
|
|
216
|
+
this.resolve(e.encoder, !0), this.restore(e);
|
|
217
|
+
let t = e.descriptor.multisample?.count ?? 1;
|
|
218
|
+
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);}" });
|
|
220
|
+
this.presentation = this.device.createRenderPipeline({
|
|
221
|
+
layout: "auto",
|
|
222
|
+
vertex: {
|
|
223
|
+
module: n,
|
|
224
|
+
entryPoint: "vertex"
|
|
225
|
+
},
|
|
226
|
+
fragment: {
|
|
227
|
+
module: n,
|
|
228
|
+
entryPoint: "fragment",
|
|
229
|
+
targets: [{ format: e.format }]
|
|
230
|
+
},
|
|
231
|
+
multisample: e.descriptor.multisample
|
|
232
|
+
}), this.presentationGroup = g(this.device, this.presentation, this.resolvedColor.createView()), this.presentationFormat = e.format, this.presentationSamples = t;
|
|
233
|
+
}
|
|
234
|
+
let n = this.presentationPass ??= { colorAttachments: [{
|
|
235
|
+
view: e.color,
|
|
236
|
+
loadOp: "clear",
|
|
237
|
+
storeOp: "store"
|
|
238
|
+
}] };
|
|
239
|
+
n.colorAttachments[0].view = e.color, n.colorAttachments[0].loadOp = s(e, "clear");
|
|
240
|
+
let r = e.encoder.beginRenderPass(n);
|
|
241
|
+
r.setPipeline(this.presentation), r.setBindGroup(0, this.presentationGroup), o(r, e), r.draw(3), r.end();
|
|
242
|
+
}
|
|
243
|
+
dispose() {
|
|
244
|
+
for (let e of this.textures) u(this.device, e);
|
|
245
|
+
this.textures = [], this.pipelines.clear();
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
function x(e) {
|
|
249
|
+
let t = e[_];
|
|
250
|
+
return t?.color === e.color && t.depth === e.depth ? t : void 0;
|
|
251
|
+
}
|
|
252
|
+
//#endregion
|
|
253
|
+
//#region ../shared/gpu.js
|
|
254
|
+
function S(e, t, n) {
|
|
255
|
+
return e.createBindGroup({
|
|
256
|
+
layout: t.getBindGroupLayout(0),
|
|
257
|
+
entries: n.map((e, t) => ({
|
|
258
|
+
binding: t,
|
|
259
|
+
resource: e
|
|
260
|
+
}))
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
function C(e, t, n, r = {}) {
|
|
264
|
+
let i = e.createShaderModule({ code: t });
|
|
265
|
+
return e.createRenderPipeline({
|
|
266
|
+
layout: "auto",
|
|
267
|
+
vertex: {
|
|
268
|
+
module: i,
|
|
269
|
+
entryPoint: "vertex"
|
|
270
|
+
},
|
|
271
|
+
fragment: {
|
|
272
|
+
module: i,
|
|
273
|
+
entryPoint: "fragment",
|
|
274
|
+
targets: [{ format: n }]
|
|
275
|
+
},
|
|
276
|
+
...r
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
function w(e, t, n = "main") {
|
|
280
|
+
return e.createComputePipeline({
|
|
281
|
+
layout: "auto",
|
|
282
|
+
compute: {
|
|
283
|
+
module: e.createShaderModule({ code: t }),
|
|
284
|
+
entryPoint: n
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
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}
|
|
289
|
+
@group(0) @binding(1) var depth:texture_depth_2d;
|
|
290
|
+
@group(0) @binding(2) var coc:texture_storage_2d<rg32float,write>;
|
|
291
|
+
@group(0) @binding(3) var tiles:texture_storage_2d<r32float,write>;
|
|
292
|
+
${A}
|
|
293
|
+
@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
|
+
var near=0.0;
|
|
295
|
+
if(all(id.xy<textureDimensions(coc))){
|
|
296
|
+
let d=textureLoad(depth,vec2i(id.xy),0);
|
|
297
|
+
let ortho=p.ortho.x>0.5;
|
|
298
|
+
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);
|
|
299
|
+
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;
|
|
300
|
+
textureStore(coc,vec2i(id.xy),vec4f(radius,z,0,0));near=-radius;
|
|
301
|
+
}
|
|
302
|
+
let m=groupMax(near,local);
|
|
303
|
+
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;
|
|
305
|
+
for(var i=0u;i<${e}u;i++){
|
|
306
|
+
let farPixel=clamp(pixel+vec2i(round(spiral(i,${e}.0)*center.x)),vec2i(0),limit);
|
|
307
|
+
let farCoC=textureLoad(coc,farPixel,0).xy;let farW=1.0;farTotal+=farW;
|
|
308
|
+
if(farCoC.x>=0.0&&farCoC.y>=center.y-max(0.001,center.y*0.01)){farColor+=textureLoad(scene,farPixel,0).rgb;farWeight+=1.0;}
|
|
309
|
+
else if(farCoC.x>0.5){
|
|
310
|
+
let w=farW*covers(vec2f(farPixel-pixel),farCoC.x,pixel)*center.x*center.x/(farCoC.x*farCoC.x);
|
|
311
|
+
frontColor+=textureLoad(scene,farPixel,0).rgb*w;frontWeight+=w;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
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}
|
|
316
|
+
@group(0) @binding(3) var coarse:texture_storage_2d<rgba16float,write>;
|
|
317
|
+
@compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u) {
|
|
318
|
+
if(any(id.xy>=textureDimensions(coarse))){return;}
|
|
319
|
+
let pixel=vec2i(id.xy)*2;let limit=vec2i(textureDimensions(coc))-1;let center=textureLoad(coc,pixel,0).xy;
|
|
320
|
+
var farColor=textureLoad(scene,pixel,0).rgb;var farWeight=1.0;
|
|
321
|
+
let wide=center.x>3.19&&p.frame.w!=0.0;
|
|
322
|
+
if(wide){${M(64)}}
|
|
323
|
+
textureStore(coarse,id.xy,vec4f(farColor/farWeight,select(0.0,center.y,wide)));
|
|
324
|
+
}`, F = `${k}
|
|
325
|
+
@group(0) @binding(1) var scene:texture_2d<f32>;
|
|
326
|
+
@group(0) @binding(2) var coc:texture_2d<f32>;
|
|
327
|
+
@group(0) @binding(3) var nearTaps:texture_storage_2d<rgba16float,write>;
|
|
328
|
+
@group(0) @binding(4) var nearShapes:texture_storage_2d<rgba16float,write>;
|
|
329
|
+
@group(0) @binding(5) var nearScene:texture_storage_2d<rgba16float,write>;
|
|
330
|
+
@compute @workgroup_size(8,8) fn main(@builtin(global_invocation_id) id:vec3u) {
|
|
331
|
+
if(any(id.xy>=textureDimensions(nearTaps))||p.frame.w==0.0){return;}
|
|
332
|
+
let at=vec2i(id.xy);let s=textureLoad(coc,at,0).xy;let color=textureLoad(scene,at,0).rgb;
|
|
333
|
+
let g=select(0.0,1.0/max(s.x*s.x,1.0),s.x< -0.5);
|
|
334
|
+
textureStore(nearTaps,at,vec4f(color*g,g));
|
|
335
|
+
textureStore(nearShapes,at,vec4f(-s.x*g,s.y*g,0,0));
|
|
336
|
+
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}
|
|
339
|
+
@group(0) @binding(3) var result:texture_storage_2d<rgba16float,write>;
|
|
340
|
+
@group(0) @binding(4) var tiles:texture_2d<f32>;
|
|
341
|
+
@group(0) @binding(5) var coarse:texture_2d<f32>;
|
|
342
|
+
@group(0) @binding(6) var filtered:sampler;
|
|
343
|
+
@group(0) @binding(7) var nearTaps:texture_2d<f32>;
|
|
344
|
+
@group(0) @binding(8) var nearShapes:texture_2d<f32>;
|
|
345
|
+
@group(0) @binding(9) var nearScene:texture_2d<f32>;
|
|
346
|
+
${A}
|
|
347
|
+
fn disk(i:u32)->vec2f {return spiral(i,64.0);}
|
|
348
|
+
const tapArea=1.0;
|
|
349
|
+
fn nearLod(radius:f32)->f32 {return max(log2(radius*0.2215567*sqrt(tapArea)/1.7320508),0.0);}
|
|
350
|
+
@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;
|
|
352
|
+
var mine=0.0;
|
|
353
|
+
for(var t=i32(local);t<span*span;t+=64){
|
|
354
|
+
let o=vec2i(t%span,t/span)-reach;let at=vec2i(tile.xy)+o;
|
|
355
|
+
if(all(at>=vec2i(0))&&all(at<count)){
|
|
356
|
+
let m=textureLoad(tiles,at,0).x;let gap=vec2f(max(abs(o)*8-7,vec2i(0)));
|
|
357
|
+
if(length(gap)<=m+1.0+footprint){mine=max(mine,m);}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
let nearMax=groupMax(mine,local);
|
|
361
|
+
let size=textureDimensions(result);if(any(id.xy>=size)){return;}
|
|
362
|
+
let pixel=vec2i(id.xy);let center=textureLoad(coc,pixel,0).xy;
|
|
363
|
+
let sharp=textureLoad(scene,pixel,0);
|
|
364
|
+
if(p.frame.w==0.0){textureStore(result,pixel,sharp);return;}
|
|
365
|
+
var farColor=sharp.rgb;var farWeight=1.0;
|
|
366
|
+
var nearColor=vec3f(0);var nearWeight=0.0;var backColor=vec3f(0);var backWeight=0.0;
|
|
367
|
+
let limit=vec2i(size)-1;
|
|
368
|
+
if(center.x>3.19){
|
|
369
|
+
let q=pixel/2;let f=vec2f(pixel%2)*0.5;let last=vec2i(textureDimensions(coarse))-1;var up=vec4f(0);
|
|
370
|
+
for(var k=0;k<4;k++){
|
|
371
|
+
let o=vec2i(k%2,k/2);let w=mix(1.0-f.x,f.x,f32(o.x))*mix(1.0-f.y,f.y,f32(o.y));
|
|
372
|
+
let s=textureLoad(coarse,min(q+o,last),0);
|
|
373
|
+
if(w>0.0&&abs(s.w-center.y)<=0.05*center.y){up+=vec4f(s.rgb,1.0)*w;}
|
|
374
|
+
}
|
|
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);
|
|
378
|
+
let low=floor(lod);let blend=lod-low;
|
|
379
|
+
let nearSize=vec2f(textureDimensions(nearTaps));
|
|
380
|
+
for(var i=0u;i<reachTaps;i++){
|
|
381
|
+
let nearTap=disk(i)*R;let nearPixel=pixel+vec2i(round(nearTap));
|
|
382
|
+
if(any(nearPixel<vec2i(0))||any(nearPixel>limit)){continue;}
|
|
383
|
+
let at=(vec2f(pixel)+0.5+nearTap)/nearSize;${L("source", "nearTaps")}
|
|
384
|
+
var share=0.0;var tint=vec3f(0);
|
|
385
|
+
if(source.w>0.0){
|
|
386
|
+
${L("nearForm", "nearShapes")}let form=nearForm.xy/source.w;let radius=form.x;tint=source.rgb/source.w;
|
|
387
|
+
if(form.y<=center.y+max(0.001,center.y*0.01)){
|
|
388
|
+
share=min(source.w*max(radius*radius,1.0),1.0);
|
|
389
|
+
let inside=clamp(radius-length(nearTap)+0.5,0.0,1.0);
|
|
390
|
+
let weight=R*R*source.w*inside;
|
|
391
|
+
nearColor+=tint*weight;nearWeight+=weight;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
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;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if(center.x< -0.5){
|
|
399
|
+
let weight=R*R/(center.x*center.x);nearColor+=sharp.rgb*weight;nearWeight+=weight;
|
|
400
|
+
if(backWeight>0.0){farColor=backColor;farWeight=backWeight;}
|
|
401
|
+
}
|
|
402
|
+
let coverage=clamp(nearWeight/64.0,0.0,1.0);
|
|
403
|
+
let color=mix(farColor/farWeight,nearColor/max(nearWeight,0.0001),coverage);
|
|
404
|
+
textureStore(result,pixel,vec4f(color,sharp.a));
|
|
405
|
+
}`, z = `
|
|
406
|
+
@group(0) @binding(0) var scene:texture_2d<f32>;
|
|
407
|
+
@vertex fn vertex(@builtin(vertex_index) i:u32)->@builtin(position) vec4f {
|
|
408
|
+
let xy=array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3));return vec4f(xy[i],0,1);
|
|
409
|
+
}
|
|
410
|
+
${T}
|
|
411
|
+
@fragment fn fragment(@builtin(position) pos:vec4f)->@location(0) vec4f {
|
|
412
|
+
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>;
|
|
414
|
+
@group(0) @binding(1) var coc:texture_2d<f32>;
|
|
415
|
+
@group(0) @binding(2) var halfScene:texture_storage_2d<rgba16float,write>;
|
|
416
|
+
@group(0) @binding(3) var halfCoc:texture_storage_2d<rg32float,write>;
|
|
417
|
+
@group(0) @binding(4) var tiles:texture_storage_2d<r32float,write>;
|
|
418
|
+
${A}
|
|
419
|
+
@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
|
+
var near=0.0;
|
|
421
|
+
if(all(id.xy<textureDimensions(halfCoc))){
|
|
422
|
+
let last=vec2i(textureDimensions(coc))-1;var color=vec4f(0);var c=vec2f(0,3.4e38);
|
|
423
|
+
for(var k=0;k<4;k++){
|
|
424
|
+
let at=min(vec2i(id.xy)*2+vec2i(k%2,k/2),last);color+=textureLoad(scene,at,0);
|
|
425
|
+
let s=textureLoad(coc,at,0).xy;if(s.y<c.y){c=s;}
|
|
426
|
+
}
|
|
427
|
+
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;
|
|
428
|
+
}
|
|
429
|
+
let m=groupMax(near,local);
|
|
430
|
+
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) {
|
|
433
|
+
let t = (e) => {
|
|
434
|
+
let t = e.split("f32(i)*2.39996323");
|
|
435
|
+
if (t.length < 2) throw new r("unsupported", "DOF temporal needs the gather kernels of its own version");
|
|
436
|
+
return t.join("(f32(i)*2.39996323+p.ortho.w)");
|
|
437
|
+
}, n = "textureStore(result,pixel,vec4f(color,sharp.a));", i = "if(p.frame.w==0.0){textureStore(result,pixel,sharp);return;}";
|
|
438
|
+
if (e.bokehShader.split(n).length !== 2 || e.bokehShader.split(i).length !== 2) throw new r("unsupported", "DOF temporal needs the gather kernels of its own version");
|
|
439
|
+
return {
|
|
440
|
+
...e,
|
|
441
|
+
coarseShader: t(e.coarseShader),
|
|
442
|
+
bokehShader: t(e.bokehShader).replace(n, "textureStore(result,pixel,vec4f(color,coverage));").replace(i, "if(p.frame.w==0.0){textureStore(result,pixel,vec4f(sharp.rgb,0));return;}")
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
//#endregion
|
|
446
|
+
//#region src/index.ts
|
|
447
|
+
var U = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
448
|
+
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 = {
|
|
450
|
+
view: void 0,
|
|
451
|
+
loadOp: "clear",
|
|
452
|
+
storeOp: "store"
|
|
453
|
+
}, this.presentation = { colorAttachments: [this.attachment] }, this.disposed = !1, this.key = "", this.validate();
|
|
454
|
+
}
|
|
455
|
+
static attach(t, r = {}) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
456
|
+
let i = new e(r);
|
|
457
|
+
return n(t).use(i), i;
|
|
458
|
+
}
|
|
459
|
+
get depthTexture() {
|
|
460
|
+
return this.host ? this.host.depthTexture : this.depth;
|
|
461
|
+
}
|
|
462
|
+
get outputTexture() {
|
|
463
|
+
return this.paused ? this.scene : this.output;
|
|
464
|
+
}
|
|
465
|
+
get focalLength() {
|
|
466
|
+
return this.values[1] || (this.options.focalLength ?? .05);
|
|
467
|
+
}
|
|
468
|
+
get enabled() {
|
|
469
|
+
return this.options.enabled ?? !0;
|
|
470
|
+
}
|
|
471
|
+
set enabled(e) {
|
|
472
|
+
if (typeof e != "boolean") throw TypeError("DOF enabled must be boolean");
|
|
473
|
+
this.options.enabled = e;
|
|
474
|
+
}
|
|
475
|
+
joinStack(e) {
|
|
476
|
+
if (this.engine || this.host || this.disposed) throw new r("attached", "DOF is already attached; create a new DOF");
|
|
477
|
+
return this.host = e, {
|
|
478
|
+
kind: "hdr",
|
|
479
|
+
render: (e, t, n) => this.stage(e, t, n)
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
validate() {
|
|
483
|
+
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");
|
|
485
|
+
if (t !== void 0 && !(Number.isFinite(t) && t > 0) || e.sensorFit !== void 0 && ![
|
|
486
|
+
"auto",
|
|
487
|
+
"horizontal",
|
|
488
|
+
"vertical"
|
|
489
|
+
].includes(e.sensorFit)) throw RangeError("Invalid DOF filmGauge or sensorFit; use millimetres > 0 and 'auto', 'horizontal' or 'vertical'");
|
|
490
|
+
}
|
|
491
|
+
setup(e) {
|
|
492
|
+
if (this.disposed || !e.gpu) throw new r(this.disposed ? "disposed" : "unsupported", "DOF requires WebGPU and a live effect; attach a new DOF to a WebGPU engine; see https://margoengine.com/errors#webgpu");
|
|
493
|
+
if (d(e.gpu, "DOF HDR processing", "half-float"), !this.host && (e.hasPlugin("ssao") || e.hasPlugin("bloom") || e.hasPlugin("post-processing") || e.hasPlugin("tonemap") && !e.hasPlugin(this.name))) throw new r("state", "DOF owns scene capture; attach it before Tonemap, without SSAO or PostProcessing");
|
|
494
|
+
this.engine = e, this.uniform = e.gpu.createBuffer({
|
|
495
|
+
size: 48,
|
|
496
|
+
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({
|
|
505
|
+
size: 48,
|
|
506
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
507
|
+
}), this.halvePipeline = w(e.gpu, B), this.joinPipeline = w(e.gpu, V)), this.cocPipeline = e.gpu.createComputePipeline({
|
|
508
|
+
layout: "auto",
|
|
509
|
+
compute: {
|
|
510
|
+
module: e.gpu.createShaderModule({ code: a.cocShader }),
|
|
511
|
+
entryPoint: "main"
|
|
512
|
+
}
|
|
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({
|
|
514
|
+
magFilter: "linear",
|
|
515
|
+
minFilter: "linear",
|
|
516
|
+
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
|
+
});
|
|
527
|
+
}
|
|
528
|
+
get paused() {
|
|
529
|
+
return this.waiting && !this.host;
|
|
530
|
+
}
|
|
531
|
+
registerPasses(e) {
|
|
532
|
+
e.add({
|
|
533
|
+
name: "dof:capture",
|
|
534
|
+
order: -55,
|
|
535
|
+
execute: (e) => {
|
|
536
|
+
e.backend === "webgpu" && this.capture(e);
|
|
537
|
+
}
|
|
538
|
+
}), !this.host && (e.add({
|
|
539
|
+
name: "dof:coc",
|
|
540
|
+
order: 20,
|
|
541
|
+
execute: (e) => {
|
|
542
|
+
e.backend === "webgpu" && !this.paused && this.circles(e);
|
|
543
|
+
}
|
|
544
|
+
}), e.add({
|
|
545
|
+
name: "dof:bokeh",
|
|
546
|
+
order: 21,
|
|
547
|
+
execute: (e) => {
|
|
548
|
+
e.backend === "webgpu" && !this.paused && this.blur(e);
|
|
549
|
+
}
|
|
550
|
+
}), e.add({
|
|
551
|
+
name: "dof:present",
|
|
552
|
+
order: 30,
|
|
553
|
+
execute: (e) => {
|
|
554
|
+
e.backend === "webgpu" && this.present(e);
|
|
555
|
+
}
|
|
556
|
+
}));
|
|
557
|
+
}
|
|
558
|
+
lens(e, t, n) {
|
|
559
|
+
let r = this.options.sensorFit ?? "auto";
|
|
560
|
+
return e / (2e3 * Math.tan(t * Math.PI / 360) * (r === "horizontal" || r === "auto" && n >= 1 ? n : 1));
|
|
561
|
+
}
|
|
562
|
+
capture(e) {
|
|
563
|
+
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");
|
|
569
|
+
if (!this.host) {
|
|
570
|
+
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
|
+
this.resize(e), this.originalColor = e.color, this.originalDepth = e.depth;
|
|
572
|
+
let t = this.shaders.get(e.shader);
|
|
573
|
+
if (t === void 0) {
|
|
574
|
+
if (t = D(e.shader), t === e.shader) throw new r("unsupported", O("DOF"));
|
|
575
|
+
this.shaders.set(e.shader, t);
|
|
576
|
+
}
|
|
577
|
+
e.shader = t, this.captureTarget.begin(e);
|
|
578
|
+
}
|
|
579
|
+
if (e.device.queue.writeBuffer(this.uniform, 0, i), this.halfUniform) {
|
|
580
|
+
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);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
resize(e) {
|
|
585
|
+
let t = e.descriptor.depthStencil.format;
|
|
586
|
+
if (this.presentPipeline && this.scene?.width === e.width && this.scene.height === e.height && this.format === e.format && this.depthFormat === t && this.captureTarget?.samples === (e.descriptor.multisample?.count ?? 4)) return;
|
|
587
|
+
this.releaseTextures(), this.format = e.format, this.depthFormat = t;
|
|
588
|
+
let n = GPUTextureUsage.TEXTURE_BINDING;
|
|
589
|
+
this.scene = e.device.createTexture({
|
|
590
|
+
size: [e.width, e.height],
|
|
591
|
+
format: "rgba16float",
|
|
592
|
+
usage: n | GPUTextureUsage.RENDER_ATTACHMENT
|
|
593
|
+
}), this.depth = e.device.createTexture({
|
|
594
|
+
size: [e.width, e.height],
|
|
595
|
+
format: t,
|
|
596
|
+
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 });
|
|
599
|
+
this.presentPipeline = e.device.createRenderPipeline({
|
|
600
|
+
layout: "auto",
|
|
601
|
+
vertex: {
|
|
602
|
+
module: s,
|
|
603
|
+
entryPoint: "vertex"
|
|
604
|
+
},
|
|
605
|
+
fragment: {
|
|
606
|
+
module: s,
|
|
607
|
+
entryPoint: "fragment",
|
|
608
|
+
targets: [{ format: e.format }]
|
|
609
|
+
},
|
|
610
|
+
multisample: e.descriptor.multisample
|
|
611
|
+
}), this.groups = [
|
|
612
|
+
this.cocGroup(e.device, this.depth, !0),
|
|
613
|
+
this.halfUniform ? void 0 : e.device.createBindGroup({
|
|
614
|
+
layout: this.bokehPipeline.getBindGroupLayout(0),
|
|
615
|
+
entries: [
|
|
616
|
+
{
|
|
617
|
+
binding: 0,
|
|
618
|
+
resource: { buffer: this.uniform }
|
|
619
|
+
},
|
|
620
|
+
{
|
|
621
|
+
binding: 1,
|
|
622
|
+
resource: this.sceneView
|
|
623
|
+
},
|
|
624
|
+
{
|
|
625
|
+
binding: 2,
|
|
626
|
+
resource: r
|
|
627
|
+
},
|
|
628
|
+
{
|
|
629
|
+
binding: 3,
|
|
630
|
+
resource: i
|
|
631
|
+
},
|
|
632
|
+
{
|
|
633
|
+
binding: 4,
|
|
634
|
+
resource: a
|
|
635
|
+
},
|
|
636
|
+
{
|
|
637
|
+
binding: 5,
|
|
638
|
+
resource: o
|
|
639
|
+
},
|
|
640
|
+
{
|
|
641
|
+
binding: 6,
|
|
642
|
+
resource: this.nearSampler
|
|
643
|
+
},
|
|
644
|
+
...this.nearViews.map((e, t) => ({
|
|
645
|
+
binding: 7 + t,
|
|
646
|
+
resource: e
|
|
647
|
+
}))
|
|
648
|
+
]
|
|
649
|
+
}),
|
|
650
|
+
e.device.createBindGroup({
|
|
651
|
+
layout: this.presentPipeline.getBindGroupLayout(0),
|
|
652
|
+
entries: [{
|
|
653
|
+
binding: 0,
|
|
654
|
+
resource: i
|
|
655
|
+
}]
|
|
656
|
+
}),
|
|
657
|
+
this.halfUniform ? void 0 : S(e.device, this.coarsePipeline, [
|
|
658
|
+
{ buffer: this.uniform },
|
|
659
|
+
this.sceneView,
|
|
660
|
+
r,
|
|
661
|
+
o
|
|
662
|
+
])
|
|
663
|
+
], this.halfUniform || (this.groups[8] = this.nearGroup(e.device, { buffer: this.uniform }, this.sceneView, r)), this.halfUniform && this.halfGroups(e.device, this.sceneView);
|
|
664
|
+
}
|
|
665
|
+
halfGroups(e, t) {
|
|
666
|
+
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, [
|
|
668
|
+
n,
|
|
669
|
+
r,
|
|
670
|
+
i,
|
|
671
|
+
o,
|
|
672
|
+
a,
|
|
673
|
+
this.coarseView,
|
|
674
|
+
this.nearSampler,
|
|
675
|
+
...this.nearViews
|
|
676
|
+
]), this.groups[3] = S(e, this.coarsePipeline, [
|
|
677
|
+
n,
|
|
678
|
+
r,
|
|
679
|
+
i,
|
|
680
|
+
this.coarseView
|
|
681
|
+
]), this.groups[8] = this.nearGroup(e, n, r, i), this.groups[5] = S(e, this.halvePipeline, [
|
|
682
|
+
t,
|
|
683
|
+
this.cocView,
|
|
684
|
+
r,
|
|
685
|
+
i,
|
|
686
|
+
...this.tilesPipeline ? [] : [a]
|
|
687
|
+
]), this.groups[6] = S(e, this.joinPipeline, [
|
|
688
|
+
t,
|
|
689
|
+
this.cocView,
|
|
690
|
+
o,
|
|
691
|
+
this.outputView
|
|
692
|
+
]), this.tilesPipeline && (this.groups[7] = S(e, this.tilesPipeline, [i, a]));
|
|
693
|
+
}
|
|
694
|
+
nearGroup(e, t, n, r) {
|
|
695
|
+
return S(e, this.nearPipeline, [
|
|
696
|
+
t,
|
|
697
|
+
n,
|
|
698
|
+
r,
|
|
699
|
+
...this.nearBase
|
|
700
|
+
]);
|
|
701
|
+
}
|
|
702
|
+
cocGroup(e, t, n = !1) {
|
|
703
|
+
return this.tilesPipeline ? (this.tilesGroup = S(e, this.tilesPipeline, [this.cocView, this.tileView]), S(e, this.cocPipeline, [
|
|
704
|
+
{ buffer: this.uniform },
|
|
705
|
+
t.createView({ aspect: "depth-only" }),
|
|
706
|
+
this.cocView
|
|
707
|
+
])) : n ? e.createBindGroup({
|
|
708
|
+
layout: this.cocPipeline.getBindGroupLayout(0),
|
|
709
|
+
entries: [
|
|
710
|
+
{
|
|
711
|
+
binding: 0,
|
|
712
|
+
resource: { buffer: this.uniform }
|
|
713
|
+
},
|
|
714
|
+
{
|
|
715
|
+
binding: 1,
|
|
716
|
+
resource: t.createView({ aspect: "depth-only" })
|
|
717
|
+
},
|
|
718
|
+
{
|
|
719
|
+
binding: 2,
|
|
720
|
+
resource: this.cocView
|
|
721
|
+
},
|
|
722
|
+
{
|
|
723
|
+
binding: 3,
|
|
724
|
+
resource: this.tileView
|
|
725
|
+
}
|
|
726
|
+
]
|
|
727
|
+
}) : S(e, this.cocPipeline, [
|
|
728
|
+
{ buffer: this.uniform },
|
|
729
|
+
t.createView({ aspect: "depth-only" }),
|
|
730
|
+
this.cocView,
|
|
731
|
+
this.tileView
|
|
732
|
+
]);
|
|
733
|
+
}
|
|
734
|
+
circles(e) {
|
|
735
|
+
this.dispatch(e, this.cocPipeline, this.groups[0]), this.tilesPipeline && !this.halvePipeline && this.dispatch(e, this.tilesPipeline, this.tilesGroup, 64), this.halvePipeline && this.dispatch(e, this.halvePipeline, this.groups[5], 16), this.tilesPipeline && this.halvePipeline && this.dispatch(e, this.tilesPipeline, this.groups[7], 128);
|
|
736
|
+
}
|
|
737
|
+
blur(e) {
|
|
738
|
+
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]);
|
|
740
|
+
}
|
|
741
|
+
dispatch(e, t, n, r = 8) {
|
|
742
|
+
t === this.cocPipeline && this.captureTarget?.resolve(e.encoder, !0);
|
|
743
|
+
let i = e.encoder.beginComputePass();
|
|
744
|
+
i.setPipeline(t), i.setBindGroup(0, n), i.dispatchWorkgroups(Math.ceil(e.width / r), Math.ceil(e.height / r)), i.end();
|
|
745
|
+
}
|
|
746
|
+
present(e) {
|
|
747
|
+
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");
|
|
749
|
+
let n = e.encoder.beginRenderPass(this.presentation);
|
|
750
|
+
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
|
+
}
|
|
752
|
+
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, [
|
|
755
|
+
{ buffer: this.uniform },
|
|
756
|
+
t,
|
|
757
|
+
this.cocView,
|
|
758
|
+
this.outputView,
|
|
759
|
+
this.tileView,
|
|
760
|
+
this.coarseView,
|
|
761
|
+
this.nearSampler,
|
|
762
|
+
...this.nearViews
|
|
763
|
+
]), this.groups[3] = S(r, this.coarsePipeline, [
|
|
764
|
+
{ buffer: this.uniform },
|
|
765
|
+
t,
|
|
766
|
+
this.cocView,
|
|
767
|
+
this.coarseView
|
|
768
|
+
]), 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();
|
|
772
|
+
}
|
|
773
|
+
targets(e, t, n) {
|
|
774
|
+
let r = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING;
|
|
775
|
+
this.coc = e.createTexture({
|
|
776
|
+
size: [t, n],
|
|
777
|
+
format: "rg32float",
|
|
778
|
+
usage: r
|
|
779
|
+
}), this.output = e.createTexture({
|
|
780
|
+
size: [t, n],
|
|
781
|
+
format: "rgba16float",
|
|
782
|
+
usage: r | GPUTextureUsage.COPY_SRC
|
|
783
|
+
}), this.tiles = e.createTexture({
|
|
784
|
+
size: [Math.ceil(t / 8), Math.ceil(n / 8)],
|
|
785
|
+
format: "r32float",
|
|
786
|
+
usage: r
|
|
787
|
+
});
|
|
788
|
+
let i = this.halfUniform ? 2 : 1, a = Math.ceil(t / i), o = Math.ceil(n / i);
|
|
789
|
+
this.coarse = e.createTexture({
|
|
790
|
+
size: [Math.ceil(a / 2), Math.ceil(o / 2)],
|
|
791
|
+
format: "rgba16float",
|
|
792
|
+
usage: r
|
|
793
|
+
}), this.halfUniform && (this.halfScene = e.createTexture({
|
|
794
|
+
size: [a, o],
|
|
795
|
+
format: "rgba16float",
|
|
796
|
+
usage: r
|
|
797
|
+
}), this.halfCoc = e.createTexture({
|
|
798
|
+
size: [a, o],
|
|
799
|
+
format: "rg32float",
|
|
800
|
+
usage: r
|
|
801
|
+
}), this.halfTiles = e.createTexture({
|
|
802
|
+
size: [Math.ceil(a / 8), Math.ceil(o / 8)],
|
|
803
|
+
format: "r32float",
|
|
804
|
+
usage: r
|
|
805
|
+
}), this.blurred = e.createTexture({
|
|
806
|
+
size: [a, o],
|
|
807
|
+
format: "rgba16float",
|
|
808
|
+
usage: r
|
|
809
|
+
}));
|
|
810
|
+
let s = Math.min(4, Math.floor(Math.log2(Math.max(a, o))) + 1), c = () => e.createTexture({
|
|
811
|
+
size: [a, o],
|
|
812
|
+
format: "rgba16float",
|
|
813
|
+
usage: r,
|
|
814
|
+
mipLevelCount: s
|
|
815
|
+
}), l = [
|
|
816
|
+
this.nearTaps = c(),
|
|
817
|
+
this.nearShapes = c(),
|
|
818
|
+
this.nearScene = c()
|
|
819
|
+
], u = (e) => l.map((t) => t.createView({
|
|
820
|
+
baseMipLevel: e,
|
|
821
|
+
mipLevelCount: 1
|
|
822
|
+
}));
|
|
823
|
+
this.nearViews = l.map((e) => e.createView()), this.nearBase = u(0);
|
|
824
|
+
let d = this.nearBase;
|
|
825
|
+
this.nearLevels = [];
|
|
826
|
+
for (let t = 1; t < s; t++) {
|
|
827
|
+
let n = u(t);
|
|
828
|
+
this.nearLevels.push(S(e, this.nearDownPipeline, [...d, ...n])), d = n;
|
|
829
|
+
}
|
|
830
|
+
this.cocView = this.coc.createView(), this.outputView = this.output.createView(), this.tileView = this.tiles.createView(), this.coarseView = this.coarse.createView();
|
|
831
|
+
}
|
|
832
|
+
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 = "";
|
|
834
|
+
}
|
|
835
|
+
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();
|
|
837
|
+
}
|
|
838
|
+
onDeviceRestored(e) {
|
|
839
|
+
if (!this.disposed) {
|
|
840
|
+
this.release();
|
|
841
|
+
try {
|
|
842
|
+
this.setup(e);
|
|
843
|
+
} catch (e) {
|
|
844
|
+
throw this.dispose(), e;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
dispose() {
|
|
849
|
+
this.disposed || (this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.release(), this.engine = void 0);
|
|
850
|
+
}
|
|
851
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
852
|
+
this.dispose();
|
|
853
|
+
}
|
|
854
|
+
destroy() {
|
|
855
|
+
this.dispose();
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
//#endregion
|
|
859
|
+
export { U as DOF };
|