RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
scene graph plan, stage 1: pass spike
oxydizer958422f5ee10
diff --git a/AGENTS.md b/AGENTS.mdindex 87fc762..f7f379f 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -48,6 +48,11 @@ the one for the directory you work in: `src/files`, `src/editor`,never from the output being updated: it is in the effect's hash, and anoutput-dependent value made outputs of different scales re-blur and damageeach other forever (`glass_hold.py`).+* **Owned GL beside wlroots:** wlroots draws with client-side vertex arrays, so+ any GL work of ours must put `GL_ARRAY_BUFFER` (and the rest of the state it+ touched) back before a wlroots pass runs, as `glass.c`'s `leave()` does; a quad+ VBO left bound makes wlroots draw half a rectangle. A border is drawn in the+ same pass as its body, never over it (`docs/plans/scene-graph-stage1.md`).* **Commit only via `Output.commitFrameState`**, never`wlr_output_commit_state`. `commitIsRedundant` skips commits with no damage,colour change, attach-render lock or cursor change; register anything newdiff --git a/TESTING.md b/TESTING.mdindex fbea74f..2b9b30f 100644--- a/TESTING.md+++ b/TESTING.md@@ -15,6 +15,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, 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-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`. |diff --git a/build.zig b/build.zigindex 1b7cc1a..c5358f1 100644--- a/build.zig+++ b/build.zig@@ -690,6 +690,18 @@ pub fn build(b: *std.Build) void {const run_glass_tests = b.addRunArtifact(glass_tests);run_glass_tests.setEnvironmentVariable("WLR_RENDERER", "gles2");b.step("test-glass", "Run backdrop pixel checks with a headless GLES2 renderer").dependOn(&run_glass_tests.step);+ // Stage 1 spike of docs/plans/scene-graph.md: the owned pass's primitives, pixel-tested.+ const pass_spike_tests = b.addExecutable(.{+ .name = "pass-spike-tests",+ .root_module = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true }),+ });+ pass_spike_tests.root_module.addCSourceFile(.{ .file = b.path("src/pass_spike_test.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra" } });+ inline for (.{ "wlroots-0.20", "wayland-server", "pixman-1", "egl", "glesv2" }) |lib| {+ pass_spike_tests.root_module.linkSystemLibrary(lib, .{});+ }+ const run_pass_spike_tests = b.addRunArtifact(pass_spike_tests);+ run_pass_spike_tests.setEnvironmentVariable("WLR_RENDERER", "gles2");+ b.step("test-pass-spike", "Run the owned-pass spike's pixel gates on a headless GLES2 renderer").dependOn(&run_pass_spike_tests.step);const run_glass_damage_tests = b.addRunArtifact(glass_tests);run_glass_damage_tests.addArg("--damage-only");b.step("test-glass-damage", "Test backdrop invalidation without a GPU").dependOn(&run_glass_damage_tests.step);diff --git a/docs/plans/scene-graph-stage1.md b/docs/plans/scene-graph-stage1.mdnew file mode 100644index 0000000..362d716--- /dev/null+++ b/docs/plans/scene-graph-stage1.md@@ -0,0 +1,121 @@+# Scene graph plan, stage 1: pass spike++Status: stage 1 of [scene-graph.md](scene-graph.md), run 2026-10-09 on the+machine and conditions of the [stage 0 baseline](scene-graph-baseline.md).+Result: **every gate passes, after two design changes the gates forced.** Stage+2 can start on this design; the open integration risks are listed at the end.++## What was built++A noninstalled fixture, not linked into the compositor:++* `src/pass_spike.c`: the primitives an owned output pass needs, drawn on+ wlroots' own GLES2 context and a wlroots-allocated buffer's FBO, entering and+ leaving that context the way `glass.c` does. One fragment shader family (2D+ and external-OES samplers) evaluates a rounded-rectangle signed distance field+ in device pixels for the body (texture or solid), the border, an analytic+ shadow and a blurred-backdrop composite; the backdrop is a framebuffer copy,+ two blur passes at quarter size and a clipped composite.+* `src/pass_spike_test.c`: ten pixel gates and a cost measurement.+* `zig build test-pass-spike` (needs a render node; `WLR_RENDER_DRM_DEVICE` on a+ multi-GPU machine; add `-Doptimize=ReleaseSafe` for cost figures).++Conventions the spike fixes: device pixels, y down, buffer row 0 is the top row+(`ndc_y = py / height * 2 - 1`, so a GL scissor rectangle is an image+rectangle), colours premultiplied, `glBlendFunc(ONE, ONE_MINUS_SRC_ALPHA)`. A+window is an integer device rectangle with one radius per corner; titlebar,+client and footer are quads that tile it (edges snapped as+`round(off*s)`..`round((off+len)*s)`), all covered by the same distance field.++## Gates++| # | Gate | Result |+|---|---|---|+| 1 | Rounded clip against an 8×8 supersampled area-coverage reference at scales 1, 1.5, 2 and zoom 1, .85, .70, .55, .25; no channel outside the range of body and background (no fringe). | Pass: max error 12 levels (5.8 % of the 208-level range, the usual cost of distance-based AA on arcs), zero fringe values. |+| 2 | Titlebar, client and footer pieces: every interior pixel is exactly its piece's colour, at every scale and zoom (no seam, no background leak). | Pass. |+| 3 | Border and body in the same colour must equal one plain shape; with distinct colours the outer row is the border and the next row the body. | Pass (max diff 0) **after a design change, below.** |+| 4 | Premultiplied edges: a 50 % premultiplied texel over the backdrop, opacity 0.5, and an XRGB texel with a junk alpha byte. | Pass (±2). |+| 5 | An external-OES texture (an EGLImage of a dmabuf, bound to `GL_TEXTURE_EXTERNAL_OES`) through the clip equals the same pixels in a 2D texture, across the scale and zoom grid. | Pass (max diff 0). |+| 6 | Analytic shadow: monotonic falloff, zero outside its footprint and inside the window, equal to the Gaussian edge profile on a straight edge. | Pass (max diff 0 against `erfc`). Corners are approximate; not compared to the current CPU shadow yet. |+| 7 | Backdrop from a framebuffer copy: red/blue step under the window blurs to an even mix at the step, monotonic, far sides unchanged, nothing leaks past the clip; at the buffer edge the blur replicates the edge, no black. | Pass. |+| 8 | Buffer-age damage under an effect: repaint only the damaged rectangle into the previous frame's buffer. | Pass: expanding damage to the effect's shape when it reaches its sample extent equals a full repaint; without the expansion 3146 values stay stale (the control). |+| 9 | Cost against the stage 0 baseline. | Pass, below. |+| 10 | The spike and wlroots' own render pass draw the same premultiplied rectangle into the same buffer identically (orientation, blend, colour). | Pass (max diff 0), **after a test fix that is itself an integration rule, below.** |++### Cost (783×69 titlebar effect at scale 1.5, ReleaseSafe, AC, 5 runs)++| | Stage 0 baseline (one re-blurred `glass.c` effect) | Spike |+|---|---|---|+| CPU per frame | +0.17 ms | 0.006-0.019 ms submit |+| GPU per frame | +0.2 ms | 0.208-0.224 ms wall including the GPU |+| One full window (shadow, three pieces, border) | n/a | 0.003-0.007 ms submit, 0.079-0.089 ms wall |++The CPU cost nearly disappears (no scene prefix walk, no private render pass,+no per-effect context switch or `glFlush`); the GPU cost is the same, since the+copy and the blur remain. The wall figures are one `glFinish` per 300 frames, so+they are throughput, not latency.++## Design changes the gates forced++1. **The border belongs in the piece's fragment shader, not in a second draw.**+ A ring drawn "over" a body leaves `dst × a_body × a_ring` of the backdrop+ showing through wherever both are partly covered: a bright seam of 53 levels+ along the inner edge of every border (gate 3 failed on this). Both are+ evaluated at the same fragment (`inner = cov(d + b)`, `ring = cov(d) -+ cov(d + b)`), so their coverages add to the shape's. The same rule applies to+ the backdrop composite. The plan's separate "border primitive" becomes a+ parameter of every piece.+2. **A scissor and damage rule for effects.** A partial repaint of a buffer with+ age must also repaint every effect whose sample extent (shape plus padding)+ the damage touches, even though the effect's own rectangle is undamaged; the+ copy then reads current pixels in the damage and valid stale ones elsewhere.+ The effect's composite runs under the caller's scissor while its copy and+ blur passes run unscissored (gate 8).++Smaller points the spike settled: piece joins on integer device pixels need no+special seam handling (a quad covers each pixel once; only the shape edge is+anti-aliased); a shadow is clipped out of the window only when the body is+translucent (otherwise it would be attenuated twice at the edge); the backdrop+copy is clamped to the framebuffer with the blur's sampling limits set, which+replicates the edge instead of bleeding black.++## Integration rules found++* **wlroots draws with client-side vertex arrays**, which read from the bound+ `GL_ARRAY_BUFFER` if there is one. Calling wlroots' pass with the spike's quad+ buffer still bound drew half a rectangle (gate 10 failed on this, 103 levels).+ Any owned GL work has to restore the array-buffer binding (and, as `glass.c`'s+ `leave()` does, the rest of the state it touches) before wlroots draws, and+ re-enter afterwards. Once the output pass is owned, this is only a concern for+ the remaining wlroots entry points (screenshots, texture uploads).+* The owned pass can write the swapchain buffer through+ `wlr_gles2_renderer_get_buffer_fbo` and agrees with wlroots' pass on+ orientation, blend and colour (gate 10), so scanout and capture see the same+ image.++## Not yet proven (for stage 2 and 3)++* Real client textures: shm uploads through `wlr_client_buffer`, the texture+ target wlroots reports (`wlr_gles2_texture_get_attribs`), and YUV dmabufs that+ need external sampling with driver conversion. The spike imports one RGBA+ dmabuf itself.+* The swapchain's real buffer age and `wlr_damage_ring` (the spike keeps one+ persistent buffer), explicit-sync acquire and release points, software+ cursors, and subsurface and popup ordering.+* Direct scanout: finding 6 of the baseline made it work in the old presenter;+ the owned pass must keep one entry per scanout-eligible client or it+ regresses that.+* Quality of the quarter-size blur against `glass.c` side by side, the analytic+ shadow's corners against the CPU shadow, and the colour of window opacity+ under zoom.+* Multi-output with different scales (the pass renders per output; effects are+ per output and need no shared scale, which also removes the idle loop the+ stage 0 glass fix addressed).++## Verdict++The primitives work on wlroots' own GLES2 context and buffers, produce no seams+or fringe at any tested scale or zoom, draw an external-OES texture identically+to a 2D one, handle damage under an effect, and cost no more than the current+glass path (far less CPU, the same GPU). Proceed to stage 2, carrying the two+design changes above.diff --git a/docs/plans/scene-graph.md b/docs/plans/scene-graph.mdindex 6c1eb63..cb7e69c 100644--- a/docs/plans/scene-graph.md+++ b/docs/plans/scene-graph.md@@ -55,8 +55,11 @@ Client surface semantics stay behind an adapter; they are not reimplemented.skirt, `probeClientEdge`, `side_fills`, the straight-rect border path and thechrome arcs. xdg popups are separate trees and are never clipped by aparent's shape.-* *Analytic primitives.* Borders and shadows become shader primitives. CPU- raster is left for what is really content: title text, icons, buttons.+* *Analytic primitives.* Borders and shadows become shader primitives. A+ border is evaluated in the same fragment pass as the body it surrounds, never+ as a second draw over it (the two partial coverages multiply and leave a seam;+ stage 1, gate 3). CPU raster is left for what is really content: title text,+ icons, buttons.* *Output pass.* Per output: acquire swapchain buffer (age), accumulatedamage, walk in paint order with opaque-region culling, draw with an ownedGLES2 program driven directly on the wlroots context and buffer FBO, as@@ -113,6 +116,9 @@ environment variable: no test hooks in production), removed in stage 6.function and primaries, range/encoding, premultiplied blending, filters.* Confirm the policy defaults above and the renderer decision below.1. **Pass spike (noninstalled fixture, `test-glass`-style pixel tests).**+ *Done 2026-10-09: all ten gates pass, after two design changes; results,+ costs and the open integration risks are in+ [scene-graph-stage1.md](scene-graph-stage1.md).*Draw a 2D and an external-OES client texture through the rounded clip atfractional scale and zoom; analytic border and shadow; blur from aframebuffer copy. Gate: no seams or fringe (extend `window_corners.py`diff --git a/src/pass_spike.c b/src/pass_spike.cnew file mode 100644index 0000000..6d49357--- /dev/null+++ b/src/pass_spike.c@@ -0,0 +1,514 @@+// Stage 1 spike of docs/plans/scene-graph.md: the primitives an owned output+// pass needs, drawn on wlroots' own GLES2 context and a wlroots-allocated+// buffer's FBO exactly as glass.c does. Not installed and not linked into the+// compositor; pass_spike_test.c holds the pixel gates. If the design survives,+// stage 2/3 move this into the compositor.+//+// Conventions: device pixels, y down, row 0 of the buffer is the top row (the+// vertex shader maps py/height straight to NDC y, so memory order is image+// order, as in glass.c). Colours are premultiplied. A "shape" is an integer+// device rectangle with one corner radius per corner; every piece of a window+// (titlebar, client, footer, border, shadow) is covered by the same signed+// distance function, so there is no per-piece corner approximation.+#define _POSIX_C_SOURCE 200809L+#define WLR_USE_UNSTABLE+#include <EGL/egl.h>+#include <EGL/eglext.h>+#include <GLES2/gl2.h>+#include <GLES2/gl2ext.h>+#include <assert.h>+#include <drm_fourcc.h>+#include <math.h>+#include <stdbool.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <time.h>+#include <wlr/backend.h>+#include <wlr/render/allocator.h>+#include <wlr/render/drm_format_set.h>+#include <wlr/render/egl.h>+#include <wlr/render/gles2.h>+#include <wlr/render/wlr_renderer.h>+#include <wlr/types/wlr_buffer.h>+#include <wlr/util/log.h>++// ---------------------------------------------------------------------------+// Context: the renderer's own EGL context, saved and restored around the pass.+struct spike_gl_state {+ EGLDisplay display, previous_display;+ EGLContext previous_context;+ EGLSurface draw, read;+ GLint fbo, program, array_buffer, active_texture, texture, viewport[4];+ GLboolean blend, scissor;+};++struct spike {+ struct wlr_renderer *renderer;+ struct wlr_allocator *allocator;+ GLuint vertices;+ struct spike_program {+ GLuint id;+ GLint dst, target, rect, size, radii, border, bcolor, color, alpha, mode, content, src,+ shadow, opaque, tex, saturation, tint;+ } plain, external;+ struct spike_blur {+ GLuint id;+ GLint tex, step_uv, sigma, limits;+ } blur;+ PFNEGLCREATEIMAGEKHRPROC create_image;+ PFNEGLDESTROYIMAGEKHRPROC destroy_image;+ PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture;+ bool has_external;+};++static bool spike_enter(struct spike *spike, struct spike_gl_state *state)+{+ struct wlr_egl *egl = wlr_gles2_renderer_get_egl(spike->renderer);+ state->display = wlr_egl_get_display(egl);+ state->previous_display = eglGetCurrentDisplay();+ state->previous_context = eglGetCurrentContext();+ state->draw = eglGetCurrentSurface(EGL_DRAW);+ state->read = eglGetCurrentSurface(EGL_READ);+ if (!eglMakeCurrent(state->display, EGL_NO_SURFACE, EGL_NO_SURFACE, wlr_egl_get_context(egl)))+ return false;+ glGetIntegerv(GL_FRAMEBUFFER_BINDING, &state->fbo);+ glGetIntegerv(GL_CURRENT_PROGRAM, &state->program);+ glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &state->array_buffer);+ glGetIntegerv(GL_ACTIVE_TEXTURE, &state->active_texture);+ glGetIntegerv(GL_TEXTURE_BINDING_2D, &state->texture);+ glGetIntegerv(GL_VIEWPORT, state->viewport);+ state->blend = glIsEnabled(GL_BLEND);+ state->scissor = glIsEnabled(GL_SCISSOR_TEST);+ return true;+}+static void spike_leave(struct spike_gl_state *state)+{+ glBindFramebuffer(GL_FRAMEBUFFER, state->fbo);+ glUseProgram(state->program);+ glBindTexture(GL_TEXTURE_2D, state->texture);+ glActiveTexture(state->active_texture);+ glViewport(state->viewport[0], state->viewport[1], state->viewport[2], state->viewport[3]);+ (state->blend ? glEnable : glDisable)(GL_BLEND);+ (state->scissor ? glEnable : glDisable)(GL_SCISSOR_TEST);+ glBindBuffer(GL_ARRAY_BUFFER, state->array_buffer);+ if (state->previous_display != EGL_NO_DISPLAY)+ eglMakeCurrent(state->previous_display, state->draw, state->read,+ state->previous_context);+ else+ eglMakeCurrent(state->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);+}++// ---------------------------------------------------------------------------+// Shaders. One fragment source, built for a 2D or an external-OES sampler.+static const char spike_vertex_source[] =+ "attribute vec2 position; uniform vec4 dst; uniform vec2 target; uniform vec4 rect;"+ "varying vec2 v_px;"+ "void main(){ vec2 px=dst.xy+position*dst.zw; v_px=px-rect.xy;"+ "gl_Position=vec4(px.x/target.x*2.-1.,px.y/target.y*2.-1.,0.,1.); }";++static const char spike_fragment_body[] =+ "precision highp float; varying vec2 v_px;"+ "uniform vec2 size; uniform vec4 radii, color, bcolor, content, src, shadow;"+ "uniform float border, alpha, mode, opaque, saturation; uniform vec4 tint;"+ "float sdf(vec2 p, vec2 sz, vec4 r){ vec2 q=p-sz*.5;"+ "float rad=q.x<0.?(q.y<0.?r.x:r.w):(q.y<0.?r.y:r.z);"+ "vec2 d=abs(q)-(sz*.5-vec2(rad));"+ "return length(max(d,0.))+min(max(d.x,d.y),0.)-rad; }"+ "float cov(float d){ return clamp(.5-d,0.,1.); }"+ // 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; }"+ "vec4 sample_content(){ vec2 uv=mix(src.xy,src.zw,(v_px-content.xy)/content.zw);"+ "return texture2D(tex,uv); }"+ "void main(){"+ "float d=sdf(v_px,size,radii);"+ // 0: texture body, 1: solid body. A border is part of the same pass: the+ // body keeps the inside of the inset shape and the ring the rest, both+ // evaluated at this fragment, so their coverages add to the shape's. As+ // two draws they would multiply (a seam where each is partly covered).+ "if(mode<1.5){ float inner=border>0.?cov(d+border):cov(d);"+ "float ring=border>0.?cov(d)-cov(d+border):0.;"+ "vec4 t=mode<.5?sample_content():color; if(opaque>.5) t.a=1.;"+ "gl_FragColor=t*alpha*inner+bcolor*ring; }"+ // 3: shadow, a Gaussian-blurred shape approximated through its distance field.+ "else if(mode<3.5){ float ds=sdf(v_px-shadow.xy,size,radii);"+ "float s=.5*(1.-erf1(ds/(shadow.z*1.41421356)));"+ "if(shadow.w>.5) s*=1.-cov(d); gl_FragColor=color*s; }"+ // 4: blurred backdrop composited through the clip.+ "else { vec3 rgb=sample_content().rgb; float l=dot(rgb,vec3(.213,.715,.072));"+ "rgb=clamp(vec3(l)+(rgb-vec3(l))*saturation,0.,1.); rgb=mix(rgb,tint.rgb,tint.a);"+ "float inner=border>0.?cov(d+border):cov(d); float ring=border>0.?cov(d)-cov(d+border):0.;"+ "gl_FragColor=vec4(rgb,1.)*inner*alpha+bcolor*ring; }"+ "}";++static const char spike_blur_vertex[] =+ "attribute vec2 position; varying vec2 uv;"+ "void main(){ uv=position*.5+.5; gl_Position=vec4(position,0.,1.); }";+// The kernel glass.c uses: 49 taps, sigma in texels of the target.+static const char spike_blur_fragment[] =+ "precision highp float; varying vec2 uv; uniform sampler2D tex;"+ "uniform vec2 step_uv; uniform float sigma; uniform vec4 limits;"+ "void main(){ vec4 sum=vec4(0.); float weight=0.;"+ "for(int i=-24;i<=24;i++){ float x=float(i); float w=exp(-.5*x*x/(sigma*sigma));"+ "sum+=texture2D(tex,clamp(uv+x*step_uv,limits.xy,limits.zw))*w; weight+=w; }"+ "gl_FragColor=sum/weight; }";++static GLuint spike_shader(GLenum type, const char *prefix, const char *source)+{+ const char *parts[2] = {prefix, source};+ GLuint id = glCreateShader(type);+ glShaderSource(id, 2, parts, NULL);+ glCompileShader(id);+ GLint ok;+ glGetShaderiv(id, GL_COMPILE_STATUS, &ok);+ if (!ok) {+ char log[1024];+ glGetShaderInfoLog(id, sizeof(log), NULL, log);+ fprintf(stderr, "spike shader: %s\n", log);+ glDeleteShader(id);+ return 0;+ }+ return id;+}+static GLuint spike_link(const char *vertex_prefix, const char *vertex, const char *fragment_prefix,+ const char *fragment)+{+ GLuint vs = spike_shader(GL_VERTEX_SHADER, vertex_prefix, vertex);+ GLuint fs = spike_shader(GL_FRAGMENT_SHADER, fragment_prefix, fragment);+ if (!vs || !fs)+ return 0;+ GLuint id = glCreateProgram();+ glAttachShader(id, vs);+ glAttachShader(id, fs);+ glBindAttribLocation(id, 0, "position");+ glLinkProgram(id);+ glDeleteShader(vs);+ glDeleteShader(fs);+ GLint ok;+ glGetProgramiv(id, GL_LINK_STATUS, &ok);+ return ok ? id : 0;+}+static bool spike_build_program(struct spike_program *p, bool external)+{+ const char *prefix = external ? "#extension GL_OES_EGL_image_external : require\n"+ "#define EXTERNAL\nprecision highp float;\n"+ "uniform samplerExternalOES tex;\n"+ : "precision highp float;\nuniform sampler2D tex;\n";+ // The sampler declaration comes before the shared body, which only uses it.+ p->id = spike_link("", spike_vertex_source, prefix, spike_fragment_body);+ if (!p->id)+ return false;+#define LOC(field, name) p->field = glGetUniformLocation(p->id, name)+ LOC(dst, "dst"), LOC(target, "target"), LOC(rect, "rect"), LOC(size, "size");+ LOC(radii, "radii"), LOC(border, "border"), LOC(bcolor, "bcolor"), LOC(color, "color");+ LOC(alpha, "alpha");+ LOC(mode, "mode"), LOC(content, "content"), LOC(src, "src"), LOC(shadow, "shadow");+ LOC(opaque, "opaque"), LOC(tex, "tex"), LOC(saturation, "saturation"), LOC(tint, "tint");+#undef LOC+ return true;+}++static struct spike *spike_create(struct wlr_renderer *renderer, struct wlr_allocator *allocator)+{+ if (!wlr_renderer_is_gles2(renderer))+ return NULL;+ struct spike *spike = calloc(1, sizeof(*spike));+ assert(spike);+ spike->renderer = renderer;+ spike->allocator = allocator;+ struct spike_gl_state state;+ if (!spike_enter(spike, &state)) {+ free(spike);+ return NULL;+ }+ bool ok = spike_build_program(&spike->plain, false);+ spike->has_external = wlr_gles2_renderer_check_ext(renderer, "GL_OES_EGL_image_external");+ if (spike->has_external) {+ spike->create_image = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");+ spike->destroy_image = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");+ spike->image_target_texture = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress(+ "glEGLImageTargetTexture2DOES");+ spike->has_external = spike->create_image && spike->destroy_image &&+ spike->image_target_texture && spike_build_program(&spike->external, true);+ }+ spike->blur.id = spike_link("", spike_blur_vertex, "", spike_blur_fragment);+ ok = ok && spike->blur.id;+ spike->blur.tex = glGetUniformLocation(spike->blur.id, "tex");+ spike->blur.step_uv = glGetUniformLocation(spike->blur.id, "step_uv");+ spike->blur.sigma = glGetUniformLocation(spike->blur.id, "sigma");+ spike->blur.limits = glGetUniformLocation(spike->blur.id, "limits");+ const GLfloat quad[] = {0, 0, 1, 0, 0, 1, 1, 1};+ glGenBuffers(1, &spike->vertices);+ glBindBuffer(GL_ARRAY_BUFFER, spike->vertices);+ glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW);+ spike_leave(&state);+ if (!ok) {+ free(spike);+ return NULL;+ }+ return spike;+}++// ---------------------------------------------------------------------------+// Targets: a wlroots-allocated buffer drawn through the renderer's FBO for it.+struct spike_target {+ struct wlr_buffer *buffer;+ GLuint fbo;+ int width, height;+};+static bool spike_target_create(struct spike *spike, struct spike_target *target, int width,+ int height)+{+ uint64_t modifier = DRM_FORMAT_MOD_INVALID;+ struct wlr_drm_format format = {+ .format = DRM_FORMAT_ARGB8888, .len = 1, .capacity = 1, .modifiers = &modifier};+ target->buffer = wlr_allocator_create_buffer(spike->allocator, width, height, &format);+ if (!target->buffer)+ return false;+ target->width = width;+ target->height = height;+ target->fbo = wlr_gles2_renderer_get_buffer_fbo(spike->renderer, target->buffer);+ return target->fbo != 0;+}++// ---------------------------------------------------------------------------+// Drawing.+struct spike_shape {+ int x, y, w, h; // integer device rectangle+ float radii[4]; // tl, tr, br, bl, device px+};+struct spike_texture {+ GLenum target; // GL_TEXTURE_2D or GL_TEXTURE_EXTERNAL_OES+ GLuint id;+ bool opaque; // XRGB: alpha is not meaningful+};++static struct spike_program *spike_program_for(struct spike *spike, GLenum target)+{+ return target == GL_TEXTURE_EXTERNAL_OES ? &spike->external : &spike->plain;+}+static void spike_bind_quad(void)+{+ glEnableVertexAttribArray(0);+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);+}+static void spike_set_common(struct spike *spike, struct spike_program *p, struct spike_target *t,+ const struct spike_shape *shape, float border, const float *bcolor)+{+ glUseProgram(p->id);+ glBindBuffer(GL_ARRAY_BUFFER, spike->vertices);+ spike_bind_quad();+ glUniform2f(p->target, t->width, t->height);+ glUniform4f(p->rect, shape->x, shape->y, shape->w, shape->h);+ glUniform2f(p->size, shape->w, shape->h);+ glUniform4f(p->radii, shape->radii[0], shape->radii[1], shape->radii[2], shape->radii[3]);+ glUniform1f(p->border, border);+ glUniform4f(p->bcolor, bcolor ? bcolor[0] : 0, bcolor ? bcolor[1] : 0, bcolor ? bcolor[2] : 0,+ bcolor ? bcolor[3] : 0);+ glUniform1f(p->alpha, 1.f);+ glUniform1f(p->opaque, 0.f);+ glUniform1f(p->saturation, 1.f);+ glUniform4f(p->tint, 0, 0, 0, 0);+ glUniform4f(p->shadow, 0, 0, 1, 0);+ glUniform1i(p->tex, 0);+}+// A quad in shape-local device pixels; (x, y, w, h) is also the piece's content rectangle.+static void spike_quad(struct spike_program *p, const struct spike_shape *shape, float x, float y,+ float w, float h)+{+ glUniform4f(p->dst, shape->x + x, shape->y + y, w, h);+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);+}+static void spike_begin(struct spike_target *t)+{+ glBindFramebuffer(GL_FRAMEBUFFER, t->fbo);+ glViewport(0, 0, t->width, t->height);+ // Premultiplied "over": the same blend wlroots' own pass uses. The scissor+ // is the caller's: a partial repaint keeps it across every piece.+ glEnable(GL_BLEND);+ glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);+}++// A solid piece of the window body: rect (x, y, w, h) in shape-local pixels.+static void spike_solid(struct spike *spike, struct spike_target *t, const struct spike_shape *shape,+ float border, const float *bcolor, const float color[4], int x, int y, int w,+ int h)+{+ struct spike_program *p = &spike->plain;+ spike_set_common(spike, p, t, shape, border, bcolor);+ glUniform1f(p->mode, 1.f);+ glUniform4f(p->color, color[0], color[1], color[2], color[3]);+ spike_quad(p, shape, x, y, w, h);+}+// A textured piece: the whole texture (or `src`, in uv) is stretched over (x, y, w, h).+static void spike_textured(struct spike *spike, struct spike_target *t,+ const struct spike_shape *shape, float border, const float *bcolor,+ const struct spike_texture *tex, const float src[4], float alpha, int x,+ int y, int w, int h)+{+ struct spike_program *p = spike_program_for(spike, tex->target);+ spike_set_common(spike, p, t, shape, border, bcolor);+ glUniform1f(p->mode, 0.f);+ glUniform4f(p->content, x, y, w, h);+ glUniform4f(p->src, src[0], src[1], src[2], src[3]);+ glUniform1f(p->alpha, alpha);+ glUniform1f(p->opaque, tex->opaque);+ glActiveTexture(GL_TEXTURE0);+ glBindTexture(tex->target, tex->id);+ glTexParameteri(tex->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);+ glTexParameteri(tex->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);+ glTexParameteri(tex->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);+ glTexParameteri(tex->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);+ spike_quad(p, shape, x, y, w, h);+ glBindTexture(tex->target, 0);+}+// Shadow behind the shape: drawn over a quad that reaches 3 sigma past it.+static void spike_shadow(struct spike *spike, struct spike_target *t, const struct spike_shape *shape,+ float offset_x, float offset_y, float sigma, const float color[4],+ bool clip_inside)+{+ struct spike_program *p = &spike->plain;+ spike_set_common(spike, p, t, shape, 0, NULL);+ glUniform1f(p->mode, 3.f);+ glUniform4f(p->color, color[0], color[1], color[2], color[3]);+ glUniform4f(p->shadow, offset_x, offset_y, sigma, clip_inside ? 1.f : 0.f);+ int pad = (int)ceilf(3 * sigma) + (int)ceilf(fmaxf(fabsf(offset_x), fabsf(offset_y)));+ spike_quad(p, shape, -pad, -pad, shape->w + 2 * pad, shape->h + 2 * pad);+}++// ---------------------------------------------------------------------------+// Backdrop effect: copy the framebuffer under the shape, blur it at quarter+// size in two separable passes, composite through the clip. Replaces glass.c's+// prefix re-render of the scene into a private buffer.+struct spike_backdrop {+ GLuint copy, a, b;+ GLuint fbo_a, fbo_b;+ int copy_w, copy_h, small_w, small_h;+};+static GLuint spike_texture_new(int w, int h)+{+ GLuint id;+ glGenTextures(1, &id);+ glBindTexture(GL_TEXTURE_2D, id);+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);+ return id;+}+static void spike_backdrop_init(struct spike_backdrop *b, int copy_w, int copy_h)+{+ b->copy_w = copy_w;+ b->copy_h = copy_h;+ b->small_w = (copy_w + 3) / 4;+ b->small_h = (copy_h + 3) / 4;+ b->copy = spike_texture_new(copy_w, copy_h);+ b->a = spike_texture_new(b->small_w, b->small_h);+ b->b = spike_texture_new(b->small_w, b->small_h);+ glGenFramebuffers(1, &b->fbo_a);+ glBindFramebuffer(GL_FRAMEBUFFER, b->fbo_a);+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, b->a, 0);+ glGenFramebuffers(1, &b->fbo_b);+ glBindFramebuffer(GL_FRAMEBUFFER, b->fbo_b);+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, b->b, 0);+}+static void spike_backdrop_destroy(struct spike_backdrop *b)+{+ glDeleteTextures(1, &b->copy);+ glDeleteTextures(1, &b->a);+ glDeleteTextures(1, &b->b);+ glDeleteFramebuffers(1, &b->fbo_a);+ glDeleteFramebuffers(1, &b->fbo_b);+}+struct spike_blur_params {+ float sigma; // device px+ float saturation;+ float tint[4]; // rgb, mix amount+ float alpha;+ int padding; // capture margin around the shape, device px+ float border; // device px; the ring is drawn in the composite pass+ float border_color[4];+};+// Returns false when the capture could not be taken.+static void spike_backdrop_draw(struct spike *spike, struct spike_target *t,+ struct spike_backdrop *b, const struct spike_shape *shape,+ const struct spike_blur_params *params)+{+ // 1. Copy the region the blur reads, clamped to the framebuffer. Texels+ // that fall outside are replicated by CLAMP_TO_EDGE, never black.+ int x0 = shape->x - params->padding, y0 = shape->y - params->padding;+ int x1 = x0 + b->copy_w, y1 = y0 + b->copy_h;+ int cx0 = x0 < 0 ? 0 : x0, cy0 = y0 < 0 ? 0 : y0;+ int cx1 = x1 > t->width ? t->width : x1, cy1 = y1 > t->height ? t->height : y1;+ // The caller's scissor limits what is repainted, not what is read or blurred.+ const GLboolean scissored = glIsEnabled(GL_SCISSOR_TEST);+ glDisable(GL_SCISSOR_TEST);+ glBindFramebuffer(GL_FRAMEBUFFER, t->fbo);+ glActiveTexture(GL_TEXTURE0);+ glBindTexture(GL_TEXTURE_2D, b->copy);+ glCopyTexSubImage2D(GL_TEXTURE_2D, 0, cx0 - x0, cy0 - y0, cx0, cy0, cx1 - cx0, cy1 - cy0);+ // 2. Separable blur at quarter size: horizontal into a, vertical into b.+ glDisable(GL_BLEND);+ glUseProgram(spike->blur.id);+ glBindBuffer(GL_ARRAY_BUFFER, spike->vertices);+ const GLfloat clip_quad[] = {-1, -1, 1, -1, -1, 1, 1, 1};+ GLuint clip_vbo;+ glGenBuffers(1, &clip_vbo);+ glBindBuffer(GL_ARRAY_BUFFER, clip_vbo);+ glBufferData(GL_ARRAY_BUFFER, sizeof(clip_quad), clip_quad, GL_STREAM_DRAW);+ glEnableVertexAttribArray(0);+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);+ glUniform1i(spike->blur.tex, 0);+ float sigma_small = params->sigma / 4.f;+ glUniform1f(spike->blur.sigma, sigma_small);+ float lx = (float)(cx0 - x0) / b->copy_w + .5f / b->copy_w;+ float ly = (float)(cy0 - y0) / b->copy_h + .5f / b->copy_h;+ float hx = (float)(cx1 - x0) / b->copy_w - .5f / b->copy_w;+ float hy = (float)(cy1 - y0) / b->copy_h - .5f / b->copy_h;+ glViewport(0, 0, b->small_w, b->small_h);+ glBindFramebuffer(GL_FRAMEBUFFER, b->fbo_a);+ glUniform2f(spike->blur.step_uv, 1.f / b->small_w, 0);+ glUniform4f(spike->blur.limits, lx, ly, hx, hy);+ glBindTexture(GL_TEXTURE_2D, b->copy);+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);+ glBindFramebuffer(GL_FRAMEBUFFER, b->fbo_b);+ glUniform2f(spike->blur.step_uv, 0, 1.f / b->small_h);+ glUniform4f(spike->blur.limits, 0.f, 0.f, 1.f, 1.f);+ glBindTexture(GL_TEXTURE_2D, b->a);+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);+ glDeleteBuffers(1, &clip_vbo);+ // 3. Composite through the clip.+ spike_begin(t);+ if (scissored)+ glEnable(GL_SCISSOR_TEST);+ struct spike_program *p = &spike->plain;+ spike_set_common(spike, p, t, shape, params->border, params->border_color);+ glUniform1f(p->mode, 4.f);+ glUniform4f(p->content, -params->padding, -params->padding, b->copy_w, b->copy_h);+ glUniform4f(p->src, 0, 0, 1, 1);+ glUniform1f(p->saturation, params->saturation);+ glUniform4f(p->tint, params->tint[0], params->tint[1], params->tint[2], params->tint[3]);+ glUniform1f(p->alpha, params->alpha);+ glBindTexture(GL_TEXTURE_2D, b->b);+ spike_quad(p, shape, 0, 0, shape->w, shape->h);+ glBindTexture(GL_TEXTURE_2D, 0);+}++// ---------------------------------------------------------------------------+// Readback in image order (row 0 = top) for the tests.+static void spike_read(struct spike_target *t, uint8_t *rgba)+{+ glBindFramebuffer(GL_FRAMEBUFFER, t->fbo);+ glPixelStorei(GL_PACK_ALIGNMENT, 1);+ glReadPixels(0, 0, t->width, t->height, GL_RGBA, GL_UNSIGNED_BYTE, rgba);+}+static void spike_clear(struct spike_target *t, const float color[4])+{+ glBindFramebuffer(GL_FRAMEBUFFER, t->fbo);+ glDisable(GL_SCISSOR_TEST);+ glClearColor(color[0], color[1], color[2], color[3]);+ glClear(GL_COLOR_BUFFER_BIT);+}diff --git a/src/pass_spike_test.c b/src/pass_spike_test.cnew file mode 100644index 0000000..9118138--- /dev/null+++ b/src/pass_spike_test.c@@ -0,0 +1,643 @@+// Pixel gates for the stage 1 pass spike (docs/plans/scene-graph.md); run with+// `zig build test-pass-spike` on a GLES2 host. Each gate is a criterion the+// plan names: no seams or fringe at any scale or zoom, correct premultiplied+// edges, an external-OES texture identical to a 2D one, buffer-age damage+// correct under a backdrop effect, and cost within the stage 0 baseline.+#include "pass_spike.c"+#include <stdarg.h>+#include <wlr/backend/headless.h>+#include <wlr/render/dmabuf.h>+#include <wlr/render/pass.h>++static const float BG[4] = {224 / 255.f, 225 / 255.f, 232 / 255.f, 1};+static const float BODY[4] = {16 / 255.f, 18 / 255.f, 21 / 255.f, 1};+static const float SCALES[] = {1.f, 1.5f, 2.f};+static const float ZOOMS[] = {1.f, .85f, .70f, .55f, .25f};++static void check(bool ok, const char *what, ...)+{+ if (ok)+ return;+ char text[256];+ va_list args;+ va_start(args, what);+ vsnprintf(text, sizeof(text), what, args);+ va_end(args);+ fprintf(stderr, "FAIL: %s\n", text);+ exit(1);+}++struct image {+ int w, h;+ uint8_t *p;+};+static uint8_t *at(const struct image *img, int x, int y) { return img->p + 4 * (y * img->w + x); }+static void read_image(struct spike_target *t, struct image *img)+{+ img->w = t->width;+ img->h = t->height;+ img->p = realloc(img->p, 4 * img->w * img->h);+ spike_read(t, img->p);+}+static int diff(int a, int b) { return abs(a - b); }++// The snapping rule AGENTS.md states: a node's device box is round(off*s) to+// round((off+len)*s), never round(len*s), so neighbours share an edge.+static int snap(double v) { return (int)lround(v); }+struct boxes {+ struct spike_shape shape;+ int title_h, footer_h;+};+static struct boxes window_boxes(float scale, float zoom, float radius_logical)+{+ const float k = scale * zoom;+ struct boxes b;+ int x0 = snap(30 * k), y0 = snap(24 * k);+ b.shape.x = x0 + 8;+ b.shape.y = y0 + 8;+ b.shape.w = snap((30 + 200) * k) - x0;+ b.shape.h = snap((24 + 140) * k) - y0;+ for (int i = 0; i < 4; i++)+ b.shape.radii[i] = radius_logical * k;+ // Title and footer boundaries come from the same snapping as the shape.+ b.title_h = snap((24 + 36) * k) - y0;+ b.footer_h = snap((24 + 140) * k) - snap((24 + 140 - 10) * k);+ return b;+}++// ---- Gate 1: the clip against a supersampled reference -----------------------------+static bool inside_rrect(double px, double py, const struct spike_shape *s)+{+ double x = px - s->x, y = py - s->y;+ if (x < 0 || y < 0 || x > s->w || y > s->h)+ return false;+ double r = (x < s->w / 2.) ? (y < s->h / 2. ? s->radii[0] : s->radii[3])+ : (y < s->h / 2. ? s->radii[1] : s->radii[2]);+ double cx = x < s->w / 2. ? r : s->w - r, cy = y < s->h / 2. ? r : s->h - r;+ bool corner = (x < r || x > s->w - r) && (y < r || y > s->h - r);+ if (!corner)+ return true;+ return hypot(x - cx, y - cy) <= r;+}+static double reference_coverage(const struct spike_shape *s, int x, int y)+{+ const int n = 8;+ int hits = 0;+ for (int j = 0; j < n; j++)+ for (int i = 0; i < n; i++)+ hits += inside_rrect(x + (i + .5) / n, y + (j + .5) / n, s);+ return (double)hits / (n * n);+}+static void gate_clip_accuracy(struct spike *spike, struct spike_target *t, struct image *img)+{+ int worst = 0, fringe = 0;+ for (size_t si = 0; si < sizeof(SCALES) / sizeof(*SCALES); si++)+ for (size_t zi = 0; zi < sizeof(ZOOMS) / sizeof(*ZOOMS); zi++) {+ struct boxes b = window_boxes(SCALES[si], ZOOMS[zi], 10);+ spike_clear(t, BG);+ spike_begin(t);+ spike_solid(spike, t, &b.shape, 0, NULL, BODY, 0, 0, b.shape.w, b.shape.h);+ read_image(t, img);+ for (int y = b.shape.y - 2; y < b.shape.y + b.shape.h + 2; y++)+ for (int x = b.shape.x - 2; x < b.shape.x + b.shape.w + 2; x++) {+ double c = reference_coverage(&b.shape, x, y);+ const uint8_t bg[3] = {224, 225, 232}, body[3] = {16, 18, 21};+ for (int ch = 0; ch < 3; ch++) {+ double expect = bg[ch] * (1 - c) + body[ch] * c;+ int got = at(img, x, y)[ch];+ if (diff(got, (int)lround(expect)) > worst)+ worst = diff(got, (int)lround(expect));+ // No fringe: never outside the range of the two colours.+ int lo = body[ch] < bg[ch] ? body[ch] : bg[ch];+ int hi = body[ch] < bg[ch] ? bg[ch] : body[ch];+ if (got < lo - 1 || got > hi + 1)+ fringe++;+ }+ }+ }+ check(fringe == 0, "gate 1: %d channel values fall outside the bg/body range (fringe)", fringe);+ // Distance-field AA is the formula glass.c already uses; the bound documents+ // how far it sits from true area coverage (6% of the 208-level range).+ check(worst <= 14, "gate 1: clip differs from supersampled coverage by %d levels", worst);+ printf("PASS gate 1: rounded clip within %d levels of area coverage, no fringe, "+ "%zu scales x %zu zoom steps\n",+ worst, sizeof(SCALES) / sizeof(*SCALES), sizeof(ZOOMS) / sizeof(*ZOOMS));+}++// ---- Gate 2: pieces of one window share the clip, with no seam ---------------------+static void gate_no_seams(struct spike *spike, struct spike_target *t, struct image *img)+{+ const float red[4] = {1, 0, 0, 1}, green[4] = {0, 1, 0, 1}, blue[4] = {0, 0, 1, 1};+ for (size_t si = 0; si < sizeof(SCALES) / sizeof(*SCALES); si++)+ for (size_t zi = 0; zi < sizeof(ZOOMS) / sizeof(*ZOOMS); zi++) {+ struct boxes b = window_boxes(SCALES[si], ZOOMS[zi], 10);+ struct spike_shape *s = &b.shape;+ spike_clear(t, BG);+ spike_begin(t);+ spike_solid(spike, t, s, 0, NULL, red, 0, 0, s->w, b.title_h);+ spike_solid(spike, t, s, 0, NULL, green, 0, b.title_h, s->w, s->h - b.title_h - b.footer_h);+ spike_solid(spike, t, s, 0, NULL, blue, 0, s->h - b.footer_h, s->w, b.footer_h);+ read_image(t, img);+ int margin = (int)ceilf(s->radii[0]) + 2;+ for (int y = 0; y < s->h; y++)+ for (int x = margin; x < s->w - margin; x++) {+ const float *want = y < b.title_h ? red : y < s->h - b.footer_h ? green : blue;+ uint8_t *px = at(img, s->x + x, s->y + y);+ for (int ch = 0; ch < 3; ch++)+ check(diff(px[ch], (int)lround(want[ch] * 255)) <= 1,+ "gate 2: seam at scale %.2f zoom %.2f, row %d col %d channel %d = %d",+ SCALES[si], ZOOMS[zi], y, x, ch, px[ch]);+ }+ }+ puts("PASS gate 2: titlebar, client and footer pieces meet without a seam at every scale and zoom");+}++// ---- Gate 3: border and body partition the shape's coverage -------------------------+static void gate_border_partition(struct spike *spike, struct spike_target *t, struct image *img,+ struct image *ref)+{+ int worst = 0;+ for (size_t si = 0; si < sizeof(SCALES) / sizeof(*SCALES); si++)+ for (size_t zi = 0; zi < sizeof(ZOOMS) / sizeof(*ZOOMS); zi++) {+ struct boxes b = window_boxes(SCALES[si], ZOOMS[zi], 10);+ struct spike_shape *s = &b.shape;+ float border = fmaxf(1.f, roundf(1.f * SCALES[si] * ZOOMS[zi]));+ // Ring and body in one colour, in three pieces: the result must equal+ // a plain body, whatever the split between them, or coverage leaks.+ spike_clear(t, BG);+ spike_begin(t);+ spike_solid(spike, t, s, 0, NULL, BODY, 0, 0, s->w, s->h);+ read_image(t, ref);+ spike_clear(t, BG);+ spike_begin(t);+ spike_solid(spike, t, s, border, BODY, BODY, 0, 0, s->w, b.title_h);+ spike_solid(spike, t, s, border, BODY, BODY, 0, b.title_h, s->w, s->h - b.title_h - b.footer_h);+ spike_solid(spike, t, s, border, BODY, BODY, 0, s->h - b.footer_h, s->w, b.footer_h);+ read_image(t, img);+ for (int i = 0; i < img->w * img->h * 4; i++)+ if (diff(img->p[i], ref->p[i]) > worst)+ worst = diff(img->p[i], ref->p[i]);+ // And the ring really is the ring: its own colour on the outer row of a+ // straight edge, the body colour just inside it.+ const float ring[4] = {1, 0, 0, 1};+ spike_clear(t, BG);+ spike_begin(t);+ spike_solid(spike, t, s, border, ring, BODY, 0, 0, s->w, s->h);+ read_image(t, img);+ int cx = s->x + s->w / 2;+ check(at(img, cx, s->y)[0] >= 254 && at(img, cx, s->y)[1] <= 1,+ "gate 3: outer row is not the border colour at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ check(diff(at(img, cx, s->y + (int)border)[0], 16) <= 1,+ "gate 3: row inside the border is not the body at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ }+ check(worst <= 2, "gate 3: border + body differ from one shape by %d levels", worst);+ printf("PASS gate 3: border ring and body, evaluated in one pass, add up to exactly the shape's coverage (max diff %d)\n", worst);+}++// ---- Gate 4: premultiplied texture edges and opaque (XRGB) textures ------------------+static GLuint texture_2d(int w, int h, const uint8_t *rgba)+{+ GLuint id;+ glGenTextures(1, &id);+ glBindTexture(GL_TEXTURE_2D, id);+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba);+ return id;+}+static void gate_premultiplied(struct spike *spike, struct spike_target *t, struct image *img)+{+ // 50% red, premultiplied: (64, 0, 0, 128) is a quarter-intensity red over 50% alpha.+ uint8_t half[4 * 4 * 4];+ for (int i = 0; i < 16; i++)+ memcpy(half + 4 * i, (uint8_t[]){128, 0, 0, 128}, 4);+ struct spike_texture tex = {GL_TEXTURE_2D, texture_2d(4, 4, half), false};+ struct spike_shape s = {40, 40, 80, 80, {0, 0, 0, 0}};+ const float src[4] = {0, 0, 1, 1};+ spike_clear(t, BG);+ spike_begin(t);+ spike_textured(spike, t, &s, 0, NULL, &tex, src, 1.f, 0, 0, 80, 80);+ read_image(t, img);+ uint8_t *px = at(img, 80, 80);+ // premultiplied "over": 128 + 224*(1-.50196) = 239.5 red; 0 + 225*.498 = 112; 232*.498 = 115.5+ check(diff(px[0], 240) <= 2 && diff(px[1], 112) <= 2 && diff(px[2], 116) <= 2,+ "gate 4: premultiplied half-transparent texel gave (%d,%d,%d)", px[0], px[1], px[2]);+ // Opacity: alpha .5 on an opaque white texel is a 50% mix with the backdrop.+ uint8_t white[4] = {255, 255, 255, 255};+ struct spike_texture w = {GL_TEXTURE_2D, texture_2d(1, 1, white), false};+ spike_clear(t, (float[4]){0, 0, 0, 1});+ spike_begin(t);+ spike_textured(spike, t, &s, 0, NULL, &w, src, .5f, 0, 0, 80, 80);+ read_image(t, img);+ check(diff(at(img, 80, 80)[0], 128) <= 2, "gate 4: opacity .5 gave %d", at(img, 80, 80)[0]);+ // XRGB: the unused alpha byte must be ignored, not drawn as transparency.+ uint8_t junk[4] = {10, 200, 30, 0};+ struct spike_texture x = {GL_TEXTURE_2D, texture_2d(1, 1, junk), true};+ spike_clear(t, BG);+ spike_begin(t);+ spike_textured(spike, t, &s, 0, NULL, &x, src, 1.f, 0, 0, 80, 80);+ read_image(t, img);+ px = at(img, 80, 80);+ check(diff(px[0], 10) <= 1 && diff(px[1], 200) <= 1 && diff(px[2], 30) <= 1,+ "gate 4: XRGB texel drawn as (%d,%d,%d)", px[0], px[1], px[2]);+ glDeleteTextures(1, &tex.id);+ glDeleteTextures(1, &w.id);+ glDeleteTextures(1, &x.id);+ puts("PASS gate 4: premultiplied edges, opacity and XRGB textures blend correctly");+}++// ---- Gate 5: an external-OES texture equals the same pixels as a 2D texture ----------+static void gate_external(struct spike *spike, struct spike_target *t, struct image *img,+ struct image *ref)+{+ if (!spike->has_external) {+ puts("SKIP gate 5: no GL_OES_EGL_image_external");+ return;+ }+ // A dmabuf-backed buffer holding a pattern (drawn through its own FBO), imported+ // twice: as an EGLImage bound to an external texture, and read back into a 2D one.+ struct spike_target src;+ check(spike_target_create(spike, &src, 64, 64), "gate 5: cannot allocate the source dmabuf");+ glBindFramebuffer(GL_FRAMEBUFFER, src.fbo);+ glEnable(GL_SCISSOR_TEST);+ const float colors[4][4] = {{1, 0, 0, 1}, {0, 1, 0, 1}, {0, 0, 1, 1}, {1, 1, 0, 1}};+ for (int i = 0; i < 4; i++) {+ glScissor((i % 2) * 32, (i / 2) * 32, 32, 32);+ glClearColor(colors[i][0], colors[i][1], colors[i][2], colors[i][3]);+ glClear(GL_COLOR_BUFFER_BIT);+ }+ glDisable(GL_SCISSOR_TEST);+ struct image pattern = {0};+ struct spike_target src_view = src;+ read_image(&src_view, &pattern);+ struct wlr_dmabuf_attributes dma;+ check(wlr_buffer_get_dmabuf(src.buffer, &dma), "gate 5: the buffer is not a dmabuf");+ EGLint attribs[32];+ int n = 0;+ attribs[n++] = EGL_WIDTH, attribs[n++] = dma.width;+ attribs[n++] = EGL_HEIGHT, attribs[n++] = dma.height;+ attribs[n++] = EGL_LINUX_DRM_FOURCC_EXT, attribs[n++] = dma.format;+ attribs[n++] = EGL_DMA_BUF_PLANE0_FD_EXT, attribs[n++] = dma.fd[0];+ attribs[n++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT, attribs[n++] = dma.offset[0];+ attribs[n++] = EGL_DMA_BUF_PLANE0_PITCH_EXT, attribs[n++] = dma.stride[0];+ if (dma.modifier != DRM_FORMAT_MOD_INVALID) {+ attribs[n++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, attribs[n++] = dma.modifier & 0xffffffff;+ attribs[n++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, attribs[n++] = dma.modifier >> 32;+ }+ attribs[n++] = EGL_NONE;+ struct wlr_egl *egl = wlr_gles2_renderer_get_egl(spike->renderer);+ EGLImageKHR image = spike->create_image(wlr_egl_get_display(egl), EGL_NO_CONTEXT,+ EGL_LINUX_DMA_BUF_EXT, NULL, attribs);+ check(image != EGL_NO_IMAGE_KHR, "gate 5: eglCreateImageKHR failed (0x%x)", eglGetError());+ struct spike_texture ext = {GL_TEXTURE_EXTERNAL_OES, 0, false};+ glGenTextures(1, &ext.id);+ glBindTexture(GL_TEXTURE_EXTERNAL_OES, ext.id);+ spike->image_target_texture(GL_TEXTURE_EXTERNAL_OES, image);+ struct spike_texture flat = {GL_TEXTURE_2D, texture_2d(64, 64, pattern.p), false};+ const float uv[4] = {0, 0, 1, 1};+ int worst = 0;+ for (size_t si = 0; si < sizeof(SCALES) / sizeof(*SCALES); si++)+ for (size_t zi = 0; zi < sizeof(ZOOMS) / sizeof(*ZOOMS); zi++) {+ struct boxes b = window_boxes(SCALES[si], ZOOMS[zi], 10);+ struct spike_texture *draws[2] = {&flat, &ext};+ struct image *outs[2] = {ref, img};+ for (int k = 0; k < 2; k++) {+ spike_clear(t, BG);+ spike_begin(t);+ spike_textured(spike, t, &b.shape, 0, NULL, draws[k], uv, 1.f, 0, 0, b.shape.w, b.shape.h);+ read_image(t, outs[k]);+ }+ for (int i = 0; i < img->w * img->h * 4; i++)+ if (diff(img->p[i], ref->p[i]) > worst)+ worst = diff(img->p[i], ref->p[i]);+ }+ check(worst <= 2, "gate 5: external texture differs from the 2D one by %d levels", worst);+ glDeleteTextures(1, &ext.id);+ glDeleteTextures(1, &flat.id);+ spike->destroy_image(wlr_egl_get_display(egl), image);+ wlr_buffer_drop(src.buffer);+ free(pattern.p);+ printf("PASS gate 5: external-OES texture through the clip matches the 2D path (max diff %d)\n", worst);+}++// ---- Gate 6: analytic shadow ----------------------------------------------------------+static double erfc_ref(double x) { return erfc(x); }+static void gate_shadow(struct spike *spike, struct spike_target *t, struct image *img)+{+ const float white[4] = {1, 1, 1, 1};+ const float black_half[4] = {0, 0, 0, .5f};+ struct spike_shape s = {80, 60, 200, 120, {0, 0, 0, 0}};+ const float sigma = 12.f, oy = 6.f;+ spike_clear(t, white);+ spike_begin(t);+ spike_shadow(spike, t, &s, 0, oy, sigma, black_half, true);+ read_image(t, img);+ int x = s.x + s.w / 2, bottom = s.y + s.h;+ int 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;+ double expect = 255 * (1 - .5 * .5 * erfc_ref(d / (sigma * 1.41421356237)));+ int got = at(img, x, bottom + dy)[0];+ worst = diff(got, (int)lround(expect)) > worst ? diff(got, (int)lround(expect)) : worst;+ check(got >= last, "gate 6: shadow is not monotonic at %d px (%d after %d)", dy, got, last);+ last = got;+ }+ check(worst <= 3, "gate 6: shadow profile differs from the Gaussian edge by %d levels", worst);+ check(at(img, 10, 10)[0] == 255 && at(img, x, s.y + s.h / 2)[0] == 255,+ "gate 6: shadow reaches outside its footprint or inside the window");+ printf("PASS gate 6: analytic shadow follows the Gaussian edge (max diff %d), clipped out of the window\n",+ worst);+}++// ---- Gate 7: backdrop blur from a framebuffer copy -----------------------------------+static void gate_backdrop(struct spike *spike, struct spike_target *t, struct image *img)+{+ const int pad = 60;+ struct spike_shape s = {100, 100, 200, 80, {12, 12, 12, 12}};+ struct spike_blur_params params = {.sigma = 12, .saturation = 1, .tint = {0, 0, 0, 0}, .alpha = 1,+ .padding = pad};+ struct spike_backdrop b;+ spike_backdrop_init(&b, s.w + 2 * pad, s.h + 2 * pad);+ // Left half red, right half blue.+ spike_clear(t, (float[4]){1, 0, 0, 1});+ glBindFramebuffer(GL_FRAMEBUFFER, t->fbo);+ glEnable(GL_SCISSOR_TEST);+ glScissor(200, 0, t->width - 200, t->height);+ glClearColor(0, 0, 1, 1);+ glClear(GL_COLOR_BUFFER_BIT);+ glDisable(GL_SCISSOR_TEST);+ spike_backdrop_draw(spike, t, &b, &s, ¶ms);+ read_image(t, img);+ int mid = 200, y = s.y + s.h / 2;+ uint8_t *left = at(img, s.x + 12, y), *center = at(img, mid, y), *right = at(img, s.x + s.w - 12, y);+ check(left[0] >= 250 && left[2] <= 5, "gate 7: far-left backdrop is (%d,%d,%d), expected red", left[0], left[1], left[2]);+ check(right[2] >= 250 && right[0] <= 5, "gate 7: far-right backdrop is (%d,%d,%d), expected blue", right[0], right[1], right[2]);+ check(diff(center[0], 128) <= 8 && diff(center[2], 128) <= 8,+ "gate 7: blur centre is (%d,%d,%d), expected an even mix", center[0], center[1], center[2]);+ int last = 256;+ for (int x = mid - 40; x <= mid + 40; x++) {+ int r = at(img, x, y)[0];+ check(r <= last, "gate 7: blurred edge is not monotonic at x=%d", x);+ last = r;+ }+ check(at(img, 50, y)[0] == 255 && at(img, 340, y)[2] == 255, "gate 7: pixels outside the clip changed");+ // Nothing drawn outside the shape: the row above is the unblurred backdrop.+ check(at(img, mid - 20, s.y - 2)[0] == 255 && at(img, mid + 20, s.y - 2)[2] == 255,+ "gate 7: the effect leaked past its clip");+ spike_backdrop_destroy(&b);+ puts("PASS gate 7: backdrop copy, two-pass blur and clipped composite behave as a blur of what is below");+}+// Edge handling: a window at the bottom of the framebuffer samples a replicated+// edge, not black from beyond it.+static void gate_backdrop_edge(struct spike *spike, struct spike_target *t, struct image *img)+{+ const int pad = 60;+ struct spike_shape s = {100, t->height - 60, 200, 60, {0, 0, 0, 0}};+ struct spike_blur_params params = {.sigma = 12, .saturation = 1, .tint = {0, 0, 0, 0}, .alpha = 1, .padding = pad};+ struct spike_backdrop b;+ spike_backdrop_init(&b, s.w + 2 * pad, s.h + 2 * pad);+ spike_clear(t, (float[4]){0, 0, 1, 1});+ spike_backdrop_draw(spike, t, &b, &s, ¶ms);+ read_image(t, img);+ for (int y = s.y; y < t->height; y += 7)+ for (int x = s.x; x < s.x + s.w; x += 9)+ check(at(img, x, y)[2] >= 250 && at(img, x, y)[0] <= 5,+ "gate 7b: edge blur bled black at (%d,%d): (%d,%d,%d)", x, y, at(img, x, y)[0],+ at(img, x, y)[1], at(img, x, y)[2]);+ spike_backdrop_destroy(&b);+ puts("PASS gate 7b: a blur at the framebuffer edge replicates the edge, no black bleed");+}++// ---- Gate 8: buffer-age damage under a backdrop effect --------------------------------+struct scene {+ struct spike_shape shape;+ struct spike_backdrop backdrop;+ struct spike_blur_params params;+ int square_x;+};+static void draw_scene(struct spike *spike, struct spike_target *t, struct scene *sc,+ const int *scissor)+{+ glBindFramebuffer(GL_FRAMEBUFFER, t->fbo);+ // GL window y runs with the buffer's rows here (row 0 is the top), so an+ // image-space rectangle is a scissor rectangle as it stands.+ if (scissor) {+ glEnable(GL_SCISSOR_TEST);+ glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);+ } else {+ glDisable(GL_SCISSOR_TEST);+ }+ glClearColor(0, 0, 0, 1);+ glClear(GL_COLOR_BUFFER_BIT);+ spike_begin(t);+ struct spike_shape full = {0, 0, t->width, t->height, {0, 0, 0, 0}};+ const float bands[4][4] = {{.9f, .2f, .1f, 1}, {.1f, .8f, .2f, 1}, {.1f, .2f, .9f, 1}, {.9f, .9f, .1f, 1}};+ for (int i = 0; i < 4; i++)+ spike_solid(spike, t, &full, 0, NULL, bands[i], i * t->width / 4, 0, t->width / 4, t->height);+ // A small white square that moves between frames.+ struct spike_shape square = {sc->square_x, sc->shape.y - 30, 24, 24, {0, 0, 0, 0}};+ spike_solid(spike, t, &square, 0, NULL, (float[4]){1, 1, 1, 1}, 0, 0, 24, 24);+ spike_backdrop_draw(spike, t, &sc->backdrop, &sc->shape, &sc->params);+ glDisable(GL_SCISSOR_TEST);+}+static void gate_damage(struct spike *spike, struct spike_target *t, struct spike_target *t2,+ struct image *img, struct image *ref)+{+ struct scene sc = {.shape = {120, 120, 300, 100, {10, 10, 10, 10}},+ .params = {.sigma = 10, .saturation = 1, .tint = {0, 0, 0, 0}, .alpha = 1, .padding = 40,+ .border = 1, .border_color = {1, 1, 1, 1}}};+ spike_backdrop_init(&sc.backdrop, sc.shape.w + 80, sc.shape.h + 80);+ // Frame 1: the square sits above the window's left part; frame 2 moves it+ // within the blur's reach (3 sigma = 30 px) but outside the shape itself.+ sc.square_x = 150;+ draw_scene(spike, t, &sc, NULL);+ int old_x = sc.square_x;+ sc.square_x = 260;+ int square_y = sc.shape.y - 30;+ // Damage: both positions of the square.+ int dx0 = old_x, dx1 = sc.square_x + 24;+ int damage[4] = {dx0, square_y, dx1 - dx0, 24};+ // Reference: frame 2 rendered from scratch.+ draw_scene(spike, t2, &sc, NULL);+ read_image(t2, ref);+ // Without expanding the damage to the effect, the blur under it is stale.+ draw_scene(spike, t, &sc, damage);+ read_image(t, img);+ int stale = 0;+ for (int i = 0; i < img->w * img->h * 4; i++)+ if (diff(img->p[i], ref->p[i]) > 2)+ stale++;+ check(stale > 0, "gate 8: the unexpanded damage should leave a stale blur (control)");+ // Expand: damage within the effect's sample extent (shape + padding) pulls in the shape.+ int ex0 = sc.shape.x - sc.params.padding, ey0 = sc.shape.y - sc.params.padding;+ int ex1 = sc.shape.x + sc.shape.w + sc.params.padding, ey1 = sc.shape.y + sc.shape.h + sc.params.padding;+ bool touches = damage[0] < ex1 && damage[0] + damage[2] > ex0 && damage[1] < ey1 && damage[1] + damage[3] > ey0;+ check(touches, "gate 8: the test damage must reach the effect's sample extent");+ int x0 = damage[0] < sc.shape.x ? damage[0] : sc.shape.x, y0 = damage[1] < sc.shape.y ? damage[1] : sc.shape.y;+ int x1 = damage[0] + damage[2] > sc.shape.x + sc.shape.w ? damage[0] + damage[2] : sc.shape.x + sc.shape.w;+ int y1 = damage[1] + damage[3] > sc.shape.y + sc.shape.h ? damage[1] + damage[3] : sc.shape.y + sc.shape.h;+ int expanded[4] = {x0, y0, x1 - x0, y1 - y0};+ // Reset to the frame 1 content, then repaint with the expanded damage.+ sc.square_x = old_x;+ draw_scene(spike, t, &sc, NULL);+ sc.square_x = 260;+ draw_scene(spike, t, &sc, expanded);+ read_image(t, img);+ int bad = 0;+ for (int i = 0; i < img->w * img->h * 4; i++)+ if (diff(img->p[i], ref->p[i]) > 2)+ bad++;+ check(bad == 0, "gate 8: %d values differ after painting only the expanded damage", bad);+ spike_backdrop_destroy(&sc.backdrop);+ printf("PASS gate 8: partial repaint with damage expanded under the effect equals a full repaint "+ "(the unexpanded one leaves %d stale values)\n", stale);+}++// ---- Gate 10: the same buffer, drawn by wlroots' own pass and by the spike ------------+static void gate_matches_wlroots(struct spike *spike, struct spike_gl_state *state,+ struct spike_target *t, struct image *img, struct image *ref)+{+ // A premultiplied half-transparent rectangle off the origin: any flip of the+ // image, wrong blend or colour-space difference between the two shows up.+ const float tint[4] = {.2f, .1f, .05f, .5f};+ // wlroots draws with client-side vertex arrays, which read from the bound+ // GL_ARRAY_BUFFER if there is one: handing it a context with the spike's+ // quad buffer still bound draws half a rectangle. leave() restores what+ // was bound before the spike, as glass.c's does.+ spike_leave(state);+ struct wlr_render_pass *pass = wlr_renderer_begin_buffer_pass(spike->renderer, t->buffer, NULL);+ check(pass != NULL, "gate 10: cannot begin a wlroots pass");+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = {0, 0, t->width, t->height},+ .color = {BG[0], BG[1], BG[2], 1},+ .blend_mode = WLR_RENDER_BLEND_MODE_NONE});+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = {40, 30, 100, 60},+ .color = {tint[0], tint[1], tint[2], tint[3]}});+ check(wlr_render_pass_submit(pass), "gate 10: the wlroots pass failed");+ check(spike_enter(spike, state), "gate 10: cannot re-enter the context after a wlroots pass");+ read_image(t, ref);+ struct spike_shape s = {40, 30, 100, 60, {0, 0, 0, 0}};+ spike_clear(t, BG);+ spike_begin(t);+ spike_solid(spike, t, &s, 0, NULL, tint, 0, 0, 100, 60);+ read_image(t, img);+ int worst = 0;+ for (int i = 0; i < img->w * img->h * 4; i++)+ worst = diff(img->p[i], ref->p[i]) > worst ? diff(img->p[i], ref->p[i]) : worst;+ check(worst <= 1, "gate 10: the spike and wlroots' pass differ by %d levels", worst);+ check(at(ref, 41, 31)[0] != at(ref, 41, 100)[0] || at(ref, 41, 31)[1] != at(ref, 41, 100)[1],+ "gate 10: the control rectangle was not drawn where expected");+ printf("PASS gate 10: same orientation, blend and colour as wlroots' own render pass (max diff %d)\n", worst);+}++// ---- Gate 9: cost ---------------------------------------------------------------------+static double now_ms(void)+{+ struct timespec ts;+ clock_gettime(CLOCK_MONOTONIC, &ts);+ return ts.tv_sec * 1e3 + ts.tv_nsec / 1e6;+}+static void gate_cost(struct spike *spike)+{+ // Window B's titlebar effect at scale 1.5, as in the stage 0 baseline:+ // 522x46 logical -> 783x69 device, sigma 22 logical (33 device), 3 sigma padding.+ struct spike_target t;+ check(spike_target_create(spike, &t, 1920, 1080), "gate 9: cannot allocate 1920x1080");+ struct spike_shape effect = {300, 200, 783, 69, {15, 15, 0, 0}};+ struct spike_blur_params params = {.sigma = 33, .saturation = 1.5f, .tint = {.06f, .07f, .08f, .5f},+ .alpha = 1, .padding = 99};+ struct spike_backdrop b;+ spike_backdrop_init(&b, effect.w + 2 * params.padding, effect.h + 2 * params.padding);+ struct spike_shape window = {300, 200, 783, 534, {15, 15, 15, 15}};+ const float colors[3][4] = {{.06f, .07f, .08f, .8f}, {.06f, .07f, .08f, .8f}, {.06f, .07f, .08f, .8f}};+ const int iterations = 300;+ double effect_cpu = 0, effect_wall = 0, window_cpu = 0, window_wall = 0;+ for (int round = 0; round < 2; round++) {+ spike_clear(&t, BG);+ glFinish();+ double start = now_ms(), cpu = 0;+ for (int i = 0; i < iterations; i++) {+ double c0 = now_ms();+ spike_backdrop_draw(spike, &t, &b, &effect, ¶ms);+ cpu += now_ms() - c0;+ }+ glFinish();+ if (round) {+ effect_cpu = cpu / iterations;+ effect_wall = (now_ms() - start) / iterations;+ }+ start = now_ms();+ cpu = 0;+ for (int i = 0; i < iterations; i++) {+ double c0 = now_ms();+ spike_begin(&t);+ spike_shadow(spike, &t, &window, 0, 6, 12, (float[4]){0, 0, 0, .4f}, true);+ const float ring[4] = {.3f, .3f, .3f, 1};+ spike_solid(spike, &t, &window, 1, ring, colors[0], 0, 0, window.w, 69);+ spike_solid(spike, &t, &window, 1, ring, colors[1], 0, 69, window.w, window.h - 69 - 15);+ spike_solid(spike, &t, &window, 1, ring, colors[2], 0, window.h - 15, window.w, 15);+ cpu += now_ms() - c0;+ }+ glFinish();+ if (round) {+ window_cpu = cpu / iterations;+ window_wall = (now_ms() - start) / iterations;+ }+ }+ printf("COST one backdrop effect (783x69 at scale 1.5): CPU submit %.3f ms, with GPU %.3f ms per frame\n",+ effect_cpu, effect_wall);+ printf("COST one window (shadow, 3 pieces, border): CPU submit %.3f ms, with GPU %.3f ms per frame\n",+ window_cpu, window_wall);+ // Stage 0 baseline, one re-blurred effect (spinner under glass vs clear, scale 1.5):+ // +0.17 ms CPU and +0.2 ms GPU per frame.+ check(effect_cpu <= 0.17 * 1.5, "gate 9: effect CPU %.3f ms exceeds the baseline's 0.17 ms by more than 50%%", effect_cpu);+ check(effect_wall <= (0.17 + 0.2) * 1.5, "gate 9: effect wall %.3f ms exceeds the baseline's 0.37 ms by more than 50%%", effect_wall);+ puts("PASS gate 9: a backdrop effect costs no more than glass.c's re-blur in the stage 0 baseline");+ spike_backdrop_destroy(&b);+ wlr_buffer_drop(t.buffer);+}++int main(void)+{+ wlr_log_init(WLR_ERROR, NULL);+ struct wl_display *display = wl_display_create();+ struct wlr_backend *backend = wlr_headless_backend_create(wl_display_get_event_loop(display));+ assert(backend);+ struct wlr_renderer *renderer = wlr_renderer_autocreate(backend);+ assert(renderer && wlr_renderer_is_gles2(renderer));+ struct wlr_allocator *allocator = wlr_allocator_autocreate(backend, renderer);+ assert(allocator);+ struct spike *spike = spike_create(renderer, allocator);+ check(spike != NULL, "the spike's programs failed to build");+ struct spike_gl_state state;+ check(spike_enter(spike, &state), "cannot enter the renderer's context");++ struct spike_target t, t2;+ check(spike_target_create(spike, &t, 640, 480) && spike_target_create(spike, &t2, 640, 480),+ "cannot allocate the 640x480 targets");+ struct image img = {0}, ref = {0};+ gate_clip_accuracy(spike, &t, &img);+ gate_no_seams(spike, &t, &img);+ gate_border_partition(spike, &t, &img, &ref);+ gate_premultiplied(spike, &t, &img);+ gate_external(spike, &t, &img, &ref);+ gate_shadow(spike, &t, &img);+ gate_backdrop(spike, &t, &img);+ gate_backdrop_edge(spike, &t, &img);+ gate_damage(spike, &t, &t2, &img, &ref);+ gate_matches_wlroots(spike, &state, &t, &img, &ref);+ gate_cost(spike);++ spike_leave(&state);+ free(img.p);+ free(ref.p);+ wlr_buffer_drop(t.buffer);+ wlr_buffer_drop(t2.buffer);+ wlr_allocator_destroy(allocator);+ wlr_renderer_destroy(renderer);+ wlr_backend_destroy(backend);+ wl_display_destroy(display);+ puts("pass spike gates passed");+ return 0;+}