niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use std::cell::RefCell; |
| 2 | |
| 3 | use glam::Mat3; |
| 4 | use smithay::backend::renderer::gles::{ |
| 5 | GlesError, GlesFrame, GlesRenderer, GlesTexProgram, Uniform, UniformName, UniformType, |
| 6 | UniformValue, |
| 7 | }; |
| 8 | |
| 9 | use super::renderer::NiriRenderer; |
| 10 | use super::shader_element::ShaderProgram; |
| 11 | use crate::render_helpers::blur::BlurProgram; |
| 12 | |
| 13 | pub struct Shaders { |
| 14 | pub border: Option<ShaderProgram>, |
| 15 | pub shadow: Option<ShaderProgram>, |
| 16 | pub clipped_surface: Option<GlesTexProgram>, |
| 17 | pub postprocess_and_clip: Option<GlesTexProgram>, |
| 18 | pub resize: Option<ShaderProgram>, |
| 19 | pub gradient_fade: Option<GlesTexProgram>, |
| 20 | pub blur: Option<BlurProgram>, |
| 21 | pub custom_resize: RefCell<Option<ShaderProgram>>, |
| 22 | pub custom_close: RefCell<Option<ShaderProgram>>, |
| 23 | pub custom_open: RefCell<Option<ShaderProgram>>, |
| 24 | } |
| 25 | |
| 26 | #[derive(Debug, Clone, Copy)] |
| 27 | pub enum ProgramType { |
| 28 | Border, |
| 29 | Shadow, |
| 30 | Resize, |
| 31 | Close, |
| 32 | Open, |
| 33 | } |
| 34 | |
| 35 | impl Shaders { |
| 36 | fn compile(renderer: &mut GlesRenderer) -> Self { |
| 37 | let _span = tracy_client::span!("Shaders::compile"); |
| 38 | |
| 39 | let border = ShaderProgram::compile( |
| 40 | renderer, |
| 41 | concat!( |
| 42 | include_str!("border.frag"), |
| 43 | include_str!("rounding_alpha.frag") |
| 44 | ), |
| 45 | &[ |
| 46 | UniformName::new("colorspace", UniformType::_1f), |
| 47 | UniformName::new("hue_interpolation", UniformType::_1f), |
| 48 | UniformName::new("color_from", UniformType::_4f), |
| 49 | UniformName::new("color_to", UniformType::_4f), |
| 50 | UniformName::new("grad_offset", UniformType::_2f), |
| 51 | UniformName::new("grad_width", UniformType::_1f), |
| 52 | UniformName::new("grad_vec", UniformType::_2f), |
| 53 | UniformName::new("input_to_geo", UniformType::Matrix3x3), |
| 54 | UniformName::new("geo_size", UniformType::_2f), |
| 55 | UniformName::new("outer_radius", UniformType::_4f), |
| 56 | UniformName::new("border_width", UniformType::_1f), |
| 57 | ], |
| 58 | &[], |
| 59 | ) |
| 60 | .map_err(|err| { |
| 61 | warn!("error compiling border shader: {err:?}"); |
| 62 | }) |
| 63 | .ok(); |
| 64 | |
| 65 | let shadow = ShaderProgram::compile( |
| 66 | renderer, |
| 67 | concat!( |
| 68 | include_str!("shadow.frag"), |
| 69 | include_str!("rounding_alpha.frag") |
| 70 | ), |
| 71 | &[ |
| 72 | UniformName::new("shadow_color", UniformType::_4f), |
| 73 | UniformName::new("sigma", UniformType::_1f), |
| 74 | UniformName::new("input_to_geo", UniformType::Matrix3x3), |
| 75 | UniformName::new("geo_size", UniformType::_2f), |
| 76 | UniformName::new("corner_radius", UniformType::_4f), |
| 77 | UniformName::new("window_input_to_geo", UniformType::Matrix3x3), |
| 78 | UniformName::new("window_geo_size", UniformType::_2f), |
| 79 | UniformName::new("window_corner_radius", UniformType::_4f), |
| 80 | ], |
| 81 | &[], |
| 82 | ) |
| 83 | .map_err(|err| { |
| 84 | warn!("error compiling shadow shader: {err:?}"); |
| 85 | }) |
| 86 | .ok(); |
| 87 | |
| 88 | let clipped_surface = renderer |
| 89 | .compile_custom_texture_shader( |
| 90 | concat!( |
| 91 | include_str!("clipped_surface.frag"), |
| 92 | include_str!("rounding_alpha.frag"), |
| 93 | "\nvec4 postprocess(vec4 color) { return color; }", |
| 94 | ), |
| 95 | &[ |
| 96 | UniformName::new("niri_scale", UniformType::_1f), |
| 97 | UniformName::new("geo_size", UniformType::_2f), |
| 98 | UniformName::new("corner_radius", UniformType::_4f), |
| 99 | UniformName::new("input_to_geo", UniformType::Matrix3x3), |
| 100 | ], |
| 101 | ) |
| 102 | .map_err(|err| { |
| 103 | warn!("error compiling clipped surface shader: {err:?}"); |
| 104 | }) |
| 105 | .ok(); |
| 106 | |
| 107 | let postprocess_and_clip = renderer |
| 108 | .compile_custom_texture_shader( |
| 109 | concat!( |
| 110 | include_str!("clipped_surface.frag"), |
| 111 | include_str!("rounding_alpha.frag"), |
| 112 | include_str!("postprocess.frag"), |
| 113 | ), |
| 114 | &[ |
| 115 | UniformName::new("niri_scale", UniformType::_1f), |
| 116 | UniformName::new("geo_size", UniformType::_2f), |
| 117 | UniformName::new("corner_radius", UniformType::_4f), |
| 118 | UniformName::new("input_to_geo", UniformType::Matrix3x3), |
| 119 | UniformName::new("noise", UniformType::_1f), |
| 120 | UniformName::new("saturation", UniformType::_1f), |
| 121 | UniformName::new("bg_color", UniformType::_4f), |
| 122 | ], |
| 123 | ) |
| 124 | .map_err(|err| { |
| 125 | warn!("error compiling postprocess_and_clip shader: {err:?}"); |
| 126 | }) |
| 127 | .ok(); |
| 128 | |
| 129 | let resize = compile_resize_program(renderer, include_str!("resize.frag")) |
| 130 | .map_err(|err| { |
| 131 | warn!("error compiling resize shader: {err:?}"); |
| 132 | }) |
| 133 | .ok(); |
| 134 | |
| 135 | let gradient_fade = renderer |
| 136 | .compile_custom_texture_shader( |
| 137 | include_str!("gradient_fade.frag"), |
| 138 | &[UniformName::new("cutoff", UniformType::_2f)], |
| 139 | ) |
| 140 | .map_err(|err| { |
| 141 | warn!("error compiling gradient fade shader: {err:?}"); |
| 142 | }) |
| 143 | .ok(); |
| 144 | |
| 145 | let blur = BlurProgram::compile(renderer) |
| 146 | .map_err(|err| { |
| 147 | warn!("error compiling blur shaders: {err:?}"); |
| 148 | }) |
| 149 | .ok(); |
| 150 | |
| 151 | Self { |
| 152 | border, |
| 153 | shadow, |
| 154 | clipped_surface, |
| 155 | postprocess_and_clip, |
| 156 | resize, |
| 157 | gradient_fade, |
| 158 | blur, |
| 159 | custom_resize: RefCell::new(None), |
| 160 | custom_close: RefCell::new(None), |
| 161 | custom_open: RefCell::new(None), |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | pub fn get_from_frame<'a>(frame: &'a mut GlesFrame<'_, '_>) -> &'a Self { |
| 166 | let data = frame.egl_context().user_data(); |
| 167 | data.get() |
| 168 | .expect("shaders::init() must be called when creating the renderer") |
| 169 | } |
| 170 | |
| 171 | pub fn get(renderer: &mut impl NiriRenderer) -> &Self { |
| 172 | let renderer = renderer.as_gles_renderer(); |
| 173 | let data = renderer.egl_context().user_data(); |
| 174 | data.get() |
| 175 | .expect("shaders::init() must be called when creating the renderer") |
| 176 | } |
| 177 | |
| 178 | pub fn replace_custom_resize_program( |
| 179 | &self, |
| 180 | program: Option<ShaderProgram>, |
| 181 | ) -> Option<ShaderProgram> { |
| 182 | self.custom_resize.replace(program) |
| 183 | } |
| 184 | |
| 185 | pub fn replace_custom_close_program( |
| 186 | &self, |
| 187 | program: Option<ShaderProgram>, |
| 188 | ) -> Option<ShaderProgram> { |
| 189 | self.custom_close.replace(program) |
| 190 | } |
| 191 | |
| 192 | pub fn replace_custom_open_program( |
| 193 | &self, |
| 194 | program: Option<ShaderProgram>, |
| 195 | ) -> Option<ShaderProgram> { |
| 196 | self.custom_open.replace(program) |
| 197 | } |
| 198 | |
| 199 | pub fn program(&self, program: ProgramType) -> Option<ShaderProgram> { |
| 200 | match program { |
| 201 | ProgramType::Border => self.border.clone(), |
| 202 | ProgramType::Shadow => self.shadow.clone(), |
| 203 | ProgramType::Resize => self |
| 204 | .custom_resize |
| 205 | .borrow() |
| 206 | .clone() |
| 207 | .or_else(|| self.resize.clone()), |
| 208 | ProgramType::Close => self.custom_close.borrow().clone(), |
| 209 | ProgramType::Open => self.custom_open.borrow().clone(), |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | pub fn init(renderer: &mut GlesRenderer) { |
| 215 | let shaders = Shaders::compile(renderer); |
| 216 | let data = renderer.egl_context().user_data(); |
| 217 | if !data.insert_if_missing(|| shaders) { |
| 218 | error!("shaders were already compiled"); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | fn compile_resize_program( |
| 223 | renderer: &mut GlesRenderer, |
| 224 | src: &str, |
| 225 | ) -> Result<ShaderProgram, GlesError> { |
| 226 | let mut program = include_str!("resize_prelude.frag").to_string(); |
| 227 | program.push_str(src); |
| 228 | program.push_str(include_str!("resize_epilogue.frag")); |
| 229 | program.push_str(include_str!("rounding_alpha.frag")); |
| 230 | |
| 231 | ShaderProgram::compile( |
| 232 | renderer, |
| 233 | &program, |
| 234 | &[ |
| 235 | UniformName::new("niri_input_to_curr_geo", UniformType::Matrix3x3), |
| 236 | UniformName::new("niri_curr_geo_to_prev_geo", UniformType::Matrix3x3), |
| 237 | UniformName::new("niri_curr_geo_to_next_geo", UniformType::Matrix3x3), |
| 238 | UniformName::new("niri_curr_geo_size", UniformType::_2f), |
| 239 | UniformName::new("niri_geo_to_tex_prev", UniformType::Matrix3x3), |
| 240 | UniformName::new("niri_geo_to_tex_next", UniformType::Matrix3x3), |
| 241 | UniformName::new("niri_progress", UniformType::_1f), |
| 242 | UniformName::new("niri_clamped_progress", UniformType::_1f), |
| 243 | UniformName::new("niri_corner_radius", UniformType::_4f), |
| 244 | UniformName::new("niri_clip_to_geometry", UniformType::_1f), |
| 245 | ], |
| 246 | &["niri_tex_prev", "niri_tex_next"], |
| 247 | ) |
| 248 | } |
| 249 | |
| 250 | pub fn set_custom_resize_program(renderer: &mut GlesRenderer, src: Option<&str>) { |
| 251 | let program = if let Some(src) = src { |
| 252 | match compile_resize_program(renderer, src) { |
| 253 | Ok(program) => Some(program), |
| 254 | Err(err) => { |
| 255 | warn!("error compiling custom resize shader: {err:?}"); |
| 256 | return; |
| 257 | } |
| 258 | } |
| 259 | } else { |
| 260 | None |
| 261 | }; |
| 262 | |
| 263 | if let Some(prev) = Shaders::get(renderer).replace_custom_resize_program(program) { |
| 264 | if let Err(err) = prev.destroy(renderer) { |
| 265 | warn!("error destroying previous custom resize shader: {err:?}"); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | fn compile_close_program( |
| 271 | renderer: &mut GlesRenderer, |
| 272 | src: &str, |
| 273 | ) -> Result<ShaderProgram, GlesError> { |
| 274 | let mut program = include_str!("close_prelude.frag").to_string(); |
| 275 | program.push_str(src); |
| 276 | program.push_str(include_str!("close_epilogue.frag")); |
| 277 | |
| 278 | ShaderProgram::compile( |
| 279 | renderer, |
| 280 | &program, |
| 281 | &[ |
| 282 | UniformName::new("niri_input_to_geo", UniformType::Matrix3x3), |
| 283 | UniformName::new("niri_geo_size", UniformType::_2f), |
| 284 | UniformName::new("niri_geo_to_tex", UniformType::Matrix3x3), |
| 285 | UniformName::new("niri_progress", UniformType::_1f), |
| 286 | UniformName::new("niri_clamped_progress", UniformType::_1f), |
| 287 | UniformName::new("niri_random_seed", UniformType::_1f), |
| 288 | ], |
| 289 | &["niri_tex"], |
| 290 | ) |
| 291 | } |
| 292 | |
| 293 | pub fn set_custom_close_program(renderer: &mut GlesRenderer, src: Option<&str>) { |
| 294 | let program = if let Some(src) = src { |
| 295 | match compile_close_program(renderer, src) { |
| 296 | Ok(program) => Some(program), |
| 297 | Err(err) => { |
| 298 | warn!("error compiling custom close shader: {err:?}"); |
| 299 | return; |
| 300 | } |
| 301 | } |
| 302 | } else { |
| 303 | None |
| 304 | }; |
| 305 | |
| 306 | if let Some(prev) = Shaders::get(renderer).replace_custom_close_program(program) { |
| 307 | if let Err(err) = prev.destroy(renderer) { |
| 308 | warn!("error destroying previous custom close shader: {err:?}"); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | fn compile_open_program( |
| 314 | renderer: &mut GlesRenderer, |
| 315 | src: &str, |
| 316 | ) -> Result<ShaderProgram, GlesError> { |
| 317 | let mut program = include_str!("open_prelude.frag").to_string(); |
| 318 | program.push_str(src); |
| 319 | program.push_str(include_str!("open_epilogue.frag")); |
| 320 | |
| 321 | ShaderProgram::compile( |
| 322 | renderer, |
| 323 | &program, |
| 324 | &[ |
| 325 | UniformName::new("niri_input_to_geo", UniformType::Matrix3x3), |
| 326 | UniformName::new("niri_geo_size", UniformType::_2f), |
| 327 | UniformName::new("niri_geo_to_tex", UniformType::Matrix3x3), |
| 328 | UniformName::new("niri_progress", UniformType::_1f), |
| 329 | UniformName::new("niri_clamped_progress", UniformType::_1f), |
| 330 | UniformName::new("niri_random_seed", UniformType::_1f), |
| 331 | ], |
| 332 | &["niri_tex"], |
| 333 | ) |
| 334 | } |
| 335 | |
| 336 | pub fn set_custom_open_program(renderer: &mut GlesRenderer, src: Option<&str>) { |
| 337 | let program = if let Some(src) = src { |
| 338 | match compile_open_program(renderer, src) { |
| 339 | Ok(program) => Some(program), |
| 340 | Err(err) => { |
| 341 | warn!("error compiling custom open shader: {err:?}"); |
| 342 | return; |
| 343 | } |
| 344 | } |
| 345 | } else { |
| 346 | None |
| 347 | }; |
| 348 | |
| 349 | if let Some(prev) = Shaders::get(renderer).replace_custom_open_program(program) { |
| 350 | if let Err(err) = prev.destroy(renderer) { |
| 351 | warn!("error destroying previous custom open shader: {err:?}"); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | pub fn mat3_uniform(name: &str, mat: Mat3) -> Uniform<'_> { |
| 357 | Uniform::new( |
| 358 | name, |
| 359 | UniformValue::Matrix3x3 { |
| 360 | matrices: vec![mat.to_cols_array()], |
| 361 | transpose: false, |
| 362 | }, |
| 363 | ) |
| 364 | } |