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; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 3 | use crate::dmabuf::{self, DmabufCaps, RENDER_USAGE, SAMPLE_USAGE, VulkanAllocator}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 4 | use crate::format::{BYTES_PER_PIXEL, SUPPORTED}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 5 | use crate::frame::{Op, VulkanFrame}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 6 | use crate::image::{ |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 7 | ImageInner, Staging, VulkanImage, VulkanMapping, VulkanTarget, VulkanTexture, color_range, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 8 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 9 | use crate::pipeline::Pipelines; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 10 | use crate::sync::{FenceInner, VkFence}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 11 | use ash::vk; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 12 | use gpu_allocator::MemoryLocation; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 13 | use smithay::backend::allocator::Fourcc; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 14 | use smithay::backend::allocator::dmabuf::{Dmabuf, WeakDmabuf}; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 15 | use smithay::backend::allocator::format::FormatSet; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 16 | use smithay::backend::renderer::ImportMemWl; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 17 | use smithay::backend::renderer::sync::SyncPoint; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 18 | use smithay::backend::renderer::{ |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 19 | Bind, ContextId, DebugFlags, ExportMem, ImportDma, ImportDmaWl, ImportMem, Offscreen, Renderer, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 20 | RendererSuper, Texture, TextureFilter, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 21 | }; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 22 | use smithay::reexports::wayland_server::protocol::wl_buffer::WlBuffer; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 23 | use smithay::utils::{Buffer, Physical, Rectangle, Size, Transform}; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 24 | use smithay::wayland::compositor::SurfaceData; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 25 | use smithay::wayland::shm::{shm_format_to_fourcc, with_buffer_contents}; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 26 | use std::collections::HashMap; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 27 | use std::fmt; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 28 | use std::marker::PhantomData; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 29 | use std::sync::{Arc, Mutex}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 30 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 31 | struct Pending { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 32 | fence: Arc<FenceInner>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 33 | cmd: vk::CommandBuffer, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 34 | _keep: Vec<Arc<ImageInner>>, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 35 | _staging: Option<Staging>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 36 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 37 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 38 | pub struct VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 39 | pending: Vec<Pending>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 40 | dmabuf_textures: HashMap<WeakDmabuf, VulkanTexture>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 41 | dmabuf_targets: HashMap<WeakDmabuf, Arc<ImageInner>>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 42 | pipelines: Pipelines, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 43 | pool: vk::CommandPool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 44 | samplers: [vk::Sampler; 4], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 45 | min_filter: TextureFilter, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 46 | mag_filter: TextureFilter, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 47 | debug: DebugFlags, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 48 | context: ContextId<VulkanTexture>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 49 | caps: Arc<DmabufCaps>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 50 | shared: Arc<Shared>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 51 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 52 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 53 | impl fmt::Debug for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 54 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 55 | f.debug_struct("VulkanRenderer") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 56 | .field("device", &self.shared.device.name) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 57 | .finish() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 58 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 59 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 60 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 61 | impl VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 62 | pub fn new() -> Result<Self, Error> { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 63 | Self::with_node(None) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 64 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 65 | |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 66 | /// A renderer on the GPU behind DRM device number `node`, so it can |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 67 | /// render into buffers that GPU scans out. |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 68 | pub fn for_node(node: u64) -> Result<Self, Error> { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 69 | Self::with_node(Some(node)) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 70 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 71 | |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 72 | fn with_node(node: Option<u64>) -> Result<Self, Error> { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 73 | let shared = Arc::new(Shared::new(node)?); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 74 | let d = &shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 75 | let pool = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 76 | d.create_command_pool( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 77 | &vk::CommandPoolCreateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 78 | .queue_family_index(shared.device.queue_family) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 79 | .flags(vk::CommandPoolCreateFlags::TRANSIENT), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 80 | None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 81 | )? |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 82 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 83 | let filter = |f: TextureFilter| match f { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 84 | TextureFilter::Linear => vk::Filter::LINEAR, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 85 | TextureFilter::Nearest => vk::Filter::NEAREST, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 86 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 87 | let mut samplers = [vk::Sampler::null(); 4]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 88 | for (i, s) in samplers.iter_mut().enumerate() { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 89 | let (min, mag) = (sampler_filter(i >> 1), sampler_filter(i & 1)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 90 | *s = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 91 | d.create_sampler( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 92 | &vk::SamplerCreateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 93 | .min_filter(filter(min)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 94 | .mag_filter(filter(mag)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 95 | .address_mode_u(vk::SamplerAddressMode::CLAMP_TO_EDGE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 96 | .address_mode_v(vk::SamplerAddressMode::CLAMP_TO_EDGE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 97 | .address_mode_w(vk::SamplerAddressMode::CLAMP_TO_EDGE), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 98 | None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 99 | )? |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 100 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 101 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 102 | Ok(Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 103 | pending: Vec::new(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 104 | dmabuf_textures: HashMap::new(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 105 | dmabuf_targets: HashMap::new(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 106 | pipelines: Pipelines::new(&shared)?, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 107 | pool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 108 | samplers, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 109 | min_filter: TextureFilter::Linear, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 110 | mag_filter: TextureFilter::Linear, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 111 | debug: DebugFlags::empty(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 112 | context: ContextId::new(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 113 | caps: Arc::new(DmabufCaps::query(&shared)), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 114 | shared, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 115 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 116 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 117 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 118 | /// Name of the physical device in use. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 119 | pub fn device_name(&self) -> &str { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 120 | &self.shared.device.name |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 121 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 122 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 123 | /// Whether dmabufs can be imported, rendered into and allocated. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 124 | pub fn supports_dmabuf(&self) -> bool { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 125 | self.caps.render.iter().next().is_some() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 126 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 127 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 128 | /// Formats and modifiers this renderer can render into as dmabufs. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 129 | pub fn render_formats(&self) -> FormatSet { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 130 | self.caps.render.clone() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 131 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 132 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 133 | /// An allocator for render-capable dmabufs on this device. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 134 | pub fn allocator(&self) -> VulkanAllocator { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 135 | VulkanAllocator { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 136 | shared: self.shared.clone(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 137 | caps: self.caps.clone(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 138 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 139 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 140 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 141 | pub(crate) fn shared(&self) -> &Arc<Shared> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 142 | &self.shared |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 143 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 144 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 145 | pub(crate) fn context_id_ref(&self) -> &ContextId<VulkanTexture> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 146 | &self.context |
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 sampler(&self) -> vk::Sampler { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 150 | let idx = |f: TextureFilter| usize::from(f == TextureFilter::Nearest); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 151 | self.samplers[(idx(self.min_filter) << 1) | idx(self.mag_filter)] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 152 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 153 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 154 | fn reap(&mut self) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 155 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 156 | let pool = self.pool; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 157 | self.pending.retain(|p| { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 158 | if !p.fence.signaled() { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 159 | return true; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 160 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 161 | unsafe { d.free_command_buffers(pool, &[p.cmd]) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 162 | false |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 163 | }); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 164 | self.dmabuf_textures.retain(|k, _| !k.is_gone()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 165 | self.dmabuf_targets.retain(|k, _| !k.is_gone()); |
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 | fn begin(&self) -> Result<vk::CommandBuffer, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 169 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 170 | let cmd = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 171 | d.allocate_command_buffers( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 172 | &vk::CommandBufferAllocateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 173 | .command_pool(self.pool) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 174 | .level(vk::CommandBufferLevel::PRIMARY) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 175 | .command_buffer_count(1), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 176 | )?[0] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 177 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 178 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 179 | d.begin_command_buffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 180 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 181 | &vk::CommandBufferBeginInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 182 | .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT), |
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 | Ok(cmd) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 186 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 187 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 188 | fn end_and_submit(&self, cmd: vk::CommandBuffer) -> Result<Arc<FenceInner>, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 189 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 190 | unsafe { d.end_command_buffer(cmd)? }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 191 | let fence = FenceInner::new(&self.shared)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 192 | let cmds = [vk::CommandBufferSubmitInfo::default().command_buffer(cmd)]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 193 | let submit = vk::SubmitInfo2::default().command_buffer_infos(&cmds); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 194 | let queue = self |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 195 | .shared |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 196 | .device |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 197 | .queue |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 198 | .lock() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 199 | .unwrap_or_else(|e| e.into_inner()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 200 | unsafe { d.queue_submit2(*queue, &[submit], fence.fence())? }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 201 | Ok(fence) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 202 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 203 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 204 | /// Records, submits and waits for a short command buffer. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 205 | fn one_shot( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 206 | &mut self, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 207 | record: impl FnOnce(&ash::Device, vk::CommandBuffer, u32), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 208 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 209 | let cmd = self.begin()?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 210 | record( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 211 | &self.shared.device.device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 212 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 213 | self.shared.device.queue_family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 214 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 215 | let fence = self.end_and_submit(cmd)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 216 | let res = fence.wait(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 217 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 218 | self.shared |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 219 | .device |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 220 | .device |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 221 | .free_command_buffers(self.pool, &[cmd]) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 222 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 223 | Ok(res?) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 224 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 225 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 226 | pub(crate) fn submit( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 227 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 228 | target: &mut VulkanTarget<'_>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 229 | ops: Vec<Op>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 230 | ) -> Result<SyncPoint, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 231 | self.reap(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 232 | let image = target.0.clone(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 233 | let mut sampled: Vec<Arc<ImageInner>> = Vec::new(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 234 | for op in &ops { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 235 | if let Op::Draw { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 236 | texture: Some(t), .. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 237 | } = op |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 238 | && !sampled.iter().any(|s| Arc::ptr_eq(s, &t.0)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 239 | { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 240 | sampled.push(t.0.clone()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 241 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 242 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 243 | let mut pipelines = Vec::with_capacity(ops.len()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 244 | for op in &ops { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 245 | pipelines.push(match op { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 246 | Op::Draw { kind, .. } => Some(self.pipelines.get(image.format, *kind)?), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 247 | Op::Clear { .. } => None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 248 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 249 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 250 | let cmd = self.begin()?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 251 | let d = &self.shared.device.device; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 252 | let family = self.shared.device.queue_family; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 253 | let push = &self.shared.device.push_descriptor; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 254 | let (w, h) = (image.width, image.height); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 255 | let sampler = self.sampler(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 256 | unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 257 | for t in &sampled { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 258 | acquire(d, cmd, family, t, vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 259 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 260 | acquire( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 261 | d, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 262 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 263 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 264 | &image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 265 | vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 266 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 267 | let attachments = [vk::RenderingAttachmentInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 268 | .image_view(image.view) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 269 | .image_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 270 | .load_op(vk::AttachmentLoadOp::LOAD) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 271 | .store_op(vk::AttachmentStoreOp::STORE)]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 272 | let area = vk::Rect2D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 273 | offset: vk::Offset2D::default(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 274 | extent: vk::Extent2D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 275 | width: w, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 276 | height: h, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 277 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 278 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 279 | d.cmd_begin_rendering( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 280 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 281 | &vk::RenderingInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 282 | .render_area(area) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 283 | .layer_count(1) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 284 | .color_attachments(&attachments), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 285 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 286 | d.cmd_set_viewport( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 287 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 288 | 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 289 | &[vk::Viewport { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 290 | x: 0.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 291 | y: 0.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 292 | width: w as f32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 293 | height: h as f32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 294 | min_depth: 0.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 295 | max_depth: 1.0, |
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 | let mut bound = vk::Pipeline::null(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 299 | for (op, pipeline) in ops.iter().zip(pipelines) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 300 | match op { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 301 | Op::Clear { color, rects } => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 302 | let clear = [vk::ClearAttachment { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 303 | aspect_mask: vk::ImageAspectFlags::COLOR, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 304 | color_attachment: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 305 | clear_value: vk::ClearValue { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 306 | color: vk::ClearColorValue { float32: *color }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 307 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 308 | }]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 309 | let rects: Vec<_> = rects |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 310 | .iter() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 311 | .map(|&rect| vk::ClearRect { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 312 | rect, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 313 | base_array_layer: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 314 | layer_count: 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 315 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 316 | .collect(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 317 | d.cmd_clear_attachments(cmd, &clear, &rects); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 318 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 319 | Op::Draw { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 320 | texture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 321 | push: constants, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 322 | scissors, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 323 | .. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 324 | } => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 325 | let pipeline = pipeline.unwrap_or_default(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 326 | if pipeline != bound { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 327 | d.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipeline); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 328 | bound = pipeline; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 329 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 330 | if let Some(t) = texture { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 331 | let info = [vk::DescriptorImageInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 332 | .image_view(t.0.view) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 333 | .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 334 | .sampler(sampler)]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 335 | let write = vk::WriteDescriptorSet::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 336 | .dst_binding(0) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 337 | .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 338 | .image_info(&info); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 339 | push.cmd_push_descriptor_set( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 340 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 341 | vk::PipelineBindPoint::GRAPHICS, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 342 | self.pipelines.layout, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 343 | 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 344 | &[write], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 345 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 346 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 347 | d.cmd_push_constants( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 348 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 349 | self.pipelines.layout, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 350 | vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 351 | 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 352 | constants.bytes(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 353 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 354 | for s in scissors { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 355 | d.cmd_set_scissor(cmd, 0, &[*s]); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 356 | d.cmd_draw(cmd, 4, 1, 0, 0); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 357 | } |
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 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 361 | d.cmd_end_rendering(cmd); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 362 | release( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 363 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 364 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 365 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 366 | &image, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 367 | vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 368 | ); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 369 | for t in &sampled { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 370 | release(d, cmd, family, t, vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 371 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 372 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 373 | let fence = self.end_and_submit(cmd)?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 374 | sampled.push(image); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 375 | self.pending.push(Pending { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 376 | fence: fence.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 377 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 378 | _keep: sampled, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 379 | _staging: None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 380 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 381 | Ok(SyncPoint::from(VkFence(fence))) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 382 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 383 | |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 384 | /// Copies `region` of a full-image `data` into `image`, rows top-down or |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 385 | /// flipped. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 386 | fn upload( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 387 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 388 | image: &Arc<ImageInner>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 389 | data: &[u8], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 390 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 391 | flipped: bool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 392 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 393 | let width = image.width as usize; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 394 | let rows = image.height as usize; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 395 | let needed = width * rows * BYTES_PER_PIXEL; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 396 | if data.len() < needed { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 397 | return Err(Error::BufferTooSmall { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 398 | needed, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 399 | got: data.len(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 400 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 401 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 402 | let stride = width * BYTES_PER_PIXEL; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 403 | self.upload_with(image, region, |y| { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 404 | let from = if flipped { rows - 1 - y } else { y }; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 405 | let start = from * stride + region.loc.x as usize * BYTES_PER_PIXEL; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 406 | &data[start..start + region.size.w as usize * BYTES_PER_PIXEL] |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 407 | }) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 408 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 409 | |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 410 | /// Uploads `region` of `image`, taking each image row `y` in the region |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 411 | /// from `row(y)`. The CPU does not wait: the copy is ordered before later |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 412 | /// frames by its barrier, and its staging memory lives until its fence. |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 413 | fn upload_with<'a>( |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 414 | &mut self, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 415 | image: &Arc<ImageInner>, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 416 | region: Rectangle<i32, Buffer>, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 417 | row: impl Fn(usize) -> &'a [u8], |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 418 | ) -> Result<(), Error> { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 419 | if image.external { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 420 | return Err(Error::ForeignTexture); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 421 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 422 | self.reap(); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 423 | let (w, h) = (region.size.w as usize, region.size.h as usize); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 424 | if w == 0 || h == 0 { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 425 | return Ok(()); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 426 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 427 | let line = w * BYTES_PER_PIXEL; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 428 | let mut staging = Staging::new( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 429 | &self.shared, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 430 | line * h, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 431 | vk::BufferUsageFlags::TRANSFER_SRC, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 432 | MemoryLocation::CpuToGpu, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 433 | )?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 434 | let dst = staging.bytes_mut(); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 435 | for i in 0..h { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 436 | dst[i * line..(i + 1) * line].copy_from_slice(row(region.loc.y as usize + i)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 437 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 438 | let copy = vk::BufferImageCopy::default() |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 439 | .buffer_row_length(w as u32) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 440 | .image_subresource( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 441 | vk::ImageSubresourceLayers::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 442 | .aspect_mask(vk::ImageAspectFlags::COLOR) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 443 | .layer_count(1), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 444 | ) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 445 | .image_offset(vk::Offset3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 446 | x: region.loc.x, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 447 | y: region.loc.y, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 448 | z: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 449 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 450 | .image_extent(vk::Extent3D { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 451 | width: w as u32, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 452 | height: h as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 453 | depth: 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 454 | }); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 455 | let d = &self.shared.device.device; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 456 | let family = self.shared.device.queue_family; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 457 | let cmd = self.begin()?; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 458 | unsafe { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 459 | acquire(d, cmd, family, image, vk::ImageLayout::TRANSFER_DST_OPTIMAL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 460 | d.cmd_copy_buffer_to_image( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 461 | cmd, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 462 | staging.buffer, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 463 | image.image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 464 | vk::ImageLayout::TRANSFER_DST_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 465 | &[copy], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 466 | ); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 467 | acquire( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 468 | d, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 469 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 470 | family, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 471 | image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 472 | vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 473 | ); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 474 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 475 | let fence = self.end_and_submit(cmd)?; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 476 | self.pending.push(Pending { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 477 | fence, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 478 | cmd, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 479 | _keep: vec![image.clone()], |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 480 | _staging: Some(staging), |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 481 | }); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 482 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 483 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 484 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 485 | fn read_back( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 486 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 487 | image: &Arc<ImageInner>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 488 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 489 | fourcc: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 490 | ) -> Result<VulkanMapping, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 491 | let region = region |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 492 | .intersection(Rectangle::from_size( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 493 | (image.width as i32, image.height as i32).into(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 494 | )) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 495 | .unwrap_or_default(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 496 | let (w, h) = (region.size.w.max(0) as u32, region.size.h.max(0) as u32); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 497 | let swap = needs_swizzle(image.fourcc, fourcc)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 498 | let len = (w as usize * h as usize * BYTES_PER_PIXEL).max(BYTES_PER_PIXEL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 499 | let staging = Staging::new( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 500 | &self.shared, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 501 | len, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 502 | vk::BufferUsageFlags::TRANSFER_DST, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 503 | MemoryLocation::GpuToCpu, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 504 | )?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 505 | if w > 0 && h > 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 506 | let copy = vk::BufferImageCopy::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 507 | .image_subresource( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 508 | vk::ImageSubresourceLayers::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 509 | .aspect_mask(vk::ImageAspectFlags::COLOR) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 510 | .layer_count(1), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 511 | ) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 512 | .image_offset(vk::Offset3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 513 | x: region.loc.x, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 514 | y: region.loc.y, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 515 | z: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 516 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 517 | .image_extent(vk::Extent3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 518 | width: w, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 519 | height: h, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 520 | depth: 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 521 | }); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 522 | let buffer = staging.buffer; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 523 | let source = image.clone(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 524 | self.one_shot(|d, cmd, family| unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 525 | acquire( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 526 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 527 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 528 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 529 | &source, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 530 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 531 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 532 | d.cmd_copy_image_to_buffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 533 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 534 | source.image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 535 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 536 | buffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 537 | &[copy], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 538 | ); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 539 | release( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 540 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 541 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 542 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 543 | &source, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 544 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 545 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 546 | })?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 547 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 548 | let mut data = staging.bytes()[..w as usize * h as usize * BYTES_PER_PIXEL].to_vec(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 549 | if swap { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 550 | for px in data.as_chunks_mut::<BYTES_PER_PIXEL>().0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 551 | px.swap(0, 2); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 552 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 553 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 554 | Ok(VulkanMapping { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 555 | data, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 556 | width: w, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 557 | height: h, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 558 | fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 559 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 560 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 561 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 562 | fn own(&self, image: &Arc<ImageInner>) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 563 | if Arc::ptr_eq(&image.shared, &self.shared) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 564 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 565 | } else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 566 | Err(Error::ForeignTexture) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 567 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 568 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 569 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 570 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 571 | impl Drop for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 572 | fn drop(&mut self) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 573 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 574 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 575 | let _ = d.device_wait_idle(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 576 | for p in self.pending.drain(..) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 577 | d.free_command_buffers(self.pool, &[p.cmd]); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 578 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 579 | for s in self.samplers { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 580 | d.destroy_sampler(s, None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 581 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 582 | d.destroy_command_pool(self.pool, None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 583 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 584 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 585 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 586 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 587 | fn sampler_filter(bit: usize) -> TextureFilter { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 588 | if bit == 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 589 | TextureFilter::Linear |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 590 | } else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 591 | TextureFilter::Nearest |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 592 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 593 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 594 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 595 | /// A full barrier between all earlier and later work on the image, optionally |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 596 | /// transferring ownership between queue families. Coarse, but correct for |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 597 | /// every transition the renderer makes. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 598 | unsafe fn barrier( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 599 | d: &ash::Device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 600 | cmd: vk::CommandBuffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 601 | image: vk::Image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 602 | old: vk::ImageLayout, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 603 | new: vk::ImageLayout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 604 | src_family: u32, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 605 | dst_family: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 606 | ) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 607 | let b = [vk::ImageMemoryBarrier2::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 608 | .src_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 609 | .src_access_mask(vk::AccessFlags2::MEMORY_WRITE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 610 | .dst_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 611 | .dst_access_mask(vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 612 | .old_layout(old) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 613 | .new_layout(new) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 614 | .src_queue_family_index(src_family) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 615 | .dst_queue_family_index(dst_family) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 616 | .image(image) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 617 | .subresource_range(color_range())]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 618 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 619 | d.cmd_pipeline_barrier2( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 620 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 621 | &vk::DependencyInfo::default().image_memory_barriers(&b), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 622 | ) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 623 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 624 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 625 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 626 | /// Moves an image into `layout` for use on this queue. Dmabuf images come |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 627 | /// from the foreign queue family in the general layout, others continue from |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 628 | /// their tracked layout. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 629 | unsafe fn acquire( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 630 | d: &ash::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 631 | cmd: vk::CommandBuffer, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 632 | family: u32, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 633 | image: &ImageInner, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 634 | layout: vk::ImageLayout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 635 | ) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 636 | if image.external { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 637 | unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 638 | barrier( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 639 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 640 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 641 | image.image, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 642 | vk::ImageLayout::GENERAL, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 643 | layout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 644 | vk::QUEUE_FAMILY_FOREIGN_EXT, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 645 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 646 | ) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 647 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 648 | } else { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 649 | let old = image.layout(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 650 | if old != layout { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 651 | unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 652 | barrier( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 653 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 654 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 655 | image.image, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 656 | old, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 657 | layout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 658 | vk::QUEUE_FAMILY_IGNORED, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 659 | vk::QUEUE_FAMILY_IGNORED, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 660 | ) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 661 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 662 | image.set_layout(layout); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 663 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 664 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 665 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 666 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 667 | /// Hands a dmabuf image back to the foreign queue family after use. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 668 | unsafe fn release( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 669 | d: &ash::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 670 | cmd: vk::CommandBuffer, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 671 | family: u32, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 672 | image: &ImageInner, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 673 | layout: vk::ImageLayout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 674 | ) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 675 | if image.external { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 676 | unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 677 | barrier( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 678 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 679 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 680 | image.image, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 681 | layout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 682 | vk::ImageLayout::GENERAL, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 683 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 684 | vk::QUEUE_FAMILY_FOREIGN_EXT, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 685 | ) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 686 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 687 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 688 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 689 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 690 | /// Whether reading `from` as `to` needs red and blue swapped. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 691 | fn needs_swizzle(from: Fourcc, to: Fourcc) -> Result<bool, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 692 | let bgr = |f: Fourcc| match f { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 693 | Fourcc::Argb8888 | Fourcc::Xrgb8888 => Ok(true), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 694 | Fourcc::Abgr8888 | Fourcc::Xbgr8888 => Ok(false), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 695 | f => Err(Error::UnsupportedFormat(f)), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 696 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 697 | Ok(bgr(from)? != bgr(to)?) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 698 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 699 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 700 | impl RendererSuper for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 701 | type Error = Error; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 702 | type TextureId = VulkanTexture; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 703 | type Framebuffer<'buffer> = VulkanTarget<'buffer>; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 704 | type Frame<'frame, 'buffer> |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 705 | = VulkanFrame<'frame, 'buffer> |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 706 | where |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 707 | 'buffer: 'frame, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 708 | Self: 'frame; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 709 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 710 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 711 | impl Renderer for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 712 | fn context_id(&self) -> ContextId<VulkanTexture> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 713 | self.context.clone() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 714 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 715 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 716 | fn downscale_filter(&mut self, filter: TextureFilter) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 717 | self.min_filter = filter; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 718 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 719 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 720 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 721 | fn upscale_filter(&mut self, filter: TextureFilter) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 722 | self.mag_filter = filter; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 723 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 724 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 725 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 726 | fn set_debug_flags(&mut self, flags: DebugFlags) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 727 | self.debug = flags; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 728 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 729 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 730 | fn debug_flags(&self) -> DebugFlags { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 731 | self.debug |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 732 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 733 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 734 | fn render<'frame, 'buffer>( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 735 | &'frame mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 736 | framebuffer: &'frame mut VulkanTarget<'buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 737 | output_size: Size<i32, Physical>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 738 | dst_transform: Transform, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 739 | ) -> Result<VulkanFrame<'frame, 'buffer>, Error> |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 740 | where |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 741 | 'buffer: 'frame, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 742 | { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 743 | self.own(&framebuffer.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 744 | self.reap(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 745 | Ok(VulkanFrame::new( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 746 | self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 747 | framebuffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 748 | output_size, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 749 | dst_transform, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 750 | )) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 751 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 752 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 753 | fn wait(&mut self, sync: &SyncPoint) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 754 | sync.wait().map_err(|_| Error::Interrupted) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 755 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 756 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 757 | fn cleanup_texture_cache(&mut self) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 758 | self.reap(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 759 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 760 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 761 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 762 | fn invalidate_caches(&mut self) -> Result<(), Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 763 | self.reap(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 764 | self.dmabuf_textures.clear(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 765 | self.dmabuf_targets.clear(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 766 | Ok(()) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 767 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 768 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 769 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 770 | impl Bind<VulkanImage> for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 771 | fn bind<'a>(&mut self, target: &'a mut VulkanImage) -> Result<VulkanTarget<'a>, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 772 | self.own(&target.0)?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 773 | Ok(VulkanTarget(target.0.clone(), PhantomData)) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 774 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 775 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 776 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 777 | impl Bind<Dmabuf> for VulkanRenderer { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 778 | fn bind<'a>(&mut self, target: &'a mut Dmabuf) -> Result<VulkanTarget<'a>, Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 779 | let key = target.weak(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 780 | let image = match self.dmabuf_targets.get(&key) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 781 | Some(image) => image.clone(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 782 | None => { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 783 | let image = dmabuf::import(&self.shared, &self.caps, target, RENDER_USAGE)?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 784 | self.dmabuf_targets.insert(key, image.clone()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 785 | image |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 786 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 787 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 788 | Ok(VulkanTarget(image, PhantomData)) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 789 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 790 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 791 | fn supported_formats(&self) -> Option<FormatSet> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 792 | Some(self.caps.render.clone()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 793 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 794 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 795 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 796 | impl Offscreen<VulkanImage> for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 797 | fn create_buffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 798 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 799 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 800 | size: Size<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 801 | ) -> Result<VulkanImage, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 802 | if size.w <= 0 || size.h <= 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 803 | return Err(Error::InvalidSize(size.w, size.h)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 804 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 805 | let usage = vk::ImageUsageFlags::COLOR_ATTACHMENT |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 806 | | vk::ImageUsageFlags::SAMPLED |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 807 | | vk::ImageUsageFlags::TRANSFER_SRC |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 808 | | vk::ImageUsageFlags::TRANSFER_DST; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 809 | Ok(VulkanImage(ImageInner::create( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 810 | &self.shared, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 811 | format, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 812 | size.w as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 813 | size.h as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 814 | usage, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 815 | )?)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 816 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 817 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 818 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 819 | impl ImportMem for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 820 | fn import_memory( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 821 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 822 | data: &[u8], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 823 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 824 | size: Size<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 825 | flipped: bool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 826 | ) -> Result<VulkanTexture, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 827 | if size.w <= 0 || size.h <= 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 828 | return Err(Error::InvalidSize(size.w, size.h)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 829 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 830 | let usage = vk::ImageUsageFlags::SAMPLED |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 831 | | vk::ImageUsageFlags::TRANSFER_DST |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 832 | | vk::ImageUsageFlags::TRANSFER_SRC; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 833 | let image = ImageInner::create(&self.shared, format, size.w as u32, size.h as u32, usage)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 834 | self.upload(&image, data, Rectangle::from_size(size), flipped)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 835 | Ok(VulkanTexture(image)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 836 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 837 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 838 | fn update_memory( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 839 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 840 | texture: &VulkanTexture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 841 | data: &[u8], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 842 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 843 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 844 | self.own(&texture.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 845 | let Some(region) = region.intersection(Rectangle::from_size(texture.size())) else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 846 | return Ok(()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 847 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 848 | self.upload(&texture.0, data, region, false) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 849 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 850 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 851 | fn mem_formats(&self) -> Box<dyn Iterator<Item = Fourcc>> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 852 | Box::new(SUPPORTED.into_iter()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 853 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 854 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 855 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 856 | impl ImportDma for VulkanRenderer { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 857 | fn dmabuf_formats(&self) -> FormatSet { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 858 | self.caps.sample.clone() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 859 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 860 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 861 | fn import_dmabuf( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 862 | &mut self, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 863 | buffer: &Dmabuf, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 864 | _damage: Option<&[Rectangle<i32, Buffer>]>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 865 | ) -> Result<VulkanTexture, Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 866 | let key = buffer.weak(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 867 | if let Some(t) = self.dmabuf_textures.get(&key) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 868 | return Ok(t.clone()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 869 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 870 | let texture = VulkanTexture(dmabuf::import( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 871 | &self.shared, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 872 | &self.caps, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 873 | buffer, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 874 | SAMPLE_USAGE, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 875 | )?); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 876 | self.dmabuf_textures.insert(key, texture.clone()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 877 | Ok(texture) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 878 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 879 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 880 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 881 | impl ImportDmaWl for VulkanRenderer {} |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 882 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 883 | impl ExportMem for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 884 | type TextureMapping = VulkanMapping; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 885 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 886 | fn copy_framebuffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 887 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 888 | target: &VulkanTarget<'_>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 889 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 890 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 891 | ) -> Result<VulkanMapping, Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 892 | self.own(&target.0)?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 893 | let image = target.0.clone(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 894 | self.read_back(&image, region, format) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 895 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 896 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 897 | fn copy_texture( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 898 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 899 | texture: &VulkanTexture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 900 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 901 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 902 | ) -> Result<VulkanMapping, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 903 | self.own(&texture.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 904 | self.read_back(&texture.0, region, format) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 905 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 906 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 907 | fn can_read_texture(&mut self, texture: &VulkanTexture) -> Result<bool, Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 908 | Ok(texture.0.external || texture.0.layout() != vk::ImageLayout::UNDEFINED) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 909 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 910 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 911 | fn map_texture<'a>(&mut self, mapping: &'a VulkanMapping) -> Result<&'a [u8], Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 912 | Ok(&mapping.data) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 913 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 914 | } |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 915 | |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 916 | impl ImportMemWl for VulkanRenderer { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 917 | /// Keeps one texture per surface and uploads only the damaged rows into |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 918 | /// it, straight from the client's shm pool. |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 919 | fn import_shm_buffer( |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 920 | &mut self, |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 921 | buffer: &WlBuffer, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 922 | surface: Option<&SurfaceData>, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 923 | damage: &[Rectangle<i32, Buffer>], |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 924 | ) -> Result<VulkanTexture, Error> { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 925 | let cache = surface.map(|s| { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 926 | s.data_map |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 927 | .insert_if_missing_threadsafe(|| ShmCache(Mutex::new(None))); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 928 | s.data_map.get::<ShmCache>().expect("just inserted") |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 929 | }); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 930 | with_buffer_contents(buffer, |ptr, len, data| { |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 931 | let fourcc = shm_format_to_fourcc(data.format).ok_or(Error::ShmAccess)?; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 932 | let (w, h, stride, offset) = ( |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 933 | data.width as usize, |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 934 | data.height as usize, |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 935 | data.stride as usize, |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 936 | data.offset as usize, |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 937 | ); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 938 | let line = w * BYTES_PER_PIXEL; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 939 | if w == 0 || h == 0 || stride < line || offset + stride * (h - 1) + line > len { |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 940 | return Err(Error::ShmAccess); |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 941 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 942 | let size = Size::from((data.width, data.height)); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 943 | let full = Rectangle::from_size(size); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 944 | let reused = cache |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 945 | .and_then(|c| c.0.lock().unwrap_or_else(|e| e.into_inner()).clone()) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 946 | .filter(|t| { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 947 | t.0.fourcc == fourcc |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 948 | && t.size() == size |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 949 | && Arc::ptr_eq(&t.0.shared, &self.shared) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 950 | }); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 951 | let (texture, region) = match reused { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 952 | Some(t) => { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 953 | let dirty = damage |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 954 | .iter() |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 955 | .filter_map(|d| d.intersection(full)) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 956 | .reduce(|a, b| a.merge(b)); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 957 | (t, if damage.is_empty() { Some(full) } else { dirty }) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 958 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 959 | None => { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 960 | let usage = vk::ImageUsageFlags::SAMPLED |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 961 | | vk::ImageUsageFlags::TRANSFER_DST |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 962 | | vk::ImageUsageFlags::TRANSFER_SRC; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 963 | let image = |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 964 | ImageInner::create(&self.shared, fourcc, w as u32, h as u32, usage)?; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 965 | (VulkanTexture(image), Some(full)) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 966 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 967 | }; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 968 | if let Some(region) = region { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 969 | let x = region.loc.x as usize * BYTES_PER_PIXEL; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 970 | let rw = region.size.w as usize * BYTES_PER_PIXEL; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 971 | self.upload_with(&texture.0, region, |y| unsafe { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 972 | std::slice::from_raw_parts(ptr.add(offset + y * stride + x), rw) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 973 | })?; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 974 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 975 | if let Some(c) = cache { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 976 | *c.0.lock().unwrap_or_else(|e| e.into_inner()) = Some(texture.clone()); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 977 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 978 | Ok(texture) |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 979 | }) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 980 | .map_err(|_| Error::ShmAccess)? |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 981 | } |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 982 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 983 | |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 984 | /// The texture last imported for a surface's shm buffers. |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 985 | struct ShmCache(Mutex<Option<VulkanTexture>>); |