ProCraft for Android: An Android native painting app written in Rust
Rust
perf(gpu): take GPU memory in small blocks, so a first read back no longer keeps 64 MB
Josh45bb3ab1ae9a
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rsindex df21898..c212bdf 100644--- a/crates/gpu/src/lib.rs+++ b/crates/gpu/src/lib.rs@@ -91,7 +91,10 @@ impl Gpu {required_features: adapter.features() & wgpu::Features::DUAL_SOURCE_BLENDING,// the adapter's own limits, so large canvases fit where the GPU allows themrequired_limits: adapter.limits(),- memory_hints: wgpu::MemoryHints::Performance,+ // memory taken in small blocks (a few MB up to 64): a block of each kind, once made,+ // is kept for good, and the larger ones held what was never used (a first read back+ // of a few bytes kept 64 MB the CPU can see)+ memory_hints: wgpu::MemoryHints::MemoryUsage,..Default::default()})).map_err(|e| GpuError::Device(e.to_string()))?;diff --git a/crates/gpu/tests/layers.rs b/crates/gpu/tests/layers.rsindex 72a049d..d33e5ab 100644--- a/crates/gpu/tests/layers.rs+++ b/crates/gpu/tests/layers.rs@@ -977,3 +977,33 @@ fn a_canvas_makes_its_scratch_and_second_composite_only_once_wanted() {assert!(canvas.texture_bytes() >= per * (4 + 4 + 4 + 4 + 8) + images + small + undo);assert_eq!(gpu.errors(), 0);}++/// What the GPU's allocator holds aside, used or not, bytes, once the work sent so far is done;+/// none where the backend gives no report.+fn reserved(gpu: &Gpu) -> Option<u64> {+ gpu.flush();+ gpu.device.generate_allocator_report().map(|r| r.total_reserved_bytes)+}++#[test]+fn the_gpu_memory_set_aside_stays_near_what_is_used() {+ let Some(gpu) = Gpu::headless().ok() else { return eprintln!("skipped: no GPU") };+ let side = 2048u32;+ let mut doc = Document::new(side, side);+ let mut canvas = Canvas::new(&gpu, &doc).unwrap();+ canvas.render(&gpu, &mut doc, None);+ let (Some(used), Some(held)) = (allocated(&gpu), reserved(&gpu)) else { return };+ // a canvas's textures (some 76 MB at this size) in blocks of 64 MB at most, not 128 to 256+ assert!(held <= used + (96 << 20), "{} MB held for {} MB", held >> 20, used >> 20);+ // the first read back of a few bytes sets aside a few MB the CPU can see, not 64+ let before = reserved(&gpu).unwrap_or(0);+ let mut small = Document::new(2, 2);+ let mut tiny = Canvas::new(&gpu, &small).unwrap();+ tiny.render(&gpu, &mut small, None);+ let between = reserved(&gpu).unwrap_or(0);+ assert_eq!(tiny.read_composite(&gpu).unwrap().len(), 16);+ let after = reserved(&gpu).unwrap_or(0);+ assert!(after.saturating_sub(between) <= 8 << 20, "{} MB for a read back", (after - between) >> 20);+ assert!(between >= before);+ assert_eq!(gpu.errors(), 0);+}