3D engine that AI agents drive
Add the card field bench scene, instance measurement and PNG identity check (#1055)
Nguyen Xuan An0527df93550b
diff --git a/bench/cycles-parity/native-identity-pngs.mjs b/bench/cycles-parity/native-identity-pngs.mjsnew file mode 100644index 00000000..b2eb7d9a--- /dev/null+++ b/bench/cycles-parity/native-identity-pngs.mjs@@ -0,0 +1,44 @@+// SHA-256 of the PNG of a few scenes under the old bounds, traced through the job export on both native kernels (#1055). Run it on a+// build before a change to the instance bound and on the build after it, and compare the output: the pictures must be equal.+// node bench/cycles-parity/native-identity-pngs.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 { exportNativeSceneTables } from '../../scripts/native-bake-page.js';+import { NATIVE_CAP_ENVIRONMENT, NATIVE_CAP_PACK_OPTIONS, buildNativeCapParts, nativeCapCamera } from './native-cap-scene.mjs';+import { CARD_FIELD_ENVIRONMENT, CARD_FIELD_PACK_OPTIONS, buildCardField, buildCardFieldMerged, cardFieldCamera } from './native-instance-scene.mjs';++const SAMPLES = 8;+const CARD_COUNT = 3000;+const modules = { buildTraceScene, refuseLocalVolumes, resolveTexture, buildNativeInput, buildJobNativeInput, buildFlattenedJobNativeInput, pack };++const scenes = [+ { name: 'native-cap-20000', parts: buildNativeCapParts(20_000, true).parts, environment: NATIVE_CAP_ENVIRONMENT, camera: nativeCapCamera(), options: NATIVE_CAP_PACK_OPTIONS },+ { name: 'cards-3000-shared', parts: buildCardField(CARD_COUNT, 2000), environment: CARD_FIELD_ENVIRONMENT, camera: cardFieldCamera(CARD_COUNT), options: CARD_FIELD_PACK_OPTIONS },+ { name: 'cards-3000-merged', parts: buildCardFieldMerged(CARD_COUNT, 2000), environment: CARD_FIELD_ENVIRONMENT, camera: cardFieldCamera(CARD_COUNT), options: CARD_FIELD_PACK_OPTIONS },+];+const routes = [+ { name: 'cycles-instanced', kernel: 'cycles', instancing: true },+ { name: 'cycles-flattened', kernel: 'cycles', instancing: false },+ { name: 'legacy', kernel: 'legacy', instancing: false },+];++const tracer = await import('../../packages/render-native/index.mjs');+const { traceFrameNative } = await import('../../scripts/agent-native-trace.mjs');+const hashes = {};+for (const scene of scenes) {+ for (const route of routes) {+ try {+ const exported = exportNativeSceneTables({ parts: scene.parts, environment: scene.environment, camera: scene.camera, options: scene.options, kernel: route.kernel, instancing: route.instancing, modules });+ const frame = await traceFrameNative({ tracer, exported: { ...exported, ignored: [] }, width: scene.options.width, height: scene.options.height, samples: SAMPLES,+ bounces: scene.options.bounces, seed: 1, denoise: 'none', kernel: route.kernel });+ hashes[`${scene.name} ${route.name}`] = `${createHash('sha256').update(Buffer.from(frame.data, 'base64')).digest('hex')} instances=${exported.instances ?? 0}`;+ } catch (error) {+ // A refusal is part of the comparison: the old and new build must refuse a scene with the same words.+ hashes[`${scene.name} ${route.name}`] = `refused: ${error.message}`;+ }+ }+}+console.log(`NATIVE_IDENTITY ${JSON.stringify(hashes, null, 1)}`);diff --git a/bench/cycles-parity/native-instance-measure.mjs b/bench/cycles-parity/native-instance-measure.mjsnew file mode 100644index 00000000..9e292e88--- /dev/null+++ b/bench/cycles-parity/native-instance-measure.mjs@@ -0,0 +1,114 @@+// Measures the page-side pack of a card field with many instances (#1055), and optionally traces it on the native tracer.+// node bench/cycles-parity/native-instance-measure.mjs --count 250000 [--variant instanced|merged] [--trace] [--samples 16]+// [--no-encode] [--png <file>]+// One process per case, so the peak memory is the case's own. It prints one NATIVE_INSTANCE_CASE line of JSON.+// instanced: buildTraceScene({ instancing: true, maximumInstances }) + buildJobNativeInput + encodeNativeInputV2, the job export path.+// merged: the same cards written into two world-space meshes and exported as a flattened scene.+// --no-encode stops after the pack: a table above 536,870,888 base64 characters cannot be encoded in V8.+import { spawn } from 'node:child_process';+import { createHash } from 'node:crypto';+import { mkdirSync, writeFileSync } from 'node:fs';+import { dirname } from 'node:path';+import * as pack from '../../src/path-tracer-gpu-pack.js';+import { buildFlattenedJobNativeInput, buildJobNativeInput } from '../../packages/render-native/job-input.mjs';+import { MAX_NATIVE_TRACE_INSTANCES, buildTraceScene, resolveTexture } from '../../src/path-tracer-scene.js';+import { encodeNativeInputV2 } from '../../scripts/native-bake-page.js';+import { readFlag } from './sequence-bench-core.mjs';+import { CARD_FIELD_ENVIRONMENT, CARD_FIELD_PACK_OPTIONS, buildCardField, buildCardFieldMerged, cardFieldCamera } from './native-instance-scene.mjs';++const V8_STRING_LIMIT = 536_870_888;+const BYTES_PER_MEGABYTE = 1024 * 1024;++function secondsSince(startedAt) {+ return Number(((performance.now() - startedAt) / 1000).toFixed(3));+}++function megabytes(bytes) {+ return Number((bytes / BYTES_PER_MEGABYTE).toFixed(1));+}++function tableBytes(input) {+ const bytes = {};+ for (const [name, value] of Object.entries(input)) {+ if (ArrayBuffer.isView(value)) bytes[name] = value.byteLength;+ }+ return bytes;+}++async function traceExported(exported, samples, pngPath) {+ const tracer = await import('../../packages/render-native/index.mjs');+ const { traceFrameNative } = await import('../../scripts/agent-native-trace.mjs');+ const { width, height } = CARD_FIELD_PACK_OPTIONS;+ const frame = await traceFrameNative({ tracer, exported: { ...exported, ignored: [] }, width, height, samples, bounces: CARD_FIELD_PACK_OPTIONS.bounces, seed: 1, denoise: 'none', kernel: 'cycles' });+ const png = Buffer.from(frame.data, 'base64');+ if (pngPath) {+ mkdirSync(dirname(pngPath), { recursive: true });+ writeFileSync(pngPath, png);+ }+ const { report } = frame;+ return { tracedSeconds: Number(report.tracedSeconds.toFixed(3)), accelerationBuildSeconds: report.accelerationBuildSeconds, uploadSeconds: report.uploadSeconds,+ adapter: report.gpu, inputBytes: report.inputBytes, pngSha256: createHash('sha256').update(png).digest('hex') };+}++// The card memory in use, sampled by an nvidia-smi child while the trace blocks this process; the first sample is the idle level.+function startCardMeter() {+ const samples = [];+ let pending = '';+ const meter = spawn('nvidia-smi', ['--query-gpu=memory.used', '--format=csv,noheader,nounits', '-lms', '100'], { stdio: ['ignore', 'pipe', 'ignore'] });+ meter.on('error', () => {});+ meter.stdout.on('data', chunk => {+ pending += chunk;+ const lines = pending.split('\n');+ pending = lines.pop();+ for (const line of lines) if (line.trim() !== '') samples.push(Number(line));+ });+ return async function stopCardMeter() {+ await new Promise(resolve => setTimeout(resolve, 400));+ meter.kill();+ return samples.length === 0 ? null : { idleMegabytes: samples[0], peakMegabytes: Math.max(...samples) };+ };+}++const argumentList = process.argv.slice(2);+const count = Number(readFlag(argumentList, '--count', 250000));+const variant = readFlag(argumentList, '--variant', 'instanced');+const samples = Number(readFlag(argumentList, '--samples', 16));+const encode = !argumentList.includes('--no-encode');+const redFrom = Math.max(0, count - Math.floor(count / 4));+const camera = cardFieldCamera(count);+const result = { variant, count };+const phases = {};++let startedAt = performance.now();+const parts = variant === 'instanced' ? buildCardField(count, redFrom) : buildCardFieldMerged(count, redFrom);+phases.buildPartsSeconds = secondsSince(startedAt);+startedAt = performance.now();+const traceScene = variant === 'instanced'+ ? buildTraceScene(parts, CARD_FIELD_ENVIRONMENT, { instancing: true, maximumTriangles: 1_500_000, maximumInstances: MAX_NATIVE_TRACE_INSTANCES })+ : buildTraceScene(parts, CARD_FIELD_ENVIRONMENT, { maximumTriangles: 1_500_000 });+phases.buildSceneSeconds = secondsSince(startedAt);+startedAt = performance.now();+const input = variant === 'instanced'+ ? buildJobNativeInput(traceScene, camera, CARD_FIELD_PACK_OPTIONS, pack, resolveTexture)+ : buildFlattenedJobNativeInput(traceScene, camera, CARD_FIELD_PACK_OPTIONS, pack, resolveTexture);+phases.packSeconds = secondsSince(startedAt);+Object.assign(result, phases, { instances: input.instanceCount, triangles: input.triangleCount, tableBytes: tableBytes(input) });+let exported;+if (encode) {+ startedAt = performance.now();+ exported = { ...encodeNativeInputV2(input), triangleCount: input.triangleCount, triangles: input.triangleCount, instances: input.instanceCount, wholeMaterials: true };+ result.encodeSeconds = secondsSince(startedAt);+ const lengths = Object.fromEntries(Object.entries(exported.tables).map(([name, text]) => [name, text.length]));+ result.largestBase64Table = Object.entries(lengths).sort((first, second) => second[1] - first[1])[0];+ result.base64Characters = Object.values(lengths).reduce((total, length) => total + length, 0);+ result.overStringLimit = result.largestBase64Table[1] > V8_STRING_LIMIT || result.base64Characters > V8_STRING_LIMIT;+}+result.heapUsedMegabytes = megabytes(process.memoryUsage().heapUsed);+result.peakResidentMegabytes = megabytes(process.resourceUsage().maxRSS * 1024);+if (argumentList.includes('--trace')) {+ const stopCardMeter = startCardMeter();+ await new Promise(resolve => setTimeout(resolve, 400));+ result.trace = await traceExported(exported, samples, readFlag(argumentList, '--png', null));+ result.trace.card = await stopCardMeter();+}+console.log(`NATIVE_INSTANCE_CASE ${JSON.stringify(result)}`);diff --git a/bench/cycles-parity/native-instance-scene.mjs b/bench/cycles-parity/native-instance-scene.mjsnew file mode 100644index 00000000..5b0b8474--- /dev/null+++ b/bench/cycles-parity/native-instance-scene.mjs@@ -0,0 +1,111 @@+// A deterministic field of small cards for the native instance bound (#1055): `count` copies of one two-triangle card, tilted+// and turned by a fixed hash, on a square grid under a sun. Cards from `redFrom` on are red and the others grey, so a picture+// shows whether the instances past an index were traced at all. `buildCardFieldMerged` writes the same cards into two+// world-space meshes (grey and red) for the picture the instanced one is compared with.+import { createCamera } from '../../src/path-tracer.js';++export const CARD_FIELD_ENVIRONMENT = { keyColor: [3, 3, 3], lightDirection: [0.4, 0.8, 0.5] };+export const CARD_FIELD_PACK_OPTIONS = { width: 160, height: 120, bounces: 3, seed: 1, fireflyClamp: 20, noiseThreshold: 0 };++const CARD_HALF_SIZE = 0.5;+const CARD_SPACING = 1.2;+const GREY_MATERIAL = { color: [0.6, 0.6, 0.6], roughness: 0.7 };+const RED_MATERIAL = { color: [0.8, 0.1, 0.1], roughness: 0.7 };+const GROUND_MATERIAL = { color: [0.5, 0.5, 0.5], roughness: 0.9 };++const CARD = {+ positions: new Float32Array([-CARD_HALF_SIZE, 0, -CARD_HALF_SIZE, CARD_HALF_SIZE, 0, -CARD_HALF_SIZE, CARD_HALF_SIZE, 0, CARD_HALF_SIZE, -CARD_HALF_SIZE, 0, CARD_HALF_SIZE]),+ normals: new Float32Array([0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]),+ uvs: new Float32Array([0, 0, 1, 0, 1, 1, 0, 1]),+ indices: new Uint32Array([0, 2, 1, 0, 3, 2]),+};++function hashUnit(index, salt) {+ let state = Math.imul(index + 1, 0x9e3779b1) ^ Math.imul(salt + 1, 0x85ebca6b);+ state = Math.imul(state ^ (state >>> 15), 0x2c1b3c6d);+ state = Math.imul(state ^ (state >>> 12), 0x297a2d39);+ return ((state ^ (state >>> 15)) >>> 0) / 4294967296;+}++// Column-major: a turn about Y, then a tilt about X, then the move to the grid cell of the card.+function cardMatrix(index, columns) {+ const turn = hashUnit(index, 1) * Math.PI * 2;+ const tilt = (hashUnit(index, 2) - 0.5) * 0.8;+ const cosineTurn = Math.cos(turn);+ const sineTurn = Math.sin(turn);+ const cosineTilt = Math.cos(tilt);+ const sineTilt = Math.sin(tilt);+ const x = ((index % columns) - columns / 2) * CARD_SPACING;+ const z = (Math.floor(index / columns) - columns / 2) * CARD_SPACING;+ return new Float32Array([+ cosineTurn, 0, -sineTurn, 0,+ sineTurn * sineTilt, cosineTilt, cosineTurn * sineTilt, 0,+ sineTurn * cosineTilt, -sineTilt, cosineTurn * cosineTilt, 0,+ x, 0.2 + hashUnit(index, 3) * 0.3, z, 1,+ ]);+}++function groundPart(halfSize) {+ const geometry = {+ positions: new Float32Array([-halfSize, -0.05, -halfSize, halfSize, -0.05, -halfSize, halfSize, -0.05, halfSize, -halfSize, -0.05, halfSize]),+ normals: new Float32Array([0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]),+ uvs: CARD.uvs,+ indices: new Uint32Array([0, 2, 1, 0, 3, 2]),+ };+ return { name: 'ground', geometry, material: GROUND_MATERIAL };+}++function fieldSize(count) {+ const columns = Math.ceil(Math.sqrt(count));+ return { columns, halfSize: columns * CARD_SPACING / 2 + 1 };+}++/** The camera looks straight down at the whole field, so every card is in the picture. */+export function cardFieldCamera(count) {+ const { halfSize } = fieldSize(count);+ const { width, height } = CARD_FIELD_PACK_OPTIONS;+ return createCamera({ eye: [0, halfSize * 1.9, 0.001], target: [0, 0, 0], fov: 50, near: 0.05, far: halfSize * 4 }, width, height);+}++/** `count` cards sharing one geometry object: the instanced scene. Cards from `redFrom` on are red. */+export function buildCardField(count, redFrom) {+ const { columns, halfSize } = fieldSize(count);+ const parts = [groundPart(halfSize)];+ for (let index = 0; index < count; index++) {+ parts.push({ name: `card-${index}`, geometry: CARD, matrix: cardMatrix(index, columns), material: index >= redFrom ? RED_MATERIAL : GREY_MATERIAL });+ }+ return parts;+}++function appendCard(mesh, matrix) {+ const vertexOffset = mesh.positions.length / 3;+ for (let vertex = 0; vertex < 4; vertex++) {+ const x = CARD.positions[vertex * 3];+ const y = CARD.positions[vertex * 3 + 1];+ const z = CARD.positions[vertex * 3 + 2];+ for (let axis = 0; axis < 3; axis++) mesh.positions.push(matrix[axis] * x + matrix[4 + axis] * y + matrix[8 + axis] * z + matrix[12 + axis]);+ for (let axis = 0; axis < 3; axis++) mesh.normals.push(matrix[4 + axis]);+ mesh.uvs.push(CARD.uvs[vertex * 2], CARD.uvs[vertex * 2 + 1]);+ }+ for (const corner of CARD.indices) mesh.indices.push(vertexOffset + corner);+}++function meshPart(name, mesh, material) {+ return {+ name,+ geometry: { positions: Float32Array.from(mesh.positions), normals: Float32Array.from(mesh.normals), uvs: Float32Array.from(mesh.uvs), indices: Uint32Array.from(mesh.indices) },+ material,+ };+}++/** The same cards merged into one grey and one red world-space mesh, with the same ground: the flattened reference picture. */+export function buildCardFieldMerged(count, redFrom) {+ const { columns, halfSize } = fieldSize(count);+ const grey = { positions: [], normals: [], uvs: [], indices: [] };+ const red = { positions: [], normals: [], uvs: [], indices: [] };+ for (let index = 0; index < count; index++) appendCard(index >= redFrom ? red : grey, cardMatrix(index, columns));+ const parts = [groundPart(halfSize)];+ if (grey.indices.length > 0) parts.push(meshPart('grey-cards', grey, GREY_MATERIAL));+ if (red.indices.length > 0) parts.push(meshPart('red-cards', red, RED_MATERIAL));+ return parts;+}