atagen

monolith

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