-
Couldn't load subscription status.
- Fork 1.1k
Add pci bus and device uuid to AdapterInfo
#8290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cwfitzgerald
merged 13 commits into
gfx-rs:trunk
from
tychedelia:pci-device-adapter-info
Oct 24, 2025
+189
−0
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d63362
Add support for device uuid and pci bus id to AdapterInfo.
tychedelia ec98dbc
Gles.
tychedelia 1f042e9
Option -> String.
tychedelia 3d6087c
Fix vulkan.
tychedelia 9f1caea
User iter methods.
tychedelia 83dab09
Merge branch 'trunk' into pci-device-adapter-info
tychedelia ed0011f
Add Dx12 pci bus info, remove Uuid.
tychedelia f0cf730
Merge branch 'trunk' into pci-device-adapter-info
tychedelia 74bc3fa
Ci.
tychedelia 4234419
Ci.
tychedelia 956f99b
Clippy.
tychedelia c2d5648
Merge branch 'trunk' into pci-device-adapter-info
tychedelia 333bc45
Merge branch 'trunk' into pci-device-adapter-info
tychedelia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,12 @@ use parking_lot::Mutex; | |
| use windows::{ | ||
| core::Interface as _, | ||
| Win32::{ | ||
| Devices::DeviceAndDriverInstallation::{ | ||
| SetupDiDestroyDeviceInfoList, SetupDiEnumDeviceInfo, SetupDiGetClassDevsW, | ||
| SetupDiGetDeviceRegistryPropertyW, DIGCF_PRESENT, GUID_DEVCLASS_DISPLAY, HDEVINFO, | ||
| SPDRP_ADDRESS, SPDRP_BUSNUMBER, SPDRP_HARDWAREID, SP_DEVINFO_DATA, | ||
| }, | ||
| Foundation::{GetLastError, ERROR_NO_MORE_ITEMS}, | ||
| Graphics::{Direct3D, Direct3D12, Dxgi}, | ||
| UI::WindowsAndMessaging, | ||
| }, | ||
|
|
@@ -127,6 +133,7 @@ impl super::Adapter { | |
| } else { | ||
| wgt::DeviceType::DiscreteGpu | ||
| }, | ||
| device_pci_bus_id: get_adapter_pci_info(desc.VendorId, desc.DeviceId), | ||
| driver: { | ||
| if let Ok(i) = unsafe { adapter.CheckInterfaceSupport(&Dxgi::IDXGIDevice::IID) } { | ||
| const MASK: i64 = 0xFFFF; | ||
|
|
@@ -1023,3 +1030,147 @@ impl crate::Adapter for super::Adapter { | |
| wgt::PresentationTimestamp(self.presentation_timer.get_timestamp_ns()) | ||
| } | ||
| } | ||
|
|
||
| fn get_adapter_pci_info(vendor_id: u32, device_id: u32) -> String { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How the hell did you figure out how to do this lmao |
||
| // SAFETY: SetupDiGetClassDevsW is called with valid parameters | ||
| let device_info_set = unsafe { | ||
| match SetupDiGetClassDevsW(Some(&GUID_DEVCLASS_DISPLAY), None, None, DIGCF_PRESENT) { | ||
| Ok(set) => set, | ||
| Err(_) => return String::new(), | ||
| } | ||
| }; | ||
|
|
||
| struct DeviceInfoSetGuard(HDEVINFO); | ||
| impl Drop for DeviceInfoSetGuard { | ||
| fn drop(&mut self) { | ||
| // SAFETY: device_info_set is a valid HDEVINFO and is only dropped once via this guard | ||
| unsafe { | ||
| let _ = SetupDiDestroyDeviceInfoList(self.0); | ||
| } | ||
| } | ||
| } | ||
| let _guard = DeviceInfoSetGuard(device_info_set); | ||
|
|
||
| let mut device_index = 0u32; | ||
| loop { | ||
| let mut device_info_data = SP_DEVINFO_DATA { | ||
| cbSize: size_of::<SP_DEVINFO_DATA>() as u32, | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| // SAFETY: device_info_set is a valid HDEVINFO, device_index starts at 0 and | ||
| // device_info_data is properly initialized above | ||
| unsafe { | ||
| if SetupDiEnumDeviceInfo(device_info_set, device_index, &mut device_info_data).is_err() | ||
| { | ||
| if GetLastError() == ERROR_NO_MORE_ITEMS { | ||
| break; | ||
| } | ||
| device_index += 1; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| let mut hardware_id_size = 0u32; | ||
| // SAFETY: device_info_set and device_info_data are valid | ||
| unsafe { | ||
| let _ = SetupDiGetDeviceRegistryPropertyW( | ||
| device_info_set, | ||
| &device_info_data, | ||
| SPDRP_HARDWAREID, | ||
| None, | ||
| None, | ||
| Some(&mut hardware_id_size), | ||
| ); | ||
| } | ||
|
|
||
| if hardware_id_size == 0 { | ||
| device_index += 1; | ||
| continue; | ||
| } | ||
|
|
||
| let mut hardware_id_buffer = vec![0u8; hardware_id_size as usize]; | ||
| // SAFETY: device_info_set and device_info_data are valid | ||
| unsafe { | ||
| if SetupDiGetDeviceRegistryPropertyW( | ||
| device_info_set, | ||
| &device_info_data, | ||
| SPDRP_HARDWAREID, | ||
| None, | ||
| Some(&mut hardware_id_buffer), | ||
| Some(&mut hardware_id_size), | ||
| ) | ||
| .is_err() | ||
| { | ||
| device_index += 1; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| let hardware_id_u16: Vec<u16> = hardware_id_buffer | ||
| .chunks_exact(2) | ||
| .map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]])) | ||
| .collect(); | ||
| let hardware_ids: Vec<String> = hardware_id_u16 | ||
| .split(|&c| c == 0) | ||
| .filter(|s| !s.is_empty()) | ||
| .map(|s| String::from_utf16_lossy(s).to_uppercase()) | ||
| .collect(); | ||
|
|
||
| // https://learn.microsoft.com/en-us/windows-hardware/drivers/install/identifiers-for-pci-devices | ||
| let expected_id = format!("PCI\\VEN_{vendor_id:04X}&DEV_{device_id:04X}"); | ||
| if !hardware_ids.iter().any(|id| id.contains(&expected_id)) { | ||
| device_index += 1; | ||
| continue; | ||
| } | ||
|
|
||
| let mut bus_buffer = [0u8; 4]; | ||
| let mut data_size = bus_buffer.len() as u32; | ||
| // SAFETY: device_info_set and device_info_data are valid | ||
| let bus_number = unsafe { | ||
| if SetupDiGetDeviceRegistryPropertyW( | ||
| device_info_set, | ||
| &device_info_data, | ||
| SPDRP_BUSNUMBER, | ||
| None, | ||
| Some(&mut bus_buffer), | ||
| Some(&mut data_size), | ||
| ) | ||
| .is_err() | ||
| { | ||
| device_index += 1; | ||
| continue; | ||
| } | ||
| u32::from_le_bytes(bus_buffer) | ||
| }; | ||
|
|
||
| let mut addr_buffer = [0u8; 4]; | ||
| let mut addr_size = addr_buffer.len() as u32; | ||
| // SAFETY: device_info_set and device_info_data are valid | ||
| unsafe { | ||
| if SetupDiGetDeviceRegistryPropertyW( | ||
| device_info_set, | ||
| &device_info_data, | ||
| SPDRP_ADDRESS, | ||
| None, | ||
| Some(&mut addr_buffer), | ||
| Some(&mut addr_size), | ||
| ) | ||
| .is_err() | ||
| { | ||
| device_index += 1; | ||
| continue; | ||
| } | ||
| } | ||
| let address = u32::from_le_bytes(addr_buffer); | ||
|
|
||
| // https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/obtaining-device-configuration-information-at-irql---dispatch-level | ||
| let device = (address >> 16) & 0x0000FFFF; | ||
| let function = address & 0x0000FFFF; | ||
|
|
||
| // domain:bus:device.function | ||
| return format!("{:04x}:{:02x}:{:02x}.{:x}", 0, bus_number, device, function); | ||
| } | ||
|
|
||
| String::new() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kpreid is this a problem for feature sets? I'm never quite sure how implicit features work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh hmm, i may have resolved a merge conflict incorrectly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it — this line should be dropped. It would not hurt but it would not have any effect.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rule is that if
dep:syntax anywhere in[features],then it gets an implicit feature. (When RFC 3491 is implemented in a future edition, implicit features will no longer be created and this will be an error.)