Add depth buffer
This commit is contained in:
parent
25aff0cc1f
commit
3d350448dc
|
@ -81,7 +81,7 @@ async fn main() {
|
||||||
shader_id: 0,
|
shader_id: 0,
|
||||||
texture: diffuse_texture.clone()
|
texture: diffuse_texture.clone()
|
||||||
}),
|
}),
|
||||||
TransformComponent::from(Transform::from_xyz(0.005, 0.0, 0.0)),
|
TransformComponent::from(Transform::from_xyz(0.005, 0.0, -2.0)),
|
||||||
));
|
));
|
||||||
|
|
||||||
world.spawn((MeshComponent::new(
|
world.spawn((MeshComponent::new(
|
||||||
|
@ -92,7 +92,7 @@ async fn main() {
|
||||||
shader_id: 0,
|
shader_id: 0,
|
||||||
texture: diffuse_texture
|
texture: diffuse_texture
|
||||||
}),
|
}),
|
||||||
TransformComponent::from(Transform::from_xyz(0.005, 0.7, 0.0)),
|
TransformComponent::from(Transform::from_xyz(0.005, 0.7, -0.5)),
|
||||||
));
|
));
|
||||||
|
|
||||||
let mut camera = CameraComponent::new();
|
let mut camera = CameraComponent::new();
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::{ops::Range, cell::Ref};
|
||||||
|
|
||||||
use wgpu::{PipelineLayout, RenderPipeline, RenderPass, VertexBufferLayout, BindGroupLayout};
|
use wgpu::{PipelineLayout, RenderPipeline, RenderPass, VertexBufferLayout, BindGroupLayout};
|
||||||
|
|
||||||
use super::{render_job::RenderJob, vertex::Vertex, desc_buf_lay::DescVertexBufferLayout};
|
use super::{render_job::RenderJob, vertex::Vertex, desc_buf_lay::DescVertexBufferLayout, texture::RenderTexture};
|
||||||
|
|
||||||
pub struct FullRenderPipeline {
|
pub struct FullRenderPipeline {
|
||||||
layout: PipelineLayout,
|
layout: PipelineLayout,
|
||||||
|
@ -59,7 +59,13 @@ impl FullRenderPipeline {
|
||||||
// Requires Features::CONSERVATIVE_RASTERIZATION
|
// Requires Features::CONSERVATIVE_RASTERIZATION
|
||||||
conservative: false,
|
conservative: false,
|
||||||
},
|
},
|
||||||
depth_stencil: None,
|
depth_stencil: Some(wgpu::DepthStencilState {
|
||||||
|
format: RenderTexture::DEPTH_FORMAT,
|
||||||
|
depth_write_enabled: true,
|
||||||
|
depth_compare: wgpu::CompareFunction::Less,
|
||||||
|
stencil: wgpu::StencilState::default(), // TODO: stencil buffer
|
||||||
|
bias: wgpu::DepthBiasState::default(),
|
||||||
|
}),
|
||||||
multisample: wgpu::MultisampleState {
|
multisample: wgpu::MultisampleState {
|
||||||
count: 1,
|
count: 1,
|
||||||
mask: !0,
|
mask: !0,
|
||||||
|
|
|
@ -159,6 +159,8 @@ pub struct BasicRenderer {
|
||||||
inuse_camera: RenderCamera,
|
inuse_camera: RenderCamera,
|
||||||
camera_buffer: wgpu::Buffer,
|
camera_buffer: wgpu::Buffer,
|
||||||
camera_bind_group: wgpu::BindGroup,
|
camera_bind_group: wgpu::BindGroup,
|
||||||
|
|
||||||
|
depth_buffer_texture: RenderTexture,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BasicRenderer {
|
impl BasicRenderer {
|
||||||
|
@ -344,6 +346,8 @@ impl BasicRenderer {
|
||||||
label: Some("camera_bind_group"),
|
label: Some("camera_bind_group"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let depth_texture = RenderTexture::create_depth_texture(&device, &config, "Depth Buffer");
|
||||||
|
|
||||||
let mut pipelines = HashMap::new();
|
let mut pipelines = HashMap::new();
|
||||||
pipelines.insert(0, Arc::new(FullRenderPipeline::new(&device, &config, &shader,
|
pipelines.insert(0, Arc::new(FullRenderPipeline::new(&device, &config, &shader,
|
||||||
vec![super::vertex::Vertex::desc(),],
|
vec![super::vertex::Vertex::desc(),],
|
||||||
|
@ -373,6 +377,8 @@ impl BasicRenderer {
|
||||||
inuse_camera: RenderCamera::new(size),
|
inuse_camera: RenderCamera::new(size),
|
||||||
camera_buffer,
|
camera_buffer,
|
||||||
camera_bind_group,
|
camera_bind_group,
|
||||||
|
|
||||||
|
depth_buffer_texture: depth_texture,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -624,7 +630,15 @@ impl Renderer for BasicRenderer {
|
||||||
store: true,
|
store: true,
|
||||||
},
|
},
|
||||||
})],
|
})],
|
||||||
depth_stencil_attachment: None,
|
// enable depth buffer
|
||||||
|
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||||
|
view: self.depth_buffer_texture.view(),
|
||||||
|
depth_ops: Some(wgpu::Operations {
|
||||||
|
load: wgpu::LoadOp::Clear(1.0),
|
||||||
|
store: true,
|
||||||
|
}),
|
||||||
|
stencil_ops: None,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Pop off jobs from the queue as they're being processed
|
// Pop off jobs from the queue as they're being processed
|
||||||
|
@ -679,8 +693,10 @@ impl Renderer for BasicRenderer {
|
||||||
self.size = new_size;
|
self.size = new_size;
|
||||||
self.config.width = new_size.width;
|
self.config.width = new_size.width;
|
||||||
self.config.height = new_size.height;
|
self.config.height = new_size.height;
|
||||||
self.surface.configure(&self.device, &self.config);
|
|
||||||
|
|
||||||
|
// tell other things of updated resize
|
||||||
|
self.surface.configure(&self.device, &self.config);
|
||||||
|
self.depth_buffer_texture = RenderTexture::create_depth_texture(&self.device, &self.config, "Depth Buffer Texture");
|
||||||
self.inuse_camera.update_aspect_ratio(self.size);
|
self.inuse_camera.update_aspect_ratio(self.size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,8 @@ pub struct RenderTexture {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenderTexture {
|
impl RenderTexture {
|
||||||
|
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
||||||
|
|
||||||
pub fn from_bytes(device: &wgpu::Device, queue: &wgpu::Queue, bytes: &[u8], texture_id: u32, label: &str) -> anyhow::Result<Self> {
|
pub fn from_bytes(device: &wgpu::Device, queue: &wgpu::Queue, bytes: &[u8], texture_id: u32, label: &str) -> anyhow::Result<Self> {
|
||||||
let img = image::load_from_memory(bytes)?;
|
let img = image::load_from_memory(bytes)?;
|
||||||
Self::from_image(device, queue, &img, texture_id, Some(label))
|
Self::from_image(device, queue, &img, texture_id, Some(label))
|
||||||
|
@ -121,6 +123,49 @@ impl RenderTexture {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 // we'll be rendering to it
|
||||||
|
| wgpu::TextureUsages::TEXTURE_BINDING,
|
||||||
|
view_formats: &[],
|
||||||
|
};
|
||||||
|
let texture = device.create_texture(&desc);
|
||||||
|
|
||||||
|
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
let sampler = device.create_sampler(
|
||||||
|
&wgpu::SamplerDescriptor { // 4.
|
||||||
|
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: 0.0,
|
||||||
|
lod_max_clamp: 100.0,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
texture: Arc::new(texture),
|
||||||
|
view: Arc::new(view),
|
||||||
|
sampler: Arc::new(sampler),
|
||||||
|
texture_id: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn inner_texture(&self) -> &wgpu::Texture {
|
pub fn inner_texture(&self) -> &wgpu::Texture {
|
||||||
&self.texture
|
&self.texture
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue