Add instances support
This commit is contained in:
parent
f8b81f3269
commit
77c717f90b
7 changed files with 158 additions and 31 deletions
|
@ -1,9 +1,26 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use glam::{Mat4, Quat, Vec3};
|
||||
use vulkano::{
|
||||
Validated,
|
||||
buffer::{
|
||||
AllocateBufferError, Buffer, BufferContents, BufferCreateInfo, BufferUsage, Subbuffer,
|
||||
},
|
||||
memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator},
|
||||
pipeline::graphics::vertex_input::Vertex,
|
||||
};
|
||||
|
||||
pub struct Transform {
|
||||
position: Vec3,
|
||||
rotation: Quat,
|
||||
scale: Vec3,
|
||||
pub position: Vec3,
|
||||
pub rotation: Quat,
|
||||
pub scale: Vec3,
|
||||
}
|
||||
|
||||
#[derive(BufferContents, Vertex)]
|
||||
#[repr(C)]
|
||||
pub struct TransformRaw {
|
||||
#[format(R32G32B32A32_SFLOAT)]
|
||||
pub model: [[f32; 4]; 4],
|
||||
}
|
||||
|
||||
impl Default for Transform {
|
||||
|
@ -29,9 +46,35 @@ impl Transform {
|
|||
self.scale *= scale;
|
||||
}
|
||||
|
||||
pub fn get_mat4(&self) -> Mat4 {
|
||||
Mat4::from_translation(self.position)
|
||||
* Mat4::from_quat(self.rotation)
|
||||
* Mat4::from_scale(self.scale)
|
||||
pub fn into_raw(&self) -> TransformRaw {
|
||||
TransformRaw {
|
||||
model: (Mat4::from_translation(self.position)
|
||||
* Mat4::from_quat(self.rotation)
|
||||
* Mat4::from_scale(self.scale))
|
||||
.to_cols_array_2d(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_buffer(
|
||||
memory_allocator: &Arc<StandardMemoryAllocator>,
|
||||
transforms: &[Transform],
|
||||
) -> Result<Subbuffer<[TransformRaw]>, Validated<AllocateBufferError>> {
|
||||
let transform_raws: Vec<TransformRaw> = transforms.iter().map(|t| t.into_raw()).collect();
|
||||
|
||||
let buffer = 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()
|
||||
},
|
||||
transform_raws,
|
||||
)?;
|
||||
|
||||
Ok(buffer)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,3 +10,13 @@ pub struct Vertex2D {
|
|||
#[format(R32G32_SFLOAT)]
|
||||
pub uv: [f32; 2],
|
||||
}
|
||||
|
||||
#[derive(BufferContents, Vertex)]
|
||||
#[repr(C)]
|
||||
pub struct Vertex3D {
|
||||
#[format(R32G32B32_SFLOAT)]
|
||||
pub position: [f32; 3],
|
||||
|
||||
#[format(R32G32_SFLOAT)]
|
||||
pub uv: [f32; 2],
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue