Change "uniform" naming to "camera"

pull/224/head
Martino Fontana 3 years ago
parent dc2fb23f76
commit b2b77294f6

@ -89,20 +89,20 @@ impl Camera {
}
}
struct UniformStaging {
struct CameraStaging {
camera: Camera,
model_rotation: cgmath::Deg<f32>,
}
impl UniformStaging {
impl CameraStaging {
fn new(camera: Camera) -> Self {
Self {
camera,
model_rotation: cgmath::Deg(0.0),
}
}
fn update_uniforms(&self, uniforms: &mut Uniforms) {
uniforms.model_view_proj = (OPENGL_TO_WGPU_MATRIX
fn update_camera(&self, camera_uniform: &mut CameraUniform) {
camera_uniform.model_view_proj = (OPENGL_TO_WGPU_MATRIX
* self.camera.build_view_projection_matrix()
* cgmath::Matrix4::from_angle_z(self.model_rotation))
.into();
@ -111,11 +111,11 @@ impl UniformStaging {
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
model_view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
use cgmath::SquareMatrix;
Self {
@ -238,10 +238,10 @@ struct State {
diffuse_texture: texture::Texture,
diffuse_bind_group: wgpu::BindGroup,
camera_controller: CameraController,
uniforms: Uniforms,
uniform_staging: UniformStaging,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_staging: CameraStaging,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
size: winit::dpi::PhysicalSize<u32>,
}
@ -337,17 +337,17 @@ impl State {
};
let camera_controller = CameraController::new(0.2);
let mut uniforms = Uniforms::new();
let uniform_staging = UniformStaging::new(camera);
uniform_staging.update_uniforms(&mut uniforms);
let mut camera_uniform = CameraUniform::new();
let camera_staging = CameraStaging::new(camera);
camera_staging.update_camera(&mut camera_uniform);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -359,16 +359,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
@ -380,7 +380,7 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout, &uniform_bind_group_layout],
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
push_constant_ranges: &[],
});
@ -449,10 +449,10 @@ impl State {
diffuse_texture,
diffuse_bind_group,
camera_controller,
uniform_staging,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_staging,
camera_buffer,
camera_bind_group,
camera_uniform,
size,
}
}
@ -464,7 +464,7 @@ impl State {
self.sc_desc.height = new_size.height;
self.swap_chain = self.device.create_swap_chain(&self.surface, &self.sc_desc);
self.uniform_staging.camera.aspect =
self.camera_staging.camera.aspect =
self.sc_desc.width as f32 / self.sc_desc.height as f32;
}
}
@ -475,13 +475,13 @@ impl State {
fn update(&mut self) {
self.camera_controller
.update_camera(&mut self.uniform_staging.camera);
self.uniform_staging.model_rotation += cgmath::Deg(2.0);
self.uniform_staging.update_uniforms(&mut self.uniforms);
.update_camera(&mut self.camera_staging.camera);
self.camera_staging.model_rotation += cgmath::Deg(2.0);
self.camera_staging.update_camera(&mut self.camera_uniform);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
}
@ -515,7 +515,7 @@ impl State {
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);

@ -91,11 +91,11 @@ impl Camera {
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
use cgmath::SquareMatrix;
Self {
@ -225,9 +225,9 @@ struct State {
// NEW!
camera: Camera,
camera_controller: CameraController,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
}
impl State {
@ -322,16 +322,16 @@ impl State {
};
let camera_controller = CameraController::new(0.2);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -343,16 +343,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
@ -364,7 +364,7 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout, &uniform_bind_group_layout],
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
push_constant_ranges: &[],
});
@ -435,9 +435,9 @@ impl State {
diffuse_bind_group,
camera,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
}
}
@ -458,11 +458,11 @@ impl State {
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.uniforms.update_view_proj(&self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
}
@ -496,7 +496,7 @@ impl State {
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);

@ -6,7 +6,7 @@ layout(location=1) in vec2 a_tex_coords;
layout(location=0) out vec2 v_tex_coords;
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
mat4 u_view_proj;
};

@ -1,11 +1,11 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
struct VertexInput {
[[location(0)]] position: vec3<f32>;
@ -23,7 +23,7 @@ fn main(
) -> VertexOutput {
var out: VertexOutput;
out.tex_coords = model.tex_coords;
out.clip_position = uniforms.view_proj * vec4<f32>(model.position, 1.0);
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
return out;
}

@ -99,11 +99,11 @@ impl Camera {
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_proj: cgmath::Matrix4::identity().into(),
@ -291,9 +291,9 @@ struct State {
diffuse_bind_group: wgpu::BindGroup,
camera: Camera,
camera_controller: CameraController,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
size: winit::dpi::PhysicalSize<u32>,
instances: Vec<Instance>,
instance_buffer: wgpu::Buffer,
@ -419,12 +419,12 @@ impl State {
};
let camera_controller = CameraController::new(0.2);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -460,7 +460,7 @@ impl State {
usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -472,16 +472,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
@ -493,7 +493,7 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout, &uniform_bind_group_layout],
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
push_constant_ranges: &[],
});
@ -563,9 +563,9 @@ impl State {
diffuse_bind_group,
camera,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
size,
instances,
instance_buffer,
@ -589,11 +589,11 @@ impl State {
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.uniforms.update_view_proj(&self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
for instance in &mut self.instances {
@ -644,7 +644,7 @@ impl State {
render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.num_indices, 0, 0..self.instances.len() as u32);

@ -6,7 +6,7 @@ layout(location=1) in vec2 a_tex_coords;
layout(location=0) out vec2 v_tex_coords;
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
mat4 u_view_proj;
};

@ -99,11 +99,11 @@ impl Camera {
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_proj: cgmath::Matrix4::identity().into(),
@ -282,9 +282,9 @@ struct State {
diffuse_bind_group: wgpu::BindGroup,
camera: Camera,
camera_controller: CameraController,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
// NEW!
instances: Vec<Instance>,
#[allow(dead_code)]
@ -383,12 +383,12 @@ impl State {
};
let camera_controller = CameraController::new(0.2);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -424,7 +424,7 @@ impl State {
usage: wgpu::BufferUsage::VERTEX,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -436,16 +436,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
@ -457,7 +457,7 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout, &uniform_bind_group_layout],
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
push_constant_ranges: &[],
});
@ -528,9 +528,9 @@ impl State {
diffuse_bind_group,
camera,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
// NEW!
instances,
instance_buffer,
@ -554,11 +554,11 @@ impl State {
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.uniforms.update_view_proj(&self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
}
@ -592,7 +592,7 @@ impl State {
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);

@ -6,7 +6,7 @@ layout(location=1) in vec2 a_tex_coords;
layout(location=0) out vec2 v_tex_coords;
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
mat4 u_view_proj;
};

@ -1,11 +1,11 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
struct VertexInput {
[[location(0)]] position: vec3<f32>;
@ -36,7 +36,7 @@ fn main(
);
var out: VertexOutput;
out.tex_coords = model.tex_coords;
out.clip_position = uniforms.view_proj * model_matrix * vec4<f32>(model.position, 1.0);
out.clip_position = camera.view_proj * model_matrix * vec4<f32>(model.position, 1.0);
return out;
}

@ -122,11 +122,11 @@ impl Camera {
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_proj: cgmath::Matrix4::identity().into(),
@ -479,9 +479,9 @@ struct State {
diffuse_bind_group: wgpu::BindGroup,
camera: Camera,
camera_controller: CameraController,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
size: winit::dpi::PhysicalSize<u32>,
instances: Vec<Instance>,
#[allow(dead_code)]
@ -581,12 +581,12 @@ impl State {
};
let camera_controller = CameraController::new(0.2);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -622,7 +622,7 @@ impl State {
usage: wgpu::BufferUsage::VERTEX,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -634,16 +634,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
@ -655,7 +655,7 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout, &uniform_bind_group_layout],
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
push_constant_ranges: &[],
});
@ -737,9 +737,9 @@ impl State {
diffuse_bind_group,
camera,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
size,
instances,
instance_buffer,
@ -765,11 +765,11 @@ impl State {
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.uniforms.update_view_proj(&self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
}
@ -811,7 +811,7 @@ impl State {
render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.num_indices, 0, 0..self.instances.len() as u32);

@ -99,11 +99,11 @@ impl Camera {
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_proj: cgmath::Matrix4::identity().into(),
@ -291,9 +291,9 @@ struct State {
diffuse_bind_group: wgpu::BindGroup,
camera: Camera,
camera_controller: CameraController,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
instances: Vec<Instance>,
#[allow(dead_code)]
instance_buffer: wgpu::Buffer,
@ -393,12 +393,12 @@ impl State {
};
let camera_controller = CameraController::new(0.2);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -434,7 +434,7 @@ impl State {
usage: wgpu::BufferUsage::VERTEX,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -446,16 +446,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
@ -470,7 +470,7 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout, &uniform_bind_group_layout],
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
push_constant_ranges: &[],
});
@ -547,9 +547,9 @@ impl State {
diffuse_bind_group,
camera,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
instances,
instance_buffer,
depth_texture,
@ -578,11 +578,11 @@ impl State {
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.uniforms.update_view_proj(&self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
}
@ -624,7 +624,7 @@ impl State {
render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.num_indices, 0, 0..self.instances.len() as u32);

@ -6,7 +6,7 @@ layout(location=1) in vec2 a_tex_coords;
layout(location=0) out vec2 v_tex_coords;
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
mat4 u_view_proj;
};

@ -1,11 +1,11 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
struct VertexInput {
[[location(0)]] position: vec3<f32>;
@ -36,7 +36,7 @@ fn main(
);
var out: VertexOutput;
out.tex_coords = model.tex_coords;
out.clip_position = uniforms.view_proj * model_matrix * vec4<f32>(model.position, 1.0);
out.clip_position = camera.view_proj * model_matrix * vec4<f32>(model.position, 1.0);
return out;
}

@ -43,11 +43,11 @@ impl Camera {
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_proj: cgmath::Matrix4::identity().into(),
@ -230,9 +230,9 @@ struct State {
obj_model: model::Model,
camera: Camera,
camera_controller: CameraController,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
instances: Vec<Instance>,
#[allow(dead_code)]
instance_buffer: wgpu::Buffer,
@ -314,12 +314,12 @@ impl State {
};
let camera_controller = CameraController::new(0.2);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -353,7 +353,7 @@ impl State {
usage: wgpu::BufferUsage::VERTEX,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -365,16 +365,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let res_dir = std::path::Path::new(env!("OUT_DIR")).join("res");
@ -400,7 +400,7 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout, &uniform_bind_group_layout],
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
push_constant_ranges: &[],
});
@ -461,9 +461,9 @@ impl State {
obj_model,
camera,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
instances,
instance_buffer,
depth_texture,
@ -490,11 +490,11 @@ impl State {
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.uniforms.update_view_proj(&self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
}
@ -538,7 +538,7 @@ impl State {
render_pass.draw_model_instanced(
&self.obj_model,
0..self.instances.len() as u32,
&self.uniform_bind_group,
&self.camera_bind_group,
);
}

@ -157,21 +157,21 @@ impl Model {
}
pub trait DrawModel<'a> {
fn draw_mesh(&mut self, mesh: &'a Mesh, material: &'a Material, uniforms: &'a wgpu::BindGroup);
fn draw_mesh(&mut self, mesh: &'a Mesh, material: &'a Material, camera: &'a wgpu::BindGroup);
fn draw_mesh_instanced(
&mut self,
mesh: &'a Mesh,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
);
fn draw_model(&mut self, model: &'a Model, uniforms: &'a wgpu::BindGroup);
fn draw_model(&mut self, model: &'a Model, camera: &'a wgpu::BindGroup);
fn draw_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
);
}
@ -179,8 +179,8 @@ impl<'a, 'b> DrawModel<'b> for wgpu::RenderPass<'a>
where
'b: 'a,
{
fn draw_mesh(&mut self, mesh: &'b Mesh, material: &'b Material, uniforms: &'b wgpu::BindGroup) {
self.draw_mesh_instanced(mesh, material, 0..1, uniforms);
fn draw_mesh(&mut self, mesh: &'b Mesh, material: &'b Material, camera: &'b wgpu::BindGroup) {
self.draw_mesh_instanced(mesh, material, 0..1, camera);
}
fn draw_mesh_instanced(
@ -188,28 +188,28 @@ where
mesh: &'b Mesh,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, uniforms, &[]);
self.set_bind_group(1, camera, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
fn draw_model(&mut self, model: &'b Model, uniforms: &'b wgpu::BindGroup) {
self.draw_model_instanced(model, 0..1, uniforms);
fn draw_model(&mut self, model: &'b Model, camera: &'b wgpu::BindGroup) {
self.draw_model_instanced(model, 0..1, camera);
}
fn draw_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
let material = &model.materials[mesh.material];
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera);
}
}
}

@ -6,7 +6,7 @@ layout(location=1) in vec2 a_tex_coords;
layout(location=0) out vec2 v_tex_coords;
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
mat4 u_view_proj;
};

@ -1,11 +1,11 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
struct VertexInput {
[[location(0)]] position: vec3<f32>;
@ -36,7 +36,7 @@ fn main(
);
var out: VertexOutput;
out.tex_coords = model.tex_coords;
out.clip_position = uniforms.view_proj * model_matrix * vec4<f32>(model.position, 1.0);
out.clip_position = camera.view_proj * model_matrix * vec4<f32>(model.position, 1.0);
return out;
}

@ -5,7 +5,7 @@ layout(location=0) in vec3 a_position;
layout(location=0) out vec3 v_color;
layout(set=0, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position; // unused
mat4 u_view_proj;
};

@ -1,12 +1,12 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(0), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
[[block]]
struct Light {
@ -31,7 +31,7 @@ fn main(
) -> VertexOutput {
let scale = 0.25;
var out: VertexOutput;
out.clip_position = uniforms.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.clip_position = camera.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.color = light.color;
return out;
}

@ -43,12 +43,12 @@ impl Camera {
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_position: [f32; 4],
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_position: [0.0; 4],
@ -57,7 +57,7 @@ impl Uniforms {
}
fn update_view_proj(&mut self, camera: &Camera) {
// We're using Vector4 because ofthe uniforms 16 byte spacing requirement
// We're using Vector4 because ofthe camera_uniform 16 byte spacing requirement
self.view_position = camera.eye.to_homogeneous().into();
self.view_proj = camera.build_view_projection_matrix().into();
}
@ -259,9 +259,9 @@ struct State {
obj_model: model::Model,
camera: Camera,
camera_controller: CameraController,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
instances: Vec<Instance>,
#[allow(dead_code)]
instance_buffer: wgpu::Buffer,
@ -405,12 +405,12 @@ impl State {
let camera_controller = CameraController::new(0.2);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -444,7 +444,7 @@ impl State {
usage: wgpu::BufferUsage::VERTEX,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -456,16 +456,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let res_dir = std::path::Path::new(env!("OUT_DIR")).join("res");
@ -521,7 +521,7 @@ impl State {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[
&texture_bind_group_layout,
&uniform_bind_group_layout,
&camera_bind_group_layout,
&light_bind_group_layout,
],
push_constant_ranges: &[],
@ -546,7 +546,7 @@ impl State {
let light_render_pipeline = {
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Light Pipeline Layout"),
bind_group_layouts: &[&uniform_bind_group_layout, &light_bind_group_layout],
bind_group_layouts: &[&camera_bind_group_layout, &light_bind_group_layout],
push_constant_ranges: &[],
});
let shader = wgpu::ShaderModuleDescriptor {
@ -574,9 +574,9 @@ impl State {
obj_model,
camera,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
instances,
instance_buffer,
depth_texture,
@ -609,11 +609,11 @@ impl State {
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.uniforms.update_view_proj(&self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
// Update the light
@ -665,14 +665,14 @@ impl State {
render_pass.set_pipeline(&self.light_render_pipeline);
render_pass.draw_light_model(
&self.obj_model,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
render_pass.set_pipeline(&self.render_pipeline);
render_pass.draw_model_instanced(
&self.obj_model,
0..self.instances.len() as u32,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
}

@ -161,7 +161,7 @@ pub trait DrawModel<'a> {
&mut self,
mesh: &'a Mesh,
material: &'a Material,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_mesh_instanced(
@ -169,21 +169,21 @@ pub trait DrawModel<'a> {
mesh: &'a Mesh,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -196,10 +196,10 @@ where
&mut self,
mesh: &'b Mesh,
material: &'b Material,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_mesh_instanced(mesh, material, 0..1, uniforms, light);
self.draw_mesh_instanced(mesh, material, 0..1, camera, light);
}
fn draw_mesh_instanced(
@ -207,13 +207,13 @@ where
mesh: &'b Mesh,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, uniforms, &[]);
self.set_bind_group(1, camera, &[]);
self.set_bind_group(2, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -221,22 +221,22 @@ where
fn draw_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_model_instanced(model, 0..1, uniforms, light);
self.draw_model_instanced(model, 0..1, camera, light);
}
fn draw_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
let material = &model.materials[mesh.material];
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
}
@ -245,28 +245,28 @@ pub trait DrawLight<'a> {
fn draw_light_mesh(
&mut self,
mesh: &'a Mesh,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_mesh_instanced(
&mut self,
mesh: &'a Mesh,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -278,22 +278,22 @@ where
fn draw_light_mesh(
&mut self,
mesh: &'b Mesh,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_mesh_instanced(mesh, 0..1, uniforms, light);
self.draw_light_mesh_instanced(mesh, 0..1, camera, light);
}
fn draw_light_mesh_instanced(
&mut self,
mesh: &'b Mesh,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(0, camera, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -301,20 +301,20 @@ where
fn draw_light_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_model_instanced(model, 0..1, uniforms, light);
self.draw_light_model_instanced(model, 0..1, camera, light);
}
fn draw_light_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_light_mesh_instanced(mesh, instances.clone(), uniforms, light);
self.draw_light_mesh_instanced(mesh, instances.clone(), camera, light);
}
}
}

@ -10,7 +10,7 @@ layout(set = 0, binding = 0) uniform texture2D t_diffuse;
layout(set = 0, binding = 1) uniform sampler s_diffuse;
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj; // unused
};

@ -10,7 +10,7 @@ layout(set = 0, binding = 0) uniform texture2D t_diffuse;
layout(set = 0, binding = 1) uniform sampler s_diffuse;
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj; // unused
};

@ -9,7 +9,7 @@ layout(location=1) out vec3 v_normal;
layout(location=2) out vec3 v_position;
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position; // unused
mat4 u_view_proj;
};

@ -1,12 +1,12 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
[[block]]
struct Light {
@ -59,7 +59,7 @@ fn main(
out.world_normal = normal_matrix * model.normal;
var world_position: vec4<f32> = model_matrix * vec4<f32>(model.position, 1.0);
out.world_position = world_position.xyz;
out.clip_position = uniforms.view_proj * world_position;
out.clip_position = camera.view_proj * world_position;
return out;
}
@ -79,7 +79,7 @@ fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
let ambient_color = light.color * ambient_strength;
let light_dir = normalize(light.position - in.world_position);
let view_dir = normalize(uniforms.view_pos.xyz - in.world_position);
let view_dir = normalize(camera.view_pos.xyz - in.world_position);
let half_dir = normalize(view_dir + light_dir);
let diffuse_strength = max(dot(in.world_normal, light_dir), 0.0);

@ -5,7 +5,7 @@ layout(location=0) in vec3 a_position;
layout(location=0) out vec3 v_color;
layout(set=0, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj;
};

@ -1,12 +1,12 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(0), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
[[block]]
struct Light {
@ -31,7 +31,7 @@ fn main(
) -> VertexOutput {
let scale = 0.25;
var out: VertexOutput;
out.clip_position = uniforms.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.clip_position = camera.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.color = light.color;
return out;
}

@ -43,12 +43,12 @@ impl Camera {
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_position: [f32; 4],
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_position: [0.0; 4],
@ -258,9 +258,9 @@ struct State {
obj_model: model::Model,
camera: Camera,
camera_controller: CameraController,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
instances: Vec<Instance>,
#[allow(dead_code)]
instance_buffer: wgpu::Buffer,
@ -426,12 +426,12 @@ impl State {
let camera_controller = CameraController::new(0.2);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -465,7 +465,7 @@ impl State {
usage: wgpu::BufferUsage::VERTEX,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -477,16 +477,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let res_dir = std::path::Path::new(env!("OUT_DIR")).join("res");
@ -542,7 +542,7 @@ impl State {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[
&texture_bind_group_layout,
&uniform_bind_group_layout,
&camera_bind_group_layout,
&light_bind_group_layout,
],
push_constant_ranges: &[],
@ -567,7 +567,7 @@ impl State {
let light_render_pipeline = {
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Light Pipeline Layout"),
bind_group_layouts: &[&uniform_bind_group_layout, &light_bind_group_layout],
bind_group_layouts: &[&camera_bind_group_layout, &light_bind_group_layout],
push_constant_ranges: &[],
});
let shader = wgpu::ShaderModuleDescriptor {
@ -625,9 +625,9 @@ impl State {
obj_model,
camera,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
instances,
instance_buffer,
depth_texture,
@ -662,11 +662,11 @@ impl State {
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.uniforms.update_view_proj(&self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
// Update the light
@ -718,7 +718,7 @@ impl State {
render_pass.set_pipeline(&self.light_render_pipeline);
render_pass.draw_light_model(
&self.obj_model,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
@ -726,7 +726,7 @@ impl State {
render_pass.draw_model_instanced(
&self.obj_model,
0..self.instances.len() as u32,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
}

@ -282,7 +282,7 @@ pub trait DrawModel<'a> {
&mut self,
mesh: &'a Mesh,
material: &'a Material,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_mesh_instanced(
@ -290,21 +290,21 @@ pub trait DrawModel<'a> {
mesh: &'a Mesh,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced_with_material(
@ -312,7 +312,7 @@ pub trait DrawModel<'a> {
model: &'a Model,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -325,10 +325,10 @@ where
&mut self,
mesh: &'b Mesh,
material: &'b Material,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_mesh_instanced(mesh, material, 0..1, uniforms, light);
self.draw_mesh_instanced(mesh, material, 0..1, camera, light);
}
fn draw_mesh_instanced(
@ -336,13 +336,13 @@ where
mesh: &'b Mesh,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, uniforms, &[]);
self.set_bind_group(1, camera, &[]);
self.set_bind_group(2, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -350,22 +350,22 @@ where
fn draw_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_model_instanced(model, 0..1, uniforms, light);
self.draw_model_instanced(model, 0..1, camera, light);
}
fn draw_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
let material = &model.materials[mesh.material];
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
@ -374,11 +374,11 @@ where
model: &'b Model,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
}
@ -387,28 +387,28 @@ pub trait DrawLight<'a> {
fn draw_light_mesh(
&mut self,
mesh: &'a Mesh,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_mesh_instanced(
&mut self,
mesh: &'a Mesh,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -420,22 +420,22 @@ where
fn draw_light_mesh(
&mut self,
mesh: &'b Mesh,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_mesh_instanced(mesh, 0..1, uniforms, light);
self.draw_light_mesh_instanced(mesh, 0..1, camera, light);
}
fn draw_light_mesh_instanced(
&mut self,
mesh: &'b Mesh,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(0, camera, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -443,20 +443,20 @@ where
fn draw_light_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_model_instanced(model, 0..1, uniforms, light);
self.draw_light_model_instanced(model, 0..1, camera, light);
}
fn draw_light_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_light_mesh_instanced(mesh, instances.clone(), uniforms, light);
self.draw_light_mesh_instanced(mesh, instances.clone(), camera, light);
}
}
}

@ -12,7 +12,7 @@ layout(location=2) out vec3 v_light_position; // NEW!
layout(location=3) out vec3 v_view_position; // NEW!
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj;
};

@ -12,7 +12,7 @@ layout(location=2) out vec3 v_light_position; // NEW!
layout(location=3) out vec3 v_view_position; // NEW!
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj;
};

@ -1,12 +1,12 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
[[block]]
struct Light {
@ -71,10 +71,10 @@ fn main(
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
var out: VertexOutput;
out.clip_position = uniforms.view_proj * world_position;
out.clip_position = camera.view_proj * world_position;
out.tex_coords = model.tex_coords;
out.tangent_position = tangent_matrix * world_position.xyz;
out.tangent_view_position = tangent_matrix * uniforms.view_pos.xyz;
out.tangent_view_position = tangent_matrix * camera.view_pos.xyz;
out.tangent_light_position = tangent_matrix * light.position;
return out;
}

@ -12,7 +12,7 @@ layout(set = 0, binding = 2) uniform texture2D t_normal;
layout(set = 0, binding = 3) uniform sampler s_normal;
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj; // unused
};

@ -12,7 +12,7 @@ layout(location=1) out vec3 v_position; // UPDATED!
layout(location=2) out mat3 v_tangent_matrix; // NEW!
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj;
};

@ -5,7 +5,7 @@ layout(location=0) in vec3 a_position;
layout(location=0) out vec3 v_color;
layout(set=0, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj;
};

@ -1,12 +1,12 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(0), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
[[block]]
struct Light {
@ -31,7 +31,7 @@ fn main(
) -> VertexOutput {
let scale = 0.25;
var out: VertexOutput;
out.clip_position = uniforms.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.clip_position = camera.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.color = light.color;
return out;
}

@ -18,12 +18,12 @@ const NUM_INSTANCES_PER_ROW: u32 = 10;
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_position: [f32; 4],
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_position: [0.0; 4],
@ -136,9 +136,9 @@ struct State {
camera: camera::Camera, // UPDATED!
projection: camera::Projection, // NEW!
camera_controller: camera::CameraController, // UPDATED!
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
instances: Vec<Instance>,
#[allow(dead_code)]
instance_buffer: wgpu::Buffer,
@ -300,12 +300,12 @@ impl State {
camera::Projection::new(sc_desc.width, sc_desc.height, cgmath::Deg(45.0), 0.1, 100.0);
let camera_controller = camera::CameraController::new(4.0, 0.4);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera, &projection);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera, &projection);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -339,7 +339,7 @@ impl State {
usage: wgpu::BufferUsage::VERTEX,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -351,16 +351,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let res_dir = std::path::Path::new(env!("OUT_DIR")).join("res");
@ -418,7 +418,7 @@ impl State {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[
&texture_bind_group_layout,
&uniform_bind_group_layout,
&camera_bind_group_layout,
&light_bind_group_layout,
],
push_constant_ranges: &[],
@ -443,7 +443,7 @@ impl State {
let light_render_pipeline = {
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Light Pipeline Layout"),
bind_group_layouts: &[&uniform_bind_group_layout, &light_bind_group_layout],
bind_group_layouts: &[&camera_bind_group_layout, &light_bind_group_layout],
push_constant_ranges: &[],
});
let shader = wgpu::ShaderModuleDescriptor {
@ -502,9 +502,9 @@ impl State {
camera,
projection,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
instances,
instance_buffer,
depth_texture,
@ -568,12 +568,12 @@ impl State {
fn update(&mut self, dt: std::time::Duration) {
// UPDATED!
self.camera_controller.update_camera(&mut self.camera, dt);
self.uniforms
self.camera_uniform
.update_view_proj(&self.camera, &self.projection);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
// Update the light
@ -625,7 +625,7 @@ impl State {
render_pass.set_pipeline(&self.light_render_pipeline);
render_pass.draw_light_model(
&self.obj_model,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
@ -633,7 +633,7 @@ impl State {
render_pass.draw_model_instanced(
&self.obj_model,
0..self.instances.len() as u32,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
}

@ -282,7 +282,7 @@ pub trait DrawModel<'a> {
&mut self,
mesh: &'a Mesh,
material: &'a Material,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_mesh_instanced(
@ -290,21 +290,21 @@ pub trait DrawModel<'a> {
mesh: &'a Mesh,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced_with_material(
@ -312,7 +312,7 @@ pub trait DrawModel<'a> {
model: &'a Model,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -325,10 +325,10 @@ where
&mut self,
mesh: &'b Mesh,
material: &'b Material,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_mesh_instanced(mesh, material, 0..1, uniforms, light);
self.draw_mesh_instanced(mesh, material, 0..1, camera, light);
}
fn draw_mesh_instanced(
@ -336,13 +336,13 @@ where
mesh: &'b Mesh,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, uniforms, &[]);
self.set_bind_group(1, camera, &[]);
self.set_bind_group(2, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -350,22 +350,22 @@ where
fn draw_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_model_instanced(model, 0..1, uniforms, light);
self.draw_model_instanced(model, 0..1, camera, light);
}
fn draw_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
let material = &model.materials[mesh.material];
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
@ -374,11 +374,11 @@ where
model: &'b Model,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
}
@ -387,28 +387,28 @@ pub trait DrawLight<'a> {
fn draw_light_mesh(
&mut self,
mesh: &'a Mesh,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_mesh_instanced(
&mut self,
mesh: &'a Mesh,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -420,22 +420,22 @@ where
fn draw_light_mesh(
&mut self,
mesh: &'b Mesh,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_mesh_instanced(mesh, 0..1, uniforms, light);
self.draw_light_mesh_instanced(mesh, 0..1, camera, light);
}
fn draw_light_mesh_instanced(
&mut self,
mesh: &'b Mesh,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(0, camera, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -443,20 +443,20 @@ where
fn draw_light_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_model_instanced(model, 0..1, uniforms, light);
self.draw_light_model_instanced(model, 0..1, camera, light);
}
fn draw_light_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_light_mesh_instanced(mesh, instances.clone(), uniforms, light);
self.draw_light_mesh_instanced(mesh, instances.clone(), camera, light);
}
}
}

@ -12,7 +12,7 @@ layout(location=2) out vec3 v_light_position; // NEW!
layout(location=3) out vec3 v_view_position; // NEW!
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj;
};

@ -1,12 +1,12 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
[[block]]
struct Light {
@ -71,10 +71,10 @@ fn main(
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
var out: VertexOutput;
out.clip_position = uniforms.view_proj * world_position;
out.clip_position = camera.view_proj * world_position;
out.tex_coords = model.tex_coords;
out.tangent_position = tangent_matrix * world_position.xyz;
out.tangent_view_position = tangent_matrix * uniforms.view_pos.xyz;
out.tangent_view_position = tangent_matrix * camera.view_pos.xyz;
out.tangent_light_position = tangent_matrix * light.position;
return out;
}

@ -5,7 +5,7 @@ layout(location=0) in vec3 a_position;
layout(location=0) out vec3 v_color;
layout(set=0, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj;
};

@ -1,12 +1,12 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(0), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
[[block]]
struct Light {
@ -31,7 +31,7 @@ fn main(
) -> VertexOutput {
let scale = 0.25;
var out: VertexOutput;
out.clip_position = uniforms.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.clip_position = camera.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.color = light.color;
return out;
}

@ -18,12 +18,12 @@ const NUM_INSTANCES_PER_ROW: u32 = 10;
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_position: [f32; 4],
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_position: [0.0; 4],
@ -136,9 +136,9 @@ struct State {
camera: camera::Camera,
projection: camera::Projection,
camera_controller: camera::CameraController,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
instances: Vec<Instance>,
#[allow(dead_code)]
instance_buffer: wgpu::Buffer,
@ -299,12 +299,12 @@ impl State {
camera::Projection::new(sc_desc.width, sc_desc.height, cgmath::Deg(45.0), 0.1, 100.0);
let camera_controller = camera::CameraController::new(4.0, 0.4);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera, &projection);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera, &projection);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -340,7 +340,7 @@ impl State {
usage: wgpu::BufferUsage::VERTEX,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -352,16 +352,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let res_dir = std::path::Path::new(env!("OUT_DIR")).join("res");
@ -417,7 +417,7 @@ impl State {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[
&texture_bind_group_layout,
&uniform_bind_group_layout,
&camera_bind_group_layout,
&light_bind_group_layout,
],
push_constant_ranges: &[],
@ -442,7 +442,7 @@ impl State {
let light_render_pipeline = {
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Light Pipeline Layout"),
bind_group_layouts: &[&uniform_bind_group_layout, &light_bind_group_layout],
bind_group_layouts: &[&camera_bind_group_layout, &light_bind_group_layout],
push_constant_ranges: &[],
});
let shader = wgpu::ShaderModuleDescriptor {
@ -501,9 +501,9 @@ impl State {
camera,
projection,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
instances,
instance_buffer,
depth_texture,
@ -563,12 +563,12 @@ impl State {
fn update(&mut self, dt: std::time::Duration) {
self.camera_controller.update_camera(&mut self.camera, dt);
self.uniforms
self.camera_uniform
.update_view_proj(&self.camera, &self.projection);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
// Update the light
@ -620,7 +620,7 @@ impl State {
render_pass.set_pipeline(&self.light_render_pipeline);
render_pass.draw_light_model(
&self.obj_model,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
@ -628,7 +628,7 @@ impl State {
render_pass.draw_model_instanced(
&self.obj_model,
0..self.instances.len() as u32,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
}

@ -282,7 +282,7 @@ pub trait DrawModel<'a> {
&mut self,
mesh: &'a Mesh,
material: &'a Material,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_mesh_instanced(
@ -290,21 +290,21 @@ pub trait DrawModel<'a> {
mesh: &'a Mesh,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced_with_material(
@ -312,7 +312,7 @@ pub trait DrawModel<'a> {
model: &'a Model,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -325,10 +325,10 @@ where
&mut self,
mesh: &'b Mesh,
material: &'b Material,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_mesh_instanced(mesh, material, 0..1, uniforms, light);
self.draw_mesh_instanced(mesh, material, 0..1, camera, light);
}
fn draw_mesh_instanced(
@ -336,13 +336,13 @@ where
mesh: &'b Mesh,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, uniforms, &[]);
self.set_bind_group(1, camera, &[]);
self.set_bind_group(2, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -350,22 +350,22 @@ where
fn draw_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_model_instanced(model, 0..1, uniforms, light);
self.draw_model_instanced(model, 0..1, camera, light);
}
fn draw_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
let material = &model.materials[mesh.material];
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
@ -374,11 +374,11 @@ where
model: &'b Model,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
}
@ -387,28 +387,28 @@ pub trait DrawLight<'a> {
fn draw_light_mesh(
&mut self,
mesh: &'a Mesh,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_mesh_instanced(
&mut self,
mesh: &'a Mesh,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -420,22 +420,22 @@ where
fn draw_light_mesh(
&mut self,
mesh: &'b Mesh,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_mesh_instanced(mesh, 0..1, uniforms, light);
self.draw_light_mesh_instanced(mesh, 0..1, camera, light);
}
fn draw_light_mesh_instanced(
&mut self,
mesh: &'b Mesh,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(0, camera, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -443,20 +443,20 @@ where
fn draw_light_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_model_instanced(model, 0..1, uniforms, light);
self.draw_light_model_instanced(model, 0..1, camera, light);
}
fn draw_light_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_light_mesh_instanced(mesh, instances.clone(), uniforms, light);
self.draw_light_mesh_instanced(mesh, instances.clone(), camera, light);
}
}
}

@ -12,7 +12,7 @@ layout(location=2) out vec3 v_light_position; // NEW!
layout(location=3) out vec3 v_view_position; // NEW!
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj;
};

@ -1,12 +1,12 @@
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
[[block]]
struct Light {
@ -71,10 +71,10 @@ fn main(
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
var out: VertexOutput;
out.clip_position = uniforms.view_proj * world_position;
out.clip_position = camera.view_proj * world_position;
out.tex_coords = model.tex_coords;
out.tangent_position = tangent_matrix * world_position.xyz;
out.tangent_view_position = tangent_matrix * uniforms.view_pos.xyz;
out.tangent_view_position = tangent_matrix * camera.view_pos.xyz;
out.tangent_light_position = tangent_matrix * light.position;
return out;
}

@ -5,7 +5,7 @@ layout(location=0) in vec3 a_position;
layout(location=0) out vec3 v_color;
layout(set=0, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj;
};

@ -21,12 +21,12 @@ const NUM_INSTANCES_PER_ROW: u32 = 10;
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_position: [f32; 4],
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_position: [0.0; 4],
@ -122,9 +122,9 @@ struct State {
camera: camera::Camera,
projection: camera::Projection,
camera_controller: camera::CameraController,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
instances: Vec<Instance>,
#[allow(dead_code)]
instance_buffer: wgpu::Buffer,
@ -230,12 +230,12 @@ impl State {
camera::Projection::new(sc_desc.width, sc_desc.height, cgmath::Deg(45.0), 0.1, 100.0);
let camera_controller = camera::CameraController::new(4.0, 0.4);
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera, &projection);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera, &projection);
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -271,7 +271,7 @@ impl State {
usage: wgpu::BufferUsage::VERTEX,
});
let uniform_bind_group_layout =
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -283,16 +283,16 @@ impl State {
},
count: None,
}],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
let res_dir = std::path::Path::new(env!("OUT_DIR")).join("res");
@ -352,7 +352,7 @@ impl State {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[
&texture_bind_group_layout,
&uniform_bind_group_layout,
&camera_bind_group_layout,
&light_bind_group_layout,
],
push_constant_ranges: &[],
@ -371,7 +371,7 @@ impl State {
let light_render_pipeline = {
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Light Pipeline Layout"),
bind_group_layouts: &[&uniform_bind_group_layout, &light_bind_group_layout],
bind_group_layouts: &[&camera_bind_group_layout, &light_bind_group_layout],
push_constant_ranges: &[],
});
@ -427,9 +427,9 @@ impl State {
camera,
projection,
camera_controller,
uniform_buffer,
uniform_bind_group,
uniforms,
camera_buffer,
camera_bind_group,
camera_uniform,
instances,
instance_buffer,
depth_texture,
@ -498,12 +498,12 @@ impl State {
fn update(&mut self, dt: std::time::Duration) {
self.camera_controller.update_camera(&mut self.camera, dt);
self.uniforms
self.camera_uniform
.update_view_proj(&self.camera, &self.projection);
self.queue.write_buffer(
&self.uniform_buffer,
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.uniforms]),
bytemuck::cast_slice(&[self.camera_uniform]),
);
// Update the light
@ -558,7 +558,7 @@ impl State {
render_pass.set_pipeline(&self.light_render_pipeline);
render_pass.draw_light_model(
&self.obj_model,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
@ -566,7 +566,7 @@ impl State {
render_pass.draw_model_instanced(
&self.obj_model,
0..self.instances.len() as u32,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
}

@ -388,7 +388,7 @@ pub trait DrawModel<'a> {
&mut self,
mesh: &'a Mesh,
material: &'a Material,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_mesh_instanced(
@ -396,21 +396,21 @@ pub trait DrawModel<'a> {
mesh: &'a Mesh,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced_with_material(
@ -418,7 +418,7 @@ pub trait DrawModel<'a> {
model: &'a Model,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -431,10 +431,10 @@ where
&mut self,
mesh: &'b Mesh,
material: &'b Material,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_mesh_instanced(mesh, material, 0..1, uniforms, light);
self.draw_mesh_instanced(mesh, material, 0..1, camera, light);
}
fn draw_mesh_instanced(
@ -442,13 +442,13 @@ where
mesh: &'b Mesh,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, uniforms, &[]);
self.set_bind_group(1, camera, &[]);
self.set_bind_group(2, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -456,22 +456,22 @@ where
fn draw_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_model_instanced(model, 0..1, uniforms, light);
self.draw_model_instanced(model, 0..1, camera, light);
}
fn draw_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
let material = &model.materials[mesh.material];
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
@ -480,11 +480,11 @@ where
model: &'b Model,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
}
@ -493,28 +493,28 @@ pub trait DrawLight<'a> {
fn draw_light_mesh(
&mut self,
mesh: &'a Mesh,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_mesh_instanced(
&mut self,
mesh: &'a Mesh,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -526,22 +526,22 @@ where
fn draw_light_mesh(
&mut self,
mesh: &'b Mesh,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_mesh_instanced(mesh, 0..1, uniforms, light);
self.draw_light_mesh_instanced(mesh, 0..1, camera, light);
}
fn draw_light_mesh_instanced(
&mut self,
mesh: &'b Mesh,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(0, camera, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -549,20 +549,20 @@ where
fn draw_light_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_model_instanced(model, 0..1, uniforms, light);
self.draw_light_model_instanced(model, 0..1, camera, light);
}
fn draw_light_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_light_mesh_instanced(mesh, instances.clone(), uniforms, light);
self.draw_light_mesh_instanced(mesh, instances.clone(), camera, light);
}
}
}

@ -12,7 +12,7 @@ layout(location=2) out vec3 v_light_position; // NEW!
layout(location=3) out vec3 v_view_position; // NEW!
layout(set=1, binding=0)
uniform Uniforms {
uniform Camera {
vec3 u_view_position;
mat4 u_view_proj;
};

@ -94,12 +94,12 @@ pub struct UniformData {
unsafe impl bytemuck::Zeroable for UniformData {}
unsafe impl bytemuck::Pod for UniformData {}
pub struct Uniforms {
pub struct CameraUniform {
data: UniformData,
buffer: wgpu::Buffer,
}
impl Uniforms {
impl CameraUniform {
pub fn new(device: &wgpu::Device) -> Self {
let data = UniformData {
view_position: Zero::zero(),
@ -137,7 +137,7 @@ impl Uniforms {
/**
* Holds the wgpu::BindGroupLayout and one wgpu::BindGroup for the
* just the Uniforms struct.
* just the CameraUniform struct.
*/
pub struct UniformBinding {
pub layout: wgpu::BindGroupLayout,
@ -145,7 +145,7 @@ pub struct UniformBinding {
}
impl UniformBinding {
pub fn new(device: &wgpu::Device, uniforms: &Uniforms) -> Self {
pub fn new(device: &wgpu::Device, camera_uniform: &CameraUniform) -> Self {
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
@ -163,7 +163,7 @@ impl UniformBinding {
layout: &layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniforms.buffer.as_entire_binding(),
resource: camera_uniform.buffer.as_entire_binding(),
}],
label: Some("UniformBinding::bind_group"),
});
@ -171,12 +171,12 @@ impl UniformBinding {
Self { layout, bind_group }
}
pub fn rebind(&mut self, device: &wgpu::Device, uniforms: &Uniforms) {
pub fn rebind(&mut self, device: &wgpu::Device, camera_uniform: &CameraUniform) {
self.bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniforms.buffer.as_entire_binding(),
resource: camera_uniform.buffer.as_entire_binding(),
}],
label: Some("UniformBinding::bind_group"),
});

@ -248,7 +248,7 @@ pub trait DrawModel<'a> {
&mut self,
mesh: &'a Mesh,
material: &'a Material,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_mesh_instanced(
@ -256,21 +256,21 @@ pub trait DrawModel<'a> {
mesh: &'a Mesh,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced_with_material(
@ -278,7 +278,7 @@ pub trait DrawModel<'a> {
model: &'a Model,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -291,10 +291,10 @@ where
&mut self,
mesh: &'b Mesh,
material: &'b Material,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_mesh_instanced(mesh, material, 0..1, uniforms, light);
self.draw_mesh_instanced(mesh, material, 0..1, camera, light);
}
fn draw_mesh_instanced(
@ -302,13 +302,13 @@ where
mesh: &'b Mesh,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, uniforms, &[]);
self.set_bind_group(1, camera, &[]);
self.set_bind_group(2, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -316,22 +316,22 @@ where
fn draw_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_model_instanced(model, 0..1, uniforms, light);
self.draw_model_instanced(model, 0..1, camera, light);
}
fn draw_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
let material = &model.materials[mesh.material];
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
@ -340,11 +340,11 @@ where
model: &'b Model,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
}
@ -353,28 +353,28 @@ pub trait DrawLight<'a> {
fn draw_light_mesh(
&mut self,
mesh: &'a Mesh,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_mesh_instanced(
&mut self,
mesh: &'a Mesh,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -386,22 +386,22 @@ where
fn draw_light_mesh(
&mut self,
mesh: &'b Mesh,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_mesh_instanced(mesh, 0..1, uniforms, light);
self.draw_light_mesh_instanced(mesh, 0..1, camera, light);
}
fn draw_light_mesh_instanced(
&mut self,
mesh: &'b Mesh,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(0, camera, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -409,20 +409,20 @@ where
fn draw_light_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_model_instanced(model, 0..1, uniforms, light);
self.draw_light_model_instanced(model, 0..1, camera, light);
}
fn draw_light_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_light_mesh_instanced(mesh, instances.clone(), uniforms, light);
self.draw_light_mesh_instanced(mesh, instances.clone(), camera, light);
}
}
}

@ -87,20 +87,20 @@ Now that we have our camera, and it can make us a view projection matrix, we nee
## The uniform buffer
Up to this point we've used `Buffer`s to store our vertex and index data, and even to load our textures. We are going to use them again to create what's known as a uniform buffer. A uniform is a blob of data that is available to every invocation of a set of shaders. We've technically already used uniforms for our texture and sampler. We're going to use them again to store our view projection matrix. To start let's create a struct to hold our `Uniforms`.
Up to this point we've used `Buffer`s to store our vertex and index data, and even to load our textures. We are going to use them again to create what's known as a uniform buffer. A uniform is a blob of data that is available to every invocation of a set of shaders. We've technically already used uniforms for our texture and sampler. We're going to use them again to store our view projection matrix. To start let's create a struct to hold our uniform.
```rust
// We need this for Rust to store our data correctly for the shaders
#[repr(C)]
// This is so we can store this in a buffer
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
// We can't use cgmath with bytemuck directly so we'll have
// to convert the Matrix4 into a 4x4 f32 array
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
use cgmath::SquareMatrix;
Self {
@ -114,18 +114,18 @@ impl Uniforms {
}
```
Now that we have our data structured, let's make our `uniform_buffer`.
Now that we have our data structured, let's make our `camera_buffer`.
```rust
// in new() after creating `camera`
let mut uniforms = Uniforms::new();
uniforms.update_view_proj(&camera);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let uniform_buffer = device.create_buffer_init(
let camera_buffer = device.create_buffer_init(
&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[uniforms]),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
}
);
@ -136,7 +136,7 @@ let uniform_buffer = device.create_buffer_init(
Cool, now that we have a uniform buffer, what do we do with it? The answer is we create a bind group for it. First we have to create the bind group layout.
```rust
let uniform_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
let camera_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
@ -149,7 +149,7 @@ let uniform_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroup
count: None,
}
],
label: Some("uniform_bind_group_layout"),
label: Some("camera_bind_group_layout"),
});
```
@ -159,19 +159,19 @@ let uniform_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroup
Now we can create the actual bind group.
```rust
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &uniform_bind_group_layout,
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
resource: camera_buffer.as_entire_binding(),
}
],
label: Some("uniform_bind_group"),
label: Some("camera_bind_group"),
});
```
Like with our texture, we need to register our `uniform_bind_group_layout` with the render pipeline.
Like with our texture, we need to register our `camera_bind_group_layout` with the render pipeline.
```rust
let render_pipeline_layout = device.create_pipeline_layout(
@ -179,22 +179,22 @@ let render_pipeline_layout = device.create_pipeline_layout(
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[
&texture_bind_group_layout,
&uniform_bind_group_layout,
&camera_bind_group_layout,
],
push_constant_ranges: &[],
}
);
```
Now we need to add `uniform_buffer` and `uniform_bind_group` to `State`
Now we need to add `camera_buffer` and `camera_bind_group` to `State`
```rust
struct State {
// ...
camera: Camera,
uniforms: Uniforms,
uniform_buffer: wgpu::Buffer,
uniform_bind_group: wgpu::BindGroup,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
}
async fn new(window: &Window) -> Self {
@ -202,9 +202,9 @@ async fn new(window: &Window) -> Self {
Self {
// ...
camera,
uniforms,
uniform_buffer,
uniform_bind_group,
camera_uniform,
camera_buffer,
camera_bind_group,
}
}
```
@ -215,14 +215,14 @@ The final thing we need to do before we get into shaders is use the bind group i
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
// NEW!
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
```
## Using the uniforms in the vertex shader
## Using the uniform in the vertex shader
Modify the vertex shader to include the following.
@ -230,11 +230,11 @@ Modify the vertex shader to include the following.
// Vertex shader
[[block]] // 1.
struct Uniforms {
struct CameraUniform {
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]] // 2.
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
struct VertexInput {
[[location(0)]] position: vec3<f32>;
@ -252,13 +252,13 @@ fn main(
) -> VertexOutput {
var out: VertexOutput;
out.tex_coords = model.tex_coords;
out.clip_position = uniforms.view_proj * vec4<f32>(model.position, 1.0); // 3.
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0); // 3.
return out;
}
```
1. According to the [WGSL Spec](https://gpuweb.github.io/gpuweb/wgsl/), The block decorator indicates this structure type represents the contents of a buffer resource occupying a single binding slot in the shaders resource interface. Any structure used as a `uniform` must be annotated with `[[block]]`
2. Because we've created a new bind group, we need to specify which one we're using in the shader. The number is determined by our `render_pipeline_layout`. The `texture_bind_group_layout` is listed first, thus it's `group(0)`, and `uniform_bind_group` is second, so it's `group(1)`.
2. Because we've created a new bind group, we need to specify which one we're using in the shader. The number is determined by our `render_pipeline_layout`. The `texture_bind_group_layout` is listed first, thus it's `group(0)`, and `camera_bind_group` is second, so it's `group(1)`.
3. Multiplication order is important when it comes to matrices. The vector goes on the right, and the matrices gone on the left in order of importance.
## A controller for our camera
@ -407,7 +407,7 @@ fn input(&mut self, event: &WindowEvent) -> bool {
```
Up to this point, the camera controller isn't actually doing anything. The values in our uniform buffer need to be updated. There are a few main methods to do that.
1. We can create a separate buffer and copy it's contents to our `uniform_buffer`. The new buffer is known as a staging buffer. This method is usually how it's done as it allows the contents of the main buffer (in this case `uniform_buffer`) to only be accessible by the gpu. The gpu can do some speed optimizations which it couldn't if we could access the buffer via the cpu.
1. We can create a separate buffer and copy it's contents to our `camera_buffer`. The new buffer is known as a staging buffer. This method is usually how it's done as it allows the contents of the main buffer (in this case `camera_buffer`) to only be accessible by the gpu. The gpu can do some speed optimizations which it couldn't if we could access the buffer via the cpu.
2. We can call on of the mapping method's `map_read_async`, and `map_write_async` on the buffer itself. These allow us to access a buffer's contents directly, but requires us to deal with the `async` aspect of these methods this also requires our buffer to use the `BufferUsage::MAP_READ` and/or `BufferUsage::MAP_WRITE`. We won't talk about it here, but you check out [Wgpu without a window](../../showcase/windowless) tutorial if you want to know more.
3. We can use `write_buffer` on `queue`.
@ -416,8 +416,8 @@ We're going to use option number 3.
```rust
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.uniforms.update_view_proj(&self.camera);
self.queue.write_buffer(&self.uniform_buffer, 0, bytemuck::cast_slice(&[self.uniforms]));
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(&self.camera_buffer, 0, bytemuck::cast_slice(&[self.camera_uniform]));
}
```

@ -202,7 +202,7 @@ The last change we need to make is in the `render()` method. We need to bind our
```rust
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
// NEW!
render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
@ -214,7 +214,7 @@ render_pass.draw_indexed(0..self.num_indices, 0, 0..self.instances.len() as _);
<div class="warning">
Make sure if you add new instances to the `Vec`, that you recreate the `instance_buffer` and as well as `uniform_bind_group`, otherwise your new instances won't show up correctly.
Make sure if you add new instances to the `Vec`, that you recreate the `instance_buffer` and as well as `camera_bind_group`, otherwise your new instances won't show up correctly.
</div>
@ -247,7 +247,7 @@ fn main(
}
```
We'll apply the `model_matrix` before we apply `uniforms.view_proj`. We do this because the `uniforms.view_proj` changes the coordinate system from `world space` to `camera space`. Our `model_matrix` is a `world space` transformation, so we don't want to be in `camera space` when using it.
We'll apply the `model_matrix` before we apply `camera_uniform.view_proj`. We do this because the `camera_uniform.view_proj` changes the coordinate system from `world space` to `camera space`. Our `model_matrix` is a `world space` transformation, so we don't want to be in `camera space` when using it.
```wgsl
[[stage(vertex)]]
@ -258,7 +258,7 @@ fn main(
// ...
var out: VertexOutput;
out.tex_coords = model.tex_coords;
out.clip_position = uniforms.view_proj * model_matrix * vec4<f32>(model.position, 1.0);
out.clip_position = camera.view_proj * model_matrix * vec4<f32>(model.position, 1.0);
return out;
}
```

@ -328,7 +328,7 @@ We could have put this methods in `impl Model`, but I felt it made more sense to
render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
use model::DrawModel;
render_pass.draw_mesh_instanced(&self.obj_model.meshes[0], 0..self.instances.len() as u32);
@ -402,13 +402,13 @@ We're going to add a material parameter to `DrawModel`.
```rust
pub trait DrawModel<'a> {
fn draw_mesh(&mut self, mesh: &'a Mesh, material: &'a Material, uniforms: &'a wgpu::BindGroup);
fn draw_mesh(&mut self, mesh: &'a Mesh, material: &'a Material, camera: &'a wgpu::BindGroup);
fn draw_mesh_instanced(
&mut self,
mesh: &'a Mesh,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
);
}
@ -417,8 +417,8 @@ impl<'a, 'b> DrawModel<'a, 'b> for wgpu::RenderPass<'a>
where
'b: 'a,
{
fn draw_mesh(&mut self, mesh: &'b Mesh, material: &'b Material, uniforms: &'b wgpu::BindGroup) {
self.draw_mesh_instanced(mesh, material, 0..1, uniforms);
fn draw_mesh(&mut self, mesh: &'b Mesh, material: &'b Material, camera: &'b wgpu::BindGroup) {
self.draw_mesh_instanced(mesh, material, 0..1, camera);
}
fn draw_mesh_instanced(
@ -426,12 +426,12 @@ where
mesh: &'b Mesh,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, uniforms, &[]);
self.set_bind_group(1, camera, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
}
@ -446,7 +446,7 @@ render_pass.set_pipeline(&self.render_pipeline);
let mesh = &self.obj_model.meshes[0];
let material = &self.obj_model.materials[mesh.material];
render_pass.draw_mesh_instanced(mesh, material, 0..self.instances.len() as u32, &self.uniform_bind_group);
render_pass.draw_mesh_instanced(mesh, material, 0..self.instances.len() as u32, &self.camera_bind_group);
```
With all that in place we should get the following.
@ -460,12 +460,12 @@ Right now we are specifying the mesh and the material directly. This is useful i
```rust
pub trait DrawModel<'a> {
// ...
fn draw_model(&mut self, model: &'a Model, uniforms: &'a wgpu::BindGroup);
fn draw_model(&mut self, model: &'a Model, camera: &'a wgpu::BindGroup);
fn draw_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
);
}
@ -473,19 +473,19 @@ impl<'a, 'b> DrawModel<'b> for wgpu::RenderPass<'a>
where
'b: 'a, {
// ...
fn draw_model(&mut self, model: &'b Model, uniforms: &'b wgpu::BindGroup) {
self.draw_model_instanced(model, 0..1, uniforms);
fn draw_model(&mut self, model: &'b Model, camera: &'b wgpu::BindGroup) {
self.draw_model_instanced(model, 0..1, camera);
}
fn draw_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
let material = &model.materials[mesh.material];
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera);
}
}
}
@ -496,7 +496,7 @@ The code in `main.rs` will change accordingly.
```rust
render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
render_pass.set_pipeline(&self.render_pipeline);
render_pass.draw_model_instanced(&self.obj_model, 0..self.instances.len() as u32, &self.uniform_bind_group);
render_pass.draw_model_instanced(&self.obj_model, 0..self.instances.len() as u32, &self.camera_bind_group);
```
<AutoGithubLink/>

@ -85,7 +85,7 @@ Add those to `State`, and also update the `render_pipeline_layout`.
let render_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[
&texture_bind_group_layout,
&uniform_bind_group_layout,
&camera_bind_group_layout,
&light_bind_group_layout,
],
});
@ -196,7 +196,7 @@ pub trait DrawModel<'a> {
&mut self,
mesh: &'a Mesh,
material: &'a Material,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_mesh_instanced(
@ -204,21 +204,21 @@ pub trait DrawModel<'a> {
mesh: &'a Mesh,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -231,10 +231,10 @@ where
&mut self,
mesh: &'b Mesh,
material: &'b Material,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_mesh_instanced(mesh, material, 0..1, uniforms, light);
self.draw_mesh_instanced(mesh, material, 0..1, camera, light);
}
fn draw_mesh_instanced(
@ -242,13 +242,13 @@ where
mesh: &'b Mesh,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, &mesh.vertex_buffer, 0, 0);
self.set_index_buffer(&mesh.index_buffer, 0, 0);
self.set_bind_group(0, &material.bind_group, &[]);
self.set_bind_group(1, uniforms, &[]);
self.set_bind_group(1, camera, &[]);
self.set_bind_group(2, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -256,22 +256,22 @@ where
fn draw_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_model_instanced(model, 0..1, uniforms, light);
self.draw_model_instanced(model, 0..1, camera, light);
}
fn draw_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
let material = &model.materials[mesh.material];
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
}
@ -283,7 +283,7 @@ With that done we can create another render pipeline for our light.
let light_render_pipeline = {
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Light Pipeline Layout"),
bind_group_layouts: &[&uniform_bind_group_layout, &light_bind_group_layout],
bind_group_layouts: &[&camera_bind_group_layout, &light_bind_group_layout],
push_constant_ranges: &[],
});
let shader = wgpu::ShaderModuleDescriptor {
@ -310,11 +310,11 @@ With that in place we need to write the actual shaders.
// Vertex shader
[[block]]
struct Uniforms {
struct Camera {
view_proj: mat4x4<f32>;
};
[[group(0), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
[[block]]
struct Light {
@ -339,7 +339,7 @@ fn main(
) -> VertexOutput {
let scale = 0.25;
var out: VertexOutput;
out.clip_position = uniforms.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.clip_position = camera.view_proj * vec4<f32>(model.position * scale + light.position, 1.0);
out.color = light.color;
return out;
}
@ -359,28 +359,28 @@ pub trait DrawLight<'a> {
fn draw_light_mesh(
&mut self,
mesh: &'a Mesh,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_mesh_instanced(
&mut self,
mesh: &'a Mesh,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model(
&mut self,
model: &'a Model,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
fn draw_light_model_instanced(
&mut self,
model: &'a Model,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -392,22 +392,22 @@ where
fn draw_light_mesh(
&mut self,
mesh: &'b Mesh,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_mesh_instanced(mesh, 0..1, uniforms, light);
self.draw_light_mesh_instanced(mesh, 0..1, camera, light);
}
fn draw_light_mesh_instanced(
&mut self,
mesh: &'b Mesh,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
self.set_bind_group(0, uniforms, &[]);
self.set_bind_group(0, camera, &[]);
self.set_bind_group(1, light, &[]);
self.draw_indexed(0..mesh.num_elements, 0, instances);
}
@ -415,20 +415,20 @@ where
fn draw_light_model(
&mut self,
model: &'b Model,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
self.draw_light_model_instanced(model, 0..1, uniforms, light);
self.draw_light_model_instanced(model, 0..1, camera, light);
}
fn draw_light_model_instanced(
&mut self,
model: &'b Model,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_light_mesh_instanced(mesh, instances.clone(), uniforms, light);
self.draw_light_mesh_instanced(mesh, instances.clone(), camera, light);
}
}
}
@ -447,7 +447,7 @@ impl State {
render_pass.set_pipeline(&self.light_render_pipeline); // NEW!
render_pass.draw_light_model(
&self.obj_model,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
); // NEW!
@ -455,7 +455,7 @@ impl State {
render_pass.draw_model_instanced(
&self.obj_model,
0..self.instances.len() as u32,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
}
@ -550,7 +550,7 @@ fn main(
out.world_normal = model.normal;
var world_position: vec4<f32> = model_matrix * vec4<f32>(model.position, 1.0);
out.world_position = world_position.xyz;
out.clip_position = uniforms.view_proj * world_position;
out.clip_position = camera.view_proj * world_position;
return out;
}
```
@ -740,7 +740,7 @@ fn main(
out.world_normal = normal_matrix * model.normal;
var world_position: vec4<f32> = model_matrix * vec4<f32>(model.position, 1.0);
out.world_position = world_position.xyz;
out.clip_position = uniforms.view_proj * world_position;
out.clip_position = camera.view_proj * world_position;
return out;
}
```
@ -771,32 +771,32 @@ Because this is relative to the view angle, we are going to need to pass in the
```wgsl
[[block]]
struct Uniforms {
struct Camera {
view_pos: vec4<f32>;
view_proj: mat4x4<f32>;
};
[[group(1), binding(0)]]
var<uniform> uniforms: Uniforms;
var<uniform> camera: Camera;
```
<div class="note">
Don't forget to update the `Uniforms` struct in `light.wgsl` as well, as if it doesn't match the `Uniforms` struct in rust, the light will render wrong.
Don't forget to update the `Camera` struct in `light.wgsl` as well, as if it doesn't match the `CameraUniform` struct in rust, the light will render wrong.
</div>
We're going to need to update the `Uniforms` struct as well.
We're going to need to update the `CameraUniform` struct as well.
```rust
// main.rs
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Uniforms {
struct CameraUniform {
view_position: [f32; 4],
view_proj: [[f32; 4]; 4],
}
impl Uniforms {
impl CameraUniform {
fn new() -> Self {
Self {
view_position: [0.0; 4],
@ -816,7 +816,7 @@ Since we want to use our uniforms in the fragment shader now, we need to change
```rust
// main.rs
let uniform_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
let camera_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
wgpu::BindGroupLayoutBinding {
// ...
@ -833,7 +833,7 @@ We're going to get the direction from the fragment's position to the camera, and
```wgsl
// In the fragment shader...
let view_dir = normalize(uniforms.view_pos.xyz - in.world_position);
let view_dir = normalize(camera.view_pos.xyz - in.world_position);
let reflect_dir = reflect(-light_dir, in.world_normal);
```
@ -863,7 +863,7 @@ If we just look at the `specular_color` on it's own we get this.
Up to this point we've actually only implemented the Phong part of Blinn-Phong. The Phong reflection model works well, but it can break down under [certain circumstances](https://learnopengl.com/Advanced-Lighting/Advanced-Lighting). The Blinn part of Blinn-Phong comes from the realization that if you add the `view_dir`, and `light_dir` together, normalize the result and use the dot product of that and the `normal`, you get roughly the same results without the issues that using `reflect_dir` had.
```wgsl
let view_dir = normalize(uniforms.view_pos.xyz - in.world_position);
let view_dir = normalize(camera.view_pos.xyz - in.world_position);
let half_dir = normalize(view_dir + light_dir);
let specular_strength = pow(max(dot(in.world_normal, half_dir), 0.0), 32.0);

@ -369,10 +369,10 @@ fn main(
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
var out: VertexOutput;
out.clip_position = uniforms.view_proj * world_position;
out.clip_position = camera_uniform.view_proj * world_position;
out.tex_coords = model.tex_coords;
out.tangent_position = tangent_matrix * world_position.xyz;
out.tangent_view_position = tangent_matrix * uniforms.view_pos.xyz;
out.tangent_view_position = tangent_matrix * camera_uniform.view_pos.xyz;
out.tangent_light_position = tangent_matrix * light.position;
return out;
}
@ -557,7 +557,7 @@ pub trait DrawModel<'a> {
model: &'a Model,
material: &'a Material,
instances: Range<u32>,
uniforms: &'a wgpu::BindGroup,
camera: &'a wgpu::BindGroup,
light: &'a wgpu::BindGroup,
);
}
@ -572,11 +572,11 @@ where
model: &'b Model,
material: &'b Material,
instances: Range<u32>,
uniforms: &'b wgpu::BindGroup,
camera: &'b wgpu::BindGroup,
light: &'b wgpu::BindGroup,
) {
for mesh in &model.meshes {
self.draw_mesh_instanced(mesh, material, instances.clone(), uniforms, light);
self.draw_mesh_instanced(mesh, material, instances.clone(), camera, light);
}
}
}
@ -610,7 +610,7 @@ render_pass.draw_model_instanced_with_material(
&self.obj_model,
&self.debug_material,
0..self.instances.len() as u32,
&self.uniform_bind_group,
&self.camera_bind_group,
&self.light_bind_group,
);
```

@ -248,7 +248,7 @@ We need to update `update_view_proj` to use our new `Camera` and `Projection`.
```rust
impl Uniforms {
impl CameraUniform {
// ...
// UPDATED!
@ -289,7 +289,7 @@ impl State {
// ...
uniforms.update_view_proj(&camera, &projection); // UPDATED!
camera_uniform.update_view_proj(&camera, &projection); // UPDATED!
// ...
@ -403,7 +403,7 @@ The `update` function requires a bit more explanation. The `update_camera` funct
fn update(&mut self, dt: std::time::Duration) {
// UPDATED!
self.camera_controller.update_camera(&mut self.camera, dt);
self.uniforms.update_view_proj(&self.camera, &self.projection);
self.camera_uniform.update_view_proj(&self.camera, &self.projection);
// ..
}

Loading…
Cancel
Save