Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Give rough metals their multiple scattering and filter glossy lobes after a diffuse bounce (#959)

Nguyen Xuan An96db52b778c1
diff --git a/changes/959-metal-gap.md b/changes/959-metal-gap.md
new file mode 100644
index 00000000..ef77c8a3
--- /dev/null
+++ b/changes/959-metal-gap.md
@@ -0,0 +1 @@
+- Cook keeps the light that bounces between microfacets on rough metal and widens glossy lobes after a diffuse bounce as Cycles does, so the metal reference scene lands 3.4 times closer to Cycles at 256 samples (#959)
diff --git a/changes/959-metal-gap.size.md b/changes/959-metal-gap.size.md
new file mode 100644
index 00000000..61a6834e
--- /dev/null
+++ b/changes/959-metal-gap.size.md
@@ -0,0 +1 @@
+- scene:agent-scene+lazy: 0 bytes. The change is in the Cook WGSL kernels and does not touch the scene bundles; the webgpu kernel string grows by about 2.5 KB of source (an 81 entry table and three small functions), which gzips to under 1 KB, for a 5.5 times lower error against Cycles on the metal scene outside the fireflies (#959).
diff --git a/docs/path-tracer.md b/docs/path-tracer.md
index 31909fc7..e962e830 100644
--- a/docs/path-tracer.md
+++ b/docs/path-tracer.md
@@ -21,6 +21,7 @@ Unidirectional path tracing against the triangle BVH that the [light bake](light
- Lights: the sun (`keyColor`, `lightDirection`, as a 1 degree disk), point lights with a radius, spot lights with a soft cone, rectangular area lights, and the environment map with its rotation. All of them cast soft shadows, whatever their `castShadow` says, as in Cycles. Light linking is honoured: a light with `affects` (or the sun with `environment.keyAffects`) adds nothing at a part it skips, as a direct light or as a bounce source, and still shadows the other lights.
- Faces on one plane: the trace uses the same cut geometry as the raster view (faces hidden under a face on their plane are removed), and a face the flush pass ranked in front of another instead of cutting it is lifted by 0.1 mm per rank, so the face in front is the one a ray hits (#751).
- Light that bounces between surfaces, and the sky seen in mirrors and through glass.
+- Rough metal that keeps its light: the GGX lobe gives back the light that bounces between microfacets (Fdez-Aguera 2019, from a table of the lobe's albedo), and after a bounce that spread the light the glossy lobes are widened the way Cycles does with `filter_glossy` 1, so a floor lit through a steel surface does not turn into rare, very bright speckles (#959).
- Emission: `emission` times `emissiveColor` (the base colour when none is given) times the `emissiveMap` texel at the hit, so a screen or a sign glows with its picture and lights what is near it. An emissive surface is found by the paths that hit it, not sampled as a light, so a small bright panel is noisy at low sample counts.
- Volume fog: `environment.volumeFog` is traced as a homogeneous medium inside its box, with its `density`, `color` (the scattering albedo), `anisotropy` (Henyey-Greenstein) and `maxDistance`. The distance to each scattering event is drawn from the exact free-flight distribution, so there is no step count and no banding. Every event is lit by next-event estimation (sun, point, spot, area lights and the sky, with shadow rays) and light reaching a surface through the fog is dimmed by it. Fog events count as bounces. `noiseAmount`, `noiseScale`, `drift`, `steps` and the `ambient` glow are rasterizer tricks and are not used.
@@ -210,3 +211,20 @@ WebGPU backend, same setup (2026-11-09, `--backend webgpu`, median of 200 frames
The first path traced draw of a fresh page on the `gpu` backend took 108 s for one sample of `lit-interior` (960 by 540, RTX 3060, ANGLE on Direct3D 11, a profile with no shader cache, `bench/reference/cold-draw.mjs`). All of it was the driver building the kernel: scene load, BVH, packing and texture upload together took 0.15 s, linking took 73.9 s and the first dispatch another 34.1 s (the driver finishes its work on first use), while every later draw took 7 to 35 ms. The compiler inlines each call, and `addLights` called the emitter sampling and the shadow ray from four places, so the traversal was built five times. `addLights` now has one loop for "every emitter" and "one chosen emitter" and the kernel holds one shadow ray (`tests/path-tracer-gpu-kernel-size.test.js` counts the call sites of each variant and the size of the plain kernel). After the change three cold runs on the loaded machine took 27.4, 35.6 and 46.6 s (15.6 to 25.4 s linking, 11.6 to 21.1 s first dispatch), and the old and new kernels compiled side by side in one page took 36 and 7.8 s, 23 and 6.5 s. The progressive tracer at 320 by 180 first step with the random, importance and tree kernels went from 22.9, 18.5 and 38.7 s to 10.3, 9.2 and 14.1 s. That run was not yet reliably under 30 s, and merging the two copies of the traversal (the camera path and the shadow ray) was planned as the next cut; the measurement below supersedes that verdict. The images of `lit-interior` and `spot-area` at 1, 4 and 8 samples with the random, importance and tree kernels are byte for byte the same as before (18 PNGs, MD5). A second page of the same browser session starts in 0.14 s, since the driver program cache is shared, and the compiled kernel is kept per page by `getKernel`. The `webgpu` backend compiles its kernel in about 0.5 s.
Measured later with the same kernel source (the only changes since are to the bench and its test), three cold runs of `lit-interior` at 960 by 540, each in a fresh browser behind the GPU lock (`bench/reference/cold-draw.mjs lit-interior --runs 3`; the bench starts one browser per run, since pages of one browser share the driver program cache and a later page starts in 0.1 s): the wall time of the call was 9.56, 9.54 and 10.17 s. The 27 to 47 s runs above were taken on the loaded machine, the runs here behind the GPU lock on an otherwise idle card; this PR did not isolate which of load or the browser setup explains the gap, so read the 9.5 to 10.2 s range as the idle-card cold time and 27 to 47 s as the loaded one. Link and wait for completion took 4.06 to 4.32 s, the first dispatch with its readback 5.35 to 5.76 s, and every other named phase (scene load, module import, trace scene and BVH, context, source, compile and link submit, scene and texture upload, uniforms and targets) added up to 0.04 s. The named phases cover the wall time to within 0.06 s (the bench prints `wallSeconds` and `unexplainedSeconds`). The kernel is 45 KB, one program per scene. `tests/path-tracer-gpu-kernel-size.test.js` now holds a source size limit for each of the eight kernel variants, one `main` per kernel and one place that builds a program, so a larger kernel or a second program fails the test.
+
+## Metal against Cycles (issue #959)
+
+Two things made Cook's metal scene differ from Cycles outside the fireflies, and both are fixed in the shared WGSL of the native and the webgpu backend:
+
+- Rough metal lost energy. Single scattering GGX drops the light that bounces between microfacets, 8 percent head on at roughness 0.5 and more at grazing angles. The specular lobe now carries the multiple scattering factor of Fdez-Aguera (2019) from a table of the lobe's white albedo (`specularWhiteAlbedo`, integrated over the hemisphere with this kernel's Smith masking), also applied to dielectric highlights.
+- Sparse bright speckle on the floor near glossy metal came from diffuse, glossy, light paths. After a bounce Cook now widens the glossy lobes the way Cycles' `filter_glossy = 1` does: the smallest sampling density met on the path sets a floor of `0.5 * sqrt(1 - pdf)` for the lobe's alpha.
+
+Measured on the `metal` scene at 960 by 540, 256 samples, 8 bounces, firefly clamp 1000, against Cycles 5.3 at 4096 samples (Modal, A10 and L40S, seed 1; Cycles itself at 256 samples for the same scene):
+
+| | relMSE without the 100 worst pixels | mean against Cycles (R, G, B) |
+| --- | --- | --- |
+| Cycles 256 samples | 0.00038 | 0.99988, 0.99975, 0.99963 |
+| Cook before | 0.00386 (10.1 times) | 0.99434, 0.99643, 0.99639 |
+| Cook after | 0.00071 (1.85 times) | 0.99794, 0.99717, 0.99628 |
+
+The mean is still 0.2 to 0.4 percent low, so the 0.1 percent target is not met; the part that is left is not the metal lobe's energy, since the multiple scattering on its own overshoots red. The same four seeds gave means within 0.01 percent of each other, so the remainder is a bias and not noise.
diff --git a/packages/render-native/src/kernel.wgsl b/packages/render-native/src/kernel.wgsl
index 0af9a1d1..88a8dec2 100644
--- a/packages/render-native/src/kernel.wgsl
+++ b/packages/render-native/src/kernel.wgsl
@@ -446,6 +446,7 @@ var<private> surfaceViewCosine: f32;
var<private> surfaceDiffuse: vec3<f32>;
var<private> surfaceSpecularNormal: vec3<f32>;
var<private> surfaceSpecularWeight: f32;
+var<private> surfaceMultipleScattering: vec3<f32>;
var<private> surfaceSpecularAlpha: f32;
var<private> surfaceCoatWeight: f32;
var<private> surfaceCoatChance: f32;
@@ -457,12 +458,61 @@ var<private> surfaceSpecularChance: f32;
var<private> surfaceValue: vec3<f32>;
var<private> surfacePdf: f32;
-fn dielectricSpecularAlbedo(normalReflectance: f32, roughness: f32, viewCosine: f32) -> f32 {
+// The directional albedo of the specular lobe is normalReflectance * x + y.
+fn specularAlbedoTerms(roughness: f32, viewCosine: f32) -> vec2<f32> {
let smoothness = 1.0 - roughness;
let slope = min(smoothness * smoothness, exp2(-9.28 * viewCosine)) * smoothness + (0.0425 - 0.0275 * roughness);
let scale = 1.04 - 0.572 * roughness - 1.04 * slope;
let offset = 1.04 * slope + 0.022 * roughness - 0.04;
- return clamp(normalReflectance * scale + offset, 0.0, 1.0);
+ return vec2<f32>(scale, offset);
+}
+
+fn dielectricSpecularAlbedo(normalReflectance: f32, roughness: f32, viewCosine: f32) -> f32 {
+ let terms = specularAlbedoTerms(roughness, viewCosine);
+ return clamp(normalReflectance * terms.x + terms.y, 0.0, 1.0);
+}
+
+// The share of the light a rough conductor lit by a white sky keeps with single scattering GGX and this kernel's Smith masking, by roughness
+// (rows, 0 to 1 in eighths) and square root of the view cosine (columns, 0 to 1 in eighths). Integrated numerically over the hemisphere.
+var<private> specularWhiteAlbedo: array<f32, 81> = array<f32, 81>(
+ 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
+ 0.7065, 0.6828, 0.9606, 0.9926, 0.9977, 0.9990, 0.9995, 0.9997, 0.9998,
+ 0.9306, 0.9098, 0.8781, 0.9169, 0.9613, 0.9820, 0.9904, 0.9940, 0.9957,
+ 0.9224, 0.9118, 0.8819, 0.8643, 0.8847, 0.9209, 0.9492, 0.9662, 0.9756,
+ 0.8981, 0.8909, 0.8690, 0.8420, 0.8285, 0.8389, 0.8649, 0.8931, 0.9158,
+ 0.8543, 0.8478, 0.8281, 0.8004, 0.7733, 0.7574, 0.7581, 0.7730, 0.7956,
+ 0.7896, 0.7826, 0.7616, 0.7310, 0.6964, 0.6643, 0.6404, 0.6278, 0.6269,
+ 0.7070, 0.6990, 0.6752, 0.6400, 0.5985, 0.5557, 0.5154, 0.4803, 0.4521,
+ 0.6131, 0.6043, 0.5776, 0.5380, 0.4910, 0.4413, 0.3928, 0.3476, 0.3069,
+);
+
+fn lookupSpecularWhiteAlbedo(roughness: f32, viewCosine: f32) -> f32 {
+ let rowPosition = clamp(roughness, 0.0, 1.0) * 8.0;
+ let columnPosition = sqrt(clamp(viewCosine, 0.0, 1.0)) * 8.0;
+ let row = min(u32(rowPosition), 7u);
+ let column = min(u32(columnPosition), 7u);
+ let rowBlend = rowPosition - f32(row);
+ let columnBlend = columnPosition - f32(column);
+ let first = mix(specularWhiteAlbedo[row * 9u + column], specularWhiteAlbedo[row * 9u + column + 1u], columnBlend);
+ let second = mix(specularWhiteAlbedo[(row + 1u) * 9u + column], specularWhiteAlbedo[(row + 1u) * 9u + column + 1u], columnBlend);
+ return mix(first, second, rowBlend);
+}
+
+// Single scattering GGX loses the light that bounces between microfacets, a tenth of a rough metal and more at the grazing angles. This is the
+// factor that gives it back, from the albedo of the lobe with a white Fresnel term and the average Fresnel over the hemisphere (Fdez-Aguera 2019).
+fn multipleScatteringFactor(normalReflectance: vec3<f32>, roughness: f32, viewCosine: f32) -> vec3<f32> {
+ let missing = 1.0 - lookupSpecularWhiteAlbedo(roughness, viewCosine);
+ let averageFresnel = normalReflectance + (vec3<f32>(1.0) - normalReflectance) / 21.0;
+ return vec3<f32>(1.0) + averageFresnel * missing / (vec3<f32>(1.0) - missing * averageFresnel);
+}
+
+// Cycles' glossy filter (filter_glossy 1): after a bounce that spread the light, glossy lobes are widened so that a diffuse-glossy-light path
+// does not become a rare, very bright caustic. The smallest sampling density met on the path sets the widest this lobe gets to be.
+var<private> pathMinimumPdf: f32 = 1e30;
+
+fn glossyBlurAlpha() -> f32 {
+ if (pathMinimumPdf >= 1.0) { return 0.0; }
+ return 0.5 * sqrt(1.0 - pathMinimumPdf);
}
fn prepareSurface(normal: vec3<f32>, view: vec3<f32>) {
@@ -481,8 +531,10 @@ fn prepareSurface(normal: vec3<f32>, view: vec3<f32>) {
surfaceSpecularNormal = vec3<f32>(dielectricNormalReflectance * (1.0 - hitMetallic)) + hitColor * hitMetallic;
let specularLuminance = luminance(surfaceSpecularNormal);
surfaceSpecularWeight = underCoat;
- surfaceSpecularAlpha = roughnessToAlpha(hitRoughness);
- surfaceCoatAlpha = roughnessToAlpha(hitClearcoatRoughness);
+ let blurAlpha = glossyBlurAlpha();
+ surfaceSpecularAlpha = max(roughnessToAlpha(hitRoughness), blurAlpha);
+ surfaceCoatAlpha = max(roughnessToAlpha(hitClearcoatRoughness), blurAlpha);
+ surfaceMultipleScattering = multipleScatteringFactor(surfaceSpecularNormal, hitRoughness, viewCosine);
let diffuseLuminance = luminance(surfaceDiffuse);
let specularChance = schlick(specularLuminance, viewCosine) * underCoat;
let coatChance = surfaceCoatWeight;
@@ -513,7 +565,7 @@ fn evaluateSurface(lightDirection: vec3<f32>) {
if (surfaceSpecularChance > 0.0 && surfaceSpecularAlpha > MIRROR_ALPHA) {
let alpha = surfaceSpecularAlpha;
let term = microfacetTerm(alpha, viewCosine, lightCosine, halfCosine) * surfaceSpecularWeight;
- surfaceValue += schlickColor(surfaceSpecularNormal, viewHalfCosine) * term;
+ surfaceValue += schlickColor(surfaceSpecularNormal, viewHalfCosine) * surfaceMultipleScattering * term;
surfacePdf += surfaceSpecularChance * smithMasking(alpha, viewCosine) * ggxDistribution(alpha, halfCosine) / (4.0 * viewCosine);
}
if (surfaceCoatChance > 0.0 && surfaceCoatAlpha > MIRROR_ALPHA) {
@@ -956,6 +1008,7 @@ fn tracePath(origin: vec3<f32>, startDirection: vec3<f32>, pixel: u32, sampleInd
direction = startDirection;
var position = origin;
var previousPdf = 0.0;
+ pathMinimumPdf = 1e30;
var afterMirror = true;
var normal = vec3<f32>(0.0, 1.0, 0.0);
let bounces = params.sizeBouncesLights.z;
@@ -1040,6 +1093,7 @@ fn tracePath(origin: vec3<f32>, startDirection: vec3<f32>, pixel: u32, sampleInd
if (!(surfacePdf > 0.0)) { break; }
throughput *= surfaceValue * cosine / surfacePdf;
previousPdf = surfacePdf;
+ pathMinimumPdf = min(pathMinimumPdf, surfacePdf);
afterMirror = false;
}
}
diff --git a/src/path-tracer-webgpu-kernel.js b/src/path-tracer-webgpu-kernel.js
index ee99d374..a66cd4b7 100644
--- a/src/path-tracer-webgpu-kernel.js
+++ b/src/path-tracer-webgpu-kernel.js
@@ -537,6 +537,7 @@ var<private> surfaceViewCosine: f32;
var<private> surfaceDiffuse: vec3<f32>;
var<private> surfaceSpecularNormal: vec3<f32>;
var<private> surfaceSpecularWeight: f32;
+var<private> surfaceMultipleScattering: vec3<f32>;
var<private> surfaceSpecularAlpha: f32;
var<private> surfaceCoatWeight: f32;
var<private> surfaceCoatChance: f32;
@@ -548,12 +549,61 @@ var<private> surfaceSpecularChance: f32;
var<private> surfaceValue: vec3<f32>;
var<private> surfacePdf: f32;
-fn dielectricSpecularAlbedo(normalReflectance: f32, roughness: f32, viewCosine: f32) -> f32 {
+// The directional albedo of the specular lobe is normalReflectance * x + y.
+fn specularAlbedoTerms(roughness: f32, viewCosine: f32) -> vec2<f32> {
let smoothness = 1.0 - roughness;
let slope = min(smoothness * smoothness, exp2(-9.28 * viewCosine)) * smoothness + (0.0425 - 0.0275 * roughness);
let scale = 1.04 - 0.572 * roughness - 1.04 * slope;
let offset = 1.04 * slope + 0.022 * roughness - 0.04;
- return clamp(normalReflectance * scale + offset, 0.0, 1.0);
+ return vec2<f32>(scale, offset);
+}
+
+fn dielectricSpecularAlbedo(normalReflectance: f32, roughness: f32, viewCosine: f32) -> f32 {
+ let terms = specularAlbedoTerms(roughness, viewCosine);
+ return clamp(normalReflectance * terms.x + terms.y, 0.0, 1.0);
+}
+
+// The share of the light a rough conductor lit by a white sky keeps with single scattering GGX and this kernel's Smith masking, by roughness
+// (rows, 0 to 1 in eighths) and square root of the view cosine (columns, 0 to 1 in eighths). Integrated numerically over the hemisphere.
+var<private> specularWhiteAlbedo: array<f32, 81> = array<f32, 81>(
+ 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
+ 0.7065, 0.6828, 0.9606, 0.9926, 0.9977, 0.9990, 0.9995, 0.9997, 0.9998,
+ 0.9306, 0.9098, 0.8781, 0.9169, 0.9613, 0.9820, 0.9904, 0.9940, 0.9957,
+ 0.9224, 0.9118, 0.8819, 0.8643, 0.8847, 0.9209, 0.9492, 0.9662, 0.9756,
+ 0.8981, 0.8909, 0.8690, 0.8420, 0.8285, 0.8389, 0.8649, 0.8931, 0.9158,
+ 0.8543, 0.8478, 0.8281, 0.8004, 0.7733, 0.7574, 0.7581, 0.7730, 0.7956,
+ 0.7896, 0.7826, 0.7616, 0.7310, 0.6964, 0.6643, 0.6404, 0.6278, 0.6269,
+ 0.7070, 0.6990, 0.6752, 0.6400, 0.5985, 0.5557, 0.5154, 0.4803, 0.4521,
+ 0.6131, 0.6043, 0.5776, 0.5380, 0.4910, 0.4413, 0.3928, 0.3476, 0.3069,
+);
+
+fn lookupSpecularWhiteAlbedo(roughness: f32, viewCosine: f32) -> f32 {
+ let rowPosition = clamp(roughness, 0.0, 1.0) * 8.0;
+ let columnPosition = sqrt(clamp(viewCosine, 0.0, 1.0)) * 8.0;
+ let row = min(u32(rowPosition), 7u);
+ let column = min(u32(columnPosition), 7u);
+ let rowBlend = rowPosition - f32(row);
+ let columnBlend = columnPosition - f32(column);
+ let first = mix(specularWhiteAlbedo[row * 9u + column], specularWhiteAlbedo[row * 9u + column + 1u], columnBlend);
+ let second = mix(specularWhiteAlbedo[(row + 1u) * 9u + column], specularWhiteAlbedo[(row + 1u) * 9u + column + 1u], columnBlend);
+ return mix(first, second, rowBlend);
+}
+
+// Single scattering GGX loses the light that bounces between microfacets, a tenth of a rough metal and more at the grazing angles. This is the
+// factor that gives it back, from the albedo of the lobe with a white Fresnel term and the average Fresnel over the hemisphere (Fdez-Aguera 2019).
+fn multipleScatteringFactor(normalReflectance: vec3<f32>, roughness: f32, viewCosine: f32) -> vec3<f32> {
+ let missing = 1.0 - lookupSpecularWhiteAlbedo(roughness, viewCosine);
+ let averageFresnel = normalReflectance + (vec3<f32>(1.0) - normalReflectance) / 21.0;
+ return vec3<f32>(1.0) + averageFresnel * missing / (vec3<f32>(1.0) - missing * averageFresnel);
+}
+
+// Cycles' glossy filter (filter_glossy 1): after a bounce that spread the light, glossy lobes are widened so that a diffuse-glossy-light path
+// does not become a rare, very bright caustic. The smallest sampling density met on the path sets the widest this lobe gets to be.
+var<private> pathMinimumPdf: f32 = 1e30;
+
+fn glossyBlurAlpha() -> f32 {
+ if (pathMinimumPdf >= 1.0) { return 0.0; }
+ return 0.5 * sqrt(1.0 - pathMinimumPdf);
}
fn prepareSurface(normal: vec3<f32>, view: vec3<f32>) {
@@ -572,8 +622,10 @@ fn prepareSurface(normal: vec3<f32>, view: vec3<f32>) {
surfaceSpecularNormal = vec3<f32>(dielectricNormalReflectance * (1.0 - hitMetallic)) + hitColor * hitMetallic;
let specularLuminance = luminance(surfaceSpecularNormal);
surfaceSpecularWeight = underCoat;
- surfaceSpecularAlpha = roughnessToAlpha(hitRoughness);
- surfaceCoatAlpha = roughnessToAlpha(hitClearcoatRoughness);
+ let blurAlpha = glossyBlurAlpha();
+ surfaceSpecularAlpha = max(roughnessToAlpha(hitRoughness), blurAlpha);
+ surfaceCoatAlpha = max(roughnessToAlpha(hitClearcoatRoughness), blurAlpha);
+ surfaceMultipleScattering = multipleScatteringFactor(surfaceSpecularNormal, hitRoughness, viewCosine);
let diffuseLuminance = luminance(surfaceDiffuse);
let specularChance = schlick(specularLuminance, viewCosine) * underCoat;
let coatChance = surfaceCoatWeight;
@@ -604,7 +656,7 @@ fn evaluateSurface(lightDirection: vec3<f32>) {
if (surfaceSpecularChance > 0.0 && surfaceSpecularAlpha > MIRROR_ALPHA) {
let alpha = surfaceSpecularAlpha;
let term = microfacetTerm(alpha, viewCosine, lightCosine, halfCosine) * surfaceSpecularWeight;
- surfaceValue += schlickColor(surfaceSpecularNormal, viewHalfCosine) * term;
+ surfaceValue += schlickColor(surfaceSpecularNormal, viewHalfCosine) * surfaceMultipleScattering * term;
surfacePdf += surfaceSpecularChance * smithMasking(alpha, viewCosine) * ggxDistribution(alpha, halfCosine) / (4.0 * viewCosine);
}
if (surfaceCoatChance > 0.0 && surfaceCoatAlpha > MIRROR_ALPHA) {
@@ -1063,6 +1115,7 @@ fn tracePath(origin: vec3<f32>, startDirection: vec3<f32>, pixel: u32, sampleInd
direction = startDirection;
var position = origin;
var previousPdf = 0.0;
+ pathMinimumPdf = 1e30;
var afterMirror = true;
var normal = vec3<f32>(0.0, 1.0, 0.0);
let bounces = params.sizeBouncesLights.z;
@@ -1148,6 +1201,7 @@ fn tracePath(origin: vec3<f32>, startDirection: vec3<f32>, pixel: u32, sampleInd
if (!(surfacePdf > 0.0)) { break; }
throughput *= surfaceValue * cosine / surfacePdf;
previousPdf = surfacePdf;
+ pathMinimumPdf = min(pathMinimumPdf, surfacePdf);
afterMirror = false;
}
${realtime ? realtime.record : ''}
@@ -1258,7 +1312,7 @@ fn generatePaths(@builtin(global_invocation_id) id: vec3<u32>) {
let base = id.x * PATH_STATE_VECTORS;
storeVector(base, vec4<f32>(params.eye.xyz, 0.0));
storeVector(base + 1u, vec4<f32>(rayDirection, 1.0));
- storeVector(base + 2u, vec4<f32>(1.0, 1.0, 1.0, 0.0));
+ storeVector(base + 2u, vec4<f32>(1.0, 1.0, 1.0, 1e30));
storeVector(base + 3u, vec4<f32>(0.0));
storeVector(base + 4u, vec4<f32>(1.0, 1.0, 1.0, 0.0));
storeVector(base + 5u, vec4<f32>(0.0, 0.0, 1.0, 0.0));
@@ -1367,6 +1421,7 @@ fn shadePaths(@builtin(global_invocation_id) id: vec3<u32>) {
direction = directionSelection.xyz;
selectionProbability = directionSelection.w;
throughput = loadVector(base + 2u).xyz;
+ pathMinimumPdf = loadVector(base + 2u).w;
radiance = radianceData.xyz;
nearestSlot = i32(pathMeta.z);
nearestU = hitInfo.x;
@@ -1438,6 +1493,7 @@ fn shadePaths(@builtin(global_invocation_id) id: vec3<u32>) {
if (surfacePdf > 0.0) {
throughput *= surfaceValue * cosine / surfacePdf;
previousPdf = surfacePdf;
+ pathMinimumPdf = min(pathMinimumPdf, surfacePdf);
afterMirror = false;
} else {
alive = false;
@@ -1451,7 +1507,7 @@ fn shadePaths(@builtin(global_invocation_id) id: vec3<u32>) {
}
storeVector(base, vec4<f32>(nextOrigin, previousPdf));
storeVector(base + 1u, vec4<f32>(direction, selectionProbability));
- storeVector(base + 2u, vec4<f32>(throughput, 0.0));
+ storeVector(base + 2u, vec4<f32>(throughput, pathMinimumPdf));
storeVector(base + 3u, vec4<f32>(radiance, radianceData.w));
pathMeta.x = rngState;
pathMeta.y = select(0u, FLAG_ALIVE, alive) | select(0u, FLAG_AFTER_MIRROR, afterMirror) | FLAG_SHADED;
diff --git a/tests/cook-kernel-parity.test.js b/tests/cook-kernel-parity.test.js
index 96591e78..7b47ae63 100644
--- a/tests/cook-kernel-parity.test.js
+++ b/tests/cook-kernel-parity.test.js
@@ -16,9 +16,14 @@ const SHARED_FUNCTIONS = [
'roughnessToAlpha',
'sampleVisibleNormal',
'microfacetTerm',
+ 'specularAlbedoTerms',
+ 'lookupSpecularWhiteAlbedo',
+ 'multipleScatteringFactor',
'dielectricSpecularAlbedo',
'evaluateSurface',
'sampleSurface',
+ 'glossyBlurAlpha',
+ 'prepareSurface',
'mirrorWeight',
'addRadiance',
];