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
196 changes: 194 additions & 2 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2711,12 +2711,45 @@ pub(super) unsafe fn record_deferred(wrapper: *mut c_void) {
Deferred::Indirect(sig, max, args, args_off, count_buf, count_off) => super::bind::execute_indirect(seg, sig, max, args, args_off, count_buf, count_off),
}
let ext = *(seg as *const usize);
pc::<unsafe extern "system" fn(*mut c_void) -> i32>(seg, PC_CLOSE)(seg);
let hr = segment_close(wrapper, seg);
real::preserve_vtable(seg, ext);
if hr < 0 {
// a recording the runtime refused is not submitted: the segment leaves the recording
// (a list whose Close failed is still open and cannot be reset), its call is dropped,
// and the next one does not follow it
static CLOSE_ERR: AtomicU64 = AtomicU64::new(0);
warn(&CLOSE_ERR, "deferred call", format_args!("Close failed ({hr:#x}), the call is dropped"));
super::bind::dispatch_dropped();
super::title_list::drop_segment(wrapper, seg);
continue;
}
last = Some(seg);
}
}

/// (tests) What the next native Close of a put-off segment of this wrapper does instead
/// (`segment_close`; tests run in parallel, so it is the given wrapper's only).
#[cfg(test)]
type CloseHook = Box<dyn FnOnce(*mut c_void) -> i32 + Send>;
#[cfg(test)]
static CLOSE_HOOK: Mutex<Vec<(usize, CloseHook)>> = Mutex::new(Vec::new());

/// The native Close of `wrapper`'s put-off segment `p`: its HRESULT.
unsafe fn segment_close(wrapper: *mut c_void, p: *mut c_void) -> i32 {
#[cfg(test)]
{
let hook = {
let mut g = CLOSE_HOOK.lock().unwrap();
g.iter().position(|h| h.0 == wrapper as usize).map(|i| g.remove(i).1)
};
if let Some(hook) = hook {
return hook(p);
}
}
let _ = wrapper;
pc::<unsafe extern "system" fn(*mut c_void) -> i32>(p, PC_CLOSE)(p)
}

/// The allocator `list`'s put-off calls recorded on is reset once the GPU is done with them, so
/// their split dispatches' scratch does not outlive them.
unsafe fn reset_own_alloc(list: *mut c_void) {
Expand Down Expand Up @@ -2969,8 +3002,17 @@ unsafe fn deferred_segment(wrapper: *mut c_void, list: *mut c_void, ty: D3D12_CO
None => real::create_raw_pc_command_list(dev.as_raw(), ty, super::alloc_pool::current(own), 0)?,
};
let ext = *(p as *const usize);
pc::<unsafe extern "system" fn(*mut c_void) -> i32>(p, PC_CLOSE)(p);
let hr = segment_close(wrapper, p);
real::preserve_vtable(p, ext);
if hr < 0 {
// a list that did not close is no segment: the call waits in its list instead
static CLOSE_ERR: AtomicU64 = AtomicU64::new(0);
warn(&CLOSE_ERR, "put-off segment", format_args!("Close failed ({hr:#x})"));
real::dispatch_chain::list_reset(p as usize);
let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(p as *const *const usize)).add(2));
release(p);
return None;
}
super::title_list::push_segment(wrapper, p);
Some(p)
}
Expand Down Expand Up @@ -7079,6 +7121,156 @@ mod tests {
}
}

/// A direct queue, its allocator and a title list wrapped as XCL wraps one, bound to `standin`
/// (None: no adapter).
unsafe fn put_off_rig(standin: usize) -> Option<(ID3D12Device, ID3D12CommandQueue, ID3D12CommandAllocator, *mut c_void, *mut c_void)> {
use super::super::{bind, title_list};
use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0;
use windows::Win32::Graphics::Dxgi::*;
let mut dev: Option<ID3D12Device> = None;
if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two requested AMD RDNA2 tests call D3D12CreateDevice(None, ...), so XCL_ADAPTER=6800M does not select their device. On this hybrid AMD laptop, a standalone call with the same arguments succeeded and its device LUID matched the integrated AMD Radeon(TM) Graphics (DEV_1638), not the RX 6800M (DEV_73DF); the fixture therefore would not enter its WARP fallback but would test the iGPU. Could the rig honor XCL_ADAPTER, or at least print the actual device adapter and WARP fallback status, before these results are counted as RX 6800M coverage? This is source review plus an external default-device probe; I did not run the PR tests.

AI-assisted review: OpenAI Codex (GPT-6; exact serving variant unavailable) and a Luna subagent.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @luckyteagirl for catching this, and @futurepro62 for the full-suite report. You are right: our two new tests share put_off_rig at list_x.rs:7124, whose D3D12CreateDevice(None, ...) call at 7129 ignores XCL_ADAPTER. Their helper calls are at 7166 and 7228. On a hybrid machine they use the default adapter, or WARP if default-device creation fails.

Checked: at 008201c4, list_x.rs has 15 direct D3D12CreateDevice(None, ...) call sites: this shared helper plus 14 other fixture calls at lines 4645, 4667, 4715, 5111, 5935, 6013, 6199, 6278, 6451, 6627, 6794, 6916, 7052 and 7267. Our tests followed that existing pattern, so their adapter attribution needs correcting.

@futurepro62, the two new tests in your “AMD iGPU (XCL_ADAPTER=radeon)” run therefore most likely ran on your default adapter. Could you confirm which adapter that is? We should hold their AMD-specific coverage attribution until the actual device is identified.

I can add the actual adapter name/LUID and WARP status to these two tests now. Making the broader list_x fixtures honor XCL_ADAPTER through gpu::adapter / the runner's selection pattern can be a separate test-only PR.

Not tested: no new device probe or GPU run for this reply; source inspection and the linked reports only.

AI use: Codex (GPT-6.1-Sol) checked the fixture call sites and drafted this reply. Claude Code (Claude Opus 5.5) checked the count and posted it.

let warp = CreateDXGIFactory1::<IDXGIFactory4>().and_then(|f| f.EnumWarpAdapter::<IDXGIAdapter>());
if warp.and_then(|w| D3D12CreateDevice(&w, D3D_FEATURE_LEVEL_11_0, &mut dev)).is_err() {
return None;
}
}
let dev = dev.unwrap();
let ty = D3D12_COMMAND_LIST_TYPE_DIRECT;
let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: ty, ..Default::default() }).unwrap();
let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).unwrap();
let raw: ID3D12GraphicsCommandList = dev.CreateCommandList(0, ty, &alloc, None).unwrap();
let pc0 = raw.into_raw();
real::adopt(pc0, real::Kind::CommandList);
on_create(pc0, alloc.as_raw() as usize, 0, ty);
bind::reset(pc0, alloc.as_raw() as usize, standin);
let wrap = title_list::wrap(pc0);
Some((dev, queue, alloc, pc0, wrap))
}

/// A put-off call whose recording the driver refuses at the submit (its segment's Close
/// fails) is not submitted as it is: the segment goes out empty, the call is dropped, and the
/// rest of the submission runs (its fence is signalled, the device stays).
#[test]
fn a_put_off_call_whose_close_fails_is_not_submitted() {
use super::super::{bind, pso_async, title_list};
use std::time::{Duration, Instant};
unsafe {
let standin = 0x7E57_C105usize;
static BUILD: AtomicBool = AtomicBool::new(false);
BUILD.store(false, Ordering::Release);
pso_async::submit(standin, true, Box::new(|| {
let t = Instant::now();
while !BUILD.load(Ordering::Acquire) && t.elapsed() < Duration::from_secs(10) {
std::thread::sleep(Duration::from_millis(5));
}
0
}));
let Some((dev, queue, _alloc, pc0, wrap)) = put_off_rig(standin) else {
crate::gpu::recomp::runner::skip_test("no GPU");
return;
};
bind::dispatch(pc0, 1, 1, 1);
let mut segs = Vec::new();
assert!(title_list::push_segments(wrap, &mut segs));
assert_eq!(segs.len(), 3, "the dispatch was put off");
let refused = segs[1];
let add_ref: unsafe extern "system" fn(*mut c_void) -> u32 =
std::mem::transmute(*(*(refused as *const *const usize)).add(1));
add_ref(refused); // Keep a test reference so final Release can reveal a lost owner.

// the replay's recording fails in the runtime: a query begun and never ended
let mut heap: Option<ID3D12QueryHeap> = None;
dev.CreateQueryHeap(&D3D12_QUERY_HEAP_DESC { Type: D3D12_QUERY_HEAP_TYPE_OCCLUSION, Count: 1, NodeMask: 0 }, &mut heap).unwrap();
let heap = heap.unwrap();
let h = heap.as_raw() as usize;
static REFUSED: AtomicU32 = AtomicU32::new(0);
CLOSE_HOOK.lock().unwrap().push((wrap as usize, Box::new(move |p| {
pc::<QueryFn>(p, PC_BEGIN_QUERY)(p, h, D3D12_QUERY_TYPE_OCCLUSION, 0);
let hr = pc::<unsafe extern "system" fn(*mut c_void) -> i32>(p, PC_CLOSE)(p);
REFUSED.store(hr as u32, Ordering::Release);
hr
})));
let close_fn: unsafe extern "system" fn(*mut c_void) -> i32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(9));
assert_eq!(close_fn(wrap), 0);
BUILD.store(true, Ordering::Release);
record_deferred(wrap);
assert!(!CLOSE_HOOK.lock().unwrap().iter().any(|h| h.0 == wrap as usize), "the replay closed its segment");
let release = |list: *mut c_void| {
let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(list as *const *const usize)).add(2));
f(list)
};
if (REFUSED.load(Ordering::Acquire) as i32) >= 0 {
// Some runtimes accept Close with an open query, so this run never hits the failure path.
release(wrap);
release(refused);
let _ = pso_async::released(standin);
crate::gpu::recomp::runner::skip_test("the runtime closed a list with a query left open");
return;
}
// what the submit collects after the replay (`q_execute_command_lists`)
segs.clear();
assert!(title_list::push_segments(wrap, &mut segs));
assert_eq!(segs.len(), 2, "the refused segment is not submitted");

let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
type ExecFn = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void);
let exec: ExecFn = std::mem::transmute(*(*(queue.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS));
exec(queue.as_raw(), segs.len() as u32, segs.as_ptr());
queue.Signal(&done, 1).unwrap();
crate::gpu::fence::wait(&done, 1, &dev, None, 2000).expect("the submission runs");
assert!(dev.GetDeviceRemovedReason().is_ok(), "the device stays");

// The failed-Close list cannot Reset. Its next spare reuse must not lose the
// wrapper's owning reference when a new segment is made instead.
assert!(deferred_segment(wrap, pc0, D3D12_COMMAND_LIST_TYPE_DIRECT).is_some());
release(wrap);
assert_eq!(release(refused), 0, "the refused segment retained an untracked owner");
let _ = pso_async::released(standin);
}
}

/// A put-off segment that does not close when it is made is not used: the call waits in its
/// list instead of being put off.
#[test]
fn a_put_off_segment_that_does_not_close_is_not_used() {
use super::super::{bind, pso_async, title_list};
unsafe {
let standin = 0x7E57_C106usize;
static BUILD: AtomicBool = AtomicBool::new(false);
BUILD.store(false, Ordering::Release);
pso_async::submit(standin, true, Box::new(|| {
let t = std::time::Instant::now();
while !BUILD.load(Ordering::Acquire) && t.elapsed() < std::time::Duration::from_secs(10) {
std::thread::sleep(std::time::Duration::from_millis(5));
}
0
}));
let Some((_dev, _queue, _alloc, pc0, wrap)) = put_off_rig(standin) else {
crate::gpu::recomp::runner::skip_test("no GPU");
return;
};
CLOSE_HOOK.lock().unwrap().push((wrap as usize, Box::new(|p| {
pc::<unsafe extern "system" fn(*mut c_void) -> i32>(p, PC_CLOSE)(p);
windows::Win32::Foundation::E_FAIL.0
})));
// (the call that is not put off waits for the build in the call; started here, after
// the device is made, so the build is still going when the call comes)
let ends = std::thread::spawn(|| {
std::thread::sleep(std::time::Duration::from_millis(300));
BUILD.store(true, Ordering::Release);
});
bind::dispatch(pc0, 1, 1, 1);
ends.join().unwrap();
assert!(!CLOSE_HOOK.lock().unwrap().iter().any(|h| h.0 == wrap as usize), "the put-off segment was closed");
let mut segs = Vec::new();
assert!(title_list::push_segments(wrap, &mut segs));
assert_eq!(segs.len(), 1, "nothing was put off");
let rel_fn: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(2));
rel_fn(wrap);
let _ = pso_async::released(standin);
}
}

/// Reproduces the exact intra-list fork-join pattern seen on retail compute queues:
/// q_compute list O_c: Write64(cell_A, frame) -> Wait64(cell_B >= frame) -> Write64(cell_C, frame)
/// q_direct list O_d: Wait64(cell_A >= frame) -> Write64(cell_B, frame)
Expand Down
13 changes: 13 additions & 0 deletions crates/xcl/src/hle/d3d12x/title_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ pub(super) fn put_spare(wrapper: *mut c_void, pc: *mut c_void) {
sg.get_or_insert_with(HashMap::new).entry(wrapper as usize).or_default().spares.push(pc as usize);
}

/// Take a refused segment out of `wrapper`'s recording and release it. A list whose Close failed
/// cannot be reset, so it cannot be kept as a reusable spare.
pub(super) unsafe fn drop_segment(wrapper: *mut c_void, pc: *mut c_void) {
let mut sg = SEGMENTS.lock().unwrap();
if let Some(b) = sg.as_mut().and_then(|m| m.get_mut(&(wrapper as usize))) {
b.extra_active.retain(|&p| p != pc as usize);
}
drop(sg);
super::real::dispatch_chain::list_reset(pc as usize);
let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(pc_slot(pc, 2));
release(pc);
}

/// Add the closed segment `pc` to `wrapper`'s recording, after the ones it has.
pub(super) fn push_segment(wrapper: *mut c_void, pc: *mut c_void) {
SEGMENTS.lock().unwrap().get_or_insert_with(HashMap::new).entry(wrapper as usize).or_default().extra_active.push(pc as usize);
Expand Down
Loading