forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

shadow.frag142 lines, 4.6 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 rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius) {
76 vec2 center;
77 float radius;
78
79 if (coords.x < corner_radius.x && coords.y < corner_radius.x) {
80 radius = corner_radius.x;
81 center = vec2(radius, radius);
82 } else if (size.x - corner_radius.y < coords.x && coords.y < corner_radius.y) {
83 radius = corner_radius.y;
84 center = vec2(size.x - radius, radius);
85 } else if (size.x - corner_radius.z < coords.x && size.y - corner_radius.z < coords.y) {
86 radius = corner_radius.z;
87 center = vec2(size.x - radius, size.y - radius);
88 } else if (coords.x < corner_radius.w && size.y - corner_radius.w < coords.y) {
89 radius = corner_radius.w;
90 center = vec2(radius, size.y - radius);
91 } else {
92 return 1.0;
93 }
94
95 float dist = distance(coords, center);
96 float half_px = 0.5 / niri_scale;
97 return 1.0 - smoothstep(radius - half_px, radius + half_px, dist);
98}
99
100void main() {
101 vec3 coords_geo = input_to_geo * vec3(niri_v_coords, 1.0);
102 vec3 coords_window_geo = window_input_to_geo * vec3(niri_v_coords, 1.0);
103
104 vec4 color = shadow_color;
105
106 float shadow_value;
107 if (sigma < 0.1) {
108 // With low enough sigma just draw a rounded rectangle.
109 shadow_value = rounding_alpha(coords_geo.xy, geo_size, corner_radius);
110 } else {
111 shadow_value = roundedBoxShadow(
112 vec2(0.0, 0.0),
113 geo_size,
114 coords_geo.xy,
115 sigma,
116 // FIXME: figure out how to blur with different corner radii.
117 //
118 // GTK seems to call blurring separately for the rect and for the 4 corners:
119 // https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-4-16/gsk/gpu/shaders/gskgpuboxshadow.glsl
120 corner_radius.x
121 );
122 }
123 color = color * shadow_value;
124
125 // Cut out the inside of the window geometry if requested.
126 if (window_geo_size != vec2(0.0, 0.0)) {
127 if (0.0 <= coords_window_geo.x && coords_window_geo.x <= window_geo_size.x
128 && 0.0 <= coords_window_geo.y && coords_window_geo.y <= window_geo_size.y) {
129 float alpha = rounding_alpha(coords_window_geo.xy, window_geo_size, window_corner_radius);
130 color = color * (1.0 - alpha);
131 }
132 }
133
134 color = color * niri_alpha;
135
136#if defined(DEBUG_FLAGS)
137 if (niri_tint == 1.0)
138 color = vec4(0.0, 0.2, 0.0, 0.2) + color * 0.8;
139#endif
140
141 gl_FragColor = color;
142}