atagen

monolith

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

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

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