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