823: Fix crash-on-resize in skybox example r=kvark a=danwilhelm

Problem: Skybox example currently crashes on resize. 
- Solution: Now, recreates depth texture on resize event.
---
Problem: Shadow example currently duplicates texture creation code on resize. 
- Solution: Now, shadow is refactored identically to skybox & water examples -- minimizes code duplication, makes examples more similar.

Co-authored-by: Dan Wilhelm <dan@danwilhelm.com>
This commit is contained in:
bors[bot]
2021-03-30 03:55:48 +00:00
committed by GitHub
2 changed files with 54 additions and 46 deletions

View File

@@ -185,6 +185,27 @@ impl Example {
let mx_correction = framework::OPENGL_TO_WGPU_MATRIX;
mx_correction * mx_projection * mx_view
}
fn create_depth_texture(
sc_desc: &wgpu::SwapChainDescriptor,
device: &wgpu::Device,
) -> wgpu::TextureView {
let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width: sc_desc.width,
height: sc_desc.height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
label: None,
});
depth_texture.create_view(&wgpu::TextureViewDescriptor::default())
}
}
impl framework::Example for Example {
@@ -637,19 +658,7 @@ impl framework::Example for Example {
}
};
let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width: sc_desc.width,
height: sc_desc.height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
label: None,
});
let forward_depth = Self::create_depth_texture(sc_desc, device);
Example {
entities,
@@ -657,7 +666,7 @@ impl framework::Example for Example {
lights_are_dirty: true,
shadow_pass,
forward_pass,
forward_depth: depth_texture.create_view(&wgpu::TextureViewDescriptor::default()),
forward_depth,
light_storage_buf,
entity_uniform_buf,
entity_bind_group,
@@ -683,20 +692,7 @@ impl framework::Example for Example {
bytemuck::cast_slice(mx_ref),
);
let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width: sc_desc.width,
height: sc_desc.height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
label: None,
});
self.forward_depth = depth_texture.create_view(&wgpu::TextureViewDescriptor::default());
self.forward_depth = Self::create_depth_texture(sc_desc, device);
}
fn render(

View File

@@ -69,7 +69,30 @@ pub struct Skybox {
staging_belt: wgpu::util::StagingBelt,
}
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth24Plus;
impl Skybox {
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth24Plus;
fn create_depth_texture(
sc_desc: &wgpu::SwapChainDescriptor,
device: &wgpu::Device,
) -> wgpu::TextureView {
let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width: sc_desc.width,
height: sc_desc.height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
label: None,
});
depth_texture.create_view(&wgpu::TextureViewDescriptor::default())
}
}
impl framework::Example for Skybox {
fn optional_features() -> wgpu::Features {
@@ -204,7 +227,7 @@ impl framework::Example for Skybox {
..Default::default()
},
depth_stencil: Some(wgpu::DepthStencilState {
format: DEPTH_FORMAT,
format: Self::DEPTH_FORMAT,
depth_write_enabled: false,
depth_compare: wgpu::CompareFunction::LessEqual,
stencil: wgpu::StencilState::default(),
@@ -235,7 +258,7 @@ impl framework::Example for Skybox {
..Default::default()
},
depth_stencil: Some(wgpu::DepthStencilState {
format: DEPTH_FORMAT,
format: Self::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::LessEqual,
stencil: wgpu::StencilState::default(),
@@ -337,19 +360,7 @@ impl framework::Example for Skybox {
label: None,
});
let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width: sc_desc.width,
height: sc_desc.height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: DEPTH_FORMAT,
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
label: None,
});
let depth_view = Self::create_depth_texture(sc_desc, device);
Skybox {
camera,
@@ -358,7 +369,7 @@ impl framework::Example for Skybox {
bind_group,
uniform_buf,
entities,
depth_view: depth_texture.create_view(&wgpu::TextureViewDescriptor::default()),
depth_view,
staging_belt: wgpu::util::StagingBelt::new(0x100),
}
}
@@ -379,9 +390,10 @@ impl framework::Example for Skybox {
fn resize(
&mut self,
sc_desc: &wgpu::SwapChainDescriptor,
_device: &wgpu::Device,
device: &wgpu::Device,
_queue: &wgpu::Queue,
) {
self.depth_view = Self::create_depth_texture(sc_desc, device);
self.camera.screen_size = (sc_desc.width, sc_desc.height);
}