Depth buffer

This commit is contained in:
Werner
2021-11-03 19:32:28 -03:00
parent a0392e676b
commit 7792887fba
2 changed files with 71 additions and 5 deletions

View File

@ -99,6 +99,7 @@ struct State {
camera_controller: CameraController,
instances: Vec<Instance>,
instance_buffer: wgpu::Buffer,
depth_texture: Texture,
}
impl State {
@ -290,6 +291,8 @@ impl State {
label: Some("camera_bind_group"),
});
let depth_texture = Texture::create_depth_texture(&device, &config, "depth_texture");
// Shader
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
@ -337,11 +340,17 @@ impl State {
// Requires Features::CONSERVATIVE_RASTERIZATION
conservative: false,
},
depth_stencil: None, // 1.
depth_stencil: Some(wgpu::DepthStencilState {
format: Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState {
count: 1, // 2.
mask: !0, // 3.
alpha_to_coverage_enabled: false, // 4.
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
});
@ -415,6 +424,7 @@ impl State {
camera_controller,
instances,
instance_buffer,
depth_texture,
}
}
@ -425,6 +435,8 @@ impl State {
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
}
self.depth_texture =
Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
}
fn input(&mut self, event: &WindowEvent) -> bool {
@ -472,7 +484,14 @@ impl State {
},
},
],
depth_stencil_attachment: None,
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth_texture.view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: true,
}),
stencil_ops: None,
}),
});
render_pass.set_pipeline(&self.render_pipeline);

View File

@ -8,6 +8,8 @@ pub struct Texture {
}
impl Texture {
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
pub fn from_bytes(
device: &wgpu::Device,
queue: &wgpu::Queue,
@ -77,4 +79,49 @@ impl Texture {
sampler,
})
}
pub fn create_depth_texture(
device: &wgpu::Device,
config: &wgpu::SurfaceConfiguration,
label: &str,
) -> Self {
let size = wgpu::Extent3d {
width: config.width,
height: config.height,
depth_or_array_layers: 1,
};
let desc = wgpu::TextureDescriptor {
label: Some(label),
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
};
let texture = device.create_texture(&desc);
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Nearest,
compare: Some(wgpu::CompareFunction::LessEqual),
lod_min_clamp: -100.0,
lod_max_clamp: 100.0,
..Default::default()
});
Self {
texture,
view,
sampler,
}
}
}