diff --git a/code/intermediate/tutorial10-lighting/src/main.rs b/code/intermediate/tutorial10-lighting/src/main.rs index ff8c30f5..acfa856d 100644 --- a/code/intermediate/tutorial10-lighting/src/main.rs +++ b/code/intermediate/tutorial10-lighting/src/main.rs @@ -190,17 +190,14 @@ unsafe impl bytemuck::Pod for InstanceRaw {} unsafe impl bytemuck::Zeroable for InstanceRaw {} #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] struct Light { - position: cgmath::Vector3, + position: [f32; 3], // Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here _padding: u32, - color: cgmath::Vector3, + color: [f32; 3], } -unsafe impl bytemuck::Zeroable for Light {} -unsafe impl bytemuck::Pod for Light {} - struct State { surface: wgpu::Surface, device: wgpu::Device, @@ -448,9 +445,9 @@ impl State { .unwrap(); let light = Light { - position: (2.0, 2.0, 2.0).into(), + position: [2.0, 2.0, 2.0], _padding: 0, - color: (1.0, 1.0, 1.0).into(), + color: [1.0, 1.0, 1.0], }; let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { @@ -572,10 +569,10 @@ impl State { ); // Update the light - let old_position = self.light.position; + let old_position: cgmath::Vector3<_> = self.light.position.into(); self.light.position = - cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0)) - * old_position; + (cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0)) + * old_position).into(); self.queue .write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light])); } diff --git a/code/intermediate/tutorial11-normals/src/main.rs b/code/intermediate/tutorial11-normals/src/main.rs index 4d692549..6cab0a96 100644 --- a/code/intermediate/tutorial11-normals/src/main.rs +++ b/code/intermediate/tutorial11-normals/src/main.rs @@ -51,14 +51,14 @@ struct Uniforms { impl Uniforms { fn new() -> Self { Self { - view_position: Zero::zero(), - view_proj: cgmath::Matrix4::identity(), + view_position: [0.0; 4], + view_proj: cgmath::Matrix4::identity().into(), } } fn update_view_proj(&mut self, camera: &Camera) { - self.view_position = camera.eye.to_homogeneous(); - self.view_proj = OPENGL_TO_WGPU_MATRIX * camera.build_view_projection_matrix(); + self.view_position = camera.eye.to_homogeneous().into(); + self.view_proj = (OPENGL_TO_WGPU_MATRIX * camera.build_view_projection_matrix()).into(); } } @@ -186,17 +186,14 @@ unsafe impl bytemuck::Pod for InstanceRaw {} unsafe impl bytemuck::Zeroable for InstanceRaw {} #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] struct Light { - position: cgmath::Vector3, + position: [f32; 3], // Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here _padding: u32, - color: cgmath::Vector3, + color: [f32; 3], } -unsafe impl bytemuck::Zeroable for Light {} -unsafe impl bytemuck::Pod for Light {} - struct State { surface: wgpu::Surface, device: wgpu::Device, @@ -463,9 +460,9 @@ impl State { .unwrap(); let light = Light { - position: (2.0, 2.0, 2.0).into(), + position: [2.0, 2.0, 2.0], _padding: 0, - color: (1.0, 1.0, 1.0).into(), + color: [1.0, 1.0, 1.0], }; let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { @@ -619,10 +616,10 @@ impl State { ); // Update the light - let old_position = self.light.position; + let old_position: cgmath::Vector3<_> = self.light.position.into(); self.light.position = - cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0)) - * old_position; + (cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0)) + * old_position).into(); self.queue .write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light])); } diff --git a/code/intermediate/tutorial12-camera/src/main.rs b/code/intermediate/tutorial12-camera/src/main.rs index eaab6e06..5945bc17 100644 --- a/code/intermediate/tutorial12-camera/src/main.rs +++ b/code/intermediate/tutorial12-camera/src/main.rs @@ -64,17 +64,14 @@ unsafe impl bytemuck::Pod for InstanceRaw {} unsafe impl bytemuck::Zeroable for InstanceRaw {} #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] struct Light { - position: cgmath::Vector3, + position: [f32; 3], // Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here _padding: u32, - color: cgmath::Vector3, + color: [f32; 3], } -unsafe impl bytemuck::Zeroable for Light {} -unsafe impl bytemuck::Pod for Light {} - struct State { surface: wgpu::Surface, device: wgpu::Device, @@ -341,9 +338,9 @@ impl State { println!("Elapsed (Original): {:?}", std::time::Instant::now() - now); let light = Light { - position: (2.0, 2.0, 2.0).into(), + position: [2.0, 2.0, 2.0], _padding: 0, - color: (1.0, 1.0, 1.0).into(), + color: [1.0, 1.0, 1.0], }; let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { @@ -537,10 +534,10 @@ impl State { ); // Update the light - let old_position = self.light.position; + let old_position: cgmath::Vector3<_> = self.light.position.into(); self.light.position = - cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0)) - * old_position; + (cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0)) + * old_position).into(); self.queue .write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light])); } diff --git a/code/intermediate/tutorial13-threading/src/main.rs b/code/intermediate/tutorial13-threading/src/main.rs index 18b14edc..5a049fa5 100644 --- a/code/intermediate/tutorial13-threading/src/main.rs +++ b/code/intermediate/tutorial13-threading/src/main.rs @@ -64,17 +64,14 @@ unsafe impl bytemuck::Pod for InstanceRaw {} unsafe impl bytemuck::Zeroable for InstanceRaw {} #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] struct Light { - position: cgmath::Vector3, + position: [f32; 3], // Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here _padding: u32, - color: cgmath::Vector3, + color: [f32; 3], } -unsafe impl bytemuck::Zeroable for Light {} -unsafe impl bytemuck::Pod for Light {} - struct State { surface: wgpu::Surface, device: wgpu::Device, @@ -336,9 +333,9 @@ impl State { .unwrap(); let light = Light { - position: (2.0, 2.0, 2.0).into(), + position: [2.0, 2.0, 2.0], _padding: 0, - color: (1.0, 1.0, 1.0).into(), + color: [1.0, 1.0, 1.0], }; let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { @@ -528,10 +525,10 @@ impl State { ); // Update the light - let old_position = self.light.position; + let old_position: cgmath::Vector3<_> = self.light.position.into(); self.light.position = - cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0)) - * old_position; + (cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0)) + * old_position).into(); self.queue .write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light])); } diff --git a/code/research/performance/src/main.rs b/code/research/performance/src/main.rs index f2f92715..7f639b67 100644 --- a/code/research/performance/src/main.rs +++ b/code/research/performance/src/main.rs @@ -52,19 +52,17 @@ struct Uniforms { impl Uniforms { fn new() -> Self { Self { - view_position: Zero::zero(), - view_proj: cgmath::Matrix4::identity(), + view_position: [0.0; 4], + view_proj: cgmath::Matrix4::identity().into(), } } fn update_view_proj(&mut self, camera: &Camera) { - self.view_position = camera.eye.to_homogeneous(); - self.view_proj = OPENGL_TO_WGPU_MATRIX * camera.build_view_projection_matrix(); + self.view_position = camera.eye.to_homogeneous().into(); + self.view_proj = (OPENGL_TO_WGPU_MATRIX * camera.build_view_projection_matrix()).into(); } } -unsafe impl bytemuck::Zeroable for Uniforms {} -unsafe impl bytemuck::Pod for Uniforms {} struct CameraController { speed: f32, @@ -189,17 +187,14 @@ unsafe impl bytemuck::Pod for InstanceRaw {} unsafe impl bytemuck::Zeroable for InstanceRaw {} #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] struct Light { - position: cgmath::Vector3, + position: [f32; 3], // Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here _padding: u32, - color: cgmath::Vector3, + color: [f32; 3], } -unsafe impl bytemuck::Zeroable for Light {} -unsafe impl bytemuck::Pod for Light {} - struct State { surface: wgpu::Surface, device: wgpu::Device, @@ -404,9 +399,9 @@ impl State { queue.submit(&cmds); let light = Light { - position: (2.0, 2.0, 2.0).into(), + position: [2.0, 2.0, 2.0], _padding: 0, - color: (1.0, 1.0, 1.0).into(), + color: [1.0, 1.0, 1.0], }; let light_buffer = device.create_buffer_with_data( @@ -560,11 +555,11 @@ impl State { ); // Update the light - let old_position = self.light.position; - self.light.position = cgmath::Quaternion::from_axis_angle( + let old_position: cgmath::Vector3<_> = self.light.position.into(); + self.light.position = (cgmath::Quaternion::from_axis_angle( (0.0, 1.0, 0.0).into(), cgmath::Deg(45.0) * dt.as_secs_f32(), - ) * old_position; + ) * old_position).into(); let staging_buffer = self.device.create_buffer_with_data( bytemuck::cast_slice(&[self.light]), diff --git a/docs/intermediate/tutorial10-lighting/README.md b/docs/intermediate/tutorial10-lighting/README.md index f8827dc5..8f899bb1 100644 --- a/docs/intermediate/tutorial10-lighting/README.md +++ b/docs/intermediate/tutorial10-lighting/README.md @@ -21,16 +21,13 @@ Before we can get into that though, we need to add a light to our scene. ```rust // main.rs #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] struct Light { - position: cgmath::Vector3, + position: [f32; 3], // Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here _padding: u32, - color: cgmath::Vector3, + color: [f32; 3], } - -unsafe impl bytemuck::Zeroable for Light {} -unsafe impl bytemuck::Pod for Light {} ``` Our `Light` represents a colored point in space. We're just going to use pure white light, but it's good to allow different colors of light. @@ -100,7 +97,7 @@ Let's also update the lights position in the `update()` method, so we can see wh ```rust // Update the light -let old_position = self.light.position; +let old_position: cgmath::Vector3<_> = self.light.position.into(); self.light.position = cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0)) * old_position; @@ -637,9 +634,7 @@ struct Uniforms { view_position: [f32; 4], view_proj: [[f32; 4]; 4], } -//If we want to use bytemuck, we must first implement these two traits -unsafe impl bytemuck::Zeroable for Uniforms {} -unsafe impl bytemuck::Pod for Uniforms {} + impl Uniforms { fn new() -> Self { Self {