RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
scene graph plan, stage 4d: analytic window shadows on the owned path
oxydizer6d2ebd6416dc
diff --git a/AGENTS.md b/AGENTS.mdindex efce0e6..0581fcd 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -33,7 +33,10 @@ the one for the directory you work in: `src/files`, `src/editor`,the chrome buffers are plain bands (`ChromeState.radius` 0), the footer is`frame_border`, border rects are off, and `shadow_tree`, the resize-grab treeand every xdg popup carry `setTreeUnclipped` (a new node that hangs outside the- window needs it too). CSD windows have no shape and keep their rect outline.+ window needs it too). The shadow is analytic (`setTreeShadow` on `shadow_tree`,+ drawn by the pass with the CPU shadow's smoothstep); its leaf, `shadow_fallback`+ without a buffer, carries footprint, opacity and input only, and the projection+ damages that footprint itself, since wlroots damages nothing for a bufferless view. CSD windows have no shape and keep their rect outline.A changed clip damages its rim itself (`projection.c`); a point the shape cutsaway is declined in `accepts()`, so the pick falls to what is below. Windowcapture draws the client alone and is handed the shape explicitly.diff --git a/TESTING.md b/TESTING.mdindex c2f6ebb..3e7d0fd 100644--- a/TESTING.md+++ b/TESTING.md@@ -16,7 +16,7 @@ debugging on the host, see AGENTS.md, "Rendering".| `zig build test` | In-process unit tests: rasterizers vs. full rasters, UI, config, IPC parser/serializer fixtures, glass damage, projection damage and its typed graph (paint order, unrounded edges, list lifetime), lifecycle/allocation failures, Files/desktop logic. No compositor or bus. || `zig build test-glass` | Real GLES2 pixels on a headless backend (needs a DRM render node). || `zig build test-pass-spike` | The owned-pass spike of `docs/plans/scene-graph-stage1.md`: rounded clip, border, shadow, external-OES texture, backdrop blur, buffer-age damage and cost on headless GLES2 (needs a render node; `-Doptimize=ReleaseSafe` for cost). |-| `zig build test-scene-pass` | Stage 3's owned GLES2 drawing backend against wlroots: transforms, crops, filters, premultiplied alpha, damage, external-OES (an RGB EGLImage and a linear NV12 dmabuf; needs gbm), GL/EGL state and acquire/release timelines. Needs a compatible render node (`WLR_RENDER_DRM_DEVICE` on multi-GPU machines); also run with `-Doptimize=ReleaseSafe`. The unavailable-acquire test intentionally logs `drmSyncobjTransfer failed`. Stage 4b's rounded-shape gates are in the same fixture: clip against a supersampled reference, piece joins, border/body, premultiplied edges, the opaque cover, shadow, and partial repaint across a clip edge; the 128 parity cases also run through the shaped programs. |+| `zig build test-scene-pass` | Stage 3's owned GLES2 drawing backend against wlroots: transforms, crops, filters, premultiplied alpha, damage, external-OES (an RGB EGLImage and a linear NV12 dmabuf; needs gbm), GL/EGL state and acquire/release timelines. Needs a compatible render node (`WLR_RENDER_DRM_DEVICE` on multi-GPU machines); also run with `-Doptimize=ReleaseSafe`. The unavailable-acquire test intentionally logs `drmSyncobjTransfer failed`. Stage 4b's rounded-shape gates are in the same fixture: clip against a supersampled reference, piece joins, border/body, premultiplied edges, the opaque cover, shadow, and partial repaint across a clip edge; the 128 parity cases also run through the shaped programs. Stage 4d's shadow gate compares every pixel with the CPU chrome's shadow formula. || `zig build test-lock` | Lock/greeter painter, password editing, PAM transitions (PAM is replaced at link time in test binaries only), greeter↔rediwm-dm conversation. || `zig build test-dm` | rediwm-dm relay state machine and framing. || `zig build test-dbus`, `test-polkit`, `test-polkit-ui`, `test-notifications`, `test-settings-portal`, `test-power`, `test-power-profiles`, `test-tray`, `test-app-activation`, `test-rediwm-session`, `test-layout-autosave`, `test-files`, `test-images` | Subsystem suites; the D-Bus ones re-exec under `dbus-run-session`. |@@ -112,8 +112,9 @@ origin), `zoom_osd.py` (shared zoom OSD,integer/fractional scales, plus workspace focus outlines on server/clientdecorations, mode transitions, fullscreen and default/explicit gaps; under`REDIWM_TEST_SCENE_RENDERER=owned` it also runs `frame_parity` (titlebar, sides,-client and shadow bit-identical to the legacy frame at scales 1 to 2, the-rightmost column excepted on fractional scales) and checks transparent client+client bit-identical to the legacy frame at scales 1 to 2, the rightmost column+excepted on fractional scales; the analytic shadow, corners included, within 2+levels of the CPU one, apart from the legacy 9-slice's gap under the bottom arcs) and checks transparent clientcorners and that a click on a clipped corner reaches the window below), `switcher.py`,`switcher_live.py`, `workspaces.py` (floating rules, Settings/IPC layout controls,versioned restart/named-save restoration, duplicate app occurrences, restored tab groups,diff --git a/docs/plans/scene-graph-stage4.md b/docs/plans/scene-graph-stage4.mdindex c390175..2292dd4 100644--- a/docs/plans/scene-graph-stage4.md+++ b/docs/plans/scene-graph-stage4.md@@ -1,6 +1,6 @@# Scene graph plan, stage 4: clipping and primitives-Status: **4a to 4c done, 4d and 4e not started**, 2026-10-10. Stage 3 of+Status: **4a to 4d done, 4e not started**, 2026-10-10. Stage 3 of[scene-graph.md](scene-graph.md) (the owned output pass, radius zero) is doneapart from hardware qualification; this stage is why the plan exists: the userrequires clipped client corners, and wlroots 0.20 cannot mask a client. The five@@ -395,6 +395,46 @@ an agreed tolerance and no visible change in `appearance_chrome`. If the cornersdo not pass, keep the CPU patches and drop this sub-stage; it is not on thecritical path.+*Done, gate met.* The shadow is a property of the window's shadow tree+(`rediwm_projection_set_tree_shadow`, `rediwm_shadow_spec` in `scene_graph.h`),+resolved like the clip into `item.shadow` on a `REDIWM_ITEM_SHADOW` leaf. The leaf+is the old fallback node without a buffer: it carries the footprint, the window's+opacity and input (`chromeAcceptsInput`, as before), never pixels. Because wlroots+damages nothing for a bufferless view, `present` damages the footprint it last+drew and the new one when the box, the shadow, the opacity or visibility changes,+and when the leaf is hidden, unreached or destroyed (`test_shadow_output`).+`scene_output.c` draws it with `rediwm_scene_pass_add_shadow`, the offset taken to+device pixels as the difference of two snapped boxes so output transforms turn it.++The look did not come from the spike. The CPU shadow is not a Gaussian: it is full+strength inside the moved shape and falls off with a smoothstep over+`shadow_size` (`chrome.shadowCoverage`), so the shaped program's mode 2 now+evaluates exactly that on the same distance field instead of the spike's erf.+`rediwm_pass_shadow.sigma` became `softness`. Because both are functions of the+one distance field, the corners match as well as the edges, which removes the+plan's worry about the spike's corner approximation. Gates:++* `test-scene-pass`: every pixel of a radius 10, softness 24, offset 8 shadow at+ scales 1 and 1.5 is within **1 level** of the CPU formula, corners included.+* `window_corners.py` `frame_parity` (owned against legacy, same binary, scales 1,+ 1.25, 1.5, 2): the shadow above, left of and at the top corners of the window,+ and (at integer scales, where the 9 px taller legacy frame lands on whole pixels)+ below it with both bottom corners, is within **0 to 1 level** of the CPU+ patches. The one exception is a legacy defect the comparison found: the 9-slice+ side patches treat the window's edge as straight down to the offset corner+ patch, so a few pixels under each bottom arc showed bare wallpaper where the+ shadow is at full strength (6 pixels at scale 1, 48 at scale 2). The analytic+ shadow fills them; the test allows only those, and only under a bottom arc.+* `appearance_chrome.py` passes under owned; `window_corners.py` passes apart+ from the `workspace_focus` flake, which fails at the same rate on HEAD (1 of 3+ runs at 1.5 on each).++The CPU patch set is no longer built on the owned path, so its cache costs nothing+there; its code goes in 4e. Cost was not measured here (see **Measurement**): one+shaded quad per window replaces eight textured ones, and the collector already+culls it under the window's opaque client, so it shades only the rim and the+translucent titlebar.+**4e. Flip and delete.** Make `owned` the default (it requires the stage 3 hardwarechecklist to have passed on a real KMS session), then delete the code only thelegacy presenter needed: `probeClientEdge` and the edge-sample machinery indiff --git a/src/Toplevel.zig b/src/Toplevel.zigindex 2e6b187..c6d42cd 100644--- a/src/Toplevel.zig+++ b/src/Toplevel.zig@@ -3009,6 +3009,28 @@ fn syncShadow(toplevel: *Toplevel, width: i32, height: i32, radius: f32) !void {const offset_y: i32 = @intFromFloat(t.shadow_offset_y);const c_size: i32 = @intFromFloat(@ceil(radius + t.shadow_size));+ if (chrome.analytic_frames) {+ // The owned pass draws the shadow from the shape; the fallback node,+ // without a buffer, only carries its footprint, opacity and input.+ for (toplevel.shadow_corners) |node| node.node.setEnabled(false);+ for (toplevel.shadow_edges) |node| node.node.setEnabled(false);+ toplevel.shadow_fallback.node.setEnabled(true);+ toplevel.shadow_fallback.node.setPosition(-margin, -margin + offset_y);+ toplevel.shadow_fallback.setDestSize(width + 2 * margin, height + 2 * margin);+ const r: f64 = radius;+ if (!projection.setTreeShadow(&toplevel.shadow_tree.node, .{+ .width = width,+ .height = height,+ .radii = .{ r, r, r, r },+ .offset_x = 0,+ .offset_y = @floatFromInt(offset_y),+ .softness = t.shadow_size,+ .color = col.Straight.fromRgba(t.shadow).premultiply().rgba,+ })) log.warn("could not set the window shadow ({d}x{d})", .{ width, height });+ toplevel.last_shadow = memo;+ return;+ }+if (width < 2 * c_size or height < 2 * c_size) {for (toplevel.shadow_corners) |node| node.node.setEnabled(false);for (toplevel.shadow_edges) |node| node.node.setEnabled(false);diff --git a/src/projection.c b/src/projection.cindex 954d1f8..fbf7e82 100644--- a/src/projection.c+++ b/src/projection.c@@ -98,6 +98,14 @@ struct leaf {// geometry. Kept separately so an unchanged/held blur survives the next sync.struct rediwm_item effect_item;struct rediwm_clip effect_clip;+ // `item.shadow` points at `shadow` when the leaf is under a shadow tree. A+ // shadow's view has no buffer, so wlroots damages nothing for it: present()+ // damages the footprint it last drew (`shadow_drawn`) and the new one.+ struct rediwm_shadow shadow;+ struct rediwm_shadow shadow_drawn_as;+ struct rediwm_snap shadow_drawn;+ float shadow_drawn_opacity;+ bool has_shadow_drawn;struct wlr_buffer *last_buffer;// The client buffer whose view lock we told wlroots to ignore, if any.struct wlr_client_buffer *marked;@@ -116,12 +124,15 @@ struct leaf {// * unclipped: ends the inherited clip for the subtree. A popup's scene tree is a// child of its parent surface's, so it sits under the window's clipped tree in// the source graph while belonging to no window shape. The subtree's own clip,-// if it sets one, still applies.+// if it sets one, still applies;+// * shadow: every leaf under the tree is the analytic shadow of this shape+// (REDIWM_ITEM_SHADOW), resolved like the clip.struct tree_props {struct wlr_addon addon;- bool has_zoom, has_scale, has_clip, unclipped;+ bool has_zoom, has_scale, has_clip, unclipped, has_shadow;double zoom, scale;struct rediwm_clip_spec clip;+ struct rediwm_shadow_spec shadow;// The clip as the last resolve() saw it in layout space, to tell a changed// shape (to be damaged) from a moved window (whose views damage themselves).struct rediwm_clip applied;@@ -179,6 +190,24 @@ bool rediwm_projection_set_tree_clip(struct wlr_scene_node *node,props->has_clip = true;return true;}+bool rediwm_projection_set_tree_shadow(struct wlr_scene_node *node,+ const struct rediwm_shadow_spec *spec) {+ if (!spec) {+ struct tree_props *props = tree_props_find(node);+ if (props) props->has_shadow = false;+ return true;+ }+ if (spec->width <= 0 || spec->height <= 0 || !finite_at_least_zero(spec->softness) ||+ !isfinite(spec->offset_x) || !isfinite(spec->offset_y))+ return false;+ for (int i = 0; i < 4; i++)+ if (!finite_at_least_zero(spec->radii[i]) || !isfinite(spec->color[i])) return false;+ struct tree_props *props = tree_props_get(node);+ if (!props) return false;+ props->shadow = *spec;+ props->has_shadow = true;+ return true;+}bool rediwm_projection_set_tree_unclipped(struct wlr_scene_node *node, bool unclipped) {struct tree_props *props = unclipped ? tree_props_get(node) : tree_props_find(node);if (unclipped && !props) return false;@@ -328,7 +357,9 @@ static void mark_view_buffer(struct leaf *l, struct wlr_buffer *backing) {client->private.n_ignore_locks++;l->marked = client;}+static void damage_shadow(struct leaf *l, bool show, struct rediwm_snap at);static void destroy_leaf(struct leaf *l) {+ damage_shadow(l, false, (struct rediwm_snap){0});unmark_view_buffer(l);route_surface(l, l->surface ? l->surface->buffer : NULL);wl_list_remove(&l->commit.link);@@ -564,12 +595,14 @@ static void damage_clip_change(struct rediwm_projection *p, const struct rediwm_static void resolve_node(struct rediwm_projection *p, struct group *g,struct wlr_scene_node *node, double x, double y,bool enabled, struct rediwm_xform t,- const struct rediwm_clip *clip) {+ const struct rediwm_clip *clip,+ const struct rediwm_shadow *shadow) {x += node->x;y += node->y;enabled = enabled && node->enabled;if (node->type == WLR_SCENE_NODE_TREE) {struct rediwm_clip here;+ struct rediwm_shadow cast;struct tree_props *props = tree_props_find(node);if (props) {// Window zoom composes with the camera, around the frame origin.@@ -588,10 +621,14 @@ static void resolve_node(struct rediwm_projection *p, struct group *g,if (enabled) damage_clip_change(p, &props->applied, NULL);props->has_applied = false;}+ if (props->has_shadow) {+ cast = rediwm_shadow_resolve(t, x, y, &props->shadow);+ shadow = &cast;+ }}struct wlr_scene_node *child;wl_list_for_each(child, &wlr_scene_tree_from_node(node)->children, link)- resolve_node(p, g, child, x, y, enabled, t, clip);+ resolve_node(p, g, child, x, y, enabled, t, clip, shadow);return;}struct leaf *l = get_leaf(p, node);@@ -646,9 +683,41 @@ static void resolve_node(struct rediwm_projection *p, struct group *g,content->color_encoding = b->color_encoding;content->color_range = b->color_range;}+ if (shadow && node->type == WLR_SCENE_NODE_BUFFER) {+ item->kind = REDIWM_ITEM_SHADOW;+ l->shadow = *shadow;+ item->shadow = &l->shadow;+ } else if (item->shadow) {+ item->shadow = NULL;+ }item->edges = rediwm_xform_rect(t, x, y, item->width, item->height);}+// What a shadow draws changed, or it appeared or went: damage the footprint it+// last drew and the one it draws now, in layout space.+static void damage_shadow(struct leaf *l, bool show, struct rediwm_snap at) {+ const float opacity = l->item.content.opacity;+ if (show == l->has_shadow_drawn &&+ (!show || (at.x == l->shadow_drawn.x && at.y == l->shadow_drawn.y &&+ at.width == l->shadow_drawn.width && at.height == l->shadow_drawn.height &&+ opacity == l->shadow_drawn_opacity &&+ rediwm_shadow_equal(&l->shadow, &l->shadow_drawn_as))))+ return;+ pixman_region32_t region;+ pixman_region32_init(®ion);+ if (l->has_shadow_drawn)+ pixman_region32_union_rect(®ion, ®ion, l->shadow_drawn.x, l->shadow_drawn.y,+ l->shadow_drawn.width, l->shadow_drawn.height);+ if (show) pixman_region32_union_rect(®ion, ®ion, at.x, at.y, at.width, at.height);+ if (pixman_region32_not_empty(®ion))+ rediwm_scene_damage_layout(l->projection->scene, ®ion);+ pixman_region32_fini(®ion);+ l->has_shadow_drawn = show;+ l->shadow_drawn = at;+ l->shadow_drawn_as = l->shadow;+ l->shadow_drawn_opacity = opacity;+}+// present(): graph -> view tree. Reads items and group origins, never the// source's geometry; rounds, orders and applies, and tells wlroots about the// buffers and damage that changed.@@ -743,6 +812,10 @@ static void present_leaf(struct group *g, struct leaf *l,}pixman_region32_fini(&mirrored);}+ if (item->kind == REDIWM_ITEM_SHADOW || l->has_shadow_drawn)+ damage_shadow(l, item->kind == REDIWM_ITEM_SHADOW && next.enabled &&+ item->content.opacity > 0 && item->shadow->color[3] > 0,+ snap);l->dirty = false;l->full_damage = false;l->owned_damage = false;@@ -866,7 +939,7 @@ static void resolve(struct rediwm_projection *p, double z, double cx, double cy,rediwm_xform_point(t, node->x, node->y, &g->x, &g->y);g->enabled = node->enabled;wl_list_insert(p->paint.prev, &g->paint);- resolve_node(p, g, node, 0, 0, true, t, NULL);+ resolve_node(p, g, node, 0, 0, true, t, NULL, NULL);}}// Graph -> view tree, then hide what the graph no longer reaches.@@ -889,6 +962,7 @@ static void present(struct rediwm_projection *p) {}struct leaf *l;wl_list_for_each(l, &p->leaves, link) if (!l->seen) {+ damage_shadow(l, false, (struct rediwm_snap){0});wlr_scene_node_set_enabled(l->view, false);// The cached state no longer describes the view; reapply on return.l->has_applied = false;diff --git a/src/projection.h b/src/projection.hindex 1e65858..5efa1c0 100644--- a/src/projection.h+++ b/src/projection.h@@ -40,6 +40,12 @@ double rediwm_projection_node_zoom(struct wlr_scene_node *);// units and anchored at its origin; NULL removes it. Returns false for a size// that is not positive or a radius or border that is negative or not finite.bool rediwm_projection_set_tree_clip(struct wlr_scene_node *, const struct rediwm_clip_spec *);+// Every leaf under the tree draws the analytic shadow of this shape (see+// rediwm_shadow_spec in scene_graph.h); its node supplies only the footprint,+// opacity and input. NULL removes it. False for a size that is not positive, or a+// radius or softness that is negative or not finite.+struct rediwm_shadow_spec;+bool rediwm_projection_set_tree_shadow(struct wlr_scene_node *, const struct rediwm_shadow_spec *);// Ends the clip inherited from an ancestor for this subtree (an xdg popup lives// under its parent's tree but is no part of the window's shape).bool rediwm_projection_set_tree_unclipped(struct wlr_scene_node *, bool);diff --git a/src/projection.zig b/src/projection.zigindex 9a40c15..e16a8f9 100644--- a/src/projection.zig+++ b/src/projection.zig@@ -45,6 +45,26 @@ pub fn setTreeClip(node: *wlr.SceneNode, spec: ?ClipSpec) bool {if (spec) |value| return rediwm_projection_set_tree_clip(node, &value);return rediwm_projection_set_tree_clip(node, null);}+/// The analytic drop shadow of a window's shape (see scene_graph.h): every leaf+/// under the tree draws it in the owned pass. `width`, `height` and `radii` are+/// the shape it is kept out of, in source units at the tree's origin; `color` is+/// premultiplied.+pub const ShadowSpec = extern struct {+ width: c_int,+ height: c_int,+ radii: [4]f64,+ offset_x: f64,+ offset_y: f64,+ softness: f64,+ color: [4]f32,+};+extern fn rediwm_projection_set_tree_shadow(node: *wlr.SceneNode, spec: ?*const ShadowSpec) bool;+/// A null spec removes the shadow. False for a size that is not positive or a+/// radius or softness that is negative or not finite.+pub fn setTreeShadow(node: *wlr.SceneNode, spec: ?ShadowSpec) bool {+ if (spec) |value| return rediwm_projection_set_tree_shadow(node, &value);+ return rediwm_projection_set_tree_shadow(node, null);+}/// Ends the clip inherited from an ancestor for this subtree: an xdg popup or a/// window's shadow hangs under the window's tree but is no part of its shape.extern fn rediwm_projection_set_tree_unclipped(node: *wlr.SceneNode, unclipped: bool) bool;diff --git a/src/projection_test.c b/src/projection_test.cindex 9ce04a9..e73ceac 100644--- a/src/projection_test.c+++ b/src/projection_test.c@@ -639,6 +639,66 @@ static void test_clip_output(struct wlr_scene *scene, struct wlr_scene_output *owlr_buffer_drop(under_buffer);wlr_buffer_drop(client_buffer);}+// Stage 4d: a shadow's view has no buffer, so wlroots damages nothing for it and the+// projection damages the footprint itself: when it appears, moves, changes, is+// hidden with its window, or goes away, and never when nothing changed.+static void test_shadow_output(struct wlr_scene *scene, struct wlr_scene_output *out) {+ struct wlr_scene_tree *source = wlr_scene_tree_create(&scene->tree);+ struct wlr_scene_tree *view = wlr_scene_tree_create(&scene->tree);+ struct rediwm_projection *p = rediwm_projection_create(source, view, scene);+ assert(p);+ struct wlr_scene_tree *frame = wlr_scene_tree_create(source);+ struct wlr_scene_tree *shadow_tree = wlr_scene_tree_create(frame);+ struct wlr_scene_buffer *footprint = wlr_scene_buffer_create(shadow_tree, NULL);+ assert(frame && shadow_tree && footprint);+ wlr_scene_node_set_position(&frame->node, 100, 100);+ wlr_scene_node_set_position(&footprint->node, -16, -8);+ wlr_scene_buffer_set_dest_size(footprint, 152, 112);+ struct rediwm_shadow_spec spec = {.width = 120, .height = 80, .radii = {10, 10, 10, 10},+ .offset_y = 8, .softness = 16, .color = {0, 0, 0, .4f}};+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ struct leaf *l = get_leaf(p, &footprint->node);+ assert(l->item.kind == REDIWM_ITEM_BUFFER && !l->item.shadow);+ clear_damage(out);++ assert(rediwm_projection_set_tree_shadow(&shadow_tree->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(l->item.kind == REDIWM_ITEM_SHADOW && l->item.shadow);+ assert(near(l->item.shadow->shape.edges.x1, 100) && near(l->item.shadow->shape.edges.y2, 180));+ assert(near(l->item.shadow->offset_y, 8) && near(l->item.shadow->softness, 16));+ assert(layout_damaged(out, 85, 93) && layout_damaged(out, 235, 203) && layout_damaged(out, 160, 140));+ assert(!layout_damaged(out, 80, 140) && !layout_damaged(out, 160, 90));+ clear_damage(out);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0); // Nothing changed.+ assert(!pixman_region32_not_empty(&out->damage_ring.current));+ // A moved window damages where its shadow was and where it is.+ wlr_scene_node_set_position(&frame->node, 140, 100);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 85, 140) && layout_damaged(out, 270, 140));+ clear_damage(out);+ // A different colour damages it in place.+ spec.color[3] = .5f;+ assert(rediwm_projection_set_tree_shadow(&shadow_tree->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 130, 140) && !layout_damaged(out, 100, 140));+ clear_damage(out);+ // Hidden with its window, shown again, and destroyed.+ wlr_scene_node_set_enabled(&frame->node, false);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 130, 140));+ clear_damage(out);+ wlr_scene_node_set_enabled(&frame->node, true);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 130, 140));+ clear_damage(out);+ wlr_scene_node_destroy(&footprint->node);+ assert(layout_damaged(out, 130, 140));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);++ wlr_scene_node_destroy(&view->node);+ rediwm_projection_destroy(p);+ wlr_scene_node_destroy(&source->node);+}// A client surface's opaque region has to reach its view, or nothing below the// window is ever culled. The buffers here reveal no format, like a// wlr_client_buffer, so only the node's own region can say they are opaque.@@ -1002,6 +1062,7 @@ int main(void) {test_clip_opaque();test_clip_projection(scene);test_clip_output(scene, out);+ test_shadow_output(scene, out);test_opaque(scene);wlr_scene_node_destroy(&scene->tree.node);wlr_buffer_drop(a);diff --git a/src/scene_graph.h b/src/scene_graph.hindex dbc904b..6ba588c 100644--- a/src/scene_graph.h+++ b/src/scene_graph.h@@ -162,11 +162,54 @@ static inline int rediwm_clip_opaque(const struct rediwm_clip *clip,return n;}+// Stage 4d: the analytic drop shadow of a window's shape, set on the tree that+// holds the shadow's transport leaf. `width`, `height` and `radii` are the shape+// the shadow is kept out of (the window's, at the tree's origin, in source+// units); the shadow is that shape moved by the offset, at full strength inside+// it and falling to nothing `softness` units outside with the smoothstep of the+// CPU chrome (`chrome.shadowCoverage`). Colour premultiplied.+struct rediwm_shadow_spec {+ int width, height;+ double radii[4];+ double offset_x, offset_y, softness;+ float color[4];+};+struct rediwm_shadow {+ struct rediwm_clip shape; // in layout space; no border+ double offset_x, offset_y, softness;+ float color[4];+};+static inline struct rediwm_shadow rediwm_shadow_resolve(struct rediwm_xform t, double x,+ double y,+ const struct rediwm_shadow_spec *spec) {+ struct rediwm_clip_spec shape = {.width = spec->width, .height = spec->height};+ for (int i = 0; i < 4; i++) shape.radii[i] = spec->radii[i];+ struct rediwm_shadow shadow = {.shape = rediwm_clip_resolve(t, x, y, &shape),+ .offset_x = spec->offset_x * t.scale,+ .offset_y = spec->offset_y * t.scale,+ .softness = spec->softness * t.scale};+ for (int i = 0; i < 4; i++) shadow.color[i] = spec->color[i];+ return shadow;+}+static inline bool rediwm_shadow_equal(const struct rediwm_shadow *a,+ const struct rediwm_shadow *b) {+ if (a->shape.edges.x1 != b->shape.edges.x1 || a->shape.edges.y1 != b->shape.edges.y1 ||+ a->shape.edges.x2 != b->shape.edges.x2 || a->shape.edges.y2 != b->shape.edges.y2 ||+ a->offset_x != b->offset_x || a->offset_y != b->offset_y || a->softness != b->softness)+ return false;+ for (int i = 0; i < 4; i++)+ if (a->shape.radii[i] != b->shape.radii[i] || a->color[i] != b->color[i]) return false;+ return true;+}+enum rediwm_item_kind {REDIWM_ITEM_RECT, // a solid colourREDIWM_ITEM_SURFACE, // the buffer of a client wl_surfaceREDIWM_ITEM_BUFFER, // a compositor-owned raster: chrome, text, panels, shadowsREDIWM_ITEM_EFFECT, // a backdrop effect; its pixels belong to glass+ // The analytic shadow of `item.shadow`; the leaf transports input, opacity and+ // the footprint (its edges), never pixels.+ REDIWM_ITEM_SHADOW,};// What a leaf draws, apart from where. `color` is premultiplied.struct rediwm_content {@@ -216,4 +259,6 @@ struct rediwm_item {// The shape clipping this item with the rest of its tree, or NULL. Owned by the// leaf the item belongs to: valid for the callback, until the next resolve.const struct rediwm_clip *clip;+ // Set exactly for REDIWM_ITEM_SHADOW, with the same lifetime as `clip`.+ const struct rediwm_shadow *shadow;};diff --git a/src/scene_output.c b/src/scene_output.cindex 3aaf10c..28f5755 100644--- a/src/scene_output.c+++ b/src/scene_output.c@@ -148,6 +148,9 @@ static bool collect(const struct rediwm_item *item, struct wlr_scene_node *transif (!wlr_box_intersection(&intersection, &box, &collector->view->logical)) return true;if (item->kind == REDIWM_ITEM_RECT) {if (item->content.color[3] == 0) return true;+ } else if (item->kind == REDIWM_ITEM_SHADOW) {+ // No buffer: the pixels are the shadow's, and never opaque.+ if (item->content.opacity == 0 || item->shadow->color[3] == 0) return true;} else {struct wlr_scene_buffer *b = wlr_scene_buffer_from_node(transport);if (item->content.opacity == 0 ||@@ -166,7 +169,7 @@ static bool collect(const struct rediwm_item *item, struct wlr_scene_node *trans.box = box, .visible = visible};pixman_region32_init(&e->opaque);bool full = item->kind == REDIWM_ITEM_RECT && item->content.color[3] == 1;- if (item->kind != REDIWM_ITEM_RECT) {+ if (item->kind != REDIWM_ITEM_RECT && item->kind != REDIWM_ITEM_SHADOW) {struct wlr_scene_buffer *b = wlr_scene_buffer_from_node(transport);// Match the surface adapter's opacity policy. A cached raster whose// buffer was released must not suddenly hide previously visible peers.@@ -327,7 +330,24 @@ static bool render_entry(struct entry *e, struct wlr_scene_output *out,pixman_region32_subtract(&clip, &clip, &core);}const struct rediwm_content *c = &e->item.content;- if (e->item.kind == REDIWM_ITEM_RECT) {+ if (e->item.kind == REDIWM_ITEM_SHADOW) {+ // The offset is taken to device pixels as the difference of two snapped+ // boxes, so an output transform turns it with them.+ const struct rediwm_shadow *sh = e->item.shadow;+ struct rediwm_clip moved = sh->shape;+ moved.edges.x1 += sh->offset_x;+ moved.edges.x2 += sh->offset_x;+ moved.edges.y1 += sh->offset_y;+ moved.edges.y2 += sh->offset_y;+ struct rediwm_pass_shape at;+ shape_for(&shape, &sh->shape, v);+ shape_for(&at, &moved, v);+ struct rediwm_pass_shadow shadow = {+ .offset_x = (float)(at.box.x - shape.box.x), .offset_y = (float)(at.box.y - shape.box.y),+ .softness = (float)(sh->softness * v->scale), .hide_inside = true, .clip = &clip};+ for (int i = 0; i < 4; i++) shadow.color[i] = sh->color[i] * c->opacity;+ rediwm_scene_pass_add_shadow(pass, &shape, &shadow);+ } else if (e->item.kind == REDIWM_ITEM_RECT) {struct wlr_render_rect_options rect = {.box = dst,.color = {c->color[0], c->color[1], c->color[2], c->color[3]}};if (pixman_region32_not_empty(&core)) {diff --git a/src/scene_pass.c b/src/scene_pass.cindex 9973692..b70ed90 100644--- a/src/scene_pass.c+++ b/src/scene_pass.c@@ -22,7 +22,7 @@ struct program {unsigned bit; // which bit of pass.targets_set tracks this program's target sizeGLint target, dst, color, alpha, opaque, sampler, coords;// Shaped programs only (-1 elsewhere): the shape, and the shadow's own terms.- GLint mode, origin, size, radii, border, bcolor, offset, sigma, hide;+ GLint mode, origin, size, radii, border, bcolor, offset, softness, hide;};struct attrib_state {GLint enabled, size, type, normalized, stride, buffer;@@ -179,19 +179,16 @@ static const char shaped_vertex[] =// add up to the shape's, the spike's model. As two draws the coverages would// multiply, and a seam would show where each is partly covered.//-// `mode` 0 draws the texture, 1 the solid colour, 2 the shape's shadow: a-// Gaussian-blurred copy of the shape, offset, approximated through its distance-// field (exact on straight edges, close at corners), kept out of the shape when-// `hide` is set, which matters under translucent windows. One program for the+// `mode` 0 draws the texture, 1 the solid colour, 2 the shape's shadow: the+// shape moved by `offset`, falling off over `softness` outside it with the+// smoothstep of the CPU chrome's shadow (`chrome.shadowCoverage`, the same+// distance field, so corners match it as exactly as edges), kept out of the shape+// when `hide` is set, which matters under translucent windows. One program for the// three because a program costs the driver 130-150 KiB however small it is.static const char shaped_fragment[] = SHAPED_HEAD- "uniform float alpha, opaque, mode, sigma, hide; uniform vec2 offset;"- // Abramowitz-Stegun 7.1.26, |error| < 1.5e-7.- "float erf1(float x){ float s=x<0.?-1.:1.; x=abs(x); float t=1./(1.+.3275911*x);"- "float y=1.-(((((1.061405429*t-1.453152027)*t)+1.421413741)*t-.284496736)*t+.254829592)*t*exp(-x*x);"- "return s*y; }"+ "uniform float alpha, opaque, mode, softness, hide; uniform vec2 offset;""void main(){"- "if(mode>1.5){ float s=.5*(1.-erf1(sdf(local-offset,radii)/(sigma*1.41421356)));"+ "if(mode>1.5){ float s=clamp(1.-sdf(local-offset,radii)/softness,0.,1.); s=s*s*(3.-2.*s);""if(hide>.5)s*=1.-cov(sdf(local,radii)); gl_FragColor=color*s; return; }""vec4 c=color; if(mode<.5){ c=texture2D(tex,uv); if(opaque>.5)c.a=1.; c*=alpha; }""float d=sdf(local,radii);"@@ -255,7 +252,7 @@ static bool program_init(struct program *p, unsigned bit, const char *vertex_soup->border = glGetUniformLocation(p->id, "border");p->bcolor = glGetUniformLocation(p->id, "bcolor");p->offset = glGetUniformLocation(p->id, "offset");- p->sigma = glGetUniformLocation(p->id, "sigma");+ p->softness = glGetUniformLocation(p->id, "softness");p->hide = glGetUniformLocation(p->id, "hide");return true;}@@ -571,15 +568,15 @@ void rediwm_scene_pass_add_shadow(struct wlr_render_pass *base,struct program *g = &p->renderer->shaped;if (p->failed || (shadow->clip && !pixman_region32_not_empty(shadow->clip))) return;if (!g->id || !shape_usable(shape) || !isfinite(shadow->offset_x) ||- !isfinite(shadow->offset_y) || !isfinite(shadow->sigma) || shadow->sigma > 1024 ||+ !isfinite(shadow->offset_y) || !isfinite(shadow->softness) || shadow->softness > 4096 ||fabsf(shadow->offset_x) > 65536 || fabsf(shadow->offset_y) > 65536) {p->failed = true;return;}// A quarter pixel is a hard edge; zero would divide by it.- const float sigma = shadow->sigma < .25f ? .25f : shadow->sigma;- // The quad reaches three sigma past the shape, and as far again as the offset.- const int pad = (int)ceilf(3 * sigma) + (int)ceilf(fmaxf(fabsf(shadow->offset_x), fabsf(shadow->offset_y)));+ const float softness = shadow->softness < .25f ? .25f : shadow->softness;+ // The quad reaches the softness past the shape, and as far again as the offset.+ const int pad = (int)ceilf(softness) + (int)ceilf(fmaxf(fabsf(shadow->offset_x), fabsf(shadow->offset_y)));const struct wlr_box box = {shape->box.x - pad, shape->box.y - pad,shape->box.width + 2 * pad, shape->box.height + 2 * pad};setup(p, g, box, WLR_RENDER_BLEND_MODE_PREMULTIPLIED);@@ -589,7 +586,7 @@ void rediwm_scene_pass_add_shadow(struct wlr_render_pass *base,set_shape(g, &bare);glUniform1f(g->mode, 2.f);glUniform2f(g->offset, shadow->offset_x, shadow->offset_y);- glUniform1f(g->sigma, sigma);+ glUniform1f(g->softness, softness);glUniform1f(g->hide, shadow->hide_inside ? 1.f : 0.f);draw(shadow->clip);}diff --git a/src/scene_pass.h b/src/scene_pass.hindex 287cf0e..65b5995 100644--- a/src/scene_pass.h+++ b/src/scene_pass.h@@ -42,7 +42,9 @@ struct rediwm_pass_shape {};struct rediwm_pass_shadow {float offset_x, offset_y; // how far the shadow is shifted from the shape- float sigma; // Gaussian standard deviation; below 0.25 it is 0.25+ // Full strength inside the moved shape, falling to nothing this far outside it+ // with a smoothstep, as the CPU chrome's shadow does; below 0.25 it is 0.25.+ float softness;float color[4]; // premultiplied, at full strength under the shape's edge// Keep the shadow out of the shape, so a translucent window does not show it.bool hide_inside;@@ -67,6 +69,6 @@ void rediwm_scene_pass_add_rect_shaped(struct wlr_render_pass *,const struct wlr_render_rect_options *, const struct rediwm_pass_shape *);void rediwm_scene_pass_add_texture_shaped(struct wlr_render_pass *,const struct wlr_render_texture_options *, const struct rediwm_pass_shape *);-// The shape's shadow. The quad reaches three sigma and the offset past the box.+// The shape's shadow. The quad reaches the softness and the offset past the box.void rediwm_scene_pass_add_shadow(struct wlr_render_pass *, const struct rediwm_pass_shape *,const struct rediwm_pass_shadow *);diff --git a/src/scene_pass_test.c b/src/scene_pass_test.cindex 4c4ad14..5c68c15 100644--- a/src/scene_pass_test.c+++ b/src/scene_pass_test.c@@ -771,36 +771,61 @@ static void gate_opaque_cover(struct rediwm_scene_renderer *r, struct wlr_buffer}// ---- Gate 6: analytic shadow ------------------------------------------------------------+// The CPU chrome's shadow (chrome.zig: ShadowBuffer.rasterize, shadowCoverage,+// edgeCoverage) at one pixel: smoothstep falloff from the moved shape, cut out of+// the unmoved one with a one-pixel ramp. Every pixel is compared, corners too.+static double cpu_shadow(double px, double py, const struct rediwm_pass_shape *s, double oy, double softness) {+ const double radii[4] = {s->radii[0], s->radii[1], s->radii[2], s->radii[3]};+ const double d = rediwm_sd_rounded_box(px, py, s->box.x, s->box.y + oy, s->box.width, s->box.height, radii);+ const double frame_d = rediwm_sd_rounded_box(px, py, s->box.x, s->box.y, s->box.width, s->box.height, radii);+ double t = 1 - d / softness;+ t = t < 0 ? 0 : t > 1 ? 1 : t;+ double inside = .5 - frame_d;+ inside = inside < 0 ? 0 : inside > 1 ? 1 : inside;+ return t * t * (3 - 2 * t) * (1 - inside);+}static void gate_shadow(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {const float white[4] = {1, 1, 1, 1};- const struct rediwm_pass_shape s = {.box = {80, 60, 200, 120}};- const float sigma = 12, oy = 6;- struct wlr_render_pass *pass = begin_fill(r, buf, white, NULL);- rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){- .offset_y = oy, .sigma = sigma, .color = {0, 0, 0, .5f}, .hide_inside = true});- finish(pass);- read_image(r, buf, img);- int x = s.box.x + s.box.width / 2, bottom = s.box.y + s.box.height, worst = 0, last = 0;- for (int dy = 0; dy < 60; dy++) {- // Distance of the pixel centre below the offset shape's bottom edge.- double d = (dy + .5) - oy;- int want = (int)lround(255 * (1 - .5 * .5 * erfc(d / (sigma * 1.41421356237))));- int got = at(img, x, bottom + dy)[0];- if (diff(got, want) > worst) worst = diff(got, want);- check(got >= last, "shadow is not monotonic at %d px (%d after %d)", dy, got, last);- last = got;+ // The default theme: radius 10, softness 24, offset 8, at scale 1 and 1.5.+ const float scales[] = {1, 1.5f};+ int worst = 0;+ for (int si = 0; si < 2; si++) {+ const float k = scales[si];+ const float softness = 24 * k, oy = roundf(8 * k), radius = 10 * k;+ const struct rediwm_pass_shape s = {.box = {80, 60, 200, 120}, .radii = {radius, radius, radius, radius}};+ struct wlr_render_pass *pass = begin_fill(r, buf, white, NULL);+ rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){+ .offset_y = oy, .softness = softness, .color = {0, 0, 0, .5f}, .hide_inside = true});+ finish(pass);+ read_image(r, buf, img);+ for (int y = 0; y < img->h; y++)+ for (int x = 0; x < img->w; x++) {+ const int want = (int)lround(255 * (1 - .5 * cpu_shadow(x + .5, y + .5, &s, oy, softness)));+ const int got = at(img, x, y)[0];+ if (diff(got, want) > worst) worst = diff(got, want);+ }+ const int cx = s.box.x + s.box.width / 2, bottom = s.box.y + s.box.height;+ int last = 255;+ for (int dy = (int)oy; dy < 60; dy++) {+ const int got = at(img, cx, bottom + dy)[0];+ check(got >= last || dy == (int)oy, "shadow is not monotonic at %d px (%d after %d)", dy, got, last);+ last = got;+ }+ check(at(img, 10, 10)[0] == 255 && at(img, cx, s.box.y + s.box.height / 2)[0] == 255,+ "shadow reaches outside its footprint or inside the window");}- check(worst <= 3, "shadow profile differs from the Gaussian edge by %d levels", worst);- check(at(img, 10, 10)[0] == 255 && at(img, x, s.box.y + s.box.height / 2)[0] == 255,- "shadow reaches outside its footprint or inside the window");+ check(worst <= 1, "shadow differs from the CPU chrome's by %d levels", worst);// Not hidden inside: the same draw shows through the window.- pass = begin_fill(r, buf, white, NULL);+ const struct rediwm_pass_shape s = {.box = {80, 60, 200, 120}};+ struct wlr_render_pass *pass = begin_fill(r, buf, white, NULL);rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){- .offset_y = oy, .sigma = sigma, .color = {0, 0, 0, .5f}});+ .offset_y = 6, .softness = 24, .color = {0, 0, 0, .5f}});finish(pass);read_image(r, buf, img);- check(at(img, x, s.box.y + s.box.height / 2)[0] < 200, "an unhidden shadow does not show under the shape");- printf("PASS: analytic shadow follows the Gaussian edge (max diff %d) and stays out of the window\n", worst);+ check(at(img, s.box.x + s.box.width / 2, s.box.y + s.box.height / 2)[0] < 200,+ "an unhidden shadow does not show under the shape");+ printf("PASS: analytic shadow equals the CPU chrome's at every pixel, corners included (max diff %d), "+ "and stays out of the window\n", worst);}// ---- Gate 7: a partial repaint across a clip edge equals a full repaint -----------------@@ -815,7 +840,7 @@ static void draw_scene(struct rediwm_scene_renderer *r, struct wlr_buffer *buf,memcpy(s.border_color, rings[variant], sizeof(rings[variant]));struct wlr_render_pass *pass = begin_fill(r, buf, BG, clip);rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){- .offset_y = 12, .sigma = 12,+ .offset_y = 12, .softness = 24,.color = {0, 0, 0, .4f}, .hide_inside = true, .clip = clip});piece(pass, &s, w->title, bodies[variant][0], clip);piece(pass, &s, w->client, bodies[variant][1], clip);diff --git a/tests/window_corners.py b/tests/window_corners.pyindex 57ab0f2..d9eb64e 100644--- a/tests/window_corners.py+++ b/tests/window_corners.py@@ -261,8 +261,8 @@ def _frame_screenshot(scale):def frame_parity(scale):"""A shape changes the corners and the bottom band and nothing else: the- titlebar, the side borders, the client and the shadow match the legacy frame- pixel for pixel, at every scale."""+ titlebar, the side borders and the client match the legacy frame pixel for+ pixel, at every scale, and the analytic shadow matches the CPU one."""from PIL import ImageChopslegacy, lwin, ldbg = frame_screenshot(scale, "legacy")owned, owin, odbg = frame_screenshot(scale, "owned")@@ -284,15 +284,55 @@ def frame_parity(scale):"left and right side columns": (round(x * scale), round((y + titlebar) * scale),round((x + w) * scale) - (0 if scale == int(scale) else math.ceil(scale)),round((y + h) * scale) - arc),- "shadow left of the window": (round((x - 14) * scale), round((y + 20) * scale),- round(x * scale), round((y + h) * scale) - arc),- "shadow above the window": (round((x + 20) * scale), round((y - 14) * scale),- round((x + w - 20) * scale), round(y * scale)),}for name, box in regions.items():diff = ImageChops.difference(legacy.crop(box), owned.crop(box))assert diff.getbbox() is None, (name, scale, box, diff.getbbox(), diff.getextrema())- print(f"PASS scale {scale}: titlebar, sides, client and shadow identical to the legacy frame")+ # Stage 4d: the owned pass draws the shadow from the shape, the legacy one from+ # CPU patches of the same formula; they agree to a level or two, corners too.+ # Only pixels clear of the window (one device pixel past its arc) are compared.+ # The legacy frame is 9 px taller, so its bottom shadow is compared moved up by+ # that, which lands on whole device pixels at integer scales only.+ soft, radius = 16, 10+ def outside(px, py):+ lx, ly = (px + 0.5) / scale, (py + 0.5) / scale+ qx = abs(lx - (x + w / 2)) - (w / 2 - radius)+ qy = abs(ly - (y + h / 2)) - (h / 2 - radius)+ d = math.hypot(max(qx, 0), max(qy, 0)) + min(max(qx, qy), 0) - radius+ return d * scale > 1+ shadow_regions = {+ "shadow left of the window": ((x - soft, y + 20, x, y + h - radius - 2), 0),+ "shadow above the window": ((x + 20, y - soft, x + w - 20, y), 0),+ "shadow at the top corners": ((x - soft, y - soft, x + w + soft, y + radius + 2), 0),+ }+ if scale == int(scale):+ shadow_regions["shadow below the window, corners included"] = (+ (x - soft, y + h - radius - 2, x + w + soft, y + h + soft + 8), round(9 * scale))+ # The legacy 9-slice has one known gap: its side patches take the window's edge+ # as straight down to the offset corner patch, so a few pixels under each bottom+ # arc show bare wallpaper where the shadow is at full strength. The owned shadow+ # must fill them.+ wallpaper = (48, 50, 56)+ worst, gap = 0, 0+ for name, (box, shift) in shadow_regions.items():+ x1, y1, x2, y2 = (round(v * scale) for v in box)+ for py in range(y1, y2):+ for px in range(x1, x2):+ if not outside(px, py):+ continue+ a, b = legacy.getpixel((px, py + shift)), owned.getpixel((px, py))+ level = max(abs(i - j) for i, j in zip(a, b))+ if level > 2 and a == wallpaper and sum(b) < sum(wallpaper) - 30:+ # Only ever under a bottom arc.+ lx, ly = (px + 0.5) / scale, (py + 0.5) / scale+ assert y + h - radius <= ly <= y + h and (lx <= x + radius or lx >= x + w - radius), \+ ("the shadow differs outside the legacy corner gap", scale, (px, py), a, b)+ gap += 1+ continue+ worst = max(worst, level)+ assert level <= 2, (name, scale, (px, py), a, b)+ print(f"PASS scale {scale}: titlebar, sides and client identical to the legacy frame, "+ f"the analytic shadow within {worst} levels of the CPU one ({gap} legacy gap pixels filled)")def run(scale, background=(48, 50, 56), chrome_alpha=0.98):