shdrlib 0.1.5

A three-tiered Vulkan shader compilation and rendering framework built in pure Rust
Documentation
//! EZ-tier resource wrappers with automatic cleanup
//!
//! Unlike CORE tier resources which require manual `.destroy()` calls,
//! EZ-tier wrappers automatically clean up GPU resources when dropped.

use crate::core::{Buffer as CoreBuffer, Device, Image as CoreImage};
use ash::vk;
use std::sync::Arc;

/// Buffer wrapper with automatic cleanup
///
/// Wraps a CORE `Buffer` and automatically destroys it when dropped.
/// No need to call `.destroy()` manually!
///
/// # Example
///
/// ```rust,no_run
/// use shdrlib::ez::*;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut renderer = EzRenderer::new()?;
///
/// // Create a buffer - cleanup is automatic!
/// let buffer = renderer.create_vertex_buffer(&[1.0f32, 2.0, 3.0])?;
///
/// // Use the buffer...
/// // No cleanup needed - Drop handles it!
/// # Ok(())
/// # }
/// ```
pub struct EzBuffer {
    device: Arc<Device>,
    buffer: CoreBuffer,
}

impl EzBuffer {
    /// Create a new EzBuffer (internal use)
    pub(crate) fn new(device: Arc<Device>, buffer: CoreBuffer) -> Self {
        Self { device, buffer }
    }

    /// Get the raw Vulkan buffer handle
    #[inline]
    pub fn handle(&self) -> vk::Buffer {
        self.buffer.handle()
    }

    /// Get the buffer size in bytes
    #[inline]
    pub fn size(&self) -> vk::DeviceSize {
        self.buffer.size()
    }

    /// Get the buffer usage flags
    #[inline]
    pub fn usage(&self) -> vk::BufferUsageFlags {
        self.buffer.usage()
    }

    /// Get a reference to the underlying CORE buffer (for advanced usage)
    #[inline]
    pub fn core_buffer(&self) -> &CoreBuffer {
        &self.buffer
    }
}

impl Drop for EzBuffer {
    fn drop(&mut self) {
        self.buffer.destroy(&self.device);
    }
}

/// Image wrapper with automatic cleanup
///
/// Wraps a CORE `Image` and automatically destroys it when dropped.
/// No need to call `.destroy()` manually!
///
/// # Example
///
/// ```rust,no_run
/// use shdrlib::ez::*;
/// use ash::vk;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut renderer = EzRenderer::new()?;
///
/// // Create a texture - cleanup is automatic!
/// let texture = renderer.create_texture(512, 512, vk::Format::R8G8B8A8_UNORM)?;
///
/// // Use the texture...
/// // No cleanup needed - Drop handles it!
/// # Ok(())
/// # }
/// ```
pub struct EzImage {
    device: Arc<Device>,
    image: CoreImage,
}

impl EzImage {
    /// Create a new EzImage (internal use)
    pub(crate) fn new(device: Arc<Device>, image: CoreImage) -> Self {
        Self { device, image }
    }

    /// Get the raw Vulkan image handle
    #[inline]
    pub fn handle(&self) -> vk::Image {
        self.image.handle()
    }

    /// Get the image view handle
    #[inline]
    pub fn view(&self) -> vk::ImageView {
        self.image.view()
    }

    /// Get the image extent
    #[inline]
    pub fn extent(&self) -> vk::Extent3D {
        self.image.extent()
    }

    /// Get the image format
    #[inline]
    pub fn format(&self) -> vk::Format {
        self.image.format()
    }

    /// Get the image usage flags
    #[inline]
    pub fn usage(&self) -> vk::ImageUsageFlags {
        self.image.usage()
    }

    /// Get a reference to the underlying CORE image (for advanced usage)
    #[inline]
    pub fn core_image(&self) -> &CoreImage {
        &self.image
    }
}

impl Drop for EzImage {
    fn drop(&mut self) {
        self.image.destroy(&self.device);
    }
}