@tinypoly/gpu-renderer 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/ARCHITECTURE.md +81 -10
  2. package/README.md +53 -5
  3. package/dist/atmosphere.d.ts +21 -0
  4. package/dist/atmosphere.d.ts.map +1 -0
  5. package/dist/atmosphere.js +72 -0
  6. package/dist/atmosphere.js.map +1 -0
  7. package/dist/bvh.d.ts +11 -6
  8. package/dist/bvh.d.ts.map +1 -1
  9. package/dist/bvh.js +64 -11
  10. package/dist/bvh.js.map +1 -1
  11. package/dist/denoise.d.ts +17 -2
  12. package/dist/denoise.d.ts.map +1 -1
  13. package/dist/denoise.js +95 -27
  14. package/dist/denoise.js.map +1 -1
  15. package/dist/dispatchScheduler.d.ts +11 -4
  16. package/dist/dispatchScheduler.d.ts.map +1 -1
  17. package/dist/dispatchScheduler.js +14 -7
  18. package/dist/dispatchScheduler.js.map +1 -1
  19. package/dist/frameLayout.d.ts.map +1 -1
  20. package/dist/frameLayout.js +18 -0
  21. package/dist/frameLayout.js.map +1 -1
  22. package/dist/geometry.d.ts.map +1 -1
  23. package/dist/geometry.js +6 -3
  24. package/dist/geometry.js.map +1 -1
  25. package/dist/index.d.ts +4 -0
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +3 -0
  28. package/dist/index.js.map +1 -1
  29. package/dist/lensPass.d.ts +60 -0
  30. package/dist/lensPass.d.ts.map +1 -0
  31. package/dist/lensPass.js +235 -0
  32. package/dist/lensPass.js.map +1 -0
  33. package/dist/lightTree.d.ts +43 -0
  34. package/dist/lightTree.d.ts.map +1 -0
  35. package/dist/lightTree.js +107 -0
  36. package/dist/lightTree.js.map +1 -0
  37. package/dist/lights.d.ts +19 -1
  38. package/dist/lights.d.ts.map +1 -1
  39. package/dist/lights.js +63 -1
  40. package/dist/lights.js.map +1 -1
  41. package/dist/materialLayout.d.ts +24 -0
  42. package/dist/materialLayout.d.ts.map +1 -1
  43. package/dist/materialLayout.js +36 -1
  44. package/dist/materialLayout.js.map +1 -1
  45. package/dist/pathTracePass.d.ts +6 -3
  46. package/dist/pathTracePass.d.ts.map +1 -1
  47. package/dist/pathTracePass.js +16 -10
  48. package/dist/pathTracePass.js.map +1 -1
  49. package/dist/renderer.d.ts +19 -7
  50. package/dist/renderer.d.ts.map +1 -1
  51. package/dist/renderer.js +198 -36
  52. package/dist/renderer.js.map +1 -1
  53. package/dist/sceneUpload.d.ts +19 -1
  54. package/dist/sceneUpload.d.ts.map +1 -1
  55. package/dist/sceneUpload.js +25 -3
  56. package/dist/sceneUpload.js.map +1 -1
  57. package/dist/settings.d.ts +136 -0
  58. package/dist/settings.d.ts.map +1 -0
  59. package/dist/settings.js +100 -0
  60. package/dist/settings.js.map +1 -0
  61. package/dist/subsurface.d.ts +76 -0
  62. package/dist/subsurface.d.ts.map +1 -0
  63. package/dist/subsurface.js +70 -0
  64. package/dist/subsurface.js.map +1 -0
  65. package/dist/texturePack.d.ts +16 -4
  66. package/dist/texturePack.d.ts.map +1 -1
  67. package/dist/texturePack.js +61 -8
  68. package/dist/texturePack.js.map +1 -1
  69. package/dist/volumeContainment.d.ts +17 -0
  70. package/dist/volumeContainment.d.ts.map +1 -0
  71. package/dist/volumeContainment.js +61 -0
  72. package/dist/volumeContainment.js.map +1 -0
  73. package/dist/wgsl/bsdf.wgsl.d.ts +1 -1
  74. package/dist/wgsl/bsdf.wgsl.d.ts.map +1 -1
  75. package/dist/wgsl/bsdf.wgsl.js +35 -3
  76. package/dist/wgsl/bsdf.wgsl.js.map +1 -1
  77. package/dist/wgsl/bvh.wgsl.d.ts +8 -2
  78. package/dist/wgsl/bvh.wgsl.d.ts.map +1 -1
  79. package/dist/wgsl/bvh.wgsl.js +167 -96
  80. package/dist/wgsl/bvh.wgsl.js.map +1 -1
  81. package/dist/wgsl/common.wgsl.d.ts.map +1 -1
  82. package/dist/wgsl/common.wgsl.js +24 -7
  83. package/dist/wgsl/common.wgsl.js.map +1 -1
  84. package/dist/wgsl/lens.wgsl.d.ts +17 -0
  85. package/dist/wgsl/lens.wgsl.d.ts.map +1 -0
  86. package/dist/wgsl/lens.wgsl.js +315 -0
  87. package/dist/wgsl/lens.wgsl.js.map +1 -0
  88. package/dist/wgsl/lights.wgsl.d.ts +1 -1
  89. package/dist/wgsl/lights.wgsl.d.ts.map +1 -1
  90. package/dist/wgsl/lights.wgsl.js +206 -33
  91. package/dist/wgsl/lights.wgsl.js.map +1 -1
  92. package/dist/wgsl/pathTrace.wgsl.d.ts +1 -1
  93. package/dist/wgsl/pathTrace.wgsl.d.ts.map +1 -1
  94. package/dist/wgsl/pathTrace.wgsl.js +410 -25
  95. package/dist/wgsl/pathTrace.wgsl.js.map +1 -1
  96. package/dist/wgsl/random.wgsl.d.ts +1 -1
  97. package/dist/wgsl/random.wgsl.d.ts.map +1 -1
  98. package/dist/wgsl/random.wgsl.js +9 -17
  99. package/dist/wgsl/random.wgsl.js.map +1 -1
  100. package/dist/wgsl/sampling.wgsl.d.ts +1 -1
  101. package/dist/wgsl/sampling.wgsl.d.ts.map +1 -1
  102. package/dist/wgsl/sampling.wgsl.js +25 -0
  103. package/dist/wgsl/sampling.wgsl.js.map +1 -1
  104. package/dist/wgsl/surface.wgsl.d.ts +1 -1
  105. package/dist/wgsl/surface.wgsl.d.ts.map +1 -1
  106. package/dist/wgsl/surface.wgsl.js +47 -4
  107. package/dist/wgsl/surface.wgsl.js.map +1 -1
  108. package/dist/wgsl/texture.wgsl.d.ts +1 -1
  109. package/dist/wgsl/texture.wgsl.d.ts.map +1 -1
  110. package/dist/wgsl/texture.wgsl.js +10 -11
  111. package/dist/wgsl/texture.wgsl.js.map +1 -1
  112. package/package.json +1 -1
  113. package/src/atmosphere.ts +95 -0
  114. package/src/bvh.ts +81 -12
  115. package/src/denoise.ts +110 -27
  116. package/src/dispatchScheduler.ts +16 -7
  117. package/src/frameLayout.ts +18 -0
  118. package/src/geometry.ts +6 -2
  119. package/src/index.ts +13 -0
  120. package/src/lensPass.ts +299 -0
  121. package/src/lightTree.ts +172 -0
  122. package/src/lights.ts +90 -2
  123. package/src/materialLayout.ts +52 -1
  124. package/src/pathTracePass.ts +19 -10
  125. package/src/renderer.ts +258 -43
  126. package/src/sceneUpload.ts +41 -4
  127. package/src/settings.ts +234 -0
  128. package/src/subsurface.ts +153 -0
  129. package/src/texturePack.ts +80 -13
  130. package/src/volumeContainment.ts +81 -0
  131. package/src/wgsl/bsdf.wgsl.ts +35 -3
  132. package/src/wgsl/bvh.wgsl.ts +167 -96
  133. package/src/wgsl/common.wgsl.ts +24 -7
  134. package/src/wgsl/lens.wgsl.ts +314 -0
  135. package/src/wgsl/lights.wgsl.ts +206 -33
  136. package/src/wgsl/pathTrace.wgsl.ts +410 -25
  137. package/src/wgsl/random.wgsl.ts +9 -17
  138. package/src/wgsl/sampling.wgsl.ts +25 -0
  139. package/src/wgsl/surface.wgsl.ts +47 -4
  140. package/src/wgsl/texture.wgsl.ts +10 -11
@@ -175,6 +175,12 @@ fn coatTransmittance(surface: Surface, view: vec3<f32>) -> f32 {
175
175
  return 1.0 - surface.clearcoat * schlick3(vec3<f32>(0.04), 1.0, nv).x;
176
176
  }
177
177
 
178
+ /**
179
+ * A point inside a volume, where light scatters by the medium's phase function instead of off a surface. Only the
180
+ * integrator makes one: its normal faces back along the ray, its albedo is the medium's, its anisotropy the phase's.
181
+ */
182
+ const KIND_VOLUME: u32 = 100u;
183
+
178
184
  /** How often each lobe is chosen, as (diffuse, specular, clearcoat): by how much of the reflection each accounts for. */
179
185
  fn lobeWeights(surface: Surface, view: vec3<f32>) -> vec3<f32> {
180
186
  let nv = max(dot(surface.normal, view), 1e-4);
@@ -192,7 +198,8 @@ fn lobeWeights(surface: Surface, view: vec3<f32>) -> vec3<f32> {
192
198
 
193
199
  let base = coatTransmittance(surface, view);
194
200
  // The floors keep every lobe reachable: the diffuse one also carries the sheen and the multiple scattering term.
195
- let diffuse = (luminanceOf(surface.albedo * (1.0 - surface.metalness)) + max3(surface.sheenColor)) * base + 0.02;
201
+ let diffuse = (luminanceOf(surface.albedo * ((1.0 - surface.metalness) * (1.0 - surface.subsurface)))
202
+ + max3(surface.sheenColor)) * base + 0.02;
196
203
  let specular = luminanceOf(schlick3(surface.specularColor, surface.specularF90, nv)
197
204
  * (1.0 - surface.metalness) + surface.albedo * surface.metalness) * base + 0.05;
198
205
  let coat = select(0.0, (1.0 - base) + 0.02, surface.clearcoat > 0.0);
@@ -235,7 +242,8 @@ fn physicalValue(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> vec3<f3
235
242
  let average = f0 + (vec3<f32>(1.0) - f0) * 0.047619;
236
243
  let multiple = essV * essL * average / (vec3<f32>(1.0) - emsV * emsL * average + 1e-7) * (emsV * emsL);
237
244
 
238
- var diffuse = surface.albedo * (1.0 - surface.metalness) / PI;
245
+ // What scatters inside the material leaves it elsewhere, by the random walk, not from this point.
246
+ var diffuse = surface.albedo * ((1.0 - surface.metalness) * (1.0 - surface.subsurface)) / PI;
239
247
  var specular = single + multiple;
240
248
 
241
249
  // Three's sheen: its lobe sits on top and takes back the energy it reflects from the layers below.
@@ -344,6 +352,16 @@ fn evaluateBsdf(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> BsdfEval
344
352
  let nl = dot(normal, light);
345
353
  let nv = dot(normal, view);
346
354
 
355
+ // In a medium every direction is open: the phase function stands for the lobes, and samples itself exactly.
356
+ if (surface.kind == KIND_VOLUME) {
357
+ let phase = henyeyGreenstein(dot(-view, light), surface.anisotropy);
358
+
359
+ result.value = surface.albedo * phase;
360
+ result.pdf = phase;
361
+
362
+ return result;
363
+ }
364
+
347
365
  if (nv <= 0.0) {
348
366
  return result;
349
367
  }
@@ -507,6 +525,15 @@ fn sampleBsdf(surface: Surface, view: vec3<f32>, lobe: f32, u: vec2<f32>) -> Bsd
507
525
  result.pdf = 0.0;
508
526
  result.diffuse = true;
509
527
 
528
+ if (surface.kind == KIND_VOLUME) {
529
+ result.direction = sampleHenyeyGreenstein(-view, surface.anisotropy, u);
530
+ result.weight = surface.albedo;
531
+ result.pdf = henyeyGreenstein(dot(-view, result.direction), surface.anisotropy);
532
+ result.valid = true;
533
+
534
+ return result;
535
+ }
536
+
510
537
  if (dot(normal, view) <= 0.0) {
511
538
  return result;
512
539
  }
@@ -574,6 +601,11 @@ fn diffuseValue(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> vec3<f32
574
601
  return surface.albedo / PI * toonRamp(surface.gradientMap, nl);
575
602
  }
576
603
 
604
+ // Light scattered in a medium is all diffuse, as the denoiser sees it.
605
+ if (surface.kind == KIND_VOLUME) {
606
+ return surface.albedo * henyeyGreenstein(dot(-view, light), surface.anisotropy);
607
+ }
608
+
577
609
  if (nl <= 0.0) {
578
610
  return vec3<f32>(0.0);
579
611
  }
@@ -582,7 +614,7 @@ fn diffuseValue(surface: Surface, view: vec3<f32>, light: vec3<f32>) -> vec3<f32
582
614
  return surface.albedo / PI * nl;
583
615
  }
584
616
 
585
- var diffuse = surface.albedo * (1.0 - surface.metalness) / PI;
617
+ var diffuse = surface.albedo * ((1.0 - surface.metalness) * (1.0 - surface.subsurface)) / PI;
586
618
 
587
619
  if (any(surface.sheenColor > vec3<f32>(0.0))) {
588
620
  diffuse = diffuse * (1.0 - max3(surface.sheenColor)
@@ -1,17 +1,33 @@
1
- /** Triangle intersection and the iterative traversal, with the fixed stack the builder caps its depth to. */
1
+ /**
2
+ * Triangle intersection and the iterative traversal, with the fixed stack the builder caps its depth to.
3
+ *
4
+ * The positions are uploaded in the hierarchy's leaf order, so a leaf is a run of triangles read straight off the
5
+ * buffer; the scene's own triangle number is looked up once, for the hit a trace returns. Every other reader goes the
6
+ * other way, from a triangle number to where its corners sit.
7
+ */
2
8
  export const BVH_WGSL = /* wgsl */ `
3
- fn triangleVertex(triangle: u32, corner: u32) -> vec3<f32> {
4
- let base = triangle * 9u + corner * 3u;
9
+ /** A corner of the triangle at this position of the hierarchy's leaf order. */
10
+ fn leafVertex(at: u32, corner: u32) -> vec3<f32> {
11
+ let base = at * 9u + corner * 3u;
5
12
 
6
13
  return vec3<f32>(positions[base], positions[base + 1u], positions[base + 2u]);
7
14
  }
8
15
 
16
+ /** Where a triangle of the scene sits in the leaf order the positions are stored in. */
17
+ fn leafOf(triangle: u32) -> u32 {
18
+ return indices[frame.leafOffset + triangle];
19
+ }
20
+
21
+ fn triangleVertex(triangle: u32, corner: u32) -> vec3<f32> {
22
+ return leafVertex(leafOf(triangle), corner);
23
+ }
24
+
9
25
  /** Moller-Trumbore. Returns the distance in x and the barycentric coordinates in yz, or a negative distance. */
10
- fn intersectTriangle(triangle: u32, origin: vec3<f32>, direction: vec3<f32>) -> vec3<f32> {
26
+ fn intersectTriangle(at: u32, origin: vec3<f32>, direction: vec3<f32>) -> vec3<f32> {
11
27
  let miss = vec3<f32>(-1.0, 0.0, 0.0);
12
- let a = triangleVertex(triangle, 0u);
13
- let edge1 = triangleVertex(triangle, 1u) - a;
14
- let edge2 = triangleVertex(triangle, 2u) - a;
28
+ let a = leafVertex(at, 0u);
29
+ let edge1 = leafVertex(at, 1u) - a;
30
+ let edge2 = leafVertex(at, 2u) - a;
15
31
  let p = cross(direction, edge2);
16
32
  let determinant = dot(edge1, p);
17
33
 
@@ -37,10 +53,10 @@ fn intersectTriangle(triangle: u32, origin: vec3<f32>, direction: vec3<f32>) ->
37
53
  return vec3<f32>(dot(edge2, q) * inverse, u, v);
38
54
  }
39
55
 
40
- /** Where a ray enters a node's box, or 1e30 when it misses it or only meets it past maxDistance. */
41
- fn enterBox(node: BvhNode, origin: vec3<f32>, inverse: vec3<f32>, maxDistance: f32) -> f32 {
42
- let first = (node.low - origin) * inverse;
43
- let second = (node.high - origin) * inverse;
56
+ /** Where a ray enters a box, or 1e30 when it misses it or only meets it past maxDistance. */
57
+ fn enterBox(low: vec3<f32>, high: vec3<f32>, origin: vec3<f32>, inverse: vec3<f32>, maxDistance: f32) -> f32 {
58
+ let first = (low - origin) * inverse;
59
+ let second = (high - origin) * inverse;
44
60
  let near = min(first, second);
45
61
  let far = max(first, second);
46
62
  let enter = max(max(near.x, near.y), max(near.z, 0.0));
@@ -49,27 +65,67 @@ fn enterBox(node: BvhNode, origin: vec3<f32>, inverse: vec3<f32>, maxDistance: f
49
65
  return select(1e30, enter, enter <= exit);
50
66
  }
51
67
 
52
- /** The triangles of a leaf, against the closest hit so far. */
53
- fn intersectLeaf(node: BvhNode, origin: vec3<f32>, direction: vec3<f32>, hit: ptr<function, Hit>) {
54
- let start = u32(-1 - node.link);
68
+ /** The triangles of a leaf, against the closest hit so far, which records where in the leaf order it found one. */
69
+ fn intersectLeaf(link: i32, count: i32, origin: vec3<f32>, direction: vec3<f32>, hit: ptr<function, Hit>) {
70
+ let start = u32(-1 - link);
55
71
 
56
- for (var i = 0u; i < u32(node.count); i = i + 1u) {
57
- let triangle = orderedTriangle(start + i);
58
- let result = intersectTriangle(triangle, origin, direction);
72
+ for (var i = 0u; i < u32(count); i = i + 1u) {
73
+ let result = intersectTriangle(start + i, origin, direction);
59
74
 
60
75
  if (result.x > RAY_EPSILON && result.x < (*hit).distance) {
61
76
  (*hit).distance = result.x;
62
- (*hit).triangle = triangle;
77
+ (*hit).triangle = start + i;
63
78
  (*hit).u = result.y;
64
79
  (*hit).v = result.z;
65
80
  }
66
81
  }
67
82
  }
68
83
 
84
+ /** Whether any triangle of a leaf lies along the ray before maxDistance: its position in the leaf order, or NO_HIT. */
85
+ fn anyInLeaf(link: i32, count: i32, origin: vec3<f32>, direction: vec3<f32>, maxDistance: f32) -> u32 {
86
+ let start = u32(-1 - link);
87
+
88
+ for (var i = 0u; i < u32(count); i = i + 1u) {
89
+ let result = intersectTriangle(start + i, origin, direction);
90
+
91
+ if (result.x > RAY_EPSILON && result.x < maxDistance) {
92
+ return start + i;
93
+ }
94
+ }
95
+
96
+ return NO_HIT;
97
+ }
98
+
99
+ /** No node to go to: the traversal takes the next entry off the stack. */
100
+ const NO_NODE: u32 = 0xffffffffu;
101
+
102
+ /**
103
+ * A stack entry that is a leaf rather than a node: the node holding it and which of its two children it is. A leaf
104
+ * has no node of its own, so this is how one waits its turn.
105
+ */
106
+ const LEAF_ENTRY: u32 = 0x80000000u;
107
+
108
+ fn leafEntry(parent: u32, right: bool) -> u32 {
109
+ return LEAF_ENTRY | (parent << 1u) | select(0u, 1u, right);
110
+ }
111
+
112
+ /** A waiting leaf's link and count, read back from the node that holds it. */
113
+ fn waitingLeaf(entry: u32) -> vec2<i32> {
114
+ let parent = (entry & ~LEAF_ENTRY) >> 1u;
115
+
116
+ if ((entry & 1u) != 0u) {
117
+ return vec2<i32>(nodes[parent].rightLink, nodes[parent].rightCount);
118
+ }
119
+
120
+ return vec2<i32>(nodes[parent].leftLink, nodes[parent].leftCount);
121
+ }
122
+
69
123
  /**
70
- * The closest hit along a ray. Both children of a node are tested together and the nearer one is visited first, so
71
- * the closest hit shrinks the search early and whole subtrees behind it are skipped; each node remembers where the ray
72
- * entered it, and one whose entry is already past the closest hit is dropped without being opened.
124
+ * The closest hit along a ray. A node holds both of its children's boxes, so one read tests them both. The nearer is
125
+ * visited first, so the closest hit shrinks the search early and whole subtrees behind it are skipped; the farther one
126
+ * waits on the stack with where the ray entered it, and is dropped unopened if something closer turned up meanwhile.
127
+ * A leaf is tested at the top of the next step, the one place the loop tests triangles, which keeps the kernel small.
128
+ * This visits what the binary tree's traversal visited, in the same order, so it finds the same hit.
73
129
  */
74
130
  fn traceScene(origin: vec3<f32>, direction: vec3<f32>, maxDistance: f32) -> Hit {
75
131
  var hit: Hit;
@@ -85,74 +141,81 @@ fn traceScene(origin: vec3<f32>, direction: vec3<f32>, maxDistance: f32) -> Hit
85
141
  // A huge finite reciprocal instead of an infinite one: an axis aligned slab would otherwise produce 0 * inf.
86
142
  let signs = select(vec3<f32>(-1.0), vec3<f32>(1.0), direction >= vec3<f32>(0.0));
87
143
  let inverse = signs / max(abs(direction), vec3<f32>(1e-20));
88
- let rootEnter = enterBox(nodes[0], origin, inverse, hit.distance);
89
-
90
- if (rootEnter >= 1e30) {
91
- return hit;
92
- }
93
144
 
94
145
  var stack: array<u32, 32>;
95
146
  var enters: array<f32, 32>;
96
- var depth = 1u;
97
- stack[0] = 0u;
98
- enters[0] = rootEnter;
147
+ var depth = 0u;
148
+ var current = 0u;
149
+ // A leaf to test before anything else: its link, and its triangle count, zero when there is none.
150
+ var leaf = vec2<i32>(0, 0);
99
151
 
100
152
  loop {
101
- if (depth == 0u) {
102
- break;
153
+ if (leaf.y > 0) {
154
+ intersectLeaf(leaf.x, leaf.y, origin, direction, &hit);
155
+ leaf.y = 0;
103
156
  }
104
157
 
105
- depth = depth - 1u;
158
+ if (current == NO_NODE) {
159
+ if (depth == 0u) {
160
+ break;
161
+ }
106
162
 
107
- // Something closer was found since this node was pushed.
108
- if (enters[depth] > hit.distance) {
109
- continue;
110
- }
163
+ depth = depth - 1u;
111
164
 
112
- let node = nodes[stack[depth]];
165
+ // Something closer was found since this was pushed.
166
+ if (enters[depth] > hit.distance) {
167
+ continue;
168
+ }
113
169
 
114
- if (node.link < 0) {
115
- intersectLeaf(node, origin, direction, &hit);
116
- continue;
117
- }
170
+ let entry = stack[depth];
118
171
 
119
- let leftIndex = u32(node.link);
120
- let rightIndex = u32(node.count);
121
- let leftEnter = enterBox(nodes[leftIndex], origin, inverse, hit.distance);
122
- let rightEnter = enterBox(nodes[rightIndex], origin, inverse, hit.distance);
123
- let leftHit = leftEnter < 1e30;
124
- let rightHit = rightEnter < 1e30;
172
+ if ((entry & LEAF_ENTRY) != 0u) {
173
+ leaf = waitingLeaf(entry);
174
+ continue;
175
+ }
125
176
 
126
- // The builder caps its depth so both children always fit; the guard keeps a malformed tree inside the stack.
127
- if (depth + 2u > 32u) {
128
- continue;
177
+ current = entry;
129
178
  }
130
179
 
131
- if (leftHit && rightHit) {
132
- // The farther first, so the nearer comes off the stack next.
133
- let leftNearer = leftEnter <= rightEnter;
134
- stack[depth] = select(leftIndex, rightIndex, leftNearer);
135
- enters[depth] = select(leftEnter, rightEnter, leftNearer);
136
- stack[depth + 1u] = select(rightIndex, leftIndex, leftNearer);
137
- enters[depth + 1u] = select(rightEnter, leftEnter, leftNearer);
138
- depth = depth + 2u;
139
- } else if (leftHit) {
140
- stack[depth] = leftIndex;
141
- enters[depth] = leftEnter;
142
- depth = depth + 1u;
143
- } else if (rightHit) {
144
- stack[depth] = rightIndex;
145
- enters[depth] = rightEnter;
180
+ let node = nodes[current];
181
+ let leftEnter = enterBox(node.leftLow, node.leftHigh, origin, inverse, hit.distance);
182
+ let rightEnter = enterBox(node.rightLow, node.rightHigh, origin, inverse, hit.distance);
183
+ let leftFirst = leftEnter <= rightEnter;
184
+ let nearLink = select(node.rightLink, node.leftLink, leftFirst);
185
+ let farEnter = select(leftEnter, rightEnter, leftFirst);
186
+ let farLink = select(node.leftLink, node.rightLink, leftFirst);
187
+ let parent = current;
188
+
189
+ current = NO_NODE;
190
+
191
+ // The nearer child: a miss means both missed.
192
+ if (min(leftEnter, rightEnter) < 1e30) {
193
+ if (nearLink < 0) {
194
+ leaf = vec2<i32>(nearLink, select(node.rightCount, node.leftCount, leftFirst));
195
+ } else {
196
+ current = u32(nearLink);
197
+ }
198
+ }
199
+
200
+ // The builder caps its depth so the far children always fit; the guard keeps a malformed tree inside the stack.
201
+ if (farEnter < 1e30 && depth < 32u) {
202
+ stack[depth] = select(u32(farLink), leafEntry(parent, leftFirst), farLink < 0);
203
+ enters[depth] = farEnter;
146
204
  depth = depth + 1u;
147
205
  }
148
206
  }
149
207
 
208
+ if (hit.triangle != NO_HIT) {
209
+ hit.triangle = orderedTriangle(hit.triangle);
210
+ }
211
+
150
212
  return hit;
151
213
  }
152
214
 
153
215
  /**
154
216
  * Whether anything at all lies along a ray before maxDistance, stopping at the first triangle it finds rather than the
155
- * closest: what a shadow ray asks when every surface it could meet blocks it. Returns that triangle, or NO_HIT.
217
+ * closest: what a shadow ray asks when every surface it could meet blocks it. Returns that triangle, or NO_HIT. The
218
+ * right child goes first and the left one waits, the order the binary tree's traversal took.
156
219
  */
157
220
  fn traceAny(origin: vec3<f32>, direction: vec3<f32>, maxDistance: f32) -> u32 {
158
221
  if (frame.triangleCount == 0u) {
@@ -162,52 +225,60 @@ fn traceAny(origin: vec3<f32>, direction: vec3<f32>, maxDistance: f32) -> u32 {
162
225
  let signs = select(vec3<f32>(-1.0), vec3<f32>(1.0), direction >= vec3<f32>(0.0));
163
226
  let inverse = signs / max(abs(direction), vec3<f32>(1e-20));
164
227
 
165
- if (enterBox(nodes[0], origin, inverse, maxDistance) >= 1e30) {
166
- return NO_HIT;
167
- }
168
-
169
228
  var stack: array<u32, 32>;
170
- var depth = 1u;
171
- stack[0] = 0u;
229
+ var depth = 0u;
230
+ var current = 0u;
231
+ var leaf = vec2<i32>(0, 0);
172
232
 
173
233
  loop {
174
- if (depth == 0u) {
175
- break;
176
- }
234
+ if (leaf.y > 0) {
235
+ let found = anyInLeaf(leaf.x, leaf.y, origin, direction, maxDistance);
177
236
 
178
- depth = depth - 1u;
237
+ if (found != NO_HIT) {
238
+ return orderedTriangle(found);
239
+ }
179
240
 
180
- let node = nodes[stack[depth]];
241
+ leaf.y = 0;
242
+ }
181
243
 
182
- if (node.link < 0) {
183
- let start = u32(-1 - node.link);
244
+ if (current == NO_NODE) {
245
+ if (depth == 0u) {
246
+ break;
247
+ }
248
+
249
+ depth = depth - 1u;
184
250
 
185
- for (var i = 0u; i < u32(node.count); i = i + 1u) {
186
- let triangle = orderedTriangle(start + i);
187
- let result = intersectTriangle(triangle, origin, direction);
251
+ let entry = stack[depth];
188
252
 
189
- if (result.x > RAY_EPSILON && result.x < maxDistance) {
190
- return triangle;
191
- }
253
+ if ((entry & LEAF_ENTRY) != 0u) {
254
+ leaf = waitingLeaf(entry);
255
+ continue;
192
256
  }
193
257
 
194
- continue;
258
+ current = entry;
195
259
  }
196
260
 
197
- let leftIndex = u32(node.link);
198
- let rightIndex = u32(node.count);
261
+ let node = nodes[current];
262
+ let rightHit = enterBox(node.rightLow, node.rightHigh, origin, inverse, maxDistance) < 1e30;
263
+ let leftHit = enterBox(node.leftLow, node.leftHigh, origin, inverse, maxDistance) < 1e30;
264
+ let parent = current;
199
265
 
200
- if (depth + 2u > 32u) {
201
- continue;
202
- }
266
+ current = NO_NODE;
203
267
 
204
- if (enterBox(nodes[leftIndex], origin, inverse, maxDistance) < 1e30) {
205
- stack[depth] = leftIndex;
206
- depth = depth + 1u;
268
+ // The right child is taken now and the left one waits; with only one of them hit, that one is taken now.
269
+ let takeRight = rightHit;
270
+ let takenLink = select(node.leftLink, node.rightLink, takeRight);
271
+
272
+ if (rightHit || leftHit) {
273
+ if (takenLink < 0) {
274
+ leaf = vec2<i32>(takenLink, select(node.leftCount, node.rightCount, takeRight));
275
+ } else {
276
+ current = u32(takenLink);
277
+ }
207
278
  }
208
279
 
209
- if (enterBox(nodes[rightIndex], origin, inverse, maxDistance) < 1e30) {
210
- stack[depth] = rightIndex;
280
+ if (rightHit && leftHit && depth < 32u) {
281
+ stack[depth] = select(u32(node.leftLink), leafEntry(parent, false), node.leftLink < 0);
211
282
  depth = depth + 1u;
212
283
  }
213
284
  }
@@ -1,15 +1,21 @@
1
1
  import { FRAME_LAYOUT } from "../frameLayout.js";
2
2
  import { MATERIAL_KIND, MATERIAL_LAYOUT } from "../materialLayout.js";
3
+ import { SOBOL_TABLE, SRGB_TABLE, TEXTURE_TABLE } from "../texturePack.js";
3
4
 
4
5
  /** Bindings and structs shared by every stage of the path tracing module. */
5
6
  export const COMMON_WGSL = /* wgsl */ `
6
7
  ${FRAME_LAYOUT.wgsl}
7
- // Thirty two bytes: a corner and a link, twice. A leaf stores -1 - start in place of its left child.
8
+ // Sixty four bytes: the boxes of a node's two children, each a corner and a link, then a corner and a count. A link of
9
+ // zero or more is the child's own node; a leaf stores -1 - start in it, into the hierarchy's leaf order. See bvh.ts.
8
10
  struct BvhNode {
9
- low: vec3<f32>,
10
- link: i32,
11
- high: vec3<f32>,
12
- count: i32,
11
+ leftLow: vec3<f32>,
12
+ leftLink: i32,
13
+ leftHigh: vec3<f32>,
14
+ leftCount: i32,
15
+ rightLow: vec3<f32>,
16
+ rightLink: i32,
17
+ rightHigh: vec3<f32>,
18
+ rightCount: i32,
13
19
  };
14
20
 
15
21
  /** As materialLayout.ts packs it. A map index of NO_TEXTURE means the slot is empty. */
@@ -93,6 +99,10 @@ const MATERIAL_OBJECT_NORMAL_MAP: u32 = 256u;
93
99
  const MATERIAL_PATCHED: u32 = 512u;
94
100
  const MATERIAL_ALPHA_HASH: u32 = 2048u;
95
101
  const MATERIAL_SPECULAR: u32 = 4096u;
102
+ // The mesh only bounds a medium: its surface is invisible and the space inside scatters light.
103
+ const MATERIAL_VOLUME: u32 = 8192u;
104
+ // An unlit material of a fixed colour, which direct sampling aims at: it emits what it shows.
105
+ const MATERIAL_UNLIT_LIGHT: u32 = 16384u;
96
106
 
97
107
  const KIND_STANDARD: u32 = ${MATERIAL_KIND.standard}u;
98
108
  const KIND_PHYSICAL: u32 = ${MATERIAL_KIND.physical}u;
@@ -119,14 +129,21 @@ const BLEND_MULTIPLY: u32 = 4u;
119
129
  const NO_TEXTURE: u32 = 0xffffffffu;
120
130
  const NO_PROGRAM: u32 = 0xffffffffu;
121
131
 
132
+ // The lookup tables that lead the texture buffer, and where its table of textures starts after them: see texturePack.ts.
133
+ const SOBOL_TABLE: u32 = ${SOBOL_TABLE}u;
134
+ const SRGB_TABLE: u32 = ${SRGB_TABLE}u;
135
+ const TEXTURE_TABLE: u32 = ${TEXTURE_TABLE}u;
136
+
122
137
  // A WebGPU device only has to offer eight storage buffers per stage, so arrays of the same kind share one binding and
123
138
  // the uniform says where the second one starts. See sceneUpload.ts, which joins them.
124
139
  @group(0) @binding(0) var<uniform> frame: Frame;
140
+ // Nine coordinates per triangle, in the hierarchy's leaf order: see leafOf in bvh.wgsl.ts.
125
141
  @group(0) @binding(1) var<storage, read> positions: array<f32>;
126
142
  // Normals, then the UV sets from frame.uvOffset, vertex colours from frame.colorOffset and shader varyings from
127
143
  // frame.varyingOffset.
128
144
  @group(0) @binding(2) var<storage, read> attributes: array<f32>;
129
- // One material index per triangle, then the hierarchy's triangle order from frame.orderOffset on.
145
+ // One material index per triangle, then the hierarchy's triangle order from frame.orderOffset on (the triangle at each
146
+ // position of the leaf order), then from frame.leafOffset on the other way (the position of each triangle).
130
147
  @group(0) @binding(3) var<storage, read> indices: array<u32>;
131
148
  @group(0) @binding(4) var<storage, read> materials: array<Material>;
132
149
  @group(0) @binding(5) var<storage, read> nodes: array<BvhNode>;
@@ -174,7 +191,7 @@ fn extraVec4(at: u32) -> vec4<f32> {
174
191
 
175
192
  /** One entry of the texture table, unpacked from the words it shares with the texels. */
176
193
  fn textureInfo(index: u32) -> TextureInfo {
177
- let at = index * 16u;
194
+ let at = TEXTURE_TABLE + index * 16u;
178
195
  var info: TextureInfo;
179
196
 
180
197
  info.size = vec2<u32>(textures[at], textures[at + 1u]);