Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ A camera with an `fStop` is a thin lens in the `cycles` kernel, with Cycles' mod

A part's material takes `diffuseTransmission` (0 to 1) and `diffuseTransmissionColor` (three numbers from 0 to 1; the base colour when left out) for a thin sheet that lets diffuse light through: a curtain, a leaf, a lampshade. The weight splits the diffuse layer between the usual reflecting lobe and a cosine lobe on the far side of the surface, so a lamp behind the sheet lights its front and the specular layers keep their strength. Cycles 5.3 has no such weight on the Principled BSDF; [`bench/cycles-parity/render-cycles.py`](https://github.com/codepawl/crucible/blob/main/bench/cycles-parity/render-cycles.py) builds the same split from a Principled BSDF at `(1 - weight) * colour` plus a Translucent BSDF at `weight * colour`. A material without the field packs the tables it always did; a scene that uses it adds one extension record per material to the material table ([FORMAT-v2.md](./FORMAT-v2.md), "Material extension records"). The legacy kernel, the WebGPU and WebGL tracers and the raster renderer ignore the field. Numbers: [docs/cycles-parity.md](https://github.com/codepawl/crucible/blob/main/docs/cycles-parity.md), "Diffuse transmission (#1058)".

A part's material takes `dispersion` (0 to 10, `20 / Abbe number`, the scale of glTF `KHR_materials_dispersion` and of Blender 5.3's Principled BSDF `Dispersion`) on a glass (`transmission` above 0, not thin-walled) to split white light into a spectrum: a prism throws a rainbow, a gem shows colour fire. The `cycles` kernel traces one hero wavelength per path and refracts it by Cauchy's equation, so the spectrum is continuous, not three bands. `dispersion: 1` is an Abbe number of 20 (dense flint; diamond is about 0.36, crown glass about 0.3). Leave the field out, or set it to 0, and the packed tables and the image are bit for bit what they were. The cost is colour noise on the paths that go through the glass, which falls with the sample count. The format is in `FORMAT-v2.md` ("Material extension records"). The legacy kernel ignores the field.
A part's material takes `dispersion` (0 to 10, `20 / Abbe number`, the scale of glTF `KHR_materials_dispersion` and of Blender 5.3's Principled BSDF `Dispersion`) on a glass (`transmission` above 0, not thin-walled) to split white light into a spectrum: a prism throws a rainbow, a gem shows colour fire. The `cycles` kernel traces one hero wavelength per path and refracts it by Cauchy's equation, so the spectrum is continuous, not three bands. `dispersion: 1` is an Abbe number of 20 (dense flint; diamond is about 0.36, crown glass about 0.3). Leave the field out, or set it to 0, and the packed tables and the image are bit for bit what they were. A single wavelength lies outside the sRGB gamut, so a dispersive scene keeps negative channels in its samples, as Cycles does: the image can hold small negative values at the most saturated fringes, and an encoder clamps them. The cost is colour noise on the paths that go through the glass, which falls with the sample count. The format is in `FORMAT-v2.md` ("Material extension records"). The legacy kernel ignores the field.

## Dispatches

Expand Down
6 changes: 3 additions & 3 deletions src/cycles/bsdf_glass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ const SPECTRAL_F_LINE_UM: f32 = 0.4861;
const SPECTRAL_C_LINE_UM: f32 = 0.6563;
// The Abbe number of a material whose `dispersion` is 1 (the OpenPBR default).
const SPECTRAL_ABBE_AT_UNIT_DISPERSION: f32 = 20.0;
// Mean of the clamped linear sRGB response over 400 to 700 nm, so that the weight averages to one per channel.
const SPECTRAL_RGB_MEAN: vec3<f32> = vec3<f32>(0.58696402, 0.38445508, 0.36257666);
// Mean of the signed linear sRGB response over 400 to 700 nm, so that the weight averages to one per channel.
const SPECTRAL_RGB_MEAN: vec3<f32> = vec3<f32>(0.42780193, 0.33839936, 0.32177923);

fn spectral_wavelength_um(unit: f32) -> f32 {
return SPECTRAL_WAVELENGTH_MIN_UM + SPECTRAL_WAVELENGTH_RANGE_UM * unit;
Expand Down Expand Up @@ -421,7 +421,7 @@ fn spectral_rgb_weight(wavelength_um: f32) -> vec3<f32> {
-0.9689 * x_bar + 1.8758 * y_bar + 0.0415 * z_bar,
0.0557 * x_bar - 0.2040 * y_bar + 1.0570 * z_bar,
);
return max(linear_rgb, vec3<f32>(0.0)) / SPECTRAL_RGB_MEAN;
return linear_rgb / SPECTRAL_RGB_MEAN;
}

// The refraction index at `wavelength_um` of a glass whose index at the d line is `ior`. Zero dispersion, or an index of one
Expand Down
10 changes: 7 additions & 3 deletions src/cycles/integrator.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ fn int_luminance(rgb: vec3<f32>) -> f32 {
return 0.2126729 * rgb.x + 0.7151522 * rgb.y + 0.0721750 * rgb.z;
}

// A contribution is kept when it is finite. A scene with dispersion also keeps negative channels: the colour of a single
// wavelength lies outside the sRGB gamut, so its linear sRGB weight has negative lobes that cancel the positive ones only once
// the wavelengths of a pixel are summed (Cycles accumulates them signed too). Clamping each sample at zero instead drops the
// negative lobes and desaturates the fringes while it raises the mean.
fn int_sanitize_radiance(value: vec3<f32>) -> vec3<f32> {
let valid_x = value.x >= 0.0 && value.x < 1.0e30;
let valid_y = value.y >= 0.0 && value.y < 1.0e30;
let valid_z = value.z >= 0.0 && value.z < 1.0e30;
let valid_x = (value.x >= 0.0 || SCENE_HAS_DISPERSION) && abs(value.x) < 1.0e30;
let valid_y = (value.y >= 0.0 || SCENE_HAS_DISPERSION) && abs(value.y) < 1.0e30;
let valid_z = (value.z >= 0.0 || SCENE_HAS_DISPERSION) && abs(value.z) < 1.0e30;
return vec3<f32>(
select(0.0, value.x, valid_x),
select(0.0, value.y, valid_y),
Expand Down
2 changes: 2 additions & 0 deletions src/cycles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@ const SCENE_HAS_DIFFUSE_TRANSMISSION: bool = false;";
glass_ior = bsdf_glass_dispersive_ior("), "the index follows the wavelength only when on");
assert!(kernel.contains("if (SCENE_HAS_DISPERSION && arrayLength(&materials) >= 2u * kernel_params.material_count) {"));
assert!(kernel.contains("fn closure_build(point: ptr<function, ShadingPoint>, wo: vec3<f32>, wavelength_um: f32)"));
// A negative channel survives the sanitising of a sample only for a dispersive scene: the spectral weight has negative lobes.
assert!(kernel.contains("let valid_x = (value.x >= 0.0 || SCENE_HAS_DISPERSION) && abs(value.x) < 1.0e30;"));
}

#[test]
Expand Down
18 changes: 13 additions & 5 deletions tests/dispersion.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ function wyman(nm, center, sigmaBelow, sigmaAbove) {
return Math.exp(-0.5 * ((nm - center) / sigma) ** 2);
}

function clampedRgb(nm) {
function signedRgb(nm) {
const x = 1.056 * wyman(nm, 599.8, 37.9, 31.0) + 0.362 * wyman(nm, 442.0, 16.0, 26.7) - 0.065 * wyman(nm, 501.1, 20.4, 26.2);
const y = 0.821 * wyman(nm, 568.8, 46.9, 40.5) + 0.286 * wyman(nm, 530.9, 16.3, 31.1);
const z = 1.217 * wyman(nm, 437.0, 11.8, 36.0) + 0.681 * wyman(nm, 459.0, 26.0, 13.8);
return [
Math.max(3.2406 * x - 1.5372 * y - 0.4986 * z, 0),
Math.max(-0.9689 * x + 1.8758 * y + 0.0415 * z, 0),
Math.max(0.0557 * x - 0.2040 * y + 1.0570 * z, 0),
3.2406 * x - 1.5372 * y - 0.4986 * z,
-0.9689 * x + 1.8758 * y + 0.0415 * z,
0.0557 * x - 0.2040 * y + 1.0570 * z,
];
}

Expand All @@ -81,14 +81,22 @@ test('the spectral weight of the kernel averages to one per channel over the sam
const steps = 6000;
const mean = [0, 0, 0];
for (let step = 0; step < steps; step++) {
const rgb = clampedRgb(400 + (300 * (step + 0.5)) / steps);
const rgb = signedRgb(400 + (300 * (step + 0.5)) / steps);
for (let channel = 0; channel < 3; channel++) mean[channel] += rgb[channel] / steps;
}
for (let channel = 0; channel < 3; channel++) {
assert.ok(Math.abs(mean[channel] - declaredMean[channel]) < 1e-6, `channel ${channel}: ${mean[channel]} against ${declaredMean[channel]}`);
}
});

test('the spectral weight keeps its negative lobes and only a dispersive scene accumulates a negative sample', () => {
assert.match(glassSource, /return linear_rgb \/ SPECTRAL_RGB_MEAN;/, 'the weight is not clamped at zero before it scales the path');
assert.ok(signedRgb(450)[1] < 0, 'a blue wavelength has a negative green lobe, outside the sRGB gamut');
assert.ok(signedRgb(510)[0] < 0, 'a green wavelength has a negative red lobe');
const integratorSource = readFileSync(new URL('../src/cycles/integrator.wgsl', import.meta.url), 'utf8');
assert.match(integratorSource, /\(value\.x >= 0\.0 \|\| SCENE_HAS_DISPERSION\) && abs\(value\.x\) < 1\.0e30/, 'a negative channel is kept only when the scene has dispersion');
});

test('the kernel reads dispersion as 20 divided by the Abbe number around the d line', () => {
assert.match(glassSource, /const SPECTRAL_ABBE_AT_UNIT_DISPERSION: f32 = 20\.0;/);
assert.match(glassSource, /const SPECTRAL_D_LINE_UM: f32 = 0\.5876;/);
Expand Down
Loading