Merge pull request #82 from certified-retart/feat/adapter-numeric-model-fallback

This commit is contained in:
ivall
2026-11-23 11:30:51 +01:00
2 changed files with 88 additions and 6 deletions
+55 -5
View File
@@ -3,7 +3,9 @@
//! - `auto` (or empty): the hardware adapters in high-performance order, the first one that
//! creates a device;
//! - `warp`: the software renderer;
//! - a number `N`: the N-th hardware adapter in high-performance order (0 = the first);
//! - a number `N`: the N-th hardware adapter in high-performance order (0 = the first); if `N`
//! is out of range, falls back to matching `N` as a standalone numeric token in the adapter
//! name (so e.g. `5070`, `3080`, or `7900` selects that GPU model);
//! - anything else: the hardware adapters whose name contains it (case-insensitive), e.g.
//! `radeon` or `rtx 3080`.
//!
@@ -34,8 +36,11 @@ impl Selector {
Selector::Auto
} else if s.eq_ignore_ascii_case("warp") {
Selector::Warp
} else if let Ok(n) = s.parse::<usize>() {
Selector::Index(n)
} else if (s == "0" || !s.starts_with('0')) && s.bytes().all(|b| b.is_ascii_digit()) {
match s.parse::<usize>() {
Ok(n) => Selector::Index(n),
Err(_) => Selector::Name(s.to_lowercase()),
}
} else {
Selector::Name(s.to_lowercase())
}
@@ -49,7 +54,8 @@ impl Selector {
}
}
/// Whether the hardware adapter at `index` (high-performance order) named `name` is chosen.
/// Whether the hardware adapter at `index` (high-performance order) named `name` matches
/// directly (without the out-of-range numeric model fallback applied by [`candidates`]).
pub fn allows(&self, index: usize, name: &str) -> bool {
match self {
Selector::Auto => true,
@@ -71,6 +77,44 @@ impl fmt::Display for Selector {
}
}
/// True if `name` contains `digits` not immediately preceded or followed by another ASCII digit
/// (e.g. `"5070"` matches `"RTX 5070"` and `"RX 7900XTX"` matches `"7900"`, while `"2"` does not
/// match `"RTX 2080"`).
fn contains_numeric_token(name: &str, digits: &str) -> bool {
if digits.is_empty() {
return false;
}
let bytes = name.as_bytes();
let mut start = 0;
while let Some(rel) = name[start..].find(digits) {
let pos = start + rel;
let end = pos + digits.len();
let before_ok = pos == 0 || !bytes[pos - 1].is_ascii_digit();
let after_ok = end == bytes.len() || !bytes[end].is_ascii_digit();
if before_ok && after_ok {
return true;
}
start = end;
}
false
}
/// Indices into `names` allowed by `sel`. For `Selector::Index(n)` with `n >= names.len()`,
/// falls back to matching `n` as a standalone numeric token in each adapter's name.
pub(crate) fn select_indices<S: AsRef<str>>(names: &[S], sel: &Selector) -> Vec<usize> {
let direct: Vec<usize> = names.iter().enumerate().filter(|(i, n)| sel.allows(*i, n.as_ref())).map(|(i, _)| i).collect();
if !direct.is_empty() {
return direct;
}
if let Selector::Index(n) = sel {
if *n >= names.len() {
let token = n.to_string();
return names.iter().enumerate().filter(|(_, name)| contains_numeric_token(name.as_ref(), &token)).map(|(i, _)| i).collect();
}
}
Vec::new()
}
/// The adapter's name from its description.
pub fn name(desc: &DXGI_ADAPTER_DESC1) -> String {
let n = desc.Description.iter().position(|&c| c == 0).unwrap_or(desc.Description.len());
@@ -96,7 +140,13 @@ pub fn hardware(factory: &IDXGIFactory6) -> Vec<(IDXGIAdapter1, DXGI_ADAPTER_DES
/// The hardware adapters `sel` allows, in high-performance order (empty for `warp`).
pub fn candidates(factory: &IDXGIFactory6, sel: &Selector) -> Vec<(IDXGIAdapter1, DXGI_ADAPTER_DESC1)> {
hardware(factory).into_iter().enumerate().filter(|(i, (_, d))| sel.allows(*i, &name(d))).map(|(_, a)| a).collect()
let hw = hardware(factory);
let names: Vec<String> = hw.iter().map(|(_, d)| name(d)).collect();
let mut keep = vec![false; hw.len()];
for i in select_indices(&names, sel) {
keep[i] = true;
}
hw.into_iter().enumerate().filter(|(i, _)| keep[*i]).map(|(_, a)| a).collect()
}
/// "`sel` matches no adapter (have: 0 = …, 1 = …)" – for logs and test skips.
+33 -1
View File
@@ -1,11 +1,14 @@
use super::Selector;
use super::{select_indices, Selector};
#[test]
fn selector_parsing() {
assert_eq!(Selector::parse(""), Selector::Auto);
assert_eq!(Selector::parse(" Auto "), Selector::Auto);
assert_eq!(Selector::parse("WARP"), Selector::Warp);
assert_eq!(Selector::parse("0"), Selector::Index(0));
assert_eq!(Selector::parse("1"), Selector::Index(1));
assert_eq!(Selector::parse("007"), Selector::Name("007".into()));
assert_eq!(Selector::parse("+1"), Selector::Name("+1".into()));
assert_eq!(Selector::parse("Radeon"), Selector::Name("radeon".into()));
assert_eq!(Selector::parse(" RTX 3080 "), Selector::Name("rtx 3080".into()));
}
@@ -21,3 +24,32 @@ fn selector_matching() {
assert!(radeon.allows(1, amd) && !radeon.allows(0, nv));
assert!(Selector::parse("geforce rtx").allows(0, nv));
}
#[test]
fn numeric_selector_falls_back_to_model_token_when_out_of_range() {
let gpus = [
"NVIDIA GeForce RTX 5070",
"AMD Radeon™ RX 7900XTX",
"Intel® Arc™ A770 Graphics",
];
// In-range numbers still pick by index.
assert_eq!(select_indices(&gpus, &Selector::parse("0")), vec![0]);
assert_eq!(select_indices(&gpus, &Selector::parse("1")), vec![1]);
assert_eq!(select_indices(&gpus, &Selector::parse("2")), vec![2]);
// Out-of-range numbers match standalone numeric model tokens (bounded by non-digits).
assert_eq!(select_indices(&gpus, &Selector::parse("5070")), vec![0]);
assert_eq!(select_indices(&gpus, &Selector::parse("7900")), vec![1]);
assert_eq!(select_indices(&gpus, &Selector::parse("770")), vec![2]);
// Empty adapter list produces no match.
let empty: [&str; 0] = [];
assert!(select_indices(&empty, &Selector::parse("0")).is_empty());
// Partial digit substrings inside a larger model number must NOT match.
let two = ["NVIDIA GeForce RTX 2080", "AMD Radeon(TM) Graphics"];
assert!(select_indices(&two, &Selector::parse("2")).is_empty());
assert!(select_indices(&two, &Selector::parse("208")).is_empty());
assert!(select_indices(&two, &Selector::parse("80")).is_empty());
assert_eq!(select_indices(&two, &Selector::parse("2080")), vec![0]);
}