| 1 | use crate::Error; |
| 2 | use crate::device::Shared; |
| 3 | use crate::format::{components, vk_format}; |
| 4 | use ash::vk; |
| 5 | use gpu_allocator::MemoryLocation; |
| 6 | use gpu_allocator::vulkan::{Allocation, AllocationCreateDesc, AllocationScheme}; |
| 7 | use smithay::backend::allocator::Fourcc; |
| 8 | use smithay::backend::renderer::{Texture, TextureMapping}; |
| 9 | use std::fmt; |
| 10 | use std::marker::PhantomData; |
| 11 | use std::sync::{Arc, Mutex}; |
| 12 | |
| 13 | pub(crate) enum Memory { |
| 14 | Allocated(Allocation), |
| 15 | /// Memory imported from a dmabuf, owned directly. |
| 16 | Imported(vk::DeviceMemory), |
| 17 | } |
| 18 | |
| 19 | pub(crate) struct ImageInner { |
| 20 | pub shared: Arc<Shared>, |
| 21 | pub image: vk::Image, |
| 22 | pub view: vk::ImageView, |
| 23 | pub memory: Mutex<Option<Memory>>, |
| 24 | pub fourcc: Fourcc, |
| 25 | pub format: vk::Format, |
| 26 | pub width: u32, |
| 27 | pub height: u32, |
| 28 | pub layout: Mutex<vk::ImageLayout>, |
| 29 | /// Shared with other devices or processes through a dmabuf. Every use |
| 30 | /// acquires the image from the foreign queue family and releases it after. |
| 31 | pub external: bool, |
| 32 | } |
| 33 | |
| 34 | impl fmt::Debug for ImageInner { |
| 35 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 36 | f.debug_struct("ImageInner") |
| 37 | .field("image", &self.image) |
| 38 | .field("fourcc", &self.fourcc) |
| 39 | .field("size", &(self.width, self.height)) |
| 40 | .field("external", &self.external) |
| 41 | .finish() |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl Drop for ImageInner { |
| 46 | fn drop(&mut self) { |
| 47 | let d = &self.shared.device.device; |
| 48 | unsafe { |
| 49 | d.destroy_image_view(self.view, None); |
| 50 | d.destroy_image(self.image, None); |
| 51 | } |
| 52 | match self.memory.lock().unwrap_or_else(|e| e.into_inner()).take() { |
| 53 | Some(Memory::Allocated(a)) => { |
| 54 | let _ = self |
| 55 | .shared |
| 56 | .allocator |
| 57 | .lock() |
| 58 | .unwrap_or_else(|e| e.into_inner()) |
| 59 | .free(a); |
| 60 | } |
| 61 | Some(Memory::Imported(m)) => unsafe { d.free_memory(m, None) }, |
| 62 | None => {} |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl ImageInner { |
| 68 | pub fn create( |
| 69 | shared: &Arc<Shared>, |
| 70 | fourcc: Fourcc, |
| 71 | width: u32, |
| 72 | height: u32, |
| 73 | usage: vk::ImageUsageFlags, |
| 74 | ) -> Result<Arc<Self>, Error> { |
| 75 | let fmt = vk_format(fourcc).ok_or(Error::UnsupportedFormat(fourcc))?; |
| 76 | if width == 0 || height == 0 { |
| 77 | return Err(Error::InvalidSize(width as i32, height as i32)); |
| 78 | } |
| 79 | let d = &shared.device.device; |
| 80 | let info = vk::ImageCreateInfo::default() |
| 81 | .image_type(vk::ImageType::TYPE_2D) |
| 82 | .format(fmt.format) |
| 83 | .extent(vk::Extent3D { |
| 84 | width, |
| 85 | height, |
| 86 | depth: 1, |
| 87 | }) |
| 88 | .mip_levels(1) |
| 89 | .array_layers(1) |
| 90 | .samples(vk::SampleCountFlags::TYPE_1) |
| 91 | .tiling(vk::ImageTiling::OPTIMAL) |
| 92 | .usage(usage) |
| 93 | .initial_layout(vk::ImageLayout::UNDEFINED); |
| 94 | let image = unsafe { d.create_image(&info, None)? }; |
| 95 | let requirements = unsafe { d.get_image_memory_requirements(image) }; |
| 96 | let allocation = shared |
| 97 | .allocator |
| 98 | .lock() |
| 99 | .unwrap_or_else(|e| e.into_inner()) |
| 100 | .allocate(&AllocationCreateDesc { |
| 101 | name: "image", |
| 102 | requirements, |
| 103 | location: MemoryLocation::GpuOnly, |
| 104 | linear: false, |
| 105 | allocation_scheme: AllocationScheme::GpuAllocatorManaged, |
| 106 | }); |
| 107 | let allocation = match allocation { |
| 108 | Ok(a) => a, |
| 109 | Err(e) => { |
| 110 | unsafe { d.destroy_image(image, None) }; |
| 111 | return Err(e.into()); |
| 112 | } |
| 113 | }; |
| 114 | let memory = Memory::Allocated(allocation); |
| 115 | let (mem, offset) = match &memory { |
| 116 | Memory::Allocated(a) => (unsafe { a.memory() }, a.offset()), |
| 117 | Memory::Imported(m) => (*m, 0), |
| 118 | }; |
| 119 | Self::finish( |
| 120 | shared, image, memory, mem, offset, fourcc, width, height, false, |
| 121 | ) |
| 122 | } |
| 123 | |
| 124 | /// Binds memory, creates the view and wraps everything up. Takes |
| 125 | /// ownership of `image` and `memory`, releasing both on failure. |
| 126 | #[expect(clippy::too_many_arguments)] |
| 127 | pub fn finish( |
| 128 | shared: &Arc<Shared>, |
| 129 | image: vk::Image, |
| 130 | memory: Memory, |
| 131 | mem: vk::DeviceMemory, |
| 132 | offset: u64, |
| 133 | fourcc: Fourcc, |
| 134 | width: u32, |
| 135 | height: u32, |
| 136 | external: bool, |
| 137 | ) -> Result<Arc<Self>, Error> { |
| 138 | let fmt = vk_format(fourcc).ok_or(Error::UnsupportedFormat(fourcc))?; |
| 139 | let d = &shared.device.device; |
| 140 | let mut inner = Self { |
| 141 | shared: shared.clone(), |
| 142 | image, |
| 143 | view: vk::ImageView::null(), |
| 144 | memory: Mutex::new(Some(memory)), |
| 145 | fourcc, |
| 146 | format: fmt.format, |
| 147 | width, |
| 148 | height, |
| 149 | layout: Mutex::new(if external { |
| 150 | vk::ImageLayout::GENERAL |
| 151 | } else { |
| 152 | vk::ImageLayout::UNDEFINED |
| 153 | }), |
| 154 | external, |
| 155 | }; |
| 156 | unsafe { d.bind_image_memory(image, mem, offset)? }; |
| 157 | let view_info = vk::ImageViewCreateInfo::default() |
| 158 | .image(image) |
| 159 | .view_type(vk::ImageViewType::TYPE_2D) |
| 160 | .format(fmt.format) |
| 161 | .components(components(fmt.opaque)) |
| 162 | .subresource_range(color_range()); |
| 163 | inner.view = unsafe { d.create_image_view(&view_info, None)? }; |
| 164 | Ok(Arc::new(inner)) |
| 165 | } |
| 166 | |
| 167 | pub fn layout(&self) -> vk::ImageLayout { |
| 168 | *self.layout.lock().unwrap_or_else(|e| e.into_inner()) |
| 169 | } |
| 170 | |
| 171 | pub fn set_layout(&self, layout: vk::ImageLayout) { |
| 172 | *self.layout.lock().unwrap_or_else(|e| e.into_inner()) = layout; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | pub(crate) fn color_range() -> vk::ImageSubresourceRange { |
| 177 | vk::ImageSubresourceRange::default() |
| 178 | .aspect_mask(vk::ImageAspectFlags::COLOR) |
| 179 | .level_count(1) |
| 180 | .layer_count(1) |
| 181 | } |
| 182 | |
| 183 | /// A host-visible buffer for uploads and readbacks. |
| 184 | pub(crate) struct Staging { |
| 185 | pub shared: Arc<Shared>, |
| 186 | pub buffer: vk::Buffer, |
| 187 | pub allocation: Option<Allocation>, |
| 188 | } |
| 189 | |
| 190 | impl Staging { |
| 191 | pub fn new( |
| 192 | shared: &Arc<Shared>, |
| 193 | size: usize, |
| 194 | usage: vk::BufferUsageFlags, |
| 195 | location: MemoryLocation, |
| 196 | ) -> Result<Self, Error> { |
| 197 | let d = &shared.device.device; |
| 198 | let buffer = unsafe { |
| 199 | d.create_buffer( |
| 200 | &vk::BufferCreateInfo::default() |
| 201 | .size(size as u64) |
| 202 | .usage(usage), |
| 203 | None, |
| 204 | )? |
| 205 | }; |
| 206 | let requirements = unsafe { d.get_buffer_memory_requirements(buffer) }; |
| 207 | let allocation = shared |
| 208 | .allocator |
| 209 | .lock() |
| 210 | .unwrap_or_else(|e| e.into_inner()) |
| 211 | .allocate(&AllocationCreateDesc { |
| 212 | name: "staging", |
| 213 | requirements, |
| 214 | location, |
| 215 | linear: true, |
| 216 | allocation_scheme: AllocationScheme::GpuAllocatorManaged, |
| 217 | }); |
| 218 | let allocation = match allocation { |
| 219 | Ok(a) => a, |
| 220 | Err(e) => { |
| 221 | unsafe { d.destroy_buffer(buffer, None) }; |
| 222 | return Err(e.into()); |
| 223 | } |
| 224 | }; |
| 225 | unsafe { d.bind_buffer_memory(buffer, allocation.memory(), allocation.offset())? }; |
| 226 | Ok(Self { |
| 227 | shared: shared.clone(), |
| 228 | buffer, |
| 229 | allocation: Some(allocation), |
| 230 | }) |
| 231 | } |
| 232 | |
| 233 | pub fn bytes(&self) -> &[u8] { |
| 234 | self.allocation |
| 235 | .as_ref() |
| 236 | .and_then(|a| a.mapped_slice()) |
| 237 | .unwrap_or(&[]) |
| 238 | } |
| 239 | |
| 240 | pub fn bytes_mut(&mut self) -> &mut [u8] { |
| 241 | self.allocation |
| 242 | .as_mut() |
| 243 | .and_then(|a| a.mapped_slice_mut()) |
| 244 | .unwrap_or(&mut []) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | impl Drop for Staging { |
| 249 | fn drop(&mut self) { |
| 250 | unsafe { self.shared.device.device.destroy_buffer(self.buffer, None) }; |
| 251 | if let Some(a) = self.allocation.take() { |
| 252 | let _ = self |
| 253 | .shared |
| 254 | .allocator |
| 255 | .lock() |
| 256 | .unwrap_or_else(|e| e.into_inner()) |
| 257 | .free(a); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /// A sampled texture. |
| 263 | #[derive(Clone, Debug)] |
| 264 | pub struct VulkanTexture(pub(crate) Arc<ImageInner>); |
| 265 | |
| 266 | /// An image the renderer can draw into, from [`smithay::backend::renderer::Offscreen`]. |
| 267 | #[derive(Debug)] |
| 268 | pub struct VulkanImage(pub(crate) Arc<ImageInner>); |
| 269 | |
| 270 | impl VulkanImage { |
| 271 | /// The image's contents as a texture, for drawing it elsewhere. |
| 272 | pub fn texture(&self) -> VulkanTexture { |
| 273 | VulkanTexture(self.0.clone()) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /// A bound render target, borrowing whatever it was bound from. |
| 278 | #[derive(Debug)] |
| 279 | pub struct VulkanTarget<'a>( |
| 280 | pub(crate) Arc<ImageInner>, |
| 281 | pub(crate) PhantomData<&'a mut ()>, |
| 282 | ); |
| 283 | |
| 284 | /// Pixels read back from the GPU. |
| 285 | #[derive(Debug)] |
| 286 | pub struct VulkanMapping { |
| 287 | pub(crate) data: Vec<u8>, |
| 288 | pub(crate) width: u32, |
| 289 | pub(crate) height: u32, |
| 290 | pub(crate) fourcc: Fourcc, |
| 291 | } |
| 292 | |
| 293 | macro_rules! texture_impl { |
| 294 | ($t:ty, |$s:ident| $inner:expr) => { |
| 295 | impl Texture for $t { |
| 296 | fn width(&self) -> u32 { |
| 297 | let $s = self; |
| 298 | $inner.width |
| 299 | } |
| 300 | fn height(&self) -> u32 { |
| 301 | let $s = self; |
| 302 | $inner.height |
| 303 | } |
| 304 | fn format(&self) -> Option<Fourcc> { |
| 305 | let $s = self; |
| 306 | Some($inner.fourcc) |
| 307 | } |
| 308 | } |
| 309 | }; |
| 310 | } |
| 311 | |
| 312 | texture_impl!(VulkanTexture, |s| s.0); |
| 313 | texture_impl!(VulkanImage, |s| s.0); |
| 314 | texture_impl!(VulkanTarget<'_>, |s| s.0); |
| 315 | texture_impl!(VulkanMapping, |s| s); |
| 316 | |
| 317 | impl TextureMapping for VulkanMapping { |
| 318 | fn flipped(&self) -> bool { |
| 319 | false |
| 320 | } |
| 321 | } |