Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Measure shared material records, raise the job instance bound to 1,000,000 and fix the unshared-pack test (#1100)

Nguyen Xuan An71bb26468a4d
diff --git a/bench/cycles-parity/native-export-size-check.mjs b/bench/cycles-parity/native-export-size-check.mjs
index 2e46bec4..1e68378e 100644
--- a/bench/cycles-parity/native-export-size-check.mjs
+++ b/bench/cycles-parity/native-export-size-check.mjs
@@ -1,6 +1,6 @@
// Job-level check of the export size rule (#1055): about 1,500,000 unique triangles with 150,000 instances stay inside both job bounds but
// not inside one message, so the export must refuse with the new RangeError; the same triangles with 20,000 instances must export.
-// NODE_OPTIONS=--max-old-space-size=16384 node bench/cycles-parity/native-export-size-check.mjs
+// NODE_OPTIONS=--max-old-space-size=16384 node bench/cycles-parity/native-export-size-check.mjs [card counts]
import * as pack from '../../src/path-tracer-gpu-pack.js';
import { buildNativeInput } from '../../packages/render-native/pack-input.mjs';
import { buildFlattenedJobNativeInput, buildJobNativeInput } from '../../packages/render-native/job-input.mjs';
@@ -22,4 +22,5 @@ function exportWithCards(cardCount) {
}
}
-for (const cardCount of [20_000, 150_000]) console.log(`NATIVE_EXPORT_SIZE ${JSON.stringify(exportWithCards(cardCount))}`);
+const requestedCounts = process.argv.slice(2).map(Number);
+for (const cardCount of requestedCounts.length > 0 ? requestedCounts : [20_000, 150_000]) console.log(`NATIVE_EXPORT_SIZE ${JSON.stringify(exportWithCards(cardCount))}`);
diff --git a/bench/cycles-parity/native-shared-record-identity.mjs b/bench/cycles-parity/native-shared-record-identity.mjs
index 69466a4e..27a57004 100644
--- a/bench/cycles-parity/native-shared-record-identity.mjs
+++ b/bench/cycles-parity/native-shared-record-identity.mjs
@@ -103,4 +103,48 @@ for (const [sceneName, buildParts] of Object.entries(sceneBuilders)) {
results[`${sceneName} explicit-override`] = { hash: `refused: ${error.message}` };
}
}
+// An eight-frame sequence through one native session, as a stills job traces it: the camera moves every frame and the roughness of
+// card 2 follows a track. In "equal-start" card 2 starts equal to the other grey cards (so it shares their record at frame 0 and stops
+// sharing at frame 1: that frame takes a new session), in "own-record" it differs from every other card from the start (every later
+// frame is an update). Each frame is traced with a session and one-shot; the hashes of the two must be equal.
+const { createNativeSequence } = await import('../../scripts/agent-native-sequence.mjs');
+const SEQUENCE_FRAMES = 8;
+
+function sequenceParts(frame, startsEqual) {
+ const parts = [GROUND];
+ for (let index = 0; index < 8; index++) {
+ const animated = index === 2;
+ const roughness = animated ? (startsEqual ? 0.7 - frame * 0.08 : 0.65 - frame * 0.05) : 0.7;
+ parts.push(placed(`card-${index}`, index, { color: [0.6, 0.6, 0.6], roughness }));
+ }
+ return parts;
+}
+
+async function traceSequence(startsEqual, useSession) {
+ const sequence = useSession ? createNativeSequence({ tracer }) : undefined;
+ const hashes = [];
+ const routes = [];
+ const materialRecords = [];
+ for (let frame = 0; frame < SEQUENCE_FRAMES; frame++) {
+ const frameCamera = createCamera({ eye: [frame * 0.3 - 1, 3, 9], target: [0, 0.5, 0], fov: 50 }, WIDTH, HEIGHT);
+ const exported = exportNativeSceneTables({ parts: sequenceParts(frame, startsEqual), environment: ENVIRONMENT, camera: frameCamera, options: PACK_OPTIONS, kernel: 'cycles', instancing: true, modules });
+ const traced = await traceFrameNative({ tracer, exported: { ...exported, ignored: [] }, width: WIDTH, height: HEIGHT, samples: SAMPLES, bounces: PACK_OPTIONS.bounces, seed: 1, denoise: 'none', kernel: 'cycles', sequence });
+ hashes.push(sha256(Buffer.from(traced.data, 'base64')));
+ routes.push(traced.report?.nativeSession?.path ?? 'one-shot');
+ materialRecords.push(exported.counts?.materialCount ?? null);
+ }
+ sequence?.close();
+ return { hashes, routes, materialRecords };
+}
+
+for (const [name, startsEqual] of [['equal-start', true], ['own-record', false]]) {
+ try {
+ const withSession = await traceSequence(startsEqual, true);
+ const oneShot = await traceSequence(startsEqual, false);
+ results[`sequence ${name}`] = { hash: sha256(withSession.hashes.join(',')), sessionEqualsOneShot: withSession.hashes.every((hash, index) => hash === oneShot.hashes[index]),
+ routes: withSession.routes, materialRecords: withSession.materialRecords };
+ } catch (error) {
+ results[`sequence ${name}`] = { hash: `refused: ${error.message}` };
+ }
+}
console.log(`SHARED_RECORD_IDENTITY ${JSON.stringify(results, null, 1)}`);
diff --git a/changes/1100.md b/changes/1100.md
index a7ec8e7b..fb6771c3 100644
--- a/changes/1100.md
+++ b/changes/1100.md
@@ -1 +1 @@
-- Copies of a mesh that share a material (or have equal ones) now share one 256-byte material record in the native path tracer's input, so each copy costs about 160 bytes instead of 416 and a job fits more instances (#1100)
+- Parts of a native path-traced scene whose materials are equal now share one material record, so a copy of a mesh costs 160 bytes in the input instead of 416 and a native render job takes up to 1,000,000 instances (#1100)
diff --git a/docs/cycles-parity.md b/docs/cycles-parity.md
index 8c5bc760..9e5bda39 100644
--- a/docs/cycles-parity.md
+++ b/docs/cycles-parity.md
@@ -1437,7 +1437,7 @@ Same picture: the 1,002,528 triangle scene (four meshes, two of them shared by a
## Instance bound of a native scene (#1055)
-`buildTraceScene` bounds an instanced scene at 65,536 instances (one per part) for the CPU and WebGL tracers. A scene built for the native tracer asks for another bound with `maximumInstances`, an integer from 1 to `MAX_NATIVE_TRACE_INSTANCES` (1,000,000). A render job on the Cycles-matching kernel passes 500,000 (`NATIVE_JOB_MAXIMUM_INSTANCES` in `scripts/native-bake-page.js`); the legacy kernel never instances, and the CPU and WebGL tracers keep 65,536 and its error text. The option is separate from `maximumTriangles`: the unique and total triangle bounds apply as before, so copies of a large mesh still hit the total bound (four times `maximumTriangles`) long before the instance bound.
+`buildTraceScene` bounds an instanced scene at 65,536 instances (one per part) for the CPU and WebGL tracers. A scene built for the native tracer asks for another bound with `maximumInstances`, an integer from 1 to `MAX_NATIVE_TRACE_INSTANCES` (1,000,000). A render job on the Cycles-matching kernel passes 1,000,000 (`NATIVE_JOB_MAXIMUM_INSTANCES` in `scripts/native-bake-page.js`, 500,000 before #1100); the legacy kernel never instances, and the CPU and WebGL tracers keep 65,536 and its error text. The option is separate from `maximumTriangles`: the unique and total triangle bounds apply as before, so copies of a large mesh still hit the total bound (four times `maximumTriangles`) long before the instance bound.
Every place on the way to the kernel that could assume 65,536 instances, read in the code `[measured by reading]`:
@@ -1446,12 +1446,12 @@ Every place on the way to the kernel that could assume 65,536 instances, read in
| `MAX_TRACE_INSTANCES` check in `requireInstancedCaps` (`src/path-tracer-scene.js`) | the only 65,536; a guard for the CPU and WebGL paths, now a caller-set bound |
| `packInstancedGeometry` and `writeInstanceRecordBytes` (`pack-input-v2.mjs`), `job-input.mjs` | none; counts are 32-bit and the loops are linear |
| instance table, 160 bytes per instance | 160 MB at 1,000,000 |
-| material table, 256 bytes per part (a material record per part, not per distinct material) | the largest table: 256 MB at 1,000,000 |
+| material table, 256 bytes per distinct material record (#1100: parts whose materials are equal share one record; before, one per part) | 256 MB at 1,000,000 when every part has a material of its own, under 1 KB when the copies share a few |
| emissive and light tree records that store an instance index | `u32`; the light tree takes 4,194,304 emitters (`LIGHT_TREE_MAX_EMITTERS`), which bounds emissive copies, not plain ones |
| WGSL `instance_custom_data` and `int_resolve_instance_index` | the hardware custom index has 24 bits: 16,777,216 (`MAX_TLAS_INSTANCES` in `host/accel.rs`) |
| V8 string of the job export | see below; this is the bound of the job |
-Measured on an RTX 4090 box, one process per row, a field of two-triangle cards sharing one geometry (`bench/cycles-parity/native-instance-measure.mjs`), packed the way the job page does and traced at 160 by 120 with 16 samples `[measured]`:
+Measured before #1100 (one 256-byte material record per part) on an RTX 4090 box, one process per row, a field of two-triangle cards sharing one geometry (`bench/cycles-parity/native-instance-measure.mjs`), packed the way the job page does and traced at 160 by 120 with 16 samples `[measured]`:
| instances | pack s | base64 s | material table MB | all tables, base64 characters | peak RSS MB | traced s | acceleration build s | card MB (process included) |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
@@ -1460,8 +1460,29 @@ Measured on an RTX 4090 box, one process per row, a field of two-triangle cards
| 500,001 | 3.24 | 7.13 | 128.0 | 277.3 M | 2,363 | 0.73 | 0.100 | 796 |
| 1,000,000 | 7.17 | 14.34 | 256.0 | 554.7 M | 4,511 | 1.59 | 0.193 | 1,557 |
-Each instance costs 416 bytes packed (160 for the instance and 256 for its material) and 555 base64 characters. What bounds it `[measured, inferred for the reason]`: the table bytes and V8 strings, not the card or the acceleration build. At 1,000,000 instances the largest table is 341 M characters, which encodes, but the tables together are 554.7 M characters, above the 536,870,888 of one V8 string, so a page that returns the export as one string (a JSON message) would fail; 500,000 instances alone are 277 M characters, 52% of that limit, but the tables of one export share the message, so the bounds are not independent. The export sums the real base64 length of every table (`4 * ceil(bytes / 3)` each) before encoding, and refuses above 95% of the V8 limit (510,027,343 characters, `NATIVE_EXPORT_MAXIMUM_CHARACTERS`) with a `RangeError` that names the total and the largest tables and says to pass fewer parts or fewer unique triangles. The job rethrows that refusal (as it does the texture pool one), so the job report says why the native export failed and no other packing is tried. Worked example `[measured sizes]`: 1,500,000 unique triangles are 448 M characters (299 each), which leaves 62 M for instances at 555 each: about 110,000 instances (the 65,536 of before, 36 M, still fit; 150,000 instances, 83 M, give 531 M and are refused). Without triangles, 500,000 instances (277 M) fit alone. The job stops at 500,000 instances, and the library ceiling is 1,000,000, the size measured on the card. The card was never the limit: the acceleration build took 0.19 s at 1,000,000 instances and the traces ran in 1.6 s. The hardware allows 16,777,216 instances, and nothing above 1,000,000 instances was measured.
+Before #1100 each instance cost 416 bytes packed (160 for the instance and 256 for its material record) and 555 base64 characters, because the packer wrote one material record per part even when a thousand copies of one mesh used one material.
-Pictures: scenes under the old bounds are byte for byte the same. The PNG SHA-256 of three scenes (the 20,000 triangle cap scene with copies, a field of 3,000 shared cards and the same cards merged) on the job export of `cycles` instanced, `cycles` flattened and `legacy` are equal on `main` and on this change, and the refusal of the scene the flattened and legacy routes cannot take has the same words (`bench/cycles-parity/native-identity-pngs.mjs`, `[measured]`). Over the old bound, `tests/native-instancing-limit.test.js` traces 85,537 parts (the last 20,000 red, all past index 65,536) and the same cards merged into two meshes: 3,381 red pixels in both, mean ratio within 2% per channel, tiles within 5%, and a relative mean squared error of 1.5e-7, against 1.4e-3 between two seeds of the merged picture.
+Since #1100 the packer gives parts whose 256-byte records are equal one record (`shareEqualMaterialRecords` in `pack-input-v2.mjs`), so a copy that uses a material another part already uses costs the 160 bytes of its instance record and 214 base64 characters. Same card field (two materials and a ground, so three records), one process per row, RTX 4090 box, default Node heap, the first 750,002 and 1,000,000 rows traced at 160 by 120 with 16 samples `[measured]`:
-Not measured: Chrome's memory for a page that builds 500,000 parts (the numbers above are Node's), and a scene whose copies are emissive (each emissive copy adds light tree emitters and emissive records).
+| instances | pack s | instance table bytes | material table bytes | tables, bytes per instance | all tables, base64 characters | peak RSS MB |
+| --- | --- | --- | --- | --- | --- | --- |
+| 100,002 before | 0.51 | 16,000,320 | 25,600,512 | 416.0 | 55.5 M | 679 |
+| 100,002 | 0.77 | 16,000,320 | 768 | 160.0 | 21.3 M | 587 |
+| 500,002 before | 2.94 | 80,000,320 | 128,000,512 | 416.0 | 277.3 M | 2,356 |
+| 500,002 | 4.02 | 80,000,320 | 768 | 160.0 | 106.7 M | 1,884 |
+| 750,002 | 4.69 | 120,000,320 | 768 | 160.0 | 160.0 M | 2,730 |
+| 1,000,000 | 6.97 | 160,000,000 | 768 | 160.0 | 213.3 M | 3,568 |
+
+The pack takes 30 to 50% longer in these runs (it hashes and compares every part's record), within the spread of a shared machine; the memory is lower, because the encoded tables are smaller. The 750,002 and 1,000,000 instance rows traced in 0.84 s and 1.21 s; the 1,000,000 row has no counterpart before, since its tables were 554.7 M characters and did not fit. A part whose material equals no other still costs 416 bytes and 555 characters, so the worst case is unchanged: 1,000,000 parts that all differ stay refused by the size check below.
+
+What bounds the instance count `[measured, inferred for the reason]`: the table bytes and V8 strings, not the card or the acceleration build. Every table of one export shares one message, and one V8 string cannot exceed 536,870,888 characters. The export sums the real base64 length of every table (`4 * ceil(bytes / 3)` each) before encoding, and refuses above 95% of that limit (510,027,343 characters, `NATIVE_EXPORT_MAXIMUM_CHARACTERS`) with a `RangeError` that names the total and the largest tables and says to pass fewer parts or fewer unique triangles. The job rethrows that refusal (as it does the texture pool one), so the job report says why the native export failed and no other packing is tried. With shared records 1,000,000 instances are 213 M characters, 42% of the limit. Worked example `[measured]`: 1,500,000 unique triangles are 448 M characters (299 each), which leaves 62 M for instances: with shared records 280,008 instances export (507.7 M characters) and 330,008 are refused (518.4 M); before #1100, at 555 each, about 110,000 fitted and 150,000 were refused (they now export at 480.0 M). The job stops at 1,000,000 instances, which is the library ceiling and the largest size measured on the card and in Node; the earlier 500,000 followed from the table bytes of one material record per part. The card was never the limit: the acceleration build took 0.17 s at 1,000,000 instances. The hardware allows 16,777,216 instances, and nothing above 1,000,000 instances was measured.
+
+What makes two records distinct `[measured by reading]`: a part names its material by index (the triangle's `material_index`, the instance's `material_override`, the emissive entry's `material_index`), and the 256-byte record holds every value of the material, its flags and the index of every texture slot (equal images share one texture index). Two records are equal exactly when their bytes are equal and they derive the same emission, alpha mode and subsurface flag (`meanEmissiveRadiance`, `isEmissive`, `alphaMode`, `usesSubsurface`; each follows from the bytes, and the comparison checks them as well). Light-group masks, ray visibility and the object colour live on the instance, so they never keep two records apart. This branch has no separate record for a diffuse-transmission extension. The flattened packer (`buildFlattenedJobNativeInput` and the v1 upgrade) keeps one record per part: each part there is a mesh of its own, so no copy pays for one, and the legacy kernel reads the v1 tables and is untouched. The index remapping: the triangle record names the record of the mesh's first copy, a later copy gets a `material_override` only when its record differs, an explicit `materialOverride` or `overrideMaterial` is kept and points at the shared record, and an index past the table is left as it is.
+
+Session updates: a session keeps its tables at the size it was created with, and an update replaces the whole `materials` and `instances` tables with the ones packed from the new frame, so a changed material cannot reach a part that was packed with a record of its own. When parts share a record and one of them changes, the next frame packs one record more, `materialCount` changes and the session refuses the update (`planNativeSessionUpdate` throws, and the sequence takes a new session for that frame). That is copy-on-write at pack time. It was chosen over leaving the records of animated parts unshared because the packer cannot know which parts will change, and an extra session per divergence is the whole cost; the pictures are exact either way. A part whose track gives it values that equal no other part, or a material that several parts name by one id (they share a record before and after), keeps the `updated` route. Measured on 8-frame jobs through the job export and `createNativeSequence` (`bench/cycles-parity/native-shared-record-identity.mjs`, 96 by 72, 8 samples) `[measured]`: when the animated card starts equal to seven others the routes are `new-session`, `new-session`, then `updated` six times (before: `new-session`, then `updated` seven times), and when it differs from the start they are `new-session`, then `updated` seven times, as before; each frame equals its one-shot render byte for byte. The demo room of `bench/native-tracer/sequence-job.mjs` animates `plaster`, a material id of its own, so its routes should not change `[inferred by reading the recipe]`, but that job did not start in the box's headless Chrome (`Connection closed`, before and after the change), so it was not run.
+
+Pictures (#1055): scenes under the old bounds are byte for byte the same. The PNG SHA-256 of three scenes (the 20,000 triangle cap scene with copies, a field of 3,000 shared cards and the same cards merged) on the job export of `cycles` instanced, `cycles` flattened and `legacy` are equal on `main` and on this change, and the refusal of the scene the flattened and legacy routes cannot take has the same words (`bench/cycles-parity/native-identity-pngs.mjs`, `[measured]`). Over the old bound, `tests/native-instancing-limit.test.js` traces 85,537 parts (the last 20,000 red, all past index 65,536) and the same cards merged into two meshes: 3,381 red pixels in both, mean ratio within 2% per channel, tiles within 5%, and a relative mean squared error of 1.5e-7, against 1.4e-3 between two seeds of the merged picture.
+
+Pictures (#1100) `[measured]`: `bench/cycles-parity/native-shared-record-identity.mjs` hashes the pictures of four scenes of eight cards (textures, emitters, two materials, metal and glass) on `cycles` instanced, `cycles` flattened, `legacy` and an explicit-override pack, and two 8-frame session sequences, on the build before the change and on the build after it: all 18 hashes are equal (the instanced packs go from 9 records to 3). The PNG of the 250,001-card field is byte for byte the same (SHA-256 372e60c2e317...). `tests/native-shared-material-records.test.js` renders a shared pack and the same pack with one record per instance and compares them byte for byte.
+
+Not measured: Chrome's memory for a page that builds 1,000,000 parts (the numbers above are Node's, whose default heap held the 1,000,000-part case at 2.4 GB used and 3.6 GB resident), and the demo room sequence job.
diff --git a/docs/path-tracer.md b/docs/path-tracer.md
index 0809257d..ea9442db 100644
--- a/docs/path-tracer.md
+++ b/docs/path-tracer.md
@@ -61,7 +61,7 @@ Also checked before any ray is traced: samples times pixels times bounces at mos
`buildTraceScene(parts, environment, { instancing: true })` keeps parts that share one geometry object as instances of it, and `buildNativeInputV2` packs them as one bottom-level structure per geometry plus one instance record per part, for `traceNative` with `kernel: "cycles"`. Without the option the scene is flattened as before, and a scene where no two parts share a geometry is flattened too, with output identical to the default.
- Sharing is decided after the trace geometry is resolved. A skinned, flush-lifted or box-mapped part gets a geometry of its own, and a geometry with per-triangle materials is not shared either: those parts are moved into world space once and traced as instances with an identity matrix.
-- Limits: 400,000 triangles counted once per shared geometry, 1,024 distinct geometries (the part limit, counted on geometries) and 65,536 instances. `buildTraceScene(parts, environment, { instancing: true, maximumTriangles, maximumInstances })` replaces the 400,000 for a scene exported to the native tracer: an integer from 1 to 4,000,000 (`MAX_NATIVE_TRACE_TRIANGLES`) bounds the unique triangles (all triangles of a flattened scene), and four times that bounds the triangles with every copy counted. A value outside that range is refused, and the CPU and WebGL tracers never pass it, so they keep 400,000 and their error text. `maximumInstances` does the same for the 65,536 instances: an integer from 1 to 1,000,000 (`MAX_NATIVE_TRACE_INSTANCES`), counted over the parts of an instanced scene (each part is one instance, and the unique-geometry bound stays 1,024). A value outside that range is refused, and a flattened scene ignores it. A native render job passes 1,500,000 triangles and 500,000 instances, and its export also refuses a scene whose tables together exceed 95% of one V8 string (about 299 characters per unique triangle and 555 per instance: 1,500,000 triangles leave room for about 110,000 instances; see "Triangle bound of a native scene" and "Instance bound of a native scene" in [cycles-parity.md](./cycles-parity.md)). An instanced scene has no world-space triangle arrays or CPU BVH, so only `buildNativeInputV2` reads it.
+- Limits: 400,000 triangles counted once per shared geometry, 1,024 distinct geometries (the part limit, counted on geometries) and 65,536 instances. `buildTraceScene(parts, environment, { instancing: true, maximumTriangles, maximumInstances })` replaces the 400,000 for a scene exported to the native tracer: an integer from 1 to 4,000,000 (`MAX_NATIVE_TRACE_TRIANGLES`) bounds the unique triangles (all triangles of a flattened scene), and four times that bounds the triangles with every copy counted. A value outside that range is refused, and the CPU and WebGL tracers never pass it, so they keep 400,000 and their error text. `maximumInstances` does the same for the 65,536 instances: an integer from 1 to 1,000,000 (`MAX_NATIVE_TRACE_INSTANCES`), counted over the parts of an instanced scene (each part is one instance, and the unique-geometry bound stays 1,024). A value outside that range is refused, and a flattened scene ignores it. A native render job passes 1,500,000 triangles and 1,000,000 instances, and its export also refuses a scene whose tables together exceed 95% of one V8 string (about 299 characters per unique triangle and 214 per instance, 555 for an instance whose material record is not shared with another part, since parts with equal materials share one record: 1,500,000 triangles leave room for about 280,000 instances that share materials; see "Triangle bound of a native scene" and "Instance bound of a native scene" in [cycles-parity.md](./cycles-parity.md)). An instanced scene has no world-space triangle arrays or CPU BVH, so only `buildNativeInputV2` reads it.
- A part's material is its own entry in `materials`, so copies may differ in material; a multi-material mesh stays one geometry with the materials the part names.
- `buildNativeInput` (the `legacy` kernel) refuses an instanced scene with an error; build the scene without `instancing` for that kernel. `traceNative` with `kernel: "legacy"` refuses a FORMAT-v2 input the same way.
- Render jobs with `renderer.backend: "native"` keep repeated geometry as instances when the job traces with the `cycles` kernel (the default): the job page exports FORMAT-v2 tables of the unique triangles and one record per part, the job runner rebuilds the input for `traceNative`, and the report names the `instances` and counts `triangles` once per shared geometry. With `JS67_NATIVE_KERNEL=legacy`, `JS67_NATIVE_INSTANCING=off` (flattened triangles on the `cycles` kernel, to compare the two), or a scene where no geometry is shared, the job exports flattened geometry as before; on the `cycles` kernel the materials are still sent whole (next point), and only the legacy kernel reads the v1 tables.
diff --git a/packages/render-native/FORMAT-v2.md b/packages/render-native/FORMAT-v2.md
index e81b25ad..7572a409 100644
--- a/packages/render-native/FORMAT-v2.md
+++ b/packages/render-native/FORMAT-v2.md
@@ -135,6 +135,7 @@ When `InstanceRecord.emissive_base_offset != 0xFFFFFFFF` and `TriangleRecord.loc
**Sharing rules.** Instances that share a mesh share one slice of `triangles` (one BLAS, one `triangle_offset`), so everything in a `TriangleRecord` is the same for every copy. What can differ per copy lives in the `InstanceRecord`: the transform, `material_override`, `visibility_flags`, the light masks, the object id, random and colour, and `emissive_base_offset`. The packer applies these rules:
+- Parts whose `MaterialRecord` bytes are equal (and that derive the same emission) share one record: `material_count` counts distinct records, a copy needs a `material_override` only when its record differs from the first copy's, and every `material_index` (triangle, override, emissive entry) names the shared record. A session update that makes shared parts differ changes `material_count` and is refused.
- A mesh with per-triangle materials is not shared; its triangles are baked into world space and traced with an identity transform.
- `local_emissive_offset` counts the triangles that keep an area after an instance's transform, in triangle order, and is the same for every copy. A copy that would emit a different set of triangles is refused. A copy whose resolved material is not emissive gets `emissive_base_offset = 0xFFFFFFFF`, so its triangles never index the emissive list.
- A shadow-only copy clears the camera bit of `visibility_flags` and keeps the others. A mirrored copy (negative determinant) sets the negative-scale bit.
diff --git a/packages/render-native/pack-input-v2.mjs b/packages/render-native/pack-input-v2.mjs
index dae916ff..b6ffcdbb 100644
--- a/packages/render-native/pack-input-v2.mjs
+++ b/packages/render-native/pack-input-v2.mjs
@@ -2304,10 +2304,19 @@ export function shareEqualMaterialRecords(materialRecords) {
};
}
+// The words of a record, read in place when its bytes are aligned (the records `packMaterialRecordV2` returns are).
+function materialRecordWords(bytes) {
+ const wordCount = bytes.byteLength / 4;
+ return bytes.byteOffset % 4 === 0
+ ? new Uint32Array(bytes.buffer, bytes.byteOffset, wordCount)
+ : new Uint32Array(Uint8Array.from(bytes).buffer);
+}
+
function hashMaterialRecordBytes(bytes) {
+ const words = materialRecordWords(bytes);
let hash = 0x811c9dc5;
- for (let index = 0; index < bytes.length; index++) {
- hash = Math.imul(hash ^ bytes[index], 0x01000193);
+ for (let index = 0; index < words.length; index++) {
+ hash = Math.imul(hash ^ words[index], 0x01000193);
}
return hash >>> 0;
}
@@ -2315,7 +2324,12 @@ function hashMaterialRecordBytes(bytes) {
function materialRecordsAreEqual(first, second) {
if (first.isEmissive !== second.isEmissive || first.usesSubsurface !== second.usesSubsurface || first.alphaMode !== second.alphaMode) return false;
if (first.meanEmissiveRadiance.some((value, channel) => !Object.is(value, second.meanEmissiveRadiance[channel]))) return false;
- return first.bytes.every((byte, index) => byte === second.bytes[index]);
+ const firstWords = materialRecordWords(first.bytes);
+ const secondWords = materialRecordWords(second.bytes);
+ for (let index = 0; index < firstWords.length; index++) {
+ if (firstWords[index] !== secondWords[index]) return false;
+ }
+ return true;
}
function packInstancedGeometry(instancesInput, rawMaterials, textureCollector, reducedMaterials) {
diff --git a/scripts/native-bake-page.js b/scripts/native-bake-page.js
index 8fcca180..25ca9dbe 100644
--- a/scripts/native-bake-page.js
+++ b/scripts/native-bake-page.js
@@ -47,11 +47,11 @@ export function encodeNativeInputV2(input) {
// other tracer keep the 400,000 of the CPU and WebGL paths.
export const NATIVE_JOB_MAXIMUM_TRIANGLES = 1_500_000;
// The instance bound of the same export (see buildTraceScene's maximumInstances).
-export const NATIVE_JOB_MAXIMUM_INSTANCES = 500_000;
+export const NATIVE_JOB_MAXIMUM_INSTANCES = 1_000_000;
// Every table of the export travels as base64 inside one evaluate result, one JSON string, and V8 refuses a string above
-// 536,870,888 characters. The bounds above are independent (about 299 characters per unique triangle, 555 per instance), so the
-// export checks the sum of its real tables against this share of the limit; the rest is the JSON around the tables.
+// 536,870,888 characters. The bounds above are independent (about 299 characters per unique triangle, 214 per instance whose material record is shared
+// and up to 555 per instance with a record of its own), so the export checks the sum of its real tables against this share of the limit; the rest is the JSON around the tables.
export const V8_MAXIMUM_STRING_LENGTH = 536_870_888;
export const NATIVE_EXPORT_MAXIMUM_CHARACTERS = Math.floor(V8_MAXIMUM_STRING_LENGTH * 0.95);
@@ -68,7 +68,7 @@ export function requireTableSizesFit(tables) {
const total = encodedExportLength(tables.map(([, bytes]) => bytes));
if (total <= NATIVE_EXPORT_MAXIMUM_CHARACTERS) return;
const largest = [...tables].sort((first, second) => second[1] - first[1]).slice(0, 3).map(([name, bytes]) => `${name} ${encodedExportLength([bytes])}`).join(', ');
- const error = new RangeError(`The native scene export is ${total} base64 characters in total (largest tables: ${largest}), over the ${NATIVE_EXPORT_MAXIMUM_CHARACTERS} one message can carry. Pass fewer parts (each instance takes about 555 characters), fewer unique triangles (about 299 each), or smaller textures.`);
+ const error = new RangeError(`The native scene export is ${total} base64 characters in total (largest tables: ${largest}), over the ${NATIVE_EXPORT_MAXIMUM_CHARACTERS} one message can carry. Pass fewer parts (each instance takes about 214 characters, 555 when its material is not shared with another part), fewer unique triangles (about 299 each), or smaller textures.`);
error.exportTooLarge = true;
throw error;
}
diff --git a/tests/native-shared-material-records.test.js b/tests/native-shared-material-records.test.js
index 2951ec9f..e85aaeb0 100644
--- a/tests/native-shared-material-records.test.js
+++ b/tests/native-shared-material-records.test.js
@@ -23,6 +23,7 @@ const environment = { keyColor: [3, 3, 3], lightDirection: [0.4, 0.8, 0.5] };
const MATERIAL_OFFSET_IN_INSTANCE = 112;
const MATERIAL_INDEX_OFFSET_IN_TRIANGLE = 12;
const MATERIAL_INDEX_OFFSET_IN_EMISSIVE = 60;
+const MATERIAL_COUNT_OFFSET_IN_PARAMETERS = 92;
const quad = {
positions: Float32Array.from([-0.6, 0, -0.6, 0.6, 0, -0.6, 0.6, 0, 0.6, -0.6, 0, 0.6]),
@@ -217,7 +218,10 @@ function withUnsharedRecords(input) {
materials.set(input.materials.subarray(resolved * recordBytes, (resolved + 1) * recordBytes), copyIndex * recordBytes);
view.setUint32(index * instanceBytes + MATERIAL_OFFSET_IN_INSTANCE, copyIndex, true);
}
- return { ...input, instances, materials, materialCount: input.materialCount + input.instanceCount };
+ const materialCount = input.materialCount + input.instanceCount;
+ const kernelParams = Uint8Array.from(input.kernelParams);
+ new DataView(kernelParams.buffer).setUint32(MATERIAL_COUNT_OFFSET_IN_PARAMETERS, materialCount, true);
+ return { ...input, instances, materials, materialCount, kernelParams };
}
test('shared records trace the picture of one record per part', { skip: addonSkip }, t => {