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
38 changes: 34 additions & 4 deletions crates/xcl/src/gpu/recomp/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1696,10 +1696,39 @@ pub(crate) unsafe fn record_dispatch_chain<'a>(
}
}

/// One round's root-CBV constants, padded to D3D12's 256-byte CBV alignment. Its Dispatch
/// arguments begin after the eight words and are shared by every piece in that round.
/// One round of an indirect chain (written by `INDIRECT_SLICER`): the eight words a direct
/// dispatch sets as its root constants, then its Dispatch arguments, shared by every piece in
/// that round (`chain_command_signature`).
pub(crate) const CHAIN_COMMAND_BYTES: u32 = 256;
pub(crate) const CHAIN_DISPATCH_AT: u64 = 32;
// (`chain_command_signature` reads the Dispatch arguments right after its eight root constants)
const _: () = assert!(CHAIN_DISPATCH_AT == 8 * 4);

/// The command signature of a chain piece's indirect dispatch under the chain root signature
/// `rs` (`dispatch_chain_root_signature`): a round's eight words go to the root constants a
/// direct dispatch sets (parameter 7, `record_dispatch_chain`), then its Dispatch arguments. So
/// direct and indirect dispatches run the same pieces, built once under one root signature.
pub(crate) fn chain_command_signature(dev: &ID3D12Device, rs: &ID3D12RootSignature) -> Result<ID3D12CommandSignature, String> {
// (by root signature, kept alive here so its address names no other)
static MADE: Mutex<Vec<(ID3D12RootSignature, ID3D12CommandSignature)>> = Mutex::new(Vec::new());
let mut made = MADE.lock().unwrap();
if let Some((_, sig)) = made.iter().find(|m| windows::core::Interface::as_raw(&m.0) == windows::core::Interface::as_raw(rs)) {
return Ok(sig.clone());
}
let args = [
D3D12_INDIRECT_ARGUMENT_DESC {
Type: D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT,
Anonymous: D3D12_INDIRECT_ARGUMENT_DESC_0 { Constant: D3D12_INDIRECT_ARGUMENT_DESC_0_1 { RootParameterIndex: 7, DestOffsetIn32BitValues: 0, Num32BitValuesToSet: 8 } },
},
D3D12_INDIRECT_ARGUMENT_DESC { Type: D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH, ..Default::default() },
];
let desc = D3D12_COMMAND_SIGNATURE_DESC { ByteStride: CHAIN_COMMAND_BYTES, NumArgumentDescs: 2, pArgumentDescs: args.as_ptr(), NodeMask: 0 };
let mut sig = None;
unsafe { dev.CreateCommandSignature(&desc, rs, &mut sig) }.map_err(|e| format!("CreateCommandSignature (chain): {e}"))?;
let sig: ID3D12CommandSignature = sig.ok_or("CreateCommandSignature (chain)")?;
made.push((rs.clone(), sig.clone()));
Ok(sig)
}

pub(crate) fn dispatch_command_signature(dev: &ID3D12Device) -> Result<ID3D12CommandSignature, String> {
static MADE: Mutex<Option<(usize, ID3D12CommandSignature)>> = Mutex::new(None);
Expand Down Expand Up @@ -1823,6 +1852,8 @@ pub(crate) unsafe fn restore_indirect_commands(list: &ID3D12GraphicsCommandList,
StateAfter: D3D12_RESOURCE_STATE_UNORDERED_ACCESS }) } }]);
}

/// The pieces of a chain for the `rounds` the slicer wrote into `commands`, under the chain root
/// signature: `sig` is its `chain_command_signature`.
pub(crate) unsafe fn record_dispatch_chain_indirect<'a>(
list: &ID3D12GraphicsCommandList, sig: &ID3D12CommandSignature, commands: &ID3D12Resource, rounds: u32,
scratch: &ID3D12Resource, pieces: impl IntoIterator<Item = &'a ID3D12PipelineState> + Clone,
Expand All @@ -1831,12 +1862,11 @@ pub(crate) unsafe fn record_dispatch_chain_indirect<'a>(
list.SetComputeRootUnorderedAccessView(6, scratch.GetGPUVirtualAddress());
for r in 0..rounds {
let offset = u64::from(r) * u64::from(CHAIN_COMMAND_BYTES);
list.SetComputeRootConstantBufferView(7, commands.GetGPUVirtualAddress() + offset);
for piece in pieces.clone() {
list.ResourceBarrier(&[D3D12_RESOURCE_BARRIER { Type: D3D12_RESOURCE_BARRIER_TYPE_UAV, Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE,
Anonymous: D3D12_RESOURCE_BARRIER_0 { UAV: std::mem::ManuallyDrop::new(D3D12_RESOURCE_UAV_BARRIER { pResource: std::mem::ManuallyDrop::new(None) }) } }]);
bind_pipeline(piece);
list.ExecuteIndirect(sig, 1, commands, offset + CHAIN_DISPATCH_AT, None::<&ID3D12Resource>, 0);
list.ExecuteIndirect(sig, 1, commands, offset, None::<&ID3D12Resource>, 0);
}
}
}
Expand Down
66 changes: 54 additions & 12 deletions crates/xcl/src/gpu/recomp/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1710,10 +1710,28 @@ pub(crate) fn dispatch_split_by(
dispatch_split_samples(chain, groups, &[], mem, most, 1, false).map(|(m, _)| m)
}

/// `dispatch_split_by` as an indirect dispatch (`dispatch_chain::execute_indirect`): the groups
/// in an argument buffer, the slicer's rounds (one for each slab) run by the same pieces.
#[cfg(test)]
pub(crate) fn dispatch_split_indirect_by(
chain: &crate::gpu::dxil::kernel::DispatchChain, groups: [u32; 3], mem: &[u32], most: u64,
) -> Result<Vec<u32>, String> {
dispatch_split_forms(chain, groups, &[], mem, most, 1, false, true).map(|(m, _)| m)
}

#[cfg(test)]
pub(crate) fn dispatch_split_samples(
chain: &crate::gpu::dxil::kernel::DispatchChain, groups: [u32; 3], sgprs: &[u32], mem: &[u32],
most: u64, reps: usize, timed: bool,
) -> Result<(Vec<u32>, Vec<f64>), String> {
dispatch_split_forms(chain, groups, sgprs, mem, most, reps, timed, false)
}

#[cfg(test)]
#[allow(clippy::too_many_arguments)]
fn dispatch_split_forms(
chain: &crate::gpu::dxil::kernel::DispatchChain, groups: [u32; 3], sgprs: &[u32], mem: &[u32],
most: u64, reps: usize, timed: bool, indirect: bool,
) -> Result<(Vec<u32>, Vec<f64>), String> {
let mut g = GPU.lock().unwrap();
let gpu = g.get_or_insert_with(init).as_mut().map_err(|e| e.clone())?;
Expand All @@ -1726,7 +1744,7 @@ pub(crate) fn dispatch_split_samples(
}).map_err(e)
}).collect::<Result<_,_>>()?;
dispatch_with(&gpu.dev, &gpu.queue, &gpu.fence, &mut gpu.value, &pieces[0], &rs, groups, sgprs, mem, reps, timed,
Some((&pieces, chain.dwords_per_group, most)))
Some((&pieces, chain.dwords_per_group, most, indirect)))
}

/// `dispatch` run `reps` times in one command list (a UAV barrier between them, memory not
Expand Down Expand Up @@ -1781,7 +1799,7 @@ pub(crate) fn profile_buffer_forms(
let i = variant - 1;
let words = chains[i].dwords_per_group;
let most = (budget / (u64::from(words) * 4)).max(1);
(&pieces[i][0], &rs, Some((pieces[i].as_slice(), words, most)))
(&pieces[i][0], &rs, Some((pieces[i].as_slice(), words, most, false)))
};
let (out, times) = dispatch_with(&gpu.dev, &gpu.queue, &gpu.fence, &mut gpu.value, pso, signature,
groups, sgprs, mem, 12, true, chain)?;
Expand Down Expand Up @@ -1822,7 +1840,8 @@ fn dispatch_with(
mem: &[u32],
reps: usize,
timed: bool,
chain: Option<(&[ID3D12PipelineState], u32, u64)>,
// (pieces, scratch words per group, groups at once, dispatched indirectly)
chain: Option<(&[ID3D12PipelineState], u32, u64, bool)>,
) -> Result<(Vec<u32>, Vec<f64>), String> {
let mut io = sgprs.to_vec();
io.resize(KERNEL_SGPRS, 0);
Expand Down Expand Up @@ -1875,14 +1894,32 @@ fn dispatch_with(
dev.CreateUnorderedAccessView(None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&raw_window_uav(4)), h);
}
list.SetDescriptorHeaps(&[Some(dh.clone()), Some(bound._heaps[1].clone())]);
list.SetComputeRootSignature(rs);
list.SetComputeRootUnorderedAccessView(0, io_buf.GetGPUVirtualAddress());
list.SetComputeRootUnorderedAccessView(1, res_buf.GetGPUVirtualAddress());
// every window (u2..u5) is `mem`, as in the heap above
for k in 2..6 {
list.SetComputeRootUnorderedAccessView(k, mem_buf.GetGPUVirtualAddress());
}
let scratch = chain.map(|(_, words, most)| {
let bind_root = || {
list.SetComputeRootSignature(rs);
list.SetComputeRootUnorderedAccessView(0, io_buf.GetGPUVirtualAddress());
list.SetComputeRootUnorderedAccessView(1, res_buf.GetGPUVirtualAddress());
// every window (u2..u5) is `mem`, as in the heap above
for k in 2..6 {
list.SetComputeRootUnorderedAccessView(k, mem_buf.GetGPUVirtualAddress());
}
};
bind_root();
// an indirect chain: the groups in an argument buffer, the slicer's commands (a round for
// each slab) and the pieces' command signature
let indirect = match chain {
Some((_, _, most, true)) => {
let rounds = super::pso::dispatch_slabs(groups, most).len() as u32;
let args = buffer(dev, 12, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, false)?;
let mut a = std::ptr::null_mut();
args.Map(0, None, Some(&mut a)).map_err(e)?;
std::ptr::copy_nonoverlapping(groups.as_ptr(), a as *mut u32, 3);
args.Unmap(0, None);
let commands = super::pso::chain_commands_storage(dev, super::pso::chain_tail_offset(rounds) + 12).ok_or("chain commands")?;
Some((rounds, args, commands, super::pso::chain_command_signature(dev, rs)?))
}
_ => None,
};
let scratch = chain.map(|(_, words, most, _)| {
let first = super::pso::dispatch_slabs(groups, most)[0].1;
let bytes = first.iter().fold(4 * u64::from(words), |n, &g| n * u64::from(g));
buffer(dev, bytes, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, true)
Expand All @@ -1905,7 +1942,12 @@ fn dispatch_with(
if let Some((heap, _)) = &queries {
list.EndQuery(heap, D3D12_QUERY_TYPE_TIMESTAMP, 2 * k);
}
if let (Some((pieces, _, most)), Some(scratch)) = (chain, &scratch) {
if let (Some((pieces, _, most, _)), Some(scratch), Some((rounds, args, commands, sig))) = (chain, &scratch, &indirect) {
super::pso::record_indirect_slicer(&list, args.GetGPUVirtualAddress(), *rounds, most, commands, |slicer| list.SetPipelineState(slicer))?;
bind_root();
super::pso::record_dispatch_chain_indirect(&list, sig, commands, *rounds, scratch, pieces, |piece| list.SetPipelineState(piece));
super::pso::restore_indirect_commands(&list, commands);
} else if let (Some((pieces, _, most, _)), Some(scratch)) = (chain, &scratch) {
super::pso::record_dispatch_chain(
&list, groups, most, scratch, k > 0, pieces,
|piece| list.SetPipelineState(piece),
Expand Down
7 changes: 4 additions & 3 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3800,17 +3800,18 @@ unsafe fn execute_indirect_snapshot(
}
// a split compute program's one Dispatch-only command runs its chain (`dispatch_chain`)
if compute && max == 1 && count_buf == 0 && signature.dispatch_only().is_some() {
let chained = indirect_address(args, args_off_title, args_off).map_or(Ok(false), |address| {
use super::real::dispatch_chain::Indirect;
let chained = indirect_address(args, args_off_title, args_off).map_or(Ok(Indirect::Whole), |address| {
super::real::dispatch_chain::execute_indirect(list, raw_pso, pso, with(this, |s| s.allocator), address,
|root| bind_abi_with_root_resolved(this, list, raw_pso, pso, false, root, [0; 3], None, 0).map(|_| ()))
});
match chained {
Ok(true) => {
Ok(Indirect::Ran) => {
ei_work(this, dispatches, super::targets::RAN);
count(DISPATCHES);
return;
}
Ok(false) => {}
Ok(Indirect::Whole) => {}
Err(e) => {
ei_work(this, dispatches, super::targets::SKIPPED);
count(DISPATCHES_SKIPPED);
Expand Down
5 changes: 0 additions & 5 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6209,7 +6209,6 @@ enum ComputeBuild {
CachedWhole,
// The chain root signature has a separate on-disk namespace.
DirectChain,
IndirectChain,
}

unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPELINE_STATE_DESC, rs: *mut c_void,
Expand All @@ -6222,8 +6221,6 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL
let library_name = match mode {
ComputeBuild::DirectChain => driver_cache::name_for_chain_root_node(
dxil, copy.Flags.0, driver_cache::ChainRoot::DirectConstants, samplers, copy.NodeMask),
ComputeBuild::IndirectChain => driver_cache::name_for_chain_root_node(
dxil, copy.Flags.0, driver_cache::ChainRoot::IndirectCbv, samplers, copy.NodeMask),
_ => driver_cache::name_for_root_node(dxil, copy.Flags.0, false, samplers, copy.NodeMask),
};
if !ppv.is_null() {
Expand All @@ -6240,8 +6237,6 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL
let disk = match mode {
ComputeBuild::DirectChain => driver_cache::path_for_chain_root_node(
dxil, copy.Flags.0, driver_cache::ChainRoot::DirectConstants, samplers, copy.NodeMask),
ComputeBuild::IndirectChain => driver_cache::path_for_chain_root_node(
dxil, copy.Flags.0, driver_cache::ChainRoot::IndirectCbv, samplers, copy.NodeMask),
_ => driver_cache::path_for_root_node(dxil, copy.Flags.0, false, samplers, copy.NodeMask),
};
let from_disk = cached.is_none();
Expand Down
Loading
Loading