Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Refuse a bootstrapKernel switch on a film, close the session on a failed update, name NativeSession.update in its errors (#896)

Nguyen Xuan Anf208872ef534
diff --git a/packages/render-native/README.md b/packages/render-native/README.md
index 221fec1e..7cdbf7ac 100644
--- a/packages/render-native/README.md
+++ b/packages/render-native/README.md
@@ -101,6 +101,6 @@ A call must keep the image size and every render setting of the call that began
The pinned wgpu has no TLAS refit (`PreferUpdate` is accepted and rebuilds), so a rebuild is the cost of moving an instance; the BLAS, which hold the triangles, are never touched. `update` returns `uploadedTables`, `accelerationRebuilt`, `changedInstances`, `kernelRespecialized` (the new tables change which features the kernel text is specialized for, for example the first transmissive material, so the next render compiles) and the seconds. It clears the film, ends a progressive image, and the next frame equals the `traceNative` render of `newInput` byte for byte. The next frame reports `accelerationReused: true`, and `pipelineReused: false` only when `kernelRespecialized` was true.
-An update that changes a record count (instances, triangles, materials, emissive triangles, lights, light tree nodes, textures), the geometry, the textures, the environment map or which mesh an instance places is refused before anything changes, with an error that names what differs and says to create a new session. Moving an emissive instance means the input carries new `emissiveTriangles` and light tree tables for it, which the update applies like any other table.
+An update that changes a record count (instances, triangles, materials, emissive triangles, lights, light tree nodes, textures), the geometry, the textures, the environment map or which mesh an instance places is refused before anything changes, with an error that names what differs and says to create a new session. Moving an emissive instance means the input carries new `emissiveTriangles` and light tree tables for it, which the update applies like any other table. If the card fails part way through an applied update, the film is cleared and the session is closed (the error says so), so it never renders a half updated scene.
A frame may change these words of the 320 byte block: the camera, lens, aperture and sensor shift (bytes 112 to 191), `seed`, the bounce limits, clamps, filter, adaptive sampling, `film_exposure`, `filter_glossy`, the environment rotation and colour (204, 208), the fog fields (224 to 267) and `kernel_flags`. The scene-bound words stay with the session (above), until an update brings new selection weights.
diff --git a/packages/render-native/index.mjs b/packages/render-native/index.mjs
index f8188004..09464a06 100644
--- a/packages/render-native/index.mjs
+++ b/packages/render-native/index.mjs
@@ -369,22 +369,22 @@ function patchedCyclesKernelParams(v2Input, kernelParams, options, callerName) {
}
// Exported for tests; not part of the public API.
-export function resolveSessionCreation(input, options) {
+export function resolveSessionCreation(input, options, callerName = 'createNativeSession') {
if (input === null || typeof input !== 'object') {
- throw new TypeError('createNativeSession: `input` must be a FORMAT-v2 input object from buildNativeInputV2.');
+ throw new TypeError(`${callerName}: \`input\` must be a FORMAT-v2 input object from buildNativeInputV2.`);
}
if (input.schemaVersion !== FORMAT_V2_VERSION) {
throw new TypeError(
- `createNativeSession: \`input.schemaVersion\` must be ${FORMAT_V2_VERSION} (a FORMAT-v2 input from buildNativeInputV2); got ${JSON.stringify(input.schemaVersion)}. Convert a v1 input with upgradeNativeInputV1ToV2.`
+ `${callerName}: \`input.schemaVersion\` must be ${FORMAT_V2_VERSION} (a FORMAT-v2 input from buildNativeInputV2); got ${JSON.stringify(input.schemaVersion)}. Convert a v1 input with upgradeNativeInputV1ToV2.`
);
}
- requireSessionKernelParams(input.kernelParams, 'createNativeSession');
+ requireSessionKernelParams(input.kernelParams, callerName);
if (options === null || typeof options !== 'object') {
- throw new TypeError('createNativeSession: `options` must be an object.');
+ throw new TypeError(`${callerName}: \`options\` must be an object.`);
}
- requireCyclesKernelOption(options.kernel, 'createNativeSession');
- validatePackOptions(options, 'createNativeSession');
- return { ...input, kernelParams: patchedCyclesKernelParams(input, input.kernelParams, options, 'createNativeSession') };
+ requireCyclesKernelOption(options.kernel, callerName);
+ validatePackOptions(options, callerName);
+ return { ...input, kernelParams: patchedCyclesKernelParams(input, input.kernelParams, options, callerName) };
}
// Exported for tests; not part of the public API.
@@ -538,7 +538,7 @@ export function wrapNativeSession(nativeSession, sessionInput, sessionOptions) {
},
update(newInput) {
requireOpen('update');
- const nextInput = resolveSessionCreation(newInput, sessionOptions);
+ const nextInput = resolveSessionCreation(newInput, sessionOptions, 'NativeSession.update');
const changedTables = planNativeSessionUpdate(heldInput, nextInput);
const report = nativeSession.update({ kernelParams: nextInput.kernelParams, ...changedTables });
heldInput = nextInput;
diff --git a/packages/render-native/src/host/cycles_session.rs b/packages/render-native/src/host/cycles_session.rs
index 02998f3d..cb46c70b 100644
--- a/packages/render-native/src/host/cycles_session.rs
+++ b/packages/render-native/src/host/cycles_session.rs
@@ -14,7 +14,7 @@ use crate::cycles::{
check_update_params_keep_scene_shape, scan_scene_specialization, specialize_kernel_source,
trace_entry_point_for_params, SceneSpecialization,
};
-use crate::host::accel::{build_cycles_acceleration_structure, rebuild_cycles_tlas, CyclesAcceleration};
+use crate::host::accel::{build_cycles_acceleration_structure, rebuild_cycles_tlas, CyclesAcceleration, INSTANCE_RECORD_BYTES};
use crate::host::cycles_update::{choose_film_use, plan_instance_update, FilmRequest, ProgressiveFilm, UpdatableTable};
use crate::host::device::{failure, storage_buffer};
use crate::host::film::{image_accumulation_bytes, image_workgroups, read_accumulation};
@@ -373,6 +373,7 @@ impl CyclesSession {
accumulate: request.accumulate,
target_samples: request.target_samples,
frame_kernel_params: &frame_kernel_params,
+ bootstrap_kernel: request.bootstrap_kernel,
},
)?;
@@ -521,6 +522,58 @@ impl CyclesSession {
None => None,
};
+ // An empty acceleration (no triangles) holds a dummy TLAS that no ray reaches, so there is nothing to rebuild.
+ let held_instance_count = self.gpu.as_ref().ok_or_else(session_closed_failure)?.acceleration.instance_blas_indices.len();
+ let acceleration_rebuilt = held_instance_count > 0 && instance_plan.as_ref().is_some_and(|plan| plan.tlas_dirty);
+ if let (true, Some(bytes)) = (acceleration_rebuilt, new_instances) {
+ if bytes.len() != held_instance_count * INSTANCE_RECORD_BYTES {
+ return Err(failure(format!(
+ "The new instances table holds {} bytes but the acceleration structure holds {held_instance_count} instances ({} bytes). Create a new session for this input.",
+ bytes.len(),
+ held_instance_count * INSTANCE_RECORD_BYTES
+ )));
+ }
+ }
+
+ // Past this point the session changes. A step that still fails (a driver or device error) cannot be undone, so the
+ // film is cleared and the session closed rather than left half updated.
+ let applied = self.apply_tables(update, new_instances, acceleration_rebuilt);
+ let (upload_seconds, acceleration_rebuild_seconds) = match applied {
+ Ok(seconds) => seconds,
+ Err(error) => {
+ self.clear_progressive_film();
+ self.gpu = None;
+ return Err(failure(format!(
+ "The update failed part way ({error}) and the session was closed so it cannot render a half updated scene. Create a new session with createNativeSession(input)."
+ )));
+ }
+ };
+ self.session_kernel_params = update.kernel_params.to_vec();
+ let respecialized = self.retained.specialization();
+ let kernel_respecialized = respecialized != self.scene_specialization;
+ self.scene_specialization = respecialized;
+ self.clear_progressive_film();
+
+ Ok(CyclesUpdateReport {
+ uploaded_tables: update.tables.iter().map(|(table, _)| table.input_name()).collect(),
+ acceleration_rebuilt,
+ changed_instances: instance_plan.map_or(0, |plan| plan.changed_instances as u32),
+ kernel_respecialized,
+ upload_seconds,
+ acceleration_rebuild_seconds,
+ update_seconds: update_started.elapsed().as_secs_f64(),
+ })
+ }
+}
+
+impl CyclesSession {
+ /// Uploads the new tables, rebuilds the TLAS when asked and rebinds the film; returns the seconds spent uploading and rebuilding.
+ fn apply_tables(
+ &mut self,
+ update: &CyclesSceneUpdate<'_>,
+ new_instances: Option<&[u8]>,
+ acceleration_rebuilt: bool,
+ ) -> Result<(f64, f64)> {
let upload_started = Instant::now();
let context = &self.context;
let gpu = self.gpu.as_mut().ok_or_else(session_closed_failure)?;
@@ -532,7 +585,6 @@ impl CyclesSession {
let upload_seconds = upload_started.elapsed().as_secs_f64();
let rebuild_started = Instant::now();
- let acceleration_rebuilt = instance_plan.as_ref().is_some_and(|plan| plan.tlas_dirty);
if let (true, Some(bytes)) = (acceleration_rebuilt, new_instances) {
rebuild_cycles_tlas(&context.device, &context.queue, &mut gpu.acceleration, bytes)?;
}
@@ -541,21 +593,7 @@ impl CyclesSession {
if !update.tables.is_empty() {
rebind_film(context, gpu)?;
}
- self.session_kernel_params = update.kernel_params.to_vec();
- let respecialized = self.retained.specialization();
- let kernel_respecialized = respecialized != self.scene_specialization;
- self.scene_specialization = respecialized;
- self.clear_progressive_film();
-
- Ok(CyclesUpdateReport {
- uploaded_tables: update.tables.iter().map(|(table, _)| table.input_name()).collect(),
- acceleration_rebuilt,
- changed_instances: instance_plan.map_or(0, |plan| plan.changed_instances as u32),
- kernel_respecialized,
- upload_seconds,
- acceleration_rebuild_seconds,
- update_seconds: update_started.elapsed().as_secs_f64(),
- })
+ Ok((upload_seconds, acceleration_rebuild_seconds))
}
}
diff --git a/packages/render-native/src/host/cycles_update.rs b/packages/render-native/src/host/cycles_update.rs
index 619b4558..508539de 100644
--- a/packages/render-native/src/host/cycles_update.rs
+++ b/packages/render-native/src/host/cycles_update.rs
@@ -128,6 +128,8 @@ pub(crate) struct ProgressiveFilm {
pub(crate) target_samples: u32,
pub(crate) samples_drawn: u32,
pub(crate) signature: Vec<u8>,
+ /// The diagnostic kernel adds a different quantity, so a film cannot mix calls of the two kernels.
+ pub(crate) bootstrap_kernel: bool,
}
/// What one render call asks of the film.
@@ -138,6 +140,7 @@ pub(crate) struct FilmRequest<'a> {
pub(crate) accumulate: bool,
pub(crate) target_samples: Option<u32>,
pub(crate) frame_kernel_params: &'a [u8],
+ pub(crate) bootstrap_kernel: bool,
}
/// Which samples a render call draws and into which film.
@@ -182,6 +185,7 @@ pub(crate) fn choose_film_use(film: Option<&ProgressiveFilm>, request: &FilmRequ
target_samples,
samples_drawn: request.samples,
signature,
+ bootstrap_kernel: request.bootstrap_kernel,
};
return Ok(FilmUse { first_sample: 0, total_samples: target_samples, starts_new_image: true, film_after: Some(film_after) });
};
@@ -204,6 +208,12 @@ pub(crate) fn choose_film_use(film: Option<&ProgressiveFilm>, request: &FilmRequ
"The kernel parameters (camera, seed, sampler or other render settings) differ from the ones this film started with, so the new samples would not belong to the same image. {REQUIRES_RESET}"
)));
}
+ if request.bootstrap_kernel != film.bootstrap_kernel {
+ return Err(failure(format!(
+ "bootstrapKernel is {} but the film started with {}, and the two kernels do not add the same samples. {REQUIRES_RESET}",
+ request.bootstrap_kernel, film.bootstrap_kernel
+ )));
+ }
let remaining_samples = film.target_samples - film.samples_drawn;
if request.samples > remaining_samples {
return Err(failure(format!(
@@ -316,7 +326,7 @@ mod tests {
}
fn request<'a>(kernel_params: &'a [u8], samples: u32, accumulate: bool, target_samples: Option<u32>) -> FilmRequest<'a> {
- FilmRequest { width: 24, height: 16, samples, accumulate, target_samples, frame_kernel_params: kernel_params }
+ FilmRequest { width: 24, height: 16, samples, accumulate, target_samples, frame_kernel_params: kernel_params, bootstrap_kernel: false }
}
#[test]
@@ -369,6 +379,11 @@ mod tests {
moved_camera[200] ^= 1;
let camera_message = choose_film_use(Some(&started), &request(&moved_camera, 2, true, None)).unwrap_err().to_string();
assert!(camera_message.contains("kernel parameters"), "{camera_message}");
+
+ let mut bootstrap_call = request(&params, 2, true, None);
+ bootstrap_call.bootstrap_kernel = true;
+ let bootstrap_message = choose_film_use(Some(&started), &bootstrap_call).unwrap_err().to_string();
+ assert!(bootstrap_message.contains("bootstrapKernel") && bootstrap_message.contains("reset()"), "{bootstrap_message}");
}
#[test]
diff --git a/tests/native-session.test.js b/tests/native-session.test.js
index 55f01afd..227e0407 100644
--- a/tests/native-session.test.js
+++ b/tests/native-session.test.js
@@ -369,7 +369,7 @@ test('a refused update changes nothing and a closed session refuses reset, resol
scene.instances.push({ geometry: UNIT_QUAD, matrix: horizontalQuadMatrix(1, 1, true), materialIndex: 0 });
});
assert.throws(() => session.update(withExtraQuad), { message: /instanceCount/ });
- assert.throws(() => session.update(undefined), { name: 'TypeError', message: /FORMAT-v2/ });
+ assert.throws(() => session.update(undefined), { name: 'TypeError', message: /^NativeSession.update: `input` must be a FORMAT-v2/ });
assert.deepEqual(calls, [], 'the addon is not called for a refused update');
session.close();
for (const method of ['reset', 'resolve']) {
@@ -379,6 +379,29 @@ test('a refused update changes nothing and a closed session refuses reset, resol
assert.deepEqual(calls, []);
});
+test('an update the addon refuses leaves the wrapper diffing against the scene the addon still holds', () => {
+ const current = roomInputWith(() => {});
+ const changed = roomInputWith((scene) => {
+ scene.materials[0].color = [0.2, 0.5, 0.8];
+ });
+ const updates = [];
+ let refuse = true;
+ const nativeSession = {
+ update(options) {
+ updates.push(options);
+ if (refuse) throw new Error('The update failed part way');
+ return {};
+ },
+ close() {},
+ };
+ const session = wrapNativeSession(nativeSession, resolveSessionCreation(current, {}), {});
+ assert.throws(() => session.update(changed), { message: /failed part way/ });
+ refuse = false;
+ session.update(changed);
+ assert.equal(updates.length, 2);
+ assert.deepEqual(Object.keys(updates[1]).sort(), Object.keys(updates[0]).sort(), 'the retry diffs against the old scene, so it sends the same tables');
+});
+
// ---- Renders on the GPU ----
test('a session render equals the one-shot render byte for byte and the second frame reuses the setup', { skip: skipWithoutAddon }, () => {