PhotoCraft: An open-source, photo editor written by scratch in pure Rust
perf(gpu): cap the GPU memory budgets by a discrete GPU's own memory (#5234)
FinnNk297520b9c82d
diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.tomlindex aeefacaf2..61f21885f 100644--- a/crates/ui-egui/Cargo.toml+++ b/crates/ui-egui/Cargo.toml@@ -47,7 +47,7 @@ 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"] }+winsafe = { version = "=0.0.29", default-features = false, features = ["kernel", "dxgi"] }[dev-dependencies]# egui's own default fonts in tests: the oracle for egui_fonts.rs, and contexts made withdiff --git a/crates/ui-egui/examples/huge_doc_bench.rs b/crates/ui-egui/examples/huge_doc_bench.rsindex 0454aefc3..6b0d03cda 100644--- a/crates/ui-egui/examples/huge_doc_bench.rs+++ b/crates/ui-egui/examples/huge_doc_bench.rs@@ -200,9 +200,17 @@ fn main() {.map(|s| s.tiles().map(|(_, t)| t.bytes().len() as u64).sum::<u64>()).sum();let ram = photocraft_ui_egui::gpu_canvas::physical_memory();- let budget = photocraft_ui_egui::gpu_canvas::memory_budget(allowance, pixels, ram);+ let vram = g.video_memory();+ let budget = photocraft_ui_egui::gpu_canvas::memory_budget(allowance, pixels, ram, vram);g.set_memory_budget(budget);- println!("RAM {} MB, document pixels {} MB, Memory Usage {} MB -> GPU budget {} MB", ram.unwrap_or(0) >> 20, pixels >> 20, allowance >> 20, budget >> 20);+ println!(+ "RAM {} MB, VRAM {} MB, document pixels {} MB, Memory Usage {} MB -> GPU budget {} MB",+ ram.unwrap_or(0) >> 20,+ vram.unwrap_or(0) >> 20,+ pixels >> 20,+ allowance >> 20,+ budget >> 20+ );let first_view = Rect::from_xywh((w as f32 * 0.1) as i32, (h as f32 * 0.1) as i32, 2560, 1440);g.set_focus(Some(if op == "view" { first_view } else { doc.bounds() }));diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 3f4ae7a34..a85c25c3c 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -1674,8 +1674,9 @@ fn ensure_gpu(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, visible:}/// Point the wgpu compositor at what the view shows, and size its memory budget from Memory-/// Usage, the document's pixels and History, and physical memory: on full refreshes (pixels-/// counted once per structural change, not per brush dab) and whenever the preference changes.+/// Usage, the document's pixels and History, physical memory and the GPU's own: on full refreshes+/// (pixels counted once per structural change, not per brush dab) and whenever the preference+/// changes.fn gpu_budget(app: &mut PhotocraftApp, gpu: &crate::gpu_canvas::GpuCanvas, idx: usize, partial: bool, visible: DRect) {gpu.set_focus(Some(visible));let allowance = u64::from(app.session.prefs().performance.memory_usage_mb).saturating_mul(1 << 20);@@ -1684,7 +1685,7 @@ fn gpu_budget(app: &mut PhotocraftApp, gpu: &crate::gpu_canvas::GpuCanvas, idx:}let Some(st) = app.session.documents().get(idx) else { return };let pixels = st.memory_bytes() as u64;- let budget = crate::gpu_canvas::memory_budget(allowance, pixels, crate::gpu_canvas::physical_memory());+ let budget = crate::gpu_canvas::memory_budget(allowance, pixels, crate::gpu_canvas::physical_memory(), gpu.video_memory());gpu.set_memory_budget(budget);app.perf.gpu_budget_mb = budget >> 20;app.perf.gpu_budget_allowance = allowance;diff --git a/crates/ui-egui/src/gpu_canvas.rs b/crates/ui-egui/src/gpu_canvas.rsindex 391b0b64a..e53e89888 100644--- a/crates/ui-egui/src/gpu_canvas.rs+++ b/crates/ui-egui/src/gpu_canvas.rs@@ -27,6 +27,7 @@use std::collections::HashMap;use std::num::NonZeroU64;+use std::sync::atomic::Ordering;use eframe::egui_wgpu::{self, CallbackResources, CallbackTrait, RenderState, ScreenDescriptor};use eframe::wgpu;@@ -110,6 +111,11 @@ pub struct GpuCanvas {tile: u32,high: HighPolicy,health: photocraft_gpu::DeviceHealth,+ /// [`video_memory`] of the adapter, detected once.+ vram: Option<u64>,+ /// [`canvas_budget`], shared with the paint callback's resources: [`GpuCanvas::format_for`]+ /// reads it while the renderer is locked.+ canvas_budget: std::sync::Arc<std::sync::atomic::AtomicU64>,}/// Whether `regenerate_mips` builds each level in a scratch texture and copies it back: on@@ -158,9 +164,16 @@ impl GpuCanvas {res.health = health.clone();let info = rs.adapter.get_info();res.separate_mip_targets = separate_mip_targets(info.backend, info.vendor, std::env::var("PHOTOCRAFT_SEPARATE_MIPS").ok().as_deref());+ let vram = video_memory(&info);+ res.canvas_budget.store(canvas_budget(physical_memory(), vram), Ordering::Relaxed);+ let budget = res.canvas_budget.clone();rs.renderer.write().callback_resources.insert(res);- log::info!("gpu canvas: target {:?}, max texture {max}, tile {tile}, 16F canvas {high:?}", rs.target_format);- Self { rs: rs.clone(), tile, high, health }+ log::info!(+ "gpu canvas: target {:?}, max texture {max}, tile {tile}, 16F canvas {high:?}, video memory {}",+ rs.target_format,+ vram.map_or_else(|| "unknown".to_owned(), |b| format!("{} MB", b >> 20))+ );+ Self { rs: rs.clone(), tile, high, health, vram, canvas_budget: budget }}/// The device's health flag (shared with the wgpu compositor and the paint callback).@@ -192,6 +205,12 @@ impl GpuCanvas {self.rs.adapter.get_info()}+ /// The adapter's own memory where it has one and the platform reports it (see+ /// [`video_memory`]); it caps [`memory_budget`] and [`canvas_budget`].+ pub fn video_memory(&self) -> Option<u64> {+ self.vram+ }+/// The canvas texture format for a document of `depth` and `size`: `Rgba16Float` for 16/32-bit/// documents when the adapter supports it and the document fits [`F16_BUDGET_PX`], else/// `Rgba8Unorm`.@@ -204,7 +223,11 @@ impl GpuCanvas {let px = size[0] as u64 * size[1] as u64;let high = match self.high {HighPolicy::Off => false,- HighPolicy::Budget => px <= F16_BUDGET_PX,+ // Within the canvas budget too: a 16-bit canvas a small card can't hold as floats+ // stays on the GPU as `Rgba8Unorm` rather than falling back to the CPU canvas.+ HighPolicy::Budget => {+ px <= F16_BUDGET_PX && canvas_texture_bytes(size, self.tile, texel_bytes(FORMAT_HIGH)) <= self.canvas_budget.load(Ordering::Relaxed)+ }HighPolicy::Always => true,};if high && depth != photocraft_doc::SampleType::U8 { FORMAT_HIGH } else { FORMAT }@@ -240,10 +263,7 @@ impl GpuCanvas {/// Set the GPU bytes one document's display texture set may take (see [`canvas_budget`]).pub fn set_canvas_budget(&self, bytes: u64) {- let mut r = self.rs.renderer.write();- if let Some(res) = r.callback_resources.get_mut::<Resources>() {- res.canvas_budget = bytes;- }+ self.canvas_budget.store(bytes, Ordering::Relaxed);}/// Whether `doc`'s display textures fit [`canvas_budget`]; when they don't, nothing is@@ -252,7 +272,7 @@ impl GpuCanvas {let size = [doc.size.width, doc.size.height];let bytes = canvas_texture_bytes(size, self.tile, texel_bytes(self.format_for(doc.depth, size)));let r = self.rs.renderer.read();- r.callback_resources.get::<Resources>().is_some_and(|res| bytes <= res.canvas_budget)+ r.callback_resources.get::<Resources>().is_some_and(|res| bytes <= res.canvas_budget.load(Ordering::Relaxed))}/// Cap lower-stack composite reuse; zero disables it for profiling the uncached path.@@ -934,6 +954,9 @@ pub struct GpuInfo {pub device_type: String,/// Driver name and version as the driver reports them.pub driver: String,+ /// The adapter's own memory in MB ([`video_memory`]): discrete GPUs where the platform+ /// reports it. It caps the GPU memory budgets.+ pub video_memory_mb: Option<u64>,/// `performance.gpuBackend` at launch.pub preference: String,/// The backend startup chose ("auto", "dx12", "cpu", …, or "env" for `WGPU_BACKEND`).@@ -964,6 +987,7 @@ impl GpuInfo {}.to_string();self.driver = [info.driver.as_str(), info.driver_info.as_str()].iter().filter(|s| !s.is_empty()).copied().collect::<Vec<_>>().join(" ");+ self.video_memory_mb = video_memory(info).map(|b| b >> 20);}/// Human-readable lines for Help › System Info.@@ -1196,13 +1220,22 @@ pub const OVER_BUDGET: &str = "layers exceed the GPU memory budget; full refresh/// GPU memory the wgpu compositor may hold for layer pages and effect maps: what Memory Usage/// (`allowance`, Preferences › Performance) leaves after the document's pixels and History-/// (`pixels`), at most a quarter of physical memory (`ram`; 16 GB assumed when unknown), and at-/// least [`MIN_GPU_BUDGET`]. On unified-memory machines these textures share RAM with the-/// document, so a huge document keeps its layers on the GPU only as far as memory allows; the-/// rest is uploaded per refresh as the view needs it.-pub fn memory_budget(allowance: u64, pixels: u64, ram: Option<u64>) -> u64 {+/// (`pixels`), at most a quarter of physical memory (`ram`; 16 GB assumed when unknown) and+/// [`vram_share`] of a discrete GPU's own memory (`vram`, from [`video_memory`]), and at least+/// [`MIN_GPU_BUDGET`]. On unified-memory machines these textures share RAM with the document, so+/// a huge document keeps its layers on the GPU only as far as memory allows; the rest is+/// uploaded per refresh as the view needs it.+pub fn memory_budget(allowance: u64, pixels: u64, ram: Option<u64>, vram: Option<u64>) -> u64 {let ram = ram.unwrap_or(16 << 30);- allowance.saturating_sub(pixels).min(ram / 4).max(MIN_GPU_BUDGET)+ allowance.saturating_sub(pixels).min(ram / 4).min(vram.map_or(u64::MAX, vram_share)).max(MIN_GPU_BUDGET)+}++/// The share of a discrete GPU's memory each of [`memory_budget`] and [`canvas_budget`] may+/// plan for: three eighths, so the two together leave a quarter of the card to the window's+/// swapchain, egui and other applications instead of over-committing it and letting the driver+/// page (#4536).+pub fn vram_share(vram: u64) -> u64 {+ vram / 8 * 3}/// Why a document whose display textures don't fit [`canvas_budget`] is drawn on the CPU canvas.@@ -1220,11 +1253,12 @@ pub const REFRESH_DEFERRED: &str = "deferred";pub const DEFER_PX: u64 = 1 << 22;/// GPU bytes one document's display texture set (every tile and mip level) may take: a quarter-/// of physical memory (`ram`; 16 GB assumed when unknown), at least [`MIN_GPU_BUDGET`]. The set-/// covers the whole canvas at full resolution, so its size follows the declared canvas, not the-/// layer pixels; a larger canvas is drawn on the (reduced) CPU canvas instead (#2934).-pub fn canvas_budget(ram: Option<u64>) -> u64 {- (ram.unwrap_or(16 << 30) / 4).max(MIN_GPU_BUDGET)+/// of physical memory (`ram`; 16 GB assumed when unknown) and at most [`vram_share`] of a+/// discrete GPU's own memory (`vram`), at least [`MIN_GPU_BUDGET`]. The set covers the whole+/// canvas at full resolution, so its size follows the declared canvas, not the layer pixels; a+/// larger canvas is drawn on the (reduced) CPU canvas instead (#2934).+pub fn canvas_budget(ram: Option<u64>, vram: Option<u64>) -> u64 {+ (ram.unwrap_or(16 << 30) / 4).min(vram.map_or(u64::MAX, vram_share)).max(MIN_GPU_BUDGET)}/// GPU bytes of a display texture set for a `size` canvas in `tile`-sided tiles of `bpp` bytes@@ -1288,6 +1322,69 @@ fn detect_physical_memory() -> Option<u64> {None}+/// The memory of `adapter` itself, in bytes, when it is a discrete GPU and the platform reports+/// it: DXGI's dedicated video memory on Windows, amdgpu's VRAM size on Linux. `None` for+/// integrated and software adapters, whose textures live in system memory (the RAM-based+/// budgets already cover them), and where nothing reports it. `PHOTOCRAFT_VRAM_MB` overrides it+/// for any adapter (to simulate a smaller card).+pub fn video_memory(adapter: &wgpu::AdapterInfo) -> Option<u64> {+ if let Some(mb) = std::env::var("PHOTOCRAFT_VRAM_MB").ok().and_then(|v| v.trim().parse::<u64>().ok()) {+ return mb.checked_mul(1 << 20).filter(|b| *b > 0);+ }+ if adapter.device_type != wgpu::DeviceType::DiscreteGpu {+ return None;+ }+ // Detected once per adapter, as `physical_memory` is: on Windows each detection builds a+ // DXGI factory, and startup asks three times. Keyed by the adapter, since a GPU retry can+ // pick another one.+ /// (vendor, device) ids and the memory found for them.+ type Seen = Vec<((u32, u32), Option<u64>)>;+ static SEEN: std::sync::Mutex<Seen> = std::sync::Mutex::new(Vec::new());+ let key = (adapter.vendor, adapter.device);+ let mut seen = SEEN.lock().unwrap_or_else(std::sync::PoisonError::into_inner);+ if let Some((_, bytes)) = seen.iter().find(|(k, _)| *k == key) {+ return *bytes;+ }+ let bytes = detect_video_memory(key.0, key.1).filter(|b| *b > 0);+ seen.push((key, bytes));+ bytes+}++#[cfg(windows)]+fn detect_video_memory(vendor: u32, device: u32) -> Option<u64> {+ use winsafe::prelude::*;+ let factory = winsafe::CreateDXGIFactory1().ok()?;+ factory+ .EnumAdapters1()+ .filter_map(|a| a.ok()?.GetDesc1().ok())+ .find(|d| d.VendorId == vendor && d.DeviceId == device)+ .and_then(|d| u64::try_from(d.DedicatedVideoMemory).ok())+}++#[cfg(target_os = "linux")]+fn detect_video_memory(vendor: u32, device: u32) -> Option<u64> {+ drm_video_memory(std::path::Path::new("/sys/class/drm"), vendor, device)+}++/// amdgpu's VRAM size (`<card>/device/mem_info_vram_total`, bytes) of the card under `drm` (the+/// sysfs `/sys/class/drm` folder) whose PCI `vendor` and `device` ids (hex files beside it) match.+#[cfg(any(target_os = "linux", test))]+fn drm_video_memory(drm: &std::path::Path, vendor: u32, device: u32) -> Option<u64> {+ let hex = |p: std::path::PathBuf| u32::from_str_radix(std::fs::read_to_string(p).ok()?.trim().trim_start_matches("0x"), 16).ok();+ std::fs::read_dir(drm).ok()?.flatten().find_map(|card| {+ let dev = card.path().join("device");+ if hex(dev.join("vendor"))? != vendor || hex(dev.join("device"))? != device {+ return None;+ }+ std::fs::read_to_string(dev.join("mem_info_vram_total")).ok()?.trim().parse().ok()+ })+}++#[cfg(not(any(target_os = "linux", windows)))]+fn detect_video_memory(_vendor: u32, _device: u32) -> Option<u64> {+ None+}+struct Resources {view_bgl: wgpu::BindGroupLayout,tile_bgl: wgpu::BindGroupLayout,@@ -1318,7 +1415,7 @@ struct Resources {compositor_cache_budget: Option<u64>,compositor_focus: Option<photocraft_geom::Rect>,/// GPU bytes one document's display texture set may take (see [`canvas_budget`]).- canvas_budget: u64,+ canvas_budget: std::sync::Arc<std::sync::atomic::AtomicU64>,encode_bgl: wgpu::BindGroupLayout,encode_pipeline: wgpu::RenderPipeline,lut_bgl: wgpu::BindGroupLayout,@@ -1584,7 +1681,7 @@ impl Resources {compositor_budget: None,compositor_cache_budget: None,compositor_focus: None,- canvas_budget: canvas_budget(physical_memory()),+ canvas_budget: std::sync::Arc::new(std::sync::atomic::AtomicU64::new(canvas_budget(physical_memory(), None))),encode_bgl,encode_pipeline,}@@ -1623,7 +1720,7 @@ impl DocTextures {/// The texture set for a `size` canvas, or `None` (nothing created) when it doesn't fit/// the canvas budget.fn new(device: &wgpu::Device, res: &Resources, size: [u32; 2], tile: u32, format: wgpu::TextureFormat) -> Option<Self> {- if canvas_texture_bytes(size, tile, texel_bytes(format)) > res.canvas_budget {+ if canvas_texture_bytes(size, tile, texel_bytes(format)) > res.canvas_budget.load(Ordering::Relaxed) {return None;}let mut tiles = Vec::new();@@ -2241,15 +2338,72 @@ mod tests {fn gpu_budget_follows_memory_usage_and_ram() {const G: u64 = 1 << 30;// What Memory Usage leaves after the document, within a quarter of RAM.- assert_eq!(memory_budget(8 * G, 3 * G, Some(48 * G)), 5 * G);- assert_eq!(memory_budget(8 * G, 3 * G, Some(16 * G)), 4 * G);- assert_eq!(memory_budget(64 * G, 3 * G, Some(16 * G)), 4 * G);+ assert_eq!(memory_budget(8 * G, 3 * G, Some(48 * G), None), 5 * G);+ assert_eq!(memory_budget(8 * G, 3 * G, Some(16 * G), None), 4 * G);+ assert_eq!(memory_budget(64 * G, 3 * G, Some(16 * G), None), 4 * G);// Never below the floor, even with nothing left; 16 GB assumed when RAM is unknown.- assert_eq!(memory_budget(8 * G, 10 * G, Some(48 * G)), MIN_GPU_BUDGET);- assert_eq!(memory_budget(64 * G, 0, None), 4 * G);+ assert_eq!(memory_budget(8 * G, 10 * G, Some(48 * G), None), MIN_GPU_BUDGET);+ assert_eq!(memory_budget(64 * G, 0, None, None), 4 * G);assert!(physical_memory().is_none_or(|b| b >= 256 << 20));}+ /// An 8 GB card in a 64 GB machine: both budgets used to plan for 16 GB, twice the card (#4536).+ #[test]+ fn gpu_budgets_fit_a_discrete_gpus_own_memory() {+ const G: u64 = 1 << 30;+ let (ram, vram) = (Some(64 * G), Some(8 * G));+ assert_eq!(memory_budget(64 * G, 3 * G, ram, vram), 3 * G);+ assert_eq!(canvas_budget(ram, vram), 3 * G);+ assert!(memory_budget(64 * G, 0, ram, vram) + canvas_budget(ram, vram) <= 6 * G, "a quarter of the card stays free");+ // A card larger than a quarter of RAM changes nothing; a tiny one keeps the floor.+ assert_eq!(memory_budget(8 * G, 3 * G, Some(48 * G), Some(24 * G)), 5 * G);+ assert_eq!(canvas_budget(Some(16 * G), Some(24 * G)), 4 * G);+ assert_eq!(memory_budget(64 * G, 0, ram, Some(G)), MIN_GPU_BUDGET);+ assert_eq!(canvas_budget(ram, Some(G)), MIN_GPU_BUDGET);+ }++ /// DXGI reports each adapter's dedicated memory, found by the vendor and device ids wgpu gives.+ #[cfg(windows)]+ #[test]+ fn windows_reports_the_adapters_own_memory() {+ use winsafe::prelude::*;+ let descs: Vec<_> = winsafe::CreateDXGIFactory1().unwrap().EnumAdapters1().map(|a| a.unwrap().GetDesc1().unwrap()).collect();+ assert!(!descs.is_empty(), "DXGI lists at least its software adapter");+ for d in descs.iter().filter(|d| d.DedicatedVideoMemory > 0) {+ // The first adapter with these ids is the one matched.+ let first = descs.iter().find(|e| e.VendorId == d.VendorId && e.DeviceId == d.DeviceId).unwrap();+ assert_eq!(detect_video_memory(d.VendorId, d.DeviceId), Some(first.DedicatedVideoMemory as u64));+ }+ assert_eq!(detect_video_memory(0xdead, 0xbeef), None, "no such adapter");+ }++ /// The Linux reader finds the card by its PCI ids in a `/sys/class/drm`-shaped folder and reads+ /// amdgpu's VRAM size; a card without one, unreadable ids or a malformed size give nothing.+ #[test]+ fn drm_video_memory_reads_the_matching_cards_vram() {+ let drm = std::env::temp_dir().join(format!("pc-drm-{}", std::process::id()));+ let card = |name: &str, vendor: &str, device: &str, vram: Option<&str>| {+ let dev = drm.join(name).join("device");+ std::fs::create_dir_all(&dev).unwrap();+ std::fs::write(dev.join("vendor"), vendor).unwrap();+ std::fs::write(dev.join("device"), device).unwrap();+ if let Some(v) = vram {+ std::fs::write(dev.join("mem_info_vram_total"), v).unwrap();+ }+ };+ // An Intel iGPU (no VRAM file), an AMD card, and an entry with garbage ids.+ card("card0", "0x8086\n", "0x9a49\n", None);+ card("card1", "0x1002\n", "0x73bf\n", Some("17163091968\n"));+ card("card2", "not hex\n", "0x73bf\n", Some("1\n"));+ card("card3", "0x10de\n", "0x2684\n", Some("lots\n"));+ assert_eq!(drm_video_memory(&drm, 0x1002, 0x73bf), Some(17_163_091_968));+ assert_eq!(drm_video_memory(&drm, 0x8086, 0x9a49), None, "no amdgpu VRAM file");+ assert_eq!(drm_video_memory(&drm, 0x10de, 0x2684), None, "a malformed size");+ assert_eq!(drm_video_memory(&drm, 0xdead, 0xbeef), None, "no such card");+ assert_eq!(drm_video_memory(&drm.join("missing"), 0x1002, 0x73bf), None, "no drm folder");+ std::fs::remove_dir_all(&drm).unwrap();+ }+/// Windows used to report no RAM at all, so the budget assumed 16 GB on every machine.#[cfg(windows)]#[test]@@ -2280,12 +2434,12 @@ mod tests {assert_eq!(canvas_texture_bytes([0, 500], 64, 4), 0);// A canvas declared in a few bytes of a file is priced without building (or looping over) it.let huge = canvas_texture_bytes([300_000, 300_000], 8192, 4);- assert!(huge > 300_000u64 * 300_000 * 4 && huge > canvas_budget(Some(1 << 40)));+ assert!(huge > 300_000u64 * 300_000 * 4 && huge > canvas_budget(Some(1 << 40), None));assert_eq!(canvas_texture_bytes([u32::MAX, u32::MAX], 64, 8), u64::MAX, "saturates");// A quarter of RAM (16 GB assumed), never below the compositor's floor.- assert_eq!(canvas_budget(None), 4 << 30);- assert_eq!(canvas_budget(Some(1 << 30)), MIN_GPU_BUDGET);- assert!(canvas_texture_bytes([12_000, 8_000], DEFAULT_TILE, 8) < canvas_budget(None), "a 96 MP 16-bit canvas stays on the GPU");+ assert_eq!(canvas_budget(None, None), 4 << 30);+ assert_eq!(canvas_budget(Some(1 << 30), None), MIN_GPU_BUDGET);+ assert!(canvas_texture_bytes([12_000, 8_000], DEFAULT_TILE, 8) < canvas_budget(None, None), "a 96 MP 16-bit canvas stays on the GPU");}#[test]diff --git a/crates/ui-egui/tests/canvas_16f.rs b/crates/ui-egui/tests/canvas_16f.rsindex 068de36c4..279a6dac6 100644--- a/crates/ui-egui/tests/canvas_16f.rs+++ b/crates/ui-egui/tests/canvas_16f.rs@@ -259,3 +259,24 @@ fn thirty_two_bit_preview_exposes_values_above_one() {eprintln!("shown {got}, want {want:.1} (clipped would be {clipped:.1})");assert!((got - want).abs() <= 3.0, "shown {got}, want {want:.1} (clipped {clipped:.1})");}++/// A 16-bit canvas whose float textures don't fit the canvas budget (a small graphics card,+/// #4536) stays on the GPU as `Rgba8Unorm` instead of falling back to the CPU canvas.+#[test]+fn sixteen_bit_canvas_too_big_for_floats_stays_on_the_gpu_in_8_bit() {+ let _gpu = gpu_lock();+ let Some((g, _rs)) = canvas() else { return };+ let doc = gradient(SampleType::U16, 512, 256, 0.0, 1.0);+ let size = [512, 256];+ let floats = gpu_canvas::canvas_texture_bytes(size, gpu_canvas::DEFAULT_TILE, 8);+ assert_eq!(g.format_for(SampleType::U16, size), TextureFormat::Rgba16Float, "fits by default");+ // Room for the 8-bit set, not the float one.+ g.set_canvas_budget(floats - 1);+ assert_eq!(g.format_for(SampleType::U16, size), TextureFormat::Rgba8Unorm);+ assert!(g.fits_canvas(&doc));+ assert_ne!(g.refresh(doc.id.0, &doc, None, None).kind, gpu_canvas::REFRESH_OVER_BUDGET);+ assert_eq!(g.texture_info(doc.id.0).map(|(f, _)| f), Some(TextureFormat::Rgba8Unorm));+ g.set_canvas_budget(floats);+ assert_eq!(g.format_for(SampleType::U16, size), TextureFormat::Rgba16Float);+ g.release();+}