Implement custom shader for window-close anim
Browse files at this commit9004c8395438038d69f7b479a8a9035663cb4be0Parent: 29c755214 changed files +361 -30
- Cargo.lock +8 -7
- Cargo.toml +1 -0
- niri-config/src/lib.rs +30 -13
- src/backend/tty.rs +3 -0
- src/backend/winit.rs +3 -0
- src/layout/closing_window.rs +54 -6
- src/layout/workspace.rs +2 -2
- src/niri.rs +10 -0
- src/render_helpers/shaders/close_epilogue.frag +16 -0
- src/render_helpers/shaders/close_prelude.frag +21 -0
- src/render_helpers/shaders/mod.rs +54 -0
- wiki/Configuration:-Animations.md +41 -0
- wiki/examples/close_custom_shader.frag +113 -0
- wiki/examples/resize_custom_shader.frag +5 -2
Cargo.lockmodified+8 -7
| @@ -186,7 +186,7 @@ dependencies = [ | |||
| 186 | "async-lock 3.3.0", | 186 | "async-lock 3.3.0", |
| 187 | "async-task", | 187 | "async-task", |
| 188 | "concurrent-queue", | 188 | "concurrent-queue", |
| 189 | - "fastrand 2.0.2", | 189 | + "fastrand 2.1.0", |
| 190 | "futures-lite 2.3.0", | 190 | "futures-lite 2.3.0", |
| 191 | "slab", | 191 | "slab", |
| 192 | ] | 192 | ] |
| @@ -434,7 +434,7 @@ dependencies = [ | |||
| 434 | "async-channel", | 434 | "async-channel", |
| 435 | "async-lock 3.3.0", | 435 | "async-lock 3.3.0", |
| 436 | "async-task", | 436 | "async-task", |
| 437 | - "fastrand 2.0.2", | 437 | + "fastrand 2.1.0", |
| 438 | "futures-io", | 438 | "futures-io", |
| 439 | "futures-lite 2.3.0", | 439 | "futures-lite 2.3.0", |
| 440 | "piper", | 440 | "piper", |
| @@ -1081,9 +1081,9 @@ dependencies = [ | |||
| 1081 | 1081 | ||
| 1082 | [[package]] | 1082 | [[package]] |
| 1083 | name = "fastrand" | 1083 | name = "fastrand" |
| 1084 | -version = "2.0.2" | 1084 | +version = "2.1.0" |
| 1085 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1085 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1086 | -checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984" | 1086 | +checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" |
| 1087 | 1087 | ||
| 1088 | [[package]] | 1088 | [[package]] |
| 1089 | name = "fdeflate" | 1089 | name = "fdeflate" |
| @@ -1225,7 +1225,7 @@ version = "2.3.0" | |||
| 1225 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1225 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1226 | checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5" | 1226 | checksum = "52527eb5074e35e9339c6b4e8d12600c7128b68fb25dcb9fa9dec18f7c25f3a5" |
| 1227 | dependencies = [ | 1227 | dependencies = [ |
| 1228 | - "fastrand 2.0.2", | 1228 | + "fastrand 2.1.0", |
| 1229 | "futures-core", | 1229 | "futures-core", |
| 1230 | "futures-io", | 1230 | "futures-io", |
| 1231 | "parking", | 1231 | "parking", |
| @@ -2203,6 +2203,7 @@ dependencies = [ | |||
| 2203 | "clap", | 2203 | "clap", |
| 2204 | "directories", | 2204 | "directories", |
| 2205 | "drm-ffi 0.7.1", | 2205 | "drm-ffi 0.7.1", |
| 2206 | + "fastrand 2.1.0", | ||
| 2206 | "futures-util", | 2207 | "futures-util", |
| 2207 | "git-version", | 2208 | "git-version", |
| 2208 | "glam", | 2209 | "glam", |
| @@ -2583,7 +2584,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 2583 | checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4" | 2584 | checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4" |
| 2584 | dependencies = [ | 2585 | dependencies = [ |
| 2585 | "atomic-waker", | 2586 | "atomic-waker", |
| 2586 | - "fastrand 2.0.2", | 2587 | + "fastrand 2.1.0", |
| 2587 | "futures-io", | 2588 | "futures-io", |
| 2588 | ] | 2589 | ] |
| 2589 | 2590 | ||
| @@ -3337,7 +3338,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 3337 | checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" | 3338 | checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" |
| 3338 | dependencies = [ | 3339 | dependencies = [ |
| 3339 | "cfg-if", | 3340 | "cfg-if", |
| 3340 | - "fastrand 2.0.2", | 3341 | + "fastrand 2.1.0", |
| 3341 | "rustix 0.38.32", | 3342 | "rustix 0.38.32", |
| 3342 | "windows-sys 0.52.0", | 3343 | "windows-sys 0.52.0", |
| 3343 | ] | 3344 | ] |
Cargo.tomlmodified+1 -0
| @@ -51,6 +51,7 @@ calloop = { version = "0.13.0", features = ["executor", "futures-io"] } | |||
| 51 | clap = { workspace = true, features = ["string"] } | 51 | clap = { workspace = true, features = ["string"] } |
| 52 | directories = "5.0.1" | 52 | directories = "5.0.1" |
| 53 | drm-ffi = "0.7.1" | 53 | drm-ffi = "0.7.1" |
| 54 | +fastrand = "2.1.0" | ||
| 54 | futures-util = { version = "0.3.30", default-features = false, features = ["std", "io"] } | 55 | futures-util = { version = "0.3.30", default-features = false, features = ["std", "io"] } |
| 55 | git-version = "0.3.9" | 56 | git-version = "0.3.9" |
| 56 | glam = "0.27.0" | 57 | glam = "0.27.0" |
niri-config/src/lib.rsmodified+30 -13
| @@ -552,18 +552,24 @@ impl Default for WindowOpenAnim { | |||
| 552 | } | 552 | } |
| 553 | } | 553 | } |
| 554 | 554 | ||
| 555 | -#[derive(Debug, Clone, Copy, PartialEq)] | 555 | +#[derive(Debug, Clone, PartialEq)] |
| 556 | -pub struct WindowCloseAnim(pub Animation); | 556 | +pub struct WindowCloseAnim { |
| 557 | + pub anim: Animation, | ||
| 558 | + pub custom_shader: Option<String>, | ||
| 559 | +} | ||
| 557 | 560 | ||
| 558 | impl Default for WindowCloseAnim { | 561 | impl Default for WindowCloseAnim { |
| 559 | fn default() -> Self { | 562 | fn default() -> Self { |
| 560 | - Self(Animation { | 563 | + Self { |
| 561 | - off: false, | 564 | + anim: Animation { |
| 562 | - kind: AnimationKind::Easing(EasingParams { | 565 | + off: false, |
| 563 | - duration_ms: 150, | 566 | + kind: AnimationKind::Easing(EasingParams { |
| 564 | - curve: AnimationCurve::EaseOutQuad, | 567 | + duration_ms: 150, |
| 565 | - }), | 568 | + curve: AnimationCurve::EaseOutQuad, |
| 566 | - }) | 569 | + }), |
| 570 | + }, | ||
| 571 | + custom_shader: None, | ||
| 572 | + } | ||
| 567 | } | 573 | } |
| 568 | } | 574 | } |
| 569 | 575 | ||
| @@ -1420,10 +1426,21 @@ where | |||
| 1420 | node: &knuffel::ast::SpannedNode<S>, | 1426 | node: &knuffel::ast::SpannedNode<S>, |
| 1421 | ctx: &mut knuffel::decode::Context<S>, | 1427 | ctx: &mut knuffel::decode::Context<S>, |
| 1422 | ) -> Result<Self, DecodeError<S>> { | 1428 | ) -> Result<Self, DecodeError<S>> { |
| 1423 | - let default = Self::default().0; | 1429 | + let default = Self::default().anim; |
| 1424 | - Ok(Self(Animation::decode_node(node, ctx, default, |_, _| { | 1430 | + let mut custom_shader = None; |
| 1425 | - Ok(false) | 1431 | + let anim = Animation::decode_node(node, ctx, default, |child, ctx| { |
| 1426 | - })?)) | 1432 | + if &**child.node_name == "custom-shader" { |
| 1433 | + custom_shader = parse_arg_node("custom-shader", child, ctx)?; | ||
| 1434 | + Ok(true) | ||
| 1435 | + } else { | ||
| 1436 | + Ok(false) | ||
| 1437 | + } | ||
| 1438 | + })?; | ||
| 1439 | + | ||
| 1440 | + Ok(Self { | ||
| 1441 | + anim, | ||
| 1442 | + custom_shader, | ||
| 1443 | + }) | ||
| 1427 | } | 1444 | } |
| 1428 | } | 1445 | } |
| 1429 | 1446 | ||
src/backend/tty.rsmodified+3 -0
| @@ -500,6 +500,9 @@ impl Tty { | |||
| 500 | if let Some(src) = config.animations.window_resize.custom_shader.as_deref() { | 500 | if let Some(src) = config.animations.window_resize.custom_shader.as_deref() { |
| 501 | shaders::set_custom_resize_program(gles_renderer, Some(src)); | 501 | shaders::set_custom_resize_program(gles_renderer, Some(src)); |
| 502 | } | 502 | } |
| 503 | + if let Some(src) = config.animations.window_close.custom_shader.as_deref() { | ||
| 504 | + shaders::set_custom_close_program(gles_renderer, Some(src)); | ||
| 505 | + } | ||
| 503 | drop(config); | 506 | drop(config); |
| 504 | 507 | ||
| 505 | niri.layout.update_shaders(); | 508 | niri.layout.update_shaders(); |
src/backend/winit.rsmodified+3 -0
| @@ -140,6 +140,9 @@ impl Winit { | |||
| 140 | if let Some(src) = config.animations.window_resize.custom_shader.as_deref() { | 140 | if let Some(src) = config.animations.window_resize.custom_shader.as_deref() { |
| 141 | shaders::set_custom_resize_program(renderer, Some(src)); | 141 | shaders::set_custom_resize_program(renderer, Some(src)); |
| 142 | } | 142 | } |
| 143 | + if let Some(src) = config.animations.window_close.custom_shader.as_deref() { | ||
| 144 | + shaders::set_custom_close_program(renderer, Some(src)); | ||
| 145 | + } | ||
| 143 | drop(config); | 146 | drop(config); |
| 144 | 147 | ||
| 145 | niri.layout.update_shaders(); | 148 | niri.layout.update_shaders(); |
src/layout/closing_window.rsmodified+54 -6
| @@ -1,6 +1,8 @@ | |||
| 1 | +use std::collections::HashMap; | ||
| 1 | use std::time::Duration; | 2 | use std::time::Duration; |
| 2 | 3 | ||
| 3 | use anyhow::Context as _; | 4 | use anyhow::Context as _; |
| 5 | +use glam::{Mat3, Vec2}; | ||
| 4 | use niri_config::BlockOutFrom; | 6 | use niri_config::BlockOutFrom; |
| 5 | use smithay::backend::allocator::Fourcc; | 7 | use smithay::backend::allocator::Fourcc; |
| 6 | use smithay::backend::renderer::element::texture::TextureRenderElement; | 8 | use smithay::backend::renderer::element::texture::TextureRenderElement; |
| @@ -8,13 +10,15 @@ use smithay::backend::renderer::element::utils::{ | |||
| 8 | Relocate, RelocateRenderElement, RescaleRenderElement, | 10 | Relocate, RelocateRenderElement, RescaleRenderElement, |
| 9 | }; | 11 | }; |
| 10 | use smithay::backend::renderer::element::{Id, Kind, RenderElement}; | 12 | use smithay::backend::renderer::element::{Id, Kind, RenderElement}; |
| 11 | -use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | 13 | +use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture, Uniform}; |
| 12 | -use smithay::backend::renderer::Renderer as _; | 14 | +use smithay::backend::renderer::{Renderer as _, Texture}; |
| 13 | use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform}; | 15 | use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform}; |
| 14 | 16 | ||
| 15 | use crate::animation::Animation; | 17 | use crate::animation::Animation; |
| 16 | use crate::niri_render_elements; | 18 | use crate::niri_render_elements; |
| 17 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; | 19 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| 20 | +use crate::render_helpers::shader_element::ShaderRenderElement; | ||
| 21 | +use crate::render_helpers::shaders::{mat3_uniform, ProgramType, Shaders}; | ||
| 18 | use crate::render_helpers::snapshot::RenderSnapshot; | 22 | use crate::render_helpers::snapshot::RenderSnapshot; |
| 19 | use crate::render_helpers::{render_to_encompassing_texture, RenderTarget}; | 23 | use crate::render_helpers::{render_to_encompassing_texture, RenderTarget}; |
| 20 | 24 | ||
| @@ -49,11 +53,15 @@ pub struct ClosingWindow { | |||
| 49 | 53 | ||
| 50 | /// The closing animation. | 54 | /// The closing animation. |
| 51 | anim: Animation, | 55 | anim: Animation, |
| 56 | + | ||
| 57 | + /// Random seed for the shader. | ||
| 58 | + random_seed: f32, | ||
| 52 | } | 59 | } |
| 53 | 60 | ||
| 54 | niri_render_elements! { | 61 | niri_render_elements! { |
| 55 | ClosingWindowRenderElement => { | 62 | ClosingWindowRenderElement => { |
| 56 | Texture = RelocateRenderElement<RescaleRenderElement<PrimaryGpuTextureRenderElement>>, | 63 | Texture = RelocateRenderElement<RescaleRenderElement<PrimaryGpuTextureRenderElement>>, |
| 64 | + Shader = ShaderRenderElement, | ||
| 57 | } | 65 | } |
| 58 | } | 66 | } |
| 59 | 67 | ||
| @@ -100,6 +108,7 @@ impl ClosingWindow { | |||
| 100 | texture_offset, | 108 | texture_offset, |
| 101 | blocked_out_texture_offset, | 109 | blocked_out_texture_offset, |
| 102 | anim, | 110 | anim, |
| 111 | + random_seed: fastrand::f32(), | ||
| 103 | }) | 112 | }) |
| 104 | } | 113 | } |
| 105 | 114 | ||
| @@ -108,16 +117,18 @@ impl ClosingWindow { | |||
| 108 | } | 117 | } |
| 109 | 118 | ||
| 110 | pub fn are_animations_ongoing(&self) -> bool { | 119 | pub fn are_animations_ongoing(&self) -> bool { |
| 111 | - !self.anim.is_clamped_done() | 120 | + !self.anim.is_done() |
| 112 | } | 121 | } |
| 113 | 122 | ||
| 114 | pub fn render( | 123 | pub fn render( |
| 115 | &self, | 124 | &self, |
| 125 | + renderer: &mut GlesRenderer, | ||
| 116 | view_rect: Rectangle<i32, Logical>, | 126 | view_rect: Rectangle<i32, Logical>, |
| 117 | scale: Scale<f64>, | 127 | scale: Scale<f64>, |
| 118 | target: RenderTarget, | 128 | target: RenderTarget, |
| 119 | ) -> ClosingWindowRenderElement { | 129 | ) -> ClosingWindowRenderElement { |
| 120 | - let val = self.anim.clamped_value(); | 130 | + let progress = self.anim.value(); |
| 131 | + let clamped_progress = self.anim.clamped_value().clamp(0., 1.); | ||
| 121 | 132 | ||
| 122 | let (texture, offset) = if target.should_block_out(self.block_out_from) { | 133 | let (texture, offset) = if target.should_block_out(self.block_out_from) { |
| 123 | (&self.blocked_out_texture, self.blocked_out_texture_offset) | 134 | (&self.blocked_out_texture, self.blocked_out_texture_offset) |
| @@ -125,6 +136,43 @@ impl ClosingWindow { | |||
| 125 | (&self.texture, self.texture_offset) | 136 | (&self.texture, self.texture_offset) |
| 126 | }; | 137 | }; |
| 127 | 138 | ||
| 139 | + if Shaders::get(renderer).program(ProgramType::Close).is_some() { | ||
| 140 | + let area_loc = Vec2::new(view_rect.loc.x as f32, view_rect.loc.y as f32); | ||
| 141 | + let area_size = Vec2::new(view_rect.size.w as f32, view_rect.size.h as f32); | ||
| 142 | + | ||
| 143 | + let geo_loc = Vec2::new(self.pos.x as f32, self.pos.y as f32); | ||
| 144 | + let geo_size = Vec2::new(self.geo_size.w as f32, self.geo_size.h as f32); | ||
| 145 | + | ||
| 146 | + let input_to_geo = Mat3::from_scale(area_size / geo_size) | ||
| 147 | + * Mat3::from_translation((area_loc - geo_loc) / area_size); | ||
| 148 | + | ||
| 149 | + let tex_scale = Vec2::new(self.texture_scale.x as f32, self.texture_scale.y as f32); | ||
| 150 | + let tex_loc = Vec2::new(offset.x as f32, offset.y as f32); | ||
| 151 | + let tex_size = Vec2::new(texture.width() as f32, texture.height() as f32) / tex_scale; | ||
| 152 | + | ||
| 153 | + let geo_to_tex = | ||
| 154 | + Mat3::from_translation(-tex_loc / tex_size) * Mat3::from_scale(geo_size / tex_size); | ||
| 155 | + | ||
| 156 | + return ShaderRenderElement::new( | ||
| 157 | + ProgramType::Close, | ||
| 158 | + view_rect.size, | ||
| 159 | + None, | ||
| 160 | + 1., | ||
| 161 | + vec![ | ||
| 162 | + mat3_uniform("niri_input_to_geo", input_to_geo), | ||
| 163 | + Uniform::new("niri_geo_size", geo_size.to_array()), | ||
| 164 | + mat3_uniform("niri_geo_to_tex", geo_to_tex), | ||
| 165 | + Uniform::new("niri_progress", progress as f32), | ||
| 166 | + Uniform::new("niri_clamped_progress", clamped_progress as f32), | ||
| 167 | + Uniform::new("niri_random_seed", self.random_seed), | ||
| 168 | + ], | ||
| 169 | + HashMap::from([(String::from("niri_tex"), texture.clone())]), | ||
| 170 | + Kind::Unspecified, | ||
| 171 | + ) | ||
| 172 | + .with_location(Point::from((0, 0))) | ||
| 173 | + .into(); | ||
| 174 | + } | ||
| 175 | + | ||
| 128 | let elem = TextureRenderElement::from_static_texture( | 176 | let elem = TextureRenderElement::from_static_texture( |
| 129 | Id::new(), | 177 | Id::new(), |
| 130 | self.texture_renderer_id, | 178 | self.texture_renderer_id, |
| @@ -132,7 +180,7 @@ impl ClosingWindow { | |||
| 132 | texture.clone(), | 180 | texture.clone(), |
| 133 | self.texture_scale.x as i32, | 181 | self.texture_scale.x as i32, |
| 134 | Transform::Normal, | 182 | Transform::Normal, |
| 135 | - Some(val.clamp(0., 1.) as f32), | 183 | + Some(1. - clamped_progress as f32), |
| 136 | None, | 184 | None, |
| 137 | None, | 185 | None, |
| 138 | None, | 186 | None, |
| @@ -145,7 +193,7 @@ impl ClosingWindow { | |||
| 145 | let elem = RescaleRenderElement::from_element( | 193 | let elem = RescaleRenderElement::from_element( |
| 146 | elem, | 194 | elem, |
| 147 | (center - offset).to_physical_precise_round(scale), | 195 | (center - offset).to_physical_precise_round(scale), |
| 148 | - (val / 5. + 0.8).max(0.), | 196 | + ((1. - clamped_progress) / 5. + 0.8).max(0.), |
| 149 | ); | 197 | ); |
| 150 | 198 | ||
| 151 | let mut location = self.pos.to_f64() + offset; | 199 | let mut location = self.pos.to_f64() + offset; |
src/layout/workspace.rsmodified+2 -2
| @@ -1344,7 +1344,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1344 | tile_pos.x -= offset; | 1344 | tile_pos.x -= offset; |
| 1345 | } | 1345 | } |
| 1346 | 1346 | ||
| 1347 | - let anim = Animation::new(1., 0., 0., self.options.animations.window_close.0); | 1347 | + let anim = Animation::new(0., 1., 0., self.options.animations.window_close.anim); |
| 1348 | 1348 | ||
| 1349 | let res = ClosingWindow::new( | 1349 | let res = ClosingWindow::new( |
| 1350 | renderer, | 1350 | renderer, |
| @@ -2095,7 +2095,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 2095 | // Draw the closing windows on top. | 2095 | // Draw the closing windows on top. |
| 2096 | let view_rect = Rectangle::from_loc_and_size((self.view_pos(), 0), self.view_size); | 2096 | let view_rect = Rectangle::from_loc_and_size((self.view_pos(), 0), self.view_size); |
| 2097 | for closing in &self.closing_windows { | 2097 | for closing in &self.closing_windows { |
| 2098 | - let elem = closing.render(view_rect, output_scale, target); | 2098 | + let elem = closing.render(renderer.as_gles_renderer(), view_rect, output_scale, target); |
| 2099 | rv.push(elem.into()); | 2099 | rv.push(elem.into()); |
| 2100 | } | 2100 | } |
| 2101 | 2101 | ||
src/niri.rsmodified+10 -0
| @@ -941,6 +941,16 @@ impl State { | |||
| 941 | shaders_changed = true; | 941 | shaders_changed = true; |
| 942 | } | 942 | } |
| 943 | 943 | ||
| 944 | + if config.animations.window_close.custom_shader | ||
| 945 | + != old_config.animations.window_close.custom_shader | ||
| 946 | + { | ||
| 947 | + let src = config.animations.window_close.custom_shader.as_deref(); | ||
| 948 | + self.backend.with_primary_renderer(|renderer| { | ||
| 949 | + shaders::set_custom_close_program(renderer, src); | ||
| 950 | + }); | ||
| 951 | + shaders_changed = true; | ||
| 952 | + } | ||
| 953 | + | ||
| 944 | if config.debug != old_config.debug { | 954 | if config.debug != old_config.debug { |
| 945 | debug_config_changed = true; | 955 | debug_config_changed = true; |
| 946 | } | 956 | } |
src/render_helpers/shaders/close_epilogue.fragadded+16 -0
| @@ -0,0 +1,16 @@ | |||
| 1 | + | ||
| 2 | +void main() { | ||
| 3 | + vec3 coords_geo = niri_input_to_geo * vec3(niri_v_coords, 1.0); | ||
| 4 | + vec3 size_geo = vec3(niri_geo_size, 1.0); | ||
| 5 | + | ||
| 6 | + vec4 color = close_color(coords_geo, size_geo); | ||
| 7 | + | ||
| 8 | + color = color * niri_alpha; | ||
| 9 | + | ||
| 10 | +#if defined(DEBUG_FLAGS) | ||
| 11 | + if (niri_tint == 1.0) | ||
| 12 | + color = vec4(0.0, 0.2, 0.0, 0.2) + color * 0.8; | ||
| 13 | +#endif | ||
| 14 | + | ||
| 15 | + gl_FragColor = color; | ||
| 16 | +} | ||
src/render_helpers/shaders/close_prelude.fragadded+21 -0
| @@ -0,0 +1,21 @@ | |||
| 1 | +precision mediump float; | ||
| 2 | + | ||
| 3 | +#if defined(DEBUG_FLAGS) | ||
| 4 | +uniform float niri_tint; | ||
| 5 | +#endif | ||
| 6 | + | ||
| 7 | +varying vec2 niri_v_coords; | ||
| 8 | +uniform vec2 niri_size; | ||
| 9 | + | ||
| 10 | +uniform mat3 niri_input_to_geo; | ||
| 11 | +uniform vec2 niri_geo_size; | ||
| 12 | + | ||
| 13 | +uniform sampler2D niri_tex; | ||
| 14 | +uniform mat3 niri_geo_to_tex; | ||
| 15 | + | ||
| 16 | +uniform float niri_progress; | ||
| 17 | +uniform float niri_clamped_progress; | ||
| 18 | +uniform float niri_random_seed; | ||
| 19 | + | ||
| 20 | +uniform float niri_alpha; | ||
| 21 | + | ||
src/render_helpers/shaders/mod.rsmodified+54 -0
| @@ -14,12 +14,14 @@ pub struct Shaders { | |||
| 14 | pub clipped_surface: Option<GlesTexProgram>, | 14 | pub clipped_surface: Option<GlesTexProgram>, |
| 15 | pub resize: Option<ShaderProgram>, | 15 | pub resize: Option<ShaderProgram>, |
| 16 | pub custom_resize: RefCell<Option<ShaderProgram>>, | 16 | pub custom_resize: RefCell<Option<ShaderProgram>>, |
| 17 | + pub custom_close: RefCell<Option<ShaderProgram>>, | ||
| 17 | } | 18 | } |
| 18 | 19 | ||
| 19 | #[derive(Debug, Clone, Copy)] | 20 | #[derive(Debug, Clone, Copy)] |
| 20 | pub enum ProgramType { | 21 | pub enum ProgramType { |
| 21 | Border, | 22 | Border, |
| 22 | Resize, | 23 | Resize, |
| 24 | + Close, | ||
| 23 | } | 25 | } |
| 24 | 26 | ||
| 25 | impl Shaders { | 27 | impl Shaders { |
| @@ -72,6 +74,7 @@ impl Shaders { | |||
| 72 | clipped_surface, | 74 | clipped_surface, |
| 73 | resize, | 75 | resize, |
| 74 | custom_resize: RefCell::new(None), | 76 | custom_resize: RefCell::new(None), |
| 77 | + custom_close: RefCell::new(None), | ||
| 75 | } | 78 | } |
| 76 | } | 79 | } |
| 77 | 80 | ||
| @@ -95,6 +98,13 @@ impl Shaders { | |||
| 95 | self.custom_resize.replace(program) | 98 | self.custom_resize.replace(program) |
| 96 | } | 99 | } |
| 97 | 100 | ||
| 101 | + pub fn replace_custom_close_program( | ||
| 102 | + &self, | ||
| 103 | + program: Option<ShaderProgram>, | ||
| 104 | + ) -> Option<ShaderProgram> { | ||
| 105 | + self.custom_close.replace(program) | ||
| 106 | + } | ||
| 107 | + | ||
| 98 | pub fn program(&self, program: ProgramType) -> Option<ShaderProgram> { | 108 | pub fn program(&self, program: ProgramType) -> Option<ShaderProgram> { |
| 99 | match program { | 109 | match program { |
| 100 | ProgramType::Border => self.border.clone(), | 110 | ProgramType::Border => self.border.clone(), |
| @@ -103,6 +113,7 @@ impl Shaders { | |||
| 103 | .borrow() | 113 | .borrow() |
| 104 | .clone() | 114 | .clone() |
| 105 | .or_else(|| self.resize.clone()), | 115 | .or_else(|| self.resize.clone()), |
| 116 | + ProgramType::Close => self.custom_close.borrow().clone(), | ||
| 106 | } | 117 | } |
| 107 | } | 118 | } |
| 108 | } | 119 | } |
| @@ -162,6 +173,49 @@ pub fn set_custom_resize_program(renderer: &mut GlesRenderer, src: Option<&str>) | |||
| 162 | } | 173 | } |
| 163 | } | 174 | } |
| 164 | 175 | ||
| 176 | +fn compile_close_program( | ||
| 177 | + renderer: &mut GlesRenderer, | ||
| 178 | + src: &str, | ||
| 179 | +) -> Result<ShaderProgram, GlesError> { | ||
| 180 | + let mut program = include_str!("close_prelude.frag").to_string(); | ||
| 181 | + program.push_str(src); | ||
| 182 | + program.push_str(include_str!("close_epilogue.frag")); | ||
| 183 | + | ||
| 184 | + ShaderProgram::compile( | ||
| 185 | + renderer, | ||
| 186 | + &program, | ||
| 187 | + &[ | ||
| 188 | + UniformName::new("niri_input_to_geo", UniformType::Matrix3x3), | ||
| 189 | + UniformName::new("niri_geo_size", UniformType::_2f), | ||
| 190 | + UniformName::new("niri_geo_to_tex", UniformType::Matrix3x3), | ||
| 191 | + UniformName::new("niri_progress", UniformType::_1f), | ||
| 192 | + UniformName::new("niri_clamped_progress", UniformType::_1f), | ||
| 193 | + UniformName::new("niri_random_seed", UniformType::_1f), | ||
| 194 | + ], | ||
| 195 | + &["niri_tex"], | ||
| 196 | + ) | ||
| 197 | +} | ||
| 198 | + | ||
| 199 | +pub fn set_custom_close_program(renderer: &mut GlesRenderer, src: Option<&str>) { | ||
| 200 | + let program = if let Some(src) = src { | ||
| 201 | + match compile_close_program(renderer, src) { | ||
| 202 | + Ok(program) => Some(program), | ||
| 203 | + Err(err) => { | ||
| 204 | + warn!("error compiling custom close shader: {err:?}"); | ||
| 205 | + return; | ||
| 206 | + } | ||
| 207 | + } | ||
| 208 | + } else { | ||
| 209 | + None | ||
| 210 | + }; | ||
| 211 | + | ||
| 212 | + if let Some(prev) = Shaders::get(renderer).replace_custom_close_program(program) { | ||
| 213 | + if let Err(err) = prev.destroy(renderer) { | ||
| 214 | + warn!("error destroying previous custom close shader: {err:?}"); | ||
| 215 | + } | ||
| 216 | + } | ||
| 217 | +} | ||
| 218 | + | ||
| 165 | pub fn mat3_uniform(name: &str, mat: Mat3) -> Uniform { | 219 | pub fn mat3_uniform(name: &str, mat: Mat3) -> Uniform { |
| 166 | Uniform::new( | 220 | Uniform::new( |
| 167 | name, | 221 | name, |
wiki/Configuration:-Animations.mdmodified+41 -0
| @@ -169,6 +169,47 @@ animations { | |||
| 169 | } | 169 | } |
| 170 | ``` | 170 | ``` |
| 171 | 171 | ||
| 172 | +##### `custom-shader` | ||
| 173 | + | ||
| 174 | +<sup>Since: 0.1.6, experimental</sup> | ||
| 175 | + | ||
| 176 | +You can write a custom shader for drawing the window during a close animation. | ||
| 177 | + | ||
| 178 | +See [this example shader](./examples/close-custom-shader.frag) for a full documentation with several animations to experiment with. | ||
| 179 | + | ||
| 180 | +If a custom shader fails to compile, niri will print a warning and fall back to the default, or previous successfully compiled shader. | ||
| 181 | + | ||
| 182 | +> [!WARNING] | ||
| 183 | +> | ||
| 184 | +> Custom shaders do not have a backwards compatibility guarantee. | ||
| 185 | +> I may need to change their interface as I'm developing new features. | ||
| 186 | + | ||
| 187 | +Example: close will fill the current geometry with a solid gradient that gradually fades away. | ||
| 188 | + | ||
| 189 | +``` | ||
| 190 | +animations { | ||
| 191 | + window-resize { | ||
| 192 | + spring damping-ratio=1.0 stiffness=800 epsilon=0.0001 | ||
| 193 | + | ||
| 194 | + custom-shader r" | ||
| 195 | + vec4 close_color(vec3 coords_geo, vec3 size_geo) { | ||
| 196 | + vec4 color = vec4(0.0); | ||
| 197 | + | ||
| 198 | + if (0.0 <= coords_geo.x && coords_geo.x <= 1.0 | ||
| 199 | + && 0.0 <= coords_geo.y && coords_geo.y <= 1.0) | ||
| 200 | + { | ||
| 201 | + vec4 from = vec4(1.0, 0.0, 0.0, 1.0); | ||
| 202 | + vec4 to = vec4(0.0, 1.0, 0.0, 1.0); | ||
| 203 | + color = mix(from, to, coords_geo.y); | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | + return color * (1.0 - niri_clamped_progress); | ||
| 207 | + } | ||
| 208 | + " | ||
| 209 | + } | ||
| 210 | +} | ||
| 211 | +``` | ||
| 212 | + | ||
| 172 | #### `horizontal-view-movement` | 213 | #### `horizontal-view-movement` |
| 173 | 214 | ||
| 174 | All horizontal camera view movement animations, such as: | 215 | All horizontal camera view movement animations, such as: |
wiki/examples/close_custom_shader.fragadded+113 -0
| @@ -0,0 +1,113 @@ | |||
| 1 | +// Your shader must contain one function (see the bottom of this file). | ||
| 2 | +// | ||
| 3 | +// It should not contain any uniform definitions or anything else, as niri | ||
| 4 | +// provides them for you. | ||
| 5 | +// | ||
| 6 | +// All symbols defined by niri will have a niri_ prefix, so don't use it for | ||
| 7 | +// your own variables and functions. | ||
| 8 | + | ||
| 9 | +// The function that you must define looks like this: | ||
| 10 | +vec4 close_color(vec3 coords_geo, vec3 size_geo) { | ||
| 11 | + vec4 color = /* ...compute the color... */; | ||
| 12 | + return color; | ||
| 13 | +} | ||
| 14 | + | ||
| 15 | +// It takes as input: | ||
| 16 | +// | ||
| 17 | +// * coords_geo: coordinates of the current pixel relative to the window | ||
| 18 | +// geometry. | ||
| 19 | +// | ||
| 20 | +// These are homogeneous (the Z component is equal to 1) and scaled in such a | ||
| 21 | +// way that the 0 to 1 coordinates lie within the window geometry. Pixels | ||
| 22 | +// outside the window geometry will have coordinates below 0 or above 1. | ||
| 23 | +// | ||
| 24 | +// The window geometry is its "visible bounds" from the user's perspective. | ||
| 25 | +// | ||
| 26 | +// The shader runs over the full screen area, so you must expect and handle | ||
| 27 | +// coordinates outside the [0, 1] range. If the window is scrolled off-screen, | ||
| 28 | +// all of the coordinates to the shader can fall outside the [0, 1] range. | ||
| 29 | +// | ||
| 30 | +// * size_geo: size of the window geometry in logical pixels. | ||
| 31 | +// | ||
| 32 | +// It is homogeneous (the Z component is equal to 1). | ||
| 33 | +// | ||
| 34 | +// The function must return the color of the pixel (with premultiplied alpha). | ||
| 35 | +// The pixel color will be further processed by niri (for example, to apply the | ||
| 36 | +// final opacity from window rules). | ||
| 37 | + | ||
| 38 | +// Now let's go over the uniforms that niri defines. | ||
| 39 | +// | ||
| 40 | +// You should only rely on the uniforms documented here. Any other uniforms can | ||
| 41 | +// change or be removed without notice. | ||
| 42 | + | ||
| 43 | +// The window texture. | ||
| 44 | +uniform sampler2D niri_tex; | ||
| 45 | + | ||
| 46 | +// Matrix that converts geometry coordinates into the window texture | ||
| 47 | +// coordinates. | ||
| 48 | +// | ||
| 49 | +// The window texture can and will go outside the geometry (for client-side | ||
| 50 | +// decoration shadows for example), which is why this matrix is necessary. | ||
| 51 | +uniform mat3 niri_geo_to_tex; | ||
| 52 | + | ||
| 53 | + | ||
| 54 | +// Unclamped progress of the animation. | ||
| 55 | +// | ||
| 56 | +// Goes from 0 to 1 but may overshoot and oscillate. | ||
| 57 | +uniform float niri_progress; | ||
| 58 | + | ||
| 59 | +// Clamped progress of the animation. | ||
| 60 | +// | ||
| 61 | +// Goes from 0 to 1, but will stop at 1 as soon as it first reaches 1. Will not | ||
| 62 | +// overshoot or oscillate. | ||
| 63 | +uniform float niri_clamped_progress; | ||
| 64 | + | ||
| 65 | +// Random float in [0; 1), consistent for the duration of the animation. | ||
| 66 | +uniform float niri_random_seed; | ||
| 67 | + | ||
| 68 | +// Now let's look at some examples. You can copy everything below this line | ||
| 69 | +// into your custom-shader to experiment. | ||
| 70 | + | ||
| 71 | +// Example: fill the current geometry with a solid vertical gradient and | ||
| 72 | +// gradually make transparent. | ||
| 73 | +vec4 solid_gradient(vec3 coords_geo, vec3 size_geo) { | ||
| 74 | + vec4 color = vec4(0.0); | ||
| 75 | + | ||
| 76 | + // Paint only the area inside the current geometry. | ||
| 77 | + if (0.0 <= coords_geo.x && coords_geo.x <= 1.0 | ||
| 78 | + && 0.0 <= coords_geo.y && coords_geo.y <= 1.0) | ||
| 79 | + { | ||
| 80 | + vec4 from = vec4(1.0, 0.0, 0.0, 1.0); | ||
| 81 | + vec4 to = vec4(0.0, 1.0, 0.0, 1.0); | ||
| 82 | + color = mix(from, to, coords_geo.y); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + // Make it transparent. | ||
| 86 | + color *= (1.0 - niri_clamped_progress); | ||
| 87 | + | ||
| 88 | + return color; | ||
| 89 | +} | ||
| 90 | + | ||
| 91 | +// Example: gradually scale down and make transparent, equivalent to the | ||
| 92 | +// default closing animation. | ||
| 93 | +vec4 default_close(vec3 coords_geo, vec3 size_geo) { | ||
| 94 | + // Scale down the window. | ||
| 95 | + float scale = max(0.0, ((1.0 - niri_clamped_progress) / 5.0 + 0.8)); | ||
| 96 | + coords_geo = vec3((coords_geo.xy - vec2(0.5)) / scale + vec2(0.5), 1.0); | ||
| 97 | + | ||
| 98 | + // Get color from the window texture. | ||
| 99 | + vec3 coords_tex = niri_geo_to_tex * coords_geo; | ||
| 100 | + vec4 color = texture2D(niri_tex, coords_tex.st); | ||
| 101 | + | ||
| 102 | + // Make the window transparent. | ||
| 103 | + color *= (1.0 - niri_clamped_progress); | ||
| 104 | + | ||
| 105 | + return color; | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +// This is the function that you must define. | ||
| 109 | +vec4 close_color(vec3 coords_geo, vec3 size_geo) { | ||
| 110 | + // You can pick one of the example functions or write your own. | ||
| 111 | + return solid_gradient(coords_geo, size_geo); | ||
| 112 | +} | ||
| 113 | + | ||
wiki/examples/resize_custom_shader.fragmodified+5 -2
| @@ -37,6 +37,9 @@ vec4 resize_color(vec3 coords_curr_geo, vec3 size_curr_geo) { | |||
| 37 | // final opacity from window rules). | 37 | // final opacity from window rules). |
| 38 | 38 | ||
| 39 | // Now let's go over the uniforms that niri defines. | 39 | // Now let's go over the uniforms that niri defines. |
| 40 | +// | ||
| 41 | +// You should only rely on the uniforms documented here. Any other uniforms can | ||
| 42 | +// change or be removed without notice. | ||
| 40 | 43 | ||
| 41 | // Previous (before resize) window texture. | 44 | // Previous (before resize) window texture. |
| 42 | uniform sampler2D niri_tex_prev; | 45 | uniform sampler2D niri_tex_prev; |
| @@ -65,12 +68,12 @@ uniform mat3 niri_curr_geo_to_prev_geo; | |||
| 65 | uniform mat3 niri_curr_geo_to_next_geo; | 68 | uniform mat3 niri_curr_geo_to_next_geo; |
| 66 | 69 | ||
| 67 | 70 | ||
| 68 | -// Unclamped progress of the resize. | 71 | +// Unclamped progress of the animation. |
| 69 | // | 72 | // |
| 70 | // Goes from 0 to 1 but may overshoot and oscillate. | 73 | // Goes from 0 to 1 but may overshoot and oscillate. |
| 71 | uniform float niri_progress; | 74 | uniform float niri_progress; |
| 72 | 75 | ||
| 73 | -// Clamped progress of the resize. | 76 | +// Clamped progress of the animation. |
| 74 | // | 77 | // |
| 75 | // Goes from 0 to 1, but will stop at 1 as soon as it first reaches 1. Will not | 78 | // Goes from 0 to 1, but will stop at 1 as soon as it first reaches 1. Will not |
| 76 | // overshoot or oscillate. | 79 | // overshoot or oscillate. |