Skip to content

Commit 0cd51d2

Browse files
authored
[d3d12] fix panics in adapter creation (gfx-rs#8845)
1 parent a6d3f4f commit 0cd51d2

2 files changed

Lines changed: 37 additions & 16 deletions

File tree

wgpu-hal/src/dx12/adapter.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,18 @@ impl super::Adapter {
7070
telemetry: Option<crate::Telemetry>,
7171
) -> Option<crate::ExposedAdapter<super::Api>> {
7272
let desc = unsafe { adapter.GetDesc2() }.unwrap();
73-
let driver_version =
74-
unsafe { adapter.CheckInterfaceSupport(&Dxgi::IDXGIDevice::IID) }.unwrap() as u64;
75-
let driver_version = [
76-
(driver_version >> 48) as u16,
77-
(driver_version >> 32) as u16,
78-
(driver_version >> 16) as u16,
79-
driver_version as u16,
80-
];
73+
let driver_version = unsafe { adapter.CheckInterfaceSupport(&Dxgi::IDXGIDevice::IID) };
74+
let driver_version = driver_version
75+
.map(|driver_version| {
76+
let driver_version = driver_version as u64;
77+
[
78+
(driver_version >> 48) as u16,
79+
(driver_version >> 32) as u16,
80+
(driver_version >> 16) as u16,
81+
driver_version as u16,
82+
]
83+
})
84+
.map_err(|e| e.code());
8185

8286
// Create the device so that we can get the capabilities.
8387
let res = {
@@ -124,7 +128,16 @@ impl super::Adapter {
124128
Direct3D::D3D_FEATURE_LEVEL_12_0 => FeatureLevel::_12_0,
125129
Direct3D::D3D_FEATURE_LEVEL_12_1 => FeatureLevel::_12_1,
126130
Direct3D::D3D_FEATURE_LEVEL_12_2 => FeatureLevel::_12_2,
127-
_ => unreachable!(),
131+
fl => {
132+
if let Some(telemetry) = telemetry {
133+
(telemetry.d3d12_expose_adapter)(
134+
&desc,
135+
driver_version,
136+
crate::D3D12ExposeAdapterResult::UnknownFeatureLevel(fl.0),
137+
);
138+
}
139+
return None;
140+
}
128141
};
129142

130143
let device_name = auxil::dxgi::conv::map_adapter_name(desc.Description);
@@ -157,10 +170,20 @@ impl super::Adapter {
157170
// use a version that starts with 10.x.x.x. Versions that ship from Nuget use 1.0.x.x.
158171
//
159172
// As far as we know, this is only an issue on the Nuget versions.
160-
if is_warp && driver_version >= [1, 0, 13, 0] && driver_version[0] < 10 {
161-
workarounds.avoid_shader_debug_info = true;
173+
if let Ok(driver_version) = driver_version {
174+
if is_warp && driver_version >= [1, 0, 13, 0] && driver_version[0] < 10 {
175+
workarounds.avoid_shader_debug_info = true;
176+
}
162177
}
163178

179+
let driver_version_string = {
180+
let driver_version = driver_version.unwrap_or([0, 0, 0, 0]);
181+
format!(
182+
"{}.{}.{}.{}",
183+
driver_version[0], driver_version[1], driver_version[2], driver_version[3]
184+
)
185+
};
186+
164187
let info = wgt::AdapterInfo {
165188
backend: wgt::Backend::Dx12,
166189
name: device_name,
@@ -176,10 +199,7 @@ impl super::Adapter {
176199
wgt::DeviceType::DiscreteGpu
177200
},
178201
device_pci_bus_id: get_adapter_pci_info(desc.VendorId, desc.DeviceId),
179-
driver: format!(
180-
"{}.{}.{}.{}",
181-
driver_version[0], driver_version[1], driver_version[2], driver_version[3]
182-
),
202+
driver: driver_version_string,
183203
driver_info: String::new(),
184204
subgroup_min_size: features1.WaveLaneCountMin,
185205
subgroup_max_size: features1.WaveLaneCountMax,

wgpu-hal/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2814,6 +2814,7 @@ pub struct TlasInstance {
28142814
#[cfg(dx12)]
28152815
pub enum D3D12ExposeAdapterResult {
28162816
CreateDeviceError(dx12::CreateDeviceError),
2817+
UnknownFeatureLevel(i32),
28172818
ResourceBindingTier2Requirement,
28182819
ShaderModel6Requirement,
28192820
Success(dx12::FeatureLevel, dx12::ShaderModel),
@@ -2825,7 +2826,7 @@ pub struct Telemetry {
28252826
#[cfg(dx12)]
28262827
pub d3d12_expose_adapter: fn(
28272828
desc: &windows::Win32::Graphics::Dxgi::DXGI_ADAPTER_DESC2,
2828-
driver_version: [u16; 4],
2829+
driver_version: Result<[u16; 4], windows_core::HRESULT>,
28292830
result: D3D12ExposeAdapterResult,
28302831
),
28312832
}

0 commit comments

Comments
 (0)