Skip to content

Commit 912bdcd

Browse files
committed
[d3d12] simplify enumerate_adapters() fn
Also fixes an issue where only high performance adapters would be enumerated if DXGI 1.6 is available.
1 parent 6c2f23c commit 912bdcd

File tree

3 files changed

+11
-78
lines changed

3 files changed

+11
-78
lines changed

wgpu-hal/src/auxil/dxgi/factory.rs

Lines changed: 8 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -47,75 +47,27 @@ fn should_keep_adapter(adapter: &Dxgi::IDXGIAdapter1) -> bool {
4747
}
4848

4949
pub enum DxgiAdapter {
50-
Adapter1(Dxgi::IDXGIAdapter1),
51-
Adapter2(Dxgi::IDXGIAdapter2),
50+
/// Provided by DXGI 1.4
5251
Adapter3(Dxgi::IDXGIAdapter3),
52+
/// Provided by DXGI 1.6
5353
Adapter4(Dxgi::IDXGIAdapter4),
5454
}
5555

56-
impl windows::core::Param<Dxgi::IDXGIAdapter> for &DxgiAdapter {
57-
unsafe fn param(self) -> windows::core::ParamValue<Dxgi::IDXGIAdapter> {
58-
unsafe { self.deref().param() }
59-
}
60-
}
61-
6256
impl Deref for DxgiAdapter {
63-
type Target = Dxgi::IDXGIAdapter;
57+
type Target = Dxgi::IDXGIAdapter3;
6458

6559
fn deref(&self) -> &Self::Target {
6660
match self {
67-
DxgiAdapter::Adapter1(a) => a,
68-
DxgiAdapter::Adapter2(a) => a,
6961
DxgiAdapter::Adapter3(a) => a,
7062
DxgiAdapter::Adapter4(a) => a,
7163
}
7264
}
7365
}
7466

75-
impl DxgiAdapter {
76-
pub fn as_adapter2(&self) -> Option<&Dxgi::IDXGIAdapter2> {
77-
match self {
78-
Self::Adapter1(_) => None,
79-
Self::Adapter2(f) => Some(f),
80-
Self::Adapter3(f) => Some(f),
81-
Self::Adapter4(f) => Some(f),
82-
}
83-
}
84-
85-
pub fn unwrap_adapter2(&self) -> &Dxgi::IDXGIAdapter2 {
86-
self.as_adapter2().unwrap()
87-
}
88-
}
89-
9067
pub fn enumerate_adapters(factory: DxgiFactory) -> Vec<DxgiAdapter> {
9168
let mut adapters = Vec::with_capacity(8);
9269

9370
for cur_index in 0.. {
94-
if let DxgiFactory::Factory6(ref factory6) = factory {
95-
profiling::scope!("IDXGIFactory6::EnumAdapterByGpuPreference");
96-
// We're already at dxgi1.6, we can grab IDXGIAdapter4 directly
97-
let adapter4: Dxgi::IDXGIAdapter4 = match unsafe {
98-
factory6.EnumAdapterByGpuPreference(
99-
cur_index,
100-
Dxgi::DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
101-
)
102-
} {
103-
Ok(a) => a,
104-
Err(e) if e.code() == Dxgi::DXGI_ERROR_NOT_FOUND => break,
105-
Err(e) => {
106-
log::error!("Failed enumerating adapters: {}", e);
107-
break;
108-
}
109-
};
110-
111-
if !should_keep_adapter(&adapter4) {
112-
continue;
113-
}
114-
115-
adapters.push(DxgiAdapter::Adapter4(adapter4));
116-
continue;
117-
}
118-
11971
profiling::scope!("IDXGIFactory1::EnumAdapters1");
12072
let adapter1: Dxgi::IDXGIAdapter1 = match unsafe { factory.EnumAdapters1(cur_index) } {
12173
Ok(a) => a,
@@ -130,31 +82,12 @@ pub fn enumerate_adapters(factory: DxgiFactory) -> Vec<DxgiAdapter> {
13082
continue;
13183
}
13284

133-
// Do the most aggressive casts first, skipping Adapter4 as we definitely don't have dxgi1_6.
134-
135-
// Adapter1 -> Adapter3
136-
match adapter1.cast::<Dxgi::IDXGIAdapter3>() {
137-
Ok(adapter3) => {
138-
adapters.push(DxgiAdapter::Adapter3(adapter3));
139-
continue;
140-
}
141-
Err(err) => {
142-
log::warn!("Failed casting Adapter1 to Adapter3: {}", err);
143-
}
144-
}
145-
146-
// Adapter1 -> Adapter2
147-
match adapter1.cast::<Dxgi::IDXGIAdapter2>() {
148-
Ok(adapter2) => {
149-
adapters.push(DxgiAdapter::Adapter2(adapter2));
150-
continue;
151-
}
152-
Err(err) => {
153-
log::warn!("Failed casting Adapter1 to Adapter2: {}", err);
154-
}
85+
if let Ok(adapter4) = adapter1.cast::<Dxgi::IDXGIAdapter4>() {
86+
adapters.push(DxgiAdapter::Adapter4(adapter4));
87+
} else {
88+
let adapter3 = adapter1.cast::<Dxgi::IDXGIAdapter3>().unwrap();
89+
adapters.push(DxgiAdapter::Adapter3(adapter3));
15590
}
156-
157-
adapters.push(DxgiAdapter::Adapter1(adapter1));
15891
}
15992

16093
adapters

wgpu-hal/src/dx12/adapter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl super::Adapter {
9595

9696
// We have found a possible adapter.
9797
// Acquire the device information.
98-
let desc = unsafe { adapter.unwrap_adapter2().GetDesc2() }.unwrap();
98+
let desc = unsafe { adapter.GetDesc2() }.unwrap();
9999

100100
let device_name = auxil::dxgi::conv::map_adapter_name(desc.Description);
101101

wgpu-hal/src/dx12/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use std::{ffi, fmt, mem, num::NonZeroU32, ops::Deref, sync::Arc};
4949
use arrayvec::ArrayVec;
5050
use parking_lot::{Mutex, RwLock};
5151
use windows::{
52-
core::{Free, Interface, Param as _},
52+
core::{Free, Interface},
5353
Win32::{
5454
Foundation,
5555
Graphics::{Direct3D, Direct3D12, DirectComposition, Dxgi},
@@ -118,7 +118,7 @@ impl D3D12Lib {
118118
let mut result__ = None;
119119

120120
(func)(
121-
unsafe { adapter.param().abi() },
121+
adapter.as_raw(),
122122
feature_level,
123123
// TODO: Generic?
124124
&Direct3D12::ID3D12Device::IID,

0 commit comments

Comments
 (0)