From 60f3c5b71b6dd88437cc4403a7471bf4943b4f67 Mon Sep 17 00:00:00 2001 From: Maraco91 <43787609+Maraco91@users.noreply.github.com> Date: Tue, 24 Nov 2026 02:48:40 +0100 Subject: [PATCH] GPU: GetDevice and the DXGI device hand out the device at every level The device answers QueryInterface for ID3D12Device and for its newer levels (Grand Theft Auto VI creates it as ID3D12Device11). A device child's GetDevice and the DXGI device's QueryInterface knew only the first: asked for the device by the interface the title holds, they answered E_NOINTERFACE with a null pointer. Both now ask the device's own test (is_device). Co-Authored-By: Claude Fable 5.2 --- crates/victory-gpu/src/device.rs | 51 +++++++++++++++++++++++++++++++- crates/victory-gpu/src/dxgi.rs | 2 +- crates/victory-gpu/src/object.rs | 5 ++-- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/crates/victory-gpu/src/device.rs b/crates/victory-gpu/src/device.rs index 39a38a0..cce7ed2 100644 --- a/crates/victory-gpu/src/device.rs +++ b/crates/victory-gpu/src/device.rs @@ -88,7 +88,8 @@ const LEVEL_IIDS: [Iid; 9] = [ ), ]; -fn is_device(iid: *const Iid) -> bool { +/// Whether `iid` names the device: its first interface or a newer level. +pub(crate) fn is_device(iid: *const Iid) -> bool { object::is(iid, &[IID]) || object::is(iid, &LEVEL_IIDS) } @@ -445,6 +446,54 @@ mod tests { assert_eq!(missing(), E_NOTIMPL); } + /// A device child, and the DXGI device, hand out the device as any + /// interface the device itself answers to. + #[test] + fn the_device_is_found_at_every_level_through_others() { + type Get = extern "system" fn(This, *const Iid, *mut This) -> HRESULT; + let fence_iid = &crate::fence::FENCE_IIDS[0]; + let mut fence = std::ptr::null(); + assert_eq!( + crate::fence::create(std::ptr::null(), 0, 0, fence_iid, &mut fence), + S_OK + ); + let get_device = slot::(fence, 0x38); + let dxgi = dxgi::device(); + let query = slot::(dxgi, 0); + for iid in std::iter::once(&IID).chain(&LEVEL_IIDS) { + let mut found = std::ptr::null(); + assert_eq!(get_device(fence, iid, &mut found), S_OK, "{iid:02x?}"); + assert_eq!(found, device()); + let mut found = std::ptr::null(); + assert_eq!(query(dxgi, iid, &mut found), S_OK, "{iid:02x?}"); + assert_eq!(found, device()); + } + // What the device is not, it is not through them either. + let mut found = device(); + assert_eq!(get_device(fence, fence_iid, &mut found), E_NOINTERFACE); + assert!(found.is_null()); + slot:: u32>(fence, 0x10)(fence); + } + + /// The host's: a fence's `GetDevice` for `ID3D12Device5`. + #[cfg(windows)] + #[test] + fn the_hosts_children_hand_out_newer_device_levels() { + use windows::Win32::Graphics::Direct3D12::{ + D3D12_FENCE_FLAG_NONE, ID3D12Device5, ID3D12Fence, + }; + let Some(host) = crate::host::host() else { + return; // no host GPU here + }; + // SAFETY: plain calls on the host device and its fence. + unsafe { + let fence: ID3D12Fence = host.device.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let mut device: Option = None; + assert!(fence.GetDevice(&mut device).is_ok()); + assert!(device.is_some()); + } + } + #[test] fn virtual_memory_configuration_reports_user_space() { let mut config = VirtualMemory { diff --git a/crates/victory-gpu/src/dxgi.rs b/crates/victory-gpu/src/dxgi.rs index 1ce11c7..c7f7c45 100644 --- a/crates/victory-gpu/src/dxgi.rs +++ b/crates/victory-gpu/src/dxgi.rs @@ -92,7 +92,7 @@ fn query(kind: usize, iid: *const Iid, known: &[Iid], found: This, out: *mut Thi /// The DXGI device also answers for the D3D12 device. extern "system" fn device_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT { - if object::is(iid, &[crate::device::IID]) { + if crate::device::is_device(iid) { return object::answer(DXGI_DEVICE, iid, Some(crate::device::device()), out); } query(DXGI_DEVICE, iid, &DEVICE_IIDS, this, out) diff --git a/crates/victory-gpu/src/object.rs b/crates/victory-gpu/src/object.rs index 3d48fe3..156c92d 100644 --- a/crates/victory-gpu/src/object.rs +++ b/crates/victory-gpu/src/object.rs @@ -223,13 +223,14 @@ pub(crate) extern "system" fn set_name(this: This, name: *const u16) -> HRESULT S_OK } -/// The device-child method: the device that made the object. +/// The device-child method: the device that made the object, as any of +/// the interfaces the device itself answers to. pub(crate) extern "system" fn parent_device(_: This, iid: *const Iid, out: *mut This) -> HRESULT { let device = crate::device::device(); answer( DEVICE, iid, - is(iid, &[crate::device::IID]).then_some(device), + crate::device::is_device(iid).then_some(device), out, ) }