Skip to content

Commit

Permalink
Use generated COM chain macro
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Mar 2, 2022
1 parent 1ce3c63 commit fce8c14
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 136 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
176 changes: 50 additions & 126 deletions wgpu-hal/src/auxil/dxgi/factory.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand All @@ -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<native::Factory6> {
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<native::Factory2> {
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<native::WeakPtr<dxgi1_5::IDXGIFactory5>> {
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<native::WeakPtr<dxgi1_2::IDXGIAdapter2>> {
let factory6 = self.as_factory6();
pub fn enumerate_adapters(
factory: native::DxgiFactory,
) -> Vec<native::WeakPtr<dxgi1_2::IDXGIAdapter2>> {
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::<dxgi1_2::IDXGIAdapter2>::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::<dxgi::IDXGIAdapter1>::null();
for cur_index in 0.. {
if let Some(factory6) = factory.as_factory6() {
profiling::scope!("IDXGIFactory6::EnumAdapterByGpuPreference");
let mut adapter2 = native::WeakPtr::<dxgi1_2::IDXGIAdapter2>::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 {
Expand All @@ -134,20 +39,39 @@ impl DxgiFactory {
break;
}

match unsafe { adapter1.cast::<dxgi1_2::IDXGIAdapter2>() }.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::<dxgi::IDXGIAdapter1>::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::<dxgi1_2::IDXGIAdapter2>() }.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,
Expand All @@ -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();
Expand Down Expand Up @@ -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 => {
Expand All @@ -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)));
}
}
}
Expand Down Expand Up @@ -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 => {
Expand All @@ -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)))
}
4 changes: 1 addition & 3 deletions wgpu-hal/src/dx11/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![allow(dead_code)]
#![allow(unused_variables)]

use crate::auxil;

mod adapter;
mod command;
mod device;
Expand Down Expand Up @@ -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 {}
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/dx12/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -64,7 +64,7 @@ impl crate::Instance<super::Api> for super::Instance {
}

unsafe fn enumerate_adapters(&self) -> Vec<crate::ExposedAdapter<super::Api>> {
let adapters = self.factory.enumerate_adapters();
let adapters = auxil::dxgi::factory::enumerate_adapters(self.factory);

adapters
.into_iter()
Expand Down
6 changes: 3 additions & 3 deletions wgpu-hal/src/dx12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<native::D3D12Lib>,
_lib_dxgi: native::DxgiLib,
flags: crate::InstanceFlags,
Expand Down Expand Up @@ -126,7 +126,7 @@ enum SurfaceTarget {
}

pub struct Surface {
factory: auxil::dxgi::factory::DxgiFactory,
factory: native::DxgiFactory,
target: SurfaceTarget,
swap_chain: Option<SwapChain>,
}
Expand Down Expand Up @@ -661,7 +661,7 @@ impl crate::Surface<Api> 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,
);
Expand Down

0 comments on commit fce8c14

Please sign in to comment.