summaryrefslogtreecommitdiff
path: root/sway/desktop/shaders
diff options
context:
space:
mode:
authorWilliam McKinnon <[email protected]>2023-01-18 01:49:26 -0500
committerGitHub <[email protected]>2023-01-18 01:49:26 -0500
commit588ea8e290a61a8a98e72617fd679689891e2e39 (patch)
tree128bf22c2f419c793e1a6b84ee03c91af4253629 /sway/desktop/shaders
parent1baba77c74302b8c1f212e851f194deae0fae46b (diff)
feat: Add box shadows (#64)
Co-authored-by: Erik Reider <[email protected]>
Diffstat (limited to 'sway/desktop/shaders')
-rw-r--r--sway/desktop/shaders/box_shadow.frag74
-rw-r--r--sway/desktop/shaders/meson.build1
-rw-r--r--sway/desktop/shaders/quad_round.frag10
3 files changed, 82 insertions, 3 deletions
diff --git a/sway/desktop/shaders/box_shadow.frag b/sway/desktop/shaders/box_shadow.frag
new file mode 100644
index 00000000..c9b2b91f
--- /dev/null
+++ b/sway/desktop/shaders/box_shadow.frag
@@ -0,0 +1,74 @@
+// Writeup: https://madebyevan.com/shaders/fast-rounded-rectangle-shadows/
+
+precision mediump float;
+varying vec4 v_color;
+varying vec2 v_texcoord;
+
+uniform vec2 position;
+uniform vec2 size;
+uniform float blur_sigma;
+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
+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 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
+float random() {
+ return fract(sin(dot(vec2(12.9898, 78.233), gl_FragCoord.xy)) * 43758.5453);
+}
+
+void main() {
+ 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(v_color.rgb, frag_alpha);
+}
diff --git a/sway/desktop/shaders/meson.build b/sway/desktop/shaders/meson.build
index 08efb967..ce2439de 100644
--- a/sway/desktop/shaders/meson.build
+++ b/sway/desktop/shaders/meson.build
@@ -7,6 +7,7 @@ shaders = [
'quad_round_tl.frag',
'quad_round_tr.frag',
'corner.frag',
+ 'box_shadow.frag',
'tex_rgba.frag',
'tex_rgbx.frag',
'tex_external.frag',
diff --git a/sway/desktop/shaders/quad_round.frag b/sway/desktop/shaders/quad_round.frag
index e347284b..ddd6fe9b 100644
--- a/sway/desktop/shaders/quad_round.frag
+++ b/sway/desktop/shaders/quad_round.frag
@@ -7,9 +7,13 @@ uniform vec2 position;
uniform float radius;
void main() {
- vec2 half_size = size / 2.0;
+ vec2 half_size = size * 0.5;
vec2 q = abs(gl_FragCoord.xy - position - half_size) - half_size + radius;
- float distance = min(max(q.x,q.y),0.0) + length(max(q,0.0)) - radius;
- float smoothedAlpha = 1.0 - smoothstep(-1.0, 1.0, distance);
+ float dist = min(max(q.x,q.y), 0.0) + length(max(q, 0.0)) - radius;
+ float smoothedAlpha = 1.0 - smoothstep(-1.0, 0.5, dist);
gl_FragColor = mix(vec4(0), v_color, smoothedAlpha);
+
+ if (gl_FragColor.a == 0.0) {
+ discard;
+ }
}