diff --git a/crates/metal2vulkan-runtime/src/tests/heap_alias.rs b/crates/metal2vulkan-runtime/src/tests/heap_alias.rs index 3eded4bc0..57439794d 100644 --- a/crates/metal2vulkan-runtime/src/tests/heap_alias.rs +++ b/crates/metal2vulkan-runtime/src/tests/heap_alias.rs @@ -644,4 +644,216 @@ pub(crate) mod on_device { println!("heap alias: {name}: the aliased arrays read back across command buffers"); }); } + + /// The pairs [`hand_overs`] places at offset 0 of one heap, A then B, B over A's bytes: each + /// one's format, width and height. + const PAIRS: [[(PixelFormat, u32, u32); 2]; 3] = [ + [ + (PixelFormat::RGBA16Float, 128, 64), + (PixelFormat::RGBA16Float, 128, 64), + ], + [ + (PixelFormat::RGBA16Float, 128, 64), + (PixelFormat::RGBA32Float, 64, 64), + ], + [ + (PixelFormat::RGBA8Unorm, 256, 40), + (PixelFormat::R32Float, 200, 96), + ], + ]; + + /// The runs of each pair under each hand-over. + const RUNS: u32 = 256; + + /// How [`hand_overs`] hands A's bytes to B. + #[derive(Clone, Copy, Debug)] + enum HandOver { + /// One serial compute encoder: A written, A read, B written over A's bytes, B read, with + /// no fence. + OneEncoder, + /// A written and read in one command buffer, B in the next of the same queue. + NextCommandBuffer, + /// A placed, written and read on one queue and released once its command buffer + /// completed; then B placed, written and read on another queue, and released before the + /// next run places A. + OtherQueue, + } + + /// Encodes into `e` `rows_write` of `input` into `p` itself, then `rows_read` of `p` into + /// `out`, with no fence between. + fn write_then_read( + name: &str, + e: &mut crate::ComputeCommandEncoder, + kernels: &Kernels, + p: &Placed, + (input, out): (&Buffer, &Buffer), + ) { + let done = |result: Result<(), crate::Error>| { + result.unwrap_or_else(|error| panic!("{name}: texture {}: {error}", p.t)) + }; + done(e.set_compute_pipeline_state(&kernels.write)); + done(e.set_buffer(Some(input), 0, 0)); + done(e.set_bytes(&p.width.to_ne_bytes(), 1)); + done(e.set_texture(Some(&p.texture), 0)); + done(e.dispatch_threads([p.width, p.height, 1], [8, 8, 1])); + done(e.set_compute_pipeline_state(&kernels.read)); + done(e.set_texture(Some(&p.texture), 0)); + done(e.set_buffer(Some(out), 0, 0)); + done(e.set_bytes(&p.width.to_ne_bytes(), 1)); + done(e.dispatch_threads([p.width, p.height, 1], [8, 8, 1])); + } + + /// A program's hand-overs of one heap memory, which keep Metal's aliasing contract: for each + /// of the [`PAIRS`], [`RUNS`] runs of A written and read, then B written over A's bytes and + /// read, handed over by `how`, each run's reads compared with what each texture's write + /// wrote. A line for each read that is not its write, naming its run, the first 16; and a + /// printed count of the runs that read one. + fn hand_overs(name: &str, device: &Device, kernels: &Kernels, how: HandOver) -> Vec { + let len = MemoryClass::Private.block_len(); + let block = (device.take_block(MemoryClass::Private, len)) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let heap = HeapRange { + block: block.id, + offset: 0, + len, + }; + let first = 4500; + let _grant = (device.grant_indices(IndexClass::Texture, first, 4)) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let queues = [0; 2].map(|_| device.new_command_queue(4).expect("a queue")); + let mut lines = Vec::new(); + for (at, [a_shape, b_shape]) in PAIRS.into_iter().enumerate() { + let t = 40 + 2 * at as u32; + let (a_format, a_width, a_height) = a_shape; + let (b_format, b_width, b_height) = b_shape; + let a_place = || place(name, device, heap, (t, a_format, a_width, a_height), first); + let b_place = || { + place( + name, + device, + heap, + (t + 1, b_format, b_width, b_height), + first + 2, + ) + }; + let mut buffers = Buffers::new(name, device, 4); + let floats = [input(&a_place()), input(&b_place())]; + let inputs = [0, 1].map(|at| buffers.of(name, device, &floats[at])); + let outs = [0, 1].map(|at| buffers.of(name, device, &vec![f32::NAN; floats[at].len()])); + let (a, mut b) = match how { + HandOver::OtherQueue => (None, None), + _ => (Some(a_place()), Some(b_place())), + }; + let encode = |queue: &crate::CommandQueue, ps: &[(&Placed, usize)]| { + let cb = queue.command_buffer().expect("a command buffer"); + let mut e = (cb.compute_command_encoder(DispatchType::Serial)).expect("an encoder"); + for &(p, at) in ps { + write_then_read(name, &mut e, kernels, p, (&inputs[at], &outs[at])); + } + e.end_encoding(); + cb.commit().expect("a commit"); + cb + }; + let done = |what: &str, cb: &CommandBuffer| { + finished(name, cb); + assert_eq!(cb.error(), None, "{name}: {what}"); + }; + let check = |what: &str, p: &Placed, at: usize| { + wrong(what, p, &outs[at], &reads(p, &floats[at])) + }; + let mut runs_wrong = 0; + for run in 0..RUNS { + for out in &outs { + let contents = out.contents().expect("Shared"); + // SAFETY: the buffer's bytes, which no command uses between runs. + unsafe { contents.as_ptr().write_bytes(0xff, out.length() as usize) }; + } + let what = format!("{how:?}, pair {at}, run {run}"); + let mut this = Vec::new(); + match how { + HandOver::OneEncoder => { + let (a, b) = (a.as_ref().expect("A"), b.as_ref().expect("B")); + done(&what, &encode(&queues[0], &[(a, 0), (b, 1)])); + this.extend(check(&what, a, 0)); + this.extend(check(&what, b, 1)); + } + HandOver::NextCommandBuffer => { + let (a, b) = (a.as_ref().expect("A"), b.as_ref().expect("B")); + let cbs = [encode(&queues[0], &[(a, 0)]), encode(&queues[0], &[(b, 1)])]; + for cb in &cbs { + done(&what, cb); + } + this.extend(check(&what, a, 0)); + this.extend(check(&what, b, 1)); + } + HandOver::OtherQueue => { + // The last run's B goes before A is placed over its bytes. + b = None; + let placed = a_place(); + done(&what, &encode(&queues[0], &[(&placed, 0)])); + this.extend(check(&what, &placed, 0)); + drop(placed); + let placed = b_place(); + done(&what, &encode(&queues[1], &[(&placed, 1)])); + this.extend(check(&what, &placed, 1)); + b = Some(placed); + } + } + if !this.is_empty() { + runs_wrong += 1; + if lines.len() < 16 { + lines.extend(this); + } + } + } + println!( + "heap alias: {name}: {how:?}, pair {at} ({a_shape:?} then {b_shape:?}): \ + {runs_wrong} of {RUNS} runs read a value not written" + ); + drop((a, b, inputs, outs, buffers)); + } + drop(queues); + (device.release_block(block)).unwrap_or_else(|error| panic!("{name}: {error}")); + lines + } + + /// A program's hand-overs of one heap memory in one serial compute encoder, with no fence: + /// run after run, each read is its write. + #[test] + fn every_device_reads_each_aliased_write_in_one_serial_encoder_run_after_run() { + let Some(libraries) = libraries() else { + return; + }; + each_device("heap_alias_one_encoder", |name, _, device| { + let kernels = kernels(name, &device, &libraries); + let lines = hand_overs(name, &device, &kernels, HandOver::OneEncoder); + assert!(lines.is_empty(), "{name}: {lines:#?}"); + }); + } + + /// As in one serial encoder, with B in the next command buffer of the same queue. + #[test] + fn every_device_reads_each_aliased_write_in_the_next_command_buffer_run_after_run() { + let Some(libraries) = libraries() else { + return; + }; + each_device("heap_alias_next_command_buffer", |name, _, device| { + let kernels = kernels(name, &device, &libraries); + let lines = hand_overs(name, &device, &kernels, HandOver::NextCommandBuffer); + assert!(lines.is_empty(), "{name}: {lines:#?}"); + }); + } + + /// As in one serial encoder, with A released and B placed and used on another queue. + #[test] + fn every_device_reads_each_aliased_write_on_another_queue_run_after_run() { + let Some(libraries) = libraries() else { + return; + }; + each_device("heap_alias_other_queue", |name, _, device| { + let kernels = kernels(name, &device, &libraries); + let lines = hand_overs(name, &device, &kernels, HandOver::OtherQueue); + assert!(lines.is_empty(), "{name}: {lines:#?}"); + }); + } }