commit
1ae8f260d8
4 files changed
+1199
-103
No files matched your search
@@ -117,7 +117,9 @@ const WINDOW_MOST: u64 = 64;
|
|||||||
/// apart after it (16, then twice as many each time, [`WINDOW_MOST`] at
|
/// apart after it (16, then twice as many each time, [`WINDOW_MOST`] at
|
||||||
/// most, never past the buffer) into
|
/// most, never past the buffer) into
|
||||||
/// the list's scratch buffer in one pass, and the draws that follow read
|
/// the list's scratch buffer in one pass, and the draws that follow read
|
||||||
/// their record there with no pass of ours, until one of these: a compute
|
/// their record there with no pass of ours and no barrier (the scratch
|
||||||
|
/// buffer stays readable as arguments until the next clamp), until one of
|
||||||
|
/// these: a compute
|
||||||
/// dispatch, a direct draw, an indirect call with a count buffer or more
|
/// dispatch, a direct draw, an indirect call with a count buffer or more
|
||||||
/// than one record, a draw whose shaders can store to buffers, a record
|
/// than one record, a draw whose shaders can store to buffers, a record
|
||||||
/// outside the window, another host list.
|
/// outside the window, another host list.
|
||||||
@@ -2512,7 +2514,6 @@ impl State {
|
|||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
if !ran {
|
if !ran {
|
||||||
crate::indirect::done(list, &bounded);
|
|
||||||
why("prepare failed");
|
why("prepare failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -2538,7 +2539,9 @@ impl State {
|
|||||||
if kind == MESH {
|
if kind == MESH {
|
||||||
self.mesh_scissor(list, false);
|
self.mesh_scissor(list, false);
|
||||||
}
|
}
|
||||||
crate::indirect::done(list, &bounded);
|
// The scratch buffer stays readable as arguments: the next draw of
|
||||||
|
// the window reads it with no barrier, and the next clamp moves it
|
||||||
|
// back (`crate::indirect`).
|
||||||
if kind == DISPATCH {
|
if kind == DISPATCH {
|
||||||
DISPATCHED.fetch_add(1, Ordering::Relaxed);
|
DISPATCHED.fetch_add(1, Ordering::Relaxed);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -789,8 +789,13 @@ pub(crate) struct HostList {
|
|||||||
/// The pipelines the list binds, for reports of slow submissions.
|
/// The pipelines the list binds, for reports of slow submissions.
|
||||||
pipelines: Mutex<std::collections::BTreeSet<String>>,
|
pipelines: Mutex<std::collections::BTreeSet<String>>,
|
||||||
/// The work its indirect executions may still do (`indirect`): a
|
/// The work its indirect executions may still do (`indirect`): a
|
||||||
/// shader-writable word, zeroed when the list is submitted.
|
/// shader-writable word, zeroed when the list is submitted, and after
|
||||||
|
/// it what the clamp notes about mesh records. In memory the CPU maps.
|
||||||
budget: OnceLock<Option<(ID3D12Resource, usize)>>,
|
budget: OnceLock<Option<(ID3D12Resource, usize)>>,
|
||||||
|
/// The same words in the card's own memory ([`card_budget`]), which the
|
||||||
|
/// clamp uses instead when there is one: zeroed in the list before its
|
||||||
|
/// first clamp, its notes copied to `budget` when the list closes.
|
||||||
|
card_budget: OnceLock<Option<ID3D12Resource>>,
|
||||||
/// Device-local scratch buffer for clamped indirect arguments (`indirect`),
|
/// Device-local scratch buffer for clamped indirect arguments (`indirect`),
|
||||||
/// grown to the largest indirect execution recorded on the list.
|
/// grown to the largest indirect execution recorded on the list.
|
||||||
indirect_args: Mutex<Option<(ID3D12Resource, u64)>>,
|
indirect_args: Mutex<Option<(ID3D12Resource, u64)>>,
|
||||||
@@ -1270,6 +1275,16 @@ pub(crate) fn dred_on() -> bool {
|
|||||||
/// budget buffers, a run late).
|
/// budget buffers, a run late).
|
||||||
static MESH_GROUPS: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2];
|
static MESH_GROUPS: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2];
|
||||||
|
|
||||||
|
/// Whether a list's indirect-work budget lives in the card's own memory
|
||||||
|
/// (`VICTORY_INDIRECT_CARD_BUDGET=0` keeps it in memory the CPU maps, as
|
||||||
|
/// before). The clamp charges it with atomics, at least two in a row for
|
||||||
|
/// every clamp; in mapped memory on a card of its own each of them
|
||||||
|
/// crosses the bus.
|
||||||
|
fn card_budget() -> bool {
|
||||||
|
static ON: OnceLock<bool> = OnceLock::new();
|
||||||
|
*ON.get_or_init(|| std::env::var_os("VICTORY_INDIRECT_CARD_BUDGET").is_none_or(|v| v != "0"))
|
||||||
|
}
|
||||||
|
|
||||||
/// Whether each list of a submission runs and is timed on its own, slow
|
/// Whether each list of a submission runs and is timed on its own, slow
|
||||||
/// ones logged with their commands (`VICTORY_GPU_TIMING`, diagnostics
|
/// ones logged with their commands (`VICTORY_GPU_TIMING`, diagnostics
|
||||||
/// builds; the commands are noted with `VICTORY_D3D12_DRED` on).
|
/// builds; the commands are noted with `VICTORY_D3D12_DRED` on).
|
||||||
@@ -1655,8 +1670,14 @@ struct Deferred {
|
|||||||
/// memory they go to, the bytes of a row, the rows, the title's row
|
/// memory they go to, the bytes of a row, the rows, the title's row
|
||||||
/// pitch.
|
/// pitch.
|
||||||
backs: Vec<(ID3D12Resource, usize, [usize; 3])>,
|
backs: Vec<(ID3D12Resource, usize, [usize; 3])>,
|
||||||
/// Whether a step zeroes the indirect-work budget.
|
/// Whether the indirect-work budget is zeroed for this recording (a
|
||||||
|
/// step on the CPU, or in the list for the card's copy).
|
||||||
budget_zeroed: bool,
|
budget_zeroed: bool,
|
||||||
|
/// The indirect scratch buffer (`HostList::indirect_args`, by its
|
||||||
|
/// interface pointer) that a clamp in this recording left readable as
|
||||||
|
/// indirect arguments; 0 if none. It stays readable until the next
|
||||||
|
/// clamp into it ([`HostList::indirect_state`]).
|
||||||
|
indirect_readable: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deferred {
|
impl Deferred {
|
||||||
@@ -2994,6 +3015,7 @@ impl Host {
|
|||||||
prelude,
|
prelude,
|
||||||
pipelines: Mutex::default(),
|
pipelines: Mutex::default(),
|
||||||
budget: OnceLock::new(),
|
budget: OnceLock::new(),
|
||||||
|
card_budget: OnceLock::new(),
|
||||||
indirect_args: Mutex::default(),
|
indirect_args: Mutex::default(),
|
||||||
_alive: Counted::new(),
|
_alive: Counted::new(),
|
||||||
#[cfg(feature = "diagnostics")]
|
#[cfg(feature = "diagnostics")]
|
||||||
@@ -4607,6 +4629,11 @@ impl HostList {
|
|||||||
|
|
||||||
pub(crate) fn close(&self) {
|
pub(crate) fn close(&self) {
|
||||||
self.twins_to_depth();
|
self.twins_to_depth();
|
||||||
|
let scratch = self.indirect_args.lock().unwrap_or_else(|e| e.into_inner()).clone();
|
||||||
|
if let Some((scratch, _)) = scratch {
|
||||||
|
self.indirect_common(&scratch);
|
||||||
|
}
|
||||||
|
self.card_budget_out();
|
||||||
#[cfg(feature = "diagnostics")]
|
#[cfg(feature = "diagnostics")]
|
||||||
if let Some((heap, back)) = self.stamps() {
|
if let Some((heap, back)) = self.stamps() {
|
||||||
let count = (self.notes.lock().unwrap_or_else(|e| e.into_inner()).len() as u32).min(STAMPS - 1);
|
let count = (self.notes.lock().unwrap_or_else(|e| e.into_inner()).len() as u32).min(STAMPS - 1);
|
||||||
@@ -5190,6 +5217,7 @@ impl HostList {
|
|||||||
Some((b, d as usize))
|
Some((b, d as usize))
|
||||||
})
|
})
|
||||||
.as_ref()?;
|
.as_ref()?;
|
||||||
|
let card = self.card_budget.get_or_init(|| self.make_card_budget()).as_ref();
|
||||||
let mut deferred = self.deferred();
|
let mut deferred = self.deferred();
|
||||||
if !deferred.budget_zeroed {
|
if !deferred.budget_zeroed {
|
||||||
deferred.budget_zeroed = true;
|
deferred.budget_zeroed = true;
|
||||||
@@ -5197,7 +5225,8 @@ impl HostList {
|
|||||||
// SAFETY: the list's own mapped words, written before it runs.
|
// SAFETY: the list's own mapped words, written before it runs.
|
||||||
// The words after the budget hold what the clamp noted about the
|
// The words after the budget hold what the clamp noted about the
|
||||||
// list's last run (mesh dispatches: the largest group count
|
// list's last run (mesh dispatches: the largest group count
|
||||||
// asked for, the groups asked in all): taken, then zeroed.
|
// asked for, the groups asked in all; with the card's copy, as
|
||||||
|
// the list's end copied them here): taken, then zeroed.
|
||||||
deferred.copies.push(Step::Host(Box::new(move || unsafe {
|
deferred.copies.push(Step::Host(Box::new(move || unsafe {
|
||||||
let words = at as *mut u32;
|
let words = at as *mut u32;
|
||||||
let (largest, all) = (words.add(1).read_volatile(), words.add(2).read_volatile());
|
let (largest, all) = (words.add(1).read_volatile(), words.add(2).read_volatile());
|
||||||
@@ -5207,9 +5236,84 @@ impl HostList {
|
|||||||
words.add(k).write_volatile(0);
|
words.add(k).write_volatile(0);
|
||||||
}
|
}
|
||||||
})));
|
})));
|
||||||
|
drop(deferred);
|
||||||
|
if let Some(card) = card {
|
||||||
|
self.zero_card_budget(card);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// SAFETY: a live buffer.
|
// SAFETY: a live buffer.
|
||||||
Some(unsafe { buffer.GetGPUVirtualAddress() })
|
Some(unsafe { card.unwrap_or(buffer).GetGPUVirtualAddress() })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The budget's words in the card's own memory ([`card_budget`]), if
|
||||||
|
/// the list can zero them itself (`WriteBufferImmediate`).
|
||||||
|
fn make_card_budget(&self) -> Option<ID3D12Resource> {
|
||||||
|
if !card_budget() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
self.list.cast::<ID3D12GraphicsCommandList2>().ok()?;
|
||||||
|
let buffer = host()?.uav_buffer(256)?;
|
||||||
|
name(&buffer, || "indirect budget".to_owned());
|
||||||
|
Some(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Records, before the list's first clamp, the zeroing of the card's
|
||||||
|
/// copy of the budget: common, written as a copy, then left for
|
||||||
|
/// unordered access until the list closes ([`HostList::card_budget_out`]).
|
||||||
|
fn zero_card_budget(&self, card: &ID3D12Resource) {
|
||||||
|
let Ok(list2) = self.list.cast::<ID3D12GraphicsCommandList2>() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
// SAFETY: a live buffer.
|
||||||
|
let at = unsafe { card.GetGPUVirtualAddress() };
|
||||||
|
let zeros = [0u64, 4, 8].map(|offset| D3D12_WRITEBUFFERIMMEDIATE_PARAMETER {
|
||||||
|
Dest: at + offset,
|
||||||
|
Value: 0,
|
||||||
|
});
|
||||||
|
// SAFETY: an open list; the buffer is the list's own and in the
|
||||||
|
// common state at the start of every recording.
|
||||||
|
unsafe {
|
||||||
|
self.list.ResourceBarrier(&[transition(
|
||||||
|
card,
|
||||||
|
D3D12_RESOURCE_STATE_COMMON,
|
||||||
|
D3D12_RESOURCE_STATE_COPY_DEST,
|
||||||
|
)]);
|
||||||
|
list2.WriteBufferImmediate(zeros.len() as u32, zeros.as_ptr(), None);
|
||||||
|
self.list.ResourceBarrier(&[transition(
|
||||||
|
card,
|
||||||
|
D3D12_RESOURCE_STATE_COPY_DEST,
|
||||||
|
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
|
||||||
|
)]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Before the list closes, if this recording charged the card's copy
|
||||||
|
/// of the budget: its notes on mesh records go to the mapped words
|
||||||
|
/// (read at the next submission), and it goes back to common.
|
||||||
|
fn card_budget_out(&self) {
|
||||||
|
if !self.deferred().budget_zeroed {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let (Some(Some(card)), Some(Some((mapped, _)))) =
|
||||||
|
(self.card_budget.get(), self.budget.get())
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
// SAFETY: an open list; both buffers are the list's own. The
|
||||||
|
// mapped one is promoted from common to the copy destination.
|
||||||
|
unsafe {
|
||||||
|
self.list.ResourceBarrier(&[transition(
|
||||||
|
card,
|
||||||
|
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
|
||||||
|
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
||||||
|
)]);
|
||||||
|
self.list.CopyBufferRegion(mapped, 4, card, 4, 8);
|
||||||
|
self.list.ResourceBarrier(&[transition(
|
||||||
|
card,
|
||||||
|
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
||||||
|
D3D12_RESOURCE_STATE_COMMON,
|
||||||
|
)]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A device-local unordered-access buffer of at least `size` bytes for
|
/// A device-local unordered-access buffer of at least `size` bytes for
|
||||||
@@ -5234,11 +5338,57 @@ impl HostList {
|
|||||||
let old = slot.replace((buffer.clone(), alloc));
|
let old = slot.replace((buffer.clone(), alloc));
|
||||||
drop(slot);
|
drop(slot);
|
||||||
if let Some((old_buf, _)) = old {
|
if let Some((old_buf, _)) = old {
|
||||||
|
// It ends the list common, as every scratch buffer does.
|
||||||
|
self.indirect_common(&old_buf);
|
||||||
self.keep(&old_buf);
|
self.keep(&old_buf);
|
||||||
}
|
}
|
||||||
Some(buffer)
|
Some(buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Moves `scratch` back to the common state if a clamp left it readable
|
||||||
|
/// as indirect arguments ([`HostList::indirect_readable`]): where the
|
||||||
|
/// list's first clamp expects it. Done before the list closes and before
|
||||||
|
/// a bigger scratch buffer replaces it: one barrier per list (where
|
||||||
|
/// there was one per indirect execution), and the list ends as it
|
||||||
|
/// begins however it is submitted.
|
||||||
|
fn indirect_common(&self, scratch: &ID3D12Resource) {
|
||||||
|
{
|
||||||
|
let mut deferred = self.deferred();
|
||||||
|
if deferred.indirect_readable != scratch.as_raw() as usize {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
deferred.indirect_readable = 0;
|
||||||
|
}
|
||||||
|
let barrier = transition(
|
||||||
|
scratch,
|
||||||
|
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
||||||
|
D3D12_RESOURCE_STATE_COMMON,
|
||||||
|
);
|
||||||
|
// SAFETY: an open list and a live buffer this recording left in
|
||||||
|
// `INDIRECT_ARGUMENT`.
|
||||||
|
unsafe { self.list.ResourceBarrier(&[barrier]) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The state the list's indirect scratch buffer `scratch` is in at this
|
||||||
|
/// point of the recording: indirect arguments if a clamp in this
|
||||||
|
/// recording left it so ([`HostList::indirect_readable`]), otherwise
|
||||||
|
/// common: the state a buffer is made in, and the one a recording
|
||||||
|
/// leaves it in when it closes or replaces it ([`HostList::indirect_common`]).
|
||||||
|
pub(crate) fn indirect_state(&self, scratch: &ID3D12Resource) -> D3D12_RESOURCE_STATES {
|
||||||
|
if self.deferred().indirect_readable == scratch.as_raw() as usize {
|
||||||
|
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT
|
||||||
|
} else {
|
||||||
|
D3D12_RESOURCE_STATE_COMMON
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A clamp recorded in this list left `scratch` readable as indirect
|
||||||
|
/// arguments. It stays so for every execution that reads it, until the
|
||||||
|
/// next clamp moves it back to unordered access.
|
||||||
|
pub(crate) fn indirect_readable(&self, scratch: &ID3D12Resource) {
|
||||||
|
self.deferred().indirect_readable = scratch.as_raw() as usize;
|
||||||
|
}
|
||||||
|
|
||||||
/// Keeps `object` alive until the list is reset.
|
/// Keeps `object` alive until the list is reset.
|
||||||
pub(crate) fn keep(&self, object: &impl Interface) {
|
pub(crate) fn keep(&self, object: &impl Interface) {
|
||||||
if let Ok(object) = object.cast::<windows::core::IUnknown>() {
|
if let Ok(object) = object.cast::<windows::core::IUnknown>() {
|
||||||
|
|||||||
@@ -8,6 +8,15 @@
|
|||||||
//! shader with its root signature inside), leaving the title's argument and
|
//! shader with its root signature inside), leaving the title's argument and
|
||||||
//! count buffers untouched.
|
//! count buffers untouched.
|
||||||
//!
|
//!
|
||||||
|
//! The scratch buffer is left readable as indirect arguments after a clamp
|
||||||
|
//! and stays so until the next clamp into it, which moves it straight back
|
||||||
|
//! to unordered access; it goes back to the common state once, when the
|
||||||
|
//! list closes. The executions that read it record no barrier of their
|
||||||
|
//! own, so draws that read records clamped together (`crate::draw`'s
|
||||||
|
//! windows) follow each other with nothing between them. Before, each
|
||||||
|
//! execution was followed by a move of the scratch buffer back to the
|
||||||
|
//! common state, which drivers may treat as a full wait.
|
||||||
|
//!
|
||||||
//! When a count buffer is given, threads at or past `min(max, count)` return
|
//! When a count buffer is given, threads at or past `min(max, count)` return
|
||||||
//! immediately without charging the list's budget or reading uninitialized
|
//! immediately without charging the list's budget or reading uninitialized
|
||||||
//! tail slots. Each list has a budget (2^27 vertex instances or threads,
|
//! tail slots. Each list has a budget (2^27 vertex instances or threads,
|
||||||
@@ -49,31 +58,49 @@ RWByteAddressBuffer budget : register(u1);
|
|||||||
static const uint BUDGET = 1u << 27;
|
static const uint BUDGET = 1u << 27;
|
||||||
// Groups of one dispatch; more run empty.
|
// Groups of one dispatch; more run empty.
|
||||||
static const uint GROUPS = 1u << 20;
|
static const uint GROUPS = 1u << 20;
|
||||||
|
// Each group charges the budget once: the costs of its records go here,
|
||||||
|
// one thread charges their sum, saturating as each record would in lane
|
||||||
|
// order, and every thread then reads whether its own record fitted. A
|
||||||
|
// compare and swap per record, 64 threads racing for one word, cost a
|
||||||
|
// round for each record of a window, and in memory the CPU maps (where the
|
||||||
|
// budget was, and is with VICTORY_INDIRECT_CARD_BUDGET=0) every atomic
|
||||||
|
// crosses the bus on a card of its own.
|
||||||
|
groupshared uint costs[64];
|
||||||
|
groupshared uint fits[64];
|
||||||
|
groupshared uint largest_asked;
|
||||||
|
groupshared uint all_asked;
|
||||||
[RootSignature(RS)]
|
[RootSignature(RS)]
|
||||||
[numthreads(64, 1, 1)]
|
[numthreads(64, 1, 1)]
|
||||||
void main(uint3 id : SV_DispatchThreadID) {
|
void main(uint3 id : SV_DispatchThreadID, uint gi : SV_GroupIndex) {
|
||||||
uint eff_count = records;
|
uint eff_count = records;
|
||||||
bool ahead = (has_count & 2u) != 0;
|
bool ahead = (has_count & 2u) != 0;
|
||||||
if ((has_count & 1u) != 0) {
|
if ((has_count & 1u) != 0) {
|
||||||
eff_count = min(eff_count, count_buf.Load(count_offset));
|
eff_count = min(eff_count, count_buf.Load(count_offset));
|
||||||
}
|
}
|
||||||
if (id.x >= eff_count) return;
|
// Threads past the records take part in the group's charge (cost 0)
|
||||||
|
// and store nothing.
|
||||||
|
bool active = id.x < eff_count;
|
||||||
uint src_at = args_offset + id.x * stride;
|
uint src_at = args_offset + id.x * stride;
|
||||||
uint dst_at = id.x * stride;
|
uint dst_at = id.x * stride;
|
||||||
uint cost;
|
uint cost = 0;
|
||||||
|
uint asked = 0;
|
||||||
uint2 draw_counts = uint2(0, 0);
|
uint2 draw_counts = uint2(0, 0);
|
||||||
uint3 groups = uint3(0, 0, 0);
|
uint3 groups = uint3(0, 0, 0);
|
||||||
if (kind <= 1) {
|
if (gi == 0) {
|
||||||
// Draws: vertex or index count, then instances.
|
largest_asked = 0;
|
||||||
uint2 asked = src_args.Load2(src_at);
|
all_asked = 0;
|
||||||
if (ahead && (asked.x > (1u << 24) || asked.y > (1u << 24))) {
|
|
||||||
asked = uint2(0, 0);
|
|
||||||
}
|
}
|
||||||
uint count = min(asked.x, 1u << 24);
|
if (active && kind <= 1) {
|
||||||
uint instances = min(asked.y, (1u << 26) / max(count, 1u));
|
// Draws: vertex or index count, then instances.
|
||||||
|
uint2 want = src_args.Load2(src_at);
|
||||||
|
if (ahead && (want.x > (1u << 24) || want.y > (1u << 24))) {
|
||||||
|
want = uint2(0, 0);
|
||||||
|
}
|
||||||
|
uint count = min(want.x, 1u << 24);
|
||||||
|
uint instances = min(want.y, (1u << 26) / max(count, 1u));
|
||||||
cost = count * instances;
|
cost = count * instances;
|
||||||
draw_counts = uint2(count, instances);
|
draw_counts = uint2(count, instances);
|
||||||
} else if (kind == 2) {
|
} else if (active && kind == 2) {
|
||||||
uint3 g = min(src_args.Load3(src_at), uint3(65535u, 65535u, 65535u));
|
uint3 g = min(src_args.Load3(src_at), uint3(65535u, 65535u, 65535u));
|
||||||
// x * y fits 32 bits; the whole count may not.
|
// x * y fits 32 bits; the whole count may not.
|
||||||
if (g.z != 0 && g.x * g.y > GROUPS / g.z) {
|
if (g.z != 0 && g.x * g.y > GROUPS / g.z) {
|
||||||
@@ -81,7 +108,7 @@ void main(uint3 id : SV_DispatchThreadID) {
|
|||||||
}
|
}
|
||||||
cost = g.x * g.y * g.z * 64u;
|
cost = g.x * g.y * g.z * 64u;
|
||||||
groups = g;
|
groups = g;
|
||||||
} else {
|
} else if (active) {
|
||||||
// Mesh dispatches: the console's are one-dimensional.
|
// Mesh dispatches: the console's are one-dimensional.
|
||||||
uint3 g = src_args.Load3(src_at);
|
uint3 g = src_args.Load3(src_at);
|
||||||
// Ahead of its draw, a record over the most a dispatch runs is a
|
// Ahead of its draw, a record over the most a dispatch runs is a
|
||||||
@@ -90,29 +117,68 @@ void main(uint3 id : SV_DispatchThreadID) {
|
|||||||
if (ahead && g.x > 65535u) {
|
if (ahead && g.x > 65535u) {
|
||||||
g = uint3(0, 0, 0);
|
g = uint3(0, 0, 0);
|
||||||
}
|
}
|
||||||
// Diagnostics, in the budget buffer's next words: the largest
|
asked = g.x;
|
||||||
// group count a mesh record asked for, and the groups asked in all.
|
|
||||||
uint was;
|
|
||||||
budget.InterlockedMax(4, g.x, was);
|
|
||||||
budget.InterlockedAdd(8, min(g.x, 1u << 24), was);
|
|
||||||
g = uint3(min(g.x, 65535u), min(g.y, 1u), min(g.z, 1u));
|
g = uint3(min(g.x, 65535u), min(g.y, 1u), min(g.z, 1u));
|
||||||
cost = g.x * g.y * g.z * 128u;
|
cost = g.x * g.y * g.z * 128u;
|
||||||
groups = g;
|
groups = g;
|
||||||
}
|
}
|
||||||
bool over = false;
|
costs[gi] = cost;
|
||||||
if (cost > 0) {
|
GroupMemoryBarrierWithGroupSync();
|
||||||
|
if (active && kind > 2) {
|
||||||
|
// Diagnostics, in the budget buffer's next words (below): the
|
||||||
|
// largest group count a mesh record asked for, and the groups
|
||||||
|
// asked in all.
|
||||||
|
uint was;
|
||||||
|
InterlockedMax(largest_asked, asked, was);
|
||||||
|
InterlockedAdd(all_asked, min(asked, 1u << 24), was);
|
||||||
|
}
|
||||||
|
// The group's own records: none for a group past a count buffer's
|
||||||
|
// count (an execution may ask for thousands and count a few), which
|
||||||
|
// touches the budget not at all, as its threads did not before.
|
||||||
|
uint first = id.x - gi;
|
||||||
|
uint n = eff_count > first ? min(64u, eff_count - first) : 0u;
|
||||||
|
if (gi == 0 && n > 0) {
|
||||||
|
// What the records charge, from `before` on, in lane order: a
|
||||||
|
// record that does not fit whole runs empty and takes the rest.
|
||||||
|
// Only the group's own records are walked (one, clamped alone).
|
||||||
uint before;
|
uint before;
|
||||||
budget.InterlockedOr(0, 0, before);
|
budget.InterlockedOr(0, 0, before);
|
||||||
[allow_uav_condition]
|
[allow_uav_condition]
|
||||||
while (before < BUDGET) {
|
while (before < BUDGET) {
|
||||||
uint next = (BUDGET - before <= cost) ? BUDGET : (before + cost);
|
uint next = before;
|
||||||
|
for (uint j = 0; j < n; j++) {
|
||||||
|
uint c = costs[j];
|
||||||
|
if (c > 0 && next < BUDGET) {
|
||||||
|
next = (BUDGET - next <= c) ? BUDGET : (next + c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (next == before) break;
|
||||||
uint prev;
|
uint prev;
|
||||||
budget.InterlockedCompareExchange(0, before, next, prev);
|
budget.InterlockedCompareExchange(0, before, next, prev);
|
||||||
if (prev == before) break;
|
if (prev == before) break;
|
||||||
before = prev;
|
before = prev;
|
||||||
}
|
}
|
||||||
over = before >= BUDGET || BUDGET - before < cost;
|
uint at = before;
|
||||||
|
for (uint k = 0; k < n; k++) {
|
||||||
|
uint c = costs[k];
|
||||||
|
uint fit = 1;
|
||||||
|
if (c > 0) {
|
||||||
|
fit = (at < BUDGET && BUDGET - at >= c) ? 1u : 0u;
|
||||||
|
at = (at >= BUDGET || BUDGET - at <= c) ? BUDGET : (at + c);
|
||||||
}
|
}
|
||||||
|
fits[k] = fit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GroupMemoryBarrierWithGroupSync();
|
||||||
|
if (gi == 0 && kind > 2 && n > 0) {
|
||||||
|
uint was;
|
||||||
|
budget.InterlockedMax(4, largest_asked, was);
|
||||||
|
budget.InterlockedAdd(8, all_asked, was);
|
||||||
|
}
|
||||||
|
if (!active) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bool over = fits[gi] == 0;
|
||||||
if (kind <= 1) {
|
if (kind <= 1) {
|
||||||
if (over) {
|
if (over) {
|
||||||
draw_counts = uint2(0, 0);
|
draw_counts = uint2(0, 0);
|
||||||
@@ -162,13 +228,19 @@ fn make() -> windows::core::Result<Clamp> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn make_for(device: &ID3D12Device) -> windows::core::Result<Clamp> {
|
fn make_for(device: &ID3D12Device) -> windows::core::Result<Clamp> {
|
||||||
|
make_from(device, SOURCE)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The clamp compiled from `source` (ours, or in the benchmark the one it
|
||||||
|
/// replaced).
|
||||||
|
fn make_from(device: &ID3D12Device, source: &str) -> windows::core::Result<Clamp> {
|
||||||
let mut code: Option<ID3DBlob> = None;
|
let mut code: Option<ID3DBlob> = None;
|
||||||
let mut errors: Option<ID3DBlob> = None;
|
let mut errors: Option<ID3DBlob> = None;
|
||||||
// SAFETY: the source and names live through the call; out pointers ours.
|
// SAFETY: the source and names live through the call; out pointers ours.
|
||||||
let compiled = unsafe {
|
let compiled = unsafe {
|
||||||
D3DCompile(
|
D3DCompile(
|
||||||
SOURCE.as_ptr().cast(),
|
source.as_ptr().cast(),
|
||||||
SOURCE.len(),
|
source.len(),
|
||||||
s!("clamp"),
|
s!("clamp"),
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
@@ -241,9 +313,11 @@ fn validate_layout(
|
|||||||
/// Records, in `list`, the non-destructive clamp of up to `records` records of
|
/// Records, in `list`, the non-destructive clamp of up to `records` records of
|
||||||
/// `kind` (PC argument numbering) `stride` bytes apart from `src` (and bounded
|
/// `kind` (PC argument numbering) `stride` bytes apart from `src` (and bounded
|
||||||
/// by the 32-bit count in `count` when given) into a device-local scratch
|
/// by the 32-bit count in `count` when given) into a device-local scratch
|
||||||
/// buffer, leaving that scratch buffer in `INDIRECT_ARGUMENT` at offset `0`.
|
/// buffer, at offset `0`. The scratch buffer is left in `INDIRECT_ARGUMENT`
|
||||||
/// Replaces the list's compute root signature and pipeline (callers bind
|
/// until the next clamp into it: executions read it with no barrier of
|
||||||
/// theirs after). `None` if the clamp or alignment is not available.
|
/// their own. Replaces the list's compute root signature and pipeline
|
||||||
|
/// (callers bind theirs after). `None` if the clamp or alignment is not
|
||||||
|
/// available.
|
||||||
pub(crate) fn bound(
|
pub(crate) fn bound(
|
||||||
list: &HostList,
|
list: &HostList,
|
||||||
src: (&ID3D12Resource, u64),
|
src: (&ID3D12Resource, u64),
|
||||||
@@ -252,7 +326,7 @@ pub(crate) fn bound(
|
|||||||
) -> Option<ID3D12Resource> {
|
) -> Option<ID3D12Resource> {
|
||||||
let bytes = validate_layout(spec, src.1, count.map(|(_, off)| off))?;
|
let bytes = validate_layout(spec, src.1, count.map(|(_, off)| off))?;
|
||||||
let (c, budget, dst) = (clamp()?, list.budget()?, list.indirect_scratch(bytes)?);
|
let (c, budget, dst) = (clamp()?, list.budget()?, list.indirect_scratch(bytes)?);
|
||||||
record_clamp(c, &list.list, &dst, budget, src, count, spec).then_some(dst)
|
clamp_into(list, c, &dst, budget, src, count, spec, false).then_some(dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [`bound`] for a window of records clamped ahead of the draws that will
|
/// [`bound`] for a window of records clamped ahead of the draws that will
|
||||||
@@ -272,28 +346,41 @@ pub(crate) fn bound_ahead(
|
|||||||
) -> Option<ID3D12Resource> {
|
) -> Option<ID3D12Resource> {
|
||||||
let bytes = validate_layout(spec, src.1, None)?;
|
let bytes = validate_layout(spec, src.1, None)?;
|
||||||
let (c, budget, dst) = (clamp()?, list.budget()?, list.indirect_scratch(bytes)?);
|
let (c, budget, dst) = (clamp()?, list.budget()?, list.indirect_scratch(bytes)?);
|
||||||
record_clamp_as(c, &list.list, &dst, budget, src, None, spec, true).then_some(dst)
|
clamp_into(list, c, &dst, budget, src, None, spec, true).then_some(dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn record_clamp(
|
/// [`record_clamp_as`] into `list`'s scratch buffer `dst`, from the state
|
||||||
|
/// the list left it in; afterwards the list knows it is readable as
|
||||||
|
/// indirect arguments.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
fn clamp_into(
|
||||||
|
list: &HostList,
|
||||||
c: &Clamp,
|
c: &Clamp,
|
||||||
l: &ID3D12GraphicsCommandList,
|
|
||||||
dst: &ID3D12Resource,
|
dst: &ID3D12Resource,
|
||||||
budget: u64,
|
budget: u64,
|
||||||
src: (&ID3D12Resource, u64),
|
src: (&ID3D12Resource, u64),
|
||||||
count: Option<(&ID3D12Resource, u64)>,
|
count: Option<(&ID3D12Resource, u64)>,
|
||||||
spec: (u32, u32, u32),
|
spec: (u32, u32, u32),
|
||||||
|
ahead: bool,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
record_clamp_as(c, l, dst, budget, src, count, spec, false)
|
let before = list.indirect_state(dst);
|
||||||
|
let recorded = record_clamp_as(c, &list.list, (dst, before), budget, src, count, spec, ahead);
|
||||||
|
if recorded {
|
||||||
|
list.indirect_readable(dst);
|
||||||
|
}
|
||||||
|
recorded
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [`record_clamp`]; `ahead`: the records are a window clamped ahead of
|
/// Records the clamp into `dst`, which is in state `before` (common, or
|
||||||
/// their draws ([`bound_ahead`]).
|
/// indirect arguments after an earlier clamp into it), and leaves `dst` in
|
||||||
|
/// `INDIRECT_ARGUMENT`. `ahead`: the records are a window clamped ahead of
|
||||||
|
/// their draws ([`bound_ahead`]). False, with nothing recorded, if the
|
||||||
|
/// layout or the addresses do not fit the clamp.
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn record_clamp_as(
|
fn record_clamp_as(
|
||||||
c: &Clamp,
|
c: &Clamp,
|
||||||
l: &ID3D12GraphicsCommandList,
|
l: &ID3D12GraphicsCommandList,
|
||||||
dst: &ID3D12Resource,
|
(dst, before): (&ID3D12Resource, D3D12_RESOURCE_STATES),
|
||||||
budget: u64,
|
budget: u64,
|
||||||
(src, src_offset): (&ID3D12Resource, u64),
|
(src, src_offset): (&ID3D12Resource, u64),
|
||||||
count: Option<(&ID3D12Resource, u64)>,
|
count: Option<(&ID3D12Resource, u64)>,
|
||||||
@@ -347,7 +434,9 @@ fn record_clamp_as(
|
|||||||
// SAFETY: an open list; our root signature and pipeline; live source,
|
// SAFETY: an open list; our root signature and pipeline; live source,
|
||||||
// count, scratch destination, and budget buffers. `src` and `count` stay
|
// count, scratch destination, and budget buffers. `src` and `count` stay
|
||||||
// in `COMMON` / `GENERIC_READ` via implicit buffer promotion while `dst`
|
// in `COMMON` / `GENERIC_READ` via implicit buffer promotion while `dst`
|
||||||
// transitions `COMMON -> UNORDERED_ACCESS -> INDIRECT_ARGUMENT`.
|
// transitions `before -> UNORDERED_ACCESS -> INDIRECT_ARGUMENT`. Moving
|
||||||
|
// it out of `INDIRECT_ARGUMENT` orders this clamp's writes after every
|
||||||
|
// earlier execution's reads of the scratch buffer; nothing else writes it.
|
||||||
// Diagnostics: counted; `VICTORY_BARRIER_TEST=indirect` leaves the two
|
// Diagnostics: counted; `VICTORY_BARRIER_TEST=indirect` leaves the two
|
||||||
// waits for every earlier shader write out (the state changes stay).
|
// waits for every earlier shader write out (the state changes stay).
|
||||||
#[cfg(feature = "diagnostics")]
|
#[cfg(feature = "diagnostics")]
|
||||||
@@ -358,11 +447,7 @@ fn record_clamp_as(
|
|||||||
let waits = true;
|
let waits = true;
|
||||||
let first = [
|
let first = [
|
||||||
uav_barrier(),
|
uav_barrier(),
|
||||||
transition(
|
transition(dst, before, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
|
||||||
dst,
|
|
||||||
D3D12_RESOURCE_STATE_COMMON,
|
|
||||||
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
|
|
||||||
),
|
|
||||||
];
|
];
|
||||||
let second = [
|
let second = [
|
||||||
uav_barrier(),
|
uav_barrier(),
|
||||||
@@ -391,23 +476,6 @@ fn record_clamp_as(
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
/// After indirect execution (or if preparation failed after [`bound`]):
|
|
||||||
/// returns the scratch buffer to the common state for the next call.
|
|
||||||
pub(crate) fn done(list: &HostList, scratch: &ID3D12Resource) {
|
|
||||||
done_on(&list.list, scratch);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn done_on(list: &ID3D12GraphicsCommandList, scratch: &ID3D12Resource) {
|
|
||||||
// SAFETY: an open list and a live scratch buffer in INDIRECT_ARGUMENT.
|
|
||||||
unsafe {
|
|
||||||
list.ResourceBarrier(&[transition(
|
|
||||||
scratch,
|
|
||||||
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
|
||||||
D3D12_RESOURCE_STATE_COMMON,
|
|
||||||
)])
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
fn uav_barrier() -> D3D12_RESOURCE_BARRIER {
|
fn uav_barrier() -> D3D12_RESOURCE_BARRIER {
|
||||||
D3D12_RESOURCE_BARRIER {
|
D3D12_RESOURCE_BARRIER {
|
||||||
Type: D3D12_RESOURCE_BARRIER_TYPE_UAV,
|
Type: D3D12_RESOURCE_BARRIER_TYPE_UAV,
|
||||||
@@ -833,14 +901,15 @@ void main() {
|
|||||||
// Record Call #1 (`eff_count = 3` out of `MAX = 4096`), copy the first
|
// Record Call #1 (`eff_count = 3` out of `MAX = 4096`), copy the first
|
||||||
// 5 records of `dst_buf` out for inspection, then record Call #2
|
// 5 records of `dst_buf` out for inspection, then record Call #2
|
||||||
// (`eff_count = 2` out of `MAX = 4096`) reusing `dst_buf` in the same list!
|
// (`eff_count = 2` out of `MAX = 4096`) reusing `dst_buf` in the same list!
|
||||||
assert!(record_clamp(
|
assert!(record_clamp_as(
|
||||||
&warp.clamp,
|
&warp.clamp,
|
||||||
&warp.list,
|
&warp.list,
|
||||||
&dst_buf,
|
(&dst_buf, D3D12_RESOURCE_STATE_COMMON),
|
||||||
budget_gpu,
|
budget_gpu,
|
||||||
(&src_buf, ARGS_OFFSET as u64),
|
(&src_buf, ARGS_OFFSET as u64),
|
||||||
Some((&count_buf, COUNT_OFFSET_1 as u64)),
|
Some((&count_buf, COUNT_OFFSET_1 as u64)),
|
||||||
(KIND_DISPATCH, STRIDE, MAX),
|
(KIND_DISPATCH, STRIDE, MAX),
|
||||||
|
false,
|
||||||
));
|
));
|
||||||
// SAFETY: open command list, valid worker PSO/root and buffers.
|
// SAFETY: open command list, valid worker PSO/root and buffers.
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -856,15 +925,14 @@ void main() {
|
|||||||
COUNT_OFFSET_1 as u64,
|
COUNT_OFFSET_1 as u64,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
done_on(&warp.list, &dst_buf);
|
|
||||||
|
|
||||||
// Snapshot `dst_buf` after Call #1 (from `COMMON`, copy 5 * STRIDE bytes,
|
// Snapshot `dst_buf` after Call #1 (copy 5 * STRIDE bytes, then leave
|
||||||
// then return `dst_buf` to `COMMON` before Call #2).
|
// `dst_buf` readable as arguments again, as the clamp left it).
|
||||||
// SAFETY: open command list; `dst_buf` returned to `COMMON` by `done_on`.
|
// SAFETY: open command list; the clamp left `dst_buf` in `INDIRECT_ARGUMENT`.
|
||||||
unsafe {
|
unsafe {
|
||||||
warp.list.ResourceBarrier(&[transition(
|
warp.list.ResourceBarrier(&[transition(
|
||||||
&dst_buf,
|
&dst_buf,
|
||||||
D3D12_RESOURCE_STATE_COMMON,
|
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
||||||
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
||||||
)]);
|
)]);
|
||||||
warp.list
|
warp.list
|
||||||
@@ -872,20 +940,23 @@ void main() {
|
|||||||
warp.list.ResourceBarrier(&[transition(
|
warp.list.ResourceBarrier(&[transition(
|
||||||
&dst_buf,
|
&dst_buf,
|
||||||
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
||||||
D3D12_RESOURCE_STATE_COMMON,
|
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
||||||
)]);
|
)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call #2 in the SAME command list (`eff_count = 2` -> records 0 and 1:
|
// Call #2 in the SAME command list (`eff_count = 2` -> records 0 and 1:
|
||||||
// 6 + 5 = 11 groups). Would run empty if Call #1's tail blew `BUDGET`!
|
// 6 + 5 = 11 groups). Would run empty if Call #1's tail blew `BUDGET`!
|
||||||
assert!(record_clamp(
|
// The scratch buffer goes straight from arguments back to unordered
|
||||||
|
// access.
|
||||||
|
assert!(record_clamp_as(
|
||||||
&warp.clamp,
|
&warp.clamp,
|
||||||
&warp.list,
|
&warp.list,
|
||||||
&dst_buf,
|
(&dst_buf, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT),
|
||||||
budget_gpu,
|
budget_gpu,
|
||||||
(&src_buf, ARGS_OFFSET as u64),
|
(&src_buf, ARGS_OFFSET as u64),
|
||||||
Some((&count_buf, COUNT_OFFSET_2 as u64)),
|
Some((&count_buf, COUNT_OFFSET_2 as u64)),
|
||||||
(KIND_DISPATCH, STRIDE, MAX),
|
(KIND_DISPATCH, STRIDE, MAX),
|
||||||
|
false,
|
||||||
));
|
));
|
||||||
// SAFETY: open command list, valid worker PSO/root and buffers.
|
// SAFETY: open command list, valid worker PSO/root and buffers.
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -901,7 +972,6 @@ void main() {
|
|||||||
COUNT_OFFSET_2 as u64,
|
COUNT_OFFSET_2 as u64,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
done_on(&warp.list, &dst_buf);
|
|
||||||
|
|
||||||
warp.submit_and_wait();
|
warp.submit_and_wait();
|
||||||
|
|
||||||
@@ -998,27 +1068,22 @@ void main() {
|
|||||||
assert!(record_clamp_as(
|
assert!(record_clamp_as(
|
||||||
&warp.clamp,
|
&warp.clamp,
|
||||||
&warp.list,
|
&warp.list,
|
||||||
&dst_buf,
|
(&dst_buf, D3D12_RESOURCE_STATE_COMMON),
|
||||||
budget_gpu,
|
budget_gpu,
|
||||||
(&src_buf, 0),
|
(&src_buf, 0),
|
||||||
None,
|
None,
|
||||||
(KIND_MESH, STRIDE, RECORDS),
|
(KIND_MESH, STRIDE, RECORDS),
|
||||||
ahead,
|
ahead,
|
||||||
));
|
));
|
||||||
done_on(&warp.list, &dst_buf);
|
// SAFETY: an open list; the scratch buffer, which the clamp left
|
||||||
// SAFETY: an open list; the scratch buffer is copied for the check.
|
// readable as arguments, is copied for the check.
|
||||||
unsafe {
|
unsafe {
|
||||||
warp.list.ResourceBarrier(&[transition(
|
warp.list.ResourceBarrier(&[transition(
|
||||||
&dst_buf,
|
&dst_buf,
|
||||||
D3D12_RESOURCE_STATE_COMMON,
|
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
||||||
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
||||||
)]);
|
)]);
|
||||||
warp.list.CopyBufferRegion(&readback_buf, 0, &dst_buf, 0, total as u64);
|
warp.list.CopyBufferRegion(&readback_buf, 0, &dst_buf, 0, total as u64);
|
||||||
warp.list.ResourceBarrier(&[transition(
|
|
||||||
&dst_buf,
|
|
||||||
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
|
||||||
D3D12_RESOURCE_STATE_COMMON,
|
|
||||||
)]);
|
|
||||||
}
|
}
|
||||||
warp.submit_and_wait();
|
warp.submit_and_wait();
|
||||||
// SAFETY: the work is done; mapped buffers.
|
// SAFETY: the work is done; mapped buffers.
|
||||||
@@ -1088,31 +1153,27 @@ void main() {
|
|||||||
std::ptr::write_bytes(readback_ptr, 0, total_bytes);
|
std::ptr::write_bytes(readback_ptr, 0, total_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert!(record_clamp(
|
assert!(record_clamp_as(
|
||||||
&warp.clamp,
|
&warp.clamp,
|
||||||
&warp.list,
|
&warp.list,
|
||||||
&dst_buf,
|
(&dst_buf, D3D12_RESOURCE_STATE_COMMON),
|
||||||
budget_gpu,
|
budget_gpu,
|
||||||
(&src_buf, 0),
|
(&src_buf, 0),
|
||||||
None,
|
None,
|
||||||
(KIND_INDEXED, STRIDE, RECORDS),
|
(KIND_INDEXED, STRIDE, RECORDS),
|
||||||
|
false,
|
||||||
));
|
));
|
||||||
done_on(&warp.list, &dst_buf);
|
|
||||||
|
|
||||||
// SAFETY: open command list; copy clamped scratch buffer for verification.
|
// SAFETY: open command list; copy clamped scratch buffer (left
|
||||||
|
// readable as arguments) for verification.
|
||||||
unsafe {
|
unsafe {
|
||||||
warp.list.ResourceBarrier(&[transition(
|
warp.list.ResourceBarrier(&[transition(
|
||||||
&dst_buf,
|
&dst_buf,
|
||||||
D3D12_RESOURCE_STATE_COMMON,
|
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
||||||
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
||||||
)]);
|
)]);
|
||||||
warp.list
|
warp.list
|
||||||
.CopyBufferRegion(&readback_buf, 0, &dst_buf, 0, total_bytes as u64);
|
.CopyBufferRegion(&readback_buf, 0, &dst_buf, 0, total_bytes as u64);
|
||||||
warp.list.ResourceBarrier(&[transition(
|
|
||||||
&dst_buf,
|
|
||||||
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
|
||||||
D3D12_RESOURCE_STATE_COMMON,
|
|
||||||
)]);
|
|
||||||
}
|
}
|
||||||
warp.submit_and_wait();
|
warp.submit_and_wait();
|
||||||
|
|
||||||
@@ -1152,13 +1213,16 @@ void main() {
|
|||||||
assert_eq!((kept, zeroed), (2, (RECORDS as usize) - 2));
|
assert_eq!((kept, zeroed), (2, (RECORDS as usize) - 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Exercises `HostList::indirect_scratch`, `bound`, `done`, `Host::execute`,
|
/// Exercises `HostList::indirect_scratch`, `bound`, the scratch buffer's
|
||||||
/// and `HostList::reset` end-to-end on the host device.
|
/// tracked state, `Host::execute`, and `HostList::reset` end-to-end on
|
||||||
|
/// the host device.
|
||||||
#[test]
|
#[test]
|
||||||
fn host_list_reuses_and_grows_scratch_across_submissions() {
|
fn host_list_reuses_and_grows_scratch_across_submissions() {
|
||||||
let Some(host) = super::host() else {
|
let Some(host) = super::host() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
// What earlier tests left in the debug layer's queue, if it is on.
|
||||||
|
let _ = host.log_messages();
|
||||||
let Ok(list) = host.new_list() else {
|
let Ok(list) = host.new_list() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -1174,43 +1238,420 @@ void main() {
|
|||||||
(count_ptr as *mut u32).write_volatile(2);
|
(count_ptr as *mut u32).write_volatile(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let args = D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT;
|
||||||
// First two calls fit inside the initial 64 KiB scratch buffer and
|
// First two calls fit inside the initial 64 KiB scratch buffer and
|
||||||
// must reuse the exact same `ID3D12Resource`.
|
// must reuse the exact same `ID3D12Resource`, which stays readable
|
||||||
|
// as arguments between them.
|
||||||
let b1 = bound(&list, (&src_buf, 0), Some((&count_buf, 0)), (0, 16, 64)).unwrap();
|
let b1 = bound(&list, (&src_buf, 0), Some((&count_buf, 0)), (0, 16, 64)).unwrap();
|
||||||
done(&list, &b1);
|
assert_eq!(
|
||||||
|
list.indirect_state(&b1),
|
||||||
|
args,
|
||||||
|
"a clamp leaves the scratch readable as arguments"
|
||||||
|
);
|
||||||
let b2 = bound(&list, (&src_buf, 64), Some((&count_buf, 0)), (1, 20, 128)).unwrap();
|
let b2 = bound(&list, (&src_buf, 64), Some((&count_buf, 0)), (1, 20, 128)).unwrap();
|
||||||
done(&list, &b2);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
b1.as_raw(),
|
b1.as_raw(),
|
||||||
b2.as_raw(),
|
b2.as_raw(),
|
||||||
"scratch buffer within capacity must be reused"
|
"scratch buffer within capacity must be reused"
|
||||||
);
|
);
|
||||||
|
assert_eq!(list.indirect_state(&b2), args);
|
||||||
|
|
||||||
// A call needing >64 KiB grows the scratch buffer mid-list while keeping
|
// A call needing >64 KiB grows the scratch buffer mid-list while keeping
|
||||||
// `b1` alive until reset.
|
// `b1` alive until reset. The new buffer starts out common.
|
||||||
|
let grown = list.indirect_scratch(5000 * 20).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
list.indirect_state(&grown),
|
||||||
|
D3D12_RESOURCE_STATE_COMMON,
|
||||||
|
"a new scratch buffer is common"
|
||||||
|
);
|
||||||
let b3 = bound(&list, (&src_buf, 0), Some((&count_buf, 0)), (1, 20, 5000)).unwrap();
|
let b3 = bound(&list, (&src_buf, 0), Some((&count_buf, 0)), (1, 20, 5000)).unwrap();
|
||||||
done(&list, &b3);
|
assert_eq!(b3.as_raw(), grown.as_raw());
|
||||||
assert_ne!(
|
assert_ne!(
|
||||||
b3.as_raw(),
|
b3.as_raw(),
|
||||||
b1.as_raw(),
|
b1.as_raw(),
|
||||||
"larger request must grow scratch buffer"
|
"larger request must grow scratch buffer"
|
||||||
);
|
);
|
||||||
|
assert_eq!(list.indirect_state(&b3), args);
|
||||||
|
assert_eq!(
|
||||||
|
list.indirect_state(&b1),
|
||||||
|
D3D12_RESOURCE_STATE_COMMON,
|
||||||
|
"the replaced buffer went back to common"
|
||||||
|
);
|
||||||
|
|
||||||
list.close();
|
list.close();
|
||||||
|
// With the debug layer on (`VICTORY_D3D12_DEBUG=1`, diagnostics):
|
||||||
|
// every barrier recorded named the state the buffer was in. Read
|
||||||
|
// before the submission, which moves the messages to the log.
|
||||||
|
assert_eq!(host.log_messages(), None, "the debug layer found an error");
|
||||||
host.execute(&[&list]);
|
host.execute(&[&list]);
|
||||||
list.reset();
|
list.reset();
|
||||||
|
|
||||||
// After reset, requests up to the grown high-water mark reuse `b3` with
|
// After reset, requests up to the grown high-water mark reuse `b3` with
|
||||||
// zero new allocations.
|
// zero new allocations; the buffer decayed to common when the
|
||||||
|
// submission ran.
|
||||||
|
assert_eq!(
|
||||||
|
list.indirect_state(&b3),
|
||||||
|
D3D12_RESOURCE_STATE_COMMON,
|
||||||
|
"a reset list starts with a common scratch buffer"
|
||||||
|
);
|
||||||
let b4 = bound(&list, (&src_buf, 0), Some((&count_buf, 0)), (1, 20, 4096)).unwrap();
|
let b4 = bound(&list, (&src_buf, 0), Some((&count_buf, 0)), (1, 20, 4096)).unwrap();
|
||||||
done(&list, &b4);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
b4.as_raw(),
|
b4.as_raw(),
|
||||||
b3.as_raw(),
|
b3.as_raw(),
|
||||||
"grown scratch buffer must persist across reset"
|
"grown scratch buffer must persist across reset"
|
||||||
);
|
);
|
||||||
|
assert_eq!(list.indirect_state(&b4), args);
|
||||||
list.close();
|
list.close();
|
||||||
|
assert_eq!(host.log_messages(), None, "the debug layer found an error");
|
||||||
host.execute(&[&list]);
|
host.execute(&[&list]);
|
||||||
list.reset();
|
list.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A group charges its records in lane order: with 300 left of the
|
||||||
|
/// budget, the first draw (100) fits, the second (250) does not and
|
||||||
|
/// takes the rest, and the ones after it run empty. (Racing for the
|
||||||
|
/// word record by record, which of them fitted was up to the GPU.)
|
||||||
|
#[test]
|
||||||
|
fn warp_a_group_charges_its_records_in_lane_order() {
|
||||||
|
let Some(mut warp) = WarpHarness::new() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
const KIND_DRAW: u32 = 0;
|
||||||
|
const STRIDE: u32 = 16;
|
||||||
|
const BUDGET_CAP: u32 = 1 << 27;
|
||||||
|
let counts = [100u32, 250, 50, 100];
|
||||||
|
let total = counts.len() * STRIDE as usize;
|
||||||
|
let (src_buf, src_ptr, _) = warp.shared_buffer(total as u64);
|
||||||
|
let (_budget_buf, budget_ptr, budget_gpu) = warp.shared_buffer(256);
|
||||||
|
let (readback_buf, readback_ptr, _) = warp.shared_buffer(total as u64);
|
||||||
|
let dst_buf = warp.default_uav(total as u64);
|
||||||
|
// SAFETY: mapped buffers of these sizes.
|
||||||
|
unsafe {
|
||||||
|
for (i, &count) in counts.iter().enumerate() {
|
||||||
|
let words = [count, 1, 7, 9];
|
||||||
|
std::ptr::copy_nonoverlapping(
|
||||||
|
words.as_ptr().cast::<u8>(),
|
||||||
|
src_ptr.add(i * STRIDE as usize),
|
||||||
|
16,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
std::ptr::write_bytes(budget_ptr, 0, 256);
|
||||||
|
(budget_ptr as *mut u32).write_volatile(BUDGET_CAP - 300);
|
||||||
|
}
|
||||||
|
assert!(record_clamp_as(
|
||||||
|
&warp.clamp,
|
||||||
|
&warp.list,
|
||||||
|
(&dst_buf, D3D12_RESOURCE_STATE_COMMON),
|
||||||
|
budget_gpu,
|
||||||
|
(&src_buf, 0),
|
||||||
|
None,
|
||||||
|
(KIND_DRAW, STRIDE, counts.len() as u32),
|
||||||
|
false,
|
||||||
|
));
|
||||||
|
// SAFETY: an open list; the clamped records are copied for the check.
|
||||||
|
unsafe {
|
||||||
|
warp.list.ResourceBarrier(&[transition(
|
||||||
|
&dst_buf,
|
||||||
|
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
||||||
|
D3D12_RESOURCE_STATE_COPY_SOURCE,
|
||||||
|
)]);
|
||||||
|
warp.list.CopyBufferRegion(&readback_buf, 0, &dst_buf, 0, total as u64);
|
||||||
|
}
|
||||||
|
warp.submit_and_wait();
|
||||||
|
// SAFETY: the work is done; mapped buffers.
|
||||||
|
let (spent, out) = unsafe {
|
||||||
|
(
|
||||||
|
(budget_ptr as *const u32).read_volatile(),
|
||||||
|
std::slice::from_raw_parts(readback_ptr.cast::<u32>().cast_const(), total / 4)
|
||||||
|
.to_vec(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
assert_eq!(out[0..4], [100, 1, 7, 9], "the first record fits");
|
||||||
|
for i in 1..counts.len() {
|
||||||
|
assert_eq!(out[i * 4..i * 4 + 4], [0, 0, 7, 9], "record {i} runs empty");
|
||||||
|
}
|
||||||
|
assert_eq!(spent, BUDGET_CAP, "the record that did not fit took the rest");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The list's budget starts empty at each submission, wherever it
|
||||||
|
/// lives (the card's copy is zeroed in the list): a recording whose
|
||||||
|
/// clamps spend all of it is followed, after the reset, by one whose
|
||||||
|
/// small record still runs.
|
||||||
|
#[test]
|
||||||
|
fn host_list_budget_starts_empty_at_each_submission() {
|
||||||
|
let Some(host) = super::host() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Ok(list) = host.new_list() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let _ = host.log_messages();
|
||||||
|
let worker = compile_cs(&host.device, WORKER_CS).expect("worker shader must compile");
|
||||||
|
const KIND_DISPATCH: u32 = 2;
|
||||||
|
const STRIDE: u32 = 12;
|
||||||
|
let signature = {
|
||||||
|
let arg = D3D12_INDIRECT_ARGUMENT_DESC {
|
||||||
|
Type: D3D12_INDIRECT_ARGUMENT_TYPE(KIND_DISPATCH as i32),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let desc = D3D12_COMMAND_SIGNATURE_DESC {
|
||||||
|
ByteStride: STRIDE,
|
||||||
|
NumArgumentDescs: 1,
|
||||||
|
pArgumentDescs: &arg,
|
||||||
|
NodeMask: 0,
|
||||||
|
};
|
||||||
|
let mut sig: Option<ID3D12CommandSignature> = None;
|
||||||
|
// SAFETY: a valid single-argument command signature description.
|
||||||
|
unsafe {
|
||||||
|
host.device
|
||||||
|
.CreateCommandSignature(&desc, None::<&ID3D12RootSignature>, &mut sig)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
sig.unwrap()
|
||||||
|
};
|
||||||
|
// Three records of 65,535 x 16 groups: about half the budget each.
|
||||||
|
let Some((big, big_ptr)) = host.shared_buffer(3 * u64::from(STRIDE)) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Some((small, small_ptr)) = host.shared_buffer(u64::from(STRIDE)) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Some((hits_buf, hits_ptr)) = host.shared_buffer(256) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
// SAFETY: mapped buffers of these sizes; nothing runs yet.
|
||||||
|
unsafe {
|
||||||
|
for i in 0..3 {
|
||||||
|
let words = [65535u32, 16, 1];
|
||||||
|
std::ptr::copy_nonoverlapping(
|
||||||
|
words.as_ptr().cast::<u8>(),
|
||||||
|
big_ptr.add(i * STRIDE as usize),
|
||||||
|
12,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
std::ptr::copy_nonoverlapping([1u32, 1, 1].as_ptr().cast::<u8>(), small_ptr, 12);
|
||||||
|
std::ptr::write_bytes(hits_ptr, 0, 256);
|
||||||
|
}
|
||||||
|
// SAFETY: a live buffer.
|
||||||
|
let hits_gpu = unsafe { hits_buf.GetGPUVirtualAddress() };
|
||||||
|
for round in 0..2 {
|
||||||
|
// Spends the budget (clamped, not executed), then one group.
|
||||||
|
bound(&list, (&big, 0), None, (KIND_DISPATCH, STRIDE, 3)).unwrap();
|
||||||
|
let scratch = bound(&list, (&small, 0), None, (KIND_DISPATCH, STRIDE, 1)).unwrap();
|
||||||
|
// SAFETY: an open list; the worker's root signature and
|
||||||
|
// pipeline; buffers the test holds until the list has run.
|
||||||
|
unsafe {
|
||||||
|
list.list.SetComputeRootSignature(&worker.root);
|
||||||
|
list.list.SetPipelineState(&worker.pso);
|
||||||
|
list.list.SetComputeRootUnorderedAccessView(0, hits_gpu);
|
||||||
|
list.list.ExecuteIndirect(&signature, 1, &scratch, 0, None, 0);
|
||||||
|
}
|
||||||
|
list.close();
|
||||||
|
assert_eq!(host.log_messages(), None, "the debug layer found an error");
|
||||||
|
host.execute(&[&list]);
|
||||||
|
list.reset();
|
||||||
|
// SAFETY: the list has run; a mapped buffer.
|
||||||
|
let hits = unsafe { (hits_ptr as *const u32).read_volatile() };
|
||||||
|
// The small record comes after the big ones spent the budget:
|
||||||
|
// it runs empty, in every round alike (none carries over).
|
||||||
|
assert_eq!(hits, 0, "round {round}: the spent budget empties the record after it");
|
||||||
|
}
|
||||||
|
// A recording with only the small record: the budget is empty
|
||||||
|
// again, so it runs.
|
||||||
|
let scratch = bound(&list, (&small, 0), None, (KIND_DISPATCH, STRIDE, 1)).unwrap();
|
||||||
|
// SAFETY: as above.
|
||||||
|
unsafe {
|
||||||
|
list.list.SetComputeRootSignature(&worker.root);
|
||||||
|
list.list.SetPipelineState(&worker.pso);
|
||||||
|
list.list.SetComputeRootUnorderedAccessView(0, hits_gpu);
|
||||||
|
list.list.ExecuteIndirect(&signature, 1, &scratch, 0, None, 0);
|
||||||
|
}
|
||||||
|
list.close();
|
||||||
|
assert_eq!(host.log_messages(), None, "the debug layer found an error");
|
||||||
|
host.execute(&[&list]);
|
||||||
|
list.reset();
|
||||||
|
// SAFETY: the list has run; a mapped buffer.
|
||||||
|
let hits = unsafe { (hits_ptr as *const u32).read_volatile() };
|
||||||
|
assert_eq!(hits, 1, "the budget starts empty at the next submission");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Records the dispatch records `groups` (three words each, `step`
|
||||||
|
/// bytes apart, the rest of each record ones) into a fresh mapped
|
||||||
|
/// buffer of `warp`'s device.
|
||||||
|
fn dispatch_records(warp: &WarpHarness, groups: &[u32], step: u32) -> ID3D12Resource {
|
||||||
|
let total = groups.len() * step as usize;
|
||||||
|
let (buffer, ptr, _) = warp.shared_buffer(total as u64);
|
||||||
|
let mut bytes = vec![0xffu8; total];
|
||||||
|
for (i, &g) in groups.iter().enumerate() {
|
||||||
|
for (w, word) in [g, 1, 1].into_iter().enumerate() {
|
||||||
|
let at = i * step as usize + w * 4;
|
||||||
|
bytes[at..at + 4].copy_from_slice(&word.to_le_bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// SAFETY: the mapping holds `total` bytes.
|
||||||
|
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), ptr, total) };
|
||||||
|
buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Two windows in one list, as `crate::draw` records them: each clamps
|
||||||
|
/// its records ahead, then one execution per record reads the scratch
|
||||||
|
/// buffer with no barrier between them; the second clamp moves the
|
||||||
|
/// scratch buffer straight from arguments back to unordered access.
|
||||||
|
/// Every record runs once, with the group count it asked for.
|
||||||
|
#[test]
|
||||||
|
fn warp_window_executions_read_their_records_with_no_barrier_between() {
|
||||||
|
let Some(mut warp) = WarpHarness::new() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
const KIND_DISPATCH: u32 = 2;
|
||||||
|
// The title names 12 bytes and keeps its records 32 apart.
|
||||||
|
const STRIDE: u32 = 12;
|
||||||
|
const STEP: u32 = 32;
|
||||||
|
let windows: [&[u32]; 2] = [&[1, 2, 3, 4], &[10, 11, 12, 13]];
|
||||||
|
let sources: Vec<ID3D12Resource> =
|
||||||
|
windows.iter().map(|g| dispatch_records(&warp, g, STEP)).collect();
|
||||||
|
let (_budget_buf, budget_ptr, budget_gpu) = warp.shared_buffer(256);
|
||||||
|
let (_hits_buf, hits_ptr, hits_gpu) = warp.shared_buffer(256);
|
||||||
|
let dst_buf = warp.default_uav(u64::from(STEP) * 4);
|
||||||
|
let sig = warp.signature(KIND_DISPATCH, STRIDE);
|
||||||
|
// SAFETY: mapped buffers of these sizes.
|
||||||
|
unsafe {
|
||||||
|
std::ptr::write_bytes(budget_ptr, 0, 256);
|
||||||
|
std::ptr::write_bytes(hits_ptr, 0, 256);
|
||||||
|
}
|
||||||
|
let mut state = D3D12_RESOURCE_STATE_COMMON;
|
||||||
|
for (groups, src) in windows.iter().zip(&sources) {
|
||||||
|
assert!(record_clamp_as(
|
||||||
|
&warp.clamp,
|
||||||
|
&warp.list,
|
||||||
|
(&dst_buf, state),
|
||||||
|
budget_gpu,
|
||||||
|
(src, 0),
|
||||||
|
None,
|
||||||
|
(KIND_DISPATCH, STEP, groups.len() as u32),
|
||||||
|
true,
|
||||||
|
));
|
||||||
|
state = D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT;
|
||||||
|
// SAFETY: an open list; the worker's root signature and
|
||||||
|
// pipeline; the scratch buffer is readable as arguments.
|
||||||
|
unsafe {
|
||||||
|
warp.list.SetComputeRootSignature(&warp.worker.root);
|
||||||
|
warp.list.SetPipelineState(&warp.worker.pso);
|
||||||
|
warp.list.SetComputeRootUnorderedAccessView(0, hits_gpu);
|
||||||
|
for i in 0..groups.len() as u64 {
|
||||||
|
warp.list.ExecuteIndirect(&sig, 1, &dst_buf, i * u64::from(STEP), None, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
warp.submit_and_wait();
|
||||||
|
// SAFETY: the work is done; mapped buffers.
|
||||||
|
let (hits, spent) = unsafe {
|
||||||
|
(
|
||||||
|
(hits_ptr as *const u32).read_volatile(),
|
||||||
|
(budget_ptr as *const u32).read_volatile(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
let expected: u32 = windows.iter().flat_map(|g| g.iter()).sum();
|
||||||
|
assert_eq!(hits, expected, "every record of both windows runs once");
|
||||||
|
assert_eq!(spent, expected * 64, "and is charged what it asked for");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The windows of `warp_window_executions_read_their_records_with_no_barrier_between`
|
||||||
|
/// through a host list (`bound_ahead`, the list's tracked scratch state,
|
||||||
|
/// `Host::execute`), twice: on the first recording the scratch buffer
|
||||||
|
/// starts common, on the second (after the reset) it starts common
|
||||||
|
/// again. With the debug layer on (`VICTORY_D3D12_DEBUG=1` in a
|
||||||
|
/// diagnostics build) no barrier names a state the buffer is not in.
|
||||||
|
#[test]
|
||||||
|
fn host_list_window_executions_need_no_barrier_of_their_own() {
|
||||||
|
let Some(host) = super::host() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
// What earlier tests left in the debug layer's queue, if it is on.
|
||||||
|
let _ = host.log_messages();
|
||||||
|
let Ok(list) = host.new_list() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let worker = compile_cs(&host.device, WORKER_CS).expect("worker shader must compile");
|
||||||
|
const KIND_DISPATCH: u32 = 2;
|
||||||
|
const STRIDE: u32 = 12;
|
||||||
|
const STEP: u32 = 32;
|
||||||
|
let signature = {
|
||||||
|
let arg = D3D12_INDIRECT_ARGUMENT_DESC {
|
||||||
|
Type: D3D12_INDIRECT_ARGUMENT_TYPE(KIND_DISPATCH as i32),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let desc = D3D12_COMMAND_SIGNATURE_DESC {
|
||||||
|
ByteStride: STRIDE,
|
||||||
|
NumArgumentDescs: 1,
|
||||||
|
pArgumentDescs: &arg,
|
||||||
|
NodeMask: 0,
|
||||||
|
};
|
||||||
|
let mut sig: Option<ID3D12CommandSignature> = None;
|
||||||
|
// SAFETY: a valid single-argument command signature description.
|
||||||
|
unsafe {
|
||||||
|
host.device
|
||||||
|
.CreateCommandSignature(&desc, None::<&ID3D12RootSignature>, &mut sig)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
sig.unwrap()
|
||||||
|
};
|
||||||
|
let windows: [&[u32]; 2] = [&[1, 2, 3, 4], &[10, 11, 12, 13]];
|
||||||
|
let mut sources = Vec::new();
|
||||||
|
for groups in windows {
|
||||||
|
let total = groups.len() * STEP as usize;
|
||||||
|
let Some((buffer, ptr)) = host.shared_buffer(total as u64) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let mut bytes = vec![0xffu8; total];
|
||||||
|
for (i, &g) in groups.iter().enumerate() {
|
||||||
|
for (w, word) in [g, 1, 1].into_iter().enumerate() {
|
||||||
|
let at = i * STEP as usize + w * 4;
|
||||||
|
bytes[at..at + 4].copy_from_slice(&word.to_le_bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// SAFETY: the mapping holds `total` bytes.
|
||||||
|
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), ptr, total) };
|
||||||
|
sources.push(buffer);
|
||||||
|
}
|
||||||
|
let Some((hits_buf, hits_ptr)) = host.shared_buffer(256) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let expected: u32 = windows.iter().flat_map(|g| g.iter()).sum();
|
||||||
|
// SAFETY: a live buffer.
|
||||||
|
let hits_gpu = unsafe { hits_buf.GetGPUVirtualAddress() };
|
||||||
|
for _ in 0..2 {
|
||||||
|
// SAFETY: the mapping holds 256 bytes; nothing runs.
|
||||||
|
unsafe { std::ptr::write_bytes(hits_ptr, 0, 256) };
|
||||||
|
for (groups, src) in windows.iter().zip(&sources) {
|
||||||
|
let spec = (KIND_DISPATCH, STEP, groups.len() as u32);
|
||||||
|
let scratch = bound_ahead(&list, (src, 0), spec).unwrap();
|
||||||
|
assert_eq!(list.indirect_state(&scratch), D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
|
||||||
|
// SAFETY: an open list; the worker's root signature and
|
||||||
|
// pipeline; buffers the test holds until the list has run.
|
||||||
|
unsafe {
|
||||||
|
list.list.SetComputeRootSignature(&worker.root);
|
||||||
|
list.list.SetPipelineState(&worker.pso);
|
||||||
|
list.list.SetComputeRootUnorderedAccessView(0, hits_gpu);
|
||||||
|
for i in 0..groups.len() as u64 {
|
||||||
|
let at = i * u64::from(STEP);
|
||||||
|
list.list.ExecuteIndirect(&signature, 1, &scratch, at, None, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list.close();
|
||||||
|
// With the debug layer on: nothing it objects to was recorded.
|
||||||
|
// Read before the submission, which moves the messages to the log.
|
||||||
|
assert_eq!(host.log_messages(), None, "the debug layer found an error");
|
||||||
|
host.execute(&[&list]);
|
||||||
|
list.reset();
|
||||||
|
// SAFETY: the list has run; a mapped buffer.
|
||||||
|
let hits = unsafe { (hits_ptr as *const u32).read_volatile() };
|
||||||
|
assert_eq!(hits, expected, "every record of both windows runs once");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The benchmark of the clamp and its barriers (ignored by default).
|
||||||
|
#[cfg(test)]
|
||||||
|
mod bench;
|
||||||
@@ -0,0 +1,502 @@
|
|||||||
|
//! A frame's worth of one-record indirect draws on the host GPU, clamped one
|
||||||
|
//! at a time and in windows, recorded as `main` does (the scratch buffer
|
||||||
|
//! back to the common state after every execution, the clamp charging the
|
||||||
|
//! budget record by record) and as this tree does (readable until the next
|
||||||
|
//! clamp, one charge per group). Ignored by default; run it with
|
||||||
|
//! `cargo test -p victory-gpu --release --lib indirect::bench -- --ignored --nocapture`.
|
||||||
|
//! It needs no title files: the draws are tiny triangles of ours.
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use windows::Win32::Foundation::RECT;
|
||||||
|
use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SAMPLE_DESC};
|
||||||
|
use windows::Win32::Graphics::Dxgi::{
|
||||||
|
CreateDXGIFactory2, DXGI_CREATE_FACTORY_FLAGS, IDXGIFactory4,
|
||||||
|
};
|
||||||
|
use windows::Win32::System::Threading::{CreateEventW, INFINITE, WaitForSingleObject};
|
||||||
|
use windows::core::{Interface, PCSTR};
|
||||||
|
|
||||||
|
/// Draws a frame (the title's gameplay frames hold about 3,200 indirect
|
||||||
|
/// executions, #107), records a window holds, and how far apart the
|
||||||
|
/// records are (the title keeps them 32 bytes apart).
|
||||||
|
const DRAWS: u32 = 3200;
|
||||||
|
const WINDOW: u32 = 32;
|
||||||
|
const STEP: u32 = 32;
|
||||||
|
const RUNS: usize = 9;
|
||||||
|
|
||||||
|
const SHADERS: &str = r#"
|
||||||
|
#define RS "RootFlags(0)"
|
||||||
|
[RootSignature(RS)]
|
||||||
|
float4 vs(uint v : SV_VertexID, uint i : SV_InstanceID) : SV_Position {
|
||||||
|
float2 p = float2((v & 1) ? 0.05 : 0.0, (v & 2) ? 0.05 : 0.0) - 0.5;
|
||||||
|
return float4(p, 0.5, 1);
|
||||||
|
}
|
||||||
|
float4 ps() : SV_Target { return float4(1, 0.5, 0.25, 1); }
|
||||||
|
"#;
|
||||||
|
|
||||||
|
fn compile(entry: &str, target: &str) -> Vec<u8> {
|
||||||
|
let entry = std::ffi::CString::new(entry).unwrap();
|
||||||
|
let target = std::ffi::CString::new(target).unwrap();
|
||||||
|
let mut code: Option<ID3DBlob> = None;
|
||||||
|
// SAFETY: the source and names outlive the call.
|
||||||
|
unsafe {
|
||||||
|
D3DCompile(
|
||||||
|
SHADERS.as_ptr().cast(),
|
||||||
|
SHADERS.len(),
|
||||||
|
s!("bench"),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
PCSTR(entry.as_ptr().cast()),
|
||||||
|
PCSTR(target.as_ptr().cast()),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
&mut code,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let code = code.unwrap();
|
||||||
|
std::slice::from_raw_parts(code.GetBufferPointer().cast::<u8>(), code.GetBufferSize())
|
||||||
|
.to_vec()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The clamp as it was before it charged the budget once per group
|
||||||
|
/// (record by record, every thread racing for the budget word), kept
|
||||||
|
/// here to measure against.
|
||||||
|
const OLD_SOURCE: &str = r#"
|
||||||
|
#define RS "RootConstants(num32BitConstants=6, b0), SRV(t0), SRV(t1), UAV(u0), UAV(u1)"
|
||||||
|
cbuffer Record : register(b0) {
|
||||||
|
uint kind;
|
||||||
|
uint stride;
|
||||||
|
uint records;
|
||||||
|
uint args_offset;
|
||||||
|
// Bit 0: a count buffer bounds the records. Bit 1: the records are a
|
||||||
|
// window clamped ahead of their draws: one that asks for more than can
|
||||||
|
// run is left empty and costs nothing.
|
||||||
|
uint has_count;
|
||||||
|
uint count_offset;
|
||||||
|
};
|
||||||
|
ByteAddressBuffer src_args : register(t0);
|
||||||
|
ByteAddressBuffer count_buf : register(t1);
|
||||||
|
RWByteAddressBuffer dst_args : register(u0);
|
||||||
|
// The list's work so far (vertex instances, threads): past the budget,
|
||||||
|
// records run empty.
|
||||||
|
RWByteAddressBuffer budget : register(u1);
|
||||||
|
static const uint BUDGET = 1u << 27;
|
||||||
|
// Groups of one dispatch; more run empty.
|
||||||
|
static const uint GROUPS = 1u << 20;
|
||||||
|
[RootSignature(RS)]
|
||||||
|
[numthreads(64, 1, 1)]
|
||||||
|
void main(uint3 id : SV_DispatchThreadID) {
|
||||||
|
uint eff_count = records;
|
||||||
|
bool ahead = (has_count & 2u) != 0;
|
||||||
|
if ((has_count & 1u) != 0) {
|
||||||
|
eff_count = min(eff_count, count_buf.Load(count_offset));
|
||||||
|
}
|
||||||
|
if (id.x >= eff_count) return;
|
||||||
|
uint src_at = args_offset + id.x * stride;
|
||||||
|
uint dst_at = id.x * stride;
|
||||||
|
uint cost;
|
||||||
|
uint2 draw_counts = uint2(0, 0);
|
||||||
|
uint3 groups = uint3(0, 0, 0);
|
||||||
|
if (kind <= 1) {
|
||||||
|
// Draws: vertex or index count, then instances.
|
||||||
|
uint2 asked = src_args.Load2(src_at);
|
||||||
|
if (ahead && (asked.x > (1u << 24) || asked.y > (1u << 24))) {
|
||||||
|
asked = uint2(0, 0);
|
||||||
|
}
|
||||||
|
uint count = min(asked.x, 1u << 24);
|
||||||
|
uint instances = min(asked.y, (1u << 26) / max(count, 1u));
|
||||||
|
cost = count * instances;
|
||||||
|
draw_counts = uint2(count, instances);
|
||||||
|
} else if (kind == 2) {
|
||||||
|
uint3 g = min(src_args.Load3(src_at), uint3(65535u, 65535u, 65535u));
|
||||||
|
// x * y fits 32 bits; the whole count may not.
|
||||||
|
if (g.z != 0 && g.x * g.y > GROUPS / g.z) {
|
||||||
|
g = uint3(0, 0, 0);
|
||||||
|
}
|
||||||
|
cost = g.x * g.y * g.z * 64u;
|
||||||
|
groups = g;
|
||||||
|
} else {
|
||||||
|
// Mesh dispatches: the console's are one-dimensional.
|
||||||
|
uint3 g = src_args.Load3(src_at);
|
||||||
|
// Ahead of its draw, a record over the most a dispatch runs is a
|
||||||
|
// slot the title has not filled (the title leaves all
|
||||||
|
// ones there): empty, and not counted below either.
|
||||||
|
if (ahead && g.x > 65535u) {
|
||||||
|
g = uint3(0, 0, 0);
|
||||||
|
}
|
||||||
|
// Diagnostics, in the budget buffer's next words: the largest
|
||||||
|
// group count a mesh record asked for, and the groups asked in all.
|
||||||
|
uint was;
|
||||||
|
budget.InterlockedMax(4, g.x, was);
|
||||||
|
budget.InterlockedAdd(8, min(g.x, 1u << 24), was);
|
||||||
|
g = uint3(min(g.x, 65535u), min(g.y, 1u), min(g.z, 1u));
|
||||||
|
cost = g.x * g.y * g.z * 128u;
|
||||||
|
groups = g;
|
||||||
|
}
|
||||||
|
bool over = false;
|
||||||
|
if (cost > 0) {
|
||||||
|
uint before;
|
||||||
|
budget.InterlockedOr(0, 0, before);
|
||||||
|
[allow_uav_condition]
|
||||||
|
while (before < BUDGET) {
|
||||||
|
uint next = (BUDGET - before <= cost) ? BUDGET : (before + cost);
|
||||||
|
uint prev;
|
||||||
|
budget.InterlockedCompareExchange(0, before, next, prev);
|
||||||
|
if (prev == before) break;
|
||||||
|
before = prev;
|
||||||
|
}
|
||||||
|
over = before >= BUDGET || BUDGET - before < cost;
|
||||||
|
}
|
||||||
|
if (kind <= 1) {
|
||||||
|
if (over) {
|
||||||
|
draw_counts = uint2(0, 0);
|
||||||
|
}
|
||||||
|
dst_args.Store2(dst_at, draw_counts);
|
||||||
|
for (uint off = 8; off < stride; off += 4) {
|
||||||
|
dst_args.Store(dst_at + off, src_args.Load(src_at + off));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (over) {
|
||||||
|
groups = uint3(0, 0, 0);
|
||||||
|
}
|
||||||
|
dst_args.Store3(dst_at, groups);
|
||||||
|
for (uint off = 12; off < stride; off += 4) {
|
||||||
|
dst_args.Store(dst_at + off, src_args.Load(src_at + off));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
|
enum Barriers {
|
||||||
|
/// Back to the common state after every execution (as before).
|
||||||
|
Old,
|
||||||
|
/// Readable as arguments until the next clamp, common at the end.
|
||||||
|
New,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore = "benchmark; see the module notes"]
|
||||||
|
fn bench_indirect_draw_barriers() {
|
||||||
|
let Some(host) = super::host() else {
|
||||||
|
eprintln!("no host GPU");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let device = &host.device;
|
||||||
|
let clamps = [
|
||||||
|
make_from(device, OLD_SOURCE).expect("the old clamp"),
|
||||||
|
make_for(device).expect("the clamp"),
|
||||||
|
];
|
||||||
|
// SAFETY: plain D3D12 object creation with valid descriptions; the
|
||||||
|
// objects outlive every list recorded and run below.
|
||||||
|
unsafe {
|
||||||
|
let adapter = {
|
||||||
|
let luid = device.GetAdapterLuid();
|
||||||
|
CreateDXGIFactory2::<IDXGIFactory4>(DXGI_CREATE_FACTORY_FLAGS(0))
|
||||||
|
.and_then(|f| {
|
||||||
|
f.EnumAdapterByLuid::<windows::Win32::Graphics::Dxgi::IDXGIAdapter1>(luid)
|
||||||
|
})
|
||||||
|
.and_then(|a| a.GetDesc1())
|
||||||
|
.map(|d| {
|
||||||
|
String::from_utf16_lossy(&d.Description)
|
||||||
|
.trim_end_matches('\0')
|
||||||
|
.to_owned()
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|_| "an unnamed adapter".to_owned())
|
||||||
|
};
|
||||||
|
let queue: ID3D12CommandQueue = device
|
||||||
|
.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC {
|
||||||
|
Type: D3D12_COMMAND_LIST_TYPE_DIRECT,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let allocator: ID3D12CommandAllocator = device
|
||||||
|
.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT)
|
||||||
|
.unwrap();
|
||||||
|
let list: ID3D12GraphicsCommandList = device
|
||||||
|
.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &allocator, None)
|
||||||
|
.unwrap();
|
||||||
|
list.Close().unwrap();
|
||||||
|
let fence: ID3D12Fence = device.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
|
||||||
|
let event = CreateEventW(None, false, false, None).unwrap();
|
||||||
|
let frequency = queue.GetTimestampFrequency().unwrap() as f64;
|
||||||
|
|
||||||
|
// The pipeline: tiny triangles into a small target.
|
||||||
|
let (vs, ps) = (compile("vs", "vs_5_1"), compile("ps", "ps_5_1"));
|
||||||
|
let root: ID3D12RootSignature = device.CreateRootSignature(0, &vs).unwrap();
|
||||||
|
let mut desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC {
|
||||||
|
pRootSignature: std::mem::transmute_copy(&root),
|
||||||
|
VS: D3D12_SHADER_BYTECODE {
|
||||||
|
pShaderBytecode: vs.as_ptr().cast(),
|
||||||
|
BytecodeLength: vs.len(),
|
||||||
|
},
|
||||||
|
PS: D3D12_SHADER_BYTECODE {
|
||||||
|
pShaderBytecode: ps.as_ptr().cast(),
|
||||||
|
BytecodeLength: ps.len(),
|
||||||
|
},
|
||||||
|
SampleMask: u32::MAX,
|
||||||
|
RasterizerState: D3D12_RASTERIZER_DESC {
|
||||||
|
FillMode: D3D12_FILL_MODE_SOLID,
|
||||||
|
CullMode: D3D12_CULL_MODE_NONE,
|
||||||
|
DepthClipEnable: true.into(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
PrimitiveTopologyType: D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
|
||||||
|
NumRenderTargets: 1,
|
||||||
|
SampleDesc: DXGI_SAMPLE_DESC {
|
||||||
|
Count: 1,
|
||||||
|
Quality: 0,
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
desc.BlendState.RenderTarget[0].RenderTargetWriteMask =
|
||||||
|
D3D12_COLOR_WRITE_ENABLE_ALL.0 as u8;
|
||||||
|
desc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||||
|
let pso: ID3D12PipelineState = device.CreateGraphicsPipelineState(&desc).unwrap();
|
||||||
|
let target_desc = D3D12_RESOURCE_DESC {
|
||||||
|
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
|
||||||
|
Width: 256,
|
||||||
|
Height: 256,
|
||||||
|
DepthOrArraySize: 1,
|
||||||
|
MipLevels: 1,
|
||||||
|
Format: DXGI_FORMAT_R8G8B8A8_UNORM,
|
||||||
|
SampleDesc: DXGI_SAMPLE_DESC {
|
||||||
|
Count: 1,
|
||||||
|
Quality: 0,
|
||||||
|
},
|
||||||
|
Flags: D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let mut target: Option<ID3D12Resource> = None;
|
||||||
|
device
|
||||||
|
.CreateCommittedResource(
|
||||||
|
&D3D12_HEAP_PROPERTIES {
|
||||||
|
Type: D3D12_HEAP_TYPE_DEFAULT,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
D3D12_HEAP_FLAG_NONE,
|
||||||
|
&target_desc,
|
||||||
|
D3D12_RESOURCE_STATE_RENDER_TARGET,
|
||||||
|
None,
|
||||||
|
&mut target,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let target = target.unwrap();
|
||||||
|
let rtvs: ID3D12DescriptorHeap = device
|
||||||
|
.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC {
|
||||||
|
Type: D3D12_DESCRIPTOR_HEAP_TYPE_RTV,
|
||||||
|
NumDescriptors: 1,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
let rtv = rtvs.GetCPUDescriptorHandleForHeapStart();
|
||||||
|
device.CreateRenderTargetView(&target, None, rtv);
|
||||||
|
|
||||||
|
// The title's records: three vertices, one instance, 32 bytes apart.
|
||||||
|
let (src, src_ptr) = host.shared_buffer(u64::from(DRAWS * STEP)).unwrap();
|
||||||
|
for i in 0..DRAWS as usize {
|
||||||
|
let words = [3u32, 1, 0, 0];
|
||||||
|
std::ptr::copy_nonoverlapping(
|
||||||
|
words.as_ptr().cast::<u8>(),
|
||||||
|
src_ptr.add(i * STEP as usize),
|
||||||
|
16,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let (budget, budget_ptr) = host.shared_buffer(256).unwrap();
|
||||||
|
let budget_gpu = budget.GetGPUVirtualAddress();
|
||||||
|
// The same words in the card's own memory (zeroed when made; the
|
||||||
|
// runs below charge far less than the budget).
|
||||||
|
let card_budget = host.uav_buffer(256).unwrap();
|
||||||
|
let card_budget_gpu = card_budget.GetGPUVirtualAddress();
|
||||||
|
let scratch = host
|
||||||
|
.uav_buffer(u64::from(WINDOW * STEP).max(64 << 10))
|
||||||
|
.unwrap();
|
||||||
|
let signature = {
|
||||||
|
let arg = D3D12_INDIRECT_ARGUMENT_DESC {
|
||||||
|
Type: D3D12_INDIRECT_ARGUMENT_TYPE_DRAW,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let desc = D3D12_COMMAND_SIGNATURE_DESC {
|
||||||
|
ByteStride: 16,
|
||||||
|
NumArgumentDescs: 1,
|
||||||
|
pArgumentDescs: &arg,
|
||||||
|
NodeMask: 0,
|
||||||
|
};
|
||||||
|
let mut sig: Option<ID3D12CommandSignature> = None;
|
||||||
|
device
|
||||||
|
.CreateCommandSignature(&desc, None::<&ID3D12RootSignature>, &mut sig)
|
||||||
|
.unwrap();
|
||||||
|
sig.unwrap()
|
||||||
|
};
|
||||||
|
let stamps: ID3D12QueryHeap = {
|
||||||
|
let mut heap: Option<ID3D12QueryHeap> = None;
|
||||||
|
device
|
||||||
|
.CreateQueryHeap(
|
||||||
|
&D3D12_QUERY_HEAP_DESC {
|
||||||
|
Type: D3D12_QUERY_HEAP_TYPE_TIMESTAMP,
|
||||||
|
Count: 2,
|
||||||
|
NodeMask: 0,
|
||||||
|
},
|
||||||
|
&mut heap,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
heap.unwrap()
|
||||||
|
};
|
||||||
|
let mut back: Option<ID3D12Resource> = None;
|
||||||
|
device
|
||||||
|
.CreateCommittedResource(
|
||||||
|
&D3D12_HEAP_PROPERTIES {
|
||||||
|
Type: D3D12_HEAP_TYPE_READBACK,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
D3D12_HEAP_FLAG_NONE,
|
||||||
|
&D3D12_RESOURCE_DESC {
|
||||||
|
Dimension: D3D12_RESOURCE_DIMENSION_BUFFER,
|
||||||
|
Width: 16,
|
||||||
|
Height: 1,
|
||||||
|
DepthOrArraySize: 1,
|
||||||
|
MipLevels: 1,
|
||||||
|
SampleDesc: DXGI_SAMPLE_DESC {
|
||||||
|
Count: 1,
|
||||||
|
Quality: 0,
|
||||||
|
},
|
||||||
|
Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
D3D12_RESOURCE_STATE_COPY_DEST,
|
||||||
|
None,
|
||||||
|
&mut back,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let back = back.unwrap();
|
||||||
|
let mut submitted = 0u64;
|
||||||
|
|
||||||
|
// One frame's lists: GPU ms (timestamps around the list), CPU ms
|
||||||
|
// recording it, ms from submission to the end of its run.
|
||||||
|
let mut frame = |barriers: Barriers, clamp: &Clamp, window: u32, card: bool| -> [f64; 3] {
|
||||||
|
allocator.Reset().unwrap();
|
||||||
|
list.Reset(&allocator, None).unwrap();
|
||||||
|
let recording = std::time::Instant::now();
|
||||||
|
list.EndQuery(&stamps, D3D12_QUERY_TYPE_TIMESTAMP, 0);
|
||||||
|
list.OMSetRenderTargets(1, Some(&rtv), false, None);
|
||||||
|
list.RSSetViewports(&[D3D12_VIEWPORT {
|
||||||
|
Width: 256.0,
|
||||||
|
Height: 256.0,
|
||||||
|
MaxDepth: 1.0,
|
||||||
|
..Default::default()
|
||||||
|
}]);
|
||||||
|
list.RSSetScissorRects(&[RECT {
|
||||||
|
left: 0,
|
||||||
|
top: 0,
|
||||||
|
right: 256,
|
||||||
|
bottom: 256,
|
||||||
|
}]);
|
||||||
|
list.IASetPrimitiveTopology(
|
||||||
|
windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST,
|
||||||
|
);
|
||||||
|
list.SetGraphicsRootSignature(&root);
|
||||||
|
let mut state = D3D12_RESOURCE_STATE_COMMON;
|
||||||
|
for first in (0..DRAWS).step_by(window as usize) {
|
||||||
|
let records = window.min(DRAWS - first);
|
||||||
|
assert!(record_clamp_as(
|
||||||
|
clamp,
|
||||||
|
&list,
|
||||||
|
(&scratch, state),
|
||||||
|
if card { card_budget_gpu } else { budget_gpu },
|
||||||
|
(&src, u64::from(first * STEP)),
|
||||||
|
None,
|
||||||
|
(0, STEP, records),
|
||||||
|
window > 1,
|
||||||
|
));
|
||||||
|
state = D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT;
|
||||||
|
list.SetPipelineState(&pso);
|
||||||
|
for k in 0..records {
|
||||||
|
list.ExecuteIndirect(&signature, 1, &scratch, u64::from(k * STEP), None, 0);
|
||||||
|
if barriers == Barriers::Old {
|
||||||
|
list.ResourceBarrier(&[transition(
|
||||||
|
&scratch,
|
||||||
|
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
||||||
|
D3D12_RESOURCE_STATE_COMMON,
|
||||||
|
)]);
|
||||||
|
state = D3D12_RESOURCE_STATE_COMMON;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if state != D3D12_RESOURCE_STATE_COMMON {
|
||||||
|
list.ResourceBarrier(&[transition(&scratch, state, D3D12_RESOURCE_STATE_COMMON)]);
|
||||||
|
}
|
||||||
|
list.EndQuery(&stamps, D3D12_QUERY_TYPE_TIMESTAMP, 1);
|
||||||
|
list.ResolveQueryData(&stamps, D3D12_QUERY_TYPE_TIMESTAMP, 0, 2, &back, 0);
|
||||||
|
list.Close().unwrap();
|
||||||
|
let recorded = recording.elapsed().as_secs_f64() * 1e3;
|
||||||
|
let running = std::time::Instant::now();
|
||||||
|
queue.ExecuteCommandLists(&[Some(list.cast::<ID3D12CommandList>().unwrap())]);
|
||||||
|
submitted += 1;
|
||||||
|
queue.Signal(&fence, submitted).unwrap();
|
||||||
|
fence.SetEventOnCompletion(submitted, event).unwrap();
|
||||||
|
WaitForSingleObject(event, INFINITE);
|
||||||
|
let ran = running.elapsed().as_secs_f64() * 1e3;
|
||||||
|
let mut data = std::ptr::null_mut();
|
||||||
|
back.Map(0, None, Some(&mut data)).unwrap();
|
||||||
|
let t = std::slice::from_raw_parts(data.cast::<u64>(), 2);
|
||||||
|
let gpu = t[1].saturating_sub(t[0]) as f64 / frequency * 1e3;
|
||||||
|
back.Unmap(0, None);
|
||||||
|
(budget_ptr as *mut u32).write_volatile(0);
|
||||||
|
[gpu, recorded, ran]
|
||||||
|
};
|
||||||
|
|
||||||
|
// What `main` records, then this branch; the windows a step at a
|
||||||
|
// time (barriers, then the clamp's one charge per group).
|
||||||
|
let (old, new) = (&clamps[0], &clamps[1]);
|
||||||
|
// The last two keep the budget in the card's memory instead of
|
||||||
|
// memory the CPU maps (where the list's budget is now).
|
||||||
|
let cases: [(&str, Barriers, &Clamp, u32, bool); 7] = [
|
||||||
|
("one at a time, main", Barriers::Old, old, 1, false),
|
||||||
|
("one at a time, this", Barriers::New, new, 1, false),
|
||||||
|
("windows of 32, main", Barriers::Old, old, WINDOW, false),
|
||||||
|
("windows, new barriers", Barriers::New, old, WINDOW, false),
|
||||||
|
("windows of 32, this", Barriers::New, new, WINDOW, false),
|
||||||
|
("one at a time, card", Barriers::New, new, 1, true),
|
||||||
|
("windows of 32, card", Barriers::New, new, WINDOW, true),
|
||||||
|
];
|
||||||
|
// Warm up, then the cases in turn, so drift falls on all alike.
|
||||||
|
for &(_, barriers, clamp, window, card) in &cases {
|
||||||
|
frame(barriers, clamp, window, card);
|
||||||
|
}
|
||||||
|
let mut times: Vec<Vec<[f64; 3]>> = vec![Vec::new(); cases.len()];
|
||||||
|
for _ in 0..RUNS {
|
||||||
|
for (i, &(_, barriers, clamp, window, card)) in cases.iter().enumerate() {
|
||||||
|
times[i].push(frame(barriers, clamp, window, card));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let median = |v: &[[f64; 3]], k: usize| {
|
||||||
|
let mut x: Vec<f64> = v.iter().map(|t| t[k]).collect();
|
||||||
|
x.sort_by(f64::total_cmp);
|
||||||
|
x[x.len() / 2]
|
||||||
|
};
|
||||||
|
eprintln!(
|
||||||
|
"indirect draw barriers on {adapter}: {DRAWS} one-record draws a frame, \
|
||||||
|
medians of {RUNS} frames"
|
||||||
|
);
|
||||||
|
for (i, (name, ..)) in cases.iter().enumerate() {
|
||||||
|
let gpu = median(×[i], 0);
|
||||||
|
eprintln!(
|
||||||
|
" {name:<22} GPU {gpu:7.2} ms ({:5.2} us a draw), recording {:6.2} ms, \
|
||||||
|
submit to done {:7.2} ms",
|
||||||
|
gpu * 1e3 / f64::from(DRAWS),
|
||||||
|
median(×[i], 1),
|
||||||
|
median(×[i], 2),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for (from, to) in [(0, 1), (2, 3), (3, 4), (2, 4), (1, 5), (4, 6)] {
|
||||||
|
let (a, b) = (median(×[from], 0), median(×[to], 0));
|
||||||
|
eprintln!(
|
||||||
|
" {} -> {}: GPU time {:+.1} %",
|
||||||
|
cases[from].0,
|
||||||
|
cases[to].0,
|
||||||
|
(b / a - 1.0) * 100.0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user