Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Add the many-lights-500 parity scene and measure the light tree on it against the flat pick and Cycles 5.3 (#895)

Nguyen Xuan An7999f3fd1678
diff --git a/bench/cycles-parity/export-scene.mjs b/bench/cycles-parity/export-scene.mjs
index b1acff79..4948b5fe 100644
--- a/bench/cycles-parity/export-scene.mjs
+++ b/bench/cycles-parity/export-scene.mjs
@@ -4,7 +4,7 @@ import { mkdir, writeFile } from 'node:fs/promises';
import { join, resolve } from 'node:path';
import { createCamera } from '../../src/path-tracer.js';
import * as packModule from '../../src/path-tracer-gpu-pack.js';
-import { buildTraceScene } from '../../src/path-tracer-scene.js';
+import { buildTraceSceneWithManyLights } from './trace-scene-many-lights.mjs';
import { buildNativeInput } from '../../packages/render-native/pack-input.mjs';
import { loadReferenceScene, reconstructTraceSceneSource, serializeSceneDescription } from './scene-bridge.mjs';
@@ -35,7 +35,7 @@ function buildPackedTables(traceSource) {
fireflyClamp: 1e9,
noiseThreshold: 0,
};
- const traceScene = buildTraceScene(traceSource.parts, traceSource.environment);
+ const traceScene = buildTraceSceneWithManyLights(traceSource.parts, traceSource.environment);
const camera = createCamera(traceSource.camera, options.width, options.height);
return buildNativeInput(traceScene, camera, options, packModule);
}
diff --git a/bench/cycles-parity/run-m1-parity.py b/bench/cycles-parity/run-m1-parity.py
index cdc09733..7d0256ab 100644
--- a/bench/cycles-parity/run-m1-parity.py
+++ b/bench/cycles-parity/run-m1-parity.py
@@ -15,6 +15,7 @@ M1_SCENES = [
"glass",
"outdoor-hdri",
"lit-interior",
+ "many-lights-500",
]
diff --git a/bench/cycles-parity/trace-native.mjs b/bench/cycles-parity/trace-native.mjs
index 615be561..8d3bda64 100644
--- a/bench/cycles-parity/trace-native.mjs
+++ b/bench/cycles-parity/trace-native.mjs
@@ -5,7 +5,7 @@ import { constants, setPriority } from 'node:os';
import { dirname, resolve } from 'node:path';
import { createCamera } from '../../src/path-tracer.js';
import * as packModule from '../../src/path-tracer-gpu-pack.js';
-import { buildTraceScene } from '../../src/path-tracer-scene.js';
+import { buildTraceSceneWithManyLights } from './trace-scene-many-lights.mjs';
import * as renderNative from '../../packages/render-native/index.mjs';
import { writeFloat32Npy } from './npy.mjs';
import { reconstructTraceSceneSource } from './scene-bridge.mjs';
@@ -135,7 +135,7 @@ async function runCli() {
const totalStarted = performance.now();
const buildStarted = performance.now();
const traceSource = await reconstructTraceSceneSource(description);
- const traceScene = buildTraceScene(traceSource.parts, traceSource.environment);
+ const traceScene = buildTraceSceneWithManyLights(traceSource.parts, traceSource.environment);
const camera = createCamera(traceSource.camera, options.width, options.height);
const samplesPerDispatch = options.samplesPerDispatch !== undefined
? options.samplesPerDispatch
diff --git a/bench/cycles-parity/trace-scene-many-lights.mjs b/bench/cycles-parity/trace-scene-many-lights.mjs
new file mode 100644
index 00000000..e9b779de
--- /dev/null
+++ b/bench/cycles-parity/trace-scene-many-lights.mjs
@@ -0,0 +1,46 @@
+// buildTraceScene takes at most MAX_TRACE_LIGHTS point, spot and area lights, because light linking keeps one mask bit per light.
+// The parity harness has no light links, so a scene with more lights is built in chunks of that size and the light lists are joined.
+// Every joined light gets bit -1, which the native packer reads as "all groups", so no two lights share a link bit.
+import { buildTraceScene, MAX_TRACE_LIGHTS } from '../../src/path-tracer-scene.js';
+
+const UNLINKED_LIGHT_BIT = -1;
+
+function listAnalyticLights(environment) {
+ return [
+ ...(environment.pointLights ?? []).map(light => ({ kind: 'pointLights', light })),
+ ...(environment.spotLights ?? []).map(light => ({ kind: 'spotLights', light })),
+ ...(environment.areaLights ?? []).map(light => ({ kind: 'areaLights', light })),
+ ];
+}
+
+function chunkEnvironment(environment, chunk) {
+ const lightsByKind = { pointLights: [], spotLights: [], areaLights: [] };
+ for (const entry of chunk) {
+ lightsByKind[entry.kind].push(entry.light);
+ }
+ return { ...environment, keyColor: [0, 0, 0], ...lightsByKind };
+}
+
+function unlinkedLights(lights) {
+ return lights.map(light => ({ ...light, bit: UNLINKED_LIGHT_BIT }));
+}
+
+export function buildTraceSceneWithManyLights(parts, environment) {
+ const analyticLights = listAnalyticLights(environment);
+ if (analyticLights.length <= MAX_TRACE_LIGHTS) {
+ return buildTraceScene(parts, environment);
+ }
+ const points = [];
+ const spots = [];
+ const areas = [];
+ for (let start = 0; start < analyticLights.length; start += MAX_TRACE_LIGHTS) {
+ const chunk = analyticLights.slice(start, start + MAX_TRACE_LIGHTS);
+ const chunkLights = buildTraceScene(parts, chunkEnvironment(environment, chunk)).lights;
+ points.push(...unlinkedLights(chunkLights.points));
+ spots.push(...unlinkedLights(chunkLights.spots));
+ areas.push(...unlinkedLights(chunkLights.areas));
+ }
+ const traceScene = buildTraceScene(parts, { ...environment, pointLights: [], spotLights: [], areaLights: [] });
+ traceScene.lights = { sun: traceScene.lights.sun, points, spots, areas };
+ return traceScene;
+}
diff --git a/bench/reference/scenes.js b/bench/reference/scenes.js
index e3af3c61..3851c0bb 100644
--- a/bench/reference/scenes.js
+++ b/bench/reference/scenes.js
@@ -683,6 +683,162 @@ async function subsurfaceScene(weight) {
};
}
+// Issue #895: a 60 m hall cut into ten cells by partition walls with a doorway each, lit by 500 small lights (200 point, 150 spot,
+// 100 emissive quads and 50 emissive triangles) placed from a fixed seed. A surface sees the lights of its own cell and, through
+// the doorways, a few of the next ones, so most of the 500 are far or hidden from any shading point and a flat pick wastes
+// nearly every sample. The count is above MAX_TRACE_LIGHTS, so the parity harness builds it with trace-scene-many-lights.mjs.
+const MANY_LIGHTS_500_SEED = 895;
+const MANY_LIGHTS_500_HALF_LENGTH = 30;
+const MANY_LIGHTS_500_HALF_WIDTH = 4;
+const MANY_LIGHTS_500_HEIGHT = 5;
+const MANY_LIGHTS_500_CELL_LENGTH = 6;
+const MANY_LIGHTS_500_PARTITION_HALF_THICKNESS = 0.1;
+const MANY_LIGHTS_500_DOORWAY_HALF_WIDTH = 1.5;
+const MANY_LIGHTS_500_DOORWAY_HEIGHT = 3.2;
+const MANY_LIGHTS_500_COUNTS = { point: 200, spot: 150, quad: 100, triangle: 50 };
+const MANY_LIGHTS_500_PALETTE = [[1, 0.72, 0.45], [0.8, 0.88, 1], [1, 0.95, 0.85]];
+
+function manyLights500PartitionPlanes() {
+ const planes = [];
+ for (let plane = -MANY_LIGHTS_500_HALF_LENGTH + MANY_LIGHTS_500_CELL_LENGTH; plane < MANY_LIGHTS_500_HALF_LENGTH; plane += MANY_LIGHTS_500_CELL_LENGTH) {
+ planes.push(plane);
+ }
+ return planes;
+}
+
+function manyLights500Shell(cube, wall) {
+ const length = MANY_LIGHTS_500_HALF_LENGTH * 2;
+ const width = MANY_LIGHTS_500_HALF_WIDTH * 2;
+ const middle = MANY_LIGHTS_500_HEIGHT / 2;
+ return [
+ part('hall-floor', cube, [0, -0.05, 0], [width, 0.1, length], { color: [0.5, 0.45, 0.4], roughness: 0.6 }),
+ part('hall-ceiling', cube, [0, MANY_LIGHTS_500_HEIGHT + 0.05, 0], [width, 0.1, length], wall),
+ part('hall-left', cube, [-MANY_LIGHTS_500_HALF_WIDTH - 0.05, middle, 0], [0.1, MANY_LIGHTS_500_HEIGHT + 0.2, length], { ...wall, color: [0.6, 0.35, 0.3] }),
+ part('hall-right', cube, [MANY_LIGHTS_500_HALF_WIDTH + 0.05, middle, 0], [0.1, MANY_LIGHTS_500_HEIGHT + 0.2, length], { ...wall, color: [0.3, 0.42, 0.58] }),
+ part('hall-end', cube, [0, middle, -MANY_LIGHTS_500_HALF_LENGTH - 0.05], [width, MANY_LIGHTS_500_HEIGHT + 0.2, 0.1], wall),
+ part('hall-start', cube, [0, middle, MANY_LIGHTS_500_HALF_LENGTH + 0.05], [width, MANY_LIGHTS_500_HEIGHT + 0.2, 0.1], wall),
+ ];
+}
+
+function manyLights500Partition(cube, wall, planeZ, index) {
+ const thickness = MANY_LIGHTS_500_PARTITION_HALF_THICKNESS * 2;
+ const sideWidth = MANY_LIGHTS_500_HALF_WIDTH - MANY_LIGHTS_500_DOORWAY_HALF_WIDTH;
+ const sideCenter = MANY_LIGHTS_500_DOORWAY_HALF_WIDTH + sideWidth / 2;
+ const lintelHeight = MANY_LIGHTS_500_HEIGHT - MANY_LIGHTS_500_DOORWAY_HEIGHT;
+ return [
+ part(`partition-${index}-left`, cube, [-sideCenter, MANY_LIGHTS_500_HEIGHT / 2, planeZ], [sideWidth, MANY_LIGHTS_500_HEIGHT, thickness], wall),
+ part(`partition-${index}-right`, cube, [sideCenter, MANY_LIGHTS_500_HEIGHT / 2, planeZ], [sideWidth, MANY_LIGHTS_500_HEIGHT, thickness], wall),
+ part(`partition-${index}-lintel`, cube, [0, MANY_LIGHTS_500_DOORWAY_HEIGHT + lintelHeight / 2, planeZ], [MANY_LIGHTS_500_DOORWAY_HALF_WIDTH * 2, lintelHeight, thickness], wall),
+ ];
+}
+
+// A position along the hall that keeps clear of every partition, so no lamp sits inside a wall.
+function manyLights500FreeZ(random, partitionPlanes) {
+ const clearance = MANY_LIGHTS_500_PARTITION_HALF_THICKNESS + 0.3;
+ for (;;) {
+ const candidate = (random() * 2 - 1) * (MANY_LIGHTS_500_HALF_LENGTH - 0.5);
+ if (partitionPlanes.every(plane => Math.abs(candidate - plane) > clearance)) return candidate;
+ }
+}
+
+function manyLights500Color(random) {
+ return MANY_LIGHTS_500_PALETTE[Math.floor(random() * MANY_LIGHTS_500_PALETTE.length)];
+}
+
+// Small emitters: a quad in the YZ plane facing +x or -x, and a triangle in the XZ plane facing down.
+function manyLights500EmitterGeometry(kind, halfSize) {
+ if (kind === 'quad-right') {
+ return {
+ positions: new Float32Array([0, -halfSize, -halfSize, 0, halfSize, -halfSize, 0, halfSize, halfSize, 0, -halfSize, halfSize]),
+ normals: new Float32Array([1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]),
+ indices: new Uint32Array([0, 1, 2, 0, 2, 3]),
+ };
+ }
+ if (kind === 'quad-left') {
+ return {
+ positions: new Float32Array([0, -halfSize, -halfSize, 0, halfSize, -halfSize, 0, halfSize, halfSize, 0, -halfSize, halfSize]),
+ normals: new Float32Array([-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0]),
+ indices: new Uint32Array([0, 2, 1, 0, 3, 2]),
+ };
+ }
+ return {
+ positions: new Float32Array([-halfSize, 0, -halfSize, halfSize, 0, -halfSize, 0, 0, halfSize]),
+ normals: new Float32Array([0, -1, 0, 0, -1, 0, 0, -1, 0]),
+ indices: new Uint32Array([0, 1, 2]),
+ };
+}
+
+function manyLights500Emitters(random, partitionPlanes) {
+ const quadHalfSize = 0.15;
+ const triangleHalfSize = 0.2;
+ const geometries = {
+ 'quad-right': manyLights500EmitterGeometry('quad-right', quadHalfSize),
+ 'quad-left': manyLights500EmitterGeometry('quad-left', quadHalfSize),
+ triangle: manyLights500EmitterGeometry('triangle', triangleHalfSize),
+ };
+ const emitters = [];
+ for (let index = 0; index < MANY_LIGHTS_500_COUNTS.quad; index++) {
+ const onLeftWall = index % 2 === 0;
+ const wallX = (onLeftWall ? -1 : 1) * (MANY_LIGHTS_500_HALF_WIDTH - 0.05);
+ const position = [wallX, 0.6 + random() * 2.9, manyLights500FreeZ(random, partitionPlanes)];
+ const material = { color: [0.02, 0.02, 0.02], roughness: 0.6, emission: 12, emissiveColor: manyLights500Color(random) };
+ emitters.push(part(`emitter-quad-${index}`, geometries[onLeftWall ? 'quad-right' : 'quad-left'], position, [1, 1, 1], material));
+ }
+ for (let index = 0; index < MANY_LIGHTS_500_COUNTS.triangle; index++) {
+ const position = [(random() * 2 - 1) * (MANY_LIGHTS_500_HALF_WIDTH - 0.5), MANY_LIGHTS_500_HEIGHT - 0.05, manyLights500FreeZ(random, partitionPlanes)];
+ const material = { color: [0.02, 0.02, 0.02], roughness: 0.6, emission: 20, emissiveColor: manyLights500Color(random) };
+ emitters.push(part(`emitter-triangle-${index}`, geometries.triangle, position, [1, 1, 1], material));
+ }
+ return emitters;
+}
+
+function manyLights500PointLights(random, partitionPlanes) {
+ return Array.from({ length: MANY_LIGHTS_500_COUNTS.point }, () => ({
+ position: [(random() * 2 - 1) * (MANY_LIGHTS_500_HALF_WIDTH - 0.4), 2 + random() * 2.6, manyLights500FreeZ(random, partitionPlanes)],
+ color: manyLights500Color(random),
+ intensity: 0.25 + random() * 0.65,
+ radius: 0.05,
+ castShadow: false,
+ }));
+}
+
+function manyLights500SpotLights(random, partitionPlanes) {
+ return Array.from({ length: MANY_LIGHTS_500_COUNTS.spot }, () => ({
+ position: [(random() * 2 - 1) * (MANY_LIGHTS_500_HALF_WIDTH - 0.4), MANY_LIGHTS_500_HEIGHT - 0.3, manyLights500FreeZ(random, partitionPlanes)],
+ direction: [(random() * 2 - 1) * 0.5, -1, (random() * 2 - 1) * 0.5],
+ angle: 0.35 + random() * 0.35,
+ penumbra: 0.3,
+ color: manyLights500Color(random),
+ intensity: 0.6 + random() * 1.2,
+ radius: 0.05,
+ castShadow: false,
+ }));
+}
+
+async function manyLights500Scene() {
+ const lighting = constantLighting(0.01);
+ const cube = box(0, 1);
+ const wall = { color: [0.72, 0.7, 0.66], roughness: 0.9 };
+ const random = seededRandom(MANY_LIGHTS_500_SEED);
+ const partitionPlanes = manyLights500PartitionPlanes();
+ const emitters = manyLights500Emitters(random, partitionPlanes);
+ const pointLights = manyLights500PointLights(random, partitionPlanes);
+ const spotLights = manyLights500SpotLights(random, partitionPlanes);
+ const parts = [
+ ...manyLights500Shell(cube, wall),
+ ...partitionPlanes.flatMap((planeZ, index) => manyLights500Partition(cube, wall, planeZ, index)),
+ part('pillar', cube, [-1.2, 0.8, 21], [0.5, 1.6, 0.5], { color: [0.6, 0.6, 0.65], roughness: 0.5 }),
+ part('ball', sphere(48, 32), [0.8, 0.5, 15], [0.5, 0.5, 0.5], { color: [0.8, 0.25, 0.2], roughness: 0.3 }),
+ part('block', cube, [0.7, 0.4, 9], [0.8, 0.8, 0.8], { color: [0.2, 0.45, 0.75], roughness: 0.6 }),
+ ...emitters,
+ ];
+ return {
+ parts, blenderWorld: lighting.blenderWorld,
+ environment: environment(lighting, { pointLights, spotLights }),
+ camera: { eye: [0.3, 1.7, 29], target: [0, 1.9, -20], up: [0, 1, 0], fov: 60, near: 0.05, far: 80 },
+ };
+}
+
export const REFERENCE_SCENES = {
subsurface: () => subsurfaceScene(1),
'subsurface-off': () => subsurfaceScene(0),
@@ -868,6 +1024,9 @@ export const REFERENCE_SCENES = {
};
},
+ // Issue #895: 500 small lights in a hall of ten cells (point, spot, emissive quads and triangles), for the light tree gate.
+ 'many-lights-500': manyLights500Scene,
+
// A street 140 m long under the sun. A single shadow map has to cover the whole street, so its texels are about 8 cm wide;
// cascades give the cars and lamp posts near the camera 1 to 2 cm texels. harness.js draws it with one map by default and
// with cascades when run.mjs gets --cascades <count>.
diff --git a/changes/895-many-lights.md b/changes/895-many-lights.md
new file mode 100644
index 00000000..0d11c383
--- /dev/null
+++ b/changes/895-many-lights.md
@@ -0,0 +1 @@
+- Add the parity scene `many-lights-500` (a 60 m hall with 500 small point, spot and emissive lights) and let the parity harness build scenes with more than 32 analytic lights, to measure the Cook light tree against the flat pick and against Cycles 5.3 (#895)
diff --git a/docs/cycles-parity.md b/docs/cycles-parity.md
index 42e183b7..030cfe49 100644
--- a/docs/cycles-parity.md
+++ b/docs/cycles-parity.md
@@ -370,6 +370,27 @@ The `cycles` kernel picks one emitter per surface bounce by walking the Cycles l
On `lit-interior` the tree costs 30 % more per sample and removes 37 % of the noise, so it wins by about 17 % at equal time (inferred from relMSE falling as 1/samples). The probe is a GPU test entry, not a Rust port; `tests/native-light-tree-kernel.test.js` repeats the three checks on a small scene.
+### Seventh scene: `many-lights-500` (light tree gate, #895)
+
+A 60 m hall cut into ten cells by partition walls with one doorway each, lit by 500 small lights placed from a fixed seed: 200 point lights, 150 spot lights, 100 emissive quads on the side walls and 50 emissive triangles on the ceiling (350 analytic lights and 200 emissive triangles). A surface sees the lights of its own cell and, through the doorways, a few of the next ones, so most of the 500 are far or hidden from any shading point and a flat pick wastes almost every sample. It is defined in [`bench/reference/scenes.js`](../bench/reference/scenes.js) as `many-lights-500` (the id `many-lights` is the 24-lamp corridor of issue #623), exported by the same `export-scene.mjs` path as the other six, and built by `render-cycles.py` from the same JSON, so both renderers read one scene. Export, a Cycles reference and a Cook run are the same commands as for the six, with `--scene many-lights-500`; `run-m1-parity.py` lists it as the seventh scene.
+
+`buildTraceScene` takes at most 32 analytic lights (`MAX_TRACE_LIGHTS`, one light-link mask bit each). The parity harness has no light links, so [`trace-scene-many-lights.mjs`](../bench/cycles-parity/trace-scene-many-lights.mjs) builds the lights in chunks of 32 and joins the lists, giving every light the "all groups" bit; the core limit and the legacy kernel are unchanged. `tests/cycles-parity-many-lights.test.js` checks the counts, the fixed seed and the join.
+
+Cycles 5.3 reference: Blender 5.3.0 OptiX, 960x540, 4096 spp, seeds 1 and 2, 8 bounces, light tree on (the Cycles default, `use_light_tree = true` in the render report), box filter 1, no denoiser, no clamps. The seed 1 versus seed 2 noise floor is relMSE 0.000791, per-channel mean ratio 1.0000 / 1.0000 / 1.0000, tiles 0.9995 to 1.0009, FLIP LDR 0.0173 `[measured]`. Sampling took 732 s (seed 1) and 1391 s (seed 2) with other jobs on the card, so they are not a speed figure.
+
+**Gate 1, the tree against the flat pick** (Cook `cycles` kernel, seed 1, 8 bounces, sampling seconds from the report, three interleaved rounds, relMSE against the seed 1 reference; the three runs of a rung give the same image, so relMSE has no range) `[measured]`:
+
+| spp | uniform relMSE | tree relMSE | uniform / tree error | uniform sampling s | tree sampling s | tree / uniform time |
+| ---: | ---: | ---: | ---: | --- | --- | ---: |
+| 16 | 0.37364 | 0.12644 | 2.95 | 1.344 [1.331-1.348] | 1.558 [1.555-1.559] | 1.16 |
+| 64 | 0.08809 | 0.03041 | 2.90 | 5.114 [5.095-5.160] | 5.991 [5.968-6.074] | 1.17 |
+| 256 | 0.02032 | 0.00766 | 2.65 | 20.474 [20.447-21.442] | 24.159 [24.015-24.450] | 1.18 |
+| 1024 | 0.00442 | 0.00218 | 2.03 | 81.958 [81.881-82.966] | 96.484 [95.916-96.609] | 1.18 |
+
+Time to equal error, from the two ladders on a log-log interpolation of time against relMSE (both curves cover 0.00442 to 0.126, so no extrapolation): at relMSE 0.0237 (geometric middle of that range) the flat pick needs 17.7 s and the tree 7.7 s, 2.30 times faster; at 0.00664 (1.5 times the lowest error both reach) 56.7 s and 28.3 s, 2.00 times faster; at 0.0304 (the tree at 64 spp) 14.0 s and 6.0 s, 2.33 times faster `[measured and interpolated]`. **The gate is met**: the tree reaches equal error in less than half the time. The tree costs 16 to 18 % more per sample here and removes 51 to 66 % of the noise per sample. At 1024 spp both curves approach the floor set by the 4096 spp reference's own noise (about half of the 0.000791 seed-to-seed figure), which is why the ratio falls toward 2.
+
+**Gate 2, parity with Cycles** (Cook `cycles` kernel, tree, 4096 spp, seed 1, against the Cycles seed 1 reference, `run-m1-parity.py`) `[measured]`: per-channel mean ratio 1.0009 / 1.0010 / 1.0009 (limit 2 %); 64 of 64 tiles within 5 %, tile ratios 0.9999 to 1.0034; FLIP LDR 0.0178 against a ceiling of 0.0373; relMSE 0.000832 against the 0.000791 noise floor. All gates pass, and nothing in the kernel or the scene export was changed to get there: light types, spot cone and units already matched (spot `angle`, `penumbra` and the 4 pi and pi watt factors are the ones the other scenes use; emissive quads and triangles go through Principled emission strength on both sides, as the kernel emits). Cook took 386.6 s of sampling for 4096 spp with other jobs on the card.
+
## Reproducible Commands
Run from the repository root in PowerShell. Set `$env:BLENDER_BIN` if Blender 5.3 is installed outside `C:\Program Files\Blender Foundation\Blender 5.3\blender.exe`, and pass an untracked `<out-dir>` (such as `artifacts/cycles-parity`) so binary `.exr` and `.npy` files stay out of Git:
@@ -385,8 +406,8 @@ $OutDir = "artifacts/cycles-parity"
# 2. Build packages/render-native if build/render-native.node is not present yet
node packages/render-native/build.mjs
-# 3. Export all 6 M1 reference scenes and verify byte-identical round-trip packing
-foreach ($Scene in @("spot-area", "light-linking", "metal", "glass", "outdoor-hdri", "lit-interior")) {
+# 3. Export all 7 reference scenes (the six M1 scenes and many-lights-500) and verify byte-identical round-trip packing
+foreach ($Scene in @("spot-area", "light-linking", "metal", "glass", "outdoor-hdri", "lit-interior", "many-lights-500")) {
node bench/cycles-parity/export-scene.mjs $OutDir --scene $Scene
}
diff --git a/tests/cycles-parity-many-lights.test.js b/tests/cycles-parity-many-lights.test.js
new file mode 100644
index 00000000..1859aa8e
--- /dev/null
+++ b/tests/cycles-parity-many-lights.test.js
@@ -0,0 +1,56 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { createCamera } from '../src/path-tracer.js';
+import * as packModule from '../src/path-tracer-gpu-pack.js';
+import { MAX_TRACE_LIGHTS } from '../src/path-tracer-scene.js';
+import { buildNativeInputV2 } from '../packages/render-native/index.mjs';
+import { loadReferenceScene } from '../bench/cycles-parity/scene-bridge.mjs';
+import { buildTraceSceneWithManyLights } from '../bench/cycles-parity/trace-scene-many-lights.mjs';
+
+const SCENE_ID = 'many-lights-500';
+const POINT_LIGHT_COUNT = 200;
+const SPOT_LIGHT_COUNT = 150;
+const EMISSIVE_QUAD_COUNT = 100;
+const EMISSIVE_TRIANGLE_COUNT = 50;
+
+test('many-lights-500 holds 500 lights placed from a fixed seed', async () => {
+ const first = await loadReferenceScene(SCENE_ID);
+ const second = await loadReferenceScene(SCENE_ID);
+ const { pointLights, spotLights } = first.rawScene.environment;
+ const emitters = first.rawScene.parts.filter(entry => entry.material.emission > 0);
+ assert.equal(pointLights.length, POINT_LIGHT_COUNT);
+ assert.equal(spotLights.length, SPOT_LIGHT_COUNT);
+ assert.equal(emitters.length, EMISSIVE_QUAD_COUNT + EMISSIVE_TRIANGLE_COUNT);
+ assert.deepEqual(second.rawScene.environment.pointLights, pointLights);
+ assert.deepEqual(second.rawScene.environment.spotLights, spotLights);
+ assert.deepEqual(second.rawScene.parts.map(entry => [...entry.matrix]), first.rawScene.parts.map(entry => [...entry.matrix]));
+});
+
+test('buildTraceSceneWithManyLights joins chunks beyond the trace light limit and leaves small scenes alone', async () => {
+ const { traceSource } = await loadReferenceScene(SCENE_ID);
+ const traceScene = buildTraceSceneWithManyLights(traceSource.parts, traceSource.environment);
+ assert.ok(POINT_LIGHT_COUNT + SPOT_LIGHT_COUNT > MAX_TRACE_LIGHTS);
+ assert.equal(traceScene.lights.points.length, POINT_LIGHT_COUNT);
+ assert.equal(traceScene.lights.spots.length, SPOT_LIGHT_COUNT);
+ assert.equal(traceScene.lights.areas.length, 0);
+ assert.deepEqual(traceScene.lights.points[199].position, traceSource.environment.pointLights[199].position);
+ assert.deepEqual(traceScene.lights.spots[149].direction.map(value => Number(value.toFixed(6))), (() => {
+ const direction = traceSource.environment.spotLights[149].direction;
+ const length = Math.hypot(...direction);
+ return direction.map(value => Number((value / length).toFixed(6)));
+ })());
+ const smallEnvironment = { ...traceSource.environment, pointLights: traceSource.environment.pointLights.slice(0, 3), spotLights: [] };
+ const smallScene = buildTraceSceneWithManyLights(traceSource.parts, smallEnvironment);
+ assert.equal(smallScene.lights.points.length, 3);
+ assert.equal(smallScene.lights.points[2].bit, 3);
+});
+
+test('many-lights-500 packs for the cycles kernel with a light tree over 350 lights and 200 emissive triangles', async () => {
+ const { traceSource } = await loadReferenceScene(SCENE_ID);
+ const traceScene = buildTraceSceneWithManyLights(traceSource.parts, traceSource.environment);
+ const camera = createCamera(traceSource.camera, 64, 36);
+ const nativeInput = buildNativeInputV2(traceScene, camera, { width: 64, height: 36, bounces: 8, seed: 1, parts: traceSource.parts }, packModule);
+ assert.equal(nativeInput.lightCount, POINT_LIGHT_COUNT + SPOT_LIGHT_COUNT);
+ assert.equal(nativeInput.emissiveTriangleCount, EMISSIVE_QUAD_COUNT * 2 + EMISSIVE_TRIANGLE_COUNT);
+ assert.ok(nativeInput.lightTreeNodeCount > 1);
+});