From d6f30c1809b5350f09c492cc97ffb29088c56cc3 Mon Sep 17 00:00:00 2001 From: voiceovarian1-max <360456911+voiceovarian1-max@users.noreply.github.com> Date: Fri, 27 Nov 2026 14:23:36 +0400 Subject: [PATCH] D3D12.X: the count check's buffer stays INDIRECT_ARGUMENT between checks The indirect count check (`indirect_guard`) moved its count buffer to INDIRECT_ARGUMENT for each ExecuteIndirect and back to COMMON after it: two transitions per check, each a barrier the GPU waits at. The buffer now stays INDIRECT_ARGUMENT from one check to the next on a list (`check_in`, `run_keeping`, `guard_out_ia`) and goes back only when something else needs it. GPU test included. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/bind.rs | 14 ++- crates/xcl/src/hle/d3d12x/indirect_guard.rs | 98 ++++++++++++++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index f2a771ee06..11931aac34 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -63,6 +63,8 @@ struct State { /// the next dispatch's thread limits (`IO_THREAD_LIMIT`; a `DispatchX` with thread /// dimensions), taken by its `bind_abi` limits: [u32; 3], + /// the count check's buffer was left INDIRECT_ARGUMENT in this recording (`indirect_guard`) + guard_out_ia: bool, } /// A recording's blocks of rewritten indirect arguments, and most bytes of them (past it, its @@ -445,6 +447,7 @@ pub(super) fn reset(list: *mut c_void, allocator: usize, initial_pso: usize) { s.heaps = [0; 2]; // (the scratch blocks are in `res_retired` already) (s.scratch, s.scratch_used, s.scratch_taken, s.scratch_in_args, s.record_open) = (None, 0, 0, false, None); + s.guard_out_ia = false; let mut res_bufs = std::mem::take(&mut s.res_retired); if let Some(b) = s.res_buf.take() { res_bufs.push(b); @@ -503,6 +506,7 @@ pub(super) fn inherit_for_split(from: *mut c_void, to: *mut c_void, allocator: u // the two-pass record buffers and argument scratch of `to`'s previous recording, as in // `reset`: the GPU may still read them, so the new one takes fresh ones (s.scratch, s.scratch_used, s.scratch_taken, s.scratch_in_args, s.record_open) = (None, 0, 0, false, None); + s.guard_out_ia = false; let mut res_bufs = std::mem::take(&mut s.res_retired); if let Some(b) = s.res_buf.take() { res_bufs.push(b); @@ -2340,7 +2344,12 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us trace_ui_draw(this, raw_pso, true); } // (binds a compute root signature and a pipeline: before the dispatch's or draw's own) - let checked = super::indirect_guard::check(this, pso, sig, max, args, args_off, count_buf); + let out_ia = with(this, |s| s.guard_out_ia); + let checked = super::indirect_guard::check_in(this, pso, sig, max, args, args_off, count_buf, out_ia); + if checked.is_some() { + // (the check wrote it: UNORDERED_ACCESS now) + with(this, |s| s.guard_out_ia = false); + } match bind_abi(this, list, pso, gfx, None, 0) { Ok(b) => { let whole = if compute { @@ -2399,7 +2408,8 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us } (Some(c), None) => { super::gpu_profile::note_counts(&t, c.slot); - c.run(this, |buffer, off| pc(this, sig, 1, buffer, off, 0, 0)); + c.run_keeping(this, |buffer, off| pc(this, sig, 1, buffer, off, 0, 0)); + with(this, |s| s.guard_out_ia = true); } (None, None) => fresh_indirect_arguments(this, || pc(this, sig, max, args, args_off, count_buf, count_off)), } diff --git a/crates/xcl/src/hle/d3d12x/indirect_guard.rs b/crates/xcl/src/hle/d3d12x/indirect_guard.rs index e34169cfc4..d47edc7ac4 100644 --- a/crates/xcl/src/hle/d3d12x/indirect_guard.rs +++ b/crates/xcl/src/hle/d3d12x/indirect_guard.rs @@ -303,6 +303,14 @@ pub(super) struct Checked { /// offset in it) on `list`; None: not one this checks, it runs as it is. Binds a compute root /// signature and a pipeline: the caller binds the dispatch's or draw's after it. pub(super) unsafe fn check(list: *mut c_void, pso: usize, sig: usize, max: u32, args: usize, args_off: u64, count_buf: usize) -> Option { + check_in(list, pso, sig, max, args, args_off, count_buf, false) +} + +/// `check` on a list where the check's buffer was left INDIRECT_ARGUMENT (`out_ia`, +/// `Checked::run_keeping`): it goes back to UNORDERED_ACCESS in the same ResourceBarrier call as +/// the check's UAV barrier (vkd3d-proton makes one pipeline barrier of a call's barriers). +#[allow(clippy::too_many_arguments)] +pub(super) unsafe fn check_in(list: *mut c_void, pso: usize, sig: usize, max: u32, args: usize, args_off: u64, count_buf: usize, out_ia: bool) -> Option { if max != 1 || count_buf != 0 { return None; } @@ -342,7 +350,26 @@ pub(super) unsafe fn check(list: *mut c_void, pso: usize, sig: usize, max: u32, Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, Anonymous: D3D12_RESOURCE_BARRIER_0 { UAV: ManuallyDrop::new(D3D12_RESOURCE_UAV_BARRIER { pResource: ManuallyDrop::new(None) }) }, }; - l.ResourceBarrier(&[written]); + if out_ia { + let back = D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + Transition: ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: ManuallyDrop::new(Some(g.out.clone())), + Subresource: D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, + StateBefore: D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, + StateAfter: D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + }), + }, + }; + // (a bitwise copy into the call: the one reference is released once below) + l.ResourceBarrier(&[written.clone(), std::ptr::read(&back)]); + let mut back = back; + ManuallyDrop::drop(&mut (*back.Anonymous.Transition).pResource); + } else { + l.ResourceBarrier(&[written.clone()]); + } l.SetComputeRootSignature(&g.rs); // (past the hook of a title's call: what the title bound stays the list's pipeline) super::list_x::set_compute_pipeline(list, g.pso.as_raw()); @@ -381,6 +408,14 @@ impl Checked { pub(super) unsafe fn read(&self, list: *mut c_void) { super::list_x::transition(list, self.guard.out.as_raw() as usize, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON); } + + /// `run`, leaving the buffer INDIRECT_ARGUMENT: the list's next check takes it back + /// (`check_in` with `out_ia`); a buffer goes back to COMMON when its submission is done. + pub(super) unsafe fn run_keeping(&self, list: *mut c_void, op: impl FnOnce(usize, u64)) { + let out = self.guard.out.as_raw() as usize; + super::list_x::transition(list, out, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT); + op(out, self.slot as u64 * SLOT as u64); + } } /// The counts slot `slot`'s ExecuteIndirect was asked to run (groups; vertices or indices, @@ -619,6 +654,67 @@ void main() { c.InterlockedAdd(0, 1u); } } } + /// Three checked ExecuteIndirects on one list (in range, garbage, in range), the check's + /// buffer left INDIRECT_ARGUMENT between them (`run_keeping`, `check_in` with `out_ia`): each + /// runs its own checked counts. + #[test] + fn checked_execute_indirects_in_one_list_keep_the_buffer_between_them() { + let src = r#" +RWByteAddressBuffer c : register(u0); +[numthreads(1, 1, 1)] +void main() { c.InterlockedAdd(0, 1u); } +"#; + unsafe { + let _one = ONE.lock().unwrap_or_else(|e| e.into_inner()); + let Some(dev) = device() else { return }; + let cs = real::compile(src, "cs_5_0"); + assert!(!cs.is_empty()); + let param = D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_UAV, + Anonymous: D3D12_ROOT_PARAMETER_0 { Descriptor: D3D12_ROOT_DESCRIPTOR { ShaderRegister: 0, RegisterSpace: 0 } }, + ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, + }; + let desc = D3D12_ROOT_SIGNATURE_DESC { NumParameters: 1, pParameters: ¶m, ..Default::default() }; + let mut blob: Option = None; + D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &mut blob, None).unwrap(); + let blob = blob.unwrap(); + let rs: ID3D12RootSignature = dev.CreateRootSignature(0, std::slice::from_raw_parts(blob.GetBufferPointer() as *const u8, blob.GetBufferSize())).unwrap(); + let code = D3D12_SHADER_BYTECODE { pShaderBytecode: cs.as_ptr() as _, BytecodeLength: cs.len() }; + let pso: ID3D12PipelineState = dev.CreateComputePipelineState(&D3D12_COMPUTE_PIPELINE_STATE_DESC { pRootSignature: transmute_copy(&rs), CS: code, ..Default::default() }).unwrap(); + let ty = D3D12_COMMAND_LIST_TYPE_COMPUTE; + let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: ty, ..Default::default() }).unwrap(); + let sig = signature(&dev, D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH, 12); + let ((args, args_at), (groups, groups_at)) = (cpu_buffer(&dev, 256), cpu_buffer(&dev, 256)); + let (s, a) = (sig.as_raw() as usize, args.as_raw() as usize); + // in range (7), garbage (the bits of 1.0f: none), in range (3 x 4 x 5) + let cases: [[u32; 3]; 3] = [[7, 1, 1], [0x3F80_0000, 1, 1], [3, 4, 5]]; + for (k, want) in cases.iter().enumerate() { + for (i, w) in want.iter().enumerate() { + *args_at.add(4 * k + i) = *w; + } + } + *groups_at = 0; + if let Some(g) = GUARD.lock().unwrap().as_ref() { + g.next.store(0, Ordering::Relaxed); + } + let (_alloc, list) = title_list(&dev, ty); + let raw = list.as_raw(); + type Ei = unsafe extern "system" fn(*mut c_void, usize, u32, usize, u64, usize, u64); + let ei: Ei = std::mem::transmute(real::orig_fn(raw, 59)); + let mut out_ia = false; + for k in 0..cases.len() { + let checked = check_in(raw, 0, s, 1, a, 16 * k as u64, 0, out_ia).expect("checked"); + list.SetComputeRootSignature(&rs); + list.SetPipelineState(&pso); + list.SetComputeRootUnorderedAccessView(0, groups.GetGPUVirtualAddress()); + checked.run_keeping(raw, |b, o| ei(raw, s, 1, b, o, 0, 0)); + out_ia = true; + } + run(&dev, &queue, &list); + assert_eq!(std::ptr::read_volatile(groups_at), 7 + 60, "groups run: the in-range ones only"); + } + } + /// The arguments a draw gets: all of them as they are, or zeros where a count is garbage. #[test] fn an_execute_indirect_with_garbage_counts_draws_nothing() {