WIP shadowmapping

This commit is contained in:
Lauri Räsänen 2023-01-30 00:40:50 +02:00
parent 43b4eaf5ab
commit c5e32a830d
7 changed files with 145 additions and 91 deletions

View file

@ -27,35 +27,6 @@ impl PerspectiveProjection {
}
}
pub struct OrthoProjection {
pub left: f32,
pub right: f32,
pub bottom: f32,
pub top: f32,
pub znear: f32,
pub zfar: f32,
}
impl OrthoProjection {
pub fn resize(&mut self, width: u32, height: u32) {
self.left = width as f32 * -0.5;
self.right = width as f32 * 0.5;
self.bottom = height as f32 * -0.5;
self.top = height as f32 * 0.5;
}
pub fn get_matrix(&self) -> cgmath::Matrix4<f32> {
cgmath::ortho(
self.left,
self.right,
self.bottom,
self.top,
self.znear,
self.zfar,
)
}
}
impl Camera {
pub fn new(
position: cgmath::Point3<f32>,

View file

@ -2,20 +2,39 @@ use std::ops::Range;
use super::model::{Mesh, Model};
use cgmath::{Matrix4, Vector3};
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct LightUniform {
pub position: [f32; 3],
_padding: u32,
pub color: [f32; 4],
pub matrices: [[[f32; 4]; 4]; 6],
pub active_matrix: u32,
_padding2: [u32; 3],
}
impl LightUniform {
pub fn new(position: [f32; 3], color: [f32; 4]) -> Self {
let proj = cgmath::perspective(cgmath::Deg(90.0), 1.0, 0.1, 1000.0);
#[rustfmt::skip]
let matrices: [[[f32; 4]; 4]; 6] = [
(proj * Matrix4::look_to_rh(position.into(), Into::<Vector3<f32>>::into(position) + Vector3::new( 1.0, 0.0, 0.0), Vector3::new(0.0,-1.0, 0.0))).into(),
(proj * Matrix4::look_to_rh(position.into(), Into::<Vector3<f32>>::into(position) + Vector3::new(-1.0, 0.0, 0.0), Vector3::new(0.0,-1.0, 0.0))).into(),
(proj * Matrix4::look_to_rh(position.into(), Into::<Vector3<f32>>::into(position) + Vector3::new( 0.0, 1.0, 0.0), Vector3::new(0.0, 0.0, 1.0))).into(),
(proj * Matrix4::look_to_rh(position.into(), Into::<Vector3<f32>>::into(position) + Vector3::new( 0.0,-1.0, 0.0), Vector3::new(0.0, 0.0,-1.0))).into(),
(proj * Matrix4::look_to_rh(position.into(), Into::<Vector3<f32>>::into(position) + Vector3::new( 0.0, 0.0, 1.0), Vector3::new(0.0,-1.0, 0.0))).into(),
(proj * Matrix4::look_to_rh(position.into(), Into::<Vector3<f32>>::into(position) + Vector3::new( 0.0, 0.0,-1.0), Vector3::new(0.0,-1.0, 0.0))).into(),
];
Self {
position,
_padding: 0,
color,
matrices: matrices,
active_matrix: 0,
_padding2: [0, 0, 0]
}
}
}

View file

@ -1,4 +1,5 @@
use cgmath::prelude::*;
use std::num::NonZeroU32;
use std::time::Duration;
use wgpu::util::DeviceExt;
@ -35,7 +36,7 @@ pub struct State {
light_debug_pass: RenderPass,
light_bind_group: wgpu::BindGroup,
light_depth_pass: RenderPass,
light_depth_texture: Texture,
light_depth_textures: [Texture; 6],
}
impl State {
@ -57,7 +58,8 @@ impl State {
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
features: wgpu::Features::empty(),
features: wgpu::Features::TEXTURE_BINDING_ARRAY
| wgpu::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING,
limits: if cfg!(target_arch = "wasm32") {
wgpu::Limits::downlevel_webgl2_defaults()
} else {
@ -128,13 +130,34 @@ impl State {
&config,
"depth_texture",
Some(wgpu::CompareFunction::LessEqual),
1,
);
let light_depth_texture = Texture::create_depth_texture(
&device,
&config,
"light_depth_texture",
Some(wgpu::CompareFunction::LessEqual),
);
let light_depth_textures: [Texture; 6] = (0..6)
.map(|i| {
Texture::create_depth_texture(
&device,
&config,
format!("light_depth_texture_{}", i).as_str(),
Some(wgpu::CompareFunction::LessEqual),
1,
)
})
.collect::<Vec<_>>()
.try_into()
.expect("failed to create light depth textures");
let light_depth_texture_views: [&wgpu::TextureView; 6] = (0..6)
.map(|i| &light_depth_textures[i].view)
.collect::<Vec<_>>()
.try_into()
.expect("failed to create light depth texture views");
let light_depth_texture_samplers: [&wgpu::Sampler; 6] = (0..6)
.map(|i| &light_depth_textures[i].sampler)
.collect::<Vec<_>>()
.try_into()
.expect("failed to create light depth texture samplers");
let light_uniform = LightUniform::new([100.0, 60.0, 0.0], [1.0, 1.0, 1.0, 200000.0]);
@ -148,6 +171,7 @@ impl State {
let light_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
// LightUniform
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
@ -167,13 +191,13 @@ impl State {
view_dimension: wgpu::TextureViewDimension::D2,
sample_type: wgpu::TextureSampleType::Depth,
},
count: None,
count: NonZeroU32::new(6),
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison),
count: None,
count: NonZeroU32::new(6),
},
],
label: Some("Light Bind Group Layout"),
@ -188,11 +212,11 @@ impl State {
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&light_depth_texture.view),
resource: wgpu::BindingResource::TextureViewArray(&light_depth_texture_views),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&light_depth_texture.sampler),
resource: wgpu::BindingResource::SamplerArray(&light_depth_texture_samplers),
},
],
label: Some("Light Bind Group"),
@ -351,7 +375,7 @@ impl State {
light_debug_pass,
light_bind_group,
light_depth_pass,
light_depth_texture,
light_depth_textures,
}
}
@ -369,13 +393,17 @@ impl State {
&self.config,
"depth_texture",
Some(wgpu::CompareFunction::LessEqual),
1,
);
self.light_depth_texture = Texture::create_depth_texture(
&self.device,
&self.config,
"light_depth_texture",
Some(wgpu::CompareFunction::LessEqual),
);
for i in 0..6 {
self.light_depth_textures[i] = Texture::create_depth_texture(
&self.device,
&self.config,
format!("light_depth_texture_{}", i).as_str(),
Some(wgpu::CompareFunction::LessEqual),
1,
);
}
}
}
@ -422,14 +450,20 @@ impl State {
label: Some("Render Encoder"),
});
/*
{
for i in 0..6 {
self.light_uniform.active_matrix = i as u32;
self.queue.write_buffer(
&self.light_buffer,
0,
bytemuck::cast_slice(&[self.light_uniform]),
);
let mut light_depth_render_pass =
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Light Depth Render Pass"),
color_attachments: &[],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.light_depth_texture.view,
view: &self.light_depth_textures[i].view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: true,
@ -440,14 +474,14 @@ impl State {
light_depth_render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
light_depth_render_pass.set_pipeline(&self.light_depth_pass.pipeline);
// TODO separate func
light_depth_render_pass.draw_model_instanced(
&self.model,
&self.light_model,
0..self.instances.len() as u32,
&self.camera_bind_group, // TODO light projection
&self.light_bind_group, // TODO remove
&self.camera_bind_group,
&self.light_bind_group,
);
}
*/
{
let mut geom_render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {

View file

@ -1,5 +1,6 @@
use anyhow::*;
#[derive(Debug)]
pub struct Texture {
pub texture: wgpu::Texture,
pub view: wgpu::TextureView,
@ -14,11 +15,12 @@ impl Texture {
config: &wgpu::SurfaceConfiguration,
label: &str,
compare: Option<wgpu::CompareFunction>,
layers: u32,
) -> Self {
let size = wgpu::Extent3d {
width: config.width,
height: config.height,
depth_or_array_layers: 1,
depth_or_array_layers: layers,
};
let desc = wgpu::TextureDescriptor {
label: Some(label),