Refactor vertex buffer creations

This commit is contained in:
Florian RICHER 2024-12-11 20:41:04 +01:00
parent 1169c76b41
commit 11a5083513
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
7 changed files with 132 additions and 108 deletions

View file

@ -1,12 +1,67 @@
use std::sync::Arc;
use vulkano::buffer::{Buffer, BufferCreateInfo, BufferUsage, Subbuffer};
use vulkano::buffer::Subbuffer;
use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer};
use vulkano::device::Device;
use vulkano::memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator};
use vulkano::memory::allocator::StandardMemoryAllocator;
use vulkano::pipeline::GraphicsPipeline;
use vulkano::swapchain::Swapchain;
use crate::renderer::{Vertex2D, create_triangle_pipeline};
use crate::renderer::{create_triangle_pipeline, Vertex2D};
const VERTICES: [Vertex2D; 12] = [
// Triangle en haut à gauche
Vertex2D {
position: [-0.5, -0.75],
color: [1.0, 0.0, 0.0],
},
Vertex2D {
position: [-0.75, -0.25],
color: [0.0, 1.0, 0.0],
},
Vertex2D {
position: [-0.25, -0.25],
color: [0.0, 0.0, 1.0],
},
// Triangle en bas à gauche
Vertex2D {
position: [-0.5, 0.25],
color: [0.5, 0.5, 0.5],
},
Vertex2D {
position: [-0.75, 0.75],
color: [0.2, 0.8, 0.2],
},
Vertex2D {
position: [-0.25, 0.75],
color: [0.8, 0.2, 0.2],
},
// Triangle en haut à droite
Vertex2D {
position: [0.5, -0.75],
color: [1.0, 1.0, 0.0],
},
Vertex2D {
position: [0.25, -0.25],
color: [0.0, 1.0, 1.0],
},
Vertex2D {
position: [0.75, -0.25],
color: [1.0, 0.0, 1.0],
},
// Triangle en bas à droite
Vertex2D {
position: [0.5, 0.25],
color: [0.1, 0.5, 0.8],
},
Vertex2D {
position: [0.25, 0.75],
color: [0.8, 0.6, 0.1],
},
Vertex2D {
position: [0.75, 0.75],
color: [0.3, 0.4, 0.6],
},
];
pub struct Scene {
pipeline: Arc<GraphicsPipeline>,
@ -14,66 +69,14 @@ pub struct Scene {
}
impl Scene {
fn create_vertex_buffer(
memory_allocator: &Arc<StandardMemoryAllocator>,
) -> Subbuffer<[Vertex2D]> {
let vertices = [
// Triangle en haut à gauche
Vertex2D::new([-0.5, -0.75], // Coin supérieur gauche
[1.0, 0.0, 0.0]), // Couleur rouge
Vertex2D::new([-0.75, -0.25], // Coin supérieur
[0.0, 1.0, 0.0]), // Couleur verte
Vertex2D::new([-0.25, -0.25], // Coin supérieur droit
[0.0, 0.0, 1.0]), // Couleur bleue
// Triangle en bas à gauche
Vertex2D::new([-0.5, 0.25], // Coin inférieur gauche
[0.5, 0.5, 0.5]), // Couleur gris
Vertex2D::new([-0.75, 0.75], // Coin inférieur
[0.2, 0.8, 0.2]), // Couleur vert clair
Vertex2D::new([-0.25, 0.75], // Coin inférieur droit
[0.8, 0.2, 0.2]), // Couleur rouge clair
// Triangle en haut à droite
Vertex2D::new([0.5, -0.75], // Coin gauche supérieur
[1.0, 1.0, 0.0]), // Couleur jaune
Vertex2D::new([0.25, -0.25], // Coin gauche
[0.0, 1.0, 1.0]), // Couleur cyan
Vertex2D::new([0.75, -0.25], // Coin gauche inférieur
[1.0, 0.0, 1.0]), // Couleur magenta
// Triangle en bas à droite
Vertex2D::new([0.5, 0.25], // Coin droit supérieur
[0.1, 0.5, 0.8]), // Couleur bleu clair
Vertex2D::new([0.25, 0.75], // Coin droit
[0.8, 0.6, 0.1]), // Couleur or
Vertex2D::new([0.75, 0.75], // Coin droit inférieur
[0.3, 0.4, 0.6]), // Couleur bleu foncé
];
Buffer::from_iter(
memory_allocator.clone(),
BufferCreateInfo {
usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
AllocationCreateInfo {
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
..Default::default()
},
vertices,
)
.unwrap()
}
pub fn initialize(
device: &Arc<Device>,
swapchain: &Arc<Swapchain>,
memory_allocator: &Arc<StandardMemoryAllocator>,
) -> Scene {
let pipeline = create_triangle_pipeline(device, swapchain);
let vertex_buffer = Self::create_vertex_buffer(memory_allocator);
let vertex_buffer =
Vertex2D::create_buffer(Vec::from_iter(VERTICES), memory_allocator).unwrap();
Scene {
pipeline,
@ -93,4 +96,4 @@ impl Scene {
unsafe { builder.draw(vertex_count, instance_count, 0, 0) }.unwrap();
}
}
}