@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
@@ -0,0 +1,314 @@
1
+ /**
2
+ * What the lens and the sensor do to the light before it is exposed: a halo around bright light (bloom), colour
3
+ * fringes towards the corners (lateral chromatic aberration) and the camera's white balance. Everything here is linear
4
+ * light, between the traced (or denoised) radiance and the presentation pass, which exposes and tone maps the result.
5
+ *
6
+ * The halo is built over a pyramid of half sized copies of the image, as Jimenez's "Next generation post processing in
7
+ * Call of Duty" (2014) builds it: each level is a 13 tap filter of the one above, and the levels are folded back from
8
+ * the smallest up with a tent filter, each mixed with the one below by the radius. The result is a wide, smooth spread
9
+ * whose weights sum to one, so a flat image is left as it was and the halo takes light from where it is rather than
10
+ * adding any: the image keeps its exact mean.
11
+ *
12
+ * The rays a bright light throws through the aperture are gathered the same way, from a quarter sized level: each
13
+ * texel takes the light of the texels behind it along every ray's direction, fading with the distance, with weights
14
+ * that again sum to one.
15
+ */
16
+ export const LENS_WGSL = /* wgsl */ `
17
+ struct Lens {
18
+ resolution: vec2<u32>,
19
+ // Whether the colour comes from the denoiser's output rather than straight from the accumulation.
20
+ denoised: u32,
21
+ // Upsampling only: the level read from sits in the downsampled pyramid (the smallest one) instead of the folded one.
22
+ fromDown: u32,
23
+ sourceSize: vec2<u32>,
24
+ // Where the level read from starts in its pyramid; a size of the full resolution reads the image itself.
25
+ sourceOffset: u32,
26
+ // The first downsample reads the image itself, and averages its taps by luminance against fireflies.
27
+ fromImage: u32,
28
+ targetSize: vec2<u32>,
29
+ targetOffset: u32,
30
+ // Where the largest level of the folded pyramid starts, which the composite reads the halo from.
31
+ haloOffset: u32,
32
+ haloSize: vec2<u32>,
33
+ // How much of the light the halo takes, and how much each level takes from the one below it.
34
+ intensity: f32,
35
+ radius: f32,
36
+ // How far red and blue move apart at the corners, as a share of the distance from the centre.
37
+ aberration: f32,
38
+ // How much of the light the aperture's rays take, how many there are, and how far they reach in streak texels.
39
+ glare: f32,
40
+ rays: u32,
41
+ glareLength: f32,
42
+ // The white balance, a linear sRGB matrix, row by row.
43
+ white0: vec4<f32>,
44
+ white1: vec4<f32>,
45
+ white2: vec4<f32>,
46
+ // The size of the level the rays are gathered at, and the angle of the first ray, in radians.
47
+ streakSize: vec2<u32>,
48
+ glareAngle: f32,
49
+ pad0: f32,
50
+ };
51
+
52
+ const PI: f32 = 3.14159265358979;
53
+
54
+ /** Taps along each of the aperture's rays. */
55
+ const STREAK_TAPS: i32 = 24;
56
+
57
+ @group(0) @binding(0) var<uniform> lens: Lens;
58
+ @group(0) @binding(1) var<storage, read> accumulation: array<vec4<f32>>;
59
+ @group(0) @binding(2) var<storage, read> denoised: array<vec4<f32>>;
60
+ @group(0) @binding(3) var<storage, read_write> down: array<vec4<f32>>;
61
+ @group(0) @binding(4) var<storage, read_write> up: array<vec4<f32>>;
62
+ @group(0) @binding(5) var<storage, read_write> graded: array<vec4<f32>>;
63
+ @group(0) @binding(6) var<storage, read_write> streaked: array<vec4<f32>>;
64
+
65
+ /** The image's own texel: its mean so far, or nothing where no path has landed yet. */
66
+ fn imageTexel(x: i32, y: i32) -> vec3<f32> {
67
+ let size = vec2<i32>(lens.resolution);
68
+ let index = u32(clamp(y, 0, size.y - 1) * size.x + clamp(x, 0, size.x - 1));
69
+
70
+ if (lens.denoised != 0u) {
71
+ return denoised[index].rgb;
72
+ }
73
+
74
+ let stored = accumulation[index];
75
+
76
+ return select(stored.rgb / stored.w, vec3<f32>(0.0), stored.w <= 0.0);
77
+ }
78
+
79
+ fn downTexel(offset: u32, size: vec2<u32>, x: i32, y: i32) -> vec3<f32> {
80
+ let s = vec2<i32>(size);
81
+
82
+ return down[offset + u32(clamp(y, 0, s.y - 1) * s.x + clamp(x, 0, s.x - 1))].rgb;
83
+ }
84
+
85
+ fn upTexel(offset: u32, size: vec2<u32>, x: i32, y: i32) -> vec3<f32> {
86
+ let s = vec2<i32>(size);
87
+
88
+ return up[offset + u32(clamp(y, 0, s.y - 1) * s.x + clamp(x, 0, s.x - 1))].rgb;
89
+ }
90
+
91
+ /** Bilinear weights and the texel below and left of a point given in texels, edges clamped. */
92
+ fn bilinearAt(position: vec2<f32>) -> vec4<f32> {
93
+ let p = position - 0.5;
94
+ let base = floor(p);
95
+
96
+ return vec4<f32>(base, p - base);
97
+ }
98
+
99
+ fn imageBilinear(position: vec2<f32>) -> vec3<f32> {
100
+ let b = bilinearAt(position);
101
+ let x = i32(b.x);
102
+ let y = i32(b.y);
103
+
104
+ return mix(
105
+ mix(imageTexel(x, y), imageTexel(x + 1, y), b.z),
106
+ mix(imageTexel(x, y + 1), imageTexel(x + 1, y + 1), b.z), b.w);
107
+ }
108
+
109
+ fn downBilinear(offset: u32, size: vec2<u32>, position: vec2<f32>) -> vec3<f32> {
110
+ let b = bilinearAt(position);
111
+ let x = i32(b.x);
112
+ let y = i32(b.y);
113
+
114
+ return mix(
115
+ mix(downTexel(offset, size, x, y), downTexel(offset, size, x + 1, y), b.z),
116
+ mix(downTexel(offset, size, x, y + 1), downTexel(offset, size, x + 1, y + 1), b.z), b.w);
117
+ }
118
+
119
+ fn upBilinear(offset: u32, size: vec2<u32>, position: vec2<f32>) -> vec3<f32> {
120
+ let b = bilinearAt(position);
121
+ let x = i32(b.x);
122
+ let y = i32(b.y);
123
+
124
+ return mix(
125
+ mix(upTexel(offset, size, x, y), upTexel(offset, size, x + 1, y), b.z),
126
+ mix(upTexel(offset, size, x, y + 1), upTexel(offset, size, x + 1, y + 1), b.z), b.w);
127
+ }
128
+
129
+ /** The level being filtered, read bilinearly at a point given in its own texels. */
130
+ fn sourceAt(position: vec2<f32>) -> vec3<f32> {
131
+ if (lens.fromImage != 0u) {
132
+ return imageBilinear(position);
133
+ }
134
+
135
+ return downBilinear(lens.sourceOffset, lens.sourceSize, position);
136
+ }
137
+
138
+ fn luminance(color: vec3<f32>) -> f32 {
139
+ return dot(color, vec3<f32>(0.2126, 0.7152, 0.0722));
140
+ }
141
+
142
+ /** Karis' average: a group of taps weighed by one over one plus its luminance, so a lone firefly cannot flare. */
143
+ fn karis(sum: vec3<f32>) -> vec4<f32> {
144
+ let weight = 1.0 / (1.0 + luminance(sum));
145
+
146
+ return vec4<f32>(sum * weight, weight);
147
+ }
148
+
149
+ /** One level of the pyramid from the one above it: Jimenez's 13 taps, four overlapping boxes and a centre one. */
150
+ @compute @workgroup_size(8, 8, 1)
151
+ fn downsample(@builtin(global_invocation_id) id: vec3<u32>) {
152
+ if (id.x >= lens.targetSize.x || id.y >= lens.targetSize.y) {
153
+ return;
154
+ }
155
+
156
+ let scale = vec2<f32>(lens.sourceSize) / vec2<f32>(lens.targetSize);
157
+ let centre = (vec2<f32>(id.xy) + 0.5) * scale;
158
+ let a = sourceAt(centre + vec2<f32>(-2.0, -2.0));
159
+ let b = sourceAt(centre + vec2<f32>(0.0, -2.0));
160
+ let c = sourceAt(centre + vec2<f32>(2.0, -2.0));
161
+ let d = sourceAt(centre + vec2<f32>(-2.0, 0.0));
162
+ let e = sourceAt(centre);
163
+ let f = sourceAt(centre + vec2<f32>(2.0, 0.0));
164
+ let g = sourceAt(centre + vec2<f32>(-2.0, 2.0));
165
+ let h = sourceAt(centre + vec2<f32>(0.0, 2.0));
166
+ let i = sourceAt(centre + vec2<f32>(2.0, 2.0));
167
+ let j = sourceAt(centre + vec2<f32>(-1.0, -1.0));
168
+ let k = sourceAt(centre + vec2<f32>(1.0, -1.0));
169
+ let l = sourceAt(centre + vec2<f32>(-1.0, 1.0));
170
+ let m = sourceAt(centre + vec2<f32>(1.0, 1.0));
171
+ var result: vec3<f32>;
172
+
173
+ if (lens.fromImage != 0u) {
174
+ let groups = karis((j + k + l + m) * 0.25) * 0.5
175
+ + karis((a + b + d + e) * 0.25) * 0.125
176
+ + karis((b + c + e + f) * 0.25) * 0.125
177
+ + karis((d + e + g + h) * 0.25) * 0.125
178
+ + karis((e + f + h + i) * 0.25) * 0.125;
179
+
180
+ result = groups.rgb / max(groups.w, 1e-6);
181
+ } else {
182
+ result = e * 0.125 + (a + c + g + i) * 0.03125 + (b + d + f + h) * 0.0625 + (j + k + l + m) * 0.125;
183
+ }
184
+
185
+ down[lens.targetOffset + id.y * lens.targetSize.x + id.x] = vec4<f32>(max(result, vec3<f32>(0.0)), 1.0);
186
+ }
187
+
188
+ /** A 3 x 3 tent over the smaller level, read at a point given in its texels. */
189
+ fn tent(position: vec2<f32>) -> vec3<f32> {
190
+ var sum = vec3<f32>(0.0);
191
+
192
+ for (var dy = -1; dy <= 1; dy = dy + 1) {
193
+ for (var dx = -1; dx <= 1; dx = dx + 1) {
194
+ let weight = f32((2 - abs(dx)) * (2 - abs(dy))) / 16.0;
195
+ let at = position + vec2<f32>(f32(dx), f32(dy));
196
+
197
+ if (lens.fromDown != 0u) {
198
+ sum = sum + downBilinear(lens.sourceOffset, lens.sourceSize, at) * weight;
199
+ } else {
200
+ sum = sum + upBilinear(lens.sourceOffset, lens.sourceSize, at) * weight;
201
+ }
202
+ }
203
+ }
204
+
205
+ return sum;
206
+ }
207
+
208
+ /** One level folded back: its own light, mixed by the radius with the wider spread of every level below it. */
209
+ @compute @workgroup_size(8, 8, 1)
210
+ fn upsample(@builtin(global_invocation_id) id: vec3<u32>) {
211
+ if (id.x >= lens.targetSize.x || id.y >= lens.targetSize.y) {
212
+ return;
213
+ }
214
+
215
+ let scale = vec2<f32>(lens.sourceSize) / vec2<f32>(lens.targetSize);
216
+ let wider = tent((vec2<f32>(id.xy) + 0.5) * scale);
217
+ let index = lens.targetOffset + id.y * lens.targetSize.x + id.x;
218
+
219
+ up[index] = vec4<f32>(mix(down[index].rgb, wider, lens.radius), 1.0);
220
+ }
221
+
222
+ /** The aperture's rays at one texel of the streak level: the light behind it along each ray, fading with distance. */
223
+ @compute @workgroup_size(8, 8, 1)
224
+ fn streaks(@builtin(global_invocation_id) id: vec3<u32>) {
225
+ if (id.x >= lens.targetSize.x || id.y >= lens.targetSize.y) {
226
+ return;
227
+ }
228
+
229
+ let centre = vec2<f32>(id.xy) + 0.5;
230
+ var sum = vec3<f32>(0.0);
231
+ var total = 0.0;
232
+
233
+ for (var r = 0u; r < lens.rays; r = r + 1u) {
234
+ let angle = lens.glareAngle + f32(r) * 2.0 * PI / f32(lens.rays);
235
+ let along = vec2<f32>(cos(angle), sin(angle));
236
+
237
+ for (var j = 1; j <= STREAK_TAPS; j = j + 1) {
238
+ let t = f32(j) / f32(STREAK_TAPS);
239
+ let weight = exp(-4.0 * t);
240
+
241
+ sum = sum + downBilinear(lens.sourceOffset, lens.sourceSize, centre - along * (t * lens.glareLength)) * weight;
242
+ total = total + weight;
243
+ }
244
+ }
245
+
246
+ streaked[id.y * lens.targetSize.x + id.x] = vec4<f32>(sum / max(total, 1e-6), 1.0);
247
+ }
248
+
249
+ fn streakTexel(x: i32, y: i32) -> vec3<f32> {
250
+ let s = vec2<i32>(lens.streakSize);
251
+
252
+ return streaked[u32(clamp(y, 0, s.y - 1) * s.x + clamp(x, 0, s.x - 1))].rgb;
253
+ }
254
+
255
+ fn streakBilinear(position: vec2<f32>) -> vec3<f32> {
256
+ let b = bilinearAt(position);
257
+ let x = i32(b.x);
258
+ let y = i32(b.y);
259
+
260
+ return mix(mix(streakTexel(x, y), streakTexel(x + 1, y), b.z), mix(streakTexel(x, y + 1), streakTexel(x + 1, y + 1), b.z), b.w);
261
+ }
262
+
263
+ /** The folded halo at full resolution: a tent over its largest level, which is half the image's size. */
264
+ fn tentHalo(position: vec2<f32>) -> vec3<f32> {
265
+ var sum = vec3<f32>(0.0);
266
+
267
+ for (var dy = -1; dy <= 1; dy = dy + 1) {
268
+ for (var dx = -1; dx <= 1; dx = dx + 1) {
269
+ let weight = f32((2 - abs(dx)) * (2 - abs(dy))) / 16.0;
270
+
271
+ sum = sum + upBilinear(lens.haloOffset, lens.haloSize, position + vec2<f32>(f32(dx), f32(dy)) * 0.5) * weight;
272
+ }
273
+ }
274
+
275
+ return sum;
276
+ }
277
+
278
+ /** The image as the lens delivers it: fringed towards the corners, with its halo, balanced for the camera's white. */
279
+ @compute @workgroup_size(8, 8, 1)
280
+ fn composite(@builtin(global_invocation_id) id: vec3<u32>) {
281
+ if (id.x >= lens.resolution.x || id.y >= lens.resolution.y) {
282
+ return;
283
+ }
284
+
285
+ let size = vec2<f32>(lens.resolution);
286
+ let position = vec2<f32>(id.xy) + 0.5;
287
+ var color = imageTexel(i32(id.x), i32(id.y));
288
+
289
+ // Lateral chromatic aberration: each colour is magnified a little differently, red outwards and blue inwards,
290
+ // in proportion to the distance from the centre, which is how a lens's dispersion moves them.
291
+ if (lens.aberration > 0.0) {
292
+ let centre = size * 0.5;
293
+ let away = position - centre;
294
+ let red = imageBilinear(centre + away * (1.0 - lens.aberration));
295
+ let blue = imageBilinear(centre + away * (1.0 + lens.aberration));
296
+
297
+ color = vec3<f32>(red.r, color.g, blue.b);
298
+ }
299
+
300
+ if (lens.intensity > 0.0) {
301
+ let halo = tentHalo(position * vec2<f32>(lens.haloSize) / size);
302
+
303
+ color = mix(color, halo, lens.intensity);
304
+ }
305
+
306
+ if (lens.glare > 0.0) {
307
+ color = mix(color, streakBilinear(position * vec2<f32>(lens.streakSize) / size), lens.glare);
308
+ }
309
+
310
+ let balanced = vec3<f32>(dot(lens.white0.xyz, color), dot(lens.white1.xyz, color), dot(lens.white2.xyz, color));
311
+
312
+ graded[id.y * lens.resolution.x + id.x] = vec4<f32>(max(balanced, vec3<f32>(0.0)), 1.0);
313
+ }
314
+ `;
@@ -41,7 +41,8 @@ fn lightByProbability(u: f32) -> Light {
41
41
 
42
42
  let middle = (low + high) / 2u;
43
43
 
44
- if (lights[middle].cdf > u) {
44
+ // The probabilities packed tight in the texture buffer: the same floats as lights[].cdf, a word apart.
45
+ if (extraFloat(frame.lightCdf + middle) > u) {
45
46
  high = middle;
46
47
  } else {
47
48
  low = middle + 1u;
@@ -51,11 +52,143 @@ fn lightByProbability(u: f32) -> Light {
51
52
  return lights[low];
52
53
  }
53
54
 
55
+ const TREE_NODE_WORDS: u32 = 16u;
56
+ const TREE_LEAF: u32 = 0x80000000u;
57
+ const TREE_ROOT_PARENT: u32 = 0xffffffffu;
58
+ const NO_LIGHT: u32 = 0xffffffffu;
59
+
54
60
  /**
55
- * The area density with which direct sampling would have picked a point on this triangle, or zero if it never would.
56
- * Only the leading entries are emissive triangles, and they are in triangle order, so one binary search answers it.
61
+ * How much the lights under a node of the light tree can deliver to a point: their power over the square of the
62
+ * distance, times how well their cone of directions and the receiving surface face each other, each widened by the
63
+ * angle the node's box takes from the point (Conty Estevez and Kulla's importance). A point inside the box rules
64
+ * nothing out. A zero normal is a receiver that takes light from everywhere, such as a point in a volume.
57
65
  */
58
- fn lightPdfOfTriangle(triangle: u32) -> f32 {
66
+ fn lightNodeImportance(node: u32, position: vec3<f32>, normal: vec3<f32>) -> f32 {
67
+ let at = frame.lightTree + node * TREE_NODE_WORDS;
68
+ let low = extraVec4(at);
69
+ let high = extraVec4(at + 4u);
70
+ let cone = extraVec4(at + 8u);
71
+
72
+ if (low.w <= 0.0) {
73
+ return 0.0;
74
+ }
75
+
76
+ let centre = (low.xyz + high.xyz) * 0.5;
77
+ let radius = length(high.xyz - low.xyz) * 0.5;
78
+ let offset = position - centre;
79
+ let distance2 = dot(offset, offset);
80
+
81
+ if (distance2 <= radius * radius) {
82
+ return low.w / max(radius * radius, 1e-12);
83
+ }
84
+
85
+ let distance = sqrt(distance2);
86
+ let spread = asin(clamp(radius / distance, 0.0, 1.0));
87
+ var orientation = 1.0;
88
+
89
+ // A cone of minus one is the whole sphere: such lights shine towards every point alike.
90
+ if (high.w > -1.0) {
91
+ let theta = acos(clamp(dot(cone.xyz, offset / distance), -1.0, 1.0));
92
+ let past = max(0.0, theta - acos(high.w) - spread);
93
+
94
+ orientation = select(0.0, cos(past), past <= acos(clamp(cone.w, -1.0, 1.0)));
95
+ }
96
+
97
+ var facing = 1.0;
98
+
99
+ if (any(normal != vec3<f32>(0.0))) {
100
+ let past = max(0.0, acos(clamp(dot(normal, -offset / distance), -1.0, 1.0)) - spread);
101
+
102
+ facing = select(0.0, cos(past), past < 0.5 * PI);
103
+ }
104
+
105
+ return low.w * orientation * facing / distance2;
106
+ }
107
+
108
+ /** The chance of stepping from a node of the tree to each of its children, from this point. */
109
+ fn lightChildOdds(left: u32, right: u32, position: vec3<f32>, normal: vec3<f32>) -> vec2<f32> {
110
+ var importance = vec2<f32>(0.0);
111
+
112
+ // One loop over the two children, so the importance is written into the kernel once per walk.
113
+ for (var k = 0u; k < 2u; k = k + 1u) {
114
+ importance[k] = lightNodeImportance(select(right, left, k == 0u), position, normal);
115
+ }
116
+
117
+ let total = importance.x + importance.y;
118
+
119
+ return select(importance / total, vec2<f32>(0.0), total <= 0.0);
120
+ }
121
+
122
+ /** A light picked down the tree by importance from this point, with the chance of picking it; NO_LIGHT for none. */
123
+ fn sampleLightTree(position: vec3<f32>, normal: vec3<f32>, u: f32, pdf: ptr<function, f32>,
124
+ density: ptr<function, f32>) -> u32 {
125
+ var node = 0u;
126
+ var chance = 1.0;
127
+ var x = u;
128
+
129
+ for (var depth = 0u; depth < 64u; depth = depth + 1u) {
130
+ let at = frame.lightTree + node * TREE_NODE_WORDS;
131
+ let first = textures[at + 12u];
132
+
133
+ if ((first & TREE_LEAF) != 0u) {
134
+ *pdf = chance;
135
+ *density = extraFloat(at + 13u);
136
+
137
+ return first & ~TREE_LEAF;
138
+ }
139
+
140
+ let second = textures[at + 13u];
141
+ let odds = lightChildOdds(first, second, position, normal);
142
+
143
+ if (odds.x + odds.y <= 0.0) {
144
+ return NO_LIGHT;
145
+ }
146
+
147
+ if (x < odds.x) {
148
+ node = first;
149
+ chance = chance * odds.x;
150
+ x = x / odds.x;
151
+ } else {
152
+ node = second;
153
+ chance = chance * odds.y;
154
+ x = (x - odds.x) / max(odds.y, 1e-20);
155
+ }
156
+
157
+ x = min(x, 0.99999994);
158
+ }
159
+
160
+ return NO_LIGHT;
161
+ }
162
+
163
+ /** The chance the tree would have picked the local light at this index from this point, walked up from its leaf. */
164
+ fn lightTreePdf(index: u32, position: vec3<f32>, normal: vec3<f32>) -> f32 {
165
+ var node = textures[frame.lightLeaves + index];
166
+ var chance = extraFloat(frame.lightTree + node * TREE_NODE_WORDS + 13u);
167
+
168
+ for (var depth = 0u; depth < 64u; depth = depth + 1u) {
169
+ let parent = textures[frame.lightTree + node * TREE_NODE_WORDS + 14u];
170
+
171
+ if (parent == TREE_ROOT_PARENT) {
172
+ return chance;
173
+ }
174
+
175
+ let at = frame.lightTree + parent * TREE_NODE_WORDS;
176
+ let first = textures[at + 12u];
177
+ let odds = lightChildOdds(first, textures[at + 13u], position, normal);
178
+
179
+ chance = chance * select(odds.y, odds.x, node == first);
180
+ node = parent;
181
+ }
182
+
183
+ return 0.0;
184
+ }
185
+
186
+ /**
187
+ * The area density with which direct sampling from this point would have picked a point on this triangle, or zero if
188
+ * it never would. Only the leading entries are emissive triangles, and they are in triangle order, so one binary search
189
+ * finds the entry; the tree, when there is one, says how likely it was from here.
190
+ */
191
+ fn lightPdfOfTriangle(triangle: u32, position: vec3<f32>, normal: vec3<f32>) -> f32 {
59
192
  var low = 0u;
60
193
  var high = frame.areaLightCount;
61
194
 
@@ -65,9 +198,13 @@ fn lightPdfOfTriangle(triangle: u32) -> f32 {
65
198
  }
66
199
 
67
200
  let middle = (low + high) / 2u;
68
- let candidate = lights[middle].triangle;
201
+ let candidate = textures[frame.lightTriangles + middle];
69
202
 
70
203
  if (candidate == triangle) {
204
+ if (frame.lightTree != 0u) {
205
+ return frame.localLightPick * lightTreePdf(middle, position, normal);
206
+ }
207
+
71
208
  return lights[middle].pdf;
72
209
  }
73
210
 
@@ -127,12 +264,27 @@ fn rectanglePoint(light: Light, u: vec2<f32>) -> vec3<f32> {
127
264
  * Where the light that pick selects arrives from, and how much of it. u places the point on a surface light or spreads a
128
265
  * point light's shadow, so the same two dimensions serve every kind.
129
266
  */
130
- fn sampleLight(position: vec3<f32>, pick: f32, u: vec2<f32>) -> LightSample {
267
+ fn sampleLight(position: vec3<f32>, normal: vec3<f32>, pick: f32, u: vec2<f32>) -> LightSample {
131
268
  if (frame.lightCount == 0u) {
132
269
  return noLight();
133
270
  }
134
271
 
135
- return sampleLightEntry(lightByProbability(pick), position, u);
272
+ // The lights in the scene go through the tree, by what they can deliver here; what lies outside it by its weight.
273
+ if (frame.lightTree != 0u && pick < frame.localLightPick) {
274
+ var chance = 0.0;
275
+ var density = 1.0;
276
+ let index = sampleLightTree(position, normal, pick / frame.localLightPick, &chance, &density);
277
+
278
+ if (index == NO_LIGHT || chance <= 0.0) {
279
+ return noLight();
280
+ }
281
+
282
+ return sampleLightEntry(lights[index], frame.localLightPick * chance * density, position, u);
283
+ }
284
+
285
+ let light = lightByProbability(pick);
286
+
287
+ return sampleLightEntry(light, light.pdf, position, u);
136
288
  }
137
289
 
138
290
  /** Three's spotLightMap: inside the projection's frustum the texture tints the light; outside it leaves it alone. */
@@ -159,7 +311,8 @@ fn spotLightTint(light: Light, position: vec3<f32>) -> vec3<f32> {
159
311
  return sampleTextureRaw(map, projected.xy).rgb;
160
312
  }
161
313
 
162
- fn sampleLightEntry(light: Light, position: vec3<f32>, u: vec2<f32>) -> LightSample {
314
+ /** pdf is the chance of picking this light, times the area density of a point on it where it has an area. */
315
+ fn sampleLightEntry(light: Light, pdf: f32, position: vec3<f32>, u: vec2<f32>) -> LightSample {
163
316
  var sample: LightSample;
164
317
  sample.valid = true;
165
318
  sample.kind = light.kind;
@@ -175,7 +328,7 @@ fn sampleLightEntry(light: Light, position: vec3<f32>, u: vec2<f32>) -> LightSam
175
328
  return noLight();
176
329
  }
177
330
 
178
- let density = light.pdf * drawn.pdf;
331
+ let density = pdf * drawn.pdf;
179
332
 
180
333
  sample.direction = drawn.direction;
181
334
  sample.distance = 1e30;
@@ -190,7 +343,7 @@ fn sampleLightEntry(light: Light, position: vec3<f32>, u: vec2<f32>) -> LightSam
190
343
  sample.direction = spreadDirection(light.direction, u);
191
344
  sample.distance = 1e30;
192
345
  // The colour already carries the intensity, which for a directional light is the irradiance it delivers.
193
- sample.value = light.color / light.pdf;
346
+ sample.value = light.color / pdf;
194
347
 
195
348
  return sample;
196
349
  }
@@ -219,7 +372,7 @@ fn sampleLightEntry(light: Light, position: vec3<f32>, u: vec2<f32>) -> LightSam
219
372
 
220
373
  // Stop just short of the light so its own surface is not reported as the blocker.
221
374
  sample.distance = distance * (1.0 - 1e-3);
222
- let density = light.pdf * distanceSquared / cosLight;
375
+ let density = pdf * distanceSquared / cosLight;
223
376
  // The emissive map is read at the very point that was sampled, so this agrees with what a bounce ray would find.
224
377
  let emission = select(light.color, emissionAt(light.triangle, bary), area);
225
378
 
@@ -259,7 +412,7 @@ fn sampleLightEntry(light: Light, position: vec3<f32>, u: vec2<f32>) -> LightSam
259
412
 
260
413
  sample.direction = spreadDirection(towards, u);
261
414
  sample.distance = distance;
262
- sample.value = light.color * (power / light.pdf);
415
+ sample.value = light.color * (power / pdf);
263
416
 
264
417
  if (light.kind == LIGHT_SPOT) {
265
418
  sample.value = sample.value * spotLightTint(light, position);
@@ -276,7 +429,7 @@ fn blocksAll(m: u32) -> bool {
276
429
  let flags = materials[m].flags;
277
430
  let blending = materials[m].blending;
278
431
 
279
- return (flags & (MATERIAL_TRANSPARENT | MATERIAL_ALPHA_HASH | MATERIAL_WIREFRAME)) == 0u
432
+ return (flags & (MATERIAL_TRANSPARENT | MATERIAL_ALPHA_HASH | MATERIAL_WIREFRAME | MATERIAL_VOLUME)) == 0u
280
433
  && materials[m].alphaTest <= 0.0
281
434
  && materials[m].transmission <= 0.0
282
435
  && materials[m].clipCount == 0u
@@ -332,45 +485,63 @@ fn blendColor(triangle: u32, bary: vec2<f32>) -> vec3<f32> {
332
485
  /**
333
486
  * How much of the light this sample came from reaches the shaded point. A surface that only partly covers its triangle
334
487
  * blocks the ray only that often, which is what makes a cutout leaf cast a cutout shadow; a thin transmissive wall
335
- * lets its own share through, tinted.
488
+ * lets its own share through, tinted. A volume's boundary lets the ray in or out, and the medium inside takes its
489
+ * share over the distance between; medium is the density at the shaded point itself, zero outside any volume.
336
490
  */
337
- fn lightTransmittance(
338
- position: vec3<f32>, chosen: LightSample, rng: ptr<function, PathSampler>, skipMaterial: u32) -> vec3<f32> {
339
- if (!chosen.occlude) {
491
+ fn lightTransmittance(position: vec3<f32>, chosen: LightSample, rng: ptr<function, PathSampler>, skipMaterial: u32,
492
+ medium: f32) -> vec3<f32> {
493
+ // A light that casts no shadow is blocked by nothing, but a medium on the way still takes its share: then the walk
494
+ // below only looks for volumes and passes every other surface.
495
+ if (!chosen.occlude && !VOLUMES) {
340
496
  return vec3<f32>(1.0);
341
497
  }
342
498
 
343
- // Most blockers are plainly opaque, and for those any triangle on the way settles it: the search stops at the first
344
- // one found instead of looking for the closest. Anything that lets light through goes the long way below.
345
- let first = traceAny(position, chosen.direction, chosen.distance);
499
+ if (chosen.occlude) {
500
+ // Most blockers are plainly opaque, and for those any triangle on the way settles it: the search stops at the
501
+ // first one found instead of looking for the closest. Anything that lets light through goes the long way below.
502
+ let first = traceAny(position, chosen.direction, chosen.distance);
346
503
 
347
- if (first == NO_HIT) {
348
- return vec3<f32>(1.0);
349
- }
504
+ if (first == NO_HIT) {
505
+ return vec3<f32>(exp(-medium * chosen.distance));
506
+ }
350
507
 
351
- let firstMaterial = materialIndexOf(first);
508
+ let firstMaterial = materialIndexOf(first);
352
509
 
353
- if (firstMaterial != skipMaterial && blocksAll(firstMaterial)) {
354
- return vec3<f32>(0.0);
510
+ if (firstMaterial != skipMaterial && blocksAll(firstMaterial)) {
511
+ return vec3<f32>(0.0);
512
+ }
355
513
  }
356
514
 
357
515
  var origin = position;
358
516
  var remaining = chosen.distance;
359
517
  var through = vec3<f32>(1.0);
518
+ // The medium the ray is in, and how far along the ray it entered it.
519
+ var density = medium;
520
+ var travelled = 0.0;
521
+ var enteredAt = 0.0;
360
522
 
361
523
  for (var skip = 0u; skip < MAX_SHADOW_SKIPS; skip = skip + 1u) {
362
524
  let blocker = traceScene(origin, chosen.direction, remaining);
363
525
 
364
526
  if (blocker.triangle == NO_HIT) {
365
- return through;
527
+ return through * exp(-density * (travelled + remaining - enteredAt));
366
528
  }
367
529
 
368
530
  let bary = vec2<f32>(blocker.u, blocker.v);
369
531
  let m = materialIndexOf(blocker.triangle);
370
532
  let blending = materials[m].blending;
371
533
 
372
- // A shadow catcher catches shadows rather than casting them, and a surface that adds light blocks none.
373
- if (m != skipMaterial && materials[m].kind != KIND_SHADOW && blending != BLEND_ADDITIVE) {
534
+ if (VOLUMES && (materials[m].flags & MATERIAL_VOLUME) != 0u) {
535
+ let at = travelled + blocker.distance;
536
+
537
+ if (dot(authoredNormal(blocker.triangle, bary), chosen.direction) < 0.0) {
538
+ density = select(0.0, materials[m].volume.w, frame.volumesEnabled != 0u);
539
+ enteredAt = at;
540
+ } else {
541
+ through = through * exp(-density * (at - enteredAt));
542
+ density = 0.0;
543
+ }
544
+ } else if (chosen.occlude && m != skipMaterial && materials[m].kind != KIND_SHADOW && blending != BLEND_ADDITIVE) {
374
545
  let coverage = coverageAt(blocker.triangle, bary, chosen.direction);
375
546
 
376
547
  if (blending == BLEND_MULTIPLY) {
@@ -389,13 +560,15 @@ fn lightTransmittance(
389
560
  // The ray carries on from just past the surface it saw through.
390
561
  origin = origin + chosen.direction * (blocker.distance + RAY_EPSILON);
391
562
  remaining = remaining - blocker.distance - RAY_EPSILON;
563
+ travelled = travelled + blocker.distance + RAY_EPSILON;
392
564
 
393
565
  if (remaining <= RAY_EPSILON) {
394
- return through;
566
+ return through * exp(-density * (travelled - enteredAt));
395
567
  }
396
568
  }
397
569
 
398
- return vec3<f32>(0.0);
570
+ // Too many surfaces to see through: a shadow ray gives up dark, a ray that only counts the media keeps what it has.
571
+ return select(vec3<f32>(0.0), through, !chosen.occlude);
399
572
  }
400
573
 
401
574
  /**
@@ -417,13 +590,13 @@ fn shadowMaskAt(position: vec3<f32>, rng: ptr<function, PathSampler>) -> f32 {
417
590
  continue;
418
591
  }
419
592
 
420
- let chosen = sampleLightEntry(light, position, sample2D(rng));
593
+ let chosen = sampleLightEntry(light, light.pdf, position, sample2D(rng));
421
594
 
422
595
  if (!chosen.valid) {
423
596
  continue;
424
597
  }
425
598
 
426
- let visible = luminanceOf(lightTransmittance(position, chosen, rng, NO_MATERIAL));
599
+ let visible = luminanceOf(lightTransmittance(position, chosen, rng, NO_MATERIAL, 0.0));
427
600
 
428
601
  shadow = max(shadow, 1.0 - clamp(visible, 0.0, 1.0));
429
602
  }