summaryrefslogtreecommitdiff
path: root/src/instance.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/instance.rs')
-rw-r--r--src/instance.rs12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/instance.rs b/src/instance.rs
index bacad56..2bff797 100644
--- a/src/instance.rs
+++ b/src/instance.rs
@@ -21,7 +21,7 @@ pub struct Instance {
/// Rotation, in radians
pub rotation: f32,
/// z-index
- pub z_index: u32, // TODO something more interesting with z-axis
+ pub z_index: f32,
}
impl Default for Instance {
@@ -30,7 +30,7 @@ impl Default for Instance {
position: [0.0; 2],
size: [1.0; 2],
rotation: 0.0,
- z_index: 0,
+ z_index: 0.0,
texture_coordinates: [0.0; 2],
texture_size: [1.0; 2],
texture_atlas_index: 0,
@@ -42,7 +42,7 @@ impl Instance {
// whenever this is updated, please also update `sprite.wgsl`
const ATTRIBUTES: [wgpu::VertexAttribute; 7] = wgpu::vertex_attr_array![
1 => Float32x2, 2 => Float32x2, 3 => Float32x2, 4 => Float32x2,
- 5 => Uint32, 6 => Float32, 7 => Uint32
+ 5 => Uint32, 6 => Float32, 7 => Float32
];
pub(crate) fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
@@ -135,10 +135,14 @@ impl InstanceBuffer {
self.expand_buffer(device);
}
+ // the instances must be sorted by z-index before being handed to the GPU
+ let mut sorted = self.instances.clone();
+ sorted.sort_by(|a, b| a.z_index.total_cmp(&b.z_index));
+
queue.write_buffer(
&self.instance_buffer,
0 as wgpu::BufferAddress,
- bytemuck::cast_slice(&self.instances),
+ bytemuck::cast_slice(&sorted),
);
}
}