3D engine that AI agents drive
Record the Cook parity and equal-error runs repeated on an NVIDIA A10G (#895)
Nguyen Xuan An12714c8bd765
diff --git a/changes/895-light-tree.md b/changes/895-light-tree.mdindex ffab9a16..e5f34b3b 100644--- a/changes/895-light-tree.md+++ b/changes/895-light-tree.md@@ -1 +1,2 @@- The Cook `cycles` kernel picks next event lights through the Cycles light tree (`lightSampling`: `auto`, `tree` or `uniform`; `--light-sampling` in `trace-native.mjs`), built by the host over analytic lights and emissive triangles (#895)+- A packed FORMAT-v2 input without light tree emitter tables is rejected when it asks for the tree (`traceNative` throws for `lightSampling: 'tree'` and uses the flat pick for `auto`), `filterType` and `filterWidth` now apply to an already packed input, and `buildLightTree` throws above 32 distant lights (#895)diff --git a/docs/cycles-parity.md b/docs/cycles-parity.mdindex 75fd9a5e..afa7f0b7 100644--- a/docs/cycles-parity.md+++ b/docs/cycles-parity.md@@ -389,7 +389,7 @@ Cycles 5.3 reference: Blender 5.3.0 OptiX, 960x540, 4096 spp, seeds 1 and 2, 8 bTime 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.+**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. These RTX 3060 figures come from before the light tree importance fix described under "Repeat on Modal" below; the Modal run reproduces the same gate values.### Equal-error time against Cycles@@ -425,11 +425,43 @@ What the table says:- `lit-interior`: at equal samples Cook has 7 to 15 % lower error (0.00274 against 0.00310 at 256 spp, 0.000656 against 0.000771 at 1024) and takes 5 to 14 % less time on the medians from 64 spp up, so it reaches equal error about 1.2 times faster. The 16 and 64 spp Cycles times carry up to 5 s of noise in their ranges, probably start-up and load from other sessions on the shared card `[inferred]`; the cost per sample of both is close to 24 to 25 ms at 1024 spp. The earlier figure of 11.4 s for Cycles at 256 spp differs from the 5.7 to 8.3 s measured here; the card was shared in both cases.- `many-lights-500`: Cook is slightly worse. The error is 9 to 10 % higher at equal samples at every rung, while the time per sample is between 1.5 % higher and 8 % lower than Cycles (16 spp +1.5 %, 64 spp -1 %, 256 spp -8 %, 1024 spp -3 %), so Cook needs about 3 to 6 % more time for the same error. This is the one scene where Cook is slower. The gap is small next to the run-to-run range of the Cycles 1024 spp time (93 to 145 s), so read it as a tie to slightly behind, not a clear loss. Where the 9 % comes from (differences between this tree and the Cycles tree, such as the double-sided emissive-triangle cone, or the sampler dimensions) was not isolated.- `metal`: no equal-error time can be given. The error looks dominated by a few very bright paths rather than by smooth noise `[inferred]`. The reference's own seed-to-seed relMSE is 0.0034, higher than both targets, and neither ladder falls monotonically (Cook 0.0045, 0.281, 0.0181, 0.0083; Cycles 0.628, 0.0042, 0.0750, 0.0059): a single firefly decides a rung. Per sample the two run at the same speed (9.5 s against 9.3 s at 1024 spp). Parity of the converged image is separate and passes (4096 spp relMSE 0.00271 against the 0.0034 noise floor).+- The error gap on `many-lights-500` survived the review fix to `ltr_importance` (maximum importance now divided by the squared nearest distance and minimum by the squared farthest, as in Cycles): see "Repeat on Modal".- Nothing here changes a gate or retunes a kernel default. The comparison is one seed per renderer and one Cycles reference; the error of a single rung moves with the seed, and the interpolation uses four points per curve, so ratios under about 10 % are not resolved.+### Repeat on Modal after the review fixes (NVIDIA A10G, 2026-11-12)++The review of #895 found that `ltr_importance` had the distances swapped against `kernel/light/tree.h` (the maximum importance is divided by the squared minimum distance, the minimum importance by the squared maximum distance). That is fixed, and the ladders were measured again on a Modal container instead of the RTX 3060: one NVIDIA A10G (driver 610.57, Vulkan 1.4 with ray queries; Cook picked the `NVIDIA A10G` adapter, not llvmpipe), Blender 5.3.0 Cycles on OptiX on the same card, one container for the whole run so the timings are comparable, seed 3 for both, three rounds in which each rung runs Cook then Cycles. Error is relMSE against the same 4096 spp seed 1 Cycles reference `[measured]`; seconds are `samplingSeconds` with the range of three runs. The harness is `modal/app.py` outside the repository (Modal functions `build_and_test`, `measure`, `parity`).++| scene | spp | Cook relMSE | Cycles relMSE | Cook s | Cycles s |+| --- | ---: | ---: | ---: | --- | --- |+| `lit-interior` | 16 | 0.06529 | 0.07024 | 0.196 [0.177-0.345] | 0.229 [0.227-0.230] |+| `lit-interior` | 64 | 0.01283 | 0.01483 | 0.651 [0.629-0.884] | 0.828 [0.826-0.830] |+| `lit-interior` | 256 | 0.002741 | 0.003103 | 2.500 [2.498-2.518] | 3.250 [3.249-3.257] |+| `lit-interior` | 1024 | 0.000656 | 0.000771 | 10.04 [10.04-10.05] | 13.07 [13.03-13.07] |+| `many-lights-500` | 16 | 0.1276 | 0.1173 | 0.663 [0.662-0.919] | 0.734 [0.734-0.737] |+| `many-lights-500` | 64 | 0.03059 | 0.02793 | 2.487 [2.483-2.499] | 2.795 [2.795-2.804] |+| `many-lights-500` | 256 | 0.007630 | 0.006984 | 9.843 [9.839-9.856] | 11.07 [11.04-11.12] |+| `many-lights-500` | 1024 | 0.002174 | 0.002001 | 39.32 [39.32-39.36] | 44.20 [44.06-44.21] |++Time to equal error, same interpolation as above `[measured and interpolated]`:++| scene | relMSE target | Cook s | Cycles s | Cook relative to Cycles |+| --- | ---: | ---: | ---: | ---: |+| `lit-interior` | 0.005 | 1.48 | 2.14 | 1.45x faster |+| `lit-interior` | 0.001 | 6.66 | 10.08 | 1.51x faster |+| `lit-interior` | 0.000771 (lowest both reach) | 8.58 | 13.07 | 1.52x faster |+| `many-lights-500` | 0.005 | 15.69 | 16.02 | 1.02x faster |+| `many-lights-500` | 0.002174 (lowest both reach) | 39.32 | 40.32 | 1.03x faster |++- Against the RTX 3060 figures, the Cook relMSE moved by at most 0.1 percent on `lit-interior` and 0.1 percent on `many-lights-500` (for example 0.127595 against 0.127697 at 16 spp and 0.002174 against 0.002175 at 1024 spp): the importance fix changes which emitter is picked, but not by much on these two scenes, and it does not close the 9 percent error gap to Cycles on `many-lights-500` (0.002174 against 0.002001 at 1024 spp). That gap is still not isolated.+- Both renderers are 2.0 to 2.5 times faster on the A10G than on the 3060 in these runs (Cook 2.4 to 2.5 times, Cycles 2.0 to 2.2 times), and the job ran alone in its container, so the Cycles ranges are tight (no 5 s outliers). On this card Cook is 1.45 to 1.52 times faster at equal error on `lit-interior` (1.2 times on the 3060) and level with Cycles on `many-lights-500` (3 to 6 percent slower on the 3060). The ratios depend on the GPU and on load `[inferred]`; no claim is made about other cards.+- The tree against the flat pick on `many-lights-500` (Cook, seed 3, A10G): error ratio uniform / tree 2.98, 2.86, 2.67 and 2.03 at 16, 64, 256 and 1024 spp, tree time 1.30 to 1.32 times the flat pick per sample (1.16 to 1.18 on the 3060). Time to equal error, tree against flat: 2.03 times faster at relMSE 0.0237, 2.05 at 0.0304, 1.80 at 0.00664 and 1.70 at 0.005 (the flat pick's 1024 spp run reaches only 0.00442, so it was run once there). The "less than half the time" reading of the gate holds at the larger errors and is 1.7 to 1.8 times near 0.005 on this card; the RTX 3060 figures above (2.0 to 2.3 times) were measured before the fix and on a different card.+- 4096 spp parity of the `cycles` kernel at its defaults, seven scenes, seed 1, on a Modal NVIDIA A10: 7 of 7 pass, with the same mean ratios, tile counts, FLIP LDR and relMSE as the table below to the digits shown (worst per-channel mean ratio 1.0035 on `lit-interior`, against the 2 percent limit). Cook sampling seconds at 4096 spp: `spot-area` 24.3, `light-linking` 27.2, `metal` 17.8, `glass` 30.1, `outdoor-hdri` 9.4, `lit-interior` 49.4, `many-lights-500` 205.7.+- Checks on the same Modal container: `cargo test` 25 passed, 0 failed (no GPU needed; the napi symbols are left unresolved at link time on Linux, and the one new test covers a tree flag without emitter tables); `node --test tests/native-*.test.js tests/cook-kernel-parity.test.js tests/cycles-parity-many-lights.test.js` 76 tests, 75 passed, 1 skipped, 0 failed; lobe probes 22 of 22 and sphere furnace scenes 6 of 6 at 512 spp.+### Final gates for #895-Release addon built; `cargo test --target x86_64-pc-windows-msvc`: 24 pass; `node --test tests/native-*.test.js`: 53 pass; lobe probes 22 of 22 and sphere furnace scenes 6 of 6 pass (512 spp); 4096 spp parity of the `cycles` kernel at its defaults on all seven scenes, 7 of 7 pass `[measured]`:+Release addon built; on the RTX 3060 `cargo test --target x86_64-pc-windows-msvc`: 24 pass; `node --test tests/native-*.test.js`: 53 pass; lobe probes 22 of 22 and sphere furnace scenes 6 of 6 pass (512 spp); 4096 spp parity of the `cycles` kernel at its defaults on all seven scenes, 7 of 7 pass `[measured]`. The Modal run after the review fixes gives the same table (see above) with 25 cargo tests and 75 node tests passing:| scene | mean ratio (R / G / B) | tiles within 5 % | FLIP LDR (ceiling) | relMSE (noise floor) || --- | --- | ---: | --- | --- |diff --git a/docs/path-tracer.md b/docs/path-tracer.mdindex 8d4e1f58..bf667775 100644--- a/docs/path-tracer.md+++ b/docs/path-tracer.md@@ -61,7 +61,7 @@ The same scene and options give the same bytes. Random numbers come from a hash`backend: "native"` is an agent render job setting, not a `renderPathTracedImage` option: [`packages/render-native`](../packages/render-native/README.md) runs the same kernel on Vulkan ray queries. On the demo room at 1920 by 1080 and 64 samples an MCP image job took 25 to 35 s with native against 58 s with gpu (trace alone 2.7 to 3.1 s against 38.8 s; the rest is the page export, the wavelet denoise and the PNG), FLIP 0.0047 between them. A missing package, binary or adapter falls back to gpu with a note in the job report. `maxSeconds` is honoured: past it the native trace stops and the job fails, as on the other backends, instead of rerunning on gpu; `noiseThreshold` is too (the kernel applies the CPU stop rule per pixel, so `averageSamples` falls below `samples`; the `gpu` and `webgpu` backends still draw every sample). Volume fog and subsurface scattering are in the native kernel, compiled in only for a scene that uses them; `bench/native-tracer/scenes.mjs` draws the reference scenes on native and on the WebGPU megakernel for a FLIP comparison. Alpha cutouts (`alphaMode: "mask"`) work: cutout triangles are their own geometry and the kernel confirms a hit only when the colour map alpha reaches the cutoff, with no step limit. In a dense cutout scene native is the more correct tracer: the CPU and WebGL2 shadow ray takes the first hit the traversal finds, not the nearest, and when that hit is a transparent texel it skips occluders it had not reached yet, so those backends leak light (300 leaf cards: native vs cpu FLIP 0.043, and 0.0002 once the CPU shadow ray takes the nearest hit; bench/native-tracer/cutout-bisect.mjs shows one card agrees exactly). The steps before: up to 8 layers deep (`bench/native-tracer/cutout.mjs`: 300 masked leaf cards trace in 0.1 s at 640 by 360 and 64 samples). See [agent-render-jobs.md](agent-render-jobs.md#path-tracer).-The native package has two kernels. The default `legacy` kernel is the one described above. Pass `kernel: 'cycles'` to `traceNative` (or set `JS67_NATIVE_KERNEL=cycles`) for the opt-in kernel that follows Blender Cycles 5.3: on six reference scenes at 4096 samples it matches Cycles within 1 percent of the mean and within 1.8 percent on every 8 by 8 tile, and its error at 256 samples on `lit-interior` is 0.0027 against 0.0198 for the legacy kernel (Cycles: 0.0031). Three options follow Cycles: `sampler` (`'sobol-burley'` blue-noise pattern by default, `'white'` for the hash sampler), `filterType` and `filterWidth` (`'box'` width 1 by default, `'gaussian'` and `'blackman-harris'` from the Cycles filter table) and `lightSampling` (`'auto'`, `'tree'` or `'uniform'`; the Cycles light tree is used from more than one emitter). It is 2.3 to 3.1 times slower per sample than the legacy kernel before the light tree (0.92 s against 2.86 s on `lit-interior`, 960 by 540, 256 samples), and the light tree adds 16 to 30 percent per sample on top; at equal error against Cycles 5.3 it is 1.2 times faster on `lit-interior` and 3 to 6 percent slower on `many-lights-500`. Tables, method and the equal-error caveat are in [cycles-parity.md](cycles-parity.md#selecting-the-kernel).+The native package has two kernels. The default `legacy` kernel is the one described above. Pass `kernel: 'cycles'` to `traceNative` (or set `JS67_NATIVE_KERNEL=cycles`) for the opt-in kernel that follows Blender Cycles 5.3: on six reference scenes at 4096 samples it matches Cycles within 1 percent of the mean and within 1.8 percent on every 8 by 8 tile, and its error at 256 samples on `lit-interior` is 0.0027 against 0.0198 for the legacy kernel (Cycles: 0.0031). Three options follow Cycles: `sampler` (`'sobol-burley'` blue-noise pattern by default, `'white'` for the hash sampler), `filterType` and `filterWidth` (`'box'` width 1 by default, `'gaussian'` and `'blackman-harris'` from the Cycles filter table) and `lightSampling` (`'auto'`, `'tree'` or `'uniform'`; the Cycles light tree is used from more than one emitter). It is 2.3 to 3.1 times slower per sample than the legacy kernel before the light tree (0.92 s against 2.86 s on `lit-interior`, 960 by 540, 256 samples), and the light tree adds 16 to 32 percent per sample on top; at equal error against Cycles 5.3 it is 1.2 times faster on `lit-interior` and 3 to 6 percent slower on `many-lights-500` on an RTX 3060, and 1.5 times faster and level (1.02 to 1.03 times) on an NVIDIA A10G. Tables, method and the equal-error caveat are in [cycles-parity.md](cycles-parity.md#selecting-the-kernel).`backend: 'cpu'` (the default) is the reference: it reuses `src/triangle-bvh.js` and `src/ray-query.js`, runs on workers in the job page next to the scene, needs no GPU, and gives the same bytes for the same scene and options. It measured 25 to 238 seconds for a 960 by 540 still at 32 samples on a Ryzen 5 5600X with four threads, while another session also loaded the machine (table in `bench/reference/README.md`).diff --git a/packages/render-native/FORMAT-v2.md b/packages/render-native/FORMAT-v2.mdindex e77cd2a5..70e244fc 100644--- a/packages/render-native/FORMAT-v2.md+++ b/packages/render-native/FORMAT-v2.md@@ -310,7 +310,7 @@ Light tree revision 1 (`LIGHT_TREE_REVISION` in `light-tree-v2.mjs`; `schemaVers- **Shape.** Node 0 is the root and children are written in pre-order, so a left child is its parent index plus 1. With sun lights and local emitters (analytic lights other than the sun, plus emissive triangles) the root is an inner node: left child the local tree, right child one distant leaf holding every sun light, as Cycles does. With only one kind the root is that tree or that leaf. The environment is not an emitter of this tree. With no emitters `light_tree_node_count` is `0` and the buffer holds one zeroed node.- **Split.** Top-down surface area orientation heuristic: 12 centroid buckets per axis, cost `energy * box area * cone measure` (`area == 0` uses the box diagonal), regularized by `max_extent / extent`. A node splits while a split is cheaper than a leaf or while it holds more than 8 emitters; a split with no cheaper cut falls back to the middle of the list. From depth 24 on nodes split by count, which bounds the depth at 46 so a 64-bit trail always fits. Leaves list their emitters in unified-index order. Emitters are sorted by centroid with the unified index as tie break, so the output is deterministic.-- **Counts.** At most `4194304` emitters (`LIGHT_TREE_MAX_EMITTERS`) and `65535` emitters per leaf.+- **Counts.** At most `4194304` emitters (`LIGHT_TREE_MAX_EMITTERS`) and `32` emitters per leaf (`LIGHT_TREE_KERNEL_MAX_LEAF_EMITTERS`, the most the kernel reads from one leaf; only the distant leaf can hold more than `8`, so `buildLightTree` throws for more than `32` sun or distant lights).- **Node fields.** `energy` is the sum of the children's energies (leaf: the sum of its emitters). `bounds_*` is the union of the emitters' boxes with energy above zero; a distant leaf and a zero-energy node store `0`. `theta_o`, `cone_axis`, `theta_e` are the merged bounding cone (Cycles `merge`). Internal node: `left_child >= 0`, `right_or_first_emitter` is the right child. Leaf: `left_child == -1`, `right_or_first_emitter` is the first slot in `light_tree_emitters` and bits `0..15` of `emitter_count_and_kind` are the emitter count. Higher bits of that word: bit `16` the subtree has an emissive triangle, bit `17` it is the distant leaf, bit `18` it has an analytic light (internal nodes OR their children's bits, so a mixed leaf sets both 16 and 18; the per-emitter kind in section 7b is authoritative).- **Emitter energy.** Emissive triangle `area * mean(|radiance|)`. Area light `mean(|radiance|) * area`. Point and spot `mean(|radiance|)` for a delta light, `mean(|radiance|) * pi * radius^2` otherwise. Sun `mean(|radiance|) * solid_angle`.- **Emitter cone.** Point `axis = direction, theta_o = pi, theta_e = pi/2`. Spot `theta_o = 0, theta_e = min(pi/2, acos(cos_half_angle))`. Area `theta_o = 0, theta_e = atan(tan_half_spread)` (`pi/2` for the default spread). Sun `theta_o = 0, theta_e = acos(cos_half_angle)`. Emissive triangle: the kernel emits from both sides, so the double-sided cone of Cycles, an in-plane axis with `theta_o = theta_e = pi/2`.