diff --git a/wgpu-hal/src/auxil/dxgi/hdr.rs b/wgpu-hal/src/auxil/dxgi/hdr.rs index 3d810ee63..415c21762 100644 --- a/wgpu-hal/src/auxil/dxgi/hdr.rs +++ b/wgpu-hal/src/auxil/dxgi/hdr.rs @@ -13,6 +13,8 @@ use windows::{ }, }; +use crate::dx12::DxgiLib; + /// The primary colors of a color space, in CIE 1931 xy: /// `[[red_x, red_y], [green_x, green_y], [blue_x, blue_y]]`. type Primaries = [[f32; 2]; 3]; @@ -114,6 +116,7 @@ fn display_hdr_info_from_desc1( pub struct DxgiHdrSource { hwnd: HWND, factory: Mutex>, + dxgi_lib: Option, } // SAFETY: `HWND` and `IDXGIFactory1` are `!Send`/`!Sync` in windows-rs. The `HWND` @@ -131,6 +134,9 @@ impl DxgiHdrSource { Self { hwnd, factory: Mutex::new(None), + dxgi_lib: DxgiLib::new() + .inspect_err(|e| log::warn!("DxgiLib::new failed: {e}")) + .ok(), } } @@ -161,7 +167,7 @@ impl DxgiHdrSource { if need_new { // SAFETY: `CreateDXGIFactory1` takes no caller pointers; the `windows` // binding fills the interface out-pointer itself. - match unsafe { Dxgi::CreateDXGIFactory1() } { + match self.dxgi_lib.as_ref()?.create_factory1() { Ok(new) => *factory = Some(new), Err(e) => { log::warn!("CreateDXGIFactory1 failed: {e}"); diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index 285d63566..2995f6a94 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -358,6 +358,25 @@ impl DxgiLib { result__.ok_or(crate::DeviceError::Unexpected).map(Some) } + /// Will error with crate::DeviceError::Unexpected if DXGI 1.1 is not available. + pub fn create_factory1(&self) -> Result { + // Calls windows::Win32::Graphics::Dxgi::CreateDXGIFactory1 on dxgi.dll + type Fun = extern "system" fn( + riid: *const windows_core::GUID, + ppfactory: *mut *mut ffi::c_void, + ) -> windows_core::HRESULT; + let func: libloading::Symbol = + unsafe { self.lib.get(c"CreateDXGIFactory1".to_bytes()) }?; + + let mut result__ = None; + + (func)(&Dxgi::IDXGIFactory1::IID, <*mut _>::cast(&mut result__)) + .ok() + .into_device_result("create_factory1")?; + + result__.ok_or(crate::DeviceError::Unexpected) + } + /// Will error with crate::DeviceError::Unexpected if DXGI 1.4 is not available. pub fn create_factory4( &self,