forge
Explore
Log in

Don't you dare go Hollow

shaders: Extract niri_rounding_alpha() into a file

Browse files at this commit

5abeb923de0891cdcc1f354ece7933b81155f3d9Ivan MolodetskikhParent: dd1f289
To have it in one place.
src/render_helpers/shaders/border.fragmodified+3 -26
@@ -208,35 +208,12 @@ vec4 gradient_color(vec2 coords) {
208208 return color_mix(color_from, color_to, frac);
209209 }
210210
211-float rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius) {
212- vec2 center;
213- float radius;
214-
215- if (coords.x < corner_radius.x && coords.y < corner_radius.x) {
216- radius = corner_radius.x;
217- center = vec2(radius, radius);
218- } else if (size.x - corner_radius.y < coords.x && coords.y < corner_radius.y) {
219- radius = corner_radius.y;
220- center = vec2(size.x - radius, radius);
221- } else if (size.x - corner_radius.z < coords.x && size.y - corner_radius.z < coords.y) {
222- radius = corner_radius.z;
223- center = vec2(size.x - radius, size.y - radius);
224- } else if (coords.x < corner_radius.w && size.y - corner_radius.w < coords.y) {
225- radius = corner_radius.w;
226- center = vec2(radius, size.y - radius);
227- } else {
228- return 1.0;
229- }
230-
231- float dist = distance(coords, center);
232- float half_px = 0.5 / niri_scale;
233- return 1.0 - smoothstep(radius - half_px, radius + half_px, dist);
234-}
211+float niri_rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius);
235212
236213 void main() {
237214 vec3 coords_geo = input_to_geo * vec3(niri_v_coords, 1.0);
238215 vec4 color = gradient_color(coords_geo.xy);
239- color = color * rounding_alpha(coords_geo.xy, geo_size, outer_radius);
216+ color = color * niri_rounding_alpha(coords_geo.xy, geo_size, outer_radius);
240217
241218 if (border_width > 0.0) {
242219 coords_geo -= vec3(border_width);
@@ -245,7 +222,7 @@ void main() {
245222 && 0.0 <= coords_geo.y && coords_geo.y <= inner_geo_size.y)
246223 {
247224 vec4 inner_radius = max(outer_radius - vec4(border_width), 0.0);
248- color = color * (1.0 - rounding_alpha(coords_geo.xy, inner_geo_size, inner_radius));
225+ color = color * (1.0 - niri_rounding_alpha(coords_geo.xy, inner_geo_size, inner_radius));
249226 }
250227 }
251228
src/render_helpers/shaders/clipped_surface.fragmodified+2 -25
@@ -26,30 +26,7 @@ uniform vec2 geo_size;
2626 uniform vec4 corner_radius;
2727 uniform mat3 input_to_geo;
2828
29-float rounding_alpha(vec2 coords, vec2 size) {
30- vec2 center;
31- float radius;
32-
33- if (coords.x < corner_radius.x && coords.y < corner_radius.x) {
34- radius = corner_radius.x;
35- center = vec2(radius, radius);
36- } else if (size.x - corner_radius.y < coords.x && coords.y < corner_radius.y) {
37- radius = corner_radius.y;
38- center = vec2(size.x - radius, radius);
39- } else if (size.x - corner_radius.z < coords.x && size.y - corner_radius.z < coords.y) {
40- radius = corner_radius.z;
41- center = vec2(size.x - radius, size.y - radius);
42- } else if (coords.x < corner_radius.w && size.y - corner_radius.w < coords.y) {
43- radius = corner_radius.w;
44- center = vec2(radius, size.y - radius);
45- } else {
46- return 1.0;
47- }
48-
49- float dist = distance(coords, center);
50- float half_px = 0.5 / niri_scale;
51- return 1.0 - smoothstep(radius - half_px, radius + half_px, dist);
52-}
29+float niri_rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius);
5330
5431 void main() {
5532 vec3 coords_geo = input_to_geo * vec3(v_coords, 1.0);
@@ -65,7 +42,7 @@ void main() {
6542 color = vec4(0.0);
6643 } else {
6744 // Apply corner rounding inside geometry.
68- color = color * rounding_alpha(coords_geo.xy * geo_size, geo_size);
45+ color = color * niri_rounding_alpha(coords_geo.xy * geo_size, geo_size, corner_radius);
6946 }
7047
7148 // Apply final alpha and tint.
src/render_helpers/shaders/mod.rsmodified+13 -3
@@ -35,7 +35,10 @@ impl Shaders {
3535
3636 let border = ShaderProgram::compile(
3737 renderer,
38- include_str!("border.frag"),
38+ concat!(
39+ include_str!("border.frag"),
40+ include_str!("rounding_alpha.frag")
41+ ),
3942 &[
4043 UniformName::new("colorspace", UniformType::_1f),
4144 UniformName::new("hue_interpolation", UniformType::_1f),
@@ -58,7 +61,10 @@ impl Shaders {
5861
5962 let shadow = ShaderProgram::compile(
6063 renderer,
61- include_str!("shadow.frag"),
64+ concat!(
65+ include_str!("shadow.frag"),
66+ include_str!("rounding_alpha.frag")
67+ ),
6268 &[
6369 UniformName::new("shadow_color", UniformType::_4f),
6470 UniformName::new("sigma", UniformType::_1f),
@@ -78,7 +84,10 @@ impl Shaders {
7884
7985 let clipped_surface = renderer
8086 .compile_custom_texture_shader(
81- include_str!("clipped_surface.frag"),
87+ concat!(
88+ include_str!("clipped_surface.frag"),
89+ include_str!("rounding_alpha.frag")
90+ ),
8291 &[
8392 UniformName::new("niri_scale", UniformType::_1f),
8493 UniformName::new("geo_size", UniformType::_2f),
@@ -183,6 +192,7 @@ fn compile_resize_program(
183192 let mut program = include_str!("resize_prelude.frag").to_string();
184193 program.push_str(src);
185194 program.push_str(include_str!("resize_epilogue.frag"));
195+ program.push_str(include_str!("rounding_alpha.frag"));
186196
187197 ShaderProgram::compile(
188198 renderer,
src/render_helpers/shaders/resize_epilogue.fragmodified+1 -1
@@ -12,7 +12,7 @@ void main() {
1212 color = vec4(0.0);
1313 } else {
1414 // Apply corner rounding inside geometry.
15- color = color * niri_rounding_alpha(coords_curr_geo.xy * size_curr_geo.xy, size_curr_geo.xy);
15+ color = color * niri_rounding_alpha(coords_curr_geo.xy * size_curr_geo.xy, size_curr_geo.xy, niri_corner_radius);
1616 }
1717 }
1818
src/render_helpers/shaders/resize_prelude.fragmodified+1 -24
@@ -27,27 +27,4 @@ uniform float niri_clip_to_geometry;
2727 uniform float niri_alpha;
2828 uniform float niri_scale;
2929
30-float niri_rounding_alpha(vec2 coords, vec2 size) {
31- vec2 center;
32- float radius;
33-
34- if (coords.x < niri_corner_radius.x && coords.y < niri_corner_radius.x) {
35- radius = niri_corner_radius.x;
36- center = vec2(radius, radius);
37- } else if (size.x - niri_corner_radius.y < coords.x && coords.y < niri_corner_radius.y) {
38- radius = niri_corner_radius.y;
39- center = vec2(size.x - radius, radius);
40- } else if (size.x - niri_corner_radius.z < coords.x && size.y - niri_corner_radius.z < coords.y) {
41- radius = niri_corner_radius.z;
42- center = vec2(size.x - radius, size.y - radius);
43- } else if (coords.x < niri_corner_radius.w && size.y - niri_corner_radius.w < coords.y) {
44- radius = niri_corner_radius.w;
45- center = vec2(radius, size.y - radius);
46- } else {
47- return 1.0;
48- }
49-
50- float dist = distance(coords, center);
51- float half_px = 0.5 / niri_scale;
52- return 1.0 - smoothstep(radius - half_px, radius + half_px, dist);
53-}
30+float niri_rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius);
src/render_helpers/shaders/rounding_alpha.fragadded+24 -0
@@ -0,0 +1,24 @@
1+float niri_rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius) {
2+ vec2 center;
3+ float radius;
4+
5+ if (coords.x < corner_radius.x && coords.y < corner_radius.x) {
6+ radius = corner_radius.x;
7+ center = vec2(radius, radius);
8+ } else if (size.x - corner_radius.y < coords.x && coords.y < corner_radius.y) {
9+ radius = corner_radius.y;
10+ center = vec2(size.x - radius, radius);
11+ } else if (size.x - corner_radius.z < coords.x && size.y - corner_radius.z < coords.y) {
12+ radius = corner_radius.z;
13+ center = vec2(size.x - radius, size.y - radius);
14+ } else if (coords.x < corner_radius.w && size.y - corner_radius.w < coords.y) {
15+ radius = corner_radius.w;
16+ center = vec2(radius, size.y - radius);
17+ } else {
18+ return 1.0;
19+ }
20+
21+ float dist = distance(coords, center);
22+ float half_px = 0.5 / niri_scale;
23+ return 1.0 - smoothstep(radius - half_px, radius + half_px, dist);
24+}
src/render_helpers/shaders/shadow.fragmodified+3 -26
@@ -72,30 +72,7 @@ float roundedBoxShadow(vec2 lower, vec2 upper, vec2 point, float sigma, float co
7272 return value;
7373 }
7474
75-float 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-}
75+float niri_rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius);
9976
10077 void main() {
10178 vec3 coords_geo = input_to_geo * vec3(niri_v_coords, 1.0);
@@ -106,7 +83,7 @@ void main() {
10683 float shadow_value;
10784 if (sigma < 0.1) {
10885 // With low enough sigma just draw a rounded rectangle.
109- shadow_value = rounding_alpha(coords_geo.xy, geo_size, corner_radius);
86+ shadow_value = niri_rounding_alpha(coords_geo.xy, geo_size, corner_radius);
11087 } else {
11188 shadow_value = roundedBoxShadow(
11289 vec2(0.0, 0.0),
@@ -126,7 +103,7 @@ void main() {
126103 if (window_geo_size != vec2(0.0, 0.0)) {
127104 if (0.0 <= coords_window_geo.x && coords_window_geo.x <= window_geo_size.x
128105 && 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);
106+ float alpha = niri_rounding_alpha(coords_window_geo.xy, window_geo_size, window_corner_radius);
130107 color = color * (1.0 - alpha);
131108 }
132109 }