Compute dispatches keep their bind state between dispatches, and pipelines keep four plans
A compute encoder's dispatch that did not take what the last one settled rebuilt its whole bind
state, listing every resource the encoder made resident, and each pipeline state kept one plan, so
per-dispatch cost grew with the resident count and two encoders of one pipeline with different
resident sets planned again at every dispatch.
- compute/live.rs: the encoder keeps its bind state (LiveDispatch), brought in place at each
dispatch as a render encoder's draw does (live_bind.rs): the kernel table is read again only
where its stamp, interface, sampler maps or no-copy moves changed, and only the slots calls
changed where the table keeps them; the used resources are listed again only where the
residency's generation, a no-copy move or an indirect command buffer's slot changed; the shape
is kept while no change may have changed it. Debug builds check the whole state against one
built from scratch at every dispatch. The state goes to the device's spares at the encoder's
end (four kept), with its memo let go first, so a kept state holds nothing alive.
- DispatchMemo stays the "nothing changed" shortcut, now owned by the live state.
- pipeline/mod.rs: a pipeline state keeps four plans, least recently used first out, found by
identity and then compared in place (BindState::position_of_shape), with the same key.
- The ICB and host-step dispatch paths keep the fresh path.
- WorkCounts: dispatch_shapes_made, dispatch_shapes_found, dispatch_used_resources_listed.
- residency.rs: the Generations doc says what is listed once per change, and where.
The same Vulkan commands, barriers, notices and refusals at the same calls; retains unchanged.
Checks that fail without it:
- runtime_lane::dispatch_work::dispatches_list_what_their_encoder_made_resident_once_a_change:
with the residency reuse forced off, every offset change lists again ((1,0,1) for (0,0,1)).
- dispatches_take_what_their_state_planned_for_the_same_shape gains a third encoder of the first
shape: with one kept plan it plans again ("an encoder of the first shape", 0 for 1).
Release, M3 Max, MoltenVK 1.4.2, host_ab encode (3 rounds, median, us per dispatch), base 096b427da3:
| workload | before | after |
|---|---:|---:|
| dispatch_resident_moving_0 | 2.090 | 1.757 |
| dispatch_resident_moving_16 | 2.570 | 1.754 |
| dispatch_resident_moving_256 | 11.472 | 1.764 |
| dispatch_resident_moving_4096 | 209.803 | 1.799 |
| dispatch_offsets | 2.186 | 1.929 |
| dispatch_churn | 2.164 | 1.928 |
| dispatch_static (memo hit) | 0.846 | 0.848 |
| dispatch_resident_4096 (memo hit) | 0.961 | 0.807 |
host_ab calls: many_dispatch 6397 -> 4095 ns and 74 -> 49 allocations; many_dispatch_resident
5331 -> 3496 ns, 78 -> 50; wide_dispatch 2825 -> 2293 ns, 40.9 -> 36.6. percall allocation
counts unchanged.
Verified on MoltenVK (with the tracker change that follows also applied): the runtime GPU suite
1504/1504; the validation runtime lanes, but for two failures that fail the same at the base
(function_tables::tables_and_handles_answer_as_metal_s_do,
predication::every_dispatch_form_runs_in_each_pass_past_the_copies).
(cherry picked from commit 8f350dddbe5c1cb3f5a486413b19ff23aa4f16c6)
(cherry picked from commit 5b3671810c7d658fd4223d5a6c97c1e33443e8a2)
(cherry picked from commit 03fe129bd31904ddf5013631ce104bb47438eaa9)
(cherry picked from commit 6457fa412ca00092e5b0a8603e05cbd99cd130b4)
This commit is contained in:
1 parent
f2097a3b2e
commit
a4546da224
11 files changed
+824
-172
No files matched your search
@@ -1719,6 +1719,12 @@ impl Gpu for Runtime {
|
||||
),
|
||||
("draw_shapes_made", c.draw_shapes_made),
|
||||
("draw_shapes_found", c.draw_shapes_found),
|
||||
("dispatch_shapes_made", c.dispatch_shapes_made),
|
||||
("dispatch_shapes_found", c.dispatch_shapes_found),
|
||||
(
|
||||
"dispatch_used_resources_listed",
|
||||
c.dispatch_used_resources_listed,
|
||||
),
|
||||
("argument_samplers_taken", c.argument_samplers_taken),
|
||||
("bind_views_worked_out", c.bind_views_worked_out),
|
||||
("stage_tables_read", c.stage_tables_read),
|
||||
|
||||
@@ -1397,9 +1397,11 @@ impl ArgumentTable {
|
||||
);
|
||||
}
|
||||
// A table read whole binds nothing else (`stage_table` starts from the empty table), but
|
||||
// for the object stage's threadgroup lengths, the encoder's, which the draw writes into
|
||||
// the kept table after the read (`RenderLowering::refresh_bind_state`).
|
||||
let lengths = stage != Stage::Object && !kept.threadgroup_lengths.is_empty();
|
||||
// for the object stage's and the kernel's threadgroup lengths, the encoder's, which the
|
||||
// draw or the dispatch writes into the kept table after the read
|
||||
// (`RenderLowering::refresh_bind_state`, `LiveDispatch::refresh`).
|
||||
let encoders = matches!(stage, Stage::Object | Stage::Kernel);
|
||||
let lengths = !encoders && !kept.threadgroup_lengths.is_empty();
|
||||
assert!(
|
||||
!lengths && kept.acceleration_structures.is_empty(),
|
||||
"{DIFFERS}: {stage:?} keeps threadgroup lengths {:?} and acceleration structures {:?}, \
|
||||
|
||||
@@ -13,9 +13,10 @@
|
||||
//! threadgroup memory length that is not a multiple of 16 is taken (the binder rounds every
|
||||
//! length up to 16, as Metal does). `set_imageblock_size` is checked where Metal checks it, at
|
||||
//! the call.
|
||||
//! 2. The tables become a `bind::BindState`; the state's memo gives the plan of its shape, on the
|
||||
//! variant the dispatch runs on (compiled when first needed), and `instantiate_dispatch` the
|
||||
//! command's regions, bytes, and push holes.
|
||||
//! 2. The tables become a `bind::BindState`, brought in place from the one the encoder's last
|
||||
//! dispatch bound (`compute/live.rs`); the pipeline state's kept plans give the plan of its
|
||||
//! shape, on the variant the dispatch runs on (compiled when first needed), and
|
||||
//! `instantiate_dispatch` the command's regions, bytes, and push holes.
|
||||
//! 3. [`binding`](super::binding) gives the descriptors, uploads, and push bytes; the variant
|
||||
//! gives set 0 and one `VkPipeline` per region specialization, and the command buffer set 1,
|
||||
//! the heap, which its dispatches on the variant share ([`heap_sets`](super::heap_sets)).
|
||||
@@ -97,6 +98,11 @@ use std::ops::Range;
|
||||
use std::sync::atomic::AtomicU64;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
|
||||
mod live;
|
||||
|
||||
use live::LiveDispatch;
|
||||
pub(crate) use live::SpareLiveDispatches;
|
||||
|
||||
/// Entries of a compute encoder's threadgroup memory table.
|
||||
pub(super) const THREADGROUP_SLOTS: u32 = 31;
|
||||
|
||||
@@ -176,8 +182,9 @@ pub struct ComputeCommandEncoder {
|
||||
/// Its dispatches encoded so far: the next one's order among them, which one whose
|
||||
/// pipelines are being made keeps for its failure (`FailedPipeline::draw`).
|
||||
dispatches: usize,
|
||||
/// What its last dispatch settled ([`DispatchMemo`]).
|
||||
memo: DispatchMemo,
|
||||
/// Its bind state as its last dispatch that settled bound it, with what that one settled
|
||||
/// (`compute/live.rs`, [`DispatchMemo`]).
|
||||
live: LiveDispatch,
|
||||
/// The residency's generation as a command last noted the heaps of what it made resident
|
||||
/// (`command_encoded`): no command notes them again before it changes.
|
||||
heaps_noted: Option<u64>,
|
||||
@@ -280,7 +287,8 @@ impl CommandBuffer {
|
||||
log: (self.device().shared().rebinds.any())
|
||||
.then(|| self.device().shared().scheduler.spare_logs().compute()),
|
||||
dispatches: 0,
|
||||
memo: DispatchMemo::default(),
|
||||
// The bind state the device's last ended compute encoder left (`compute/live.rs`).
|
||||
live: self.device().shared().live_dispatches.take(),
|
||||
heaps_noted: None,
|
||||
})
|
||||
}
|
||||
@@ -309,7 +317,7 @@ impl ComputeCommandEncoder {
|
||||
captures: Captures::default(),
|
||||
log: None,
|
||||
dispatches: 0,
|
||||
memo: DispatchMemo::default(),
|
||||
live: command_buffer.device().shared().live_dispatches.take(),
|
||||
heaps_noted: None,
|
||||
})
|
||||
}
|
||||
@@ -321,6 +329,8 @@ impl ComputeCommandEncoder {
|
||||
// (`predication.rs`).
|
||||
self.end_predication()?;
|
||||
self.table.let_go_all(&mut self.core);
|
||||
// Its bind state goes on to the device's next compute encoder (`compute/live.rs`).
|
||||
self.core.device().shared().live_dispatches.give(self.live);
|
||||
let replayed = self.core.finish_replay()?;
|
||||
Ok((replayed, Some(self.residency)))
|
||||
}
|
||||
@@ -1101,7 +1111,7 @@ impl ComputeCommandEncoder {
|
||||
&tables,
|
||||
call,
|
||||
Counts::Flat(flat),
|
||||
(Some(self.dispatches), None),
|
||||
Some((self.dispatches, &mut self.live)),
|
||||
)?
|
||||
{
|
||||
return Ok(());
|
||||
@@ -1594,6 +1604,8 @@ impl ComputeCommandEncoder {
|
||||
// Its unit of command predication takes what it encoded (`predication.rs`).
|
||||
self.end_predication()?;
|
||||
self.table.let_go_all(&mut self.core);
|
||||
// Its bind state goes on to the device's next compute encoder (`compute/live.rs`).
|
||||
self.core.device().shared().live_dispatches.give(self.live);
|
||||
// Its log, which its command buffer keeps where a rebind may need it (`replay.rs`).
|
||||
let replay = self.log.map(|log| Replay::Compute {
|
||||
dispatch_type: self.dispatch_type,
|
||||
@@ -1688,7 +1700,7 @@ impl ComputeCommandEncoder {
|
||||
&tables,
|
||||
call,
|
||||
Counts::Direct(dispatch),
|
||||
(Some(self.dispatches), Some(&mut self.memo)),
|
||||
Some((self.dispatches, &mut self.live)),
|
||||
)
|
||||
.map(drop)
|
||||
}
|
||||
@@ -1784,7 +1796,7 @@ pub(super) fn record_dispatch(
|
||||
call: &'static str,
|
||||
dispatch: Dispatch,
|
||||
) -> Result<(), Error> {
|
||||
record(core, tables, call, Counts::Direct(dispatch), (None, None)).map(drop)
|
||||
record(core, tables, call, Counts::Direct(dispatch), None).map(drop)
|
||||
}
|
||||
|
||||
/// Where a recorded dispatch's group counts come from.
|
||||
@@ -1881,36 +1893,37 @@ fn apart_samplers(device: &Device, objects: &VariantObjects) -> bool {
|
||||
.contains(&RuntimeDriverDefect::ImmutableSamplersLostAfterCopies)
|
||||
}
|
||||
|
||||
/// What a dispatch settles before it binds ([`settle_dispatch`]): the device's sampler states it
|
||||
/// lists (less those at IDs no set-1 sampler array of the variant holds, `unlisted` saying so:
|
||||
/// `heap_sets::fit_samplers`), the visible-function tables its state made, its runtime samplers'
|
||||
/// address-mode maps, its bind state, and what it planned: the state's shape, and the variant it
|
||||
/// runs on with its plan, for `planned.dispatch`.
|
||||
/// What a dispatch settles before it binds ([`settle_dispatch`], [`settle_live`]): the device's
|
||||
/// sampler states it lists (less those at IDs no set-1 sampler array of the variant holds,
|
||||
/// `unlisted` saying so: `heap_sets::fit_samplers`), the visible-function tables its state made,
|
||||
/// its runtime samplers' address-mode maps, and what it planned: the state's shape, and the variant
|
||||
/// it runs on with its plan, for `planned.dispatch`. Its bind state is apart: the encoder's kept one
|
||||
/// (`compute/live.rs`), or one of its own.
|
||||
pub(super) struct SettledDispatch {
|
||||
argument_samplers: Vec<SamplerState>,
|
||||
function_tables: Vec<VisibleFunctionTable>,
|
||||
unlisted: Option<(u64, u64)>,
|
||||
sampler_maps: SamplerMaps,
|
||||
state: BindState,
|
||||
planned: Arc<Planned>,
|
||||
}
|
||||
|
||||
/// A flat indirect call and its preprocess's description (`indirect_dispatch.rs`).
|
||||
type Flat<'a> = Option<(FlatCall<'a>, FlatIndirect)>;
|
||||
|
||||
/// What a dispatch of `tables`, of `direct` group counts or of a flat indirect call (`gpu`),
|
||||
/// settles ([`SettledDispatch`]) with `function_tables`, its state's, and `samplers`, the
|
||||
/// device's sampler states: the flat call's preprocess description beside it, and `None` where
|
||||
/// What a dispatch of `tables` that keeps no bind state (an indirect command's, a host step's),
|
||||
/// of `direct` group counts or of a flat indirect call (`gpu`), settles ([`SettledDispatch`])
|
||||
/// with `function_tables`, its state's, and `samplers`, the device's sampler states, and the bind
|
||||
/// state it settled, made whole: the flat call's preprocess description beside it, and `None` where
|
||||
/// the GPU path does not take an indirect dispatch (`indirect_dispatch.rs`). A direct dispatch
|
||||
/// whose bind state has the shape the state's last direct dispatch planned, of the same dispatch
|
||||
/// and buffer forms, takes what that one planned ([`Planned`]); one that plans keeps what it
|
||||
/// planned for the next.
|
||||
/// whose bind state has the shape one its pipeline state planned lately for the same dispatch and
|
||||
/// buffer forms, compared in place (`ComputePipelineState::planned_of`), takes what that one
|
||||
/// planned ([`Planned`]); one that plans keeps what it planned for the next.
|
||||
fn settle_dispatch<'a>(
|
||||
tables: &DispatchTables<'_>,
|
||||
(direct, gpu): (Option<Dispatch>, Option<FlatCall<'a>>),
|
||||
function_tables: Vec<VisibleFunctionTable>,
|
||||
samplers: Vec<SamplerState>,
|
||||
) -> Result<Option<(SettledDispatch, Flat<'a>)>, Error> {
|
||||
) -> Result<Option<(BindState, SettledDispatch, Flat<'a>)>, Error> {
|
||||
let pipeline = tables.pipeline;
|
||||
// The device's sampler states, less those at IDs no set-1 sampler array of the variant
|
||||
// holds (each array has the variant's heap capacity of elements): a degradation where
|
||||
@@ -1919,79 +1932,153 @@ fn settle_dispatch<'a>(
|
||||
let mut unlisted = None;
|
||||
// Its runtime samplers' address modes as the textures they sample map them.
|
||||
let sampler_maps = tables.sampler_maps()?;
|
||||
let last = match direct {
|
||||
Some(dispatch) => pipeline
|
||||
.planned()
|
||||
.filter(|last| last.dispatch == dispatch && last.forms == tables.forms),
|
||||
None => None,
|
||||
};
|
||||
let (state, planned, as_before, flat) = loop {
|
||||
let mut state = tables.bind_state(&argument_samplers, &function_tables);
|
||||
sampler_maps.apply(&mut state);
|
||||
let last = last.as_ref().filter(|last| state.has_shape(&last.shape));
|
||||
let (planned, as_before, flat) = match (last, direct, gpu) {
|
||||
(Some(last), ..) => (Arc::clone(last), true, None),
|
||||
(None, Some(dispatch), _) => {
|
||||
let shape = state.shape();
|
||||
let flat = flat_direct(pipeline, &shape, &dispatch, tables.forms)?;
|
||||
let (variant, plan) = match flat {
|
||||
Some(flat) => flat,
|
||||
None => pipeline.settle(&shape, &dispatch, tables.forms)?,
|
||||
};
|
||||
let planned = Planned {
|
||||
shape: Arc::new(shape),
|
||||
dispatch,
|
||||
forms: tables.forms,
|
||||
variant,
|
||||
plan,
|
||||
made: OnceLock::new(),
|
||||
};
|
||||
(Arc::new(planned), false, None)
|
||||
let last = direct.and_then(|dispatch| pipeline.planned_of(&state, &dispatch, tables.forms));
|
||||
let (planned, as_before, flat) = match last {
|
||||
Some(last) => (last, true, None),
|
||||
None => {
|
||||
let shape = Arc::new(state.shape());
|
||||
match plan_now(pipeline, tables.forms, shape, (direct, gpu))? {
|
||||
Some((planned, flat)) => (planned, false, flat),
|
||||
None => return Ok(None),
|
||||
}
|
||||
}
|
||||
(None, None, Some(gpu)) => {
|
||||
let shape = state.shape();
|
||||
let Some((variant, plan, indirect)) = settle_indirect(pipeline, gpu, &shape)?
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
let planned = Planned {
|
||||
shape: Arc::new(shape),
|
||||
dispatch: flat_dispatch(indirect.threads_per_threadgroup),
|
||||
forms: tables.forms,
|
||||
variant,
|
||||
plan,
|
||||
made: OnceLock::new(),
|
||||
};
|
||||
(Arc::new(planned), false, Some((gpu, indirect)))
|
||||
}
|
||||
(None, None, None) => return Err(internal("a dispatch of no counts")),
|
||||
};
|
||||
let set = planned.variant.pipeline().layout.sets.get(1);
|
||||
if heap_sets::fit_samplers(set, &mut argument_samplers, &mut unlisted) {
|
||||
break (state, planned, as_before, flat);
|
||||
}
|
||||
};
|
||||
match (as_before, direct) {
|
||||
(true, _) => (pipeline.device().shared().counts).dispatch_planned_as_before(),
|
||||
// A flat dispatch's variant and plan are the flat path's: none a direct one takes.
|
||||
(false, Some(_)) => pipeline.keep_planned(Arc::clone(&planned)),
|
||||
(false, None) => {}
|
||||
}
|
||||
planned_as_before(pipeline, &planned, as_before, direct);
|
||||
let settled = SettledDispatch {
|
||||
argument_samplers,
|
||||
function_tables,
|
||||
unlisted,
|
||||
sampler_maps,
|
||||
planned,
|
||||
};
|
||||
Ok(Some((state, settled, flat)))
|
||||
}
|
||||
|
||||
/// [`settle_dispatch`] of a compute encoder's own dispatch, through the encoder's kept bind state
|
||||
/// `live`, brought in place (`LiveDispatch::refresh`), with `samplers`, the device's sampler
|
||||
/// states at `changed` changes of their slots: the state's shape kept where nothing brought in may
|
||||
/// have changed it, else found among those the pipeline state planned for lately or made
|
||||
/// (`LiveDispatch::shape`), and what it planned for that shape, the very one looked for first
|
||||
/// (`ComputePipelineState::planned_for`).
|
||||
fn settle_live<'a>(
|
||||
live: &mut LiveDispatch,
|
||||
tables: &DispatchTables<'_>,
|
||||
(direct, gpu): (Option<Dispatch>, Option<FlatCall<'a>>),
|
||||
function_tables: Vec<VisibleFunctionTable>,
|
||||
(changed, samplers): (u64, Vec<SamplerState>),
|
||||
) -> Result<Option<(SettledDispatch, Flat<'a>)>, Error> {
|
||||
let pipeline = tables.pipeline;
|
||||
let counts = &pipeline.device().shared().counts;
|
||||
let mut argument_samplers = samplers;
|
||||
let mut unlisted = None;
|
||||
let sampler_maps = tables.sampler_maps()?;
|
||||
// Whether the states were trimmed to those a variant's set-1 sampler arrays hold.
|
||||
let mut trimmed = false;
|
||||
let (planned, as_before, flat) = loop {
|
||||
let listing = (changed, &argument_samplers[..], trimmed);
|
||||
live.refresh(tables, &sampler_maps, listing, &function_tables, counts);
|
||||
let shape = live.shape(counts, |state| pipeline.planned_shape(state));
|
||||
let last =
|
||||
direct.and_then(|dispatch| pipeline.planned_for(&shape, &dispatch, tables.forms));
|
||||
let (planned, as_before, flat) = match last {
|
||||
Some(last) => (last, true, None),
|
||||
None => match plan_now(pipeline, tables.forms, shape, (direct, gpu))? {
|
||||
Some((planned, flat)) => (planned, false, flat),
|
||||
None => return Ok(None),
|
||||
},
|
||||
};
|
||||
let set = planned.variant.pipeline().layout.sets.get(1);
|
||||
if heap_sets::fit_samplers(set, &mut argument_samplers, &mut unlisted) {
|
||||
break (planned, as_before, flat);
|
||||
}
|
||||
trimmed = true;
|
||||
};
|
||||
planned_as_before(pipeline, &planned, as_before, direct);
|
||||
let settled = SettledDispatch {
|
||||
argument_samplers,
|
||||
function_tables,
|
||||
unlisted,
|
||||
sampler_maps,
|
||||
state,
|
||||
planned,
|
||||
};
|
||||
Ok(Some((settled, flat)))
|
||||
}
|
||||
|
||||
/// What a compute encoder's last dispatch settled ([`SettledDispatch`]), its commands, and the
|
||||
/// `VkPipeline`s its regions found made, kept with what they are a function of: a later dispatch
|
||||
/// of the encoder of the same inputs settles, plans and instantiates nothing again, and looks no
|
||||
/// pipeline up (`WorkCounts::dispatches_settled_as_before`). Exact:
|
||||
/// What a dispatch of a bind state of `shape` in `forms` plans now, of `direct` group counts or of
|
||||
/// a flat indirect call (`gpu`): the direct dispatch's flat variant where it takes one, else
|
||||
/// `ComputePipelineState::settle`'s; the indirect one's flat or folded variant, with its preprocess
|
||||
/// description, or `None` where neither takes it, which then takes its host step.
|
||||
fn plan_now<'a>(
|
||||
pipeline: &ComputePipelineState,
|
||||
forms: BufferForms,
|
||||
shape: Arc<BindShape>,
|
||||
(direct, gpu): (Option<Dispatch>, Option<FlatCall<'a>>),
|
||||
) -> Result<Option<(Arc<Planned>, Flat<'a>)>, Error> {
|
||||
match (direct, gpu) {
|
||||
(Some(dispatch), _) => {
|
||||
let flat = flat_direct(pipeline, &shape, &dispatch, forms)?;
|
||||
let (variant, plan) = match flat {
|
||||
Some(flat) => flat,
|
||||
None => pipeline.settle(&shape, &dispatch, forms)?,
|
||||
};
|
||||
let planned = Planned {
|
||||
shape,
|
||||
dispatch,
|
||||
forms,
|
||||
variant,
|
||||
plan,
|
||||
made: OnceLock::new(),
|
||||
};
|
||||
Ok(Some((Arc::new(planned), None)))
|
||||
}
|
||||
(None, Some(gpu)) => {
|
||||
let Some((variant, plan, indirect)) = settle_indirect(pipeline, gpu, &shape)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let planned = Planned {
|
||||
shape,
|
||||
dispatch: flat_dispatch(indirect.threads_per_threadgroup),
|
||||
forms,
|
||||
variant,
|
||||
plan,
|
||||
made: OnceLock::new(),
|
||||
};
|
||||
Ok(Some((Arc::new(planned), Some((gpu, indirect)))))
|
||||
}
|
||||
(None, None) => Err(internal("a dispatch of no counts")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Count a direct dispatch that took what its pipeline state planned before (`as_before`), or
|
||||
/// keep what one planned now for the next; a flat dispatch's variant and plan are the flat path's,
|
||||
/// none a direct one takes.
|
||||
fn planned_as_before(
|
||||
pipeline: &ComputePipelineState,
|
||||
planned: &Arc<Planned>,
|
||||
as_before: bool,
|
||||
direct: Option<Dispatch>,
|
||||
) {
|
||||
match (as_before, direct) {
|
||||
(true, _) => (pipeline.device().shared().counts).dispatch_planned_as_before(),
|
||||
(false, Some(_)) => pipeline.keep_planned(Arc::clone(planned)),
|
||||
(false, None) => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// What a compute encoder's last dispatch that settled settled ([`SettledDispatch`]), its
|
||||
/// commands, and the `VkPipeline`s its regions found made, kept with what they are a function of
|
||||
/// beside the encoder's kept bind state, which that dispatch brought in place and binds
|
||||
/// (`compute/live.rs`): a later dispatch of the encoder of the same inputs settles, plans and
|
||||
/// instantiates nothing again, brings no state in place and looks no pipeline up
|
||||
/// (`WorkCounts::dispatches_settled_as_before`). Exact:
|
||||
///
|
||||
/// - **The bind state** is a function of the pipeline state (its interface, view base alignment
|
||||
/// and imageblock among it), of what the table binds (its stamp: every call that may change it
|
||||
@@ -2005,18 +2092,19 @@ fn settle_dispatch<'a>(
|
||||
/// buffer made later with the ID takes: `Residency::holds_slots_as`), of the device's live
|
||||
/// sampler states (`Device::sampler_slots_changed`), of the state's visible-function tables
|
||||
/// (none: a dispatch of a state that made one takes no memo), and of the sampler maps, which
|
||||
/// are the table's and the residency's.
|
||||
/// are the table's and the residency's. The kept state is this entry's: a dispatch that brings
|
||||
/// it in place keeps its own entry or none (`LiveDispatch::let_go`).
|
||||
/// - **The variant and plan** are `ComputePipelineState::settle`'s of the shape, the dispatch and
|
||||
/// the buffer forms; **the commands** are the plan's instantiation for the state and the
|
||||
/// dispatch, their regions keyed by the variant for the device (`Variant::keyed_regions`).
|
||||
/// - **A region's `VkPipeline`** found made is its variant's for the region's key, which its
|
||||
/// making never changes; regions not all made are looked up again at the next dispatch.
|
||||
/// - **A dispatch that settles again** (another table, another residency) takes the variant, the
|
||||
/// plan and the made `VkPipeline`s of the state's last direct dispatch that planned where its
|
||||
/// bind state has that one's shape ([`Planned`], [`settle_dispatch`]): so does one whose
|
||||
/// buffers' offsets alone moved, and an encoder's first dispatch of the state.
|
||||
/// plan and the made `VkPipeline`s of one of its pipeline state's latest direct dispatches that
|
||||
/// planned, where its bind state has that one's shape ([`Planned`], [`settle_live`]): so does one
|
||||
/// whose buffers' offsets alone moved, and an encoder's first dispatch of the state.
|
||||
#[derive(Default)]
|
||||
pub(super) struct DispatchMemo(Option<Arc<KeptDispatch>>);
|
||||
pub(super) struct DispatchMemo(Option<KeptDispatch>);
|
||||
|
||||
/// A [`DispatchMemo`]'s entry and what it was settled under.
|
||||
pub(super) struct KeptDispatch {
|
||||
@@ -2038,37 +2126,33 @@ pub(super) struct KeptDispatch {
|
||||
}
|
||||
|
||||
impl DispatchMemo {
|
||||
/// The entry, where it is of a dispatch of `tables` and `dispatch` at `counted` moves and
|
||||
/// sampler slot changes.
|
||||
fn find(
|
||||
/// Whether the entry is of a dispatch of `tables` and `dispatch` at `counted` moves and
|
||||
/// sampler slot changes, `state` the kept bind state it settled.
|
||||
fn holds(
|
||||
&self,
|
||||
state: &BindState,
|
||||
tables: &DispatchTables<'_>,
|
||||
dispatch: &Dispatch,
|
||||
counted: (u64, u64),
|
||||
) -> Option<Arc<KeptDispatch>> {
|
||||
let kept = self.0.as_ref()?;
|
||||
let same = kept.pipeline == *tables.pipeline
|
||||
&& kept.stamp == tables.table.stamp()
|
||||
&& kept.settled.planned.dispatch == *dispatch
|
||||
&& kept.counted == counted
|
||||
&& kept.forms == tables.forms
|
||||
&& kept.imageblock == tables.imageblock
|
||||
&& kept.stage_in_region == tables.stage_in_region
|
||||
&& kept.threadgroup_lengths == *tables.threadgroup_lengths
|
||||
&& kept.residency == tables.residency.generation()
|
||||
&& (tables.residency).holds_slots_as(&kept.settled.state.used_resources);
|
||||
same.then(|| Arc::clone(kept))
|
||||
) -> bool {
|
||||
self.0.as_ref().is_some_and(|kept| {
|
||||
kept.pipeline == *tables.pipeline
|
||||
&& kept.stamp == tables.table.stamp()
|
||||
&& kept.settled.planned.dispatch == *dispatch
|
||||
&& kept.counted == counted
|
||||
&& kept.forms == tables.forms
|
||||
&& kept.imageblock == tables.imageblock
|
||||
&& kept.stage_in_region == tables.stage_in_region
|
||||
&& kept.threadgroup_lengths == *tables.threadgroup_lengths
|
||||
&& kept.residency == tables.residency.generation()
|
||||
&& (tables.residency).holds_slots_as(&state.used_resources)
|
||||
})
|
||||
}
|
||||
|
||||
/// Keep `settled`, a dispatch of `tables` at `counted` moves and sampler slot changes, in
|
||||
/// place of the entry.
|
||||
fn keep(
|
||||
&mut self,
|
||||
tables: &DispatchTables<'_>,
|
||||
counted: (u64, u64),
|
||||
settled: SettledDispatch,
|
||||
) -> Arc<KeptDispatch> {
|
||||
let kept = Arc::new(KeptDispatch {
|
||||
fn keep(&mut self, tables: &DispatchTables<'_>, counted: (u64, u64), settled: SettledDispatch) {
|
||||
self.0 = Some(KeptDispatch {
|
||||
pipeline: tables.pipeline.clone(),
|
||||
stamp: tables.table.stamp(),
|
||||
threadgroup_lengths: tables.threadgroup_lengths.clone(),
|
||||
@@ -2081,25 +2165,30 @@ impl DispatchMemo {
|
||||
commands: OnceLock::new(),
|
||||
made: OnceLock::new(),
|
||||
});
|
||||
self.0 = Some(Arc::clone(&kept));
|
||||
kept
|
||||
}
|
||||
|
||||
/// The entry, where one is kept.
|
||||
fn entry(&self) -> Option<&KeptDispatch> {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
/// Plan, bind, and record the dispatch of `counts` with `tables` into `core` (the module's steps
|
||||
/// 2 to 4), for Metal's `call`: `Ok(false)`, recording nothing, where the GPU path does not take
|
||||
/// an indirect dispatch (`indirect_dispatch.rs`); a direct one is always recorded or refused.
|
||||
/// An encoder's own dispatch (`own`, its order among the encoder's dispatches) inside a block of
|
||||
/// command predication runs from the gates its unit's step writes (`predication.rs`), and takes
|
||||
/// what its last dispatch settled where it has the same inputs (`memo`, [`DispatchMemo`]). And
|
||||
/// one whose `VkPipeline`s are not made yet hands their making to the device's pool and records
|
||||
/// later (`pending_dispatch.rs`): an execution's or a host step's dispatch waits for them.
|
||||
/// An encoder's own dispatch (`own`: its order among the encoder's dispatches, and the encoder's
|
||||
/// kept bind state) inside a block of command predication runs from the gates its unit's step
|
||||
/// writes (`predication.rs`), binds the kept state brought in place (`compute/live.rs`), and a
|
||||
/// direct one takes what its last dispatch settled where it has the same inputs
|
||||
/// ([`DispatchMemo`]). And one whose `VkPipeline`s are not made yet hands their making to the
|
||||
/// device's pool and records later (`pending_dispatch.rs`): an execution's or a host step's
|
||||
/// dispatch waits for them.
|
||||
fn record(
|
||||
core: &mut EncoderCore,
|
||||
tables: &DispatchTables<'_>,
|
||||
call: &'static str,
|
||||
counts: Counts<'_>,
|
||||
(own, memo): (Option<usize>, Option<&mut DispatchMemo>),
|
||||
own: Option<(usize, &mut LiveDispatch)>,
|
||||
) -> Result<bool, Error> {
|
||||
let pipeline = tables.pipeline;
|
||||
let (direct, gpu) = match counts {
|
||||
@@ -2109,57 +2198,75 @@ fn record(
|
||||
// The live visible-function tables the pipeline state made: the encoder's, or an indirect
|
||||
// command's own state's.
|
||||
let function_tables = pipeline.function_tables();
|
||||
// An encoder's own direct dispatch, of a state that made no table, takes the memo.
|
||||
let memo = match (memo, direct) {
|
||||
(Some(memo), Some(dispatch)) if function_tables.is_empty() => Some((memo, dispatch)),
|
||||
_ => None,
|
||||
let (own, mut live) = match own {
|
||||
Some((own, live)) => (Some(own), Some(live)),
|
||||
None => (None, None),
|
||||
};
|
||||
let mut fresh = None;
|
||||
// What a dispatch that takes no memo entry settled, and the bind state one that keeps none
|
||||
// made.
|
||||
let mut settled_here = None;
|
||||
let mut state_here = None;
|
||||
let mut flat = None;
|
||||
let kept = match memo {
|
||||
Some((memo, dispatch)) => {
|
||||
match live.as_deref_mut() {
|
||||
Some(live) => {
|
||||
// A direct dispatch, of a state that made no table, takes the memo.
|
||||
let memo = direct.filter(|_| function_tables.is_empty());
|
||||
let moved = super::buffer::host_cells_moved();
|
||||
let counted = (moved, core.device().sampler_slots_changed());
|
||||
match memo.find(tables, &dispatch, counted) {
|
||||
Some(kept) => {
|
||||
core.device().shared().counts.dispatch_settled_as_before();
|
||||
Some(kept)
|
||||
}
|
||||
None => {
|
||||
// The sampler states, with the count of changes they are live at.
|
||||
let (changed, samplers) = core.device().argument_samplers_at();
|
||||
let settled =
|
||||
settle_dispatch(tables, (direct, None), function_tables, samplers)?;
|
||||
let Some((settled, _)) = settled else {
|
||||
return Err(internal("a direct dispatch the GPU path does not take"));
|
||||
};
|
||||
Some(memo.keep(tables, (moved, changed), settled))
|
||||
let held = memo.is_some_and(|dispatch| {
|
||||
let counted = (moved, core.device().sampler_slots_changed());
|
||||
live.memo.holds(&live.state, tables, &dispatch, counted)
|
||||
});
|
||||
if held {
|
||||
core.device().shared().counts.dispatch_settled_as_before();
|
||||
} else {
|
||||
// The sampler states, with the count of changes they are live at.
|
||||
let (changed, samplers) = core.device().argument_samplers_at();
|
||||
let settling = (changed, samplers);
|
||||
let settled = settle_live(live, tables, (direct, gpu), function_tables, settling);
|
||||
// The kept state is no longer what the memo's entry settled.
|
||||
let Some((settled, flat_call)) = settled.inspect_err(|_| live.let_go())? else {
|
||||
live.let_go();
|
||||
return Ok(false);
|
||||
};
|
||||
match memo {
|
||||
Some(_) => live.memo.keep(tables, (moved, changed), settled),
|
||||
None => {
|
||||
live.let_go();
|
||||
settled_here = Some(settled);
|
||||
flat = flat_call;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let samplers = core.device().argument_samplers();
|
||||
let Some((settled, flat_call)) =
|
||||
let Some((state, settled, flat_call)) =
|
||||
settle_dispatch(tables, (direct, gpu), function_tables, samplers)?
|
||||
else {
|
||||
return Ok(false);
|
||||
};
|
||||
fresh = Some(settled);
|
||||
state_here = Some(state);
|
||||
settled_here = Some(settled);
|
||||
flat = flat_call;
|
||||
None
|
||||
}
|
||||
};
|
||||
let settled = match (&kept, &fresh) {
|
||||
}
|
||||
let live = live.as_deref();
|
||||
let kept = live.and_then(|live| live.memo.entry());
|
||||
let settled = match (kept, &settled_here) {
|
||||
(Some(kept), _) => &kept.settled,
|
||||
(None, Some(fresh)) => fresh,
|
||||
(None, Some(settled)) => settled,
|
||||
(None, None) => return Err(internal("a dispatch settled nowhere")),
|
||||
};
|
||||
let state = match (&state_here, live) {
|
||||
(Some(state), _) => state,
|
||||
(None, Some(live)) => &live.state,
|
||||
(None, None) => return Err(internal("a dispatch's bind state nowhere")),
|
||||
};
|
||||
let SettledDispatch {
|
||||
argument_samplers,
|
||||
function_tables,
|
||||
unlisted,
|
||||
sampler_maps,
|
||||
state,
|
||||
planned,
|
||||
} = settled;
|
||||
let Planned {
|
||||
@@ -2183,14 +2290,14 @@ fn record(
|
||||
// The commands, their regions keyed (`Variant::keyed_regions`): those the memo's entry holds,
|
||||
// where it holds them.
|
||||
let mut instantiated = None;
|
||||
let commands = match kept.as_ref().and_then(|kept| kept.commands.get()) {
|
||||
let commands = match kept.and_then(|kept| kept.commands.get()) {
|
||||
Some(commands) => commands,
|
||||
None => {
|
||||
let mut commands = plan
|
||||
.instantiate_dispatch(state, &dispatch)
|
||||
.map_err(Error::Bind)?;
|
||||
variant.keyed_regions(core.device(), &mut commands);
|
||||
match &kept {
|
||||
match kept {
|
||||
Some(kept) => kept.commands.get_or_init(|| commands),
|
||||
None => &*instantiated.insert(commands),
|
||||
}
|
||||
@@ -2380,7 +2487,7 @@ fn record(
|
||||
// bakes them records in one of its own, as one being made does.
|
||||
// Those the memo's entry holds, else those found made for what the dispatch planned, of its
|
||||
// regions' keys ([`Planned`]).
|
||||
let kept_made = (kept.as_ref())
|
||||
let kept_made = kept
|
||||
.and_then(|kept| kept.made.get())
|
||||
.or_else(|| planned.made_for(commands))
|
||||
.filter(|_| !apart_samplers(&device, &objects));
|
||||
@@ -2391,7 +2498,7 @@ fn record(
|
||||
let waits = laying && !apart_samplers(&device, &objects);
|
||||
let (pipelines, pending) = match (own.filter(|_| !waits), kept_made) {
|
||||
(Some(_), Some(made)) => {
|
||||
if let Some(kept) = &kept {
|
||||
if let Some(kept) = kept {
|
||||
drop(kept.made.set(made.clone()));
|
||||
}
|
||||
(made.clone(), None)
|
||||
@@ -2416,7 +2523,7 @@ fn record(
|
||||
Some(made) if !apart_samplers(&device, &objects) => {
|
||||
// Kept with the entry, which the next dispatch of its inputs takes, and with
|
||||
// what it planned, which the next dispatch of its shape takes.
|
||||
if let Some(kept) = &kept {
|
||||
if let Some(kept) = kept {
|
||||
drop(kept.made.set(made.clone()));
|
||||
}
|
||||
// Not a key's residual form, which serves only while its own is made.
|
||||
|
||||
@@ -0,0 +1,319 @@
|
||||
//! A compute encoder's bind state, kept between its dispatches: each dispatch that does not take
|
||||
//! what the last one settled (`DispatchMemo`) brings the state the encoder's last such dispatch
|
||||
//! bound to its own in place, and makes its bind shape again only where a change may have changed
|
||||
//! it, as a render encoder's draw does (`live_bind.rs`).
|
||||
//!
|
||||
//! - **Brought in place.** The kernel's slots are derived again from the encoder's table and
|
||||
//! compared with the kept ones (`ArgumentTable::refresh_reading`), at each dispatch whose table
|
||||
//! may bring in another: a table read last through the same pipeline state's interface and
|
||||
//! samplers' maps, that no call changed since (its stamp, `argument_table::Stamp`), whose slots
|
||||
//! that tell the device's view base alignment apart the state still holds as they were read
|
||||
//! (`argument_table::AtBindViews`), and that no rebind or detach of a no-copy buffer may have
|
||||
//! changed (`buffer::host_cells_moved`), is not read again ([`ReadFrom`]; checked whole in debug
|
||||
//! builds). Where only calls changed it since it was so read, and the table keeps the slots they
|
||||
//! changed (`ArgumentTable::refresh_changed`), only those slots are brought in. Every other field
|
||||
//! is computed as before and compared ([`Reshape::field`]); the stage-in region, which no shape
|
||||
//! reads, is stored uncompared. The new value is always stored, so the state is the dispatch's
|
||||
//! exactly. While the same slots stay bound, nothing is allocated.
|
||||
//! - **What the encoder made resident, listed once a change.** The state's used resources are
|
||||
//! listed again (`Residency::used_resources`, `WorkCounts::dispatch_used_resources_listed`) only
|
||||
//! where the residency changed its generation since they were listed (`Residency::generation`:
|
||||
//! each change of what it holds draws a new one), a no-copy buffer moved since (their
|
||||
//! addresses), or an indirect command buffer made resident no longer holds its ID's slot as they
|
||||
//! state (`Residency::holds_slots_as`): what they are a function of, but for those, is the
|
||||
//! residency's contents, which a generation names. Checked whole in debug builds.
|
||||
//! - **The shape found again only where it may have changed.** Where no change brought in may have
|
||||
//! changed the shape, it is the kept one (`WorkCounts::dispatch_shapes_made` and
|
||||
//! `dispatch_shapes_found` stay); after any other change, it is one the pipeline state planned
|
||||
//! for lately where it has it (`ComputePipelineState::planned_shape`, compared in place), else it
|
||||
//! is made again, whole. The pipeline state's plan of a kept shape is found by identity first
|
||||
//! (`ComputePipelineState::planned_for`), so a dispatch whose offsets alone moved compares no
|
||||
//! shape at all, however many resources the encoder made resident.
|
||||
//! - **Kept from one encoder to the next.** An encoder that ends gives its state, with its shape,
|
||||
//! to its device ([`SpareLiveDispatches`]), and the next compute encoder opened starts from it,
|
||||
//! as a dispatch starts from the one before it: exact for the reason a dispatch's is, every field
|
||||
//! of the state a dispatch binds being derived again at each dispatch that settles. The memo of
|
||||
//! what its last dispatch settled goes first ([`LiveDispatch::let_go`]): it holds the pipeline
|
||||
//! state, sampler states and pipelines, where the state holds values alone (its bindings, its used
|
||||
//! resources and argument samplers as a bind state lists them, its table's pipeline state held
|
||||
//! weakly), so a kept one holds nothing alive.
|
||||
|
||||
use metal2vulkan::bind::{BindShape, BindState, HeapPlacement, ImageblockSize};
|
||||
use metal2vulkan::pipeline::Stage;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use super::{DispatchMemo, DispatchTables};
|
||||
use crate::address_map::SamplerMaps;
|
||||
use crate::argument_table::Stamp;
|
||||
use crate::device::lock;
|
||||
use crate::function_table::VisibleFunctionTable;
|
||||
use crate::heap_sets;
|
||||
use crate::live_bind::Reshape;
|
||||
use crate::pipeline::{ComputePipelineState, WeakComputePipelineState};
|
||||
use crate::sampler::SamplerState;
|
||||
use crate::work_counts::Counters;
|
||||
|
||||
/// The most bind states a device keeps for its compute encoders to start from: one per encoder
|
||||
/// open at once beyond it starts empty, as every encoder did before.
|
||||
const SPARE_LIVE_DISPATCHES: usize = 4;
|
||||
|
||||
/// A compute encoder's bind state as its last dispatch that settled bound it, its shape while no
|
||||
/// change may have changed it, and what that dispatch settled (the module docs).
|
||||
#[derive(Default)]
|
||||
pub(in crate::compute) struct LiveDispatch {
|
||||
/// The state. Each dispatch that settles brings it to its own through [`Reshape`]
|
||||
/// ([`refresh`](Self::refresh)).
|
||||
pub(in crate::compute) state: BindState,
|
||||
/// The state's shape, `None` where a change may have changed it.
|
||||
shape: Option<Arc<BindShape>>,
|
||||
/// The count of the device's sampler IDs' slots' changes (`Device::sampler_slots_changed`)
|
||||
/// at which the state's argument samplers are every live sampler state, `None` where they are
|
||||
/// not (none listed yet, or those a variant's set-1 arrays hold alone).
|
||||
listed_at: Option<u64>,
|
||||
/// What the state's kernel table was read from ([`ReadFrom`]).
|
||||
read_from: ReadFrom,
|
||||
/// What the dispatch that last brought the state in place settled, where it is kept for the
|
||||
/// next dispatch of the same inputs (`DispatchMemo`): of this state, so a dispatch that takes
|
||||
/// it binds this state.
|
||||
pub(in crate::compute) memo: DispatchMemo,
|
||||
}
|
||||
|
||||
/// What a kept state's kernel table was read from at the dispatch that read it last
|
||||
/// ([`LiveDispatch::refresh`]): the encoder's table by its stamp, the pipeline state whose
|
||||
/// interface it was read through, the samplers' address-mode maps, and the count of no-copy
|
||||
/// buffers' moves (`buffer::host_cells_moved`); and the generation of the residency the state's
|
||||
/// used resources were listed of (`Residency::generation`). A table that keeps its stamp, read
|
||||
/// through the same state with the same maps while no no-copy buffer moved, brings in what it
|
||||
/// brought in then where the state's slots that tell the view base alignment apart are still those
|
||||
/// it read under (`AtBindViews::holds`): a slot's binding is a function of the object bound there,
|
||||
/// of the table's other entries, of the function's reading of the slot (the interface), of the
|
||||
/// maps, and of those slots.
|
||||
#[derive(Default)]
|
||||
struct ReadFrom {
|
||||
/// The table's stamp.
|
||||
table: Option<Stamp>,
|
||||
/// The pipeline state it was read through, held weakly: while it is held, no other state lies
|
||||
/// where that one lay, so a state it names is the very one.
|
||||
pipeline: Option<WeakComputePipelineState>,
|
||||
/// The maps it was read with.
|
||||
maps: SamplerMaps,
|
||||
/// The count of no-copy buffers' moves as it was read.
|
||||
moved: u64,
|
||||
/// The generation of the residency the state's used resources were listed of, at `moved`
|
||||
/// moves; `None` before any were.
|
||||
resident: Option<u64>,
|
||||
}
|
||||
|
||||
impl ReadFrom {
|
||||
/// Whether the table was read through `pipeline` with `maps` at `moved` moves: then a table
|
||||
/// that keeps its stamp brings in what it brought in.
|
||||
fn reads(&self, pipeline: &ComputePipelineState, maps: &SamplerMaps, moved: u64) -> bool {
|
||||
self.moved == moved
|
||||
&& (self.pipeline.as_ref()).is_some_and(|read| read.names(pipeline))
|
||||
&& self.maps == *maps
|
||||
}
|
||||
}
|
||||
|
||||
impl LiveDispatch {
|
||||
/// Bring the state to the one a dispatch of `tables` binds, in place (the module docs): the
|
||||
/// kernel's table read as the pipeline state's interface reads it (a buffer where the kernel
|
||||
/// reads a visible-function table reads as a null table: `ArgumentTable::refresh_reading`),
|
||||
/// its samplers bound as `maps` binds them, its threadgroup memory lengths, the imageblock size
|
||||
/// for a kernel with an explicit imageblock (one without plans with none, so its shape is the
|
||||
/// one the executor computes for it), the stage-in region, the resources the encoder made
|
||||
/// resident, `samplers` listed (the device's sampler states at `changed` changes of their
|
||||
/// slots, those a variant's set-1 arrays hold alone where `trimmed`), and the live tables the
|
||||
/// pipeline state made. Each change that may change the shape is noted, and the shape kept only
|
||||
/// where none may have.
|
||||
pub(in crate::compute) fn refresh(
|
||||
&mut self,
|
||||
tables: &DispatchTables<'_>,
|
||||
maps: &SamplerMaps,
|
||||
(changed, samplers, trimmed): (u64, &[SamplerState], bool),
|
||||
function_tables: &[VisibleFunctionTable],
|
||||
counts: &Counters,
|
||||
) {
|
||||
let mut reshape = Reshape::default();
|
||||
let pipeline = tables.pipeline;
|
||||
let views = pipeline.at_bind_views();
|
||||
let state = &mut self.state;
|
||||
reshape.field(&mut state.view_base_alignment, views.alignment());
|
||||
let moved = crate::buffer::host_cells_moved();
|
||||
let same = self.read_from.reads(pipeline, maps, moved);
|
||||
// The state's used resources are what the encoder made resident still where the residency
|
||||
// kept its generation since they were listed of it (`Residency::generation`), no no-copy
|
||||
// buffer moved since (their addresses), and each indirect command buffer made resident
|
||||
// holds its ID's slot as they state (`Residency::holds_slots_as`).
|
||||
let residency = tables.residency;
|
||||
let generation = residency.generation();
|
||||
let resident = self.read_from.moved == moved
|
||||
&& self.read_from.resident == Some(generation)
|
||||
&& residency.holds_slots_as(&state.used_resources);
|
||||
let table = tables.table;
|
||||
let read = (Stage::Kernel, pipeline.interface());
|
||||
let kept = state.stages.entry(Stage::Kernel).or_default();
|
||||
// Its slots that tell the view base alignment apart are as the state holds them
|
||||
// (`AtBindViews::holds`): another table's offsets may have left some out since.
|
||||
let views_held = views.holds(Stage::Kernel, kept.at_bind_views);
|
||||
let read_under = self.read_from.table.filter(|_| same && views_held);
|
||||
if read_under == Some(table.stamp()) {
|
||||
#[cfg(debug_assertions)]
|
||||
table.check_reading(kept, read, (maps, views));
|
||||
} else {
|
||||
// Only the slots calls changed since it was read, where the table keeps them.
|
||||
let in_part = read_under.is_some_and(|stamp| {
|
||||
table.refresh_changed((&mut *kept, stamp), read, (maps, views), &mut reshape)
|
||||
});
|
||||
if !in_part {
|
||||
table.refresh_reading(kept, read, (maps, views), &mut reshape);
|
||||
}
|
||||
}
|
||||
reshape.field_from(&mut kept.threadgroup_lengths, tables.threadgroup_lengths);
|
||||
self.read_from.table = Some(table.stamp());
|
||||
if !(self.read_from.pipeline.as_ref()).is_some_and(|read| read.names(pipeline)) {
|
||||
self.read_from.pipeline = Some(pipeline.downgrade());
|
||||
}
|
||||
if self.read_from.maps != *maps {
|
||||
self.read_from.maps = maps.clone();
|
||||
}
|
||||
self.read_from.moved = moved;
|
||||
let imageblock = match pipeline.imageblock_cell_bytes() {
|
||||
Some(_) => tables.imageblock,
|
||||
None => ImageblockSize::default(),
|
||||
};
|
||||
reshape.field(&mut state.imageblock_size, imageblock);
|
||||
// No shape reads it (`BindState::stage_in_region`).
|
||||
state.stage_in_region = tables.stage_in_region;
|
||||
debug_assert!(
|
||||
!resident || state.used_resources == residency.used_resources(),
|
||||
"a dispatch's kept used resources that are not its residency's"
|
||||
);
|
||||
if !resident {
|
||||
counts.dispatch_used_resources_listed();
|
||||
reshape.field(&mut state.used_resources, residency.used_resources());
|
||||
}
|
||||
self.read_from.resident = Some(generation);
|
||||
// Listed again only where the device's sampler states changed since: not where the
|
||||
// listing kept is of every live state at the count of their slots' changes they were taken
|
||||
// at; else they are compared state by state.
|
||||
if trimmed || self.listed_at != Some(changed) {
|
||||
if !heap_sets::lists(&state.argument_samplers, samplers) {
|
||||
reshape.field(&mut state.argument_samplers, heap_sets::listed(samplers));
|
||||
}
|
||||
self.listed_at = (!trimmed).then_some(changed);
|
||||
}
|
||||
let listed = function_tables
|
||||
.iter()
|
||||
.map(VisibleFunctionTable::listed)
|
||||
.collect();
|
||||
reshape.field(&mut state.function_tables, listed);
|
||||
// A value no shape reads (`BindState::heap_placement`).
|
||||
state.heap_placement = HeapPlacement::ResourceId;
|
||||
#[cfg(debug_assertions)]
|
||||
self.check_whole(tables, maps, samplers, function_tables);
|
||||
if reshape.may_reshape() {
|
||||
self.shape = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// That the state brought in place is the one a dispatch of `tables` makes whole
|
||||
/// (`DispatchTables::bind_state` with `maps` applied), field by field (debug builds): the
|
||||
/// kernel's table as `ArgumentTable::check_reading` compares it (a float's sign told apart,
|
||||
/// every NaN alike), and every other field equal.
|
||||
#[cfg(debug_assertions)]
|
||||
fn check_whole(
|
||||
&self,
|
||||
tables: &DispatchTables<'_>,
|
||||
maps: &SamplerMaps,
|
||||
samplers: &[SamplerState],
|
||||
function_tables: &[VisibleFunctionTable],
|
||||
) {
|
||||
let mut whole = tables.bind_state(samplers, function_tables);
|
||||
maps.apply(&mut whole);
|
||||
let state = &self.state;
|
||||
let kept = &state.stages[&Stage::Kernel];
|
||||
let read = (Stage::Kernel, tables.pipeline.interface());
|
||||
let views = tables.pipeline.at_bind_views();
|
||||
tables.table.check_reading(kept, read, (maps, views));
|
||||
const DIFFERS: &str = "a dispatch's bind state brought in place that is not the one made";
|
||||
assert!(
|
||||
kept.threadgroup_lengths == whole.stages[&Stage::Kernel].threadgroup_lengths,
|
||||
"{DIFFERS}: its threadgroup lengths"
|
||||
);
|
||||
assert!(
|
||||
heap_sets::lists(&state.argument_samplers, samplers),
|
||||
"{DIFFERS}: its argument samplers"
|
||||
);
|
||||
// Every other field equal, the table and the listing compared above, as their values
|
||||
// print (a NaN LOD clamp equals none).
|
||||
let mut rest = state.clone();
|
||||
for of in [&mut whole, &mut rest] {
|
||||
of.stages.clear();
|
||||
of.argument_samplers.clear();
|
||||
}
|
||||
assert!(whole == rest, "{DIFFERS}");
|
||||
}
|
||||
|
||||
/// The state's shape: the kept one, else the one `found` finds of the state, else made again
|
||||
/// (counted in `counts`, found or made).
|
||||
pub(in crate::compute) fn shape(
|
||||
&mut self,
|
||||
counts: &Counters,
|
||||
found: impl FnOnce(&BindState) -> Option<Arc<BindShape>>,
|
||||
) -> Arc<BindShape> {
|
||||
if let Some(shape) = &self.shape {
|
||||
debug_assert!(
|
||||
**shape == self.state.shape(),
|
||||
"a kept bind shape that is not its state's"
|
||||
);
|
||||
return Arc::clone(shape);
|
||||
}
|
||||
let shape = match found(&self.state) {
|
||||
Some(shape) => {
|
||||
debug_assert!(
|
||||
*shape == self.state.shape(),
|
||||
"a bind shape found that is not its state's"
|
||||
);
|
||||
counts.dispatch_shape_found();
|
||||
shape
|
||||
}
|
||||
None => {
|
||||
counts.dispatch_shape_made();
|
||||
Arc::new(self.state.shape())
|
||||
}
|
||||
};
|
||||
self.shape = Some(Arc::clone(&shape));
|
||||
shape
|
||||
}
|
||||
|
||||
/// Let go of what its last dispatch settled (`DispatchMemo`), which holds the objects it
|
||||
/// bound: the state alone, which holds values, is kept for the next encoder.
|
||||
pub(in crate::compute) fn let_go(&mut self) {
|
||||
self.memo = DispatchMemo::default();
|
||||
}
|
||||
}
|
||||
|
||||
/// A device's bind states that ended compute encoders left, which the compute encoders it opens
|
||||
/// next start from (the module docs): at most [`SPARE_LIVE_DISPATCHES`], the latest given taken
|
||||
/// first. An encoder holds its own in place, so its open and end allocate none.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct SpareLiveDispatches(Mutex<Vec<LiveDispatch>>);
|
||||
|
||||
impl SpareLiveDispatches {
|
||||
/// The state a compute encoder opening starts from: the latest an encoder gave back, else an
|
||||
/// empty one.
|
||||
pub(in crate::compute) fn take(&self) -> LiveDispatch {
|
||||
lock(&self.0).pop().unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Keep `live`, an ended compute encoder's state, for the next encoder to start from, its
|
||||
/// memo let go ([`LiveDispatch::let_go`]), where fewer than [`SPARE_LIVE_DISPATCHES`] are
|
||||
/// kept; else it goes.
|
||||
pub(in crate::compute) fn give(&self, mut live: LiveDispatch) {
|
||||
live.let_go();
|
||||
let mut spare = lock(&self.0);
|
||||
if spare.len() < SPARE_LIVE_DISPATCHES {
|
||||
spare.push(live);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -491,6 +491,9 @@ pub(super) struct DeviceShared {
|
||||
/// The bind states its ended render encoders left, which the next ones start from
|
||||
/// (`live_bind.rs`).
|
||||
pub(super) live_binds: crate::live_bind::SpareLiveBinds,
|
||||
/// The bind states its ended compute encoders left, which the next ones start from
|
||||
/// (`compute/live.rs`).
|
||||
pub(super) live_dispatches: crate::compute::SpareLiveDispatches,
|
||||
/// The bind memos its ended render encoders left, which the next ones take (`binding.rs`).
|
||||
pub(super) bind_memos: crate::binding::SpareBindMemos,
|
||||
/// The rooms its ended render encoders' draws gathered into, which the next ones take
|
||||
@@ -1962,6 +1965,7 @@ impl Device {
|
||||
sparse,
|
||||
spares: Spares::default(),
|
||||
live_binds: Default::default(),
|
||||
live_dispatches: Default::default(),
|
||||
bind_memos: Default::default(),
|
||||
draw_rooms: Default::default(),
|
||||
kept_rooms: Default::default(),
|
||||
|
||||
@@ -121,6 +121,12 @@ impl Reshape {
|
||||
self.0 = true;
|
||||
}
|
||||
|
||||
/// Whether a change it was told of may have changed the shape: a compute encoder's kept state
|
||||
/// then finds its shape again (`compute/live.rs`).
|
||||
pub(super) fn may_reshape(self) -> bool {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Store `value` in `field`, a field of the kept state beside its stage tables: any change
|
||||
/// may change the shape.
|
||||
pub(super) fn field<T: PartialEq>(&mut self, field: &mut T, value: T) {
|
||||
|
||||
@@ -122,7 +122,7 @@ use crate::resource_ids::{
|
||||
use crate::unique_identifier::UniqueIdentifier;
|
||||
use memo::{Lru, Remembered};
|
||||
use metal2vulkan::bind::{
|
||||
BindShape, BindingPlan, Dispatch, DispatchCommands, DispatchRegion, IndirectRef,
|
||||
BindShape, BindState, BindingPlan, Dispatch, DispatchCommands, DispatchRegion, IndirectRef,
|
||||
};
|
||||
use metal2vulkan::cache::{SpecializationKey, VariantKey};
|
||||
use metal2vulkan::error::{Feature, Needs, RuntimeInput};
|
||||
@@ -134,7 +134,7 @@ use metal2vulkan::pipeline::{
|
||||
};
|
||||
use metal2vulkan::reflection::ComputePipelineReflection;
|
||||
use sources::Sources;
|
||||
use std::collections::{BTreeMap, HashSet};
|
||||
use std::collections::{BTreeMap, HashSet, VecDeque};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex, OnceLock, Weak};
|
||||
|
||||
@@ -198,6 +198,10 @@ const PLANS: usize = 64;
|
||||
const PIPELINES: usize = 32;
|
||||
/// The most variants besides the first a state keeps.
|
||||
const VARIANTS: usize = 8;
|
||||
/// The most plans a state keeps of its latest direct dispatches that planned ([`Planned`]): a
|
||||
/// dispatch of a shape a later one did not push out takes its plan, so encoders of the state whose
|
||||
/// bind states have shapes of their own (other resources made resident) each keep theirs.
|
||||
const PLANNED: usize = 4;
|
||||
|
||||
/// A Metal compute pipeline state (`MTLComputePipelineState`). Cheap to clone (a retain); `Send +
|
||||
/// Sync`.
|
||||
@@ -213,6 +217,12 @@ impl WeakComputePipelineState {
|
||||
pub(crate) fn upgrade(&self) -> Option<ComputePipelineState> {
|
||||
self.0.upgrade().map(ComputePipelineState)
|
||||
}
|
||||
|
||||
/// Whether it names `state`: the very state, not one alike. While it is held, no other state
|
||||
/// lies where it lay, even once the program let it go.
|
||||
pub(crate) fn names(&self, state: &ComputePipelineState) -> bool {
|
||||
std::ptr::eq(Weak::as_ptr(&self.0), Arc::as_ptr(&state.0))
|
||||
}
|
||||
}
|
||||
|
||||
/// What a compute state runs, held without its ID: an indirect command keeps the program of the
|
||||
@@ -319,8 +329,9 @@ pub(super) struct Program {
|
||||
/// Its buffer slots whose bind offset class tells the device's view base alignment apart
|
||||
/// (`argument_table::AtBindViews`), of its base's interface.
|
||||
at_bind_views: AtBindViews,
|
||||
/// What its last direct dispatch that planned planned ([`Planned`]).
|
||||
planned: Mutex<Option<Arc<Planned>>>,
|
||||
/// What its latest direct dispatches that planned planned ([`Planned`]), the latest used
|
||||
/// first, [`PLANNED`] at most.
|
||||
planned: Mutex<VecDeque<Arc<Planned>>>,
|
||||
}
|
||||
|
||||
/// What a direct dispatch of a compute state planned (`compute::settle_dispatch`): the variant
|
||||
@@ -328,7 +339,8 @@ pub(super) struct Program {
|
||||
/// [`ComputePipelineState::settle`] answers them, a function of those three alone; and the
|
||||
/// `VkPipeline`s of its regions of those keys, where all were found made. A later dispatch of the
|
||||
/// same dispatch and forms whose bind state has the shape takes them, from any encoder, rather
|
||||
/// than settling them again (`WorkCounts::dispatches_planned_as_before`).
|
||||
/// than settling them again (`WorkCounts::dispatches_planned_as_before`), while the state keeps
|
||||
/// them among its latest [`PLANNED`] ([`ComputePipelineState::planned_for`]).
|
||||
pub(crate) struct Planned {
|
||||
pub(crate) shape: Arc<BindShape>,
|
||||
pub(crate) dispatch: Dispatch,
|
||||
@@ -340,6 +352,13 @@ pub(crate) struct Planned {
|
||||
pub(crate) made: OnceLock<(Vec<RegionKey>, Vec<Arc<RawPipeline>>)>,
|
||||
}
|
||||
|
||||
/// The entry at `at` of a state's kept plans ([`Planned`]), moved first as the one used last.
|
||||
fn used(planned: &mut VecDeque<Arc<Planned>>, at: usize) -> Arc<Planned> {
|
||||
let entry = planned.remove(at).expect("a kept plan's position");
|
||||
planned.push_front(Arc::clone(&entry));
|
||||
entry
|
||||
}
|
||||
|
||||
impl Planned {
|
||||
/// The made `VkPipeline`s of `commands`' regions, where they are as many, each of the key of
|
||||
/// one found made.
|
||||
@@ -850,7 +869,7 @@ impl ComputePipelineState {
|
||||
label: options.label.clone(),
|
||||
pieces,
|
||||
at_bind_views,
|
||||
planned: Mutex::new(None),
|
||||
planned: Mutex::new(VecDeque::with_capacity(PLANNED)),
|
||||
};
|
||||
StateShared {
|
||||
id,
|
||||
@@ -1289,14 +1308,63 @@ impl ComputePipelineState {
|
||||
ComputeProgram(Arc::clone(&self.0.program))
|
||||
}
|
||||
|
||||
/// What its last direct dispatch that planned planned ([`Planned`]).
|
||||
pub(super) fn planned(&self) -> Option<Arc<Planned>> {
|
||||
lock(&self.0.planned).clone()
|
||||
/// The shape of `state` among those of what its latest direct dispatches planned
|
||||
/// ([`Planned`]), compared with the state in place (`BindState::position_of_shape`, which makes
|
||||
/// the shape of the resources it lists at most once for all of them): what a dispatch of
|
||||
/// `state` plans for where it has one, found without its shape made (`compute/live.rs`).
|
||||
pub(super) fn planned_shape(&self, state: &BindState) -> Option<Arc<BindShape>> {
|
||||
let planned = lock(&self.0.planned);
|
||||
let at = state.position_of_shape(planned.iter().map(|planned| &*planned.shape))?;
|
||||
Some(Arc::clone(&planned[at].shape))
|
||||
}
|
||||
|
||||
/// Keep `planned`, a direct dispatch's, in place of what the last planned.
|
||||
/// What its latest direct dispatches planned for a dispatch of `dispatch` in `forms` of a bind
|
||||
/// state of shape `shape` ([`Planned`]): the very shape looked for first (its encoder's kept
|
||||
/// one, `compute/live.rs`, or one [`planned_shape`](Self::planned_shape) found here), else an
|
||||
/// equal one compared whole. Used now, so it is the last to go.
|
||||
pub(super) fn planned_for(
|
||||
&self,
|
||||
shape: &Arc<BindShape>,
|
||||
dispatch: &Dispatch,
|
||||
forms: BufferForms,
|
||||
) -> Option<Arc<Planned>> {
|
||||
let mut planned = lock(&self.0.planned);
|
||||
let of = |p: &Arc<Planned>| p.dispatch == *dispatch && p.forms == forms;
|
||||
let at = (planned.iter())
|
||||
.position(|p| of(p) && Arc::ptr_eq(&p.shape, shape))
|
||||
.or_else(|| (planned.iter()).position(|p| of(p) && p.shape == *shape))?;
|
||||
Some(used(&mut planned, at))
|
||||
}
|
||||
|
||||
/// What its latest direct dispatches planned for a dispatch of `dispatch` in `forms` of
|
||||
/// `state`, compared with the state in place ([`planned_shape`](Self::planned_shape)), for a
|
||||
/// dispatch that keeps no state of its own (an indirect command's, a host step's). Used now, so
|
||||
/// it is the last to go.
|
||||
pub(super) fn planned_of(
|
||||
&self,
|
||||
state: &BindState,
|
||||
dispatch: &Dispatch,
|
||||
forms: BufferForms,
|
||||
) -> Option<Arc<Planned>> {
|
||||
let mut planned = lock(&self.0.planned);
|
||||
let of = (planned.iter()).filter(|p| p.dispatch == *dispatch && p.forms == forms);
|
||||
let found = state.position_of_shape(of.map(|p| &*p.shape))?;
|
||||
let at = (planned.iter())
|
||||
.enumerate()
|
||||
.filter(|(_, p)| p.dispatch == *dispatch && p.forms == forms)
|
||||
.nth(found)
|
||||
.map(|(at, _)| at)?;
|
||||
Some(used(&mut planned, at))
|
||||
}
|
||||
|
||||
/// Keep `planned`, a direct dispatch's, first among what its latest direct dispatches planned,
|
||||
/// in place of the one used least lately where [`PLANNED`] are kept.
|
||||
pub(super) fn keep_planned(&self, planned: Arc<Planned>) {
|
||||
*lock(&self.0.planned) = Some(planned);
|
||||
let mut kept = lock(&self.0.planned);
|
||||
if kept.len() == PLANNED {
|
||||
kept.pop_back();
|
||||
}
|
||||
kept.push_front(planned);
|
||||
}
|
||||
|
||||
/// The variant `dispatch` of a bind state of `shape` runs on in `forms`, and its plan for
|
||||
|
||||
@@ -65,12 +65,13 @@
|
||||
//! `n` lookups, whatever the encoder made resident before.
|
||||
//! - **Generations.** Each change of what a residency holds (a resource made resident, a usage or
|
||||
//! stage widened, a heap used or widened, a live table given or taken) gives it a generation no
|
||||
//! other residency of the process had ([`Residency::generation`]): a compute encoder's dispatch
|
||||
//! that finds the generation, and every other input, its last dispatch settled with takes what
|
||||
//! that one settled (`compute.rs`, `DispatchMemo`), and its residency's share of the bind state
|
||||
//! (the used resources, their shape and the plan's commands) is made once per change. What a
|
||||
//! used resource states besides (a buffer's addresses, an indirect command buffer's hold on
|
||||
//! its ID's slot) changes outside the residency: the memo keys on those apart.
|
||||
//! other residency of the process had ([`Residency::generation`]). An encoder lists its
|
||||
//! residency's share of the bind state (the used resources) again only when the generation, or
|
||||
//! what a used resource states besides (a buffer's addresses, an indirect command buffer's hold
|
||||
//! on its ID's slot), changed since its last draw or dispatch listed it (`live_bind.rs`,
|
||||
//! `compute/live.rs`): so that share is listed once per change, whatever else a command
|
||||
//! changes. A compute dispatch that finds the generation, and every other input, its last
|
||||
//! dispatch settled with takes what that one settled (`compute.rs`, `DispatchMemo`).
|
||||
//! - **Stages.** A render encoder makes a resource resident for the render stages it names
|
||||
//! (`useResource:usage:stages:`, `useHeap:stages:`), joined as the usages are: a draw's
|
||||
//! functions reach it in those of their stages alone (the binder's `UsedResource::stages`), and
|
||||
|
||||
@@ -231,6 +231,9 @@ pub(crate) struct Counters {
|
||||
stand_in_copies_made_at_draws: AtomicU64,
|
||||
draw_shapes_made: AtomicU64,
|
||||
draw_shapes_found: AtomicU64,
|
||||
dispatch_shapes_made: AtomicU64,
|
||||
dispatch_shapes_found: AtomicU64,
|
||||
dispatch_used_resources_listed: AtomicU64,
|
||||
argument_samplers_taken: AtomicU64,
|
||||
bind_views_worked_out: AtomicU64,
|
||||
stage_tables_read: AtomicU64,
|
||||
@@ -1663,6 +1666,25 @@ impl Counters {
|
||||
self.draw_shapes_found.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// A compute dispatch made the bind shape of its encoder's kept bind state
|
||||
/// (`LiveDispatch::shape`).
|
||||
pub(crate) fn dispatch_shape_made(&self) {
|
||||
self.dispatch_shapes_made.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// A compute dispatch found the bind shape of its encoder's kept bind state among those its
|
||||
/// pipeline state planned for lately (`LiveDispatch::shape`).
|
||||
pub(crate) fn dispatch_shape_found(&self) {
|
||||
self.dispatch_shapes_found.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// A compute dispatch listed what its encoder made resident as its kept bind state's used
|
||||
/// resources (`LiveDispatch::refresh`).
|
||||
pub(crate) fn dispatch_used_resources_listed(&self) {
|
||||
self.dispatch_used_resources_listed
|
||||
.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// A draw took the device's live sampler states to list (`sampler::ArgumentSamplers`).
|
||||
pub(crate) fn argument_samplers_taken(&self) {
|
||||
self.argument_samplers_taken.fetch_add(1, Ordering::Relaxed);
|
||||
@@ -4179,6 +4201,22 @@ pub struct WorkCounts {
|
||||
/// other bind state's shape is its encoder's last draw's (values no shape reads, such as the
|
||||
/// depth/stencil state, the stencil references or the blend color, may differ).
|
||||
pub draw_shapes_found: u64,
|
||||
/// Bind shapes compute encoders' dispatches made of their encoder's kept bind state
|
||||
/// (`compute/live.rs`): none for a dispatch that took what its encoder's last dispatch settled
|
||||
/// (`dispatches_settled_as_before`), nor for one whose bound slots keep their shapes and whose
|
||||
/// other bind state's shape is its encoder's last dispatch's that settled (an offset moved).
|
||||
pub dispatch_shapes_made: u64,
|
||||
/// Bind shapes compute dispatches found among those their pipeline states planned for lately,
|
||||
/// compared with their bind states in place: none for a dispatch as `dispatch_shapes_made`
|
||||
/// says.
|
||||
pub dispatch_shapes_found: u64,
|
||||
/// Compute dispatches that listed what their encoder made resident as their bind state's used
|
||||
/// resources (`Residency::used_resources`): one at an encoder's first dispatch that settles
|
||||
/// after each change of what it made resident, after a no-copy buffer moved, or after an
|
||||
/// indirect command buffer made resident lost its ID's slot; none for a dispatch whose
|
||||
/// encoder's residency kept its generation since its kept bind state listed them
|
||||
/// (`compute/live.rs`), however many resources it made resident.
|
||||
pub dispatch_used_resources_listed: u64,
|
||||
/// Draws (and commands of other encoders that settle as draws do) that took the device's
|
||||
/// live sampler states to list as their argument samplers: none for a draw whose kept bind
|
||||
/// state lists them at the count of their slots' changes that still holds, whose variant has
|
||||
@@ -5567,6 +5605,10 @@ impl Device {
|
||||
.load(Ordering::Relaxed),
|
||||
draw_shapes_made: counters.draw_shapes_made.load(Ordering::Relaxed),
|
||||
draw_shapes_found: counters.draw_shapes_found.load(Ordering::Relaxed),
|
||||
dispatch_shapes_made: counters.dispatch_shapes_made.load(Ordering::Relaxed),
|
||||
dispatch_shapes_found: counters.dispatch_shapes_found.load(Ordering::Relaxed),
|
||||
dispatch_used_resources_listed: (counters.dispatch_used_resources_listed)
|
||||
.load(Ordering::Relaxed),
|
||||
argument_samplers_taken: counters.argument_samplers_taken.load(Ordering::Relaxed),
|
||||
bind_views_worked_out: counters.bind_views_worked_out.load(Ordering::Relaxed),
|
||||
stage_tables_read: counters.stage_tables_read.load(Ordering::Relaxed),
|
||||
|
||||
@@ -414,11 +414,13 @@ struct Planning {
|
||||
settled: bool,
|
||||
}
|
||||
|
||||
/// A dispatch whose bind state has the shape its pipeline state's last direct dispatch planned,
|
||||
/// of the same grid, takes that one's variant, plan and made pipelines: one whose buffer's offset
|
||||
/// alone moved, and another encoder's first dispatch of the state. A grid or a resource made
|
||||
/// resident plans again, and the next dispatch of the new shape takes that. Every dispatch writes
|
||||
/// its words. Both ways ([`on_device_both_ways`]).
|
||||
/// A dispatch whose bind state has the shape one of its pipeline state's latest direct dispatches
|
||||
/// planned, of the same grid, takes that one's variant, plan and made pipelines: one whose buffer's
|
||||
/// offset alone moved, and another encoder's first dispatch of the state. A grid or a resource
|
||||
/// made resident plans again, and the next dispatch of the new shape takes that; and an encoder of
|
||||
/// the first shape again, after one of the other planned, takes what that first shape planned, which
|
||||
/// the state keeps beside the other (`pipeline::Planned`), where it kept one alone before. Every
|
||||
/// dispatch writes its words. Both ways ([`on_device_both_ways`]).
|
||||
#[test]
|
||||
fn dispatches_take_what_their_state_planned_for_the_same_shape() {
|
||||
on_device_both_ways(|device| {
|
||||
@@ -427,7 +429,7 @@ fn dispatches_take_what_their_state_planned_for_the_same_shape() {
|
||||
.flat_map(u32::to_le_bytes)
|
||||
.collect();
|
||||
let input = buffer_with(device, &input).unwrap();
|
||||
const WORDS: u64 = 12 * THREADS;
|
||||
const WORDS: u64 = 14 * THREADS;
|
||||
let out = buffer_with(device, &vec![0; (4 * WORDS) as usize]).unwrap();
|
||||
let queue = device.new_command_queue().unwrap();
|
||||
let command_buffer = queue.command_buffer().unwrap();
|
||||
@@ -466,6 +468,12 @@ fn dispatches_take_what_their_state_planned_for_the_same_shape() {
|
||||
),
|
||||
step("nothing", 10 * THREADS, 2 * THREADS, (false, true)),
|
||||
],
|
||||
vec![step(
|
||||
"an encoder of the first shape",
|
||||
12 * THREADS,
|
||||
2 * THREADS,
|
||||
(true, false),
|
||||
)],
|
||||
];
|
||||
let mut seen = Vec::new();
|
||||
for steps in &encoders {
|
||||
@@ -522,3 +530,89 @@ fn dispatches_take_what_their_state_planned_for_the_same_shape() {
|
||||
assert_eq!(words, expected);
|
||||
});
|
||||
}
|
||||
|
||||
/// The buffers [`dispatches_list_what_their_encoder_made_resident_once_a_change`] makes resident.
|
||||
const RESIDENT: usize = 64;
|
||||
|
||||
/// A compute encoder's dispatches bring the bind state its last dispatch bound in place: where only
|
||||
/// a buffer's offset moved between them, after the encoder made [`RESIDENT`] buffers resident, the
|
||||
/// first lists them as its bind state's used resources and finds or makes its shape, and none after
|
||||
/// it does either (`WorkCounts::dispatch_used_resources_listed`, `dispatch_shapes_made` and
|
||||
/// `dispatch_shapes_found`), each taking what its pipeline state planned for that shape
|
||||
/// (`dispatches_planned_as_before`); a buffer made resident lists them again at the next dispatch,
|
||||
/// which finds or makes its shape again, and the dispatch after it neither. Every dispatch writes
|
||||
/// its words. Both ways ([`on_device_both_ways`]).
|
||||
#[test]
|
||||
fn dispatches_list_what_their_encoder_made_resident_once_a_change() {
|
||||
const MOVES: u64 = 6;
|
||||
on_device_both_ways(|device| {
|
||||
let kernel = state(device, "a4_index_word", false);
|
||||
let input: Vec<u8> = (0..3 * THREADS as u32).flat_map(u32::to_le_bytes).collect();
|
||||
let input = buffer_with(device, &input).unwrap();
|
||||
let words = THREADS * (MOVES + 2);
|
||||
let out = buffer_with(device, &vec![0; (4 * words) as usize]).unwrap();
|
||||
let resident: Vec<_> = (0..=RESIDENT)
|
||||
.map(|_| buffer_with(device, &[0; 64]).unwrap())
|
||||
.collect();
|
||||
let queue = device.new_command_queue().unwrap();
|
||||
let command_buffer = queue.command_buffer().unwrap();
|
||||
let mut encoder = command_buffer.compute_command_encoder().unwrap();
|
||||
encoder.set_compute_pipeline_state(&kernel).unwrap();
|
||||
encoder.set_buffer(Some(&input), 0, 0).unwrap();
|
||||
let first: Vec<_> = resident[..RESIDENT]
|
||||
.iter()
|
||||
.map(ResourceRef::Buffer)
|
||||
.collect();
|
||||
encoder.use_resources(&first, ResourceUsage::READ).unwrap();
|
||||
// Per dispatch: what it changed, and whether it listed the resources, found or made its
|
||||
// shape, and took what its state planned.
|
||||
let mut seen = Vec::new();
|
||||
for at in 0..MOVES + 2 {
|
||||
let change = match at {
|
||||
0 => "the first",
|
||||
MOVES => "a buffer made resident",
|
||||
_ => "out's offset",
|
||||
};
|
||||
if at == MOVES {
|
||||
let last = ResourceRef::Buffer(&resident[RESIDENT]);
|
||||
encoder.use_resource(last, ResourceUsage::READ).unwrap();
|
||||
}
|
||||
encoder.set_buffer(Some(&out), 4 * THREADS * at, 1).unwrap();
|
||||
let before = device.work_counts();
|
||||
encoder
|
||||
.dispatch_threads(Size::new(THREADS, 1, 1), Size::new(THREADS, 1, 1))
|
||||
.unwrap();
|
||||
let after = device.work_counts();
|
||||
let listed = grew(&before, &after, |c| c.dispatch_used_resources_listed);
|
||||
let shaped = grew(&before, &after, |c| c.dispatch_shapes_made)
|
||||
+ grew(&before, &after, |c| c.dispatch_shapes_found);
|
||||
let planned = grew(&before, &after, |c| c.dispatches_planned_as_before);
|
||||
seen.push((change, listed, shaped, planned));
|
||||
}
|
||||
encoder.end_encoding().unwrap();
|
||||
command_buffer.commit().unwrap();
|
||||
command_buffer.wait_until_completed_for(WAIT).unwrap();
|
||||
assert_eq!(command_buffer.status(), CommandBufferStatus::Completed);
|
||||
// The first of each residency lists it and shapes its state; whether it then plans is the
|
||||
// pipeline state's (none of the first shape planned yet; the second's neither).
|
||||
let want: Vec<_> = (0..MOVES + 2)
|
||||
.map(|at| match at {
|
||||
0 => ("the first", 1, 1, 0),
|
||||
MOVES => ("a buffer made resident", 1, 1, 0),
|
||||
_ => ("out's offset", 0, 0, 1),
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(seen, want);
|
||||
let words: Vec<u32> = read_buffer(&out, 0, 4 * words)
|
||||
.unwrap()
|
||||
.as_chunks::<4>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|word| u32::from_le_bytes(*word))
|
||||
.collect();
|
||||
let want: Vec<u32> = (0..MOVES + 2)
|
||||
.flat_map(|_| (0..THREADS as u32).map(|t| 3 * t + 1))
|
||||
.collect();
|
||||
assert_eq!(words, want);
|
||||
});
|
||||
}
|
||||
@@ -2582,6 +2582,9 @@ field metal2vulkan-runtime::WorkCounts.dirty_wait_nanos pub dirty_wait_nanos: u6
|
||||
field metal2vulkan-runtime::WorkCounts.dirty_waits pub dirty_waits: u64
|
||||
field metal2vulkan-runtime::WorkCounts.dirty_waits_unanswered pub dirty_waits_unanswered: u64
|
||||
field metal2vulkan-runtime::WorkCounts.dispatch_pipelines_pooled pub dispatch_pipelines_pooled: u64
|
||||
field metal2vulkan-runtime::WorkCounts.dispatch_shapes_found pub dispatch_shapes_found: u64
|
||||
field metal2vulkan-runtime::WorkCounts.dispatch_shapes_made pub dispatch_shapes_made: u64
|
||||
field metal2vulkan-runtime::WorkCounts.dispatch_used_resources_listed pub dispatch_used_resources_listed: u64
|
||||
field metal2vulkan-runtime::WorkCounts.dispatches_dropped_still_making pub dispatches_dropped_still_making: u64
|
||||
field metal2vulkan-runtime::WorkCounts.dispatches_planned_as_before pub dispatches_planned_as_before: u64
|
||||
field metal2vulkan-runtime::WorkCounts.dispatches_pushing_set_zero pub dispatches_pushing_set_zero: u64
|
||||
|
||||
|
Can't render this file because it is too large.
|
Reference in new issue
Block a user