atagen

monolith

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