824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1 | use crate::Error; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 2 | use crate::image::{VulkanTarget, VulkanTexture}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 3 | use crate::pipeline::{Kind, Push}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 4 | use crate::renderer::VulkanRenderer; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 5 | use crate::transform::{rect_to_framebuffer, texture_uvs, to_framebuffer}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 6 | use ash::vk; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 7 | use smithay::backend::renderer::sync::SyncPoint; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 8 | use smithay::backend::renderer::{Color32F, ContextId, Frame, Texture}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 9 | use smithay::utils::{Buffer, Physical, Rectangle, Size, Transform}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 10 | use std::sync::Arc; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 11 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 12 | pub(crate) enum Op { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 13 | Clear { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 14 | color: [f32; 4], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 15 | rects: Vec<vk::Rect2D>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 16 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 17 | Draw { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 18 | kind: Kind, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 19 | texture: Option<VulkanTexture>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 20 | push: Push, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 21 | scissors: Vec<vk::Rect2D>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 22 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 23 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 24 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 25 | /// A frame in progress. Draws are recorded and submitted together by [`Frame::finish`]. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 26 | pub struct VulkanFrame<'frame, 'buffer> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 27 | renderer: &'frame mut VulkanRenderer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 28 | target: &'frame mut VulkanTarget<'buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 29 | output: Size<i32, Physical>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 30 | transform: Transform, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 31 | ops: Vec<Op>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 32 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 33 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 34 | impl std::fmt::Debug for VulkanFrame<'_, '_> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 35 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 36 | f.debug_struct("VulkanFrame") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 37 | .field("output", &self.output) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 38 | .field("ops", &self.ops.len()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 39 | .finish() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 40 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 41 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 42 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 43 | impl<'frame, 'buffer> VulkanFrame<'frame, 'buffer> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 44 | pub(crate) fn new( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 45 | renderer: &'frame mut VulkanRenderer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 46 | target: &'frame mut VulkanTarget<'buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 47 | output: Size<i32, Physical>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 48 | transform: Transform, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 49 | ) -> Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 50 | Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 51 | renderer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 52 | target, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 53 | output, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 54 | transform, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 55 | ops: Vec::new(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 56 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 57 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 58 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 59 | fn fb_size(&self) -> Size<i32, Physical> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 60 | (self.target.width() as i32, self.target.height() as i32).into() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 61 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 62 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 63 | fn fb_rect(&self, r: Rectangle<i32, Physical>) -> Option<vk::Rect2D> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 64 | let r = rect_to_framebuffer(r, self.output, self.transform) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 65 | .intersection(Rectangle::from_size(self.fb_size()))?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 66 | Some(vk::Rect2D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 67 | offset: vk::Offset2D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 68 | x: r.loc.x, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 69 | y: r.loc.y, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 70 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 71 | extent: vk::Extent2D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 72 | width: r.size.w as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 73 | height: r.size.h as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 74 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 75 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 76 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 77 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 78 | fn scissors( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 79 | &self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 80 | dst: Rectangle<i32, Physical>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 81 | damage: &[Rectangle<i32, Physical>], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 82 | ) -> Vec<vk::Rect2D> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 83 | damage |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 84 | .iter() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 85 | .filter_map(|d| d.intersection(Rectangle::from_size(dst.size))) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 86 | .filter_map(|mut d| { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 87 | d.loc += dst.loc; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 88 | self.fb_rect(d) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 89 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 90 | .collect() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 91 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 92 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 93 | fn ndc(&self, r: Rectangle<i32, Physical>) -> [f32; 4] { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 94 | let fb = self.fb_size(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 95 | let (w, h) = (fb.w as f32, fb.h as f32); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 96 | [ |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 97 | r.loc.x as f32 / w * 2.0 - 1.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 98 | r.loc.y as f32 / h * 2.0 - 1.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 99 | r.size.w as f32 / w * 2.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 100 | r.size.h as f32 / h * 2.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 101 | ] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 102 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 103 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 104 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 105 | impl Frame for VulkanFrame<'_, '_> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 106 | type Error = Error; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 107 | type TextureId = VulkanTexture; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 108 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 109 | fn context_id(&self) -> ContextId<VulkanTexture> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 110 | self.renderer.context_id_ref().clone() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 111 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 112 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 113 | fn clear(&mut self, color: Color32F, at: &[Rectangle<i32, Physical>]) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 114 | let rects: Vec<_> = at.iter().filter_map(|r| self.fb_rect(*r)).collect(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 115 | if !rects.is_empty() { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 116 | self.ops.push(Op::Clear { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 117 | color: color.components(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 118 | rects, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 119 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 120 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 121 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 122 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 123 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 124 | fn draw_solid( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 125 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 126 | dst: Rectangle<i32, Physical>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 127 | damage: &[Rectangle<i32, Physical>], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 128 | color: Color32F, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 129 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 130 | let scissors = self.scissors(dst, damage); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 131 | if scissors.is_empty() { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 132 | return Ok(()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 133 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 134 | let (fb, _) = to_framebuffer(dst, [[0.0; 2]; 4], self.output, self.transform); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 135 | let push = Push { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 136 | dst: self.ndc(fb), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 137 | uv: [[0.0; 2]; 4], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 138 | color: color.components(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 139 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 140 | self.ops.push(Op::Draw { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 141 | kind: Kind::Solid, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 142 | texture: None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 143 | push, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 144 | scissors, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 145 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 146 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 147 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 148 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 149 | fn render_texture_from_to( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 150 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 151 | texture: &VulkanTexture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 152 | src: Rectangle<f64, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 153 | dst: Rectangle<i32, Physical>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 154 | damage: &[Rectangle<i32, Physical>], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 155 | _opaque_regions: &[Rectangle<i32, Physical>], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 156 | src_transform: Transform, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 157 | alpha: f32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 158 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 159 | if !Arc::ptr_eq(&texture.0.shared, self.renderer.shared()) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 160 | return Err(Error::ForeignTexture); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 161 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 162 | if dst.size.w <= 0 || dst.size.h <= 0 || Arc::ptr_eq(&texture.0, &self.target.0.0) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 163 | return Ok(()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 164 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 165 | let scissors = self.scissors(dst, damage); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 166 | if scissors.is_empty() { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 167 | return Ok(()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 168 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 169 | let uvs = texture_uvs(src, dst.size, texture.size(), src_transform); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 170 | let (fb, uv) = to_framebuffer(dst, uvs, self.output, self.transform); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 171 | let push = Push { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 172 | dst: self.ndc(fb), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 173 | uv, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 174 | color: [1.0, 1.0, 1.0, alpha], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 175 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 176 | self.ops.push(Op::Draw { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 177 | kind: Kind::Texture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 178 | texture: Some(texture.clone()), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 179 | push, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 180 | scissors, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 181 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 182 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 183 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 184 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 185 | fn transformation(&self) -> Transform { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 186 | self.transform |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 187 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 188 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 189 | fn output_size(&self) -> Size<i32, Physical> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 190 | self.output |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 191 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 192 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 193 | fn wait(&mut self, sync: &SyncPoint) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 194 | sync.wait().map_err(|_| Error::Interrupted) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 195 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 196 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 197 | fn finish(mut self) -> Result<SyncPoint, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 198 | let ops = std::mem::take(&mut self.ops); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 199 | self.renderer.submit(self.target, ops) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 200 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 201 | } |