add depth buffer
This commit is contained in:
parent
a8f72096d8
commit
295037edc6
56
src/lib.rs
56
src/lib.rs
|
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
camera::OrbitalCamera,
|
camera::OrbitalCamera,
|
||||||
ray::Ray,
|
ray::Ray,
|
||||||
render::{
|
render::{
|
||||||
OUTPUT_FORMAT,
|
DEPTH_FORMAT, OUTPUT_FORMAT,
|
||||||
lines::{LookParams, Mesh, Pipeline, Vertex},
|
lines::{LookParams, Mesh, Pipeline, Vertex},
|
||||||
},
|
},
|
||||||
trace::{Hit, Lambertian, Reflector, Scene, Source, Sphere},
|
trace::{Hit, Lambertian, Reflector, Scene, Source, Sphere},
|
||||||
|
|
@ -67,6 +67,8 @@ pub struct Core {
|
||||||
queue: wgpu::Queue,
|
queue: wgpu::Queue,
|
||||||
surface: wgpu::Surface<'static>,
|
surface: wgpu::Surface<'static>,
|
||||||
|
|
||||||
|
depth: wgpu::Texture,
|
||||||
|
|
||||||
pipeline: Pipeline,
|
pipeline: Pipeline,
|
||||||
mesh_pipe: render::faces::Pipeline,
|
mesh_pipe: render::faces::Pipeline,
|
||||||
tripod: Mesh,
|
tripod: Mesh,
|
||||||
|
|
@ -134,18 +136,20 @@ impl Core {
|
||||||
let pipeline = Pipeline::new(&device);
|
let pipeline = Pipeline::new(&device);
|
||||||
let mesh_pipe = render::faces::Pipeline::new(&device);
|
let mesh_pipe = render::faces::Pipeline::new(&device);
|
||||||
let tripod = new_tripod(&device);
|
let tripod = new_tripod(&device);
|
||||||
|
let depth = Self::create_depth_buffer(&device, pixel_size);
|
||||||
queue.submit([]); // flush buffer updates
|
queue.submit([]); // flush buffer updates
|
||||||
|
|
||||||
let mut this = Self {
|
Self::configure_surface(&surface, &device, pixel_size);
|
||||||
|
|
||||||
|
Self {
|
||||||
device,
|
device,
|
||||||
queue,
|
queue,
|
||||||
surface,
|
surface,
|
||||||
|
depth,
|
||||||
pipeline,
|
pipeline,
|
||||||
tripod,
|
tripod,
|
||||||
mesh_pipe,
|
mesh_pipe,
|
||||||
};
|
}
|
||||||
this.configure(pixel_size);
|
|
||||||
this
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(&self, output: &wgpu::Texture, args: &RedrawArgs) {
|
fn render(&self, output: &wgpu::Texture, args: &RedrawArgs) {
|
||||||
|
|
@ -176,6 +180,9 @@ impl Core {
|
||||||
self.queue.submit([]); // flush buffer updates
|
self.queue.submit([]); // flush buffer updates
|
||||||
|
|
||||||
let view = output.create_view(&wgpu::TextureViewDescriptor::default());
|
let view = output.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
let depth_view = self
|
||||||
|
.depth
|
||||||
|
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
let mut encoder = self
|
let mut encoder = self
|
||||||
.device
|
.device
|
||||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
|
||||||
|
|
@ -189,7 +196,14 @@ impl Core {
|
||||||
store: wgpu::StoreOp::Store,
|
store: wgpu::StoreOp::Store,
|
||||||
},
|
},
|
||||||
})],
|
})],
|
||||||
depth_stencil_attachment: None,
|
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||||
|
view: &depth_view,
|
||||||
|
depth_ops: Some(wgpu::Operations {
|
||||||
|
load: wgpu::LoadOp::Clear(1.),
|
||||||
|
store: wgpu::StoreOp::Discard,
|
||||||
|
}),
|
||||||
|
stencil_ops: None,
|
||||||
|
}),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
if args.show_axes {
|
if args.show_axes {
|
||||||
|
|
@ -389,10 +403,26 @@ impl Core {
|
||||||
self.queue.submit(std::iter::once(encoder.finish()));
|
self.queue.submit(std::iter::once(encoder.finish()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configures the renderer for a given target size.
|
fn create_depth_buffer(device: &wgpu::Device, pixel_size: UVec2) -> wgpu::Texture {
|
||||||
pub fn configure(&mut self, pixel_size: UVec2) {
|
device.create_texture(&wgpu::TextureDescriptor {
|
||||||
self.surface.configure(
|
label: Some("depth buffer"),
|
||||||
&self.device,
|
size: wgpu::Extent3d {
|
||||||
|
width: pixel_size.x,
|
||||||
|
height: pixel_size.y,
|
||||||
|
depth_or_array_layers: 1,
|
||||||
|
},
|
||||||
|
mip_level_count: 1,
|
||||||
|
sample_count: 1,
|
||||||
|
dimension: wgpu::TextureDimension::D2,
|
||||||
|
format: DEPTH_FORMAT,
|
||||||
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||||
|
view_formats: &[],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn configure_surface(surface: &wgpu::Surface, device: &wgpu::Device, pixel_size: UVec2) {
|
||||||
|
surface.configure(
|
||||||
|
device,
|
||||||
&wgpu::SurfaceConfiguration {
|
&wgpu::SurfaceConfiguration {
|
||||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
|
||||||
format: OUTPUT_FORMAT,
|
format: OUTPUT_FORMAT,
|
||||||
|
|
@ -406,6 +436,12 @@ impl Core {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configures the renderer for a given target size.
|
||||||
|
pub fn configure(&mut self, pixel_size: UVec2) {
|
||||||
|
Self::configure_surface(&self.surface, &self.device, pixel_size);
|
||||||
|
self.depth = Self::create_depth_buffer(&self.device, pixel_size);
|
||||||
|
}
|
||||||
|
|
||||||
/// Redraws the entire surface.
|
/// Redraws the entire surface.
|
||||||
///
|
///
|
||||||
/// [`Self::configure`] must be called at least once before this.
|
/// [`Self::configure`] must be called at least once before this.
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use bytemuck::{Pod, Zeroable, bytes_of, cast_slice};
|
||||||
use glam::{Mat4, Vec3};
|
use glam::{Mat4, Vec3};
|
||||||
use wgpu::util::DeviceExt as _;
|
use wgpu::util::DeviceExt as _;
|
||||||
|
|
||||||
use crate::render::OUTPUT_FORMAT;
|
use crate::render::{DEPTH_FORMAT, OUTPUT_FORMAT};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
|
@ -109,7 +109,13 @@ impl Pipeline {
|
||||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
depth_stencil: None,
|
depth_stencil: Some(wgpu::DepthStencilState {
|
||||||
|
format: DEPTH_FORMAT,
|
||||||
|
depth_write_enabled: true,
|
||||||
|
depth_compare: wgpu::CompareFunction::LessEqual,
|
||||||
|
stencil: wgpu::StencilState::default(),
|
||||||
|
bias: wgpu::DepthBiasState::default(),
|
||||||
|
}),
|
||||||
multisample: wgpu::MultisampleState {
|
multisample: wgpu::MultisampleState {
|
||||||
count: 1,
|
count: 1,
|
||||||
mask: !0,
|
mask: !0,
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use bytemuck::{Pod, Zeroable, bytes_of, cast_slice};
|
||||||
use glam::{Mat4, Vec3};
|
use glam::{Mat4, Vec3};
|
||||||
use wgpu::util::DeviceExt as _;
|
use wgpu::util::DeviceExt as _;
|
||||||
|
|
||||||
use crate::render::OUTPUT_FORMAT;
|
use crate::render::{DEPTH_FORMAT, OUTPUT_FORMAT};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
|
@ -91,7 +91,17 @@ impl Pipeline {
|
||||||
topology: wgpu::PrimitiveTopology::LineList,
|
topology: wgpu::PrimitiveTopology::LineList,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
depth_stencil: None,
|
depth_stencil: Some(wgpu::DepthStencilState {
|
||||||
|
format: DEPTH_FORMAT,
|
||||||
|
depth_write_enabled: true,
|
||||||
|
depth_compare: wgpu::CompareFunction::LessEqual,
|
||||||
|
stencil: wgpu::StencilState::default(),
|
||||||
|
bias: wgpu::DepthBiasState {
|
||||||
|
constant: -2,
|
||||||
|
slope_scale: 0.,
|
||||||
|
clamp: 0.,
|
||||||
|
},
|
||||||
|
}),
|
||||||
multisample: wgpu::MultisampleState {
|
multisample: wgpu::MultisampleState {
|
||||||
count: 1,
|
count: 1,
|
||||||
mask: !0,
|
mask: !0,
|
||||||
|
|
|
||||||
|
|
@ -4,3 +4,4 @@ pub mod lines;
|
||||||
static SIMPLE_SHADER: &str = include_str!("simple.wgsl");
|
static SIMPLE_SHADER: &str = include_str!("simple.wgsl");
|
||||||
|
|
||||||
pub const OUTPUT_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb;
|
pub const OUTPUT_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb;
|
||||||
|
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth24Plus;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user