@tinypoly/gpu-renderer 0.0.5 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +59 -3
- package/README.md +52 -4
- package/dist/atmosphere.d.ts +21 -0
- package/dist/atmosphere.d.ts.map +1 -0
- package/dist/atmosphere.js +72 -0
- package/dist/atmosphere.js.map +1 -0
- package/dist/denoise.d.ts +11 -1
- package/dist/denoise.d.ts.map +1 -1
- package/dist/denoise.js +34 -6
- package/dist/denoise.js.map +1 -1
- package/dist/frameLayout.d.ts.map +1 -1
- package/dist/frameLayout.js +15 -0
- package/dist/frameLayout.js.map +1 -1
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +6 -3
- package/dist/geometry.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/lensPass.d.ts +60 -0
- package/dist/lensPass.d.ts.map +1 -0
- package/dist/lensPass.js +235 -0
- package/dist/lensPass.js.map +1 -0
- package/dist/lightTree.d.ts +43 -0
- package/dist/lightTree.d.ts.map +1 -0
- package/dist/lightTree.js +107 -0
- package/dist/lightTree.js.map +1 -0
- package/dist/lights.d.ts +10 -0
- package/dist/lights.d.ts.map +1 -1
- package/dist/lights.js +54 -1
- package/dist/lights.js.map +1 -1
- package/dist/materialLayout.d.ts +24 -0
- package/dist/materialLayout.d.ts.map +1 -1
- package/dist/materialLayout.js +36 -1
- package/dist/materialLayout.js.map +1 -1
- package/dist/pathTracePass.d.ts +6 -3
- package/dist/pathTracePass.d.ts.map +1 -1
- package/dist/pathTracePass.js +16 -10
- package/dist/pathTracePass.js.map +1 -1
- package/dist/renderer.d.ts +19 -7
- package/dist/renderer.d.ts.map +1 -1
- package/dist/renderer.js +118 -14
- package/dist/renderer.js.map +1 -1
- package/dist/sceneUpload.d.ts +5 -0
- package/dist/sceneUpload.d.ts.map +1 -1
- package/dist/sceneUpload.js +4 -0
- package/dist/sceneUpload.js.map +1 -1
- package/dist/settings.d.ts +136 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/settings.js +100 -0
- package/dist/settings.js.map +1 -0
- package/dist/subsurface.d.ts +76 -0
- package/dist/subsurface.d.ts.map +1 -0
- package/dist/subsurface.js +70 -0
- package/dist/subsurface.js.map +1 -0
- package/dist/volumeContainment.d.ts +17 -0
- package/dist/volumeContainment.d.ts.map +1 -0
- package/dist/volumeContainment.js +61 -0
- package/dist/volumeContainment.js.map +1 -0
- package/dist/wgsl/bsdf.wgsl.d.ts +1 -1
- package/dist/wgsl/bsdf.wgsl.d.ts.map +1 -1
- package/dist/wgsl/bsdf.wgsl.js +35 -3
- package/dist/wgsl/bsdf.wgsl.js.map +1 -1
- package/dist/wgsl/common.wgsl.d.ts.map +1 -1
- package/dist/wgsl/common.wgsl.js +4 -0
- package/dist/wgsl/common.wgsl.js.map +1 -1
- package/dist/wgsl/lens.wgsl.d.ts +17 -0
- package/dist/wgsl/lens.wgsl.d.ts.map +1 -0
- package/dist/wgsl/lens.wgsl.js +315 -0
- package/dist/wgsl/lens.wgsl.js.map +1 -0
- package/dist/wgsl/lights.wgsl.d.ts +1 -1
- package/dist/wgsl/lights.wgsl.d.ts.map +1 -1
- package/dist/wgsl/lights.wgsl.js +203 -31
- package/dist/wgsl/lights.wgsl.js.map +1 -1
- package/dist/wgsl/pathTrace.wgsl.d.ts +1 -1
- package/dist/wgsl/pathTrace.wgsl.d.ts.map +1 -1
- package/dist/wgsl/pathTrace.wgsl.js +396 -25
- package/dist/wgsl/pathTrace.wgsl.js.map +1 -1
- package/dist/wgsl/sampling.wgsl.d.ts +1 -1
- package/dist/wgsl/sampling.wgsl.d.ts.map +1 -1
- package/dist/wgsl/sampling.wgsl.js +25 -0
- package/dist/wgsl/sampling.wgsl.js.map +1 -1
- package/dist/wgsl/surface.wgsl.d.ts +1 -1
- package/dist/wgsl/surface.wgsl.d.ts.map +1 -1
- package/dist/wgsl/surface.wgsl.js +21 -1
- package/dist/wgsl/surface.wgsl.js.map +1 -1
- package/package.json +1 -1
- package/src/atmosphere.ts +95 -0
- package/src/denoise.ts +44 -6
- package/src/frameLayout.ts +15 -0
- package/src/geometry.ts +6 -2
- package/src/index.ts +13 -0
- package/src/lensPass.ts +299 -0
- package/src/lightTree.ts +172 -0
- package/src/lights.ts +71 -1
- package/src/materialLayout.ts +52 -1
- package/src/pathTracePass.ts +19 -10
- package/src/renderer.ts +166 -22
- package/src/sceneUpload.ts +9 -0
- package/src/settings.ts +234 -0
- package/src/subsurface.ts +153 -0
- package/src/volumeContainment.ts +81 -0
- package/src/wgsl/bsdf.wgsl.ts +35 -3
- package/src/wgsl/common.wgsl.ts +4 -0
- package/src/wgsl/lens.wgsl.ts +314 -0
- package/src/wgsl/lights.wgsl.ts +203 -31
- package/src/wgsl/pathTrace.wgsl.ts +396 -25
- package/src/wgsl/sampling.wgsl.ts +25 -0
- package/src/wgsl/surface.wgsl.ts +21 -1
|
@@ -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
|
+
`;
|
package/src/wgsl/lights.wgsl.ts
CHANGED
|
@@ -52,11 +52,143 @@ fn lightByProbability(u: f32) -> Light {
|
|
|
52
52
|
return lights[low];
|
|
53
53
|
}
|
|
54
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
|
+
|
|
55
60
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
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.
|
|
58
65
|
*/
|
|
59
|
-
fn
|
|
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 {
|
|
60
192
|
var low = 0u;
|
|
61
193
|
var high = frame.areaLightCount;
|
|
62
194
|
|
|
@@ -69,6 +201,10 @@ fn lightPdfOfTriangle(triangle: u32) -> f32 {
|
|
|
69
201
|
let candidate = textures[frame.lightTriangles + middle];
|
|
70
202
|
|
|
71
203
|
if (candidate == triangle) {
|
|
204
|
+
if (frame.lightTree != 0u) {
|
|
205
|
+
return frame.localLightPick * lightTreePdf(middle, position, normal);
|
|
206
|
+
}
|
|
207
|
+
|
|
72
208
|
return lights[middle].pdf;
|
|
73
209
|
}
|
|
74
210
|
|
|
@@ -128,12 +264,27 @@ fn rectanglePoint(light: Light, u: vec2<f32>) -> vec3<f32> {
|
|
|
128
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
|
|
129
265
|
* point light's shadow, so the same two dimensions serve every kind.
|
|
130
266
|
*/
|
|
131
|
-
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 {
|
|
132
268
|
if (frame.lightCount == 0u) {
|
|
133
269
|
return noLight();
|
|
134
270
|
}
|
|
135
271
|
|
|
136
|
-
|
|
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);
|
|
137
288
|
}
|
|
138
289
|
|
|
139
290
|
/** Three's spotLightMap: inside the projection's frustum the texture tints the light; outside it leaves it alone. */
|
|
@@ -160,7 +311,8 @@ fn spotLightTint(light: Light, position: vec3<f32>) -> vec3<f32> {
|
|
|
160
311
|
return sampleTextureRaw(map, projected.xy).rgb;
|
|
161
312
|
}
|
|
162
313
|
|
|
163
|
-
|
|
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 {
|
|
164
316
|
var sample: LightSample;
|
|
165
317
|
sample.valid = true;
|
|
166
318
|
sample.kind = light.kind;
|
|
@@ -176,7 +328,7 @@ fn sampleLightEntry(light: Light, position: vec3<f32>, u: vec2<f32>) -> LightSam
|
|
|
176
328
|
return noLight();
|
|
177
329
|
}
|
|
178
330
|
|
|
179
|
-
let density =
|
|
331
|
+
let density = pdf * drawn.pdf;
|
|
180
332
|
|
|
181
333
|
sample.direction = drawn.direction;
|
|
182
334
|
sample.distance = 1e30;
|
|
@@ -191,7 +343,7 @@ fn sampleLightEntry(light: Light, position: vec3<f32>, u: vec2<f32>) -> LightSam
|
|
|
191
343
|
sample.direction = spreadDirection(light.direction, u);
|
|
192
344
|
sample.distance = 1e30;
|
|
193
345
|
// The colour already carries the intensity, which for a directional light is the irradiance it delivers.
|
|
194
|
-
sample.value = light.color /
|
|
346
|
+
sample.value = light.color / pdf;
|
|
195
347
|
|
|
196
348
|
return sample;
|
|
197
349
|
}
|
|
@@ -220,7 +372,7 @@ fn sampleLightEntry(light: Light, position: vec3<f32>, u: vec2<f32>) -> LightSam
|
|
|
220
372
|
|
|
221
373
|
// Stop just short of the light so its own surface is not reported as the blocker.
|
|
222
374
|
sample.distance = distance * (1.0 - 1e-3);
|
|
223
|
-
let density =
|
|
375
|
+
let density = pdf * distanceSquared / cosLight;
|
|
224
376
|
// The emissive map is read at the very point that was sampled, so this agrees with what a bounce ray would find.
|
|
225
377
|
let emission = select(light.color, emissionAt(light.triangle, bary), area);
|
|
226
378
|
|
|
@@ -260,7 +412,7 @@ fn sampleLightEntry(light: Light, position: vec3<f32>, u: vec2<f32>) -> LightSam
|
|
|
260
412
|
|
|
261
413
|
sample.direction = spreadDirection(towards, u);
|
|
262
414
|
sample.distance = distance;
|
|
263
|
-
sample.value = light.color * (power /
|
|
415
|
+
sample.value = light.color * (power / pdf);
|
|
264
416
|
|
|
265
417
|
if (light.kind == LIGHT_SPOT) {
|
|
266
418
|
sample.value = sample.value * spotLightTint(light, position);
|
|
@@ -277,7 +429,7 @@ fn blocksAll(m: u32) -> bool {
|
|
|
277
429
|
let flags = materials[m].flags;
|
|
278
430
|
let blending = materials[m].blending;
|
|
279
431
|
|
|
280
|
-
return (flags & (MATERIAL_TRANSPARENT | MATERIAL_ALPHA_HASH | MATERIAL_WIREFRAME)) == 0u
|
|
432
|
+
return (flags & (MATERIAL_TRANSPARENT | MATERIAL_ALPHA_HASH | MATERIAL_WIREFRAME | MATERIAL_VOLUME)) == 0u
|
|
281
433
|
&& materials[m].alphaTest <= 0.0
|
|
282
434
|
&& materials[m].transmission <= 0.0
|
|
283
435
|
&& materials[m].clipCount == 0u
|
|
@@ -333,45 +485,63 @@ fn blendColor(triangle: u32, bary: vec2<f32>) -> vec3<f32> {
|
|
|
333
485
|
/**
|
|
334
486
|
* How much of the light this sample came from reaches the shaded point. A surface that only partly covers its triangle
|
|
335
487
|
* blocks the ray only that often, which is what makes a cutout leaf cast a cutout shadow; a thin transmissive wall
|
|
336
|
-
* 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.
|
|
337
490
|
*/
|
|
338
|
-
fn lightTransmittance(
|
|
339
|
-
|
|
340
|
-
|
|
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) {
|
|
341
496
|
return vec3<f32>(1.0);
|
|
342
497
|
}
|
|
343
498
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
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);
|
|
347
503
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
504
|
+
if (first == NO_HIT) {
|
|
505
|
+
return vec3<f32>(exp(-medium * chosen.distance));
|
|
506
|
+
}
|
|
351
507
|
|
|
352
|
-
|
|
508
|
+
let firstMaterial = materialIndexOf(first);
|
|
353
509
|
|
|
354
|
-
|
|
355
|
-
|
|
510
|
+
if (firstMaterial != skipMaterial && blocksAll(firstMaterial)) {
|
|
511
|
+
return vec3<f32>(0.0);
|
|
512
|
+
}
|
|
356
513
|
}
|
|
357
514
|
|
|
358
515
|
var origin = position;
|
|
359
516
|
var remaining = chosen.distance;
|
|
360
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;
|
|
361
522
|
|
|
362
523
|
for (var skip = 0u; skip < MAX_SHADOW_SKIPS; skip = skip + 1u) {
|
|
363
524
|
let blocker = traceScene(origin, chosen.direction, remaining);
|
|
364
525
|
|
|
365
526
|
if (blocker.triangle == NO_HIT) {
|
|
366
|
-
return through;
|
|
527
|
+
return through * exp(-density * (travelled + remaining - enteredAt));
|
|
367
528
|
}
|
|
368
529
|
|
|
369
530
|
let bary = vec2<f32>(blocker.u, blocker.v);
|
|
370
531
|
let m = materialIndexOf(blocker.triangle);
|
|
371
532
|
let blending = materials[m].blending;
|
|
372
533
|
|
|
373
|
-
|
|
374
|
-
|
|
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) {
|
|
375
545
|
let coverage = coverageAt(blocker.triangle, bary, chosen.direction);
|
|
376
546
|
|
|
377
547
|
if (blending == BLEND_MULTIPLY) {
|
|
@@ -390,13 +560,15 @@ fn lightTransmittance(
|
|
|
390
560
|
// The ray carries on from just past the surface it saw through.
|
|
391
561
|
origin = origin + chosen.direction * (blocker.distance + RAY_EPSILON);
|
|
392
562
|
remaining = remaining - blocker.distance - RAY_EPSILON;
|
|
563
|
+
travelled = travelled + blocker.distance + RAY_EPSILON;
|
|
393
564
|
|
|
394
565
|
if (remaining <= RAY_EPSILON) {
|
|
395
|
-
return through;
|
|
566
|
+
return through * exp(-density * (travelled - enteredAt));
|
|
396
567
|
}
|
|
397
568
|
}
|
|
398
569
|
|
|
399
|
-
|
|
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);
|
|
400
572
|
}
|
|
401
573
|
|
|
402
574
|
/**
|
|
@@ -418,13 +590,13 @@ fn shadowMaskAt(position: vec3<f32>, rng: ptr<function, PathSampler>) -> f32 {
|
|
|
418
590
|
continue;
|
|
419
591
|
}
|
|
420
592
|
|
|
421
|
-
let chosen = sampleLightEntry(light, position, sample2D(rng));
|
|
593
|
+
let chosen = sampleLightEntry(light, light.pdf, position, sample2D(rng));
|
|
422
594
|
|
|
423
595
|
if (!chosen.valid) {
|
|
424
596
|
continue;
|
|
425
597
|
}
|
|
426
598
|
|
|
427
|
-
let visible = luminanceOf(lightTransmittance(position, chosen, rng, NO_MATERIAL));
|
|
599
|
+
let visible = luminanceOf(lightTransmittance(position, chosen, rng, NO_MATERIAL, 0.0));
|
|
428
600
|
|
|
429
601
|
shadow = max(shadow, 1.0 - clamp(visible, 0.0, 1.0));
|
|
430
602
|
}
|