Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Decide Russian roulette in the cycles kernel like Cycles, by the square root of the throughput (#966)

Nguyen Xuan An26d3ae572396
diff --git a/packages/render-native/src/cycles/integrator.wgsl b/packages/render-native/src/cycles/integrator.wgsl
index 4ec6fe39..a4a2db87 100644
--- a/packages/render-native/src/cycles/integrator.wgsl
+++ b/packages/render-native/src/cycles/integrator.wgsl
@@ -31,6 +31,14 @@ fn int_sanitize_radiance(value: vec3<f32>) -> vec3<f32> {
);
}
+// Probability that a path with this throughput continues after Russian roulette: the square root of the largest channel, as in
+// Cycles `path_state_continuation_probability` (kernel/integrator/path_state.h), which keeps deep paths alive longer than the plain
+// throughput and so adds less variance. The floor only guards against a division by a tiny survival probability.
+fn int_russian_roulette_survival(throughput: vec3<f32>) -> f32 {
+ let largest_channel = max(abs(throughput.x), max(abs(throughput.y), abs(throughput.z)));
+ return clamp(sqrt(largest_channel), 0.05, 1.0);
+}
+
fn int_clamp_contribution(raw_value: vec3<f32>, clamp_bounce: i32) -> vec3<f32> {
var value = int_sanitize_radiance(raw_value);
let ceiling = select(kernel_params.clamp_direct, kernel_params.clamp_indirect, clamp_bounce > 0);
@@ -840,8 +848,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
terminate_on_hit = true;
}
if (!terminate_on_hit && bounce > kernel_params.min_bounce_rr) {
- let max_channel = max(throughput.x, max(throughput.y, throughput.z));
- let survival = clamp(max_channel, 0.05, 1.0);
+ let survival = int_russian_roulette_survival(throughput);
if (survival < 1.0) {
smp_seek(&rng, step, SMP_SLOT_TERMINATE);
if (smp_next_1d(&rng) >= survival) {
@@ -1057,8 +1064,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
}
if (!terminate_on_hit && bounce > kernel_params.min_bounce_rr) {
- let max_channel = max(throughput.x, max(throughput.y, throughput.z));
- let survival = clamp(max_channel, 0.05, 1.0);
+ let survival = int_russian_roulette_survival(throughput);
if (survival < 1.0) {
smp_seek(&rng, step, SMP_SLOT_TERMINATE);
if (smp_next_1d(&rng) >= survival) {
diff --git a/tests/native-cycles-russian-roulette.test.js b/tests/native-cycles-russian-roulette.test.js
new file mode 100644
index 00000000..156e548e
--- /dev/null
+++ b/tests/native-cycles-russian-roulette.test.js
@@ -0,0 +1,36 @@
+import assert from 'node:assert/strict';
+import { readFileSync } from 'node:fs';
+import test from 'node:test';
+
+// The survival rule is GPU code, so these tests pin its source: the Cycles rule (square root of the largest throughput channel,
+// kernel/integrator/path_state.h `path_state_continuation_probability`) and its use at both Russian roulette sites. Measured effect
+// (#966): with the plain throughput, emissive scenes carried 47 percent more error than Cycles at equal samples.
+const integratorSource = readFileSync(new URL('../packages/render-native/src/cycles/integrator.wgsl', import.meta.url), 'utf8');
+
+function functionBody(source, name) {
+ const start = source.indexOf(`fn ${name}(`);
+ assert.notEqual(start, -1, `integrator.wgsl declares ${name}`);
+ const open = source.indexOf('{', start);
+ let depth = 0;
+ for (let index = open; index < source.length; index++) {
+ if (source[index] === '{') depth++;
+ if (source[index] === '}') {
+ depth--;
+ if (depth === 0) return source.slice(open + 1, index);
+ }
+ }
+ throw new Error(`${name} has no closing brace`);
+}
+
+test('the Russian roulette survival is the square root of the largest throughput channel, as in Cycles', () => {
+ const body = functionBody(integratorSource, 'int_russian_roulette_survival');
+ assert.match(body, /abs\(throughput\.x\)/, 'channels enter by magnitude, like fabs() in Cycles');
+ assert.match(body, /sqrt\(largest_channel\)/, 'the largest channel goes through a square root');
+ assert.match(body, /clamp\([^;]*,\s*0\.05,\s*1\.0\)/, 'survival stays in [0.05, 1]');
+});
+
+test('surface and volume paths both use the shared survival rule and keep no inline linear rule', () => {
+ const callSites = integratorSource.match(/int_russian_roulette_survival\(throughput\)/g) ?? [];
+ assert.equal(callSites.length, 2, 'one call after a volume scatter and one after a surface bounce');
+ assert.doesNotMatch(integratorSource, /clamp\(max_channel,/, 'no survival probability is computed from the plain throughput');
+});