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) {
208 return color_mix(color_from, color_to, frac);208 return color_mix(color_from, color_to, frac);
209 }209 }
210 210
211-float rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius) {211+float niri_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-}
235 212
236 void main() {213 void main() {
237 vec3 coords_geo = input_to_geo * vec3(niri_v_coords, 1.0);214 vec3 coords_geo = input_to_geo * vec3(niri_v_coords, 1.0);
238 vec4 color = gradient_color(coords_geo.xy);215 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);
240 217
241 if (border_width > 0.0) {218 if (border_width > 0.0) {
242 coords_geo -= vec3(border_width);219 coords_geo -= vec3(border_width);
@@ -245,7 +222,7 @@ void main() {
245 && 0.0 <= coords_geo.y && coords_geo.y <= inner_geo_size.y)222 && 0.0 <= coords_geo.y && coords_geo.y <= inner_geo_size.y)
246 {223 {
247 vec4 inner_radius = max(outer_radius - vec4(border_width), 0.0);224 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));
249 }226 }
250 }227 }
251 228
src/render_helpers/shaders/clipped_surface.fragmodified+2 -25
@@ -26,30 +26,7 @@ uniform vec2 geo_size;
26 uniform vec4 corner_radius;26 uniform vec4 corner_radius;
27 uniform mat3 input_to_geo;27 uniform mat3 input_to_geo;
28 28
29-float rounding_alpha(vec2 coords, vec2 size) {29+float niri_rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius);
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-}
53 30
54 void main() {31 void main() {
55 vec3 coords_geo = input_to_geo * vec3(v_coords, 1.0);32 vec3 coords_geo = input_to_geo * vec3(v_coords, 1.0);
@@ -65,7 +42,7 @@ void main() {
65 color = vec4(0.0);42 color = vec4(0.0);
66 } else {43 } else {
67 // Apply corner rounding inside geometry.44 // 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);
69 }46 }
70 47
71 // Apply final alpha and tint.48 // Apply final alpha and tint.
src/render_helpers/shaders/mod.rsmodified+13 -3
@@ -35,7 +35,10 @@ impl Shaders {
35 35
36 let border = ShaderProgram::compile(36 let border = ShaderProgram::compile(
37 renderer,37 renderer,
38- include_str!("border.frag"),38+ concat!(
39+ include_str!("border.frag"),
40+ include_str!("rounding_alpha.frag")
41+ ),
39 &[42 &[
40 UniformName::new("colorspace", UniformType::_1f),43 UniformName::new("colorspace", UniformType::_1f),
41 UniformName::new("hue_interpolation", UniformType::_1f),44 UniformName::new("hue_interpolation", UniformType::_1f),
@@ -58,7 +61,10 @@ impl Shaders {
58 61
59 let shadow = ShaderProgram::compile(62 let shadow = ShaderProgram::compile(
60 renderer,63 renderer,
61- include_str!("shadow.frag"),64+ concat!(
65+ include_str!("shadow.frag"),
66+ include_str!("rounding_alpha.frag")
67+ ),
62 &[68 &[
63 UniformName::new("shadow_color", UniformType::_4f),69 UniformName::new("shadow_color", UniformType::_4f),
64 UniformName::new("sigma", UniformType::_1f),70 UniformName::new("sigma", UniformType::_1f),
@@ -78,7 +84,10 @@ impl Shaders {
78 84
79 let clipped_surface = renderer85 let clipped_surface = renderer
80 .compile_custom_texture_shader(86 .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+ ),
82 &[91 &[
83 UniformName::new("niri_scale", UniformType::_1f),92 UniformName::new("niri_scale", UniformType::_1f),
84 UniformName::new("geo_size", UniformType::_2f),93 UniformName::new("geo_size", UniformType::_2f),
@@ -183,6 +192,7 @@ fn compile_resize_program(
183 let mut program = include_str!("resize_prelude.frag").to_string();192 let mut program = include_str!("resize_prelude.frag").to_string();
184 program.push_str(src);193 program.push_str(src);
185 program.push_str(include_str!("resize_epilogue.frag"));194 program.push_str(include_str!("resize_epilogue.frag"));
195+ program.push_str(include_str!("rounding_alpha.frag"));
186 196
187 ShaderProgram::compile(197 ShaderProgram::compile(
188 renderer,198 renderer,
src/render_helpers/shaders/resize_epilogue.fragmodified+1 -1
@@ -12,7 +12,7 @@ void main() {
12 color = vec4(0.0);12 color = vec4(0.0);
13 } else {13 } else {
14 // Apply corner rounding inside geometry.14 // 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);
16 }16 }
17 }17 }
18 18
src/render_helpers/shaders/resize_prelude.fragmodified+1 -24
@@ -27,27 +27,4 @@ uniform float niri_clip_to_geometry;
27 uniform float niri_alpha;27 uniform float niri_alpha;
28 uniform float niri_scale;28 uniform float niri_scale;
29 29
30-float niri_rounding_alpha(vec2 coords, vec2 size) {30+float niri_rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius);
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-}
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
72 return value;72 return value;
73 }73 }
74 74
75-float rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius) {75+float niri_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 76
100 void main() {77 void main() {
101 vec3 coords_geo = input_to_geo * vec3(niri_v_coords, 1.0);78 vec3 coords_geo = input_to_geo * vec3(niri_v_coords, 1.0);
@@ -106,7 +83,7 @@ void main() {
106 float shadow_value;83 float shadow_value;
107 if (sigma < 0.1) {84 if (sigma < 0.1) {
108 // With low enough sigma just draw a rounded rectangle.85 // 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);
110 } else {87 } else {
111 shadow_value = roundedBoxShadow(88 shadow_value = roundedBoxShadow(
112 vec2(0.0, 0.0),89 vec2(0.0, 0.0),
@@ -126,7 +103,7 @@ void main() {
126 if (window_geo_size != vec2(0.0, 0.0)) {103 if (window_geo_size != vec2(0.0, 0.0)) {
127 if (0.0 <= coords_window_geo.x && coords_window_geo.x <= window_geo_size.x104 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) {105 && 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);
130 color = color * (1.0 - alpha);107 color = color * (1.0 - alpha);
131 }108 }
132 }109 }