summaryrefslogtreecommitdiff
path: root/src/instance.rs
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2022-09-18 16:45:05 -0400
committerMicha White <botahamec@outlook.com>2022-09-18 16:45:05 -0400
commit83295e01008bdf25e03f6b5aa18b93b735a5e326 (patch)
tree7010e0f2fb8994a7168e3a11b4fa996804a49342 /src/instance.rs
parent3e913d329ebdb36b66ccb8ac7ccea203db266d20 (diff)
instancing
Diffstat (limited to 'src/instance.rs')
-rw-r--r--src/instance.rs46
1 files changed, 46 insertions, 0 deletions
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::<Self>() as wgpu::BufferAddress,
+ step_mode: wgpu::VertexStepMode::Instance,
+ attributes: &Self::ATTRIBUTES,
+ }
+ }
+}