From 83295e01008bdf25e03f6b5aa18b93b735a5e326 Mon Sep 17 00:00:00 2001 From: Micha White Date: Sun, 18 Sep 2022 16:45:05 -0400 Subject: instancing --- src/instance.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/instance.rs (limited to 'src/instance.rs') diff --git a/src/instance.rs b/src/instance.rs new file mode 100644 index 0000000..6de0133 --- /dev/null +++ b/src/instance.rs @@ -0,0 +1,46 @@ +use std::mem::size_of; + +use bytemuck::{Pod, Zeroable}; + +#[repr(C)] +#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)] +pub struct Instance { + /// Position on the screen + pub position: [f32; 2], + /// Relative size + pub size: [f32; 2], + /// Rotation, in radians + pub rotation: f32, + /// z-index + pub z_index: i32, +} + +impl Default for Instance { + fn default() -> Self { + Self { + position: [0.0; 2], + size: [1.0; 2], + rotation: 0.0, + z_index: 0, + } + } +} + +impl Instance { + // whenever this is updated, please also update `sprite.wgsl` + const ATTRIBUTES: [wgpu::VertexAttribute; 4] = + wgpu::vertex_attr_array![1 => Float32x2, 2 => Float32x2, 3 => Float32, 4 => Sint32]; + + pub(crate) fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { + // make sure these two don't conflict + debug_assert_eq!( + Self::ATTRIBUTES[0].shader_location as usize, + crate::Vertex::ATTRIBUTES.len() + ); + wgpu::VertexBufferLayout { + array_stride: size_of::() as wgpu::BufferAddress, + step_mode: wgpu::VertexStepMode::Instance, + attributes: &Self::ATTRIBUTES, + } + } +} -- cgit v1.2.3