diff --git a/Cargo.lock b/Cargo.lock index 259684350db..dc93068a6ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -436,7 +436,7 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" [[package]] name = "d3d12" version = "0.4.1" -source = "git+https://github.com/cwfitzgerald/d3d12-rs.git?rev=0be1b3bc8ba3018a8994f7ebdf1fee8491920b93#0be1b3bc8ba3018a8994f7ebdf1fee8491920b93" +source = "git+https://github.com/cwfitzgerald/d3d12-rs.git?rev=6669e13af9afa300e5b0cd2007d458ded72578e2#6669e13af9afa300e5b0cd2007d458ded72578e2" dependencies = [ "bitflags", "libloading", diff --git a/Cargo.toml b/Cargo.toml index 5c7b908e411..5f9e1538ae9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ default-members = ["wgpu", "wgpu-hal", "wgpu-info"] [patch.crates-io] #naga = { path = "../naga" } #glow = { path = "../glow" } -d3d12 = { git = "https://github.com/cwfitzgerald/d3d12-rs.git", rev = "0be1b3bc8ba3018a8994f7ebdf1fee8491920b93" } +d3d12 = { git = "https://github.com/cwfitzgerald/d3d12-rs.git", rev = "6669e13af9afa300e5b0cd2007d458ded72578e2" } # d3d12 = { path = "../d3d12-rs" } #metal = { path = "../metal-rs" } #web-sys = { path = "../wasm-bindgen/crates/web-sys" } diff --git a/wgpu-hal/src/auxil/dxgi/factory.rs b/wgpu-hal/src/auxil/dxgi/factory.rs index 086847f1526..5f2a72a057c 100644 --- a/wgpu-hal/src/auxil/dxgi/factory.rs +++ b/wgpu-hal/src/auxil/dxgi/factory.rs @@ -1,5 +1,5 @@ use winapi::{ - shared::{dxgi, dxgi1_2, dxgi1_4, dxgi1_5, dxgi1_6, winerror}, + shared::{dxgi, dxgi1_2, dxgi1_6, winerror}, Interface, }; @@ -13,117 +13,22 @@ pub enum DxgiFactoryType { Factory6, } -#[derive(Copy, Clone)] -pub enum DxgiFactory { - Factory1(native::Factory1), - Factory2(native::Factory2), - Factory4(native::Factory4), - Factory6(native::Factory6), -} - -impl DxgiFactory { - pub fn destroy(&self) { - match *self { - DxgiFactory::Factory1(f) => unsafe { f.destroy() }, - DxgiFactory::Factory2(f) => unsafe { f.destroy() }, - DxgiFactory::Factory4(f) => unsafe { f.destroy() }, - DxgiFactory::Factory6(f) => unsafe { f.destroy() }, - } - } - - #[track_caller] - pub fn as_factory6(self) -> Option { - match self { - DxgiFactory::Factory1(_) => None, - DxgiFactory::Factory2(_) => None, - DxgiFactory::Factory4(_) => None, - DxgiFactory::Factory6(f) => Some(f), - } - } - - #[track_caller] - pub fn as_factory1(self) -> native::Factory1 { - match self { - DxgiFactory::Factory1(f) => f, - DxgiFactory::Factory2(f) => unsafe { - native::Factory1::from_raw(f.as_mut_ptr() as *mut dxgi::IDXGIFactory1) - }, - DxgiFactory::Factory4(f) => unsafe { - native::Factory1::from_raw(f.as_mut_ptr() as *mut dxgi::IDXGIFactory1) - }, - DxgiFactory::Factory6(f) => unsafe { - native::Factory1::from_raw(f.as_mut_ptr() as *mut dxgi::IDXGIFactory1) - }, - } - } - - #[track_caller] - pub fn as_factory2(self) -> Option { - match self { - DxgiFactory::Factory1(_) => None, - DxgiFactory::Factory2(f) => Some(f), - DxgiFactory::Factory4(f) => unsafe { - Some(native::Factory2::from_raw( - f.as_mut_ptr() as *mut dxgi1_2::IDXGIFactory2 - )) - }, - DxgiFactory::Factory6(f) => unsafe { - Some(native::Factory2::from_raw( - f.as_mut_ptr() as *mut dxgi1_2::IDXGIFactory2 - )) - }, - } - } - - #[track_caller] - pub fn as_factory5(self) -> Option> { - match self { - DxgiFactory::Factory1(_) => None, - DxgiFactory::Factory2(_) => None, - DxgiFactory::Factory4(_) => None, - DxgiFactory::Factory6(f) => unsafe { - Some(native::WeakPtr::from_raw( - f.as_mut_ptr() as *mut dxgi1_5::IDXGIFactory5 - )) - }, - } - } - - pub fn enumerate_adapters(&self) -> Vec> { - let factory6 = self.as_factory6(); +pub fn enumerate_adapters( + factory: native::DxgiFactory, +) -> Vec> { + let mut adapters = Vec::with_capacity(8); - let mut adapters = Vec::with_capacity(8); - - for cur_index in 0.. { - if let Some(factory) = factory6 { - profiling::scope!("IDXGIFactory6::EnumAdapterByGpuPreference"); - let mut adapter2 = native::WeakPtr::::null(); - let hr = unsafe { - factory.EnumAdapterByGpuPreference( - cur_index, - dxgi1_6::DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, - &dxgi1_2::IDXGIAdapter2::uuidof(), - adapter2.mut_void(), - ) - }; - - if hr == winerror::DXGI_ERROR_NOT_FOUND { - break; - } - if let Err(err) = hr.into_result() { - log::error!("Failed enumerating adapters: {}", err); - break; - } - - adapters.push(adapter2); - continue; - } - - profiling::scope!("IDXGIFactory1::EnumAdapters1"); - let mut adapter1 = native::WeakPtr::::null(); + for cur_index in 0.. { + if let Some(factory6) = factory.as_factory6() { + profiling::scope!("IDXGIFactory6::EnumAdapterByGpuPreference"); + let mut adapter2 = native::WeakPtr::::null(); let hr = unsafe { - self.as_factory1() - .EnumAdapters1(cur_index, adapter1.mut_void() as *mut *mut _) + factory6.EnumAdapterByGpuPreference( + cur_index, + dxgi1_6::DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, + &dxgi1_2::IDXGIAdapter2::uuidof(), + adapter2.mut_void(), + ) }; if hr == winerror::DXGI_ERROR_NOT_FOUND { @@ -134,20 +39,39 @@ impl DxgiFactory { break; } - match unsafe { adapter1.cast::() }.into_result() { - Ok(adapter2) => { - unsafe { adapter1.destroy() }; - adapters.push(adapter2); - } - Err(err) => { - log::error!("Failed casting Adapter1 to Adapter2: {}", err); - break; - } - } + adapters.push(adapter2); + continue; } - adapters + profiling::scope!("IDXGIFactory1::EnumAdapters1"); + let mut adapter1 = native::WeakPtr::::null(); + let hr = unsafe { + factory + .unwrap_factory1() + .EnumAdapters1(cur_index, adapter1.mut_void() as *mut *mut _) + }; + + if hr == winerror::DXGI_ERROR_NOT_FOUND { + break; + } + if let Err(err) = hr.into_result() { + log::error!("Failed enumerating adapters: {}", err); + break; + } + + match unsafe { adapter1.cast::() }.into_result() { + Ok(adapter2) => { + unsafe { adapter1.destroy() }; + adapters.push(adapter2); + } + Err(err) => { + log::error!("Failed casting Adapter1 to Adapter2: {}", err); + break; + } + } } + + adapters } /// Tries to create a IDXGIFactory6, then a IDXGIFactory4, then a IDXGIFactory2, then a IDXGIFactory1, @@ -156,7 +80,7 @@ impl DxgiFactory { pub fn create_factory( required_factory_type: DxgiFactoryType, instance_flags: crate::InstanceFlags, -) -> Result<(native::DxgiLib, DxgiFactory), crate::InstanceError> { +) -> Result<(native::DxgiLib, native::DxgiFactory), crate::InstanceError> { let lib_dxgi = native::DxgiLib::new().map_err(|_| crate::InstanceError)?; let mut factory_flags = native::FactoryCreationFlags::empty(); @@ -214,7 +138,7 @@ pub fn create_factory( unsafe { factory4.destroy(); } - return Ok((lib_dxgi, DxgiFactory::Factory6(factory6))); + return Ok((lib_dxgi, native::DxgiFactory::Factory6(factory6))); } // If we require factory6, hard error. Err(err) if required_factory_type == DxgiFactoryType::Factory6 => { @@ -224,7 +148,7 @@ pub fn create_factory( // If we don't print it to info. Err(err) => { log::info!("Failed to cast IDXGIFactory4 to IDXGIFactory6: {:?}", err); - return Ok((lib_dxgi, DxgiFactory::Factory4(factory4))); + return Ok((lib_dxgi, native::DxgiFactory::Factory4(factory4))); } } } @@ -252,7 +176,7 @@ pub fn create_factory( unsafe { factory1.destroy(); } - return Ok((lib_dxgi, DxgiFactory::Factory2(factory2))); + return Ok((lib_dxgi, native::DxgiFactory::Factory2(factory2))); } // If we require factory2, hard error. Err(err) if required_factory_type == DxgiFactoryType::Factory2 => { @@ -266,5 +190,5 @@ pub fn create_factory( } // We tried to create 4 and 2, but only succeeded with 1. - Ok((lib_dxgi, DxgiFactory::Factory1(factory1))) + Ok((lib_dxgi, native::DxgiFactory::Factory1(factory1))) } diff --git a/wgpu-hal/src/dx11/mod.rs b/wgpu-hal/src/dx11/mod.rs index c068ad91eaa..7cfa88aea59 100644 --- a/wgpu-hal/src/dx11/mod.rs +++ b/wgpu-hal/src/dx11/mod.rs @@ -1,8 +1,6 @@ #![allow(dead_code)] #![allow(unused_variables)] -use crate::auxil; - mod adapter; mod command; mod device; @@ -39,7 +37,7 @@ impl crate::Api for Api { pub struct Instance { lib_dxgi: native::DxgiLib, - factory: auxil::dxgi::factory::DxgiFactory, + factory: native::DxgiFactory, } unsafe impl Send for Instance {} diff --git a/wgpu-hal/src/dx12/instance.rs b/wgpu-hal/src/dx12/instance.rs index be6ca454f6d..6129d53a717 100644 --- a/wgpu-hal/src/dx12/instance.rs +++ b/wgpu-hal/src/dx12/instance.rs @@ -4,7 +4,7 @@ use std::sync::Arc; impl Drop for super::Instance { fn drop(&mut self) { - self.factory.destroy(); + unsafe { self.factory.destroy() }; crate::auxil::dxgi::exception::unregister_exception_handler(); } } @@ -64,7 +64,7 @@ impl crate::Instance for super::Instance { } unsafe fn enumerate_adapters(&self) -> Vec> { - let adapters = self.factory.enumerate_adapters(); + let adapters = auxil::dxgi::factory::enumerate_adapters(self.factory); adapters .into_iter() diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index d4cfb7b8fe0..df19ed7784a 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -86,7 +86,7 @@ const MAX_ROOT_ELEMENTS: usize = 64; const ZERO_BUFFER_SIZE: wgt::BufferAddress = 256 << 10; pub struct Instance { - factory: auxil::dxgi::factory::DxgiFactory, + factory: native::DxgiFactory, library: Arc, _lib_dxgi: native::DxgiLib, flags: crate::InstanceFlags, @@ -126,7 +126,7 @@ enum SurfaceTarget { } pub struct Surface { - factory: auxil::dxgi::factory::DxgiFactory, + factory: native::DxgiFactory, target: SurfaceTarget, swap_chain: Option, } @@ -661,7 +661,7 @@ impl crate::Surface for Surface { // Disable automatic Alt+Enter handling by DXGI. const DXGI_MWA_NO_WINDOW_CHANGES: u32 = 1; const DXGI_MWA_NO_ALT_ENTER: u32 = 2; - self.factory.as_factory1().MakeWindowAssociation( + self.factory.unwrap_factory1().MakeWindowAssociation( wnd_handle, DXGI_MWA_NO_WINDOW_CHANGES | DXGI_MWA_NO_ALT_ENTER, );