Files
metal2vulkan/crates/metal2vulkan-runtime/src/executed_commands.rs
T
Anees Iqbal 48caeeb155 DegradeNotice::command_buffer: the command buffer whose work announced a notice
A consumer that makes some notices fatal (reims-vgpu's ReleasedCommandState,
PrivateResourceOption, HostDeallocatorDelayed) could not tell which command
buffer a notice was about: a ReleasedCommandState announced at a host step on
the scheduler's steps thread ended its whole VM. Now every notice carries the
ID of the command buffer whose work the runtime was doing on the announcing
thread, so the consumer can fail that one command buffer.

What changed:
- CommandBuffer::object_id: an ObjectId, minted at the command buffer's
  making from the process-wide count buffers, textures and heaps use.
- DegradeNotice::command_buffer: Option<ObjectId>, read at the notice's making
  from a thread-local scope (degradation::NoticesFor), as `call` reads the
  call being lowered. The scope is set where the runtime holds the command
  buffer and does its work: the compute and blit encoders' logged funnels and
  their other recording calls and ends, the render lowering's at-call funnel,
  its unit calls, its batches' lowering (on the lowering thread or a drain)
  and its end, the resource state encoder's calls, encoder openings, the
  commit (at the call or queued behind a render end), a batch's submission and
  the storage-form settle that rides on it, host steps, a rebind's encoding
  again, and GPU-time reads. Nested scopes name the innermost.
- None, explicitly, for a recovery of the device (device-recovered), for
  grouped-completion (several command buffers' batches), and inside the
  program's code the runtime calls (exit_guard::program, indirect command
  checks, executed-commands handlers).
- A bind's memoized notices, announced again at each later bind under
  announce_each, are named as that bind's: its command buffer and the call
  being lowered (the call was the first bind's before; same mechanism).
- ObjectId docs, docs/API.md section 9, docs/RUNTIME-PORTING.md; API
  signatures re-blessed (two records: the field and the method).

Tests (runtime crate, device tests):
- notice_command_buffers: an ICB execution recorded at a host step announces
  released-command-state on the steps thread naming the executing command
  buffer; its commit's host-replayed-indirect-commands, on the program's
  thread, names it too; private-resource-option of an ICB made between the
  encoder's calls names none.
- deferred_calls: the call-naming test also checks the command buffer under
  both lowering policies; a new test waits for the lowering thread to announce
  unsized-points after end_encoding and checks the thread, call and command
  buffer, and that a device notice between the encoder's calls names none.
- vertex_conversion_notices: a memoized bind notice (device-nan-encoding)
  announced at a second command buffer's draw names that command buffer and
  its own call index, with bound_notices_made proving the memo was reused.

(cherry picked from commit b4c2ba83ea726229f129a405ce6f432b0bdb0127)
2026-11-08 04:45:15 -08:00

138 lines
6.6 KiB
Rust

//! What an execution of an indirect command buffer runs, as the runtime reads its records: a
//! runtime extension, with no Metal counterpart.
//!
//! - **Why.** Which pipeline state an indirect command runs is decided by its record, which the
//! CPU or the GPU may write until the runtime reads it: in `commit` for the CPU's records, in
//! the host step for records the GPU may write (`IndirectCommandBuffer::gpu_written`), or on
//! the GPU alone (`gpu_execute.rs`). A program that decides something of its own by the
//! states the commands run (whether one can encode indirect commands, and which indirect
//! command buffers the command buffer made resident for writing) learns them there, from the
//! runtime, without reading the records again.
//! - **The hook.** [`CommandBuffer::add_executed_commands_handler`] adds, before `commit`, a
//! handler the runtime calls with an [`ExecutedCommands`] each time it reads an execution's
//! records, on the thread that reads them ([`RecordsRead`]), with no lock of the command buffer
//! held. An execution `commit` recorded from the CPU's records whose buffer an encoder makes
//! resident for writing before its batch is submitted is read again in its host step, and the
//! handler sees it again. An execution in an encoder the command buffer's failure runs nothing
//! of (`commit_failed`) is never read.
//! - **Its cost.** None without a handler but one lock of the command buffer's state per
//! execution read; with one, the facts are gathered from what the reading resolved already.
use super::command_buffer::CommandBuffer;
use super::degradation::NoticesFor;
use super::error::{invalid, Error, Problem};
use super::indirect_command_buffer::IndirectCommandBuffer;
use super::residency::Residency;
use std::sync::Arc;
/// A handler of a command buffer's executions ([`CommandBuffer::add_executed_commands_handler`]).
pub type ExecutedCommandsHandler = Arc<dyn Fn(&ExecutedCommands) + Send + Sync + 'static>;
/// Where the runtime read an execution's records ([`ExecutedCommands::read`]).
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RecordsRead {
/// In `commit`, on the committing thread: the CPU's records as they stood then.
Commit,
/// In the host step, on one of the steps' threads (`scheduler/steps.rs`): the records as the
/// GPU and the CPU left them, once the work before the execution completed.
HostStep,
/// Not on the host: the GPU reads them as it runs (`gpu_execute.rs`), so
/// [`ExecutedCommands::commands`] is empty and
/// [`ExecutedCommands::unread_states_can_encode`] tells of every state the records may name.
/// Decided in `commit`, on the committing thread, and again on the thread of each rebind of
/// a buffer over host pages before the command buffer's first submission, which records the
/// execution again.
Gpu,
}
/// A command of an execution that runs, as the runtime read it: one that dispatches or draws
/// with a pipeline state, its own or the encoder's that the buffer inherits.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExecutedCommand {
/// Its index in the buffer.
pub index: u64,
/// Whether the state it runs may encode, reset or copy indirect commands
/// (`ComputePipelineState::can_encode_indirect_commands`,
/// `RenderPipelineState::can_encode_indirect_commands`).
pub can_encode_indirect_commands: bool,
}
/// An execution of an indirect command buffer's commands as the runtime read its records
/// (the module docs).
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct ExecutedCommands {
/// The buffer executed.
pub buffer: IndirectCommandBuffer,
/// Where its records were read.
pub read: RecordsRead,
/// The commands of its range that run, in order. Empty where [`RecordsRead::Gpu`].
pub commands: Vec<ExecutedCommand>,
/// Where [`RecordsRead::Gpu`]: whether a state its records may name (the encoder's where the
/// buffer inherits it, else every state made with `supportIndirectCommandBuffers` alive at
/// the commit) may encode, reset or copy indirect commands. `false` otherwise.
pub unread_states_can_encode: bool,
/// The indirect command buffers the command buffer's encoders made resident for writing
/// (`use_resource` or `use_resources` with `ResourceUsage::WRITE`), as its residency stood
/// where the records were read: Metal's residency is the command buffer's, every encoder's.
pub writable: Vec<IndirectCommandBuffer>,
}
impl CommandBuffer {
/// Call `handler` each time the runtime reads the records of an execution of an indirect
/// command buffer of this command buffer, on the thread that reads them, with no lock of the
/// command buffer held (`executed_commands.rs`). A runtime extension, with no Metal
/// counterpart. Refused once the command buffer is committed (`Invalid`,
/// `Problem::HandlerAfterCommit`).
pub fn add_executed_commands_handler(
&self,
handler: ExecutedCommandsHandler,
) -> Result<(), Error> {
let mut state = self.state();
if state.enqueued() {
return Err(invalid(
"add_executed_commands_handler",
Problem::HandlerAfterCommit,
));
}
state.executed_handlers.push(handler);
Ok(())
}
/// Hand the handlers the facts `facts` gathers of an execution of `buffer` read at `read`,
/// under `residency`, the command buffer's, where it has handlers; with no lock held.
/// Whether the command buffer has an executed-commands handler, which an execution reports
/// the commands it ran to ([`report_executed`](Self::report_executed)).
pub(crate) fn reports_executed(&self) -> bool {
!self.state().executed_handlers.is_empty()
}
pub(crate) fn report_executed(
&self,
buffer: &IndirectCommandBuffer,
read: RecordsRead,
residency: &Residency,
facts: impl FnOnce() -> (Vec<ExecutedCommand>, bool),
) {
let handlers = self.state().executed_handlers.clone();
if handlers.is_empty() {
return;
}
let (commands, unread_states_can_encode) = facts();
let executed = ExecutedCommands {
buffer: buffer.clone(),
read,
commands,
unread_states_can_encode,
writable: residency.writable_indirect_command_buffers(),
};
// The program's code: no command buffer's work of the runtime's.
let _program = NoticesFor::none();
for handler in &handlers {
handler(&executed);
}
}
}