Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
502 changes: 451 additions & 51 deletions d3d12/crates/leonida-gpu/src/draw.rs

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions d3d12/crates/leonida-gpu/src/indirect/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ impl Table {
}
Some(place(i))
}

/// Diagnostics (`LEONIDA_BATCH_WHY`, `crate::draw`): whether [`Table::place_packed`] has no
/// place for a record of the host copy `id` because every job is taken: the pass reads that
/// copy and has [`JOBS`] jobs. (Else the record is in another source, or at an offset the
/// pass does not take.)
#[cfg(any(test, feature = "diagnostics"))]
pub(crate) fn full_for(&self, id: u64) -> bool {
self.sources.contains(&id) && self.times.len() as u32 >= JOBS
}
}

/// What the pass waits for before it reads the records ([`record`]).
Expand Down Expand Up @@ -393,3 +402,28 @@ pub(super) fn record(
l.ResourceBarrier(&[after]);
}
}

#[cfg(test)]
mod tests {
use super::{JOBS, TABLE, Table};

/// A batch with no place for an execution's record either does not read the copy the record
/// is in or has every job taken: `LEONIDA_BATCH_WHY` counts the two apart.
#[test]
fn a_table_is_full_for_a_copy_it_reads_once_every_job_is_taken() {
const DISPATCH: u32 = 2;
let mut memory = vec![0u32; (TABLE / 4) as usize];
// SAFETY: TABLE bytes on 4, alive past the table, read by no list.
let mut table = unsafe { Table::new(memory.as_mut_ptr().cast(), 0, [7, 9, 0, 0]) };
for i in 0..JOBS {
assert!(!table.full_for(7), "{i} jobs");
assert!(table.place(7, u64::from(i) * 32, DISPATCH).is_some());
}
assert!(table.place(7, u64::from(JOBS) * 32, DISPATCH).is_none());
assert!(table.full_for(7));
assert!(table.full_for(9), "another copy the pass reads");
assert!(!table.full_for(8), "a copy the pass does not read: another source");
// A record it has already is still placed.
assert!(table.place(7, 0, DISPATCH).is_some());
}
}
12 changes: 12 additions & 0 deletions d3d12/crates/leonida-gpu/src/picked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ pub(crate) fn picks(shader: &Arc<crate::pipeline::Shader>, known: &AtomicU8) ->
}
}

/// Diagnostics (`LEONIDA_BATCH_WHY`, `crate::draw`): what [`picks`] says of `shader`, asked
/// for if nobody has yet: `None` until its thread has looked.
#[cfg(feature = "diagnostics")]
pub(crate) fn answer(shader: &Arc<crate::pipeline::Shader>, known: &AtomicU8) -> Option<bool> {
picks(shader, known);
match known.load(Relaxed) {
YES => Some(true),
NO => Some(false),
_ => None,
}
}

fn ask(shader: Arc<crate::pipeline::Shader>) {
use std::sync::mpsc::{Sender, channel};
static ASKS: OnceLock<Mutex<Sender<Arc<crate::pipeline::Shader>>>> = OnceLock::new();
Expand Down
199 changes: 199 additions & 0 deletions d3d12/crates/leonida-gpu/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,18 @@ pub(crate) fn stores_bound(shader: &Arc<Shader>, t: &Translated) -> bool {
}
}

/// Diagnostics (`LEONIDA_BATCH_WHY`, `crate::draw`): what [`stores_bound`] says of `shader`
/// (translated as `t`), asked for if nobody has yet: `None` until its thread has looked.
#[cfg(all(windows, feature = "diagnostics"))]
pub(crate) fn bound_answer(shader: &Arc<Shader>, t: &Translated) -> Option<bool> {
stores_bound(shader, t);
match t.bound.load(std::sync::atomic::Ordering::Relaxed) {
BOUND_YES => Some(true),
BOUND_NO => Some(false),
_ => None,
}
}

#[cfg(windows)]
fn ask_bound(shader: Arc<Shader>) {
use std::sync::atomic::Ordering::Relaxed;
Expand Down Expand Up @@ -1269,7 +1281,9 @@ pub fn save_pipeline_cache() {
/// Which stores [`stores_in_bound_views`] takes as declared at a bind (`LEONIDA_INDIRECT_BATCH=4`).
#[cfg(all(test, windows))]
mod bound_tests {
use leonida_shader::gfx10::Fields;
use leonida_shader::native::{ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource};
use leonida_shader::resource::{Role, Source};

fn used(kind: ResourceType, root: RootType, at: u32) -> UsedResource {
UsedResource { kind, root, table_offset_dwords: at, lower_bound: 0, stride: 4, dwords: 4, user_data: 4, slot: 1 }
Expand Down Expand Up @@ -1343,6 +1357,191 @@ mod bound_tests {
ps.stage = HwStage::Ps;
assert!(!super::bound_programs(&[bound.clone(), ps]), "a second storing program the host does not bind by");
}

/// A program that also has the view heap among its parameters (in s6, after the table in s4):
/// it loads a buffer view from the heap at a slot it computes, reads through it, and stores
/// what it read through the descriptor in s[`at`..`at` + 4]: 12, the table's descriptor 1 (an
/// entry of unordered access), or 16, the one it loaded from the heap.
fn heap_reader(at: u32) -> NativeProgram {
let code = [
0xBE85_0380, // s_mov_b32 s5, 0
0xBE87_0380, // s_mov_b32 s7, 0
0x8F08_8500, // s_lshl_b32 s8, s0, 5 (a slot of the heap, 32 bytes each)
0xF408_0403,
0x08 << 25, // s_load_dwordx4 s[16:19], s[6:7], s8
0xF408_0302,
0x7D << 25 | 0x20, // s_load_dwordx4 s[12:15], s[4:5], 0x20
0x342A_0084, // v_lshlrev_b32 v21, 4, v0
0xE038_1000,
0x8004_0D15, // buffer_load_dwordx4 v[13:16], v21, s[16:19], 0 offen
0xE078_1000,
0x8000_0D15 | (at / 4) << 16, // buffer_store_dwordx4 v[13:16], v21, s[at:at+3], 0 offen
];
with_heap(program(RootType::Table, vec![used(ResourceType::Uav, RootType::Table, 8)], &code), 6)
}

/// `p` with the view heap among its parameters too (a `ResourceHeap` root parameter in one
/// register, at `user_data`), its user registers reaching to it.
fn with_heap(mut p: NativeProgram, user_data: u8) -> NativeProgram {
p.layout.push(RootParam { root: RootType::ResourceHeap, table_offset_dwords: 0, sgprs: 1, base_register: 0, user_data, slot: 2 });
p.io = StageIo::Cs(ComputeIo { rsrc2: (u32::from(user_data) + 1) << 1, threads: [32, 1, 1] });
p
}

/// What resource tracking finds for the descriptor of `p`'s buffer store, and for each source
/// it picks from, the root parameter of a descriptor loaded from one and whether at an offset
/// the program computes (`None` for any other source).
fn store_traced(p: &NativeProgram) -> (Source, Vec<Option<(usize, bool)>>) {
let built = leonida_shader::spill::build(p).expect("builds");
let res = leonida_shader::resource::track(&built, &p.layout, p.stage, None).expect("tracked");
let d = res
.descriptors
.iter()
.find(|d| {
let inst = &built.blocks[d.site.0].insts[d.site.1];
d.role == Role::Resource && matches!(inst.fields, Fields::Buffer { .. }) && inst.name().contains("store")
})
.expect("the store's descriptor");
let leaves = d
.source
.leaves()
.into_iter()
.map(|leaf| match leaf {
Source::Loaded { load, .. } => {
res.scalar.iter().find(|(site, _)| site == load).map(|(_, a)| (a.param, a.dynamic.is_some()))
}
_ => None,
})
.collect();
(d.source.clone(), leaves)
}

/// A store through a descriptor made of numbers in the code (neither a table entry nor a root
/// view): declared nowhere.
#[test]
fn a_store_through_a_descriptor_made_in_the_code_is_not_declared() {
let code = [
0xBE8C_03FF,
0x0000_1000, // s_mov_b32 s12, 0x1000
0xBE8D_0380, // s_mov_b32 s13, 0
0xBE8E_03C1, // s_mov_b32 s14, -1
0xBE8F_03FF,
0x0002_7FAC, // s_mov_b32 s15, 0x27fac
0x342A_0084, // v_lshlrev_b32 v21, 4, v0
0xE078_1000,
0x8003_0D15, // buffer_store_dwordx4 v[13:16], v21, s[12:15], 0 offen
];
let p = program(RootType::Table, vec![used(ResourceType::Uav, RootType::Table, 8)], &code);
assert!(super::stores(&p));
let (source, _) = store_traced(&p);
assert!(!matches!(source, Source::Loaded { .. } | Source::Built { .. }), "{source:?}");
assert!(!super::stores_in_bound_views(&p));
}

/// A store through the table's descriptor 1 loaded at an offset the program computes (s8, from
/// s0): any of the table's, whether or not the program also has the view heap.
#[test]
fn a_store_at_a_table_offset_the_program_computes_is_not_declared() {
let alone = [
0xBE85_0380, // s_mov_b32 s5, 0
0x8F08_8500, // s_lshl_b32 s8, s0, 5
0xF408_0302,
0x08 << 25 | 0x20, // s_load_dwordx4 s[12:15], s[4:5], s8 + 0x20
0x342A_0084, // v_lshlrev_b32 v21, 4, v0
0xE078_1000,
0x8003_0D15, // buffer_store_dwordx4 v[13:16], v21, s[12:15], 0 offen
];
// The same beside a read through a view loaded from the heap in s6.
let beside_heap = [
0xBE85_0380, // s_mov_b32 s5, 0
0xBE87_0380, // s_mov_b32 s7, 0
0x8F08_8500, // s_lshl_b32 s8, s0, 5
0xF408_0403,
0x08 << 25, // s_load_dwordx4 s[16:19], s[6:7], s8
0xF408_0302,
0x08 << 25 | 0x20, // s_load_dwordx4 s[12:15], s[4:5], s8 + 0x20
0x342A_0084, // v_lshlrev_b32 v21, 4, v0
0xE038_1000,
0x8004_0D15, // buffer_load_dwordx4 v[13:16], v21, s[16:19], 0 offen
0xE078_1000,
0x8003_0D15, // buffer_store_dwordx4 v[13:16], v21, s[12:15], 0 offen
];
let table = || vec![used(ResourceType::Uav, RootType::Table, 8)];
for p in [program(RootType::Table, table(), &alone), with_heap(program(RootType::Table, table(), &beside_heap), 6)] {
assert!(super::stores(&p));
assert_eq!(store_traced(&p).1, [Some((1, true))], "the table's, at a computed offset");
assert!(!super::stores_in_bound_views(&p));
}
}

/// A store through a descriptor chosen (`s_cselect_b64`) between the table's entry of
/// unordered access and one loaded from the heap: not declared, though one of the two is.
#[test]
fn a_store_through_a_descriptor_chosen_between_the_table_and_the_heap_is_not_declared() {
let code = [
0xBE85_0380, // s_mov_b32 s5, 0
0xBE87_0380, // s_mov_b32 s7, 0
0x8F08_8500, // s_lshl_b32 s8, s0, 5
0xF408_0403,
0x08 << 25, // s_load_dwordx4 s[16:19], s[6:7], s8
0xF408_0302,
0x7D << 25 | 0x20, // s_load_dwordx4 s[12:15], s[4:5], 0x20
0xBF06_8001, // s_cmp_eq_u32 s1, 0
0x8594_100C, // s_cselect_b64 s[20:21], s[12:13], s[16:17]
0x8596_120E, // s_cselect_b64 s[22:23], s[14:15], s[18:19]
0x342A_0084, // v_lshlrev_b32 v21, 4, v0
0xE078_1000,
0x8005_0D15, // buffer_store_dwordx4 v[13:16], v21, s[20:23], 0 offen
];
let p = with_heap(program(RootType::Table, vec![used(ResourceType::Uav, RootType::Table, 8)], &code), 6);
assert!(super::stores(&p));
let (source, leaves) = store_traced(&p);
assert!(matches!(source, Source::Select { .. }), "{source:?}");
assert_eq!(leaves, [Some((1, false)), Some((2, true))], "the table's entry, then the heap's");
assert!(!super::stores_in_bound_views(&p));
}

/// A program that reads through a view it loads from the heap (s8) and stores through a
/// descriptor built around its root view of unordered access (s[4:5]): declared.
#[test]
fn reads_from_the_heap_leave_a_store_through_a_root_view_declared() {
let code = [
0xBE89_0380, // s_mov_b32 s9, 0
0x8F0A_8500, // s_lshl_b32 s10, s0, 5
0xF408_0404,
0x0A << 25, // s_load_dwordx4 s[16:19], s[8:9], s10
0x342A_0084, // v_lshlrev_b32 v21, 4, v0
0xE038_1000,
0x8004_0D15, // buffer_load_dwordx4 v[13:16], v21, s[16:19], 0 offen
0xBE86_03C1, // s_mov_b32 s6, -1
0xBE87_03FF,
0x0002_7FAC, // s_mov_b32 s7, 0x27fac
0xE078_1000,
0x8001_0D15, // buffer_store_dwordx4 v[13:16], v21, s[4:7], 0 offen
];
let p = with_heap(program(RootType::Descriptor, vec![used(ResourceType::Uav, RootType::Descriptor, 0)], &code), 8);
assert!(super::stores(&p));
assert!(matches!(store_traced(&p).0, Source::Built { .. }));
assert!(super::stores_in_bound_views(&p));
// The same root view named as read only: the bind declares nothing for it.
let read_only = with_heap(program(RootType::Descriptor, vec![used(ResourceType::Srv, RootType::Descriptor, 0)], &code), 8);
assert!(!super::stores_in_bound_views(&read_only));
}

/// What a program reads through views it loads from the heap is no store: its stores through
/// the table entry its bind declares are declared all the same (`crate::draw::stores_declared`,
/// `LEONIDA_BATCH_HEAP_READS`); a store through the view it loaded is not.
#[test]
fn reads_through_views_loaded_from_the_heap_leave_bound_stores_declared() {
let reads = heap_reader(12);
assert!(super::stores(&reads), "the program stores");
assert!(super::stores_in_bound_views(&reads));
assert!(super::bound_programs(std::slice::from_ref(&reads)));
let stores = heap_reader(16);
assert!(super::stores(&stores));
assert!(!super::stores_in_bound_views(&stores));
assert!(!super::bound_programs(std::slice::from_ref(&stores)));
}
}

#[cfg(test)]
Expand Down
Loading