texture: First image load
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Has been cancelled

This commit is contained in:
Florian RICHER 2025-05-27 22:25:17 +02:00
parent 5b0ab19207
commit 29a4da5666
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
11 changed files with 220 additions and 72 deletions

View file

@ -1,4 +1,5 @@
pub mod pipelines;
pub mod primitives;
pub mod render_context;
pub mod texture;
pub mod vulkan_context;

View file

@ -8,7 +8,7 @@ use vulkano::device::Device;
use vulkano::format::Format;
use vulkano::pipeline::graphics::GraphicsPipelineCreateInfo;
use vulkano::pipeline::graphics::color_blend::{ColorBlendAttachmentState, ColorBlendState};
use vulkano::pipeline::graphics::input_assembly::InputAssemblyState;
use vulkano::pipeline::graphics::input_assembly::{InputAssemblyState, PrimitiveTopology};
use vulkano::pipeline::graphics::multisample::MultisampleState;
use vulkano::pipeline::graphics::rasterization::RasterizationState;
use vulkano::pipeline::graphics::subpass::PipelineRenderingCreateInfo;
@ -52,19 +52,42 @@ pub fn create_triangle_pipeline(
PipelineShaderStageCreateInfo::new(fs),
];
let mut bindings = BTreeMap::<u32, DescriptorSetLayoutBinding>::new();
let mut descriptor_set_layout_binding =
DescriptorSetLayoutBinding::descriptor_type(DescriptorType::UniformBuffer);
descriptor_set_layout_binding.stages = ShaderStages::VERTEX;
bindings.insert(0, descriptor_set_layout_binding);
let vertex_bindings = BTreeMap::<u32, DescriptorSetLayoutBinding>::from_iter([(
0,
DescriptorSetLayoutBinding {
stages: ShaderStages::VERTEX,
..DescriptorSetLayoutBinding::descriptor_type(DescriptorType::UniformBuffer)
},
)]);
let fragment_bindings = 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)
},
),
]);
let descriptor_set_layout = DescriptorSetLayoutCreateInfo {
bindings,
let vertex_descriptor_set_layout = DescriptorSetLayoutCreateInfo {
bindings: vertex_bindings,
..Default::default()
};
let fragment_descriptor_set_layout = DescriptorSetLayoutCreateInfo {
bindings: fragment_bindings,
..Default::default()
};
let create_info = PipelineDescriptorSetLayoutCreateInfo {
set_layouts: vec![descriptor_set_layout],
set_layouts: vec![vertex_descriptor_set_layout, fragment_descriptor_set_layout],
flags: PipelineLayoutCreateFlags::default(),
push_constant_ranges: vec![],
}
@ -83,7 +106,10 @@ pub fn create_triangle_pipeline(
GraphicsPipelineCreateInfo {
stages: stages.into_iter().collect(),
vertex_input_state: Some(vertex_input_state),
input_assembly_state: Some(InputAssemblyState::default()),
input_assembly_state: Some(InputAssemblyState {
topology: PrimitiveTopology::TriangleStrip,
..Default::default()
}),
viewport_state: Some(ViewportState::default()),
rasterization_state: Some(RasterizationState::default()),
multisample_state: Some(MultisampleState::default()),

View file

@ -12,8 +12,8 @@ pub struct Vertex2D {
#[format(R32G32_SFLOAT)]
pub position: [f32; 2],
#[format(R32G32B32_SFLOAT)]
pub color: [f32; 3],
#[format(R32G32_SFLOAT)]
pub uv: [f32; 2],
}
impl Vertex2D {

113
src/core/render/texture.rs Normal file
View file

@ -0,0 +1,113 @@
use std::{path::Path, sync::Arc};
use anyhow::Error;
use image::{DynamicImage, EncodableLayout};
use vulkano::{
buffer::{Buffer, BufferCreateInfo, BufferUsage},
command_buffer::{AutoCommandBufferBuilder, CopyBufferToImageInfo, PrimaryAutoCommandBuffer},
device::Device,
format::Format,
image::{
Image, ImageCreateInfo, ImageType, ImageUsage,
sampler::{Filter, Sampler, SamplerAddressMode, SamplerCreateInfo},
view::ImageView,
},
memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator},
};
pub struct Texture {
texture: Arc<ImageView>,
sampler: Arc<Sampler>,
}
impl Texture {
fn new(texture: Arc<ImageView>, sampler: Arc<Sampler>) -> Self {
Self { texture, sampler }
}
pub fn from_file(
device: &Arc<Device>,
memory_allocator: &Arc<StandardMemoryAllocator>,
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
path: &str,
) -> Result<Self, Error> {
let image = image::open(path)?;
Self::from_dynamic_image(device, memory_allocator, builder, image)
}
pub fn from_bytes(
device: &Arc<Device>,
memory_allocator: &Arc<StandardMemoryAllocator>,
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
bytes: &[u8],
) -> Result<Self, Error> {
let image = image::load_from_memory(bytes)?;
Self::from_dynamic_image(device, memory_allocator, builder, image)
}
pub fn from_dynamic_image(
device: &Arc<Device>,
memory_allocator: &Arc<StandardMemoryAllocator>,
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
image: DynamicImage,
) -> Result<Self, Error> {
let image_data = image.to_rgba8();
let image_dimensions = image_data.dimensions();
let upload_buffer = Buffer::from_iter(
memory_allocator.clone(),
BufferCreateInfo {
usage: BufferUsage::TRANSFER_SRC,
..Default::default()
},
AllocationCreateInfo {
memory_type_filter: MemoryTypeFilter::PREFER_HOST
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
..Default::default()
},
image_data.to_vec(),
)?;
let image = Image::new(
memory_allocator.clone(),
ImageCreateInfo {
image_type: ImageType::Dim2d,
format: Format::R8G8B8A8_SRGB,
extent: [image_dimensions.0 as u32, image_dimensions.1 as u32, 1],
array_layers: 1,
usage: ImageUsage::TRANSFER_DST | ImageUsage::SAMPLED,
..Default::default()
},
AllocationCreateInfo::default(),
)?;
builder.copy_buffer_to_image(CopyBufferToImageInfo::buffer_image(
upload_buffer,
image.clone(),
))?;
let sampler = Sampler::new(
device.clone(),
SamplerCreateInfo {
mag_filter: Filter::Linear,
min_filter: Filter::Linear,
address_mode: [SamplerAddressMode::Repeat; 3],
..Default::default()
},
)?;
let image_view = ImageView::new_default(image)?;
log::trace!("Texture loaded with dimensions {:?}", image_dimensions);
Ok(Self::new(image_view, sampler))
}
pub fn get_texture(&self) -> &Arc<ImageView> {
&self.texture
}
pub fn get_sampler(&self) -> &Arc<Sampler> {
&self.sampler
}
}