Skip to content
Open
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
31 changes: 20 additions & 11 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us
let pc: F = std::mem::transmute(super::real::orig_fn(this, 59));
let pso = super::pso_async::resolve_wait(with(this, |s| s.pso));
let (gfx, compute) = (super::real::is_translated_graphics(pso), super::real::is_translated_compute(pso));
let (console_args_off, console_count_off) = (args_off, count_off);
let (args_off, count_off) = (args_off + super::real::buffer_delta(args), count_off + super::real::buffer_delta(count_buf));
if !gfx && !compute {
// A pipeline still being built (or whose build failed): its stand-in draws nothing, and
Expand Down Expand Up @@ -297,17 +298,25 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us
return;
}
let Some(list) = ID3D12GraphicsCommandList::from_raw_borrowed(&this) else { return };
match bind_abi(this, list, pso, gfx) {
Ok(()) => {
count(if gfx { DRAWS } else { DISPATCHES });
fresh_indirect_arguments(this, || pc(this, sig, max, args, args_off, count_buf, count_off))
}
Err(e) => {
count(if gfx { DRAWS_SKIPPED } else { DISPATCHES_SKIPPED });
static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
if N.fetch_add(1, Ordering::Relaxed) < 4 {
crate::xlog_warn!("D3D12.X: ExecuteIndirect with a translated pipeline skipped: {e}");
}
// the records bounded on the GPU first (a garbage record would hang it): this changes the
// list's compute bindings, which `bind_abi` sets again
let bounded = super::indirect_clamp::bound(list, with(this, |s| s.allocator), sig, max, args, console_args_off, count_buf, console_count_off);
let result = bind_abi(this, list, pso, gfx);
if let Ok(()) = result {
count(if gfx { DRAWS } else { DISPATCHES });
fresh_indirect_arguments(this, || match &bounded {
Some(b) => pc(this, sig, max, b.raw(), 0, count_buf, count_off),
None => pc(this, sig, max, args, args_off, count_buf, count_off),
});
}
if let Some(b) = bounded {
b.finish(list);
}
if let Err(e) = result {
count(if gfx { DRAWS_SKIPPED } else { DISPATCHES_SKIPPED });
static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
if N.fetch_add(1, Ordering::Relaxed) < 4 {
crate::xlog_warn!("D3D12.X: ExecuteIndirect with a translated pipeline skipped: {e}");
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/hle/d3d12x/device_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ pub(super) unsafe extern "system" fn create_command_signature_x(this: *mut c_voi
}
if hr >= 0 && !ppv.is_null() && !(*ppv).is_null() {
super::bind::note_signature_dispatches(*ppv as usize, list.iter().any(|a| a[0] == D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH.0 as u32));
super::indirect_clamp::note_signature(*ppv as usize, u32::from_le_bytes(pc[..4].try_into().unwrap()), &list);
}
hr
}
Expand Down
94 changes: 94 additions & 0 deletions crates/xcl/src/hle/d3d12x/indirect_clamp.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Bounds a title's indirect draw and dispatch records before ExecuteIndirect reads them
// (`indirect_clamp.rs`). One group walks every record in chunks of THREADS: each record is
// copied whole, its draw or dispatch arguments are clamped to per-record limits, and a running
// total of the work the records ask for (a prefix sum across the chunk) empties every record
// past BUDGET.

#define THREADS 1024
// per record: vertices (or indices) per instance, vertices times instances, groups per
// dimension, groups per dispatch; a group counts as 64 threads, a mesh group as 128
#define MAX_VERTICES (1u << 24)
#define MAX_VERTEX_INSTANCES (1u << 26)
#define MAX_GROUPS_DIM 65535u
#define MAX_GROUPS (1u << 22)
#define BUDGET (1u << 29)

RWByteAddressBuffer source : register(u0); // the UMA window holding the title's records
RWByteAddressBuffer bounded : register(u1); // our copy, which ExecuteIndirect reads
RWByteAddressBuffer counts : register(u2); // the UMA window holding the count (if any)

cbuffer Config : register(b0) {
uint records; // the call's maximum command count
uint source_at; // byte offset of record 0 in `source`
uint stride; // bytes per record (a multiple of 4)
uint action_at; // byte offset of the draw or dispatch arguments in a record
uint kind; // 0 draw, 1 indexed draw, 2 dispatch, 3 mesh dispatch
uint count_at; // byte offset of the count in `counts`; 0xFFFFFFFF: no count buffer
};

groupshared uint scan[THREADS];
groupshared uint carry;

uint sat_add(uint a, uint b) {
uint s = a + b;
return s < a ? 0xFFFFFFFFu : s;
}

// Clamps one record's arguments in place and returns the work they ask for.
uint clamp_args(inout uint3 a) {
if (kind <= 1) {
a.x = min(a.x, MAX_VERTICES);
a.y = min(a.y, MAX_VERTEX_INSTANCES / max(a.x, 1u));
return a.x * a.y;
}
a = min(a, uint3(MAX_GROUPS_DIM, MAX_GROUPS_DIM, MAX_GROUPS_DIM));
a.y = min(a.y, MAX_GROUPS / max(a.x, 1u));
a.z = min(a.z, MAX_GROUPS / max(a.x * a.y, 1u));
return a.x * a.y * a.z * (kind == 3 ? 128u : 64u);
}

[numthreads(THREADS, 1, 1)]
void main(uint t : SV_GroupIndex) {
if (t == 0)
carry = 0;
uint live = records;
if (count_at != 0xFFFFFFFFu)
live = min(live, counts.Load(count_at));
GroupMemoryBarrierWithGroupSync();
for (uint first = 0; first < records; first += THREADS) {
uint i = first + t;
uint3 a = uint3(0, 0, 0);
uint work = 0;
if (i < records) {
uint src = source_at + i * stride;
uint dst = i * stride;
for (uint k = 0; k < stride; k += 4)
bounded.Store(dst + k, source.Load(src + k));
a = source.Load3(src + action_at);
work = clamp_args(a);
}
// records past the count are never executed: they take none of the budget
scan[t] = i < live ? work : 0;
GroupMemoryBarrierWithGroupSync();
for (uint d = 1; d < THREADS; d <<= 1) {
uint before = t >= d ? scan[t - d] : 0;
GroupMemoryBarrierWithGroupSync();
scan[t] = sat_add(scan[t], before);
GroupMemoryBarrierWithGroupSync();
}
uint total = sat_add(carry, scan[t]);
if (i < records) {
if (total > BUDGET) {
if (kind <= 1)
a.y = 0;
else
a = uint3(0, 0, 0);
}
bounded.Store3(i * stride + action_at, a);
}
GroupMemoryBarrierWithGroupSync();
if (t == THREADS - 1)
carry = total;
GroupMemoryBarrierWithGroupSync();
}
}
Loading
Loading