atagen

monolith

public
Star0
Watch0
30aece3a63cef1251f54902da43c4ccca42cce54
1
ssh://brewery@pls.lobotomise.me:33/atagen/monolith.git

Blame: crates/render-vk/src/renderer.rs

824bf02b6e55atagen2026-10-16 09:15 UTC1
use crate::Error;
824bf02b6e55atagen2026-10-16 09:15 UTC2
use crate::device::Shared;
7de49c2aa70batagen2026-10-16 09:27 UTC3
use crate::dmabuf::{self, DmabufCaps, RENDER_USAGE, SAMPLE_USAGE, VulkanAllocator};
824bf02b6e55atagen2026-10-16 09:15 UTC4
use crate::format::{BYTES_PER_PIXEL, SUPPORTED};
824bf02b6e55atagen2026-10-16 09:15 UTC5
use crate::frame::{Op, VulkanFrame};
824bf02b6e55atagen2026-10-16 09:15 UTC6
use crate::image::{
824bf02b6e55atagen2026-10-16 09:15 UTC7
    ImageInner, Staging, VulkanImage, VulkanMapping, VulkanTarget, VulkanTexture, color_range,
824bf02b6e55atagen2026-10-16 09:15 UTC8
};
824bf02b6e55atagen2026-10-16 09:15 UTC9
use crate::pipeline::Pipelines;
824bf02b6e55atagen2026-10-16 09:15 UTC10
use crate::sync::{FenceInner, VkFence};
824bf02b6e55atagen2026-10-16 09:15 UTC11
use ash::vk;
824bf02b6e55atagen2026-10-16 09:15 UTC12
use gpu_allocator::MemoryLocation;
824bf02b6e55atagen2026-10-16 09:15 UTC13
use smithay::backend::allocator::Fourcc;
7de49c2aa70batagen2026-10-16 09:27 UTC14
use smithay::backend::allocator::dmabuf::{Dmabuf, WeakDmabuf};
7de49c2aa70batagen2026-10-16 09:27 UTC15
use smithay::backend::allocator::format::FormatSet;
d4a64ce87164atagen2026-10-16 09:34 UTC16
use smithay::backend::renderer::ImportMemWl;
824bf02b6e55atagen2026-10-16 09:15 UTC17
use smithay::backend::renderer::sync::SyncPoint;
824bf02b6e55atagen2026-10-16 09:15 UTC18
use smithay::backend::renderer::{
7de49c2aa70batagen2026-10-16 09:27 UTC19
    Bind, ContextId, DebugFlags, ExportMem, ImportDma, ImportDmaWl, ImportMem, Offscreen, Renderer,
7de49c2aa70batagen2026-10-16 09:27 UTC20
    RendererSuper, Texture, TextureFilter,
824bf02b6e55atagen2026-10-16 09:15 UTC21
};
d4a64ce87164atagen2026-10-16 09:34 UTC22
use smithay::reexports::wayland_server::protocol::wl_buffer::WlBuffer;
824bf02b6e55atagen2026-10-16 09:15 UTC23
use smithay::utils::{Buffer, Physical, Rectangle, Size, Transform};
d4a64ce87164atagen2026-10-16 09:34 UTC24
use smithay::wayland::compositor::SurfaceData;
d4a64ce87164atagen2026-10-16 09:34 UTC25
use smithay::wayland::shm::{shm_format_to_fourcc, with_buffer_contents};
7de49c2aa70batagen2026-10-16 09:27 UTC26
use std::collections::HashMap;
824bf02b6e55atagen2026-10-16 09:15 UTC27
use std::fmt;
7de49c2aa70batagen2026-10-16 09:27 UTC28
use std::marker::PhantomData;
48af562d968batagen2026-10-16 12:39 UTC29
use std::sync::{Arc, Mutex};
824bf02b6e55atagen2026-10-16 09:15 UTC30
824bf02b6e55atagen2026-10-16 09:15 UTC31
struct Pending {
824bf02b6e55atagen2026-10-16 09:15 UTC32
    fence: Arc<FenceInner>,
824bf02b6e55atagen2026-10-16 09:15 UTC33
    cmd: vk::CommandBuffer,
7de49c2aa70batagen2026-10-16 09:27 UTC34
    _keep: Vec<Arc<ImageInner>>,
48af562d968batagen2026-10-16 12:39 UTC35
    _staging: Option<Staging>,
824bf02b6e55atagen2026-10-16 09:15 UTC36
}
824bf02b6e55atagen2026-10-16 09:15 UTC37
824bf02b6e55atagen2026-10-16 09:15 UTC38
pub struct VulkanRenderer {
824bf02b6e55atagen2026-10-16 09:15 UTC39
    pending: Vec<Pending>,
7de49c2aa70batagen2026-10-16 09:27 UTC40
    dmabuf_textures: HashMap<WeakDmabuf, VulkanTexture>,
7de49c2aa70batagen2026-10-16 09:27 UTC41
    dmabuf_targets: HashMap<WeakDmabuf, Arc<ImageInner>>,
824bf02b6e55atagen2026-10-16 09:15 UTC42
    pipelines: Pipelines,
824bf02b6e55atagen2026-10-16 09:15 UTC43
    pool: vk::CommandPool,
824bf02b6e55atagen2026-10-16 09:15 UTC44
    samplers: [vk::Sampler; 4],
824bf02b6e55atagen2026-10-16 09:15 UTC45
    min_filter: TextureFilter,
824bf02b6e55atagen2026-10-16 09:15 UTC46
    mag_filter: TextureFilter,
824bf02b6e55atagen2026-10-16 09:15 UTC47
    debug: DebugFlags,
824bf02b6e55atagen2026-10-16 09:15 UTC48
    context: ContextId<VulkanTexture>,
7de49c2aa70batagen2026-10-16 09:27 UTC49
    caps: Arc<DmabufCaps>,
824bf02b6e55atagen2026-10-16 09:15 UTC50
    shared: Arc<Shared>,
824bf02b6e55atagen2026-10-16 09:15 UTC51
}
824bf02b6e55atagen2026-10-16 09:15 UTC52
824bf02b6e55atagen2026-10-16 09:15 UTC53
impl fmt::Debug for VulkanRenderer {
824bf02b6e55atagen2026-10-16 09:15 UTC54
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
824bf02b6e55atagen2026-10-16 09:15 UTC55
        f.debug_struct("VulkanRenderer")
824bf02b6e55atagen2026-10-16 09:15 UTC56
            .field("device", &self.shared.device.name)
824bf02b6e55atagen2026-10-16 09:15 UTC57
            .finish()
824bf02b6e55atagen2026-10-16 09:15 UTC58
    }
824bf02b6e55atagen2026-10-16 09:15 UTC59
}
824bf02b6e55atagen2026-10-16 09:15 UTC60
824bf02b6e55atagen2026-10-16 09:15 UTC61
impl VulkanRenderer {
824bf02b6e55atagen2026-10-16 09:15 UTC62
    pub fn new() -> Result<Self, Error> {
f27fa4c2ade9atagen2026-10-16 12:59 UTC63
        Self::with_node(None)
f27fa4c2ade9atagen2026-10-16 12:59 UTC64
    }
f27fa4c2ade9atagen2026-10-16 12:59 UTC65
f27fa4c2ade9atagen2026-10-16 12:59 UTC66
    /// A renderer on the GPU behind DRM device number `node`, so it can
f27fa4c2ade9atagen2026-10-16 12:59 UTC67
    /// render into buffers that GPU scans out.
f27fa4c2ade9atagen2026-10-16 12:59 UTC68
    pub fn for_node(node: u64) -> Result<Self, Error> {
f27fa4c2ade9atagen2026-10-16 12:59 UTC69
        Self::with_node(Some(node))
f27fa4c2ade9atagen2026-10-16 12:59 UTC70
    }
f27fa4c2ade9atagen2026-10-16 12:59 UTC71
f27fa4c2ade9atagen2026-10-16 12:59 UTC72
    fn with_node(node: Option<u64>) -> Result<Self, Error> {
f27fa4c2ade9atagen2026-10-16 12:59 UTC73
        let shared = Arc::new(Shared::new(node)?);
824bf02b6e55atagen2026-10-16 09:15 UTC74
        let d = &shared.device.device;
824bf02b6e55atagen2026-10-16 09:15 UTC75
        let pool = unsafe {
824bf02b6e55atagen2026-10-16 09:15 UTC76
            d.create_command_pool(
824bf02b6e55atagen2026-10-16 09:15 UTC77
                &vk::CommandPoolCreateInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC78
                    .queue_family_index(shared.device.queue_family)
824bf02b6e55atagen2026-10-16 09:15 UTC79
                    .flags(vk::CommandPoolCreateFlags::TRANSIENT),
824bf02b6e55atagen2026-10-16 09:15 UTC80
                None,
824bf02b6e55atagen2026-10-16 09:15 UTC81
            )?
824bf02b6e55atagen2026-10-16 09:15 UTC82
        };
824bf02b6e55atagen2026-10-16 09:15 UTC83
        let filter = |f: TextureFilter| match f {
824bf02b6e55atagen2026-10-16 09:15 UTC84
            TextureFilter::Linear => vk::Filter::LINEAR,
824bf02b6e55atagen2026-10-16 09:15 UTC85
            TextureFilter::Nearest => vk::Filter::NEAREST,
824bf02b6e55atagen2026-10-16 09:15 UTC86
        };
824bf02b6e55atagen2026-10-16 09:15 UTC87
        let mut samplers = [vk::Sampler::null(); 4];
824bf02b6e55atagen2026-10-16 09:15 UTC88
        for (i, s) in samplers.iter_mut().enumerate() {
824bf02b6e55atagen2026-10-16 09:15 UTC89
            let (min, mag) = (sampler_filter(i >> 1), sampler_filter(i & 1));
824bf02b6e55atagen2026-10-16 09:15 UTC90
            *s = unsafe {
824bf02b6e55atagen2026-10-16 09:15 UTC91
                d.create_sampler(
824bf02b6e55atagen2026-10-16 09:15 UTC92
                    &vk::SamplerCreateInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC93
                        .min_filter(filter(min))
824bf02b6e55atagen2026-10-16 09:15 UTC94
                        .mag_filter(filter(mag))
824bf02b6e55atagen2026-10-16 09:15 UTC95
                        .address_mode_u(vk::SamplerAddressMode::CLAMP_TO_EDGE)
824bf02b6e55atagen2026-10-16 09:15 UTC96
                        .address_mode_v(vk::SamplerAddressMode::CLAMP_TO_EDGE)
824bf02b6e55atagen2026-10-16 09:15 UTC97
                        .address_mode_w(vk::SamplerAddressMode::CLAMP_TO_EDGE),
824bf02b6e55atagen2026-10-16 09:15 UTC98
                    None,
824bf02b6e55atagen2026-10-16 09:15 UTC99
                )?
824bf02b6e55atagen2026-10-16 09:15 UTC100
            };
824bf02b6e55atagen2026-10-16 09:15 UTC101
        }
824bf02b6e55atagen2026-10-16 09:15 UTC102
        Ok(Self {
824bf02b6e55atagen2026-10-16 09:15 UTC103
            pending: Vec::new(),
7de49c2aa70batagen2026-10-16 09:27 UTC104
            dmabuf_textures: HashMap::new(),
7de49c2aa70batagen2026-10-16 09:27 UTC105
            dmabuf_targets: HashMap::new(),
824bf02b6e55atagen2026-10-16 09:15 UTC106
            pipelines: Pipelines::new(&shared)?,
824bf02b6e55atagen2026-10-16 09:15 UTC107
            pool,
824bf02b6e55atagen2026-10-16 09:15 UTC108
            samplers,
824bf02b6e55atagen2026-10-16 09:15 UTC109
            min_filter: TextureFilter::Linear,
824bf02b6e55atagen2026-10-16 09:15 UTC110
            mag_filter: TextureFilter::Linear,
824bf02b6e55atagen2026-10-16 09:15 UTC111
            debug: DebugFlags::empty(),
824bf02b6e55atagen2026-10-16 09:15 UTC112
            context: ContextId::new(),
7de49c2aa70batagen2026-10-16 09:27 UTC113
            caps: Arc::new(DmabufCaps::query(&shared)),
824bf02b6e55atagen2026-10-16 09:15 UTC114
            shared,
824bf02b6e55atagen2026-10-16 09:15 UTC115
        })
824bf02b6e55atagen2026-10-16 09:15 UTC116
    }
824bf02b6e55atagen2026-10-16 09:15 UTC117
824bf02b6e55atagen2026-10-16 09:15 UTC118
    /// Name of the physical device in use.
824bf02b6e55atagen2026-10-16 09:15 UTC119
    pub fn device_name(&self) -> &str {
824bf02b6e55atagen2026-10-16 09:15 UTC120
        &self.shared.device.name
824bf02b6e55atagen2026-10-16 09:15 UTC121
    }
824bf02b6e55atagen2026-10-16 09:15 UTC122
7de49c2aa70batagen2026-10-16 09:27 UTC123
    /// Whether dmabufs can be imported, rendered into and allocated.
7de49c2aa70batagen2026-10-16 09:27 UTC124
    pub fn supports_dmabuf(&self) -> bool {
7de49c2aa70batagen2026-10-16 09:27 UTC125
        self.caps.render.iter().next().is_some()
7de49c2aa70batagen2026-10-16 09:27 UTC126
    }
7de49c2aa70batagen2026-10-16 09:27 UTC127
7de49c2aa70batagen2026-10-16 09:27 UTC128
    /// Formats and modifiers this renderer can render into as dmabufs.
7de49c2aa70batagen2026-10-16 09:27 UTC129
    pub fn render_formats(&self) -> FormatSet {
7de49c2aa70batagen2026-10-16 09:27 UTC130
        self.caps.render.clone()
7de49c2aa70batagen2026-10-16 09:27 UTC131
    }
7de49c2aa70batagen2026-10-16 09:27 UTC132
7de49c2aa70batagen2026-10-16 09:27 UTC133
    /// An allocator for render-capable dmabufs on this device.
7de49c2aa70batagen2026-10-16 09:27 UTC134
    pub fn allocator(&self) -> VulkanAllocator {
7de49c2aa70batagen2026-10-16 09:27 UTC135
        VulkanAllocator {
7de49c2aa70batagen2026-10-16 09:27 UTC136
            shared: self.shared.clone(),
7de49c2aa70batagen2026-10-16 09:27 UTC137
            caps: self.caps.clone(),
7de49c2aa70batagen2026-10-16 09:27 UTC138
        }
7de49c2aa70batagen2026-10-16 09:27 UTC139
    }
7de49c2aa70batagen2026-10-16 09:27 UTC140
824bf02b6e55atagen2026-10-16 09:15 UTC141
    pub(crate) fn shared(&self) -> &Arc<Shared> {
824bf02b6e55atagen2026-10-16 09:15 UTC142
        &self.shared
824bf02b6e55atagen2026-10-16 09:15 UTC143
    }
824bf02b6e55atagen2026-10-16 09:15 UTC144
824bf02b6e55atagen2026-10-16 09:15 UTC145
    pub(crate) fn context_id_ref(&self) -> &ContextId<VulkanTexture> {
824bf02b6e55atagen2026-10-16 09:15 UTC146
        &self.context
824bf02b6e55atagen2026-10-16 09:15 UTC147
    }
824bf02b6e55atagen2026-10-16 09:15 UTC148
824bf02b6e55atagen2026-10-16 09:15 UTC149
    fn sampler(&self) -> vk::Sampler {
824bf02b6e55atagen2026-10-16 09:15 UTC150
        let idx = |f: TextureFilter| usize::from(f == TextureFilter::Nearest);
824bf02b6e55atagen2026-10-16 09:15 UTC151
        self.samplers[(idx(self.min_filter) << 1) | idx(self.mag_filter)]
824bf02b6e55atagen2026-10-16 09:15 UTC152
    }
824bf02b6e55atagen2026-10-16 09:15 UTC153
824bf02b6e55atagen2026-10-16 09:15 UTC154
    fn reap(&mut self) {
824bf02b6e55atagen2026-10-16 09:15 UTC155
        let d = &self.shared.device.device;
824bf02b6e55atagen2026-10-16 09:15 UTC156
        let pool = self.pool;
824bf02b6e55atagen2026-10-16 09:15 UTC157
        self.pending.retain(|p| {
824bf02b6e55atagen2026-10-16 09:15 UTC158
            if !p.fence.signaled() {
824bf02b6e55atagen2026-10-16 09:15 UTC159
                return true;
824bf02b6e55atagen2026-10-16 09:15 UTC160
            }
824bf02b6e55atagen2026-10-16 09:15 UTC161
            unsafe { d.free_command_buffers(pool, &[p.cmd]) };
824bf02b6e55atagen2026-10-16 09:15 UTC162
            false
824bf02b6e55atagen2026-10-16 09:15 UTC163
        });
7de49c2aa70batagen2026-10-16 09:27 UTC164
        self.dmabuf_textures.retain(|k, _| !k.is_gone());
7de49c2aa70batagen2026-10-16 09:27 UTC165
        self.dmabuf_targets.retain(|k, _| !k.is_gone());
824bf02b6e55atagen2026-10-16 09:15 UTC166
    }
824bf02b6e55atagen2026-10-16 09:15 UTC167
824bf02b6e55atagen2026-10-16 09:15 UTC168
    fn begin(&self) -> Result<vk::CommandBuffer, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC169
        let d = &self.shared.device.device;
824bf02b6e55atagen2026-10-16 09:15 UTC170
        let cmd = unsafe {
824bf02b6e55atagen2026-10-16 09:15 UTC171
            d.allocate_command_buffers(
824bf02b6e55atagen2026-10-16 09:15 UTC172
                &vk::CommandBufferAllocateInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC173
                    .command_pool(self.pool)
824bf02b6e55atagen2026-10-16 09:15 UTC174
                    .level(vk::CommandBufferLevel::PRIMARY)
824bf02b6e55atagen2026-10-16 09:15 UTC175
                    .command_buffer_count(1),
824bf02b6e55atagen2026-10-16 09:15 UTC176
            )?[0]
824bf02b6e55atagen2026-10-16 09:15 UTC177
        };
824bf02b6e55atagen2026-10-16 09:15 UTC178
        unsafe {
824bf02b6e55atagen2026-10-16 09:15 UTC179
            d.begin_command_buffer(
824bf02b6e55atagen2026-10-16 09:15 UTC180
                cmd,
824bf02b6e55atagen2026-10-16 09:15 UTC181
                &vk::CommandBufferBeginInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC182
                    .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT),
824bf02b6e55atagen2026-10-16 09:15 UTC183
            )?
824bf02b6e55atagen2026-10-16 09:15 UTC184
        };
824bf02b6e55atagen2026-10-16 09:15 UTC185
        Ok(cmd)
824bf02b6e55atagen2026-10-16 09:15 UTC186
    }
824bf02b6e55atagen2026-10-16 09:15 UTC187
30aece3a63ceatagen2026-10-16 13:48 UTC188
    #[profiling::function]
824bf02b6e55atagen2026-10-16 09:15 UTC189
    fn end_and_submit(&self, cmd: vk::CommandBuffer) -> Result<Arc<FenceInner>, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC190
        let d = &self.shared.device.device;
824bf02b6e55atagen2026-10-16 09:15 UTC191
        unsafe { d.end_command_buffer(cmd)? };
824bf02b6e55atagen2026-10-16 09:15 UTC192
        let fence = FenceInner::new(&self.shared)?;
824bf02b6e55atagen2026-10-16 09:15 UTC193
        let cmds = [vk::CommandBufferSubmitInfo::default().command_buffer(cmd)];
824bf02b6e55atagen2026-10-16 09:15 UTC194
        let submit = vk::SubmitInfo2::default().command_buffer_infos(&cmds);
824bf02b6e55atagen2026-10-16 09:15 UTC195
        let queue = self
824bf02b6e55atagen2026-10-16 09:15 UTC196
            .shared
824bf02b6e55atagen2026-10-16 09:15 UTC197
            .device
824bf02b6e55atagen2026-10-16 09:15 UTC198
            .queue
824bf02b6e55atagen2026-10-16 09:15 UTC199
            .lock()
824bf02b6e55atagen2026-10-16 09:15 UTC200
            .unwrap_or_else(|e| e.into_inner());
7de49c2aa70batagen2026-10-16 09:27 UTC201
        unsafe { d.queue_submit2(*queue, &[submit], fence.fence())? };
824bf02b6e55atagen2026-10-16 09:15 UTC202
        Ok(fence)
824bf02b6e55atagen2026-10-16 09:15 UTC203
    }
824bf02b6e55atagen2026-10-16 09:15 UTC204
824bf02b6e55atagen2026-10-16 09:15 UTC205
    /// Records, submits and waits for a short command buffer.
824bf02b6e55atagen2026-10-16 09:15 UTC206
    fn one_shot(
824bf02b6e55atagen2026-10-16 09:15 UTC207
        &mut self,
7de49c2aa70batagen2026-10-16 09:27 UTC208
        record: impl FnOnce(&ash::Device, vk::CommandBuffer, u32),
824bf02b6e55atagen2026-10-16 09:15 UTC209
    ) -> Result<(), Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC210
        let cmd = self.begin()?;
7de49c2aa70batagen2026-10-16 09:27 UTC211
        record(
7de49c2aa70batagen2026-10-16 09:27 UTC212
            &self.shared.device.device,
7de49c2aa70batagen2026-10-16 09:27 UTC213
            cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC214
            self.shared.device.queue_family,
7de49c2aa70batagen2026-10-16 09:27 UTC215
        );
824bf02b6e55atagen2026-10-16 09:15 UTC216
        let fence = self.end_and_submit(cmd)?;
824bf02b6e55atagen2026-10-16 09:15 UTC217
        let res = fence.wait();
824bf02b6e55atagen2026-10-16 09:15 UTC218
        unsafe {
824bf02b6e55atagen2026-10-16 09:15 UTC219
            self.shared
824bf02b6e55atagen2026-10-16 09:15 UTC220
                .device
824bf02b6e55atagen2026-10-16 09:15 UTC221
                .device
824bf02b6e55atagen2026-10-16 09:15 UTC222
                .free_command_buffers(self.pool, &[cmd])
824bf02b6e55atagen2026-10-16 09:15 UTC223
        };
824bf02b6e55atagen2026-10-16 09:15 UTC224
        Ok(res?)
824bf02b6e55atagen2026-10-16 09:15 UTC225
    }
824bf02b6e55atagen2026-10-16 09:15 UTC226
30aece3a63ceatagen2026-10-16 13:48 UTC227
    #[profiling::function]
824bf02b6e55atagen2026-10-16 09:15 UTC228
    pub(crate) fn submit(
824bf02b6e55atagen2026-10-16 09:15 UTC229
        &mut self,
824bf02b6e55atagen2026-10-16 09:15 UTC230
        target: &mut VulkanTarget<'_>,
824bf02b6e55atagen2026-10-16 09:15 UTC231
        ops: Vec<Op>,
824bf02b6e55atagen2026-10-16 09:15 UTC232
    ) -> Result<SyncPoint, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC233
        self.reap();
7de49c2aa70batagen2026-10-16 09:27 UTC234
        let image = target.0.clone();
7de49c2aa70batagen2026-10-16 09:27 UTC235
        let mut sampled: Vec<Arc<ImageInner>> = Vec::new();
824bf02b6e55atagen2026-10-16 09:15 UTC236
        for op in &ops {
824bf02b6e55atagen2026-10-16 09:15 UTC237
            if let Op::Draw {
824bf02b6e55atagen2026-10-16 09:15 UTC238
                texture: Some(t), ..
824bf02b6e55atagen2026-10-16 09:15 UTC239
            } = op
7de49c2aa70batagen2026-10-16 09:27 UTC240
                && !sampled.iter().any(|s| Arc::ptr_eq(s, &t.0))
824bf02b6e55atagen2026-10-16 09:15 UTC241
            {
7de49c2aa70batagen2026-10-16 09:27 UTC242
                sampled.push(t.0.clone());
824bf02b6e55atagen2026-10-16 09:15 UTC243
            }
824bf02b6e55atagen2026-10-16 09:15 UTC244
        }
824bf02b6e55atagen2026-10-16 09:15 UTC245
        let mut pipelines = Vec::with_capacity(ops.len());
824bf02b6e55atagen2026-10-16 09:15 UTC246
        for op in &ops {
824bf02b6e55atagen2026-10-16 09:15 UTC247
            pipelines.push(match op {
824bf02b6e55atagen2026-10-16 09:15 UTC248
                Op::Draw { kind, .. } => Some(self.pipelines.get(image.format, *kind)?),
824bf02b6e55atagen2026-10-16 09:15 UTC249
                Op::Clear { .. } => None,
824bf02b6e55atagen2026-10-16 09:15 UTC250
            });
824bf02b6e55atagen2026-10-16 09:15 UTC251
        }
824bf02b6e55atagen2026-10-16 09:15 UTC252
        let cmd = self.begin()?;
824bf02b6e55atagen2026-10-16 09:15 UTC253
        let d = &self.shared.device.device;
7de49c2aa70batagen2026-10-16 09:27 UTC254
        let family = self.shared.device.queue_family;
824bf02b6e55atagen2026-10-16 09:15 UTC255
        let push = &self.shared.device.push_descriptor;
824bf02b6e55atagen2026-10-16 09:15 UTC256
        let (w, h) = (image.width, image.height);
824bf02b6e55atagen2026-10-16 09:15 UTC257
        let sampler = self.sampler();
824bf02b6e55atagen2026-10-16 09:15 UTC258
        unsafe {
7de49c2aa70batagen2026-10-16 09:27 UTC259
            for t in &sampled {
7de49c2aa70batagen2026-10-16 09:27 UTC260
                acquire(d, cmd, family, t, vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL);
824bf02b6e55atagen2026-10-16 09:15 UTC261
            }
7de49c2aa70batagen2026-10-16 09:27 UTC262
            acquire(
824bf02b6e55atagen2026-10-16 09:15 UTC263
                d,
824bf02b6e55atagen2026-10-16 09:15 UTC264
                cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC265
                family,
7de49c2aa70batagen2026-10-16 09:27 UTC266
                &image,
824bf02b6e55atagen2026-10-16 09:15 UTC267
                vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,
824bf02b6e55atagen2026-10-16 09:15 UTC268
            );
824bf02b6e55atagen2026-10-16 09:15 UTC269
            let attachments = [vk::RenderingAttachmentInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC270
                .image_view(image.view)
824bf02b6e55atagen2026-10-16 09:15 UTC271
                .image_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL)
824bf02b6e55atagen2026-10-16 09:15 UTC272
                .load_op(vk::AttachmentLoadOp::LOAD)
824bf02b6e55atagen2026-10-16 09:15 UTC273
                .store_op(vk::AttachmentStoreOp::STORE)];
824bf02b6e55atagen2026-10-16 09:15 UTC274
            let area = vk::Rect2D {
824bf02b6e55atagen2026-10-16 09:15 UTC275
                offset: vk::Offset2D::default(),
824bf02b6e55atagen2026-10-16 09:15 UTC276
                extent: vk::Extent2D {
824bf02b6e55atagen2026-10-16 09:15 UTC277
                    width: w,
824bf02b6e55atagen2026-10-16 09:15 UTC278
                    height: h,
824bf02b6e55atagen2026-10-16 09:15 UTC279
                },
824bf02b6e55atagen2026-10-16 09:15 UTC280
            };
824bf02b6e55atagen2026-10-16 09:15 UTC281
            d.cmd_begin_rendering(
824bf02b6e55atagen2026-10-16 09:15 UTC282
                cmd,
824bf02b6e55atagen2026-10-16 09:15 UTC283
                &vk::RenderingInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC284
                    .render_area(area)
824bf02b6e55atagen2026-10-16 09:15 UTC285
                    .layer_count(1)
824bf02b6e55atagen2026-10-16 09:15 UTC286
                    .color_attachments(&attachments),
824bf02b6e55atagen2026-10-16 09:15 UTC287
            );
824bf02b6e55atagen2026-10-16 09:15 UTC288
            d.cmd_set_viewport(
824bf02b6e55atagen2026-10-16 09:15 UTC289
                cmd,
824bf02b6e55atagen2026-10-16 09:15 UTC290
                0,
824bf02b6e55atagen2026-10-16 09:15 UTC291
                &[vk::Viewport {
824bf02b6e55atagen2026-10-16 09:15 UTC292
                    x: 0.0,
824bf02b6e55atagen2026-10-16 09:15 UTC293
                    y: 0.0,
824bf02b6e55atagen2026-10-16 09:15 UTC294
                    width: w as f32,
824bf02b6e55atagen2026-10-16 09:15 UTC295
                    height: h as f32,
824bf02b6e55atagen2026-10-16 09:15 UTC296
                    min_depth: 0.0,
824bf02b6e55atagen2026-10-16 09:15 UTC297
                    max_depth: 1.0,
824bf02b6e55atagen2026-10-16 09:15 UTC298
                }],
824bf02b6e55atagen2026-10-16 09:15 UTC299
            );
824bf02b6e55atagen2026-10-16 09:15 UTC300
            let mut bound = vk::Pipeline::null();
824bf02b6e55atagen2026-10-16 09:15 UTC301
            for (op, pipeline) in ops.iter().zip(pipelines) {
824bf02b6e55atagen2026-10-16 09:15 UTC302
                match op {
824bf02b6e55atagen2026-10-16 09:15 UTC303
                    Op::Clear { color, rects } => {
824bf02b6e55atagen2026-10-16 09:15 UTC304
                        let clear = [vk::ClearAttachment {
824bf02b6e55atagen2026-10-16 09:15 UTC305
                            aspect_mask: vk::ImageAspectFlags::COLOR,
824bf02b6e55atagen2026-10-16 09:15 UTC306
                            color_attachment: 0,
824bf02b6e55atagen2026-10-16 09:15 UTC307
                            clear_value: vk::ClearValue {
824bf02b6e55atagen2026-10-16 09:15 UTC308
                                color: vk::ClearColorValue { float32: *color },
824bf02b6e55atagen2026-10-16 09:15 UTC309
                            },
824bf02b6e55atagen2026-10-16 09:15 UTC310
                        }];
824bf02b6e55atagen2026-10-16 09:15 UTC311
                        let rects: Vec<_> = rects
824bf02b6e55atagen2026-10-16 09:15 UTC312
                            .iter()
824bf02b6e55atagen2026-10-16 09:15 UTC313
                            .map(|&rect| vk::ClearRect {
824bf02b6e55atagen2026-10-16 09:15 UTC314
                                rect,
824bf02b6e55atagen2026-10-16 09:15 UTC315
                                base_array_layer: 0,
824bf02b6e55atagen2026-10-16 09:15 UTC316
                                layer_count: 1,
824bf02b6e55atagen2026-10-16 09:15 UTC317
                            })
824bf02b6e55atagen2026-10-16 09:15 UTC318
                            .collect();
824bf02b6e55atagen2026-10-16 09:15 UTC319
                        d.cmd_clear_attachments(cmd, &clear, &rects);
824bf02b6e55atagen2026-10-16 09:15 UTC320
                    }
824bf02b6e55atagen2026-10-16 09:15 UTC321
                    Op::Draw {
824bf02b6e55atagen2026-10-16 09:15 UTC322
                        texture,
824bf02b6e55atagen2026-10-16 09:15 UTC323
                        push: constants,
824bf02b6e55atagen2026-10-16 09:15 UTC324
                        scissors,
824bf02b6e55atagen2026-10-16 09:15 UTC325
                        ..
824bf02b6e55atagen2026-10-16 09:15 UTC326
                    } => {
824bf02b6e55atagen2026-10-16 09:15 UTC327
                        let pipeline = pipeline.unwrap_or_default();
824bf02b6e55atagen2026-10-16 09:15 UTC328
                        if pipeline != bound {
824bf02b6e55atagen2026-10-16 09:15 UTC329
                            d.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipeline);
824bf02b6e55atagen2026-10-16 09:15 UTC330
                            bound = pipeline;
824bf02b6e55atagen2026-10-16 09:15 UTC331
                        }
824bf02b6e55atagen2026-10-16 09:15 UTC332
                        if let Some(t) = texture {
824bf02b6e55atagen2026-10-16 09:15 UTC333
                            let info = [vk::DescriptorImageInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC334
                                .image_view(t.0.view)
824bf02b6e55atagen2026-10-16 09:15 UTC335
                                .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)
824bf02b6e55atagen2026-10-16 09:15 UTC336
                                .sampler(sampler)];
824bf02b6e55atagen2026-10-16 09:15 UTC337
                            let write = vk::WriteDescriptorSet::default()
824bf02b6e55atagen2026-10-16 09:15 UTC338
                                .dst_binding(0)
824bf02b6e55atagen2026-10-16 09:15 UTC339
                                .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER)
824bf02b6e55atagen2026-10-16 09:15 UTC340
                                .image_info(&info);
824bf02b6e55atagen2026-10-16 09:15 UTC341
                            push.cmd_push_descriptor_set(
824bf02b6e55atagen2026-10-16 09:15 UTC342
                                cmd,
824bf02b6e55atagen2026-10-16 09:15 UTC343
                                vk::PipelineBindPoint::GRAPHICS,
824bf02b6e55atagen2026-10-16 09:15 UTC344
                                self.pipelines.layout,
824bf02b6e55atagen2026-10-16 09:15 UTC345
                                0,
824bf02b6e55atagen2026-10-16 09:15 UTC346
                                &[write],
824bf02b6e55atagen2026-10-16 09:15 UTC347
                            );
824bf02b6e55atagen2026-10-16 09:15 UTC348
                        }
824bf02b6e55atagen2026-10-16 09:15 UTC349
                        d.cmd_push_constants(
824bf02b6e55atagen2026-10-16 09:15 UTC350
                            cmd,
824bf02b6e55atagen2026-10-16 09:15 UTC351
                            self.pipelines.layout,
824bf02b6e55atagen2026-10-16 09:15 UTC352
                            vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT,
824bf02b6e55atagen2026-10-16 09:15 UTC353
                            0,
824bf02b6e55atagen2026-10-16 09:15 UTC354
                            constants.bytes(),
824bf02b6e55atagen2026-10-16 09:15 UTC355
                        );
824bf02b6e55atagen2026-10-16 09:15 UTC356
                        for s in scissors {
824bf02b6e55atagen2026-10-16 09:15 UTC357
                            d.cmd_set_scissor(cmd, 0, &[*s]);
824bf02b6e55atagen2026-10-16 09:15 UTC358
                            d.cmd_draw(cmd, 4, 1, 0, 0);
824bf02b6e55atagen2026-10-16 09:15 UTC359
                        }
824bf02b6e55atagen2026-10-16 09:15 UTC360
                    }
824bf02b6e55atagen2026-10-16 09:15 UTC361
                }
824bf02b6e55atagen2026-10-16 09:15 UTC362
            }
824bf02b6e55atagen2026-10-16 09:15 UTC363
            d.cmd_end_rendering(cmd);
7de49c2aa70batagen2026-10-16 09:27 UTC364
            release(
7de49c2aa70batagen2026-10-16 09:27 UTC365
                d,
7de49c2aa70batagen2026-10-16 09:27 UTC366
                cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC367
                family,
7de49c2aa70batagen2026-10-16 09:27 UTC368
                &image,
7de49c2aa70batagen2026-10-16 09:27 UTC369
                vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,
7de49c2aa70batagen2026-10-16 09:27 UTC370
            );
7de49c2aa70batagen2026-10-16 09:27 UTC371
            for t in &sampled {
7de49c2aa70batagen2026-10-16 09:27 UTC372
                release(d, cmd, family, t, vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL);
7de49c2aa70batagen2026-10-16 09:27 UTC373
            }
824bf02b6e55atagen2026-10-16 09:15 UTC374
        }
824bf02b6e55atagen2026-10-16 09:15 UTC375
        let fence = self.end_and_submit(cmd)?;
7de49c2aa70batagen2026-10-16 09:27 UTC376
        sampled.push(image);
824bf02b6e55atagen2026-10-16 09:15 UTC377
        self.pending.push(Pending {
824bf02b6e55atagen2026-10-16 09:15 UTC378
            fence: fence.clone(),
824bf02b6e55atagen2026-10-16 09:15 UTC379
            cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC380
            _keep: sampled,
48af562d968batagen2026-10-16 12:39 UTC381
            _staging: None,
824bf02b6e55atagen2026-10-16 09:15 UTC382
        });
824bf02b6e55atagen2026-10-16 09:15 UTC383
        Ok(SyncPoint::from(VkFence(fence)))
824bf02b6e55atagen2026-10-16 09:15 UTC384
    }
824bf02b6e55atagen2026-10-16 09:15 UTC385
48af562d968batagen2026-10-16 12:39 UTC386
    /// Copies `region` of a full-image `data` into `image`, rows top-down or
48af562d968batagen2026-10-16 12:39 UTC387
    /// flipped.
824bf02b6e55atagen2026-10-16 09:15 UTC388
    fn upload(
824bf02b6e55atagen2026-10-16 09:15 UTC389
        &mut self,
824bf02b6e55atagen2026-10-16 09:15 UTC390
        image: &Arc<ImageInner>,
824bf02b6e55atagen2026-10-16 09:15 UTC391
        data: &[u8],
824bf02b6e55atagen2026-10-16 09:15 UTC392
        region: Rectangle<i32, Buffer>,
824bf02b6e55atagen2026-10-16 09:15 UTC393
        flipped: bool,
824bf02b6e55atagen2026-10-16 09:15 UTC394
    ) -> Result<(), Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC395
        let width = image.width as usize;
48af562d968batagen2026-10-16 12:39 UTC396
        let rows = image.height as usize;
48af562d968batagen2026-10-16 12:39 UTC397
        let needed = width * rows * BYTES_PER_PIXEL;
824bf02b6e55atagen2026-10-16 09:15 UTC398
        if data.len() < needed {
824bf02b6e55atagen2026-10-16 09:15 UTC399
            return Err(Error::BufferTooSmall {
824bf02b6e55atagen2026-10-16 09:15 UTC400
                needed,
824bf02b6e55atagen2026-10-16 09:15 UTC401
                got: data.len(),
824bf02b6e55atagen2026-10-16 09:15 UTC402
            });
824bf02b6e55atagen2026-10-16 09:15 UTC403
        }
48af562d968batagen2026-10-16 12:39 UTC404
        let stride = width * BYTES_PER_PIXEL;
48af562d968batagen2026-10-16 12:39 UTC405
        self.upload_with(image, region, |y| {
48af562d968batagen2026-10-16 12:39 UTC406
            let from = if flipped { rows - 1 - y } else { y };
48af562d968batagen2026-10-16 12:39 UTC407
            let start = from * stride + region.loc.x as usize * BYTES_PER_PIXEL;
48af562d968batagen2026-10-16 12:39 UTC408
            &data[start..start + region.size.w as usize * BYTES_PER_PIXEL]
48af562d968batagen2026-10-16 12:39 UTC409
        })
48af562d968batagen2026-10-16 12:39 UTC410
    }
48af562d968batagen2026-10-16 12:39 UTC411
48af562d968batagen2026-10-16 12:39 UTC412
    /// Uploads `region` of `image`, taking each image row `y` in the region
48af562d968batagen2026-10-16 12:39 UTC413
    /// from `row(y)`. The CPU does not wait: the copy is ordered before later
48af562d968batagen2026-10-16 12:39 UTC414
    /// frames by its barrier, and its staging memory lives until its fence.
30aece3a63ceatagen2026-10-16 13:48 UTC415
    #[profiling::function]
48af562d968batagen2026-10-16 12:39 UTC416
    fn upload_with<'a>(
48af562d968batagen2026-10-16 12:39 UTC417
        &mut self,
48af562d968batagen2026-10-16 12:39 UTC418
        image: &Arc<ImageInner>,
48af562d968batagen2026-10-16 12:39 UTC419
        region: Rectangle<i32, Buffer>,
48af562d968batagen2026-10-16 12:39 UTC420
        row: impl Fn(usize) -> &'a [u8],
48af562d968batagen2026-10-16 12:39 UTC421
    ) -> Result<(), Error> {
48af562d968batagen2026-10-16 12:39 UTC422
        if image.external {
48af562d968batagen2026-10-16 12:39 UTC423
            return Err(Error::ForeignTexture);
48af562d968batagen2026-10-16 12:39 UTC424
        }
48af562d968batagen2026-10-16 12:39 UTC425
        self.reap();
48af562d968batagen2026-10-16 12:39 UTC426
        let (w, h) = (region.size.w as usize, region.size.h as usize);
48af562d968batagen2026-10-16 12:39 UTC427
        if w == 0 || h == 0 {
48af562d968batagen2026-10-16 12:39 UTC428
            return Ok(());
48af562d968batagen2026-10-16 12:39 UTC429
        }
48af562d968batagen2026-10-16 12:39 UTC430
        let line = w * BYTES_PER_PIXEL;
824bf02b6e55atagen2026-10-16 09:15 UTC431
        let mut staging = Staging::new(
824bf02b6e55atagen2026-10-16 09:15 UTC432
            &self.shared,
48af562d968batagen2026-10-16 12:39 UTC433
            line * h,
824bf02b6e55atagen2026-10-16 09:15 UTC434
            vk::BufferUsageFlags::TRANSFER_SRC,
824bf02b6e55atagen2026-10-16 09:15 UTC435
            MemoryLocation::CpuToGpu,
824bf02b6e55atagen2026-10-16 09:15 UTC436
        )?;
824bf02b6e55atagen2026-10-16 09:15 UTC437
        let dst = staging.bytes_mut();
48af562d968batagen2026-10-16 12:39 UTC438
        for i in 0..h {
48af562d968batagen2026-10-16 12:39 UTC439
            dst[i * line..(i + 1) * line].copy_from_slice(row(region.loc.y as usize + i));
824bf02b6e55atagen2026-10-16 09:15 UTC440
        }
824bf02b6e55atagen2026-10-16 09:15 UTC441
        let copy = vk::BufferImageCopy::default()
48af562d968batagen2026-10-16 12:39 UTC442
            .buffer_row_length(w as u32)
824bf02b6e55atagen2026-10-16 09:15 UTC443
            .image_subresource(
824bf02b6e55atagen2026-10-16 09:15 UTC444
                vk::ImageSubresourceLayers::default()
824bf02b6e55atagen2026-10-16 09:15 UTC445
                    .aspect_mask(vk::ImageAspectFlags::COLOR)
824bf02b6e55atagen2026-10-16 09:15 UTC446
                    .layer_count(1),
824bf02b6e55atagen2026-10-16 09:15 UTC447
            )
824bf02b6e55atagen2026-10-16 09:15 UTC448
            .image_offset(vk::Offset3D {
824bf02b6e55atagen2026-10-16 09:15 UTC449
                x: region.loc.x,
824bf02b6e55atagen2026-10-16 09:15 UTC450
                y: region.loc.y,
824bf02b6e55atagen2026-10-16 09:15 UTC451
                z: 0,
824bf02b6e55atagen2026-10-16 09:15 UTC452
            })
824bf02b6e55atagen2026-10-16 09:15 UTC453
            .image_extent(vk::Extent3D {
48af562d968batagen2026-10-16 12:39 UTC454
                width: w as u32,
48af562d968batagen2026-10-16 12:39 UTC455
                height: h as u32,
824bf02b6e55atagen2026-10-16 09:15 UTC456
                depth: 1,
824bf02b6e55atagen2026-10-16 09:15 UTC457
            });
48af562d968batagen2026-10-16 12:39 UTC458
        let d = &self.shared.device.device;
48af562d968batagen2026-10-16 12:39 UTC459
        let family = self.shared.device.queue_family;
48af562d968batagen2026-10-16 12:39 UTC460
        let cmd = self.begin()?;
48af562d968batagen2026-10-16 12:39 UTC461
        unsafe {
48af562d968batagen2026-10-16 12:39 UTC462
            acquire(d, cmd, family, image, vk::ImageLayout::TRANSFER_DST_OPTIMAL);
824bf02b6e55atagen2026-10-16 09:15 UTC463
            d.cmd_copy_buffer_to_image(
824bf02b6e55atagen2026-10-16 09:15 UTC464
                cmd,
48af562d968batagen2026-10-16 12:39 UTC465
                staging.buffer,
48af562d968batagen2026-10-16 12:39 UTC466
                image.image,
824bf02b6e55atagen2026-10-16 09:15 UTC467
                vk::ImageLayout::TRANSFER_DST_OPTIMAL,
824bf02b6e55atagen2026-10-16 09:15 UTC468
                &[copy],
824bf02b6e55atagen2026-10-16 09:15 UTC469
            );
7de49c2aa70batagen2026-10-16 09:27 UTC470
            acquire(
824bf02b6e55atagen2026-10-16 09:15 UTC471
                d,
824bf02b6e55atagen2026-10-16 09:15 UTC472
                cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC473
                family,
48af562d968batagen2026-10-16 12:39 UTC474
                image,
824bf02b6e55atagen2026-10-16 09:15 UTC475
                vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,
824bf02b6e55atagen2026-10-16 09:15 UTC476
            );
48af562d968batagen2026-10-16 12:39 UTC477
        }
48af562d968batagen2026-10-16 12:39 UTC478
        let fence = self.end_and_submit(cmd)?;
48af562d968batagen2026-10-16 12:39 UTC479
        self.pending.push(Pending {
48af562d968batagen2026-10-16 12:39 UTC480
            fence,
48af562d968batagen2026-10-16 12:39 UTC481
            cmd,
48af562d968batagen2026-10-16 12:39 UTC482
            _keep: vec![image.clone()],
48af562d968batagen2026-10-16 12:39 UTC483
            _staging: Some(staging),
48af562d968batagen2026-10-16 12:39 UTC484
        });
48af562d968batagen2026-10-16 12:39 UTC485
        Ok(())
824bf02b6e55atagen2026-10-16 09:15 UTC486
    }
824bf02b6e55atagen2026-10-16 09:15 UTC487
824bf02b6e55atagen2026-10-16 09:15 UTC488
    fn read_back(
824bf02b6e55atagen2026-10-16 09:15 UTC489
        &mut self,
824bf02b6e55atagen2026-10-16 09:15 UTC490
        image: &Arc<ImageInner>,
824bf02b6e55atagen2026-10-16 09:15 UTC491
        region: Rectangle<i32, Buffer>,
824bf02b6e55atagen2026-10-16 09:15 UTC492
        fourcc: Fourcc,
824bf02b6e55atagen2026-10-16 09:15 UTC493
    ) -> Result<VulkanMapping, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC494
        let region = region
824bf02b6e55atagen2026-10-16 09:15 UTC495
            .intersection(Rectangle::from_size(
824bf02b6e55atagen2026-10-16 09:15 UTC496
                (image.width as i32, image.height as i32).into(),
824bf02b6e55atagen2026-10-16 09:15 UTC497
            ))
824bf02b6e55atagen2026-10-16 09:15 UTC498
            .unwrap_or_default();
824bf02b6e55atagen2026-10-16 09:15 UTC499
        let (w, h) = (region.size.w.max(0) as u32, region.size.h.max(0) as u32);
824bf02b6e55atagen2026-10-16 09:15 UTC500
        let swap = needs_swizzle(image.fourcc, fourcc)?;
824bf02b6e55atagen2026-10-16 09:15 UTC501
        let len = (w as usize * h as usize * BYTES_PER_PIXEL).max(BYTES_PER_PIXEL);
824bf02b6e55atagen2026-10-16 09:15 UTC502
        let staging = Staging::new(
824bf02b6e55atagen2026-10-16 09:15 UTC503
            &self.shared,
824bf02b6e55atagen2026-10-16 09:15 UTC504
            len,
824bf02b6e55atagen2026-10-16 09:15 UTC505
            vk::BufferUsageFlags::TRANSFER_DST,
824bf02b6e55atagen2026-10-16 09:15 UTC506
            MemoryLocation::GpuToCpu,
824bf02b6e55atagen2026-10-16 09:15 UTC507
        )?;
824bf02b6e55atagen2026-10-16 09:15 UTC508
        if w > 0 && h > 0 {
824bf02b6e55atagen2026-10-16 09:15 UTC509
            let copy = vk::BufferImageCopy::default()
824bf02b6e55atagen2026-10-16 09:15 UTC510
                .image_subresource(
824bf02b6e55atagen2026-10-16 09:15 UTC511
                    vk::ImageSubresourceLayers::default()
824bf02b6e55atagen2026-10-16 09:15 UTC512
                        .aspect_mask(vk::ImageAspectFlags::COLOR)
824bf02b6e55atagen2026-10-16 09:15 UTC513
                        .layer_count(1),
824bf02b6e55atagen2026-10-16 09:15 UTC514
                )
824bf02b6e55atagen2026-10-16 09:15 UTC515
                .image_offset(vk::Offset3D {
824bf02b6e55atagen2026-10-16 09:15 UTC516
                    x: region.loc.x,
824bf02b6e55atagen2026-10-16 09:15 UTC517
                    y: region.loc.y,
824bf02b6e55atagen2026-10-16 09:15 UTC518
                    z: 0,
824bf02b6e55atagen2026-10-16 09:15 UTC519
                })
824bf02b6e55atagen2026-10-16 09:15 UTC520
                .image_extent(vk::Extent3D {
824bf02b6e55atagen2026-10-16 09:15 UTC521
                    width: w,
824bf02b6e55atagen2026-10-16 09:15 UTC522
                    height: h,
824bf02b6e55atagen2026-10-16 09:15 UTC523
                    depth: 1,
824bf02b6e55atagen2026-10-16 09:15 UTC524
                });
7de49c2aa70batagen2026-10-16 09:27 UTC525
            let buffer = staging.buffer;
7de49c2aa70batagen2026-10-16 09:27 UTC526
            let source = image.clone();
7de49c2aa70batagen2026-10-16 09:27 UTC527
            self.one_shot(|d, cmd, family| unsafe {
7de49c2aa70batagen2026-10-16 09:27 UTC528
                acquire(
7de49c2aa70batagen2026-10-16 09:27 UTC529
                    d,
7de49c2aa70batagen2026-10-16 09:27 UTC530
                    cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC531
                    family,
7de49c2aa70batagen2026-10-16 09:27 UTC532
                    &source,
7de49c2aa70batagen2026-10-16 09:27 UTC533
                    vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
7de49c2aa70batagen2026-10-16 09:27 UTC534
                );
824bf02b6e55atagen2026-10-16 09:15 UTC535
                d.cmd_copy_image_to_buffer(
824bf02b6e55atagen2026-10-16 09:15 UTC536
                    cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC537
                    source.image,
824bf02b6e55atagen2026-10-16 09:15 UTC538
                    vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
824bf02b6e55atagen2026-10-16 09:15 UTC539
                    buffer,
824bf02b6e55atagen2026-10-16 09:15 UTC540
                    &[copy],
824bf02b6e55atagen2026-10-16 09:15 UTC541
                );
7de49c2aa70batagen2026-10-16 09:27 UTC542
                release(
7de49c2aa70batagen2026-10-16 09:27 UTC543
                    d,
7de49c2aa70batagen2026-10-16 09:27 UTC544
                    cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC545
                    family,
7de49c2aa70batagen2026-10-16 09:27 UTC546
                    &source,
7de49c2aa70batagen2026-10-16 09:27 UTC547
                    vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
7de49c2aa70batagen2026-10-16 09:27 UTC548
                );
824bf02b6e55atagen2026-10-16 09:15 UTC549
            })?;
824bf02b6e55atagen2026-10-16 09:15 UTC550
        }
824bf02b6e55atagen2026-10-16 09:15 UTC551
        let mut data = staging.bytes()[..w as usize * h as usize * BYTES_PER_PIXEL].to_vec();
824bf02b6e55atagen2026-10-16 09:15 UTC552
        if swap {
824bf02b6e55atagen2026-10-16 09:15 UTC553
            for px in data.as_chunks_mut::<BYTES_PER_PIXEL>().0 {
824bf02b6e55atagen2026-10-16 09:15 UTC554
                px.swap(0, 2);
824bf02b6e55atagen2026-10-16 09:15 UTC555
            }
824bf02b6e55atagen2026-10-16 09:15 UTC556
        }
824bf02b6e55atagen2026-10-16 09:15 UTC557
        Ok(VulkanMapping {
824bf02b6e55atagen2026-10-16 09:15 UTC558
            data,
824bf02b6e55atagen2026-10-16 09:15 UTC559
            width: w,
824bf02b6e55atagen2026-10-16 09:15 UTC560
            height: h,
824bf02b6e55atagen2026-10-16 09:15 UTC561
            fourcc,
824bf02b6e55atagen2026-10-16 09:15 UTC562
        })
824bf02b6e55atagen2026-10-16 09:15 UTC563
    }
824bf02b6e55atagen2026-10-16 09:15 UTC564
824bf02b6e55atagen2026-10-16 09:15 UTC565
    fn own(&self, image: &Arc<ImageInner>) -> Result<(), Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC566
        if Arc::ptr_eq(&image.shared, &self.shared) {
824bf02b6e55atagen2026-10-16 09:15 UTC567
            Ok(())
824bf02b6e55atagen2026-10-16 09:15 UTC568
        } else {
824bf02b6e55atagen2026-10-16 09:15 UTC569
            Err(Error::ForeignTexture)
824bf02b6e55atagen2026-10-16 09:15 UTC570
        }
824bf02b6e55atagen2026-10-16 09:15 UTC571
    }
824bf02b6e55atagen2026-10-16 09:15 UTC572
}
824bf02b6e55atagen2026-10-16 09:15 UTC573
824bf02b6e55atagen2026-10-16 09:15 UTC574
impl Drop for VulkanRenderer {
824bf02b6e55atagen2026-10-16 09:15 UTC575
    fn drop(&mut self) {
824bf02b6e55atagen2026-10-16 09:15 UTC576
        let d = &self.shared.device.device;
824bf02b6e55atagen2026-10-16 09:15 UTC577
        unsafe {
824bf02b6e55atagen2026-10-16 09:15 UTC578
            let _ = d.device_wait_idle();
824bf02b6e55atagen2026-10-16 09:15 UTC579
            for p in self.pending.drain(..) {
824bf02b6e55atagen2026-10-16 09:15 UTC580
                d.free_command_buffers(self.pool, &[p.cmd]);
824bf02b6e55atagen2026-10-16 09:15 UTC581
            }
824bf02b6e55atagen2026-10-16 09:15 UTC582
            for s in self.samplers {
824bf02b6e55atagen2026-10-16 09:15 UTC583
                d.destroy_sampler(s, None);
824bf02b6e55atagen2026-10-16 09:15 UTC584
            }
824bf02b6e55atagen2026-10-16 09:15 UTC585
            d.destroy_command_pool(self.pool, None);
824bf02b6e55atagen2026-10-16 09:15 UTC586
        }
824bf02b6e55atagen2026-10-16 09:15 UTC587
    }
824bf02b6e55atagen2026-10-16 09:15 UTC588
}
824bf02b6e55atagen2026-10-16 09:15 UTC589
824bf02b6e55atagen2026-10-16 09:15 UTC590
fn sampler_filter(bit: usize) -> TextureFilter {
824bf02b6e55atagen2026-10-16 09:15 UTC591
    if bit == 0 {
824bf02b6e55atagen2026-10-16 09:15 UTC592
        TextureFilter::Linear
824bf02b6e55atagen2026-10-16 09:15 UTC593
    } else {
824bf02b6e55atagen2026-10-16 09:15 UTC594
        TextureFilter::Nearest
824bf02b6e55atagen2026-10-16 09:15 UTC595
    }
824bf02b6e55atagen2026-10-16 09:15 UTC596
}
824bf02b6e55atagen2026-10-16 09:15 UTC597
7de49c2aa70batagen2026-10-16 09:27 UTC598
/// A full barrier between all earlier and later work on the image, optionally
7de49c2aa70batagen2026-10-16 09:27 UTC599
/// transferring ownership between queue families. Coarse, but correct for
7de49c2aa70batagen2026-10-16 09:27 UTC600
/// every transition the renderer makes.
824bf02b6e55atagen2026-10-16 09:15 UTC601
unsafe fn barrier(
824bf02b6e55atagen2026-10-16 09:15 UTC602
    d: &ash::Device,
824bf02b6e55atagen2026-10-16 09:15 UTC603
    cmd: vk::CommandBuffer,
824bf02b6e55atagen2026-10-16 09:15 UTC604
    image: vk::Image,
824bf02b6e55atagen2026-10-16 09:15 UTC605
    old: vk::ImageLayout,
824bf02b6e55atagen2026-10-16 09:15 UTC606
    new: vk::ImageLayout,
7de49c2aa70batagen2026-10-16 09:27 UTC607
    src_family: u32,
7de49c2aa70batagen2026-10-16 09:27 UTC608
    dst_family: u32,
824bf02b6e55atagen2026-10-16 09:15 UTC609
) {
824bf02b6e55atagen2026-10-16 09:15 UTC610
    let b = [vk::ImageMemoryBarrier2::default()
824bf02b6e55atagen2026-10-16 09:15 UTC611
        .src_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS)
824bf02b6e55atagen2026-10-16 09:15 UTC612
        .src_access_mask(vk::AccessFlags2::MEMORY_WRITE)
824bf02b6e55atagen2026-10-16 09:15 UTC613
        .dst_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS)
824bf02b6e55atagen2026-10-16 09:15 UTC614
        .dst_access_mask(vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE)
824bf02b6e55atagen2026-10-16 09:15 UTC615
        .old_layout(old)
824bf02b6e55atagen2026-10-16 09:15 UTC616
        .new_layout(new)
7de49c2aa70batagen2026-10-16 09:27 UTC617
        .src_queue_family_index(src_family)
7de49c2aa70batagen2026-10-16 09:27 UTC618
        .dst_queue_family_index(dst_family)
824bf02b6e55atagen2026-10-16 09:15 UTC619
        .image(image)
824bf02b6e55atagen2026-10-16 09:15 UTC620
        .subresource_range(color_range())];
824bf02b6e55atagen2026-10-16 09:15 UTC621
    unsafe {
824bf02b6e55atagen2026-10-16 09:15 UTC622
        d.cmd_pipeline_barrier2(
824bf02b6e55atagen2026-10-16 09:15 UTC623
            cmd,
824bf02b6e55atagen2026-10-16 09:15 UTC624
            &vk::DependencyInfo::default().image_memory_barriers(&b),
824bf02b6e55atagen2026-10-16 09:15 UTC625
        )
824bf02b6e55atagen2026-10-16 09:15 UTC626
    };
824bf02b6e55atagen2026-10-16 09:15 UTC627
}
824bf02b6e55atagen2026-10-16 09:15 UTC628
7de49c2aa70batagen2026-10-16 09:27 UTC629
/// Moves an image into `layout` for use on this queue. Dmabuf images come
7de49c2aa70batagen2026-10-16 09:27 UTC630
/// from the foreign queue family in the general layout, others continue from
7de49c2aa70batagen2026-10-16 09:27 UTC631
/// their tracked layout.
7de49c2aa70batagen2026-10-16 09:27 UTC632
unsafe fn acquire(
7de49c2aa70batagen2026-10-16 09:27 UTC633
    d: &ash::Device,
7de49c2aa70batagen2026-10-16 09:27 UTC634
    cmd: vk::CommandBuffer,
7de49c2aa70batagen2026-10-16 09:27 UTC635
    family: u32,
7de49c2aa70batagen2026-10-16 09:27 UTC636
    image: &ImageInner,
7de49c2aa70batagen2026-10-16 09:27 UTC637
    layout: vk::ImageLayout,
7de49c2aa70batagen2026-10-16 09:27 UTC638
) {
7de49c2aa70batagen2026-10-16 09:27 UTC639
    if image.external {
7de49c2aa70batagen2026-10-16 09:27 UTC640
        unsafe {
7de49c2aa70batagen2026-10-16 09:27 UTC641
            barrier(
7de49c2aa70batagen2026-10-16 09:27 UTC642
                d,
7de49c2aa70batagen2026-10-16 09:27 UTC643
                cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC644
                image.image,
7de49c2aa70batagen2026-10-16 09:27 UTC645
                vk::ImageLayout::GENERAL,
7de49c2aa70batagen2026-10-16 09:27 UTC646
                layout,
7de49c2aa70batagen2026-10-16 09:27 UTC647
                vk::QUEUE_FAMILY_FOREIGN_EXT,
7de49c2aa70batagen2026-10-16 09:27 UTC648
                family,
7de49c2aa70batagen2026-10-16 09:27 UTC649
            )
7de49c2aa70batagen2026-10-16 09:27 UTC650
        };
7de49c2aa70batagen2026-10-16 09:27 UTC651
    } else {
7de49c2aa70batagen2026-10-16 09:27 UTC652
        let old = image.layout();
7de49c2aa70batagen2026-10-16 09:27 UTC653
        if old != layout {
7de49c2aa70batagen2026-10-16 09:27 UTC654
            unsafe {
7de49c2aa70batagen2026-10-16 09:27 UTC655
                barrier(
7de49c2aa70batagen2026-10-16 09:27 UTC656
                    d,
7de49c2aa70batagen2026-10-16 09:27 UTC657
                    cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC658
                    image.image,
7de49c2aa70batagen2026-10-16 09:27 UTC659
                    old,
7de49c2aa70batagen2026-10-16 09:27 UTC660
                    layout,
7de49c2aa70batagen2026-10-16 09:27 UTC661
                    vk::QUEUE_FAMILY_IGNORED,
7de49c2aa70batagen2026-10-16 09:27 UTC662
                    vk::QUEUE_FAMILY_IGNORED,
7de49c2aa70batagen2026-10-16 09:27 UTC663
                )
7de49c2aa70batagen2026-10-16 09:27 UTC664
            };
7de49c2aa70batagen2026-10-16 09:27 UTC665
            image.set_layout(layout);
7de49c2aa70batagen2026-10-16 09:27 UTC666
        }
7de49c2aa70batagen2026-10-16 09:27 UTC667
    }
7de49c2aa70batagen2026-10-16 09:27 UTC668
}
7de49c2aa70batagen2026-10-16 09:27 UTC669
7de49c2aa70batagen2026-10-16 09:27 UTC670
/// Hands a dmabuf image back to the foreign queue family after use.
7de49c2aa70batagen2026-10-16 09:27 UTC671
unsafe fn release(
7de49c2aa70batagen2026-10-16 09:27 UTC672
    d: &ash::Device,
7de49c2aa70batagen2026-10-16 09:27 UTC673
    cmd: vk::CommandBuffer,
7de49c2aa70batagen2026-10-16 09:27 UTC674
    family: u32,
7de49c2aa70batagen2026-10-16 09:27 UTC675
    image: &ImageInner,
7de49c2aa70batagen2026-10-16 09:27 UTC676
    layout: vk::ImageLayout,
7de49c2aa70batagen2026-10-16 09:27 UTC677
) {
7de49c2aa70batagen2026-10-16 09:27 UTC678
    if image.external {
7de49c2aa70batagen2026-10-16 09:27 UTC679
        unsafe {
7de49c2aa70batagen2026-10-16 09:27 UTC680
            barrier(
7de49c2aa70batagen2026-10-16 09:27 UTC681
                d,
7de49c2aa70batagen2026-10-16 09:27 UTC682
                cmd,
7de49c2aa70batagen2026-10-16 09:27 UTC683
                image.image,
7de49c2aa70batagen2026-10-16 09:27 UTC684
                layout,
7de49c2aa70batagen2026-10-16 09:27 UTC685
                vk::ImageLayout::GENERAL,
7de49c2aa70batagen2026-10-16 09:27 UTC686
                family,
7de49c2aa70batagen2026-10-16 09:27 UTC687
                vk::QUEUE_FAMILY_FOREIGN_EXT,
7de49c2aa70batagen2026-10-16 09:27 UTC688
            )
7de49c2aa70batagen2026-10-16 09:27 UTC689
        };
7de49c2aa70batagen2026-10-16 09:27 UTC690
    }
7de49c2aa70batagen2026-10-16 09:27 UTC691
}
7de49c2aa70batagen2026-10-16 09:27 UTC692
824bf02b6e55atagen2026-10-16 09:15 UTC693
/// Whether reading `from` as `to` needs red and blue swapped.
824bf02b6e55atagen2026-10-16 09:15 UTC694
fn needs_swizzle(from: Fourcc, to: Fourcc) -> Result<bool, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC695
    let bgr = |f: Fourcc| match f {
824bf02b6e55atagen2026-10-16 09:15 UTC696
        Fourcc::Argb8888 | Fourcc::Xrgb8888 => Ok(true),
824bf02b6e55atagen2026-10-16 09:15 UTC697
        Fourcc::Abgr8888 | Fourcc::Xbgr8888 => Ok(false),
824bf02b6e55atagen2026-10-16 09:15 UTC698
        f => Err(Error::UnsupportedFormat(f)),
824bf02b6e55atagen2026-10-16 09:15 UTC699
    };
824bf02b6e55atagen2026-10-16 09:15 UTC700
    Ok(bgr(from)? != bgr(to)?)
824bf02b6e55atagen2026-10-16 09:15 UTC701
}
824bf02b6e55atagen2026-10-16 09:15 UTC702
824bf02b6e55atagen2026-10-16 09:15 UTC703
impl RendererSuper for VulkanRenderer {
824bf02b6e55atagen2026-10-16 09:15 UTC704
    type Error = Error;
824bf02b6e55atagen2026-10-16 09:15 UTC705
    type TextureId = VulkanTexture;
824bf02b6e55atagen2026-10-16 09:15 UTC706
    type Framebuffer<'buffer> = VulkanTarget<'buffer>;
824bf02b6e55atagen2026-10-16 09:15 UTC707
    type Frame<'frame, 'buffer>
824bf02b6e55atagen2026-10-16 09:15 UTC708
        = VulkanFrame<'frame, 'buffer>
824bf02b6e55atagen2026-10-16 09:15 UTC709
    where
824bf02b6e55atagen2026-10-16 09:15 UTC710
        'buffer: 'frame,
824bf02b6e55atagen2026-10-16 09:15 UTC711
        Self: 'frame;
824bf02b6e55atagen2026-10-16 09:15 UTC712
}
824bf02b6e55atagen2026-10-16 09:15 UTC713
824bf02b6e55atagen2026-10-16 09:15 UTC714
impl Renderer for VulkanRenderer {
824bf02b6e55atagen2026-10-16 09:15 UTC715
    fn context_id(&self) -> ContextId<VulkanTexture> {
824bf02b6e55atagen2026-10-16 09:15 UTC716
        self.context.clone()
824bf02b6e55atagen2026-10-16 09:15 UTC717
    }
824bf02b6e55atagen2026-10-16 09:15 UTC718
824bf02b6e55atagen2026-10-16 09:15 UTC719
    fn downscale_filter(&mut self, filter: TextureFilter) -> Result<(), Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC720
        self.min_filter = filter;
824bf02b6e55atagen2026-10-16 09:15 UTC721
        Ok(())
824bf02b6e55atagen2026-10-16 09:15 UTC722
    }
824bf02b6e55atagen2026-10-16 09:15 UTC723
824bf02b6e55atagen2026-10-16 09:15 UTC724
    fn upscale_filter(&mut self, filter: TextureFilter) -> Result<(), Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC725
        self.mag_filter = filter;
824bf02b6e55atagen2026-10-16 09:15 UTC726
        Ok(())
824bf02b6e55atagen2026-10-16 09:15 UTC727
    }
824bf02b6e55atagen2026-10-16 09:15 UTC728
824bf02b6e55atagen2026-10-16 09:15 UTC729
    fn set_debug_flags(&mut self, flags: DebugFlags) {
824bf02b6e55atagen2026-10-16 09:15 UTC730
        self.debug = flags;
824bf02b6e55atagen2026-10-16 09:15 UTC731
    }
824bf02b6e55atagen2026-10-16 09:15 UTC732
824bf02b6e55atagen2026-10-16 09:15 UTC733
    fn debug_flags(&self) -> DebugFlags {
824bf02b6e55atagen2026-10-16 09:15 UTC734
        self.debug
824bf02b6e55atagen2026-10-16 09:15 UTC735
    }
824bf02b6e55atagen2026-10-16 09:15 UTC736
824bf02b6e55atagen2026-10-16 09:15 UTC737
    fn render<'frame, 'buffer>(
824bf02b6e55atagen2026-10-16 09:15 UTC738
        &'frame mut self,
824bf02b6e55atagen2026-10-16 09:15 UTC739
        framebuffer: &'frame mut VulkanTarget<'buffer>,
824bf02b6e55atagen2026-10-16 09:15 UTC740
        output_size: Size<i32, Physical>,
824bf02b6e55atagen2026-10-16 09:15 UTC741
        dst_transform: Transform,
824bf02b6e55atagen2026-10-16 09:15 UTC742
    ) -> Result<VulkanFrame<'frame, 'buffer>, Error>
824bf02b6e55atagen2026-10-16 09:15 UTC743
    where
824bf02b6e55atagen2026-10-16 09:15 UTC744
        'buffer: 'frame,
824bf02b6e55atagen2026-10-16 09:15 UTC745
    {
7de49c2aa70batagen2026-10-16 09:27 UTC746
        self.own(&framebuffer.0)?;
824bf02b6e55atagen2026-10-16 09:15 UTC747
        self.reap();
824bf02b6e55atagen2026-10-16 09:15 UTC748
        Ok(VulkanFrame::new(
824bf02b6e55atagen2026-10-16 09:15 UTC749
            self,
824bf02b6e55atagen2026-10-16 09:15 UTC750
            framebuffer,
824bf02b6e55atagen2026-10-16 09:15 UTC751
            output_size,
824bf02b6e55atagen2026-10-16 09:15 UTC752
            dst_transform,
824bf02b6e55atagen2026-10-16 09:15 UTC753
        ))
824bf02b6e55atagen2026-10-16 09:15 UTC754
    }
824bf02b6e55atagen2026-10-16 09:15 UTC755
824bf02b6e55atagen2026-10-16 09:15 UTC756
    fn wait(&mut self, sync: &SyncPoint) -> Result<(), Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC757
        sync.wait().map_err(|_| Error::Interrupted)
824bf02b6e55atagen2026-10-16 09:15 UTC758
    }
824bf02b6e55atagen2026-10-16 09:15 UTC759
824bf02b6e55atagen2026-10-16 09:15 UTC760
    fn cleanup_texture_cache(&mut self) -> Result<(), Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC761
        self.reap();
824bf02b6e55atagen2026-10-16 09:15 UTC762
        Ok(())
824bf02b6e55atagen2026-10-16 09:15 UTC763
    }
7de49c2aa70batagen2026-10-16 09:27 UTC764
7de49c2aa70batagen2026-10-16 09:27 UTC765
    fn invalidate_caches(&mut self) -> Result<(), Error> {
7de49c2aa70batagen2026-10-16 09:27 UTC766
        self.reap();
7de49c2aa70batagen2026-10-16 09:27 UTC767
        self.dmabuf_textures.clear();
7de49c2aa70batagen2026-10-16 09:27 UTC768
        self.dmabuf_targets.clear();
7de49c2aa70batagen2026-10-16 09:27 UTC769
        Ok(())
7de49c2aa70batagen2026-10-16 09:27 UTC770
    }
824bf02b6e55atagen2026-10-16 09:15 UTC771
}
824bf02b6e55atagen2026-10-16 09:15 UTC772
824bf02b6e55atagen2026-10-16 09:15 UTC773
impl Bind<VulkanImage> for VulkanRenderer {
824bf02b6e55atagen2026-10-16 09:15 UTC774
    fn bind<'a>(&mut self, target: &'a mut VulkanImage) -> Result<VulkanTarget<'a>, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC775
        self.own(&target.0)?;
7de49c2aa70batagen2026-10-16 09:27 UTC776
        Ok(VulkanTarget(target.0.clone(), PhantomData))
7de49c2aa70batagen2026-10-16 09:27 UTC777
    }
7de49c2aa70batagen2026-10-16 09:27 UTC778
}
7de49c2aa70batagen2026-10-16 09:27 UTC779
7de49c2aa70batagen2026-10-16 09:27 UTC780
impl Bind<Dmabuf> for VulkanRenderer {
30aece3a63ceatagen2026-10-16 13:48 UTC781
    #[profiling::function]
7de49c2aa70batagen2026-10-16 09:27 UTC782
    fn bind<'a>(&mut self, target: &'a mut Dmabuf) -> Result<VulkanTarget<'a>, Error> {
7de49c2aa70batagen2026-10-16 09:27 UTC783
        let key = target.weak();
7de49c2aa70batagen2026-10-16 09:27 UTC784
        let image = match self.dmabuf_targets.get(&key) {
7de49c2aa70batagen2026-10-16 09:27 UTC785
            Some(image) => image.clone(),
7de49c2aa70batagen2026-10-16 09:27 UTC786
            None => {
7de49c2aa70batagen2026-10-16 09:27 UTC787
                let image = dmabuf::import(&self.shared, &self.caps, target, RENDER_USAGE)?;
7de49c2aa70batagen2026-10-16 09:27 UTC788
                self.dmabuf_targets.insert(key, image.clone());
7de49c2aa70batagen2026-10-16 09:27 UTC789
                image
7de49c2aa70batagen2026-10-16 09:27 UTC790
            }
7de49c2aa70batagen2026-10-16 09:27 UTC791
        };
7de49c2aa70batagen2026-10-16 09:27 UTC792
        Ok(VulkanTarget(image, PhantomData))
7de49c2aa70batagen2026-10-16 09:27 UTC793
    }
7de49c2aa70batagen2026-10-16 09:27 UTC794
7de49c2aa70batagen2026-10-16 09:27 UTC795
    fn supported_formats(&self) -> Option<FormatSet> {
7de49c2aa70batagen2026-10-16 09:27 UTC796
        Some(self.caps.render.clone())
824bf02b6e55atagen2026-10-16 09:15 UTC797
    }
824bf02b6e55atagen2026-10-16 09:15 UTC798
}
824bf02b6e55atagen2026-10-16 09:15 UTC799
824bf02b6e55atagen2026-10-16 09:15 UTC800
impl Offscreen<VulkanImage> for VulkanRenderer {
824bf02b6e55atagen2026-10-16 09:15 UTC801
    fn create_buffer(
824bf02b6e55atagen2026-10-16 09:15 UTC802
        &mut self,
824bf02b6e55atagen2026-10-16 09:15 UTC803
        format: Fourcc,
824bf02b6e55atagen2026-10-16 09:15 UTC804
        size: Size<i32, Buffer>,
824bf02b6e55atagen2026-10-16 09:15 UTC805
    ) -> Result<VulkanImage, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC806
        if size.w <= 0 || size.h <= 0 {
824bf02b6e55atagen2026-10-16 09:15 UTC807
            return Err(Error::InvalidSize(size.w, size.h));
824bf02b6e55atagen2026-10-16 09:15 UTC808
        }
824bf02b6e55atagen2026-10-16 09:15 UTC809
        let usage = vk::ImageUsageFlags::COLOR_ATTACHMENT
824bf02b6e55atagen2026-10-16 09:15 UTC810
            | vk::ImageUsageFlags::SAMPLED
824bf02b6e55atagen2026-10-16 09:15 UTC811
            | vk::ImageUsageFlags::TRANSFER_SRC
824bf02b6e55atagen2026-10-16 09:15 UTC812
            | vk::ImageUsageFlags::TRANSFER_DST;
824bf02b6e55atagen2026-10-16 09:15 UTC813
        Ok(VulkanImage(ImageInner::create(
824bf02b6e55atagen2026-10-16 09:15 UTC814
            &self.shared,
824bf02b6e55atagen2026-10-16 09:15 UTC815
            format,
824bf02b6e55atagen2026-10-16 09:15 UTC816
            size.w as u32,
824bf02b6e55atagen2026-10-16 09:15 UTC817
            size.h as u32,
824bf02b6e55atagen2026-10-16 09:15 UTC818
            usage,
824bf02b6e55atagen2026-10-16 09:15 UTC819
        )?))
824bf02b6e55atagen2026-10-16 09:15 UTC820
    }
824bf02b6e55atagen2026-10-16 09:15 UTC821
}
824bf02b6e55atagen2026-10-16 09:15 UTC822
824bf02b6e55atagen2026-10-16 09:15 UTC823
impl ImportMem for VulkanRenderer {
824bf02b6e55atagen2026-10-16 09:15 UTC824
    fn import_memory(
824bf02b6e55atagen2026-10-16 09:15 UTC825
        &mut self,
824bf02b6e55atagen2026-10-16 09:15 UTC826
        data: &[u8],
824bf02b6e55atagen2026-10-16 09:15 UTC827
        format: Fourcc,
824bf02b6e55atagen2026-10-16 09:15 UTC828
        size: Size<i32, Buffer>,
824bf02b6e55atagen2026-10-16 09:15 UTC829
        flipped: bool,
824bf02b6e55atagen2026-10-16 09:15 UTC830
    ) -> Result<VulkanTexture, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC831
        if size.w <= 0 || size.h <= 0 {
824bf02b6e55atagen2026-10-16 09:15 UTC832
            return Err(Error::InvalidSize(size.w, size.h));
824bf02b6e55atagen2026-10-16 09:15 UTC833
        }
824bf02b6e55atagen2026-10-16 09:15 UTC834
        let usage = vk::ImageUsageFlags::SAMPLED
824bf02b6e55atagen2026-10-16 09:15 UTC835
            | vk::ImageUsageFlags::TRANSFER_DST
824bf02b6e55atagen2026-10-16 09:15 UTC836
            | vk::ImageUsageFlags::TRANSFER_SRC;
824bf02b6e55atagen2026-10-16 09:15 UTC837
        let image = ImageInner::create(&self.shared, format, size.w as u32, size.h as u32, usage)?;
824bf02b6e55atagen2026-10-16 09:15 UTC838
        self.upload(&image, data, Rectangle::from_size(size), flipped)?;
824bf02b6e55atagen2026-10-16 09:15 UTC839
        Ok(VulkanTexture(image))
824bf02b6e55atagen2026-10-16 09:15 UTC840
    }
824bf02b6e55atagen2026-10-16 09:15 UTC841
824bf02b6e55atagen2026-10-16 09:15 UTC842
    fn update_memory(
824bf02b6e55atagen2026-10-16 09:15 UTC843
        &mut self,
824bf02b6e55atagen2026-10-16 09:15 UTC844
        texture: &VulkanTexture,
824bf02b6e55atagen2026-10-16 09:15 UTC845
        data: &[u8],
824bf02b6e55atagen2026-10-16 09:15 UTC846
        region: Rectangle<i32, Buffer>,
824bf02b6e55atagen2026-10-16 09:15 UTC847
    ) -> Result<(), Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC848
        self.own(&texture.0)?;
824bf02b6e55atagen2026-10-16 09:15 UTC849
        let Some(region) = region.intersection(Rectangle::from_size(texture.size())) else {
824bf02b6e55atagen2026-10-16 09:15 UTC850
            return Ok(());
824bf02b6e55atagen2026-10-16 09:15 UTC851
        };
824bf02b6e55atagen2026-10-16 09:15 UTC852
        self.upload(&texture.0, data, region, false)
824bf02b6e55atagen2026-10-16 09:15 UTC853
    }
824bf02b6e55atagen2026-10-16 09:15 UTC854
824bf02b6e55atagen2026-10-16 09:15 UTC855
    fn mem_formats(&self) -> Box<dyn Iterator<Item = Fourcc>> {
824bf02b6e55atagen2026-10-16 09:15 UTC856
        Box::new(SUPPORTED.into_iter())
824bf02b6e55atagen2026-10-16 09:15 UTC857
    }
824bf02b6e55atagen2026-10-16 09:15 UTC858
}
824bf02b6e55atagen2026-10-16 09:15 UTC859
7de49c2aa70batagen2026-10-16 09:27 UTC860
impl ImportDma for VulkanRenderer {
7de49c2aa70batagen2026-10-16 09:27 UTC861
    fn dmabuf_formats(&self) -> FormatSet {
7de49c2aa70batagen2026-10-16 09:27 UTC862
        self.caps.sample.clone()
7de49c2aa70batagen2026-10-16 09:27 UTC863
    }
7de49c2aa70batagen2026-10-16 09:27 UTC864
30aece3a63ceatagen2026-10-16 13:48 UTC865
    #[profiling::function]
7de49c2aa70batagen2026-10-16 09:27 UTC866
    fn import_dmabuf(
7de49c2aa70batagen2026-10-16 09:27 UTC867
        &mut self,
7de49c2aa70batagen2026-10-16 09:27 UTC868
        buffer: &Dmabuf,
7de49c2aa70batagen2026-10-16 09:27 UTC869
        _damage: Option<&[Rectangle<i32, Buffer>]>,
7de49c2aa70batagen2026-10-16 09:27 UTC870
    ) -> Result<VulkanTexture, Error> {
7de49c2aa70batagen2026-10-16 09:27 UTC871
        let key = buffer.weak();
7de49c2aa70batagen2026-10-16 09:27 UTC872
        if let Some(t) = self.dmabuf_textures.get(&key) {
7de49c2aa70batagen2026-10-16 09:27 UTC873
            return Ok(t.clone());
7de49c2aa70batagen2026-10-16 09:27 UTC874
        }
7de49c2aa70batagen2026-10-16 09:27 UTC875
        let texture = VulkanTexture(dmabuf::import(
7de49c2aa70batagen2026-10-16 09:27 UTC876
            &self.shared,
7de49c2aa70batagen2026-10-16 09:27 UTC877
            &self.caps,
7de49c2aa70batagen2026-10-16 09:27 UTC878
            buffer,
7de49c2aa70batagen2026-10-16 09:27 UTC879
            SAMPLE_USAGE,
7de49c2aa70batagen2026-10-16 09:27 UTC880
        )?);
7de49c2aa70batagen2026-10-16 09:27 UTC881
        self.dmabuf_textures.insert(key, texture.clone());
7de49c2aa70batagen2026-10-16 09:27 UTC882
        Ok(texture)
7de49c2aa70batagen2026-10-16 09:27 UTC883
    }
7de49c2aa70batagen2026-10-16 09:27 UTC884
}
7de49c2aa70batagen2026-10-16 09:27 UTC885
7de49c2aa70batagen2026-10-16 09:27 UTC886
impl ImportDmaWl for VulkanRenderer {}
7de49c2aa70batagen2026-10-16 09:27 UTC887
824bf02b6e55atagen2026-10-16 09:15 UTC888
impl ExportMem for VulkanRenderer {
824bf02b6e55atagen2026-10-16 09:15 UTC889
    type TextureMapping = VulkanMapping;
824bf02b6e55atagen2026-10-16 09:15 UTC890
824bf02b6e55atagen2026-10-16 09:15 UTC891
    fn copy_framebuffer(
824bf02b6e55atagen2026-10-16 09:15 UTC892
        &mut self,
824bf02b6e55atagen2026-10-16 09:15 UTC893
        target: &VulkanTarget<'_>,
824bf02b6e55atagen2026-10-16 09:15 UTC894
        region: Rectangle<i32, Buffer>,
824bf02b6e55atagen2026-10-16 09:15 UTC895
        format: Fourcc,
824bf02b6e55atagen2026-10-16 09:15 UTC896
    ) -> Result<VulkanMapping, Error> {
7de49c2aa70batagen2026-10-16 09:27 UTC897
        self.own(&target.0)?;
7de49c2aa70batagen2026-10-16 09:27 UTC898
        let image = target.0.clone();
824bf02b6e55atagen2026-10-16 09:15 UTC899
        self.read_back(&image, region, format)
824bf02b6e55atagen2026-10-16 09:15 UTC900
    }
824bf02b6e55atagen2026-10-16 09:15 UTC901
824bf02b6e55atagen2026-10-16 09:15 UTC902
    fn copy_texture(
824bf02b6e55atagen2026-10-16 09:15 UTC903
        &mut self,
824bf02b6e55atagen2026-10-16 09:15 UTC904
        texture: &VulkanTexture,
824bf02b6e55atagen2026-10-16 09:15 UTC905
        region: Rectangle<i32, Buffer>,
824bf02b6e55atagen2026-10-16 09:15 UTC906
        format: Fourcc,
824bf02b6e55atagen2026-10-16 09:15 UTC907
    ) -> Result<VulkanMapping, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC908
        self.own(&texture.0)?;
824bf02b6e55atagen2026-10-16 09:15 UTC909
        self.read_back(&texture.0, region, format)
824bf02b6e55atagen2026-10-16 09:15 UTC910
    }
824bf02b6e55atagen2026-10-16 09:15 UTC911
7de49c2aa70batagen2026-10-16 09:27 UTC912
    fn can_read_texture(&mut self, texture: &VulkanTexture) -> Result<bool, Error> {
7de49c2aa70batagen2026-10-16 09:27 UTC913
        Ok(texture.0.external || texture.0.layout() != vk::ImageLayout::UNDEFINED)
824bf02b6e55atagen2026-10-16 09:15 UTC914
    }
824bf02b6e55atagen2026-10-16 09:15 UTC915
824bf02b6e55atagen2026-10-16 09:15 UTC916
    fn map_texture<'a>(&mut self, mapping: &'a VulkanMapping) -> Result<&'a [u8], Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC917
        Ok(&mapping.data)
824bf02b6e55atagen2026-10-16 09:15 UTC918
    }
824bf02b6e55atagen2026-10-16 09:15 UTC919
}
d4a64ce87164atagen2026-10-16 09:34 UTC920
d4a64ce87164atagen2026-10-16 09:34 UTC921
impl ImportMemWl for VulkanRenderer {
48af562d968batagen2026-10-16 12:39 UTC922
    /// Keeps one texture per surface and uploads only the damaged rows into
48af562d968batagen2026-10-16 12:39 UTC923
    /// it, straight from the client's shm pool.
d4a64ce87164atagen2026-10-16 09:34 UTC924
    fn import_shm_buffer(
d4a64ce87164atagen2026-10-16 09:34 UTC925
        &mut self,
d4a64ce87164atagen2026-10-16 09:34 UTC926
        buffer: &WlBuffer,
48af562d968batagen2026-10-16 12:39 UTC927
        surface: Option<&SurfaceData>,
48af562d968batagen2026-10-16 12:39 UTC928
        damage: &[Rectangle<i32, Buffer>],
d4a64ce87164atagen2026-10-16 09:34 UTC929
    ) -> Result<VulkanTexture, Error> {
48af562d968batagen2026-10-16 12:39 UTC930
        let cache = surface.map(|s| {
48af562d968batagen2026-10-16 12:39 UTC931
            s.data_map
48af562d968batagen2026-10-16 12:39 UTC932
                .insert_if_missing_threadsafe(|| ShmCache(Mutex::new(None)));
48af562d968batagen2026-10-16 12:39 UTC933
            s.data_map.get::<ShmCache>().expect("just inserted")
48af562d968batagen2026-10-16 12:39 UTC934
        });
48af562d968batagen2026-10-16 12:39 UTC935
        with_buffer_contents(buffer, |ptr, len, data| {
d4a64ce87164atagen2026-10-16 09:34 UTC936
            let fourcc = shm_format_to_fourcc(data.format).ok_or(Error::ShmAccess)?;
d4a64ce87164atagen2026-10-16 09:34 UTC937
            let (w, h, stride, offset) = (
d4a64ce87164atagen2026-10-16 09:34 UTC938
                data.width as usize,
d4a64ce87164atagen2026-10-16 09:34 UTC939
                data.height as usize,
d4a64ce87164atagen2026-10-16 09:34 UTC940
                data.stride as usize,
d4a64ce87164atagen2026-10-16 09:34 UTC941
                data.offset as usize,
d4a64ce87164atagen2026-10-16 09:34 UTC942
            );
48af562d968batagen2026-10-16 12:39 UTC943
            let line = w * BYTES_PER_PIXEL;
48af562d968batagen2026-10-16 12:39 UTC944
            if w == 0 || h == 0 || stride < line || offset + stride * (h - 1) + line > len {
d4a64ce87164atagen2026-10-16 09:34 UTC945
                return Err(Error::ShmAccess);
d4a64ce87164atagen2026-10-16 09:34 UTC946
            }
48af562d968batagen2026-10-16 12:39 UTC947
            let size = Size::from((data.width, data.height));
48af562d968batagen2026-10-16 12:39 UTC948
            let full = Rectangle::from_size(size);
48af562d968batagen2026-10-16 12:39 UTC949
            let reused = cache
48af562d968batagen2026-10-16 12:39 UTC950
                .and_then(|c| c.0.lock().unwrap_or_else(|e| e.into_inner()).clone())
48af562d968batagen2026-10-16 12:39 UTC951
                .filter(|t| {
48af562d968batagen2026-10-16 12:39 UTC952
                    t.0.fourcc == fourcc
48af562d968batagen2026-10-16 12:39 UTC953
                        && t.size() == size
48af562d968batagen2026-10-16 12:39 UTC954
                        && Arc::ptr_eq(&t.0.shared, &self.shared)
48af562d968batagen2026-10-16 12:39 UTC955
                });
48af562d968batagen2026-10-16 12:39 UTC956
            let (texture, region) = match reused {
48af562d968batagen2026-10-16 12:39 UTC957
                Some(t) => {
48af562d968batagen2026-10-16 12:39 UTC958
                    let dirty = damage
48af562d968batagen2026-10-16 12:39 UTC959
                        .iter()
48af562d968batagen2026-10-16 12:39 UTC960
                        .filter_map(|d| d.intersection(full))
48af562d968batagen2026-10-16 12:39 UTC961
                        .reduce(|a, b| a.merge(b));
48af562d968batagen2026-10-16 12:39 UTC962
                    (t, if damage.is_empty() { Some(full) } else { dirty })
48af562d968batagen2026-10-16 12:39 UTC963
                }
48af562d968batagen2026-10-16 12:39 UTC964
                None => {
48af562d968batagen2026-10-16 12:39 UTC965
                    let usage = vk::ImageUsageFlags::SAMPLED
48af562d968batagen2026-10-16 12:39 UTC966
                        | vk::ImageUsageFlags::TRANSFER_DST
48af562d968batagen2026-10-16 12:39 UTC967
                        | vk::ImageUsageFlags::TRANSFER_SRC;
48af562d968batagen2026-10-16 12:39 UTC968
                    let image =
48af562d968batagen2026-10-16 12:39 UTC969
                        ImageInner::create(&self.shared, fourcc, w as u32, h as u32, usage)?;
48af562d968batagen2026-10-16 12:39 UTC970
                    (VulkanTexture(image), Some(full))
48af562d968batagen2026-10-16 12:39 UTC971
                }
48af562d968batagen2026-10-16 12:39 UTC972
            };
48af562d968batagen2026-10-16 12:39 UTC973
            if let Some(region) = region {
48af562d968batagen2026-10-16 12:39 UTC974
                let x = region.loc.x as usize * BYTES_PER_PIXEL;
48af562d968batagen2026-10-16 12:39 UTC975
                let rw = region.size.w as usize * BYTES_PER_PIXEL;
48af562d968batagen2026-10-16 12:39 UTC976
                self.upload_with(&texture.0, region, |y| unsafe {
48af562d968batagen2026-10-16 12:39 UTC977
                    std::slice::from_raw_parts(ptr.add(offset + y * stride + x), rw)
48af562d968batagen2026-10-16 12:39 UTC978
                })?;
d4a64ce87164atagen2026-10-16 09:34 UTC979
            }
48af562d968batagen2026-10-16 12:39 UTC980
            if let Some(c) = cache {
48af562d968batagen2026-10-16 12:39 UTC981
                *c.0.lock().unwrap_or_else(|e| e.into_inner()) = Some(texture.clone());
48af562d968batagen2026-10-16 12:39 UTC982
            }
48af562d968batagen2026-10-16 12:39 UTC983
            Ok(texture)
d4a64ce87164atagen2026-10-16 09:34 UTC984
        })
48af562d968batagen2026-10-16 12:39 UTC985
        .map_err(|_| Error::ShmAccess)?
d4a64ce87164atagen2026-10-16 09:34 UTC986
    }
d4a64ce87164atagen2026-10-16 09:34 UTC987
}
48af562d968batagen2026-10-16 12:39 UTC988
48af562d968batagen2026-10-16 12:39 UTC989
/// The texture last imported for a surface's shm buffers.
48af562d968batagen2026-10-16 12:39 UTC990
struct ShmCache(Mutex<Option<VulkanTexture>>);