diff --git a/include/sway/desktop/fx_renderer.h b/include/sway/desktop/fx_renderer.h index 3620a5b2..e78146fa 100644 --- a/include/sway/desktop/fx_renderer.h +++ b/include/sway/desktop/fx_renderer.h @@ -92,7 +92,7 @@ struct fx_renderer { GLint position; GLint size; GLint blur_sigma; - GLint alpha; + GLint corner_radius; } box_shadow; struct gles2_tex_shader tex_rgba; diff --git a/sway/desktop/fx_renderer.c b/sway/desktop/fx_renderer.c index 23867cb3..f5c86480 100644 --- a/sway/desktop/fx_renderer.c +++ b/sway/desktop/fx_renderer.c @@ -243,7 +243,7 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) { renderer->shaders.box_shadow.position = glGetUniformLocation(prog, "position"); renderer->shaders.box_shadow.size = glGetUniformLocation(prog, "size"); renderer->shaders.box_shadow.blur_sigma = glGetUniformLocation(prog, "blur_sigma"); - renderer->shaders.box_shadow.alpha = glGetUniformLocation(prog, "alpha"); + renderer->shaders.box_shadow.corner_radius = glGetUniformLocation(prog, "corner_radius"); // fragment shaders prog = link_program(common_vert_src, tex_rgba_frag_src); @@ -564,7 +564,8 @@ void fx_render_border_corner(struct fx_renderer *renderer, const struct wlr_box // TODO: alpha input arg? void fx_render_box_shadow(struct fx_renderer *renderer, const struct wlr_box *box, - const float color[static 4], const float projection[static 9], int radius, float blur_sigma) { + const float color[static 4], const float projection[static 9], + int corner_radius, float blur_sigma) { if (box->width == 0 || box->height == 0) { return; } @@ -590,6 +591,7 @@ void fx_render_box_shadow(struct fx_renderer *renderer, const struct wlr_box *bo glUniform3f(renderer->shaders.box_shadow.color, color[0], color[1], color[2]); glUniform1f(renderer->shaders.box_shadow.alpha, color[3]); glUniform1f(renderer->shaders.box_shadow.blur_sigma, blur_sigma); + glUniform1f(renderer->shaders.box_shadow.corner_radius, corner_radius); glUniform2f(renderer->shaders.box_shadow.size, box->width, box->height); glUniform2f(renderer->shaders.box_shadow.position, box->x, box->y); diff --git a/sway/desktop/render.c b/sway/desktop/render.c index 60e51108..d37ce36e 100644 --- a/sway/desktop/render.c +++ b/sway/desktop/render.c @@ -331,7 +331,8 @@ damage_finish: // _box.x and .y are expected to be layout-local // _box.width and .height are expected to be output-buffer-local void render_box_shadow(struct sway_output *output, pixman_region32_t *output_damage, - const struct wlr_box *_box, const float color[static 4]) { + const struct wlr_box *_box, const float color[static 4], + float blur_sigma, float corner_radius) { struct wlr_output *wlr_output = output->wlr_output; struct fx_renderer *renderer = output->server->renderer; @@ -354,7 +355,8 @@ void render_box_shadow(struct sway_output *output, pixman_region32_t *output_dam pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects); for (int i = 0; i < nrects; ++i) { scissor_output(wlr_output, &rects[i]); - fx_render_box_shadow(renderer, &box, color, wlr_output->transform_matrix, 0, 0.3); + fx_render_box_shadow(renderer, &box, color, wlr_output->transform_matrix, + corner_radius, blur_sigma); } damage_finish: @@ -1000,10 +1002,17 @@ static void render_containers_linear(struct sway_output *output, } // render shadow - const float color[4] = {0,0,0,0.7}; - struct wlr_box box = {state->x, state->y, state->width, state->height}; + const float color[4] = {0.0, 0.0, 0.0, 0.5}; + // Mimic how a CSS box-shadow works by using blur_sigma as the shadow width + float blur_sigma = 20; + struct wlr_box box = { + state->x - blur_sigma, + state->y - blur_sigma, + state->width + blur_sigma * 2, + state->height + blur_sigma * 2 + }; scale_box(&box, output->wlr_output->scale); - render_box_shadow(output, damage, &box, color); + render_box_shadow(output, damage, &box, color, blur_sigma, deco_data.corner_radius); } else { render_container(output, damage, child, parent->focused || child->current.focused); diff --git a/sway/desktop/shaders/box_shadow.frag b/sway/desktop/shaders/box_shadow.frag index 34393519..be385742 100644 --- a/sway/desktop/shaders/box_shadow.frag +++ b/sway/desktop/shaders/box_shadow.frag @@ -7,21 +7,52 @@ uniform vec3 color; uniform vec2 position; uniform vec2 size; uniform float blur_sigma; -uniform float alpha; +uniform float corner_radius; + +float gaussian(float x, float sigma) { + const float pi = 3.141592653589793; + return exp(-(x * x) / (2.0 * sigma * sigma)) / (sqrt(2.0 * pi) * sigma); +} // approximates the error function, needed for the gaussian integral -vec4 erf(vec4 x) { - vec4 s = sign(x), a = abs(x); +vec2 erf(vec2 x) { + vec2 s = sign(x), a = abs(x); x = 1.0 + (0.278393 + (0.230389 + 0.078108 * (a * a)) * a) * a; x *= x; return s - s / (x * x); } +// return the blurred mask along the x dimension +float roundedBoxShadowX(float x, float y, float sigma, float corner, vec2 halfSize) { + float delta = min(halfSize.y - corner - abs(y), 0.0); + float curved = halfSize.x - corner + sqrt(max(0.0, corner * corner - delta * delta)); + vec2 integral = 0.5 + 0.5 * erf((x + vec2(-curved, curved)) * (sqrt(0.5) / sigma)); + return integral.y - integral.x; +} + // return the mask for the shadow of a box from lower to upper -float box_shadow(vec2 lower, vec2 upper, vec2 point, float sigma) { - vec4 query = vec4(point - lower, point - upper); - vec4 integral = 0.5 + 0.5 * erf(query * (sqrt(0.5) / sigma)); - return (integral.z - integral.x) * (integral.w - integral.y); +float roundedBoxShadow(vec2 lower, vec2 upper, vec2 point, float sigma, float corner_radius) { + // Center everything to make the math easier + vec2 center = (lower + upper) * 0.5; + vec2 halfSize = (upper - lower) * 0.5; + point -= center; + + // The signal is only non-zero in a limited range, so don't waste samples + float low = point.y - halfSize.y; + float high = point.y + halfSize.y; + float start = clamp(-3.0 * sigma, low, high); + float end = clamp(3.0 * sigma, low, high); + + // Accumulate samples (we can get away with surprisingly few samples) + float step = (end - start) / 4.0; + float y = start + step * 0.5; + float value = 0.0; + for (int i = 0; i < 4; i++) { + value += roundedBoxShadowX(point.x, point.y - y, sigma, corner_radius, halfSize) * gaussian(y, sigma) * step; + y += step; + } + + return value; } // per-pixel "random" number between 0 and 1 @@ -30,10 +61,15 @@ float random() { } void main() { - float frag_alpha = alpha * box_shadow(position, position + size, gl_FragCoord.xy, blur_sigma); + float frag_alpha = v_color.a * roundedBoxShadow( + position + blur_sigma, + position + size - blur_sigma, + gl_FragCoord.xy, blur_sigma * 0.5, + corner_radius); // dither the alpha to break up color bands frag_alpha += (random() - 0.5) / 128.0; gl_FragColor = vec4(color, frag_alpha); } +