Static lightmap size, fix aspect ratio
This commit is contained in:
parent
137d0e0c0a
commit
718c9fdeb8
2 changed files with 19 additions and 59 deletions
|
@ -15,6 +15,9 @@ use super::pass::RenderPass;
|
||||||
use super::resources;
|
use super::resources;
|
||||||
use super::texture::Texture;
|
use super::texture::Texture;
|
||||||
|
|
||||||
|
const SHADOW_MAP_SIZE: u32 = 1024;
|
||||||
|
const SHADOW_MAP_LAYERS: u32 = 6;
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub size: winit::dpi::PhysicalSize<u32>,
|
pub size: winit::dpi::PhysicalSize<u32>,
|
||||||
|
|
||||||
|
@ -38,10 +41,8 @@ pub struct State {
|
||||||
light_debug_pass: RenderPass,
|
light_debug_pass: RenderPass,
|
||||||
light_bind_group: wgpu::BindGroup,
|
light_bind_group: wgpu::BindGroup,
|
||||||
light_depth_bind_group: wgpu::BindGroup,
|
light_depth_bind_group: wgpu::BindGroup,
|
||||||
light_depth_bind_group_layout: wgpu::BindGroupLayout,
|
|
||||||
light_depth_pass: RenderPass,
|
light_depth_pass: RenderPass,
|
||||||
light_depth_texture: Texture,
|
light_depth_texture_target_views: [TextureView; SHADOW_MAP_LAYERS as usize],
|
||||||
light_depth_texture_target_views: [TextureView; 6],
|
|
||||||
light_matrix_uniform: u32,
|
light_matrix_uniform: u32,
|
||||||
light_matrix_buffer: wgpu::Buffer,
|
light_matrix_buffer: wgpu::Buffer,
|
||||||
}
|
}
|
||||||
|
@ -136,23 +137,25 @@ impl State {
|
||||||
|
|
||||||
let depth_texture = Texture::create_depth_texture(
|
let depth_texture = Texture::create_depth_texture(
|
||||||
&device,
|
&device,
|
||||||
&config,
|
|
||||||
"depth_texture",
|
"depth_texture",
|
||||||
Some(wgpu::CompareFunction::Less),
|
Some(wgpu::CompareFunction::Less),
|
||||||
|
config.width,
|
||||||
|
config.height,
|
||||||
1,
|
1,
|
||||||
wgpu::TextureUsages::RENDER_ATTACHMENT,
|
wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||||
);
|
);
|
||||||
|
|
||||||
let light_depth_texture = Texture::create_depth_texture(
|
let light_depth_texture = Texture::create_depth_texture(
|
||||||
&device,
|
&device,
|
||||||
&config,
|
|
||||||
"light_depth_texture",
|
"light_depth_texture",
|
||||||
Some(wgpu::CompareFunction::LessEqual),
|
Some(wgpu::CompareFunction::LessEqual),
|
||||||
6,
|
SHADOW_MAP_SIZE,
|
||||||
|
SHADOW_MAP_SIZE,
|
||||||
|
SHADOW_MAP_LAYERS,
|
||||||
wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
|
wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
|
||||||
);
|
);
|
||||||
|
|
||||||
let light_depth_texture_target_views = (0..6)
|
let light_depth_texture_target_views = (0..SHADOW_MAP_LAYERS)
|
||||||
.map(|i| {
|
.map(|i| {
|
||||||
light_depth_texture.texture.create_view(&TextureViewDescriptor {
|
light_depth_texture.texture.create_view(&TextureViewDescriptor {
|
||||||
label: Some("light_depth_texture_view"),
|
label: Some("light_depth_texture_view"),
|
||||||
|
@ -169,7 +172,7 @@ impl State {
|
||||||
.try_into()
|
.try_into()
|
||||||
.expect("failed to create light depth texture views");
|
.expect("failed to create light depth texture views");
|
||||||
|
|
||||||
let light_uniform = LightUniform::new([100.0, 60.0, 0.0], [1.0, 1.0, 1.0, 200000.0]);
|
let light_uniform = LightUniform::new([100.0, 150.0, 0.0], [1.0, 1.0, 1.0, 200000.0]);
|
||||||
|
|
||||||
// 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 {
|
||||||
|
@ -426,10 +429,8 @@ impl State {
|
||||||
light_buffer,
|
light_buffer,
|
||||||
light_debug_pass,
|
light_debug_pass,
|
||||||
light_bind_group,
|
light_bind_group,
|
||||||
light_depth_bind_group,
|
light_depth_bind_group,
|
||||||
light_depth_bind_group_layout,
|
|
||||||
light_depth_pass,
|
light_depth_pass,
|
||||||
light_depth_texture,
|
|
||||||
light_depth_texture_target_views,
|
light_depth_texture_target_views,
|
||||||
light_matrix_uniform,
|
light_matrix_uniform,
|
||||||
light_matrix_buffer,
|
light_matrix_buffer,
|
||||||
|
@ -447,55 +448,13 @@ impl State {
|
||||||
.resize(new_size.width, new_size.height);
|
.resize(new_size.width, new_size.height);
|
||||||
self.depth_texture = Texture::create_depth_texture(
|
self.depth_texture = Texture::create_depth_texture(
|
||||||
&self.device,
|
&self.device,
|
||||||
&self.config,
|
|
||||||
"depth_texture",
|
"depth_texture",
|
||||||
Some(wgpu::CompareFunction::Less),
|
Some(wgpu::CompareFunction::Less),
|
||||||
|
self.config.width,
|
||||||
|
self.config.height,
|
||||||
1,
|
1,
|
||||||
wgpu::TextureUsages::RENDER_ATTACHMENT,
|
wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||||
);
|
);
|
||||||
|
|
||||||
// recreate light depth textures
|
|
||||||
self.light_depth_texture = Texture::create_depth_texture(
|
|
||||||
&self.device,
|
|
||||||
&self.config,
|
|
||||||
"light_depth_texture",
|
|
||||||
Some(wgpu::CompareFunction::LessEqual),
|
|
||||||
6,
|
|
||||||
wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
|
|
||||||
);
|
|
||||||
|
|
||||||
self.light_depth_texture_target_views = (0..6)
|
|
||||||
.map(|i| {
|
|
||||||
self.light_depth_texture.texture.create_view(&TextureViewDescriptor {
|
|
||||||
label: Some("light_depth_texture_view"),
|
|
||||||
format: None,
|
|
||||||
dimension: Some(wgpu::TextureViewDimension::D2),
|
|
||||||
aspect: wgpu::TextureAspect::All,
|
|
||||||
base_mip_level: 0,
|
|
||||||
mip_level_count: None,
|
|
||||||
base_array_layer: i as u32,
|
|
||||||
array_layer_count: NonZeroU32::new(1),
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.try_into()
|
|
||||||
.expect("failed to create light depth texture views");
|
|
||||||
|
|
||||||
self.light_depth_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
||||||
layout: &self.light_depth_bind_group_layout,
|
|
||||||
entries: &[
|
|
||||||
// depth textures
|
|
||||||
wgpu::BindGroupEntry {
|
|
||||||
binding: 0,
|
|
||||||
resource: wgpu::BindingResource::TextureView(&self.light_depth_texture.view),
|
|
||||||
},
|
|
||||||
wgpu::BindGroupEntry {
|
|
||||||
binding: 1,
|
|
||||||
resource: wgpu::BindingResource::Sampler(&self.light_depth_texture.sampler),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
label: Some("Light Depth Bind Group"),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -534,7 +493,7 @@ impl State {
|
||||||
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
||||||
|
|
||||||
// render light to depth textures
|
// render light to depth textures
|
||||||
for i in 0..6 {
|
for i in 0..SHADOW_MAP_LAYERS as usize {
|
||||||
self.light_matrix_uniform = i as u32;
|
self.light_matrix_uniform = i as u32;
|
||||||
self.queue.write_buffer(
|
self.queue.write_buffer(
|
||||||
&self.light_matrix_buffer,
|
&self.light_matrix_buffer,
|
||||||
|
|
|
@ -12,15 +12,16 @@ impl Texture {
|
||||||
|
|
||||||
pub fn create_depth_texture(
|
pub fn create_depth_texture(
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
config: &wgpu::SurfaceConfiguration,
|
|
||||||
label: &str,
|
label: &str,
|
||||||
compare: Option<wgpu::CompareFunction>,
|
compare: Option<wgpu::CompareFunction>,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
layers: u32,
|
layers: u32,
|
||||||
usage: wgpu::TextureUsages,
|
usage: wgpu::TextureUsages,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let size = wgpu::Extent3d {
|
let size = wgpu::Extent3d {
|
||||||
width: config.width,
|
width: width,
|
||||||
height: config.height,
|
height: height,
|
||||||
depth_or_array_layers: layers,
|
depth_or_array_layers: layers,
|
||||||
};
|
};
|
||||||
let desc = wgpu::TextureDescriptor {
|
let desc = wgpu::TextureDescriptor {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue