diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs index 2e206072f..78e2c5fc0 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/mod.rs @@ -65,6 +65,9 @@ struct Skips { /// does not build yet. texture_syncs: u64, mipmaps: u64, + /// Sampler binds of LOD clamps the runtime answers with `Gap::LodClamps`: clamps whose samples + /// read otherwise than the sampler's own. + clamped_samplers: u64, /// Every other call on a skipped object or naming one, a skipped encoder's among them. calls: u64, /// The skipped objects whose `gpuResourceID` the player asked: nothing in the play reads @@ -123,6 +126,7 @@ impl Skips { guest_ram_writes, texture_syncs, mipmaps, + clamped_samplers, calls, resource_ids, } = self; @@ -135,6 +139,7 @@ impl Skips { guest_ram_writes, texture_syncs, mipmaps, + clamped_samplers, calls, resource_ids, ]; @@ -2185,6 +2190,7 @@ impl Backend for NewRuntime { ("skipped_guest_ram_writes", self.skips.guest_ram_writes), ("skipped_texture_syncs", self.skips.texture_syncs), ("skipped_mipmaps", self.skips.mipmaps), + ("skipped_clamped_samplers", self.skips.clamped_samplers), ("substituted_surface_textures", self.substitutes.surfaces), ("substituted_tiled_textures", self.substitutes.tiled), ("substituted_gap_textures", self.substitutes.gaps), diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/render.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/render.rs index 52ec6abec..3bffd7438 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/render.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/render.rs @@ -23,11 +23,13 @@ use std::collections::HashSet; /// Where a binding is: the stage (fragment or not) and whether it is a texture. type Slot = (bool, bool, u32); -/// A render encoder, its stages' buffer and texture slots that hold a skipped object, and whether -/// its pipeline state is a skipped one. +/// A render encoder, its stages' buffer and texture slots that hold a skipped object, its stages' +/// sampler slots whose bind it skipped (`clamped`), and whether its pipeline state is a skipped +/// one. pub(super) struct Render { encoder: rt::RenderCommandEncoder, skipped: HashSet, + skipped_samplers: HashSet<(bool, u32)>, skipped_pipeline: bool, } @@ -186,6 +188,7 @@ impl NewRuntime { Ok(encoder) => unaddressed(Object::Render(Box::new(Render { encoder, skipped: HashSet::new(), + skipped_samplers: HashSet::new(), skipped_pipeline: false, }))), Err(rt::Error::Unsupported(_)) => self.skip_encoder(), @@ -465,24 +468,41 @@ impl NewRuntime { "setVertexSamplerState:atIndex:" | "setFragmentSamplerState:atIndex:" => { let sampler = self.sampler(call, arg(call, 0)?)?; let at = index(call, 1)?; - let encoder = &mut self.render(id).encoder; - match fragment { - false => encoder.set_vertex_sampler_state(sampler.as_ref(), at), - true => encoder.set_fragment_sampler_state(sampler.as_ref(), at), - } + self.bind_sampler(id, fragment, at, sampler, None) } "setVertexSamplerStates:withRange:" | "setFragmentSamplerStates:withRange:" => { let samplers = list(call, 0)?.iter(); let samplers = samplers.map(|value| self.sampler(call, value)); let samplers = samplers.collect::, _>>()?; let at = first(call, 1, samplers.len())?; - let encoder = &mut self.render(id).encoder; let mut result = Ok(()); - for (slot, sampler) in (at..).zip(&samplers) { - result = result.and(match fragment { - false => encoder.set_vertex_sampler_state(sampler.as_ref(), slot), - true => encoder.set_fragment_sampler_state(sampler.as_ref(), slot), - }); + for (slot, sampler) in (at..).zip(samplers) { + result = result.and(self.bind_sampler(id, fragment, slot, sampler, None)); + } + result + } + "setVertexSamplerState:lodMinClamp:lodMaxClamp:atIndex:" + | "setFragmentSamplerState:lodMinClamp:lodMaxClamp:atIndex:" => { + let sampler = self.sampler(call, arg(call, 0)?)?; + let clamps = [clamp(call, arg(call, 1)?)?, clamp(call, arg(call, 2)?)?]; + let at = index(call, 3)?; + self.bind_sampler(id, fragment, at, sampler, Some(clamps)) + } + "setVertexSamplerStates:lodMinClamps:lodMaxClamps:withRange:" + | "setFragmentSamplerStates:lodMinClamps:lodMaxClamps:withRange:" => { + let samplers = list(call, 0)?.iter(); + let samplers = samplers.map(|value| self.sampler(call, value)); + let samplers = samplers.collect::, _>>()?; + let (mins, maxes) = (list(call, 1)?, list(call, 2)?); + if mins.len() != samplers.len() || maxes.len() != samplers.len() { + return Err(refuse(call, "clamps of another count than the samplers")); + } + let at = first(call, 3, samplers.len())?; + let mut result = Ok(()); + for (k, (slot, sampler)) in (at..).zip(samplers).enumerate() { + let clamps = [clamp(call, &mins[k])?, clamp(call, &maxes[k])?]; + result = + result.and(self.bind_sampler(id, fragment, slot, sampler, Some(clamps))); } result } @@ -672,13 +692,53 @@ impl NewRuntime { /// buffer or texture is a skipped object. fn draw_skipped(&mut self, id: u64) -> bool { let render = self.render(id); - let skipped = render.skipped_pipeline || !render.skipped.is_empty(); + let skipped = render.skipped_pipeline + || !render.skipped.is_empty() + || !render.skipped_samplers.is_empty(); if skipped { self.skips.draws += 1; } skipped } + /// Binds `sampler` at `slot` of the stage `fragment` names, with the LOD clamps `clamps` where + /// the call states them. A bind the runtime answers with [`rt::Gap::LodClamps`] (clamps whose + /// samples read otherwise than the sampler's own) binds nothing: it is skipped and counted + /// (`clamped_samplers`), and draws are skipped while the slot holds it. + fn bind_sampler( + &mut self, + id: u64, + fragment: bool, + slot: u32, + sampler: Option, + clamps: Option<[f32; 2]>, + ) -> Result<(), rt::Error> { + let encoder = &mut self.render(id).encoder; + let sampler = sampler.as_ref(); + let bound = match (clamps, fragment) { + (None, false) => encoder.set_vertex_sampler_state(sampler, slot), + (None, true) => encoder.set_fragment_sampler_state(sampler, slot), + (Some([min, max]), false) => { + encoder.set_vertex_sampler_state_with_lod_clamps(sampler, min, max, slot) + } + (Some([min, max]), true) => { + encoder.set_fragment_sampler_state_with_lod_clamps(sampler, min, max, slot) + } + }; + match bound { + Err(rt::Error::Unsupported(rt::Gap::LodClamps)) => { + self.skips.clamped_samplers += 1; + self.render(id).skipped_samplers.insert((fragment, slot)); + Ok(()) + } + Ok(()) => { + self.render(id).skipped_samplers.remove(&(fragment, slot)); + Ok(()) + } + failed => failed, + } + } + /// The sampler state `value` names, or none where it is nil. fn sampler(&self, call: &Call, value: &Value) -> Result, PlayError> { match value { @@ -691,3 +751,11 @@ impl NewRuntime { } } } + +/// A LOD clamp argument, a number. +fn clamp(call: &Call, value: &Value) -> Result { + let clamp = value + .as_f64() + .ok_or_else(|| refuse(call, format!("{value} is no clamp")))?; + Ok(clamp as f32) +} diff --git a/crates/metal2vulkan-validation/src/runtime_lane/rt_replay.rs b/crates/metal2vulkan-validation/src/runtime_lane/rt_replay.rs index b6380c213..bcadcc623 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/rt_replay.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/rt_replay.rs @@ -12,6 +12,9 @@ //! - A compute encoder's `setStageInRegion:` plays: the runtime takes it. //! - The host's library from source is made of its display library where the backend has it, a //! function of it with it; without it both are skipped. +//! - A sampler bound with LOD clamps whose samples read as its own plays with them (its own +//! clamps, and (0, 1000) over its (0, FLT_MAX)); one bound with clamps that read otherwise is +//! the runtime's typed gap, skipped and counted, and the draw after it is skipped. //! - A Managed buffer of a stream recorded with two copies keeps both on any device (the player's //! own second copy where the device has one): a blit's fill reaches the GPU's copy and not the //! CPU's, an announcement with no write puts the CPU's bytes back into the GPU's copy at the next @@ -209,6 +212,12 @@ fn library(dir: &ScratchDir, functions: &[(&'static str, u8, usize)]) -> Vec /// A stream of a draw into a pass, its target copied to a buffer that the stream checks green /// (`green`); with `points`, a draw of points (call 19) after the draw. fn draw_into_a_pass(dir: &ScratchDir, green: &[u8], points: bool) -> PathBuf { + draw_with_clamps(dir, green, points, None) +} + +/// [`draw_into_a_pass`]'s stream; where `clamp` is given, a sampler state of the default +/// descriptor (#15) bound at fragment slot 0 with LOD clamps 0 and `clamp` before the draw. +fn draw_with_clamps(dir: &ScratchDir, green: &[u8], points: bool, clamp: Option) -> PathBuf { written(dir, |w| { let library = w.blob(&render_library(dir))?; w.call("MTLCreateSystemDefaultDevice", 0, 1, &[], &[])?; @@ -279,6 +288,23 @@ fn draw_into_a_pass(dir: &ScratchDir, green: &[u8], points: bool) -> PathBuf { let args = [Value::Object(buffer), Value::Uint(0), Value::Uint(index)]; w.call("setVertexBuffer:offset:atIndex:", 13, 0, &args, &[])?; } + if let Some(clamp) = clamp { + w.call( + "newSamplerStateWithDescriptor:", + 1, + 15, + &[sampler(None)], + &[], + )?; + let args = [ + Value::List(vec![Value::Object(15)]), + Value::List(vec![Value::F32(0.0)]), + Value::List(vec![Value::F32(clamp)]), + uints(&[("location", 0), ("length", 1)]), + ]; + let bind = "setFragmentSamplerStates:lodMinClamps:lodMaxClamps:withRange:"; + w.call(bind, 13, 0, &args, &[])?; + } // MTLPrimitiveTypeTriangle, from vertex 0, three vertices. let draw = [Value::Uint(3), Value::Uint(0), Value::Uint(3)]; w.call("drawPrimitives:vertexStart:vertexCount:", 13, 0, &draw, &[])?; @@ -376,6 +402,28 @@ fn the_host_s_library_from_source_is_made_of_its_display_library() { assert_eq!(played(None), (1, 1)); } +#[test] +fn a_sampler_bound_with_lod_clamps_plays_where_the_runtime_takes_them() { + let dir = ScratchDir::new("rt-replay-clamps").unwrap(); + let green: Vec = [0u8, 255, 0, 255].repeat(16); + // The sampler's own clamps, and (0, 1000), which reads as its own (0, FLT_MAX): both bind. + for (test, clamp) in [("clamps-own", f32::MAX), ("clamps-1000", 1000.0)] { + let (report, _) = play(test, &draw_with_clamps(&dir, &green, false, Some(clamp))); + assert_eq!(Some(report.checks[0].sha256), report.checks[0].recorded); + let executed = &report.total.executed; + assert_eq!(executed["skipped_clamped_samplers"], 0, "{executed:?}"); + assert_eq!(executed["skipped_draws"], 0, "{executed:?}"); + } + // (0, 2) reads otherwise: the runtime's gap, the bind and the draw skipped. + let (report, _) = play( + "clamps-2", + &draw_with_clamps(&dir, &green, false, Some(2.0)), + ); + let executed = &report.total.executed; + assert_eq!(executed["skipped_clamped_samplers"], 1, "{executed:?}"); + assert_eq!(executed["skipped_draws"], 1, "{executed:?}"); +} + #[test] fn a_render_state_the_runtime_does_not_build_is_counted_by_its_gap() { let dir = ScratchDir::new("rt-replay-state-gap").unwrap(); diff --git a/docs/REPLAY-STREAM.md b/docs/REPLAY-STREAM.md index bf83077fd..474e291c5 100644 --- a/docs/REPLAY-STREAM.md +++ b/docs/REPLAY-STREAM.md @@ -608,6 +608,9 @@ counted from 0), completes the work committed so far, reports as at the stream's either stage, or whose index buffer is one (`skipped_draws`); - render commands the runtime answers with a typed gap, a draw it does not build among them (`skipped_render_commands`, and a line for each gap with its first frame and call); + - render encoders' sampler binds of LOD clamps the runtime answers with its typed gap (clamps + whose samples read otherwise than the sampler's own: `skipped_clamped_samplers`), the draws + while the slot holds one skipped with them; a bind it takes plays with its clamps; - blit commands that name a skipped object (`skipped_blits`), and by kind: `synchronizeTexture:slice:level:` (`skipped_texture_syncs`), mipmaps (`skipped_mipmaps`), and the pieces of a copy from the guest's RAM that are not whole 4-byte words at a 4-byte offset,