Skip to content

Instantly share code, notes, and snippets.

@qryxip
Last active November 1, 2022 14:00
Show Gist options
  • Save qryxip/be52a959cfb8559e41adfa1bd7445823 to your computer and use it in GitHub Desktop.
Save qryxip/be52a959cfb8559e41adfa1bd7445823 to your computer and use it in GitHub Desktop.
use std::{ffi::OsString, ptr};
use eyre::ensure;
#[cfg(windows)]
fn main() -> eyre::Result<()> {
use std::os::windows::ffi::OsStringExt as _;
use winapi::{
ctypes::c_void,
shared::{
dxgi::{CreateDXGIFactory, IDXGIAdapter, IDXGIFactory, DXGI_ADAPTER_DESC},
winerror::DXGI_ERROR_NOT_FOUND,
},
Interface as _,
};
macro_rules! try_hresult {
($result:expr) => {{
let result = $result;
ensure!(result == 0, "result={result}");
}};
}
unsafe {
let mut factory = ptr::null_mut::<IDXGIFactory>();
let uuid = IDXGIFactory::uuidof();
try_hresult!(CreateDXGIFactory(
ptr::addr_of!(uuid),
ptr::addr_of_mut!(factory) as *mut *mut c_void,
));
let factory = &mut *factory;
let mut i = 0;
loop {
let mut adapter = ptr::null_mut::<IDXGIAdapter>();
let result = factory.EnumAdapters(i, ptr::addr_of_mut!(adapter));
if result == DXGI_ERROR_NOT_FOUND {
break Ok(());
}
try_hresult!(result);
let adapter = &mut *adapter;
let mut desc = DXGI_ADAPTER_DESC::default();
try_hresult!(adapter.GetDesc(ptr::addr_of_mut!(desc)));
eprintln!("====================");
dbg!(i);
dbg!(OsString::from_wide(&desc.Description));
dbg!(desc.VendorId);
dbg!(desc.DeviceId);
dbg!(desc.SubSysId);
dbg!(desc.Revision);
dbg!(desc.DedicatedVideoMemory);
dbg!(desc.DedicatedSystemMemory);
dbg!(desc.SharedSystemMemory);
dbg!(desc.AdapterLuid.LowPart);
dbg!(desc.AdapterLuid.HighPart);
i += 1;
}
}
}
@qryxip
Copy link
Author

qryxip commented Oct 31, 2022

[src\main.rs:45] i = 0
[src\main.rs:46] OsString::from_wide(&desc.Description) = "AMD Radeon(TM) Graphics\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
[src\main.rs:47] desc.VendorId = 4098
[src\main.rs:48] desc.DeviceId = 5688
[src\main.rs:49] desc.SubSysId = 175050792
[src\main.rs:50] desc.Revision = 197
[src\main.rs:51] desc.DedicatedVideoMemory = 520269824
[src\main.rs:52] desc.DedicatedSystemMemory = 0
[src\main.rs:53] desc.SharedSystemMemory = 16831870976
[src\main.rs:54] desc.AdapterLuid.LowPart = 67044
[src\main.rs:55] desc.AdapterLuid.HighPart = 0
====================
[src\main.rs:45] i = 1
[src\main.rs:46] OsString::from_wide(&desc.Description) = "NVIDIA GeForce RTX 3070 Laptop GPU\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
[src\main.rs:47] desc.VendorId = 4318
[src\main.rs:48] desc.DeviceId = 9437
[src\main.rs:49] desc.SubSysId = 177672232
[src\main.rs:50] desc.Revision = 161
[src\main.rs:51] desc.DedicatedVideoMemory = 8434745344
[src\main.rs:52] desc.DedicatedSystemMemory = 0
[src\main.rs:53] desc.SharedSystemMemory = 16831870976
[src\main.rs:54] desc.AdapterLuid.LowPart = 69400
[src\main.rs:55] desc.AdapterLuid.HighPart = 0
====================
[src\main.rs:45] i = 2
[src\main.rs:46] OsString::from_wide(&desc.Description) = "Microsoft Basic Render Driver\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
[src\main.rs:47] desc.VendorId = 5140
[src\main.rs:48] desc.DeviceId = 140
[src\main.rs:49] desc.SubSysId = 0
[src\main.rs:50] desc.Revision = 0
[src\main.rs:51] desc.DedicatedVideoMemory = 0
[src\main.rs:52] desc.DedicatedSystemMemory = 0
[src\main.rs:53] desc.SharedSystemMemory = 16831870976
[src\main.rs:54] desc.AdapterLuid.LowPart = 69312
[src\main.rs:55] desc.AdapterLuid.HighPart = 0

@qryxip
Copy link
Author

qryxip commented Oct 31, 2022

NvOptimusEnablementを宣言するとGeForceとRadeon(統合)が入れ替わる。

====================
[src\main.rs:45] i = 0
[src\main.rs:46] OsString::from_wide(&desc.Description) = "NVIDIA GeForce RTX 3070 Laptop GPU\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
[src\main.rs:47] desc.VendorId = 4318
[src\main.rs:48] desc.DeviceId = 9437
[src\main.rs:49] desc.SubSysId = 177672232
[src\main.rs:50] desc.Revision = 161
[src\main.rs:51] desc.DedicatedVideoMemory = 8434745344
[src\main.rs:52] desc.DedicatedSystemMemory = 0
[src\main.rs:53] desc.SharedSystemMemory = 16831870976
[src\main.rs:54] desc.AdapterLuid.LowPart = 69400
[src\main.rs:55] desc.AdapterLuid.HighPart = 0
====================
[src\main.rs:45] i = 1
[src\main.rs:46] OsString::from_wide(&desc.Description) = "AMD Radeon(TM) Graphics\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
[src\main.rs:47] desc.VendorId = 4098
[src\main.rs:48] desc.DeviceId = 5688
[src\main.rs:49] desc.SubSysId = 175050792
[src\main.rs:50] desc.Revision = 197
[src\main.rs:51] desc.DedicatedVideoMemory = 520269824
[src\main.rs:52] desc.DedicatedSystemMemory = 0
[src\main.rs:53] desc.SharedSystemMemory = 16831870976
[src\main.rs:54] desc.AdapterLuid.LowPart = 67044
[src\main.rs:55] desc.AdapterLuid.HighPart = 0
====================
[src\main.rs:45] i = 2
[src\main.rs:46] OsString::from_wide(&desc.Description) = "Microsoft Basic Render Driver\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
[src\main.rs:47] desc.VendorId = 5140
[src\main.rs:48] desc.DeviceId = 140
[src\main.rs:49] desc.SubSysId = 0
[src\main.rs:50] desc.Revision = 0
[src\main.rs:51] desc.DedicatedVideoMemory = 0
[src\main.rs:52] desc.DedicatedSystemMemory = 0
[src\main.rs:53] desc.SharedSystemMemory = 16831870976
[src\main.rs:54] desc.AdapterLuid.LowPart = 69312
[src\main.rs:55] desc.AdapterLuid.HighPart = 0

@qryxip
Copy link
Author

qryxip commented Nov 1, 2022

Optimusを切った場合、GeForceとMicrosoft Basic Render Driverしか見えなくなる。

@qryxip
Copy link
Author

qryxip commented Nov 1, 2022

NvOptimusEnablementを宣言したバイナリに対しては、NVIDIA Control Panelで"Integrated graphics"を指示してもGeForceが一番上に来る。

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment