@plasius/gpu-renderer 0.1.12 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3460 @@
1
+ const DEFAULT_WIDTH = 1280;
2
+ const DEFAULT_HEIGHT = 720;
3
+ const DEFAULT_MAX_DEPTH = 6;
4
+ const DEFAULT_TILE_SIZE = 128;
5
+ const DEFAULT_SAMPLES_PER_PIXEL = 1;
6
+ const DEFAULT_SCENE_OBJECT_CAPACITY = 128;
7
+ const WORKGROUP_SIZE = 64;
8
+ const RAY_RECORD_BYTES = 80;
9
+ const HIT_RECORD_BYTES = 208;
10
+ const SCENE_OBJECT_RECORD_BYTES = 96;
11
+ const MESH_VERTEX_RECORD_BYTES = 48;
12
+ const MESH_RANGE_RECORD_BYTES = 96;
13
+ const TRIANGLE_RECORD_BYTES = 208;
14
+ const BVH_NODE_RECORD_BYTES = 48;
15
+ const BVH_LEAF_REF_RECORD_BYTES = 16;
16
+ const EMISSIVE_TRIANGLE_INDEX_BYTES = 4;
17
+ const ACCUMULATION_RECORD_BYTES = 16;
18
+ const CONFIG_BUFFER_BYTES = 256;
19
+ const COUNTER_BUFFER_BYTES = 16;
20
+ const HIT_TYPE_SURFACE = 0;
21
+ const HIT_TYPE_EMISSIVE = 1;
22
+ const MATERIAL_DIFFUSE = 0;
23
+ const MATERIAL_METAL = 1;
24
+ const MATERIAL_DIELECTRIC = 2;
25
+ const MATERIAL_TRANSPARENT = 3;
26
+ const MATERIAL_EMISSIVE = 4;
27
+ const OBJECT_KIND_SPHERE = 1;
28
+ const OBJECT_KIND_BOX = 2;
29
+
30
+ const DEFAULT_CAMERA = Object.freeze({
31
+ position: Object.freeze([0, 1.15, 5.6]),
32
+ target: Object.freeze([0, 0.65, 0]),
33
+ up: Object.freeze([0, 1, 0]),
34
+ fovYDegrees: 46,
35
+ });
36
+
37
+ const DEFAULT_ENVIRONMENT_COLOR = Object.freeze([0.35, 0.43, 0.49, 1]);
38
+ const DEFAULT_AMBIENT_COLOR = Object.freeze([0.018, 0.022, 0.026, 1]);
39
+ const DEFAULT_ENVIRONMENT_LIGHTING = Object.freeze({
40
+ horizonColor: Object.freeze([0.46, 0.56, 0.68, 1]),
41
+ zenithColor: Object.freeze([0.04, 0.08, 0.16, 1]),
42
+ sunDirection: Object.freeze([0.22, 0.88, 0.42]),
43
+ sunColor: Object.freeze([2.8, 2.65, 2.35, 1]),
44
+ intensity: 1,
45
+ mode: 0,
46
+ exposure: 1,
47
+ });
48
+
49
+ export const wavefrontPathTracingComputeLimits = Object.freeze({
50
+ workgroupSize: WORKGROUP_SIZE,
51
+ rayRecordBytes: RAY_RECORD_BYTES,
52
+ hitRecordBytes: HIT_RECORD_BYTES,
53
+ sceneObjectRecordBytes: SCENE_OBJECT_RECORD_BYTES,
54
+ meshVertexRecordBytes: MESH_VERTEX_RECORD_BYTES,
55
+ meshRangeRecordBytes: MESH_RANGE_RECORD_BYTES,
56
+ triangleRecordBytes: TRIANGLE_RECORD_BYTES,
57
+ bvhNodeRecordBytes: BVH_NODE_RECORD_BYTES,
58
+ bvhLeafReferenceRecordBytes: BVH_LEAF_REF_RECORD_BYTES,
59
+ emissiveTriangleIndexBytes: EMISSIVE_TRIANGLE_INDEX_BYTES,
60
+ emissiveTriangleMetadataRecordBytes: BVH_NODE_RECORD_BYTES,
61
+ accumulationRecordBytes: ACCUMULATION_RECORD_BYTES,
62
+ });
63
+
64
+ export const wavefrontSceneObjectKinds = Object.freeze({
65
+ sphere: OBJECT_KIND_SPHERE,
66
+ box: OBJECT_KIND_BOX,
67
+ });
68
+
69
+ export const wavefrontMaterialKinds = Object.freeze({
70
+ diffuse: MATERIAL_DIFFUSE,
71
+ metal: MATERIAL_METAL,
72
+ dielectric: MATERIAL_DIELECTRIC,
73
+ transparent: MATERIAL_TRANSPARENT,
74
+ emissive: MATERIAL_EMISSIVE,
75
+ });
76
+
77
+ function readPositiveInteger(name, value, fallback) {
78
+ if (value === undefined || value === null) {
79
+ return fallback;
80
+ }
81
+ const numeric = Number(value);
82
+ if (!Number.isInteger(numeric) || numeric <= 0) {
83
+ throw new Error(`${name} must be a positive integer.`);
84
+ }
85
+ return numeric;
86
+ }
87
+
88
+ function readNonNegativeInteger(name, value, fallback) {
89
+ if (value === undefined || value === null) {
90
+ return fallback;
91
+ }
92
+ const numeric = Number(value);
93
+ if (!Number.isInteger(numeric) || numeric < 0) {
94
+ throw new Error(`${name} must be a non-negative integer.`);
95
+ }
96
+ return numeric;
97
+ }
98
+
99
+ function readFiniteNumber(name, value, fallback) {
100
+ if (value === undefined || value === null) {
101
+ return fallback;
102
+ }
103
+ const numeric = Number(value);
104
+ if (!Number.isFinite(numeric)) {
105
+ throw new Error(`${name} must be a finite number.`);
106
+ }
107
+ return numeric;
108
+ }
109
+
110
+ function assertAnalyticDisplayQualityPolicy(options = {}) {
111
+ const meshes = Array.isArray(options.meshes)
112
+ ? options.meshes
113
+ : options.mesh
114
+ ? [options.mesh]
115
+ : [];
116
+ if (options.displayQuality === true && meshes.length === 0) {
117
+ throw new Error(
118
+ "Display-quality path tracing requires mesh BVH triangle intersections. " +
119
+ "The analytic sphere/box wavefront renderer is debug-only."
120
+ );
121
+ }
122
+ }
123
+
124
+ function clamp(value, min, max) {
125
+ return Math.max(min, Math.min(max, value));
126
+ }
127
+
128
+ function asVec3(value, fallback) {
129
+ if (!Array.isArray(value) && !(ArrayBuffer.isView(value) && value.length >= 3)) {
130
+ return [...fallback];
131
+ }
132
+ return [
133
+ readFiniteNumber("vector[0]", value[0], fallback[0]),
134
+ readFiniteNumber("vector[1]", value[1], fallback[1]),
135
+ readFiniteNumber("vector[2]", value[2], fallback[2]),
136
+ ];
137
+ }
138
+
139
+ function asColor(value, fallback = [1, 1, 1, 1]) {
140
+ if (!Array.isArray(value) && !(ArrayBuffer.isView(value) && value.length >= 3)) {
141
+ return [...fallback];
142
+ }
143
+ return [
144
+ clamp(readFiniteNumber("color[0]", value[0], fallback[0]), 0, 64),
145
+ clamp(readFiniteNumber("color[1]", value[1], fallback[1]), 0, 64),
146
+ clamp(readFiniteNumber("color[2]", value[2], fallback[2]), 0, 64),
147
+ clamp(readFiniteNumber("color[3]", value[3], fallback[3] ?? 1), 0, 1),
148
+ ];
149
+ }
150
+
151
+ function emissionPower(emission) {
152
+ return Math.max(0, emission?.[0] ?? 0) + Math.max(0, emission?.[1] ?? 0) + Math.max(0, emission?.[2] ?? 0);
153
+ }
154
+
155
+ function asUnitVec3(value, fallback) {
156
+ const vector = asVec3(value, fallback);
157
+ return normalize(vector, fallback);
158
+ }
159
+
160
+ function add(a, b) {
161
+ return [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
162
+ }
163
+
164
+ function subtract(a, b) {
165
+ return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
166
+ }
167
+
168
+ function scale(a, scalar) {
169
+ return [a[0] * scalar, a[1] * scalar, a[2] * scalar];
170
+ }
171
+
172
+ function dot(a, b) {
173
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
174
+ }
175
+
176
+ function cross(a, b) {
177
+ return [
178
+ a[1] * b[2] - a[2] * b[1],
179
+ a[2] * b[0] - a[0] * b[2],
180
+ a[0] * b[1] - a[1] * b[0],
181
+ ];
182
+ }
183
+
184
+ function normalize(value, fallback = [0, 0, 1]) {
185
+ const length = Math.hypot(value[0], value[1], value[2]);
186
+ if (!Number.isFinite(length) || length <= 0.000001) {
187
+ return [...fallback];
188
+ }
189
+ return [value[0] / length, value[1] / length, value[2] / length];
190
+ }
191
+
192
+ function getArrayLikeLength(value) {
193
+ return Array.isArray(value) || ArrayBuffer.isView(value) ? value.length : 0;
194
+ }
195
+
196
+ function readVector(values, index, componentCount, fallback) {
197
+ const offset = index * componentCount;
198
+ const output = [];
199
+ for (let component = 0; component < componentCount; component += 1) {
200
+ output.push(readFiniteNumber("mesh attribute", values?.[offset + component], fallback[component] ?? 0));
201
+ }
202
+ return output;
203
+ }
204
+
205
+ function readVector2(values, index, fallback = [0, 0]) {
206
+ return readVector(values, index, 2, fallback);
207
+ }
208
+
209
+ function triangleBounds(v0, v1, v2) {
210
+ return {
211
+ min: [
212
+ Math.min(v0[0], v1[0], v2[0]),
213
+ Math.min(v0[1], v1[1], v2[1]),
214
+ Math.min(v0[2], v1[2], v2[2]),
215
+ ],
216
+ max: [
217
+ Math.max(v0[0], v1[0], v2[0]),
218
+ Math.max(v0[1], v1[1], v2[1]),
219
+ Math.max(v0[2], v1[2], v2[2]),
220
+ ],
221
+ };
222
+ }
223
+
224
+ function mergeBounds(left, right) {
225
+ if (!left) {
226
+ return {
227
+ min: [...right.min],
228
+ max: [...right.max],
229
+ };
230
+ }
231
+ return {
232
+ min: [
233
+ Math.min(left.min[0], right.min[0]),
234
+ Math.min(left.min[1], right.min[1]),
235
+ Math.min(left.min[2], right.min[2]),
236
+ ],
237
+ max: [
238
+ Math.max(left.max[0], right.max[0]),
239
+ Math.max(left.max[1], right.max[1]),
240
+ Math.max(left.max[2], right.max[2]),
241
+ ],
242
+ };
243
+ }
244
+
245
+ function boundsCentroid(bounds) {
246
+ return [
247
+ (bounds.min[0] + bounds.max[0]) * 0.5,
248
+ (bounds.min[1] + bounds.max[1]) * 0.5,
249
+ (bounds.min[2] + bounds.max[2]) * 0.5,
250
+ ];
251
+ }
252
+
253
+ function readMaterialKind(value) {
254
+ if (typeof value === "number") {
255
+ return clamp(Math.trunc(value), MATERIAL_DIFFUSE, MATERIAL_EMISSIVE);
256
+ }
257
+ switch (value) {
258
+ case "metal":
259
+ case "reflective":
260
+ return MATERIAL_METAL;
261
+ case "dielectric":
262
+ case "refractive":
263
+ case "glass":
264
+ return MATERIAL_DIELECTRIC;
265
+ case "transparent":
266
+ case "transmission":
267
+ return MATERIAL_TRANSPARENT;
268
+ case "emissive":
269
+ case "light":
270
+ return MATERIAL_EMISSIVE;
271
+ case "diffuse":
272
+ default:
273
+ return MATERIAL_DIFFUSE;
274
+ }
275
+ }
276
+
277
+ function readObjectKind(value) {
278
+ if (typeof value === "number") {
279
+ return value === OBJECT_KIND_BOX ? OBJECT_KIND_BOX : OBJECT_KIND_SPHERE;
280
+ }
281
+ switch (value) {
282
+ case "box":
283
+ case "aabb":
284
+ case "bounds":
285
+ return OBJECT_KIND_BOX;
286
+ case "sphere":
287
+ default:
288
+ return OBJECT_KIND_SPHERE;
289
+ }
290
+ }
291
+
292
+ function deriveBounds(input) {
293
+ if (Array.isArray(input?.min) && Array.isArray(input?.max)) {
294
+ const min = asVec3(input.min, [-0.5, -0.5, -0.5]);
295
+ const max = asVec3(input.max, [0.5, 0.5, 0.5]);
296
+ return {
297
+ center: scale(add(min, max), 0.5),
298
+ halfExtent: scale(subtract(max, min), 0.5).map((value) => Math.max(value, 0.001)),
299
+ };
300
+ }
301
+
302
+ if (Array.isArray(input?.bounds?.min) && Array.isArray(input?.bounds?.max)) {
303
+ return deriveBounds(input.bounds);
304
+ }
305
+
306
+ return null;
307
+ }
308
+
309
+ export function normalizeWavefrontSceneObject(input = {}, index = 0) {
310
+ const bounds = deriveBounds(input);
311
+ const kind = readObjectKind(input.kind ?? input.type ?? (bounds ? "box" : "sphere"));
312
+ const center = asVec3(input.center ?? input.position ?? bounds?.center, [0, 0, 0]);
313
+ const radius = readFiniteNumber("radius", input.radius, 0.5);
314
+ const halfExtent =
315
+ kind === OBJECT_KIND_SPHERE
316
+ ? [Math.max(radius, 0.001), Math.max(radius, 0.001), Math.max(radius, 0.001)]
317
+ : asVec3(
318
+ input.halfExtent ?? input.halfExtents ?? input.extents ?? bounds?.halfExtent,
319
+ [0.5, 0.5, 0.5]
320
+ ).map((value) => Math.max(value, 0.001));
321
+ const materialKind = readMaterialKind(input.materialKind ?? input.material?.kind);
322
+ const color = asColor(
323
+ input.color ??
324
+ input.baseColor ??
325
+ input.albedo ??
326
+ input.material?.color ??
327
+ input.material?.baseColor,
328
+ [0.72, 0.72, 0.68, 1]
329
+ );
330
+ const emission = asColor(
331
+ input.emission ?? input.emissive ?? input.material?.emission ?? input.material?.emissive,
332
+ [0, 0, 0, 1]
333
+ );
334
+
335
+ return Object.freeze({
336
+ id: readNonNegativeInteger("id", input.id, index + 1),
337
+ kind,
338
+ materialKind:
339
+ emission[0] > 0 || emission[1] > 0 || emission[2] > 0
340
+ ? MATERIAL_EMISSIVE
341
+ : materialKind,
342
+ flags: readNonNegativeInteger("flags", input.flags, 0),
343
+ center: Object.freeze(center),
344
+ halfExtent: Object.freeze(halfExtent),
345
+ color: Object.freeze(color),
346
+ emission: Object.freeze(emission),
347
+ roughness: clamp(readFiniteNumber("roughness", input.roughness ?? input.material?.roughness, 0.72), 0, 1),
348
+ metallic: clamp(readFiniteNumber("metallic", input.metallic ?? input.material?.metallic, 0), 0, 1),
349
+ opacity: clamp(readFiniteNumber("opacity", input.opacity ?? input.material?.opacity, color[3] ?? 1), 0, 1),
350
+ ior: clamp(readFiniteNumber("ior", input.ior ?? input.material?.ior, 1.45), 1, 3),
351
+ });
352
+ }
353
+
354
+ export function createDefaultWavefrontSceneObjects() {
355
+ return Object.freeze([
356
+ normalizeWavefrontSceneObject({
357
+ type: "box",
358
+ id: 1,
359
+ center: [0, -0.08, 0],
360
+ halfExtent: [3.25, 0.08, 3.25],
361
+ color: [0.45, 0.53, 0.54, 1],
362
+ roughness: 0.5,
363
+ }),
364
+ normalizeWavefrontSceneObject({
365
+ type: "box",
366
+ id: 2,
367
+ center: [0, 1.25, -1.65],
368
+ halfExtent: [2.45, 1.45, 0.08],
369
+ color: [0.42, 0.41, 0.38, 1],
370
+ roughness: 0.85,
371
+ }),
372
+ normalizeWavefrontSceneObject({
373
+ type: "sphere",
374
+ id: 3,
375
+ center: [-0.9, 0.72, 0.05],
376
+ radius: 0.72,
377
+ color: [0.76, 0.72, 0.64, 1],
378
+ materialKind: "metal",
379
+ roughness: 0.08,
380
+ metallic: 0.7,
381
+ }),
382
+ normalizeWavefrontSceneObject({
383
+ type: "sphere",
384
+ id: 4,
385
+ center: [0.85, 0.65, -0.05],
386
+ radius: 0.58,
387
+ color: [0.68, 0.82, 0.86, 0.72],
388
+ materialKind: "dielectric",
389
+ roughness: 0.02,
390
+ opacity: 0.72,
391
+ ior: 1.35,
392
+ }),
393
+ normalizeWavefrontSceneObject({
394
+ type: "sphere",
395
+ id: 5,
396
+ center: [0, 2.55, -0.65],
397
+ radius: 0.34,
398
+ color: [1, 0.94, 0.78, 1],
399
+ emission: [7.2, 6.5, 4.2, 1],
400
+ materialKind: "emissive",
401
+ }),
402
+ ]);
403
+ }
404
+
405
+ export function normalizeWavefrontMesh(input = {}, meshIndex = 0) {
406
+ const positions = input.positions;
407
+ const positionLength = getArrayLikeLength(positions);
408
+ if (positionLength < 9 || positionLength % 3 !== 0) {
409
+ throw new Error("Wavefront mesh positions must contain at least three vec3 vertices.");
410
+ }
411
+
412
+ const vertexCount = positionLength / 3;
413
+ const indices =
414
+ getArrayLikeLength(input.indices) > 0
415
+ ? Array.from(input.indices, (value) => readNonNegativeInteger("mesh index", value, 0))
416
+ : Array.from({ length: vertexCount }, (_, index) => index);
417
+ if (indices.length < 3 || indices.length % 3 !== 0) {
418
+ throw new Error("Wavefront mesh indices must contain complete triangles.");
419
+ }
420
+ if (indices.some((index) => index >= vertexCount)) {
421
+ throw new Error("Wavefront mesh index references a vertex outside the position buffer.");
422
+ }
423
+
424
+ const normals =
425
+ getArrayLikeLength(input.normals) >= positionLength
426
+ ? Array.from(input.normals, (value) => readFiniteNumber("mesh normal", value, 0))
427
+ : null;
428
+ const uvs =
429
+ getArrayLikeLength(input.uvs ?? input.texcoords ?? input.uv) >= vertexCount * 2
430
+ ? Array.from(input.uvs ?? input.texcoords ?? input.uv, (value) =>
431
+ readFiniteNumber("mesh uv", value, 0)
432
+ )
433
+ : null;
434
+ const materialKind = readMaterialKind(input.materialKind ?? input.material?.kind);
435
+ const color = asColor(
436
+ input.color ??
437
+ input.baseColor ??
438
+ input.albedo ??
439
+ input.material?.color ??
440
+ input.material?.baseColor,
441
+ [0.72, 0.72, 0.68, 1]
442
+ );
443
+ const emission = asColor(
444
+ input.emission ?? input.emissive ?? input.material?.emission ?? input.material?.emissive,
445
+ [0, 0, 0, 1]
446
+ );
447
+
448
+ return Object.freeze({
449
+ id: readNonNegativeInteger("mesh id", input.id, meshIndex + 1),
450
+ positions: Object.freeze(Array.from(positions, (value) => readFiniteNumber("mesh position", value, 0))),
451
+ indices: Object.freeze(indices),
452
+ normals: normals ? Object.freeze(normals) : null,
453
+ uvs: uvs ? Object.freeze(uvs) : null,
454
+ materialKind:
455
+ emission[0] > 0 || emission[1] > 0 || emission[2] > 0
456
+ ? MATERIAL_EMISSIVE
457
+ : materialKind,
458
+ flags: readNonNegativeInteger("mesh flags", input.flags, 0),
459
+ materialRefId: readNonNegativeInteger(
460
+ "mesh materialRefId",
461
+ input.materialRefId ?? input.material?.id ?? input.materialId,
462
+ meshIndex
463
+ ),
464
+ mediumRefId: readNonNegativeInteger(
465
+ "mesh mediumRefId",
466
+ input.mediumRefId ?? input.medium?.id ?? input.mediumId,
467
+ 0
468
+ ),
469
+ color: Object.freeze(color),
470
+ emission: Object.freeze(emission),
471
+ roughness: clamp(readFiniteNumber("roughness", input.roughness ?? input.material?.roughness, 0.72), 0, 1),
472
+ metallic: clamp(readFiniteNumber("metallic", input.metallic ?? input.material?.metallic, 0), 0, 1),
473
+ opacity: clamp(readFiniteNumber("opacity", input.opacity ?? input.material?.opacity, color[3] ?? 1), 0, 1),
474
+ ior: clamp(readFiniteNumber("ior", input.ior ?? input.material?.ior, 1.45), 1, 3),
475
+ });
476
+ }
477
+
478
+ function createMeshTriangleRecords(meshes) {
479
+ const source = Array.isArray(meshes) ? meshes : [];
480
+ let nextTriangleId = 0;
481
+ return source.flatMap((meshInput, meshIndex) => {
482
+ const mesh = normalizeWavefrontMesh(meshInput, meshIndex);
483
+ const triangles = [];
484
+ for (let index = 0; index < mesh.indices.length; index += 3) {
485
+ const a = mesh.indices[index];
486
+ const b = mesh.indices[index + 1];
487
+ const c = mesh.indices[index + 2];
488
+ const v0 = readVector(mesh.positions, a, 3, [0, 0, 0]);
489
+ const v1 = readVector(mesh.positions, b, 3, [0, 0, 0]);
490
+ const v2 = readVector(mesh.positions, c, 3, [0, 0, 0]);
491
+ const faceNormal = normalize(cross(subtract(v1, v0), subtract(v2, v0)), [0, 1, 0]);
492
+ const n0 = mesh.normals ? normalize(readVector(mesh.normals, a, 3, faceNormal), faceNormal) : faceNormal;
493
+ const n1 = mesh.normals ? normalize(readVector(mesh.normals, b, 3, faceNormal), faceNormal) : faceNormal;
494
+ const n2 = mesh.normals ? normalize(readVector(mesh.normals, c, 3, faceNormal), faceNormal) : faceNormal;
495
+ const uv0 = mesh.uvs ? readVector2(mesh.uvs, a) : [0, 0];
496
+ const uv1 = mesh.uvs ? readVector2(mesh.uvs, b) : [0, 0];
497
+ const uv2 = mesh.uvs ? readVector2(mesh.uvs, c) : [0, 0];
498
+ const bounds = triangleBounds(v0, v1, v2);
499
+
500
+ triangles.push(
501
+ Object.freeze({
502
+ triangleId: nextTriangleId,
503
+ meshId: mesh.id,
504
+ materialKind: mesh.materialKind,
505
+ flags: mesh.flags,
506
+ materialRefId: mesh.materialRefId,
507
+ mediumRefId: mesh.mediumRefId,
508
+ v0: Object.freeze(v0),
509
+ v1: Object.freeze(v1),
510
+ v2: Object.freeze(v2),
511
+ n0: Object.freeze(n0),
512
+ n1: Object.freeze(n1),
513
+ n2: Object.freeze(n2),
514
+ uv0: Object.freeze(uv0),
515
+ uv1: Object.freeze(uv1),
516
+ uv2: Object.freeze(uv2),
517
+ color: mesh.color,
518
+ emission: mesh.emission,
519
+ material: Object.freeze([mesh.roughness, mesh.metallic, mesh.opacity, mesh.ior]),
520
+ bounds: Object.freeze({
521
+ min: Object.freeze(bounds.min),
522
+ max: Object.freeze(bounds.max),
523
+ }),
524
+ centroid: Object.freeze(boundsCentroid(bounds)),
525
+ })
526
+ );
527
+ nextTriangleId += 1;
528
+ }
529
+ return triangles;
530
+ });
531
+ }
532
+
533
+ function chooseSplitAxis(triangles) {
534
+ const centroidBounds = triangles.reduce(
535
+ (bounds, triangle) => {
536
+ const pointBounds = { min: triangle.centroid, max: triangle.centroid };
537
+ return mergeBounds(bounds, pointBounds);
538
+ },
539
+ null
540
+ );
541
+ const extent = subtract(centroidBounds.max, centroidBounds.min);
542
+ if (extent[0] >= extent[1] && extent[0] >= extent[2]) {
543
+ return 0;
544
+ }
545
+ return extent[1] >= extent[2] ? 1 : 2;
546
+ }
547
+
548
+ function buildBvh(triangles, maxLeafTriangles = 4) {
549
+ if (triangles.length === 0) {
550
+ return Object.freeze({ nodes: Object.freeze([]), triangles: Object.freeze([]) });
551
+ }
552
+
553
+ const nodes = [];
554
+ const orderedTriangles = [];
555
+
556
+ function buildNode(nodeTriangles) {
557
+ const nodeIndex = nodes.length;
558
+ nodes.push(null);
559
+ const bounds = nodeTriangles.reduce((current, triangle) => mergeBounds(current, triangle.bounds), null);
560
+
561
+ if (nodeTriangles.length <= maxLeafTriangles) {
562
+ const firstTriangle = orderedTriangles.length;
563
+ orderedTriangles.push(...nodeTriangles);
564
+ nodes[nodeIndex] = Object.freeze({
565
+ bounds: Object.freeze({
566
+ min: Object.freeze(bounds.min),
567
+ max: Object.freeze(bounds.max),
568
+ }),
569
+ firstTriangle,
570
+ triangleCount: nodeTriangles.length,
571
+ leftChild: 0,
572
+ rightChild: 0,
573
+ });
574
+ return nodeIndex;
575
+ }
576
+
577
+ const axis = chooseSplitAxis(nodeTriangles);
578
+ const sorted = [...nodeTriangles].sort((left, right) => left.centroid[axis] - right.centroid[axis]);
579
+ const midpoint = Math.max(1, Math.floor(sorted.length / 2));
580
+ const leftChild = buildNode(sorted.slice(0, midpoint));
581
+ const rightChild = buildNode(sorted.slice(midpoint));
582
+ nodes[nodeIndex] = Object.freeze({
583
+ bounds: Object.freeze({
584
+ min: Object.freeze(bounds.min),
585
+ max: Object.freeze(bounds.max),
586
+ }),
587
+ firstTriangle: leftChild,
588
+ triangleCount: 0,
589
+ leftChild,
590
+ rightChild,
591
+ });
592
+ return nodeIndex;
593
+ }
594
+
595
+ buildNode(triangles);
596
+ return Object.freeze({
597
+ nodes: Object.freeze(nodes),
598
+ triangles: Object.freeze(orderedTriangles),
599
+ });
600
+ }
601
+
602
+ export function createWavefrontMeshAcceleration(meshes = []) {
603
+ const source = Array.isArray(meshes) ? meshes : [meshes];
604
+ const triangles = createMeshTriangleRecords(source);
605
+ return buildBvh(triangles);
606
+ }
607
+
608
+ function estimateMeshSourceShape(meshes) {
609
+ const source = Array.isArray(meshes) ? meshes : [];
610
+ return source.reduce(
611
+ (shape, meshInput, meshIndex) => {
612
+ const mesh = normalizeWavefrontMesh(meshInput, meshIndex);
613
+ return {
614
+ vertexCount: shape.vertexCount + mesh.positions.length / 3,
615
+ indexCount: shape.indexCount + mesh.indices.length,
616
+ meshCount: shape.meshCount + 1,
617
+ triangleCount: shape.triangleCount + mesh.indices.length / 3,
618
+ };
619
+ },
620
+ {
621
+ vertexCount: 0,
622
+ indexCount: 0,
623
+ meshCount: 0,
624
+ triangleCount: 0,
625
+ }
626
+ );
627
+ }
628
+
629
+ function estimateBinaryBvhNodeCapacity(triangleCount) {
630
+ return triangleCount <= 0 ? 0 : Math.max(1, triangleCount * 2 - 1);
631
+ }
632
+
633
+ function nextPowerOfTwo(value) {
634
+ if (value <= 1) {
635
+ return Math.max(0, value);
636
+ }
637
+ return 2 ** Math.ceil(Math.log2(value));
638
+ }
639
+
640
+ function estimateBvhLeafSortCapacity(triangleCount) {
641
+ return triangleCount <= 0 ? 0 : nextPowerOfTwo(triangleCount);
642
+ }
643
+
644
+ export function createWavefrontBvhSortStages(itemCountInput) {
645
+ const itemCount = readNonNegativeInteger("itemCount", itemCountInput, 0);
646
+ const sortCount = estimateBvhLeafSortCapacity(itemCount);
647
+ if (sortCount <= 1) {
648
+ return Object.freeze([]);
649
+ }
650
+
651
+ const stages = [];
652
+ for (let sequenceSize = 2; sequenceSize <= sortCount; sequenceSize *= 2) {
653
+ for (
654
+ let compareDistance = sequenceSize / 2;
655
+ compareDistance >= 1;
656
+ compareDistance /= 2
657
+ ) {
658
+ stages.push(
659
+ Object.freeze({
660
+ compareDistance,
661
+ sequenceSize,
662
+ })
663
+ );
664
+ }
665
+ }
666
+
667
+ return Object.freeze(stages);
668
+ }
669
+
670
+ export function createWavefrontBvhBuildLevels(triangleCountInput) {
671
+ const triangleCount = readNonNegativeInteger("triangleCount", triangleCountInput, 0);
672
+ const internalCount = Math.max(0, triangleCount - 1);
673
+ if (internalCount === 0) {
674
+ return Object.freeze([]);
675
+ }
676
+
677
+ const levels = [];
678
+ let depth = 0;
679
+ while (Math.pow(2, depth) - 1 < internalCount) {
680
+ depth += 1;
681
+ }
682
+
683
+ for (let level = depth - 1; level >= 0; level -= 1) {
684
+ const start = Math.pow(2, level) - 1;
685
+ const end = Math.min(Math.pow(2, level + 1) - 2, internalCount - 1);
686
+ if (end >= start) {
687
+ levels.push(
688
+ Object.freeze({
689
+ start,
690
+ count: end - start + 1,
691
+ })
692
+ );
693
+ }
694
+ }
695
+
696
+ return Object.freeze(levels);
697
+ }
698
+
699
+ function resolveAccelerationBuildMode(options = {}) {
700
+ const mode = options.accelerationBuildMode ?? (options.displayQuality === true ? "gpu" : "cpu-debug");
701
+ if (mode !== "gpu" && mode !== "cpu-debug") {
702
+ throw new Error("accelerationBuildMode must be either \"gpu\" or \"cpu-debug\".");
703
+ }
704
+ if (options.displayQuality === true && mode !== "gpu") {
705
+ throw new Error("Display-quality path tracing requires GPU-built mesh acceleration.");
706
+ }
707
+ return mode;
708
+ }
709
+
710
+ export function createWavefrontGpuMeshSource(meshes = []) {
711
+ const source = Array.isArray(meshes) ? meshes : [meshes];
712
+ const normalized = source.map((meshInput, meshIndex) => normalizeWavefrontMesh(meshInput, meshIndex));
713
+ const vertexCount = normalized.reduce((count, mesh) => count + mesh.positions.length / 3, 0);
714
+ const indexCount = normalized.reduce((count, mesh) => count + mesh.indices.length, 0);
715
+ const triangleCount = Math.floor(indexCount / 3);
716
+ const vertexBytes = new ArrayBuffer(Math.max(1, vertexCount) * MESH_VERTEX_RECORD_BYTES);
717
+ const indexBytes = new ArrayBuffer(Math.max(1, indexCount) * 4);
718
+ const meshBytes = new ArrayBuffer(Math.max(1, normalized.length) * MESH_RANGE_RECORD_BYTES);
719
+ const vertexFloats = new Float32Array(vertexBytes);
720
+ const indexUints = new Uint32Array(indexBytes);
721
+ const meshUints = new Uint32Array(meshBytes);
722
+ const meshFloats = new Float32Array(meshBytes);
723
+
724
+ let vertexCursor = 0;
725
+ let indexCursor = 0;
726
+ let triangleCursor = 0;
727
+
728
+ normalized.forEach((mesh, meshIndex) => {
729
+ const meshVertexBase = vertexCursor;
730
+ const meshIndexBase = indexCursor;
731
+ const meshTriangleBase = triangleCursor;
732
+ const meshVertexCount = mesh.positions.length / 3;
733
+
734
+ for (let vertexIndex = 0; vertexIndex < meshVertexCount; vertexIndex += 1) {
735
+ const recordOffset = (vertexCursor + vertexIndex) * (MESH_VERTEX_RECORD_BYTES / 4);
736
+ const position = readVector(mesh.positions, vertexIndex, 3, [0, 0, 0]);
737
+ const normal = mesh.normals ? readVector(mesh.normals, vertexIndex, 3, [0, 0, 0]) : [0, 0, 0];
738
+ const uv = mesh.uvs ? readVector2(mesh.uvs, vertexIndex) : [0, 0];
739
+ vertexFloats[recordOffset] = position[0];
740
+ vertexFloats[recordOffset + 1] = position[1];
741
+ vertexFloats[recordOffset + 2] = position[2];
742
+ vertexFloats[recordOffset + 3] = 1;
743
+ vertexFloats[recordOffset + 4] = normal[0];
744
+ vertexFloats[recordOffset + 5] = normal[1];
745
+ vertexFloats[recordOffset + 6] = normal[2];
746
+ vertexFloats[recordOffset + 7] = mesh.normals ? 1 : 0;
747
+ vertexFloats[recordOffset + 8] = uv[0];
748
+ vertexFloats[recordOffset + 9] = uv[1];
749
+ vertexFloats[recordOffset + 10] = mesh.uvs ? 1 : 0;
750
+ vertexFloats[recordOffset + 11] = 0;
751
+ }
752
+
753
+ mesh.indices.forEach((indexValue, localIndex) => {
754
+ indexUints[indexCursor + localIndex] = meshVertexBase + indexValue;
755
+ });
756
+
757
+ const meshOffset = meshIndex * (MESH_RANGE_RECORD_BYTES / 4);
758
+ meshUints[meshOffset] = mesh.id;
759
+ meshUints[meshOffset + 1] = mesh.materialKind;
760
+ meshUints[meshOffset + 2] = mesh.flags;
761
+ meshUints[meshOffset + 3] = mesh.materialRefId;
762
+ meshUints[meshOffset + 4] = mesh.mediumRefId;
763
+ meshUints[meshOffset + 5] = meshIndexBase;
764
+ meshUints[meshOffset + 6] = mesh.indices.length;
765
+ meshUints[meshOffset + 7] = meshTriangleBase;
766
+ meshUints[meshOffset + 8] = mesh.indices.length / 3;
767
+ meshUints[meshOffset + 9] = meshVertexBase;
768
+ meshUints[meshOffset + 10] = meshVertexCount;
769
+ meshUints[meshOffset + 11] = 0;
770
+ const floatOffset = meshOffset;
771
+ writeVec4(meshFloats, floatOffset * 4 + 48, mesh.color);
772
+ writeVec4(meshFloats, floatOffset * 4 + 64, mesh.emission);
773
+ writeVec4(meshFloats, floatOffset * 4 + 80, [
774
+ mesh.roughness,
775
+ mesh.metallic,
776
+ mesh.opacity,
777
+ mesh.ior,
778
+ ]);
779
+
780
+ vertexCursor += meshVertexCount;
781
+ indexCursor += mesh.indices.length;
782
+ triangleCursor += mesh.indices.length / 3;
783
+ });
784
+
785
+ return Object.freeze({
786
+ vertices: Object.freeze({
787
+ buffer: vertexBytes,
788
+ count: vertexCount,
789
+ recordBytes: MESH_VERTEX_RECORD_BYTES,
790
+ }),
791
+ indices: Object.freeze({
792
+ buffer: indexBytes,
793
+ count: indexCount,
794
+ recordBytes: 4,
795
+ }),
796
+ meshes: Object.freeze({
797
+ buffer: meshBytes,
798
+ records: Object.freeze(normalized),
799
+ count: normalized.length,
800
+ recordBytes: MESH_RANGE_RECORD_BYTES,
801
+ }),
802
+ triangleCount,
803
+ bvhNodeCapacity: estimateBinaryBvhNodeCapacity(triangleCount),
804
+ });
805
+ }
806
+
807
+ export function createWavefrontEmissiveTriangleIndexSource(meshes = [], capacityInput) {
808
+ const source = Array.isArray(meshes) ? meshes : [meshes];
809
+ const normalized = source.map((meshInput, meshIndex) => normalizeWavefrontMesh(meshInput, meshIndex));
810
+ const indices = [];
811
+ let triangleCursor = 0;
812
+
813
+ normalized.forEach((mesh) => {
814
+ const triangleCount = Math.floor(mesh.indices.length / 3);
815
+ const isEmissive =
816
+ mesh.materialKind === MATERIAL_EMISSIVE || emissionPower(mesh.emission) > 0.0001;
817
+ if (isEmissive) {
818
+ for (let triangleOffset = 0; triangleOffset < triangleCount; triangleOffset += 1) {
819
+ indices.push(triangleCursor + triangleOffset);
820
+ }
821
+ }
822
+ triangleCursor += triangleCount;
823
+ });
824
+
825
+ const capacity = Math.max(
826
+ indices.length,
827
+ readNonNegativeInteger("emissiveTriangleCapacity", capacityInput, indices.length)
828
+ );
829
+ const bytes = new ArrayBuffer(capacity * EMISSIVE_TRIANGLE_INDEX_BYTES);
830
+ const uints = new Uint32Array(bytes);
831
+ uints.fill(0xffffffff);
832
+ indices.forEach((triangleIndex, index) => {
833
+ uints[index] = triangleIndex;
834
+ });
835
+
836
+ return Object.freeze({
837
+ buffer: bytes,
838
+ indices: Object.freeze(indices),
839
+ count: indices.length,
840
+ capacity,
841
+ recordBytes: EMISSIVE_TRIANGLE_INDEX_BYTES,
842
+ });
843
+ }
844
+
845
+ function normalizeSceneObjects(sceneObjects, useDefaultScene = true) {
846
+ const source =
847
+ Array.isArray(sceneObjects) && sceneObjects.length > 0
848
+ ? sceneObjects
849
+ : useDefaultScene
850
+ ? createDefaultWavefrontSceneObjects()
851
+ : [];
852
+ return source.map((object, index) => normalizeWavefrontSceneObject(object, index));
853
+ }
854
+
855
+ function normalizeMeshes(options = {}) {
856
+ if (Array.isArray(options.meshes)) {
857
+ return options.meshes;
858
+ }
859
+ if (options.mesh) {
860
+ return [options.mesh];
861
+ }
862
+ return [];
863
+ }
864
+
865
+ function resolveEnvironmentLighting(input, environmentColor, ambientColor) {
866
+ const source = input ?? {};
867
+ return Object.freeze({
868
+ environmentColor: Object.freeze(asColor(source.environmentColor, environmentColor)),
869
+ ambientColor: Object.freeze(asColor(source.ambientColor, ambientColor)),
870
+ horizonColor: Object.freeze(asColor(source.horizonColor, environmentColor)),
871
+ zenithColor: Object.freeze(asColor(source.zenithColor, DEFAULT_ENVIRONMENT_LIGHTING.zenithColor)),
872
+ sunDirection: Object.freeze(asUnitVec3(source.sunDirection, DEFAULT_ENVIRONMENT_LIGHTING.sunDirection)),
873
+ sunColor: Object.freeze(asColor(source.sunColor, DEFAULT_ENVIRONMENT_LIGHTING.sunColor)),
874
+ intensity: Math.max(0.0001, readFiniteNumber("environmentLighting.intensity", source.intensity, DEFAULT_ENVIRONMENT_LIGHTING.intensity)),
875
+ mode: readNonNegativeInteger("environmentLighting.mode", source.mode, DEFAULT_ENVIRONMENT_LIGHTING.mode),
876
+ exposure: Math.max(0.0001, readFiniteNumber("environmentLighting.exposure", source.exposure, DEFAULT_ENVIRONMENT_LIGHTING.exposure)),
877
+ });
878
+ }
879
+
880
+ function getCanvasDimension(canvas, key, fallback) {
881
+ const value = Number(canvas?.[key]);
882
+ if (Number.isFinite(value) && value > 0) {
883
+ return Math.trunc(value);
884
+ }
885
+ return fallback;
886
+ }
887
+
888
+ function resolveCamera(input, width, height) {
889
+ const camera = input ?? DEFAULT_CAMERA;
890
+ const position = asVec3(camera.position, DEFAULT_CAMERA.position);
891
+ const target = asVec3(camera.target, DEFAULT_CAMERA.target);
892
+ const upInput = normalize(asVec3(camera.up, DEFAULT_CAMERA.up), DEFAULT_CAMERA.up);
893
+ const forward = normalize(subtract(target, position), [0, 0, -1]);
894
+ const right = normalize(cross(forward, upInput), [1, 0, 0]);
895
+ const up = normalize(cross(right, forward), [0, 1, 0]);
896
+ const fovYDegrees = clamp(
897
+ readFiniteNumber("camera.fovYDegrees", camera.fovYDegrees ?? camera.fov, DEFAULT_CAMERA.fovYDegrees),
898
+ 10,
899
+ 120
900
+ );
901
+ const aspect = width / Math.max(1, height);
902
+ const tanHalfFovY = Math.tan((fovYDegrees * Math.PI) / 360);
903
+
904
+ return Object.freeze({
905
+ position: Object.freeze(position),
906
+ forward: Object.freeze(forward),
907
+ right: Object.freeze(right),
908
+ up: Object.freeze(up),
909
+ fovYDegrees,
910
+ aspect,
911
+ tanHalfFovY,
912
+ });
913
+ }
914
+
915
+ export function estimateWavefrontPathTracingMemory(options = {}) {
916
+ const tilePixelCapacity = readPositiveInteger(
917
+ "tilePixelCapacity",
918
+ options.tilePixelCapacity,
919
+ DEFAULT_TILE_SIZE * DEFAULT_TILE_SIZE
920
+ );
921
+ const sceneObjectCapacity = readPositiveInteger(
922
+ "sceneObjectCapacity",
923
+ options.sceneObjectCapacity,
924
+ DEFAULT_SCENE_OBJECT_CAPACITY
925
+ );
926
+ const triangleCapacity = readNonNegativeInteger("triangleCapacity", options.triangleCapacity, 0);
927
+ const bvhNodeCapacity = readNonNegativeInteger("bvhNodeCapacity", options.bvhNodeCapacity, 0);
928
+ const bvhLeafSortCapacity = readNonNegativeInteger(
929
+ "bvhLeafSortCapacity",
930
+ options.bvhLeafSortCapacity,
931
+ 0
932
+ );
933
+ const emissiveTriangleCapacity = readNonNegativeInteger(
934
+ "emissiveTriangleCapacity",
935
+ options.emissiveTriangleCapacity,
936
+ 0
937
+ );
938
+ const queueBytes = tilePixelCapacity * RAY_RECORD_BYTES;
939
+ const hitBytes = tilePixelCapacity * HIT_RECORD_BYTES;
940
+ const accumulationBytes = tilePixelCapacity * ACCUMULATION_RECORD_BYTES;
941
+ const sceneObjectBytes = sceneObjectCapacity * SCENE_OBJECT_RECORD_BYTES;
942
+ const triangleBytes = triangleCapacity * TRIANGLE_RECORD_BYTES;
943
+ const bvhNodeBytes = bvhNodeCapacity * BVH_NODE_RECORD_BYTES;
944
+ const bvhLeafReferenceBytes = bvhLeafSortCapacity * BVH_LEAF_REF_RECORD_BYTES;
945
+ const emissiveTriangleMetadataBytes =
946
+ emissiveTriangleCapacity * BVH_NODE_RECORD_BYTES;
947
+
948
+ return Object.freeze({
949
+ queueBytes,
950
+ queuePairBytes: queueBytes * 2,
951
+ hitBytes,
952
+ accumulationBytes,
953
+ sceneObjectBytes,
954
+ triangleBytes,
955
+ bvhNodeBytes,
956
+ bvhLeafReferenceBytes,
957
+ emissiveTriangleMetadataBytes,
958
+ configBytes: CONFIG_BUFFER_BYTES,
959
+ counterBytes: COUNTER_BUFFER_BYTES,
960
+ totalHotBufferBytes:
961
+ queueBytes * 2 +
962
+ hitBytes +
963
+ accumulationBytes +
964
+ sceneObjectBytes +
965
+ triangleBytes +
966
+ bvhNodeBytes +
967
+ bvhLeafReferenceBytes +
968
+ emissiveTriangleMetadataBytes +
969
+ CONFIG_BUFFER_BYTES +
970
+ COUNTER_BUFFER_BYTES,
971
+ });
972
+ }
973
+
974
+ export function createWavefrontPathTracingComputeConfig(options = {}) {
975
+ assertAnalyticDisplayQualityPolicy(options);
976
+ const accelerationBuildMode = resolveAccelerationBuildMode(options);
977
+ const canvas = options.canvas;
978
+ const width = readPositiveInteger("width", options.width, getCanvasDimension(canvas, "width", DEFAULT_WIDTH));
979
+ const height = readPositiveInteger("height", options.height, getCanvasDimension(canvas, "height", DEFAULT_HEIGHT));
980
+ const maxDepth = clamp(readPositiveInteger("maxDepth", options.maxDepth, DEFAULT_MAX_DEPTH), 1, 16);
981
+ const tileSize = clamp(readPositiveInteger("tileSize", options.tileSize, DEFAULT_TILE_SIZE), 16, 512);
982
+ const samplesPerPixel = clamp(
983
+ readPositiveInteger("samplesPerPixel", options.samplesPerPixel, DEFAULT_SAMPLES_PER_PIXEL),
984
+ 1,
985
+ 64
986
+ );
987
+ const tilePixelCapacity = readPositiveInteger(
988
+ "tilePixelCapacity",
989
+ options.tilePixelCapacity,
990
+ tileSize * tileSize
991
+ );
992
+ const meshes = normalizeMeshes(options);
993
+ const meshSourceShape = estimateMeshSourceShape(meshes);
994
+ const gpuMeshSource =
995
+ meshes.length > 0
996
+ ? createWavefrontGpuMeshSource(meshes)
997
+ : createWavefrontGpuMeshSource([]);
998
+ const meshAcceleration =
999
+ accelerationBuildMode === "cpu-debug"
1000
+ ? createWavefrontMeshAcceleration(meshes)
1001
+ : Object.freeze({ nodes: Object.freeze([]), triangles: Object.freeze([]) });
1002
+ const emissiveTriangleIndices = createWavefrontEmissiveTriangleIndexSource(
1003
+ meshes,
1004
+ options.emissiveTriangleCapacity
1005
+ );
1006
+ const triangleCount =
1007
+ accelerationBuildMode === "gpu"
1008
+ ? meshSourceShape.triangleCount
1009
+ : meshAcceleration.triangles.length;
1010
+ const bvhNodeCount =
1011
+ accelerationBuildMode === "gpu"
1012
+ ? estimateBinaryBvhNodeCapacity(triangleCount)
1013
+ : meshAcceleration.nodes.length;
1014
+ const sceneObjects = Object.freeze(
1015
+ normalizeSceneObjects(options.sceneObjects, meshes.length === 0)
1016
+ );
1017
+ const sceneObjectCapacity = Math.max(
1018
+ sceneObjects.length,
1019
+ readPositiveInteger("sceneObjectCapacity", options.sceneObjectCapacity, DEFAULT_SCENE_OBJECT_CAPACITY)
1020
+ );
1021
+ const triangleCapacity = Math.max(
1022
+ triangleCount,
1023
+ readNonNegativeInteger("triangleCapacity", options.triangleCapacity, triangleCount)
1024
+ );
1025
+ const bvhNodeCapacity = Math.max(
1026
+ accelerationBuildMode === "gpu" ? estimateBinaryBvhNodeCapacity(triangleCount) : bvhNodeCount,
1027
+ readNonNegativeInteger(
1028
+ "bvhNodeCapacity",
1029
+ options.bvhNodeCapacity,
1030
+ accelerationBuildMode === "gpu" ? estimateBinaryBvhNodeCapacity(triangleCount) : bvhNodeCount
1031
+ )
1032
+ );
1033
+ const bvhLeafSortCapacity =
1034
+ accelerationBuildMode === "gpu" ? estimateBvhLeafSortCapacity(triangleCount) : 0;
1035
+ const bvhSortStages =
1036
+ accelerationBuildMode === "gpu"
1037
+ ? createWavefrontBvhSortStages(triangleCount)
1038
+ : Object.freeze([]);
1039
+ const bvhBuildLevels =
1040
+ accelerationBuildMode === "gpu"
1041
+ ? createWavefrontBvhBuildLevels(triangleCount)
1042
+ : Object.freeze([]);
1043
+ const camera = resolveCamera(options.camera, width, height);
1044
+ const environmentColor = Object.freeze(asColor(options.environmentColor, DEFAULT_ENVIRONMENT_COLOR));
1045
+ const ambientColor = Object.freeze(asColor(options.ambientColor, DEFAULT_AMBIENT_COLOR));
1046
+ const environmentLighting = resolveEnvironmentLighting(
1047
+ options.environmentLighting,
1048
+ environmentColor,
1049
+ ambientColor
1050
+ );
1051
+
1052
+ return Object.freeze({
1053
+ width,
1054
+ height,
1055
+ maxDepth,
1056
+ tileSize,
1057
+ samplesPerPixel,
1058
+ tilePixelCapacity,
1059
+ sceneObjects,
1060
+ sceneObjectCount: sceneObjects.length,
1061
+ sceneObjectCapacity,
1062
+ accelerationBuildMode,
1063
+ gpuAccelerationBuildRequired: accelerationBuildMode === "gpu" && triangleCount > 0,
1064
+ gpuMeshSource,
1065
+ meshAcceleration,
1066
+ emissiveTriangleIndices,
1067
+ emissiveTriangleCount: emissiveTriangleIndices.count,
1068
+ emissiveTriangleCapacity: emissiveTriangleIndices.capacity,
1069
+ triangleCount,
1070
+ triangleCapacity,
1071
+ bvhNodeCount,
1072
+ bvhNodeCapacity,
1073
+ bvhLeafSortCapacity,
1074
+ bvhSortStages,
1075
+ bvhBuildLevels,
1076
+ camera,
1077
+ environmentColor: environmentLighting.environmentColor,
1078
+ ambientColor: environmentLighting.ambientColor,
1079
+ environmentLighting,
1080
+ displayQuality: options.displayQuality === true,
1081
+ requiresMeshBvhForDisplayQuality: true,
1082
+ denoise: options.denoise !== false,
1083
+ frameIndex: readNonNegativeInteger("frameIndex", options.frameIndex, 0),
1084
+ memory: estimateWavefrontPathTracingMemory({
1085
+ tilePixelCapacity,
1086
+ sceneObjectCapacity,
1087
+ triangleCapacity,
1088
+ bvhNodeCapacity,
1089
+ bvhLeafSortCapacity,
1090
+ emissiveTriangleCapacity: emissiveTriangleIndices.capacity,
1091
+ }),
1092
+ });
1093
+ }
1094
+
1095
+ export function supportsWavefrontPathTracingCompute(options = {}) {
1096
+ const navigatorRef = options.navigator ?? globalThis.navigator;
1097
+ return typeof navigatorRef?.gpu?.requestAdapter === "function";
1098
+ }
1099
+
1100
+ function getGpuUsageConstants() {
1101
+ if (
1102
+ typeof GPUBufferUsage === "undefined" ||
1103
+ typeof GPUTextureUsage === "undefined" ||
1104
+ typeof GPUShaderStage === "undefined"
1105
+ ) {
1106
+ throw new Error("WebGPU runtime unavailable. Required GPU constants are missing.");
1107
+ }
1108
+
1109
+ return {
1110
+ buffer: GPUBufferUsage,
1111
+ texture: GPUTextureUsage,
1112
+ shader: GPUShaderStage,
1113
+ map: typeof GPUMapMode === "undefined" ? null : GPUMapMode,
1114
+ };
1115
+ }
1116
+
1117
+ function resolveCanvas(canvasOrSelector, documentRef = globalThis.document) {
1118
+ if (typeof canvasOrSelector === "string") {
1119
+ const resolved = documentRef?.querySelector?.(canvasOrSelector);
1120
+ if (!resolved) {
1121
+ throw new Error(`Unable to find canvas for selector: ${canvasOrSelector}`);
1122
+ }
1123
+ return resolved;
1124
+ }
1125
+
1126
+ if (canvasOrSelector?.getContext) {
1127
+ return canvasOrSelector;
1128
+ }
1129
+
1130
+ const fallback = documentRef?.querySelector?.("canvas[data-plasius-wavefront-path-tracing]");
1131
+ if (!fallback) {
1132
+ throw new Error("A canvas is required for WebGPU wavefront path tracing.");
1133
+ }
1134
+ return fallback;
1135
+ }
1136
+
1137
+ function writeVec4(floatView, byteOffset, value) {
1138
+ const index = byteOffset / 4;
1139
+ floatView[index] = value[0] ?? 0;
1140
+ floatView[index + 1] = value[1] ?? 0;
1141
+ floatView[index + 2] = value[2] ?? 0;
1142
+ floatView[index + 3] = value[3] ?? 0;
1143
+ }
1144
+
1145
+ export function packWavefrontSceneObjects(sceneObjects, capacity = sceneObjects.length) {
1146
+ const normalized =
1147
+ Array.isArray(sceneObjects) && sceneObjects.length === 0
1148
+ ? []
1149
+ : normalizeSceneObjects(sceneObjects);
1150
+ if (normalized.length > capacity) {
1151
+ throw new Error(
1152
+ `Scene object capacity ${capacity} is too small for ${normalized.length} objects.`
1153
+ );
1154
+ }
1155
+
1156
+ const bytes = new ArrayBuffer(Math.max(1, capacity) * SCENE_OBJECT_RECORD_BYTES);
1157
+ const uintView = new Uint32Array(bytes);
1158
+ const floatView = new Float32Array(bytes);
1159
+
1160
+ normalized.forEach((object, index) => {
1161
+ const byteOffset = index * SCENE_OBJECT_RECORD_BYTES;
1162
+ const u32 = byteOffset / 4;
1163
+ uintView[u32] = object.kind;
1164
+ uintView[u32 + 1] = object.id;
1165
+ uintView[u32 + 2] = object.materialKind;
1166
+ uintView[u32 + 3] = object.flags;
1167
+ writeVec4(floatView, byteOffset + 16, [...object.center, 0]);
1168
+ writeVec4(floatView, byteOffset + 32, [...object.halfExtent, 0]);
1169
+ writeVec4(floatView, byteOffset + 48, object.color);
1170
+ writeVec4(floatView, byteOffset + 64, object.emission);
1171
+ writeVec4(floatView, byteOffset + 80, [
1172
+ object.roughness,
1173
+ object.metallic,
1174
+ object.opacity,
1175
+ object.ior,
1176
+ ]);
1177
+ });
1178
+
1179
+ return Object.freeze({
1180
+ buffer: bytes,
1181
+ objects: Object.freeze(normalized),
1182
+ count: normalized.length,
1183
+ capacity,
1184
+ });
1185
+ }
1186
+
1187
+ export function packWavefrontTriangles(triangles, capacity = triangles.length) {
1188
+ if (triangles.length > capacity) {
1189
+ throw new Error(`Triangle capacity ${capacity} is too small for ${triangles.length} triangles.`);
1190
+ }
1191
+
1192
+ const bytes = new ArrayBuffer(Math.max(1, capacity) * TRIANGLE_RECORD_BYTES);
1193
+ const uintView = new Uint32Array(bytes);
1194
+ const floatView = new Float32Array(bytes);
1195
+
1196
+ triangles.forEach((triangle, index) => {
1197
+ const byteOffset = index * TRIANGLE_RECORD_BYTES;
1198
+ const u32 = byteOffset / 4;
1199
+ uintView[u32] = triangle.triangleId;
1200
+ uintView[u32 + 1] = triangle.meshId;
1201
+ uintView[u32 + 2] = triangle.materialKind;
1202
+ uintView[u32 + 3] = triangle.flags;
1203
+ uintView[u32 + 4] = triangle.materialRefId;
1204
+ uintView[u32 + 5] = triangle.mediumRefId;
1205
+ uintView[u32 + 6] = 0;
1206
+ uintView[u32 + 7] = 0;
1207
+ writeVec4(floatView, byteOffset + 32, [...triangle.v0, 0]);
1208
+ writeVec4(floatView, byteOffset + 48, [...triangle.v1, 0]);
1209
+ writeVec4(floatView, byteOffset + 64, [...triangle.v2, 0]);
1210
+ writeVec4(floatView, byteOffset + 80, [...triangle.n0, 0]);
1211
+ writeVec4(floatView, byteOffset + 96, [...triangle.n1, 0]);
1212
+ writeVec4(floatView, byteOffset + 112, [...triangle.n2, 0]);
1213
+ writeVec4(floatView, byteOffset + 128, [...triangle.uv0, ...triangle.uv1]);
1214
+ writeVec4(floatView, byteOffset + 144, [...triangle.uv2, 0, 0]);
1215
+ writeVec4(floatView, byteOffset + 160, triangle.color);
1216
+ writeVec4(floatView, byteOffset + 176, triangle.emission);
1217
+ writeVec4(floatView, byteOffset + 192, triangle.material);
1218
+ });
1219
+
1220
+ return Object.freeze({
1221
+ buffer: bytes,
1222
+ triangles: Object.freeze(triangles),
1223
+ count: triangles.length,
1224
+ capacity,
1225
+ });
1226
+ }
1227
+
1228
+ export function packWavefrontBvhNodes(nodes, capacity = nodes.length) {
1229
+ if (nodes.length > capacity) {
1230
+ throw new Error(`BVH node capacity ${capacity} is too small for ${nodes.length} nodes.`);
1231
+ }
1232
+
1233
+ const bytes = new ArrayBuffer(Math.max(1, capacity) * BVH_NODE_RECORD_BYTES);
1234
+ const uintView = new Uint32Array(bytes);
1235
+ const floatView = new Float32Array(bytes);
1236
+
1237
+ nodes.forEach((node, index) => {
1238
+ const byteOffset = index * BVH_NODE_RECORD_BYTES;
1239
+ const u32 = byteOffset / 4;
1240
+ writeVec4(floatView, byteOffset, [...node.bounds.min, 0]);
1241
+ writeVec4(floatView, byteOffset + 16, [...node.bounds.max, 0]);
1242
+ uintView[u32 + 8] = node.triangleCount > 0 ? node.firstTriangle : node.leftChild;
1243
+ uintView[u32 + 9] = node.triangleCount;
1244
+ uintView[u32 + 10] = node.rightChild;
1245
+ uintView[u32 + 11] = 0;
1246
+ });
1247
+
1248
+ return Object.freeze({
1249
+ buffer: bytes,
1250
+ nodes: Object.freeze(nodes),
1251
+ count: nodes.length,
1252
+ capacity,
1253
+ });
1254
+ }
1255
+
1256
+ function createConfigPayload(config, tile, frameIndex, buildRange = {}) {
1257
+ const bytes = new ArrayBuffer(CONFIG_BUFFER_BYTES);
1258
+ const data = new DataView(bytes);
1259
+ const floatView = new Float32Array(bytes);
1260
+ const sampleIndex = buildRange.sampleIndex ?? 0;
1261
+ const sampleWeight = buildRange.sampleWeight ?? 1;
1262
+ data.setUint32(0, config.width, true);
1263
+ data.setUint32(4, config.height, true);
1264
+ data.setUint32(8, tile.x, true);
1265
+ data.setUint32(12, tile.y, true);
1266
+ data.setUint32(16, tile.width, true);
1267
+ data.setUint32(20, tile.height, true);
1268
+ data.setUint32(24, tile.width * tile.height, true);
1269
+ data.setUint32(28, config.maxDepth, true);
1270
+ data.setUint32(32, config.sceneObjectCount, true);
1271
+ data.setUint32(36, frameIndex, true);
1272
+ data.setUint32(40, config.denoise ? 1 : 0, true);
1273
+ data.setUint32(44, config.triangleCount, true);
1274
+ data.setUint32(48, config.bvhNodeCount, true);
1275
+ data.setUint32(52, config.displayQuality ? 1 : 0, true);
1276
+ data.setUint32(56, config.gpuMeshSource.meshes.count, true);
1277
+ data.setUint32(60, config.bvhNodeCapacity, true);
1278
+ writeVec4(floatView, 64, [...config.camera.position, 1]);
1279
+ writeVec4(floatView, 80, [...config.camera.forward, 0]);
1280
+ writeVec4(floatView, 96, [...config.camera.right, 0]);
1281
+ writeVec4(floatView, 112, [...config.camera.up, 0]);
1282
+ writeVec4(floatView, 128, [
1283
+ config.camera.tanHalfFovY,
1284
+ config.camera.aspect,
1285
+ sampleWeight,
1286
+ sampleIndex,
1287
+ ]);
1288
+ writeVec4(floatView, 144, config.environmentColor);
1289
+ writeVec4(floatView, 160, config.ambientColor);
1290
+ writeVec4(floatView, 176, config.environmentLighting.horizonColor);
1291
+ writeVec4(floatView, 192, config.environmentLighting.zenithColor);
1292
+ writeVec4(floatView, 208, [
1293
+ ...config.environmentLighting.sunDirection,
1294
+ config.environmentLighting.intensity,
1295
+ ]);
1296
+ writeVec4(floatView, 224, config.environmentLighting.sunColor);
1297
+ data.setUint32(240, buildRange.start ?? 0, true);
1298
+ data.setUint32(244, buildRange.count ?? 0, true);
1299
+ data.setUint32(248, buildRange.sortItemCount ?? 0, true);
1300
+ data.setUint32(252, config.emissiveTriangleCount ?? 0, true);
1301
+ return bytes;
1302
+ }
1303
+
1304
+ function createTiles(width, height, tileSize) {
1305
+ const tiles = [];
1306
+ for (let y = 0; y < height; y += tileSize) {
1307
+ for (let x = 0; x < width; x += tileSize) {
1308
+ tiles.push(
1309
+ Object.freeze({
1310
+ x,
1311
+ y,
1312
+ width: Math.min(tileSize, width - x),
1313
+ height: Math.min(tileSize, height - y),
1314
+ })
1315
+ );
1316
+ }
1317
+ }
1318
+ return Object.freeze(tiles);
1319
+ }
1320
+
1321
+ function clampTileSizeForDevice(config, device) {
1322
+ const limit = Number(device?.limits?.maxStorageBufferBindingSize);
1323
+ if (!Number.isFinite(limit) || limit <= 0) {
1324
+ return config.tileSize;
1325
+ }
1326
+
1327
+ const maxPixelsByRay = Math.floor(limit / RAY_RECORD_BYTES);
1328
+ const maxPixelsByHit = Math.floor(limit / HIT_RECORD_BYTES);
1329
+ const maxPixels = Math.max(256, Math.min(maxPixelsByRay, maxPixelsByHit));
1330
+ if (config.tilePixelCapacity <= maxPixels) {
1331
+ return config.tileSize;
1332
+ }
1333
+
1334
+ return Math.max(16, Math.floor(Math.sqrt(maxPixels)));
1335
+ }
1336
+
1337
+ function createBuffer(device, usage, size, label) {
1338
+ return device.createBuffer({
1339
+ label,
1340
+ size: Math.max(4, size),
1341
+ usage,
1342
+ });
1343
+ }
1344
+
1345
+ function alignTo(value, alignment) {
1346
+ const resolvedAlignment = Math.max(1, alignment);
1347
+ return Math.ceil(value / resolvedAlignment) * resolvedAlignment;
1348
+ }
1349
+
1350
+ async function getPipelineDiagnostics(shaderModule) {
1351
+ if (typeof shaderModule?.compilationInfo !== "function") {
1352
+ return "";
1353
+ }
1354
+ try {
1355
+ const info = await shaderModule.compilationInfo();
1356
+ const messages = info.messages ?? [];
1357
+ if (messages.length === 0) {
1358
+ return "";
1359
+ }
1360
+ return messages
1361
+ .map((message) => {
1362
+ const line = message.lineNum ?? "?";
1363
+ const column = message.linePos ?? "?";
1364
+ return `line ${line}:${column} ${message.message}`;
1365
+ })
1366
+ .join("\n");
1367
+ } catch {
1368
+ return "";
1369
+ }
1370
+ }
1371
+
1372
+ async function createComputePipeline(device, shaderModule, layout, entryPoint, label) {
1373
+ const descriptor = {
1374
+ label,
1375
+ layout,
1376
+ compute: {
1377
+ module: shaderModule,
1378
+ entryPoint,
1379
+ },
1380
+ };
1381
+
1382
+ try {
1383
+ if (typeof device.createComputePipelineAsync === "function") {
1384
+ return await device.createComputePipelineAsync(descriptor);
1385
+ }
1386
+ return device.createComputePipeline(descriptor);
1387
+ } catch (error) {
1388
+ const diagnostics = await getPipelineDiagnostics(shaderModule);
1389
+ const suffix = diagnostics ? `\n${diagnostics}` : "";
1390
+ throw new Error(`WGSL compilation failed for ${label}: ${error.message}${suffix}`, {
1391
+ cause: error,
1392
+ });
1393
+ }
1394
+ }
1395
+
1396
+ async function createRenderPipeline(device, descriptor) {
1397
+ if (typeof device.createRenderPipelineAsync === "function") {
1398
+ return device.createRenderPipelineAsync(descriptor);
1399
+ }
1400
+ return device.createRenderPipeline(descriptor);
1401
+ }
1402
+
1403
+ const WAVEFRONT_COMPUTE_WGSL = `
1404
+ const RAY_FLAG_GUIDED_EMISSIVE: u32 = 1u;
1405
+
1406
+ struct RayRecord {
1407
+ rayId: u32,
1408
+ parentRayId: u32,
1409
+ sourcePixelId: u32,
1410
+ sampleId: u32,
1411
+ bounce: u32,
1412
+ mediumRefId: u32,
1413
+ flags: u32,
1414
+ pad0: u32,
1415
+ origin: vec4<f32>,
1416
+ direction: vec4<f32>,
1417
+ throughput: vec4<f32>,
1418
+ };
1419
+
1420
+ struct HitRecord {
1421
+ rayId: u32,
1422
+ sourcePixelId: u32,
1423
+ hitType: u32,
1424
+ objectId: u32,
1425
+ materialKind: u32,
1426
+ frontFace: u32,
1427
+ primitiveId: u32,
1428
+ materialRefId: u32,
1429
+ mediumRefId: u32,
1430
+ pad0: u32,
1431
+ pad1: u32,
1432
+ pad2: u32,
1433
+ distance: f32,
1434
+ pad3: vec3<f32>,
1435
+ position: vec4<f32>,
1436
+ geometricNormal: vec4<f32>,
1437
+ shadingNormal: vec4<f32>,
1438
+ barycentric: vec4<f32>,
1439
+ uv: vec4<f32>,
1440
+ color: vec4<f32>,
1441
+ emission: vec4<f32>,
1442
+ material: vec4<f32>,
1443
+ };
1444
+
1445
+ struct SceneObject {
1446
+ kind: u32,
1447
+ objectId: u32,
1448
+ materialKind: u32,
1449
+ flags: u32,
1450
+ center: vec4<f32>,
1451
+ halfExtent: vec4<f32>,
1452
+ color: vec4<f32>,
1453
+ emission: vec4<f32>,
1454
+ material: vec4<f32>,
1455
+ };
1456
+
1457
+ struct TriangleRecord {
1458
+ triangleId: u32,
1459
+ meshId: u32,
1460
+ materialKind: u32,
1461
+ flags: u32,
1462
+ materialRefId: u32,
1463
+ mediumRefId: u32,
1464
+ pad0: u32,
1465
+ pad1: u32,
1466
+ v0: vec4<f32>,
1467
+ v1: vec4<f32>,
1468
+ v2: vec4<f32>,
1469
+ n0: vec4<f32>,
1470
+ n1: vec4<f32>,
1471
+ n2: vec4<f32>,
1472
+ uv0uv1: vec4<f32>,
1473
+ uv2Pad: vec4<f32>,
1474
+ color: vec4<f32>,
1475
+ emission: vec4<f32>,
1476
+ material: vec4<f32>,
1477
+ };
1478
+
1479
+ struct BvhNode {
1480
+ boundsMin: vec4<f32>,
1481
+ boundsMax: vec4<f32>,
1482
+ childOrFirst: u32,
1483
+ triangleCount: u32,
1484
+ rightChild: u32,
1485
+ pad0: u32,
1486
+ };
1487
+
1488
+ struct BvhLeafRef {
1489
+ key: u32,
1490
+ triangleIndex: u32,
1491
+ pad0: u32,
1492
+ pad1: u32,
1493
+ };
1494
+
1495
+ struct ScatterResult {
1496
+ direction: vec4<f32>,
1497
+ flags: u32,
1498
+ pad0: u32,
1499
+ pad1: u32,
1500
+ pad2: u32,
1501
+ };
1502
+
1503
+ struct MeshVertex {
1504
+ position: vec4<f32>,
1505
+ normal: vec4<f32>,
1506
+ uv: vec4<f32>,
1507
+ };
1508
+
1509
+ struct MeshRange {
1510
+ meshId: u32,
1511
+ materialKind: u32,
1512
+ flags: u32,
1513
+ materialRefId: u32,
1514
+ mediumRefId: u32,
1515
+ firstIndex: u32,
1516
+ indexCount: u32,
1517
+ firstTriangle: u32,
1518
+ triangleCount: u32,
1519
+ firstVertex: u32,
1520
+ vertexCount: u32,
1521
+ pad0: u32,
1522
+ color: vec4<f32>,
1523
+ emission: vec4<f32>,
1524
+ material: vec4<f32>,
1525
+ };
1526
+
1527
+ struct FrameConfig {
1528
+ canvasWidth: u32,
1529
+ canvasHeight: u32,
1530
+ tileX: u32,
1531
+ tileY: u32,
1532
+ tileWidth: u32,
1533
+ tileHeight: u32,
1534
+ tilePixelCount: u32,
1535
+ maxDepth: u32,
1536
+ sceneObjectCount: u32,
1537
+ frameIndex: u32,
1538
+ denoise: u32,
1539
+ triangleCount: u32,
1540
+ bvhNodeCount: u32,
1541
+ displayQuality: u32,
1542
+ meshSourceCount: u32,
1543
+ bvhNodeCapacity: u32,
1544
+ cameraPosition: vec4<f32>,
1545
+ cameraForward: vec4<f32>,
1546
+ cameraRight: vec4<f32>,
1547
+ cameraUp: vec4<f32>,
1548
+ projectionAndSampling: vec4<f32>,
1549
+ environmentColor: vec4<f32>,
1550
+ ambientColor: vec4<f32>,
1551
+ environmentHorizonColor: vec4<f32>,
1552
+ environmentZenithColor: vec4<f32>,
1553
+ environmentSunDirectionIntensity: vec4<f32>,
1554
+ environmentSunColor: vec4<f32>,
1555
+ bvhBuildNodeStart: u32,
1556
+ bvhBuildNodeCount: u32,
1557
+ bvhSortItemCount: u32,
1558
+ emissiveTriangleCount: u32,
1559
+ };
1560
+
1561
+ struct Counters {
1562
+ activeCount: atomic<u32>,
1563
+ nextCount: atomic<u32>,
1564
+ terminatedCount: atomic<u32>,
1565
+ hitCount: atomic<u32>,
1566
+ };
1567
+
1568
+ struct Candidate {
1569
+ hit: u32,
1570
+ distance: f32,
1571
+ geometricNormal: vec3<f32>,
1572
+ shadingNormal: vec3<f32>,
1573
+ barycentric: vec3<f32>,
1574
+ uv: vec2<f32>,
1575
+ frontFace: u32,
1576
+ triangleIndex: u32,
1577
+ primitiveId: u32,
1578
+ materialRefId: u32,
1579
+ mediumRefId: u32,
1580
+ };
1581
+
1582
+ @group(0) @binding(0) var<storage, read_write> activeQueue: array<RayRecord>;
1583
+ @group(0) @binding(1) var<storage, read_write> nextQueue: array<RayRecord>;
1584
+ @group(0) @binding(2) var<storage, read_write> hits: array<HitRecord>;
1585
+ @group(0) @binding(3) var<storage, read_write> accumulation: array<vec4<f32>>;
1586
+ @group(0) @binding(4) var<storage, read> sceneObjects: array<SceneObject>;
1587
+ @group(0) @binding(5) var<uniform> config: FrameConfig;
1588
+ @group(0) @binding(6) var<storage, read_write> counters: Counters;
1589
+ @group(0) @binding(7) var outputImage: texture_storage_2d<rgba8unorm, write>;
1590
+ @group(0) @binding(8) var<storage, read_write> triangles: array<TriangleRecord>;
1591
+ @group(0) @binding(9) var<storage, read_write> bvhNodes: array<BvhNode>;
1592
+ @group(0) @binding(10) var<storage, read> meshVertices: array<MeshVertex>;
1593
+ @group(0) @binding(11) var<storage, read> meshIndices: array<u32>;
1594
+ @group(0) @binding(12) var<storage, read> meshRanges: array<MeshRange>;
1595
+ @group(0) @binding(13) var<storage, read_write> bvhLeafRefs: array<BvhLeafRef>;
1596
+ @group(0) @binding(14) var denoiseInputRadiance: texture_2d<f32>;
1597
+ @group(0) @binding(15) var denoisedRadianceImage: texture_storage_2d<rgba16float, write>;
1598
+ @group(0) @binding(16) var radianceImage: texture_storage_2d<rgba16float, write>;
1599
+ @group(0) @binding(17) var finalDenoiseInputRadiance: texture_2d<f32>;
1600
+ @group(0) @binding(18) var denoisedOutputImage: texture_storage_2d<rgba8unorm, write>;
1601
+
1602
+ fn hash_u32(value: u32) -> u32 {
1603
+ var x = value;
1604
+ x = ((x >> 16u) ^ x) * 0x45d9f3bu;
1605
+ x = ((x >> 16u) ^ x) * 0x45d9f3bu;
1606
+ x = (x >> 16u) ^ x;
1607
+ return x;
1608
+ }
1609
+
1610
+ fn mix_seed(pixelId: u32, sampleId: u32, bounce: u32, frameIndex: u32, dimension: u32) -> u32 {
1611
+ var x =
1612
+ (pixelId * 747796405u) ^
1613
+ (sampleId * 2891336453u) ^
1614
+ (bounce * 277803737u) ^
1615
+ (frameIndex * 1442695041u) ^
1616
+ (dimension * 1597334677u);
1617
+ x = x ^ (x >> 16u);
1618
+ x = x * 0x7feb352du;
1619
+ x = x ^ (x >> 15u);
1620
+ x = x * 0x846ca68bu;
1621
+ x = x ^ (x >> 16u);
1622
+ return x;
1623
+ }
1624
+
1625
+ fn random01(seed: u32) -> f32 {
1626
+ return f32(hash_u32(seed) & 0x00ffffffu) / 16777215.0;
1627
+ }
1628
+
1629
+ fn safe_normalize(value: vec3<f32>, fallback: vec3<f32>) -> vec3<f32> {
1630
+ let len = length(value);
1631
+ if (len <= 0.000001) {
1632
+ return fallback;
1633
+ }
1634
+ return value / len;
1635
+ }
1636
+
1637
+ fn saturate(value: f32) -> f32 {
1638
+ return clamp(value, 0.0, 1.0);
1639
+ }
1640
+
1641
+ fn environment_radiance(direction: vec3<f32>) -> vec3<f32> {
1642
+ let rayDirection = safe_normalize(direction, vec3<f32>(0.0, 1.0, 0.0));
1643
+ let upFactor = saturate(rayDirection.y * 0.5 + 0.5);
1644
+ let sunDirection = safe_normalize(
1645
+ config.environmentSunDirectionIntensity.xyz,
1646
+ vec3<f32>(0.0, 1.0, 0.0)
1647
+ );
1648
+ let sunGlow = pow(saturate(dot(rayDirection, sunDirection)), 192.0);
1649
+ let gradient =
1650
+ config.environmentHorizonColor.xyz * (1.0 - upFactor) +
1651
+ config.environmentZenithColor.xyz * upFactor;
1652
+ return (
1653
+ gradient +
1654
+ config.environmentSunColor.xyz * sunGlow
1655
+ ) * max(config.environmentSunDirectionIntensity.w, 0.0001);
1656
+ }
1657
+
1658
+ fn default_mesh_range() -> MeshRange {
1659
+ return MeshRange(
1660
+ 0u,
1661
+ 0u,
1662
+ 0u,
1663
+ 0u,
1664
+ 0u,
1665
+ 0u,
1666
+ 0u,
1667
+ 0u,
1668
+ 0u,
1669
+ 0u,
1670
+ 0u,
1671
+ 0u,
1672
+ vec4<f32>(0.72, 0.72, 0.68, 1.0),
1673
+ vec4<f32>(0.0),
1674
+ vec4<f32>(0.72, 0.0, 1.0, 1.45)
1675
+ );
1676
+ }
1677
+
1678
+ fn mesh_range_for_triangle(triangleIndex: u32) -> MeshRange {
1679
+ var selected = default_mesh_range();
1680
+ for (var meshIndex = 0u; meshIndex < config.meshSourceCount; meshIndex = meshIndex + 1u) {
1681
+ let mesh = meshRanges[meshIndex];
1682
+ let triangleStart = mesh.firstTriangle;
1683
+ let triangleEnd = mesh.firstTriangle + mesh.triangleCount;
1684
+ if (triangleIndex >= triangleStart && triangleIndex < triangleEnd) {
1685
+ selected = mesh;
1686
+ break;
1687
+ }
1688
+ }
1689
+ return selected;
1690
+ }
1691
+
1692
+ fn node_bounds_min(left: BvhNode, right: BvhNode) -> vec3<f32> {
1693
+ return min(left.boundsMin.xyz, right.boundsMin.xyz);
1694
+ }
1695
+
1696
+ fn node_bounds_max(left: BvhNode, right: BvhNode) -> vec3<f32> {
1697
+ return max(left.boundsMax.xyz, right.boundsMax.xyz);
1698
+ }
1699
+
1700
+ fn ordered_float_key(value: f32) -> u32 {
1701
+ let bits = bitcast<u32>(value);
1702
+ let sign = bits & 0x80000000u;
1703
+ let mask = select(0x80000000u, 0xffffffffu, sign != 0u);
1704
+ return bits ^ mask;
1705
+ }
1706
+
1707
+ fn split_by_3(value: u32) -> u32 {
1708
+ var x = value & 0x000003ffu;
1709
+ x = (x | (x << 16u)) & 0x030000ffu;
1710
+ x = (x | (x << 8u)) & 0x0300f00fu;
1711
+ x = (x | (x << 4u)) & 0x030c30c3u;
1712
+ x = (x | (x << 2u)) & 0x09249249u;
1713
+ return x;
1714
+ }
1715
+
1716
+ fn morton_key_from_centroid(centroid: vec3<f32>) -> u32 {
1717
+ let x = (ordered_float_key(centroid.x) >> 12u) & 0x000003ffu;
1718
+ let y = (ordered_float_key(centroid.y) >> 12u) & 0x000003ffu;
1719
+ let z = (ordered_float_key(centroid.z) >> 12u) & 0x000003ffu;
1720
+ return (split_by_3(x) << 2u) | (split_by_3(y) << 1u) | split_by_3(z);
1721
+ }
1722
+
1723
+ fn leaf_ref_less(left: BvhLeafRef, right: BvhLeafRef) -> bool {
1724
+ if (left.key < right.key) {
1725
+ return true;
1726
+ }
1727
+ if (left.key > right.key) {
1728
+ return false;
1729
+ }
1730
+ return left.triangleIndex < right.triangleIndex;
1731
+ }
1732
+
1733
+ @compute @workgroup_size(64)
1734
+ fn prepareMeshTrianglesAndLeaves(@builtin(global_invocation_id) globalId: vec3<u32>) {
1735
+ let triangleIndex = globalId.x;
1736
+ if (triangleIndex >= config.triangleCount) {
1737
+ if (triangleIndex < config.bvhSortItemCount) {
1738
+ bvhLeafRefs[triangleIndex] = BvhLeafRef(0xffffffffu, 0xffffffffu, 0u, 0u);
1739
+ }
1740
+ return;
1741
+ }
1742
+
1743
+ let mesh = mesh_range_for_triangle(triangleIndex);
1744
+ let localTriangle = triangleIndex - mesh.firstTriangle;
1745
+ let indexOffset = mesh.firstIndex + localTriangle * 3u;
1746
+ let index0 = meshIndices[indexOffset];
1747
+ let index1 = meshIndices[indexOffset + 1u];
1748
+ let index2 = meshIndices[indexOffset + 2u];
1749
+ let vertex0 = meshVertices[index0];
1750
+ let vertex1 = meshVertices[index1];
1751
+ let vertex2 = meshVertices[index2];
1752
+ let edge1 = vertex1.position.xyz - vertex0.position.xyz;
1753
+ let edge2 = vertex2.position.xyz - vertex0.position.xyz;
1754
+ let centroid = (vertex0.position.xyz + vertex1.position.xyz + vertex2.position.xyz) / 3.0;
1755
+ let faceNormal = safe_normalize(cross(edge1, edge2), vec3<f32>(0.0, 1.0, 0.0));
1756
+ let n0 = select(faceNormal, safe_normalize(vertex0.normal.xyz, faceNormal), vertex0.normal.w > 0.5);
1757
+ let n1 = select(faceNormal, safe_normalize(vertex1.normal.xyz, faceNormal), vertex1.normal.w > 0.5);
1758
+ let n2 = select(faceNormal, safe_normalize(vertex2.normal.xyz, faceNormal), vertex2.normal.w > 0.5);
1759
+ let uv0 = select(vec2<f32>(0.0), vertex0.uv.xy, vertex0.uv.z > 0.5);
1760
+ let uv1 = select(vec2<f32>(0.0), vertex1.uv.xy, vertex1.uv.z > 0.5);
1761
+ let uv2 = select(vec2<f32>(0.0), vertex2.uv.xy, vertex2.uv.z > 0.5);
1762
+
1763
+ triangles[triangleIndex] = TriangleRecord(
1764
+ triangleIndex,
1765
+ mesh.meshId,
1766
+ mesh.materialKind,
1767
+ mesh.flags,
1768
+ mesh.materialRefId,
1769
+ mesh.mediumRefId,
1770
+ 0u,
1771
+ 0u,
1772
+ vec4<f32>(vertex0.position.xyz, 0.0),
1773
+ vec4<f32>(vertex1.position.xyz, 0.0),
1774
+ vec4<f32>(vertex2.position.xyz, 0.0),
1775
+ vec4<f32>(n0, 0.0),
1776
+ vec4<f32>(n1, 0.0),
1777
+ vec4<f32>(n2, 0.0),
1778
+ vec4<f32>(uv0, uv1),
1779
+ vec4<f32>(uv2, 0.0, 0.0),
1780
+ mesh.color,
1781
+ mesh.emission,
1782
+ mesh.material
1783
+ );
1784
+
1785
+ let leafBase = config.triangleCount - 1u;
1786
+ let nodeIndex = leafBase + triangleIndex;
1787
+ let boundsMin = min(vertex0.position.xyz, min(vertex1.position.xyz, vertex2.position.xyz));
1788
+ let boundsMax = max(vertex0.position.xyz, max(vertex1.position.xyz, vertex2.position.xyz));
1789
+ bvhLeafRefs[triangleIndex] = BvhLeafRef(
1790
+ morton_key_from_centroid(centroid),
1791
+ triangleIndex,
1792
+ 0u,
1793
+ 0u
1794
+ );
1795
+ bvhNodes[nodeIndex] = BvhNode(
1796
+ vec4<f32>(boundsMin, 0.0),
1797
+ vec4<f32>(boundsMax, 0.0),
1798
+ triangleIndex,
1799
+ 1u,
1800
+ 0u,
1801
+ 0u
1802
+ );
1803
+ }
1804
+
1805
+ @compute @workgroup_size(64)
1806
+ fn sortBvhLeafRefs(@builtin(global_invocation_id) globalId: vec3<u32>) {
1807
+ let index = globalId.x;
1808
+ let sortCount = config.bvhSortItemCount;
1809
+ if (sortCount <= 1u || index >= sortCount) {
1810
+ return;
1811
+ }
1812
+
1813
+ let compareDistance = config.bvhBuildNodeStart;
1814
+ let sequenceSize = config.bvhBuildNodeCount;
1815
+ if (compareDistance == 0u || sequenceSize == 0u) {
1816
+ return;
1817
+ }
1818
+
1819
+ let partner = index ^ compareDistance;
1820
+ if (partner <= index || partner >= sortCount) {
1821
+ return;
1822
+ }
1823
+
1824
+ let left = bvhLeafRefs[index];
1825
+ let right = bvhLeafRefs[partner];
1826
+ let ascending = (index & sequenceSize) == 0u;
1827
+ let leftIsLess = leaf_ref_less(left, right);
1828
+ let rightIsLess = leaf_ref_less(right, left);
1829
+ let shouldSwap = select(leftIsLess, rightIsLess, ascending);
1830
+ if (shouldSwap) {
1831
+ bvhLeafRefs[index] = right;
1832
+ bvhLeafRefs[partner] = left;
1833
+ }
1834
+ }
1835
+
1836
+ @compute @workgroup_size(64)
1837
+ fn writeSortedBvhLeaves(@builtin(global_invocation_id) globalId: vec3<u32>) {
1838
+ let sortedIndex = globalId.x;
1839
+ if (sortedIndex >= config.triangleCount || config.triangleCount == 0u) {
1840
+ return;
1841
+ }
1842
+
1843
+ let leafRef = bvhLeafRefs[sortedIndex];
1844
+ if (leafRef.triangleIndex >= config.triangleCount) {
1845
+ return;
1846
+ }
1847
+
1848
+ let triangle = triangles[leafRef.triangleIndex];
1849
+ let boundsMin = min(triangle.v0.xyz, min(triangle.v1.xyz, triangle.v2.xyz));
1850
+ let boundsMax = max(triangle.v0.xyz, max(triangle.v1.xyz, triangle.v2.xyz));
1851
+ let leafBase = config.triangleCount - 1u;
1852
+ let nodeIndex = leafBase + sortedIndex;
1853
+ bvhNodes[nodeIndex] = BvhNode(
1854
+ vec4<f32>(boundsMin, 0.0),
1855
+ vec4<f32>(boundsMax, 0.0),
1856
+ leafRef.triangleIndex,
1857
+ 1u,
1858
+ 0u,
1859
+ 0u
1860
+ );
1861
+ }
1862
+
1863
+ @compute @workgroup_size(64)
1864
+ fn buildBvhInternalLevel(@builtin(global_invocation_id) globalId: vec3<u32>) {
1865
+ if (config.triangleCount <= 1u || globalId.x >= config.bvhBuildNodeCount) {
1866
+ return;
1867
+ }
1868
+
1869
+ let internalCount = config.triangleCount - 1u;
1870
+ let nodeIndex = config.bvhBuildNodeStart + globalId.x;
1871
+ if (nodeIndex >= internalCount || nodeIndex >= config.bvhNodeCapacity) {
1872
+ return;
1873
+ }
1874
+
1875
+ let leftIndex = nodeIndex * 2u + 1u;
1876
+ let rightIndex = nodeIndex * 2u + 2u;
1877
+ if (rightIndex >= config.bvhNodeCapacity || rightIndex >= config.bvhNodeCount) {
1878
+ return;
1879
+ }
1880
+
1881
+ let left = bvhNodes[leftIndex];
1882
+ let right = bvhNodes[rightIndex];
1883
+ bvhNodes[nodeIndex] = BvhNode(
1884
+ vec4<f32>(node_bounds_min(left, right), 0.0),
1885
+ vec4<f32>(node_bounds_max(left, right), 0.0),
1886
+ leftIndex,
1887
+ 0u,
1888
+ rightIndex,
1889
+ 0u
1890
+ );
1891
+ }
1892
+
1893
+ fn make_ray(pixelIndex: u32) -> RayRecord {
1894
+ let localX = pixelIndex % config.tileWidth;
1895
+ let localY = pixelIndex / config.tileWidth;
1896
+ let px = config.tileX + localX;
1897
+ let py = config.tileY + localY;
1898
+ let sampleId = u32(config.projectionAndSampling.w);
1899
+ let sourcePixelId = py * config.canvasWidth + px;
1900
+ let jitterX = random01(mix_seed(sourcePixelId, sampleId, 0u, config.frameIndex, 1u)) - 0.5;
1901
+ let jitterY = random01(mix_seed(sourcePixelId, sampleId, 0u, config.frameIndex, 2u)) - 0.5;
1902
+ let ndcX = ((f32(px) + 0.5 + jitterX * 0.35) / f32(config.canvasWidth)) * 2.0 - 1.0;
1903
+ let ndcY = 1.0 - ((f32(py) + 0.5 + jitterY * 0.35) / f32(config.canvasHeight)) * 2.0;
1904
+ let viewX = ndcX * config.projectionAndSampling.x * config.projectionAndSampling.y;
1905
+ let viewY = ndcY * config.projectionAndSampling.x;
1906
+ let direction = safe_normalize(
1907
+ config.cameraForward.xyz + config.cameraRight.xyz * viewX + config.cameraUp.xyz * viewY,
1908
+ config.cameraForward.xyz
1909
+ );
1910
+ return RayRecord(
1911
+ pixelIndex,
1912
+ 0xffffffffu,
1913
+ sourcePixelId,
1914
+ sampleId,
1915
+ 0u,
1916
+ 0u,
1917
+ 0u,
1918
+ 0u,
1919
+ vec4<f32>(config.cameraPosition.xyz, 1.0),
1920
+ vec4<f32>(direction, 0.0),
1921
+ vec4<f32>(1.0, 1.0, 1.0, 1.0)
1922
+ );
1923
+ }
1924
+
1925
+ fn make_miss(ray: RayRecord) -> HitRecord {
1926
+ let radiance = environment_radiance(ray.direction.xyz);
1927
+ return HitRecord(
1928
+ ray.rayId,
1929
+ ray.sourcePixelId,
1930
+ 2u,
1931
+ 0u,
1932
+ 0u,
1933
+ 1u,
1934
+ 0u,
1935
+ 0u,
1936
+ 0u,
1937
+ 0u,
1938
+ 0u,
1939
+ 0u,
1940
+ -1.0,
1941
+ vec3<f32>(0.0),
1942
+ vec4<f32>(ray.origin.xyz + ray.direction.xyz * 1000.0, 1.0),
1943
+ vec4<f32>(-ray.direction.xyz, 0.0),
1944
+ vec4<f32>(-ray.direction.xyz, 0.0),
1945
+ vec4<f32>(0.0),
1946
+ vec4<f32>(0.0),
1947
+ vec4<f32>(radiance, 1.0),
1948
+ vec4<f32>(0.0),
1949
+ vec4<f32>(1.0, 0.0, 1.0, 1.0)
1950
+ );
1951
+ }
1952
+
1953
+ fn intersect_sphere(ray: RayRecord, object: SceneObject) -> Candidate {
1954
+ let oc = ray.origin.xyz - object.center.xyz;
1955
+ let radius = max(object.halfExtent.x, 0.001);
1956
+ let halfB = dot(oc, ray.direction.xyz);
1957
+ let c = dot(oc, oc) - radius * radius;
1958
+ let discriminant = halfB * halfB - c;
1959
+ if (discriminant < 0.0) {
1960
+ return no_candidate();
1961
+ }
1962
+ let sqrtD = sqrt(discriminant);
1963
+ var distance = -halfB - sqrtD;
1964
+ if (distance <= 0.001) {
1965
+ distance = -halfB + sqrtD;
1966
+ }
1967
+ if (distance <= 0.001) {
1968
+ return no_candidate();
1969
+ }
1970
+ let position = ray.origin.xyz + ray.direction.xyz * distance;
1971
+ let outward = safe_normalize((position - object.center.xyz) / radius, vec3<f32>(0.0, 1.0, 0.0));
1972
+ let frontFace = select(0u, 1u, dot(ray.direction.xyz, outward) < 0.0);
1973
+ let normal = select(-outward, outward, frontFace == 1u);
1974
+ return surface_candidate(
1975
+ distance,
1976
+ normal,
1977
+ normal,
1978
+ vec3<f32>(1.0, 0.0, 0.0),
1979
+ vec2<f32>(0.0),
1980
+ frontFace,
1981
+ 0xffffffffu,
1982
+ object.objectId,
1983
+ object.objectId,
1984
+ 0u
1985
+ );
1986
+ }
1987
+
1988
+ fn safe_inverse(value: f32) -> f32 {
1989
+ if (abs(value) < 0.000001) {
1990
+ return select(-1000000.0, 1000000.0, value >= 0.0);
1991
+ }
1992
+ return 1.0 / value;
1993
+ }
1994
+
1995
+ fn intersect_box(ray: RayRecord, object: SceneObject) -> Candidate {
1996
+ let boxMin = object.center.xyz - object.halfExtent.xyz;
1997
+ let boxMax = object.center.xyz + object.halfExtent.xyz;
1998
+ let inv = vec3<f32>(
1999
+ safe_inverse(ray.direction.x),
2000
+ safe_inverse(ray.direction.y),
2001
+ safe_inverse(ray.direction.z)
2002
+ );
2003
+ let t0 = (boxMin - ray.origin.xyz) * inv;
2004
+ let t1 = (boxMax - ray.origin.xyz) * inv;
2005
+ let tNear = min(t0, t1);
2006
+ let tFar = max(t0, t1);
2007
+ let entry = max(max(tNear.x, tNear.y), tNear.z);
2008
+ let exit = min(min(tFar.x, tFar.y), tFar.z);
2009
+ if (exit < max(entry, 0.001)) {
2010
+ return no_candidate();
2011
+ }
2012
+ let distance = max(entry, 0.001);
2013
+ let position = ray.origin.xyz + ray.direction.xyz * distance;
2014
+ let rel = (position - object.center.xyz) / max(object.halfExtent.xyz, vec3<f32>(0.001));
2015
+ let absRel = abs(rel);
2016
+ var outward = vec3<f32>(0.0, 1.0, 0.0);
2017
+ if (absRel.x >= absRel.y && absRel.x >= absRel.z) {
2018
+ outward = vec3<f32>(select(-1.0, 1.0, rel.x >= 0.0), 0.0, 0.0);
2019
+ } else if (absRel.y >= absRel.z) {
2020
+ outward = vec3<f32>(0.0, select(-1.0, 1.0, rel.y >= 0.0), 0.0);
2021
+ } else {
2022
+ outward = vec3<f32>(0.0, 0.0, select(-1.0, 1.0, rel.z >= 0.0));
2023
+ }
2024
+ let frontFace = select(0u, 1u, dot(ray.direction.xyz, outward) < 0.0);
2025
+ let normal = select(-outward, outward, frontFace == 1u);
2026
+ return surface_candidate(
2027
+ distance,
2028
+ normal,
2029
+ normal,
2030
+ vec3<f32>(1.0, 0.0, 0.0),
2031
+ vec2<f32>(0.0),
2032
+ frontFace,
2033
+ 0xffffffffu,
2034
+ object.objectId,
2035
+ object.objectId,
2036
+ 0u
2037
+ );
2038
+ }
2039
+
2040
+ fn intersect_bounds(ray: RayRecord, boundsMin: vec3<f32>, boundsMax: vec3<f32>, nearest: f32) -> bool {
2041
+ let inv = vec3<f32>(
2042
+ safe_inverse(ray.direction.x),
2043
+ safe_inverse(ray.direction.y),
2044
+ safe_inverse(ray.direction.z)
2045
+ );
2046
+ let t0 = (boundsMin - ray.origin.xyz) * inv;
2047
+ let t1 = (boundsMax - ray.origin.xyz) * inv;
2048
+ let tNear = min(t0, t1);
2049
+ let tFar = max(t0, t1);
2050
+ let entry = max(max(tNear.x, tNear.y), tNear.z);
2051
+ let exit = min(min(tFar.x, tFar.y), tFar.z);
2052
+ return exit >= max(entry, 0.001) && entry <= nearest;
2053
+ }
2054
+
2055
+ fn repair_shading_normal(geometricNormal: vec3<f32>, shadingNormal: vec3<f32>) -> vec3<f32> {
2056
+ var normal = safe_normalize(shadingNormal, geometricNormal);
2057
+ if (dot(normal, geometricNormal) < 0.0) {
2058
+ normal = -normal;
2059
+ }
2060
+ return normal;
2061
+ }
2062
+
2063
+ fn no_candidate() -> Candidate {
2064
+ return Candidate(
2065
+ 0u,
2066
+ 0.0,
2067
+ vec3<f32>(0.0, 1.0, 0.0),
2068
+ vec3<f32>(0.0, 1.0, 0.0),
2069
+ vec3<f32>(0.0),
2070
+ vec2<f32>(0.0),
2071
+ 1u,
2072
+ 0xffffffffu,
2073
+ 0xffffffffu,
2074
+ 0u,
2075
+ 0u
2076
+ );
2077
+ }
2078
+
2079
+ fn surface_candidate(
2080
+ distance: f32,
2081
+ geometricNormal: vec3<f32>,
2082
+ shadingNormal: vec3<f32>,
2083
+ barycentric: vec3<f32>,
2084
+ uv: vec2<f32>,
2085
+ frontFace: u32,
2086
+ triangleIndex: u32,
2087
+ primitiveId: u32,
2088
+ materialRefId: u32,
2089
+ mediumRefId: u32
2090
+ ) -> Candidate {
2091
+ return Candidate(
2092
+ 1u,
2093
+ distance,
2094
+ geometricNormal,
2095
+ shadingNormal,
2096
+ barycentric,
2097
+ uv,
2098
+ frontFace,
2099
+ triangleIndex,
2100
+ primitiveId,
2101
+ materialRefId,
2102
+ mediumRefId
2103
+ );
2104
+ }
2105
+
2106
+ fn intersect_triangle(ray: RayRecord, triangle: TriangleRecord, triangleIndex: u32) -> Candidate {
2107
+ let edge1 = triangle.v1.xyz - triangle.v0.xyz;
2108
+ let edge2 = triangle.v2.xyz - triangle.v0.xyz;
2109
+ let pvec = cross(ray.direction.xyz, edge2);
2110
+ let det = dot(edge1, pvec);
2111
+ if (abs(det) < 0.0000001) {
2112
+ return no_candidate();
2113
+ }
2114
+
2115
+ let invDet = 1.0 / det;
2116
+ let tvec = ray.origin.xyz - triangle.v0.xyz;
2117
+ let u = dot(tvec, pvec) * invDet;
2118
+ if (u < 0.0 || u > 1.0) {
2119
+ return no_candidate();
2120
+ }
2121
+
2122
+ let qvec = cross(tvec, edge1);
2123
+ let v = dot(ray.direction.xyz, qvec) * invDet;
2124
+ if (v < 0.0 || u + v > 1.0) {
2125
+ return no_candidate();
2126
+ }
2127
+
2128
+ let distance = dot(edge2, qvec) * invDet;
2129
+ if (distance <= 0.001) {
2130
+ return no_candidate();
2131
+ }
2132
+
2133
+ let geometric = safe_normalize(cross(edge1, edge2), vec3<f32>(0.0, 1.0, 0.0));
2134
+ let frontFace = select(0u, 1u, dot(ray.direction.xyz, geometric) < 0.0);
2135
+ let orientedGeometric = select(-geometric, geometric, frontFace == 1u);
2136
+ let w = 1.0 - u - v;
2137
+ let interpolated =
2138
+ triangle.n0.xyz * w +
2139
+ triangle.n1.xyz * u +
2140
+ triangle.n2.xyz * v;
2141
+ let shading = repair_shading_normal(orientedGeometric, interpolated);
2142
+ let barycentric = vec3<f32>(w, u, v);
2143
+ let uv =
2144
+ triangle.uv0uv1.xy * w +
2145
+ triangle.uv0uv1.zw * u +
2146
+ triangle.uv2Pad.xy * v;
2147
+ return surface_candidate(
2148
+ distance,
2149
+ orientedGeometric,
2150
+ shading,
2151
+ barycentric,
2152
+ uv,
2153
+ frontFace,
2154
+ triangleIndex,
2155
+ triangle.triangleId,
2156
+ triangle.materialRefId,
2157
+ triangle.mediumRefId
2158
+ );
2159
+ }
2160
+
2161
+ fn intersect_bvh(ray: RayRecord, initialNearest: f32) -> Candidate {
2162
+ var nearest = initialNearest;
2163
+ var best = no_candidate();
2164
+ if (config.bvhNodeCount == 0u || config.triangleCount == 0u) {
2165
+ return best;
2166
+ }
2167
+
2168
+ var stack = array<u32, 64>();
2169
+ var stackSize = 1u;
2170
+ stack[0] = 0u;
2171
+
2172
+ loop {
2173
+ if (stackSize == 0u) {
2174
+ break;
2175
+ }
2176
+
2177
+ stackSize = stackSize - 1u;
2178
+ let nodeIndex = stack[stackSize];
2179
+ if (nodeIndex >= config.bvhNodeCount) {
2180
+ continue;
2181
+ }
2182
+
2183
+ let node = bvhNodes[nodeIndex];
2184
+ if (!intersect_bounds(ray, node.boundsMin.xyz, node.boundsMax.xyz, nearest)) {
2185
+ continue;
2186
+ }
2187
+
2188
+ if (node.triangleCount > 0u) {
2189
+ for (var offset = 0u; offset < node.triangleCount; offset = offset + 1u) {
2190
+ let triangleIndex = node.childOrFirst + offset;
2191
+ if (triangleIndex >= config.triangleCount) {
2192
+ continue;
2193
+ }
2194
+ let current = intersect_triangle(ray, triangles[triangleIndex], triangleIndex);
2195
+ if (current.hit == 1u && current.distance < nearest) {
2196
+ nearest = current.distance;
2197
+ best = current;
2198
+ }
2199
+ }
2200
+ } else {
2201
+ if (stackSize + 2u <= 64u) {
2202
+ stack[stackSize] = node.childOrFirst;
2203
+ stack[stackSize + 1u] = node.rightChild;
2204
+ stackSize = stackSize + 2u;
2205
+ }
2206
+ }
2207
+ }
2208
+
2209
+ return best;
2210
+ }
2211
+
2212
+ fn emission_power(emission: vec4<f32>) -> f32 {
2213
+ return emission.x + emission.y + emission.z;
2214
+ }
2215
+
2216
+ fn sample_weight() -> f32 {
2217
+ return max(config.projectionAndSampling.z, 0.000001);
2218
+ }
2219
+
2220
+ fn clamp_sample_radiance(value: vec3<f32>) -> vec3<f32> {
2221
+ return min(max(value, vec3<f32>(0.0)), vec3<f32>(4.0));
2222
+ }
2223
+
2224
+ fn tone_map_radiance(value: vec3<f32>) -> vec3<f32> {
2225
+ let mapped = value / (vec3<f32>(1.0) + value);
2226
+ return pow(clamp(mapped, vec3<f32>(0.0), vec3<f32>(1.0)), vec3<f32>(1.0 / 2.2));
2227
+ }
2228
+
2229
+ fn denoise_range_space(value: vec3<f32>) -> vec3<f32> {
2230
+ return value / (vec3<f32>(1.0) + value);
2231
+ }
2232
+
2233
+ @compute @workgroup_size(64)
2234
+ fn generatePrimaryRays(@builtin(global_invocation_id) globalId: vec3<u32>) {
2235
+ let index = globalId.x;
2236
+ if (index == 0u) {
2237
+ atomicStore(&counters.activeCount, config.tilePixelCount);
2238
+ atomicStore(&counters.nextCount, 0u);
2239
+ atomicStore(&counters.terminatedCount, 0u);
2240
+ atomicStore(&counters.hitCount, 0u);
2241
+ }
2242
+ if (index >= config.tilePixelCount) {
2243
+ return;
2244
+ }
2245
+ activeQueue[index] = make_ray(index);
2246
+ if (u32(config.projectionAndSampling.w) == 0u) {
2247
+ accumulation[index] = vec4<f32>(0.0);
2248
+ }
2249
+ }
2250
+
2251
+ @compute @workgroup_size(64)
2252
+ fn intersectActiveQueue(@builtin(global_invocation_id) globalId: vec3<u32>) {
2253
+ let index = globalId.x;
2254
+ let activeCount = atomicLoad(&counters.activeCount);
2255
+ if (index >= activeCount) {
2256
+ return;
2257
+ }
2258
+ let ray = activeQueue[index];
2259
+ var nearest = 1000000.0;
2260
+ var hitObject = SceneObject(
2261
+ 0u,
2262
+ 0u,
2263
+ 0u,
2264
+ 0u,
2265
+ vec4<f32>(0.0),
2266
+ vec4<f32>(0.0),
2267
+ vec4<f32>(0.0),
2268
+ vec4<f32>(0.0),
2269
+ vec4<f32>(1.0, 0.0, 1.0, 1.0)
2270
+ );
2271
+ var candidate = no_candidate();
2272
+ var hitTriangle = TriangleRecord(
2273
+ 0u,
2274
+ 0u,
2275
+ 0u,
2276
+ 0u,
2277
+ 0u,
2278
+ 0u,
2279
+ 0u,
2280
+ 0u,
2281
+ vec4<f32>(0.0),
2282
+ vec4<f32>(0.0),
2283
+ vec4<f32>(0.0),
2284
+ vec4<f32>(0.0, 1.0, 0.0, 0.0),
2285
+ vec4<f32>(0.0, 1.0, 0.0, 0.0),
2286
+ vec4<f32>(0.0, 1.0, 0.0, 0.0),
2287
+ vec4<f32>(0.0),
2288
+ vec4<f32>(0.0),
2289
+ vec4<f32>(0.0),
2290
+ vec4<f32>(0.0),
2291
+ vec4<f32>(1.0, 0.0, 1.0, 1.0)
2292
+ );
2293
+
2294
+ for (var objectIndex = 0u; objectIndex < config.sceneObjectCount; objectIndex = objectIndex + 1u) {
2295
+ let object = sceneObjects[objectIndex];
2296
+ var current = no_candidate();
2297
+ if (object.kind == 1u) {
2298
+ current = intersect_sphere(ray, object);
2299
+ } else if (object.kind == 2u) {
2300
+ current = intersect_box(ray, object);
2301
+ }
2302
+ if (current.hit == 1u && current.distance < nearest) {
2303
+ nearest = current.distance;
2304
+ hitObject = object;
2305
+ candidate = current;
2306
+ }
2307
+ }
2308
+
2309
+ let meshCandidate = intersect_bvh(ray, nearest);
2310
+ if (meshCandidate.hit == 1u && meshCandidate.distance < nearest) {
2311
+ nearest = meshCandidate.distance;
2312
+ candidate = meshCandidate;
2313
+ hitTriangle = triangles[meshCandidate.triangleIndex];
2314
+ }
2315
+
2316
+ if (candidate.hit == 0u) {
2317
+ hits[index] = make_miss(ray);
2318
+ return;
2319
+ }
2320
+
2321
+ let position = ray.origin.xyz + ray.direction.xyz * candidate.distance;
2322
+ let hitMaterialKind = select(hitObject.materialKind, hitTriangle.materialKind, candidate.triangleIndex != 0xffffffffu);
2323
+ let hitObjectId = select(hitObject.objectId, hitTriangle.meshId, candidate.triangleIndex != 0xffffffffu);
2324
+ let hitColor = select(hitObject.color, hitTriangle.color, candidate.triangleIndex != 0xffffffffu);
2325
+ let hitEmission = select(hitObject.emission, hitTriangle.emission, candidate.triangleIndex != 0xffffffffu);
2326
+ let hitMaterial = select(hitObject.material, hitTriangle.material, candidate.triangleIndex != 0xffffffffu);
2327
+ let hitPrimitiveId = select(candidate.primitiveId, hitTriangle.triangleId, candidate.triangleIndex != 0xffffffffu);
2328
+ let hitMaterialRefId = select(candidate.materialRefId, hitTriangle.materialRefId, candidate.triangleIndex != 0xffffffffu);
2329
+ let hitMediumRefId = select(candidate.mediumRefId, hitTriangle.mediumRefId, candidate.triangleIndex != 0xffffffffu);
2330
+ var hitType = 0u;
2331
+ if (hitMaterialKind == 4u || emission_power(hitEmission) > 0.0001) {
2332
+ hitType = 1u;
2333
+ } else if (hitMaterialKind == 3u || hitMaterial.z < 0.999) {
2334
+ hitType = 3u;
2335
+ }
2336
+ atomicAdd(&counters.hitCount, 1u);
2337
+ hits[index] = HitRecord(
2338
+ ray.rayId,
2339
+ ray.sourcePixelId,
2340
+ hitType,
2341
+ hitObjectId,
2342
+ hitMaterialKind,
2343
+ candidate.frontFace,
2344
+ hitPrimitiveId,
2345
+ hitMaterialRefId,
2346
+ hitMediumRefId,
2347
+ 0u,
2348
+ 0u,
2349
+ 0u,
2350
+ candidate.distance,
2351
+ vec3<f32>(0.0),
2352
+ vec4<f32>(position, 1.0),
2353
+ vec4<f32>(candidate.geometricNormal, 0.0),
2354
+ vec4<f32>(candidate.shadingNormal, 0.0),
2355
+ vec4<f32>(candidate.barycentric, 0.0),
2356
+ vec4<f32>(candidate.uv, 0.0, 0.0),
2357
+ hitColor,
2358
+ hitEmission,
2359
+ hitMaterial
2360
+ );
2361
+ }
2362
+
2363
+ fn offset_origin(position: vec3<f32>, normal: vec3<f32>) -> vec3<f32> {
2364
+ return position + normal * 0.0025;
2365
+ }
2366
+
2367
+ fn random_unit_vector(seed: u32) -> vec3<f32> {
2368
+ let z = random01(seed) * 2.0 - 1.0;
2369
+ let a = random01(seed + 11u) * 6.28318530718;
2370
+ let r = sqrt(max(0.0, 1.0 - z * z));
2371
+ return vec3<f32>(r * cos(a), r * sin(a), z);
2372
+ }
2373
+
2374
+ fn schlick(cosine: f32, refractionRatio: f32) -> f32 {
2375
+ var r0 = (1.0 - refractionRatio) / (1.0 + refractionRatio);
2376
+ r0 = r0 * r0;
2377
+ return r0 + (1.0 - r0) * pow(1.0 - cosine, 5.0);
2378
+ }
2379
+
2380
+ fn refract_direction(unitDirection: vec3<f32>, normal: vec3<f32>, etaRatio: f32) -> vec3<f32> {
2381
+ let cosTheta = min(dot(-unitDirection, normal), 1.0);
2382
+ let rOutPerp = etaRatio * (unitDirection + cosTheta * normal);
2383
+ let rOutParallel = -sqrt(abs(1.0 - dot(rOutPerp, rOutPerp))) * normal;
2384
+ return safe_normalize(rOutPerp + rOutParallel, reflect(unitDirection, normal));
2385
+ }
2386
+
2387
+ fn sample_emissive_triangle_direction(hit: HitRecord, seed: u32, fallback: vec3<f32>) -> vec3<f32> {
2388
+ if (config.emissiveTriangleCount == 0u) {
2389
+ return fallback;
2390
+ }
2391
+ let lightSlot = min(u32(random01(seed + 71u) * f32(config.emissiveTriangleCount)), config.emissiveTriangleCount - 1u);
2392
+ let lightMetadata = bvhNodes[config.bvhNodeCapacity + lightSlot];
2393
+ let triangleIndex = lightMetadata.childOrFirst;
2394
+ if (triangleIndex >= config.triangleCount) {
2395
+ return fallback;
2396
+ }
2397
+
2398
+ let lightTriangle = triangles[triangleIndex];
2399
+ let r1 = random01(seed + 101u);
2400
+ let r2 = random01(seed + 193u);
2401
+ let root = sqrt(r1);
2402
+ let b0 = 1.0 - root;
2403
+ let b1 = root * (1.0 - r2);
2404
+ let b2 = root * r2;
2405
+ let lightPoint =
2406
+ lightTriangle.v0.xyz * b0 +
2407
+ lightTriangle.v1.xyz * b1 +
2408
+ lightTriangle.v2.xyz * b2;
2409
+ return safe_normalize(lightPoint - hit.position.xyz, fallback);
2410
+ }
2411
+
2412
+ fn scatter_direction(ray: RayRecord, hit: HitRecord, seed: u32) -> ScatterResult {
2413
+ let roughness = clamp(hit.material.x, 0.0, 1.0);
2414
+ if (hit.materialKind == 1u) {
2415
+ return ScatterResult(
2416
+ vec4<f32>(
2417
+ safe_normalize(
2418
+ reflect(ray.direction.xyz, hit.shadingNormal.xyz) + random_unit_vector(seed) * roughness,
2419
+ hit.shadingNormal.xyz
2420
+ ),
2421
+ 0.0
2422
+ ),
2423
+ 0u,
2424
+ 0u,
2425
+ 0u,
2426
+ 0u
2427
+ );
2428
+ }
2429
+
2430
+ if (hit.materialKind == 2u || hit.materialKind == 3u) {
2431
+ let ior = max(hit.material.w, 1.01);
2432
+ let etaRatio = select(ior, 1.0 / ior, hit.frontFace == 1u);
2433
+ let cosTheta = min(dot(-ray.direction.xyz, hit.shadingNormal.xyz), 1.0);
2434
+ let sinTheta = sqrt(max(0.0, 1.0 - cosTheta * cosTheta));
2435
+ let cannotRefract = etaRatio * sinTheta > 1.0;
2436
+ let reflectChance = schlick(cosTheta, etaRatio);
2437
+ if (cannotRefract || random01(seed + 23u) < reflectChance) {
2438
+ return ScatterResult(vec4<f32>(reflect(ray.direction.xyz, hit.shadingNormal.xyz), 0.0), 0u, 0u, 0u, 0u);
2439
+ }
2440
+ return ScatterResult(vec4<f32>(refract_direction(ray.direction.xyz, hit.shadingNormal.xyz, etaRatio), 0.0), 0u, 0u, 0u, 0u);
2441
+ }
2442
+
2443
+ let randomDiffuse = safe_normalize(
2444
+ hit.shadingNormal.xyz + random_unit_vector(seed),
2445
+ hit.shadingNormal.xyz
2446
+ );
2447
+ let guidedLight = sample_emissive_triangle_direction(hit, seed, randomDiffuse);
2448
+ let canSampleLight = dot(hit.shadingNormal.xyz, guidedLight) > -0.04;
2449
+ let guideProbability = select(0.38, 0.72, ray.bounce == 0u);
2450
+ let useGuidedLight = canSampleLight && random01(seed + 37u) < guideProbability;
2451
+ return ScatterResult(
2452
+ vec4<f32>(select(randomDiffuse, guidedLight, useGuidedLight), 0.0),
2453
+ select(0u, RAY_FLAG_GUIDED_EMISSIVE, useGuidedLight),
2454
+ 0u,
2455
+ 0u,
2456
+ 0u
2457
+ );
2458
+ }
2459
+
2460
+ @compute @workgroup_size(64)
2461
+ fn resolveSurfaceRecords(@builtin(global_invocation_id) globalId: vec3<u32>) {
2462
+ let index = globalId.x;
2463
+ let activeCount = atomicLoad(&counters.activeCount);
2464
+ if (index >= activeCount) {
2465
+ return;
2466
+ }
2467
+
2468
+ let ray = activeQueue[index];
2469
+ let hit = hits[index];
2470
+ var contribution = vec3<f32>(0.0);
2471
+
2472
+ if (hit.hitType == 1u) {
2473
+ let guidedLightWeight = select(1.0, 0.24, (ray.flags & RAY_FLAG_GUIDED_EMISSIVE) != 0u);
2474
+ contribution = clamp_sample_radiance(
2475
+ ray.throughput.xyz * max(hit.emission.xyz, hit.color.xyz) * guidedLightWeight
2476
+ );
2477
+ accumulation[ray.rayId] =
2478
+ accumulation[ray.rayId] + vec4<f32>(contribution * sample_weight(), 1.0);
2479
+ atomicAdd(&counters.terminatedCount, 1u);
2480
+ return;
2481
+ }
2482
+
2483
+ if (hit.hitType == 2u) {
2484
+ contribution = clamp_sample_radiance(ray.throughput.xyz * max(hit.color.xyz, config.ambientColor.xyz));
2485
+ accumulation[ray.rayId] =
2486
+ accumulation[ray.rayId] + vec4<f32>(contribution * sample_weight(), 1.0);
2487
+ atomicAdd(&counters.terminatedCount, 1u);
2488
+ return;
2489
+ }
2490
+
2491
+ if (ray.bounce + 1u >= config.maxDepth) {
2492
+ accumulation[ray.rayId] =
2493
+ accumulation[ray.rayId] +
2494
+ vec4<f32>(ray.throughput.xyz * config.ambientColor.xyz * sample_weight(), 1.0);
2495
+ atomicAdd(&counters.terminatedCount, 1u);
2496
+ return;
2497
+ }
2498
+
2499
+ let seed = mix_seed(ray.sourcePixelId, ray.sampleId, ray.bounce, config.frameIndex, 11u);
2500
+ let scatter = scatter_direction(ray, hit, seed);
2501
+ let nextIndex = atomicAdd(&counters.nextCount, 1u);
2502
+ if (nextIndex >= config.tilePixelCount) {
2503
+ return;
2504
+ }
2505
+ let color = clamp(hit.color.xyz, vec3<f32>(0.0), vec3<f32>(1.0));
2506
+ let opacity = clamp(hit.material.z, 0.0, 1.0);
2507
+ let materialEnergy = select(0.68, 0.92, hit.materialKind == 1u || hit.materialKind == 2u);
2508
+ let transparentEnergy = select(materialEnergy, 0.9, hit.hitType == 3u);
2509
+ let throughput = ray.throughput.xyz * mix(vec3<f32>(1.0), color, max(opacity, 0.18)) * transparentEnergy;
2510
+ nextQueue[nextIndex] = RayRecord(
2511
+ ray.rayId,
2512
+ ray.rayId,
2513
+ ray.sourcePixelId,
2514
+ ray.sampleId,
2515
+ ray.bounce + 1u,
2516
+ ray.mediumRefId,
2517
+ scatter.flags,
2518
+ 0u,
2519
+ vec4<f32>(offset_origin(hit.position.xyz, hit.shadingNormal.xyz), 1.0),
2520
+ scatter.direction,
2521
+ vec4<f32>(throughput, ray.throughput.w)
2522
+ );
2523
+ }
2524
+
2525
+ @compute @workgroup_size(1)
2526
+ fn compactAndSwapQueues(@builtin(global_invocation_id) globalId: vec3<u32>) {
2527
+ if (globalId.x > 0u) {
2528
+ return;
2529
+ }
2530
+ let nextCount = atomicLoad(&counters.nextCount);
2531
+ atomicStore(&counters.activeCount, min(nextCount, config.tilePixelCount));
2532
+ atomicStore(&counters.nextCount, 0u);
2533
+ }
2534
+
2535
+ @compute @workgroup_size(64)
2536
+ fn accumulateTerminalRadiance(@builtin(global_invocation_id) globalId: vec3<u32>) {
2537
+ let index = globalId.x;
2538
+ if (index >= config.tilePixelCount) {
2539
+ return;
2540
+ }
2541
+ let localX = index % config.tileWidth;
2542
+ let localY = index / config.tileWidth;
2543
+ let pixel = vec2<i32>(i32(config.tileX + localX), i32(config.tileY + localY));
2544
+ let radiance = max(accumulation[index].xyz, vec3<f32>(0.0));
2545
+
2546
+ textureStore(radianceImage, pixel, vec4<f32>(radiance, 1.0));
2547
+ if (config.denoise == 0u) {
2548
+ textureStore(outputImage, pixel, vec4<f32>(tone_map_radiance(radiance), 1.0));
2549
+ }
2550
+ }
2551
+
2552
+ @compute @workgroup_size(8, 8)
2553
+ fn denoiseLinearRadiance(@builtin(global_invocation_id) globalId: vec3<u32>) {
2554
+ let x = globalId.x;
2555
+ let y = globalId.y;
2556
+ if (x >= config.canvasWidth || y >= config.canvasHeight) {
2557
+ return;
2558
+ }
2559
+
2560
+ let pixel = vec2<i32>(i32(x), i32(y));
2561
+ let center = textureLoad(denoiseInputRadiance, pixel, 0).xyz;
2562
+ var sum = center * 1.4;
2563
+ var totalWeight = 1.4;
2564
+ let centerRange = denoise_range_space(center);
2565
+
2566
+ for (var oy = -2i; oy <= 2i; oy = oy + 1i) {
2567
+ for (var ox = -2i; ox <= 2i; ox = ox + 1i) {
2568
+ if (ox == 0i && oy == 0i) {
2569
+ continue;
2570
+ }
2571
+ let sx = clamp(i32(x) + ox, 0i, i32(config.canvasWidth) - 1i);
2572
+ let sy = clamp(i32(y) + oy, 0i, i32(config.canvasHeight) - 1i);
2573
+ let sampleColor = textureLoad(denoiseInputRadiance, vec2<i32>(sx, sy), 0).xyz;
2574
+ let colorDistance = length(denoise_range_space(sampleColor) - centerRange);
2575
+ let rangeWeight = 1.0 / (1.0 + colorDistance * 7.0);
2576
+ let distanceWeight = 1.0 / (1.0 + f32(ox * ox + oy * oy) * 0.24);
2577
+ let diagonalWeight = select(1.0, 0.78, abs(ox) + abs(oy) > 2i);
2578
+ let weight = rangeWeight * diagonalWeight * distanceWeight;
2579
+ sum = sum + sampleColor * weight;
2580
+ totalWeight = totalWeight + weight;
2581
+ }
2582
+ }
2583
+
2584
+ let filtered = sum / max(totalWeight, 0.0001);
2585
+ let outlier = saturate(length(denoise_range_space(center) - denoise_range_space(filtered)) * 2.4);
2586
+ let color = min(mix(center, filtered, 0.52 + outlier * 0.18), vec3<f32>(16.0));
2587
+ textureStore(denoisedRadianceImage, pixel, vec4<f32>(color, 1.0));
2588
+ }
2589
+
2590
+ @compute @workgroup_size(8, 8)
2591
+ fn resolveDenoisedOutputImage(@builtin(global_invocation_id) globalId: vec3<u32>) {
2592
+ let x = globalId.x;
2593
+ let y = globalId.y;
2594
+ if (x >= config.canvasWidth || y >= config.canvasHeight) {
2595
+ return;
2596
+ }
2597
+
2598
+ let pixel = vec2<i32>(i32(x), i32(y));
2599
+ let center = textureLoad(finalDenoiseInputRadiance, pixel, 0).xyz;
2600
+ var sum = center * 1.25;
2601
+ var totalWeight = 1.25;
2602
+ let centerRange = denoise_range_space(center);
2603
+
2604
+ for (var oy = -1i; oy <= 1i; oy = oy + 1i) {
2605
+ for (var ox = -1i; ox <= 1i; ox = ox + 1i) {
2606
+ if (ox == 0i && oy == 0i) {
2607
+ continue;
2608
+ }
2609
+ let sx = clamp(i32(x) + ox, 0i, i32(config.canvasWidth) - 1i);
2610
+ let sy = clamp(i32(y) + oy, 0i, i32(config.canvasHeight) - 1i);
2611
+ let sampleColor = textureLoad(finalDenoiseInputRadiance, vec2<i32>(sx, sy), 0).xyz;
2612
+ let colorDistance = length(denoise_range_space(sampleColor) - centerRange);
2613
+ let rangeWeight = 1.0 / (1.0 + colorDistance * 9.0);
2614
+ let distanceWeight = 1.0 / (1.0 + f32(ox * ox + oy * oy) * 0.4);
2615
+ let weight = rangeWeight * distanceWeight;
2616
+ sum = sum + sampleColor * weight;
2617
+ totalWeight = totalWeight + weight;
2618
+ }
2619
+ }
2620
+
2621
+ let filtered = sum / max(totalWeight, 0.0001);
2622
+ let outlier = saturate(length(denoise_range_space(center) - denoise_range_space(filtered)) * 2.8);
2623
+ let radiance = min(mix(center, filtered, 0.28 + outlier * 0.12), vec3<f32>(16.0));
2624
+ textureStore(denoisedOutputImage, pixel, vec4<f32>(tone_map_radiance(radiance), 1.0));
2625
+ }
2626
+ `;
2627
+
2628
+ const PRESENT_WGSL = `
2629
+ struct VertexOut {
2630
+ @builtin(position) position: vec4<f32>,
2631
+ @location(0) uv: vec2<f32>,
2632
+ };
2633
+
2634
+ @vertex
2635
+ fn vertexMain(@builtin(vertex_index) vertexIndex: u32) -> VertexOut {
2636
+ var positions = array<vec2<f32>, 3>(
2637
+ vec2<f32>(-1.0, -1.0),
2638
+ vec2<f32>(3.0, -1.0),
2639
+ vec2<f32>(-1.0, 3.0)
2640
+ );
2641
+ var uvs = array<vec2<f32>, 3>(
2642
+ vec2<f32>(0.0, 1.0),
2643
+ vec2<f32>(2.0, 1.0),
2644
+ vec2<f32>(0.0, -1.0)
2645
+ );
2646
+ var out: VertexOut;
2647
+ out.position = vec4<f32>(positions[vertexIndex], 0.0, 1.0);
2648
+ out.uv = uvs[vertexIndex];
2649
+ return out;
2650
+ }
2651
+
2652
+ @group(0) @binding(0) var renderTexture: texture_2d<f32>;
2653
+ @group(0) @binding(1) var renderSampler: sampler;
2654
+
2655
+ @fragment
2656
+ fn fragmentMain(in: VertexOut) -> @location(0) vec4<f32> {
2657
+ return textureSample(renderTexture, renderSampler, in.uv);
2658
+ }
2659
+ `;
2660
+
2661
+ export async function createWavefrontPathTracingComputeRenderer(options = {}) {
2662
+ assertAnalyticDisplayQualityPolicy(options);
2663
+ const constants = getGpuUsageConstants();
2664
+ const navigatorRef = options.navigator ?? globalThis.navigator;
2665
+ if (!supportsWavefrontPathTracingCompute({ navigator: navigatorRef })) {
2666
+ throw new Error("WebGPU wavefront path tracing requires navigator.gpu.");
2667
+ }
2668
+
2669
+ const canvas = resolveCanvas(options.canvas, options.document);
2670
+ const initialConfig = createWavefrontPathTracingComputeConfig({
2671
+ ...options,
2672
+ canvas,
2673
+ });
2674
+ const adapter = await navigatorRef.gpu.requestAdapter({
2675
+ powerPreference: options.powerPreference ?? "high-performance",
2676
+ });
2677
+ if (!adapter) {
2678
+ throw new Error("Unable to acquire a WebGPU adapter for wavefront path tracing.");
2679
+ }
2680
+
2681
+ const device = await adapter.requestDevice();
2682
+ const context = canvas.getContext("webgpu");
2683
+ if (!context || typeof context.configure !== "function") {
2684
+ throw new Error("Canvas WebGPU context does not support configure().");
2685
+ }
2686
+
2687
+ const format =
2688
+ options.format ??
2689
+ (typeof navigatorRef.gpu.getPreferredCanvasFormat === "function"
2690
+ ? navigatorRef.gpu.getPreferredCanvasFormat()
2691
+ : "bgra8unorm");
2692
+ let config = initialConfig;
2693
+ const safeTileSize = clampTileSizeForDevice(config, device);
2694
+ if (safeTileSize !== config.tileSize) {
2695
+ config = createWavefrontPathTracingComputeConfig({
2696
+ ...options,
2697
+ canvas,
2698
+ width: config.width,
2699
+ height: config.height,
2700
+ tileSize: safeTileSize,
2701
+ });
2702
+ }
2703
+ canvas.width = config.width;
2704
+ canvas.height = config.height;
2705
+ context.configure({
2706
+ device,
2707
+ format,
2708
+ alphaMode: options.alpha === true ? "premultiplied" : "opaque",
2709
+ });
2710
+
2711
+ const bufferUsage = constants.buffer.STORAGE | constants.buffer.COPY_DST;
2712
+ const rayQueueBytes = config.tilePixelCapacity * RAY_RECORD_BYTES;
2713
+ const hitBytes = config.tilePixelCapacity * HIT_RECORD_BYTES;
2714
+ const accumulationBytes = config.tilePixelCapacity * ACCUMULATION_RECORD_BYTES;
2715
+ const activeQueue = createBuffer(device, bufferUsage, rayQueueBytes, "plasius.wavefront.activeQueue");
2716
+ const nextQueue = createBuffer(device, bufferUsage, rayQueueBytes, "plasius.wavefront.nextQueue");
2717
+ const hitBuffer = createBuffer(device, bufferUsage, hitBytes, "plasius.wavefront.hitBuffer");
2718
+ const accumulationBuffer = createBuffer(
2719
+ device,
2720
+ bufferUsage,
2721
+ accumulationBytes,
2722
+ "plasius.wavefront.accumulation"
2723
+ );
2724
+ const sceneObjectBuffer = createBuffer(
2725
+ device,
2726
+ constants.buffer.STORAGE | constants.buffer.COPY_DST,
2727
+ config.sceneObjectCapacity * SCENE_OBJECT_RECORD_BYTES,
2728
+ "plasius.wavefront.sceneObjects"
2729
+ );
2730
+ const triangleBuffer = createBuffer(
2731
+ device,
2732
+ constants.buffer.STORAGE | constants.buffer.COPY_DST,
2733
+ Math.max(1, config.triangleCapacity) * TRIANGLE_RECORD_BYTES,
2734
+ "plasius.wavefront.triangles"
2735
+ );
2736
+ const bvhNodeBuffer = createBuffer(
2737
+ device,
2738
+ constants.buffer.STORAGE | constants.buffer.COPY_DST,
2739
+ Math.max(1, config.bvhNodeCapacity + config.emissiveTriangleCapacity) *
2740
+ BVH_NODE_RECORD_BYTES,
2741
+ "plasius.wavefront.bvhNodes"
2742
+ );
2743
+ const meshVertexBuffer = createBuffer(
2744
+ device,
2745
+ constants.buffer.STORAGE | constants.buffer.COPY_DST,
2746
+ Math.max(1, config.gpuMeshSource.vertices.count) * MESH_VERTEX_RECORD_BYTES,
2747
+ "plasius.wavefront.meshVertices"
2748
+ );
2749
+ const meshIndexBuffer = createBuffer(
2750
+ device,
2751
+ constants.buffer.STORAGE | constants.buffer.COPY_DST,
2752
+ Math.max(1, config.gpuMeshSource.indices.count) * 4,
2753
+ "plasius.wavefront.meshIndices"
2754
+ );
2755
+ const meshRangeBuffer = createBuffer(
2756
+ device,
2757
+ constants.buffer.STORAGE | constants.buffer.COPY_DST,
2758
+ Math.max(1, config.gpuMeshSource.meshes.count) * MESH_RANGE_RECORD_BYTES,
2759
+ "plasius.wavefront.meshRanges"
2760
+ );
2761
+ const bvhLeafRefBuffer = createBuffer(
2762
+ device,
2763
+ constants.buffer.STORAGE | constants.buffer.COPY_DST,
2764
+ Math.max(1, config.bvhLeafSortCapacity) * BVH_LEAF_REF_RECORD_BYTES,
2765
+ "plasius.wavefront.bvhLeafRefs"
2766
+ );
2767
+ const configBuffer = createBuffer(
2768
+ device,
2769
+ constants.buffer.UNIFORM | constants.buffer.COPY_DST,
2770
+ CONFIG_BUFFER_BYTES,
2771
+ "plasius.wavefront.frameConfig"
2772
+ );
2773
+ const uniformOffsetAlignment = Number(device?.limits?.minUniformBufferOffsetAlignment);
2774
+ const configBufferStride = alignTo(
2775
+ CONFIG_BUFFER_BYTES,
2776
+ Number.isFinite(uniformOffsetAlignment) && uniformOffsetAlignment > 0
2777
+ ? uniformOffsetAlignment
2778
+ : CONFIG_BUFFER_BYTES
2779
+ );
2780
+ const bvhBuildConfigSlots =
2781
+ 1 + config.bvhSortStages.length + config.bvhBuildLevels.length;
2782
+ const bvhBuildConfigBuffer = createBuffer(
2783
+ device,
2784
+ constants.buffer.UNIFORM | constants.buffer.COPY_DST,
2785
+ Math.max(1, bvhBuildConfigSlots) * configBufferStride,
2786
+ "plasius.wavefront.bvhBuildConfig"
2787
+ );
2788
+ const counterBuffer = createBuffer(
2789
+ device,
2790
+ constants.buffer.STORAGE | constants.buffer.COPY_DST,
2791
+ COUNTER_BUFFER_BYTES,
2792
+ "plasius.wavefront.counters"
2793
+ );
2794
+
2795
+ let packedScene = packWavefrontSceneObjects(config.sceneObjects, config.sceneObjectCapacity);
2796
+ device.queue.writeBuffer(sceneObjectBuffer, 0, packedScene.buffer);
2797
+ const packedTriangles = packWavefrontTriangles(
2798
+ config.meshAcceleration.triangles,
2799
+ Math.max(1, config.triangleCapacity)
2800
+ );
2801
+ const packedBvhNodes = packWavefrontBvhNodes(
2802
+ config.meshAcceleration.nodes,
2803
+ Math.max(1, config.bvhNodeCapacity + config.emissiveTriangleCapacity)
2804
+ );
2805
+ const packedBvhNodeUints = new Uint32Array(packedBvhNodes.buffer);
2806
+ config.emissiveTriangleIndices.indices.forEach((triangleIndex, index) => {
2807
+ const nodeOffset = (config.bvhNodeCapacity + index) * (BVH_NODE_RECORD_BYTES / 4);
2808
+ packedBvhNodeUints[nodeOffset + 8] = triangleIndex;
2809
+ });
2810
+ device.queue.writeBuffer(triangleBuffer, 0, packedTriangles.buffer);
2811
+ device.queue.writeBuffer(bvhNodeBuffer, 0, packedBvhNodes.buffer);
2812
+ device.queue.writeBuffer(meshVertexBuffer, 0, config.gpuMeshSource.vertices.buffer);
2813
+ device.queue.writeBuffer(meshIndexBuffer, 0, config.gpuMeshSource.indices.buffer);
2814
+ device.queue.writeBuffer(meshRangeBuffer, 0, config.gpuMeshSource.meshes.buffer);
2815
+
2816
+ const radianceTexture = device.createTexture({
2817
+ label: "plasius.wavefront.radiance",
2818
+ size: { width: config.width, height: config.height },
2819
+ format: "rgba16float",
2820
+ usage:
2821
+ constants.texture.STORAGE_BINDING |
2822
+ constants.texture.TEXTURE_BINDING,
2823
+ });
2824
+ const radianceView = radianceTexture.createView();
2825
+ const denoiseScratchTexture = device.createTexture({
2826
+ label: "plasius.wavefront.denoiseScratch",
2827
+ size: { width: config.width, height: config.height },
2828
+ format: "rgba16float",
2829
+ usage:
2830
+ constants.texture.STORAGE_BINDING |
2831
+ constants.texture.TEXTURE_BINDING,
2832
+ });
2833
+ const denoiseScratchView = denoiseScratchTexture.createView();
2834
+ const outputTexture = device.createTexture({
2835
+ label: "plasius.wavefront.output",
2836
+ size: { width: config.width, height: config.height },
2837
+ format: "rgba8unorm",
2838
+ usage:
2839
+ constants.texture.STORAGE_BINDING |
2840
+ constants.texture.TEXTURE_BINDING |
2841
+ constants.texture.COPY_SRC,
2842
+ });
2843
+ const outputView = outputTexture.createView();
2844
+ const sampler = device.createSampler({
2845
+ label: "plasius.wavefront.presentSampler",
2846
+ magFilter: "nearest",
2847
+ minFilter: "nearest",
2848
+ });
2849
+
2850
+ const traceBindGroupLayout = device.createBindGroupLayout({
2851
+ label: "plasius.wavefront.traceBindGroupLayout",
2852
+ entries: [
2853
+ { binding: 0, visibility: constants.shader.COMPUTE, buffer: { type: "storage" } },
2854
+ { binding: 1, visibility: constants.shader.COMPUTE, buffer: { type: "storage" } },
2855
+ { binding: 2, visibility: constants.shader.COMPUTE, buffer: { type: "storage" } },
2856
+ { binding: 3, visibility: constants.shader.COMPUTE, buffer: { type: "storage" } },
2857
+ { binding: 4, visibility: constants.shader.COMPUTE, buffer: { type: "read-only-storage" } },
2858
+ {
2859
+ binding: 5,
2860
+ visibility: constants.shader.COMPUTE,
2861
+ buffer: { type: "uniform", hasDynamicOffset: true, minBindingSize: CONFIG_BUFFER_BYTES },
2862
+ },
2863
+ { binding: 6, visibility: constants.shader.COMPUTE, buffer: { type: "storage" } },
2864
+ {
2865
+ binding: 7,
2866
+ visibility: constants.shader.COMPUTE,
2867
+ storageTexture: { access: "write-only", format: "rgba8unorm" },
2868
+ },
2869
+ { binding: 8, visibility: constants.shader.COMPUTE, buffer: { type: "storage" } },
2870
+ { binding: 9, visibility: constants.shader.COMPUTE, buffer: { type: "storage" } },
2871
+ {
2872
+ binding: 16,
2873
+ visibility: constants.shader.COMPUTE,
2874
+ storageTexture: { access: "write-only", format: "rgba16float" },
2875
+ },
2876
+ ],
2877
+ });
2878
+ const accelerationBindGroupLayout = device.createBindGroupLayout({
2879
+ label: "plasius.wavefront.accelerationBindGroupLayout",
2880
+ entries: [
2881
+ {
2882
+ binding: 5,
2883
+ visibility: constants.shader.COMPUTE,
2884
+ buffer: { type: "uniform", hasDynamicOffset: true, minBindingSize: CONFIG_BUFFER_BYTES },
2885
+ },
2886
+ { binding: 8, visibility: constants.shader.COMPUTE, buffer: { type: "storage" } },
2887
+ { binding: 9, visibility: constants.shader.COMPUTE, buffer: { type: "storage" } },
2888
+ { binding: 10, visibility: constants.shader.COMPUTE, buffer: { type: "read-only-storage" } },
2889
+ { binding: 11, visibility: constants.shader.COMPUTE, buffer: { type: "read-only-storage" } },
2890
+ { binding: 12, visibility: constants.shader.COMPUTE, buffer: { type: "read-only-storage" } },
2891
+ { binding: 13, visibility: constants.shader.COMPUTE, buffer: { type: "storage" } },
2892
+ ],
2893
+ });
2894
+ const denoiseRadianceBindGroupLayout = device.createBindGroupLayout({
2895
+ label: "plasius.wavefront.denoiseRadianceBindGroupLayout",
2896
+ entries: [
2897
+ {
2898
+ binding: 5,
2899
+ visibility: constants.shader.COMPUTE,
2900
+ buffer: { type: "uniform", hasDynamicOffset: true, minBindingSize: CONFIG_BUFFER_BYTES },
2901
+ },
2902
+ {
2903
+ binding: 14,
2904
+ visibility: constants.shader.COMPUTE,
2905
+ texture: { sampleType: "float" },
2906
+ },
2907
+ {
2908
+ binding: 15,
2909
+ visibility: constants.shader.COMPUTE,
2910
+ storageTexture: { access: "write-only", format: "rgba16float" },
2911
+ },
2912
+ ],
2913
+ });
2914
+ const denoiseResolveBindGroupLayout = device.createBindGroupLayout({
2915
+ label: "plasius.wavefront.denoiseResolveBindGroupLayout",
2916
+ entries: [
2917
+ {
2918
+ binding: 5,
2919
+ visibility: constants.shader.COMPUTE,
2920
+ buffer: { type: "uniform", hasDynamicOffset: true, minBindingSize: CONFIG_BUFFER_BYTES },
2921
+ },
2922
+ {
2923
+ binding: 17,
2924
+ visibility: constants.shader.COMPUTE,
2925
+ texture: { sampleType: "float" },
2926
+ },
2927
+ {
2928
+ binding: 18,
2929
+ visibility: constants.shader.COMPUTE,
2930
+ storageTexture: { access: "write-only", format: "rgba8unorm" },
2931
+ },
2932
+ ],
2933
+ });
2934
+ const tracePipelineLayout = device.createPipelineLayout({
2935
+ label: "plasius.wavefront.tracePipelineLayout",
2936
+ bindGroupLayouts: [traceBindGroupLayout],
2937
+ });
2938
+ const accelerationPipelineLayout = device.createPipelineLayout({
2939
+ label: "plasius.wavefront.accelerationPipelineLayout",
2940
+ bindGroupLayouts: [accelerationBindGroupLayout],
2941
+ });
2942
+ const denoiseRadiancePipelineLayout = device.createPipelineLayout({
2943
+ label: "plasius.wavefront.denoiseRadiancePipelineLayout",
2944
+ bindGroupLayouts: [denoiseRadianceBindGroupLayout],
2945
+ });
2946
+ const denoiseResolvePipelineLayout = device.createPipelineLayout({
2947
+ label: "plasius.wavefront.denoiseResolvePipelineLayout",
2948
+ bindGroupLayouts: [denoiseResolveBindGroupLayout],
2949
+ });
2950
+ const computeShader = device.createShaderModule({
2951
+ label: "plasius.wavefront.computeShader",
2952
+ code: WAVEFRONT_COMPUTE_WGSL,
2953
+ });
2954
+
2955
+ const pipelines = {
2956
+ prepareMeshTrianglesAndLeaves: await createComputePipeline(
2957
+ device,
2958
+ computeShader,
2959
+ accelerationPipelineLayout,
2960
+ "prepareMeshTrianglesAndLeaves",
2961
+ "plasius.wavefront.prepareMeshTrianglesAndLeaves"
2962
+ ),
2963
+ sortBvhLeafRefs: await createComputePipeline(
2964
+ device,
2965
+ computeShader,
2966
+ accelerationPipelineLayout,
2967
+ "sortBvhLeafRefs",
2968
+ "plasius.wavefront.sortBvhLeafRefs"
2969
+ ),
2970
+ writeSortedBvhLeaves: await createComputePipeline(
2971
+ device,
2972
+ computeShader,
2973
+ accelerationPipelineLayout,
2974
+ "writeSortedBvhLeaves",
2975
+ "plasius.wavefront.writeSortedBvhLeaves"
2976
+ ),
2977
+ buildBvhInternalLevel: await createComputePipeline(
2978
+ device,
2979
+ computeShader,
2980
+ accelerationPipelineLayout,
2981
+ "buildBvhInternalLevel",
2982
+ "plasius.wavefront.buildBvhInternalLevel"
2983
+ ),
2984
+ generatePrimaryRays: await createComputePipeline(
2985
+ device,
2986
+ computeShader,
2987
+ tracePipelineLayout,
2988
+ "generatePrimaryRays",
2989
+ "plasius.wavefront.generatePrimaryRays"
2990
+ ),
2991
+ intersectActiveQueue: await createComputePipeline(
2992
+ device,
2993
+ computeShader,
2994
+ tracePipelineLayout,
2995
+ "intersectActiveQueue",
2996
+ "plasius.wavefront.intersectActiveQueue"
2997
+ ),
2998
+ resolveSurfaceRecords: await createComputePipeline(
2999
+ device,
3000
+ computeShader,
3001
+ tracePipelineLayout,
3002
+ "resolveSurfaceRecords",
3003
+ "plasius.wavefront.resolveSurfaceRecords"
3004
+ ),
3005
+ compactAndSwapQueues: await createComputePipeline(
3006
+ device,
3007
+ computeShader,
3008
+ tracePipelineLayout,
3009
+ "compactAndSwapQueues",
3010
+ "plasius.wavefront.compactAndSwapQueues"
3011
+ ),
3012
+ accumulateTerminalRadiance: await createComputePipeline(
3013
+ device,
3014
+ computeShader,
3015
+ tracePipelineLayout,
3016
+ "accumulateTerminalRadiance",
3017
+ "plasius.wavefront.accumulateTerminalRadiance"
3018
+ ),
3019
+ denoiseLinearRadiance: await createComputePipeline(
3020
+ device,
3021
+ computeShader,
3022
+ denoiseRadiancePipelineLayout,
3023
+ "denoiseLinearRadiance",
3024
+ "plasius.wavefront.denoiseLinearRadiance"
3025
+ ),
3026
+ resolveDenoisedOutputImage: await createComputePipeline(
3027
+ device,
3028
+ computeShader,
3029
+ denoiseResolvePipelineLayout,
3030
+ "resolveDenoisedOutputImage",
3031
+ "plasius.wavefront.resolveDenoisedOutputImage"
3032
+ ),
3033
+ };
3034
+
3035
+ function createTraceBindGroup(activeBuffer, nextBuffer, label, frameConfigBuffer = configBuffer) {
3036
+ return device.createBindGroup({
3037
+ label,
3038
+ layout: traceBindGroupLayout,
3039
+ entries: [
3040
+ { binding: 0, resource: { buffer: activeBuffer } },
3041
+ { binding: 1, resource: { buffer: nextBuffer } },
3042
+ { binding: 2, resource: { buffer: hitBuffer } },
3043
+ { binding: 3, resource: { buffer: accumulationBuffer } },
3044
+ { binding: 4, resource: { buffer: sceneObjectBuffer } },
3045
+ { binding: 5, resource: { buffer: frameConfigBuffer, size: CONFIG_BUFFER_BYTES } },
3046
+ { binding: 6, resource: { buffer: counterBuffer } },
3047
+ { binding: 7, resource: outputView },
3048
+ { binding: 8, resource: { buffer: triangleBuffer } },
3049
+ { binding: 9, resource: { buffer: bvhNodeBuffer } },
3050
+ { binding: 16, resource: radianceView },
3051
+ ],
3052
+ });
3053
+ }
3054
+
3055
+ const bindGroups = [
3056
+ createTraceBindGroup(activeQueue, nextQueue, "plasius.wavefront.bind.activeNext"),
3057
+ createTraceBindGroup(nextQueue, activeQueue, "plasius.wavefront.bind.nextActive"),
3058
+ ];
3059
+ const bvhBuildBindGroup = device.createBindGroup({
3060
+ label: "plasius.wavefront.bind.bvhBuild",
3061
+ layout: accelerationBindGroupLayout,
3062
+ entries: [
3063
+ { binding: 5, resource: { buffer: bvhBuildConfigBuffer, size: CONFIG_BUFFER_BYTES } },
3064
+ { binding: 8, resource: { buffer: triangleBuffer } },
3065
+ { binding: 9, resource: { buffer: bvhNodeBuffer } },
3066
+ { binding: 10, resource: { buffer: meshVertexBuffer } },
3067
+ { binding: 11, resource: { buffer: meshIndexBuffer } },
3068
+ { binding: 12, resource: { buffer: meshRangeBuffer } },
3069
+ { binding: 13, resource: { buffer: bvhLeafRefBuffer } },
3070
+ ],
3071
+ });
3072
+ function createDenoiseRadianceBindGroup(inputView, targetView, label) {
3073
+ return device.createBindGroup({
3074
+ label,
3075
+ layout: denoiseRadianceBindGroupLayout,
3076
+ entries: [
3077
+ { binding: 5, resource: { buffer: configBuffer, size: CONFIG_BUFFER_BYTES } },
3078
+ { binding: 14, resource: inputView },
3079
+ { binding: 15, resource: targetView },
3080
+ ],
3081
+ });
3082
+ }
3083
+
3084
+ function createDenoiseResolveBindGroup(inputView, targetView, label) {
3085
+ return device.createBindGroup({
3086
+ label,
3087
+ layout: denoiseResolveBindGroupLayout,
3088
+ entries: [
3089
+ { binding: 5, resource: { buffer: configBuffer, size: CONFIG_BUFFER_BYTES } },
3090
+ { binding: 17, resource: inputView },
3091
+ { binding: 18, resource: targetView },
3092
+ ],
3093
+ });
3094
+ }
3095
+
3096
+ const denoiseRadianceBindGroup = createDenoiseRadianceBindGroup(
3097
+ radianceView,
3098
+ denoiseScratchView,
3099
+ "plasius.wavefront.bind.denoise.radianceToScratch"
3100
+ );
3101
+ const denoiseResolveBindGroup = createDenoiseResolveBindGroup(
3102
+ denoiseScratchView,
3103
+ outputView,
3104
+ "plasius.wavefront.bind.denoise.scratchToOutput"
3105
+ );
3106
+
3107
+ const presentBindGroupLayout = device.createBindGroupLayout({
3108
+ label: "plasius.wavefront.presentBindGroupLayout",
3109
+ entries: [
3110
+ { binding: 0, visibility: constants.shader.FRAGMENT, texture: { sampleType: "float" } },
3111
+ { binding: 1, visibility: constants.shader.FRAGMENT, sampler: { type: "filtering" } },
3112
+ ],
3113
+ });
3114
+ const presentShader = device.createShaderModule({
3115
+ label: "plasius.wavefront.presentShader",
3116
+ code: PRESENT_WGSL,
3117
+ });
3118
+ const presentPipeline = await createRenderPipeline(device, {
3119
+ label: "plasius.wavefront.presentPipeline",
3120
+ layout: device.createPipelineLayout({
3121
+ label: "plasius.wavefront.presentPipelineLayout",
3122
+ bindGroupLayouts: [presentBindGroupLayout],
3123
+ }),
3124
+ vertex: { module: presentShader, entryPoint: "vertexMain" },
3125
+ fragment: {
3126
+ module: presentShader,
3127
+ entryPoint: "fragmentMain",
3128
+ targets: [{ format }],
3129
+ },
3130
+ primitive: { topology: "triangle-list" },
3131
+ });
3132
+ const presentBindGroup = device.createBindGroup({
3133
+ label: "plasius.wavefront.presentBindGroup",
3134
+ layout: presentBindGroupLayout,
3135
+ entries: [
3136
+ { binding: 0, resource: outputView },
3137
+ { binding: 1, resource: sampler },
3138
+ ],
3139
+ });
3140
+
3141
+ let frame = 0;
3142
+ const tiles = createTiles(config.width, config.height, config.tileSize);
3143
+
3144
+ function dispatchGpuAccelerationBuild(frameIndex) {
3145
+ if (!config.gpuAccelerationBuildRequired) {
3146
+ return;
3147
+ }
3148
+ const buildTile = tiles[0] ?? { x: 0, y: 0, width: 1, height: 1 };
3149
+ const encoder = device.createCommandEncoder({
3150
+ label: `plasius.wavefront.buildAcceleration.${frameIndex}`,
3151
+ });
3152
+ device.queue.writeBuffer(
3153
+ bvhBuildConfigBuffer,
3154
+ 0,
3155
+ createConfigPayload(config, buildTile, frameIndex, {
3156
+ sortItemCount: config.bvhLeafSortCapacity,
3157
+ })
3158
+ );
3159
+ config.bvhSortStages.forEach((sortStage, stageIndex) => {
3160
+ device.queue.writeBuffer(
3161
+ bvhBuildConfigBuffer,
3162
+ (stageIndex + 1) * configBufferStride,
3163
+ createConfigPayload(config, buildTile, frameIndex, {
3164
+ start: sortStage.compareDistance,
3165
+ count: sortStage.sequenceSize,
3166
+ sortItemCount: config.bvhLeafSortCapacity,
3167
+ })
3168
+ );
3169
+ });
3170
+ const buildLevelConfigStart = 1 + config.bvhSortStages.length;
3171
+ config.bvhBuildLevels.forEach((buildLevel, levelIndex) => {
3172
+ device.queue.writeBuffer(
3173
+ bvhBuildConfigBuffer,
3174
+ (buildLevelConfigStart + levelIndex) * configBufferStride,
3175
+ createConfigPayload(config, buildTile, frameIndex, buildLevel)
3176
+ );
3177
+ });
3178
+ const passEncoder = encoder.beginComputePass({
3179
+ label: "plasius.wavefront.buildAccelerationPass",
3180
+ });
3181
+ passEncoder.setBindGroup(0, bvhBuildBindGroup, [0]);
3182
+ passEncoder.setPipeline(pipelines.prepareMeshTrianglesAndLeaves);
3183
+ passEncoder.dispatchWorkgroups(Math.ceil(config.bvhLeafSortCapacity / WORKGROUP_SIZE));
3184
+ passEncoder.setPipeline(pipelines.sortBvhLeafRefs);
3185
+ for (let stageIndex = 0; stageIndex < config.bvhSortStages.length; stageIndex += 1) {
3186
+ passEncoder.setBindGroup(0, bvhBuildBindGroup, [
3187
+ (stageIndex + 1) * configBufferStride,
3188
+ ]);
3189
+ passEncoder.dispatchWorkgroups(Math.ceil(config.bvhLeafSortCapacity / WORKGROUP_SIZE));
3190
+ }
3191
+ passEncoder.setBindGroup(0, bvhBuildBindGroup, [0]);
3192
+ passEncoder.setPipeline(pipelines.writeSortedBvhLeaves);
3193
+ passEncoder.dispatchWorkgroups(Math.ceil(config.triangleCount / WORKGROUP_SIZE));
3194
+ passEncoder.setPipeline(pipelines.buildBvhInternalLevel);
3195
+ for (let levelIndex = 0; levelIndex < config.bvhBuildLevels.length; levelIndex += 1) {
3196
+ const buildLevel = config.bvhBuildLevels[levelIndex];
3197
+ passEncoder.setBindGroup(0, bvhBuildBindGroup, [
3198
+ (buildLevelConfigStart + levelIndex) * configBufferStride,
3199
+ ]);
3200
+ passEncoder.dispatchWorkgroups(Math.ceil(buildLevel.count / WORKGROUP_SIZE));
3201
+ }
3202
+ passEncoder.end();
3203
+ device.queue.submit([encoder.finish()]);
3204
+ }
3205
+
3206
+ function dispatchTileSample(tile, frameIndex, sampleIndex) {
3207
+ const sampleWeight = 1 / config.samplesPerPixel;
3208
+ const configPayload = createConfigPayload(config, tile, frameIndex, {
3209
+ sampleIndex,
3210
+ sampleWeight,
3211
+ });
3212
+ device.queue.writeBuffer(configBuffer, 0, configPayload);
3213
+ const encoder = device.createCommandEncoder({
3214
+ label: `plasius.wavefront.frame.${frameIndex}.tile.${tile.x}.${tile.y}.sample.${sampleIndex}`,
3215
+ });
3216
+ const passEncoder = encoder.beginComputePass({
3217
+ label: "plasius.wavefront.computePass",
3218
+ });
3219
+ const tileWorkgroups = Math.ceil((tile.width * tile.height) / WORKGROUP_SIZE);
3220
+ const capacityWorkgroups = Math.ceil(config.tilePixelCapacity / WORKGROUP_SIZE);
3221
+
3222
+ passEncoder.setBindGroup(0, bindGroups[0], [0]);
3223
+ passEncoder.setPipeline(pipelines.generatePrimaryRays);
3224
+ passEncoder.dispatchWorkgroups(tileWorkgroups);
3225
+
3226
+ for (let bounceIndex = 0; bounceIndex < config.maxDepth; bounceIndex += 1) {
3227
+ passEncoder.setBindGroup(0, bindGroups[bounceIndex % 2], [0]);
3228
+ passEncoder.setPipeline(pipelines.intersectActiveQueue);
3229
+ passEncoder.dispatchWorkgroups(capacityWorkgroups);
3230
+ passEncoder.setPipeline(pipelines.resolveSurfaceRecords);
3231
+ passEncoder.dispatchWorkgroups(capacityWorkgroups);
3232
+ passEncoder.setPipeline(pipelines.compactAndSwapQueues);
3233
+ passEncoder.dispatchWorkgroups(1);
3234
+ }
3235
+
3236
+ passEncoder.end();
3237
+ device.queue.submit([encoder.finish()]);
3238
+ }
3239
+
3240
+ function dispatchTileOutput(tile, frameIndex) {
3241
+ const configPayload = createConfigPayload(config, tile, frameIndex, {
3242
+ sampleIndex: 0,
3243
+ sampleWeight: 1 / config.samplesPerPixel,
3244
+ });
3245
+ device.queue.writeBuffer(configBuffer, 0, configPayload);
3246
+ const encoder = device.createCommandEncoder({
3247
+ label: `plasius.wavefront.frame.${frameIndex}.tile.${tile.x}.${tile.y}.output`,
3248
+ });
3249
+ const passEncoder = encoder.beginComputePass({
3250
+ label: "plasius.wavefront.outputPass",
3251
+ });
3252
+ const tileWorkgroups = Math.ceil((tile.width * tile.height) / WORKGROUP_SIZE);
3253
+
3254
+ passEncoder.setBindGroup(0, bindGroups[0], [0]);
3255
+ passEncoder.setPipeline(pipelines.accumulateTerminalRadiance);
3256
+ passEncoder.dispatchWorkgroups(tileWorkgroups);
3257
+ passEncoder.end();
3258
+ device.queue.submit([encoder.finish()]);
3259
+ }
3260
+
3261
+ function dispatchTile(tile, frameIndex) {
3262
+ for (let sampleIndex = 0; sampleIndex < config.samplesPerPixel; sampleIndex += 1) {
3263
+ dispatchTileSample(tile, frameIndex, sampleIndex);
3264
+ }
3265
+ dispatchTileOutput(tile, frameIndex);
3266
+ }
3267
+
3268
+ function dispatchDenoise(frameIndex) {
3269
+ if (!config.denoise) {
3270
+ return;
3271
+ }
3272
+ device.queue.writeBuffer(
3273
+ configBuffer,
3274
+ 0,
3275
+ createConfigPayload(
3276
+ config,
3277
+ { x: 0, y: 0, width: config.width, height: config.height },
3278
+ frameIndex,
3279
+ { sampleIndex: 0, sampleWeight: 1 / config.samplesPerPixel }
3280
+ )
3281
+ );
3282
+ const encoder = device.createCommandEncoder({
3283
+ label: `plasius.wavefront.frame.${frameIndex}.denoise`,
3284
+ });
3285
+ const radiancePass = encoder.beginComputePass({
3286
+ label: "plasius.wavefront.denoiseRadiancePass",
3287
+ });
3288
+ radiancePass.setBindGroup(0, denoiseRadianceBindGroup, [0]);
3289
+ radiancePass.setPipeline(pipelines.denoiseLinearRadiance);
3290
+ radiancePass.dispatchWorkgroups(Math.ceil(config.width / 8), Math.ceil(config.height / 8));
3291
+ radiancePass.end();
3292
+
3293
+ const resolvePass = encoder.beginComputePass({
3294
+ label: "plasius.wavefront.denoiseResolvePass",
3295
+ });
3296
+ resolvePass.setBindGroup(0, denoiseResolveBindGroup, [0]);
3297
+ resolvePass.setPipeline(pipelines.resolveDenoisedOutputImage);
3298
+ resolvePass.dispatchWorkgroups(Math.ceil(config.width / 8), Math.ceil(config.height / 8));
3299
+ resolvePass.end();
3300
+ device.queue.submit([encoder.finish()]);
3301
+ }
3302
+
3303
+ function present() {
3304
+ const texture = context.getCurrentTexture();
3305
+ const encoder = device.createCommandEncoder({
3306
+ label: `plasius.wavefront.present.${frame}`,
3307
+ });
3308
+ const passEncoder = encoder.beginRenderPass({
3309
+ label: "plasius.wavefront.presentPass",
3310
+ colorAttachments: [
3311
+ {
3312
+ view: texture.createView(),
3313
+ clearValue: { r: 0, g: 0, b: 0, a: 1 },
3314
+ loadOp: "clear",
3315
+ storeOp: "store",
3316
+ },
3317
+ ],
3318
+ });
3319
+ passEncoder.setPipeline(presentPipeline);
3320
+ passEncoder.setBindGroup(0, presentBindGroup);
3321
+ passEncoder.draw(3);
3322
+ passEncoder.end();
3323
+ device.queue.submit([encoder.finish()]);
3324
+ }
3325
+
3326
+ function renderOnce() {
3327
+ frame += 1;
3328
+ dispatchGpuAccelerationBuild(frame + config.frameIndex);
3329
+ for (const tile of tiles) {
3330
+ dispatchTile(tile, frame + config.frameIndex);
3331
+ }
3332
+ dispatchDenoise(frame + config.frameIndex);
3333
+ present();
3334
+ return Object.freeze({
3335
+ frame,
3336
+ width: config.width,
3337
+ height: config.height,
3338
+ maxDepth: config.maxDepth,
3339
+ tiles: tiles.length,
3340
+ tileSize: config.tileSize,
3341
+ samplesPerPixel: config.samplesPerPixel,
3342
+ screenRays: config.width * config.height,
3343
+ primaryRays: config.width * config.height * config.samplesPerPixel,
3344
+ sceneObjectCount: config.sceneObjectCount,
3345
+ triangleCount: config.triangleCount,
3346
+ emissiveTriangleCount: config.emissiveTriangleCount,
3347
+ bvhNodeCount: config.bvhNodeCount,
3348
+ displayQuality: config.displayQuality,
3349
+ accelerationBuildMode: config.accelerationBuildMode,
3350
+ gpuAccelerationBuildRequired: config.gpuAccelerationBuildRequired,
3351
+ memory: config.memory,
3352
+ });
3353
+ }
3354
+
3355
+ async function readOutputProbe(optionsForProbe = {}) {
3356
+ const mapMode = constants.map;
3357
+ if (!mapMode) {
3358
+ throw new Error("GPUMapMode.READ is unavailable in this environment.");
3359
+ }
3360
+ const x = clamp(readNonNegativeInteger("x", optionsForProbe.x, Math.floor(config.width / 2)), 0, config.width - 1);
3361
+ const y = clamp(readNonNegativeInteger("y", optionsForProbe.y, Math.floor(config.height / 2)), 0, config.height - 1);
3362
+ const readback = device.createBuffer({
3363
+ label: "plasius.wavefront.outputProbe",
3364
+ size: 256,
3365
+ usage: constants.buffer.COPY_DST | constants.buffer.MAP_READ,
3366
+ });
3367
+ const encoder = device.createCommandEncoder({
3368
+ label: "plasius.wavefront.outputProbe.copy",
3369
+ });
3370
+ encoder.copyTextureToBuffer(
3371
+ { texture: outputTexture, origin: { x, y } },
3372
+ { buffer: readback, bytesPerRow: 256, rowsPerImage: 1 },
3373
+ { width: 1, height: 1, depthOrArrayLayers: 1 }
3374
+ );
3375
+ device.queue.submit([encoder.finish()]);
3376
+ await readback.mapAsync(mapMode.READ);
3377
+ const bytes = new Uint8Array(readback.getMappedRange()).slice(0, 4);
3378
+ readback.unmap();
3379
+ readback.destroy?.();
3380
+ return Object.freeze({
3381
+ x,
3382
+ y,
3383
+ rgba: Object.freeze(Array.from(bytes)),
3384
+ luminance: (0.2126 * bytes[0] + 0.7152 * bytes[1] + 0.0722 * bytes[2]) / 255,
3385
+ });
3386
+ }
3387
+
3388
+ function updateSceneObjects(sceneObjects) {
3389
+ const nextPackedScene = packWavefrontSceneObjects(sceneObjects, config.sceneObjectCapacity);
3390
+ packedScene = nextPackedScene;
3391
+ config = createWavefrontPathTracingComputeConfig({
3392
+ ...options,
3393
+ canvas,
3394
+ width: config.width,
3395
+ height: config.height,
3396
+ maxDepth: config.maxDepth,
3397
+ tileSize: config.tileSize,
3398
+ samplesPerPixel: config.samplesPerPixel,
3399
+ sceneObjectCapacity: config.sceneObjectCapacity,
3400
+ sceneObjects: packedScene.objects,
3401
+ frameIndex: config.frameIndex,
3402
+ });
3403
+ device.queue.writeBuffer(sceneObjectBuffer, 0, packedScene.buffer);
3404
+ return config;
3405
+ }
3406
+
3407
+ function getSnapshot() {
3408
+ return Object.freeze({
3409
+ frame,
3410
+ width: config.width,
3411
+ height: config.height,
3412
+ maxDepth: config.maxDepth,
3413
+ tiles: tiles.length,
3414
+ tileSize: config.tileSize,
3415
+ samplesPerPixel: config.samplesPerPixel,
3416
+ sceneObjectCount: config.sceneObjectCount,
3417
+ triangleCount: config.triangleCount,
3418
+ emissiveTriangleCount: config.emissiveTriangleCount,
3419
+ bvhNodeCount: config.bvhNodeCount,
3420
+ displayQuality: config.displayQuality,
3421
+ accelerationBuildMode: config.accelerationBuildMode,
3422
+ gpuAccelerationBuildRequired: config.gpuAccelerationBuildRequired,
3423
+ memory: config.memory,
3424
+ });
3425
+ }
3426
+
3427
+ function destroy() {
3428
+ activeQueue.destroy?.();
3429
+ nextQueue.destroy?.();
3430
+ hitBuffer.destroy?.();
3431
+ accumulationBuffer.destroy?.();
3432
+ sceneObjectBuffer.destroy?.();
3433
+ triangleBuffer.destroy?.();
3434
+ bvhNodeBuffer.destroy?.();
3435
+ meshVertexBuffer.destroy?.();
3436
+ meshIndexBuffer.destroy?.();
3437
+ meshRangeBuffer.destroy?.();
3438
+ bvhLeafRefBuffer.destroy?.();
3439
+ configBuffer.destroy?.();
3440
+ bvhBuildConfigBuffer.destroy?.();
3441
+ counterBuffer.destroy?.();
3442
+ radianceTexture.destroy?.();
3443
+ denoiseScratchTexture.destroy?.();
3444
+ outputTexture.destroy?.();
3445
+ context.unconfigure?.();
3446
+ }
3447
+
3448
+ return Object.freeze({
3449
+ canvas,
3450
+ context,
3451
+ device,
3452
+ format,
3453
+ config,
3454
+ renderOnce,
3455
+ readOutputProbe,
3456
+ updateSceneObjects,
3457
+ getSnapshot,
3458
+ destroy,
3459
+ });
3460
+ }