atagen

monolith

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