This repository has been archived on 2023-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
eruption/src/vulkan/textured_quad.rs

83 lines
2.3 KiB
Rust

/// This crate will utilize a textured quad to draw a texture onto a surface
use vulkano::buffer::{Buffer, BufferContents, BufferCreateInfo, BufferUsage, Subbuffer};
use vulkano::memory::allocator::{AllocationCreateInfo, MemoryUsage, StandardMemoryAllocator};
use vulkano::pipeline::graphics::vertex_input::Vertex;
// We now create a buffer that will store the shape of our triangle. We use `#[repr(C)]` here
// to force rustc to use a defined layout for our data, as the default representation has *no
// guarantees*.
#[derive(BufferContents, Vertex)]
#[repr(C)]
pub struct TexturedVertex {
// vertex coordinates in object space
// in this case also equals to view space
#[format(R32G32_SFLOAT)]
position: [f32; 2],
// uv coordinates per vertex
#[format(R32G32_SFLOAT)]
texture: [f32; 2]
}
fn textured_quad() -> (Vec<TexturedVertex>, Vec<u32>) {
(
// vertex list
vec![
TexturedVertex {
position: [-1.0, -1.0],
texture: [0.0, 0.0]
},
TexturedVertex {
position: [1.0, -1.0],
texture: [1.0, 0.0]
},
TexturedVertex {
position: [1.0, 1.0],
texture: [1.0, 1.0]
},
TexturedVertex {
position: [-1.0, 1.0],
texture: [0.0, 1.0]
}
],
// indices list
vec![
0, 1, 2,
0, 2, 3
]
)
}
pub fn create_quad_buffer(memory_allocator: &StandardMemoryAllocator) -> (Subbuffer<[TexturedVertex]>, Subbuffer<[u32]>) {
let (vertices, indices) = textured_quad();
let vertex_buffer = Buffer::from_iter(
memory_allocator,
BufferCreateInfo {
usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
AllocationCreateInfo {
usage: MemoryUsage::Upload,
..Default::default()
},
vertices,
).unwrap();
let index_buffer = Buffer::from_iter(
memory_allocator,
BufferCreateInfo {
usage: BufferUsage::INDEX_BUFFER,
..Default::default()
},
AllocationCreateInfo {
usage: MemoryUsage::Upload,
..Default::default()
},
indices,
).unwrap();
(vertex_buffer, index_buffer)
}