Quellcode durchsuchen

Merge pull request #249 from Maraco91/gpu/get-device-newer-levels

GPU: GetDevice and the DXGI device hand out the device at every level
Marvis vor 3 Tagen
Ursprung
Commit
88b665822f

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