host: prefer high-performance discrete GPU adapter via IDXGIFactory6 #663

Open
Nyutt wants to merge 1 commit from nyutt/merge-high-perf-adapter into post-merge AGit
Contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on Merge: read from crates/darner-gpu/src/host.rs. create() calls D3D12CreateDevice(None, ...) (default adapter enumeration order), which on systems with an integrated GPU enabled (such as Ryzen 7 7800X3D iGPU + discrete RTX GPU, or laptops) can pick the wrong adapter if Windows enumerates the iGPU as index 0.

Add pick_high_performance_adapter() using IDXGIFactory6::EnumAdapterByGpuPreference(..., DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE), skipping software (DXGI_ADAPTER_FLAG_SOFTWARE) adapters and logging the chosen adapter name and vendor ID, falling back to None if enumeration fails.

How it was tested

  • Tested revision: 6e6a0c4 (based on origin/Merge 28a9b42)
  • Commands and results: Not yet run in the game on Merge.
  • Changes a default: yes, on multi-GPU systems where adapter 0 is an iGPU.
  • A/B result: not yet verified in-game on Merge.
  • Checks run on that revision:
    • cargo clippy -p darner-gpu --lib --locked -> 0 new warnings in host.rs
  • Hardware dependence: multi-GPU / hybrid systems (desktop iGPU + dGPU, laptops).
  • Coverage gaps: not yet booted in-game on Merge to observe the "host device: selected high-performance adapter" log line.

Checklist

  • I ran this change in the game, or say below why it can't be (compile + clippy verified; not yet booted on Merge).
  • This PR contains one change based on Merge.
  • The diff contains no game files, code or assets.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: <!-- paste your !myid here if wanted --> ## What it changes - The problem on `Merge`: read from `crates/darner-gpu/src/host.rs`. `create()` calls `D3D12CreateDevice(None, ...)` (default adapter enumeration order), which on systems with an integrated GPU enabled (such as Ryzen 7 7800X3D iGPU + discrete RTX GPU, or laptops) can pick the wrong adapter if Windows enumerates the iGPU as index 0. Add `pick_high_performance_adapter()` using `IDXGIFactory6::EnumAdapterByGpuPreference(..., DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE)`, skipping software (`DXGI_ADAPTER_FLAG_SOFTWARE`) adapters and logging the chosen adapter name and vendor ID, falling back to `None` if enumeration fails. ## How it was tested - Tested revision: `6e6a0c4` (based on `origin/Merge` `28a9b42`) - Commands and results: Not yet run in the game on `Merge`. - Changes a default: yes, on multi-GPU systems where adapter 0 is an iGPU. - A/B result: not yet verified in-game on `Merge`. - Checks run on that revision: - `cargo clippy -p darner-gpu --lib --locked` -> 0 new warnings in `host.rs` - Hardware dependence: multi-GPU / hybrid systems (desktop iGPU + dGPU, laptops). - Coverage gaps: not yet booted in-game on `Merge` to observe the `"host device: selected high-performance adapter"` log line. ## Checklist - [ ] I ran this change in the game, or say below why it can't be (compile + clippy verified; not yet booted on `Merge`). - [x] This PR contains one change based on `Merge`. - [x] The diff contains no game files, code or assets. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found - Replaces or overlaps: none found
TrustNo1 changed title from [pr-fix][rendering] host: prefer high-performance discrete GPU adapter via IDXGIFactory6 to host: prefer high-performance discrete GPU adapter via IDXGIFactory6 2026-12-06 03:25:23 +00:00
Contributor

Tested on a laptop with two GPUs: an RTX 4050 Laptop GPU (6 GB, driver 621.24) and the Ryzen 7 8845HS's Radeon 780M, Windows 11. Windows lists the 780M first.

Without this PR (v2026.12.05.01-test, 28a9b42): the host device is made on the 780M. The picture is washed out at about 16 fps, and the NVIDIA card sits at 0 %. A per-app "High performance" GPU preference in Windows settings doesn't change this, because D3D12CreateDevice(None) ignores it. With the 780M disabled in Device Manager, adapter 0 becomes the Microsoft Basic Render Driver (WARP).

With this PR plus the four changes below (built with cargo build --release --locked -p darner on 6e6a0c4): the device is made on the RTX 4050. The run's console shows DLSS made (960x544 to 1920x1080) and DLSS frame generation 2x on. The first start then compiles the full pipeline set for the new card, about 38,500 pipelines.

I did not run this PR on its own. From reading the code, four more places still ask DXGI for adapter 0, so on a laptop like this they look at the iGPU while the device is on the dGPU:

  • apps/darner/src/play.rs card(): decides whether the card is NVIDIA's. That picks between offering DLSS and FSR files at the start, and is passed to option_switches.
  • apps/darner/src/preload.rs driver(): the adapter and driver recorded in the preload's mark. With the iGPU's driver recorded there, a new NVIDIA driver doesn't start a new preload.
  • crates/darner-gpu/src/mfg.rs first_adapter_name(): the card name for DARNER_FORCE_MFG_RTX40 / MFG Unlocker. With the iGPU first, it reads the iGPU's name, so the RTX 40 force never applies.
  • crates/darner-shader/src/emit/host.rs: its non-WARP device is made with D3D12CreateDevice(None).

Each change is the same as this PR's: EnumAdapterByGpuPreference(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE) on an IDXGIFactory6. Diff on top of 6e6a0c4 (35 lines added, 14 removed):

diff
diff --git a/apps/darner/src/play.rs b/apps/darner/src/play.rs
index a7b58f5..7158b63 100644
--- a/apps/darner/src/play.rs
+++ b/apps/darner/src/play.rs
@@ -606,16 +606,23 @@ fn on_wine() -> bool {
     }
 }
 
-/// The adapter the layer makes its device on (the first, which
-/// `D3D12CreateDevice` takes when given none): whether it is NVIDIA's, and
+/// The adapter the layer makes its device on (the first by high-performance
+/// preference, as the host device takes it): whether it is NVIDIA's, and
 /// its name. `None` when it cannot be told. Asked of DXGI, which does not load
 /// Direct3D 12: this process is yet to pick its runtime.
 fn card() -> Option<(bool, String)> {
     static CARD: OnceLock<Option<(bool, String)>> = OnceLock::new();
     CARD.get_or_init(|| {
-        use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIFactory1};
+        use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IDXGIAdapter1, IDXGIFactory6};
         // SAFETY: plain calls on objects made here.
-        let desc = unsafe { CreateDXGIFactory1::<IDXGIFactory1>().ok()?.EnumAdapters1(0).ok()?.GetDesc1().ok()? };
+        let desc = unsafe {
+            CreateDXGIFactory1::<IDXGIFactory6>()
+                .ok()?
+                .EnumAdapterByGpuPreference::<IDXGIAdapter1>(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE)
+                .ok()?
+                .GetDesc1()
+                .ok()?
+        };
         let len = desc.Description.iter().position(|&c| c == 0).unwrap_or(desc.Description.len());
         Some((desc.VendorId == 0x10DE, String::from_utf16_lossy(&desc.Description[..len])))
     })
diff --git a/apps/darner/src/preload.rs b/apps/darner/src/preload.rs
index 3b0c9b6..c4564ac 100644
--- a/apps/darner/src/preload.rs
+++ b/apps/darner/src/preload.rs
@@ -157,14 +157,16 @@ impl Mark {
     }
 }
 
-/// The first adapter's vendor, device and user-mode driver version (the
-/// adapter the layer makes its device on); empty when it cannot be told.
+/// The high-performance adapter's vendor, device and user-mode driver version
+/// (the adapter the layer makes its device on); empty when it cannot be told.
 fn driver() -> String {
-    use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIDevice, IDXGIFactory1};
+    use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IDXGIAdapter1, IDXGIDevice, IDXGIFactory6};
     use windows::core::Interface;
     // SAFETY: plain calls on objects made here.
     unsafe {
-        let Ok(adapter) = CreateDXGIFactory1::<IDXGIFactory1>().and_then(|f| f.EnumAdapters1(0)) else {
+        let Ok(adapter) = CreateDXGIFactory1::<IDXGIFactory6>()
+            .and_then(|f| f.EnumAdapterByGpuPreference::<IDXGIAdapter1>(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE))
+        else {
             return String::new();
         };
         let ids = adapter.GetDesc1().map(|d| format!("{:04x}:{:04x}", d.VendorId, d.DeviceId)).unwrap_or_default();
diff --git a/crates/darner-gpu/src/mfg.rs b/crates/darner-gpu/src/mfg.rs
index 4eacc02..d340bea 100644
--- a/crates/darner-gpu/src/mfg.rs
+++ b/crates/darner-gpu/src/mfg.rs
@@ -129,14 +129,20 @@ fn load(path: &Path) -> Result<(), u32> {
     if module.is_null() { Err(code) } else { Ok(()) }
 }
 
-/// The name of the first adapter, which a device made with none named is
-/// made on.
+/// The name of the first adapter by high-performance preference, which the
+/// host device is made on.
 fn first_adapter_name() -> Option<String> {
-    use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory2, DXGI_CREATE_FACTORY_FLAGS, IDXGIFactory4};
+    use windows::Win32::Graphics::Dxgi::{
+        CreateDXGIFactory2, DXGI_CREATE_FACTORY_FLAGS, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IDXGIAdapter1, IDXGIFactory6,
+    };
     // SAFETY: plain DXGI queries.
     unsafe {
-        let factory = CreateDXGIFactory2::<IDXGIFactory4>(DXGI_CREATE_FACTORY_FLAGS(0)).ok()?;
-        let desc = factory.EnumAdapters1(0).ok()?.GetDesc1().ok()?;
+        let factory = CreateDXGIFactory2::<IDXGIFactory6>(DXGI_CREATE_FACTORY_FLAGS(0)).ok()?;
+        let desc = factory
+            .EnumAdapterByGpuPreference::<IDXGIAdapter1>(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE)
+            .ok()?
+            .GetDesc1()
+            .ok()?;
         let len = desc.Description.iter().position(|&c| c == 0).unwrap_or(desc.Description.len());
         Some(String::from_utf16_lossy(&desc.Description[..len]))
     }
diff --git a/crates/darner-shader/src/emit/host.rs b/crates/darner-shader/src/emit/host.rs
index f581556..b27d7ad 100644
--- a/crates/darner-shader/src/emit/host.rs
+++ b/crates/darner-shader/src/emit/host.rs
@@ -84,7 +84,13 @@ impl Host {
                 let adapter: IDXGIAdapter = factory.EnumWarpAdapter().map_err(err)?;
                 D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_12_0, &mut device).map_err(err)?;
             } else {
-                D3D12CreateDevice(None, D3D_FEATURE_LEVEL_12_0, &mut device).map_err(err)?;
+                // The high-performance adapter, as the game's host device
+                // takes it: Windows' first adapter is often an integrated GPU.
+                let preferred = CreateDXGIFactory2::<IDXGIFactory6>(DXGI_CREATE_FACTORY_FLAGS(0))
+                    .and_then(|f| f.EnumAdapterByGpuPreference::<IDXGIAdapter1>(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE))
+                    .ok()
+                    .and_then(|a| windows::core::Interface::cast::<windows::core::IUnknown>(&a).ok());
+                D3D12CreateDevice(preferred.as_ref(), D3D_FEATURE_LEVEL_12_0, &mut device).map_err(err)?;
                 // A machine without a graphics card gives the software
                 // adapter as its default device: that is no hardware
                 // device, and is refused as where there is none.

These belong in this PR rather than a new one, so I'm posting them here. I have not run cargo test or cargo clippy on this revision.

This comment was written with AI assistance (Claude Code).

Tested on a laptop with two GPUs: an RTX 4050 Laptop GPU (6 GB, driver 621.24) and the Ryzen 7 8845HS's Radeon 780M, Windows 11. Windows lists the 780M first. **Without this PR** (`v2026.12.05.01-test`, 28a9b42): the host device is made on the 780M. The picture is washed out at about 16 fps, and the NVIDIA card sits at 0 %. A per-app "High performance" GPU preference in Windows settings doesn't change this, because `D3D12CreateDevice(None)` ignores it. With the 780M disabled in Device Manager, adapter 0 becomes the Microsoft Basic Render Driver (WARP). **With this PR plus the four changes below** (built with `cargo build --release --locked -p darner` on 6e6a0c4): the device is made on the RTX 4050. The run's console shows DLSS made (960x544 to 1920x1080) and DLSS frame generation 2x on. The first start then compiles the full pipeline set for the new card, about 38,500 pipelines. I did not run this PR on its own. From reading the code, four more places still ask DXGI for adapter 0, so on a laptop like this they look at the iGPU while the device is on the dGPU: - `apps/darner/src/play.rs` `card()`: decides whether the card is NVIDIA's. That picks between offering DLSS and FSR files at the start, and is passed to `option_switches`. - `apps/darner/src/preload.rs` `driver()`: the adapter and driver recorded in the preload's mark. With the iGPU's driver recorded there, a new NVIDIA driver doesn't start a new preload. - `crates/darner-gpu/src/mfg.rs` `first_adapter_name()`: the card name for `DARNER_FORCE_MFG_RTX40` / MFG Unlocker. With the iGPU first, it reads the iGPU's name, so the RTX 40 force never applies. - `crates/darner-shader/src/emit/host.rs`: its non-WARP device is made with `D3D12CreateDevice(None)`. Each change is the same as this PR's: `EnumAdapterByGpuPreference(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE)` on an `IDXGIFactory6`. Diff on top of 6e6a0c4 (35 lines added, 14 removed): <details><summary>diff</summary> ```diff diff --git a/apps/darner/src/play.rs b/apps/darner/src/play.rs index a7b58f5..7158b63 100644 --- a/apps/darner/src/play.rs +++ b/apps/darner/src/play.rs @@ -606,16 +606,23 @@ fn on_wine() -> bool { } } -/// The adapter the layer makes its device on (the first, which -/// `D3D12CreateDevice` takes when given none): whether it is NVIDIA's, and +/// The adapter the layer makes its device on (the first by high-performance +/// preference, as the host device takes it): whether it is NVIDIA's, and /// its name. `None` when it cannot be told. Asked of DXGI, which does not load /// Direct3D 12: this process is yet to pick its runtime. fn card() -> Option<(bool, String)> { static CARD: OnceLock<Option<(bool, String)>> = OnceLock::new(); CARD.get_or_init(|| { - use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIFactory1}; + use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IDXGIAdapter1, IDXGIFactory6}; // SAFETY: plain calls on objects made here. - let desc = unsafe { CreateDXGIFactory1::<IDXGIFactory1>().ok()?.EnumAdapters1(0).ok()?.GetDesc1().ok()? }; + let desc = unsafe { + CreateDXGIFactory1::<IDXGIFactory6>() + .ok()? + .EnumAdapterByGpuPreference::<IDXGIAdapter1>(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE) + .ok()? + .GetDesc1() + .ok()? + }; let len = desc.Description.iter().position(|&c| c == 0).unwrap_or(desc.Description.len()); Some((desc.VendorId == 0x10DE, String::from_utf16_lossy(&desc.Description[..len]))) }) diff --git a/apps/darner/src/preload.rs b/apps/darner/src/preload.rs index 3b0c9b6..c4564ac 100644 --- a/apps/darner/src/preload.rs +++ b/apps/darner/src/preload.rs @@ -157,14 +157,16 @@ impl Mark { } } -/// The first adapter's vendor, device and user-mode driver version (the -/// adapter the layer makes its device on); empty when it cannot be told. +/// The high-performance adapter's vendor, device and user-mode driver version +/// (the adapter the layer makes its device on); empty when it cannot be told. fn driver() -> String { - use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIDevice, IDXGIFactory1}; + use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IDXGIAdapter1, IDXGIDevice, IDXGIFactory6}; use windows::core::Interface; // SAFETY: plain calls on objects made here. unsafe { - let Ok(adapter) = CreateDXGIFactory1::<IDXGIFactory1>().and_then(|f| f.EnumAdapters1(0)) else { + let Ok(adapter) = CreateDXGIFactory1::<IDXGIFactory6>() + .and_then(|f| f.EnumAdapterByGpuPreference::<IDXGIAdapter1>(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE)) + else { return String::new(); }; let ids = adapter.GetDesc1().map(|d| format!("{:04x}:{:04x}", d.VendorId, d.DeviceId)).unwrap_or_default(); diff --git a/crates/darner-gpu/src/mfg.rs b/crates/darner-gpu/src/mfg.rs index 4eacc02..d340bea 100644 --- a/crates/darner-gpu/src/mfg.rs +++ b/crates/darner-gpu/src/mfg.rs @@ -129,14 +129,20 @@ fn load(path: &Path) -> Result<(), u32> { if module.is_null() { Err(code) } else { Ok(()) } } -/// The name of the first adapter, which a device made with none named is -/// made on. +/// The name of the first adapter by high-performance preference, which the +/// host device is made on. fn first_adapter_name() -> Option<String> { - use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory2, DXGI_CREATE_FACTORY_FLAGS, IDXGIFactory4}; + use windows::Win32::Graphics::Dxgi::{ + CreateDXGIFactory2, DXGI_CREATE_FACTORY_FLAGS, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IDXGIAdapter1, IDXGIFactory6, + }; // SAFETY: plain DXGI queries. unsafe { - let factory = CreateDXGIFactory2::<IDXGIFactory4>(DXGI_CREATE_FACTORY_FLAGS(0)).ok()?; - let desc = factory.EnumAdapters1(0).ok()?.GetDesc1().ok()?; + let factory = CreateDXGIFactory2::<IDXGIFactory6>(DXGI_CREATE_FACTORY_FLAGS(0)).ok()?; + let desc = factory + .EnumAdapterByGpuPreference::<IDXGIAdapter1>(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE) + .ok()? + .GetDesc1() + .ok()?; let len = desc.Description.iter().position(|&c| c == 0).unwrap_or(desc.Description.len()); Some(String::from_utf16_lossy(&desc.Description[..len])) } diff --git a/crates/darner-shader/src/emit/host.rs b/crates/darner-shader/src/emit/host.rs index f581556..b27d7ad 100644 --- a/crates/darner-shader/src/emit/host.rs +++ b/crates/darner-shader/src/emit/host.rs @@ -84,7 +84,13 @@ impl Host { let adapter: IDXGIAdapter = factory.EnumWarpAdapter().map_err(err)?; D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_12_0, &mut device).map_err(err)?; } else { - D3D12CreateDevice(None, D3D_FEATURE_LEVEL_12_0, &mut device).map_err(err)?; + // The high-performance adapter, as the game's host device + // takes it: Windows' first adapter is often an integrated GPU. + let preferred = CreateDXGIFactory2::<IDXGIFactory6>(DXGI_CREATE_FACTORY_FLAGS(0)) + .and_then(|f| f.EnumAdapterByGpuPreference::<IDXGIAdapter1>(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE)) + .ok() + .and_then(|a| windows::core::Interface::cast::<windows::core::IUnknown>(&a).ok()); + D3D12CreateDevice(preferred.as_ref(), D3D_FEATURE_LEVEL_12_0, &mut device).map_err(err)?; // A machine without a graphics card gives the software // adapter as its default device: that is no hardware // device, and is refused as where there is none. ``` </details> These belong in this PR rather than a new one, so I'm posting them here. I have not run `cargo test` or `cargo clippy` on this revision. This comment was written with AI assistance (Claude Code).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/663/head:nyutt/merge-high-perf-adapter'
git switch 'nyutt/merge-high-perf-adapter'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'post-merge'
git merge --no-ff 'nyutt/merge-high-perf-adapter'
git switch 'nyutt/merge-high-perf-adapter'
git rebase 'post-merge'
git switch 'post-merge'
git merge --ff-only 'nyutt/merge-high-perf-adapter'
git switch 'nyutt/merge-high-perf-adapter'
git rebase 'post-merge'
git switch 'post-merge'
git merge --no-ff 'nyutt/merge-high-perf-adapter'
git switch 'post-merge'
git merge --squash 'nyutt/merge-high-perf-adapter'
git switch 'post-merge'
git merge --ff-only 'nyutt/merge-high-perf-adapter'
git switch 'post-merge'
git merge 'nyutt/merge-high-perf-adapter'
git push origin 'post-merge'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!663
No description provided.