shaders: Extract niri_rounding_alpha() into a file
Browse files at this commit5abeb923de0891cdcc1f354ece7933b81155f3d9Parent: dd1f2897 changed files +47 -105
- src/render_helpers/shaders/border.frag +3 -26
- src/render_helpers/shaders/clipped_surface.frag +2 -25
- src/render_helpers/shaders/mod.rs +13 -3
- src/render_helpers/shaders/resize_epilogue.frag +1 -1
- src/render_helpers/shaders/resize_prelude.frag +1 -24
- src/render_helpers/shaders/rounding_alpha.frag +24 -0
- src/render_helpers/shaders/shadow.frag +3 -26
src/render_helpers/shaders/border.fragmodified+3 -26
| @@ -208,35 +208,12 @@ vec4 gradient_color(vec2 coords) { | ||
| 208 | 208 | return color_mix(color_from, color_to, frac); |
| 209 | 209 | } |
| 210 | 210 | |
| 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); | |
| 235 | 212 | |
| 236 | 213 | void main() { |
| 237 | 214 | vec3 coords_geo = input_to_geo * vec3(niri_v_coords, 1.0); |
| 238 | 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 | 218 | if (border_width > 0.0) { |
| 242 | 219 | coords_geo -= vec3(border_width); |
| @@ -245,7 +222,7 @@ void main() { | ||
| 245 | 222 | && 0.0 <= coords_geo.y && coords_geo.y <= inner_geo_size.y) |
| 246 | 223 | { |
| 247 | 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 | 26 | uniform vec4 corner_radius; |
| 27 | 27 | uniform mat3 input_to_geo; |
| 28 | 28 | |
| 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); | |
| 53 | 30 | |
| 54 | 31 | void main() { |
| 55 | 32 | vec3 coords_geo = input_to_geo * vec3(v_coords, 1.0); |
| @@ -65,7 +42,7 @@ void main() { | ||
| 65 | 42 | color = vec4(0.0); |
| 66 | 43 | } else { |
| 67 | 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 | 48 | // Apply final alpha and tint. |
src/render_helpers/shaders/mod.rsmodified+13 -3
| @@ -35,7 +35,10 @@ impl Shaders { | ||
| 35 | 35 | |
| 36 | 36 | let border = ShaderProgram::compile( |
| 37 | 37 | renderer, |
| 38 | - include_str!("border.frag"), | |
| 38 | + concat!( | |
| 39 | + include_str!("border.frag"), | |
| 40 | + include_str!("rounding_alpha.frag") | |
| 41 | + ), | |
| 39 | 42 | &[ |
| 40 | 43 | UniformName::new("colorspace", UniformType::_1f), |
| 41 | 44 | UniformName::new("hue_interpolation", UniformType::_1f), |
| @@ -58,7 +61,10 @@ impl Shaders { | ||
| 58 | 61 | |
| 59 | 62 | let shadow = ShaderProgram::compile( |
| 60 | 63 | renderer, |
| 61 | - include_str!("shadow.frag"), | |
| 64 | + concat!( | |
| 65 | + include_str!("shadow.frag"), | |
| 66 | + include_str!("rounding_alpha.frag") | |
| 67 | + ), | |
| 62 | 68 | &[ |
| 63 | 69 | UniformName::new("shadow_color", UniformType::_4f), |
| 64 | 70 | UniformName::new("sigma", UniformType::_1f), |
| @@ -78,7 +84,10 @@ impl Shaders { | ||
| 78 | 84 | |
| 79 | 85 | let clipped_surface = renderer |
| 80 | 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 | 92 | UniformName::new("niri_scale", UniformType::_1f), |
| 84 | 93 | UniformName::new("geo_size", UniformType::_2f), |
| @@ -183,6 +192,7 @@ fn compile_resize_program( | ||
| 183 | 192 | let mut program = include_str!("resize_prelude.frag").to_string(); |
| 184 | 193 | program.push_str(src); |
| 185 | 194 | program.push_str(include_str!("resize_epilogue.frag")); |
| 195 | + program.push_str(include_str!("rounding_alpha.frag")); | |
| 186 | 196 | |
| 187 | 197 | ShaderProgram::compile( |
| 188 | 198 | renderer, |
src/render_helpers/shaders/resize_epilogue.fragmodified+1 -1
| @@ -12,7 +12,7 @@ void main() { | ||
| 12 | 12 | color = vec4(0.0); |
| 13 | 13 | } else { |
| 14 | 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 | 27 | uniform float niri_alpha; |
| 28 | 28 | uniform float niri_scale; |
| 29 | 29 | |
| 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 | ||
| 72 | 72 | return value; |
| 73 | 73 | } |
| 74 | 74 | |
| 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); | |
| 99 | 76 | |
| 100 | 77 | void main() { |
| 101 | 78 | vec3 coords_geo = input_to_geo * vec3(niri_v_coords, 1.0); |
| @@ -106,7 +83,7 @@ void main() { | ||
| 106 | 83 | float shadow_value; |
| 107 | 84 | if (sigma < 0.1) { |
| 108 | 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 | 87 | } else { |
| 111 | 88 | shadow_value = roundedBoxShadow( |
| 112 | 89 | vec2(0.0, 0.0), |
| @@ -126,7 +103,7 @@ void main() { | ||
| 126 | 103 | if (window_geo_size != vec2(0.0, 0.0)) { |
| 127 | 104 | if (0.0 <= coords_window_geo.x && coords_window_geo.x <= window_geo_size.x |
| 128 | 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 | 107 | color = color * (1.0 - alpha); |
| 131 | 108 | } |
| 132 | 109 | } |