Pass light matrix as separate uniform, fix matrices
This commit is contained in:
parent
2676b841fe
commit
010e4dedeb
4 changed files with 81 additions and 41 deletions
|
@ -13,5 +13,5 @@ fn vs_main(
|
||||||
);
|
);
|
||||||
|
|
||||||
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
|
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
|
||||||
return light.matrices[light.active_matrix] * world_position;
|
return light.matrices[light_matrix_index] * world_position;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,11 +12,13 @@ struct Light {
|
||||||
position: vec3<f32>,
|
position: vec3<f32>,
|
||||||
color: vec4<f32>,
|
color: vec4<f32>,
|
||||||
matrices: array<mat4x4<f32>, 6>,
|
matrices: array<mat4x4<f32>, 6>,
|
||||||
active_matrix: u32,
|
|
||||||
}
|
}
|
||||||
@group(1) @binding(0)
|
@group(1) @binding(0)
|
||||||
var<uniform> light: Light;
|
var<uniform> light: Light;
|
||||||
|
|
||||||
|
@group(1) @binding(1)
|
||||||
|
var<uniform> light_matrix_index: u32;
|
||||||
|
|
||||||
struct VertexInput {
|
struct VertexInput {
|
||||||
@location(0) position: vec3<f32>,
|
@location(0) position: vec3<f32>,
|
||||||
@location(1) tex_coords: vec2<f32>,
|
@location(1) tex_coords: vec2<f32>,
|
||||||
|
@ -46,7 +48,7 @@ struct VertexOutput {
|
||||||
|
|
||||||
// Fragment shader
|
// Fragment shader
|
||||||
|
|
||||||
@group(1)@binding(1)
|
@group(1)@binding(2)
|
||||||
var t_light_depth: binding_array<texture_depth_2d>;
|
var t_light_depth: binding_array<texture_depth_2d>;
|
||||||
@group(1) @binding(2)
|
@group(1) @binding(3)
|
||||||
var s_light_depth: binding_array<sampler_comparison>;
|
var s_light_depth: binding_array<sampler_comparison>;
|
||||||
|
|
|
@ -11,7 +11,6 @@ pub struct LightUniform {
|
||||||
_padding: u32,
|
_padding: u32,
|
||||||
pub color: [f32; 4],
|
pub color: [f32; 4],
|
||||||
pub matrices: [[[f32; 4]; 4]; 6],
|
pub matrices: [[[f32; 4]; 4]; 6],
|
||||||
pub active_matrix: u32,
|
|
||||||
_padding2: [u32; 3],
|
_padding2: [u32; 3],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,12 +19,12 @@ impl LightUniform {
|
||||||
let proj = cgmath::perspective(cgmath::Deg(90.0), 1.0, 0.1, 1000.0);
|
let proj = cgmath::perspective(cgmath::Deg(90.0), 1.0, 0.1, 1000.0);
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
let matrices: [[[f32; 4]; 4]; 6] = [
|
let matrices: [[[f32; 4]; 4]; 6] = [
|
||||||
(proj * Matrix4::look_to_rh(position.into(), Vector3::new( 1.0, 0.0, 0.0), Vector3::new(0.0,-1.0, 0.0))).into(),
|
(proj * Matrix4::look_to_rh(position.into(), Vector3::new( 1.0, 0.0, 0.0), Vector3::new(0.0, 1.0, 0.0))).into(),
|
||||||
(proj * Matrix4::look_to_rh(position.into(), Vector3::new(-1.0, 0.0, 0.0), Vector3::new(0.0,-1.0, 0.0))).into(),
|
(proj * Matrix4::look_to_rh(position.into(), Vector3::new(-1.0, 0.0, 0.0), Vector3::new(0.0, 1.0, 0.0))).into(),
|
||||||
(proj * Matrix4::look_to_rh(position.into(), Vector3::new( 0.0, 1.0, 0.0), Vector3::new(0.0, 0.0, 1.0))).into(),
|
(proj * Matrix4::look_to_rh(position.into(), Vector3::new( 0.0, 1.0, 0.0), Vector3::new(1.0, 0.0, 0.0))).into(),
|
||||||
(proj * Matrix4::look_to_rh(position.into(), Vector3::new( 0.0,-1.0, 0.0), Vector3::new(0.0, 0.0,-1.0))).into(),
|
(proj * Matrix4::look_to_rh(position.into(), Vector3::new( 0.0,-1.0, 0.0), Vector3::new(-1.0, 0.0, 0.0))).into(),
|
||||||
(proj * Matrix4::look_to_rh(position.into(), Vector3::new( 0.0, 0.0, 1.0), Vector3::new(0.0,-1.0, 0.0))).into(),
|
(proj * Matrix4::look_to_rh(position.into(), Vector3::new( 0.0, 0.0, 1.0), Vector3::new(0.0, 1.0, 0.0))).into(),
|
||||||
(proj * Matrix4::look_to_rh(position.into(), Vector3::new( 0.0, 0.0,-1.0), Vector3::new(0.0,-1.0, 0.0))).into(),
|
(proj * Matrix4::look_to_rh(position.into(), Vector3::new( 0.0, 0.0,-1.0), Vector3::new(0.0, 1.0, 0.0))).into(),
|
||||||
];
|
];
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
@ -33,7 +32,6 @@ impl LightUniform {
|
||||||
_padding: 0,
|
_padding: 0,
|
||||||
color,
|
color,
|
||||||
matrices: matrices,
|
matrices: matrices,
|
||||||
active_matrix: 0,
|
|
||||||
_padding2: [0, 0, 0]
|
_padding2: [0, 0, 0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,8 @@ pub struct State {
|
||||||
light_bind_group: wgpu::BindGroup,
|
light_bind_group: wgpu::BindGroup,
|
||||||
light_depth_pass: RenderPass,
|
light_depth_pass: RenderPass,
|
||||||
light_depth_textures: [Texture; 6],
|
light_depth_textures: [Texture; 6],
|
||||||
|
light_matrix_uniform: u32,
|
||||||
|
light_matrix_buffer: wgpu::Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
|
@ -167,11 +169,18 @@ impl State {
|
||||||
|
|
||||||
// We'll want to update our lights position, so we use COPY_DST
|
// We'll want to update our lights position, so we use COPY_DST
|
||||||
let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
label: Some("Light VB"),
|
label: Some("Light UB"),
|
||||||
contents: bytemuck::cast_slice(&[light_uniform]),
|
contents: bytemuck::cast_slice(&[light_uniform]),
|
||||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let light_matrix_uniform = 0;
|
||||||
|
let light_matrix_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some("Light Matrix UB"),
|
||||||
|
contents: bytemuck::cast_slice(&[light_matrix_uniform]),
|
||||||
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
|
});
|
||||||
|
|
||||||
let light_bind_group_layout =
|
let light_bind_group_layout =
|
||||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
entries: &[
|
entries: &[
|
||||||
|
@ -186,9 +195,20 @@ impl State {
|
||||||
},
|
},
|
||||||
count: None,
|
count: None,
|
||||||
},
|
},
|
||||||
// depth
|
// matrix index
|
||||||
wgpu::BindGroupLayoutEntry {
|
wgpu::BindGroupLayoutEntry {
|
||||||
binding: 1,
|
binding: 1,
|
||||||
|
visibility: wgpu::ShaderStages::VERTEX,
|
||||||
|
ty: wgpu::BindingType::Buffer {
|
||||||
|
ty: wgpu::BufferBindingType::Uniform,
|
||||||
|
has_dynamic_offset: false,
|
||||||
|
min_binding_size: None,
|
||||||
|
},
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
// depth textures
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 2,
|
||||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
ty: wgpu::BindingType::Texture {
|
ty: wgpu::BindingType::Texture {
|
||||||
multisampled: false,
|
multisampled: false,
|
||||||
|
@ -198,7 +218,7 @@ impl State {
|
||||||
count: NonZeroU32::new(6),
|
count: NonZeroU32::new(6),
|
||||||
},
|
},
|
||||||
wgpu::BindGroupLayoutEntry {
|
wgpu::BindGroupLayoutEntry {
|
||||||
binding: 2,
|
binding: 3,
|
||||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison),
|
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison),
|
||||||
count: NonZeroU32::new(6),
|
count: NonZeroU32::new(6),
|
||||||
|
@ -210,16 +230,23 @@ impl State {
|
||||||
let light_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
let light_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
layout: &light_bind_group_layout,
|
layout: &light_bind_group_layout,
|
||||||
entries: &[
|
entries: &[
|
||||||
|
// light struct
|
||||||
wgpu::BindGroupEntry {
|
wgpu::BindGroupEntry {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
resource: light_buffer.as_entire_binding(),
|
resource: light_buffer.as_entire_binding(),
|
||||||
},
|
},
|
||||||
|
// matrix index
|
||||||
wgpu::BindGroupEntry {
|
wgpu::BindGroupEntry {
|
||||||
binding: 1,
|
binding: 1,
|
||||||
|
resource: light_matrix_buffer.as_entire_binding(),
|
||||||
|
},
|
||||||
|
// depth textures
|
||||||
|
wgpu::BindGroupEntry {
|
||||||
|
binding: 2,
|
||||||
resource: wgpu::BindingResource::TextureViewArray(&light_depth_texture_views),
|
resource: wgpu::BindingResource::TextureViewArray(&light_depth_texture_views),
|
||||||
},
|
},
|
||||||
wgpu::BindGroupEntry {
|
wgpu::BindGroupEntry {
|
||||||
binding: 2,
|
binding: 3,
|
||||||
resource: wgpu::BindingResource::SamplerArray(&light_depth_texture_samplers),
|
resource: wgpu::BindingResource::SamplerArray(&light_depth_texture_samplers),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
@ -380,6 +407,8 @@ impl State {
|
||||||
light_bind_group,
|
light_bind_group,
|
||||||
light_depth_pass,
|
light_depth_pass,
|
||||||
light_depth_textures,
|
light_depth_textures,
|
||||||
|
light_matrix_uniform,
|
||||||
|
light_matrix_buffer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -444,27 +473,25 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
||||||
let surface_texture = self.surface.get_current_texture()?;
|
|
||||||
let surface_view = surface_texture
|
|
||||||
.texture
|
|
||||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
|
||||||
let mut encoder = self
|
|
||||||
.device
|
|
||||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
|
||||||
label: Some("Render Encoder"),
|
|
||||||
});
|
|
||||||
|
|
||||||
encoder.push_debug_group("shadow passes");
|
// render light to depth textures
|
||||||
for i in 0..6 {
|
for i in 0..6 {
|
||||||
self.light_uniform.active_matrix = i as u32;
|
self.light_matrix_uniform = i as u32;
|
||||||
self.queue.write_buffer(
|
self.queue.write_buffer(
|
||||||
&self.light_buffer,
|
&self.light_matrix_buffer,
|
||||||
0,
|
0,
|
||||||
bytemuck::cast_slice(&[self.light_uniform]),
|
bytemuck::cast_slice(&[self.light_matrix_uniform]),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut light_depth_render_pass =
|
let mut depth_encoder = self
|
||||||
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
.device
|
||||||
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||||
|
label: Some("Depth Encoder"),
|
||||||
|
});
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut light_depth_render_pass =
|
||||||
|
depth_encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
label: Some("Light Depth Render Pass"),
|
label: Some("Light Depth Render Pass"),
|
||||||
color_attachments: &[],
|
color_attachments: &[],
|
||||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||||
|
@ -477,17 +504,30 @@ impl State {
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
light_depth_render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
|
light_depth_render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
|
||||||
light_depth_render_pass.set_pipeline(&self.light_depth_pass.pipeline);
|
light_depth_render_pass.set_pipeline(&self.light_depth_pass.pipeline);
|
||||||
// TODO separate func
|
// TODO separate func
|
||||||
light_depth_render_pass.draw_model_instanced(
|
light_depth_render_pass.draw_model_instanced(
|
||||||
&self.model,
|
&self.model,
|
||||||
0..self.instances.len() as u32,
|
0..self.instances.len() as u32,
|
||||||
&self.camera_bind_group,
|
&self.camera_bind_group,
|
||||||
&self.light_bind_group,
|
&self.light_bind_group,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.queue.submit(std::iter::once(depth_encoder.finish()));
|
||||||
}
|
}
|
||||||
encoder.pop_debug_group();
|
|
||||||
|
// render geometry
|
||||||
|
let surface_texture = self.surface.get_current_texture()?;
|
||||||
|
let surface_view = surface_texture
|
||||||
|
.texture
|
||||||
|
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
let mut encoder = self
|
||||||
|
.device
|
||||||
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||||
|
label: Some("Render Encoder"),
|
||||||
|
});
|
||||||
|
|
||||||
encoder.push_debug_group("geometry pass");
|
encoder.push_debug_group("geometry pass");
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue