Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Send native cycles jobs whole materials: FORMAT-v2 tables from the page for every cycles job, blend opacity, sheen, specular, anisotropy, iridescence, attenuation and layer textures carried over the v1 tables (#1036)

Nguyen Xuan An293e78f4387c
diff --git a/packages/render-native/job-input.mjs b/packages/render-native/job-input.mjs
index 851ade9d..356af86c 100644
--- a/packages/render-native/job-input.mjs
+++ b/packages/render-native/job-input.mjs
@@ -1,16 +1,120 @@
-// The FORMAT-v2 input a native render job sends for an instanced scene. A job without shared geometry sends v1 tables, whose
-// material tables keep fewer fields than FORMAT-v2 materials do. An instanced job reduces its materials the same way, so a
+// The FORMAT-v2 input a native render job sends to the `cycles` kernel. The v1 material tables keep fewer fields than a
+// FORMAT-v2 material record holds, so the job takes what the tables carry from the tables (a scene that uses nothing else packs
+// to the bytes it always did) and lays the rest of the material over it: blend opacity, sheen, specular, anisotropy,
+// iridescence, attenuation and the layer textures. A flattened scene and an instanced one go through the same records, so a
// part looks the same whether or not other geometry in the scene is shared.
-import { packNativeMaterialTables } from './pack-input.mjs';
-import { buildNativeInputV2, reduceV1MaterialTable } from './pack-input-v2.mjs';
+import { buildNativeInput, packNativeMaterialTables } from './pack-input.mjs';
+import { buildNativeInputV2, reduceV1MaterialTable, upgradeNativeInputV1ToV2 } from './pack-input-v2.mjs';
const ALL_LIGHT_GROUPS = 0xffffffff;
+// Glossy iridescence defaults of glTF (KHR_materials_iridescence), which the 67js material layers follow.
+const DEFAULT_IRIDESCENCE_IOR = 1.3;
+const DEFAULT_IRIDESCENCE_THICKNESS = 400;
+
+// Record fields a material may set by their record names; a material that leaves one out keeps the record default.
+const RECORD_FIELDS = Object.freeze([
+ 'specularIorLevel', 'diffuseRoughness', 'f82Tint', 'f82Metallic', 'coatTint', 'coatIor', 'coatNormalScale', 'bumpDistance',
+ 'thinWalled', 'multiscatterGgx', 'transparentShadow', 'emissiveMis',
+]);
+
+function isWhite(color) {
+ return Array.from(color).every(channel => channel === 1);
+}
+
+function hasColor(color) {
+ return color !== undefined && Array.from(color).some(channel => channel !== 0);
+}
+
+function blendFields(material, resolved) {
+ // A cutout (mask, or a graph opacity cut at one half) keeps the cutoff the tables carry.
+ if (material.alphaMode !== 'blend' || resolved.alphaCutoff > 0) return {};
+ const opacity = material.opacity ?? 1;
+ // Blending at full opacity with no texture changes no pixel; leaving it opaque keeps the faster opaque path.
+ if (!(opacity < 1) && !resolved.texture) return {};
+ return { alphaMode: 'blend', opacity };
+}
+
+function layerFields(material) {
+ const fields = {};
+ if (material.specular !== undefined) fields.specular = material.specular;
+ if (material.specularColor !== undefined && !isWhite(material.specularColor)) fields.specularColor = material.specularColor;
+ if (material.anisotropy > 0) {
+ fields.anisotropy = material.anisotropy;
+ fields.anisotropyRotation = material.anisotropyRotation ?? 0;
+ }
+ if (material.iridescence > 0) {
+ fields.iridescence = material.iridescence;
+ fields.iridescenceIor = material.iridescenceIor ?? DEFAULT_IRIDESCENCE_IOR;
+ fields.iridescenceThickness = material.iridescenceThickness ?? DEFAULT_IRIDESCENCE_THICKNESS;
+ }
+ if (hasColor(material.sheenColor)) {
+ fields.sheenColor = material.sheenColor;
+ fields.sheenRoughness = material.sheenRoughness ?? 0;
+ }
+ if (material.attenuationDistance > 0 && Number.isFinite(material.attenuationDistance)) {
+ fields.attenuationDistance = material.attenuationDistance;
+ fields.attenuationColor = material.attenuationColor ?? [1, 1, 1];
+ }
+ for (const name of RECORD_FIELDS) {
+ if (material[name] !== undefined) fields[name] = material[name];
+ }
+ return fields;
+}
+
+// A layer texture only matters while its layer is on, so a texture on a layer that is off stays out of the record.
+function layerTextures(material, resolved, resolveTexture) {
+ const textures = {};
+ const keep = (slot, image) => {
+ if (image) textures[slot] = image;
+ };
+ if (resolved.transmission > 0) keep('transmission', resolved.transmissionTexture);
+ if (resolved.clearcoat > 0) {
+ keep('coatWeightRoughness', resolved.clearcoatTexture);
+ keep('coatNormal', resolveTexture(material.layerMaps?.clearcoatNormalTexture));
+ }
+ if (material.anisotropy > 0) keep('anisotropy', resolveTexture(material.layerMaps?.anisotropyTexture));
+ if (hasColor(material.sheenColor)) keep('sheenColorRoughness', resolveTexture(material.layerMaps?.sheenColorTexture));
+ if (material.bumpDistance > 0) keep('bump', resolveTexture(material.bumpMap));
+ return textures;
+}
+
+/**
+ * What a part's material sets beyond the v1 job tables, named as `packMaterialRecordV2` reads it: blend opacity, specular and
+ * its tint, anisotropy, iridescence, sheen, attenuation, the rest of the record and the layer textures (`textures`, images
+ * the collector can take). `material` is the part's own material, `resolved` its resolved trace material and
+ * `resolveTexture` turns a texture descriptor into such an image (`resolveTexture` of src/path-tracer-scene.js). A material
+ * that sets none of these gives an empty object, which packs to the record the v1 tables give.
+ */
+export function carriedMaterialFields(material, resolved, resolveTexture) {
+ const fields = { ...blendFields(material, resolved), ...layerFields(material) };
+ const textures = layerTextures(material, resolved, resolveTexture);
+ if (Object.keys(textures).length > 0) fields.textures = textures;
+ return fields;
+}
+
+/**
+ * The carried fields of every material of a trace scene, in the order of its material table: an instanced scene holds the
+ * parts' own materials in `materials` and the resolved ones in `traceMaterials`; a flattened scene holds the resolved ones
+ * in `materials` and the parts' own in `partMaterials`.
+ */
+export function carriedMaterialFieldsOfScene(scene, resolveTexture) {
+ const ownMaterials = scene.instances ? scene.materials : scene.partMaterials;
+ const resolvedMaterials = scene.instances ? scene.traceMaterials : scene.materials;
+ if (!Array.isArray(ownMaterials) || !Array.isArray(resolvedMaterials)) {
+ throw new TypeError('carriedMaterialFieldsOfScene reads a scene from buildTraceScene (instanced, or flattened with partMaterials).');
+ }
+ if (typeof resolveTexture !== 'function') {
+ throw new TypeError('carriedMaterialFieldsOfScene needs `resolveTexture`, the function of that name in src/path-tracer-scene.js.');
+ }
+ return resolvedMaterials.map((resolved, index) => carriedMaterialFields(ownMaterials[index] ?? {}, resolved, resolveTexture));
+}
+
/**
* Packs an instanced trace scene (`buildTraceScene` with `{ instancing: true }`) for the `cycles` kernel with the materials,
- * light masks and textures of the v1 job tables. `pack` is `src/path-tracer-gpu-pack.js`; `options` are those of the job.
+ * light masks and textures of the job. `pack` is `src/path-tracer-gpu-pack.js`; `options` are those of the job.
*/
-export function buildJobNativeInput(scene, camera, options, pack) {
+export function buildJobNativeInput(scene, camera, options, pack, resolveTexture) {
if (!scene?.instances || !scene.traceMaterials) {
throw new TypeError('buildJobNativeInput packs an instanced scene: build it with buildTraceScene(parts, environment, { instancing: true }) and share a geometry between two parts.');
}
@@ -21,6 +125,7 @@ export function buildJobNativeInput(scene, camera, options, pack) {
atlasWidth: tables.atlasWidth,
subsurfaceVec4Offset: tables.subsurfaceVec4Offset,
materialCount: scene.traceMaterials.length,
+ carriedFields: carriedMaterialFieldsOfScene(scene, resolveTexture),
});
const instances = scene.instances.map(instance => {
const lightMask = reducedMaterials.materialLightMasks[instance.materialIndex] ?? ALL_LIGHT_GROUPS;
@@ -29,3 +134,15 @@ export function buildJobNativeInput(scene, camera, options, pack) {
const { fireflyClamp, ...packOptions } = options;
return buildNativeInputV2(scene, camera, { ...packOptions, instances, reducedMaterials });
}
+
+/**
+ * Packs a flattened trace scene for the `cycles` kernel the way the kernel upgrades v1 tables, with the carried material
+ * fields added: the geometry, lights and environment are those of `buildNativeInput`, so a scene that sets no carried field
+ * gives the input the kernel builds from the v1 tables itself.
+ */
+export function buildFlattenedJobNativeInput(scene, camera, options, pack, resolveTexture) {
+ const carriedFields = carriedMaterialFieldsOfScene(scene, resolveTexture);
+ // The sample count goes into the kernel parameters, as it does when the kernel upgrades the tables itself.
+ const upgradeOptions = options.samples === undefined ? {} : { samples: options.samples };
+ return upgradeNativeInputV1ToV2(buildNativeInput(scene, camera, options, pack), upgradeOptions, carriedFields);
+}
diff --git a/packages/render-native/pack-input-v2.mjs b/packages/render-native/pack-input-v2.mjs
index 6cf91fbc..332f41d7 100644
--- a/packages/render-native/pack-input-v2.mjs
+++ b/packages/render-native/pack-input-v2.mjs
@@ -2458,13 +2458,44 @@ function extractAtlasSubtexture(atlasWords, atlasWidth, rectX, rectY, width, hei
return words;
}
+// The texture slots of a material record that the v1 tables have no atlas rectangle for, with the colour space of each.
+// `carried.textures` names them; the record field is the index the collector hands back.
+const CARRIED_TEXTURE_SLOTS = Object.freeze([
+ ['transmission', 'transmissionTex', false],
+ ['coatWeightRoughness', 'coatWeightRoughnessTex', false],
+ ['coatTint', 'coatTintTex', true],
+ ['coatNormal', 'coatNormalTex', false],
+ ['sheenColorRoughness', 'sheenColorRoughnessTex', true],
+ ['anisotropy', 'anisotropyTex', false],
+ ['bump', 'bumpTex', false],
+]);
+
+/**
+ * The record descriptor of a material with the fields beyond the v1 tables laid over it. `carried` is the result of
+ * `carriedMaterialFields` (src names of the record fields, plus `textures`, resolved images by slot). The descriptor keeps
+ * every field the v1 tables carry as they are: carried fields never replace them, so a material that carries nothing packs
+ * to the bytes it always did. Blending applies only to a material the tables made opaque.
+ */
+function withCarriedFields(descriptor, carried, textureCollector) {
+ if (!carried) return descriptor;
+ const { textures, alphaMode, ...fields } = carried;
+ const carriedDescriptor = { ...descriptor, ...fields };
+ if (alphaMode === 'blend' && descriptor.alphaMode === 'opaque') carriedDescriptor.alphaMode = 'blend';
+ for (const [slot, field, isSrgb] of CARRIED_TEXTURE_SLOTS) {
+ const image = textures?.[slot];
+ if (image) carriedDescriptor[field] = textureCollector.addTexture(image, isSrgb).index;
+ }
+ return carriedDescriptor;
+}
+
/**
- * The material records of a v1 material table, the reduction every v1 native job goes through: only the fields the v1 tables
- * carry survive (base colour, metallic, roughness, emission, clearcoat, transmission, ior, normal scale, mask cutoff,
- * subsurface and the four atlas textures). `materials` holds the table with the subsurface table after it, `atlas` the atlas
- * words and `atlasWidth` their row width.
+ * The material records of a v1 material table. The fields the v1 tables carry (base colour, metallic, roughness, emission,
+ * clearcoat, transmission, ior, normal scale, mask cutoff, subsurface and the four atlas textures) are read from the tables.
+ * `carriedFields`, when given, holds one `carriedMaterialFields` result per material for everything the tables cannot hold
+ * (blend opacity, sheen, specular, anisotropy, iridescence, attenuation, the layer textures and the rest of the record).
+ * `materials` holds the table with the subsurface table after it, `atlas` the atlas words and `atlasWidth` their row width.
*/
-export function reduceV1MaterialTable({ materials: matSource, atlas: atlasWords, atlasWidth, subsurfaceVec4Offset, materialCount }) {
+export function reduceV1MaterialTable({ materials: matSource, atlas: atlasWords, atlasWidth, subsurfaceVec4Offset, materialCount, carriedFields }) {
const textureCollector = createTextureCollector();
const atlasCache = new Map();
@@ -2527,7 +2558,7 @@ export function reduceV1MaterialTable({ materials: matSource, atlas: atlasWords,
}
}
- const packedMat = packMaterialRecordV2({
+ const packedMat = packMaterialRecordV2(withCarriedFields({
baseColor,
metallic,
roughness,
@@ -2545,7 +2576,7 @@ export function reduceV1MaterialTable({ materials: matSource, atlas: atlasWords,
metallicRoughnessTex: mrTex.index,
emissionTex: emissionTex.index,
alphaTex: alphaCutoff > 0 && baseTex.index >= 0 ? baseTex.index : -1,
- }, null);
+ }, carriedFields?.[m], textureCollector), null);
packedMat.meanEmissiveRadiance = [
packedMat.meanEmissiveRadiance[0] * emissionTex.meanLinearRgb[0],
packedMat.meanEmissiveRadiance[1] * emissionTex.meanLinearRgb[1],
@@ -2559,9 +2590,11 @@ export function reduceV1MaterialTable({ materials: matSource, atlas: atlasWords,
/**
* Upgrades a legacy v1 `buildNativeInput` payload to `FORMAT-v2` (`schemaVersion: 2`) so existing callers passing v1
- * inputs with `kernel: 'cycles'` or `JS67_NATIVE_KERNEL=cycles` run without changing call sites.
+ * inputs with `kernel: 'cycles'` or `JS67_NATIVE_KERNEL=cycles` run without changing call sites. `carriedFields` (one
+ * `carriedMaterialFields` result per material) adds what the v1 material table cannot hold; without it the records hold the
+ * v1 fields alone.
*/
-export function upgradeNativeInputV1ToV2(v1Input, options = {}) {
+export function upgradeNativeInputV1ToV2(v1Input, options = {}, carriedFields = undefined) {
if (v1Input?.schemaVersion === FORMAT_V2_VERSION) {
return v1Input;
}
@@ -2600,6 +2633,7 @@ export function upgradeNativeInputV1ToV2(v1Input, options = {}) {
atlasWidth: paramU32[45] || 1,
subsurfaceVec4Offset,
materialCount,
+ carriedFields,
});
const materialsBuffer = new Uint8Array(Math.max(1, materialRecords.length) * STRUCT_STRIDES_V2.material);
diff --git a/scripts/agent-native-trace.mjs b/scripts/agent-native-trace.mjs
index bbef0f1c..8eb5bdd8 100644
--- a/scripts/agent-native-trace.mjs
+++ b/scripts/agent-native-trace.mjs
@@ -205,7 +205,8 @@ export async function traceFrameNative({ tracer, exported, width, height, sample
gpu: traced.adapter, nativeBackend: traced.backend, nativeFeatures: traced.features, setupSeconds: traced.setupSeconds, compileSeconds: traced.compileSeconds,
uploadSeconds: traced.uploadSeconds, accelerationBuildSeconds: traced.accelerationBuildSeconds,
kernel, averageSamples: traced.averageSamples ?? samples, noiseThreshold, triangles: exported.triangles, inputBytes: tableBytes(tables), ...(exported.instances === undefined ? {} : { instances: exported.instances }),
- ...(exported.instancing === undefined ? {} : { instancing: exported.instancing, instancingReason: exported.instancingReason }), tracedSeconds, ignored: exported.ignored };
+ ...(exported.instancing === undefined ? {} : { instancing: exported.instancing, instancingReason: exported.instancingReason }),
+ ...(exported.materialsReason === undefined ? {} : { materialsReason: exported.materialsReason }), tracedSeconds, ignored: exported.ignored };
let linear = features.color;
const denoiseStarted = performance.now();
if (denoise === 'oidn') {
diff --git a/scripts/agent-preview-browser.js b/scripts/agent-preview-browser.js
index a92cd0a0..c2705229 100644
--- a/scripts/agent-preview-browser.js
+++ b/scripts/agent-preview-browser.js
@@ -989,17 +989,17 @@ window.__agentPreview = {
} else if (motionTime !== undefined) {
record.scene.sampleMotion(motionTime);
}
- const { instancing, ...packOptions } = options;
- const [{ createCamera, describeIgnoredFeatures }, { buildTraceScene, refuseLocalVolumes }, pack, { buildNativeInput }, { buildJobNativeInput }] = await Promise.all([
+ const { instancing, kernel, ...packOptions } = options;
+ const [{ createCamera, describeIgnoredFeatures }, { buildTraceScene, refuseLocalVolumes, resolveTexture }, pack, { buildNativeInput }, { buildJobNativeInput, buildFlattenedJobNativeInput }] = await Promise.all([
import('../src/path-tracer.js'), import('../src/path-tracer-scene.js'), import('../src/path-tracer-gpu-pack.js'),
import('../packages/render-native/pack-input.mjs'), import('../packages/render-native/job-input.mjs'),
]);
const environment = { ...record.scene.environment, map: agentEnvironmentMap(record.scene.environment) };
- // Copies of one geometry stay instances only when the job traces with the "cycles" kernel; the legacy kernel reads flattened triangles.
+ // The "cycles" kernel gets FORMAT-v2 tables with whole materials, and copies of one geometry stay instances; the legacy kernel reads flattened v1 triangles.
const camera = createCamera(record.scene.camera, options.width, options.height);
- const exported = exportNativeSceneTables({ parts: record.scene.parts, environment, camera, options: packOptions, instancing: instancing === true,
- modules: { buildTraceScene, refuseLocalVolumes, buildNativeInput, buildJobNativeInput, pack } });
- return { ...exported, ignored: describeIgnoredFeatures(record.scene.parts, environment) };
+ const exported = exportNativeSceneTables({ parts: record.scene.parts, environment, camera, options: packOptions, kernel, instancing: instancing === true,
+ modules: { buildTraceScene, refuseLocalVolumes, resolveTexture, buildNativeInput, buildJobNativeInput, buildFlattenedJobNativeInput, pack } });
+ return { ...exported, ignored: describeIgnoredFeatures(record.scene.parts, environment, 'cpu', { kernelMaterials: exported.wholeMaterials }) };
},
// The PNG of linear pixels (three floats each, base64) that a denoiser outside the page returned, or of the wavelet
// filter over the kept features when none came back.
diff --git a/scripts/agent-runtime.mjs b/scripts/agent-runtime.mjs
index 010f69df..e3ec7627 100644
--- a/scripts/agent-runtime.mjs
+++ b/scripts/agent-runtime.mjs
@@ -15,7 +15,7 @@ import { MATERIAL_SNIPPET_CAPABILITIES } from '../src/material-snippet.js';
import { AGENT_SCENE_CAPABILITIES, AGENT_SCENE_DEFAULT_LIMITS, RENDER_PASS_NAMES, RENDER_PASS_DEPTH_RANGE, VIDEO_ENCODERS, MAXIMUM_VIDEO_SECONDS, MAXIMUM_JOB_SIDE, MAXIMUM_JOB_PIXELS, MAXIMUM_MEMORY_MIB, BAKE_WORKER_MAXIMUM_SECONDS } from '../src/agent-scene-capabilities.js';
import { PATH_TRACER_CAPABILITIES } from '../src/path-tracer-capabilities.js';
import { chooseDenoiser, finishWithOidn } from './agent-path-trace-denoise.mjs';
-import { chooseNativeInstancing, chooseTraceBackend, createNativeTracerLocator, isNativeTimeLimitError, traceFrameNative, traceTexelsNative } from './agent-native-trace.mjs';
+import { chooseNativeInstancing, chooseNativeKernel, chooseTraceBackend, createNativeTracerLocator, isNativeTimeLimitError, traceFrameNative, traceTexelsNative } from './agent-native-trace.mjs';
import { createAgentAssetCatalog } from './agent-asset-catalog.mjs';
import { geometryOnlyGlb } from './agent-query-asset.mjs';
import { AGENT_ASSET_LIMITS, assertCombinedAssetBytes } from '../src/agent-asset-limits.js';
@@ -566,7 +566,7 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', brow
if (trace.backend === 'native') {
let exportedScene = false;
try {
- const exported = await jobPage.evaluate(input => window.__agentPreview.exportNativeScene(input), { ...frameInput, options: { width, height, bounces: tracerOptions.bounces, seed: tracerOptions.seed, fireflyClamp: 20, noiseThreshold: tracerOptions.noiseThreshold, instancing: chooseNativeInstancing() } });
+ const exported = await jobPage.evaluate(input => window.__agentPreview.exportNativeScene(input), { ...frameInput, options: { width, height, bounces: tracerOptions.bounces, seed: tracerOptions.seed, fireflyClamp: 20, noiseThreshold: tracerOptions.noiseThreshold, samples: tracerOptions.samples, instancing: chooseNativeInstancing(), kernel: chooseNativeKernel() } });
exportedScene = true;
traced = await traceFrameNative({ tracer: trace.tracer, exported, width, height, samples: tracerOptions.samples, bounces: tracerOptions.bounces,
seed: tracerOptions.seed, denoise: choice.denoise === true ? 'wavelet' : choice.denoise === false ? 'none' : choice.denoise, denoiser: choice.denoiser,
diff --git a/scripts/native-bake-page.js b/scripts/native-bake-page.js
index 127967f2..d53550d7 100644
--- a/scripts/native-bake-page.js
+++ b/scripts/native-bake-page.js
@@ -44,30 +44,44 @@ export function encodeNativeInputV2(input) {
}
/**
- * The scene of a native render job as the tables the job runner traces. With `instancing`, parts that share a geometry are
- * exported as FORMAT-v2 instances; a scene the instanced packer refuses (copies of an emissive mesh that emit different
- * triangles, say) is exported flattened, as a job without shared geometry is, and says why in `instancing` and
- * `instancingReason`. `modules` holds buildTraceScene, refuseLocalVolumes, buildNativeInput, buildJobNativeInput and `pack`
- * (src/path-tracer-gpu-pack.js), which the page loads on demand.
+ * The scene of a native render job as the tables the job runner traces. With the "cycles" kernel (`kernel`) every scene is
+ * exported as FORMAT-v2, so its materials reach the kernel whole; parts that share a geometry stay instances when
+ * `instancing` is on. A scene the packer refuses (copies of an emissive mesh that emit different triangles, say) is
+ * exported flattened, and a scene whose FORMAT-v2 materials cannot be built is exported as the v1 tables the legacy kernel
+ * reads; each says why in `instancing` and `instancingReason`, or `materialsReason`. `wholeMaterials` tells whether the
+ * kernel gets whole materials. `modules` holds buildTraceScene, refuseLocalVolumes, resolveTexture, buildNativeInput,
+ * buildJobNativeInput, buildFlattenedJobNativeInput and `pack` (src/path-tracer-gpu-pack.js), which the page loads on demand.
*/
-export function exportNativeSceneTables({ parts, environment, camera, options, instancing, modules }) {
- const { buildTraceScene, refuseLocalVolumes, buildNativeInput, buildJobNativeInput, pack } = modules;
- let traceScene = buildTraceScene(parts, environment, { instancing: instancing === true });
+export function exportNativeSceneTables({ parts, environment, camera, options, kernel, instancing, modules }) {
+ const { buildTraceScene, refuseLocalVolumes, resolveTexture, buildNativeInput, buildJobNativeInput, buildFlattenedJobNativeInput, pack } = modules;
+ const wholeMaterials = kernel === 'cycles';
+ let traceScene = buildTraceScene(parts, environment, { instancing: wholeMaterials && instancing === true });
refuseLocalVolumes(traceScene, 'the native path tracer');
let flattenedBecause;
if (traceScene.instances) {
try {
- const input = buildJobNativeInput(traceScene, camera, options, pack);
- return { ...encodeNativeInputV2(input), triangleCount: input.triangleCount, triangles: input.triangleCount, instances: input.instanceCount };
+ const input = buildJobNativeInput(traceScene, camera, options, pack, resolveTexture);
+ return { ...encodeNativeInputV2(input), triangleCount: input.triangleCount, triangles: input.triangleCount, instances: input.instanceCount, wholeMaterials };
} catch (error) {
if (!(error instanceof RangeError)) throw error;
flattenedBecause = error.message;
traceScene = buildTraceScene(parts, environment);
}
}
+ const instancingNote = flattenedBecause === undefined ? {} : { instancing: 'flattened', instancingReason: flattenedBecause };
+ let materialsReason;
+ if (wholeMaterials) {
+ try {
+ const input = buildFlattenedJobNativeInput(traceScene, camera, options, pack, resolveTexture);
+ return { ...encodeNativeInputV2(input), triangleCount: input.triangleCount, triangles: traceScene.indices.length / 3, wholeMaterials, ...instancingNote };
+ } catch (error) {
+ // The scene stays traceable: the v1 tables carry what the kernel read before, and the report says why they were used.
+ materialsReason = error.message;
+ }
+ }
const input = buildNativeInput(traceScene, camera, options, pack);
- return { tables: encodeNativeTables(input), triangleCount: input.triangleCount, triangles: traceScene.indices.length / 3,
- ...(flattenedBecause === undefined ? {} : { instancing: 'flattened', instancingReason: flattenedBecause }) };
+ return { tables: encodeNativeTables(input), triangleCount: input.triangleCount, triangles: traceScene.indices.length / 3, wholeMaterials: false,
+ ...(materialsReason === undefined ? {} : { materialsReason }), ...instancingNote };
}
function floatsFromBase64(data) {
diff --git a/src/path-tracer-scene.js b/src/path-tracer-scene.js
index 3fa898d7..d8b351c6 100644
--- a/src/path-tracer-scene.js
+++ b/src/path-tracer-scene.js
@@ -106,7 +106,8 @@ function defaultCanvas(width, height) {
return canvas;
}
-function resolveTexture(descriptor) {
+/** Pixels and sampling of a texture descriptor for the trace, or null when its image cannot be read. */
+export function resolveTexture(descriptor) {
const image = descriptor?.image;
const data = texturePixels(image);
if (!data) return null;
@@ -496,6 +497,8 @@ export function buildTraceScene(allParts, environment = {}, options = {}) {
...geometry,
bvh,
materials: parts.map(part => resolveMaterial(part.material ?? {})),
+ // The parts' own materials: the fields resolveMaterial leaves out, which the native "cycles" kernel shades.
+ partMaterials: parts.map(part => part.material ?? {}),
shadowOnlyParts: parts.some(part => part.shadowOnly === true) ? Uint8Array.from(parts, part => (part.shadowOnly === true ? 1 : 0)) : null,
lights: resolveLights(environment),
environment: resolveEnvironment(environment.map),
diff --git a/src/path-tracer.js b/src/path-tracer.js
index 3f7307d4..ef849071 100644
--- a/src/path-tracer.js
+++ b/src/path-tracer.js
@@ -45,13 +45,15 @@ const CONVERGENCE_LUMINANCE_FLOOR = 0.05;
const IGNORED_MAP_FIELDS = ['occlusionMap'];
// Shading layers of the rasterizer that the tracer's surface model has no term for.
const IGNORED_LAYERS = [
- ['anisotropy', material => material.anisotropy > 0],
- ['iridescence', material => material.iridescence > 0],
- ['sheen', material => (material.sheenColor ?? []).some(channel => channel !== 0)],
+ ['anisotropy', material => material.anisotropy > 0, true],
+ ['iridescence', material => material.iridescence > 0, true],
+ ['sheen', material => (material.sheenColor ?? []).some(channel => channel !== 0), true],
['moss', material => material.moss > 0],
- ['splat layers', material => Boolean(material.layers || Object.keys(material.layerMaps ?? {}).some(name => name !== 'clearcoatTexture'))],
+ ['splat layers', (material, kernelMaterials) => Boolean(material.layers || Object.keys(material.layerMaps ?? {}).some(name => !(kernelMaterials ? KERNEL_LAYER_MAPS : ['clearcoatTexture']).includes(name)))],
['lightMap', material => Boolean(material.lightMap)],
];
+// The layer maps the native "cycles" kernel reads with its whole materials (the roughness maps of the coat and the sheen have no slot).
+const KERNEL_LAYER_MAPS = ['clearcoatTexture', 'clearcoatNormalTexture', 'anisotropyTexture', 'sheenColorTexture'];
function hasGraphOpacity(material) {
return material.pattern?.type === 'graph' && Object.hasOwn(material.pattern.graph ?? {}, 'opacity');
@@ -974,7 +976,7 @@ export function toneMapToRgba8(linear, width, height, { viewTransform = 'aces',
}
/** Names what this scene asks for that the path tracer does not draw, so a result never claims more than it traced. */
-export function describeIgnoredFeatures(parts, environment, backend = 'cpu') {
+export function describeIgnoredFeatures(parts, environment, backend = 'cpu', { kernelMaterials = false } = {}) {
const notes = new Set();
const fog = environment?.volumeFog;
if (fog?.density > 0 && (fog.noiseAmount ?? 0.5) > 0) notes.add('volumeFog is traced as a uniform medium: noiseAmount, noiseScale and drift are ignored.');
@@ -984,10 +986,11 @@ export function describeIgnoredFeatures(parts, environment, backend = 'cpu') {
const material = part.material ?? {};
for (const field of IGNORED_MAP_FIELDS) if (material[field]) notes.add(`${field} is ignored.`);
if (material.tag && material.tag !== 'surface') notes.add(`${material.tag} materials are traced as plain surfaces.`);
- if (material.alphaMode === 'blend') notes.add(hasGraphOpacity(material) ? 'A graph opacity output is traced as a cutout at 0.5.' : 'Blended transparency is traced as opaque.');
- if (material.layerMaps?.clearcoatTexture || material.transmissionMap) notes.add('Graph clearcoat and transmission masks are read by the CPU tracer; the GPU tracers use the factors.');
+ if (material.alphaMode === 'blend' && hasGraphOpacity(material)) notes.add('A graph opacity output is traced as a cutout at 0.5.');
+ else if (material.alphaMode === 'blend' && !kernelMaterials) notes.add('Blended transparency is traced as opaque.');
+ if (!kernelMaterials && (material.layerMaps?.clearcoatTexture || material.transmissionMap)) notes.add('Graph clearcoat and transmission masks are read by the CPU tracer; the GPU tracers use the factors.');
if (material.emissionFresnel > 0 && backend !== 'cpu') notes.add('emission fresnel is ignored by the GPU path tracers; the cpu backend applies it.');
- for (const [name, isUsed] of IGNORED_LAYERS) if (isUsed(material)) notes.add(`${name} is ignored.`);
+ for (const [name, isUsed, kernelShades] of IGNORED_LAYERS) if (!(kernelMaterials && kernelShades) && isUsed(material, kernelMaterials)) notes.add(`${name} is ignored.`);
}
return [...notes];
}
diff --git a/tests/native-instancing-fallback.test.js b/tests/native-instancing-fallback.test.js
index b5278b0d..1de1076e 100644
--- a/tests/native-instancing-fallback.test.js
+++ b/tests/native-instancing-fallback.test.js
@@ -1,20 +1,20 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { buildNativeInput } from '../packages/render-native/pack-input.mjs';
-import { buildJobNativeInput } from '../packages/render-native/job-input.mjs';
-import { findNativeAddon, traceNative } from '../packages/render-native/index.mjs';
+import { buildFlattenedJobNativeInput, buildJobNativeInput } from '../packages/render-native/job-input.mjs';
+import { findNativeAddon, traceNative, upgradeNativeInputV1ToV2 } from '../packages/render-native/index.mjs';
import { traceFrameNative } from '../scripts/agent-native-trace.mjs';
import { exportNativeSceneTables } from '../scripts/native-bake-page.js';
import { createCamera } from '../src/path-tracer.js';
import * as pack from '../src/path-tracer-gpu-pack.js';
-import { buildTraceScene, refuseLocalVolumes } from '../src/path-tracer-scene.js';
+import { buildTraceScene, refuseLocalVolumes, resolveTexture } from '../src/path-tracer-scene.js';
const WIDTH = 16;
const HEIGHT = 16;
const camera = createCamera({ eye: [0, 0, 8], target: [0, 0, 0], fov: 50 }, WIDTH, HEIGHT);
const options = { width: WIDTH, height: HEIGHT, bounces: 3, seed: 1, fireflyClamp: 20, noiseThreshold: 0 };
const environment = { keyColor: [3, 3, 3], lightDirection: [0.4, 0.8, 0.5] };
-const modules = { buildTraceScene, refuseLocalVolumes, buildNativeInput, buildJobNativeInput, pack };
+const modules = { buildTraceScene, refuseLocalVolumes, resolveTexture, buildNativeInput, buildJobNativeInput, buildFlattenedJobNativeInput, pack };
// A large triangle and a small one, so a copy shrunk far enough keeps one and loses the other.
const mixedSizeMesh = {
@@ -39,13 +39,13 @@ function parts(matrices) {
}
function jobExport(matrices) {
- const exported = exportNativeSceneTables({ parts: parts(matrices), environment, camera, options, instancing: true, modules });
+ const exported = exportNativeSceneTables({ parts: parts(matrices), environment, camera, options, kernel: 'cycles', instancing: true, modules });
return JSON.parse(JSON.stringify({ ...exported, ignored: [] }));
}
+// What the kernel builds from the v1 tables of the flattened scene.
function mainsExport(matrices) {
- const input = buildNativeInput(buildTraceScene(parts(matrices), environment), camera, options, pack);
- return input;
+ return upgradeNativeInputV1ToV2(buildNativeInput(buildTraceScene(parts(matrices), environment), camera, options, pack));
}
for (const [name, scene] of Object.entries(scenes)) {
@@ -54,10 +54,12 @@ for (const [name, scene] of Object.entries(scenes)) {
const exported = jobExport(matrices);
assert.equal(exported.instancing, 'flattened');
assert.match(exported.instancingReason, reason);
- assert.equal(exported.schemaVersion, undefined, 'v1 tables');
+ assert.equal(exported.schemaVersion, 2, 'FORMAT-v2 tables without shared meshes');
+ assert.equal(exported.instances, undefined);
+ assert.equal(exported.wholeMaterials, true);
const main = mainsExport(matrices);
for (const [tableName, table] of Object.entries(main)) {
- if (!ArrayBuffer.isView(table)) continue;
+ if (!ArrayBuffer.isView(table) || tableName === 'kernelParams') continue;
assert.equal(exported.tables[tableName], Buffer.from(table.buffer, table.byteOffset, table.byteLength).toString('base64'), tableName);
}
});
diff --git a/tests/native-instancing-materials.test.js b/tests/native-instancing-materials.test.js
index e1bba53d..0551e7f4 100644
--- a/tests/native-instancing-materials.test.js
+++ b/tests/native-instancing-materials.test.js
@@ -1,11 +1,10 @@
import assert from 'node:assert/strict';
import test from 'node:test';
-import { buildNativeInput } from '../packages/render-native/pack-input.mjs';
-import { STRUCT_STRIDES_V2, upgradeNativeInputV1ToV2 } from '../packages/render-native/index.mjs';
-import { buildJobNativeInput } from '../packages/render-native/job-input.mjs';
+import { STRUCT_STRIDES_V2 } from '../packages/render-native/index.mjs';
+import { buildFlattenedJobNativeInput, 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 } from '../src/path-tracer-scene.js';
+import { buildTraceScene, resolveTexture } from '../src/path-tracer-scene.js';
const camera = createCamera({ eye: [0, 0, 8], target: [0, 0, 0], fov: 50 }, 16, 16);
const options = { width: 16, height: 16, samples: 1, bounces: 4, seed: 1, fireflyClamp: 20, noiseThreshold: 0 };
@@ -43,10 +42,9 @@ function materialRecords(input) {
return records;
}
-// What a native job without shared geometry sends: v1 tables, which the cycles kernel upgrades to FORMAT-v2.
+// What a native job without shared geometry sends.
function flattenedJobInput(parts) {
- const scene = buildTraceScene(parts, environment);
- return upgradeNativeInputV1ToV2(buildNativeInput(scene, camera, options, pack), options);
+ return buildFlattenedJobNativeInput(buildTraceScene(parts, environment), camera, options, pack, resolveTexture);
}
test('a part keeps the same material record whether or not other geometry in the job is shared', () => {
@@ -54,7 +52,7 @@ test('a part keeps the same material record whether or not other geometry in the
const flattened = materialRecords(flattenedJobInput(parts));
const instancedScene = buildTraceScene(parts, environment, { instancing: true });
assert.ok(instancedScene.instances, 'the two copies of one mesh make the job instanced');
- const instanced = materialRecords(buildJobNativeInput(instancedScene, camera, options, pack));
+ const instanced = materialRecords(buildJobNativeInput(instancedScene, camera, options, pack, resolveTexture));
assert.equal(instanced.length, parts.length);
parts.forEach((part, index) => assert.equal(instanced[index], flattened[index], `material record of ${part.name}`));
});
diff --git a/tests/native-job-materials.test.js b/tests/native-job-materials.test.js
new file mode 100644
index 00000000..d3807199
--- /dev/null
+++ b/tests/native-job-materials.test.js
@@ -0,0 +1,309 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { buildNativeInput } from '../packages/render-native/pack-input.mjs';
+import { STRUCT_STRIDES_V2, buildNativeInputV2, packMaterialRecordV2, upgradeNativeInputV1ToV2 } from '../packages/render-native/index.mjs';
+import { findNativeAddon, prepareCyclesInput, traceNative } from '../packages/render-native/index.mjs';
+import * as realTracer from '../packages/render-native/index.mjs';
+import { traceFrameNative } from '../scripts/agent-native-trace.mjs';
+import { reduceV1MaterialTable } from '../packages/render-native/pack-input-v2.mjs';
+import { packNativeMaterialTables } from '../packages/render-native/pack-input.mjs';
+import { buildFlattenedJobNativeInput, buildJobNativeInput, carriedMaterialFields } from '../packages/render-native/job-input.mjs';
+import { exportNativeSceneTables } from '../scripts/native-bake-page.js';
+import { createCamera, describeIgnoredFeatures } 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 = 24;
+const HEIGHT = 16;
+const camera = createCamera({ eye: [0, 0, 8], target: [0, 0, 0], fov: 50 }, WIDTH, HEIGHT);
+const packOptions = { width: WIDTH, height: HEIGHT, samples: 16, bounces: 4, seed: 7, fireflyClamp: 20, noiseThreshold: 0 };
+// The options the runner hands the tracer, which patch the kernel parameters of a v1 input as they are upgraded.
+const traceOptions = { width: WIDTH, height: HEIGHT, samples: 16, maxSeconds: 60, kernel: 'cycles', noiseThreshold: 0 };
+const environment = { keyColor: [3, 3, 3], lightDirection: [0.4, 0.8, 0.5] };
+
+function quad(scale, withUvs = true) {
+ return {
+ positions: Float32Array.from([-scale, -scale, 0, scale, -scale, 0, scale, scale, 0, -scale, scale, 0]),
+ normals: Float32Array.from([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]),
+ ...(withUvs ? { uvs: Float32Array.from([0, 0, 1, 0, 1, 1, 0, 1]) } : {}),
+ indices: Uint32Array.from([0, 1, 2, 0, 2, 3]),
+ };
+}
+
+const translate = x => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, 0, 0, 1];
+
+function checker(size, seedByte) {
+ const data = new Uint8ClampedArray(size * size * 4);
+ for (let texel = 0; texel < size * size; texel++) {
+ data.set([(texel * 37 + seedByte) % 256, (texel * 91) % 256, (texel * 13 + seedByte * 3) % 256, 255 - (texel % 4) * 40], texel * 4);
+ }
+ return { image: { width: size, height: size, data }, colorSpace: 'srgb' };
+}
+
+function part(name, x, material, scale = 0.8) {
+ return { name, geometry: quad(scale), matrix: translate(x), material };
+}
+
+// Scenes whose materials set none of the fields the v1 job tables drop: their job input must not change.
+const unchangedScenes = {
+ 'plain surfaces and an emitter': [
+ part('floor', 0, { color: [0.6, 0.6, 0.6], roughness: 0.9 }),
+ part('metal', 0.1, { color: [0.9, 0.7, 0.3], metallic: 1, roughness: 0.2 }),
+ part('lamp', 0.2, { color: [1, 0.8, 0.6], emission: 3 }),
+ ],
+ 'textures': [
+ part('painted', 0, { color: [1, 1, 1], map: checker(4, 1), normalMap: checker(4, 2), metallicRoughnessMap: checker(4, 3), roughness: 1, metallic: 1 }),
+ part('screen', 0.1, { color: [1, 1, 1], emission: 2, emissiveMap: checker(2, 4) }),
+ part('leaf', 0.2, { color: [1, 1, 1], map: checker(4, 5), alphaMode: 'mask', alphaCutoff: 0.4 }),
+ ],
+ 'glass, clearcoat, subsurface and a light mask': [
+ part('glass', 0, { color: [1, 1, 1], transmission: 0.9, ior: 1.45, roughness: 0.05 }),
+ part('lacquer', 0.1, { color: [0.7, 0.1, 0.1], clearcoat: 0.8, clearcoatRoughness: 0.1 }),
+ part('wax', 0.2, { color: [0.9, 0.8, 0.6], subsurface: { weight: 0.5, radius: [1, 0.4, 0.2] } }),
+ part('masked', 0.3, { color: [0.5, 0.5, 0.9], lightMask: 1 }),
+ ],
+ 'fields set to their defaults': [
+ part('blend at full opacity', 0, { color: [0.5, 0.5, 0.5], alphaMode: 'blend', opacity: 1 }),
+ part('black sheen', 0.1, { color: [0.5, 0.5, 0.5], sheenColor: [0, 0, 0], sheenRoughness: 0 }),
+ part('white specular tint', 0.2, { color: [0.9, 0.9, 0.9], metallic: 1, specularColor: [1, 1, 1], specular: 1 }),
+ part('no attenuation', 0.3, { color: [1, 1, 1], transmission: 0.5, attenuationDistance: Infinity, attenuationColor: [0.3, 0.3, 0.3] }),
+ part('flat film', 0.4, { color: [0.5, 0.5, 0.5], iridescence: 0, iridescenceThickness: 400, iridescenceIor: 1.3, anisotropy: 0, anisotropyRotation: 0 }),
+ part('one sided', 0.5, { color: [0.5, 0.5, 0.5], doubleSided: false }),
+ ],
+};
+
+function sameBytes(label, actual, expected) {
+ const keys = new Set([...Object.keys(actual), ...Object.keys(expected)]);
+ for (const key of keys) {
+ const left = actual[key];
+ const right = expected[key];
+ if (ArrayBuffer.isView(left) && ArrayBuffer.isView(right)) {
+ const leftBytes = new Uint8Array(left.buffer, left.byteOffset, left.byteLength);
+ const rightBytes = new Uint8Array(right.buffer, right.byteOffset, right.byteLength);
+ assert.equal(Buffer.compare(leftBytes, rightBytes), 0, `${label}: table ${key} differs`);
+ } else {
+ assert.deepEqual(left, right, `${label}: ${key} differs`);
+ }
+ }
+}
+
+// What a native job without carried fields sent before: the v1 tables, upgraded by the kernel with the trace options.
+function legacyJobInput(parts) {
+ return prepareCyclesInput(buildNativeInput(buildTraceScene(parts, environment), camera, packOptions, pack), traceOptions);
+}
+
+for (const [sceneName, parts] of Object.entries(unchangedScenes)) {
+ test(`a flattened job with ${sceneName} sends the tables it always did`, () => {
+ const scene = buildTraceScene(parts, environment);
+ const jobInput = buildFlattenedJobNativeInput(scene, camera, packOptions, pack, resolveTexture);
+ sameBytes(sceneName, prepareCyclesInput(jobInput, traceOptions), legacyJobInput(parts));
+ });
+}
+
+test('shared geometry without carried fields keeps the instanced input it had', () => {
+ const shared = quad(1);
+ const parts = [
+ { name: 'copy-a', geometry: shared, matrix: translate(-2), material: { color: [0.8, 0.2, 0.2], roughness: 0.5, map: checker(4, 9) } },
+ { name: 'copy-b', geometry: shared, matrix: translate(2), material: { color: [0.2, 0.2, 0.8], clearcoat: 0.5, transmission: 0.2 } },
+ ...unchangedScenes['plain surfaces and an emitter'],
+ ];
+ const scene = buildTraceScene(parts, environment, { instancing: true });
+ assert.ok(scene.instances);
+ // The instanced input before the carried fields: the same reduction, without them.
+ const tables = packNativeMaterialTables(scene.traceMaterials, null, pack);
+ const reducedMaterials = reduceV1MaterialTable({ materials: tables.materials, atlas: tables.atlas, atlasWidth: tables.atlasWidth,
+ subsurfaceVec4Offset: tables.subsurfaceVec4Offset, materialCount: scene.traceMaterials.length });
+ const instances = scene.instances.map(instance => {
+ const lightMask = reducedMaterials.materialLightMasks[instance.materialIndex] ?? 0xffffffff;
+ return { ...instance, lightMask, lightMaskHi: lightMask === 0xffffffff ? 0xffffffff : 0 };
+ });
+ const { fireflyClamp, ...withoutClamp } = packOptions;
+ const before = buildNativeInputV2(scene, camera, { ...withoutClamp, instances, reducedMaterials });
+ sameBytes('instanced', buildJobNativeInput(scene, camera, packOptions, pack, resolveTexture), before);
+});
+
+function recordFloats(input, index) {
+ const offset = input.materials.byteOffset + index * STRUCT_STRIDES_V2.material;
+ return {
+ floats: new Float32Array(input.materials.buffer, offset, STRUCT_STRIDES_V2.material / 4),
+ words: new Uint32Array(input.materials.buffer, offset, STRUCT_STRIDES_V2.material / 4),
+ indices: new Int32Array(input.materials.buffer, offset, STRUCT_STRIDES_V2.material / 4),
+ };
+}
+
+const ALPHA_BLEND = 2;
+
+// A part for each field the v1 tables drop, laid out as the record fields (float index) they land in.
+const carriedParts = [
+ part('pane', 0, { color: [0.6, 0.8, 1], opacity: 0.3, alphaMode: 'blend', roughness: 0.1 }),
+ part('velvet', 0.1, { color: [0.4, 0.1, 0.5], sheenColor: [0.9, 0.8, 0.7], sheenRoughness: 0.4, roughness: 0.8 }),
+ part('glass', 0.2, { color: [1, 1, 1], transmission: 0.9, ior: 1.45, roughness: 0.05, attenuationDistance: 2, attenuationColor: [0.5, 0.8, 0.6] }),
+ part('foil', 0.3, { color: [0.9, 0.8, 0.5], metallic: 1, anisotropy: 0.6, anisotropyRotation: 0.5, iridescence: 0.5, iridescenceThickness: 500, iridescenceIor: 1.4, specular: 0.8, specularColor: [1, 0.5, 0.2], roughness: 0.3 }),
+ part('lacquer', 0.4, { color: [0.7, 0.1, 0.1], clearcoat: 0.8, clearcoatRoughness: 0.1, coatTint: [0.5, 0.9, 0.5], coatIor: 1.6, coatNormalScale: 0.7, thinWalled: true, diffuseRoughness: 0.3, bumpDistance: 0.02 }),
+ part('cloth', 0.5, { color: [0.4, 0.4, 0.8], sheenColor: [1, 1, 1], anisotropy: 0.4, map: checker(4, 6), layerMaps: { sheenColorTexture: checker(2, 7), anisotropyTexture: checker(2, 8), clearcoatNormalTexture: checker(2, 9) }, clearcoat: 0.5 }),
+];
+
+test('a flattened job sends blend opacity, sheen, specular, anisotropy, iridescence, attenuation and the coat to the kernel', () => {
+ const scene = buildTraceScene(carriedParts, environment);
+ const input = buildFlattenedJobNativeInput(scene, camera, packOptions, pack, resolveTexture);
+ const pane = recordFloats(input, 0);
+ assert.equal(pane.words[49], ALPHA_BLEND);
+ assert.ok(Math.abs(pane.floats[14] - 0.3) < 1e-6, 'opacity');
+ const velvet = recordFloats(input, 1);
+ assert.deepEqual(Array.from(velvet.floats.slice(24, 28)).map(value => Number(value.toFixed(3))), [0.9, 0.8, 0.7, 1]);
+ assert.ok(Math.abs(velvet.floats[44] - 0.4) < 1e-6, 'sheen roughness');
+ const glass = recordFloats(input, 2);
+ assert.equal(glass.floats[31], 2);
+ assert.deepEqual(Array.from(glass.floats.slice(28, 31)).map(value => Number(value.toFixed(3))), [0.5, 0.8, 0.6]);
+ const foil = recordFloats(input, 3);
+ assert.ok(Math.abs(foil.floats[11] - 0.6) < 1e-6, 'anisotropy');
+ assert.ok(Math.abs(foil.floats[12] - 0.5) < 1e-6, 'anisotropy rotation');
+ assert.ok(Math.abs(foil.floats[6] - 0.4) < 1e-6, 'specular ior level is half the specular factor');
+ assert.deepEqual(Array.from(foil.floats.slice(8, 11)).map(value => Number(value.toFixed(3))), [1, 0.5, 0.2]);
+ assert.ok(foil.floats[22] > 0, 'thin film thickness');
+ assert.ok(Math.abs(foil.floats[23] - 1.4) < 1e-6, 'thin film ior');
+ const lacquer = recordFloats(input, 4);
+ assert.deepEqual(Array.from(lacquer.floats.slice(16, 19)).map(value => Number(value.toFixed(3))), [0.5, 0.9, 0.5]);
+ assert.ok(Math.abs(lacquer.floats[21] - 1.6) < 1e-6, 'coat ior');
+ assert.ok(Math.abs(lacquer.floats[47] - 0.7) < 1e-6, 'coat normal scale');
+ assert.ok(Math.abs(lacquer.floats[7] - 0.3) < 1e-6, 'diffuse roughness');
+ assert.ok(Math.abs(lacquer.floats[50] - 0.02) < 1e-6, 'bump distance');
+ assert.notEqual(lacquer.words[48] & 2, 0, 'thin walled flag');
+ const cloth = recordFloats(input, 5);
+ assert.ok(cloth.indices[60] >= 0, 'sheen texture');
+ assert.ok(cloth.indices[61] >= 0, 'anisotropy texture');
+ assert.ok(cloth.indices[59] >= 0, 'coat normal texture');
+ assert.ok(cloth.indices[52] >= 0, 'base colour texture still comes from the atlas');
+});
+
+test('a flattened and an instanced job send the same record for each part', () => {
+ const shared = quad(1);
+ const parts = [
+ { name: 'copy-a', geometry: shared, matrix: translate(-2), material: { color: [0.8, 0.2, 0.2] } },
+ { name: 'copy-b', geometry: shared, matrix: translate(2), material: { color: [0.2, 0.2, 0.8] } },
+ ...carriedParts,
+ ];
+ const flattened = buildFlattenedJobNativeInput(buildTraceScene(parts, environment), camera, packOptions, pack, resolveTexture);
+ const instancedScene = buildTraceScene(parts, environment, { instancing: true });
+ assert.ok(instancedScene.instances);
+ const instanced = buildJobNativeInput(instancedScene, camera, packOptions, pack, resolveTexture);
+ assert.equal(instanced.materialCount, flattened.materialCount);
+ assert.equal(Buffer.compare(Buffer.from(instanced.materials), Buffer.from(flattened.materials)), 0, 'material records');
+ assert.equal(Buffer.compare(Buffer.from(instanced.textureDescriptors), Buffer.from(flattened.textureDescriptors)), 0, 'texture descriptors');
+});
+
+test('carried fields never replace what the v1 tables carry', () => {
+ const material = { color: [0.6, 0.8, 1], opacity: 0.3, alphaMode: 'blend', map: checker(4, 3), alphaCutoff: 0.5 };
+ const scene = buildTraceScene([part('cutout', 0, { ...material, alphaMode: 'mask', opacity: 0.3 })], environment);
+ const carried = carriedMaterialFields(scene.partMaterials[0], scene.materials[0], resolveTexture);
+ assert.equal(carried.alphaMode, undefined, 'a mask keeps its cutoff and is not blended');
+ const blended = carriedMaterialFields({ alphaMode: 'blend', opacity: 1 }, { alphaCutoff: 0, texture: null }, resolveTexture);
+ assert.deepEqual(blended, {}, 'blending at full opacity changes no pixel and stays opaque');
+ const graphCutout = carriedMaterialFields({ alphaMode: 'blend', opacity: 0.4 }, { alphaCutoff: 0.5 }, resolveTexture);
+ assert.deepEqual(graphCutout, {}, 'a graph opacity output stays the cutout at one half');
+});
+
+test('layer textures reach the record only while their layer is on', () => {
+ const resolved = { transmission: 0, clearcoat: 0, transmissionTexture: { width: 1, height: 1, data: new Uint8ClampedArray(4) }, clearcoatTexture: { width: 1, height: 1, data: new Uint8ClampedArray(4) } };
+ assert.deepEqual(carriedMaterialFields({}, resolved, resolveTexture), {});
+ const layered = carriedMaterialFields({}, { ...resolved, transmission: 1, clearcoat: 1 }, resolveTexture);
+ assert.deepEqual(Object.keys(layered.textures).sort(), ['coatWeightRoughness', 'transmission']);
+});
+
+test('a carried record equals the record of the whole material where the tables agree with it', () => {
+ const material = { color: [0.4, 0.1, 0.5], roughness: 0.8, sheenColor: [0.9, 0.8, 0.7], sheenRoughness: 0.4, opacity: 0.5, alphaMode: 'blend', attenuationDistance: 3, attenuationColor: [0.2, 0.9, 0.4], transmission: 0.5 };
+ const scene = buildTraceScene([part('one', 0, material)], environment);
+ const input = buildFlattenedJobNativeInput(scene, camera, packOptions, pack, resolveTexture);
+ const whole = packMaterialRecordV2({ ...material, ...scene.materials[0] }, null).bytes;
+ const sent = new Uint8Array(input.materials.buffer, input.materials.byteOffset, STRUCT_STRIDES_V2.material);
+ assert.equal(Buffer.compare(Buffer.from(sent), Buffer.from(whole)), 0);
+});
+
+test('the job report keeps a note only for what the kernel does not shade', () => {
+ const parts = [
+ { material: { alphaMode: 'blend', opacity: 0.4, anisotropy: 0.5, iridescence: 0.3, sheenColor: [1, 1, 1], transmissionMap: {}, layerMaps: { clearcoatTexture: {}, sheenColorTexture: {}, anisotropyTexture: {}, clearcoatNormalTexture: {} } } },
+ ];
+ const legacy = describeIgnoredFeatures(parts, { map: {} });
+ assert.ok(legacy.includes('Blended transparency is traced as opaque.'));
+ assert.ok(legacy.includes('sheen is ignored.'));
+ const whole = describeIgnoredFeatures(parts, { map: {} }, 'cpu', { kernelMaterials: true });
+ assert.deepEqual(whole, []);
+ const left = describeIgnoredFeatures([
+ { material: { alphaMode: 'blend', pattern: { type: 'graph', graph: { opacity: 1 } }, moss: 1, occlusionMap: {}, layerMaps: { sheenRoughnessTexture: {}, clearcoatRoughnessTexture: {} }, lightMap: {}, tag: 'liquid' } },
+ ], { map: {} }, 'cpu', { kernelMaterials: true });
+ assert.deepEqual(left.sort(), [
+ 'A graph opacity output is traced as a cutout at 0.5.', 'lightMap is ignored.', 'liquid materials are traced as plain surfaces.', 'moss is ignored.',
+ 'occlusionMap is ignored.', 'splat layers is ignored.',
+ ].sort());
+});
+
+function exportModules(overrides = {}) {
+ return { buildTraceScene, refuseLocalVolumes: () => {}, resolveTexture, buildNativeInput: (scene, view, options, tables) => buildNativeInput(scene, view, options, tables),
+ buildJobNativeInput, buildFlattenedJobNativeInput, pack, ...overrides };
+}
+
+function exportScene(parts, { kernel = 'cycles', instancing = true, modules = exportModules() } = {}) {
+ return exportNativeSceneTables({ parts, environment, camera, options: packOptions, kernel, instancing, modules });
+}
+
+test('every cycles job is exported as FORMAT-v2, instanced or not, and the legacy kernel keeps the v1 tables', () => {
+ const flattened = exportScene(carriedParts);
+ assert.equal(flattened.schemaVersion, 2);
+ assert.equal(flattened.wholeMaterials, true);
+ assert.equal(flattened.instances, undefined);
+ const shared = quad(1);
+ const sharedParts = [part('a', -2, { color: [1, 0, 0] }), part('b', 2, { color: [0, 1, 0] })].map(entry => ({ ...entry, geometry: shared }));
+ const instanced = exportScene(sharedParts);
+ assert.equal(instanced.schemaVersion, 2);
+ assert.equal(instanced.instances, 2);
+ const withoutInstancing = exportScene(sharedParts, { instancing: false });
+ assert.equal(withoutInstancing.schemaVersion, 2);
+ assert.equal(withoutInstancing.instances, undefined);
+ const legacy = exportScene(carriedParts, { kernel: 'legacy' });
+ assert.equal(legacy.schemaVersion, undefined);
+ assert.equal(legacy.wholeMaterials, false);
+ assert.ok(legacy.tables.triangles);
+});
+
+test('a scene whose FORMAT-v2 materials cannot be built is exported as v1 tables with the reason', () => {
+ const exported = exportScene(carriedParts, { modules: exportModules({ buildFlattenedJobNativeInput: () => { throw new RangeError('too many textures'); } }) });
+ assert.equal(exported.schemaVersion, undefined);
+ assert.equal(exported.wholeMaterials, false);
+ assert.equal(exported.materialsReason, 'too many textures');
+ assert.ok(exported.tables.triangles);
+});
+
+const addonSkip = findNativeAddon() ? false : 'the native addon is not built';
+
+// The linear colour the native tracer returned for a job frame of these parts, or undefined when the box has no adapter.
+async function traceJobFrame(parts, kernel, t) {
+ const exported = JSON.parse(JSON.stringify({ ...exportScene(parts, { kernel }), ignored: [] }));
+ let traced;
+ const recordingTracer = { ...realTracer, traceNative(tables, traceOptions) {
+ traced = realTracer.traceNative(tables, traceOptions);
+ return traced;
+ } };
+ try {
+ await traceFrameNative({ tracer: recordingTracer, exported, width: WIDTH, height: HEIGHT, samples: 8, denoise: 'none', kernel });
+ } catch (error) {
+ if (/adapter|Vulkan|ray quer/i.test(error.message)) return t.skip(error.message);
+ throw error;
+ }
+ return Buffer.from(traced.rgba.buffer, traced.rgba.byteOffset, traced.rgba.byteLength);
+}
+
+test('a job frame of a scene without carried fields has the pixels of the v1 tables', { skip: addonSkip }, async t => {
+ const parts = unchangedScenes['glass, clearcoat, subsurface and a light mask'];
+ const viaV2 = await traceJobFrame(parts, 'cycles', t);
+ if (!viaV2) return;
+ const direct = traceNative(buildNativeInput(buildTraceScene(parts, environment), camera, packOptions, pack), { width: WIDTH, height: HEIGHT, samples: 8, kernel: 'cycles' });
+ assert.deepEqual(viaV2, Buffer.from(direct.rgba.buffer, direct.rgba.byteOffset, direct.rgba.byteLength));
+});
+
+test('a job frame with a blend pane differs from the same scene traced from the v1 tables with the same kernel', { skip: addonSkip }, async t => {
+ const parts = [carriedParts[0], part('wall', -0.5, { color: [0.9, 0.2, 0.2] }, 1.5)];
+ const viaV2 = await traceJobFrame(parts, 'cycles', t);
+ if (!viaV2) return;
+ const direct = traceNative(buildNativeInput(buildTraceScene(parts, environment), camera, packOptions, pack), { width: WIDTH, height: HEIGHT, samples: 8, kernel: 'cycles' });
+ assert.notDeepEqual(viaV2, Buffer.from(direct.rgba.buffer, direct.rgba.byteOffset, direct.rgba.byteLength));
+});