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::cmp::max; |
| 2 | use std::iter::{once, zip}; |
| 3 | use std::rc::Rc; |
| 4 | |
| 5 | use anyhow::{ensure, Context as _}; |
| 6 | use smithay::backend::allocator::Fourcc; |
| 7 | use smithay::backend::renderer::gles::{ffi, link_program, GlesError, GlesRenderer, GlesTexture}; |
| 8 | use smithay::backend::renderer::{ContextId, Renderer as _, Texture as _}; |
| 9 | use smithay::gpu_span_location; |
| 10 | use smithay::utils::{Buffer, Size}; |
| 11 | |
| 12 | use crate::render_helpers::shaders::Shaders; |
| 13 | |
| 14 | #[derive(Debug)] |
| 15 | pub struct Blur { |
| 16 | program: BlurProgram, |
| 17 | /// Context ID of the renderer that created the program and the textures. |
| 18 | renderer_context_id: ContextId<GlesTexture>, |
| 19 | /// Output texture followed by intermediate textures, large to small. |
| 20 | /// |
| 21 | /// Created lazily and stored here to avoid recreating blur textures frequently. |
| 22 | textures: Vec<GlesTexture>, |
| 23 | } |
| 24 | |
| 25 | #[derive(Debug, Default, Clone, Copy, PartialEq)] |
| 26 | pub struct BlurOptions { |
| 27 | pub passes: u8, |
| 28 | pub offset: f64, |
| 29 | } |
| 30 | |
| 31 | impl From<niri_config::Blur> for BlurOptions { |
| 32 | fn from(config: niri_config::Blur) -> Self { |
| 33 | Self { |
| 34 | passes: config.passes, |
| 35 | offset: config.offset, |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #[derive(Debug, Clone)] |
| 41 | pub struct BlurProgram(Rc<BlurProgramInner>); |
| 42 | |
| 43 | #[derive(Debug)] |
| 44 | struct BlurProgramInner { |
| 45 | down: BlurProgramInternal, |
| 46 | up: BlurProgramInternal, |
| 47 | } |
| 48 | |
| 49 | #[derive(Debug)] |
| 50 | struct BlurProgramInternal { |
| 51 | program: ffi::types::GLuint, |
| 52 | uniform_tex: ffi::types::GLint, |
| 53 | uniform_half_pixel: ffi::types::GLint, |
| 54 | uniform_offset: ffi::types::GLint, |
| 55 | attrib_vert: ffi::types::GLint, |
| 56 | } |
| 57 | |
| 58 | unsafe fn compile_program(gl: &ffi::Gles2, src: &str) -> Result<BlurProgramInternal, GlesError> { |
| 59 | let program = unsafe { link_program(gl, include_str!("shaders/blur.vert"), src)? }; |
| 60 | |
| 61 | let vert = c"vert"; |
| 62 | let tex = c"tex"; |
| 63 | let half_pixel = c"half_pixel"; |
| 64 | let offset = c"offset"; |
| 65 | |
| 66 | Ok(BlurProgramInternal { |
| 67 | program, |
| 68 | uniform_tex: gl.GetUniformLocation(program, tex.as_ptr()), |
| 69 | uniform_half_pixel: gl.GetUniformLocation(program, half_pixel.as_ptr()), |
| 70 | uniform_offset: gl.GetUniformLocation(program, offset.as_ptr()), |
| 71 | attrib_vert: gl.GetAttribLocation(program, vert.as_ptr()), |
| 72 | }) |
| 73 | } |
| 74 | |
| 75 | impl BlurProgram { |
| 76 | pub fn compile(renderer: &mut GlesRenderer) -> anyhow::Result<Self> { |
| 77 | renderer |
| 78 | .with_context(move |gl| unsafe { |
| 79 | let down = compile_program(gl, include_str!("shaders/blur_down.frag")) |
| 80 | .context("error compiling blur_down shader")?; |
| 81 | let up = compile_program(gl, include_str!("shaders/blur_up.frag")) |
| 82 | .context("error compiling blur_up shader")?; |
| 83 | Ok(Self(Rc::new(BlurProgramInner { down, up }))) |
| 84 | }) |
| 85 | .context("error making GL context current")? |
| 86 | } |
| 87 | |
| 88 | pub fn destroy(self, renderer: &mut GlesRenderer) -> Result<(), GlesError> { |
| 89 | renderer.with_context(move |gl| unsafe { |
| 90 | gl.DeleteProgram(self.0.down.program); |
| 91 | gl.DeleteProgram(self.0.up.program); |
| 92 | }) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl Blur { |
| 97 | pub fn new(renderer: &mut GlesRenderer) -> Option<Self> { |
| 98 | let program = Shaders::get(renderer).blur.clone()?; |
| 99 | Some(Self { |
| 100 | program, |
| 101 | renderer_context_id: renderer.context_id(), |
| 102 | textures: Vec::new(), |
| 103 | }) |
| 104 | } |
| 105 | |
| 106 | pub fn context_id(&self) -> ContextId<GlesTexture> { |
| 107 | self.renderer_context_id.clone() |
| 108 | } |
| 109 | |
| 110 | pub fn prepare_textures( |
| 111 | &mut self, |
| 112 | mut create_texture: impl FnMut(Fourcc, Size<i32, Buffer>) -> Result<GlesTexture, GlesError>, |
| 113 | source: &GlesTexture, |
| 114 | options: BlurOptions, |
| 115 | ) -> anyhow::Result<()> { |
| 116 | let _span = tracy_client::span!("Blur::prepare_textures"); |
| 117 | |
| 118 | let passes = options.passes.clamp(1, 31) as usize; |
| 119 | let size = source.size(); |
| 120 | |
| 121 | if let Some(output) = self.textures.first_mut() { |
| 122 | let old_size = output.size(); |
| 123 | if old_size != size { |
| 124 | trace!( |
| 125 | "recreating textures: output size changed from {} × {} to {} × {}", |
| 126 | old_size.w, |
| 127 | old_size.h, |
| 128 | size.w, |
| 129 | size.h |
| 130 | ); |
| 131 | self.textures.clear(); |
| 132 | } else if !output.is_unique_reference() { |
| 133 | debug!("recreating textures: not unique",); |
| 134 | // We only need to recreate the output texture here, but this case shouldn't really |
| 135 | // happen anyway, and this is simpler. |
| 136 | self.textures.clear(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Create any missing textures. |
| 141 | let mut w = size.w; |
| 142 | let mut h = size.h; |
| 143 | for i in 0..=passes { |
| 144 | let size = Size::new(w, h); |
| 145 | w = max(1, w / 2); |
| 146 | h = max(1, h / 2); |
| 147 | |
| 148 | if self.textures.len() > i { |
| 149 | // This texture already exists. |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | // debug!("creating texture for step {i} sized {w} × {h}"); |
| 154 | |
| 155 | let texture: GlesTexture = |
| 156 | create_texture(Fourcc::Abgr8888, size).context("error creating texture")?; |
| 157 | self.textures.push(texture); |
| 158 | } |
| 159 | |
| 160 | // Drop any no longer needed textures. |
| 161 | self.textures.drain(passes + 1..); |
| 162 | |
| 163 | Ok(()) |
| 164 | } |
| 165 | |
| 166 | pub fn render( |
| 167 | &mut self, |
| 168 | renderer: &mut GlesRenderer, |
| 169 | source: &GlesTexture, |
| 170 | options: BlurOptions, |
| 171 | ) -> anyhow::Result<GlesTexture> { |
| 172 | let _span = tracy_client::span!("Blur::render"); |
| 173 | trace!("rendering blur"); |
| 174 | |
| 175 | ensure!( |
| 176 | renderer.context_id() == self.renderer_context_id, |
| 177 | "wrong renderer" |
| 178 | ); |
| 179 | |
| 180 | let passes = options.passes.clamp(1, 31) as usize; |
| 181 | let size = source.size(); |
| 182 | |
| 183 | ensure!( |
| 184 | self.textures.len() == passes + 1, |
| 185 | "wrong textures len: expected {}, got {}", |
| 186 | passes + 1, |
| 187 | self.textures.len() |
| 188 | ); |
| 189 | |
| 190 | let output = &mut self.textures[0]; |
| 191 | ensure!( |
| 192 | output.size() == size, |
| 193 | "wrong output texture size: expected {size:?}, got {:?}", |
| 194 | output.size() |
| 195 | ); |
| 196 | |
| 197 | ensure!( |
| 198 | output.is_unique_reference(), |
| 199 | "output texture has a non-unique reference" |
| 200 | ); |
| 201 | |
| 202 | renderer.with_profiled_context(gpu_span_location!("Blur::render"), |gl| unsafe { |
| 203 | while gl.GetError() != ffi::NO_ERROR {} |
| 204 | |
| 205 | gl.Disable(ffi::BLEND); |
| 206 | gl.Disable(ffi::SCISSOR_TEST); |
| 207 | |
| 208 | gl.ActiveTexture(ffi::TEXTURE0); |
| 209 | |
| 210 | let mut fbos = [0; 2]; |
| 211 | gl.GenFramebuffers(fbos.len() as _, fbos.as_mut_ptr()); |
| 212 | gl.BindFramebuffer(ffi::FRAMEBUFFER, fbos[0]); |
| 213 | |
| 214 | let program = &self.program.0.down; |
| 215 | gl.UseProgram(program.program); |
| 216 | gl.Uniform1i(program.uniform_tex, 0); |
| 217 | gl.Uniform1f(program.uniform_offset, options.offset as f32); |
| 218 | |
| 219 | let vertices: [f32; 12] = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0]; |
| 220 | gl.EnableVertexAttribArray(program.attrib_vert as u32); |
| 221 | gl.BindBuffer(ffi::ARRAY_BUFFER, 0); |
| 222 | gl.VertexAttribPointer( |
| 223 | program.attrib_vert as u32, |
| 224 | 2, |
| 225 | ffi::FLOAT, |
| 226 | ffi::FALSE, |
| 227 | 0, |
| 228 | vertices.as_ptr().cast(), |
| 229 | ); |
| 230 | |
| 231 | let src = once(source).chain(&self.textures[1..]); |
| 232 | let dst = &self.textures[1..]; |
| 233 | for (src, dst) in zip(src, dst) { |
| 234 | let dst_size = dst.size(); |
| 235 | let w = dst_size.w; |
| 236 | let h = dst_size.h; |
| 237 | gl.Viewport(0, 0, w, h); |
| 238 | |
| 239 | // During downsampling, half_pixel is half of the destination pixel. |
| 240 | gl.Uniform2f(program.uniform_half_pixel, 0.5 / w as f32, 0.5 / h as f32); |
| 241 | |
| 242 | let src = src.tex_id(); |
| 243 | let dst = dst.tex_id(); |
| 244 | |
| 245 | trace!("drawing down {src} to {dst}"); |
| 246 | gl.FramebufferTexture2D( |
| 247 | ffi::FRAMEBUFFER, |
| 248 | ffi::COLOR_ATTACHMENT0, |
| 249 | ffi::TEXTURE_2D, |
| 250 | dst, |
| 251 | 0, |
| 252 | ); |
| 253 | |
| 254 | gl.BindTexture(ffi::TEXTURE_2D, src); |
| 255 | gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_MIN_FILTER, ffi::LINEAR as i32); |
| 256 | gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_MAG_FILTER, ffi::LINEAR as i32); |
| 257 | gl.TexParameteri( |
| 258 | ffi::TEXTURE_2D, |
| 259 | ffi::TEXTURE_WRAP_S, |
| 260 | ffi::CLAMP_TO_EDGE as i32, |
| 261 | ); |
| 262 | gl.TexParameteri( |
| 263 | ffi::TEXTURE_2D, |
| 264 | ffi::TEXTURE_WRAP_T, |
| 265 | ffi::CLAMP_TO_EDGE as i32, |
| 266 | ); |
| 267 | |
| 268 | gl.DrawArrays(ffi::TRIANGLES, 0, 6); |
| 269 | } |
| 270 | |
| 271 | gl.DisableVertexAttribArray(program.attrib_vert as u32); |
| 272 | |
| 273 | // Up |
| 274 | let program = &self.program.0.up; |
| 275 | gl.UseProgram(program.program); |
| 276 | gl.Uniform1i(program.uniform_tex, 0); |
| 277 | gl.Uniform1f(program.uniform_offset, options.offset as f32); |
| 278 | |
| 279 | let vertices: [f32; 12] = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0]; |
| 280 | gl.EnableVertexAttribArray(program.attrib_vert as u32); |
| 281 | gl.BindBuffer(ffi::ARRAY_BUFFER, 0); |
| 282 | gl.VertexAttribPointer( |
| 283 | program.attrib_vert as u32, |
| 284 | 2, |
| 285 | ffi::FLOAT, |
| 286 | ffi::FALSE, |
| 287 | 0, |
| 288 | vertices.as_ptr().cast(), |
| 289 | ); |
| 290 | |
| 291 | let src = self.textures.iter().rev(); |
| 292 | let dst = self.textures.iter().rev().skip(1); |
| 293 | for (src, dst) in zip(src, dst) { |
| 294 | let dst_size = dst.size(); |
| 295 | let w = dst_size.w; |
| 296 | let h = dst_size.h; |
| 297 | gl.Viewport(0, 0, w, h); |
| 298 | |
| 299 | // During upsampling, half_pixel is half of the source pixel. |
| 300 | let src_size = src.size(); |
| 301 | let src_w = src_size.w as f32; |
| 302 | let src_h = src_size.h as f32; |
| 303 | gl.Uniform2f(program.uniform_half_pixel, 0.5 / src_w, 0.5 / src_h); |
| 304 | |
| 305 | let src = src.tex_id(); |
| 306 | let dst = dst.tex_id(); |
| 307 | |
| 308 | trace!("drawing up {src} to {dst}"); |
| 309 | gl.FramebufferTexture2D( |
| 310 | ffi::FRAMEBUFFER, |
| 311 | ffi::COLOR_ATTACHMENT0, |
| 312 | ffi::TEXTURE_2D, |
| 313 | dst, |
| 314 | 0, |
| 315 | ); |
| 316 | |
| 317 | gl.BindTexture(ffi::TEXTURE_2D, src); |
| 318 | gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_MIN_FILTER, ffi::LINEAR as i32); |
| 319 | gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_MAG_FILTER, ffi::LINEAR as i32); |
| 320 | gl.TexParameteri( |
| 321 | ffi::TEXTURE_2D, |
| 322 | ffi::TEXTURE_WRAP_S, |
| 323 | ffi::CLAMP_TO_EDGE as i32, |
| 324 | ); |
| 325 | gl.TexParameteri( |
| 326 | ffi::TEXTURE_2D, |
| 327 | ffi::TEXTURE_WRAP_T, |
| 328 | ffi::CLAMP_TO_EDGE as i32, |
| 329 | ); |
| 330 | |
| 331 | gl.DrawArrays(ffi::TRIANGLES, 0, 6); |
| 332 | } |
| 333 | |
| 334 | gl.DisableVertexAttribArray(program.attrib_vert as u32); |
| 335 | |
| 336 | gl.BindFramebuffer(ffi::FRAMEBUFFER, 0); |
| 337 | gl.DeleteFramebuffers(fbos.len() as _, fbos.as_ptr()); |
| 338 | })?; |
| 339 | |
| 340 | Ok(self.textures[0].clone()) |
| 341 | } |
| 342 | } |