Files
metal2vulkan/crates/metal2vulkan-conformance/src/rt_side/mesh.rs
T
Anees Iqbal 3ab8bcd357 Conformance: the runtime's side answers the mesh pipeline calls
The rt side answered every mesh call with Unavailable. It now answers them
through the runtime's mesh render pipeline states (new module rt_side/mesh.rs):

- mesh_pipeline and mesh_pipeline_for_commands: new_mesh_render_pipeline_state
  of the case's object, mesh and fragment functions over RGBA32Uint colour
  attachments, the second with support_indirect_command_buffers set.
- mesh_pass: one pass over the targets, each cleared to zero and stored, every
  step encoded whatever an earlier one answered (the set_object_*,
  set_mesh_*, set_fragment_buffer, draw_mesh_threadgroups, draw_mesh_threads
  and draw_mesh_threadgroups_indirect calls; both barrier forms are the
  runtime's memory_barrier, whose doc names both), committed and waited for
  within WAIT; the states it set announce their notes.
- mesh_pipeline_limits: the descriptor's limits with rasterization disabled,
  the state's answers floored as the Metal side floors them, or Metal's
  refusal where the runtime's error is one (refuses_as_metal, now shared).
- mesh_descriptor_facts: the translator's MeshPipelineDescriptor stated and
  read through descriptor_reads (new state_mesh and read_mesh; the colour
  attachments' stating and reading are now one helper each, shared with the
  render walk).
- mesh_pipeline_bindings: the consumer's own (reflection_is_the_consumers).
- mesh_command_buffer: the runtime's indirect command buffer of both mesh draw
  types, which it refuses as CommandTypes (the planner's ruling: not built).

Three getters the runtime has no counterpart of become fields a side refuses
alone, the other getters still compared, as the render facts already do: a mesh
descriptor's label and binaryArchives, and a mesh state's
maxTotalThreadsPerThreadgroup (a tile threadgroup's). The Metal side answers
each as Ok, so the recordings' sites are unchanged.

The answered-calls test gains the five mesh calls with a case that reaches each.

Verified: JOBLINE
2026-12-03 17:05:45 -08:00

295 lines
13 KiB
Rust

//! The rt side's mesh render pipeline calls: a state of a case's object, mesh and fragment
//! functions over its targets, with or without `supportIndirectCommandBuffers`; one pass of a
//! case's mesh steps; what a made state answers of its limits; a mesh descriptor read back before
//! any state is made; and an indirect command buffer of mesh commands, which the runtime refuses
//! by its command types.
//!
//! The runtime takes Metal's mesh descriptor as the translator's
//! ([`rt::MeshPipelineDescriptor`]), so a descriptor is stated and read through the walk both
//! sides share ([`crate::descriptor_reads`]). The runtime keeps no label and takes no binary
//! archive, and it makes no tile render state, so those getters answer refused fields.
use super::states::refuses_as_metal;
use super::{keep, waited_for, RtSide, WAIT};
use crate::case::{
Answer, Failure, Field, Handle, MeshCommand, MeshFunctions, MeshStep, PassOutcome,
};
use crate::descriptor_reads;
use crate::mesh_descriptors::{DescriptorSpec, MeshDescriptorFacts};
use crate::pipeline_limits::{floored, Creation, MeshLimitsSpec, MeshStateLimits};
use crate::transcript::{Negative, Value};
use metal2vulkan_runtime as rt;
/// A typed error of the runtime as the failure it records.
fn of_rt(error: rt::Error) -> Failure {
Failure::of_rt(&error)
}
/// `label`: the runtime keeps none, on a descriptor or on what it makes of one.
fn label() -> Field<Option<String>> {
Err(Failure::unavailable("label"))
}
/// `binaryArchives`: the runtime takes no binary archive.
fn binary_archives() -> Field<u64> {
Err(Failure::unavailable("binaryArchives"))
}
/// `maxTotalThreadsPerThreadgroup`, a tile threadgroup's: the runtime makes no tile render state,
/// so a drawing state has no such getter.
fn tile_threads() -> Field<u64> {
Err(Failure::unavailable("maxTotalThreadsPerThreadgroup"))
}
/// The mesh family's colour format, [`crate::case::TARGET_FORMAT`].
const TARGET: rt::PixelFormat = rt::PixelFormat::RGBA32Uint;
impl RtSide<'_> {
/// The translator's mesh descriptor of the library's `mesh` function, and of `object` where
/// the case names one, every other field Metal's default.
fn mesh_stages(
&self,
library: Handle,
object: Option<&str>,
mesh: &str,
) -> Answer<rt::MeshPipelineDescriptor> {
let mut descriptor = rt::MeshPipelineDescriptor::new(self.kernel(library, mesh)?);
descriptor.object = object
.map(|object| self.kernel(library, object))
.transpose()?;
Ok(descriptor)
}
/// [`crate::case::Side::mesh_pipeline`], and with `for_commands`
/// [`crate::case::Side::mesh_pipeline_for_commands`]: the runtime's state of the functions over
/// `functions.attachments` colour attachments of [`TARGET`].
pub(super) fn mesh_state(
&mut self,
library: Handle,
functions: &MeshFunctions<'_>,
for_commands: bool,
) -> Answer<Handle> {
let mut descriptor = self.mesh_stages(library, functions.object, functions.mesh)?;
descriptor.fragment = Some(self.kernel(library, functions.fragment)?);
for at in 0..functions.attachments as usize {
let slot = (descriptor.color_attachments.get_mut(at))
.ok_or_else(|| Failure::named("NoSuchAttachment", format!("color {at}")))?;
*slot = Some(rt::ColorAttachmentDescriptor::new(TARGET));
}
descriptor.support_indirect_command_buffers = for_commands;
let on = self.on;
let state = (on.device)
.new_mesh_render_pipeline_state(&descriptor, |samplers| Ok(on.sampler_ids(samplers)))
.map_err(|error| on.failure(&error))?;
Ok(keep(&mut self.render_pipelines, state))
}
/// [`crate::case::Side::mesh_pass`]: one render pass over `targets`, each cleared to zero and
/// stored, every step encoded whatever an earlier one answered (a refused call leaves the
/// encoder as it was), then the command buffer committed and waited for within [`WAIT`].
pub(super) fn run_mesh_pass(
&mut self,
targets: &[Handle],
steps: &[MeshStep],
) -> Answer<PassOutcome> {
let mut pass = rt::RenderPassDescriptor::default();
for (at, &target) in targets.iter().enumerate() {
let texture = self.held_texture(target)?.clone();
let attachment = (pass.color_attachments.get_mut(at))
.ok_or_else(|| Failure::named("NoSuchAttachment", format!("color {at}")))?;
attachment.texture = Some(texture);
attachment.load_action = rt::LoadAction::Clear;
attachment.clear_color = rt::ClearColor::new(0.0, 0.0, 0.0, 0.0);
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 refused: Vec<Option<Failure>> = (steps.iter())
.map(|step| self.mesh_step(&mut encoder, step).err())
.collect();
encoder.end_encoding();
command_buffer.commit().map_err(of_rt)?;
for (step, refused) in steps.iter().zip(&refused) {
if let (MeshStep::Pipeline(state), None) = (step, refused) {
self.drawn.insert(state.0 as usize);
}
}
let Some(word) = waited_for(&command_buffer, WAIT) else {
return Ok(PassOutcome {
refused,
status: Value::Nothing(Negative::NeverReturned),
error: None,
});
};
Ok(PassOutcome {
refused,
status: Value::unit_variant(word),
error: (command_buffer.error()).map(|error| Value::unit_variant(format!("{error:?}"))),
})
}
/// One step of a mesh pass on `encoder`.
fn mesh_step(&self, encoder: &mut rt::RenderCommandEncoder, step: &MeshStep) -> Answer<()> {
match step {
MeshStep::Pipeline(state) => {
let state = (self.render_pipelines.get(state.0 as usize)).ok_or_else(|| {
Failure::named("NoSuchHandle", format!("mesh pipeline {}", state.0))
})?;
encoder.set_render_pipeline_state(state).map_err(of_rt)
}
MeshStep::ObjectBuffer {
buffer,
offset,
index,
} => (encoder.set_object_buffer(Some(self.held(*buffer)?), *offset, *index))
.map_err(of_rt),
MeshStep::ObjectBytes { bytes, index } => {
encoder.set_object_bytes(bytes, *index).map_err(of_rt)
}
MeshStep::MeshBuffer {
buffer,
offset,
index,
} => {
(encoder.set_mesh_buffer(Some(self.held(*buffer)?), *offset, *index)).map_err(of_rt)
}
MeshStep::FragmentBuffer {
buffer,
offset,
index,
} => (encoder.set_fragment_buffer(Some(self.held(*buffer)?), *offset, *index))
.map_err(of_rt),
MeshStep::DrawThreadgroups {
groups,
object,
mesh,
} => (encoder.draw_mesh_threadgroups(*groups, *object, *mesh)).map_err(of_rt),
MeshStep::DrawThreads {
threads,
object,
mesh,
} => (encoder.draw_mesh_threads(*threads, *object, *mesh)).map_err(of_rt),
MeshStep::DrawIndirect {
buffer,
offset,
object,
mesh,
} => (encoder.draw_mesh_threadgroups_indirect(
self.held(*buffer)?,
*offset,
*object,
*mesh,
))
.map_err(of_rt),
// The scope, the stages and the buffers only narrow what Metal orders: the runtime's
// barrier, which serves both of Metal's calls, orders every access of the encoder's
// draws so far before its later ones.
MeshStep::BarrierScope { .. } => encoder.memory_barrier().map_err(of_rt),
MeshStep::BarrierResources { buffers, .. } => {
for &buffer in buffers {
self.held(buffer)?;
}
encoder.memory_barrier().map_err(of_rt)
}
}
}
/// [`crate::case::Side::mesh_pipeline_limits`]: the runtime's state of the descriptor,
/// rasterization disabled, and what it answers of its limits, or Metal's refusal where the
/// runtime's error is one.
pub(super) fn mesh_limits(
&mut self,
library: Handle,
spec: &MeshLimitsSpec<'_>,
) -> Answer<Creation<MeshStateLimits>> {
let mut descriptor = self.mesh_stages(library, spec.object, spec.mesh)?;
descriptor.rasterization_enabled = false;
descriptor.max_vertex_amplification_count = spec.amplification;
let stated = |held: u32| (held != 0).then_some(held);
descriptor.max_total_threads_per_object_threadgroup = stated(spec.max_object);
descriptor.max_total_threads_per_mesh_threadgroup = stated(spec.max_mesh);
descriptor.max_total_threadgroups_per_mesh_grid = stated(spec.max_grid);
descriptor.payload_memory_length = stated(spec.payload);
let on = self.on;
let state = match (on.device)
.new_mesh_render_pipeline_state(&descriptor, |samplers| Ok(on.sampler_ids(samplers)))
{
Ok(state) => state,
Err(error) if refuses_as_metal(&error) => return Ok(Creation::refused()),
Err(error) => return Err(on.failure(&error)),
};
let object = spec.object.is_some();
Ok(Creation::made(MeshStateLimits {
max_total_threads_per_object_threadgroup: object
.then(|| floored(u64::from(state.max_total_threads_per_object_threadgroup()))),
max_total_threads_per_mesh_threadgroup: floored(u64::from(
state.max_total_threads_per_mesh_threadgroup(),
)),
object_thread_execution_width: object
.then(|| u64::from(state.object_thread_execution_width())),
mesh_thread_execution_width: u64::from(state.mesh_thread_execution_width()),
max_total_threadgroups_per_mesh_grid: u64::from(
state.max_total_threadgroups_per_mesh_grid(),
),
required_threads_per_object_threadgroup: state
.required_threads_per_object_threadgroup()
.map(u64::from),
required_threads_per_mesh_threadgroup: state
.required_threads_per_mesh_threadgroup()
.map(u64::from),
max_total_threads_per_threadgroup: tile_threads(),
}))
}
/// [`crate::case::Side::mesh_descriptor_facts`]: the translator's descriptor of the library's
/// stages, stated and reset as the spec's shape says, then read. The runtime has no mutable
/// descriptor object, so Metal's `init` is `MeshPipelineDescriptor::new` and its `reset` is the
/// same call again, the stages set again after it, as the Metal side sets them.
pub(super) fn mesh_descriptor(
&self,
library: Handle,
spec: &DescriptorSpec<'_>,
) -> Answer<MeshDescriptorFacts> {
let fresh = || -> Answer<rt::MeshPipelineDescriptor> {
let mut descriptor = self.mesh_stages(library, spec.object, spec.mesh)?;
descriptor.fragment = Some(self.kernel(library, spec.fragment)?);
Ok(descriptor)
};
let mut descriptor = fresh()?;
if spec.shape.sets() {
descriptor_reads::state_mesh(&mut descriptor);
}
if spec.shape.resets() {
descriptor = fresh()?;
}
Ok(descriptor_reads::read_mesh(
&descriptor,
label(),
binary_archives(),
!spec.shape.resets(),
))
}
/// [`crate::case::Side::mesh_command_buffer`]: the runtime's indirect command buffer of both
/// mesh draw kinds, each command setting its own state and buffers, which the runtime refuses
/// by its command types before it makes anything. Were one made, its commands could not be
/// encoded: the runtime encodes no command on the CPU.
pub(super) fn mesh_commands(&mut self, commands: &[MeshCommand]) -> Answer<Handle> {
let mut descriptor = rt::IndirectCommandBufferDescriptor::new();
descriptor.command_types = rt::IndirectCommandType::DRAW_MESH_THREADGROUPS
| rt::IndirectCommandType::DRAW_MESH_THREADS;
descriptor.inherit_pipeline_state = false;
descriptor.inherit_buffers = false;
let count = u32::try_from(commands.len().max(1))
.map_err(|_| Failure::named("TooManyCommands", format!("{}", commands.len())))?;
match self
.on
.device
.new_indirect_command_buffer(&descriptor, count)
{
Err(error) => Err(self.on.failure(&error)),
Ok(_) => Err(Failure::unavailable("indirectRenderCommandAtIndex:")),
}
}
}