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