Files
metal2vulkan/crates/metal2vulkan-runtime/src/tests/fill_queue.rs
T
Anees Iqbal 421799152d Fill queue: a batch's fills go there only where they write 1 MiB or more
The fill queue's own submission and the batch's wait for it cost small
copies more than the copy engine saves them. The sweep of
managed_copies_bench (a Managed buffer's copy in each frame, the fill queue
against the batch's start, p50) placed the crossover:

- RTX 2080 (18167): 128 KiB 1.04x alone and 1.11x beside another queue's
  kernels; 256 KiB 2.03x alone (0.94 against 0.46 ms), 1.09x beside;
  512 KiB 1.31x alone; from 1 MiB up 0.98-1.00x alone and 0.51-0.91x
  beside (1 MiB 1.81 against 3.57 ms).
- RTX 3090 (18199): faster or within 4% everywhere; 1 MiB alone 1.04x
  (reproduced, 17900), beside 0.51x (1.80 against 3.53 ms).
- RTX 5080 Laptop GPU (18108, before the sweep): 64 KiB 1.44x alone and
  1.06x beside; from 1 MiB up 0.93-0.99x alone and 0.56-0.96x beside.

So FILL_QUEUE_LEAST is 1 MiB, the smallest swept size from which the fill
queue is no slower beside the kernels on any of the three and at most 1.04
times as long alone: Taken::take_fills takes the batch's fills only where
they write that many bytes in all (copies and zeros), and fewer stay at the
batch's start, as on a device without a fill queue. Decided from bytes, not
from the device.

The fill-queue test gains a last command buffer whose fills (a new Managed
buffer's zeros and its copy in, under half the least each) make no
fill-queue submission and read back exactly. The ledger's costs of
matching Metal gain the fill queue's bullet; the staging, create, bench and
README docs say the least and the measured numbers (the create.rs 82% came
from the raw example and is gone).

Verified: cargo fmt; device runs, lavapipe and lint follow.
2026-11-11 19:08:58 -08:00

264 lines
11 KiB
Rust

//! The fill queue (`device/staging.rs`): on a device with a family of transfers alone that has a
//! second queue (NVIDIA's copy engine), a batch's new buffers' fills go there, in a submission of
//! their own that the batch waits for, after the batches before it on its `VkQueue`.
//!
//! - **Where.** The fill queue is of a family whose flags hold transfers and no graphics or
//! compute work, and each command buffer below that took fills of at least
//! `FILL_QUEUE_LEAST` bytes made one submission there (`WorkCounts::fill_queue_submissions`);
//! a device without one (lavapipe, RADV) made none. A last command buffer, whose fills (a new
//! `Managed` buffer's zeros and its copy in, each 4 bytes short of half that) write fewer, made
//! none: they ran at its start.
//! - **Bytes.** A `Managed` buffer's copy in after `did_modify_range`, a new `Private` buffer's
//! zeros and a `Private` buffer made with bytes read back exactly, through a blit into a
//! `Shared` buffer.
//! - **Order.** A first command buffer takes the `Managed` buffer's first copy in and completes:
//! until then the copy may read the host copy, which the CPU leaves alone, as on Metal. A second
//! one's blit reads the buffer behind a long run of kernels, submitted and still in flight as
//! the CPU writes the buffer again and a third command buffer of the same queue takes that copy
//! in: the second reads the first bytes, the third the new ones. Where the device has unified
//! memory (lavapipe), whose `Managed` buffer is one copy, the second completes before the CPU
//! writes again.
//!
//! Device tests: compiled only under the GPU safety cfgs (`m2v_gpu`, `m2v_cpu_icd`), on the kind
//! of device the cfg selects.
use super::blit_holds::{device, Scratch, WAIT};
use crate::{
Buffer, CommandBuffer, CommandBufferStatus, ComputePipelineState, CpuCacheMode,
HazardTrackingMode, PipelineOptions, ResourceOptions, Size, StorageMode,
};
use ash::vk;
use std::time::Duration;
/// The `Managed` buffer's bytes: 4 MiB and a word, so the copy in is no whole number of pages.
const BYTES: u64 = (4 << 20) + 4;
/// The kernels that keep the first command buffer in flight: dispatches over a `Private` buffer
/// of [`LOAD_BYTES`]; a CPU device runs a few over a small one.
const LOAD_DISPATCHES: usize = match cfg!(m2v_cpu_icd) {
true => 2,
false => 96,
};
const LOAD_BYTES: u64 = match cfg!(m2v_cpu_icd) {
true => 1 << 20,
false => 64 << 20,
};
/// The bytes word `index` of a pattern `seed` holds.
fn pattern(seed: u32, bytes: u64) -> Vec<u8> {
(0..bytes / 4)
.flat_map(|index| {
(index as u32)
.wrapping_mul(0x9e37_79b9)
.wrapping_add(seed)
.to_le_bytes()
})
.collect()
}
/// The bytes of `buffer`, a `Shared` one a completed command buffer wrote.
fn read(buffer: &Buffer) -> Vec<u8> {
let contents = buffer.contents().unwrap();
// SAFETY: `length` bytes of a `Shared` buffer no command buffer in flight writes.
unsafe { std::slice::from_raw_parts(contents.as_ptr(), buffer.length() as usize) }.to_vec()
}
/// A blit of `source`, whole, into `target` at its start.
fn copy(command_buffer: &CommandBuffer, source: &Buffer, target: &Buffer) {
let mut blit = command_buffer.blit_command_encoder().unwrap();
(blit.copy_from_buffer_to_buffer(source, 0, target, 0, source.length())).unwrap();
blit.end_encoding().unwrap();
}
/// [`LOAD_DISPATCHES`] dispatches of `scale_and_offset` over `load`, into `scratch`.
fn load(
command_buffer: &CommandBuffer,
state: &ComputePipelineState,
load: &Buffer,
scratch: &Buffer,
) {
let mut encoder = command_buffer.compute_command_encoder().unwrap();
encoder.set_compute_pipeline_state(state).unwrap();
encoder.set_buffer(Some(load), 0, 0).unwrap();
encoder.set_buffer(Some(scratch), 0, 1).unwrap();
encoder.set_bytes(&1.0f32.to_le_bytes(), 2).unwrap();
for _ in 0..LOAD_DISPATCHES {
(encoder.dispatch_threads(Size::new(LOAD_BYTES / 4, 1, 1), Size::new(256, 1, 1))).unwrap();
}
encoder.end_encoding().unwrap();
}
/// The module docs: where the fills run, their bytes, and their order behind a command buffer in
/// flight on the same queue.
#[test]
fn a_batch_s_fills_run_on_the_fill_queue_in_order_with_exact_bytes() {
let scratch = Scratch::new("fill-queue");
let device = device(&scratch.0);
let name = device.name().to_owned();
let place = device.fill_queue_family();
if let Some((family, flags)) = place {
let work = vk::QueueFlags::GRAPHICS | vk::QueueFlags::COMPUTE;
assert!(
flags.contains(vk::QueueFlags::TRANSFER) && !flags.intersects(work),
"{name}: the fill queue's family {family} has flags {:#x}",
flags.as_raw()
);
}
let state = {
let function = super::encoder_holds::library(&device, "examples/runtime_examples.ll")
.new_function_with_name("scale_and_offset")
.unwrap();
(device.new_compute_pipeline_state_with_function(&function, &PipelineOptions::default()))
.unwrap()
};
let queue = device.new_command_queue().unwrap();
let shared = |bytes| device.new_buffer(bytes, ResourceOptions::shared()).unwrap();
let (load_buffer, load_scratch) = (
device
.new_buffer(LOAD_BYTES, ResourceOptions::private())
.unwrap(),
device
.new_buffer(LOAD_BYTES, ResourceOptions::private())
.unwrap(),
);
let managed = device
.new_buffer(
BYTES,
ResourceOptions::new(
StorageMode::Managed,
CpuCacheMode::DefaultCache,
HazardTrackingMode::Default,
),
)
.unwrap();
let write = |bytes: &[u8]| {
let contents = managed.contents().unwrap();
// SAFETY: the buffer's host copy, `BYTES` long; no command buffer reads it (the GPU
// reads the device copy, which `did_modify_range` copies into).
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), contents.as_ptr(), bytes.len()) };
managed.did_modify_range(0..BYTES).unwrap();
};
let (first, second) = (pattern(0x1234_5678, BYTES), pattern(0x0bad_cafe, BYTES));
// The device's own checks' submissions are done, so the two command buffers below are what
// takes the fills.
assert!(
device.settle_in_place_checks(Duration::from_secs(120)),
"{name}"
);
assert!(
device.settle_queue_switch_check(Duration::from_secs(120)),
"{name}"
);
let before = device.work_counts();
// The first copy in, with the load buffers' zeros, by a command buffer of its own that
// completes before the CPU writes the buffer again (the module docs).
write(&first);
let (primed, first_read) = (shared(BYTES), shared(BYTES));
let priming = queue.command_buffer().unwrap();
copy(&priming, &managed, &primed);
priming.commit().unwrap();
priming.wait_until_completed_for(WAIT).unwrap();
assert_eq!(priming.status(), CommandBufferStatus::Completed, "{name}");
let behind = queue.command_buffer().unwrap();
load(&behind, &state, &load_buffer, &load_scratch);
copy(&behind, &managed, &first_read);
behind.commit().unwrap();
// Once it was submitted, the CPU writes the buffer again while it may still run, and a third
// command buffer of the queue takes that copy in, with a new buffer's zeros and another's
// bytes: the copy must wait for the blit before it. Where the device has unified memory the
// buffer is one copy, which the CPU's write would race, as on Metal: there the second command
// buffer completes first.
behind.wait_until_scheduled().unwrap();
if device.properties().has_unified_memory {
behind.wait_until_completed_for(WAIT).unwrap();
}
write(&second);
let staged = pattern(0x5eed_0001, 1 << 20);
let zeroed = device
.new_buffer(1 << 20, ResourceOptions::private())
.unwrap();
let with_bytes = (device.new_buffer_with_bytes(&staged, ResourceOptions::private())).unwrap();
let (second_read, zeros_read, bytes_read) = (shared(BYTES), shared(1 << 20), shared(1 << 20));
let after = queue.command_buffer().unwrap();
copy(&after, &managed, &second_read);
copy(&after, &zeroed, &zeros_read);
copy(&after, &with_bytes, &bytes_read);
after.commit().unwrap();
let still_running = behind.status() != CommandBufferStatus::Completed;
for command_buffer in [&behind, &after] {
command_buffer.wait_until_completed_for(WAIT).unwrap();
assert_eq!(
command_buffer.status(),
CommandBufferStatus::Completed,
"{name}"
);
}
let counts = device.work_counts();
let submissions = counts.fill_queue_submissions - before.fill_queue_submissions;
let shown = (place
.map(|(family, flags)| format!("family {family}, flags {:#x}", flags.as_raw())))
.unwrap_or_else(|| "none".to_owned());
eprintln!(
"FILL-QUEUE {name}: fill queue {shown}, submissions {submissions}, second command buffer \
still running as the third committed: {still_running}"
);
assert!(
read(&primed) == first,
"{name}: the first command buffer read other bytes"
);
assert!(
read(&first_read) == first,
"{name}: the second command buffer read other bytes than the first copy in"
);
assert!(
read(&second_read) == second,
"{name}: the third command buffer read other bytes"
);
assert!(
read(&zeros_read).iter().all(|&byte| byte == 0),
"{name}: a new buffer not zero"
);
assert!(
read(&bytes_read) == staged,
"{name}: a buffer made with bytes read other bytes"
);
// One submission a command buffer that took fills (the first and the third); none without
// a fill queue.
let expected = match place {
Some(_) => 2,
None => 0,
};
assert_eq!(submissions, expected, "{name}");
// Fills of fewer bytes than the fill queue takes stay at the start of their batch: a new
// `Managed` buffer's zeros and its copy in, which may go in one batch.
let few = crate::device::FILL_QUEUE_LEAST / 2 - 4;
let small = device
.new_buffer(
few,
ResourceOptions::new(
StorageMode::Managed,
CpuCacheMode::DefaultCache,
HazardTrackingMode::Default,
),
)
.unwrap();
let third = pattern(0x7a11_0003, few);
let contents = small.contents().unwrap();
// SAFETY: the buffer's host copy, `few` bytes long; no command buffer reads it.
unsafe { std::ptr::copy_nonoverlapping(third.as_ptr(), contents.as_ptr(), third.len()) };
small.did_modify_range(0..few).unwrap();
let small_read = shared(few);
let last = queue.command_buffer().unwrap();
copy(&last, &small, &small_read);
last.commit().unwrap();
last.wait_until_completed_for(WAIT).unwrap();
assert_eq!(last.status(), CommandBufferStatus::Completed, "{name}");
assert!(
read(&small_read) == third,
"{name}: the last command buffer read other bytes"
);
let after_small = device.work_counts().fill_queue_submissions - counts.fill_queue_submissions;
eprintln!("FILL-QUEUE {name}: {few} bytes copied in, fill queue submissions {after_small}");
assert_eq!(after_small, 0, "{name}: {few} bytes went to the fill queue");
}