From 5a0a2e18ef34ae5d1eb745ec5eeeb74b2f43131a Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 19 Nov 2026 00:16:13 -0800 Subject: [PATCH] conformance: the new runtime's side makes compute states under a limit, queues' slots and fragment fills The rt side left compute_pipeline_limits, compute_pipeline_for_commands, queue_slots, fragment_fill and render_target at the trait default, so their cases answered Unavailable and were never compared with Metal. Each now calls the runtime's public API as the old side calls the old runtime: - compute_pipeline_limits: a compute state of the descriptor's maxTotalThreadsPerThreadgroup, its answer floored as both sides floor it, or Metal's refusal where the runtime's error is Refused or the translator's InvalidDescriptor; - compute_pipeline_for_commands: the state with supportIndirectCommandBuffers set, which the runtime refuses as Gap::IndirectCommandBuffers, so the row records that typed refusal; - queue_slots: the shared probe over a queue made with the case's count and another queue; - fragment_fill: one draw into a Private target of the fill's format, then a blit into a Shared buffer; - render_target: the mesh family's Shared RGBA32Uint target. The device test's call list names a case for each call a case reaches directly. --- .../metal2vulkan-conformance/src/rt_side.rs | 46 +++- .../src/rt_side/states.rs | 205 ++++++++++++++++++ 2 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 crates/metal2vulkan-conformance/src/rt_side/states.rs diff --git a/crates/metal2vulkan-conformance/src/rt_side.rs b/crates/metal2vulkan-conformance/src/rt_side.rs index 2b9955ed7..8601e3999 100644 --- a/crates/metal2vulkan-conformance/src/rt_side.rs +++ b/crates/metal2vulkan-conformance/src/rt_side.rs @@ -46,6 +46,7 @@ use std::time::Duration; mod compute; mod events; mod render; +mod states; mod work; /// How long a wait for a command buffer waits before the side records that it never returned: @@ -1382,6 +1383,42 @@ impl Side for RtSide<'_> { self.scheduled_handler_order_probe() } + fn compute_pipeline_limits( + &mut self, + library: Handle, + spec: &crate::pipeline_limits::ComputeLimitsSpec<'_>, + ) -> Answer> { + self.compute_limits(library, spec) + } + + fn compute_pipeline_for_commands( + &mut self, + library: Handle, + entry: &str, + stage_input: Option<&StageInput>, + ) -> Answer { + self.compute_state_for_commands(library, entry, stage_input) + } + + fn queue_slots( + &mut self, + max: Option, + cap: u32, + ) -> Answer { + self.slots(max, cap) + } + + fn fragment_fill( + &mut self, + fill: &crate::case::FragmentFill<'_>, + ) -> Answer { + self.fill(fill) + } + + fn render_target(&mut self, width: u32, height: u32) -> Answer { + self.mesh_target(width, height) + } + fn empty_command_buffers(&mut self) -> Answer { let of_rt = |error: rt::Error| Failure::of_rt(&error); let order: Arc>> = Arc::default(); @@ -1480,7 +1517,7 @@ mod answered { }; /// Each call the side answers, with a case that reaches it. - const CALLS: [(&str, &str); 7] = [ + const CALLS: [(&str, &str); 11] = [ ( "heap_buffer_size_and_align", crate::cases::device_queries::HEAP_BUFFERS, @@ -1509,6 +1546,13 @@ mod answered { "scheduled_handler_order", crate::cases::scheduled_handlers::SCHEDULED_HANDLER_ORDER, ), + ("compute_pipeline_limits", "compute-limits-plain-max-96"), + ( + "compute_pipeline_for_commands", + crate::cases::indirect_commands::COMPUTE_EXECUTED, + ), + ("queue_slots", crate::cases::command_queues::QUEUE_SLOTS), + ("fragment_fill", "rgb10a2-srgb-stores-f"), ]; /// The calls `value` records as unavailable, wherever in it they sit. diff --git a/crates/metal2vulkan-conformance/src/rt_side/states.rs b/crates/metal2vulkan-conformance/src/rt_side/states.rs new file mode 100644 index 000000000..fad4b397c --- /dev/null +++ b/crates/metal2vulkan-conformance/src/rt_side/states.rs @@ -0,0 +1,205 @@ +//! The rt side's state and target calls that the old runtime's side answers and the runtime has +//! the counterpart of: a compute state's limit under its descriptor, a compute state for indirect +//! commands (the runtime's typed refusal), a command queue's slots, a fragment fill and a render +//! target of the mesh family's format. + +use super::{keep, waited_for, RtSide, QUEUE_COMMAND_BUFFERS, WAIT}; +use crate::case::{Answer, FragmentFill, FragmentFilled, Handle, StageInput}; +use crate::pipeline_limits::{floored, ComputeLimitsSpec, ComputeStateLimits, Creation}; +use crate::transcript::{Negative, Value}; +use metal2vulkan_rt as rt; + +/// A typed error of the runtime as the failure it records. +fn of_rt(error: rt::Error) -> crate::case::Failure { + crate::case::Failure::of_rt(&error) +} + +/// Whether the runtime's error is Metal's own refusal of the descriptor: its validation's +/// (`Refused`) or the translator's refusal of a descriptor Metal refuses (`InvalidDescriptor`). +fn refuses_as_metal(error: &rt::Error) -> bool { + matches!( + error, + rt::Error::Refused(_) + | rt::Error::Translation(metal2vulkan::error::Error::InvalidDescriptor(_)) + ) +} + +impl RtSide<'_> { + /// The library's `entry` as a stage of a descriptor. + fn kernel(&self, library: Handle, entry: &str) -> Answer { + let function = (self.held_library(library)?.function(entry)) + .map_err(|error| self.on.translation_failure(&error))?; + Ok(rt::StageSource::Function(rt::FunctionDescriptor::new( + function, + ))) + } + + /// [`crate::case::Side::compute_pipeline_limits`]: the runtime's state of the descriptor, and + /// its `maxTotalThreadsPerThreadgroup`, or Metal's refusal where the runtime's error is one. + pub(super) fn compute_limits( + &mut self, + library: Handle, + spec: &ComputeLimitsSpec<'_>, + ) -> Answer> { + let mut descriptor = rt::ComputePipelineDescriptor::new(self.kernel(library, spec.entry)?); + descriptor.max_total_threads_per_threadgroup = + (spec.max_total_threads != 0).then_some(spec.max_total_threads); + let on = self.on; + match (on.device) + .new_compute_pipeline_state(&descriptor, |samplers| Ok(on.sampler_ids(samplers))) + { + Ok(state) => Ok(Creation::made(ComputeStateLimits { + max_total_threads_per_threadgroup: floored(u64::from( + state.max_total_threads_per_threadgroup(), + )), + })), + Err(error) if refuses_as_metal(&error) => Ok(Creation::refused()), + Err(error) => Err(on.failure(&error)), + } + } + + /// [`crate::case::Side::compute_pipeline_for_commands`]: the runtime's state of the kernel + /// with `supportIndirectCommandBuffers` set, under the stage input where the case names one. + pub(super) fn compute_state_for_commands( + &mut self, + library: Handle, + entry: &str, + stage_input: Option<&StageInput>, + ) -> Answer { + let mut descriptor = rt::ComputePipelineDescriptor::new(self.kernel(library, entry)?); + descriptor.support_indirect_command_buffers = true; + descriptor.stage_input = stage_input.map(crate::runtime_side::stage_input_descriptor); + let on = self.on; + let state = (on.device) + .new_compute_pipeline_state(&descriptor, |samplers| Ok(on.sampler_ids(samplers))) + .map_err(|error| on.failure(&error))?; + Ok(keep(&mut self.pipelines, state)) + } + + /// [`crate::case::Side::queue_slots`]: the probe both sides run, on a queue of the runtime's + /// made with `max` (`newCommandQueue`'s count where none is named) and another queue. + pub(super) fn slots( + &mut self, + max: Option, + cap: u32, + ) -> Answer { + let device = &self.on.device; + let queue = + (device.new_command_queue(max.unwrap_or(QUEUE_COMMAND_BUFFERS))).map_err(of_rt)?; + let other = (device.new_command_queue(QUEUE_COMMAND_BUFFERS)).map_err(of_rt)?; + crate::queue_slots::probe( + cap, + move || queue.command_buffer().map_err(of_rt), + || other.command_buffer().is_ok(), + ) + } + + /// [`crate::case::Side::fragment_fill`]: the runtime's state of the two functions over one + /// colour attachment of the fill's format, a Private target, one pass that loads nothing and + /// stores, the buffers at fragment indices 0 and up, one `drawPrimitives:` of triangles, and + /// then a blit of level 0 into a Shared buffer, each command buffer waited for within + /// [`WAIT`]. + pub(super) fn fill(&mut self, fill: &FragmentFill<'_>) -> Answer { + let function = |name: &str| -> Answer { + let function = (self.held_library(fill.library)?.function(name)) + .map_err(|error| self.on.translation_failure(&error))?; + Ok(rt::StageSource::Function(rt::FunctionDescriptor::new( + function, + ))) + }; + let mut descriptor = rt::RenderPipelineDescriptor::new(function(fill.vertex)?); + descriptor.fragment = Some(function(fill.fragment)?); + descriptor.color_attachments[0] = Some(rt::ColorAttachmentDescriptor::new(fill.format)); + let on = self.on; + let state = (on.device) + .new_render_pipeline_state(&descriptor, |samplers| Ok(on.sampler_ids(samplers))) + .map_err(|error| on.failure(&error))?; + let [width, height] = fill.size; + let mut target = + rt::TextureDescriptor::new(rt::TextureType::Type2D, fill.format, width, height); + target.usage = rt::TextureUsage::RENDER_TARGET; + target.storage_mode = rt::StorageMode::Private; + let target = on.device.new_texture(&target, None).map_err(of_rt)?; + let bound = (fill.buffers.iter()) + .map(|&handle| self.held(handle).cloned()) + .collect::>>()?; + let mut pass = rt::RenderPassDescriptor::default(); + let attachment = &mut pass.color_attachments[0]; + attachment.texture = Some(target.clone()); + attachment.load_action = rt::LoadAction::DontCare; + attachment.store_action = rt::StoreAction::Store; + let command_buffer = self.queue()?.command_buffer().map_err(of_rt)?; + let mut encoder = (command_buffer.render_command_encoder(&pass)).map_err(of_rt)?; + let encoded = (|| { + encoder.set_render_pipeline_state(&state).map_err(of_rt)?; + for (index, buffer) in bound.iter().enumerate() { + (encoder.set_fragment_buffer(Some(buffer), 0, index as u32)).map_err(of_rt)?; + } + (encoder.draw_primitives(rt::PrimitiveType::Triangle, 0, fill.vertices, 1, 0)) + .map_err(of_rt) + })(); + encoder.end_encoding(); + encoded?; + command_buffer.commit().map_err(of_rt)?; + let status = waited_for(&command_buffer, WAIT) + .map_or(Value::Nothing(Negative::NeverReturned), Value::unit_variant); + let at = keep(&mut self.render_pipelines, state); + self.drawn.insert(at.0 as usize); + let bytes = self.read_level(&target, fill.size, fill.row_pitch)?; + keep(&mut self.textures, target); + Ok(FragmentFilled { status, bytes }) + } + + /// Level 0 of `texture`, `size` texels, blitted into a Shared buffer at `row_pitch` bytes a + /// row and read once that command buffer completed within [`WAIT`]. + fn read_level( + &mut self, + texture: &rt::Texture, + size: [u32; 2], + row_pitch: u32, + ) -> Answer> { + let length = u64::from(row_pitch) * u64::from(size[1]); + let back = self.zeroed_buffer(length as usize)?; + let into = self.held(back)?.clone(); + let command_buffer = self.queue()?.command_buffer().map_err(of_rt)?; + let mut blit = command_buffer.blit_command_encoder().map_err(of_rt)?; + let place = rt::TexturePlace { + slice: 0, + level: 0, + origin: [0, 0, 0], + }; + let layout = rt::BufferLayout { + offset: 0, + bytes_per_row: u64::from(row_pitch), + bytes_per_image: length, + }; + let copied = + blit.copy_from_texture_to_buffer(texture, place, [size[0], size[1], 1], &into, layout); + blit.end_encoding(); + copied.map_err(of_rt)?; + command_buffer.commit().map_err(of_rt)?; + if waited_for(&command_buffer, WAIT).is_none() { + return Err(crate::case::Failure::named( + "ReadBackNeverReturned", + "the blit of the target into a buffer did not complete", + )); + } + crate::case::Side::read_buffer(self, back, 0, length as usize) + } + + /// [`crate::case::Side::render_target`]: a Shared 2D render target of the mesh family's + /// format, which no shader reaches. + pub(super) fn mesh_target(&mut self, width: u32, height: u32) -> Answer { + let format = rt::PixelFormat::RGBA32Uint; + let mut descriptor = + rt::TextureDescriptor::new(rt::TextureType::Type2D, format, width, height); + descriptor.usage = rt::TextureUsage::RENDER_TARGET; + descriptor.storage_mode = rt::StorageMode::Shared; + let texture = self + .on + .device + .new_texture(&descriptor, None) + .map_err(of_rt)?; + Ok(keep(&mut self.textures, texture)) + } +}