Skip to content

Commit

Permalink
Add padding to fix alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenhuyn committed Apr 30, 2023
1 parent 3f3e2f4 commit b01657b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cubeway/src/compute.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct SimParams {

struct Instance {
position: vec3<f32>,
unused: f32,
rotation: vec4<f32>,
}

Expand All @@ -17,9 +18,8 @@ struct Instances {
fn main(@builtin(global_invocation_id) GlobalInvocationID: vec3<u32>) {
var index = GlobalInvocationID.x;

instanceBuffer[index * 7u].position.y -= 0.001;

if index < 100u {
let bufferIndex = u32(f32(index) * 7.0);
instanceBuffer[index].position.y -= 0.001;
}
}
17 changes: 5 additions & 12 deletions cubeway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Vertex {
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Instance {
position: [f32; 3],
_pad: f32,
rotation: [f32; 4],
}

Expand All @@ -75,7 +76,7 @@ impl Instance {
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
offset: 16 as wgpu::BufferAddress,
shader_location: 3,
format: wgpu::VertexFormat::Float32x4,
},
Expand Down Expand Up @@ -195,20 +196,12 @@ impl State {
z: z as f32,
} - INSTANCE_DISPLACEMENT;

let rotation = if position.is_zero() {
// this is needed so an object at (0, 0, 0) won't get scaled to zero
// as Quaternions can effect scale if they're not created correctly
cgmath::Quaternion::from_axis_angle(
cgmath::Vector3::unit_z(),
cgmath::Deg(0.0),
)
} else {
cgmath::Quaternion::from_axis_angle(position.normalize(), cgmath::Deg(45.0))
};
// let rotation = cgmath::Quaternion::zero();

Instance {
position: position.into(),
rotation: rotation.into(),
_pad: 0.0,
rotation: [1., 0., 0., 0.],
}
})
})
Expand Down

0 comments on commit b01657b

Please sign in to comment.