A drawing app built for agent/human collaboration
Rust
Tiles skip clip stacks and groups with nothing in them
Indicode BV8059451ef17f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex dc34ff7..ac217f2 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.+- Drawings with many clipped layers and groups redraw several times faster: each tile skips the stacks that have nothing in it.- Switching layers no longer redraws the whole canvas when nothing on it changes.- 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.diff --git a/Cargo.lock b/Cargo.lockindex 784509b..ea19ec9 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1744,7 +1744,7 @@ dependencies = [[[package]]name = "klad-app"-version = "0.1.3"+version = "0.1.4"dependencies = ["anyhow","block2 0.6.2",@@ -1773,7 +1773,7 @@ dependencies = [[[package]]name = "klad-brush"-version = "0.1.3"+version = "0.1.4"dependencies = ["anyhow","bytemuck",@@ -1789,7 +1789,7 @@ dependencies = [[[package]]name = "klad-cli"-version = "0.1.3"+version = "0.1.4"dependencies = ["anyhow","clap",@@ -1815,7 +1815,7 @@ dependencies = [[[package]]name = "klad-core"-version = "0.1.3"+version = "0.1.4"dependencies = ["anyhow","half",@@ -1827,7 +1827,7 @@ dependencies = [[[package]]name = "klad-render"-version = "0.1.3"+version = "0.1.4"dependencies = ["anyhow","bytemuck",diff --git a/Cargo.toml b/Cargo.tomlindex 557ba95..0b8e52c 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -3,7 +3,7 @@ resolver = "3"members = ["crates/*"][workspace.package]-version = "0.1.3"+version = "0.1.4"edition = "2024"license-file = "LICENSE"repository = "https://github.com/klad-app/klad"diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rsindex c299d7c..807a983 100644--- a/crates/render/src/lib.rs+++ b/crates/render/src/lib.rs@@ -304,10 +304,59 @@ fn draw_onions<'a>(canvas: &Canvas, layers: impl Iterator<Item = &'a Layer>, ctx.try_for_each(|r| draw_raster(canvas, r, ctx))}+fn occupied_in(raster: &Raster, window: IRect) -> bool {+ tiles_in(raster.cols, raster.rows, window)+ .into_iter()+ .any(|i| raster.at(i).is_some())+}++fn ghosted(layer: &Layer, ctx: Ctx, window: IRect) -> bool {+ ctx.onions+ .and_then(|o| o.get(&layer.id))+ .is_some_and(|r| occupied_in(r, window))+}++fn may_draw(layer: &Layer, ctx: Ctx, window: IRect) -> bool {+ if !ctx.shows(layer) {+ return false;+ }+ if layer.keyed(ctx.frame).1.is_some() || layer.reads_under() {+ return true;+ }+ let reach = effects::margin(&ctx.effects(layer), 1.0);+ let window = window.with_outset((reach, reach));+ ghosted(layer, ctx, window)+ || match &layer.content {+ LayerContent::Group { children } => children.iter().any(|c| may_draw(c, ctx, window)),+ LayerContent::Adjust { .. } => true,+ LayerContent::Paint { .. } | LayerContent::Vector { .. } => {+ layer.content_at(ctx.frame).is_some_and(|cel| {+ let swapped = ctx.swap.filter(|s| s.cel == cel.id).is_some_and(|s| {+ tiles_in(s.cols, s.rows, window)+ .into_iter()+ .any(|i| matches!(s.tiles.get(&i), Some(Some(_))))+ });+ swapped+ || ctx+ .rasters+ .get(&cel.id)+ .map_or(layer.kind() == Kind::Vector, |r| occupied_in(r, window))+ })+ }+ }+}+fn draw_clipped(canvas: &Canvas, base: &Layer, clipped: &[&Layer], ctx: Ctx) -> Result<()> {if !ctx.shows(base) {return Ok(());}+ if ctx.detail == 1.0+ && let Some(area) = ctx.area+ && !may_draw(base, ctx, area)+ && clipped.iter().all(|l| !ghosted(l, ctx, area))+ {+ return Ok(());+ }if base.reads_under() {return match ctx.flat {true => adjust_under(canvas, base, ctx),diff --git a/crates/render/src/look_tests.rs b/crates/render/src/look_tests.rsindex fc3c64a..15cd65b 100644--- a/crates/render/src/look_tests.rs+++ b/crates/render/src/look_tests.rs@@ -171,6 +171,52 @@ fn adjustments_change_what_is_under_them_in_their_group_or_clip() {assert!(worst(&frame, &tiled(&doc)) <= 1);}+#[test]+fn tiles_skip_empty_clip_stacks_and_groups_and_still_match_the_whole() {+ let mut doc = Document::new(900, 700);+ doc.canvas.background = Some("#f2ede1".into());+ let clipped = |layer: Layer, blend: Blend| Layer {+ clip: true,+ blend,+ ..layer+ };+ let part = |id: &str, cx: f32, cy: f32| {+ vec![+ disc(id, cx, cy, 60.0, "#6fd6cf"),+ clipped(+ rect(&format!("{id}s"), cx, cy - 80.0, 90.0, 160.0, "#7090a0"),+ Blend::Multiply,+ ),+ clipped(+ rect(&format!("{id}r"), cx - 70.0, cy - 70.0, 40.0, 140.0, "#ffffff"),+ Blend::Normal,+ ),+ ]+ };+ let mut glowing = disc("glow", 228.0, 600.0, 14.0, "#ffd080");+ glowing.effects = effects(r#"[{"glow":{"radius":30,"strength":1.2}}]"#);+ let mut faded = Layer::group("faded", "faded", part("p3", 640.0, 560.0));+ faded.opacity = 0.7;+ let curve = Adjustment::Curves(Curves {+ points: vec![[0.0, 0.0], [0.5, 0.35], [1.0, 1.0]],+ ..Curves::default()+ });+ let mut scene = Layer::group(+ "scene",+ "scene",+ [+ vec![rect("sky", 100.0, 100.0, 380.0, 260.0, "#173b52")],+ part("p1", 180.0, 170.0),+ part("p2", 420.0, 300.0),+ vec![glowing, faded, Layer::adjust("dim", "dim", curve)],+ ]+ .concat(),+ );+ scene.effects = effects(r#"[{"noise":{"amount":0.08,"size":2,"seed":3,"mono":true}}]"#);+ doc.layers = vec![scene, rect("type", 560.0, 60.0, 280.0, 90.0, "#0e2b3c")];+ assert_eq!(worst(&full(&doc), &tiled(&doc)), 0, "tiles differ from the whole");+}+#[test]fn displacement_follows_another_layer() {let mut doc = Document::new(400, 200);diff --git a/docs/DESIGN.md b/docs/DESIGN.mdindex 442a8c7..4aa5ace 100644--- a/docs/DESIGN.md+++ b/docs/DESIGN.md@@ -1,4 +1,4 @@-_Last updated: 2026-11-07 13:30_+_Last updated: 2026-11-07 14:40_# klad: design@@ -235,7 +235,7 @@ Unknown keys are errors that list the valid ones, so a typo never passes silentlEvery layer has `blend`, `opacity`, `visible`, `locked`, optional `mask`, `clip` and (M5, format 8) `effects`. Effects are layer settings, not a layer kind: the planned `filter` kind became them.- **Blend modes.** `normal`, `multiply`, `screen`, `overlay`, `add`, and since M5 `color-dodge`, `soft-light`, `hard-light`, `lighten`, `darken`, `color`, `luminosity`, all as in the W3C compositing spec (Skia's modes; `look::blend_rgb` and `look::composite` hold the formulas the tests check them against).-- **Effects (M5).** `"effects": [{"glow": {...}}, {"outline": {...}}]`, applied in order to the layer's content (after its mask; for a clip base, to the base and its clipped layers together) before its opacity and blend: `blur` (radius, a Gaussian sigma), `glow` (radius, strength, threshold: 0 glows from every pixel, higher only from the bright ones; color: a tint; blend: how the glow mixes with what is under the layer, screen by default, normal lays it like paint under the layer), `outline` (width, color, inside), `shadow` (x, y, radius, color, laid under the layer), `noise` (amount, size, seed, mono: a grain fixed to the canvas), `displace` (amount, size, seed, by: smooth waves, or a layer whose red pushes across and green down), `relief` (strength, light, highlight, shade, softness: lights the paint height of the layer's drawings, below). Each effect knows its reach (`look::reach`), so a tile is composed from its content within the tile plus that margin and tiles meet without seams (noise and haze read only their own pixel and add no margin, so they cost no neighbouring tiles); the pixels are computed in Rust (`klad-render/effects.rs`) on f32 with absolute coordinates, so a tile equals the same pixels of the whole image byte for byte (tested). Blurs use a direct kernel, below a sigma of 2 at full size and above it on a grid of 2, 4, ... px blocks anchored to the canvas, then sampled back.+- **Effects (M5).** `"effects": [{"glow": {...}}, {"outline": {...}}]`, applied in order to the layer's content (after its mask; for a clip base, to the base and its clipped layers together) before its opacity and blend: `blur` (radius, a Gaussian sigma), `glow` (radius, strength, threshold: 0 glows from every pixel, higher only from the bright ones; color: a tint; blend: how the glow mixes with what is under the layer, screen by default, normal lays it like paint under the layer), `outline` (width, color, inside), `shadow` (x, y, radius, color, laid under the layer), `noise` (amount, size, seed, mono: a grain fixed to the canvas), `displace` (amount, size, seed, by: smooth waves, or a layer whose red pushes across and green down), `relief` (strength, light, highlight, shade, softness: lights the paint height of the layer's drawings, below). Each effect knows its reach (`look::reach`), so a tile is composed from its content within the tile plus that margin and tiles meet without seams (noise and haze read only their own pixel and add no margin, so they cost no neighbouring tiles); a tile skips a clip stack or group that puts no pixel in it (none of its rasters, live tiles or onion ghosts has a tile in the tile grown by its effects' margin; layers moved by keys, adjustment layers and vector layers drawn from their nodes always count), which leaves the tile the same since every blend leaves the pixels under a transparent layer as they are; the pixels are computed in Rust (`klad-render/effects.rs`) on f32 with absolute coordinates, so a tile equals the same pixels of the whole image byte for byte (tested). Blurs use a direct kernel, below a sigma of 2 at full size and above it on a grid of 2, 4, ... px blocks anchored to the canvas, then sampled back.- **Adjustment layers (M5).** An `adjust` layer changes what is under it in its list: the layers below it in its group, or, clipped, its base. A group holding an adjustment is composed apart, so the adjustment stays inside it. The colour math is on unpremultiplied sRGB values with alpha kept: `hsl` (hue in degrees, saturation and lightness -1..1), `levels` (black, white, gamma, low, high), `curves` (points, and red, green, blue applied before them; monotone cubic, no overshoot), `balance` (shadows, midtones, highlights: cyan to red, magenta to green, yellow to blue, keepLight), `gradient-map` (stops by brightness). The layer's opacity, blend and mask decide how much of the adjusted colour replaces the colour under it. `fill` (a solid or gradient paint) is drawn like a content layer instead, with the layer's blend.- **Rays (M5).** A vector node `{"type": "rays", "x", "y", "angle", "spread", "count", "length", "start", "falloff", "noise", "seed"}` draws soft beams from an origin (a gap `start` px wide), each a quad with a radial falloff and its own length and strength from the seed; the node's `fill` colours them and its `blur` softens them. Rays come from the agent (`klad add rays`); in the editor a selected rays node shows its settings in layer settings.- **Depth and atmosphere (M5).** A layer at the top of the stack (a plane, usually a group) may have a `depth` (0 is near; validation refuses it deeper in the tree). The document's `atmosphere` (`color`, an optional `top` colour blended down over the canvas height, `density`, `blur`, `desaturate`) turns each plane's depth into two effects after its own (`Document::effects_of`): a `haze` towards the fog of `1 - e^(-density x depth)` with `desaturate x depth` of its colour taken out, and a blur of `blur x depth` px. `haze` is an ordinary effect too. The depths are kept for M6's Parallax2D export; in the editor a depth field sits in each plane's row and layer settings, layer settings hold the atmosphere, and holding D shows the planes (each flattened alone, in the background) sliding by `offset x 0.08 / (1 + depth)` with the pointer.