forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

shadow.frag119 lines, 3.8 KiBHistoryRawDownload
1precision highp float;
2
3#if defined(DEBUG_FLAGS)
4uniform float niri_tint;
5#endif
6
7uniform float niri_alpha;
8uniform float niri_scale;
9
10uniform vec2 niri_size;
11varying vec2 niri_v_coords;
12
13uniform vec4 shadow_color;
14uniform float sigma;
15
16uniform mat3 input_to_geo;
17uniform vec2 geo_size;
18uniform vec4 corner_radius;
19
20uniform mat3 window_input_to_geo;
21uniform vec2 window_geo_size;
22uniform vec4 window_corner_radius;
23
24// Based on: https://madebyevan.com/shaders/fast-rounded-rectangle-shadows/
25//
26// License: CC0 (http://creativecommons.org/publicdomain/zero/1.0/)
27
28// A standard gaussian function, used for weighting samples
29float gaussian(float x, float sigma) {
30 const float pi = 3.141592653589793;
31 return exp(-(x * x) / (2.0 * sigma * sigma)) / (sqrt(2.0 * pi) * sigma);
32}
33
34// This approximates the error function, needed for the gaussian integral
35vec2 erf(vec2 x) {
36 vec2 s = sign(x), a = abs(x);
37 x = 1.0 + (0.278393 + (0.230389 + 0.078108 * (a * a)) * a) * a;
38 x *= x;
39 return s - s / (x * x);
40}
41
42// Return the blurred mask along the x dimension
43float roundedBoxShadowX(float x, float y, float sigma, float corner, vec2 halfSize) {
44 float delta = min(halfSize.y - corner - abs(y), 0.0);
45 float curved = halfSize.x - corner + sqrt(max(0.0, corner * corner - delta * delta));
46 vec2 integral = 0.5 + 0.5 * erf((x + vec2(-curved, curved)) * (sqrt(0.5) / sigma));
47 return integral.y - integral.x;
48}
49
50// Return the mask for the shadow of a box from lower to upper
51float roundedBoxShadow(vec2 lower, vec2 upper, vec2 point, float sigma, float corner) {
52 // Center everything to make the math easier
53 vec2 center = (lower + upper) * 0.5;
54 vec2 halfSize = (upper - lower) * 0.5;
55 point -= center;
56
57 // The signal is only non-zero in a limited range, so don't waste samples
58 float low = point.y - halfSize.y;
59 float high = point.y + halfSize.y;
60 float start = clamp(-3.0 * sigma, low, high);
61 float end = clamp(3.0 * sigma, low, high);
62
63 // Accumulate samples (we can get away with surprisingly few samples)
64 float step = (end - start) / 4.0;
65 float y = start + step * 0.5;
66 float value = 0.0;
67 for (int i = 0; i < 4; i++) {
68 value += roundedBoxShadowX(point.x, point.y - y, sigma, corner, halfSize) * gaussian(y, sigma) * step;
69 y += step;
70 }
71
72 return value;
73}
74
75float niri_rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius);
76
77void main() {
78 vec3 coords_geo = input_to_geo * vec3(niri_v_coords, 1.0);
79 vec3 coords_window_geo = window_input_to_geo * vec3(niri_v_coords, 1.0);
80
81 vec4 color = shadow_color;
82
83 float shadow_value;
84 if (sigma < 0.1) {
85 // With low enough sigma just draw a rounded rectangle.
86 shadow_value = niri_rounding_alpha(coords_geo.xy, geo_size, corner_radius);
87 } else {
88 shadow_value = roundedBoxShadow(
89 vec2(0.0, 0.0),
90 geo_size,
91 coords_geo.xy,
92 sigma,
93 // FIXME: figure out how to blur with different corner radii.
94 //
95 // GTK seems to call blurring separately for the rect and for the 4 corners:
96 // https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-4-16/gsk/gpu/shaders/gskgpuboxshadow.glsl
97 corner_radius.x
98 );
99 }
100 color = color * shadow_value;
101
102 // Cut out the inside of the window geometry if requested.
103 if (window_geo_size != vec2(0.0, 0.0)) {
104 if (0.0 <= coords_window_geo.x && coords_window_geo.x <= window_geo_size.x
105 && 0.0 <= coords_window_geo.y && coords_window_geo.y <= window_geo_size.y) {
106 float alpha = niri_rounding_alpha(coords_window_geo.xy, window_geo_size, window_corner_radius);
107 color = color * (1.0 - alpha);
108 }
109 }
110
111 color = color * niri_alpha;
112
113#if defined(DEBUG_FLAGS)
114 if (niri_tint == 1.0)
115 color = vec4(0.0, 0.2, 0.0, 0.2) + color * 0.8;
116#endif
117
118 gl_FragColor = color;
119}