From cb1c06fc57490c506534759d9e7e8b3930d4bfd3 Mon Sep 17 00:00:00 2001 From: Dan Hiorns Date: Sun, 11 Oct 2026 23:43:06 -0600 Subject: [PATCH] GPU memory budget: read installed RAM on Windows (#3189) physical_memory() had no Windows implementation, so the wgpu compositor's budget capped layer textures at a quarter of an assumed 16 GB on every Windows machine: too little on a large one (4 GB of a 62 GB PC's 8 GB Memory Usage allowance) and too much on an 8 GB laptop whose integrated GPU shares that RAM. Read it with GlobalMemoryStatusEx through winsafe, the safe binding the desktop app already uses (same pinned version, kernel feature only). Co-authored-by: Claude Opus 5.5 --- Cargo.lock | 1 + crates/ui-egui/Cargo.toml | 5 +++++ crates/ui-egui/src/gpu_canvas.rs | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index a197330ab4..34f537eb40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4599,6 +4599,7 @@ dependencies = [ "serde_json", "sys-locale", "toml", + "winsafe", ] [[package]] diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index a9af622286..568727163b 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -43,6 +43,11 @@ rayon = { workspace = true } # Safe native UI-language APIs (Windows/macOS); no subprocess or web dependency. sys-locale = "=0.3.2" +# Installed RAM for the GPU memory budget (`gpu_canvas::physical_memory`): the safe binding the +# desktop app already uses, pinned to the same version. +[target.'cfg(target_os = "windows")'.dependencies] +winsafe = { version = "=0.0.29", default-features = false, features = ["kernel"] } + [dev-dependencies] # Synthetic raw files for the open-time Camera Raw dialog tests. photocraft-raw = { workspace = true, features = ["testgen"] } diff --git a/crates/ui-egui/src/gpu_canvas.rs b/crates/ui-egui/src/gpu_canvas.rs index 702970dac3..e73c59985a 100644 --- a/crates/ui-egui/src/gpu_canvas.rs +++ b/crates/ui-egui/src/gpu_canvas.rs @@ -1210,7 +1210,12 @@ fn detect_physical_memory() -> Option { String::from_utf8(out.stdout).ok()?.trim().parse().ok() } -#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "freebsd")))] +#[cfg(windows)] +fn detect_physical_memory() -> Option { + winsafe::GlobalMemoryStatusEx().map(|m| m.ullTotalPhys).ok().filter(|b| *b > 0) +} + +#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "freebsd", windows)))] fn detect_physical_memory() -> Option { None } @@ -2160,6 +2165,15 @@ mod tests { assert!(physical_memory().is_none_or(|b| b >= 256 << 20)); } + /// Windows used to report no RAM at all, so the budget assumed 16 GB on every machine. + #[cfg(windows)] + #[test] + fn windows_reports_installed_ram() { + let ram = detect_physical_memory().unwrap_or(0); + assert!(ram >= 256 << 20, "{ram} bytes"); + assert!(ram < 1 << 50, "{ram} bytes"); + } + #[test] fn canvas_texture_bytes_price_every_tile_and_mip_level() { // The same sum DocTextures builds tile by tile (and texture_info reports).