RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
scene graph plan, stage 5b: backdrop primitives in the owned pass
oxydizer9cede81ace35
diff --git a/docs/plans/scene-graph-stage5.md b/docs/plans/scene-graph-stage5.mdindex 1f76d39..9a03011 100644--- a/docs/plans/scene-graph-stage5.md+++ b/docs/plans/scene-graph-stage5.md@@ -1,6 +1,6 @@# Scene graph plan, stage 5: backdrop effects in the pass-Status: **5a done, 5b next**, 2026-10-11. Decisions taken (all as recommended). Stage 4 of+Status: **5a and 5b done, 5c next**, 2026-10-11. Decisions taken (all as recommended). Stage 4 of[scene-graph.md](scene-graph.md) is done (window shapes, analytic shadows, theowned pass as the only presenter). This stage moves glass, the blurred backdropbehind titlebars, the taskbar and the start and power menus, from its side@@ -147,6 +147,18 @@ once, after 5e (see [scene-graph-stage4.md](scene-graph-stage4.md), Measurement)result equals `glass.c`'s finish for the same input within tolerance; edgereplication at the buffer edge; a partial repaint with damage expansion equalsa full repaint (spike gate 8); no seam between composite and target.+ *Done.* `rediwm_scene_renderer_enable_backdrops`, `rediwm_scene_pass_capture_backdrop`+ (copy into an RGB scratch texture, so an alpha-less scanout buffer can be+ copied; horizontal pass at quarter size into scratch, vertical into the+ caller's `rediwm_backdrop`), `rediwm_scene_pass_add_backdrop` (shaped program+ mode 3: saturation, coverage, the front's alpha made up to the coverage) and+ `rediwm_scene_renderer_destroy_backdrop`. Gates: within **1 level** of an exact+ full-resolution Gaussian on ARGB and XRGB buffers; a step mixes evenly and+ monotonically; the buffer edge replicates; saturation matches glass.c's+ formula; the front's alpha is made up exactly, and an opaque front gets+ nothing; a clipped composite equals the full one. The shaped programs' `front`+ sampler is fixed to unit 1 at link time: two sampler types on unit 0 (the+ external program's) made every shaped draw invalid.* **5c. Output pass integration behind `glass.c`.** `scene_output.c` draws effectitems itself: un-cover the padded extent below an effect while collecting,expand damage per D1, keep the per-effect, per-output cache, apply holds per D2.diff --git a/src/scene_pass.c b/src/scene_pass.cindex b70ed90..b49b35b 100644--- a/src/scene_pass.c+++ b/src/scene_pass.c@@ -23,6 +23,14 @@ struct program {GLint 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, softness, hide;+ // The backdrop composite (shaped mode 3) and the blur program.+ GLint bmap, fmap, saturation, front, front_alpha, has_front;+ GLint step_uv, sigma, limits, src_scale;+};+struct rediwm_backdrop {+ GLuint tex, fbo;+ int width, height; // quarter size+ struct wlr_box region; // device pixels the blur covers};struct attrib_state {GLint enabled, size, type, normalized, stride, buffer;@@ -45,6 +53,12 @@ struct rediwm_scene_renderer {// a 2D texture, a solid colour and the shape's shadow (they differ in a mode// uniform, which every fragment of a draw reads the same), one for external.struct program shaped, shaped_external;+ // Backdrops (stage 5b), built by enable_backdrops: the blur, and scratch the+ // size of the largest region so far: the copy, and the horizontal pass.+ struct program blur;+ GLuint copy_tex, small_tex, small_fbo;+ int copy_w, copy_h, small_w, small_h;+ bool has_backdrops, backdrops_tried;bool active, has_external, has_sync, has_vao, has_shapes, shapes_tried;GLuint vertex_array, vertices;PFNGLGENVERTEXARRAYSOESPROC gen_vertex_arrays;@@ -179,7 +193,8 @@ 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: the+// `mode` 0 draws the texture, 1 the solid colour, 3 a backdrop composite (see+// add_backdrop), 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@@ -187,7 +202,15 @@ static const char shaped_vertex[] =// 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, softness, hide; uniform vec2 offset;"+ "uniform sampler2D front; uniform vec4 bmap, fmap; uniform float saturation, front_alpha, has_front;""void main(){"+ // 3: a backdrop composite (stage 5b). `tex` is the kept blur; the alpha is+ // chosen against the surface drawn over it, so the pair has the coverage.+ "if(mode>2.5){ vec3 rgb=texture2D(tex,(local+bmap.xy)*bmap.zw).rgb;"+ "float l=dot(rgb,vec3(.213,.715,.072)); rgb=clamp(vec3(l)+(rgb-vec3(l))*saturation,0.,1.);"+ "float c=cov(sdf(local,radii))*alpha;"+ "float f=has_front>.5?texture2D(front,(local+fmap.xy)*fmap.zw).a*front_alpha:0.;"+ "float a=f>=.99999?0.:clamp((c-f)/(1.-f),0.,1.); gl_FragColor=vec4(rgb*a,a); return; }""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; }"@@ -195,6 +218,21 @@ static const char shaped_fragment[] = SHAPED_HEAD"float w=border>0.?1.-cov(d+border):0.;""gl_FragColor=(c*(1.-bcolor.a*w)+bcolor*w)*cov(d); }";+// The backdrop blur (stage 5b): glass.c's kernel, 49 taps, sigma in texels of the+// target, reading a source texture that may be larger than the region (scratch),+// clamped to the texels that were really copied, so an edge replicates.+static const char blur_vertex[] =+ "attribute vec2 position; varying vec2 uv;"+ "void main(){ uv=position; gl_Position=vec4(position*2.-1.,0.,1.); }";+static const char blur_fragment[] =+ "#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\n"+ "varying vec2 uv; uniform sampler2D tex;"+ "uniform vec2 step_uv, src_scale; uniform float sigma; uniform vec4 limits;"+ "void main(){ vec4 sum=vec4(0.); float weight=0.; vec2 at=uv*src_scale;"+ "for(int i=-24;i<=24;i++){ float x=float(i); float w=exp(-.5*x*x/(sigma*sigma));"+ "sum+=texture2D(tex,clamp(at+x*step_uv,limits.xy,limits.zw))*w; weight+=w; }"+ "gl_FragColor=vec4(sum.rgb/weight,1.); }";+static GLuint shader(GLenum type, const char *prefix, const char *source) {GLuint id = glCreateShader(type);if (!id) return 0;@@ -254,6 +292,22 @@ static bool program_init(struct program *p, unsigned bit, const char *vertex_soup->offset = glGetUniformLocation(p->id, "offset");p->softness = glGetUniformLocation(p->id, "softness");p->hide = glGetUniformLocation(p->id, "hide");+ p->bmap = glGetUniformLocation(p->id, "bmap");+ p->fmap = glGetUniformLocation(p->id, "fmap");+ p->saturation = glGetUniformLocation(p->id, "saturation");+ p->front = glGetUniformLocation(p->id, "front");+ p->front_alpha = glGetUniformLocation(p->id, "front_alpha");+ p->has_front = glGetUniformLocation(p->id, "has_front");+ p->step_uv = glGetUniformLocation(p->id, "step_uv");+ p->sigma = glGetUniformLocation(p->id, "sigma");+ p->limits = glGetUniformLocation(p->id, "limits");+ p->src_scale = glGetUniformLocation(p->id, "src_scale");+ // Samplers default to unit 0, and two of different types there (the external+ // program's `tex` and `front`) make every draw invalid: `front` is unit 1.+ if (p->front >= 0) {+ glUseProgram(p->id);+ glUniform1i(p->front, 1);+ }return true;}static const char ext_prefix[] =@@ -314,6 +368,10 @@ void rediwm_scene_renderer_destroy(struct rediwm_scene_renderer *r) {glDeleteProgram(r->external.id);glDeleteProgram(r->shaped.id);glDeleteProgram(r->shaped_external.id);+ glDeleteProgram(r->blur.id);+ glDeleteTextures(1, &r->copy_tex);+ glDeleteTextures(1, &r->small_tex);+ glDeleteFramebuffers(1, &r->small_fbo);leave(r, &state);}free(r);@@ -590,3 +648,180 @@ void rediwm_scene_pass_add_shadow(struct wlr_render_pass *base,glUniform1f(g->hide, shadow->hide_inside ? 1.f : 0.f);draw(shadow->clip);}++// ---- Backdrops (stage 5b) -------------------------------------------------------+bool rediwm_scene_renderer_enable_backdrops(struct rediwm_scene_renderer *r) {+ assert(!r->active);+ if (r->backdrops_tried) return r->has_backdrops;+ r->backdrops_tried = true;+ if (!r->has_shapes) return false;+ struct gl_state state;+ if (!enter(r, &state)) return false;+ r->has_backdrops = program_init(&r->blur, 32, blur_vertex, "", blur_fragment);+ if (!r->has_backdrops) {+ wlr_log(WLR_ERROR, "scene pass: blur program failed to build, glass is a plain tint");+ glDeleteProgram(r->blur.id);+ r->blur.id = 0;+ }+ leave(r, &state);+ return r->has_backdrops;+}+static void texture_storage(GLuint tex, GLenum format, int w, int h) {+ glBindTexture(GL_TEXTURE_2D, tex);+ glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, 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);+}+static bool attach(GLuint fbo, GLuint tex) {+ glBindFramebuffer(GL_FRAMEBUFFER, fbo);+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);+ return glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;+}+// Scratch grows to the largest region seen; it is shared by every effect.+static bool scratch(struct rediwm_scene_renderer *r, int copy_w, int copy_h, int small_w, int small_h) {+ if (!r->copy_tex) glGenTextures(1, &r->copy_tex);+ if (!r->small_tex) glGenTextures(1, &r->small_tex);+ if (!r->small_fbo) glGenFramebuffers(1, &r->small_fbo);+ if (copy_w > r->copy_w || copy_h > r->copy_h) {+ r->copy_w = copy_w > r->copy_w ? copy_w : r->copy_w;+ r->copy_h = copy_h > r->copy_h ? copy_h : r->copy_h;+ // RGB: a scanout buffer may have no alpha, and GLES2 copies an alpha-less+ // framebuffer only into a texture without one. The backdrop is opaque.+ texture_storage(r->copy_tex, GL_RGB, r->copy_w, r->copy_h);+ }+ if (small_w > r->small_w || small_h > r->small_h) {+ r->small_w = small_w > r->small_w ? small_w : r->small_w;+ r->small_h = small_h > r->small_h ? small_h : r->small_h;+ texture_storage(r->small_tex, GL_RGBA, r->small_w, r->small_h);+ if (!attach(r->small_fbo, r->small_tex)) return false;+ }+ return true;+}+void rediwm_scene_renderer_destroy_backdrop(struct rediwm_scene_renderer *r, struct rediwm_backdrop *b) {+ if (!b) return;+ assert(!r->active);+ struct gl_state state;+ if (enter(r, &state)) {+ glDeleteTextures(1, &b->tex);+ glDeleteFramebuffers(1, &b->fbo);+ leave(r, &state);+ }+ free(b);+}+void rediwm_scene_pass_capture_backdrop(struct wlr_render_pass *base, struct rediwm_backdrop **out,+ struct wlr_box region, float sigma) {+ struct pass *p = owned_pass(base);+ struct rediwm_scene_renderer *r = p->renderer;+ if (p->failed) return;+ if (!r->has_backdrops || region.width <= 0 || region.height <= 0 || !isfinite(sigma) ||+ sigma <= 0 || sigma > 1024 || region.width > 16384 || region.height > 16384) {+ p->failed = true;+ return;+ }+ struct rediwm_backdrop *b = *out;+ if (!b) {+ b = calloc(1, sizeof(*b));+ if (!b) { p->failed = true; return; }+ glGenTextures(1, &b->tex);+ glGenFramebuffers(1, &b->fbo);+ *out = b;+ }+ const int small_w = (region.width + 3) / 4, small_h = (region.height + 3) / 4;+ if (b->width != small_w || b->height != small_h) {+ texture_storage(b->tex, GL_RGBA, small_w, small_h);+ if (!attach(b->fbo, b->tex)) { p->failed = true; return; }+ b->width = small_w;+ b->height = small_h;+ }+ b->region = region;+ if (!scratch(r, region.width, region.height, small_w, small_h)) { p->failed = true; return; }+ // 1. Copy what lies in the buffer; texels outside are clamped away below.+ const int x0 = region.x, y0 = region.y;+ const int cx0 = x0 < 0 ? 0 : x0, cy0 = y0 < 0 ? 0 : y0;+ const int cx1 = x0 + region.width > p->buffer->width ? p->buffer->width : x0 + region.width;+ const int cy1 = y0 + region.height > p->buffer->height ? p->buffer->height : y0 + region.height;+ if (cx1 <= cx0 || cy1 <= cy0) { p->failed = true; return; }+ GLuint pass_fbo = wlr_gles2_renderer_get_buffer_fbo(r->renderer, p->buffer);+ glBindFramebuffer(GL_FRAMEBUFFER, pass_fbo);+ glDisable(GL_SCISSOR_TEST);+ glDisable(GL_BLEND);+ glActiveTexture(GL_TEXTURE0);+ glBindTexture(GL_TEXTURE_2D, r->copy_tex);+ glCopyTexSubImage2D(GL_TEXTURE_2D, 0, cx0 - x0, cy0 - y0, cx0, cy0, cx1 - cx0, cy1 - cy0);+ // 2. Horizontal at quarter size into scratch, vertical into the backdrop.+ struct program *g = &r->blur;+ glUseProgram(g->id);+ glUniform1i(g->sampler, 0);+ const float cw = r->copy_w, ch = r->copy_h, aw = r->small_w, ah = r->small_h;+ glBindFramebuffer(GL_FRAMEBUFFER, r->small_fbo);+ glViewport(0, 0, small_w, small_h);+ glUniform2f(g->src_scale, region.width / cw, region.height / ch);+ glUniform2f(g->step_uv, (float)region.width / small_w / cw, 0);+ glUniform1f(g->sigma, sigma * small_w / region.width);+ glUniform4f(g->limits, (cx0 - x0 + .5f) / cw, (cy0 - y0 + .5f) / ch,+ (cx1 - x0 - .5f) / cw, (cy1 - y0 - .5f) / ch);+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);+ glBindFramebuffer(GL_FRAMEBUFFER, b->fbo);+ glBindTexture(GL_TEXTURE_2D, r->small_tex);+ glUniform2f(g->src_scale, small_w / aw, small_h / ah);+ glUniform2f(g->step_uv, 0, 1.f / ah);+ glUniform1f(g->sigma, sigma * small_h / region.height);+ glUniform4f(g->limits, .5f / aw, .5f / ah, (small_w - .5f) / aw, (small_h - .5f) / ah);+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);+ // Back to the pass: its buffer, viewport and blending; the next draw sets the+ // program and the scissor again.+ glBindFramebuffer(GL_FRAMEBUFFER, pass_fbo);+ glViewport(0, 0, p->buffer->width, p->buffer->height);+ p->program = 0;+ p->blend_mode = -1;+}+void rediwm_scene_pass_add_backdrop(struct wlr_render_pass *base, const struct rediwm_pass_shape *shape,+ struct wlr_box box, const struct rediwm_pass_backdrop *o) {+ struct pass *p = owned_pass(base);+ struct program *g = &p->renderer->shaped;+ if (p->failed || (o->clip && !pixman_region32_not_empty(o->clip))) return;+ const struct rediwm_backdrop *b = o->backdrop;+ if (!g->id || !shape_usable(shape) || !b || !b->width || !isfinite(o->saturation) ||+ !isfinite(o->opacity) || !isfinite(o->front_opacity)) {+ p->failed = true;+ return;+ }+ struct wlr_gles2_texture_attribs front = {0};+ if (o->front) {+ if (!wlr_texture_is_gles2(o->front) || o->front_box.width <= 0 || o->front_box.height <= 0) {+ p->failed = true;+ return;+ }+ wlr_gles2_texture_get_attribs(o->front, &front);+ }+ const bool has_front = o->front && front.target == GL_TEXTURE_2D;+ setup(p, g, box, WLR_RENDER_BLEND_MODE_PREMULTIPLIED);+ struct rediwm_pass_shape bare = *shape; // the ring belongs to the surface over it+ bare.border = 0;+ set_shape(g, &bare);+ glUniform1f(g->mode, 3.f);+ glUniform1f(g->alpha, o->opacity);+ glUniform1f(g->saturation, o->saturation);+ glUniform4f(g->bmap, shape->box.x - b->region.x, shape->box.y - b->region.y,+ 1.f / b->region.width, 1.f / b->region.height);+ glActiveTexture(GL_TEXTURE1);+ glBindTexture(GL_TEXTURE_2D, has_front ? front.tex : 0);+ if (has_front) {+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);+ }+ glUniform1f(g->has_front, has_front ? 1.f : 0.f);+ glUniform1f(g->front_alpha, o->front_opacity);+ glUniform4f(g->fmap, shape->box.x - o->front_box.x, shape->box.y - o->front_box.y,+ has_front ? 1.f / o->front_box.width : 0, has_front ? 1.f / o->front_box.height : 0);+ glActiveTexture(GL_TEXTURE0);+ glBindTexture(GL_TEXTURE_2D, b->tex);+ glUniform1i(g->sampler, 0);+ draw(o->clip);+ glActiveTexture(GL_TEXTURE1);+ glBindTexture(GL_TEXTURE_2D, 0);+ glActiveTexture(GL_TEXTURE0);+}+diff --git a/src/scene_pass.h b/src/scene_pass.hindex 65b5995..ca236a6 100644--- a/src/scene_pass.h+++ b/src/scene_pass.h@@ -72,3 +72,39 @@ void rediwm_scene_pass_add_texture_shaped(struct wlr_render_pass *,// 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 *);++// Stage 5b of docs/plans/scene-graph.md: backdrop effects (glass). An effect+// blurs what the pass has drawn so far under a region and composites it through+// its shape, under the surface it backs. The blur is kept between frames in a+// `rediwm_backdrop`, owned by the caller, so an effect is re-blurred only when+// what lies below it changed (stage 5, D1); compositing a kept blur is cheap.+struct rediwm_backdrop;+// Builds the blur program (about 140 KiB), outside a pass. Requires+// rediwm_scene_renderer_enable_shapes, whose program composites. Later calls+// return the first result; without it, backdrop calls fail the pass.+bool rediwm_scene_renderer_enable_backdrops(struct rediwm_scene_renderer *);+// Frees a backdrop's GL resources; outside a pass. NULL is a no-op.+void rediwm_scene_renderer_destroy_backdrop(struct rediwm_scene_renderer *, struct rediwm_backdrop *);+// Copies `region` (device pixels of this pass's buffer; the part outside the+// buffer replicates its edge) as drawn so far and blurs it at quarter size with+// a Gaussian of `sigma` device pixels into `*backdrop`, creating it if NULL. Not+// limited by any clip: the caller must have drawn everything below the effect+// in the whole region first (with buffer age, that means damaging all of it).+void rediwm_scene_pass_capture_backdrop(struct wlr_render_pass *, struct rediwm_backdrop **,+ struct wlr_box region, float sigma);+struct rediwm_pass_backdrop {+ const struct rediwm_backdrop *backdrop;+ float saturation; // 1 leaves colours as they are+ float opacity; // the effect's, times the shape's coverage+ // The surface drawn over the effect, whose alpha the effect makes up to the+ // coverage (its alpha a: the effect draws (coverage - a) / (1 - a)), so the pair+ // is exactly as opaque as the shape and a curved edge is not covered twice.+ // NULL when nothing is drawn over it.+ struct wlr_texture *front;+ struct wlr_box front_box; // where `front` is drawn, whole texture, untransformed+ float front_opacity;+ const pixman_region32_t *clip;+};+// Composites `backdrop` through `shape` over `box` (device pixels).+void rediwm_scene_pass_add_backdrop(struct wlr_render_pass *, const struct rediwm_pass_shape *,+ struct wlr_box box, const struct rediwm_pass_backdrop *);diff --git a/src/scene_pass_test.c b/src/scene_pass_test.cindex 5c68c15..d8db26e 100644--- a/src/scene_pass_test.c+++ b/src/scene_pass_test.c@@ -905,6 +905,172 @@ static void shaped_gates(struct rediwm_scene_renderer *r, struct wlr_allocator *free(ref.p);wlr_buffer_drop(buf);}+// ---- Stage 5b: backdrops ------------------------------------------------------------+static struct wlr_buffer *target_format(struct wlr_allocator *allocator, int w, int h, uint32_t fmt) {+ uint64_t modifier = DRM_FORMAT_MOD_INVALID;+ struct wlr_drm_format format = {.format = fmt, .len = 1, .capacity = 1, .modifiers = &modifier};+ struct wlr_buffer *b = wlr_allocator_create_buffer(allocator, w, h, &format);+ assert(b);+ return b;+}+static const float BANDS[5][4] = {{1, 0, 0, 1}, {0, .8f, 0, 1}, {0, 0, 1, 1}, {1, 1, 1, 1}, {.1f, .1f, .1f, 1}};+// Vertical bands 40 px wide, then a backdrop of `region` composited over `box`.+static struct rediwm_backdrop *draw_backdrop(struct rediwm_scene_renderer *r, struct wlr_buffer *buf,+ struct rediwm_backdrop *b, struct wlr_box region, struct wlr_box box, float sigma,+ const struct rediwm_pass_backdrop *opts, const pixman_region32_t *clip) {+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ for (int x = 0, i = 0; x < buf->width; x += 40, i++)+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = {x, 0, 40, buf->height}, .color = {BANDS[i % 5][0], BANDS[i % 5][1], BANDS[i % 5][2], 1}});+ rediwm_scene_pass_capture_backdrop(pass, &b, region, sigma);+ struct rediwm_pass_backdrop o = opts ? *opts : (struct rediwm_pass_backdrop){.saturation = 1, .opacity = 1};+ o.backdrop = b;+ o.clip = clip;+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box, &o);+ finish(pass);+ return b;+}+// The exact full-resolution Gaussian of the bands, clamped at the buffer edge, as+// the reference the quarter-size blur approximates.+static double band_channel(int x, int width, int c) {+ x = x < 0 ? 0 : x >= width ? width - 1 : x;+ return BANDS[(x / 40) % 5][c];+}+static void gate_backdrop_blur(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {+ const float sigma = 16;+ const struct wlr_box region = {100, 100, 400, 200}, box = {148, 148, 304, 104};+ int worst = 0;+ for (int f = 0; f < 2; f++) {+ struct wlr_buffer *buf = target_format(allocator, GW, GH, f ? DRM_FORMAT_XRGB8888 : DRM_FORMAT_ARGB8888);+ struct rediwm_backdrop *b = draw_backdrop(r, buf, NULL, region, box, sigma, NULL, NULL);+ read_image(r, buf, img);+ for (int x = box.x; x < box.x + box.width; x++) {+ double want[3] = {0};+ double weight = 0;+ for (int k = -96; k <= 96; k++) {+ const double w = exp(-.5 * k * k / (sigma * sigma));+ // Clamped to the copied region, as the blur's limits are.+ int sx = x + k;+ sx = sx < region.x ? region.x : sx >= region.x + region.width ? region.x + region.width - 1 : sx;+ for (int c = 0; c < 3; c++) want[c] += w * band_channel(sx, GW, c);+ weight += w;+ }+ for (int y = box.y; y < box.y + box.height; y += 13)+ for (int c = 0; c < 3; c++) {+ const int d = diff(at(img, x, y)[c], (int)lround(255 * want[c] / weight));+ if (d > worst) worst = d;+ }+ }+ rediwm_scene_renderer_destroy_backdrop(r, b);+ wlr_buffer_drop(buf);+ }+ check(worst <= 6, "the quarter-size blur differs from the exact Gaussian by %d levels", worst);+ printf("PASS: backdrop blur within %d levels of the exact Gaussian, ARGB and XRGB buffers\n", worst);+}+static void gate_backdrop_properties(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {+ struct wlr_buffer *buf = target_sized(allocator, GW, GH);+ struct rediwm_backdrop *b = NULL;+ // A red/blue step at x = 320 blurs to an even mix there, monotonic across,+ // unchanged far from it.+ struct wlr_render_pass *pass = begin_fill(r, buf, (float[4]){1, 0, 0, 1}, NULL);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {320, 0, 320, GH}, .color = {0, 0, 1, 1}});+ const struct wlr_box region = {120, 100, 400, 200}, box = {168, 148, 304, 104};+ rediwm_scene_pass_capture_backdrop(pass, &b, region, 16);+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1});+ finish(pass);+ read_image(r, buf, img);+ const int y = 200;+ check(diff(at(img, 320, y)[0], 128) <= 12 && diff(at(img, 320, y)[2], 128) <= 12,+ "the step does not blur to an even mix (%d, %d)", at(img, 320, y)[0], at(img, 320, y)[2]);+ for (int x = box.x + 1; x < box.x + box.width; x++)+ check(at(img, x, y)[0] <= at(img, x - 1, y)[0] + 1, "the blurred step is not monotonic at %d", x);+ check(at(img, box.x, y)[0] >= 250 && at(img, box.x + box.width - 1, y)[2] >= 250,+ "the blur reaches too far (%d, %d)", at(img, box.x, y)[0], at(img, box.x + box.width - 1, y)[2]);+ // At the buffer edge the blur replicates it: no black comes in.+ pass = begin_fill(r, buf, (float[4]){0, 1, 0, 1}, NULL);+ const struct wlr_box edge_region = {-60, 100, 200, 200}, edge_box = {0, 148, 92, 104};+ rediwm_scene_pass_capture_backdrop(pass, &b, edge_region, 16);+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = edge_box}, edge_box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1});+ finish(pass);+ read_image(r, buf, img);+ for (int x = 0; x < 92; x++)+ check(at(img, x, 200)[1] >= 253 && at(img, x, 200)[0] <= 2, "the buffer edge darkens the blur at %d", x);+ // Saturation as glass.c applies it, on a uniform colour the blur keeps.+ const float c[3] = {.2f, .4f, .6f};+ pass = begin_fill(r, buf, (float[4]){c[0], c[1], c[2], 1}, NULL);+ rediwm_scene_pass_capture_backdrop(pass, &b, region, 16);+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1.5f, .opacity = 1});+ finish(pass);+ read_image(r, buf, img);+ const double l = .213 * c[0] + .715 * c[1] + .072 * c[2];+ for (int i = 0; i < 3; i++) {+ const int want = (int)lround(255 * (l + (c[i] - l) * 1.5));+ check(diff(at(img, 300, 200)[i], want) <= 2, "saturation: channel %d is %d, not %d", i, at(img, 300, 200)[i], want);+ }+ rediwm_scene_renderer_destroy_backdrop(r, b);+ wlr_buffer_drop(buf);+ puts("PASS: a blurred step mixes evenly and monotonically, the buffer edge replicates, saturation matches glass");+}+static void gate_backdrop_front(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {+ struct wlr_buffer *buf = target_sized(allocator, GW, GH);+ uint32_t half[16 * 16];+ for (int i = 0; i < 16 * 16; i++) half[i] = 0x80000000; // alpha 128, premultiplied black+ struct wlr_texture *front = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_ARGB8888, 16 * 4, 16, 16, half);+ assert(front);+ struct rediwm_backdrop *b = NULL;+ const struct wlr_box region = {100, 100, 200, 200}, box = {150, 150, 100, 100};+ // On a transparent buffer the composite's alpha is what it adds to the front.+ struct wlr_render_pass *pass = begin_fill(r, buf, (float[4]){1, 1, 1, 1}, NULL);+ rediwm_scene_pass_capture_backdrop(pass, &b, region, 8);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {0, 0, GW, GH},+ .color = {0, 0, 0, 0}, .blend_mode = WLR_RENDER_BLEND_MODE_NONE});+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = .5f,+ .front = front, .front_box = box, .front_opacity = .5f});+ finish(pass);+ read_image(r, buf, img);+ const double f = 128 / 255.0 * .5, want = (.5 - f) / (1 - f);+ check(diff(at(img, 200, 200)[3], (int)lround(255 * want)) <= 1, "front alpha: %d, not %d",+ at(img, 200, 200)[3], (int)lround(255 * want));+ // An opaque front leaves nothing for the effect to add.+ pass = begin_fill(r, buf, (float[4]){1, 1, 1, 1}, NULL);+ rediwm_scene_pass_capture_backdrop(pass, &b, region, 8);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {0, 0, GW, GH},+ .color = {0, 0, 0, 0}, .blend_mode = WLR_RENDER_BLEND_MODE_NONE});+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1,+ .front = front, .front_box = box, .front_opacity = 2.f});+ finish(pass);+ read_image(r, buf, img);+ check(at(img, 200, 200)[3] == 0, "an opaque front still gets a backdrop (%d)", at(img, 200, 200)[3]);+ // A clipped composite equals that part of a full one: the copy and the blur+ // ignore the clip.+ struct image full = {0};+ b = draw_backdrop(r, buf, b, (struct wlr_box){100, 100, 400, 200}, (struct wlr_box){148, 148, 304, 104}, 16, NULL, NULL);+ read_image(r, buf, &full);+ pixman_region32_t clip;+ pixman_region32_init_rect(&clip, 148, 148, 150, 104);+ b = draw_backdrop(r, buf, b, (struct wlr_box){100, 100, 400, 200}, (struct wlr_box){148, 148, 304, 104}, 16, NULL, &clip);+ read_image(r, buf, img);+ for (int y = 148; y < 252; y++) for (int x = 148; x < 298; x++) for (int c = 0; c < 4; c++)+ check(at(img, x, y)[c] == at(&full, x, y)[c], "a clipped composite differs at %d,%d", x, y);+ pixman_region32_fini(&clip);+ free(full.p);+ wlr_texture_destroy(front);+ rediwm_scene_renderer_destroy_backdrop(r, b);+ wlr_buffer_drop(buf);+ puts("PASS: the backdrop makes up the front's alpha to the coverage; a clipped composite equals a full one");+}+static void backdrop_gates(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator) {+ struct image img = {0};+ gate_backdrop_blur(r, allocator, &img);+ gate_backdrop_properties(r, allocator, &img);+ gate_backdrop_front(r, allocator, &img);+ free(img.p);+}int main(void) {wlr_log_init(WLR_ERROR, NULL);struct wl_display *display = wl_display_create();@@ -929,6 +1095,9 @@ int main(void) {external_yuv(r, actual, expected);synchronization(r, actual);shaped_gates(r, allocator);+ assert(rediwm_scene_renderer_enable_backdrops(r));+ assert(rediwm_scene_renderer_enable_backdrops(r)); // idempotent+ backdrop_gates(r, allocator);// The ordinary wlroots pass must still work after owned GL has run.parity(r, actual, expected, false);if (r->has_vao) {