From 82d1c62eb58147d019d86ab4abc565ec593d6636 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 23 Nov 2026 08:21:07 -0800 Subject: [PATCH] Player: the plural residency declarations play and count as their single forms A compute encoder's useResources:count:usage: and useHeaps:count: were refused ("not played on a compute encoder"), so a play stopped at the first one; a guest recording on the Iris Xe stopped at frame 290 of 430 there. They play now as Metal defines them: each resource of the list as useResource:usage: with the call's usage, through the runtime's use_resource, and each heap of the list as useHeap:, through use_heap. The stream records each C array and its count as one list (docs/REPLAY-STREAM.md), so the usage is argument 1. What each read before: - The compute encoder read useResource:usage: alone (its resource as {Buffer: object} or {Texture: object}, the usage at argument 1) and refused both plurals. - The covers rule and the unwritten count read useResource:usage:'s resource with as_object, which takes only a bare object, so a recorded {Buffer: object} wrote and uncovered nothing in them; both plurals fell through as calls that only name objects. They read the resource now as the stream records it (Value::as_resource), and each resource or heap of a plural as its single form. - The render encoder reads none of its residency declarations' arguments (it plays them all as nothing), so it read no usage wrongly. The player's resolvers gain held (a resource value) and block_of (a heap id), which resource and heap_of now call. docs/REPLAY-STREAM.md says compute encoders play the four forms through the runtime and that the unwritten count, the skipped calls and the covers rule read the plurals as their single forms. Tests (the two commits before this): the device test of a compute encoder's four forms, the unwritten test of the plural and of the singular on the format, and the covers test of the plurals. Not run here: no local builds; to be checked on development. --- .../src/replay/covers.rs | 61 +++++++++++---- .../src/replay/new_runtime/encoders.rs | 77 +++++++++++++++---- .../src/replay/new_runtime/mod.rs | 13 +++- .../src/replay/new_runtime/unwritten.rs | 21 +++-- .../src/replay/value.rs | 9 +++ docs/REPLAY-STREAM.md | 17 ++-- 6 files changed, 155 insertions(+), 43 deletions(-) diff --git a/crates/metal2vulkan-validation/src/replay/covers.rs b/crates/metal2vulkan-validation/src/replay/covers.rs index b192cff93..b50e08c64 100644 --- a/crates/metal2vulkan-validation/src/replay/covers.rs +++ b/crates/metal2vulkan-validation/src/replay/covers.rs @@ -10,12 +10,14 @@ //! dispatch's to the texture slots its pipeline state's interface marks written //! ([`Backend::texture_writes`](super::player::Backend::texture_writes); none where the backend //! answers no access), a render pass's to every texture its descriptor names, a blit's to its -//! destination, `generateMipmapsForTexture:`, a `useResource:usage:` with write usage, and the +//! destination, `generateMipmapsForTexture:`, a `useResource:usage:` with write usage (each +//! resource of a `useResources:count:usage:` as its own), and the //! CPU's `replaceRegion:…`. A write of a texture over a buffer is the buffer's. An object is //! written again by the player's writes into it (`m2v.write`, its contents at creation, a copy //! from the guest's RAM), a blit into it, a write of a texture over it, //! and, as the stream carries no buffer access, any binding of it to an encoder, a -//! `useResource:usage:` with write usage, or a `useHeap:` of its heap. A binding may be a read, so +//! `useResource:usage:` with write usage, or a `useHeap:` of its heap (each of a `useResources:…` +//! or `useHeaps:…` list as its own). A binding may be a read, so //! the times one took a cover off are counted apart: a difference in such an object's later //! compares may be the texture's bytes, not the play's. @@ -149,6 +151,28 @@ impl Covers { } /// `object` bound to an encoder, written whole, counted where that took a cover off. + /// `resource` used with `usage` (`useResource:usage:`): with write usage, written. + fn used(&mut self, resource: u64, usage: u64) { + const WRITE: u64 = 2; + if usage & WRITE != 0 { + match self.textures.contains(&resource) { + true if self.tracked(resource) => self.texture_written(resource), + true => {} + false => self.written(resource, None), + } + } + } + + /// Heap `heap` used (`useHeap:`): each buffer placed in it bound. + fn heap_used(&mut self, heap: u64) { + let live = self.heaps.get(&heap).cloned(); + for placed in live.into_iter().flatten() { + if !self.textures.contains(&placed) { + self.bound(placed); + } + } + } + fn bound(&mut self, object: u64) { if self.covered.contains_key(&object) { self.binding_uncovers += 1; @@ -364,23 +388,30 @@ impl Covers { } } "useResource:usage:" | "useResource:usage:stages:" => { - const WRITE: u64 = 2; - if let (Some(resource), Some(usage)) = (object(0), uint(1)) { - if usage & WRITE != 0 { - match self.textures.contains(&resource) { - true if self.tracked(resource) => self.texture_written(resource), - true => {} - false => self.written(resource, None), - } + let resource = call.args.first().and_then(Value::as_resource); + if let (Some(resource), Some(usage)) = (resource, uint(1)) { + self.used(resource, usage); + } + } + // The C array and its count are one list: each resource of it used as + // `useResource:usage:` uses its one. + "useResources:count:usage:" | "useResources:count:usage:stages:" => { + let resources = call.args.first().and_then(Value::as_list); + if let (Some(resources), Some(usage)) = (resources, uint(1)) { + for resource in resources.iter().filter_map(Value::as_resource) { + self.used(resource, usage); } } } "useHeap:" | "useHeap:stages:" => { - let live = object(0).and_then(|heap| self.heaps.get(&heap)).cloned(); - for placed in live.into_iter().flatten() { - if !self.textures.contains(&placed) { - self.bound(placed); - } + if let Some(heap) = object(0) { + self.heap_used(heap); + } + } + "useHeaps:count:" | "useHeaps:count:stages:" => { + let heaps = call.args.first().and_then(Value::as_list); + for heap in heaps.into_iter().flatten().filter_map(Value::as_object) { + self.heap_used(heap); } } _ if selector.contains("toTexture:") => { diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/encoders.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/encoders.rs index e3c353ee5..7dedc3386 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/encoders.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/encoders.rs @@ -79,7 +79,34 @@ fn buffer_layout(call: &Call, at: usize) -> Result }) } +/// A residency declaration's usage (`MTLResourceUsage`, argument 1): read, write or both. +fn resource_usage(call: &Call) -> Result { + Ok(match uint(call, 1)? { + 1 => rt::ResourceUsage::READ, + 2 => rt::ResourceUsage::WRITE, + 3 => rt::ResourceUsage::READ | rt::ResourceUsage::WRITE, + raw => return Err(refuse(call, format!("resource usage {raw}"))), + }) +} + impl NewRuntime { + /// Compute encoder `id` uses `held` with `usage`: through the runtime's `use_resource`; a + /// skipped object counted. + fn use_held(&mut self, id: u64, held: Held, usage: rt::ResourceUsage) -> Result<(), rt::Error> { + match held { + Held::Buffer(buffer) => { + (self.compute(id).encoder).use_resource(rt::Resource::Buffer(&buffer), usage) + } + Held::Texture(texture) => { + (self.compute(id).encoder).use_resource(rt::Resource::Texture(&texture), usage) + } + Held::Skipped => { + self.skips.calls += 1; + Ok(()) + } + } + } + /// An event argument: an event's, or a shared event's. fn event(&self, call: &Call, at: usize) -> Result { match self.get(call, Self::id(call, at)?)? { @@ -371,23 +398,23 @@ impl NewRuntime { } } "useResource:usage:" => { - let usage = rt::ResourceUsage::READ; - let usage = match uint(call, 1)? { - 1 => usage, - 2 => rt::ResourceUsage::WRITE, - 3 => usage | rt::ResourceUsage::WRITE, - raw => return Err(refuse(call, format!("resource usage {raw}"))), - }; - match self.resource(call, 0)? { - Held::Buffer(buffer) => (self.compute(id).encoder) - .use_resource(rt::Resource::Buffer(&buffer), usage), - Held::Texture(texture) => (self.compute(id).encoder) - .use_resource(rt::Resource::Texture(&texture), usage), - Held::Skipped => { - self.skips.calls += 1; - Ok(()) + let usage = resource_usage(call)?; + let held = self.resource(call, 0)?; + self.use_held(id, held, usage) + } + // The C array and its count are one list (docs/REPLAY-STREAM.md): each resource of + // it used as `useResource:usage:` uses its one. + "useResources:count:usage:" => { + let usage = resource_usage(call)?; + let mut used = Ok(()); + for item in list(call, 0)? { + let held = self.held(call, item)?; + used = self.use_held(id, held, usage); + if used.is_err() { + break; } } + used } "updateFence:" => { let fence = self.fence_of(call, 0)?; @@ -404,6 +431,26 @@ impl NewRuntime { Ok(()) } }, + // Each heap of the list used as `useHeap:` uses its one. + "useHeaps:count:" => { + let mut used = Ok(()); + for item in list(call, 0)? { + let id_of = item + .as_object() + .ok_or_else(|| refuse(call, format!("heap {item}")))?; + used = match self.block_of(call, id_of)? { + Some(heap) => self.compute(id).encoder.use_heap(heap), + None => { + self.skips.calls += 1; + Ok(()) + } + }; + if used.is_err() { + break; + } + } + used + } _ => return Err(refuse(call, "not played on a compute encoder")), }; result.map_err(failed(call)) diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs index 640d3258f..a4efb4847 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs @@ -1188,7 +1188,12 @@ impl NewRuntime { /// Resource argument `at`: `{Buffer: object}` or `{Texture: object}`. fn resource(&self, call: &Call, at: usize) -> Result { - let (kind, object) = variant(call, arg(call, at)?)?; + self.held(call, arg(call, at)?) + } + + /// A resource `value` of `call`: `{Buffer: object}` or `{Texture: object}`. + fn held(&self, call: &Call, value: &Value) -> Result { + let (kind, object) = variant(call, value)?; let id = object .as_object() .ok_or_else(|| refuse(call, format!("{kind} {object}")))?; @@ -1212,7 +1217,11 @@ impl NewRuntime { /// The block of the heap argument `at` names; none for a skipped object. fn heap_of(&self, call: &Call, at: usize) -> Result, PlayError> { - let id = Self::id(call, at)?; + self.block_of(call, Self::id(call, at)?) + } + + /// The block of heap `id`; none for a skipped object. + fn block_of(&self, call: &Call, id: u64) -> Result, PlayError> { match self.get(call, id)? { Object::Block(range, _, _) => Ok(Some(range.block)), Object::Skipped => Ok(None), diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/unwritten.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/unwritten.rs index 4567201f5..aa8e037f9 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/unwritten.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/unwritten.rs @@ -8,7 +8,8 @@ //! What a dispatch reads and writes: the buffer bound at each buffer slot its pipeline state uses, //! by the state's access there (`ComputePipelineState::buffer_accesses`: a `ReadOnly` slot reads, //! a `WriteOnly` one writes, a `ReadWrite` one both; a slot the state does not use, neither); a -//! buffer `useResource:usage:` names, by its usage's read and write bits; and, read and written, a +//! buffer `useResource:usage:` names (each of a `useResources:count:usage:` list), by its usage's +//! read and write bits; and, read and written, a //! buffer any other call of its encoder named since its last dispatch, and every bound buffer of a //! dispatch whose state the play skipped. Those last two are counted as read, which they may not //! be, and the line says how many. In a command buffer, a dispatch that writes a buffer comes @@ -248,10 +249,20 @@ impl Unwritten { } } } - "useResource:usage:" => { - if let (Some(id), Some(usage)) = (waiting_of(call.args.first()), uint(1)) { - let reads = usage & (USAGE_READ | USAGE_SAMPLE) != 0 || usage == 0; - encoder.used.insert(id, (reads, usage & USAGE_WRITE != 0)); + // The resource as the stream records it, `{Buffer: object}`; of the plural, each of + // the list (the C array and its count are one list). + "useResource:usage:" | "useResources:count:usage:" => { + let resources = match &*call.selector { + "useResource:usage:" => call.args.first().map(std::slice::from_ref), + _ => call.args.first().and_then(Value::as_list), + }; + let usage = uint(1); + for resource in resources.into_iter().flatten() { + let id = resource.as_resource().filter(|id| waiting.contains(id)); + if let (Some(id), Some(usage)) = (id, usage) { + let reads = usage & (USAGE_READ | USAGE_SAMPLE) != 0 || usage == 0; + encoder.used.insert(id, (reads, usage & USAGE_WRITE != 0)); + } } } selector if selector.starts_with("dispatch") => { diff --git a/crates/metal2vulkan-validation/src/replay/value.rs b/crates/metal2vulkan-validation/src/replay/value.rs index 1e40689cf..9864a1041 100644 --- a/crates/metal2vulkan-validation/src/replay/value.rs +++ b/crates/metal2vulkan-validation/src/replay/value.rs @@ -71,6 +71,15 @@ impl Value { } } + /// A resource argument's object (`docs/REPLAY-STREAM.md`, section 2): the id of + /// `{Buffer: object}` or `{Texture: object}`; `None` for every other value. + pub fn as_resource(&self) -> Option { + match self.as_map() { + Some([(kind, object)]) if matches!(&**kind, "Buffer" | "Texture") => object.as_object(), + _ => None, + } + } + /// The object's id; `None` for nil and for every other kind. pub fn as_object(&self) -> Option { match self { diff --git a/docs/REPLAY-STREAM.md b/docs/REPLAY-STREAM.md index 2f74d6b82..afe1fcdda 100644 --- a/docs/REPLAY-STREAM.md +++ b/docs/REPLAY-STREAM.md @@ -622,7 +622,8 @@ records as text, and with whatever its memory held, which is where a recording that missed the program's writes shows. A dispatch reads the buffer bound at a slot its pipeline state reads (the runtime's `ComputePipelineState::buffer_accesses`: `ReadOnly` or `ReadWrite`) and one `useResource:usage:` - names for reading; it writes the buffer at a slot the state writes and one used for writing, + names for reading (each of a `useResources:count:usage:` list as its own); it writes the buffer + at a slot the state writes and one used for writing, and a slot the state does not use touches nothing. A buffer another call of its encoder named since its last dispatch, and each bound under a state the play skipped, is taken as read and written, and the line says how many of the count were. In a command buffer a dispatch's write @@ -730,7 +731,7 @@ records as text, and options, a render target array length above 1, tile or imageblock sizes, or a skipped texture or visibility result buffer), or one the runtime answers with a typed gap; and every call of a skipped object (`skipped_calls`, with `useResource:usage:` and `useHeap:` of - one); + one, and each skipped object of a `useResources:count:usage:` or `useHeaps:count:` list); - dispatches whose pipeline state or bound buffer or texture is a skipped object (`skipped_dispatches`), and draws whose pipeline state, or a buffer or texture bound to either stage, or whose index buffer is one (`skipped_draws`); @@ -934,7 +935,10 @@ records as text, and Compute encoders play `setStageInRegion:` (the runtime takes it and it changes nothing a dispatch of a kernel without a stage-in argument does, the only kernel it builds) and `setImageblockWidth:height:`, whose size the player's pre-pass passes with each dispatch it - prepares, 1 by 1 until the encoder sets one. + prepares, 1 by 1 until the encoder sets one. They play their residency declarations through the + runtime's compute encoder: `useResource:usage:` and, for each resource of its list, + `useResources:count:usage:` through `use_resource` with the usage; `useHeap:` and, for each heap + of its list, `useHeaps:count:` through `use_heap`. `report.txt` holds one line per item: the mode line (`mode full: every call` or `mode states-only: ...`) first, then the line `recorded before the recorder's completion fix: the @@ -999,11 +1003,12 @@ compared. The ranges are the stream's offsets in the heap with a buffer's length and a texture's size as the runtime answers it, and the writes are found by structure: a draw's or a dispatch's to the texture slots the pipeline state's interface marks written, a render pass's to the textures its descriptor names, a blit's -to its destination, `generateMipmapsForTexture:`, a `useResource:usage:` with write usage and the -CPU's `replaceRegion:…`; a write of a texture over a buffer is the buffer's. An object is written +to its destination, `generateMipmapsForTexture:`, a `useResource:usage:` with write usage (and +each resource of a `useResources:count:usage:` with write usage) and the CPU's `replaceRegion:…`; a write of a texture over a buffer is the buffer's. An object is written again by the player's writes into it, a blit into it, and, as the stream carries no buffer access, any binding of it to an encoder, a `useResource:usage:` with write usage or a `useHeap:` -of its heap; a binding may be a read, so `B` counts the covers a binding or a `useHeap:` took off, +of its heap (each of a `useResources:…` or `useHeaps:…` list as its own); a binding may be a read, +so `B` counts the covers a binding or a `useHeap:` took off, and a difference in that object's later compares may be the covering texture's bytes. `receivers: C checks of surfaces' receiver buffers not compared; the textures over the surfaces hold the texels` where the stream checks a buffer a texture over a surface's plane was made