Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Give parts with equal material records one record in the FORMAT-v2 instanced pack (#1100)

Nguyen Xuan And31c3d050669
diff --git a/bench/cycles-parity/native-shared-record-identity.mjs b/bench/cycles-parity/native-shared-record-identity.mjs
new file mode 100644
index 00000000..69466a4e
--- /dev/null
+++ b/bench/cycles-parity/native-shared-record-identity.mjs
@@ -0,0 +1,106 @@
+// SHA-256 of the pictures of scenes whose parts carry equal materials, traced on both native kernels (#1100). Run it on a build before
+// the change that gives equal material records one record and on the build after it, and compare the hashes: the pictures must be
+// equal. The number of material records is printed beside each hash and is expected to differ.
+// node bench/cycles-parity/native-shared-record-identity.mjs
+import { createHash } from 'node:crypto';
+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';
+import { buildTraceScene, refuseLocalVolumes, resolveTexture } from '../../src/path-tracer-scene.js';
+import { buildNativeInputV2, traceNative } from '../../packages/render-native/index.mjs';
+import { exportNativeSceneTables } from '../../scripts/native-bake-page.js';
+import { createCamera } from '../../src/path-tracer.js';
+
+const WIDTH = 96;
+const HEIGHT = 72;
+const SAMPLES = 8;
+const PACK_OPTIONS = { width: WIDTH, height: HEIGHT, samples: SAMPLES, bounces: 4, seed: 1, fireflyClamp: 20, noiseThreshold: 0 };
+const ENVIRONMENT = { keyColor: [3, 3, 3], lightDirection: [0.4, 0.8, 0.5] };
+const camera = createCamera({ eye: [0, 3, 9], target: [0, 0.5, 0], fov: 50 }, WIDTH, HEIGHT);
+const modules = { buildTraceScene, refuseLocalVolumes, resolveTexture, buildNativeInput, buildJobNativeInput, buildFlattenedJobNativeInput, pack };
+
+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]),
+ normals: Float32Array.from([0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]),
+ uvs: Float32Array.from([0, 0, 1, 0, 1, 1, 0, 1]),
+ indices: Uint32Array.from([0, 2, 1, 0, 3, 2]),
+};
+const GROUND = {
+ name: 'ground',
+ geometry: {
+ positions: Float32Array.from([-9, -0.05, -9, 9, -0.05, -9, 9, -0.05, 9, -9, -0.05, 9]),
+ normals: QUAD.normals,
+ uvs: QUAD.uvs,
+ indices: QUAD.indices,
+ },
+ material: { color: [0.5, 0.5, 0.5], roughness: 0.9 },
+};
+
+function checkerTexture(size) {
+ const data = new Uint8ClampedArray(size * size * 4);
+ for (let texel = 0; texel < size * size; texel++) {
+ const lit = ((texel % size) + Math.floor(texel / size)) % 2 === 0;
+ data.set(lit ? [230, 200, 60, 255] : [40, 60, 200, 255], texel * 4);
+ }
+ return { image: { width: size, height: size, data }, colorSpace: 'srgb' };
+}
+
+function placed(name, index, material) {
+ const x = (index - 3.5) * 1.4;
+ const matrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, 0.1 * index, 0, 1];
+ return { name, geometry: QUAD, matrix, material };
+}
+
+const TEXTURE = checkerTexture(8);
+const OTHER_TEXTURE = checkerTexture(4);
+const GREY = { color: [0.6, 0.6, 0.6], roughness: 0.7 };
+const RED = { color: [0.8, 0.1, 0.1], roughness: 0.7 };
+
+// Every part gets a material object of its own, equal to its neighbours' values.
+const sceneBuilders = {
+ 'textures': () => [GROUND, ...Array.from({ length: 8 }, (_, index) => placed(`card-${index}`, index, { map: index % 2 === 0 ? TEXTURE : OTHER_TEXTURE, color: [1, 1, 1], roughness: 0.6 }))],
+ 'emitters': () => [GROUND, ...Array.from({ length: 8 }, (_, index) => placed(`card-${index}`, index, index % 3 === 0 ? { color: [1, 0.8, 0.6], emission: 4 } : { color: [0.6, 0.6, 0.6], roughness: 0.7 }))],
+ 'overrides': () => [GROUND, ...Array.from({ length: 8 }, (_, index) => placed(`card-${index}`, index, index < 4 ? { ...GREY } : { ...RED }))],
+ 'metal-and-glass': () => [GROUND, ...Array.from({ length: 8 }, (_, index) => placed(`card-${index}`, index, index % 2 === 0 ? { color: [0.9, 0.7, 0.4], metallic: 1, roughness: 0.3 } : { color: [1, 1, 1], transmission: 0.9, ior: 1.45, roughness: 0.05 }))],
+};
+
+const routes = [
+ { name: 'cycles-instanced', kernel: 'cycles', instancing: true },
+ { name: 'cycles-flattened', kernel: 'cycles', instancing: false },
+ { name: 'legacy', kernel: 'legacy', instancing: false },
+];
+
+function sha256(bytes) {
+ return createHash('sha256').update(bytes).digest('hex');
+}
+
+// Instance k places its mesh with the material of the part (k + 3) % count: explicit `materialOverride` records, some equal to the default's.
+function explicitOverrideHash(parts) {
+ const scene = buildTraceScene(parts, ENVIRONMENT, { instancing: true });
+ const instances = scene.instances.map((instance, index, all) => ({ ...instance, materialOverride: (index + 3) % all.length }));
+ const input = buildNativeInputV2(scene, camera, { ...PACK_OPTIONS, instances });
+ const picture = traceNative(input, { width: WIDTH, height: HEIGHT, samples: SAMPLES, kernel: 'cycles' });
+ return { hash: sha256(Buffer.from(picture.rgba.buffer, picture.rgba.byteOffset, picture.rgba.byteLength)), materialRecords: input.materialCount };
+}
+
+const tracer = await import('../../packages/render-native/index.mjs');
+const { traceFrameNative } = await import('../../scripts/agent-native-trace.mjs');
+const results = {};
+for (const [sceneName, buildParts] of Object.entries(sceneBuilders)) {
+ const parts = buildParts();
+ for (const route of routes) {
+ try {
+ const exported = exportNativeSceneTables({ parts, environment: ENVIRONMENT, camera, options: PACK_OPTIONS, kernel: route.kernel, instancing: route.instancing, modules });
+ const frame = await traceFrameNative({ tracer, exported: { ...exported, ignored: [] }, width: WIDTH, height: HEIGHT, samples: SAMPLES, bounces: PACK_OPTIONS.bounces, seed: 1, denoise: 'none', kernel: route.kernel });
+ results[`${sceneName} ${route.name}`] = { hash: sha256(Buffer.from(frame.data, 'base64')), instances: exported.instances ?? 0, materialRecords: exported.counts?.materialCount ?? null };
+ } catch (error) {
+ results[`${sceneName} ${route.name}`] = { hash: `refused: ${error.message}` };
+ }
+ }
+ try {
+ results[`${sceneName} explicit-override`] = explicitOverrideHash(parts);
+ } catch (error) {
+ results[`${sceneName} explicit-override`] = { hash: `refused: ${error.message}` };
+ }
+}
+console.log(`SHARED_RECORD_IDENTITY ${JSON.stringify(results, null, 1)}`);
diff --git a/bench/native-tracer/sequence-job.mjs b/bench/native-tracer/sequence-job.mjs
index 7ffed5d0..785c3794 100644
--- a/bench/native-tracer/sequence-job.mjs
+++ b/bench/native-tracer/sequence-job.mjs
@@ -106,7 +106,7 @@ for (const [name, { recipe, expectedLaterRoute, allowedRoutes = [expectedLaterRo
const laterRoutes = withSessions.routes.slice(1);
const routesAsExpected = withSessions.routes[0] === 'new-session' && withSessions.routes.length === STILLS_PER_JOB && laterRoutes.every(route => allowedRoutes.includes(route)) && laterRoutes.includes(expectedLaterRoute);
const sessionsOffRoutes = withoutSessions.routes;
- report.variants[name] = { identical, routes: withSessions.routes, routesAsExpected, sessionsOffRoutes, backend: withSessions.backend, backendNote: withSessions.backendNote, lastFrameSession: withSessions.lastSession };
+ report.variants[name] = { identical, frameHashes: withSessions.frameHashes, routes: withSessions.routes, routesAsExpected, sessionsOffRoutes, backend: withSessions.backend, backendNote: withSessions.backendNote, lastFrameSession: withSessions.lastSession };
if (!identical || !routesAsExpected || sessionsOffRoutes.length > 0) failed = true;
}
await writeFile(join(outputDirectory, 'sequence-job.json'), JSON.stringify(report, null, 1));
diff --git a/changes/1100.md b/changes/1100.md
new file mode 100644
index 00000000..a7ec8e7b
--- /dev/null
+++ b/changes/1100.md
@@ -0,0 +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)
diff --git a/packages/render-native/pack-input-v2.mjs b/packages/render-native/pack-input-v2.mjs
index 237ce65d..dae916ff 100644
--- a/packages/render-native/pack-input-v2.mjs
+++ b/packages/render-native/pack-input-v2.mjs
@@ -2114,7 +2114,7 @@ function packKernelParamsV2({
return new Uint8Array(buffer);
}
-function buildGeometryTriangleItem(geometry, triIndex, defaultMaterialIndex, meshId, materialRecords) {
+function buildGeometryTriangleItem(geometry, triIndex, defaultMaterialIndex, meshId, materialRecords, recordIndexOf = index => index) {
const { positions, normals, uvs, uv1s, colors, tangents, indices, triangleMaterial } = geometry;
const i0 = indices[triIndex * 3];
const i1 = indices[triIndex * 3 + 1];
@@ -2168,7 +2168,7 @@ function buildGeometryTriangleItem(geometry, triIndex, defaultMaterialIndex, mes
flags |= TRIANGLE_FLAG_HAS_UV1;
}
- const materialIndex = triangleMaterial ? triangleMaterial[triIndex] : defaultMaterialIndex;
+ const materialIndex = triangleMaterial ? recordIndexOf(triangleMaterial[triIndex]) : defaultMaterialIndex;
const matInfo = materialRecords[materialIndex];
const isOpaque = (matInfo?.alphaMode ?? ALPHA_MODE_OPAQUE) === ALPHA_MODE_OPAQUE;
@@ -2274,10 +2274,55 @@ function shareEmissiveOffsets(meshSlot, emittedLocalIndices, instanceIndex) {
}
}
+/**
+ * Gives parts whose material records are equal one record. Two records are equal when their 256 bytes are, which holds the base
+ * values, the flags, every texture index (equal textures share an index in the collector) and the extension fields, and when
+ * the derived emission they were packed with is. The unique records keep the order of their first part, so a scene without
+ * equal records keeps its table, and `recordIndexOf` maps a part's material index to its record's index; an index past the
+ * table (no record) stays as it is.
+ */
+export function shareEqualMaterialRecords(materialRecords) {
+ const uniqueRecords = [];
+ const recordIndexByPart = new Array(materialRecords.length);
+ const uniqueIndicesByHash = new Map();
+ materialRecords.forEach((record, partIndex) => {
+ const hash = hashMaterialRecordBytes(record.bytes);
+ const candidates = uniqueIndicesByHash.get(hash);
+ const sameIndex = candidates?.find(uniqueIndex => materialRecordsAreEqual(uniqueRecords[uniqueIndex], record));
+ if (sameIndex !== undefined) {
+ recordIndexByPart[partIndex] = sameIndex;
+ return;
+ }
+ recordIndexByPart[partIndex] = uniqueRecords.length;
+ if (candidates) candidates.push(uniqueRecords.length);
+ else uniqueIndicesByHash.set(hash, [uniqueRecords.length]);
+ uniqueRecords.push(record);
+ });
+ return {
+ records: uniqueRecords,
+ recordIndexOf: partIndex => recordIndexByPart[partIndex] ?? partIndex,
+ };
+}
+
+function hashMaterialRecordBytes(bytes) {
+ let hash = 0x811c9dc5;
+ for (let index = 0; index < bytes.length; index++) {
+ hash = Math.imul(hash ^ bytes[index], 0x01000193);
+ }
+ return hash >>> 0;
+}
+
+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]);
+}
+
function packInstancedGeometry(instancesInput, rawMaterials, textureCollector, reducedMaterials) {
- const materialRecords = reducedMaterials
+ const partRecords = reducedMaterials
? reducedMaterials.materialRecords
: rawMaterials.map(mat => packMaterialRecordV2(mat, textureCollector));
+ const { records: materialRecords, recordIndexOf } = shareEqualMaterialRecords(partRecords);
const materialsBuffer = new Uint8Array(Math.max(1, materialRecords.length) * STRUCT_STRIDES_V2.material);
materialRecords.forEach((rec, index) => materialsBuffer.set(rec.bytes, index * STRUCT_STRIDES_V2.material));
@@ -2287,12 +2332,12 @@ function packInstancedGeometry(instancesInput, rawMaterials, textureCollector, r
const inst = instancesInput[instIdx];
const geometry = inst?.geometry;
if (!geometry) continue;
- const defaultMat = Number(inst.materialIndex ?? 0);
+ const defaultMat = recordIndexOf(Number(inst.materialIndex ?? 0));
if (!firstDefaultMatByGeo.has(geometry)) {
firstDefaultMatByGeo.set(geometry, defaultMat);
}
const overrideMat = inst.materialOverride !== undefined
- ? (inst.materialOverride >>> 0)
+ ? recordIndexOf(inst.materialOverride >>> 0)
: (!geometry.triangleMaterial && defaultMat !== firstDefaultMatByGeo.get(geometry) ? (defaultMat >>> 0) : INVALID_INDEX_V2);
if (overrideMat !== INVALID_INDEX_V2) {
const matAlphaMode = materialRecords[overrideMat]?.alphaMode ?? ALPHA_MODE_OPAQUE;
@@ -2313,9 +2358,9 @@ function packInstancedGeometry(instancesInput, rawMaterials, textureCollector, r
if (!geometry?.positions || !geometry?.indices) {
throw new TypeError(`Instance[${instIdx}] requires a geometry with positions and indices.`);
}
- const defaultMaterialIndex = Number(inst.materialIndex ?? 0);
+ const defaultMaterialIndex = recordIndexOf(Number(inst.materialIndex ?? 0));
let materialOverride = inst.materialOverride !== undefined
- ? (inst.materialOverride >>> 0)
+ ? recordIndexOf(inst.materialOverride >>> 0)
: (inst.overrideMaterial === true ? (defaultMaterialIndex >>> 0) : INVALID_INDEX_V2);
let meshSlot = meshCache.get(geometry);
@@ -2326,7 +2371,7 @@ function packInstancedGeometry(instancesInput, rawMaterials, textureCollector, r
const opaque = [];
const cutout = [];
for (let t = 0; t < triCount; t++) {
- const item = buildGeometryTriangleItem(geometry, t, defaultMaterialIndex, blasIndex, materialRecords);
+ const item = buildGeometryTriangleItem(geometry, t, defaultMaterialIndex, blasIndex, materialRecords, recordIndexOf);
if (item.isOpaque && !forceNonOpaque) {
opaque.push(item);
} else {
diff --git a/tests/native-shared-material-records.test.js b/tests/native-shared-material-records.test.js
new file mode 100644
index 00000000..2951ec9f
--- /dev/null
+++ b/tests/native-shared-material-records.test.js
@@ -0,0 +1,241 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import {
+ STRUCT_STRIDES_V2,
+ buildNativeInputV2,
+ findNativeAddon,
+ packMaterialRecordV2,
+ planNativeSessionUpdate,
+ traceNative,
+} from '../packages/render-native/index.mjs';
+import { INVALID_INDEX_V2, shareEqualMaterialRecords } from '../packages/render-native/pack-input-v2.mjs';
+import { buildJobNativeInput } from '../packages/render-native/job-input.mjs';
+import { createCamera } from '../src/path-tracer.js';
+import * as pack from '../src/path-tracer-gpu-pack.js';
+import { buildTraceScene, resolveTexture } from '../src/path-tracer-scene.js';
+
+const WIDTH = 32;
+const HEIGHT = 24;
+const camera = createCamera({ eye: [0, 3, 9], target: [0, 0.5, 0], fov: 50 }, WIDTH, HEIGHT);
+const packOptions = { width: WIDTH, height: HEIGHT, samples: 2, bounces: 3, seed: 1, fireflyClamp: 20, noiseThreshold: 0 };
+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 quad = {
+ positions: Float32Array.from([-0.6, 0, -0.6, 0.6, 0, -0.6, 0.6, 0, 0.6, -0.6, 0, 0.6]),
+ normals: Float32Array.from([0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]),
+ uvs: Float32Array.from([0, 0, 1, 0, 1, 1, 0, 1]),
+ indices: Uint32Array.from([0, 2, 1, 0, 3, 2]),
+};
+
+function placedPart(index, material, geometry = quad) {
+ const matrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, index * 1.4, 0, 0, 1];
+ return { name: `part-${index}`, geometry, matrix, material };
+}
+
+function jobInput(parts) {
+ const scene = buildTraceScene(parts, environment, { instancing: true });
+ assert.ok(scene.instances, 'the parts share a geometry, so the scene is instanced');
+ return buildJobNativeInput(scene, camera, packOptions, pack, resolveTexture);
+}
+
+function wordAt(table, recordIndex, stride, byteOffset) {
+ return new DataView(table.buffer, table.byteOffset).getUint32(recordIndex * stride + byteOffset, true);
+}
+
+function instanceOverride(input, instanceIndex) {
+ return wordAt(input.instances, instanceIndex, STRUCT_STRIDES_V2.instance, MATERIAL_OFFSET_IN_INSTANCE);
+}
+
+function recordHex(input, recordIndex) {
+ const start = input.materials.byteOffset + recordIndex * STRUCT_STRIDES_V2.material;
+ return Buffer.from(input.materials.buffer, start, STRUCT_STRIDES_V2.material).toString('hex');
+}
+
+test('records with equal bytes share one record, in the order of their first part', () => {
+ const grey = () => packMaterialRecordV2({ color: [0.6, 0.6, 0.6], roughness: 0.7 });
+ const red = () => packMaterialRecordV2({ color: [0.8, 0.1, 0.1], roughness: 0.7 });
+ const { records, recordIndexOf } = shareEqualMaterialRecords([grey(), red(), grey(), red(), grey()]);
+ assert.equal(records.length, 2);
+ assert.deepEqual([0, 1, 2, 3, 4].map(recordIndexOf), [0, 1, 0, 1, 0]);
+});
+
+test('a scene without equal records keeps its table', () => {
+ const materials = [0.1, 0.2, 0.3, 0.4].map(roughness => packMaterialRecordV2({ roughness }));
+ const { records, recordIndexOf } = shareEqualMaterialRecords(materials);
+ assert.equal(records.length, 4);
+ assert.deepEqual([0, 1, 2, 3].map(recordIndexOf), [0, 1, 2, 3]);
+});
+
+test('any differing material field keeps its own record', () => {
+ const base = { color: [0.6, 0.5, 0.4], roughness: 0.5 };
+ const variations = {
+ color: { color: [0.6, 0.5, 0.41] },
+ metallic: { metallic: 0.5 },
+ roughness: { roughness: 0.51 },
+ ior: { ior: 1.45 },
+ emission: { emission: 1 },
+ emissionColor: { emission: 1, emissiveColor: [1, 0, 0] },
+ transmission: { transmission: 0.2 },
+ clearcoat: { clearcoat: 0.3 },
+ alphaMode: { alphaMode: 'blend', opacity: 0.5 },
+ sheen: { sheenColor: [1, 1, 1] },
+ anisotropy: { anisotropy: 0.4 },
+ subsurface: { subsurfaceWeight: 0.5 },
+ doubleSided: { doubleSided: false },
+ thinWalled: { thinWalled: true },
+ absorption: { absorptionDistance: 2, absorptionColor: [0.5, 0.8, 0.6] },
+ normalScale: { normalScale: 0.5 },
+ };
+ for (const [name, change] of Object.entries(variations)) {
+ const { records } = shareEqualMaterialRecords([packMaterialRecordV2(base), packMaterialRecordV2({ ...base, ...change })]);
+ assert.equal(records.length, 2, `a part that differs in ${name} shares nothing`);
+ }
+});
+
+test('records that differ only in a texture slot stay apart, equal textures share', () => {
+ const imageA = { image: { width: 1, height: 1, data: new Uint8ClampedArray([255, 0, 0, 255]) }, colorSpace: 'srgb' };
+ const imageB = { image: { width: 1, height: 1, data: new Uint8ClampedArray([0, 0, 255, 255]) }, colorSpace: 'srgb' };
+ const parts = [{ map: imageA }, { map: imageA }, { map: imageB }, { metallicRoughnessMap: imageA }, {}].map(material => ({ color: [1, 1, 1], ...material }));
+ const scene = buildTraceScene(parts.map((material, index) => placedPart(index, material)), environment, { instancing: true });
+ const input = buildJobNativeInput(scene, camera, packOptions, pack, resolveTexture);
+ assert.equal(input.materialCount, 4, 'two parts with the same image share, the others differ in the image or the slot');
+});
+
+test('copies of a mesh with equal materials share one record and need no override', () => {
+ const copyCount = 12;
+ const parts = Array.from({ length: copyCount }, (_, index) => placedPart(index, { color: [0.6, 0.6, 0.6], roughness: 0.7 }));
+ const input = jobInput(parts);
+ assert.equal(input.instanceCount, copyCount);
+ assert.equal(input.materialCount, 1);
+ for (let index = 0; index < copyCount; index++) {
+ assert.equal(instanceOverride(input, index), INVALID_INDEX_V2, `copy ${index} reads the triangle's material`);
+ }
+ assert.equal(wordAt(input.triangles, 0, STRUCT_STRIDES_V2.triangle, MATERIAL_INDEX_OFFSET_IN_TRIANGLE), 0);
+});
+
+test('a copy with another material keeps an override to its own record', () => {
+ const grey = () => ({ color: [0.6, 0.6, 0.6], roughness: 0.7 });
+ const red = () => ({ color: [0.8, 0.1, 0.1], roughness: 0.7 });
+ const parts = [grey(), red(), grey(), red(), grey()].map((material, index) => placedPart(index, material));
+ const input = jobInput(parts);
+ assert.equal(input.materialCount, 2);
+ assert.deepEqual([0, 1, 2, 3, 4].map(index => instanceOverride(input, index)), [INVALID_INDEX_V2, 1, INVALID_INDEX_V2, 1, INVALID_INDEX_V2]);
+ assert.notEqual(recordHex(input, 0), recordHex(input, 1));
+});
+
+test('an explicit override and an override flag point at the shared record', () => {
+ const grey = () => ({ color: [0.6, 0.6, 0.6], roughness: 0.7 });
+ const red = () => ({ color: [0.8, 0.1, 0.1], roughness: 0.7 });
+ const scene = buildTraceScene([grey(), red(), grey(), red()].map((material, index) => placedPart(index, material)), environment, { instancing: true });
+ const instances = scene.instances.map((instance, index) => {
+ if (index === 0) return { ...instance, materialOverride: 3 };
+ if (index === 2) return { ...instance, overrideMaterial: true };
+ return instance;
+ });
+ const input = buildNativeInputV2(scene, camera, { ...packOptions, instances });
+ assert.equal(input.materialCount, 2);
+ assert.equal(instanceOverride(input, 0), 1, 'part 3 is red, whose record is 1');
+ assert.equal(instanceOverride(input, 2), 0, 'part 2 is grey, whose record is 0');
+});
+
+test('a mesh with per-triangle materials names the shared records in its triangles', () => {
+ const twoMaterialMesh = { ...quad, triangleMaterial: Uint32Array.from([0, 3]) };
+ const grey = () => ({ color: [0.6, 0.6, 0.6] });
+ const materials = [grey(), { color: [0.8, 0.1, 0.1] }, grey(), grey()];
+ const scene = buildTraceScene([placedPart(0, grey()), placedPart(1, grey())], environment, { instancing: true });
+ const identity = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
+ const instances = [{ geometry: twoMaterialMesh, matrix: identity, materialIndex: 0 }];
+ const input = buildNativeInputV2(scene, camera, { ...packOptions, instances, materials });
+ assert.equal(input.materialCount, 2);
+ assert.equal(input.triangleCount, 2);
+ for (let triangle = 0; triangle < input.triangleCount; triangle++) {
+ assert.equal(wordAt(input.triangles, triangle, STRUCT_STRIDES_V2.triangle, MATERIAL_INDEX_OFFSET_IN_TRIANGLE), 0, `triangle ${triangle} reads the grey record`);
+ }
+});
+
+test('emissive copies share a record and their emissive entries name it', () => {
+ const lamp = () => ({ color: [1, 0.8, 0.6], emission: 4 });
+ const wall = () => ({ color: [0.6, 0.6, 0.6], roughness: 0.7 });
+ const parts = [lamp(), wall(), lamp(), wall(), lamp()].map((material, index) => placedPart(index, material));
+ const input = jobInput(parts);
+ assert.equal(input.materialCount, 2);
+ assert.equal(input.emissiveTriangleCount, 6, 'three lamps of two triangles each');
+ for (let entry = 0; entry < input.emissiveTriangleCount; entry++) {
+ assert.equal(wordAt(input.emissiveTriangles, entry, STRUCT_STRIDES_V2.emissiveTriangle, MATERIAL_INDEX_OFFSET_IN_EMISSIVE), 0, 'every lamp entry reads the lamp record');
+ }
+});
+
+test('a material update to one part leaves the parts that shared its record alone', () => {
+ const grey = () => ({ color: [0.6, 0.6, 0.6], roughness: 0.7 });
+ const before = jobInput([grey(), grey(), grey(), { color: [0.8, 0.1, 0.1], roughness: 0.7 }].map((material, index) => placedPart(index, material)));
+ assert.equal(before.materialCount, 2);
+
+ const otherRedPart = jobInput([grey(), grey(), grey(), { color: [0.1, 0.1, 0.8], roughness: 0.7 }].map((material, index) => placedPart(index, material)));
+ assert.deepEqual(Object.keys(planNativeSessionUpdate(before, otherRedPart)), ['materials'], 'a part with a record of its own updates in place');
+ assert.equal(recordHex(otherRedPart, 0), recordHex(before, 0), 'the shared record is untouched');
+
+ const sharedPartChanges = jobInput([grey(), { color: [0.9, 0.9, 0.2], roughness: 0.7 }, grey(), { color: [0.8, 0.1, 0.1], roughness: 0.7 }].map((material, index) => placedPart(index, material)));
+ assert.throws(() => planNativeSessionUpdate(before, sharedPartChanges), { message: /`materialCount` changes from 2 to 3/ });
+ assert.equal(sharedPartChanges.materialCount, 3);
+ assert.equal(recordHex(sharedPartChanges, 0), recordHex(before, 0), 'the parts that stayed grey keep the grey record');
+ assert.equal(instanceOverride(sharedPartChanges, 2), INVALID_INDEX_V2, 'the part that stayed grey still reads the shared record');
+ assert.notEqual(instanceOverride(sharedPartChanges, 1), INVALID_INDEX_V2, 'the changed part reads a record of its own');
+});
+
+test('a material update that remaps parts to other records is refused, not applied', () => {
+ const grey = () => ({ color: [0.6, 0.6, 0.6], roughness: 0.7 });
+ const red = () => ({ color: [0.8, 0.1, 0.1], roughness: 0.7 });
+ const green = () => ({ color: [0.1, 0.8, 0.1], roughness: 0.7 });
+ const build = materials => jobInput(materials.map((material, index) => placedPart(index, material)));
+ const before = build([grey(), grey(), red()]);
+ const swapped = build([grey(), red(), red()]);
+ assert.equal(before.materialCount, swapped.materialCount);
+ assert.ok(Object.keys(planNativeSessionUpdate(before, swapped)).includes('instances'), 'the instance overrides are replaced with the materials');
+ const regrouped = build([grey(), green(), red()]);
+ assert.throws(() => planNativeSessionUpdate(before, regrouped), { message: /`materialCount` changes from 2 to 3/ });
+});
+
+const addonSkip = findNativeAddon() ? false : 'the native addon is not built';
+
+// Rebuilds the input the way it was packed before records were shared: every instance reads a copy of the record it resolves to,
+// appended after the shared table through its override. A non-emissive scene sees the same materials by either layout.
+function withUnsharedRecords(input) {
+ const recordBytes = STRUCT_STRIDES_V2.material;
+ const instanceBytes = STRUCT_STRIDES_V2.instance;
+ const materials = new Uint8Array(recordBytes * (input.materialCount + input.instanceCount));
+ materials.set(input.materials.subarray(0, input.materialCount * recordBytes), 0);
+ const instances = Uint8Array.from(input.instances);
+ const view = new DataView(instances.buffer);
+ for (let index = 0; index < input.instanceCount; index++) {
+ const override = instanceOverride(input, index);
+ const resolved = override === INVALID_INDEX_V2 ? wordAt(input.triangles, 0, STRUCT_STRIDES_V2.triangle, MATERIAL_INDEX_OFFSET_IN_TRIANGLE) : override;
+ const copyIndex = input.materialCount + index;
+ 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 };
+}
+
+test('shared records trace the picture of one record per part', { skip: addonSkip }, t => {
+ const grey = () => ({ color: [0.6, 0.6, 0.6], roughness: 0.7 });
+ const red = () => ({ color: [0.8, 0.1, 0.1], roughness: 0.7 });
+ const parts = [grey(), red(), grey(), red(), grey(), red()].map((material, index) => placedPart(index, material));
+ const shared = jobInput(parts);
+ assert.equal(shared.materialCount, 2);
+ const unshared = withUnsharedRecords(shared);
+ const traceOptions = { width: WIDTH, height: HEIGHT, samples: 8, kernel: 'cycles' };
+ let sharedPicture;
+ let unsharedPicture;
+ try {
+ sharedPicture = traceNative(shared, traceOptions).rgba;
+ unsharedPicture = traceNative(unshared, traceOptions).rgba;
+ } catch (error) {
+ if (/adapter|Vulkan|ray quer/i.test(error.message)) return t.skip(error.message);
+ throw error;
+ }
+ assert.equal(Buffer.compare(Buffer.from(sharedPicture.buffer, sharedPicture.byteOffset, sharedPicture.byteLength), Buffer.from(unsharedPicture.buffer, unsharedPicture.byteOffset, unsharedPicture.byteLength)), 0);
+});