Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

GPU memory budget: read installed RAM on Windows (#3189)

Dan Hiornscb1c06fc5749
diff --git a/Cargo.lock b/Cargo.lock
index a197330ab..34f537eb4 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 a9af62228..568727163 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 702970dac..e73c59985 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<u64> {
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<u64> {
+ 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<u64> {
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).