atagen

monolith

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