Skip to content

Commit

Permalink
[d3d12] make DxgiLib and D3D12Lib methods consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Sep 3, 2024
1 parent 29e288f commit dd01b6d
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 134 deletions.
44 changes: 8 additions & 36 deletions wgpu-hal/src/auxil/dxgi/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,46 +225,27 @@ pub fn create_factory(
// The `DXGI_CREATE_FACTORY_DEBUG` flag is only allowed to be passed to
// `CreateDXGIFactory2` if the debug interface is actually available. So
// we check for whether it exists first.
match lib_dxgi.debug_interface1() {
Ok(pair) => match pair {
Ok(_debug_controller) => {
factory_flags |= Dxgi::DXGI_CREATE_FACTORY_DEBUG;
}
Err(err) => {
log::warn!("Unable to enable DXGI debug interface: {}", err);
}
},
Err(err) => {
log::warn!("Debug interface function for DXGI not found: {:?}", err);
}
if lib_dxgi.debug_interface1().is_ok() {
factory_flags |= Dxgi::DXGI_CREATE_FACTORY_DEBUG;
}

// Intercept `OutputDebugString` calls
super::exception::register_exception_handler();
}

// Try to create IDXGIFactory4
let factory4 = match lib_dxgi.create_factory2(factory_flags) {
Ok(pair) => match pair {
Ok(factory) => Some(factory),
// We hard error here as we _should have_ been able to make a factory4 but couldn't.
Err(err) => {
// err is a Cow<str>, not an Error implementor
return Err(crate::InstanceError::new(format!(
"failed to create IDXGIFactory4: {err:?}"
)));
}
},
let factory4 = match lib_dxgi.create_factory4(factory_flags) {
Ok(factory) => Some(factory),
// If we require factory4, hard error.
Err(err) if required_factory_type == DxgiFactoryType::Factory4 => {
return Err(crate::InstanceError::with_source(
String::from("IDXGIFactory1 creation function not found"),
String::from("IDXGIFactory4 creation failed"),
err,
));
}
// If we don't print it to warn as all win7 will hit this case.
Err(err) => {
log::warn!("IDXGIFactory1 creation function not found: {err:?}");
log::warn!("IDXGIFactory4 creation function not found: {err:?}");
None
}
};
Expand Down Expand Up @@ -293,19 +274,10 @@ pub fn create_factory(

// Try to create IDXGIFactory1
let factory1 = match lib_dxgi.create_factory1() {
Ok(pair) => match pair {
Ok(factory) => factory,
Err(err) => {
// err is a Cow<str>, not an Error implementor
return Err(crate::InstanceError::new(format!(
"failed to create IDXGIFactory1: {err:?}"
)));
}
},
// We always require at least factory1, so hard error
Ok(factory) => factory,
Err(err) => {
return Err(crate::InstanceError::with_source(
String::from("IDXGIFactory1 creation function not found"),
String::from("IDXGIFactory1 creation failed"),
err,
));
}
Expand Down
16 changes: 3 additions & 13 deletions wgpu-hal/src/dx12/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,9 @@ impl super::Adapter {
// Create the device so that we can get the capabilities.
let device = {
profiling::scope!("ID3D12Device::create_device");
match library.create_device(&adapter, Direct3D::D3D_FEATURE_LEVEL_11_0) {
Ok(pair) => match pair {
Ok(device) => device,
Err(err) => {
log::warn!("Device creation failed: {}", err);
return None;
}
},
Err(err) => {
log::warn!("Device creation function is not found: {:?}", err);
return None;
}
}
library
.create_device(&adapter, Direct3D::D3D_FEATURE_LEVEL_11_0)
.ok()?
};

profiling::scope!("feature queries");
Expand Down
51 changes: 14 additions & 37 deletions wgpu-hal/src/dx12/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,20 @@ impl crate::Instance for super::Instance {
.intersects(wgt::InstanceFlags::VALIDATION | wgt::InstanceFlags::GPU_BASED_VALIDATION)
{
// Enable debug layer
match lib_main.debug_interface() {
Ok(pair) => match pair {
Ok(debug_controller) => {
if desc.flags.intersects(wgt::InstanceFlags::VALIDATION) {
unsafe { debug_controller.EnableDebugLayer() }
}
if desc
.flags
.intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION)
{
#[allow(clippy::collapsible_if)]
if let Ok(debug1) = debug_controller.cast::<Direct3D12::ID3D12Debug1>()
{
unsafe { debug1.SetEnableGPUBasedValidation(true) }
} else {
log::warn!("Failed to enable GPU-based validation");
}
}
}
Err(err) => {
log::warn!("Unable to enable D3D12 debug interface: {}", err);
if let Ok(debug_controller) = lib_main.debug_interface() {
if desc.flags.intersects(wgt::InstanceFlags::VALIDATION) {
unsafe { debug_controller.EnableDebugLayer() }
}
if desc
.flags
.intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION)
{
#[allow(clippy::collapsible_if)]
if let Ok(debug1) = debug_controller.cast::<Direct3D12::ID3D12Debug1>() {
unsafe { debug1.SetEnableGPUBasedValidation(true) }
} else {
log::warn!("Failed to enable GPU-based validation");
}
},
Err(err) => {
log::warn!("Debug interface function for D3D12 not found: {:?}", err);
}
}
}
Expand All @@ -70,19 +59,7 @@ impl crate::Instance for super::Instance {
)?;

// Create IDXGIFactoryMedia
let factory_media = match lib_dxgi.create_factory_media() {
Ok(pair) => match pair {
Ok(factory_media) => Some(factory_media),
Err(err) => {
log::error!("Failed to create IDXGIFactoryMedia: {}", err);
None
}
},
Err(err) => {
log::warn!("IDXGIFactory1 creation function not found: {:?}", err);
None
}
};
let factory_media = lib_dxgi.create_factory_media().ok();

let mut supports_allow_tearing = false;
if let Some(factory5) = factory.as_factory5() {
Expand Down
Loading

0 comments on commit dd01b6d

Please sign in to comment.