diff --git a/crates/metal2vulkan-validation/src/runtime_lane/buffer_texture_renders.rs b/crates/metal2vulkan-validation/src/runtime_lane/buffer_texture_renders.rs index 9885da042..ab38ba257 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/buffer_texture_renders.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/buffer_texture_renders.rs @@ -29,6 +29,7 @@ use super::buffer_texture_blits::{hex_rows, texel, texels, Held, Kind, OFF}; use super::degrade_notices::counts; +use super::indirect_host_steps::encoding_state_set; use super::{ buffer_with, layer_reported_nothing_at_all, read_buffer, validated_device, Messages, ScratchCache, WAIT, @@ -1086,7 +1087,8 @@ fn a_shadowed_pass_executes_commands_the_gpu_may_write() { #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Before { Nothing, - /// A compute encoder that makes the indirect command buffer resident for writing. + /// A compute encoder that makes the indirect command buffer resident for writing, after one + /// that sets a state that may encode (`indirect_host_steps::encoding_state_set`). Writer, /// That, and a compute encoder after the pass that makes the shadowed texture resident. WriterAndTexture, @@ -1136,6 +1138,7 @@ fn executed(lane: &Lane, kind: Kind, before: Before, bpr: u64) { let o = lane.ordinary(rgba, ALL, None); let command_buffer = lane.queue.command_buffer().unwrap(); if before != Before::Nothing { + encoding_state_set(&command_buffer); let mut writer = command_buffer.compute_command_encoder().unwrap(); writer .use_resource(icb.as_resource(), ResourceUsage::WRITE) diff --git a/crates/metal2vulkan-validation/src/runtime_lane/dispatch_executions.rs b/crates/metal2vulkan-validation/src/runtime_lane/dispatch_executions.rs index 40518a254..477641600 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/dispatch_executions.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/dispatch_executions.rs @@ -277,9 +277,11 @@ impl Setup { } /// Make the buffer resident for writing in an encoder of `command_buffer` that writes - /// nothing: from then on the GPU may write its records, so an execution reads them on the - /// GPU or in a host step. + /// nothing, after one that sets a state that may encode indirect commands + /// (`indirect_host_steps::encoding_state_set`): from then on the GPU may write its records, so + /// an execution reads them on the GPU or in a host step. fn writable(&self, command_buffer: &CommandBuffer) { + indirect_host_steps::encoding_state_set(command_buffer); let mut encoder = command_buffer.compute_command_encoder().unwrap(); encoder .use_resource(self.icb.as_resource(), ResourceUsage::WRITE) diff --git a/crates/metal2vulkan-validation/src/runtime_lane/indirect_command_buffers.rs b/crates/metal2vulkan-validation/src/runtime_lane/indirect_command_buffers.rs index d4a704bef..c2570ea0f 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/indirect_command_buffers.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/indirect_command_buffers.rs @@ -9,6 +9,7 @@ use super::degrade_notices::{self as notices, counts}; use super::host_memory::on_device; +use super::indirect_host_steps; use super::{buffer_with, read_buffer, Messages, WAIT}; use crate::air_library::AirLibrary; use metal2vulkan::bind::indirect_commands::COMMAND_BYTES; @@ -3303,22 +3304,48 @@ fn states_whose_functions_use_a_direct_texture_or_sampler_are_refused_as_metal_r }); } -/// `IndirectCommandBuffer::gpu_written`: `false` for a fresh buffer, and after a read use of it, -/// an empty copy into it, an empty reset of it and a copy out of it; `true`, at the call, before -/// any commit, after a write use of it in a compute encoder, a copy of a command into it, and a -/// reset of a command of it, each on a fresh buffer. +/// `IndirectCommandBuffer::gpu_written`: `false` for a fresh buffer, after a read use of it beside +/// a state that may encode indirect commands, after a write use of it beside no state or beside +/// states that cannot encode (once its command buffer completed too, its commit counting the +/// buffer it left unmarked: `WorkCounts::indirect_write_residencies_unmarked`), and after an empty +/// copy into it, an empty reset of it and a copy out of it; `true` from the call that completes a +/// route that may write its records, before any commit: a write use after a state that may encode +/// was set, a state that may encode set after a write use, in its encoder or a later one of its +/// command buffer, a copy of a command into it, and a reset of a command of it, each on a fresh +/// buffer. #[test] -fn the_gpu_written_flag_is_set_by_a_write_use_or_a_blit_into_the_buffer() { +fn the_gpu_written_flag_is_set_by_a_write_use_beside_an_encoding_state_or_a_blit_into_it() { on_device(|device| { let queue = device.new_command_queue().unwrap(); let fresh = || icb(device, &compute_descriptor(), 2); - let used = |buffer: &IndirectCommandBuffer, usage: ResourceUsage| { + let encoding = indirect_host_steps::state(device, "icb_replay_encode", false); + let plain = template_state(device, false); + // A use of `buffer` with `usage` in a compute encoder that sets `before` first and `after` + // after: the flag after the use, after `after`, and once the command buffer completed, + // and the buffers its commit left unmarked. + let used = |buffer: &IndirectCommandBuffer, + usage: ResourceUsage, + (before, after): ( + Option<&ComputePipelineState>, + Option<&ComputePipelineState>, + )| { let command_buffer = queue.command_buffer().unwrap(); let mut encoder = command_buffer.compute_command_encoder().unwrap(); + if let Some(state) = before { + encoder.set_compute_pipeline_state(state).unwrap(); + } encoder.use_resource(buffer.as_resource(), usage).unwrap(); - let written = buffer.gpu_written(); + let at_use = buffer.gpu_written(); + if let Some(state) = after { + encoder.set_compute_pipeline_state(state).unwrap(); + } + let at_state = buffer.gpu_written(); encoder.end_encoding().unwrap(); - written + let counted = device.work_counts().indirect_write_residencies_unmarked; + command_buffer.commit().unwrap(); + command_buffer.wait_until_completed_for(WAIT).unwrap(); + let unmarked = device.work_counts().indirect_write_residencies_unmarked - counted; + (at_use, at_state, buffer.gpu_written(), unmarked) }; let blitted = |blit: &dyn Fn(&mut BlitCommandEncoder) -> Result<(), Error>, buffer: &IndirectCommandBuffer| { @@ -3329,11 +3356,21 @@ fn the_gpu_written_flag_is_set_by_a_write_use_or_a_blit_into_the_buffer() { encoder.end_encoding().unwrap(); written }; + let write = ResourceUsage::WRITE; // Nothing the GPU may write through. let buffer = fresh(); let other = fresh(); assert!(!buffer.gpu_written()); - assert!(!used(&buffer, ResourceUsage::READ)); + let read = used(&buffer, ResourceUsage::READ, (Some(&encoding), None)); + assert_eq!(read, (false, false, false, 0), "a read use"); + let alone = used(&buffer, write, (None, None)); + assert_eq!(alone, (false, false, false, 1), "a write use alone"); + let plainly = used(&buffer, write, (Some(&plain), Some(&plain))); + assert_eq!( + plainly, + (false, false, false, 1), + "a write use beside plain states" + ); assert!(!blitted( &|blit| blit.copy_indirect_command_buffer(&other, 0..0, &buffer, 0), &buffer @@ -3346,15 +3383,43 @@ fn the_gpu_written_flag_is_set_by_a_write_use_or_a_blit_into_the_buffer() { &|blit| blit.copy_indirect_command_buffer(&buffer, 0..1, &other, 0), &buffer )); - // Each way it is set, at the call, for good. + // Each way it is set, at the call that completes the route, for good. let buffer = fresh(); - assert!(used(&buffer, ResourceUsage::WRITE)); - assert!(buffer.gpu_written()); + let after_a_state = used(&buffer, write, (Some(&encoding), None)); + assert_eq!( + after_a_state, + (true, true, true, 0), + "a state, then a write use" + ); let buffer = fresh(); - assert!(used( + let before_a_state = used(&buffer, write, (Some(&plain), Some(&encoding))); + assert_eq!( + before_a_state, + (false, true, true, 0), + "a write use, then a state" + ); + let buffer = fresh(); + let both = used( &buffer, - ResourceUsage::READ.union(ResourceUsage::WRITE) - )); + ResourceUsage::READ.union(write), + (Some(&encoding), None), + ); + assert_eq!( + both, + (true, true, true, 0), + "a state, then a read and write use" + ); + let buffer = fresh(); + let command_buffer = queue.command_buffer().unwrap(); + let mut using = command_buffer.compute_command_encoder().unwrap(); + using.use_resource(buffer.as_resource(), write).unwrap(); + using.end_encoding().unwrap(); + let mut setting = command_buffer.compute_command_encoder().unwrap(); + assert!(!buffer.gpu_written()); + setting.set_compute_pipeline_state(&encoding).unwrap(); + assert!(buffer.gpu_written(), "a state set in a later encoder"); + setting.end_encoding().unwrap(); + drop(command_buffer); let buffer = fresh(); assert!(blitted( &|blit| blit.copy_indirect_command_buffer(&other, 0..1, &buffer, 1), @@ -3366,8 +3431,8 @@ fn the_gpu_written_flag_is_set_by_a_write_use_or_a_blit_into_the_buffer() { &buffer )); eprintln!( - "CBF-MARK {}: the GPU-written flag was set by a write use and blits into the buffer \ - alone", + "CBF-MARK {}: the GPU-written flag was set by a write use beside a state that may \ + encode and by blits into the buffer alone", device.name() ); }); diff --git a/crates/metal2vulkan-validation/src/runtime_lane/indirect_host_steps.rs b/crates/metal2vulkan-validation/src/runtime_lane/indirect_host_steps.rs index 829c8cc2d..ad6795c08 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/indirect_host_steps.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/indirect_host_steps.rs @@ -118,6 +118,19 @@ pub(super) fn state(device: &Device, stem: &str, indirect: bool) -> ComputePipel .unwrap() } +/// Set a state that may encode indirect commands (`icb_replay_encode`'s, made without +/// `supportIndirectCommandBuffers`, so no record names it) in an encoder of `command_buffer` of its +/// own that runs nothing: a buffer the command buffer makes resident for writing, before or after, +/// may be written by the GPU (`IndirectCommandBuffer::gpu_written`), and what its records may hold +/// gains nothing of the state, which the encoder that made it resident did not set +/// (`IndirectCommandBuffer::command_writes`). +pub(super) fn encoding_state_set(command_buffer: &CommandBuffer) { + let state = state(command_buffer.device(), "icb_replay_encode", false); + let mut encoder = command_buffer.compute_command_encoder().unwrap(); + encoder.set_compute_pipeline_state(&state).unwrap(); + encoder.end_encoding().unwrap(); +} + /// What a test runs: the writing kernel's state, the template's, a buffer of `COMMANDS` /// commands of both dispatch kinds with `bind_count` kernel buffers, the writer's argument /// buffer (the buffer's ID, then the template's), `params` and `out`. diff --git a/crates/metal2vulkan-validation/src/runtime_lane/render_merged_executions.rs b/crates/metal2vulkan-validation/src/runtime_lane/render_merged_executions.rs index 709e0ca0e..6df338e09 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/render_merged_executions.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/render_merged_executions.rs @@ -29,7 +29,7 @@ use super::degrade_notices::{ }; use super::host_memory::Mapping; use super::host_memory::Seen; -use super::indirect_host_steps::{complete, HOST_STEPS}; +use super::indirect_host_steps::{complete, encoding_state_set, HOST_STEPS}; use super::rebind_reach::{buffer_len, buffer_of, buffer_over, pages}; use super::rebinds::rebind; use super::render_executions::{forced_with, icb}; @@ -232,8 +232,9 @@ impl Scene { } /// Encode into `command_buffer`: a compute encoder that makes `icb` resident for writing, - /// `writer`, where given, writing it over `threads` threads, then a pass over the target - /// that executes commands `0..count` of it. + /// `writer`, where given, writing it over `threads` threads (where none is, an encoder of its + /// own before sets a state that may encode, so the GPU may write the records all the same), + /// then a pass over the target that executes commands `0..count` of it. fn encode( &self, device: &Device, @@ -242,6 +243,11 @@ impl Scene { (icb, threads, count): (&IndirectCommandBuffer, u64, u64), ) { let arguments = buffer_with(device, &icb.gpu_resource_id().0.to_le_bytes()).unwrap(); + // With no kernel, a state that may encode set in an encoder of its own lets the GPU write + // the records, as the kernel does. + if writer.is_none() { + encoding_state_set(command_buffer); + } let mut encoder = command_buffer.compute_command_encoder().unwrap(); encoder .use_resource(icb.as_resource(), ResourceUsage::WRITE) diff --git a/crates/metal2vulkan-validation/src/runtime_lane/render_resident_writes.rs b/crates/metal2vulkan-validation/src/runtime_lane/render_resident_writes.rs index f37509451..5dab4085d 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/render_resident_writes.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/render_resident_writes.rs @@ -1,7 +1,9 @@ //! What a draw's functions reach of the command buffer's resident resources, by the stages that //! write memory, on the device the cfg selects, under the validation layer. An indirect command //! buffer an earlier compute encoder made resident for writing is in the command buffer's -//! residency for writing, in every render stage (`runtime/residency.rs`); the host step draws its +//! residency for writing, in every render stage (`runtime/residency.rs`), and, with a state that +//! may encode set in another (`indirect_host_steps::encoding_state_set`), the GPU may write its +//! records (`IndirectCommandBuffer::gpu_written`); the host step draws its //! commands, whose vertex and fragment functions (the owned fixture `tob_renders`' `tob_triangle` //! and `tob_solid`: the triangle of the points at vertex buffer 0, an unsized buffer the host //! step's large-buffer form reaches through the resident root table, and the color at fragment @@ -11,7 +13,7 @@ //! what its fragments touch (nothing announced past the host step). use super::degrade_notices::{assert_made_once, on_device}; -use super::indirect_host_steps::HOST_STEPS; +use super::indirect_host_steps::{encoding_state_set, HOST_STEPS}; use super::render_executions::{forced_with, icb}; use super::{buffer_with, WAIT}; use crate::air_library::AirLibrary; @@ -103,6 +105,8 @@ fn run(device: &Device, state: &RenderPipelineState, commands: u64) -> (u64, Vec let before = device.work_counts().commands_recorded.cmd_pipeline_barrier; let queue = device.new_command_queue().unwrap(); let command_buffer = queue.command_buffer().unwrap(); + // A state that may encode, set in the command buffer, lets the GPU write the records. + encoding_state_set(&command_buffer); let mut encoder = command_buffer.compute_command_encoder().unwrap(); encoder .use_resource(buffer.as_resource(), ResourceUsage::WRITE) diff --git a/crates/metal2vulkan-validation/src/runtime_lane/topology_classes.rs b/crates/metal2vulkan-validation/src/runtime_lane/topology_classes.rs index c39160a99..42e6fea70 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/topology_classes.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/topology_classes.rs @@ -18,6 +18,7 @@ //! runtime records it ([`UNSIZED_POINTS`]). use super::degrade_notices::counts; +use super::indirect_host_steps::encoding_state_set; use super::render_state::{texels, texture}; use super::{ buffer_with, layer_reported_nothing_at_all, read_buffer, validated_device_with, Messages, @@ -106,7 +107,7 @@ impl Lane { fn pass( &self, messages: &Messages, - state: &RenderPipelineState, + (state, encoding): (&RenderPipelineState, bool), encode: impl FnOnce(&mut RenderCommandEncoder, &Buffer), ) -> (Left, [usize; 3]) { let announced = || { @@ -124,6 +125,9 @@ impl Lane { target.clear_color = ClearColor::new(0.2, 0.4, 0.6, 1.0); target.store_action = StoreAction::Store; let command_buffer = self.queue.command_buffer().unwrap(); + if encoding { + encoding_state_set(&command_buffer); + } let mut e = command_buffer.render_command_encoder(&pass).unwrap(); e.set_render_pipeline_state(state).unwrap(); e.set_fragment_buffer(Some(&counter), 0, 0).unwrap(); @@ -218,8 +222,10 @@ fn a_draw_outside_the_pipeline_s_class_draws_as_plain_metal() { /// `unsized-points`, asked for at every occurrence, is announced where each draw of points the /// pipeline sizes none of is decided, and each draws nothing, its vertex function run for every /// point: a direct draw at its call; an execution of an indirect command buffer's records the -/// CPU wrote in `commit`, before it returns; an execution of a buffer an encoder made resident -/// for writing (`gpu_written`) in its host step, after the commit. A draw of no vertices, a draw +/// CPU wrote in `commit`, before it returns; an execution of a buffer the GPU may write +/// (`gpu_written`: made resident for writing where a state that may encode is set) in its host +/// step, after the commit, and one made resident for writing with no such state in `commit`. A +/// draw of no vertices, a draw /// under the point class, and an execution of either, announce nothing. #[test] fn unsized_points_are_announced_at_the_draw_the_commit_and_the_host_step() { @@ -242,7 +248,7 @@ fn unsized_points_are_announced_at_the_draw_the_commit_and_the_host_step() { (Class::Point, 3, 0), ] { let state = lane.pipeline(class); - let (left, notices) = lane.pass(&messages, &state, |e, _| { + let (left, notices) = lane.pass(&messages, (&state, false), |e, _| { e.draw_primitives(point, 0, count, 1, 0).unwrap(); }); assert_eq!(notices, [announced, 0, 0], "{class:?} {count} on {name}"); @@ -266,16 +272,22 @@ fn unsized_points_are_announced_at_the_draw_the_commit_and_the_host_step() { .unwrap(); icb }; - for (class, count, gpu_written, announced) in [ - (Class::Triangle, 3, false, [0, 1, 0]), - (Class::Triangle, 0, false, [0, 0, 0]), - (Class::Point, 3, false, [0, 0, 0]), - (Class::Triangle, 3, true, [0, 0, 1]), + // An execution of a buffer made resident for writing, where a state that may encode is set in + // the command buffer (an encoder of its own before the pass), is recorded by the host step; + // without one, the GPU cannot write the records, and the commit records it, as one of a + // buffer made resident for nothing. + for (class, count, (writable, encoding), announced) in [ + (Class::Triangle, 3, (false, false), [0, 1, 0]), + (Class::Triangle, 0, (false, false), [0, 0, 0]), + (Class::Point, 3, (false, false), [0, 0, 0]), + (Class::Triangle, 3, (true, true), [0, 0, 1]), + (Class::Triangle, 3, (true, false), [0, 1, 0]), ] { + let gpu_written = writable && encoding; let state = lane.pipeline_with(class, true); let icb = commands(count); - let (left, mut notices) = lane.pass(&messages, &state, |e, _| { - if gpu_written { + let (left, mut notices) = lane.pass(&messages, (&state, encoding), |e, _| { + if writable { e.use_resource( icb.as_resource(), ResourceUsage::READ.union(ResourceUsage::WRITE), @@ -285,7 +297,9 @@ fn unsized_points_are_announced_at_the_draw_the_commit_and_the_host_step() { } e.execute_commands_in_buffer(&icb, 0..1).unwrap(); }); - let at = format!("an execution: {class:?} {count}, GPU-written {gpu_written}, on {name}"); + let at = format!( + "an execution: {class:?} {count}, writable {writable}, encoding {encoding}, on {name}" + ); assert_eq!(icb.gpu_written(), gpu_written, "{at}"); if gpu_written { // The host step runs on the steps' thread once the work before it completed,