| 1 |
use alloc::{string::String, vec::Vec}; |
| 2 |
use core::ops::Deref; |
| 3 |
|
| 4 |
use windows::{core::Interface as _, Win32::Graphics::Dxgi}; |
| 5 |
|
| 6 |
use crate::dx12::DxgiLib; |
| 7 |
|
| 8 |
use super::result::HResult as _; |
| 9 |
|
| 10 |
// We can rely on the presence of DXGI 1.4 since D3D12 requires WDDM 2.0, Windows 10 (1507), and so does DXGI 1.4. |
| 11 |
|
| 12 |
fn should_keep_adapter(adapter: &Dxgi::IDXGIAdapter1) -> bool { |
| 13 |
let desc = unsafe { adapter.GetDesc1() }.unwrap(); |
| 14 |
|
| 15 |
// The Intel Haswell family of iGPUs had support for the D3D12 API but it was later |
| 16 |
// removed due to a security vulnerability. |
| 17 |
// |
| 18 |
// We are explicitly filtering out all the devices in the family because we are now |
| 19 |
// getting reports of device loss at a later time than at device creation time (`D3D12CreateDevice`). |
| 20 |
// |
| 21 |
// See https://www.intel.com/content/www/us/en/support/articles/000057520/graphics.html |
| 22 |
// This list of device IDs is from https://dgpu-docs.intel.com/devices/hardware-table.html |
| 23 |
let haswell_device_ids = [ |
| 24 |
0x0422, 0x0426, 0x042A, 0x042B, 0x042E, 0x0C22, 0x0C26, 0x0C2A, 0x0C2B, 0x0C2E, 0x0A22, |
| 25 |
0x0A2A, 0x0A2B, 0x0D2A, 0x0D2B, 0x0D2E, 0x0A26, 0x0A2E, 0x0D22, 0x0D26, 0x0412, 0x0416, |
| 26 |
0x0D12, 0x041A, 0x041B, 0x0C12, 0x0C16, 0x0C1A, 0x0C1B, 0x0C1E, 0x0A12, 0x0A1A, 0x0A1B, |
| 27 |
0x0D16, 0x0D1A, 0x0D1B, 0x0D1E, 0x041E, 0x0A16, 0x0A1E, 0x0402, 0x0406, 0x040A, 0x040B, |
| 28 |
0x040E, 0x0C02, 0x0C06, 0x0C0A, 0x0C0B, 0x0C0E, 0x0A02, 0x0A06, 0x0A0A, 0x0A0B, 0x0A0E, |
| 29 |
0x0D02, 0x0D06, 0x0D0A, 0x0D0B, 0x0D0E, |
| 30 |
]; |
| 31 |
if desc.VendorId == 0x8086 && haswell_device_ids.contains(&desc.DeviceId) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
// If run completely headless, windows will show two different WARP adapters, one |
| 36 |
// which is lying about being an integrated card. This is so that programs |
| 37 |
// that ignore software adapters will actually run on headless/gpu-less machines. |
| 38 |
// |
| 39 |
// We don't want that and discourage that kind of filtering anyway, so we skip the integrated WARP. |
| 40 |
if desc.VendorId == 5140 |
| 41 |
&& !Dxgi::DXGI_ADAPTER_FLAG(desc.Flags as i32).contains(Dxgi::DXGI_ADAPTER_FLAG_SOFTWARE) |
| 42 |
{ |
| 43 |
let adapter_name = super::conv::map_adapter_name(desc.Description); |
| 44 |
if adapter_name.contains("Microsoft Basic Render Driver") { |
| 45 |
return false; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
true |
| 50 |
} |
| 51 |
|
| 52 |
#[derive(Clone, Debug)] |
| 53 |
pub enum DxgiAdapter { |
| 54 |
/// Provided by DXGI 1.4 |
| 55 |
Adapter3(Dxgi::IDXGIAdapter3), |
| 56 |
/// Provided by DXGI 1.6 |
| 57 |
Adapter4(Dxgi::IDXGIAdapter4), |
| 58 |
} |
| 59 |
|
| 60 |
impl DxgiAdapter { |
| 61 |
pub fn query_video_memory_info( |
| 62 |
&self, |
| 63 |
group: Dxgi::DXGI_MEMORY_SEGMENT_GROUP, |
| 64 |
) -> Result<Dxgi::DXGI_QUERY_VIDEO_MEMORY_INFO, crate::DeviceError> { |
| 65 |
let mut info = Dxgi::DXGI_QUERY_VIDEO_MEMORY_INFO::default(); |
| 66 |
unsafe { self.QueryVideoMemoryInfo(0, group, &mut info) } |
| 67 |
.into_device_result("QueryVideoMemoryInfo")?; |
| 68 |
Ok(info) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl Deref for DxgiAdapter { |
| 73 |
type Target = Dxgi::IDXGIAdapter3; |
| 74 |
|
| 75 |
fn deref(&self) -> &Self::Target { |
| 76 |
match self { |
| 77 |
DxgiAdapter::Adapter3(a) => a, |
| 78 |
DxgiAdapter::Adapter4(a) => a, |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
pub fn enumerate_adapters(factory: DxgiFactory) -> Vec<DxgiAdapter> { |
| 84 |
let mut adapters = Vec::with_capacity(8); |
| 85 |
|
| 86 |
for cur_index in 0.. { |
| 87 |
profiling::scope!("IDXGIFactory1::EnumAdapters1"); |
| 88 |
let adapter1: Dxgi::IDXGIAdapter1 = match unsafe { factory.EnumAdapters1(cur_index) } { |
| 89 |
Ok(a) => a, |
| 90 |
Err(e) if e.code() == Dxgi::DXGI_ERROR_NOT_FOUND => break, |
| 91 |
Err(e) => { |
| 92 |
log::error!("Failed enumerating adapters: {}", e); |
| 93 |
break; |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
if !should_keep_adapter(&adapter1) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
if let Ok(adapter4) = adapter1.cast::<Dxgi::IDXGIAdapter4>() { |
| 102 |
adapters.push(DxgiAdapter::Adapter4(adapter4)); |
| 103 |
} else { |
| 104 |
let adapter3 = adapter1.cast::<Dxgi::IDXGIAdapter3>().unwrap(); |
| 105 |
adapters.push(DxgiAdapter::Adapter3(adapter3)); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
adapters |
| 110 |
} |
| 111 |
|
| 112 |
#[derive(Clone, Debug)] |
| 113 |
pub enum DxgiFactory { |
| 114 |
/// Provided by DXGI 1.4 |
| 115 |
Factory4(Dxgi::IDXGIFactory4), |
| 116 |
/// Provided by DXGI 1.5 |
| 117 |
Factory5(Dxgi::IDXGIFactory5), |
| 118 |
/// Provided by DXGI 1.6 |
| 119 |
Factory6(Dxgi::IDXGIFactory6), |
| 120 |
} |
| 121 |
|
| 122 |
impl Deref for DxgiFactory { |
| 123 |
type Target = Dxgi::IDXGIFactory4; |
| 124 |
|
| 125 |
fn deref(&self) -> &Self::Target { |
| 126 |
match self { |
| 127 |
DxgiFactory::Factory4(f) => f, |
| 128 |
DxgiFactory::Factory5(f) => f, |
| 129 |
DxgiFactory::Factory6(f) => f, |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
impl DxgiFactory { |
| 135 |
pub fn as_factory5(&self) -> Option<&Dxgi::IDXGIFactory5> { |
| 136 |
match self { |
| 137 |
Self::Factory4(_) => None, |
| 138 |
Self::Factory5(f) => Some(f), |
| 139 |
Self::Factory6(f) => Some(f), |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
pub fn create_factory( |
| 145 |
instance_flags: wgt::InstanceFlags, |
| 146 |
) -> Result<(DxgiLib, DxgiFactory), crate::InstanceError> { |
| 147 |
let lib_dxgi = DxgiLib::new().map_err(|e| { |
| 148 |
crate::InstanceError::with_source(String::from("failed to load dxgi.dll"), e) |
| 149 |
})?; |
| 150 |
|
| 151 |
let mut factory_flags = Dxgi::DXGI_CREATE_FACTORY_FLAGS::default(); |
| 152 |
|
| 153 |
if instance_flags.contains(wgt::InstanceFlags::VALIDATION) { |
| 154 |
// The `DXGI_CREATE_FACTORY_DEBUG` flag is only allowed to be passed to |
| 155 |
// `CreateDXGIFactory2` if the debug interface is actually available. So |
| 156 |
// we check for whether it exists first. |
| 157 |
if let Ok(Some(_)) = lib_dxgi.debug_interface1() { |
| 158 |
factory_flags |= Dxgi::DXGI_CREATE_FACTORY_DEBUG; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
let factory4 = match lib_dxgi.create_factory4(factory_flags) { |
| 163 |
Ok(factory) => factory, |
| 164 |
Err(err) => { |
| 165 |
return Err(crate::InstanceError::with_source( |
| 166 |
String::from("IDXGIFactory4 creation failed"), |
| 167 |
err, |
| 168 |
)); |
| 169 |
} |
| 170 |
}; |
| 171 |
|
| 172 |
if let Ok(factory6) = factory4.cast::<Dxgi::IDXGIFactory6>() { |
| 173 |
return Ok((lib_dxgi, DxgiFactory::Factory6(factory6))); |
| 174 |
} |
| 175 |
|
| 176 |
if let Ok(factory5) = factory4.cast::<Dxgi::IDXGIFactory5>() { |
| 177 |
return Ok((lib_dxgi, DxgiFactory::Factory5(factory5))); |
| 178 |
} |
| 179 |
|
| 180 |
Ok((lib_dxgi, DxgiFactory::Factory4(factory4))) |
| 181 |
} |