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 | }; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 22 | use smithay::reexports::wayland_server::Resource; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 23 | use smithay::reexports::wayland_server::backend::ObjectId; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 24 | use smithay::reexports::wayland_server::protocol::wl_buffer::WlBuffer; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 25 | use smithay::utils::{Buffer, Physical, Rectangle, Size, Transform}; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 26 | use smithay::wayland::compositor::SurfaceData; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 27 | use smithay::wayland::shm::{shm_format_to_fourcc, with_buffer_contents}; |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 28 | use std::cell::Cell; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 29 | use std::collections::HashMap; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 30 | use std::fmt; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 31 | use std::marker::PhantomData; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 32 | use std::sync::{Arc, Mutex}; |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 33 | use std::time::{Duration, Instant}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 34 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 35 | struct Pending { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 36 | fence: Arc<FenceInner>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 37 | cmd: vk::CommandBuffer, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 38 | _keep: Vec<Arc<ImageInner>>, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 39 | _staging: Option<Staging>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 40 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 41 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 42 | pub struct VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 43 | pending: Vec<Pending>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 44 | dmabuf_textures: HashMap<WeakDmabuf, VulkanTexture>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 45 | dmabuf_targets: HashMap<WeakDmabuf, Arc<ImageInner>>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 46 | pipelines: Pipelines, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 47 | pool: vk::CommandPool, |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 48 | /// Command buffers whose work finished, reset and reused by `begin`. |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 49 | spare_cmds: Vec<vk::CommandBuffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 50 | samplers: [vk::Sampler; 4], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 51 | min_filter: TextureFilter, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 52 | mag_filter: TextureFilter, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 53 | debug: DebugFlags, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 54 | context: ContextId<VulkanTexture>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 55 | caps: Arc<DmabufCaps>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 56 | shared: Arc<Shared>, |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 57 | submissions: Cell<u64>, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 58 | /// Shm uploads staged off the main thread, by buffer. |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 59 | staged: HashMap<ObjectId, (WlBuffer, StagedShm)>, |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 60 | stall: Cell<StallPhase>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 61 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 62 | |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 63 | /// Where in every 1024 submissions the driver stalls. A phase is trusted |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 64 | /// once two stalls land on it. |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 65 | #[derive(Clone, Copy, Default)] |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 66 | struct StallPhase { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 67 | phase: Option<u64>, |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 68 | seen: Option<u64>, |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 69 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 70 | |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 71 | impl StallPhase { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 72 | fn saw(self, at: u64) -> Self { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 73 | if self.seen == Some(at) { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 74 | if self.phase != Some(at) { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 75 | tracing::debug!(phase = at, "queue submissions stall periodically"); |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 76 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 77 | Self { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 78 | phase: Some(at), |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 79 | seen: Some(at), |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 80 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 81 | } else { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 82 | Self { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 83 | seen: Some(at), |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 84 | ..self |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 85 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 86 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 87 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 88 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 89 | |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 90 | /// A submission this slow is a driver stall rather than work. |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 91 | const STALL: Duration = Duration::from_micros(1500); |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 92 | /// How many submissions early a stall is absorbed. Each empty one costs |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 93 | /// tens of microseconds. |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 94 | const ABSORB_AHEAD: u64 = 8; |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 95 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 96 | impl fmt::Debug for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 97 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 98 | f.debug_struct("VulkanRenderer") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 99 | .field("device", &self.shared.device.name) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 100 | .finish() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 101 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 102 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 103 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 104 | impl VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 105 | pub fn new() -> Result<Self, Error> { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 106 | Self::with_node(None) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 107 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 108 | |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 109 | /// A renderer on the GPU behind DRM device number `node`, so it can |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 110 | /// render into buffers that GPU scans out. |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 111 | pub fn for_node(node: u64) -> Result<Self, Error> { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 112 | Self::with_node(Some(node)) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 113 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 114 | |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 115 | fn with_node(node: Option<u64>) -> Result<Self, Error> { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 116 | let shared = Arc::new(Shared::new(node)?); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 117 | let d = &shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 118 | let pool = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 119 | d.create_command_pool( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 120 | &vk::CommandPoolCreateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 121 | .queue_family_index(shared.device.queue_family) |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 122 | .flags( |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 123 | vk::CommandPoolCreateFlags::TRANSIENT |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 124 | | vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER, |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 125 | ), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 126 | None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 127 | )? |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 128 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 129 | let filter = |f: TextureFilter| match f { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 130 | TextureFilter::Linear => vk::Filter::LINEAR, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 131 | TextureFilter::Nearest => vk::Filter::NEAREST, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 132 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 133 | let mut samplers = [vk::Sampler::null(); 4]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 134 | for (i, s) in samplers.iter_mut().enumerate() { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 135 | let (min, mag) = (sampler_filter(i >> 1), sampler_filter(i & 1)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 136 | *s = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 137 | d.create_sampler( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 138 | &vk::SamplerCreateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 139 | .min_filter(filter(min)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 140 | .mag_filter(filter(mag)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 141 | .address_mode_u(vk::SamplerAddressMode::CLAMP_TO_EDGE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 142 | .address_mode_v(vk::SamplerAddressMode::CLAMP_TO_EDGE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 143 | .address_mode_w(vk::SamplerAddressMode::CLAMP_TO_EDGE), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 144 | None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 145 | )? |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 146 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 147 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 148 | Ok(Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 149 | pending: Vec::new(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 150 | dmabuf_textures: HashMap::new(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 151 | dmabuf_targets: HashMap::new(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 152 | pipelines: Pipelines::new(&shared)?, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 153 | pool, |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 154 | spare_cmds: Vec::new(), |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 155 | submissions: Cell::new(0), |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 156 | staged: HashMap::new(), |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 157 | stall: Cell::new(StallPhase { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 158 | // Measured on an RTX 3060, see research/nvidia.md. |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 159 | phase: shared.device.name.contains("NVIDIA").then_some(1015), |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 160 | seen: None, |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 161 | }), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 162 | samplers, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 163 | min_filter: TextureFilter::Linear, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 164 | mag_filter: TextureFilter::Linear, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 165 | debug: DebugFlags::empty(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 166 | context: ContextId::new(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 167 | caps: Arc::new(DmabufCaps::query(&shared)), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 168 | shared, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 169 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 170 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 171 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 172 | /// Name of the physical device in use. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 173 | pub fn device_name(&self) -> &str { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 174 | &self.shared.device.name |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 175 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 176 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 177 | /// Whether dmabufs can be imported, rendered into and allocated. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 178 | pub fn supports_dmabuf(&self) -> bool { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 179 | self.caps.render.iter().next().is_some() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 180 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 181 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 182 | /// Formats and modifiers this renderer can render into as dmabufs. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 183 | pub fn render_formats(&self) -> FormatSet { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 184 | self.caps.render.clone() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 185 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 186 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 187 | /// An allocator for render-capable dmabufs on this device. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 188 | pub fn allocator(&self) -> VulkanAllocator { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 189 | VulkanAllocator { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 190 | shared: self.shared.clone(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 191 | caps: self.caps.clone(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 192 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 193 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 194 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 195 | pub(crate) fn shared(&self) -> &Arc<Shared> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 196 | &self.shared |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 197 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 198 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 199 | pub(crate) fn context_id_ref(&self) -> &ContextId<VulkanTexture> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 200 | &self.context |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 201 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 202 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 203 | fn sampler(&self) -> vk::Sampler { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 204 | let idx = |f: TextureFilter| usize::from(f == TextureFilter::Nearest); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 205 | self.samplers[(idx(self.min_filter) << 1) | idx(self.mag_filter)] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 206 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 207 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 208 | fn reap(&mut self) { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 209 | let spare = &mut self.spare_cmds; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 210 | self.pending.retain(|p| { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 211 | if !p.fence.signaled() { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 212 | return true; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 213 | } |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 214 | spare.push(p.cmd); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 215 | false |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 216 | }); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 217 | self.dmabuf_textures.retain(|k, _| !k.is_gone()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 218 | self.dmabuf_targets.retain(|k, _| !k.is_gone()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 219 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 220 | |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 221 | fn begin(&mut self) -> Result<vk::CommandBuffer, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 222 | let d = &self.shared.device.device; |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 223 | let cmd = match self.spare_cmds.pop() { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 224 | Some(cmd) => { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 225 | unsafe { d.reset_command_buffer(cmd, vk::CommandBufferResetFlags::empty())? }; |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 226 | cmd |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 227 | } |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 228 | None => unsafe { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 229 | d.allocate_command_buffers( |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 230 | &vk::CommandBufferAllocateInfo::default() |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 231 | .command_pool(self.pool) |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 232 | .level(vk::CommandBufferLevel::PRIMARY) |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 233 | .command_buffer_count(1), |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 234 | )?[0] |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 235 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 236 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 237 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 238 | d.begin_command_buffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 239 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 240 | &vk::CommandBufferBeginInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 241 | .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 242 | )? |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 243 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 244 | Ok(cmd) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 245 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 246 | |
30aece3a63ce | atagen | 2026-10-16 13:48 UTC | 247 | #[profiling::function] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 248 | fn end_and_submit(&self, cmd: vk::CommandBuffer) -> Result<Arc<FenceInner>, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 249 | let d = &self.shared.device.device; |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 250 | { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 251 | profiling::scope!("end_command_buffer"); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 252 | unsafe { d.end_command_buffer(cmd)? }; |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 253 | } |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 254 | let fence = { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 255 | profiling::scope!("create_fence"); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 256 | FenceInner::new(&self.shared)? |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 257 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 258 | let cmds = [vk::CommandBufferSubmitInfo::default().command_buffer(cmd)]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 259 | let submit = vk::SubmitInfo2::default().command_buffer_infos(&cmds); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 260 | let queue = self |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 261 | .shared |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 262 | .device |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 263 | .queue |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 264 | .lock() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 265 | .unwrap_or_else(|e| e.into_inner()); |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 266 | let began = Instant::now(); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 267 | { |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 268 | profiling::scope!("queue_submit"); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 269 | unsafe { d.queue_submit2(*queue, &[submit], fence.fence())? }; |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 270 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 271 | let n = self.submissions.get(); |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 272 | self.submissions.set(n + 1); |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 273 | if began.elapsed() > STALL { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 274 | self.stall.set(self.stall.get().saw(n % 1024)); |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 275 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 276 | Ok(fence) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 277 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 278 | |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 279 | /// Spends an upcoming stalling submission on empty command buffers, so |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 280 | /// the stall lands now, in idle time, instead of in a frame. Call it |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 281 | /// right after a flip. NVIDIA stalls for milliseconds on every 1024th |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 282 | /// submission, see research/nvidia.md. |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 283 | pub fn absorb_submit_stall(&mut self) -> Result<(), Error> { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 284 | self.reap(); |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 285 | let Some(phase) = self.stall.get().phase else { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 286 | return Ok(()); |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 287 | }; |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 288 | let ahead = (phase + 1024 - self.submissions.get() % 1024) % 1024; |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 289 | if ahead >= ABSORB_AHEAD { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 290 | return Ok(()); |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 291 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 292 | for _ in 0..=ahead { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 293 | let cmd = self.begin()?; |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 294 | let fence = self.end_and_submit(cmd)?; |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 295 | self.pending.push(Pending { |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 296 | fence, |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 297 | cmd, |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 298 | _keep: Vec::new(), |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 299 | _staging: None, |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 300 | }); |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 301 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 302 | Ok(()) |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 303 | } |
5cd954fce34b | atagen | 2026-10-16 22:42 UTC | 304 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 305 | /// Records, submits and waits for a short command buffer. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 306 | fn one_shot( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 307 | &mut self, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 308 | record: impl FnOnce(&ash::Device, vk::CommandBuffer, u32), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 309 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 310 | let cmd = self.begin()?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 311 | record( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 312 | &self.shared.device.device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 313 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 314 | self.shared.device.queue_family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 315 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 316 | let fence = self.end_and_submit(cmd)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 317 | let res = fence.wait(); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 318 | self.spare_cmds.push(cmd); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 319 | Ok(res?) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 320 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 321 | |
30aece3a63ce | atagen | 2026-10-16 13:48 UTC | 322 | #[profiling::function] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 323 | pub(crate) fn submit( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 324 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 325 | target: &mut VulkanTarget<'_>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 326 | ops: Vec<Op>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 327 | ) -> Result<SyncPoint, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 328 | self.reap(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 329 | let image = target.0.clone(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 330 | let mut sampled: Vec<Arc<ImageInner>> = Vec::new(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 331 | for op in &ops { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 332 | if let Op::Draw { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 333 | texture: Some(t), .. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 334 | } = op |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 335 | && !sampled.iter().any(|s| Arc::ptr_eq(s, &t.0)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 336 | { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 337 | sampled.push(t.0.clone()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 338 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 339 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 340 | let mut pipelines = Vec::with_capacity(ops.len()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 341 | for op in &ops { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 342 | pipelines.push(match op { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 343 | Op::Draw { kind, .. } => Some(self.pipelines.get(image.format, *kind)?), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 344 | Op::Clear { .. } => None, |
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 | let cmd = self.begin()?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 348 | let d = &self.shared.device.device; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 349 | let family = self.shared.device.queue_family; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 350 | let push = &self.shared.device.push_descriptor; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 351 | let (w, h) = (image.width, image.height); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 352 | let sampler = self.sampler(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 353 | unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 354 | for t in &sampled { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 355 | acquire(d, cmd, family, t, vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 356 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 357 | acquire( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 358 | d, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 359 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 360 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 361 | &image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 362 | vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 363 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 364 | let attachments = [vk::RenderingAttachmentInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 365 | .image_view(image.view) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 366 | .image_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 367 | .load_op(vk::AttachmentLoadOp::LOAD) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 368 | .store_op(vk::AttachmentStoreOp::STORE)]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 369 | let area = vk::Rect2D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 370 | offset: vk::Offset2D::default(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 371 | extent: vk::Extent2D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 372 | width: w, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 373 | height: h, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 374 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 375 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 376 | d.cmd_begin_rendering( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 377 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 378 | &vk::RenderingInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 379 | .render_area(area) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 380 | .layer_count(1) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 381 | .color_attachments(&attachments), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 382 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 383 | d.cmd_set_viewport( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 384 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 385 | 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 386 | &[vk::Viewport { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 387 | x: 0.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 388 | y: 0.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 389 | width: w as f32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 390 | height: h as f32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 391 | min_depth: 0.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 392 | max_depth: 1.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 393 | }], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 394 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 395 | let mut bound = vk::Pipeline::null(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 396 | for (op, pipeline) in ops.iter().zip(pipelines) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 397 | match op { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 398 | Op::Clear { color, rects } => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 399 | let clear = [vk::ClearAttachment { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 400 | aspect_mask: vk::ImageAspectFlags::COLOR, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 401 | color_attachment: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 402 | clear_value: vk::ClearValue { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 403 | color: vk::ClearColorValue { float32: *color }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 404 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 405 | }]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 406 | let rects: Vec<_> = rects |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 407 | .iter() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 408 | .map(|&rect| vk::ClearRect { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 409 | rect, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 410 | base_array_layer: 0, |
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 | .collect(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 414 | d.cmd_clear_attachments(cmd, &clear, &rects); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 415 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 416 | Op::Draw { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 417 | texture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 418 | push: constants, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 419 | scissors, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 420 | .. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 421 | } => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 422 | let pipeline = pipeline.unwrap_or_default(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 423 | if pipeline != bound { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 424 | d.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipeline); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 425 | bound = pipeline; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 426 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 427 | if let Some(t) = texture { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 428 | let info = [vk::DescriptorImageInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 429 | .image_view(t.0.view) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 430 | .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 431 | .sampler(sampler)]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 432 | let write = vk::WriteDescriptorSet::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 433 | .dst_binding(0) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 434 | .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 435 | .image_info(&info); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 436 | push.cmd_push_descriptor_set( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 437 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 438 | vk::PipelineBindPoint::GRAPHICS, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 439 | self.pipelines.layout, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 440 | 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 441 | &[write], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 442 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 443 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 444 | d.cmd_push_constants( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 445 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 446 | self.pipelines.layout, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 447 | vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 448 | 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 449 | constants.bytes(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 450 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 451 | for s in scissors { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 452 | d.cmd_set_scissor(cmd, 0, &[*s]); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 453 | d.cmd_draw(cmd, 4, 1, 0, 0); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 454 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 455 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 456 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 457 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 458 | d.cmd_end_rendering(cmd); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 459 | release( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 460 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 461 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 462 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 463 | &image, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 464 | vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 465 | ); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 466 | for t in &sampled { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 467 | release(d, cmd, family, t, vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 468 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 469 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 470 | let fence = self.end_and_submit(cmd)?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 471 | sampled.push(image); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 472 | self.pending.push(Pending { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 473 | fence: fence.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 474 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 475 | _keep: sampled, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 476 | _staging: None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 477 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 478 | Ok(SyncPoint::from(VkFence(fence))) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 479 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 480 | |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 481 | /// Copies `region` of a full-image `data` into `image`, rows top-down or |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 482 | /// flipped. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 483 | fn upload( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 484 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 485 | image: &Arc<ImageInner>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 486 | data: &[u8], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 487 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 488 | flipped: bool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 489 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 490 | let width = image.width as usize; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 491 | let rows = image.height as usize; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 492 | let needed = width * rows * BYTES_PER_PIXEL; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 493 | if data.len() < needed { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 494 | return Err(Error::BufferTooSmall { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 495 | needed, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 496 | got: data.len(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 497 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 498 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 499 | let stride = width * BYTES_PER_PIXEL; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 500 | self.upload_with(image, region, |y| { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 501 | let from = if flipped { rows - 1 - y } else { y }; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 502 | let start = from * stride + region.loc.x as usize * BYTES_PER_PIXEL; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 503 | &data[start..start + region.size.w as usize * BYTES_PER_PIXEL] |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 504 | }) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 505 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 506 | |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 507 | /// Uploads `region` of `image`, taking each image row `y` in the region |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 508 | /// from `row(y)`. The CPU does not wait: the copy is ordered before later |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 509 | /// frames by its barrier, and its staging memory lives until its fence. |
30aece3a63ce | atagen | 2026-10-16 13:48 UTC | 510 | #[profiling::function] |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 511 | fn upload_with<'a>( |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 512 | &mut self, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 513 | image: &Arc<ImageInner>, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 514 | region: Rectangle<i32, Buffer>, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 515 | row: impl Fn(usize) -> &'a [u8], |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 516 | ) -> Result<(), Error> { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 517 | if image.external { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 518 | return Err(Error::ForeignTexture); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 519 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 520 | self.reap(); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 521 | if region.size.w <= 0 || region.size.h <= 0 { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 522 | return Ok(()); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 523 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 524 | let staging = stage_rows(&self.shared, region, row)?; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 525 | self.copy_staged(image, region, staging) |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 526 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 527 | |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 528 | /// Copies staged pixels into `region` of `image` on the GPU. |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 529 | fn copy_staged( |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 530 | &mut self, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 531 | image: &Arc<ImageInner>, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 532 | region: Rectangle<i32, Buffer>, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 533 | staging: Staging, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 534 | ) -> Result<(), Error> { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 535 | let (w, h) = (region.size.w as usize, region.size.h as usize); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 536 | let copy = vk::BufferImageCopy::default() |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 537 | .buffer_row_length(w as u32) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 538 | .image_subresource( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 539 | vk::ImageSubresourceLayers::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 540 | .aspect_mask(vk::ImageAspectFlags::COLOR) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 541 | .layer_count(1), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 542 | ) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 543 | .image_offset(vk::Offset3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 544 | x: region.loc.x, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 545 | y: region.loc.y, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 546 | z: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 547 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 548 | .image_extent(vk::Extent3D { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 549 | width: w as u32, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 550 | height: h as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 551 | depth: 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 552 | }); |
d5eca1c82213 | atagen | 2026-10-16 13:56 UTC | 553 | let cmd = self.begin()?; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 554 | let d = &self.shared.device.device; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 555 | let family = self.shared.device.queue_family; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 556 | unsafe { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 557 | acquire(d, cmd, family, image, vk::ImageLayout::TRANSFER_DST_OPTIMAL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 558 | d.cmd_copy_buffer_to_image( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 559 | cmd, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 560 | staging.buffer, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 561 | image.image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 562 | vk::ImageLayout::TRANSFER_DST_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 563 | &[copy], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 564 | ); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 565 | acquire( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 566 | d, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 567 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 568 | family, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 569 | image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 570 | vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 571 | ); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 572 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 573 | let fence = self.end_and_submit(cmd)?; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 574 | self.pending.push(Pending { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 575 | fence, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 576 | cmd, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 577 | _keep: vec![image.clone()], |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 578 | _staging: Some(staging), |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 579 | }); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 580 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 581 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 582 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 583 | fn read_back( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 584 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 585 | image: &Arc<ImageInner>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 586 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 587 | fourcc: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 588 | ) -> Result<VulkanMapping, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 589 | let region = region |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 590 | .intersection(Rectangle::from_size( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 591 | (image.width as i32, image.height as i32).into(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 592 | )) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 593 | .unwrap_or_default(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 594 | 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 | 595 | let swap = needs_swizzle(image.fourcc, fourcc)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 596 | let len = (w as usize * h as usize * BYTES_PER_PIXEL).max(BYTES_PER_PIXEL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 597 | let staging = Staging::new( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 598 | &self.shared, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 599 | len, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 600 | vk::BufferUsageFlags::TRANSFER_DST, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 601 | MemoryLocation::GpuToCpu, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 602 | )?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 603 | if w > 0 && h > 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 604 | let copy = vk::BufferImageCopy::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 605 | .image_subresource( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 606 | vk::ImageSubresourceLayers::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 607 | .aspect_mask(vk::ImageAspectFlags::COLOR) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 608 | .layer_count(1), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 609 | ) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 610 | .image_offset(vk::Offset3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 611 | x: region.loc.x, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 612 | y: region.loc.y, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 613 | z: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 614 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 615 | .image_extent(vk::Extent3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 616 | width: w, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 617 | height: h, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 618 | depth: 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 619 | }); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 620 | let buffer = staging.buffer; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 621 | let source = image.clone(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 622 | self.one_shot(|d, cmd, family| unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 623 | acquire( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 624 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 625 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 626 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 627 | &source, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 628 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 629 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 630 | d.cmd_copy_image_to_buffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 631 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 632 | source.image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 633 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 634 | buffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 635 | &[copy], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 636 | ); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 637 | release( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 638 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 639 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 640 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 641 | &source, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 642 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 643 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 644 | })?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 645 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 646 | let mut data = staging.bytes()[..w as usize * h as usize * BYTES_PER_PIXEL].to_vec(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 647 | if swap { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 648 | for px in data.as_chunks_mut::<BYTES_PER_PIXEL>().0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 649 | px.swap(0, 2); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 650 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 651 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 652 | Ok(VulkanMapping { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 653 | data, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 654 | width: w, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 655 | height: h, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 656 | fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 657 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 658 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 659 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 660 | fn own(&self, image: &Arc<ImageInner>) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 661 | if Arc::ptr_eq(&image.shared, &self.shared) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 662 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 663 | } else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 664 | Err(Error::ForeignTexture) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 665 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 666 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 667 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 668 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 669 | impl Drop for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 670 | fn drop(&mut self) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 671 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 672 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 673 | let _ = d.device_wait_idle(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 674 | for p in self.pending.drain(..) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 675 | d.free_command_buffers(self.pool, &[p.cmd]); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 676 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 677 | for s in self.samplers { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 678 | d.destroy_sampler(s, None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 679 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 680 | d.destroy_command_pool(self.pool, None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 681 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 682 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 683 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 684 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 685 | fn sampler_filter(bit: usize) -> TextureFilter { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 686 | if bit == 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 687 | TextureFilter::Linear |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 688 | } else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 689 | TextureFilter::Nearest |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 690 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 691 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 692 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 693 | /// A full barrier between all earlier and later work on the image, optionally |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 694 | /// transferring ownership between queue families. Coarse, but correct for |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 695 | /// every transition the renderer makes. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 696 | unsafe fn barrier( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 697 | d: &ash::Device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 698 | cmd: vk::CommandBuffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 699 | image: vk::Image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 700 | old: vk::ImageLayout, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 701 | new: vk::ImageLayout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 702 | src_family: u32, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 703 | dst_family: u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 704 | ) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 705 | let b = [vk::ImageMemoryBarrier2::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 706 | .src_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 707 | .src_access_mask(vk::AccessFlags2::MEMORY_WRITE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 708 | .dst_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 709 | .dst_access_mask(vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 710 | .old_layout(old) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 711 | .new_layout(new) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 712 | .src_queue_family_index(src_family) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 713 | .dst_queue_family_index(dst_family) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 714 | .image(image) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 715 | .subresource_range(color_range())]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 716 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 717 | d.cmd_pipeline_barrier2( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 718 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 719 | &vk::DependencyInfo::default().image_memory_barriers(&b), |
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 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 723 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 724 | /// Moves an image into `layout` for use on this queue. Dmabuf images come |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 725 | /// from the foreign queue family in the general layout, others continue from |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 726 | /// their tracked layout. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 727 | unsafe fn acquire( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 728 | d: &ash::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 729 | cmd: vk::CommandBuffer, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 730 | family: u32, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 731 | image: &ImageInner, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 732 | layout: vk::ImageLayout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 733 | ) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 734 | if image.external { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 735 | unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 736 | barrier( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 737 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 738 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 739 | image.image, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 740 | vk::ImageLayout::GENERAL, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 741 | layout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 742 | vk::QUEUE_FAMILY_FOREIGN_EXT, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 743 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 744 | ) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 745 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 746 | } else { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 747 | let old = image.layout(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 748 | if old != layout { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 749 | unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 750 | barrier( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 751 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 752 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 753 | image.image, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 754 | old, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 755 | layout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 756 | vk::QUEUE_FAMILY_IGNORED, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 757 | vk::QUEUE_FAMILY_IGNORED, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 758 | ) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 759 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 760 | image.set_layout(layout); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 761 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 762 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 763 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 764 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 765 | /// Hands a dmabuf image back to the foreign queue family after use. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 766 | unsafe fn release( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 767 | d: &ash::Device, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 768 | cmd: vk::CommandBuffer, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 769 | family: u32, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 770 | image: &ImageInner, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 771 | layout: vk::ImageLayout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 772 | ) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 773 | if image.external { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 774 | unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 775 | barrier( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 776 | d, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 777 | cmd, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 778 | image.image, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 779 | layout, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 780 | vk::ImageLayout::GENERAL, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 781 | family, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 782 | vk::QUEUE_FAMILY_FOREIGN_EXT, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 783 | ) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 784 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 785 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 786 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 787 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 788 | /// Whether reading `from` as `to` needs red and blue swapped. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 789 | fn needs_swizzle(from: Fourcc, to: Fourcc) -> Result<bool, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 790 | let bgr = |f: Fourcc| match f { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 791 | Fourcc::Argb8888 | Fourcc::Xrgb8888 => Ok(true), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 792 | Fourcc::Abgr8888 | Fourcc::Xbgr8888 => Ok(false), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 793 | f => Err(Error::UnsupportedFormat(f)), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 794 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 795 | Ok(bgr(from)? != bgr(to)?) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 796 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 797 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 798 | impl RendererSuper for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 799 | type Error = Error; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 800 | type TextureId = VulkanTexture; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 801 | type Framebuffer<'buffer> = VulkanTarget<'buffer>; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 802 | type Frame<'frame, 'buffer> |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 803 | = VulkanFrame<'frame, 'buffer> |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 804 | where |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 805 | 'buffer: 'frame, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 806 | Self: 'frame; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 807 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 808 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 809 | impl Renderer for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 810 | fn context_id(&self) -> ContextId<VulkanTexture> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 811 | self.context.clone() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 812 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 813 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 814 | fn downscale_filter(&mut self, filter: TextureFilter) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 815 | self.min_filter = filter; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 816 | Ok(()) |
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 | fn upscale_filter(&mut self, filter: TextureFilter) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 820 | self.mag_filter = filter; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 821 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 822 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 823 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 824 | fn set_debug_flags(&mut self, flags: DebugFlags) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 825 | self.debug = flags; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 826 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 827 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 828 | fn debug_flags(&self) -> DebugFlags { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 829 | self.debug |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 830 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 831 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 832 | fn render<'frame, 'buffer>( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 833 | &'frame mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 834 | framebuffer: &'frame mut VulkanTarget<'buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 835 | output_size: Size<i32, Physical>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 836 | dst_transform: Transform, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 837 | ) -> Result<VulkanFrame<'frame, 'buffer>, Error> |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 838 | where |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 839 | 'buffer: 'frame, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 840 | { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 841 | self.own(&framebuffer.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 842 | self.reap(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 843 | Ok(VulkanFrame::new( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 844 | self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 845 | framebuffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 846 | output_size, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 847 | dst_transform, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 848 | )) |
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 wait(&mut self, sync: &SyncPoint) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 852 | sync.wait().map_err(|_| Error::Interrupted) |
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 | fn cleanup_texture_cache(&mut self) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 856 | self.reap(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 857 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 858 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 859 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 860 | fn invalidate_caches(&mut self) -> Result<(), Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 861 | self.reap(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 862 | self.dmabuf_textures.clear(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 863 | self.dmabuf_targets.clear(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 864 | Ok(()) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 865 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 866 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 867 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 868 | impl Bind<VulkanImage> for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 869 | fn bind<'a>(&mut self, target: &'a mut VulkanImage) -> Result<VulkanTarget<'a>, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 870 | self.own(&target.0)?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 871 | Ok(VulkanTarget(target.0.clone(), PhantomData)) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 872 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 873 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 874 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 875 | impl Bind<Dmabuf> for VulkanRenderer { |
30aece3a63ce | atagen | 2026-10-16 13:48 UTC | 876 | #[profiling::function] |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 877 | fn bind<'a>(&mut self, target: &'a mut Dmabuf) -> Result<VulkanTarget<'a>, Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 878 | let key = target.weak(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 879 | let image = match self.dmabuf_targets.get(&key) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 880 | Some(image) => image.clone(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 881 | None => { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 882 | let image = dmabuf::import(&self.shared, &self.caps, target, RENDER_USAGE)?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 883 | self.dmabuf_targets.insert(key, image.clone()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 884 | image |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 885 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 886 | }; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 887 | Ok(VulkanTarget(image, PhantomData)) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 888 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 889 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 890 | fn supported_formats(&self) -> Option<FormatSet> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 891 | Some(self.caps.render.clone()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 892 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 893 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 894 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 895 | impl Offscreen<VulkanImage> for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 896 | fn create_buffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 897 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 898 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 899 | size: Size<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 900 | ) -> Result<VulkanImage, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 901 | if size.w <= 0 || size.h <= 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 902 | return Err(Error::InvalidSize(size.w, size.h)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 903 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 904 | let usage = vk::ImageUsageFlags::COLOR_ATTACHMENT |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 905 | | vk::ImageUsageFlags::SAMPLED |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 906 | | vk::ImageUsageFlags::TRANSFER_SRC |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 907 | | vk::ImageUsageFlags::TRANSFER_DST; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 908 | Ok(VulkanImage(ImageInner::create( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 909 | &self.shared, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 910 | format, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 911 | size.w as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 912 | size.h as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 913 | usage, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 914 | )?)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 915 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 916 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 917 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 918 | impl ImportMem for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 919 | fn import_memory( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 920 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 921 | data: &[u8], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 922 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 923 | size: Size<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 924 | flipped: bool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 925 | ) -> Result<VulkanTexture, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 926 | if size.w <= 0 || size.h <= 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 927 | return Err(Error::InvalidSize(size.w, size.h)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 928 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 929 | let usage = vk::ImageUsageFlags::SAMPLED |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 930 | | vk::ImageUsageFlags::TRANSFER_DST |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 931 | | vk::ImageUsageFlags::TRANSFER_SRC; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 932 | let image = ImageInner::create(&self.shared, format, size.w as u32, size.h as u32, usage)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 933 | self.upload(&image, data, Rectangle::from_size(size), flipped)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 934 | Ok(VulkanTexture(image)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 935 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 936 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 937 | fn update_memory( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 938 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 939 | texture: &VulkanTexture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 940 | data: &[u8], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 941 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 942 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 943 | self.own(&texture.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 944 | let Some(region) = region.intersection(Rectangle::from_size(texture.size())) else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 945 | return Ok(()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 946 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 947 | self.upload(&texture.0, data, region, false) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 948 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 949 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 950 | fn mem_formats(&self) -> Box<dyn Iterator<Item = Fourcc>> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 951 | Box::new(SUPPORTED.into_iter()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 952 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 953 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 954 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 955 | impl ImportDma for VulkanRenderer { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 956 | fn dmabuf_formats(&self) -> FormatSet { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 957 | self.caps.sample.clone() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 958 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 959 | |
30aece3a63ce | atagen | 2026-10-16 13:48 UTC | 960 | #[profiling::function] |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 961 | fn import_dmabuf( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 962 | &mut self, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 963 | buffer: &Dmabuf, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 964 | _damage: Option<&[Rectangle<i32, Buffer>]>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 965 | ) -> Result<VulkanTexture, Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 966 | let key = buffer.weak(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 967 | if let Some(t) = self.dmabuf_textures.get(&key) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 968 | return Ok(t.clone()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 969 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 970 | let texture = VulkanTexture(dmabuf::import( |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 971 | &self.shared, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 972 | &self.caps, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 973 | buffer, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 974 | SAMPLE_USAGE, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 975 | )?); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 976 | self.dmabuf_textures.insert(key, texture.clone()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 977 | Ok(texture) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 978 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 979 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 980 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 981 | impl ImportDmaWl for VulkanRenderer {} |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 982 | |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 983 | /// Imports dmabufs on another thread. The textures it makes belong to the |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 984 | /// renderer it came from, which takes them back with `adopt_dmabuf`. |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 985 | #[derive(Clone)] |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 986 | pub struct DmabufImporter { |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 987 | shared: Arc<Shared>, |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 988 | caps: Arc<DmabufCaps>, |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 989 | } |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 990 | |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 991 | impl DmabufImporter { |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 992 | pub fn import(&self, buffer: &Dmabuf) -> Result<VulkanTexture, Error> { |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 993 | dmabuf::import(&self.shared, &self.caps, buffer, SAMPLE_USAGE).map(VulkanTexture) |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 994 | } |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 995 | } |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 996 | |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 997 | impl VulkanRenderer { |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 998 | pub fn dmabuf_importer(&self) -> DmabufImporter { |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 999 | DmabufImporter { |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1000 | shared: self.shared.clone(), |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1001 | caps: self.caps.clone(), |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1002 | } |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1003 | } |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1004 | |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1005 | pub fn has_dmabuf(&self, buffer: &Dmabuf) -> bool { |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1006 | self.dmabuf_textures.contains_key(&buffer.weak()) |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1007 | } |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1008 | |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1009 | pub fn adopt_dmabuf(&mut self, buffer: &Dmabuf, texture: VulkanTexture) -> Result<(), Error> { |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1010 | self.own(&texture.0)?; |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1011 | self.dmabuf_textures.insert(buffer.weak(), texture); |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1012 | Ok(()) |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1013 | } |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1014 | } |
8956d017e879 | atagen | 2026-10-16 22:50 UTC | 1015 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1016 | impl ExportMem for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1017 | type TextureMapping = VulkanMapping; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1018 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1019 | fn copy_framebuffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1020 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1021 | target: &VulkanTarget<'_>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1022 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1023 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1024 | ) -> Result<VulkanMapping, Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 1025 | self.own(&target.0)?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 1026 | let image = target.0.clone(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1027 | self.read_back(&image, region, format) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1028 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1029 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1030 | fn copy_texture( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1031 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1032 | texture: &VulkanTexture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1033 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1034 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1035 | ) -> Result<VulkanMapping, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1036 | self.own(&texture.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1037 | self.read_back(&texture.0, region, format) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1038 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1039 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 1040 | fn can_read_texture(&mut self, texture: &VulkanTexture) -> Result<bool, Error> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 1041 | Ok(texture.0.external || texture.0.layout() != vk::ImageLayout::UNDEFINED) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1042 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1043 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1044 | fn map_texture<'a>(&mut self, mapping: &'a VulkanMapping) -> Result<&'a [u8], Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1045 | Ok(&mapping.data) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1046 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1047 | } |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1048 | |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1049 | impl ImportMemWl for VulkanRenderer { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1050 | /// Keeps one texture per surface and uploads only the damaged rows into |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1051 | /// it, straight from the client's shm pool. |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1052 | fn import_shm_buffer( |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1053 | &mut self, |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1054 | buffer: &WlBuffer, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1055 | surface: Option<&SurfaceData>, |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1056 | damage: &[Rectangle<i32, Buffer>], |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1057 | ) -> Result<VulkanTexture, Error> { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1058 | self.staged.retain(|_, (b, _)| b.is_alive()); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1059 | let staged = self.staged.remove(&buffer.id()).map(|(_, s)| s); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1060 | let cache = surface.map(|s| { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1061 | s.data_map |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1062 | .insert_if_missing_threadsafe(|| ShmCache(Mutex::new(None))); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1063 | s.data_map.get::<ShmCache>().expect("just inserted") |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1064 | }); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1065 | with_buffer_contents(buffer, |ptr, len, data| { |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1066 | let fourcc = shm_format_to_fourcc(data.format).ok_or(Error::ShmAccess)?; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1067 | let (w, h, stride, offset) = ( |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1068 | data.width as usize, |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1069 | data.height as usize, |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1070 | data.stride as usize, |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1071 | data.offset as usize, |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1072 | ); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1073 | let line = w * BYTES_PER_PIXEL; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1074 | if w == 0 || h == 0 || stride < line || offset + stride * (h - 1) + line > len { |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1075 | return Err(Error::ShmAccess); |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1076 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1077 | let size = Size::from((data.width, data.height)); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1078 | let full = Rectangle::from_size(size); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1079 | let reused = cache |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1080 | .and_then(|c| c.0.lock().unwrap_or_else(|e| e.into_inner()).clone()) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1081 | .filter(|t| { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1082 | t.0.fourcc == fourcc |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1083 | && t.size() == size |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1084 | && Arc::ptr_eq(&t.0.shared, &self.shared) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1085 | }); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1086 | let (texture, region) = match reused { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1087 | Some(t) => { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1088 | let dirty = damage |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1089 | .iter() |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1090 | .filter_map(|d| d.intersection(full)) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1091 | .reduce(|a, b| a.merge(b)); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1092 | (t, if damage.is_empty() { Some(full) } else { dirty }) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1093 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1094 | None => { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1095 | let usage = vk::ImageUsageFlags::SAMPLED |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1096 | | vk::ImageUsageFlags::TRANSFER_DST |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1097 | | vk::ImageUsageFlags::TRANSFER_SRC; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1098 | let image = |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1099 | ImageInner::create(&self.shared, fourcc, w as u32, h as u32, usage)?; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1100 | (VulkanTexture(image), Some(full)) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1101 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1102 | }; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1103 | let staged = staged.filter(|s| { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1104 | s.fourcc == fourcc |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1105 | && s.size == size |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1106 | && region.is_some_and(|r| s.region.contains_rect(r)) |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1107 | }); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1108 | if let Some(s) = staged { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1109 | tracing::trace!(region = ?s.region, "uploading a staged shm copy"); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1110 | self.copy_staged(&texture.0, s.region, s.staging)?; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1111 | } else if let Some(region) = region { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1112 | let x = region.loc.x as usize * BYTES_PER_PIXEL; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1113 | let rw = region.size.w as usize * BYTES_PER_PIXEL; |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1114 | self.upload_with(&texture.0, region, |y| unsafe { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1115 | std::slice::from_raw_parts(ptr.add(offset + y * stride + x), rw) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1116 | })?; |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1117 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1118 | if let Some(c) = cache { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1119 | *c.0.lock().unwrap_or_else(|e| e.into_inner()) = Some(texture.clone()); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1120 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1121 | Ok(texture) |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1122 | }) |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1123 | .map_err(|_| Error::ShmAccess)? |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1124 | } |
d4a64ce87164 | atagen | 2026-10-16 09:34 UTC | 1125 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1126 | |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1127 | /// The texture last imported for a surface's shm buffers. |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 1128 | struct ShmCache(Mutex<Option<VulkanTexture>>); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1129 | |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1130 | /// Copies `region` of a full-image `row` source into new staging memory. |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1131 | fn stage_rows<'a>( |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1132 | shared: &Arc<Shared>, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1133 | region: Rectangle<i32, Buffer>, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1134 | row: impl Fn(usize) -> &'a [u8], |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1135 | ) -> Result<Staging, Error> { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1136 | let (w, h) = (region.size.w as usize, region.size.h as usize); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1137 | let line = w * BYTES_PER_PIXEL; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1138 | let mut staging = Staging::new( |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1139 | shared, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1140 | line * h, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1141 | vk::BufferUsageFlags::TRANSFER_SRC, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1142 | MemoryLocation::CpuToGpu, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1143 | )?; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1144 | let dst = staging.bytes_mut(); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1145 | for i in 0..h { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1146 | dst[i * line..(i + 1) * line].copy_from_slice(row(region.loc.y as usize + i)); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1147 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1148 | Ok(staging) |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1149 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1150 | |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1151 | /// Shm pixels copied off the main thread, waiting for the import of the |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1152 | /// commit that shows them. |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1153 | pub struct StagedShm { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1154 | staging: Staging, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1155 | region: Rectangle<i32, Buffer>, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1156 | fourcc: Fourcc, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1157 | size: Size<i32, Buffer>, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1158 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1159 | |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1160 | /// Copies shm buffers into staging memory on another thread. The renderer |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1161 | /// it came from takes the result back with `adopt_staged`. |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1162 | #[derive(Clone)] |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1163 | pub struct ShmStager { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1164 | shared: Arc<Shared>, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1165 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1166 | |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1167 | impl ShmStager { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1168 | /// Stages `region` of `buffer`, all of it when the region is `None`. |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1169 | pub fn stage( |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1170 | &self, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1171 | buffer: &WlBuffer, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1172 | region: Option<Rectangle<i32, Buffer>>, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1173 | ) -> Result<StagedShm, Error> { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1174 | with_buffer_contents(buffer, |ptr, len, data| { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1175 | let fourcc = shm_format_to_fourcc(data.format).ok_or(Error::ShmAccess)?; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1176 | let (w, h, stride, offset) = ( |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1177 | data.width as usize, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1178 | data.height as usize, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1179 | data.stride as usize, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1180 | data.offset as usize, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1181 | ); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1182 | let line = w * BYTES_PER_PIXEL; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1183 | if w == 0 || h == 0 || stride < line || offset + stride * (h - 1) + line > len { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1184 | return Err(Error::ShmAccess); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1185 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1186 | let size = Size::from((data.width, data.height)); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1187 | let full = Rectangle::from_size(size); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1188 | let region = region.and_then(|r| r.intersection(full)).unwrap_or(full); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1189 | let x = region.loc.x as usize * BYTES_PER_PIXEL; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1190 | let rw = region.size.w as usize * BYTES_PER_PIXEL; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1191 | // SAFETY: the bounds check above keeps every row inside the |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1192 | // `len` bytes smithay mapped at `ptr`. |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1193 | let staging = stage_rows(&self.shared, region, |y| unsafe { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1194 | std::slice::from_raw_parts(ptr.add(offset + y * stride + x), rw) |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1195 | })?; |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1196 | Ok(StagedShm { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1197 | staging, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1198 | region, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1199 | fourcc, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1200 | size, |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1201 | }) |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1202 | }) |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1203 | .map_err(|_| Error::ShmAccess)? |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1204 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1205 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1206 | |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1207 | impl VulkanRenderer { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1208 | pub fn shm_stager(&self) -> ShmStager { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1209 | ShmStager { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1210 | shared: self.shared.clone(), |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1211 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1212 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1213 | |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1214 | /// Hands back pixels a stager copied, used by the next import of `buffer`. |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1215 | pub fn adopt_staged(&mut self, buffer: &WlBuffer, staged: StagedShm) { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1216 | if Arc::ptr_eq(&staged.staging.shared, &self.shared) { |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1217 | self.staged.insert(buffer.id(), (buffer.clone(), staged)); |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1218 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1219 | } |
0f11410be91b | atagen | 2026-10-17 03:59 UTC | 1220 | } |