atagen

monolith

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

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

824bf02b6e55atagen2026-10-16 09:15 UTC1
use crate::Error;
824bf02b6e55atagen2026-10-16 09:15 UTC2
use crate::device::Shared;
824bf02b6e55atagen2026-10-16 09:15 UTC3
use crate::format::{components, vk_format};
824bf02b6e55atagen2026-10-16 09:15 UTC4
use ash::vk;
824bf02b6e55atagen2026-10-16 09:15 UTC5
use gpu_allocator::MemoryLocation;
824bf02b6e55atagen2026-10-16 09:15 UTC6
use gpu_allocator::vulkan::{Allocation, AllocationCreateDesc, AllocationScheme};
824bf02b6e55atagen2026-10-16 09:15 UTC7
use smithay::backend::allocator::Fourcc;
824bf02b6e55atagen2026-10-16 09:15 UTC8
use smithay::backend::renderer::{Texture, TextureMapping};
824bf02b6e55atagen2026-10-16 09:15 UTC9
use std::fmt;
7de49c2aa70batagen2026-10-16 09:27 UTC10
use std::marker::PhantomData;
824bf02b6e55atagen2026-10-16 09:15 UTC11
use std::sync::{Arc, Mutex};
824bf02b6e55atagen2026-10-16 09:15 UTC12
7de49c2aa70batagen2026-10-16 09:27 UTC13
pub(crate) enum Memory {
7de49c2aa70batagen2026-10-16 09:27 UTC14
    Allocated(Allocation),
7de49c2aa70batagen2026-10-16 09:27 UTC15
    /// Memory imported from a dmabuf, owned directly.
7de49c2aa70batagen2026-10-16 09:27 UTC16
    Imported(vk::DeviceMemory),
7de49c2aa70batagen2026-10-16 09:27 UTC17
}
7de49c2aa70batagen2026-10-16 09:27 UTC18
824bf02b6e55atagen2026-10-16 09:15 UTC19
pub(crate) struct ImageInner {
824bf02b6e55atagen2026-10-16 09:15 UTC20
    pub shared: Arc<Shared>,
824bf02b6e55atagen2026-10-16 09:15 UTC21
    pub image: vk::Image,
824bf02b6e55atagen2026-10-16 09:15 UTC22
    pub view: vk::ImageView,
7de49c2aa70batagen2026-10-16 09:27 UTC23
    pub memory: Mutex<Option<Memory>>,
824bf02b6e55atagen2026-10-16 09:15 UTC24
    pub fourcc: Fourcc,
824bf02b6e55atagen2026-10-16 09:15 UTC25
    pub format: vk::Format,
824bf02b6e55atagen2026-10-16 09:15 UTC26
    pub width: u32,
824bf02b6e55atagen2026-10-16 09:15 UTC27
    pub height: u32,
f0e45b059808atagen2026-10-19 07:34 UTC28
    pub usage: vk::ImageUsageFlags,
824bf02b6e55atagen2026-10-16 09:15 UTC29
    pub layout: Mutex<vk::ImageLayout>,
7de49c2aa70batagen2026-10-16 09:27 UTC30
    /// Shared with other devices or processes through a dmabuf. Every use
7de49c2aa70batagen2026-10-16 09:27 UTC31
    /// acquires the image from the foreign queue family and releases it after.
7de49c2aa70batagen2026-10-16 09:27 UTC32
    pub external: bool,
824bf02b6e55atagen2026-10-16 09:15 UTC33
}
824bf02b6e55atagen2026-10-16 09:15 UTC34
824bf02b6e55atagen2026-10-16 09:15 UTC35
impl fmt::Debug for ImageInner {
824bf02b6e55atagen2026-10-16 09:15 UTC36
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
824bf02b6e55atagen2026-10-16 09:15 UTC37
        f.debug_struct("ImageInner")
824bf02b6e55atagen2026-10-16 09:15 UTC38
            .field("image", &self.image)
824bf02b6e55atagen2026-10-16 09:15 UTC39
            .field("fourcc", &self.fourcc)
824bf02b6e55atagen2026-10-16 09:15 UTC40
            .field("size", &(self.width, self.height))
7de49c2aa70batagen2026-10-16 09:27 UTC41
            .field("external", &self.external)
824bf02b6e55atagen2026-10-16 09:15 UTC42
            .finish()
824bf02b6e55atagen2026-10-16 09:15 UTC43
    }
824bf02b6e55atagen2026-10-16 09:15 UTC44
}
824bf02b6e55atagen2026-10-16 09:15 UTC45
824bf02b6e55atagen2026-10-16 09:15 UTC46
impl Drop for ImageInner {
824bf02b6e55atagen2026-10-16 09:15 UTC47
    fn drop(&mut self) {
824bf02b6e55atagen2026-10-16 09:15 UTC48
        let d = &self.shared.device.device;
824bf02b6e55atagen2026-10-16 09:15 UTC49
        unsafe {
824bf02b6e55atagen2026-10-16 09:15 UTC50
            d.destroy_image_view(self.view, None);
824bf02b6e55atagen2026-10-16 09:15 UTC51
            d.destroy_image(self.image, None);
824bf02b6e55atagen2026-10-16 09:15 UTC52
        }
7de49c2aa70batagen2026-10-16 09:27 UTC53
        match self.memory.lock().unwrap_or_else(|e| e.into_inner()).take() {
7de49c2aa70batagen2026-10-16 09:27 UTC54
            Some(Memory::Allocated(a)) => {
7de49c2aa70batagen2026-10-16 09:27 UTC55
                let _ = self
7de49c2aa70batagen2026-10-16 09:27 UTC56
                    .shared
7de49c2aa70batagen2026-10-16 09:27 UTC57
                    .allocator
7de49c2aa70batagen2026-10-16 09:27 UTC58
                    .lock()
7de49c2aa70batagen2026-10-16 09:27 UTC59
                    .unwrap_or_else(|e| e.into_inner())
7de49c2aa70batagen2026-10-16 09:27 UTC60
                    .free(a);
7de49c2aa70batagen2026-10-16 09:27 UTC61
            }
7de49c2aa70batagen2026-10-16 09:27 UTC62
            Some(Memory::Imported(m)) => unsafe { d.free_memory(m, None) },
7de49c2aa70batagen2026-10-16 09:27 UTC63
            None => {}
824bf02b6e55atagen2026-10-16 09:15 UTC64
        }
824bf02b6e55atagen2026-10-16 09:15 UTC65
    }
824bf02b6e55atagen2026-10-16 09:15 UTC66
}
824bf02b6e55atagen2026-10-16 09:15 UTC67
824bf02b6e55atagen2026-10-16 09:15 UTC68
impl ImageInner {
824bf02b6e55atagen2026-10-16 09:15 UTC69
    pub fn create(
824bf02b6e55atagen2026-10-16 09:15 UTC70
        shared: &Arc<Shared>,
824bf02b6e55atagen2026-10-16 09:15 UTC71
        fourcc: Fourcc,
824bf02b6e55atagen2026-10-16 09:15 UTC72
        width: u32,
824bf02b6e55atagen2026-10-16 09:15 UTC73
        height: u32,
824bf02b6e55atagen2026-10-16 09:15 UTC74
        usage: vk::ImageUsageFlags,
824bf02b6e55atagen2026-10-16 09:15 UTC75
    ) -> Result<Arc<Self>, Error> {
824bf02b6e55atagen2026-10-16 09:15 UTC76
        let fmt = vk_format(fourcc).ok_or(Error::UnsupportedFormat(fourcc))?;
824bf02b6e55atagen2026-10-16 09:15 UTC77
        if width == 0 || height == 0 {
824bf02b6e55atagen2026-10-16 09:15 UTC78
            return Err(Error::InvalidSize(width as i32, height as i32));
824bf02b6e55atagen2026-10-16 09:15 UTC79
        }
824bf02b6e55atagen2026-10-16 09:15 UTC80
        let d = &shared.device.device;
824bf02b6e55atagen2026-10-16 09:15 UTC81
        let info = vk::ImageCreateInfo::default()
824bf02b6e55atagen2026-10-16 09:15 UTC82
            .image_type(vk::ImageType::TYPE_2D)
824bf02b6e55atagen2026-10-16 09:15 UTC83
            .format(fmt.format)
824bf02b6e55atagen2026-10-16 09:15 UTC84
            .extent(vk::Extent3D {
824bf02b6e55atagen2026-10-16 09:15 UTC85
                width,
824bf02b6e55atagen2026-10-16 09:15 UTC86
                height,
824bf02b6e55atagen2026-10-16 09:15 UTC87
                depth: 1,
824bf02b6e55atagen2026-10-16 09:15 UTC88
            })
824bf02b6e55atagen2026-10-16 09:15 UTC89
            .mip_levels(1)
824bf02b6e55atagen2026-10-16 09:15 UTC90
            .array_layers(1)
824bf02b6e55atagen2026-10-16 09:15 UTC91
            .samples(vk::SampleCountFlags::TYPE_1)
824bf02b6e55atagen2026-10-16 09:15 UTC92
            .tiling(vk::ImageTiling::OPTIMAL)
824bf02b6e55atagen2026-10-16 09:15 UTC93
            .usage(usage)
824bf02b6e55atagen2026-10-16 09:15 UTC94
            .initial_layout(vk::ImageLayout::UNDEFINED);
824bf02b6e55atagen2026-10-16 09:15 UTC95
        let image = unsafe { d.create_image(&info, None)? };
824bf02b6e55atagen2026-10-16 09:15 UTC96
        let requirements = unsafe { d.get_image_memory_requirements(image) };
824bf02b6e55atagen2026-10-16 09:15 UTC97
        let allocation = shared
824bf02b6e55atagen2026-10-16 09:15 UTC98
            .allocator
824bf02b6e55atagen2026-10-16 09:15 UTC99
            .lock()
824bf02b6e55atagen2026-10-16 09:15 UTC100
            .unwrap_or_else(|e| e.into_inner())
824bf02b6e55atagen2026-10-16 09:15 UTC101
            .allocate(&AllocationCreateDesc {
824bf02b6e55atagen2026-10-16 09:15 UTC102
                name: "image",
824bf02b6e55atagen2026-10-16 09:15 UTC103
                requirements,
824bf02b6e55atagen2026-10-16 09:15 UTC104
                location: MemoryLocation::GpuOnly,
824bf02b6e55atagen2026-10-16 09:15 UTC105
                linear: false,
824bf02b6e55atagen2026-10-16 09:15 UTC106
                allocation_scheme: AllocationScheme::GpuAllocatorManaged,
824bf02b6e55atagen2026-10-16 09:15 UTC107
            });
824bf02b6e55atagen2026-10-16 09:15 UTC108
        let allocation = match allocation {
824bf02b6e55atagen2026-10-16 09:15 UTC109
            Ok(a) => a,
824bf02b6e55atagen2026-10-16 09:15 UTC110
            Err(e) => {
824bf02b6e55atagen2026-10-16 09:15 UTC111
                unsafe { d.destroy_image(image, None) };
824bf02b6e55atagen2026-10-16 09:15 UTC112
                return Err(e.into());
824bf02b6e55atagen2026-10-16 09:15 UTC113
            }
824bf02b6e55atagen2026-10-16 09:15 UTC114
        };
7de49c2aa70batagen2026-10-16 09:27 UTC115
        let memory = Memory::Allocated(allocation);
7de49c2aa70batagen2026-10-16 09:27 UTC116
        let (mem, offset) = match &memory {
7de49c2aa70batagen2026-10-16 09:27 UTC117
            Memory::Allocated(a) => (unsafe { a.memory() }, a.offset()),
7de49c2aa70batagen2026-10-16 09:27 UTC118
            Memory::Imported(m) => (*m, 0),
7de49c2aa70batagen2026-10-16 09:27 UTC119
        };
7de49c2aa70batagen2026-10-16 09:27 UTC120
        Self::finish(
f0e45b059808atagen2026-10-19 07:34 UTC121
            shared, image, memory, mem, offset, fourcc, width, height, usage, false,
7de49c2aa70batagen2026-10-16 09:27 UTC122
        )
7de49c2aa70batagen2026-10-16 09:27 UTC123
    }
7de49c2aa70batagen2026-10-16 09:27 UTC124
7de49c2aa70batagen2026-10-16 09:27 UTC125
    /// Binds memory, creates the view and wraps everything up. Takes
7de49c2aa70batagen2026-10-16 09:27 UTC126
    /// ownership of `image` and `memory`, releasing both on failure.
7de49c2aa70batagen2026-10-16 09:27 UTC127
    #[expect(clippy::too_many_arguments)]
7de49c2aa70batagen2026-10-16 09:27 UTC128
    pub fn finish(
7de49c2aa70batagen2026-10-16 09:27 UTC129
        shared: &Arc<Shared>,
7de49c2aa70batagen2026-10-16 09:27 UTC130
        image: vk::Image,
7de49c2aa70batagen2026-10-16 09:27 UTC131
        memory: Memory,
7de49c2aa70batagen2026-10-16 09:27 UTC132
        mem: vk::DeviceMemory,
7de49c2aa70batagen2026-10-16 09:27 UTC133
        offset: u64,
7de49c2aa70batagen2026-10-16 09:27 UTC134
        fourcc: Fourcc,
7de49c2aa70batagen2026-10-16 09:27 UTC135
        width: u32,
7de49c2aa70batagen2026-10-16 09:27 UTC136
        height: u32,
f0e45b059808atagen2026-10-19 07:34 UTC137
        usage: vk::ImageUsageFlags,
7de49c2aa70batagen2026-10-16 09:27 UTC138
        external: bool,
7de49c2aa70batagen2026-10-16 09:27 UTC139
    ) -> Result<Arc<Self>, Error> {
7de49c2aa70batagen2026-10-16 09:27 UTC140
        let fmt = vk_format(fourcc).ok_or(Error::UnsupportedFormat(fourcc))?;
7de49c2aa70batagen2026-10-16 09:27 UTC141
        let d = &shared.device.device;
7de49c2aa70batagen2026-10-16 09:27 UTC142
        let mut inner = Self {
824bf02b6e55atagen2026-10-16 09:15 UTC143
            shared: shared.clone(),
824bf02b6e55atagen2026-10-16 09:15 UTC144
            image,
7de49c2aa70batagen2026-10-16 09:27 UTC145
            view: vk::ImageView::null(),
7de49c2aa70batagen2026-10-16 09:27 UTC146
            memory: Mutex::new(Some(memory)),
824bf02b6e55atagen2026-10-16 09:15 UTC147
            fourcc,
824bf02b6e55atagen2026-10-16 09:15 UTC148
            format: fmt.format,
824bf02b6e55atagen2026-10-16 09:15 UTC149
            width,
824bf02b6e55atagen2026-10-16 09:15 UTC150
            height,
f0e45b059808atagen2026-10-19 07:34 UTC151
            usage,
7de49c2aa70batagen2026-10-16 09:27 UTC152
            layout: Mutex::new(if external {
7de49c2aa70batagen2026-10-16 09:27 UTC153
                vk::ImageLayout::GENERAL
7de49c2aa70batagen2026-10-16 09:27 UTC154
            } else {
7de49c2aa70batagen2026-10-16 09:27 UTC155
                vk::ImageLayout::UNDEFINED
7de49c2aa70batagen2026-10-16 09:27 UTC156
            }),
7de49c2aa70batagen2026-10-16 09:27 UTC157
            external,
7de49c2aa70batagen2026-10-16 09:27 UTC158
        };
7de49c2aa70batagen2026-10-16 09:27 UTC159
        unsafe { d.bind_image_memory(image, mem, offset)? };
7de49c2aa70batagen2026-10-16 09:27 UTC160
        let view_info = vk::ImageViewCreateInfo::default()
7de49c2aa70batagen2026-10-16 09:27 UTC161
            .image(image)
7de49c2aa70batagen2026-10-16 09:27 UTC162
            .view_type(vk::ImageViewType::TYPE_2D)
7de49c2aa70batagen2026-10-16 09:27 UTC163
            .format(fmt.format)
7de49c2aa70batagen2026-10-16 09:27 UTC164
            .components(components(fmt.opaque))
7de49c2aa70batagen2026-10-16 09:27 UTC165
            .subresource_range(color_range());
7de49c2aa70batagen2026-10-16 09:27 UTC166
        inner.view = unsafe { d.create_image_view(&view_info, None)? };
7de49c2aa70batagen2026-10-16 09:27 UTC167
        Ok(Arc::new(inner))
824bf02b6e55atagen2026-10-16 09:15 UTC168
    }
824bf02b6e55atagen2026-10-16 09:15 UTC169
824bf02b6e55atagen2026-10-16 09:15 UTC170
    pub fn layout(&self) -> vk::ImageLayout {
824bf02b6e55atagen2026-10-16 09:15 UTC171
        *self.layout.lock().unwrap_or_else(|e| e.into_inner())
824bf02b6e55atagen2026-10-16 09:15 UTC172
    }
824bf02b6e55atagen2026-10-16 09:15 UTC173
824bf02b6e55atagen2026-10-16 09:15 UTC174
    pub fn set_layout(&self, layout: vk::ImageLayout) {
824bf02b6e55atagen2026-10-16 09:15 UTC175
        *self.layout.lock().unwrap_or_else(|e| e.into_inner()) = layout;
824bf02b6e55atagen2026-10-16 09:15 UTC176
    }
824bf02b6e55atagen2026-10-16 09:15 UTC177
}
824bf02b6e55atagen2026-10-16 09:15 UTC178
824bf02b6e55atagen2026-10-16 09:15 UTC179
pub(crate) fn color_range() -> vk::ImageSubresourceRange {
824bf02b6e55atagen2026-10-16 09:15 UTC180
    vk::ImageSubresourceRange::default()
824bf02b6e55atagen2026-10-16 09:15 UTC181
        .aspect_mask(vk::ImageAspectFlags::COLOR)
824bf02b6e55atagen2026-10-16 09:15 UTC182
        .level_count(1)
824bf02b6e55atagen2026-10-16 09:15 UTC183
        .layer_count(1)
824bf02b6e55atagen2026-10-16 09:15 UTC184
}
824bf02b6e55atagen2026-10-16 09:15 UTC185
824bf02b6e55atagen2026-10-16 09:15 UTC186
/// A sampled texture.
824bf02b6e55atagen2026-10-16 09:15 UTC187
#[derive(Clone, Debug)]
824bf02b6e55atagen2026-10-16 09:15 UTC188
pub struct VulkanTexture(pub(crate) Arc<ImageInner>);
824bf02b6e55atagen2026-10-16 09:15 UTC189
824bf02b6e55atagen2026-10-16 09:15 UTC190
/// An image the renderer can draw into, from [`smithay::backend::renderer::Offscreen`].
824bf02b6e55atagen2026-10-16 09:15 UTC191
#[derive(Debug)]
824bf02b6e55atagen2026-10-16 09:15 UTC192
pub struct VulkanImage(pub(crate) Arc<ImageInner>);
824bf02b6e55atagen2026-10-16 09:15 UTC193
824bf02b6e55atagen2026-10-16 09:15 UTC194
impl VulkanImage {
824bf02b6e55atagen2026-10-16 09:15 UTC195
    /// The image's contents as a texture, for drawing it elsewhere.
824bf02b6e55atagen2026-10-16 09:15 UTC196
    pub fn texture(&self) -> VulkanTexture {
824bf02b6e55atagen2026-10-16 09:15 UTC197
        VulkanTexture(self.0.clone())
824bf02b6e55atagen2026-10-16 09:15 UTC198
    }
824bf02b6e55atagen2026-10-16 09:15 UTC199
}
824bf02b6e55atagen2026-10-16 09:15 UTC200
7de49c2aa70batagen2026-10-16 09:27 UTC201
/// A bound render target, borrowing whatever it was bound from.
824bf02b6e55atagen2026-10-16 09:15 UTC202
#[derive(Debug)]
7de49c2aa70batagen2026-10-16 09:27 UTC203
pub struct VulkanTarget<'a>(
7de49c2aa70batagen2026-10-16 09:27 UTC204
    pub(crate) Arc<ImageInner>,
7de49c2aa70batagen2026-10-16 09:27 UTC205
    pub(crate) PhantomData<&'a mut ()>,
7de49c2aa70batagen2026-10-16 09:27 UTC206
);
824bf02b6e55atagen2026-10-16 09:15 UTC207
824bf02b6e55atagen2026-10-16 09:15 UTC208
/// Pixels read back from the GPU.
824bf02b6e55atagen2026-10-16 09:15 UTC209
#[derive(Debug)]
824bf02b6e55atagen2026-10-16 09:15 UTC210
pub struct VulkanMapping {
824bf02b6e55atagen2026-10-16 09:15 UTC211
    pub(crate) data: Vec<u8>,
824bf02b6e55atagen2026-10-16 09:15 UTC212
    pub(crate) width: u32,
824bf02b6e55atagen2026-10-16 09:15 UTC213
    pub(crate) height: u32,
824bf02b6e55atagen2026-10-16 09:15 UTC214
    pub(crate) fourcc: Fourcc,
824bf02b6e55atagen2026-10-16 09:15 UTC215
}
824bf02b6e55atagen2026-10-16 09:15 UTC216
824bf02b6e55atagen2026-10-16 09:15 UTC217
macro_rules! texture_impl {
824bf02b6e55atagen2026-10-16 09:15 UTC218
    ($t:ty, |$s:ident| $inner:expr) => {
824bf02b6e55atagen2026-10-16 09:15 UTC219
        impl Texture for $t {
824bf02b6e55atagen2026-10-16 09:15 UTC220
            fn width(&self) -> u32 {
824bf02b6e55atagen2026-10-16 09:15 UTC221
                let $s = self;
824bf02b6e55atagen2026-10-16 09:15 UTC222
                $inner.width
824bf02b6e55atagen2026-10-16 09:15 UTC223
            }
824bf02b6e55atagen2026-10-16 09:15 UTC224
            fn height(&self) -> u32 {
824bf02b6e55atagen2026-10-16 09:15 UTC225
                let $s = self;
824bf02b6e55atagen2026-10-16 09:15 UTC226
                $inner.height
824bf02b6e55atagen2026-10-16 09:15 UTC227
            }
824bf02b6e55atagen2026-10-16 09:15 UTC228
            fn format(&self) -> Option<Fourcc> {
824bf02b6e55atagen2026-10-16 09:15 UTC229
                let $s = self;
824bf02b6e55atagen2026-10-16 09:15 UTC230
                Some($inner.fourcc)
824bf02b6e55atagen2026-10-16 09:15 UTC231
            }
824bf02b6e55atagen2026-10-16 09:15 UTC232
        }
824bf02b6e55atagen2026-10-16 09:15 UTC233
    };
824bf02b6e55atagen2026-10-16 09:15 UTC234
}
824bf02b6e55atagen2026-10-16 09:15 UTC235
824bf02b6e55atagen2026-10-16 09:15 UTC236
texture_impl!(VulkanTexture, |s| s.0);
824bf02b6e55atagen2026-10-16 09:15 UTC237
texture_impl!(VulkanImage, |s| s.0);
7de49c2aa70batagen2026-10-16 09:27 UTC238
texture_impl!(VulkanTarget<'_>, |s| s.0);
824bf02b6e55atagen2026-10-16 09:15 UTC239
texture_impl!(VulkanMapping, |s| s);
824bf02b6e55atagen2026-10-16 09:15 UTC240
824bf02b6e55atagen2026-10-16 09:15 UTC241
impl TextureMapping for VulkanMapping {
824bf02b6e55atagen2026-10-16 09:15 UTC242
    fn flipped(&self) -> bool {
824bf02b6e55atagen2026-10-16 09:15 UTC243
        false
824bf02b6e55atagen2026-10-16 09:15 UTC244
    }
824bf02b6e55atagen2026-10-16 09:15 UTC245
}