Skip to content

armgit / klad

Public Imported from GitHub
0

A drawing app built for agent/human collaboration

Rust

Composed tiles reach the GPU even when egui discards a pass

Indicode BV4ae1e91b59ed
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8d05588..5319063 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,7 @@ The first builds, and klad's licences.
- Dragging in a large drawing: shapes and strokes move under the pointer from the first move, wherever they are (in groups with effects, clipped, masked, under any blend), drawn in the background tile by tile; the layers above and the rest of the dragged layer stay in place (dragging a shape on the topmost layer no longer hid the shapes above it), and letting go redraws only the tiles the move touched.
- Edits on vector layers redraw only the tiles their shapes covered before and after, not the whole layer.
- Noise and haze no longer make every edit redraw the tiles around it.
+- Fixed tiles that could stay blank or out of date when a panel or popup was laid out for the first time while the canvas was being redrawn.
## 0.1.0 (2026-11-04)
diff --git a/crates/app/src/editor.rs b/crates/app/src/editor.rs
index 93ec7f1..bfa2ccd 100644
--- a/crates/app/src/editor.rs
+++ b/crates/app/src/editor.rs
@@ -34,7 +34,7 @@ use crate::color::{self, Hsva};
use crate::commands::Cmd;
use crate::composite::{Preview, Scene, Tiles};
use crate::frames::FrameCache;
-use crate::gpu::CanvasPaint;
+use crate::gpu::{CanvasPaint, Outbox};
use crate::jobs::Jobs;
use crate::lift;
use crate::messages::{Messages, Tone};
@@ -647,6 +647,7 @@ pub(crate) struct Editor {
ctx: egui::Context,
sel_box: Option<(String, u64, [Vec2; 4])>,
preview: Option<PreviewState>,
+ outbox: Arc<Mutex<Outbox>>,
zoom_px: f32,
picked: Option<Picked>,
also: Vec<String>,
@@ -821,6 +822,7 @@ impl Editor {
ctx,
sel_box: None,
preview: None,
+ outbox: Arc::default(),
zoom_px: 1.0,
picked: None,
also: Vec::new(),
@@ -2961,14 +2963,13 @@ impl Editor {
}
uploads.extend(previewed);
let (frame_uploads, frame_drops) = self.frames.take();
+ lock(&self.outbox).send(uploads, frame_uploads, frame_drops);
ui.painter().add(egui_wgpu::Callback::new_paint_callback(
rect,
CanvasPaint {
show: show.filter(|_| self.preview.is_none()),
- frame_uploads: Mutex::new(frame_uploads),
- frame_drops,
+ outbox: self.outbox.clone(),
uniform: view_uniform,
- uploads: Mutex::new(uploads),
size: (w, h),
cols: self.scene.cols(),
pending: self.pending.take(),
diff --git a/crates/app/src/gpu.rs b/crates/app/src/gpu.rs
index 343f025..0d48452 100644
--- a/crates/app/src/gpu.rs
+++ b/crates/app/src/gpu.rs
@@ -4,7 +4,7 @@ use egui_wgpu::wgpu;
use half::f16;
use klad_brush::TILE;
use rayon::prelude::*;
-use std::collections::BTreeMap;
+use std::collections::{BTreeMap, BTreeSet};
use std::sync::{Arc, Mutex};
use std::time::Instant;
@@ -328,16 +328,35 @@ fn upload_tiles(queue: &wgpu::Queue, plane: &Plane, cols: usize, tiles: &[Upload
});
}
+#[derive(Default)]
+pub struct Outbox {
+ tiles: BTreeMap<usize, Vec<f16>>,
+ frames: BTreeMap<u64, FrameImage>,
+ drops: BTreeSet<u64>,
+}
+
+impl Outbox {
+ pub fn send(&mut self, tiles: Vec<(usize, Vec<f16>)>, frames: Vec<FrameImage>, drops: Vec<u64>) {
+ self.tiles.extend(tiles);
+ drops.into_iter().for_each(|key| {
+ self.frames.remove(&key);
+ self.drops.insert(key);
+ });
+ frames.into_iter().for_each(|image| {
+ self.drops.remove(&image.key);
+ self.frames.insert(image.key, image);
+ });
+ }
+}
+
pub struct CanvasPaint {
pub uniform: [f32; 12],
- pub uploads: Mutex<Vec<(usize, Vec<f16>)>>,
+ pub outbox: Arc<Mutex<Outbox>>,
pub size: (u32, u32),
pub cols: usize,
pub pending: Option<Instant>,
pub stats: Arc<Mutex<Stats>>,
pub show: Option<u64>,
- pub frame_uploads: Mutex<Vec<FrameImage>>,
- pub frame_drops: Vec<u64>,
}
impl egui_wgpu::CallbackTrait for CanvasPaint {
@@ -356,13 +375,14 @@ impl egui_wgpu::CallbackTrait for CanvasPaint {
gpu.resize(device, self.size);
}
queue.write_buffer(&gpu.canvas.uniform, 0, bytemuck::cast_slice(&self.uniform));
- let uploads = std::mem::take(&mut *lock(&self.uploads));
+ let Outbox { tiles, frames, drops } = std::mem::take(&mut *lock(&self.outbox));
+ let uploads: Vec<Upload> = tiles.into_iter().collect();
let start = Instant::now();
upload_tiles(queue, &gpu.canvas, self.cols, &uploads);
- self.frame_drops.iter().for_each(|key| {
+ drops.iter().for_each(|key| {
gpu.frames.remove(key);
});
- let images = std::mem::take(&mut *lock(&self.frame_uploads));
+ let images: Vec<FrameImage> = frames.into_values().collect();
let started = Instant::now();
images.iter().for_each(|image| gpu.put_frame(device, queue, image));
if let Some(plane) = self.show.and_then(|key| gpu.frames.get(&key)) {
@@ -401,6 +421,30 @@ impl egui_wgpu::CallbackTrait for CanvasPaint {
mod tests {
use super::*;
+ #[test]
+ fn the_outbox_keeps_what_a_discarded_pass_sent_once_per_tile_and_picture() {
+ let picture = |key| FrameImage {
+ key,
+ width: 1,
+ height: 1,
+ mips: vec![vec![0; 4]],
+ };
+ let tile = |v: f32| vec![f16::from_f32(v); 4];
+ let mut outbox = Outbox::default();
+ outbox.send(
+ vec![(3, tile(0.1)), (5, tile(0.2))],
+ vec![picture(7), picture(8)],
+ Vec::new(),
+ );
+ outbox.send(vec![(3, tile(0.9))], vec![picture(9)], vec![8]);
+ assert_eq!(outbox.tiles.keys().copied().collect::<Vec<_>>(), [3, 5]);
+ assert_eq!(outbox.tiles[&3][0].to_f32(), tile(0.9)[0].to_f32());
+ assert_eq!(outbox.frames.keys().copied().collect::<Vec<_>>(), [7, 9]);
+ assert_eq!(outbox.drops.iter().copied().collect::<Vec<_>>(), [8]);
+ outbox.send(Vec::new(), vec![picture(8)], Vec::new());
+ assert!(outbox.drops.is_empty() && outbox.frames.contains_key(&8));
+ }
+
#[test]
fn mip_chains_average_down_to_one_pixel() {
let tile: Vec<f16> = (0..TILE * TILE)
diff --git a/docs/DESIGN.md b/docs/DESIGN.md
index 0744235..ce57a90 100644
--- a/docs/DESIGN.md
+++ b/docs/DESIGN.md
@@ -445,6 +445,7 @@ Scripts are any language that prints edit ops as JSON lines into `klad apply`. G
- Layers (M2 batch 4): layer settings sets clip to the layer below and lock alpha, adds a mask (empty, or hiding all), turns it on and off, removes it, and "paint on the mask" makes the mask the active layer (it also has its own row under its layer). Painting on a mask hides the layer under the strokes; the eraser shows it again. While a mask is active, what it hides is tinted red ("show the mask in red", on by default), and the brush panel says whether the brush or the eraser is on. The brush and fill tools record lock alpha from the active layer.
- Live previews and clipping: the editor composes the active layer between cached images of everything below and above it only when that is exact (the active layer and the groups around it are not clipped, masked or a clip base); otherwise each dirty tile composes the whole stack. A drag (a shape with V, strokes with S or V, any grab: move, scale, turn, stretch, an alt-drag copy) previews the edit it will commit: at its first move the editor takes a snapshot of the document and the pixels (tiles are shared, so this is cheap), and while the pointer moves a background job applies the drag's ops in memory, redraws only the tiles they change (vector nodes through the tile patching below, strokes through the cel's replay) and composes those tiles through the whole stack, so clips, masks, blends, group effects and everything above the dragged thing stay exactly right. One job runs at a time and the newest pointer position wins; only tiles on screen are composed while dragging. The composed tiles go straight to the canvas plane, which is shown while a preview is up. On release the edit is written at once (it costs only its tiles), a last job composes the final position, and its tiles are taken as they are when the edit only moved vector nodes; tiles the preview touched that the edit did not are composed again. In a lean scene, while a mask is tinted, or before the canvas has caught up, a drag shows only its outline and lands on release.
- Vector edits redraw only their tiles: a changed vector cel whose node order is kept is patched where its changed nodes were and are (their bounds grown for strokes, miter joins, blur and generator instances; added, removed and hidden nodes count), drawn on one surface that holds every node touching those tiles so no node is cut at a tile edge and the tiles are bit-identical to a full rasterization; a new order, or a node without bounds, redraws the cel. A swapped vector raster (an edit, undo or redo from the remembered rasters) dirties only the tiles whose pixels differ.
+- Composed tiles and frame pictures reach the GPU through an outbox the paint callback empties, holding the newest version of each tile: a UI pass that egui discards (a grid or popup laid out for the first time) drops its paint callback, and the tiles it carried would otherwise never be uploaded.
- Direct select (A, M2): click a path, rect or ellipse (inside groups too) to show its points; clicking the selected shape again steps down to the next shape under the pointer, and layer settings list them all; click or shift click points, drag on empty canvas for a box; drag points or handles; alt click the outline adds a point, alt click a point toggles corner and smooth, alt drag a handle breaks it; delete removes points; layer settings have corner, smooth, delete points, simplify and "make it a path". While dragging, the edited outline is drawn live; on release the edit is one `path` op whose layer is rendered in the background, then swapped in.
- Select and move (V, M4 test): a press picks what you see under the pen and drags it at once: the mark S would pick and the topmost vector shape there are compared by their layers' places in the stack and the higher one wins (clicking a shape again steps to the one under it); its layer becomes the active layer. With "this layer" only the active layer's marks or shapes are picked. A stroke picked this way is the same selection as S makes (its bar, layer settings, nudges, delete, copy), so V is the one tool for "move this". A press on empty canvas lassoes strokes on the active layer. Alt drag drags a copy: the copy is made (duplicate in place, one step) when the drag first moves, and the drag moves the copy (two history steps). A, the point editor, says so when a click lands on a brush stroke: strokes are samples, not paths.
- Pen (P, M4 test): draws vector paths on a vector layer: a click places a corner, a drag pulls out symmetric handles, a click on the first point closes the path, Return finishes it open, Esc drops it, and another tool finishes it. The path is one `add` node op into the layer at the current frame, in the current colour: closed paths are filled (unless "fill" is off) and outlined (unless "outline" is off), open ones always outlined, at the width in the options bar. On other layers it refuses and says why; the brush says so on a vector layer.