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