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
64 changes: 56 additions & 8 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@ fn install_reset(
untrack_two_pass_list(list as usize);
}
unmark_deferring(list);
super::indirect_guard::retire_batches(list, old);
console_list::hold_until_reset(old, blocks);
retire_recording_res(old, res_bufs, res_marks, res_untracked);
drop(old_resolution);
Expand Down Expand Up @@ -1262,6 +1263,7 @@ pub(super) fn prepare_split(from: *mut c_void, to: *mut c_void, allocator: usize
untrack_two_pass_list(to as usize);
}
unmark_deferring(to);
super::indirect_guard::retire_batches(to, old);
console_list::hold_until_reset(old, blocks);
retire_recording_res(old, res_bufs, res_marks, res_untracked);
PreparedSplit { to: to as usize, allocator: backing, layout, args, layout_gfx, args_gfx, rs_gfx, pso, resolution, heaps }
Expand Down Expand Up @@ -1405,7 +1407,9 @@ pub(super) fn pass_arena(from: *mut c_void, to: *mut c_void) {
/// The list is gone.
pub(super) fn forget(list: *mut c_void) {
unmark_deferring(list);
if let Some(mut s) = take_state(list as usize) {
let state = take_state(list as usize);
super::indirect_guard::retire_batches(list, state.as_ref().map_or(0, |s| s.allocator));
if let Some(mut s) = state {
let batch = s.end_batch();
if !s.res_retired.is_empty() || s.res_buf.is_some() || batch.is_some() || !s.res_marks.is_empty() || s.res_untracked || !s.fb_uses.is_empty() {
untrack_two_pass_list(list as usize);
Expand Down Expand Up @@ -2096,6 +2100,16 @@ fn flush_wanted(slot: usize, newer_layout: bool, from_title: bool, deferring: bo
deferring && from_title && !keeps_two_pass_batch(slot, newer_layout)
}

/// Whether a list call through `slot` ends the list's batch of ExecuteIndirect checks
/// (`indirect_guard`, `open`): any but those a run of deferred draws goes on across and the
/// viewport, blend factor and index / vertex buffer sets – none writes memory or orders writes.
/// DispatchMesh ends it (its shaders may write a later draw's counts); an ExecuteIndirect decides
/// for itself (`indirect_guard::before_execute_indirect`: only a plain draw goes on).
fn guard_batch_end_wanted(slot: usize, newer_layout: bool, from_title: bool, open: bool) -> bool {
let dm = if newer_layout { 129 } else { 127 };
open && from_title && (slot == dm || (!keeps_two_pass_batch(slot, newer_layout) && !matches!(slot, 21 | 23 | 47 | 48)))
}

/// What ended a run (`flush_two_pass`), for the log: a title call through a console list slot
/// (`before_list_call`), or one of these.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -2153,17 +2167,24 @@ fn run_ends_text(n: usize) -> String {
v.iter().take(n).map(|&(c, i)| format!("{} {c}", run_end_name(i, newer))).collect::<Vec<_>>().join(", ")
}

/// In front of every command list slot with `XCL_TWO_PASS_BATCH=2` (`real::class_for`): a title
/// call on a list with deferred draws records them first, unless the slot keeps the run
/// (`keeps_two_pass_batch`). Calls from XCL and the runtime go on as they are.
/// In front of every command list slot with `XCL_TWO_PASS_BATCH=2` or
/// `XCL_INDIRECT_GUARD_BATCH=1` (`real::class_for`): a title call on a list with deferred draws
/// records them first, unless the slot keeps the run (`keeps_two_pass_batch`), and one on a list
/// with a batch of ExecuteIndirect checks ends it (`guard_batch_end_wanted`). Calls from XCL and
/// the runtime go on as they are.
pub(super) unsafe extern "system" fn before_list_call(caller: usize, slot: u64, frame: *const u64) -> usize {
if DEFERRING_LISTS.load(Ordering::Relaxed) == 0 {
let this = *frame.add(0x80 / 8) as *mut c_void;
let batching = super::indirect_guard::batch_open(this);
if DEFERRING_LISTS.load(Ordering::Relaxed) == 0 && !batching {
return 0;
}
let this = *frame.add(0x80 / 8) as *mut c_void;
let deferring = DEFERRING.contains(this as usize);
let deferring = DEFERRING_LISTS.load(Ordering::Relaxed) != 0 && DEFERRING.contains(this as usize);
let newer = super::real::QUEUE_SHIFTED.load(Ordering::Acquire);
if flush_wanted(slot as usize, newer, deferring && crate::modules::image_base_for_addr(caller).is_some(), deferring) {
let from_title = (deferring || batching) && crate::modules::image_base_for_addr(caller).is_some();
if guard_batch_end_wanted(slot as usize, newer, from_title, batching) {
super::indirect_guard::end_batch(this);
}
if flush_wanted(slot as usize, newer, from_title, deferring) {
flush_two_pass(this, RunEnd::Call(slot as usize));
}
0
Expand Down Expand Up @@ -2384,6 +2405,10 @@ unsafe fn emit_commands(this: *mut c_void, list: &ID3D12GraphicsCommandList, cmd
/// The deferred draws of the list `this`, recorded now (`batch_commands`), for `why`; nothing
/// when it has none.
pub(super) unsafe fn flush_two_pass(this: *mut c_void, why: RunEnd) {
// (a list or segment that closes ends its batch of ExecuteIndirect checks too)
if matches!(why, RunEnd::Close | RunEnd::Segment) {
super::indirect_guard::end_batch(this);
}
if DEFERRING_LISTS.load(Ordering::Relaxed) == 0 || !DEFERRING.contains(this as usize) {
return;
}
Expand Down Expand Up @@ -3572,6 +3597,8 @@ unsafe fn execute_indirect_snapshot(
count_buf: usize, count_off: u64, signature: IndirectSignatureInfo, raw_pso: usize,
resolution: Option<super::pso_async::PipelineResolution>, allow_defer: bool,
) {
// (whichever path it takes: one that may write counts ends a batch of checks)
super::indirect_guard::before_execute_indirect(this, sig);
type F = unsafe extern "system" fn(*mut c_void, usize, u32, usize, u64, usize, u64);
let pc: F = std::mem::transmute(super::real::orig_fn(this, 59));
let dispatches = signature.dispatches();
Expand Down Expand Up @@ -8657,6 +8684,27 @@ void main(uint t : SV_GroupIndex) {
}
}

/// A batch of ExecuteIndirect checks goes on across the calls that write no memory and order
/// no writes; anything else a title calls ends it.
#[test]
fn a_title_call_that_can_write_or_order_memory_ends_a_batch_of_checks() {
for newer in [false, true] {
let (ei, dm) = if newer { (61, 129) } else { (59, 127) };
// (an ExecuteIndirect decides for itself, `indirect_guard::before_execute_indirect`)
for slot in (0..=8).chain([20, 21, 22, 23, 24, 25, 47, 48]).chain(33..=46).chain([ei]) {
assert!(!guard_batch_end_wanted(slot, newer, true, true), "slot {slot}, newer layout: {newer}");
}
// barriers, draws, dispatches, DispatchMesh, copies, clears, render targets, heaps,
// queries, predication, bundles, stream output, Close, Reset, the console's own
// methods (waits, writes, flushes, its UAV barrier)
for slot in [26, 12, 13, 14, 15, 16, 17, 18, 19, 27, 28, 29, 30, 31, 32, 49, 50, 51, 52, 53, 54, 55, 9, 10, 11, 60, 67, 69, 71, 72, 78, 81, 85, 93, 146, dm] {
assert!(guard_batch_end_wanted(slot, newer, true, true), "slot {slot}, newer layout: {newer}");
}
assert!(!guard_batch_end_wanted(26, newer, false, true), "XCL's own calls");
assert!(!guard_batch_end_wanted(26, newer, true, false), "no batch open");
}
}

/// A two-pass draw is deferred only on a direct list, without a capture, as one command, with
/// records that fit a batch record buffer; otherwise the run is recorded and it goes at once.
#[test]
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/hle/d3d12x/console_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ pub fn allocator_reset(allocator: *mut c_void) {
unsafe { free_uma(&b) };
}
super::bind::release_allocator_res(allocator as usize);
super::indirect_guard::allocator_reset(allocator as usize);
}

fn with_list<R>(obj: usize, f: impl FnOnce(&mut ListState) -> R) -> Option<R> {
Expand Down
Loading
Loading