Ver Fonte

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 <[email protected]>
Maraco91 há 3 dias atrás
pai
commit
717f322c3c

+ 50 - 1
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::<Get>(fence, 0x38);
+        let dxgi = dxgi::device();
+        let query = slot::<Get>(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::<extern "system" fn(This) -> 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<ID3D12Device5> = None;
+            assert!(fence.GetDevice(&mut device).is_ok());
+            assert!(device.is_some());
+        }
+    }
+
     #[test]
     fn virtual_memory_configuration_reports_user_space() {
         let mut config = VirtualMemory {

+ 1 - 1
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)

+ 3 - 2
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,
     )
 }