pub mod bindless;
pub mod texture_loader;
use {
crate::{
graphics::vulkan::{raii, OwnedBlock, VulkanContext},
trace,
},
anyhow::{Context, Result},
ash::vk,
bon::bon,
};
#[derive(Debug)]
pub struct Texture {
width: u32,
height: u32,
image_view: raii::ImageView,
image: raii::Image,
block: OwnedBlock,
}
#[bon]
impl Texture {
#[builder]
pub fn new(
ctx: &VulkanContext,
dimensions: (u32, u32),
format: vk::Format,
usage: vk::ImageUsageFlags,
memory_property_flags: vk::MemoryPropertyFlags,
) -> Result<Self> {
let (width, height) = dimensions;
let (block, image) = OwnedBlock::allocate_image(
ctx.allocator.clone(),
&vk::ImageCreateInfo {
flags: vk::ImageCreateFlags::empty(),
image_type: vk::ImageType::TYPE_2D,
format,
extent: vk::Extent3D {
width,
height,
depth: 1,
},
mip_levels: 1,
array_layers: 1,
samples: vk::SampleCountFlags::TYPE_1,
tiling: vk::ImageTiling::OPTIMAL,
usage,
sharing_mode: vk::SharingMode::EXCLUSIVE,
queue_family_index_count: 1,
p_queue_family_indices: &ctx.graphics_queue_family_index,
initial_layout: vk::ImageLayout::UNDEFINED,
..Default::default()
},
memory_property_flags,
)
.with_context(trace!("Error while creating image"))?;
let image_view = raii::ImageView::new(
ctx.device.clone(),
&vk::ImageViewCreateInfo {
flags: vk::ImageViewCreateFlags::empty(),
image: image.raw,
view_type: vk::ImageViewType::TYPE_2D,
format,
components: vk::ComponentMapping::default(),
subresource_range: vk::ImageSubresourceRange {
aspect_mask: vk::ImageAspectFlags::COLOR,
base_mip_level: 0,
level_count: 1,
base_array_layer: 0,
layer_count: 1,
},
..Default::default()
},
)
.with_context(trace!("Unable to create image view!"))?;
Ok(Self {
width,
height,
image_view,
image,
block,
})
}
pub fn image(&self) -> vk::Image {
self.image.raw
}
pub fn view(&self) -> vk::ImageView {
self.image_view.raw
}
pub fn memory(&self) -> vk::DeviceMemory {
self.block.memory()
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
pub fn extent(&self) -> vk::Extent2D {
vk::Extent2D {
width: self.width(),
height: self.height(),
}
}
}