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
208 changes: 204 additions & 4 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ pub(crate) struct State {
root_uav_slots: std::collections::HashMap<(usize, u64), RootSlot>,
/// The list ran and its dynamic slots were given back.
tables_gone: bool,
/// Raw thread counts `[x, y, z]` for a partial `DispatchX` call with
/// `USE_THREAD_DIMENSIONS` (`0` along an axis means full groups / no cap).
dispatch_threads: [u32; 3],
}

impl Drop for State {
Expand Down Expand Up @@ -679,10 +682,15 @@ impl State {
dword: u32,
memory: Option<&crate::memory::Ready>,
) -> u32 {
use victory_shader::emit::{USER_MEMORY_BASE, USER_VIEW_ADDRESS, USER_VIEW_BIAS};
use victory_shader::emit::{
USER_DISPATCH_THREADS, USER_MEMORY_BASE, USER_VIEW_ADDRESS, USER_VIEW_BIAS,
};
if kind == USER_MEMORY_BASE {
return memory.map_or(0, |m| m.base);
}
if kind == USER_DISPATCH_THREADS {
return self.dispatch_threads.get(dword as usize).copied().unwrap_or(0);
}
if kind == USER_VIEW_BIAS {
// The view begins on 16 bytes of the copy the address finds
// (`root_uav`, `root_srv_table`).
Expand Down Expand Up @@ -1665,6 +1673,20 @@ impl State {

/// Runs a dispatch on the host.
pub(crate) fn dispatch(&mut self, list: &HostList, x: u32, y: u32, z: u32) {
self.dispatch_inner(list, x, y, z, 0);
}

/// Runs a console `DispatchX(x, y, z, flags)` on the host.
///
/// Bit 5 (`0x20`) of `COMPUTE_DISPATCH_INITIATOR` is
/// `USE_THREAD_DIMENSIONS`: `(x, y, z)` are raw thread counts rather than
/// thread-group counts, and trailing lanes of partial tail groups are
/// masked off in initial hardware `EXEC`.
pub(crate) fn dispatch_x(&mut self, list: &HostList, x: u32, y: u32, z: u32, flags: u32) {
self.dispatch_inner(list, x, y, z, flags);
}

fn dispatch_inner(&mut self, list: &HostList, x: u32, y: u32, z: u32, flags: u32) {
// It may write the arguments a window holds clamped.
self.window = None;
#[cfg(feature = "diagnostics")]
Expand Down Expand Up @@ -1700,7 +1722,34 @@ impl State {
once!("a dispatch with a graphics pipeline");
return;
}
if !self.bind(list, p, true) {
let t = p.stages[0].translated();
let plan = dispatch_dimensions([x, y, z], t.threads, flags, |axis| {
t.host.user_data.iter().any(|u| {
u.root == RootType::Other(victory_shader::emit::USER_DISPATCH_THREADS)
&& u.dword == axis as u32
})
});
let ([gx, gy, gz], partial) = match plan {
Ok(Some(dispatch)) => dispatch,
Ok(None) => return,
Err(reason) => {
once!(
pipeline = %p.name, threads = ?[x, y, z], group = ?t.threads,
?reason, "thread-dimension dispatch dropped before binding"
);
DROPPED.fetch_add(1, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
crate::pipeline::note_drop(self.pipeline as This);
if crate::pipeline::is_builder(self.pipeline as This) {
crate::pipeline::builder_off("a dispatch could not enforce its thread dimensions");
}
return;
}
};
self.dispatch_threads = partial;
let bound = self.bind(list, p, true);
self.dispatch_threads = [0; 3];
if !bound {
DROPPED.fetch_add(1, Ordering::Relaxed);
// A stage of the structure builder that cannot be bound is a
// dropped stage: the others stop too.
Expand All @@ -1710,9 +1759,9 @@ impl State {
return;
}
#[cfg(feature = "diagnostics")]
list.note_op(|| self.op_text(format!("dispatch {x}x{y}x{z}"), true));
list.note_op(|| self.op_text(format!("dispatch {gx}x{gy}x{gz}"), true));
// SAFETY: an open list with everything set.
unsafe { list.list.Dispatch(x, y, z) };
unsafe { list.list.Dispatch(gx, gy, gz) };
DISPATCHED.fetch_add(1, Ordering::Relaxed);
}

Expand Down Expand Up @@ -2690,3 +2739,154 @@ pub(crate) fn stencil_ref(list: &HostList, reference: u32) {
// SAFETY: an open list.
unsafe { list.list.OMSetStencilRef(reference) };
}

/// AMD GFX10 / console `COMPUTE_DISPATCH_INITIATOR` bit 5:
/// `USE_THREAD_DIMENSIONS` (`DispatchX` arguments are raw thread counts rather
/// than thread-group counts, with trailing lanes masked off in initial `EXEC`).
const DISPATCH_USE_THREAD_DIMENSIONS: u32 = 0x20;

#[derive(Debug, PartialEq, Eq)]
enum DispatchError {
UnknownGroupSize,
TooManyGroups(usize),
MissingMask(usize),
}

/// Converts console `Dispatch` / `DispatchX(x, y, z, flags)` dimensions to
/// `(host_groups, partial_thread_limits)`. A zero thread count is empty.
/// A partial axis must have its own translator-provided mask.
fn dispatch_dimensions(
dims: [u32; 3],
group: [u32; 3],
flags: u32,
has_mask: impl Fn(usize) -> bool,
) -> Result<Option<([u32; 3], [u32; 3])>, DispatchError> {
if flags & DISPATCH_USE_THREAD_DIMENSIONS == 0 {
return Ok(Some((dims, [0; 3])));
}
if dims.contains(&0) {
return Ok(None);
}
if group.contains(&0) {
return Err(DispatchError::UnknownGroupSize);
}
let mut groups = [0; 3];
let mut partial = [0; 3];
for i in 0..3 {
let g = group[i];
groups[i] = dims[i].div_ceil(g);
if groups[i] > D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION {
return Err(DispatchError::TooManyGroups(i));
}
if g > 1 && !dims[i].is_multiple_of(g) {
if !has_mask(i) {
return Err(DispatchError::MissingMask(i));
}
partial[i] = dims[i];
}
}
Ok(Some((groups, partial)))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn marked_dispatch_checks_the_rounded_d3d12_group_limit_on_each_axis() {
let max = D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION;
for axis in 0..3 {
let mut dims = [2; 3];
let mut groups = [1; 3];
groups[axis] = max;
dims[axis] = max * 2;
assert_eq!(
dispatch_dimensions(dims, [2; 3], DISPATCH_USE_THREAD_DIMENSIONS, |_| false),
Ok(Some((groups, [0; 3])))
);
dims[axis] += 1;
assert_eq!(
dispatch_dimensions(dims, [2; 3], DISPATCH_USE_THREAD_DIMENSIONS, |_| true),
Err(DispatchError::TooManyGroups(axis))
);
assert_eq!(
dispatch_dimensions(dims, [2; 3], 0, |_| panic!()),
Ok(Some((dims, [0; 3])))
);
}
}

#[test]
fn marked_dispatch_requires_known_dimensions_and_every_partial_axis_mask() {
let group = [8, 8, 4];
let raw = DISPATCH_USE_THREAD_DIMENSIONS;
assert_eq!(
dispatch_dimensions([10, 16, 5], group, raw, |axis| axis == 0),
Err(DispatchError::MissingMask(2))
);
assert_eq!(
dispatch_dimensions([10, 16, 5], group, raw, |axis| axis != 1),
Ok(Some(([2, 2, 2], [10, 0, 5])))
);
assert_eq!(
dispatch_dimensions([1, 1, 1], [32, 1, 1], raw, |_| false),
Err(DispatchError::MissingMask(0))
);
for unknown in [[0, 0, 0], [8, 0, 4]] {
assert_eq!(
dispatch_dimensions([16, 16, 8], unknown, raw, |_| true),
Err(DispatchError::UnknownGroupSize)
);
assert_eq!(
dispatch_dimensions([16, 16, 8], unknown, 0x8000, |_| panic!()),
Ok(Some(([16, 16, 8], [0; 3])))
);
}
assert_eq!(dispatch_dimensions([0, 16, 8], [0; 3], raw, |_| panic!()), Ok(None));
assert_eq!(
dispatch_dimensions([16, 16, 8], group, raw, |_| panic!()),
Ok(Some(([2, 2, 2], [0; 3])))
);
}

#[test]
fn dispatch_x_thread_dimensions_convert_to_groups_and_partial_limits() {
let dispatch_dimensions = |dims, group, flags| {
super::dispatch_dimensions(dims, group, flags, |_| true).unwrap()
};
// Normal Dispatch / DispatchX without bit 5 passes group counts through.
assert_eq!(
dispatch_dimensions([48, 1, 1], [64, 1, 1], 0),
Some(([48, 1, 1], [0, 0, 0]))
);
assert_eq!(
dispatch_dimensions([48, 1, 1], [64, 1, 1], 0x8000),
Some(([48, 1, 1], [0, 0, 0]))
);
// Bit 5 (0x20 / 0x8020): raw thread counts -> ceil(threads / group) + partial limits.
assert_eq!(
dispatch_dimensions([48, 1, 1], [64, 1, 1], 0x20),
Some(([1, 1, 1], [48, 0, 0]))
);
assert_eq!(
dispatch_dimensions([1, 1, 1], [64, 1, 1], 0x8020),
Some(([1, 1, 1], [1, 0, 0]))
);
assert_eq!(
dispatch_dimensions([65, 1, 1], [64, 1, 1], 0x20),
Some(([2, 1, 1], [65, 0, 0]))
);
// Exact multiple of group size needs no partial-lane limit.
assert_eq!(
dispatch_dimensions([128, 1, 1], [64, 1, 1], 0x20),
Some(([2, 1, 1], [0, 0, 0]))
);
// 3D partial dimensions.
assert_eq!(
dispatch_dimensions([10, 16, 5], [8, 8, 4], 0x20),
Some(([2, 2, 2], [10, 0, 5]))
);
// Zero threads along any axis is a no-op.
assert_eq!(dispatch_dimensions([0, 1, 1], [64, 1, 1], 0x20), None);
}
}
16 changes: 11 additions & 5 deletions crates/victory-gpu/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,14 +1313,16 @@ extern "system" fn driver_hint(_: This, _feature: u64, _value: u64) -> u64 {
0
}

/// `DispatchX(x, y, z, flags)`: a dispatch with console flags, which the
/// host has no use for (the ray-tracing builder dispatches this way).
extern "system" fn dispatch_x(this: This, x: u32, y: u32, z: u32, _flags: u32) {
/// `DispatchX(x, y, z, flags)`: a compute dispatch with console
/// `COMPUTE_DISPATCH_INITIATOR` flags (bit 5, `0x20`, is
/// `USE_THREAD_DIMENSIONS`: `(x, y, z)` are raw thread counts rather than
/// thread-group counts).
extern "system" fn dispatch_x(this: This, x: u32, y: u32, z: u32, flags: u32) {
#[cfg(windows)]
state::dispatch(this, x, y, z);
state::dispatch_x(this, x, y, z, flags);
#[cfg(not(windows))]
{
let _ = (this, x, y, z);
let _ = (this, x, y, z, flags);
count_dispatch();
}
}
Expand Down Expand Up @@ -1589,6 +1591,10 @@ mod state {
super::count_dispatch();
with_state(this, |list, s| s.dispatch(list, x, y, z));
}
pub(super) fn dispatch_x(this: This, x: u32, y: u32, z: u32, flags: u32) {
super::count_dispatch();
with_state(this, |list, s| s.dispatch_x(list, x, y, z, flags));
}
/// `DispatchMesh(x, y, z)` (the PC method of List6); the title's mesh
/// programs take `x` groups only.
pub(super) extern "system" fn dispatch_mesh(this: This, x: u32, y: u32, z: u32) {
Expand Down
30 changes: 19 additions & 11 deletions crates/victory-gpu/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ pub(crate) struct Translated {
/// Whether the shader's code can store to buffer memory ([`stores`]);
/// true when its code is not known.
pub(crate) stores: bool,
/// Compute thread-group dimensions `[x, y, z]` (`[0; 3]` for non-compute).
pub(crate) threads: [u32; 3],
}

#[cfg(windows)]
Expand Down Expand Up @@ -184,17 +186,23 @@ fn translate(container: &[u8]) -> Result<Translated, String> {
};
let host = crate::shader_cache::host_shader(container)?;
let stores = programs.is_empty() || programs.iter().any(stores);
let used = programs
.into_iter()
.find(|p| {
matches!(
p.stage,
HwStage::Vs | HwStage::Ps | HwStage::Ms | HwStage::Cs
)
})
.map(|p| p.used)
.unwrap_or_default();
Ok(Translated { host, used, stores })
let main = programs.into_iter().find(|p| {
matches!(
p.stage,
HwStage::Vs | HwStage::Ps | HwStage::Ms | HwStage::Cs
)
});
let threads = match main.as_ref().map(|p| &p.io) {
Some(victory_shader::native::StageIo::Cs(cs)) => cs.threads,
_ => [0; 3],
};
let used = main.map(|p| p.used).unwrap_or_default();
Ok(Translated {
host,
used,
stores,
threads,
})
}

/// The compute pipelines of the console's ray-tracing structure builder
Expand Down
Loading