Skip to content

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

GPU canvas: re-upload a layer or mask texture when its default pixel changes (#2422) (#2446)

MarekSpodBiedry8c2e16fb4a06
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs
index 5551924f0..4a6480c95 100644
--- a/crates/gpu/src/lib.rs
+++ b/crates/gpu/src/lib.rs
@@ -170,6 +170,9 @@ struct Resident {
format: PixelFormat,
tiles: HashMap<TileCoord, Arc<Tile>>,
default_nonzero: bool,
+ /// Bit pattern of the surface's default pixel when the texture was filled. Tiles the surface
+ /// doesn't store hold it, so a changed default (Invert, Fill) makes the texture stale.
+ default_bits: Vec<u32>,
doc: DocId,
last_used: u64,
/// Cell visit that last used it (least recently used pages are evicted first).
@@ -1243,7 +1246,12 @@ impl Compositor {
let format = surface.format();
let kind = TexKind::for_surface(key.1, format);
let cmyk = if format.mode == photocraft_color::ColorMode::Cmyk { self.cmyk } else { 0 };
- let stale = self.residents.get(&key).is_none_or(|r| r.region != region || r.kind != kind || r.format != format || r.cmyk != cmyk);
+ let default_pixel = surface.default_pixel();
+ let default_bits: Vec<u32> = default_pixel.iter().map(|v| v.to_bits()).collect();
+ let stale = self
+ .residents
+ .get(&key)
+ .is_none_or(|r| r.region != region || r.kind != kind || r.format != format || r.cmyk != cmyk || r.default_bits != default_bits);
if stale {
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("pc_compose_layer"),
@@ -1256,8 +1264,9 @@ impl Compositor {
view_formats: &[],
});
let view = texture.create_view(&Default::default());
- let default_nonzero = surface.default_pixel().iter().any(|v| *v != 0.0);
- let mut r = Resident { texture, view, region, kind, format, tiles: HashMap::new(), default_nonzero, doc, last_used: 0, stamp: 0, cmyk };
+ let default_nonzero = default_pixel.iter().any(|v| *v != 0.0);
+ let mut r =
+ Resident { texture, view, region, kind, format, tiles: HashMap::new(), default_nonzero, default_bits, doc, last_used: 0, stamp: 0, cmyk };
// A new texture starts zeroed: write the tiles that are present, and the default pixel
// where they are missing if it isn't zero. Coordinates sharing a tile (a solid fill,
// the default) take one upload and GPU copies (#1774).
diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs
index 342e901b7..85d3b5e21 100644
--- a/crates/gpu/tests/parity.rs
+++ b/crates/gpu/tests/parity.rs
@@ -608,6 +608,27 @@ fn incremental_updates_follow_the_document() {
check(&mut g, &d, "opacity");
}
+/// #2422: a mask whose default pixel changes without touching its tiles (Invert on a sparse mask)
+/// must re-upload the absent tiles between its stored ones, not keep the old default on the GPU.
+#[test]
+fn changed_mask_default_updates_absent_tiles() {
+ let Some(mut g) = gpu() else { return };
+ let mut d = base_doc(600, 300);
+ d.layers.push(noise_layer("top", PixelFormat::RGBA8, Rect::new(0, 0, 600, 300), 61, 0.5));
+ // Tiles in opposite corners: the resident texture spans the absent tiles between them.
+ let corners = [Rect::new(0, 0, 70, 70), Rect::new(530, 230, 600, 300)];
+ let data = vec![0.5f32; 70 * 70];
+ for default in [0.0, 1.0] {
+ let mut m = LayerMask::reveal_all();
+ m.surface = photocraft_raster::Surface::with_default(PixelFormat::GRAY8, &[default]);
+ for r in corners {
+ m.surface.write_region(r, &data);
+ }
+ d.layers[1].mask = Some(m);
+ check(&mut g, &d, &format!("mask default {default}"));
+ }
+}
+
/// #1774: tiles shared across coordinates (a solid fill, a cleared area, the default pixel of a
/// mask) upload once and are copied on the GPU, at 8 and 16 bits, in RGB and grayscale, on a new
/// layer page and on an incremental update.