RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
rendering improvements
oxydizer2b9ee1290358
diff --git a/src/images/app.zig b/src/images/app.zigindex c2fcaa7..a9843b0 100644--- a/src/images/app.zig+++ b/src/images/app.zig@@ -48,6 +48,9 @@ pub const App = struct {/// Fractional device pixels per logical pixel; `scale` is its ceiling.density: f64 = 1,dirty: bool = true,+ /// Only the toolbar strip changed (its buttons' proximity glow): the+ /// client may repaint and damage just that strip instead of the window.+ toolbar_dirty: bool = false,title_changed: bool = true,title: [1024:0]u8 = @splat(0),closed: bool = false,diff --git a/src/images/app_input.zig b/src/images/app_input.zigindex 86051af..ab5b751 100644--- a/src/images/app_input.zig+++ b/src/images/app_input.zig@@ -69,7 +69,7 @@ fn toolbarProximityChanged(new_y: f64,) bool {const reach = ui_theme.global.border_glow_reach;- if (!ui_theme.global.button_hover_glow_enabled or reach <= 0 or+ if (!button_widget.proximityGlowVisible(ui_theme.global) orself.dialog != .none or self.operation != null)return false;@@ -230,7 +230,7 @@ pub fn handleMotion(self: *App, x: f64, y: f64) void {self.hover = hover;self.dirty = true;}- if (toolbarProximityChanged(self, old_x, old_y, x, y)) self.dirty = true;+ if (toolbarProximityChanged(self, old_x, old_y, x, y)) self.toolbar_dirty = true;if (self.operation != null or (self.dialog != .none and !self.filename_drag)) {self.px = x;self.py = y;@@ -400,12 +400,13 @@ test "Images toolbar proximity drives redraws outside direct hover" {app.px = 500;app.py = 500;+ // Glow alone repaints the toolbar strip, never the whole window.app.dirty = false;app.handleMotion(x, y);- try std.testing.expect(app.dirty);+ try std.testing.expect(app.toolbar_dirty and !app.dirty);try std.testing.expect(app.hover == null);- app.dirty = false;+ app.toolbar_dirty = false;app.handleMotion(500, 500);- try std.testing.expect(app.dirty);+ try std.testing.expect(app.toolbar_dirty and !app.dirty);}diff --git a/src/images/main.zig b/src/images/main.zigindex 52d66e4..82d6ae2 100644--- a/src/images/main.zig+++ b/src/images/main.zig@@ -82,6 +82,14 @@ pub const Client = struct {outstanding: usize = 0,buffers: [2]?*Buffer = @splat(null),applied_scale: i32 = 0,+ /// Frames painted so far, and the last one whose content outside the+ /// toolbar strip changed: a buffer holding that frame or a later one+ /// needs only the strip repainted for a `toolbar_dirty` frame.+ frames: u64 = 0,+ content_frame: u64 = 0,+ /// Pending `wl_surface.frame` callback of the last commit. Glow-only+ /// frames wait for it, so they follow the display, not the pointer rate.+ frame_callback: ?*wl.Callback = null,xkb_context: ?*c.xkb_context = null,xkb_state: ?*c.xkb_state = null,key_repeat: key_repeat.KeyRepeat = .{},@@ -628,19 +636,28 @@ pub const Client = struct {}fn render(self: *Client) !void {self.updateCursor();- if (!self.configured or !self.app.dirty or self.outstanding >= 2) return;+ if (!self.configured or !(self.app.dirty or self.app.toolbar_dirty) or self.outstanding >= 2) return;+ if (!self.app.dirty and self.frame_callback != null) return;const tbh = self.titlebarHeight();const w = self.app.w;const h = self.app.h + tbh;const buffer = (try self.acquireBuffer(w * self.scale, h * self.scale)) orelse return;+ // A proximity-glow frame repaints only the toolbar strip when this+ // buffer already holds everything else (see `content_frame`).+ const partial = !self.app.dirty and buffer.frame != 0 and+ buffer.frame >= self.content_frame and self.applied_scale == self.scale;+ const strip_h: i32 = @intFromFloat(@ceil(self.app.toolbarBottom()));const cr = c.cairo_create(buffer.image) orelse return error.CairoFailed;defer c.cairo_destroy(cr);c.cairo_scale(cr, @floatFromInt(self.scale), @floatFromInt(self.scale));- if (tbh > 0) self.renderTitlebar(cr, w, tbh);+ if (partial) {+ c.cairo_rectangle(cr, 0, @floatFromInt(tbh), @floatFromInt(w), @floatFromInt(strip_h));+ c.cairo_clip(cr);+ } else if (tbh > 0) self.renderTitlebar(cr, w, tbh);c.cairo_save(cr);c.cairo_translate(cr, 0, @floatFromInt(tbh));@@ -656,10 +673,25 @@ pub const Client = struct {self.xdg_surface.setWindowGeometry(0, 0, w, h);buffer.busy = true;self.outstanding += 1;+ self.frames += 1;+ buffer.frame = self.frames;+ if (self.app.dirty) self.content_frame = self.frames;+ if (self.frame_callback == null) {+ if (self.surface.frame()) |callback| {+ callback.setListener(*Client, frameDone, self);+ self.frame_callback = callback;+ } else |_| {}+ }self.surface.attach(buffer.wl_buffer, 0, 0);- self.surface.damage(0, 0, w, h);+ if (partial) self.surface.damage(0, tbh, w, strip_h) else self.surface.damage(0, 0, w, h);self.surface.commit();self.app.dirty = false;+ self.app.toolbar_dirty = false;+ }++ fn frameDone(callback: *wl.Callback, _: wl.Callback.Event, self: *Client) void {+ callback.destroy();+ self.frame_callback = null;}fn renderTitlebar(self: *Client, cr: *c.cairo_t, w: i32, h: i32) void {@@ -715,6 +747,8 @@ const Buffer = struct {w: i32,h: i32,busy: bool = false,+ /// `Client.frames` when this buffer was last painted; 0 = never.+ frame: u64 = 0,fn create(client: *Client, w: i32, h: i32) !*Buffer {if (w <= 0 or h <= 0 or w > 16384 or h > 16384) return error.InvalidSize;@@ -792,6 +826,7 @@ pub fn run(init: std.process.Init, paths: []const [:0]const u8, selected: usize,defer for (self.buffers) |slot| {if (slot) |buffer| buffer.destroy();};+ defer if (self.frame_callback) |callback| callback.destroy();const registry = try display.getRegistry();defer registry.destroy();diff --git a/src/pdf/app.zig b/src/pdf/app.zigindex f82be3f..36b4642 100644--- a/src/pdf/app.zig+++ b/src/pdf/app.zig@@ -170,6 +170,9 @@ pub const App = struct {h: i32 = 700,scale: i32 = 1,dirty: bool = true,+ /// Only the toolbar strip changed (its buttons' proximity glow): the+ /// client may repaint and damage just that strip instead of the window.+ toolbar_dirty: bool = false,title_changed: bool = true,title: [1024:0]u8 = @splat(0),closed: bool = false,diff --git a/src/pdf/app_pointer.zig b/src/pdf/app_pointer.zigindex a1b4453..3677dd9 100644--- a/src/pdf/app_pointer.zig+++ b/src/pdf/app_pointer.zig@@ -60,7 +60,7 @@ fn toolbarProximityChanged(new_y: f64,) bool {const reach = ui_theme.global.border_glow_reach;- if (!ui_theme.global.button_hover_glow_enabled or reach <= 0 or+ if (!button_widget.proximityGlowVisible(ui_theme.global) orself.dialog != .none)return false;@@ -79,9 +79,7 @@ pub fn handleMotion(self: *App, px: f64, py: f64) void {self.px = px;self.py = py;- if (toolbarProximityChanged(self, old_x, old_y, px, py)) {- self.dirty = true;- }+ if (toolbarProximityChanged(self, old_x, old_y, px, py)) self.toolbar_dirty = true;if (self.hbar_drag.active) {if (self.hbar()) |bar| self.scroll_x = self.hbar_drag.offsetAt(bar, @floatCast(px), @floatCast(py));diff --git a/src/pdf/client_render.zig b/src/pdf/client_render.zigindex f8764d7..65c3347 100644--- a/src/pdf/client_render.zig+++ b/src/pdf/client_render.zig@@ -7,6 +7,7 @@ const shell_ui = @import("ui").cairo;const main_mod = @import("main.zig");const Client = main_mod.Client;const Buffer = main_mod.Buffer;+const wl = @import("wayland").client.wl;pub fn acquireBuffer(self: *Client, w: i32, h: i32) !?*Buffer {for (self.buffers) |slot| if (slot) |buffer| {@@ -26,19 +27,28 @@ pub fn acquireBuffer(self: *Client, w: i32, h: i32) !?*Buffer {pub fn render(self: *Client) !void {self.updateCursor();- if (!self.configured or !self.app.dirty or self.outstanding >= 2) return;+ if (!self.configured or !(self.app.dirty or self.app.toolbar_dirty) or self.outstanding >= 2) return;+ if (!self.app.dirty and self.frame_callback != null) return;const tbh = self.titlebarHeight();const w = self.app.w;const h = self.app.h + tbh;const buffer = (try self.acquireBuffer(w * self.scale, h * self.scale)) orelse return;+ // A proximity-glow frame repaints only the toolbar strip when this+ // buffer already holds everything else (see `content_frame`).+ const partial = !self.app.dirty and buffer.frame != 0 and+ buffer.frame >= self.content_frame and self.applied_scale == self.scale;+ const strip_h: i32 = @intFromFloat(@ceil(self.app.toolbarRect().h));const cr = c.api.cairo_create(buffer.image) orelse return error.CairoFailed;defer c.api.cairo_destroy(cr);c.api.cairo_scale(cr, @floatFromInt(self.scale), @floatFromInt(self.scale));- if (tbh > 0) self.renderTitlebar(cr, w, tbh);+ if (partial) {+ c.api.cairo_rectangle(cr, 0, @floatFromInt(tbh), @floatFromInt(w), @floatFromInt(strip_h));+ c.api.cairo_clip(cr);+ } else if (tbh > 0) self.renderTitlebar(cr, w, tbh);c.api.cairo_save(cr);c.api.cairo_translate(cr, 0, @floatFromInt(tbh));@@ -54,10 +64,25 @@ pub fn render(self: *Client) !void {self.xdg_surface.setWindowGeometry(0, 0, w, h);buffer.busy = true;self.outstanding += 1;+ self.frames += 1;+ buffer.frame = self.frames;+ if (self.app.dirty) self.content_frame = self.frames;+ if (self.frame_callback == null) {+ if (self.surface.frame()) |callback| {+ callback.setListener(*Client, frameDone, self);+ self.frame_callback = callback;+ } else |_| {}+ }self.surface.attach(buffer.wl_buffer, 0, 0);- self.surface.damage(0, 0, w, h);+ if (partial) self.surface.damage(0, tbh, w, strip_h) else self.surface.damage(0, 0, w, h);self.surface.commit();self.app.dirty = false;+ self.app.toolbar_dirty = false;+}++fn frameDone(callback: *wl.Callback, _: wl.Callback.Event, self: *Client) void {+ callback.destroy();+ self.frame_callback = null;}pub fn renderTitlebar(self: *Client, cr: *c.api.cairo_t, w: i32, h: i32) void {diff --git a/src/pdf/main.zig b/src/pdf/main.zigindex 8672654..8147d9f 100644--- a/src/pdf/main.zig+++ b/src/pdf/main.zig@@ -75,6 +75,14 @@ pub const Client = struct {outstanding: usize = 0,buffers: [2]?*Buffer = @splat(null),applied_scale: i32 = 0,+ /// Frames painted so far, and the last one whose content outside the+ /// toolbar strip changed: a buffer holding that frame or a later one+ /// needs only the strip repainted for a `toolbar_dirty` frame.+ frames: u64 = 0,+ content_frame: u64 = 0,+ /// Pending `wl_surface.frame` callback of the last commit. Glow-only+ /// frames wait for it, so they follow the display, not the pointer rate.+ frame_callback: ?*wl.Callback = null,xkb_context: ?*c.api.xkb_context = null,xkb_state: ?*c.api.xkb_state = null,key_repeat: key_repeat.KeyRepeat = .{},@@ -157,6 +165,8 @@ pub const Buffer = struct {w: i32,h: i32,busy: bool = false,+ /// `Client.frames` when this buffer was last painted; 0 = never.+ frame: u64 = 0,pub fn create(client: *Client, w: i32, h: i32) !*Buffer {if (w <= 0 or h <= 0 or w > 16384 or h > 16384) return error.InvalidSize;@@ -294,6 +304,7 @@ pub fn run(init: std.process.Init.Minimal) !void {defer for (self.buffers) |slot| {if (slot) |buffer| buffer.destroy();};+ defer if (self.frame_callback) |callback| callback.destroy();const registry = try display.getRegistry();defer registry.destroy();diff --git a/src/ui/input.zig b/src/ui/input.zigindex f0cda2c..d9ecce2 100644--- a/src/ui/input.zig+++ b/src/ui/input.zig@@ -58,6 +58,12 @@ pub fn updateInteractiveProximity(viewport: ProximityRect,) bool {var changed = false;+ // Samples only feed the glow painter: without a visible glow, moving+ // the pointer must not dirty (and repaint) every widget in reach.+ if (!button_widget.proximityGlowVisible(theme.global)) {+ clearInteractiveProximityChanged(root, &changed);+ return changed;+ }const root_clip = button_widget.intersectRect(viewport, widgetRect(root));sampleInteractiveProximity(root, x, y, root_clip, root_clip, &changed);return changed;@@ -264,8 +270,13 @@ pub fn clearInteractiveProximity(root: *Widget) void {}fn clearInteractiveProximityTree(widget: *Widget) void {- _ = clearWidgetProximity(widget);- for (widget.children) |*child| clearInteractiveProximityTree(child);+ var changed = false;+ clearInteractiveProximityChanged(widget, &changed);+}++fn clearInteractiveProximityChanged(widget: *Widget, changed: *bool) void {+ if (clearWidgetProximity(widget)) changed.* = true;+ for (widget.children) |*child| clearInteractiveProximityChanged(child, changed);}pub const HitResult = struct {diff --git a/src/ui/widgets/button.zig b/src/ui/widgets/button.zigindex aa323f9..5a6fb6c 100644--- a/src/ui/widgets/button.zig+++ b/src/ui/widgets/button.zig@@ -59,6 +59,10 @@ pub fn sampleProximity(box: Rect, pointer_x: f32, pointer_y: f32, clip: Rect) Pr};}+test {+ _ = @import("button_glow_tests.zig");+}+test "proximity sampling respects clipping and returns button-local coordinates" {const box: Rect = .{ .x = 20, .y = 10, .w = 40, .h = 20 };const clip: Rect = .{ .x = 30, .y = 15, .w = 20, .h = 10 };@@ -308,6 +312,13 @@ fn paintProximityGlow(r: *Renderer, box: Rect, radius: f32, state: State) void {);}+/// Whether `paintProximityGlowAt` can draw anything with theme `t`. Hosts+/// skip proximity sampling (and the repaints it causes) when it cannot.+pub fn proximityGlowVisible(t: theme.Theme) bool {+ return t.button_hover_glow_enabled and t.border_glow[3] > 0 and+ t.border_glow_reach > 0 and t.border_glow_length > 0 and t.border_glow_depth > 0;+}+/// Shared edge-light painter for buttons and other interactive surfaces./// Pointer coordinates are local to `box`.pub fn paintProximityGlowAt(@@ -319,9 +330,7 @@ pub fn paintProximityGlowAt(proximity_y: f32,) void {const t = r.palette orelse theme.global;- if (!t.button_hover_glow_enabled or proximity_distance >= std.math.inf(f32) or- t.border_glow[3] <= 0 or t.border_glow_reach <= 0 or- t.border_glow_length <= 0 or t.border_glow_depth <= 0 or+ if (!proximityGlowVisible(t) or proximity_distance >= std.math.inf(f32) orbox.w <= 0 or box.h <= 0)return;@@ -422,49 +431,95 @@ pub fn paintProximityGlowAt(.radius = radius * s,};+ // Every edge term is (falloff along the edge) * (falloff by depth), each+ // a function of one axis only. The top and bottom edges share their+ // along-x falloff, left and right their along-y one, so a column needs+ // three exponentials and a row three, instead of eight per pixel. The+ // products keep the per-pixel order (near * along * depth * coverage),+ // so the result is bit-identical; an unlit edge contributes 0.+ const top = edges[0].near;+ const right = edges[1].near;+ const bottom = edges[2].near;+ const left = edges[3].near;+ const anchor_x = edges[0].x;+ const anchor_y = edges[1].y;+ const chunk = 256;+ var top_along: [chunk]f32 = undefined;+ var bottom_along: [chunk]f32 = undefined;+ var from_left: [chunk]f32 = undefined;+ var from_right: [chunk]f32 = undefined;++ // `blendPixel` moves a channel by at most alpha * a * max(channel, 1),+ // and rounds to bytes: below a quarter of a byte step nothing changes, so+ // skipping such faint blends (most of a large surface) is exact. Scaling+ // by coverage <= 1 is monotonic, so it is applied after the test.+ const near_min = 0.25 / (255 * red.a * @max(1, @max(red.r, @max(red.g, red.b))));+ const far_min = 0.25 / (255 * dark.a);+r.clean = false;- var py = y0;- while (py < y1) : (py += 1) {- const sy = @as(f32, @floatFromInt(py)) + 0.5;- const logical_y = sy / s;- const row: usize = @intCast(py * r.width);- var px = x0;- while (px < x1) : (px += 1) {- const sx = @as(f32, @floatFromInt(px)) + 0.5;+ var cx0 = x0;+ while (cx0 < x1) : (cx0 += chunk) {+ const cx1 = @min(x1, cx0 + chunk);+ var max_top: f32 = 0;+ var max_bottom: f32 = 0;+ var max_left: f32 = 0;+ var max_right: f32 = 0;+ for (0..@intCast(cx1 - cx0)) |i| {+ const sx = @as(f32, @floatFromInt(cx0 + @as(i32, @intCast(i)))) + 0.5;const logical_x = sx / s;- const coverage = paint_mod.sdf.coverage(paint_mod.sdf.sdRoundedBox(- sx, sy, shape.x, shape.y, shape.w, shape.h, shape.radius,- ));- if (coverage <= 0) continue;-- var near_alpha: f32 = 0;- var far_alpha: f32 = 0;- for (edges) |edge| {- if (edge.near <= 0) continue;-- const along = if (edge.vertical) logical_y - box.y else logical_x - box.x;- const anchor = if (edge.vertical) edge.y else edge.x;- const along_ratio = (along - anchor) / length;- const along_falloff = @exp(-2 * along_ratio * along_ratio);-- const near_depth = if (edge.vertical)- (if (edge.negative) logical_x - box.x else box.x + box.w - logical_x)- else- (if (edge.negative) logical_y - box.y else box.y + box.h - logical_y);- const far_depth = if (edge.vertical)- (if (edge.negative) box.x + box.w - logical_x else logical_x - box.x)- else- (if (edge.negative) box.y + box.h - logical_y else logical_y - box.y);-- near_alpha = @max(near_alpha, edge.near * along_falloff *- @exp(-@max(0, near_depth) / depth) * coverage);- far_alpha = @max(far_alpha, edge.near * along_falloff *- @exp(-@max(0, far_depth) / depth) * coverage);- }+ const along_ratio = (logical_x - box.x - anchor_x) / length;+ const along_falloff = @exp(-2 * along_ratio * along_ratio);+ top_along[i] = top * along_falloff;+ bottom_along[i] = bottom * along_falloff;+ from_left[i] = @exp(-@max(0, logical_x - box.x) / depth);+ from_right[i] = @exp(-@max(0, box.x + box.w - logical_x) / depth);+ max_top = @max(max_top, top_along[i]);+ max_bottom = @max(max_bottom, bottom_along[i]);+ max_left = @max(max_left, from_left[i]);+ max_right = @max(max_right, from_right[i]);+ }- const index = row + @as(usize, @intCast(px));- if (near_alpha > 0) paint_mod.blendPixel(&r.pixels[index], red, near_alpha);- if (far_alpha > 0) paint_mod.blendPixel(&r.pixels[index], dark, far_alpha);+ var py = y0;+ while (py < y1) : (py += 1) {+ const sy = @as(f32, @floatFromInt(py)) + 0.5;+ const logical_y = sy / s;+ const along_ratio = (logical_y - box.y - anchor_y) / length;+ const along_falloff = @exp(-2 * along_ratio * along_ratio);+ const right_along = right * along_falloff;+ const left_along = left * along_falloff;+ const from_top = @exp(-@max(0, logical_y - box.y) / depth);+ const from_bottom = @exp(-@max(0, box.y + box.h - logical_y) / depth);+ // Upper bounds of this row's alphas: skip it when both are faint.+ const row_near = @max(@max(max_top * from_top, right_along * max_right), @max(max_bottom * from_bottom, left_along * max_left));+ const row_far = @max(@max(max_top * from_bottom, right_along * max_left), @max(max_bottom * from_top, left_along * max_right));+ if (row_near < near_min and row_far < far_min) continue;+ const row: usize = @intCast(py * r.width);++ var px = cx0;+ while (px < cx1) : (px += 1) {+ const i: usize = @intCast(px - cx0);+ const near_raw = @max(+ @max(top_along[i] * from_top, right_along * from_right[i]),+ @max(bottom_along[i] * from_bottom, left_along * from_left[i]),+ );+ const far_raw = @max(+ @max(top_along[i] * from_bottom, right_along * from_left[i]),+ @max(bottom_along[i] * from_top, left_along * from_right[i]),+ );+ if (near_raw < near_min and far_raw < far_min) continue;++ const sx = @as(f32, @floatFromInt(px)) + 0.5;+ const coverage = paint_mod.sdf.coverage(paint_mod.sdf.sdRoundedBox(+ sx, sy, shape.x, shape.y, shape.w, shape.h, shape.radius,+ ));+ if (coverage <= 0) continue;+ const near_alpha = near_raw * coverage;+ const far_alpha = far_raw * coverage;++ const index = row + @as(usize, @intCast(px));+ if (near_alpha >= near_min) paint_mod.blendPixel(&r.pixels[index], red, near_alpha);+ if (far_alpha >= far_min) paint_mod.blendPixel(&r.pixels[index], dark, far_alpha);+ }}}}diff --git a/src/ui/widgets/button_glow_tests.zig b/src/ui/widgets/button_glow_tests.zignew file mode 100644index 0000000..cea82d2--- /dev/null+++ b/src/ui/widgets/button_glow_tests.zig@@ -0,0 +1,142 @@+//! The separable proximity-glow painter against the per-pixel formula it replaced.++const std = @import("std");+const button = @import("button.zig");+const theme = @import("../theme.zig");+const paint_mod = @import("../paint.zig");+const Renderer = paint_mod.Renderer;+const Rect = button.Rect;++fn glowNear(px: f32, py: f32, edge_x: f32, edge_y: f32, reach: f32) f32 {+ const distance = std.math.hypot(px - edge_x, py - edge_y);+ if (!(distance < reach)) return 0;+ const n = 1 - distance / reach;+ return n * n * (3 - 2 * n);+}++/// The original painter: eight exponentials per covered pixel.+fn reference(r: *Renderer, box: Rect, radius: f32, pointer_x: f32, pointer_y: f32) void {+ const t = r.palette.?;+ const cr = std.math.clamp(radius, 0, 0.5 * @min(box.w, box.h));+ const reach = t.border_glow_reach;+ const ex = std.math.clamp(pointer_x, cr, box.w - cr);+ const ey = std.math.clamp(pointer_y, cr, box.h - cr);+ const Edge = struct { x: f32, y: f32, near: f32, vertical: bool, negative: bool };+ const edges = [_]Edge{+ .{ .x = ex, .y = 0, .near = glowNear(pointer_x, pointer_y, ex, 0, reach), .vertical = false, .negative = true },+ .{ .x = box.w, .y = ey, .near = glowNear(pointer_x, pointer_y, box.w, ey, reach), .vertical = true, .negative = false },+ .{ .x = ex, .y = box.h, .near = glowNear(pointer_x, pointer_y, ex, box.h, reach), .vertical = false, .negative = false },+ .{ .x = 0, .y = ey, .near = glowNear(pointer_x, pointer_y, 0, ey, reach), .vertical = true, .negative = true },+ };+ const s = r.scale;+ var dx0 = box.x * s;+ var dy0 = box.y * s;+ var dx1 = (box.x + box.w) * s;+ var dy1 = (box.y + box.h) * s;+ if (r.effectiveClip()) |clip| {+ dx0 = @max(dx0, clip.x * s);+ dy0 = @max(dy0, clip.y * s);+ dx1 = @min(dx1, (clip.x + clip.w) * s);+ dy1 = @min(dy1, (clip.y + clip.h) * s);+ }+ const x0: i32 = @intFromFloat(@floor(@max(0, dx0)));+ const y0: i32 = @intFromFloat(@floor(@max(0, dy0)));+ const x1: i32 = @intFromFloat(@ceil(@min(@as(f32, @floatFromInt(r.width)), dx1)));+ const y1: i32 = @intFromFloat(@ceil(@min(@as(f32, @floatFromInt(r.height)), dy1)));+ const red = paint_mod.toColor(t.border_glow);+ const dark = paint_mod.toColor(.{ 0, 0, 0, t.border_glow[3] * 0.35 });+ var py = y0;+ while (py < y1) : (py += 1) {+ const sy = @as(f32, @floatFromInt(py)) + 0.5;+ const ly = sy / s;+ var px = x0;+ while (px < x1) : (px += 1) {+ const sx = @as(f32, @floatFromInt(px)) + 0.5;+ const lx = sx / s;+ const coverage = paint_mod.sdf.coverage(paint_mod.sdf.sdRoundedBox(sx, sy, box.x * s, box.y * s, box.w * s, box.h * s, radius * s));+ if (coverage <= 0) continue;+ var near_alpha: f32 = 0;+ var far_alpha: f32 = 0;+ for (edges) |edge| {+ if (edge.near <= 0) continue;+ const along = if (edge.vertical) ly - box.y else lx - box.x;+ const anchor = if (edge.vertical) edge.y else edge.x;+ const ratio = (along - anchor) / t.border_glow_length;+ const falloff = @exp(-2 * ratio * ratio);+ const near_depth = if (edge.vertical)+ (if (edge.negative) lx - box.x else box.x + box.w - lx)+ else+ (if (edge.negative) ly - box.y else box.y + box.h - ly);+ const far_depth = if (edge.vertical)+ (if (edge.negative) box.x + box.w - lx else lx - box.x)+ else+ (if (edge.negative) box.y + box.h - ly else ly - box.y);+ near_alpha = @max(near_alpha, edge.near * falloff * @exp(-@max(0, near_depth) / t.border_glow_depth) * coverage);+ far_alpha = @max(far_alpha, edge.near * falloff * @exp(-@max(0, far_depth) / t.border_glow_depth) * coverage);+ }+ const index: usize = @intCast(py * r.width + px);+ if (near_alpha > 0) paint_mod.blendPixel(&r.pixels[index], red, near_alpha);+ if (far_alpha > 0) paint_mod.blendPixel(&r.pixels[index], dark, far_alpha);+ }+ }+}++fn fillNoise(pixels: []u32) void {+ for (pixels, 0..) |*pixel, i| pixel.* = @as(u32, @truncate(i *% 2654435761)) | 0xff000000;+}++test "separable proximity glow is bit-identical to the per-pixel formula" {+ const allocator = std.testing.allocator;+ const width = 700;+ const height = 120;+ const got = try allocator.alloc(u32, width * height);+ defer allocator.free(got);+ const want = try allocator.alloc(u32, width * height);+ defer allocator.free(want);++ var palette: theme.Theme = .{};+ palette.border_glow_reach = 56;+ palette.border_glow_length = 72;+ palette.border_glow_depth = 3;++ // Wide enough at 2x to cross the painter's column chunks; small and+ // rounded enough for the corner clamps; clipped and unclipped.+ const boxes = [_]Rect{+ .{ .x = 3.25, .y = 2.5, .w = 300, .h = 40 },+ .{ .x = 20, .y = 10, .w = 36, .h = 24 },+ .{ .x = 0, .y = 0, .w = 12, .h = 50 },+ };+ const pointers = [_][2]f32{ .{ -10, 12 }, .{ 150, -20 }, .{ 18, 20 }, .{ 310, 45 }, .{ 5, 5 }, .{ -40, -40 } };+ for ([_]f32{ 0.6, 1, 0.08 }) |strength| for ([_]f32{ 1, 1.25, 1.5, 2 }) |scale| {+ palette.border_glow[3] = strength;+ for (boxes) |box| for ([_]f32{ 0, 6, 100 }) |radius| for (pointers) |pointer| for ([_]bool{ false, true }) |clipped| {+ const distance = std.math.hypot(@max(0, @max(-pointer[0], pointer[0] - box.w)), @max(0, @max(-pointer[1], pointer[1] - box.h)));+ fillNoise(got);+ fillNoise(want);+ var a = Renderer.init(got, width, height, scale);+ var b = Renderer.init(want, width, height, scale);+ a.palette = palette;+ b.palette = palette;+ if (clipped) {+ a.clip = .{ .x = box.x + 4.5, .y = box.y + 1, .w = box.w * 0.6, .h = box.h };+ b.clip = a.clip;+ }+ button.paintProximityGlowAt(&a, box, radius, distance, pointer[0], pointer[1]);+ reference(&b, box, radius, pointer[0], pointer[1]);+ try std.testing.expectEqualSlices(u32, want, got);+ };+ };+}++test "glow visibility follows the setting and every strength key" {+ var t: theme.Theme = .{};+ try std.testing.expect(button.proximityGlowVisible(t));+ t.button_hover_glow_enabled = false;+ try std.testing.expect(!button.proximityGlowVisible(t));+ t = .{};+ t.border_glow[3] = 0;+ try std.testing.expect(!button.proximityGlowVisible(t));+ t = .{};+ t.border_glow_reach = 0;+ try std.testing.expect(!button.proximityGlowVisible(t));+}