From 912bdcd3360368b75be0a622e1420fa412063d68 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Wed, 21 Aug 2024 15:37:43 +0200 Subject: [PATCH] [d3d12] simplify `enumerate_adapters()` fn Also fixes an issue where only high performance adapters would be enumerated if DXGI 1.6 is available. --- wgpu-hal/src/auxil/dxgi/factory.rs | 83 +++--------------------------- wgpu-hal/src/dx12/adapter.rs | 2 +- wgpu-hal/src/dx12/mod.rs | 4 +- 3 files changed, 11 insertions(+), 78 deletions(-) diff --git a/wgpu-hal/src/auxil/dxgi/factory.rs b/wgpu-hal/src/auxil/dxgi/factory.rs index c98aeff15..bf806ab32 100644 --- a/wgpu-hal/src/auxil/dxgi/factory.rs +++ b/wgpu-hal/src/auxil/dxgi/factory.rs @@ -47,75 +47,27 @@ fn should_keep_adapter(adapter: &Dxgi::IDXGIAdapter1) -> bool { } pub enum DxgiAdapter { - Adapter1(Dxgi::IDXGIAdapter1), - Adapter2(Dxgi::IDXGIAdapter2), + /// Provided by DXGI 1.4 Adapter3(Dxgi::IDXGIAdapter3), + /// Provided by DXGI 1.6 Adapter4(Dxgi::IDXGIAdapter4), } -impl windows::core::Param for &DxgiAdapter { - unsafe fn param(self) -> windows::core::ParamValue { - unsafe { self.deref().param() } - } -} - impl Deref for DxgiAdapter { - type Target = Dxgi::IDXGIAdapter; + type Target = Dxgi::IDXGIAdapter3; fn deref(&self) -> &Self::Target { match self { - DxgiAdapter::Adapter1(a) => a, - DxgiAdapter::Adapter2(a) => a, DxgiAdapter::Adapter3(a) => a, DxgiAdapter::Adapter4(a) => a, } } } -impl DxgiAdapter { - pub fn as_adapter2(&self) -> Option<&Dxgi::IDXGIAdapter2> { - match self { - Self::Adapter1(_) => None, - Self::Adapter2(f) => Some(f), - Self::Adapter3(f) => Some(f), - Self::Adapter4(f) => Some(f), - } - } - - pub fn unwrap_adapter2(&self) -> &Dxgi::IDXGIAdapter2 { - self.as_adapter2().unwrap() - } -} - pub fn enumerate_adapters(factory: DxgiFactory) -> Vec { let mut adapters = Vec::with_capacity(8); for cur_index in 0.. { - if let DxgiFactory::Factory6(ref factory6) = factory { - profiling::scope!("IDXGIFactory6::EnumAdapterByGpuPreference"); - // We're already at dxgi1.6, we can grab IDXGIAdapter4 directly - let adapter4: Dxgi::IDXGIAdapter4 = match unsafe { - factory6.EnumAdapterByGpuPreference( - cur_index, - Dxgi::DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, - ) - } { - Ok(a) => a, - Err(e) if e.code() == Dxgi::DXGI_ERROR_NOT_FOUND => break, - Err(e) => { - log::error!("Failed enumerating adapters: {}", e); - break; - } - }; - - if !should_keep_adapter(&adapter4) { - continue; - } - - adapters.push(DxgiAdapter::Adapter4(adapter4)); - continue; - } - profiling::scope!("IDXGIFactory1::EnumAdapters1"); let adapter1: Dxgi::IDXGIAdapter1 = match unsafe { factory.EnumAdapters1(cur_index) } { Ok(a) => a, @@ -130,31 +82,12 @@ pub fn enumerate_adapters(factory: DxgiFactory) -> Vec { continue; } - // Do the most aggressive casts first, skipping Adapter4 as we definitely don't have dxgi1_6. - - // Adapter1 -> Adapter3 - match adapter1.cast::() { - Ok(adapter3) => { - adapters.push(DxgiAdapter::Adapter3(adapter3)); - continue; - } - Err(err) => { - log::warn!("Failed casting Adapter1 to Adapter3: {}", err); - } + if let Ok(adapter4) = adapter1.cast::() { + adapters.push(DxgiAdapter::Adapter4(adapter4)); + } else { + let adapter3 = adapter1.cast::().unwrap(); + adapters.push(DxgiAdapter::Adapter3(adapter3)); } - - // Adapter1 -> Adapter2 - match adapter1.cast::() { - Ok(adapter2) => { - adapters.push(DxgiAdapter::Adapter2(adapter2)); - continue; - } - Err(err) => { - log::warn!("Failed casting Adapter1 to Adapter2: {}", err); - } - } - - adapters.push(DxgiAdapter::Adapter1(adapter1)); } adapters diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 690c9dcb7..2c99fc8ee 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -95,7 +95,7 @@ impl super::Adapter { // We have found a possible adapter. // Acquire the device information. - let desc = unsafe { adapter.unwrap_adapter2().GetDesc2() }.unwrap(); + let desc = unsafe { adapter.GetDesc2() }.unwrap(); let device_name = auxil::dxgi::conv::map_adapter_name(desc.Description); diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index bd0691ec0..723db4e0c 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -49,7 +49,7 @@ use std::{ffi, fmt, mem, num::NonZeroU32, ops::Deref, sync::Arc}; use arrayvec::ArrayVec; use parking_lot::{Mutex, RwLock}; use windows::{ - core::{Free, Interface, Param as _}, + core::{Free, Interface}, Win32::{ Foundation, Graphics::{Direct3D, Direct3D12, DirectComposition, Dxgi}, @@ -118,7 +118,7 @@ impl D3D12Lib { let mut result__ = None; (func)( - unsafe { adapter.param().abi() }, + adapter.as_raw(), feature_level, // TODO: Generic? &Direct3D12::ID3D12Device::IID,