Fill queue: a batch takes and waits for the fills of memory its commands may reach alone
On a device with a fill queue (NVIDIA's copy engine), a batch took every fill waiting, whichever buffers its commands name, and every submission of the device's waited on the GPU for the fill queue's last submission until the host saw it complete. A fill-queue submission waits for the batches before its batch on that batch's VkQueue, so a compositor's frame that named only old buffers waited for an app's new buffer's zeros behind the app's kernels, or made them itself and waited for its own queue's last batch first. What, on such a device: - A batch takes the fills of memory its commands may reach (FillTake::Reached): memory a resource its encoders' uses name owns (used_resources::owner_of over Owners and HeapKeys: the buffer, or the heap it lies in); every fill where a command of it may reach memory no use names (State::reaches_unnamed, State::parts_reach) or where it takes a move of an image in memory another resource owns or a staged copy (Taken::every_fill), which come after every fill of that memory; and the fills of the device's own buffers, which no use names. The rest stay waiting, in their order, for a batch that may reach them or an internal submission, which takes every one. A fill's key is noted as it is registered (Filled::key), and its owner as it is taken, so a buffer's fills share their turn. - A batch waits on the GPU for the last fill-queue submission the host may not have seen complete whose fills it may reach (Device::fill_queue_wait_for), where its VkQueue did not already. Internal submissions still wait for the fill queue's last. - A command buffer that needs no GPU work goes with no submission where nothing waits but fills of the program's memory (FirstMoves::none_waiting_but). Other devices take every fill, as before. Tests: first_move::a_batch_takes_the_fills_of_memory_it_reaches (pure); fill_waits (device) holds fill_queue_waits_unused at 0, where the commit before counts the compositor's waits. Docs: device/staging.rs and texture/first_move.rs module docs, the runtime README, FAST-PATHS' fill-queue row. Verified: cargo fmt. Builds, device runs and numbers follow as potluck jobs.
This commit is contained in:
1 parent
456177d8fe
commit
781bd94935
9 files changed
+349
-69
No files matched your search
@@ -369,7 +369,9 @@ after it, and 0 of 80 with the device dropped first
|
||||
(NVIDIA's copy engine), the copies in after `did_modify_range`, and new buffers' zeros and
|
||||
staged bytes, run on that queue before the command buffer that takes them, rather than on the
|
||||
queue its kernels run on, where they come to 1 MiB or more
|
||||
(`WorkCounts::fill_queue_submissions`).
|
||||
(`WorkCounts::fill_queue_submissions`). There a command buffer takes and waits for only the
|
||||
fills of memory its commands may reach (`WorkCounts::fill_queue_waits_unused` counts those
|
||||
that waited for fills they reach none of).
|
||||
- **Past device-local memory.** Where no device-local memory type a `Private` buffer or heap, a
|
||||
texture's image or (without unified memory) a `Managed` buffer's own bytes takes has room, it
|
||||
lies in system memory, as Metal makes such resources past a discrete GPU's memory rather than refusing
|
||||
|
||||
@@ -2275,15 +2275,13 @@ impl Filled for BufferShared {
|
||||
.map(|memory| Box::new(memory) as Box<dyn Any + Send + Sync>)
|
||||
}
|
||||
|
||||
/// A buffer placed in a heap: the heap. Else the buffer, kept among the program's resources
|
||||
/// by its key (`Device::visible`), or the heap whose own buffer it is (`HeapKeys`).
|
||||
fn owner(&self) -> Option<ObjectId> {
|
||||
let key = match &self.bytes {
|
||||
/// Its identity's key (`Buffer::identity`): a buffer placed in a heap has the heap's own
|
||||
/// buffer's.
|
||||
fn key(&self) -> Option<ResourceKey> {
|
||||
Some(match &self.bytes {
|
||||
Bytes::Own(own) => own.identity.key,
|
||||
Bytes::Heap(placed) => return Some(placed.heap.object_id()),
|
||||
};
|
||||
let shared = self.device.shared();
|
||||
(shared.owners.id_of(key)).or_else(|| shared.heap_keys.id_of(key))
|
||||
Bytes::Heap(placed) => placed.bytes.identity().key,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -222,6 +222,7 @@ use crate::shadow_standing::{LetGo, Missed, Unskipped};
|
||||
use crate::sparse::bind::{bind_batch, BindFailure, Bound};
|
||||
use crate::sparse::encoder::SparseUpdate;
|
||||
use crate::texture::decoded_twin::{SecondDecode, TwinWrite};
|
||||
use crate::texture::first_move::FillTake;
|
||||
use crate::texture::wrapped::Transition;
|
||||
use crate::texture::Texture;
|
||||
use crate::tracker::{Barrier, Point, Summary};
|
||||
@@ -485,11 +486,16 @@ impl CommandBufferShared {
|
||||
let rebound = state.failing_at(device, index);
|
||||
// One that needs no GPU work goes with no submission, in its queue's order (the module
|
||||
// docs); behind a group held, it goes with the group as any batch that joins does, and
|
||||
// where the device's first work waits, it carries it as any batch does.
|
||||
// where the device's first work waits, it carries it as any batch does: on a device with
|
||||
// a fill queue, first work but the fills of the program's memory, which a batch whose
|
||||
// commands name nothing leaves waiting (`texture/first_move.rs`, `FillTake::Reached`).
|
||||
let fills_named_only = |key: ResourceKey| {
|
||||
device.has_fill_queue() && crate::used_resources::owner_of(shared, key).is_some()
|
||||
};
|
||||
if !rebound
|
||||
&& !scheduler.group.holds()
|
||||
&& state.needs_no_gpu()
|
||||
&& shared.textures.first_moves.none_waiting()
|
||||
&& (shared.textures.first_moves).none_waiting_but(fills_named_only)
|
||||
{
|
||||
let taken = self.take_unsubmitted(&mut state, index, last, scheduler);
|
||||
return Ok(Submitting::Submitted(taken));
|
||||
@@ -654,7 +660,21 @@ impl CommandBufferShared {
|
||||
// pages it then holds. Its commands bind the heap sets, so it holds the texture slots'
|
||||
// writes until its submission was made (`heap_sets::hold`).
|
||||
let firsts = &shared.textures.first_moves;
|
||||
let mut first_work = firsts.take_holding(|_| true, || crate::heap_sets::hold(device));
|
||||
// On a device with a fill queue it takes the fills of memory its commands may reach
|
||||
// alone, and of memory no use of the program's names (`FillTake::Reached`): the rest wait
|
||||
// for a batch that may reach them, so this one neither makes them nor waits for them.
|
||||
let owner_of = |key: ResourceKey| crate::used_resources::owner_of(shared, key);
|
||||
let reaches = |owner: ObjectId| state.parts_reach(shared, start..end, &[owner]);
|
||||
let fill_take = match device.has_fill_queue() {
|
||||
true => FillTake::Reached {
|
||||
owner_of: &owner_of,
|
||||
reaches: &reaches,
|
||||
},
|
||||
false => FillTake::All,
|
||||
};
|
||||
let hold = || crate::heap_sets::hold(device);
|
||||
let mut first_work = firsts.take_holding(|_| true, fill_take, hold);
|
||||
let every_fill = first_work.every_fill();
|
||||
// Its buffers' fills go to the device's fill queue where it has one with a command buffer
|
||||
// free, in a submission of their own that the batch waits for (below,
|
||||
// `device/staging.rs`), where they write at least `FILL_QUEUE_LEAST` bytes in all: not
|
||||
@@ -934,11 +954,17 @@ impl CommandBufferShared {
|
||||
wait_stages.push(vk::PipelineStageFlags::ALL_COMMANDS);
|
||||
shared.counts.staging_gpu_wait();
|
||||
}
|
||||
// And the fill queue's last submission, as the staging queue's, its place after the
|
||||
// staging queue's (`device/staging.rs`): what it filled may be among what the work reads.
|
||||
// And the last of the fill queue's submissions that the host may not have seen complete
|
||||
// and whose fills its commands may reach, as the staging queue's, its place after the
|
||||
// staging queue's (`device/staging.rs`): what it filled may be among what the work
|
||||
// reads. Each of them where a command may reach memory no use names, or where it took
|
||||
// every fill waiting; else those of memory its encoders' uses name.
|
||||
let fill_place = shared.queues.len() + 2;
|
||||
let fill_wait = (device.fill_queue_wait())
|
||||
.filter(|&(_, value)| value > scheduler.waited[queue][fill_place]);
|
||||
let fill_reaches = |owners: Option<&[ObjectId]>| {
|
||||
every_fill || owners.is_none_or(|owners| state.parts_reach(shared, start..end, owners))
|
||||
};
|
||||
let fill_wait =
|
||||
device.fill_queue_wait_for(scheduler.waited[queue][fill_place], fill_reaches);
|
||||
if let Some((timeline, value)) = fill_wait {
|
||||
wait_semaphores.push(timeline);
|
||||
wait_values.push(value);
|
||||
@@ -969,13 +995,13 @@ impl CommandBufferShared {
|
||||
// counted (`WorkCounts::fill_queue_waits`): its own, and those of the others' it waits
|
||||
// for that the host may not have seen complete.
|
||||
if fill_wait.is_some() || filled.is_some() {
|
||||
let reaches = |owners: Option<&[ObjectId]>| {
|
||||
owners.is_none_or(|owners| state.parts_reach(shared, start..end, owners))
|
||||
};
|
||||
let own = (filled.as_ref()).is_some_and(|filled| reaches(filled.owners.as_deref()));
|
||||
let own =
|
||||
(filled.as_ref()).is_some_and(|filled| fill_reaches(filled.owners.as_deref()));
|
||||
let after = scheduler.waited[queue][fill_place];
|
||||
let others = fill_wait.is_some_and(|(_, value)| {
|
||||
device.fill_queue_reached((after, value), reaches).is_some()
|
||||
device
|
||||
.fill_queue_reached((after, value), fill_reaches)
|
||||
.is_some()
|
||||
});
|
||||
shared.counts.fill_queue_waited(own || others);
|
||||
}
|
||||
|
||||
@@ -93,10 +93,22 @@
|
||||
//! took an acquire from an external agent keeps its fills at its start; so does one that finds
|
||||
//! every command buffer of the fill queue's pending ([`Device::fill_queue_has_room`]), as a wait
|
||||
//! on the host for one may wait for work that waits for the caller.
|
||||
//! - **What waits for it.** The batch, before any of its commands; and every other submission of
|
||||
//! the device's, a batch or an internal one on any `VkQueue`, on the GPU until the host saw it
|
||||
//! complete, as for the staging queue ([`Device::fill_queue_wait`]): any of their commands may
|
||||
//! reach the buffers unnamed. The fills' writes are available once the submission's signal is
|
||||
//! - **Which fills a batch takes.** Those of memory its commands may reach: memory a resource its
|
||||
//! encoders' uses name owns (`used_resources::owner_of`: the buffer, or the heap it lies in);
|
||||
//! every one where a command of it may reach memory no use names (a pointer loaded from memory,
|
||||
//! a texture's handle, work recorded on the GPU alone: `State::reaches_unnamed`), or where it
|
||||
//! takes a move of an image in memory another resource owns or a staged copy, which come after
|
||||
//! every fill of that memory; and those of the device's own buffers, which no use names
|
||||
//! (`texture/first_move.rs`, `FillTake::Reached`). The rest wait for a batch that may reach
|
||||
//! them, or for an internal submission, which takes every one. A batch over no new memory makes
|
||||
//! no fills and waits for none.
|
||||
//! - **What waits for it.** The batch, before any of its commands; and every later batch whose
|
||||
//! commands may reach what it filled, as above, on the GPU until the host saw it complete
|
||||
//! ([`Device::fill_queue_wait_for`]); and every internal submission, on any `VkQueue`
|
||||
//! ([`Device::fill_queue_wait`]). Each submission keeps what owns the memory its fills write
|
||||
//! (`Fills::owners`) until the host saw it complete. A batch that waits for none of them waits
|
||||
//! for no fill-queue work (`WorkCounts::fill_queue_waits_unused` counts the batches that waited
|
||||
//! for fills they reach none of). The fills' writes are available once the submission's signal is
|
||||
//! ("The first access scope includes all memory access performed by the device", the Vulkan
|
||||
//! specification, 7.4.1 Semaphore Signaling) and visible to the commands after the wait. Every
|
||||
//! buffer is concurrent among the device's families and this one (`sharing.rs`), so none
|
||||
@@ -493,6 +505,11 @@ impl Device {
|
||||
self.shared().staging.is_some()
|
||||
}
|
||||
|
||||
/// Whether the device has a fill queue (the module docs).
|
||||
pub(crate) fn has_fill_queue(&self) -> bool {
|
||||
self.shared().fill_queue.is_some()
|
||||
}
|
||||
|
||||
/// Whether the device has a fill queue (the module docs) with a command buffer a submission
|
||||
/// may record into now, with no wait on the host: one whose last submission the host saw
|
||||
/// complete, or room for another of [`BUFFERS`]. A submission there may wait for work that
|
||||
@@ -564,11 +581,32 @@ impl Device {
|
||||
}
|
||||
|
||||
/// The fill queue's last submission while the host has not seen it complete, as
|
||||
/// [`staging_wait`](Self::staging_wait)'s: what it filled may be among what a submission uses.
|
||||
/// [`staging_wait`](Self::staging_wait)'s: what an internal submission waits for, as what it
|
||||
/// filled may be among what the submission uses (a batch waits for those it may reach alone,
|
||||
/// [`fill_queue_wait_for`](Self::fill_queue_wait_for)).
|
||||
pub(crate) fn fill_queue_wait(&self) -> Option<(vk::Semaphore, u64)> {
|
||||
pending(self, self.shared().fill_queue.as_ref()?)
|
||||
}
|
||||
|
||||
/// The last of the fill queue's submissions after `after` that the host may not have seen
|
||||
/// complete and whose fills a batch's commands may reach, as `reaches` answers
|
||||
/// ([`fill_queue_reached`](Self::fill_queue_reached)): what the batch waits for on the GPU
|
||||
/// (the module docs). One the host sees complete with a wait of no time is none.
|
||||
pub(crate) fn fill_queue_wait_for(
|
||||
&self,
|
||||
after: u64,
|
||||
reaches: impl Fn(Option<&[ObjectId]>) -> bool,
|
||||
) -> Option<(vk::Semaphore, u64)> {
|
||||
let fill_queue = self.shared().fill_queue.as_ref()?;
|
||||
let value = self.fill_queue_reached((after, u64::MAX), reaches)?;
|
||||
let timeline = *fill_queue.timeline.get()?;
|
||||
// A failed wait (a lost device) leaves the submission waited for.
|
||||
match reached(self, fill_queue, value, 0) {
|
||||
Ok(true) => None,
|
||||
_ => Some((timeline, value)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The value of the last of the fill queue's submissions after `after` through `upto` that
|
||||
/// the host may not have seen complete and whose fills a batch's commands may reach, as
|
||||
/// `reaches` answers for what owns the memory each one's fills write (`None`: memory any
|
||||
|
||||
@@ -91,7 +91,7 @@ fn a_hand_out_takes_its_own_first_work_alone() {
|
||||
assert!(firsts.fills_wait(other));
|
||||
// The device's next submission takes the rest.
|
||||
assert_eq!(firsts.take(|_| true).submitted(None).len(), 2);
|
||||
assert!(firsts.none_waiting());
|
||||
assert!(firsts.none_waiting_but(|_| false));
|
||||
}
|
||||
|
||||
/// A resource's staged work goes as the resource goes (`FirstMoves::forget`), not at the device's
|
||||
@@ -144,7 +144,7 @@ fn a_resource_s_staged_work_goes_with_it() {
|
||||
// The device's next submission takes the rest, and holds it until it completed.
|
||||
let held = firsts.take(|_| true).submitted(None);
|
||||
assert_eq!(held.len(), 2);
|
||||
assert!(firsts.none_waiting());
|
||||
assert!(firsts.none_waiting_but(|_| false));
|
||||
drop(held);
|
||||
assert_eq!(Arc::strong_count(&device), 1);
|
||||
}
|
||||
@@ -1576,6 +1576,11 @@ impl Fresh for Image {
|
||||
.is_some_and(|first| first.rebindable())
|
||||
.then_some(self.identity.key)
|
||||
}
|
||||
|
||||
/// An image in a buffer's memory, or an automatic or placement heap's.
|
||||
fn in_other_memory(&self) -> bool {
|
||||
self.owner.memory().is_some()
|
||||
}
|
||||
}
|
||||
|
||||
/// Who owns an image and its memory.
|
||||
|
||||
@@ -56,7 +56,10 @@
|
||||
//! at least `FILL_QUEUE_LEAST` bytes in all go there instead ([`Taken::take_fills`]), in a
|
||||
//! submission of their own after the batches before it on its `VkQueue` and what it waits for,
|
||||
//! which the batch waits for before any of its commands; its moves and staged copies stay at
|
||||
//! its start, after them.
|
||||
//! its start, after them. There a batch takes only the fills of memory its commands may reach,
|
||||
//! of the device's own buffers, and every fill where it takes a move of an image in memory
|
||||
//! another resource owns, or a staged copy ([`FillTake::Reached`]); the rest wait for a batch
|
||||
//! that may reach them (`device/staging.rs`).
|
||||
//! - **Staged copies**. `replace_region` of a texture whose `VkImage` is its own for good (in
|
||||
//! memory of its own, or a heap's own pages) and takes no host or staging-queue copies (those
|
||||
//! are queued for the staging queue instead, whose submission of them every submission of the
|
||||
@@ -88,7 +91,9 @@
|
||||
//! in `GENERAL` and each fill done. On a device of several `VkQueue`s a submission on another
|
||||
//! one waits for the batch that did it until that batch completed ([`FirstMoves::waits`]): any
|
||||
//! of its commands may reach the resource unnamed. Fills made on the fill queue are waited for
|
||||
//! by every later submission until the host saw them complete (`Device::fill_queue_wait`).
|
||||
//! by every later batch whose commands may reach what they filled, and every internal
|
||||
//! submission, until the host saw them complete (`Device::fill_queue_wait_for`,
|
||||
//! `Device::fill_queue_wait`).
|
||||
//! - **Order**. Each submission holds its place in the order from its take until its submission was
|
||||
//! made, whether or not it took anything: so no submission is made between another's take and its
|
||||
//! submission (one that found nothing to take would otherwise run before the moves another took,
|
||||
@@ -658,12 +663,21 @@ pub(crate) struct FirstFill {
|
||||
/// external memory; target), recorded in the barrier before every fill, in place of copies
|
||||
/// (none then).
|
||||
acquire: Option<(u32, u32)>,
|
||||
/// What owns the memory it writes ([`Filled::owner`]), as the submission that took it last
|
||||
/// found it.
|
||||
/// The key the program's uses of the memory it writes name ([`Filled::key`]), as it was
|
||||
/// registered.
|
||||
key: Option<ResourceKey>,
|
||||
/// What owns that memory (`used_resources::owner_of`), as the batch that took it last found
|
||||
/// it ([`FillTake::Reached`]); `None` where none did, or none owns it.
|
||||
owner: Option<ObjectId>,
|
||||
}
|
||||
|
||||
impl FirstFill {
|
||||
/// The fill, with the key of `keeper`'s memory ([`Filled::key`]).
|
||||
fn keyed(mut self, keeper: &Weak<dyn Filled>) -> FirstFill {
|
||||
self.key = keeper.upgrade().and_then(|filled| filled.key());
|
||||
self
|
||||
}
|
||||
|
||||
/// The fill of `length` bytes of `buffer` from byte `start`, from `zeros`, the device's.
|
||||
fn new(buffer: vk::Buffer, start: u64, length: u64, zeros: vk::Buffer) -> FirstFill {
|
||||
let copies = (0..length.div_ceil(ZERO_BYTES))
|
||||
@@ -683,6 +697,7 @@ impl FirstFill {
|
||||
filled: None,
|
||||
staging: None,
|
||||
acquire: None,
|
||||
key: None,
|
||||
owner: None,
|
||||
}
|
||||
}
|
||||
@@ -710,6 +725,7 @@ impl FirstFill {
|
||||
filled: Some(range),
|
||||
staging: None,
|
||||
acquire: None,
|
||||
key: None,
|
||||
owner: None,
|
||||
});
|
||||
}
|
||||
@@ -742,6 +758,7 @@ impl FirstFill {
|
||||
filled: None,
|
||||
staging: Some(staging),
|
||||
acquire: None,
|
||||
key: None,
|
||||
owner: None,
|
||||
}
|
||||
}
|
||||
@@ -757,6 +774,7 @@ impl FirstFill {
|
||||
filled: None,
|
||||
staging: None,
|
||||
acquire: Some((source, target)),
|
||||
key: None,
|
||||
owner: None,
|
||||
}
|
||||
}
|
||||
@@ -841,6 +859,12 @@ pub(crate) trait Fresh: Send + Sync {
|
||||
/// host pages (the module docs): an internal submission takes that only where it names the
|
||||
/// key. `None` for every other.
|
||||
fn rebindable(&self) -> Option<ResourceKey>;
|
||||
/// Whether its image lies in memory another resource owns (a buffer's, a heap's), which that
|
||||
/// resource's fills write: a batch that takes its move takes every fill waiting (the module
|
||||
/// docs).
|
||||
fn in_other_memory(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// A new buffer whose fill waits: what a submission that takes the fill holds until it completes
|
||||
@@ -851,11 +875,10 @@ pub(crate) trait Filled: Send + Sync {
|
||||
/// none.
|
||||
fn memory(&self) -> Option<Box<dyn Any + Send + Sync>>;
|
||||
|
||||
/// The identity of what owns the memory the fill writes as the program's uses name it
|
||||
/// (`used_resources.rs`): a buffer of the program's, or the heap whose bytes it is. `None`
|
||||
/// for memory no use of the program's names (the runtime's own buffers, which its own
|
||||
/// bindings reach), or one not yet kept among the program's resources.
|
||||
fn owner(&self) -> Option<ObjectId> {
|
||||
/// The hazard-tracking key the program's uses of the memory the fill writes name: the
|
||||
/// buffer's, or for a buffer placed in a heap the heap's own buffer's. `None` for a buffer
|
||||
/// of the device's own (a neutral), which no use of the program's names.
|
||||
fn key(&self) -> Option<ResourceKey> {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -870,6 +893,31 @@ enum Waiting {
|
||||
Texels(Weak<dyn Any + Send + Sync>, TexelCopy),
|
||||
}
|
||||
|
||||
/// The fills waiting that a submission takes ([`FirstMoves::take_holding`]).
|
||||
pub(crate) enum FillTake<'r> {
|
||||
/// Every one: an internal submission's, a handle's, and a batch's on a device without a fill
|
||||
/// queue.
|
||||
All,
|
||||
/// A batch's on a device with a fill queue (the module docs): the fills of memory its commands
|
||||
/// may reach, as `reaches` answers for what owns it (`owner_of` naming the owner under a
|
||||
/// key, `used_resources::owner_of`); every fill of memory no use of the program's names; and
|
||||
/// every fill where it takes a move of an image in memory another resource owns, or a staged
|
||||
/// copy ([`Taken::every_fill`]).
|
||||
Reached {
|
||||
owner_of: &'r dyn Fn(ResourceKey) -> Option<ObjectId>,
|
||||
reaches: &'r dyn Fn(ObjectId) -> bool,
|
||||
},
|
||||
}
|
||||
|
||||
/// An entry of a take, in its order ([`FirstMoves::take_with`]).
|
||||
enum Sorted {
|
||||
Kept(Waiting),
|
||||
Taken(Held),
|
||||
/// A fill whose turn waits until every move taken is known, and its buffer, held until the
|
||||
/// lock is free.
|
||||
Fill(Weak<dyn Filled>, FirstFill, Arc<dyn Filled>),
|
||||
}
|
||||
|
||||
/// A resource whose first work a submission took, held.
|
||||
enum Held {
|
||||
Moves(Arc<dyn Fresh>, Vec<FirstMove>),
|
||||
@@ -1060,7 +1108,7 @@ impl FirstMoves {
|
||||
buffer: vk::Buffer,
|
||||
range: std::ops::Range<u64>,
|
||||
) -> Result<(), Error> {
|
||||
let fill = FirstFill::zeros(device, self, buffer, range)?;
|
||||
let fill = FirstFill::zeros(device, self, buffer, range)?.keyed(&keeper);
|
||||
let _order = self.registration_place();
|
||||
lock(&self.waiting).push(Waiting::Fill(keeper, fill));
|
||||
Ok(())
|
||||
@@ -1078,7 +1126,7 @@ impl FirstMoves {
|
||||
buffer: vk::Buffer,
|
||||
families: (u32, u32),
|
||||
) {
|
||||
let fill = FirstFill::acquired(buffer, families);
|
||||
let fill = FirstFill::acquired(buffer, families).keyed(&keeper);
|
||||
let _order = self.registration_place();
|
||||
lock(&self.waiting).push(Waiting::Fill(keeper, fill));
|
||||
}
|
||||
@@ -1096,7 +1144,7 @@ impl FirstMoves {
|
||||
length: u64,
|
||||
staging: Box<dyn Any + Send + Sync>,
|
||||
) -> bool {
|
||||
let fill = FirstFill::bytes(buffer, start, source, length, staging);
|
||||
let fill = FirstFill::bytes(buffer, start, source, length, staging).keyed(&keeper);
|
||||
let _order = self.registration_place();
|
||||
let mut waiting = lock(&self.waiting);
|
||||
waiting.push(Waiting::Fill(keeper, fill));
|
||||
@@ -1124,7 +1172,7 @@ impl FirstMoves {
|
||||
/// The submission's place in the order is held until it was made or the work given back
|
||||
/// ([`Taken`]): every submission takes, whether or not anything waits.
|
||||
pub(crate) fn take(&self, named: impl Fn(ResourceKey) -> bool) -> Taken<'_> {
|
||||
self.take_with(named, || None, |_| true)
|
||||
self.take_with(named, FillTake::All, || None, |_| true)
|
||||
}
|
||||
|
||||
/// As [`take`](Self::take), for a handle handed out (`Buffer::vk_buffer`,
|
||||
@@ -1132,19 +1180,21 @@ impl FirstMoves {
|
||||
/// it was registered, the rest left waiting for the device's next submission. A move of an
|
||||
/// image bound to host pages is taken where `Mine` names its image, whatever its key.
|
||||
pub(crate) fn take_mine(&self, mine: Mine) -> Taken<'_> {
|
||||
self.take_with(|_| true, || None, |entry| mine.names(entry))
|
||||
self.take_with(|_| true, FillTake::All, || None, |entry| mine.names(entry))
|
||||
}
|
||||
|
||||
/// As [`take`](Self::take), for a submission whose commands may bind the heap sets (a
|
||||
/// batch's): where registrations do not wait for their place ([`FirstMoves::ordered`]), it
|
||||
/// also holds the texture slots' writes (`heap_sets::hold`, which `hold` makes) from before it
|
||||
/// takes until its submission was made or the work given back (the module docs).
|
||||
/// batch's), taking the fills `fills` says: where registrations do not wait for their place
|
||||
/// ([`FirstMoves::ordered`]), it also holds the texture slots' writes (`heap_sets::hold`,
|
||||
/// which `hold` makes) from before it takes until its submission was made or the work given
|
||||
/// back (the module docs).
|
||||
pub(crate) fn take_holding(
|
||||
&self,
|
||||
named: impl Fn(ResourceKey) -> bool,
|
||||
fills: FillTake<'_>,
|
||||
hold: impl FnOnce() -> HeldSlots,
|
||||
) -> Taken<'_> {
|
||||
self.take_with(named, || (!self.ordered).then(hold), |_| true)
|
||||
self.take_with(named, fills, || (!self.ordered).then(hold), |_| true)
|
||||
}
|
||||
|
||||
/// The submissions' order, for a submission: taken, and the wait counted where another
|
||||
@@ -1175,11 +1225,15 @@ impl FirstMoves {
|
||||
)
|
||||
}
|
||||
|
||||
/// As [`take`](Self::take), holding the slots `hold` gives once its place is taken, and
|
||||
/// taking only the entries `wanted` says: the rest stay waiting, in their order.
|
||||
/// As [`take`](Self::take), holding the slots `hold` gives once its place is taken, taking
|
||||
/// only the entries `wanted` says, and of the fills those `fills` says: the rest stay waiting,
|
||||
/// in their order. A fill's turn is settled once every move taken is known: a move of an
|
||||
/// image in memory another resource owns, or a staged copy, takes every fill (the module
|
||||
/// docs).
|
||||
fn take_with(
|
||||
&self,
|
||||
named: impl Fn(ResourceKey) -> bool,
|
||||
fills: FillTake<'_>,
|
||||
hold: impl FnOnce() -> Option<HeldSlots>,
|
||||
wanted: impl Fn(&Waiting) -> bool,
|
||||
) -> Taken<'_> {
|
||||
@@ -1187,20 +1241,23 @@ impl FirstMoves {
|
||||
let slots = hold();
|
||||
let mut held = Vec::new();
|
||||
let mut left: Vec<Box<dyn Any + Send + Sync>> = Vec::new();
|
||||
let mut every_fill = matches!(fills, FillTake::All);
|
||||
{
|
||||
let mut waiting = lock(&self.waiting);
|
||||
if waiting.is_empty() {
|
||||
return Taken {
|
||||
moves: self,
|
||||
held,
|
||||
every_fill,
|
||||
_slots: slots,
|
||||
_order: order,
|
||||
};
|
||||
}
|
||||
let mut kept = Vec::new();
|
||||
// Each entry in its order: kept, taken, or a fill whose turn waits for the moves.
|
||||
let mut sorted = Vec::with_capacity(waiting.len());
|
||||
for entry in waiting.drain(..) {
|
||||
if !wanted(&entry) {
|
||||
kept.push(entry);
|
||||
sorted.push(Sorted::Kept(entry));
|
||||
continue;
|
||||
}
|
||||
match entry {
|
||||
@@ -1209,7 +1266,7 @@ impl FirstMoves {
|
||||
continue;
|
||||
};
|
||||
if fresh.rebindable().is_some_and(|key| !named(key)) {
|
||||
kept.push(Waiting::Moves(weak));
|
||||
sorted.push(Sorted::Kept(Waiting::Moves(weak)));
|
||||
left.push(Box::new(fresh));
|
||||
continue;
|
||||
}
|
||||
@@ -1221,45 +1278,75 @@ impl FirstMoves {
|
||||
if moves.is_empty() {
|
||||
left.push(Box::new(fresh));
|
||||
} else {
|
||||
held.push(Held::Moves(fresh, moves));
|
||||
every_fill |= fresh.in_other_memory();
|
||||
sorted.push(Sorted::Taken(Held::Moves(fresh, moves)));
|
||||
}
|
||||
}
|
||||
Waiting::Fill(weak, mut fill) => {
|
||||
Waiting::Fill(weak, fill) => {
|
||||
let Some(buffer) = weak.upgrade() else {
|
||||
continue;
|
||||
};
|
||||
fill.owner = buffer.owner();
|
||||
// The submission holds the buffer's memory; the buffer goes as the
|
||||
// program lets go of it, once the lock is free.
|
||||
if let Some(memory) = buffer.memory() {
|
||||
held.push(Held::Fill(weak, memory, fill));
|
||||
}
|
||||
left.push(Box::new(buffer));
|
||||
sorted.push(Sorted::Fill(weak, fill, buffer));
|
||||
}
|
||||
// An image gone takes its copy and staging buffer with it.
|
||||
Waiting::Texels(weak, copy) => {
|
||||
if let Some(image) = weak.upgrade() {
|
||||
held.push(Held::Texels(image, copy));
|
||||
every_fill = true;
|
||||
sorted.push(Sorted::Taken(Held::Texels(image, copy)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut kept = Vec::new();
|
||||
for entry in sorted {
|
||||
match entry {
|
||||
Sorted::Kept(entry) => kept.push(entry),
|
||||
Sorted::Taken(entry) => held.push(entry),
|
||||
Sorted::Fill(weak, mut fill, buffer) => {
|
||||
fill.owner = match &fills {
|
||||
FillTake::All => None,
|
||||
FillTake::Reached { owner_of, .. } => fill.key.and_then(owner_of),
|
||||
};
|
||||
let taken = every_fill
|
||||
|| match (&fills, fill.owner) {
|
||||
(FillTake::Reached { reaches, .. }, Some(owner)) => reaches(owner),
|
||||
_ => true,
|
||||
};
|
||||
match taken {
|
||||
// The submission holds the buffer's memory; the buffer goes as the
|
||||
// program lets go of it, once the lock is free.
|
||||
true => {
|
||||
if let Some(memory) = buffer.memory() {
|
||||
held.push(Held::Fill(weak, memory, fill));
|
||||
}
|
||||
}
|
||||
false => kept.push(Waiting::Fill(weak, fill)),
|
||||
}
|
||||
left.push(Box::new(buffer));
|
||||
}
|
||||
}
|
||||
}
|
||||
waiting.extend(kept);
|
||||
}
|
||||
drop(left);
|
||||
Taken {
|
||||
moves: self,
|
||||
held,
|
||||
every_fill,
|
||||
_slots: slots,
|
||||
_order: order,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether no work waits for the device's next submission: a command buffer that needs no GPU
|
||||
/// work then goes with no submission, and one that finds work waiting carries it as before
|
||||
/// Whether a batch whose commands name nothing takes no work waiting: none waits but fills
|
||||
/// of memory whose key `named_only` says only a batch that may reach it takes
|
||||
/// ([`FillTake::Reached`]). A command buffer that needs no GPU work then goes with no
|
||||
/// submission, and one that finds work waiting carries it as before
|
||||
/// (`command_buffer/submit.rs`).
|
||||
pub(crate) fn none_waiting(&self) -> bool {
|
||||
lock(&self.waiting).is_empty()
|
||||
pub(crate) fn none_waiting_but(&self, named_only: impl Fn(ResourceKey) -> bool) -> bool {
|
||||
lock(&self.waiting).iter().all(
|
||||
|entry| matches!(entry, Waiting::Fill(_, fill) if fill.key.is_some_and(&named_only)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Let go of the fills and copies that wait for what is at `keeper` (the address of what
|
||||
@@ -1629,6 +1716,8 @@ impl Zeros {
|
||||
pub(crate) struct Taken<'a> {
|
||||
moves: &'a FirstMoves,
|
||||
held: Vec<Held>,
|
||||
/// It took every fill waiting ([`every_fill`](Self::every_fill)).
|
||||
every_fill: bool,
|
||||
/// The texture slots' writes it holds ([`FirstMoves::take_holding`]), made as this drops,
|
||||
/// before its place goes: the field before `_order`, which drops after it.
|
||||
_slots: Option<HeldSlots>,
|
||||
@@ -1637,6 +1726,14 @@ pub(crate) struct Taken<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Taken<'a> {
|
||||
/// Whether it took every fill waiting: it took them all ([`FillTake::All`]), or took a move of
|
||||
/// an image in memory another resource owns, or a staged copy, each of which comes after every
|
||||
/// fill of that memory. Its commands then may reach what any fill-queue submission not yet
|
||||
/// complete filled too (`command_buffer/submit.rs`).
|
||||
pub(crate) fn every_fill(&self) -> bool {
|
||||
self.every_fill
|
||||
}
|
||||
|
||||
/// Whether it took first work ([`record`](Self::record)'s): a move or a fill.
|
||||
pub(crate) fn has_first(&self) -> bool {
|
||||
(self.held.iter()).any(|held| matches!(held, Held::Moves(..) | Held::Fill(..)))
|
||||
@@ -1984,8 +2081,9 @@ impl Fills<'_> {
|
||||
unsafe { record_fills(counted, command_buffer, &fills) };
|
||||
}
|
||||
|
||||
/// What owns the memory the fills write ([`Filled::owner`]), each once; `None` where one
|
||||
/// writes memory no use of the program's names, which any command may reach.
|
||||
/// What owns the memory the fills write, as the batch that took them found it
|
||||
/// ([`FillTake::Reached`]), each once; `None` where one writes memory no use of the
|
||||
/// program's names, or the take found no owner, which any command may reach.
|
||||
pub(crate) fn owners(&self) -> Option<Vec<ObjectId>> {
|
||||
let mut owners = Vec::new();
|
||||
for held in &self.held {
|
||||
@@ -2114,6 +2212,110 @@ mod tests {
|
||||
buffer
|
||||
}
|
||||
|
||||
/// As [`fill`], of memory whose uses name `key`.
|
||||
fn keyed_fill(moves: &FirstMoves, raw: u64, key: Option<ResourceKey>) -> Arc<Unit> {
|
||||
let buffer = fill(moves, raw, 16);
|
||||
if let Some(Waiting::Fill(_, fill)) = lock(&moves.waiting).last_mut() {
|
||||
fill.key = key;
|
||||
}
|
||||
buffer
|
||||
}
|
||||
|
||||
/// An image in memory another resource owns, holding its move.
|
||||
struct Placed(Mutex<Vec<FirstMove>>);
|
||||
|
||||
impl Fresh for Placed {
|
||||
fn take(&self) -> Vec<FirstMove> {
|
||||
lock(&self.0).drain(..).collect()
|
||||
}
|
||||
|
||||
fn give_back(&self, moves: Vec<FirstMove>) {
|
||||
lock(&self.0).extend(moves);
|
||||
}
|
||||
|
||||
fn rebindable(&self) -> Option<ResourceKey> {
|
||||
None
|
||||
}
|
||||
|
||||
fn in_other_memory(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// The `VkBuffer`s of the fills `taken` holds, in order.
|
||||
fn filled(taken: &Taken<'_>) -> Vec<u64> {
|
||||
(taken.held.iter())
|
||||
.filter_map(|held| match held {
|
||||
Held::Fill(_, _, fill) => Some(fill.buffer.as_raw()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A batch on a device with a fill queue (`FillTake::Reached`) takes the fills of memory its
|
||||
/// commands reach and of memory no use names, in their order, and leaves the rest waiting,
|
||||
/// which a batch whose commands name nothing leaves too; one that takes a move of an image in
|
||||
/// memory another resource owns, or a staged copy, takes every fill.
|
||||
#[test]
|
||||
fn a_batch_takes_the_fills_of_memory_it_reaches() {
|
||||
let moves = FirstMoves::default();
|
||||
let (reached, unreached) = (ResourceKey::new(), ResourceKey::new());
|
||||
let (owner, other) = (ObjectId::new(), ObjectId::new());
|
||||
let owner_of = |key: ResourceKey| match key == reached {
|
||||
true => Some(owner),
|
||||
false => Some(other),
|
||||
};
|
||||
let reaches = |of: ObjectId| of == owner;
|
||||
let none = |_: ObjectId| false;
|
||||
let _buffers = [
|
||||
keyed_fill(&moves, 1, Some(reached)),
|
||||
keyed_fill(&moves, 2, Some(unreached)),
|
||||
keyed_fill(&moves, 3, None),
|
||||
keyed_fill(&moves, 4, Some(reached)),
|
||||
];
|
||||
let named_only = |key: ResourceKey| owner_of(key).is_some();
|
||||
assert!(!moves.none_waiting_but(named_only));
|
||||
let fills = FillTake::Reached {
|
||||
owner_of: &owner_of,
|
||||
reaches: &reaches,
|
||||
};
|
||||
let taken = moves.take_with(|_| true, fills, || None, |_| true);
|
||||
assert!(!taken.every_fill());
|
||||
assert_eq!(filled(&taken), [1, 3, 4]);
|
||||
drop(taken.submitted(None));
|
||||
assert!(moves.none_waiting_but(named_only));
|
||||
assert!(!moves.none_waiting_but(|_| false));
|
||||
// A move of an image in another resource's memory takes the fill no use reaches.
|
||||
let placed = Arc::new(Placed(Mutex::new(vec![first(5, false)])));
|
||||
let weak: Weak<Placed> = Arc::downgrade(&placed);
|
||||
moves.register(weak);
|
||||
let fills = FillTake::Reached {
|
||||
owner_of: &owner_of,
|
||||
reaches: &none,
|
||||
};
|
||||
let taken = moves.take_with(|_| true, fills, || None, |_| true);
|
||||
assert!(taken.every_fill());
|
||||
assert_eq!(filled(&taken), [2]);
|
||||
drop(taken.submitted(None));
|
||||
assert!(moves.none_waiting_but(|_| false));
|
||||
// So does a staged copy.
|
||||
let _later = keyed_fill(&moves, 6, Some(unreached));
|
||||
let image: Arc<u32> = Arc::new(0);
|
||||
let image_weak: Weak<u32> = Arc::downgrade(&image);
|
||||
let copy = (vk::Image::null(), vk::BufferImageCopy::default());
|
||||
let staged = TexelCopy::new(vk::Buffer::null(), 16, vec![copy], Box::new(()));
|
||||
assert!(!moves.register_texels(image_weak, staged));
|
||||
let fills = FillTake::Reached {
|
||||
owner_of: &owner_of,
|
||||
reaches: &none,
|
||||
};
|
||||
let taken = moves.take_with(|_| true, fills, || None, |_| true);
|
||||
assert!(taken.every_fill());
|
||||
assert_eq!(filled(&taken), [6]);
|
||||
drop(taken.submitted(None));
|
||||
assert!(moves.none_waiting_but(|_| false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fill_copies_the_zeros_in_pieces_of_the_zeros_length() {
|
||||
let fill = FirstFill::new(
|
||||
|
||||
@@ -336,6 +336,13 @@ impl Owners {
|
||||
}
|
||||
}
|
||||
|
||||
/// What owns the memory under `key`, by its identity, while it lives: the program's buffer, or
|
||||
/// what owns a texture's memory (`Owners`); else the heap whose own buffer's key it is
|
||||
/// (`HeapKeys`). `None` for a key of the runtime's own resources.
|
||||
pub(super) fn owner_of(shared: &super::device::DeviceShared, key: ResourceKey) -> Option<ObjectId> {
|
||||
(shared.owners.id_of(key)).or_else(|| shared.heap_keys.id_of(key))
|
||||
}
|
||||
|
||||
/// Whether what `keys` name, or `residency` made resident, has an owner among `objects`, as
|
||||
/// [`CommandBuffer::used_resources`] would list it: the device's owners of the keys (`shared`), the
|
||||
/// heaps among them, and the residency's resources and heaps, each by its identity, with no
|
||||
|
||||
+4
-2
@@ -495,8 +495,10 @@ recorded here.
|
||||
batch's start's, where two devices that both copied at the batch's start differed by 0.31 to 1.85
|
||||
times (`tests::managed_copies_bench`): no gain past that spread, so smaller fills keep the batch's
|
||||
start. From 1 MiB up the other queue's kernels' share of their rate moved by -11 to +8 points on
|
||||
the RTX 3090 and RTX 2080 and by +1 to +21 on the RTX 5080 Laptop GPU; every other submission of
|
||||
the device's waits on the GPU for the fill queue's last until the host saw it complete.
|
||||
the RTX 3090 and RTX 2080 and by +1 to +21 on the RTX 5080 Laptop GPU. A batch takes and waits
|
||||
on the GPU for the fills of memory its commands may reach alone (every one where a command may
|
||||
reach memory no use names), and internal submissions for the fill queue's last, until the host
|
||||
saw it complete (`WorkCounts::fill_queue_waits_unused`).
|
||||
- **A no-copy buffer's deallocator runs in its release.** On an M3 Max under macOS 27.0.1 an unused
|
||||
no-copy buffer's deallocator ran inside its final release, on the releasing thread, every time.
|
||||
So a release that frees the buffer's import while a deallocator waits for the free frees it
|
||||
|
||||
Reference in new issue
Block a user