824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1 | use crate::Error; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 2 | use crate::device::Shared; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 3 | use crate::format::{components, vk_format}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 4 | use ash::vk; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 5 | use gpu_allocator::MemoryLocation; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 6 | use gpu_allocator::vulkan::{Allocation, AllocationCreateDesc, AllocationScheme}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 7 | use smithay::backend::allocator::Fourcc; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 8 | use smithay::backend::renderer::{Texture, TextureMapping}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 9 | use std::fmt; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 10 | use std::marker::PhantomData; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 11 | use std::sync::{Arc, Mutex}; |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 12 | use std::time::{Duration, Instant}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 13 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 14 | pub(crate) enum Memory { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 15 | Allocated(Allocation), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 16 | /// Memory imported from a dmabuf, owned directly. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 17 | Imported(vk::DeviceMemory), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 18 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 19 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 20 | pub(crate) struct ImageInner { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 21 | pub shared: Arc<Shared>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 22 | pub image: vk::Image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 23 | pub view: vk::ImageView, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 24 | pub memory: Mutex<Option<Memory>>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 25 | pub fourcc: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 26 | pub format: vk::Format, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 27 | pub width: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 28 | pub height: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 29 | pub layout: Mutex<vk::ImageLayout>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 30 | /// Shared with other devices or processes through a dmabuf. Every use |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 31 | /// acquires the image from the foreign queue family and releases it after. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 32 | pub external: bool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 33 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 34 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 35 | impl fmt::Debug for ImageInner { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 36 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 37 | f.debug_struct("ImageInner") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 38 | .field("image", &self.image) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 39 | .field("fourcc", &self.fourcc) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 40 | .field("size", &(self.width, self.height)) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 41 | .field("external", &self.external) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 42 | .finish() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 43 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 44 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 45 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 46 | impl Drop for ImageInner { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 47 | fn drop(&mut self) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 48 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 49 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 50 | d.destroy_image_view(self.view, None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 51 | d.destroy_image(self.image, None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 52 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 53 | match self.memory.lock().unwrap_or_else(|e| e.into_inner()).take() { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 54 | Some(Memory::Allocated(a)) => { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 55 | let _ = self |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 56 | .shared |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 57 | .allocator |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 58 | .lock() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 59 | .unwrap_or_else(|e| e.into_inner()) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 60 | .free(a); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 61 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 62 | Some(Memory::Imported(m)) => unsafe { d.free_memory(m, None) }, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 63 | None => {} |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 64 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 65 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 66 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 67 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 68 | impl ImageInner { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 69 | pub fn create( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 70 | shared: &Arc<Shared>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 71 | fourcc: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 72 | width: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 73 | height: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 74 | usage: vk::ImageUsageFlags, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 75 | ) -> Result<Arc<Self>, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 76 | let fmt = vk_format(fourcc).ok_or(Error::UnsupportedFormat(fourcc))?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 77 | if width == 0 || height == 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 78 | return Err(Error::InvalidSize(width as i32, height as i32)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 79 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 80 | let d = &shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 81 | let info = vk::ImageCreateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 82 | .image_type(vk::ImageType::TYPE_2D) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 83 | .format(fmt.format) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 84 | .extent(vk::Extent3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 85 | width, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 86 | height, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 87 | depth: 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 88 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 89 | .mip_levels(1) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 90 | .array_layers(1) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 91 | .samples(vk::SampleCountFlags::TYPE_1) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 92 | .tiling(vk::ImageTiling::OPTIMAL) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 93 | .usage(usage) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 94 | .initial_layout(vk::ImageLayout::UNDEFINED); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 95 | let image = unsafe { d.create_image(&info, None)? }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 96 | let requirements = unsafe { d.get_image_memory_requirements(image) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 97 | let allocation = shared |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 98 | .allocator |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 99 | .lock() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 100 | .unwrap_or_else(|e| e.into_inner()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 101 | .allocate(&AllocationCreateDesc { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 102 | name: "image", |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 103 | requirements, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 104 | location: MemoryLocation::GpuOnly, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 105 | linear: false, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 106 | allocation_scheme: AllocationScheme::GpuAllocatorManaged, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 107 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 108 | let allocation = match allocation { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 109 | Ok(a) => a, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 110 | Err(e) => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 111 | unsafe { d.destroy_image(image, None) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 112 | return Err(e.into()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 113 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 114 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 115 | let memory = Memory::Allocated(allocation); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 116 | let (mem, offset) = match &memory { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 117 | Memory::Allocated(a) => (unsafe { a.memory() }, a.offset()), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 118 | Memory::Imported(m) => (*m, 0), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 119 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 120 | Self::finish( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 121 | shared, image, memory, mem, offset, fourcc, width, height, false, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 122 | ) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 123 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 124 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 125 | /// Binds memory, creates the view and wraps everything up. Takes |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 126 | /// ownership of `image` and `memory`, releasing both on failure. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 127 | #[expect(clippy::too_many_arguments)] |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 128 | pub fn finish( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 129 | shared: &Arc<Shared>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 130 | image: vk::Image, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 131 | memory: Memory, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 132 | mem: vk::DeviceMemory, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 133 | offset: u64, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 134 | fourcc: Fourcc, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 135 | width: u32, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 136 | height: u32, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 137 | external: bool, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 138 | ) -> Result<Arc<Self>, Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 139 | let fmt = vk_format(fourcc).ok_or(Error::UnsupportedFormat(fourcc))?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 140 | let d = &shared.device.device; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 141 | let mut inner = Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 142 | shared: shared.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 143 | image, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 144 | view: vk::ImageView::null(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 145 | memory: Mutex::new(Some(memory)), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 146 | fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 147 | format: fmt.format, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 148 | width, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 149 | height, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 150 | layout: Mutex::new(if external { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 151 | vk::ImageLayout::GENERAL |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 152 | } else { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 153 | vk::ImageLayout::UNDEFINED |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 154 | }), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 155 | external, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 156 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 157 | unsafe { d.bind_image_memory(image, mem, offset)? }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 158 | let view_info = vk::ImageViewCreateInfo::default() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 159 | .image(image) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 160 | .view_type(vk::ImageViewType::TYPE_2D) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 161 | .format(fmt.format) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 162 | .components(components(fmt.opaque)) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 163 | .subresource_range(color_range()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 164 | inner.view = unsafe { d.create_image_view(&view_info, None)? }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 165 | Ok(Arc::new(inner)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 166 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 167 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 168 | pub fn layout(&self) -> vk::ImageLayout { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 169 | *self.layout.lock().unwrap_or_else(|e| e.into_inner()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 170 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 171 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 172 | pub fn set_layout(&self, layout: vk::ImageLayout) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 173 | *self.layout.lock().unwrap_or_else(|e| e.into_inner()) = layout; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 174 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 175 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 176 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 177 | pub(crate) fn color_range() -> vk::ImageSubresourceRange { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 178 | vk::ImageSubresourceRange::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 179 | .aspect_mask(vk::ImageAspectFlags::COLOR) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 180 | .level_count(1) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 181 | .layer_count(1) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 182 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 183 | |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 184 | /// A host-visible buffer for uploads and readbacks. Dropped ones go back to |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 185 | /// a pool on `Shared` instead of being freed. Allocating and freeing large |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 186 | /// host-visible memory takes a driver lock on NVIDIA that `vkQueueSubmit` |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 187 | /// also waits on, so a staging buffer made per upload on the import worker |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 188 | /// stalled the main thread's submits for 1 to 3 ms. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 189 | pub(crate) struct Staging { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 190 | pub shared: Arc<Shared>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 191 | pub buffer: vk::Buffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 192 | pub allocation: Option<Allocation>, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 193 | size: usize, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 194 | usage: vk::BufferUsageFlags, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 195 | location: MemoryLocation, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 196 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 197 | |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 198 | pub(crate) struct Pooled { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 199 | pub buffer: vk::Buffer, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 200 | pub allocation: Allocation, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 201 | size: usize, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 202 | usage: vk::BufferUsageFlags, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 203 | location: MemoryLocation, |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 204 | returned: Instant, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 205 | } |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 206 | |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 207 | /// Pooled staging memory kept at most, past which dropped buffers are freed. |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 208 | const POOL_BYTES: usize = 32 * 1024 * 1024; |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 209 | |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 210 | /// How long a pooled buffer may go unused before it is freed. |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 211 | const POOL_IDLE: Duration = Duration::from_secs(2); |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 212 | |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 213 | /// Frees pooled staging buffers that have sat unused for [`POOL_IDLE`]. |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 214 | pub(crate) fn trim_staging(shared: &Shared) { |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 215 | let now = Instant::now(); |
7e34874b2bd1 | atagen | 2026-10-19 06:54 UTC | 216 | let expired: Vec<Pooled> = shared |
7e34874b2bd1 | atagen | 2026-10-19 06:54 UTC | 217 | .staging_pool |
7e34874b2bd1 | atagen | 2026-10-19 06:54 UTC | 218 | .lock() |
7e34874b2bd1 | atagen | 2026-10-19 06:54 UTC | 219 | .unwrap_or_else(|e| e.into_inner()) |
7e34874b2bd1 | atagen | 2026-10-19 06:54 UTC | 220 | .extract_if(.., |p| now - p.returned >= POOL_IDLE) |
7e34874b2bd1 | atagen | 2026-10-19 06:54 UTC | 221 | .collect(); |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 222 | if expired.is_empty() { |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 223 | return; |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 224 | } |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 225 | let mut allocator = shared.allocator.lock().unwrap_or_else(|e| e.into_inner()); |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 226 | for p in expired { |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 227 | unsafe { shared.device.device.destroy_buffer(p.buffer, None) }; |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 228 | let _ = allocator.free(p.allocation); |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 229 | } |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 230 | } |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 231 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 232 | impl Staging { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 233 | pub fn new( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 234 | shared: &Arc<Shared>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 235 | size: usize, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 236 | usage: vk::BufferUsageFlags, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 237 | location: MemoryLocation, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 238 | ) -> Result<Self, Error> { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 239 | let reused = { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 240 | let mut pool = shared |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 241 | .staging_pool |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 242 | .lock() |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 243 | .unwrap_or_else(|e| e.into_inner()); |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 244 | let fits = |p: &Pooled| { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 245 | p.usage == usage && p.location == location && p.size >= size && p.size <= size * 2 |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 246 | }; |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 247 | pool.iter().position(fits).map(|i| pool.swap_remove(i)) |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 248 | }; |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 249 | if let Some(p) = reused { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 250 | return Ok(Self { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 251 | shared: shared.clone(), |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 252 | buffer: p.buffer, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 253 | allocation: Some(p.allocation), |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 254 | size: p.size, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 255 | usage, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 256 | location, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 257 | }); |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 258 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 259 | let d = &shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 260 | let buffer = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 261 | d.create_buffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 262 | &vk::BufferCreateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 263 | .size(size as u64) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 264 | .usage(usage), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 265 | None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 266 | )? |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 267 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 268 | let requirements = unsafe { d.get_buffer_memory_requirements(buffer) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 269 | let allocation = shared |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 270 | .allocator |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 271 | .lock() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 272 | .unwrap_or_else(|e| e.into_inner()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 273 | .allocate(&AllocationCreateDesc { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 274 | name: "staging", |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 275 | requirements, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 276 | location, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 277 | linear: true, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 278 | allocation_scheme: AllocationScheme::GpuAllocatorManaged, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 279 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 280 | let allocation = match allocation { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 281 | Ok(a) => a, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 282 | Err(e) => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 283 | unsafe { d.destroy_buffer(buffer, None) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 284 | return Err(e.into()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 285 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 286 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 287 | unsafe { d.bind_buffer_memory(buffer, allocation.memory(), allocation.offset())? }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 288 | Ok(Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 289 | shared: shared.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 290 | buffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 291 | allocation: Some(allocation), |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 292 | size, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 293 | usage, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 294 | location, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 295 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 296 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 297 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 298 | pub fn bytes(&self) -> &[u8] { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 299 | self.allocation |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 300 | .as_ref() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 301 | .and_then(|a| a.mapped_slice()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 302 | .unwrap_or(&[]) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 303 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 304 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 305 | pub fn bytes_mut(&mut self) -> &mut [u8] { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 306 | self.allocation |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 307 | .as_mut() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 308 | .and_then(|a| a.mapped_slice_mut()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 309 | .unwrap_or(&mut []) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 310 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 311 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 312 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 313 | impl Drop for Staging { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 314 | fn drop(&mut self) { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 315 | let Some(allocation) = self.allocation.take() else { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 316 | return; |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 317 | }; |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 318 | let mut pool = self |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 319 | .shared |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 320 | .staging_pool |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 321 | .lock() |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 322 | .unwrap_or_else(|e| e.into_inner()); |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 323 | let pooled: usize = pool.iter().map(|p| p.size).sum(); |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 324 | if pooled + self.size <= POOL_BYTES { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 325 | pool.push(Pooled { |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 326 | buffer: self.buffer, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 327 | allocation, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 328 | size: self.size, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 329 | usage: self.usage, |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 330 | location: self.location, |
cacda79c0faa | atagen | 2026-10-19 06:11 UTC | 331 | returned: Instant::now(), |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 332 | }); |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 333 | return; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 334 | } |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 335 | drop(pool); |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 336 | unsafe { self.shared.device.device.destroy_buffer(self.buffer, None) }; |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 337 | let _ = self |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 338 | .shared |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 339 | .allocator |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 340 | .lock() |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 341 | .unwrap_or_else(|e| e.into_inner()) |
04f0de436841 | atagen | 2026-10-17 04:23 UTC | 342 | .free(allocation); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 343 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 344 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 345 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 346 | /// A sampled texture. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 347 | #[derive(Clone, Debug)] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 348 | pub struct VulkanTexture(pub(crate) Arc<ImageInner>); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 349 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 350 | /// An image the renderer can draw into, from [`smithay::backend::renderer::Offscreen`]. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 351 | #[derive(Debug)] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 352 | pub struct VulkanImage(pub(crate) Arc<ImageInner>); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 353 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 354 | impl VulkanImage { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 355 | /// The image's contents as a texture, for drawing it elsewhere. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 356 | pub fn texture(&self) -> VulkanTexture { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 357 | VulkanTexture(self.0.clone()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 358 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 359 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 360 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 361 | /// A bound render target, borrowing whatever it was bound from. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 362 | #[derive(Debug)] |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 363 | pub struct VulkanTarget<'a>( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 364 | pub(crate) Arc<ImageInner>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 365 | pub(crate) PhantomData<&'a mut ()>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 366 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 367 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 368 | /// Pixels read back from the GPU. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 369 | #[derive(Debug)] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 370 | pub struct VulkanMapping { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 371 | pub(crate) data: Vec<u8>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 372 | pub(crate) width: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 373 | pub(crate) height: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 374 | pub(crate) fourcc: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 375 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 376 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 377 | macro_rules! texture_impl { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 378 | ($t:ty, |$s:ident| $inner:expr) => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 379 | impl Texture for $t { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 380 | fn width(&self) -> u32 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 381 | let $s = self; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 382 | $inner.width |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 383 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 384 | fn height(&self) -> u32 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 385 | let $s = self; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 386 | $inner.height |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 387 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 388 | fn format(&self) -> Option<Fourcc> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 389 | let $s = self; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 390 | Some($inner.fourcc) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 391 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 392 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 393 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 394 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 395 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 396 | texture_impl!(VulkanTexture, |s| s.0); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 397 | texture_impl!(VulkanImage, |s| s.0); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 398 | texture_impl!(VulkanTarget<'_>, |s| s.0); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 399 | texture_impl!(VulkanMapping, |s| s); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 400 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 401 | impl TextureMapping for VulkanMapping { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 402 | fn flipped(&self) -> bool { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 403 | false |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 404 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 405 | } |