Skip to content

Commit

Permalink
Raymarching: set camera center to vec4 to avoid padding
Browse files Browse the repository at this point in the history
  • Loading branch information
Azkellas committed Sep 29, 2023
1 parent 208b744 commit 101ba42
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/src/camera_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::mouse_input::MouseState;
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct CameraLookAt {
// Object the camera is looking at.
pub center: [f32; 3],
// Object the camera is looking at. Use a vec4 to avoid padding.
pub center: [f32; 4],
// Angle around the object on the horizontal plane, in radians.
pub angle: f32,
// Height between -1 and 1, 0 is flat, 1 is zenith, -1 is nadir
Expand All @@ -23,7 +23,7 @@ impl Default for CameraLookAt {
fn default() -> Self {
// See object in 0,0,0 from the front top left
CameraLookAt {
center: [0.0, 0.0, 0.0],
center: [0.0, 0.0, 0.0, 0.0],
angle: 2.0 * std::f32::consts::FRAC_PI_3,
height: 0.3,
distance: f32::sqrt(72.0),
Expand Down
13 changes: 4 additions & 9 deletions lib/src/demo_raymarching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct DemoRaymarchingSettings {
pub camera: CameraLookAt,
pub size: [f32; 2],
pub elapsed: f32, // elapsed take the speed into consideration
_padding: [f32; 3], // padding for alignment
_padding: [f32; 2], // padding for alignment
}

/// Demo raymarching program.
Expand All @@ -68,18 +68,13 @@ impl DemoRaymarchingSettings {
pub fn new() -> Self {
Self {
camera: CameraLookAt::default(),
size: [0.0, 0.0],
elapsed: 0.0,
_padding: [0.0; 3],
size: [0.0, 0.0],
_padding: [0.0; 2],
}
}

pub fn get_size() -> u64 {
dbg!(
"DemoRaymarchingSettings::get_size",
std::mem::size_of::<Self>(),
(bytemuck::cast_slice(&[Self::new()]) as &[u8]).len()
);
std::mem::size_of::<Self>() as _
}
}
Expand Down Expand Up @@ -215,7 +210,7 @@ impl DemoRaymarchingProgram {
});

let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: None,
label: Some("Raymarching Render Pipeline"),
layout: Some(&layout),
vertex: wgpu::VertexState {
module: &shader,
Expand Down
2 changes: 1 addition & 1 deletion shaders/demo_raymarching/common.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vertex shader
struct Uniforms {
camera_center: vec3<f32>,
camera_center: vec4<f32>,
camera_angle: f32,
camera_height: f32,
camera_distance: f32,
Expand Down
2 changes: 1 addition & 1 deletion shaders/demo_raymarching/draw_3d.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn sdf_3d(p: vec2<f32>) -> vec4<f32> {
var time: f32 = uniforms.elapsed;

// camera look at.
let look_at: vec3<f32> = uniforms.camera_center;
let look_at: vec3<f32> = uniforms.camera_center.xyz;

// compute direction.
var angle: vec3<f32> = vec3(cos(uniforms.camera_angle), 0.0, sin(uniforms.camera_angle));
Expand Down

0 comments on commit 101ba42

Please sign in to comment.