Wait for a staging submission once per VkQueue, not in every batch while it runs
Every batch took a GPU wait on the staging queue's last submission for as long as the host had not seen it complete, so each batch committed during a copy carried its own semaphore wait: on an RTX 5080 Laptop GPU, about 3.3 batches per staging submission (1691 to 2097 waits for 539 to 573 staging submissions in 240 frames of 100 command buffers beside a thread uploading 7 MiB of Shared and Managed texels every 8 ms). A batch with a wait cannot join a group, and on drivers whose submissions stall on semaphore operations every extra wait is another chance of a stall. One wait per VkQueue is enough: a semaphore wait's second synchronization scope holds every command later in submission order on that queue, so the copies are visible to every later batch there. The scheduler already keeps, per VkQueue and per timeline, the value a batch there last waited for (State::waited, with the staging queue's place after the VkQueues and the internal submissions); the staging wait now reads and sets it. A held group goes to its VkQueue before any later batch there, and a failed submission puts the values back, so the mark never runs ahead of a made wait. Internal submissions keep their own wait. New lane runtime_lane::staging_batches::batches_after_one_that_waited_for_a_ staging_copy_follow_its_wait: one 64 MiB replace_region, then 32 command buffers on one queue that each blit a square of it; every square reads the call's bytes, and the staging waits are one, not one per batch. Verified on the RTX 5080 Laptop GPU (release, --cfg m2v_gpu, this change on top of a lane-only commit): the new lane passed 3 of 3 (gpu_waits 1, every square's bytes right); the same tree with command_buffer/submit.rs as before this change failed it 3 of 3 (32 waits, the bytes right); the staging_batches module passed 9 of 9. In the uploading frames, staging_gpu_waits fell to the staging submission count (548 to 558) and NVIDIA's over-1 ms submits on the graphics queue from 25 to 24 per 240 frames; frame times did not move (p50 6811 to 6828 us against 6839 to 6889 us, one frame in flight 6876 to 6934 against 6911 to 7006). Not run: clippy, rustdoc and the suites on this commit. (cherry picked from commit 516d31e3d47c3c238ec7e6666f5e03097e5b9ca4)
This commit is contained in:
1 parent
58aca59d68
commit
7647e46008
4 files changed
+127
-14
No files matched your search
@@ -51,12 +51,19 @@
|
||||
//! and the RTX 3090, about 2 ms of CPU in one `vkQueueSubmit` in about every thousand waits and
|
||||
//! signals) takes no such wait: each batch with work starts instead with a command buffer of
|
||||
//! barriers whose first is [`Barrier::after_completed`], which makes what completed batches
|
||||
//! wrote visible to every later command. Every batch, on every driver, waits for the staging
|
||||
//! queue's last submission while the host has not seen it complete (`Device::staging_wait`):
|
||||
//! one that carries the copies a burst of host calls queued is made just before the batch and
|
||||
//! waited for on the GPU, not the host (`texture/staging_copy.rs`).
|
||||
//! Where every batch waited, about twice as many of its submissions took that long as where
|
||||
//! batches only signal.
|
||||
//! wrote visible to every later command. Where every batch waited, about twice as many of its
|
||||
//! submissions took that long as where batches only signal.
|
||||
//!
|
||||
//! **The staging queue's copies.** On every driver, a batch waits on the GPU for the staging
|
||||
//! queue's last submission while the host has not seen it complete (`Device::staging_wait`),
|
||||
//! unless a batch of its `VkQueue` already waited for that submission: the wait's second
|
||||
//! synchronization scope holds every command later in submission order there, so one wait per
|
||||
//! `VkQueue` makes the copies visible to every later batch of it, as one wait per value does for
|
||||
//! completed work above. A submission that carries the copies a burst of host calls queued is
|
||||
//! made just before the batch and waited for on the GPU, not the host
|
||||
//! (`texture/staging_copy.rs`). The batches after the one that waits may then join a group
|
||||
//! (**Grouped**, above), which a batch with a wait for work the host has not seen complete only
|
||||
//! leads. Counted: `WorkCounts::staging_gpu_waits`.
|
||||
//!
|
||||
//! Each barrier goes in a command buffer of barriers alone, which the scheduler records once per
|
||||
//! list of barriers and submits again once the batch that last carried it completed
|
||||
@@ -830,9 +837,16 @@ impl CommandBufferShared {
|
||||
wait_values.push(value);
|
||||
wait_stages.push(vk::PipelineStageFlags::ALL_COMMANDS);
|
||||
}
|
||||
// And the staging queue's last submission, on every `VkQueue`, until the host saw it
|
||||
// complete: what it copied may be among what the work reads (`device/staging.rs`).
|
||||
if let Some((timeline, value)) = device.staging_wait() {
|
||||
// And the staging queue's last submission, until the host saw it complete: what it
|
||||
// copied may be among what the work reads (`device/staging.rs`). Where a batch of this
|
||||
// `VkQueue` already waited for it, every later command there follows that wait in
|
||||
// submission order, this batch's too (the module docs): one wait serves them all. The
|
||||
// staging queue's place among the timelines follows the `VkQueue`s' and the internal
|
||||
// submissions' (`Device::own_completed`).
|
||||
let staging_place = shared.queues.len() + 1;
|
||||
let staging_wait = (device.staging_wait())
|
||||
.filter(|&(_, value)| value > scheduler.waited[queue][staging_place]);
|
||||
if let Some((timeline, value)) = staging_wait {
|
||||
wait_semaphores.push(timeline);
|
||||
wait_values.push(value);
|
||||
wait_stages.push(vk::PipelineStageFlags::ALL_COMMANDS);
|
||||
@@ -1080,6 +1094,11 @@ impl CommandBufferShared {
|
||||
for &(source, value) in waited.iter() {
|
||||
scheduler.waited[queue][source] = value;
|
||||
}
|
||||
// Its staging wait, made or held: a held group goes to its `VkQueue` before any later
|
||||
// batch there, and one whose submission fails puts the values back.
|
||||
if let Some((_, value)) = staging_wait {
|
||||
scheduler.waited[queue][staging_place] = value;
|
||||
}
|
||||
shared.counts.completed_waited(waited.len());
|
||||
if !single && !started {
|
||||
shared.counts.bare_start();
|
||||
|
||||
@@ -22,8 +22,10 @@
|
||||
//! that needs what it did on the CPU (`get_bytes`, a texture's `VkImage` handed out, the
|
||||
//! program's own use of a `VkQueue`) waits for it on the host. Every other submission of the
|
||||
//! device's, a batch or an internal one on any `VkQueue`, waits for it on the GPU until the host
|
||||
//! saw it complete ([`Device::staging_wait`]): the copies it made are visible to the commands
|
||||
//! after that wait, which never waits for work of the program's. A submission is not waited for
|
||||
//! saw it complete ([`Device::staging_wait`]), but a batch of a `VkQueue` where an earlier
|
||||
//! batch waited for it (`command_buffer/submit.rs`): the copies it made are visible to the
|
||||
//! commands after that wait, in submission order there, which never waits for work of the
|
||||
//! program's. A submission is not waited for
|
||||
//! before the next one is recorded: its command buffer is one of up to [`BUFFERS`], each reused
|
||||
//! once the host saw the value it signalled reached.
|
||||
//! - **Who uses it.** A texture made to copy through it (`texture/staging_copy.rs`) has its first
|
||||
@@ -353,8 +355,8 @@ impl Device {
|
||||
}
|
||||
|
||||
/// The staging queue's last submission while the host has not seen it complete: what every
|
||||
/// other submission of the device's waits for on the GPU (the module docs), as what it copied
|
||||
/// may be among what it uses. The host sees it complete once a wait of no time returned for
|
||||
/// other submission of the device's waits for on the GPU, or follows on its `VkQueue` a wait
|
||||
/// for (the module docs), as what it copied may be among what it uses. The host sees it complete once a wait of no time returned for
|
||||
/// it.
|
||||
pub(crate) fn staging_wait(&self) -> Option<(vk::Semaphore, u64)> {
|
||||
let shared = self.shared();
|
||||
|
||||
@@ -4898,7 +4898,8 @@ pub struct WorkCounts {
|
||||
pub staging_flushes: u64,
|
||||
/// Batches and internal submissions that waited on the GPU for a staging submission the host
|
||||
/// had not seen complete (`Device::staging_wait`, `device/staging.rs`), in place of a wait on
|
||||
/// the host.
|
||||
/// the host: of the batches, at most one per `VkQueue` per staging submission, as every later
|
||||
/// batch there follows its wait (`command_buffer/submit.rs`).
|
||||
pub staging_gpu_waits: u64,
|
||||
/// New `Shared` and `Managed` textures' images with host-transfer usage placed in memory the
|
||||
/// host does not cache, where the device has none the image may take
|
||||
|
||||
@@ -211,6 +211,97 @@ fn a_burst_of_replace_regions_shares_one_staging_submission() {
|
||||
});
|
||||
}
|
||||
|
||||
/// One `replace_region` of a 64 MiB `Managed` texture, then `BATCHES` command buffers committed
|
||||
/// back to back on one queue, each a blit of a 64-texel square of it of its own into a buffer:
|
||||
/// every blit reads the call's bytes. Where the device copies on its staging queue, the first
|
||||
/// batch carried the copy and waited for it on the GPU, and the batches after it on the queue,
|
||||
/// committed while the copy may still run, took no wait of their own: they follow that wait in
|
||||
/// the `VkQueue`'s submission order, which orders them after the copy (`command_buffer/submit.rs`).
|
||||
/// So the staging waits are one per staging submission, not one per batch the host submitted
|
||||
/// before it saw the copy complete.
|
||||
#[test]
|
||||
fn batches_after_one_that_waited_for_a_staging_copy_follow_its_wait() {
|
||||
const SIDE: u64 = 4096;
|
||||
const SQUARE: u64 = 64;
|
||||
const BATCHES: u64 = 32;
|
||||
on_device(|device| {
|
||||
let name = device.name();
|
||||
let queue = device.new_command_queue().unwrap();
|
||||
let texture = texture(device, PixelFormat::RGBA8Unorm, SIDE, managed());
|
||||
let written = texels(SIDE, 3);
|
||||
let before = device.work_counts();
|
||||
let region = Region::new_2d(0, 0, SIDE, SIDE);
|
||||
(texture.replace_region(region, 0, 0, &written, 4 * SIDE, 0)).unwrap();
|
||||
let burst = Path::between(&before, &device.work_counts());
|
||||
let squares: Vec<(u64, u64)> = (0..BATCHES)
|
||||
.map(|at| (SQUARE * (at % 8), SQUARE * (at / 8)))
|
||||
.collect();
|
||||
let mut last = None;
|
||||
let outs: Vec<Buffer> = (squares.iter())
|
||||
.map(|&(x, y)| {
|
||||
let command_buffer = queue.command_buffer().unwrap();
|
||||
let out = device
|
||||
.new_buffer(4 * SQUARE * SQUARE, ResourceOptions::shared())
|
||||
.unwrap();
|
||||
let mut blit = command_buffer.blit_command_encoder().unwrap();
|
||||
blit.copy_from_texture_to_buffer(
|
||||
&texture,
|
||||
0,
|
||||
0,
|
||||
Origin::new(x, y, 0),
|
||||
Size::new(SQUARE, SQUARE, 1),
|
||||
&out,
|
||||
0,
|
||||
4 * SQUARE,
|
||||
0,
|
||||
BlitOption::None,
|
||||
)
|
||||
.unwrap();
|
||||
blit.end_encoding().unwrap();
|
||||
command_buffer.commit().unwrap();
|
||||
last = Some(command_buffer);
|
||||
out
|
||||
})
|
||||
.collect();
|
||||
let last = last.unwrap();
|
||||
last.wait_until_completed_for(WAIT).unwrap();
|
||||
assert_eq!(last.error(), None, "{name}: the blits");
|
||||
let read = Path::between(&before, &device.work_counts());
|
||||
for (at, (out, &(x, y))) in outs.iter().zip(&squares).enumerate() {
|
||||
let want: Vec<u8> = (0..SQUARE)
|
||||
.flat_map(|row| {
|
||||
let from = (4 * ((y + row) * SIDE + x)) as usize;
|
||||
written[from..from + (4 * SQUARE) as usize].iter().copied()
|
||||
})
|
||||
.collect();
|
||||
assert!(
|
||||
read_buffer(out, 0, want.len() as u64).unwrap() == want,
|
||||
"{name}: batch {at}'s blit reads the replace_region's bytes"
|
||||
);
|
||||
}
|
||||
println!(
|
||||
"STAGING-BATCH {name}: one replace_region of {} MiB {}, then {BATCHES} batches on one \
|
||||
queue that read it: the call {burst:?}, with the batches {read:?}",
|
||||
(4 * SIDE * SIDE) >> 20,
|
||||
burst.said(1)
|
||||
);
|
||||
if burst.queued > 0 {
|
||||
assert_eq!(
|
||||
(read.staging, read.flushes),
|
||||
(1, 1),
|
||||
"{name}: the first batch carried the copy in one staging submission"
|
||||
);
|
||||
assert!(
|
||||
(1..=1 + read.internal).contains(&read.gpu_waits),
|
||||
"{name}: one batch on the queue waited for the staging submission on the GPU, \
|
||||
and the batches after it none (internal submissions wait for it themselves): \
|
||||
{} waits",
|
||||
read.gpu_waits
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Overlapping writes into one texture keep their order, whether read by `get_bytes` or by a later
|
||||
/// command buffer, through the texture or a view of it: the whole level written twice, then a
|
||||
/// part of it, then a part through a view; the reads see the last whole write with the two parts
|
||||
|
||||
Reference in new issue
Block a user