Skip to content
Merged
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
30 changes: 23 additions & 7 deletions crates/victory-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub(crate) struct HostHeap {
/// taken, those given back, and where they end.
pairs: Mutex<(u32, Vec<u32>)>,
pairs_end: u32,
/// Null raw SRV for explicitly bound root descriptors. Its UAV is one slot below.
null_root_srv: Option<u32>,
/// View heaps: the view-stride table translated shaders read
/// (`victory_shader::emit::VIEW_STRIDES`), a dword per title slot: the
/// byte stride of the buffer view there, 0 for none. Mapped at the
Expand Down Expand Up @@ -199,7 +201,7 @@ impl HostHeap {
u64::from(step),
sampler,
));
let h = HostHeap {
let mut h = HostHeap {
heap,
cpu,
step,
Expand All @@ -210,6 +212,7 @@ impl HostHeap {
kept: Mutex::new(Paged::new((count + extra) as usize)),
next: Mutex::new((0, vec![false; extra as usize])),
pairs: Mutex::new((count + extra, Vec::new())),
null_root_srv: None,
pairs_end: match victory_shader::emit::NULL_BUFFER_VIEW {
null if !sampler && null > count + extra && null < total => null - 1,
_ => total,
Expand All @@ -234,16 +237,29 @@ impl HostHeap {
// What shaders read instead of a slot without a buffer view where
// they take one at a computed index (`victory_shader::emit`, "Buffer
// views at computed heap indices"): a null raw SRV, and UAV below it.
let null = victory_shader::emit::NULL_BUFFER_VIEW;
if !sampler && null > count + extra && null < total {
h.raw_view_at(null, None, 0, 0, false);
h.raw_view_at(null - 1, None, 0, 0, true);
} else if !sampler {
tracing::warn!(count, total, "no room for the null buffer views: computed indices of slots without a buffer view read what is there");
if !sampler {
let fixed = victory_shader::emit::NULL_BUFFER_VIEW;
let null = if fixed > count + extra && fixed < total {
Some(fixed)
} else {
tracing::warn!(count, total, "no room for the null buffer views: computed indices of slots without a buffer view read what is there");
// Root descriptors receive a heap index at bind time and can
// use a pair inside a capped heap. Keep it for the heap's life.
h.pair().map(|first| first + 1)
};
if let Some(srv) = null {
h.raw_view_at(srv, None, 0, 0, false);
h.raw_view_at(srv - 1, None, 0, 0, true);
h.null_root_srv = Some(srv);
}
}
Some(h)
}

pub(crate) fn null_root_view(&self, uav: bool) -> Option<u32> {
self.null_root_srv.map(|srv| srv - u32::from(uav))
}

/// Makes the first `total` slots null: one null slot in a CPU-side heap,
/// doubled by copies to a block, the block copied over the heap.
fn fill_null(&self, kind: D3D12_DESCRIPTOR_HEAP_TYPE, total: u32) -> windows::core::Result<()> {
Expand Down
64 changes: 53 additions & 11 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ impl State {
&& u.dword == 0
&& let Some(Root::View(address)) =
self.roots_ref(compute).get(usize::from(u.slot))
&& *address != 0
&& let Some(shadow) = crate::shadow::find(*address, 1)
{
let uav = t.used.iter().any(|r| {
Expand All @@ -737,6 +738,7 @@ impl State {
None if u.dword == victory_shader::emit::ROOT_VIEW_OFFSET => {
// Where the root address lies in the copy its view shows.
let offset = match self.roots_ref(compute).get(usize::from(u.slot)) {
Some(&Root::View(0)) => Some(0),
Some(&Root::View(address)) => crate::shadow::find(address, 1)
.and_then(|s| u32::try_from(s.offset(address)).ok()),
_ => None,
Expand Down Expand Up @@ -891,6 +893,24 @@ impl State {
not_run("a root view the title did not set");
return None;
};
let (key, host) = match self.heaps[0] {
0 => (MEMORY_HEAP, crate::memory::heap()),
h => (
std::ptr::from_ref(crate::descriptor::heap(h as This)) as usize,
crate::descriptor::heap(h as This).host(),
),
};
let Some(host) = host else {
not_run("a root view without a host view heap");
return None;
};
if address == 0 {
let index = host.null_root_view(uav);
if index.is_none() {
not_run("no null root buffer view in the host heap");
}
return index.map(|slot| (slot, 0));
}
let Some(shadow) = crate::shadow::find(address, 1) else {
not_run("a root view outside title buffers");
return None;
Expand All @@ -906,17 +926,6 @@ impl State {
let len = if shadow.marked() { shadow.left(address) } else { shadow.left(address).min(1 << 20) };
shadow.gpu_writes(list, address, len, true);
}
let (key, host) = match self.heaps[0] {
0 => (MEMORY_HEAP, crate::memory::heap()),
h => (
std::ptr::from_ref(crate::descriptor::heap(h as This)) as usize,
crate::descriptor::heap(h as This).host(),
),
};
let Some(host) = host else {
not_run("a root view without a host view heap");
return None;
};
let copy = std::sync::Arc::as_ptr(&shadow) as usize;
if let Some(&index) = self.root_slots.get(&(key, copy, uav)) {
return Some((index, offset));
Expand Down Expand Up @@ -2202,6 +2211,39 @@ uint ps() : SV_Target { return 1; }
host.execute(&[&list]);
assert_eq!(out.read_host(dst_bytes as u64, 16), want, "round {round}");
}

// Rebind the same slots to a null SRV, then to a null UAV. The
// previous round left nonzero words in the destination.
for null_uav in [false, true] {
list.reset();
let mut state = State::default();
state.set_heaps(&[heap]);
assert!(state.root_view(&list, true, 0, false).is_none(), "unset root");
state.set_view(true, 0, if null_uav { src + 0x18_0100 } else { 0 });
state.set_view(true, 1, if null_uav { 0 } else { dst_bytes as u64 });
let (input, at) = state.root_view(&list, true, 0, false).unwrap();
let (output, out_at) = state.root_view(&list, true, 1, true).unwrap();
assert_ne!(input, output);
assert_eq!(state.root_slots.len(), 1, "only the real buffer needs a dynamic view");
assert_eq!(out_at, 0);
if !null_uav {
assert_eq!(at, 0);
}
let constants = [at, 0];
// SAFETY: all descriptors are in the bound heap and held through execution.
unsafe {
list.list.SetDescriptorHeaps(&[Some(heap_host.heap.clone())]);
list.list.SetComputeRootSignature(&root);
list.list.SetComputeRootDescriptorTable(0, heap_host.gpu(input));
list.list.SetComputeRootDescriptorTable(1, heap_host.gpu(output));
list.list.SetComputeRoot32BitConstants(2, 2, constants.as_ptr().cast(), 0);
list.list.SetPipelineState(&pso);
list.list.Dispatch(1, 1, 1);
}
list.close();
host.execute(&[&list]);
assert_eq!(out.read_host(dst_bytes as u64, 16), [0; 16]);
}
}

/// Title memory of `bytes` (64 KiB aligned) for textures at an address.
Expand Down