From d3702a97585f7241aa6edc8f902fcb647a9e1ec1 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 15 Nov 2026 08:40:19 -0800 Subject: [PATCH] Device tests: a Stencil8 attachment stores, resolves and faults as Metal's A Stencil8 texture on a device without S8_UINT is held in a combined depth/stencil image; these tests run the shapes the consumer's tests died at on Intel's UHD 630 under Windows, and a plain draw into such a target. - store_out_device: a Stencil8 texture at the start of a placement no-copy heap of bit 24 stored at Store (descriptor's and encoder's) faults its command buffer; at DontCare and a late CustomSampleDepthStore it completes. A 4-sample Stencil8 attachment resolving into it at MultisampleResolve (descriptor's and encoder's) and StoreAndMultisampleResolve faults; attached unresolved it completes. The pages keep their bytes. Each prints the format its image is held in. - stencil_target: a Stencil8 target cleared to 0x5a, drawn into over its left half with a Replace of reference 0xa5, and stored reads 0xa5 and 0x5a by halves through a copy; a later pass that loads and stores it keeps every byte, so the hidden depth aspect's DONT_CARE disturbs no stencil byte. Prints the image's format. Verified: compiled and run in the jobs named in the lane's report (the UHD 630 under Windows and lavapipe). --- crates/metal2vulkan-runtime/src/tests/mod.rs | 2 + .../src/tests/stencil_target.rs | 166 ++++++++++++++++++ .../src/tests/store_out_device.rs | 133 +++++++++++++- 3 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 crates/metal2vulkan-runtime/src/tests/stencil_target.rs diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 9cddcd73c..9b07ca044 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -758,6 +758,8 @@ mod staging_waits; #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod stall; mod stand_in; +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod stencil_target; mod storage_form; #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod storage_form_recycle; diff --git a/crates/metal2vulkan-runtime/src/tests/stencil_target.rs b/crates/metal2vulkan-runtime/src/tests/stencil_target.rs new file mode 100644 index 000000000..ea2c5955e --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/stencil_target.rs @@ -0,0 +1,166 @@ +//! A `Stencil8` render target drawn into and read back, on every device: on one without `S8_UINT` +//! its image is a combined depth/stencil one (`FormatEmulation::StencilInCombined`: Intel's UHD +//! 630 under its Windows driver), whose depth aspect no texture of the program's holds, loaded and +//! stored `DONT_CARE` (`render_target.rs`). +//! +//! - **A draw stores its stencil.** A pass clears a 16 x 16 `Stencil8` texture to 0x5a and draws a +//! triangle over the whole target, scissored to its left half, whose stencil operation replaces +//! the stencil with the reference 0xa5, and stores it: a copy out of the texture reads 0xa5 in +//! each texel of the left half and 0x5a in each of the right, a byte a texel. +//! - **A later pass keeps it.** A pass that loads the stencil and stores it, drawing nothing, +//! leaves the same bytes: the hidden depth aspect's `DONT_CARE` disturbs no stencil byte. +//! +//! Printed (`STENCIL-TARGET`): the device and the format of the texture's image. + +use super::blit_holds::{device, Scratch, WAIT}; +use super::encoder_holds::library; +use crate::{ + BlitOption, Buffer, CommandBuffer, CommandBufferStatus, CommandQueue, Device, LoadAction, + Origin, PipelineOptions, RenderPassDescriptor, RenderPipelineState, ResourceOptions, + ScissorRect, Size, StoreAction, Texture, TextureDescriptor, +}; +use metal2vulkan::metal::state::DepthStencilDescriptor; +use metal2vulkan::metal::{ + CompareFunction, PixelFormat, PrimitiveType, StencilOperation, TextureUsage, +}; +use metal2vulkan::pipeline::{ColorAttachmentDescriptor, RenderPipelineDescriptor, StageSource}; + +/// The texture's side. +const SIDE: u64 = 16; +/// The stencil the pass clears to, and the one its draw writes. +const CLEARED: u8 = 0x5a; +const DRAWN: u8 = 0xa5; + +/// Commit `command_buffer` and wait for it to complete. +fn commit(command_buffer: &CommandBuffer) { + command_buffer.commit().unwrap(); + assert!(command_buffer.wait_until_completed_for(WAIT).is_ok()); + assert_eq!(command_buffer.status(), CommandBufferStatus::Completed); +} + +/// A new `Private` `SIDE` square texture of `format` that renders. +fn target(device: &Device, format: PixelFormat) -> Texture { + let mut descriptor = TextureDescriptor::texture_2d(format, SIDE, SIDE, false); + descriptor.usage = TextureUsage::RENDER_TARGET; + descriptor.resource_options = ResourceOptions::private(); + device.new_texture(&descriptor).unwrap() +} + +/// A state that draws a triangle over the whole target into `RGBA8Unorm` beside a `Stencil8` +/// attachment. +fn draw_state(device: &Device) -> RenderPipelineState { + let scout = library(device, "engine/format_scout.ll"); + let stage = |name: &str| { + let function = scout.new_function_with_name(name).unwrap(); + StageSource::Function(function.function_descriptor()) + }; + let mut descriptor = RenderPipelineDescriptor::new(stage("scout_vertex")); + descriptor.fragment = Some(stage("scout_fragment_f")); + descriptor.color_attachments[0] = Some(ColorAttachmentDescriptor::new(PixelFormat::RGBA8Unorm)); + descriptor.stencil_format = Some(PixelFormat::Stencil8); + device + .new_render_pipeline_state(&descriptor, &PipelineOptions::default()) + .unwrap() +} + +/// The bytes of `stencil`, a byte a texel, copied out by a command buffer of `queue`. +fn stencil_bytes(queue: &CommandQueue, stencil: &Texture) -> Vec { + let out: Buffer = (queue.device()) + .new_buffer(SIDE * SIDE, ResourceOptions::shared()) + .unwrap(); + let command_buffer = queue.command_buffer().unwrap(); + let mut blit = command_buffer.blit_command_encoder().unwrap(); + blit.copy_from_texture_to_buffer( + stencil, + 0, + 0, + Origin::new(0, 0, 0), + Size::new(SIDE, SIDE, 1), + &out, + 0, + SIDE, + SIDE * SIDE, + BlitOption::None, + ) + .unwrap(); + blit.end_encoding().unwrap(); + commit(&command_buffer); + let at = out.contents().unwrap(); + // SAFETY: a `Shared` buffer's contents span its length, and no command uses them now. + unsafe { std::slice::from_raw_parts(at.as_ptr(), (SIDE * SIDE) as usize) }.to_vec() +} + +#[test] +fn a_stencil8_target_stores_what_a_draw_writes_and_a_later_pass_keeps_it() { + let scratch = Scratch::new("stencil-target"); + let device = device(&scratch.0); + let queue = device.new_command_queue().unwrap(); + let stencil = target(&device, PixelFormat::Stencil8); + let color = target(&device, PixelFormat::RGBA8Unorm); + let format = stencil.image_plan().unwrap().format; + println!( + "STENCIL-TARGET on {}: Stencil8 held in {format:?}", + device.name() + ); + let mut replace = DepthStencilDescriptor::default(); + for face in [ + &mut replace.front_face_stencil, + &mut replace.back_face_stencil, + ] { + face.stencil_compare_function = CompareFunction::Always; + face.depth_stencil_pass_operation = StencilOperation::Replace; + } + let replace = device.new_depth_stencil_state(&replace).unwrap(); + let state = draw_state(&device); + // Cleared, drawn into over the left half, and stored. + let mut pass = RenderPassDescriptor::new(); + let attachment = &mut pass.color_attachments[0]; + attachment.texture = Some(color.clone()); + attachment.load_action = LoadAction::Clear; + attachment.store_action = StoreAction::DontCare; + let attachment = &mut pass.stencil_attachment; + attachment.texture = Some(stencil.clone()); + attachment.load_action = LoadAction::Clear; + attachment.store_action = StoreAction::Store; + attachment.clear_stencil = u32::from(CLEARED); + let command_buffer = queue.command_buffer().unwrap(); + let mut encoder = command_buffer.render_command_encoder(&pass).unwrap(); + encoder.set_render_pipeline_state(&state).unwrap(); + encoder.set_depth_stencil_state(&replace).unwrap(); + encoder.set_stencil_reference_value(u32::from(DRAWN)); + let half = ScissorRect { + x: 0, + y: 0, + width: SIDE / 2, + height: SIDE, + }; + encoder.set_scissor_rect(half).unwrap(); + (encoder.draw_primitives(PrimitiveType::Triangle, 0, 3, 1, 0)).unwrap(); + encoder.end_encoding().unwrap(); + commit(&command_buffer); + let expected: Vec = (0..SIDE * SIDE) + .map(|at| match at % SIDE < SIDE / 2 { + true => DRAWN, + false => CLEARED, + }) + .collect(); + assert!( + stencil_bytes(&queue, &stencil) == expected, + "the drawn half and the cleared half" + ); + // Loaded and stored with no draw: the same bytes. + let mut pass = RenderPassDescriptor::new(); + let attachment = &mut pass.stencil_attachment; + attachment.texture = Some(stencil.clone()); + attachment.load_action = LoadAction::Load; + attachment.store_action = StoreAction::Store; + let command_buffer = queue.command_buffer().unwrap(); + (command_buffer.render_command_encoder(&pass).unwrap()) + .end_encoding() + .unwrap(); + commit(&command_buffer); + assert!( + stencil_bytes(&queue, &stencil) == expected, + "a later pass keeps them" + ); +} diff --git a/crates/metal2vulkan-runtime/src/tests/store_out_device.rs b/crates/metal2vulkan-runtime/src/tests/store_out_device.rs index f4a8bb9aa..b124fa078 100644 --- a/crates/metal2vulkan-runtime/src/tests/store_out_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/store_out_device.rs @@ -12,6 +12,15 @@ //! not, the command buffer's event signal fires, the pages keep their bytes, and the next //! command buffer of the queue runs and reads them. Each fault runs on a queue of its own: a //! queue's second fault revokes it (`command_buffer/revocation.rs`). +//! - **A stencil stored or resolved into such a placement heap faults**, as the M3 Max faults a +//! stencil's store and resolve into a heap of the bit: a `Stencil8` texture at the start of a +//! placement no-copy heap of the bit, as a guest makes a read-only heap, stored at `Store` (the +//! descriptor's and the encoder's), and a 4-sample `Stencil8` attachment of the device's own +//! resolving into it at `MultisampleResolve` (the descriptor's and the encoder's) and +//! `StoreAndMultisampleResolve`; at `DontCare`, at a late `CustomSampleDepthStore` (a stencil's +//! `DontCare`) and attached unresolved, it completes. On a device without `S8_UINT` (Intel's +//! UHD 630 under its Windows driver) its image is a combined depth/stencil one +//! (`StencilInCombined`), printed. //! - **A depth stored and resolved into such a heap completes**, its resolve dropped, as on the //! M3 Max: a multisample depth attachment of the device's own at `StoreAndMultisampleResolve` //! into a texture in a no-copy heap of the bit (the descriptor's action and one the encoder set, @@ -121,8 +130,12 @@ impl Pages { /// An automatic no-copy heap of bit 24 over the pages. fn heap(&self, device: &Device) -> Heap { - let descriptor = - HeapDescriptor::new(HeapType::Automatic, self.length() as u64, options(true)); + self.heap_of(device, HeapType::Automatic) + } + + /// A no-copy heap of `heap_type` and bit 24 over the pages. + fn heap_of(&self, device: &Device, heap_type: HeapType) -> Heap { + let descriptor = HeapDescriptor::new(heap_type, self.length() as u64, options(true)); // SAFETY: as in `memory`. unsafe { device.new_heap_with_bytes_no_copy(self.memory(), &descriptor, None, None) } .expect("a no-copy heap of bit 24 over the pages") @@ -486,6 +499,122 @@ fn a_depth_store_into_a_private_bit_heap_faults_its_command_buffer() { assert!(pages.bytes() == kept, "the pages keep their bytes"); } +/// A `SIDE` x `SIDE` `Stencil8` render target's descriptor, `Shared` (a heap of bit 24 holds it). +fn stencil_descriptor() -> TextureDescriptor { + let mut descriptor = TextureDescriptor::texture_2d(PixelFormat::Stencil8, SIDE, SIDE, false); + descriptor.usage = TextureUsage::RENDER_TARGET; + descriptor.resource_options = options(false); + descriptor +} + +/// A pass of the stencil texture `texture`, cleared to 0x5a, at `store`, resolving into `into` +/// where given. +fn stencil_pass( + texture: &Texture, + store: StoreAction, + into: Option<&Texture>, +) -> RenderPassDescriptor { + let mut pass = RenderPassDescriptor::new(); + let stencil = &mut pass.stencil_attachment; + stencil.texture = Some(texture.clone()); + stencil.load_action = LoadAction::Clear; + stencil.clear_stencil = 0x5a; + stencil.store_action = store; + stencil.resolve_texture = into.cloned(); + pass +} + +/// An encoder's late stencil store action `store`. +fn late_stencil(store: StoreAction) -> impl FnOnce(&mut RenderCommandEncoder) { + move |encoder: &mut RenderCommandEncoder| encoder.set_stencil_store_action(store).unwrap() +} + +#[test] +fn a_stencil_store_into_a_private_bit_placement_heap_faults_its_command_buffer() { + // Declared before the device, so dropped after it. + let pages; + let Some(device) = device(None) else { + return; + }; + let descriptor = stencil_descriptor(); + pages = Pages::holding(&device, &descriptor); + let kept = pages.bytes(); + let heap = pages.heap_of(&device, HeapType::Placement); + let stencil = heap.new_texture_at_offset(&descriptor, 0).unwrap(); + let format = stencil.image_plan().unwrap().format; + eprintln!("STORE-OUT: a Stencil8 texture in a placement heap of bit 24, held in {format:?}"); + // Each fault on a queue of its own (a queue's second fault revokes it). + let queue = device.new_command_queue().unwrap(); + let pass = stencil_pass(&stencil, StoreAction::Store, None); + assert_eq!( + around_pass(&device, &queue, &pass, |_| {}), + Answer::FAULTED, + "Store" + ); + let queue = device.new_command_queue().unwrap(); + let pass = stencil_pass(&stencil, StoreAction::Unknown, None); + let late = late_stencil(StoreAction::Store); + assert_eq!( + around_pass(&device, &queue, &pass, late), + Answer::FAULTED, + "a late Store" + ); + let queue = device.new_command_queue().unwrap(); + let pass = stencil_pass(&stencil, StoreAction::DontCare, None); + assert_eq!( + around_pass(&device, &queue, &pass, |_| {}), + Answer::COMPLETED, + "DontCare" + ); + let pass = stencil_pass(&stencil, StoreAction::Unknown, None); + let late = late_stencil(StoreAction::CustomSampleDepthStore); + let answer = around_pass(&device, &queue, &pass, late); + assert_eq!(answer, Answer::COMPLETED, "a late CustomSampleDepthStore"); + assert!(pages.bytes() == kept, "the pages keep their bytes"); +} + +#[test] +fn a_stencil_resolve_into_a_private_bit_placement_heap_faults_its_command_buffer() { + // Declared before the device, so dropped after it. + let pages; + let Some(device) = plan_device() else { + return; + }; + let descriptor = stencil_descriptor(); + pages = Pages::holding(&device, &descriptor); + let kept = pages.bytes(); + let heap = pages.heap_of(&device, HeapType::Placement); + let into = heap.new_texture_at_offset(&descriptor, 0).unwrap(); + let samples = own_texture(&device, PixelFormat::Stencil8, 4); + let format = samples.image_plan().unwrap().format; + eprintln!("STORE-OUT: a 4-sample Stencil8 texture held in {format:?}"); + // Each fault on a queue of its own (a queue's second fault revokes it). + let resolve = StoreAction::MultisampleResolve; + let smsr = StoreAction::StoreAndMultisampleResolve; + for (store, late) in [ + (resolve, None), + (StoreAction::Unknown, Some(resolve)), + (smsr, None), + ] { + let queue = device.new_command_queue().unwrap(); + let pass = stencil_pass(&samples, store, Some(&into)); + let answer = match late { + Some(late) => around_pass(&device, &queue, &pass, late_stencil(late)), + None => around_pass(&device, &queue, &pass, |_| {}), + }; + assert_eq!(answer, Answer::FAULTED, "{store:?}, late {late:?}"); + } + // Attached and not resolved into, it is not written: the pass completes. + let queue = device.new_command_queue().unwrap(); + let pass = stencil_pass(&samples, StoreAction::Store, Some(&into)); + assert_eq!( + around_pass(&device, &queue, &pass, |_| {}), + Answer::COMPLETED, + "Store" + ); + assert!(pages.bytes() == kept, "the pages keep their bytes"); +} + #[test] fn a_depth_stored_and_resolved_into_a_private_bit_heap_completes_without_its_resolve() { // Declared before the device, so dropped after it.