shader_element: Store and set location separately
Browse files at this commit69aeba2a4d93b34eb24319c3694ad354dc608a87Parent: 7aab4137 changed files +75 -55
- niri-visual-tests/src/cases/gradient_angle.rs +5 -4
- niri-visual-tests/src/cases/gradient_area.rs +6 -4
- src/layout/focus_ring.rs +7 -4
- src/layout/tile.rs +5 -3
- src/render_helpers/border.rs +16 -12
- src/render_helpers/resize.rs +26 -23
- src/render_helpers/shader_element.rs +10 -5
niri-visual-tests/src/cases/gradient_angle.rsmodified+5 -4
| @@ -62,15 +62,16 @@ impl TestCase for GradientAngle { | ||
| 62 | 62 | let area = Rectangle::from_loc_and_size((a, b), size); |
| 63 | 63 | |
| 64 | 64 | [BorderRenderElement::new( |
| 65 | - area, | |
| 66 | - area, | |
| 65 | + area.size, | |
| 66 | + Rectangle::from_loc_and_size((0, 0), area.size), | |
| 67 | 67 | [1., 0., 0., 1.], |
| 68 | 68 | [0., 1., 0., 1.], |
| 69 | 69 | self.angle - FRAC_PI_2, |
| 70 | - area, | |
| 70 | + Rectangle::from_loc_and_size((0, 0), area.size), | |
| 71 | 71 | 0., |
| 72 | 72 | CornerRadius::default(), |
| 73 | - )] | |
| 73 | + ) | |
| 74 | + .with_location(area.loc)] | |
| 74 | 75 | .into_iter() |
| 75 | 76 | .map(|elem| Box::new(elem) as _) |
| 76 | 77 | .collect() |
niri-visual-tests/src/cases/gradient_area.rsmodified+6 -4
| @@ -83,7 +83,8 @@ impl TestCase for GradientArea { | ||
| 83 | 83 | (size.h as f32 / 8. + size.h as f32 / 8. * 7. * f).round() as i32, |
| 84 | 84 | )); |
| 85 | 85 | let g_loc = ((size.w - g_size.w) / 2, (size.h - g_size.h) / 2); |
| 86 | - let g_area = Rectangle::from_loc_and_size(g_loc, g_size); | |
| 86 | + let mut g_area = Rectangle::from_loc_and_size(g_loc, g_size); | |
| 87 | + g_area.loc -= area.loc; | |
| 87 | 88 | |
| 88 | 89 | self.border.update(g_size, true, CornerRadius::default()); |
| 89 | 90 | rv.extend( |
| @@ -99,15 +100,16 @@ impl TestCase for GradientArea { | ||
| 99 | 100 | |
| 100 | 101 | rv.extend( |
| 101 | 102 | [BorderRenderElement::new( |
| 102 | - area, | |
| 103 | + area.size, | |
| 103 | 104 | g_area, |
| 104 | 105 | [1., 0., 0., 1.], |
| 105 | 106 | [0., 1., 0., 1.], |
| 106 | 107 | FRAC_PI_4, |
| 107 | - area, | |
| 108 | + Rectangle::from_loc_and_size((0, 0), rect_size), | |
| 108 | 109 | 0., |
| 109 | 110 | CornerRadius::default(), |
| 110 | - )] | |
| 111 | + ) | |
| 112 | + .with_location(area.loc)] | |
| 111 | 113 | .into_iter() |
| 112 | 114 | .map(|elem| Box::new(elem) as _), |
| 113 | 115 | ); |
src/layout/focus_ring.rsmodified+7 -4
| @@ -194,6 +194,9 @@ impl FocusRing { | ||
| 194 | 194 | border: &mut BorderRenderElement, |
| 195 | 195 | location: Point<i32, Logical>, |
| 196 | 196 | size: Size<i32, Logical>| { |
| 197 | + let full_rect = Rectangle::from_loc_and_size(full_rect.loc - location, full_rect.size); | |
| 198 | + let view_rect = Rectangle::from_loc_and_size(view_rect.loc - location, view_rect.size); | |
| 199 | + | |
| 197 | 200 | let elem = if has_border_shader { |
| 198 | 201 | if let Some(gradient) = gradient { |
| 199 | 202 | let gradient_area = match gradient.relative_to { |
| @@ -201,7 +204,7 @@ impl FocusRing { | ||
| 201 | 204 | GradientRelativeTo::WorkspaceView => view_rect, |
| 202 | 205 | }; |
| 203 | 206 | border.update( |
| 204 | - Rectangle::from_loc_and_size(location, size), | |
| 207 | + size, | |
| 205 | 208 | gradient_area, |
| 206 | 209 | gradient.from.into(), |
| 207 | 210 | gradient.to.into(), |
| @@ -210,10 +213,10 @@ impl FocusRing { | ||
| 210 | 213 | border_width, |
| 211 | 214 | self.radius, |
| 212 | 215 | ); |
| 213 | - Some(border.clone().into()) | |
| 216 | + Some(border.clone().with_location(location).into()) | |
| 214 | 217 | } else if self.radius != CornerRadius::default() { |
| 215 | 218 | border.update( |
| 216 | - Rectangle::from_loc_and_size(location, size), | |
| 219 | + size, | |
| 217 | 220 | full_rect, |
| 218 | 221 | color.into(), |
| 219 | 222 | color.into(), |
| @@ -222,7 +225,7 @@ impl FocusRing { | ||
| 222 | 225 | border_width, |
| 223 | 226 | self.radius, |
| 224 | 227 | ); |
| 225 | - Some(border.clone().into()) | |
| 228 | + Some(border.clone().with_location(location).into()) | |
| 226 | 229 | } else { |
| 227 | 230 | None |
| 228 | 231 | } |
src/layout/tile.rsmodified+5 -3
| @@ -720,16 +720,18 @@ impl<W: LayoutElement> Tile<W> { | ||
| 720 | 720 | // user-provided radius, so our blocked-out rendering should match that |
| 721 | 721 | // radius. |
| 722 | 722 | if radius != CornerRadius::default() && has_border_shader { |
| 723 | + let elem_geo = elem.geometry(Scale::from(1.)).to_logical(1); | |
| 723 | 724 | return BorderRenderElement::new( |
| 724 | - elem.geometry(Scale::from(1.)).to_logical(1), | |
| 725 | - Rectangle::from_loc_and_size(Point::from((0, 0)), geo.size), | |
| 725 | + elem_geo.size, | |
| 726 | + Rectangle::from_loc_and_size((0, 0), elem_geo.size), | |
| 726 | 727 | elem.color(), |
| 727 | 728 | elem.color(), |
| 728 | 729 | 0., |
| 729 | - elem.geometry(Scale::from(1.)).to_logical(1), | |
| 730 | + Rectangle::from_loc_and_size(geo.loc - elem_geo.loc, geo.size), | |
| 730 | 731 | 0., |
| 731 | 732 | radius, |
| 732 | 733 | ) |
| 734 | + .with_location(elem_geo.loc) | |
| 733 | 735 | .into(); |
| 734 | 736 | } |
| 735 | 737 |
src/render_helpers/border.rsmodified+16 -12
| @@ -5,7 +5,7 @@ use niri_config::CornerRadius; | ||
| 5 | 5 | use smithay::backend::renderer::element::{Element, Id, Kind, RenderElement, UnderlyingStorage}; |
| 6 | 6 | use smithay::backend::renderer::gles::{GlesError, GlesFrame, GlesRenderer, Uniform}; |
| 7 | 7 | use smithay::backend::renderer::utils::{CommitCounter, DamageSet}; |
| 8 | -use smithay::utils::{Buffer, Logical, Physical, Rectangle, Scale, Transform}; | |
| 8 | +use smithay::utils::{Buffer, Logical, Physical, Point, Rectangle, Scale, Size, Transform}; | |
| 9 | 9 | |
| 10 | 10 | use super::renderer::NiriRenderer; |
| 11 | 11 | use super::shader_element::ShaderRenderElement; |
| @@ -26,7 +26,7 @@ pub struct BorderRenderElement { | ||
| 26 | 26 | |
| 27 | 27 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 28 | 28 | struct Parameters { |
| 29 | - area: Rectangle<i32, Logical>, | |
| 29 | + size: Size<i32, Logical>, | |
| 30 | 30 | gradient_area: Rectangle<i32, Logical>, |
| 31 | 31 | color_from: [f32; 4], |
| 32 | 32 | color_to: [f32; 4], |
| @@ -39,7 +39,7 @@ struct Parameters { | ||
| 39 | 39 | impl BorderRenderElement { |
| 40 | 40 | #[allow(clippy::too_many_arguments)] |
| 41 | 41 | pub fn new( |
| 42 | - area: Rectangle<i32, Logical>, | |
| 42 | + size: Size<i32, Logical>, | |
| 43 | 43 | gradient_area: Rectangle<i32, Logical>, |
| 44 | 44 | color_from: [f32; 4], |
| 45 | 45 | color_to: [f32; 4], |
| @@ -52,7 +52,7 @@ impl BorderRenderElement { | ||
| 52 | 52 | let mut rv = Self { |
| 53 | 53 | inner, |
| 54 | 54 | params: Parameters { |
| 55 | - area, | |
| 55 | + size, | |
| 56 | 56 | gradient_area, |
| 57 | 57 | color_from, |
| 58 | 58 | color_to, |
| @@ -71,7 +71,7 @@ impl BorderRenderElement { | ||
| 71 | 71 | Self { |
| 72 | 72 | inner, |
| 73 | 73 | params: Parameters { |
| 74 | - area: Default::default(), | |
| 74 | + size: Default::default(), | |
| 75 | 75 | gradient_area: Default::default(), |
| 76 | 76 | color_from: Default::default(), |
| 77 | 77 | color_to: Default::default(), |
| @@ -90,7 +90,7 @@ impl BorderRenderElement { | ||
| 90 | 90 | #[allow(clippy::too_many_arguments)] |
| 91 | 91 | pub fn update( |
| 92 | 92 | &mut self, |
| 93 | - area: Rectangle<i32, Logical>, | |
| 93 | + size: Size<i32, Logical>, | |
| 94 | 94 | gradient_area: Rectangle<i32, Logical>, |
| 95 | 95 | color_from: [f32; 4], |
| 96 | 96 | color_to: [f32; 4], |
| @@ -100,7 +100,7 @@ impl BorderRenderElement { | ||
| 100 | 100 | corner_radius: CornerRadius, |
| 101 | 101 | ) { |
| 102 | 102 | let params = Parameters { |
| 103 | - area, | |
| 103 | + size, | |
| 104 | 104 | gradient_area, |
| 105 | 105 | color_from, |
| 106 | 106 | color_to, |
| @@ -119,7 +119,7 @@ impl BorderRenderElement { | ||
| 119 | 119 | |
| 120 | 120 | fn update_inner(&mut self) { |
| 121 | 121 | let Parameters { |
| 122 | - area, | |
| 122 | + size, | |
| 123 | 123 | gradient_area, |
| 124 | 124 | color_from, |
| 125 | 125 | color_to, |
| @@ -146,17 +146,16 @@ impl BorderRenderElement { | ||
| 146 | 146 | grad_vec = -grad_vec; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - let area_loc = Vec2::new(area.loc.x as f32, area.loc.y as f32); | |
| 150 | - let area_size = Vec2::new(area.size.w as f32, area.size.h as f32); | |
| 149 | + let area_size = Vec2::new(size.w as f32, size.h as f32); | |
| 151 | 150 | |
| 152 | 151 | let geo_loc = Vec2::new(geometry.loc.x as f32, geometry.loc.y as f32); |
| 153 | 152 | let geo_size = Vec2::new(geometry.size.w as f32, geometry.size.h as f32); |
| 154 | 153 | |
| 155 | 154 | let input_to_geo = |
| 156 | - Mat3::from_scale(area_size) * Mat3::from_translation((area_loc - geo_loc) / area_size); | |
| 155 | + Mat3::from_scale(area_size) * Mat3::from_translation(-geo_loc / area_size); | |
| 157 | 156 | |
| 158 | 157 | self.inner.update( |
| 159 | - area, | |
| 158 | + size, | |
| 160 | 159 | None, |
| 161 | 160 | vec![ |
| 162 | 161 | Uniform::new("color_from", color_from), |
| @@ -173,6 +172,11 @@ impl BorderRenderElement { | ||
| 173 | 172 | ); |
| 174 | 173 | } |
| 175 | 174 | |
| 175 | + pub fn with_location(mut self, location: Point<i32, Logical>) -> Self { | |
| 176 | + self.inner = self.inner.with_location(location); | |
| 177 | + self | |
| 178 | + } | |
| 179 | + | |
| 176 | 180 | pub fn has_shader(renderer: &mut impl NiriRenderer) -> bool { |
| 177 | 181 | Shaders::get(renderer) |
| 178 | 182 | .program(ProgramType::Border) |
src/render_helpers/resize.rsmodified+26 -23
| @@ -85,29 +85,32 @@ impl ResizeRenderElement { | ||
| 85 | 85 | let clip_to_geometry = if clip_to_geometry { 1. } else { 0. }; |
| 86 | 86 | |
| 87 | 87 | // Create the shader. |
| 88 | - Self(ShaderRenderElement::new( | |
| 89 | - ProgramType::Resize, | |
| 90 | - area, | |
| 91 | - None, | |
| 92 | - result_alpha, | |
| 93 | - vec![ | |
| 94 | - mat3_uniform("niri_input_to_curr_geo", input_to_curr_geo), | |
| 95 | - mat3_uniform("niri_curr_geo_to_prev_geo", curr_geo_to_prev_geo), | |
| 96 | - mat3_uniform("niri_curr_geo_to_next_geo", curr_geo_to_next_geo), | |
| 97 | - Uniform::new("niri_curr_geo_size", curr_geo_size.to_array()), | |
| 98 | - mat3_uniform("niri_geo_to_tex_prev", geo_to_tex_prev), | |
| 99 | - mat3_uniform("niri_geo_to_tex_next", geo_to_tex_next), | |
| 100 | - Uniform::new("niri_progress", progress), | |
| 101 | - Uniform::new("niri_clamped_progress", clamped_progress), | |
| 102 | - Uniform::new("niri_corner_radius", <[f32; 4]>::from(corner_radius)), | |
| 103 | - Uniform::new("niri_clip_to_geometry", clip_to_geometry), | |
| 104 | - ], | |
| 105 | - HashMap::from([ | |
| 106 | - (String::from("niri_tex_prev"), texture_prev), | |
| 107 | - (String::from("niri_tex_next"), texture_next), | |
| 108 | - ]), | |
| 109 | - Kind::Unspecified, | |
| 110 | - )) | |
| 88 | + Self( | |
| 89 | + ShaderRenderElement::new( | |
| 90 | + ProgramType::Resize, | |
| 91 | + area.size, | |
| 92 | + None, | |
| 93 | + result_alpha, | |
| 94 | + vec![ | |
| 95 | + mat3_uniform("niri_input_to_curr_geo", input_to_curr_geo), | |
| 96 | + mat3_uniform("niri_curr_geo_to_prev_geo", curr_geo_to_prev_geo), | |
| 97 | + mat3_uniform("niri_curr_geo_to_next_geo", curr_geo_to_next_geo), | |
| 98 | + Uniform::new("niri_curr_geo_size", curr_geo_size.to_array()), | |
| 99 | + mat3_uniform("niri_geo_to_tex_prev", geo_to_tex_prev), | |
| 100 | + mat3_uniform("niri_geo_to_tex_next", geo_to_tex_next), | |
| 101 | + Uniform::new("niri_progress", progress), | |
| 102 | + Uniform::new("niri_clamped_progress", clamped_progress), | |
| 103 | + Uniform::new("niri_corner_radius", <[f32; 4]>::from(corner_radius)), | |
| 104 | + Uniform::new("niri_clip_to_geometry", clip_to_geometry), | |
| 105 | + ], | |
| 106 | + HashMap::from([ | |
| 107 | + (String::from("niri_tex_prev"), texture_prev), | |
| 108 | + (String::from("niri_tex_next"), texture_next), | |
| 109 | + ]), | |
| 110 | + Kind::Unspecified, | |
| 111 | + ) | |
| 112 | + .with_location(area.loc), | |
| 113 | + ) | |
| 111 | 114 | } |
| 112 | 115 | |
| 113 | 116 | pub fn has_shader(renderer: &mut impl NiriRenderer) -> bool { |
src/render_helpers/shader_element.rsmodified+10 -5
| @@ -10,7 +10,7 @@ use smithay::backend::renderer::gles::{ | ||
| 10 | 10 | }; |
| 11 | 11 | use smithay::backend::renderer::utils::CommitCounter; |
| 12 | 12 | use smithay::backend::renderer::DebugFlags; |
| 13 | -use smithay::utils::{Buffer, Logical, Physical, Rectangle, Scale}; | |
| 13 | +use smithay::utils::{Buffer, Logical, Physical, Point, Rectangle, Scale, Size}; | |
| 14 | 14 | |
| 15 | 15 | use super::renderer::AsGlesFrame; |
| 16 | 16 | use super::resources::Resources; |
| @@ -198,7 +198,7 @@ impl ShaderRenderElement { | ||
| 198 | 198 | #[allow(clippy::too_many_arguments)] |
| 199 | 199 | pub fn new( |
| 200 | 200 | program: ProgramType, |
| 201 | - area: Rectangle<i32, Logical>, | |
| 201 | + size: Size<i32, Logical>, | |
| 202 | 202 | opaque_regions: Option<Vec<Rectangle<i32, Logical>>>, |
| 203 | 203 | alpha: f32, |
| 204 | 204 | uniforms: Vec<Uniform<'_>>, |
| @@ -209,7 +209,7 @@ impl ShaderRenderElement { | ||
| 209 | 209 | program, |
| 210 | 210 | id: Id::new(), |
| 211 | 211 | commit_counter: CommitCounter::default(), |
| 212 | - area, | |
| 212 | + area: Rectangle::from_loc_and_size((0, 0), size), | |
| 213 | 213 | opaque_regions: opaque_regions.unwrap_or_default(), |
| 214 | 214 | alpha, |
| 215 | 215 | additional_uniforms: uniforms.into_iter().map(|u| u.into_owned()).collect(), |
| @@ -238,18 +238,23 @@ impl ShaderRenderElement { | ||
| 238 | 238 | |
| 239 | 239 | pub fn update( |
| 240 | 240 | &mut self, |
| 241 | - area: Rectangle<i32, Logical>, | |
| 241 | + size: Size<i32, Logical>, | |
| 242 | 242 | opaque_regions: Option<Vec<Rectangle<i32, Logical>>>, |
| 243 | 243 | uniforms: Vec<Uniform<'_>>, |
| 244 | 244 | textures: HashMap<String, GlesTexture>, |
| 245 | 245 | ) { |
| 246 | - self.area = area; | |
| 246 | + self.area.size = size; | |
| 247 | 247 | self.opaque_regions = opaque_regions.unwrap_or_default(); |
| 248 | 248 | self.additional_uniforms = uniforms.into_iter().map(|u| u.into_owned()).collect(); |
| 249 | 249 | self.textures = textures; |
| 250 | 250 | |
| 251 | 251 | self.commit_counter.increment(); |
| 252 | 252 | } |
| 253 | + | |
| 254 | + pub fn with_location(mut self, location: Point<i32, Logical>) -> Self { | |
| 255 | + self.area.loc = location; | |
| 256 | + self | |
| 257 | + } | |
| 253 | 258 | } |
| 254 | 259 | |
| 255 | 260 | impl Element for ShaderRenderElement { |