kept_textured: the spun copies wait for the spin through a fence
Under METAL2VULKAN_LANE_VALIDATION=sync on lavapipe,
runtime_lane::kept_textured::a_kept_heap_draw_reads_what_an_earlier_command_buffer_wrote
failed on 1024 reports (512 copies, two cases): "vkCmdCopyBufferToImage(): READ_AFTER_WRITE
hazard detected. vkCmdCopyBufferToImage reads VkBuffer ..., which was previously written by
vkCmdDispatch", copy regions from offset 64, 64 bytes each.
spun_copies encodes the spin's dispatches, which store their count in the source buffer's
first word (bytes 0..4), and then copies the texels from bytes 64 on. Its premise ("so the
copies follow the spin") was Metal's whole-resource tracking of the buffer. The runtime orders
a tracked buffer by the bytes, as its README's "Hazards" says:
The runtime orders a tracked buffer by the bytes a command reaches: those its translation
proves a shader reads or writes, else from the bound offset to the buffer's end. Vulkan's
synchronization validation takes a shader's buffer as accessed over its whole descriptor,
which spans those bytes widened to 16-byte granules from the device's offset alignment, so
it reports hazards between commands whose bytes are apart, as it does between untracked
uses Metal leaves to the program; no order of either changes a result.
So on the runtime the copies did not follow the spin, and the test lost the delay it was built
on (the frame's kept draws waiting for an earlier command buffer's late writes). The layer
reported the copies' reads against the dispatch's whole descriptor.
The spin's encoder now updates a fence and the copies' blit encoder waits for it: an order
Metal keeps too (redundantly, beside its tracking), and one the runtime records as a barrier
from the spin's compute stages (fence.rs, the tracker). The copies follow the spin on both,
and the layer sees that barrier. Spin::encode takes the fence to update; Spin::new's timing
runs pass none.
Before: m3-max-32985 (msi, lavapipe, sync, at 3945a91289) failed the test with the reports
above. The proof run of this tip is in its landing report.
This commit is contained in:
1 parent
f67343b44e
commit
30331a477f
1 file changed
+20
-7
@@ -46,7 +46,7 @@ use metal2vulkan_devtools::llvm_assemble;
|
||||
use metal2vulkan_runtime::DeviceOptions;
|
||||
use metal2vulkan_runtime::{
|
||||
BlitOption, Buffer, CommandBuffer, CommandBufferStatus, CommandQueue, ComputePipelineState,
|
||||
Device, Origin, PipelineOptions, Size, Texture, WorkCounts,
|
||||
Device, Fence, Origin, PipelineOptions, Size, Texture, WorkCounts,
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -323,7 +323,7 @@ impl Spin {
|
||||
let loads = buffer_with(device, &[0; 4]).unwrap();
|
||||
let run = |spin: &Spin| {
|
||||
let command_buffer = queue.command_buffer().unwrap();
|
||||
spin.encode(&command_buffer, &loads, 1);
|
||||
spin.encode(&command_buffer, &loads, 1, None);
|
||||
let at = Instant::now();
|
||||
command_buffer.commit().unwrap();
|
||||
completed(&command_buffer);
|
||||
@@ -342,8 +342,14 @@ impl Spin {
|
||||
}
|
||||
|
||||
/// Encode, in an encoder of its own, `dispatches` dispatches of one thread spinning to the
|
||||
/// cap, each storing its count at the start of `loads`.
|
||||
fn encode(&self, command_buffer: &CommandBuffer, loads: &Buffer, dispatches: u32) {
|
||||
/// cap, each storing its count at the start of `loads`, and `then` updated after them.
|
||||
fn encode(
|
||||
&self,
|
||||
command_buffer: &CommandBuffer,
|
||||
loads: &Buffer,
|
||||
dispatches: u32,
|
||||
then: Option<&Fence>,
|
||||
) {
|
||||
let mut compute = command_buffer.compute_command_encoder().unwrap();
|
||||
compute.set_compute_pipeline_state(&self.state).unwrap();
|
||||
compute.set_buffer(Some(&self.word), 0, 0).unwrap();
|
||||
@@ -354,13 +360,18 @@ impl Spin {
|
||||
.dispatch_threadgroups(Size::new(1, 1, 1), Size::new(1, 1, 1))
|
||||
.unwrap();
|
||||
}
|
||||
if let Some(fence) = then {
|
||||
compute.update_fence(fence).unwrap();
|
||||
}
|
||||
compute.end_encoding().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Commit, on `queue`, a command buffer that spins and then copies every texel of `page`'s
|
||||
/// textures, of `shade`, from the buffer the spin wrote, so the copies follow the spin. It, not
|
||||
/// waited for.
|
||||
/// textures, of `shade`, from the buffer the spin wrote. It, not waited for. The copies wait for
|
||||
/// a fence the spin's encoder updates, so they follow the spin: the spin writes only the buffer's
|
||||
/// first word and the copies read past it, and the runtime orders a tracked buffer's commands by
|
||||
/// the bytes they share, here none, where Metal orders the whole buffer.
|
||||
fn spun_copies(
|
||||
device: &Device,
|
||||
queue: &CommandQueue,
|
||||
@@ -370,9 +381,11 @@ fn spun_copies(
|
||||
// The spin's count, then the texels, 64 bytes a texture.
|
||||
let bytes = [vec![0; 64], page.texels(shade)].concat();
|
||||
let source = buffer_with(device, &bytes).unwrap();
|
||||
let spun = device.new_fence();
|
||||
let command_buffer = queue.command_buffer().unwrap();
|
||||
spin.encode(&command_buffer, &source, DISPATCHES);
|
||||
spin.encode(&command_buffer, &source, DISPATCHES, Some(&spun));
|
||||
let mut blit = command_buffer.blit_command_encoder().unwrap();
|
||||
blit.wait_for_fence(&spun).unwrap();
|
||||
for (at, texture) in (0u64..).zip(page.textures()) {
|
||||
(blit.copy_from_buffer_to_texture(
|
||||
&source,
|
||||
|
||||
Reference in new issue
Block a user