diff --git a/crates/metal2vulkan-validation/src/replay/rt_replay.rs b/crates/metal2vulkan-validation/src/replay/rt_replay.rs index a1ddfe2ed..f72e75bd7 100644 --- a/crates/metal2vulkan-validation/src/replay/rt_replay.rs +++ b/crates/metal2vulkan-validation/src/replay/rt_replay.rs @@ -6,6 +6,10 @@ //! a pass's target that its setter records clear to red: a blit copies the target to a buffer, //! which the stream checks green, and nothing is skipped. //! - A draw of points after it plays, nothing skipped. +//! - A draw from an indirect buffer plays as the runtime's, indexed (of 16-bit and of 32-bit +//! indices) or not, nothing skipped: it reads its arguments at their offset, and its indices from +//! the index buffer's offset and the arguments' first index, so it draws from the vertex they +//! name, and the target reads green. //! - A render encoder's residency declarations play through the runtime: after each of its eight //! `useResource`, `useResources`, `useHeap` and `useHeaps` forms, with stages and without, of //! buffers, a texture and a heap, with read, read and sample, and read and write usage, a draw @@ -1190,6 +1194,196 @@ fn a_draw_of_points_plays_on_the_new_runtime() { assert_eq!(report.first_skip, None); } +/// A stream of the render fixture's triangle drawn from an indirect buffer into a pass whose +/// target its setter clears red, the target copied to a buffer that the stream checks green +/// (`green`). Vertex 0 is a blue one at the target's centre, and vertices 1 to 3 the green +/// triangle over the whole target, so only a draw from vertex 1 covers it green. With `indexed` +/// none, `drawPrimitives:indirectBuffer:indirectBufferOffset:` reads +/// `MTLDrawPrimitivesIndirectArguments` at byte 16 of #15: three vertices of one instance from +/// vertex 1. With `UInt16` or `UInt32` (`MTLIndexType` 0 or 1), +/// `drawIndexedPrimitives:indexType:indexBuffer:indexBufferOffset:indirectBuffer: +/// indirectBufferOffset:` reads `MTLDrawIndexedPrimitivesIndirectArguments` at byte 20 of #15: +/// three indices of one instance from index 1 of #16 past its offset of one index, whose indices +/// 7, 7, 1, 2, 3 make indices 1, 2 and 3 of the draw vertices 1, 2 and 3. +fn indirect_draw(dir: &ScratchDir, green: &[u8], indexed: Option) -> PathBuf { + written(dir, |w| { + let library = w.blob(&render_library(dir))?; + w.call("MTLCreateSystemDefaultDevice", 0, 1, &[], &[])?; + w.call("newCommandQueue", 1, 2, &[], &[])?; + w.call( + "newLibraryWithData:error:", + 1, + 3, + &[Value::Blob(library)], + &[], + )?; + let vertex = [Value::Str("colored_vertex".into())]; + w.call("newFunctionWithName:", 3, 4, &vertex, &[])?; + let fragment = [Value::Str("colored_fragment".into())]; + w.call("newFunctionWithName:", 3, 5, &fragment, &[])?; + let pipeline = Value::Map(vec![ + ("vertex".into(), Value::Object(4)), + ("fragment".into(), Value::Object(5)), + ("color0PixelFormat".into(), Value::Uint(70)), + ]); + let made = "newRenderPipelineStateWithDescriptor:error:"; + w.call(made, 1, 6, &[pipeline], &[])?; + // RGBA8Unorm, 4 by 4, shader-read and render-target, Private. + let target = uints(&[ + ("textureType", 2), + ("pixelFormat", 70), + ("width", 4), + ("height", 4), + ("usage", 5), + ("storageMode", 2), + ]); + w.call("newTextureWithDescriptor:", 1, 7, &[target], &[])?; + let positions = floats(&[0.0, 0.0, -1.0, -1.0, 3.0, -1.0, -1.0, 3.0]); + let colors: Vec = ([0.0, 0.0, 1.0, 1.0].into_iter()) + .chain([0.0, 1.0, 0.0, 1.0].repeat(3)) + .collect(); + let colors = floats(&colors); + let words = + |values: &[u32]| -> Vec { values.iter().flat_map(|v| v.to_le_bytes()).collect() }; + let (arguments, indices) = match indexed { + None => (words(&[99, 99, 99, 99, 3, 1, 1, 0]), None), + Some(index_type) => { + let indices: &[u32] = &[7, 7, 1, 2, 3]; + let indices: Vec = match index_type { + 0 => indices + .iter() + .flat_map(|&i| (i as u16).to_le_bytes()) + .collect(), + _ => words(indices), + }; + (words(&[99, 99, 99, 99, 99, 3, 1, 1, 0, 0]), Some(indices)) + } + }; + let buffers = [(8, positions), (9, colors), (15, Value::Bytes(arguments))]; + let indices = indices.map(|indices| (16, Value::Bytes(indices))); + for (id, bytes) in buffers.into_iter().chain(indices) { + let Value::Bytes(data) = &bytes else { + unreachable!("bytes"); + }; + let length = data.len() as u64; + let args = [bytes.clone(), Value::Uint(length), Value::Uint(0)]; + w.call("newBufferWithBytes:length:options:", 1, id, &args, &[])?; + } + let shared = [Value::Uint(64), Value::Uint(0)]; + w.call("newBufferWithLength:options:", 1, 10, &shared, &[])?; + w.call("MTLRenderPassDescriptor.new", 0, 11, &[], &[])?; + let pass = uints(&[("renderTargetWidth", 4), ("renderTargetHeight", 4)]); + w.call("m2v.setPass", 11, 0, &[pass], &[])?; + let Value::Map(mut color) = uints(&[("level", 0), ("loadAction", 2), ("storeAction", 1)]) + else { + unreachable!("a map"); + }; + color.push(("texture".into(), Value::Object(7))); + let red = [("red", 1.0), ("green", 0.0), ("blue", 0.0), ("alpha", 1.0)] + .map(|(key, value)| (key.into(), Value::F64(value))); + color.push(("clearColor".into(), Value::Map(red.to_vec()))); + let args = [Value::Uint(0), Value::Map(color)]; + w.call("m2v.setColorAttachment", 11, 0, &args, &[])?; + w.call("commandBuffer", 2, 12, &[], &[])?; + let pass = [Value::Object(11)]; + w.call("renderCommandEncoderWithDescriptor:", 12, 13, &pass, &[])?; + w.call("setRenderPipelineState:", 13, 0, &[Value::Object(6)], &[])?; + for (buffer, index) in [(8, 0), (9, 1)] { + let args = [Value::Object(buffer), Value::Uint(0), Value::Uint(index)]; + w.call("setVertexBuffer:offset:atIndex:", 13, 0, &args, &[])?; + } + // MTLPrimitiveTypeTriangle. + match indexed { + None => { + let draw = [Value::Uint(3), Value::Object(15), Value::Uint(16)]; + let selector = "drawPrimitives:indirectBuffer:indirectBufferOffset:"; + w.call(selector, 13, 0, &draw, &[])?; + } + Some(index_type) => { + let size = if index_type == 0 { 2 } else { 4 }; + let draw = [ + Value::Uint(3), + Value::Uint(index_type), + Value::Object(16), + Value::Uint(size), + Value::Object(15), + Value::Uint(20), + ]; + let selector = "drawIndexedPrimitives:indexType:indexBuffer:indexBufferOffset:\ + indirectBuffer:indirectBufferOffset:"; + w.call(selector, 13, 0, &draw, &[])?; + } + } + w.call("endEncoding", 13, 0, &[], &[])?; + w.call("blitCommandEncoder", 12, 14, &[], &[])?; + let copy = [ + Value::Object(7), + Value::Uint(0), + Value::Uint(0), + uints(&[("x", 0), ("y", 0), ("z", 0)]), + uints(&[("width", 4), ("height", 4), ("depth", 1)]), + Value::Object(10), + Value::Uint(0), + Value::Uint(16), + Value::Uint(64), + ]; + let selector = "copyFromTexture:sourceSlice:sourceLevel:sourceOrigin:sourceSize:toBuffer:\ + destinationOffset:destinationBytesPerRow:destinationBytesPerImage:"; + w.call(selector, 14, 0, ©, &[])?; + w.call("endEncoding", 14, 0, &[], &[])?; + w.call("commit", 12, 0, &[], &[])?; + w.call(WAIT_COMPLETED, 12, 0, &[], &[])?; + let sum = Value::List(vec![Value::Bytes(Sha256::digest(green).to_vec())]); + w.call("m2v.check", 0, 0, &[Value::Object(10)], &[("sha256", sum)]) + }) +} + +/// [`indirect_draw`]'s stream with `indexed` played: the draw reads its arguments, and its +/// indices, at their offsets, nothing skipped, and the target reads green. +fn indirect_draw_plays(test: &str, indexed: Option) { + let dir = ScratchDir::new(&format!("rt-replay-{test}")).unwrap(); + let green: Vec = [0u8, 255, 0, 255].repeat(16); + let path = indirect_draw(&dir, &green, indexed); + let (report, _) = play(test, &path); + let executed = &report.total.executed; + for skipped in [ + "skipped_encoders", + "skipped_render_states", + "skipped_draws", + "skipped_calls", + "skipped_objects", + ] { + assert_eq!(executed[skipped], 0, "{indexed:?} {skipped}: {executed:?}"); + } + assert_eq!(report.first_skip, None, "{indexed:?}"); + assert_eq!( + Some(report.checks[0].sha256), + report.checks[0].recorded, + "{indexed:?}" + ); +} + +/// `drawPrimitives:indirectBuffer:indirectBufferOffset:` plays as the runtime's +/// `draw_primitives_indirect`. +#[test] +fn an_indirect_draw_plays_on_the_new_runtime() { + indirect_draw_plays("indirect-draw", None); +} + +/// `drawIndexedPrimitives:indexType:indexBuffer:indexBufferOffset:indirectBuffer: +/// indirectBufferOffset:` plays as the runtime's `draw_indexed_primitives_indirect`, of 16-bit +/// indices. +#[test] +fn an_indirect_indexed_draw_of_16_bit_indices_plays_on_the_new_runtime() { + indirect_draw_plays("indirect-indexed-draw-16", Some(0)); +} + +/// The same draw of 32-bit indices. +#[test] +fn an_indirect_indexed_draw_of_32_bit_indices_plays_on_the_new_runtime() { + indirect_draw_plays("indirect-indexed-draw-32", Some(1)); +} + /// A colour attachment's `depthPlane` plays as the runtime's: of an `RGBA8Unorm` 3D texture /// (#3, 4 x 4 x 4, Private), a pass with no plane set clears plane 0 green and one at /// `depthPlane` 2 clears plane 2 red; of a 2D texture (#4), a pass at `depthPlane` 2 clears it red,