| 1 |
use alloc::{string::String, sync::Arc, vec::Vec}; |
| 2 |
|
| 3 |
use parking_lot::RwLock; |
| 4 |
use windows::{ |
| 5 |
core::Interface as _, |
| 6 |
Win32::{ |
| 7 |
Foundation, |
| 8 |
Graphics::{Direct3D12, Dxgi}, |
| 9 |
}, |
| 10 |
}; |
| 11 |
|
| 12 |
use super::SurfaceTarget; |
| 13 |
use crate::{auxil, dx12::D3D12Lib}; |
| 14 |
|
| 15 |
impl crate::Instance for super::Instance { |
| 16 |
type A = super::Api; |
| 17 |
|
| 18 |
unsafe fn init(desc: &crate::InstanceDescriptor) -> Result<Self, crate::InstanceError> { |
| 19 |
profiling::scope!("Init DX12 Backend"); |
| 20 |
let lib_main = D3D12Lib::new().map_err(|e| { |
| 21 |
crate::InstanceError::with_source(String::from("failed to load d3d12.dll"), e) |
| 22 |
})?; |
| 23 |
|
| 24 |
if desc |
| 25 |
.flags |
| 26 |
.intersects(wgt::InstanceFlags::VALIDATION | wgt::InstanceFlags::GPU_BASED_VALIDATION) |
| 27 |
{ |
| 28 |
// Enable debug layer |
| 29 |
if let Ok(Some(debug_controller)) = lib_main.debug_interface() { |
| 30 |
if desc.flags.intersects(wgt::InstanceFlags::VALIDATION) { |
| 31 |
unsafe { debug_controller.EnableDebugLayer() } |
| 32 |
} |
| 33 |
if desc |
| 34 |
.flags |
| 35 |
.intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION) |
| 36 |
{ |
| 37 |
#[allow(clippy::collapsible_if)] |
| 38 |
if let Ok(debug1) = debug_controller.cast::<Direct3D12::ID3D12Debug1>() { |
| 39 |
unsafe { debug1.SetEnableGPUBasedValidation(true) } |
| 40 |
} else { |
| 41 |
log::warn!("Failed to enable GPU-based validation"); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
let (lib_dxgi, factory) = auxil::dxgi::factory::create_factory(desc.flags)?; |
| 48 |
|
| 49 |
// Create IDXGIFactoryMedia |
| 50 |
let factory_media = lib_dxgi.create_factory_media().ok(); |
| 51 |
|
| 52 |
let mut supports_allow_tearing = false; |
| 53 |
if let Some(factory5) = factory.as_factory5() { |
| 54 |
let mut allow_tearing = Foundation::FALSE; |
| 55 |
let hr = unsafe { |
| 56 |
factory5.CheckFeatureSupport( |
| 57 |
Dxgi::DXGI_FEATURE_PRESENT_ALLOW_TEARING, |
| 58 |
<*mut _>::cast(&mut allow_tearing), |
| 59 |
size_of_val(&allow_tearing) as u32, |
| 60 |
) |
| 61 |
}; |
| 62 |
|
| 63 |
match hr { |
| 64 |
Err(err) => log::warn!("Unable to check for tearing support: {err}"), |
| 65 |
Ok(()) => supports_allow_tearing = true, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Initialize DXC shader compiler |
| 70 |
let dxc_container = match desc.backend_options.dx12.shader_compiler.clone() { |
| 71 |
wgt::Dx12Compiler::DynamicDxc { |
| 72 |
dxc_path, |
| 73 |
max_shader_model, |
| 74 |
} => { |
| 75 |
let container = super::shader_compilation::get_dynamic_dxc_container( |
| 76 |
dxc_path.into(), |
| 77 |
max_shader_model, |
| 78 |
) |
| 79 |
.map_err(|e| { |
| 80 |
crate::InstanceError::with_source(String::from("Failed to load dynamic DXC"), e) |
| 81 |
})?; |
| 82 |
|
| 83 |
Some(Arc::new(container)) |
| 84 |
} |
| 85 |
wgt::Dx12Compiler::StaticDxc => { |
| 86 |
let container = |
| 87 |
super::shader_compilation::get_static_dxc_container().map_err(|e| { |
| 88 |
crate::InstanceError::with_source( |
| 89 |
String::from("Failed to load static DXC"), |
| 90 |
e, |
| 91 |
) |
| 92 |
})?; |
| 93 |
|
| 94 |
Some(Arc::new(container)) |
| 95 |
} |
| 96 |
wgt::Dx12Compiler::Fxc => None, |
| 97 |
}; |
| 98 |
|
| 99 |
match dxc_container { |
| 100 |
Some(_) => log::debug!("Using DXC for shader compilation"), |
| 101 |
None => log::debug!("Using FXC for shader compilation"), |
| 102 |
} |
| 103 |
|
| 104 |
Ok(Self { |
| 105 |
// The call to create_factory will only succeed if we get a factory4, so this is safe. |
| 106 |
factory, |
| 107 |
factory_media, |
| 108 |
library: Arc::new(lib_main), |
| 109 |
_lib_dxgi: lib_dxgi, |
| 110 |
supports_allow_tearing, |
| 111 |
flags: desc.flags, |
| 112 |
memory_budget_thresholds: desc.memory_budget_thresholds, |
| 113 |
dxc_container, |
| 114 |
}) |
| 115 |
} |
| 116 |
|
| 117 |
unsafe fn create_surface( |
| 118 |
&self, |
| 119 |
_display_handle: raw_window_handle::RawDisplayHandle, |
| 120 |
window_handle: raw_window_handle::RawWindowHandle, |
| 121 |
) -> Result<super::Surface, crate::InstanceError> { |
| 122 |
match window_handle { |
| 123 |
raw_window_handle::RawWindowHandle::Win32(handle) => Ok(super::Surface { |
| 124 |
factory: self.factory.clone(), |
| 125 |
factory_media: self.factory_media.clone(), |
| 126 |
// https://github.com/rust-windowing/raw-window-handle/issues/171 |
| 127 |
target: SurfaceTarget::WndHandle(Foundation::HWND(handle.hwnd.get() as *mut _)), |
| 128 |
supports_allow_tearing: self.supports_allow_tearing, |
| 129 |
swap_chain: RwLock::new(None), |
| 130 |
}), |
| 131 |
_ => Err(crate::InstanceError::new(format!( |
| 132 |
"window handle {window_handle:?} is not a Win32 handle" |
| 133 |
))), |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
unsafe fn enumerate_adapters( |
| 138 |
&self, |
| 139 |
_surface_hint: Option<&super::Surface>, |
| 140 |
) -> Vec<crate::ExposedAdapter<super::Api>> { |
| 141 |
let adapters = auxil::dxgi::factory::enumerate_adapters(self.factory.clone()); |
| 142 |
|
| 143 |
adapters |
| 144 |
.into_iter() |
| 145 |
.filter_map(|raw| { |
| 146 |
super::Adapter::expose( |
| 147 |
raw, |
| 148 |
&self.library, |
| 149 |
self.flags, |
| 150 |
self.memory_budget_thresholds, |
| 151 |
self.dxc_container.clone(), |
| 152 |
) |
| 153 |
}) |
| 154 |
.collect() |
| 155 |
} |
| 156 |
} |