render: Add AsBindableDescriptorSet

This commit is contained in:
Florian RICHER 2025-06-05 13:30:55 +02:00
parent b7bc6478e2
commit 5539381f46
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
4 changed files with 115 additions and 47 deletions

View file

@ -1,10 +1,15 @@
use std::{path::Path, sync::Arc};
use std::{collections::BTreeMap, error::Error, sync::Arc};
use anyhow::Error;
use image::{DynamicImage, EncodableLayout};
use image::DynamicImage;
use vulkano::{
Validated, VulkanError,
buffer::{Buffer, BufferCreateInfo, BufferUsage},
command_buffer::{AutoCommandBufferBuilder, CopyBufferToImageInfo, PrimaryAutoCommandBuffer},
descriptor_set::{
DescriptorSet, WriteDescriptorSet,
allocator::StandardDescriptorSetAllocator,
layout::{DescriptorSetLayout, DescriptorSetLayoutBinding, DescriptorType},
},
device::Device,
format::Format,
image::{
@ -13,8 +18,11 @@ use vulkano::{
view::ImageView,
},
memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator},
shader::ShaderStages,
};
use crate::core::render::primitives::AsBindableDescriptorSet;
pub struct Texture {
texture: Arc<ImageView>,
sampler: Arc<Sampler>,
@ -30,7 +38,7 @@ impl Texture {
memory_allocator: &Arc<StandardMemoryAllocator>,
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
path: &str,
) -> Result<Self, Error> {
) -> Result<Self, Box<dyn Error>> {
let _span = tracing::info_span!("texture_load_from_file", path = path);
let bytes = std::fs::read(path)?;
@ -42,7 +50,7 @@ impl Texture {
memory_allocator: &Arc<StandardMemoryAllocator>,
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
bytes: &[u8],
) -> Result<Self, Error> {
) -> Result<Self, Box<dyn Error>> {
let image = image::load_from_memory(bytes)?;
Self::from_dynamic_image(device, memory_allocator, builder, image)
}
@ -52,7 +60,7 @@ impl Texture {
memory_allocator: &Arc<StandardMemoryAllocator>,
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
image: DynamicImage,
) -> Result<Self, Error> {
) -> Result<Self, Box<dyn Error>> {
let _span = tracing::info_span!("texture_from_dynamic_image");
let image_data = image.to_rgba8();
@ -121,3 +129,40 @@ impl Texture {
&self.sampler
}
}
impl AsBindableDescriptorSet<Texture> for Texture {
fn as_descriptor_set_layout_bindings() -> BTreeMap<u32, DescriptorSetLayoutBinding> {
BTreeMap::<u32, DescriptorSetLayoutBinding>::from_iter([
(
0,
DescriptorSetLayoutBinding {
stages: ShaderStages::FRAGMENT,
..DescriptorSetLayoutBinding::descriptor_type(DescriptorType::Sampler)
},
),
(
1,
DescriptorSetLayoutBinding {
stages: ShaderStages::FRAGMENT,
..DescriptorSetLayoutBinding::descriptor_type(DescriptorType::SampledImage)
},
),
])
}
fn as_descriptor_set(
descriptor_set_allocator: &Arc<StandardDescriptorSetAllocator>,
layout: &Arc<DescriptorSetLayout>,
data: &Texture,
) -> Result<Arc<DescriptorSet>, Validated<VulkanError>> {
DescriptorSet::new(
descriptor_set_allocator.clone(),
layout.clone(),
[
WriteDescriptorSet::sampler(0, data.sampler.clone()),
WriteDescriptorSet::image_view(1, data.texture.clone()),
],
[],
)
}
}