From 3b76e461a28be013c491e566e84f50e8ddb91bd5 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 23 Nov 2026 21:36:18 -0800 Subject: [PATCH] rt: count draws that may read their own pass's attachment beyond their slots; the known gap in the README What: Metrics::unslotted_attachment_reads counts a draw whose functions may read beyond their slots (Draws::reads_unbound, or an indirect command buffer's execution) while a use_resource read or a use_heap of the encoder covers a texture that is an attachment of the pass (a target or, in a resolving pass, a resolve) out of its home layout. The check walks at most the pass's attachments and the encoder's declarations, allocates nothing, and runs only for such draws; use_heap now records its blocks, and a texture answers the block of the heap its image lies in. The replay's counts line carries the counter. The README's known gaps of render encoders say that such a read names a descriptor whose layout differs from the image's, which Vulkan leaves undefined (VUID-vkCmdDraw-imageLayout-00344), that the counter counts it, and that the image is back home once the pass ends. The held-loop-reads test asserts one in each pass that reads the attachment through its argument buffer (after a slot's read moved it to the feedback layout, and alone, in the attachment layout) and none for the control, whose entry names a texture that is no attachment. Why: the ruling is rung 4 of the workaround ladder. A home in the feedback layout for every colour render target turns compression off on RADV (radv_layout_dcc_compressed and its HTILE and FMASK counterparts), a loss on every AMD render target; VK_KHR_unified_image_layouts would be a query-chosen tenth form. The validation layer cannot see the mismatch: core validation checks only statically accessed elements, and GPU-assisted validation's descriptor layout check is off in the layer's source. So the gap is recorded and the counter tells whether a guest meets it. How verified: the job ids of fmt, clippy (plain, m2v_gpu, m2v_cpu_icd), lavapipe and the 780M's device suite under RADV are reported to the lander. --- crates/metal2vulkan-runtime/README.md | 5 +++ crates/metal2vulkan-runtime/src/counters.rs | 16 +++++++ .../src/render_encoder.rs | 42 ++++++++++++++++++- .../src/tests/held_loop_reads.rs | 25 +++++++---- crates/metal2vulkan-runtime/src/texture.rs | 9 ++++ .../src/replay/new_runtime/mod.rs | 4 ++ crates/metal2vulkan/tests/api_signatures.tsv | 1 + 7 files changed, 92 insertions(+), 10 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 537693def..267418b72 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -4395,6 +4395,11 @@ cache is answered compile-required on both. ### Known gaps of render encoders +- A draw that reads its own pass's attachment through an argument buffer or a resource ID reads a + descriptor whose layout differs from the image's, which Vulkan leaves undefined + (`VUID-vkCmdDraw-imageLayout-00344`); such draws count in + `Metrics::unslotted_attachment_reads`, and the image is back in its home layout once the pass + ends. - An indexed indirect draw's index count is the GPU's to read, so a count that runs past the index buffer is not refused, as a direct draw's is (`Refusal::IndexBufferOffset`, which Metal's validation stops). The index buffer is bound from the indices to the end of the block's diff --git a/crates/metal2vulkan-runtime/src/counters.rs b/crates/metal2vulkan-runtime/src/counters.rs index 52dd04e06..9109e37d6 100644 --- a/crates/metal2vulkan-runtime/src/counters.rs +++ b/crates/metal2vulkan-runtime/src/counters.rs @@ -297,6 +297,8 @@ pub(crate) enum Metric { TextureClearBarriers, /// [`Metrics::attachment_loop_reads`]. AttachmentLoopReads, + /// [`Metrics::unslotted_attachment_reads`]. + UnslottedAttachmentReads, /// [`Metrics::sparse_binds`]. SparseBinds, /// [`Metrics::resolves`]. @@ -365,6 +367,7 @@ const METRIC_ORDER: [Metric; METRICS] = [ Metric::ExportedBlocksAddressed, Metric::TextureClearBarriers, Metric::AttachmentLoopReads, + Metric::UnslottedAttachmentReads, Metric::SparseBinds, Metric::Resolves, Metric::PreprocessRegionsRemade, @@ -762,6 +765,7 @@ struct Atomics { resolve_copies: AtomicU64, resolve_copy_bytes: AtomicU64, attachment_loop_reads: AtomicU64, + unslotted_attachment_reads: AtomicU64, allocations_freed: AtomicU64, submissions_during_frees: AtomicU64, submit_call_nanos_during_frees: AtomicU64, @@ -960,6 +964,7 @@ impl Counters { Metric::ExportedBlocksAddressed => &a.exported_blocks_addressed, Metric::TextureClearBarriers => &a.texture_clear_barriers, Metric::AttachmentLoopReads => &a.attachment_loop_reads, + Metric::UnslottedAttachmentReads => &a.unslotted_attachment_reads, Metric::SparseBinds => &a.sparse_binds, Metric::Resolves => &a.resolves, Metric::PreprocessRegionsRemade => &a.preprocess_regions_remade, @@ -1869,6 +1874,7 @@ impl Counters { resolve_copies: a.resolve_copies.load(Relaxed), resolve_copy_bytes: a.resolve_copy_bytes.load(Relaxed), attachment_loop_reads: a.attachment_loop_reads.load(Relaxed), + unslotted_attachment_reads: a.unslotted_attachment_reads.load(Relaxed), allocations_freed: a.allocations_freed.load(Relaxed), submissions_during_frees: a.submissions_during_frees.load(Relaxed), submit_call_nanos_during_frees: a.submit_call_nanos_during_frees.load(Relaxed), @@ -2588,6 +2594,16 @@ pub struct Metrics { /// such a draw reads the image's shadow instead, its copies in /// [`attachment_read_copies`](Self::attachment_read_copies). pub attachment_loop_reads: u64, + /// Draws whose functions may read a texture beyond their slots (an argument buffer's + /// texture, a resource ID) while a [`use_resource`](crate::RenderCommandEncoder::use_resource) + /// read or a [`use_heap`](crate::RenderCommandEncoder::use_heap) of the encoder covers a + /// texture that is an attachment of the draw's pass held in a layout other than its home + /// one: the attachment layout of a shader-read render target, or the feedback layout once a + /// draw of the pass read it through a slot. Such a read reaches the texture's own element, + /// which names the home layout, so Vulkan leaves what it reads undefined. A count of draws + /// that may read so, not of reads made: a draw whose functions read other textures beyond + /// their slots counts too. The image is back in its home layout once the pass ends. + pub unslotted_attachment_reads: u64, /// Copies between a texture over a buffer the device's linear image does not hold in place /// and its image of its own ([`Buffer::new_texture`](crate::Buffer::new_texture)): the /// buffer's bytes into the image, in the upload that leads a submission or after a command diff --git a/crates/metal2vulkan-runtime/src/render_encoder.rs b/crates/metal2vulkan-runtime/src/render_encoder.rs index cfbb9fc74..9cb6dd4d5 100644 --- a/crates/metal2vulkan-runtime/src/render_encoder.rs +++ b/crates/metal2vulkan-runtime/src/render_encoder.rs @@ -593,6 +593,19 @@ struct Declarations { /// What the first texture a declaration writes lacks for it: no storage element, or one the /// device cannot write without a format ([`TextureObject::storage_gap`]). unstored_write: Option, + /// The heaps' blocks `use_heap` declared, each once. + heaps: Vec, +} + +impl Declarations { + /// Whether a read declaration covers `texture`: a `use_resource` read of it or of a view + /// that shares its image, or a `use_heap` of the heap its image lies in. + fn reads(&self, texture: &Texture) -> bool { + let hazard = texture.hazard_id(); + (self.each.iter()).any(|&([id, _], access, ..)| id == hazard && access.reads()) + || (TextureObject::object(texture).heap_block()) + .is_some_and(|b| self.heaps.contains(&b)) + } } /// `MTLRenderCommandEncoder`: one dynamic rendering into its pass's attachments, begun as the @@ -1914,7 +1927,11 @@ impl RenderCommandEncoder { /// every function, and the pass's barriers order its draws after every earlier command and /// before every later one. Every later draw of the encoder reaches the resource with the /// declared access, as it reaches its bound ones, for the copies a texture over a buffer in an - /// image of its own takes, and for what the host reads once the command buffer completes. + /// image of its own takes, and for what the host reads once the command buffer completes. A + /// later draw that may read beyond its slots while a texture declared read is an attachment of + /// the pass out of its home layout counts in + /// [`Metrics::unslotted_attachment_reads`](crate::Metrics::unslotted_attachment_reads), as Vulkan leaves such a read + /// undefined. /// /// A usage that writes a texture, or a view of one, made for shader writes with no storage /// element (not a multisampled one, which Metal's shaders only read) makes every later draw of @@ -2001,13 +2018,19 @@ impl RenderCommandEncoder { /// The command buffer keeps the block until it retires, as /// [`ComputeCommandEncoder::use_heap`](crate::ComputeCommandEncoder::use_heap) does. It /// records nothing and declares no access, and the stages are not read: the pass's barriers - /// order its draws after every earlier command and before every later one. + /// order its draws after every earlier command and before every later one. A later draw that + /// may read beyond its slots while an attachment of the pass in the heap is out of its home + /// layout counts in [`Metrics::unslotted_attachment_reads`](crate::Metrics::unslotted_attachment_reads), as Vulkan leaves + /// such a read undefined. /// /// Err: [`Error::NoSuchBlock`] for a number no handle names. pub fn use_heap(&mut self, heap: BlockId) -> Result<(), Error> { let building = self.building(); let block = building.device.block(heap)?; building.keep_block(&block); + if !self.declared.heaps.contains(&heap) { + self.declared.heaps.push(heap); + } Ok(()) } @@ -3115,12 +3138,27 @@ impl RenderCommandEncoder { return Err(Error::Refused(Refusal::NoPipeline)); }; let draws = &state.inner.compiled.draws; + // A draw that may read beyond its slots, while a declared read covers an attachment the + // pass holds out of its home layout: such a read names the texture's own element, whose + // layout is the home one (`Metrics::unslotted_attachment_reads`). + let pass = &self.pass; + let resolves = (pass.resolving.then(|| pass.resolves())) + .into_iter() + .flatten(); + let unslotted = (draws.reads_unbound || commands) + && (pass.targets()) + .map(|t| (&t.texture, t.moves())) + .chain(resolves.map(|r| (&r.texture, r.moves()))) + .any(|(texture, moved)| moved && self.declared.reads(texture)); let tables = &self.tables; let factors = (self.factors.as_ref()) .filter(|_| draws.patches) .map(|(buffer, _)| &**buffer); let building = (self.building.as_mut()) .expect("an encoder holds its command buffer's recording until it ends"); + if unslotted { + building.count(Metric::UnslottedAttachmentReads); + } let vertex_reads = (draws.bindings.iter()) .filter_map(|&(_, index)| tables[VERTEX].buffer(index)) .chain(index.into_iter().flatten().map(BufferObject::object)) diff --git a/crates/metal2vulkan-runtime/src/tests/held_loop_reads.rs b/crates/metal2vulkan-runtime/src/tests/held_loop_reads.rs index e9878d9fe..1cbabc068 100644 --- a/crates/metal2vulkan-runtime/src/tests/held_loop_reads.rs +++ b/crates/metal2vulkan-runtime/src/tests/held_loop_reads.rs @@ -34,12 +34,14 @@ mod on_device { /// A pass over a shader-read render target, cleared, in which a draw reaches the target /// through an argument buffer: after a draw that read it through a slot, which moves it to the - /// feedback layout on a device with the loop, and in a pass of its own, which moves nothing. - /// Each pass's loop reads (`Metrics::attachment_loop_reads`) are printed and checked: one in - /// the first pass through the loop, none in the second. A control pass first draws the same - /// way through an entry naming a green texture, and the target's first pixel is green: the - /// entry's triangle covers it, so the fragment function runs and reads its texture. The - /// layer's messages, where it has any, fail the test as any other does. + /// feedback layout on a device with the loop, and in a pass of its own, where it is in the + /// attachment layout. Each counts one in `Metrics::unslotted_attachment_reads`, the known gap + /// in the runtime's README, and each pass's loop reads (`Metrics::attachment_loop_reads`) are + /// one in the first pass through the loop, none in the second. A control pass first draws the + /// same way through an entry naming a green texture, which is no attachment: it counts none, + /// and the target's first pixel is green, so the entry's triangle covers it and the fragment + /// function runs and reads its texture. The layer's messages, where it has any, fail the test + /// as any other does; it does not check a run-time-indexed element's layout. #[test] fn every_device_reads_its_attachment_through_an_argument_buffer_in_its_own_pass() { let (Some(examples), Some(engine)) = @@ -151,12 +153,19 @@ mod on_device { let after = counters.metrics(); let loop_reads = after.attachment_loop_reads - before.attachment_loop_reads; let copies = after.attachment_read_copies - before.attachment_read_copies; + let unslotted = + after.unslotted_attachment_reads - before.unslotted_attachment_reads; println!( - "held-loop-reads: {name} {case}: loop reads {loop_reads}, shadow copies \ - {copies}" + "held-loop-reads: {name} {case}: unslotted attachment reads {unslotted}, loop \ + reads {loop_reads}, shadow copies {copies}" ); let want = u64::from(through_slot && looped); assert_eq!(loop_reads, want, "{name} {case}: the pass's loop reads"); + let want = u64::from(case != "control"); + assert_eq!( + unslotted, want, + "{name} {case}: the pass's unslotted attachment reads" + ); if case == "control" { let first = read(&color, 4)[..4].to_vec(); assert_eq!( diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index 366882584..c50294a38 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -3921,6 +3921,15 @@ impl TextureInner { } impl TextureInner { + /// The block of the heap its image lies in, where it is a heap's texture: the block a + /// `use_heap` declaration names. + pub(crate) fn heap_block(&self) -> Option { + match &self.image_of()?._under { + Some(Under::Heap { block, .. }) => Some(block.id), + _ => None, + } + } + /// Its image's placement in its heap, where it was placed in one (`crate::alias`). pub(crate) fn placement(&self) -> Option<&Arc> { self.place.as_ref() diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs index 37414ce71..b96e94792 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs @@ -3150,6 +3150,10 @@ impl Backend for NewRuntime { metrics.attachment_read_copy_bytes, ), ("attachment_loop_reads", metrics.attachment_loop_reads), + ( + "unslotted_attachment_reads", + metrics.unslotted_attachment_reads, + ), ("bytes_zeroed", metrics.bytes_zeroed), ("takes_from_free_ranges", metrics.takes_from_free_ranges), ("block_zero_fills", metrics.block_zero_fills), diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 7e7ee3b53..f7546af21 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -1597,6 +1597,7 @@ field metal2vulkan-runtime::Metrics.texture_place_lock_waits pub texture_place_l field metal2vulkan-runtime::Metrics.translation_nanos pub translation_nanos: u64 field metal2vulkan-runtime::Metrics.translations pub translations: u64 field metal2vulkan-runtime::Metrics.translations_loaded pub translations_loaded: u64 +field metal2vulkan-runtime::Metrics.unslotted_attachment_reads pub unslotted_attachment_reads: u64 field metal2vulkan-runtime::Metrics.untimed_command_buffers pub untimed_command_buffers: u64 field metal2vulkan-runtime::Metrics.upload_waits pub upload_waits: u64 field metal2vulkan-runtime::Metrics.visibility_interrupts_refused pub visibility_interrupts_refused: u64