cyborg/src/pass/debug.rs

157 lines
5.3 KiB
Rust
Raw Normal View History

2022-04-24 01:57:27 +00:00
use super::*;
2022-05-17 00:57:54 +00:00
use crate::scene::{DebugDrawList, DebugIndex as Index, DebugVertex as Vertex};
2022-05-08 21:52:48 +00:00
use crate::storage::GpuVec;
2022-04-25 02:44:51 +00:00
use crate::viewport::ViewportInfo;
2022-04-24 01:57:27 +00:00
use crate::RenderLayouts;
2022-05-11 20:44:44 +00:00
use parking_lot::RwLock;
2022-04-24 01:57:27 +00:00
pub struct FrameData {
2022-05-08 21:42:30 +00:00
vertices: GpuVec<Vertex>,
indices: GpuVec<Index>,
2022-04-24 01:57:27 +00:00
}
pub struct DebugPass {
device: Arc<wgpu::Device>,
pipeline: wgpu::RenderPipeline,
2022-04-25 02:44:51 +00:00
target_info: ViewportInfo,
2022-05-11 20:44:44 +00:00
draw_list: RwLock<DebugDrawList>,
2022-04-24 01:57:27 +00:00
}
impl DebugPass {
pub fn new(
device: Arc<wgpu::Device>,
layouts: Arc<RenderLayouts>,
2022-04-25 02:44:51 +00:00
target_info: ViewportInfo,
2022-04-24 01:57:27 +00:00
) -> Self {
// TODO hook into ShaderStore system
2022-09-17 15:34:52 +00:00
let shader = device.create_shader_module(wgpu::include_wgsl!("debug_shader.wgsl"));
2022-04-24 01:57:27 +00:00
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("DebugPass Pipeline Layout"),
bind_group_layouts: &[&layouts.bind_viewport],
push_constant_ranges: &[],
});
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("DebugPass Pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[Vertex::desc()],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
2022-09-17 15:34:52 +00:00
targets: &[Some(wgpu::ColorTargetState {
2022-04-25 02:44:51 +00:00
format: target_info.output_format,
2022-04-24 01:57:27 +00:00
blend: Some(wgpu::BlendState::REPLACE),
write_mask: wgpu::ColorWrites::ALL,
2022-09-17 15:34:52 +00:00
})],
2022-04-24 01:57:27 +00:00
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::LineList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
polygon_mode: wgpu::PolygonMode::Fill,
unclipped_depth: false,
conservative: false,
},
2022-04-25 02:44:51 +00:00
depth_stencil: Some(wgpu::DepthStencilState {
format: target_info.depth_format,
depth_write_enabled: false,
depth_compare: wgpu::CompareFunction::Less,
stencil: Default::default(),
bias: Default::default(),
}),
2022-04-24 01:57:27 +00:00
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
});
Self {
device,
pipeline,
2022-04-25 02:44:51 +00:00
target_info,
2022-05-11 20:44:44 +00:00
draw_list: Default::default(),
2022-04-24 01:57:27 +00:00
}
}
2022-05-11 20:44:44 +00:00
pub fn add_draw_list(&self, draw_list: &DebugDrawList) {
self.draw_list.write().merge(draw_list);
}
2022-04-24 01:57:27 +00:00
}
impl RenderPass for DebugPass {
type FrameData = FrameData;
fn create_frame_data(&self) -> FrameData {
FrameData {
2022-05-08 21:42:30 +00:00
vertices: GpuVec::new(
self.device.clone(),
wgpu::BufferUsages::VERTEX,
1024 * 1024,
Some("Debug Vertex Buffer".to_string()),
false,
),
indices: GpuVec::new(
self.device.clone(),
wgpu::BufferUsages::INDEX,
1024 * 1024,
Some("Debug Index Buffer".to_string()),
false,
),
2022-04-24 01:57:27 +00:00
}
}
fn begin_frame(&self, data: &mut FrameData, phases: &mut Vec<Phase>, queue: &wgpu::Queue) {
phases.push(Phase::Overlay);
2022-05-11 20:44:44 +00:00
use std::mem::replace;
use std::ops::DerefMut;
2022-05-08 21:42:30 +00:00
2022-05-11 20:44:44 +00:00
let mut draw_list_lock = self.draw_list.write();
let vertices = replace(&mut draw_list_lock.vertices, Default::default());
let indices = replace(&mut draw_list_lock.indices, Default::default());
2022-04-24 01:57:27 +00:00
2022-05-11 20:44:44 +00:00
let _ = replace(data.vertices.deref_mut(), vertices);
let _ = replace(data.indices.deref_mut(), indices);
2022-04-24 01:57:27 +00:00
2022-05-08 21:42:30 +00:00
data.vertices.write(queue);
data.indices.write(queue);
2022-04-24 01:57:27 +00:00
}
fn record_render(&self, data: PhaseData<&FrameData>) -> Option<wgpu::RenderBundle> {
let mut cmds =
self.device
.create_render_bundle_encoder(&wgpu::RenderBundleEncoderDescriptor {
label: Some("DebugPass Render Bundle"),
2022-09-17 15:34:52 +00:00
color_formats: &[Some(self.target_info.output_format)],
2022-04-25 02:44:51 +00:00
depth_stencil: Some(wgpu::RenderBundleDepthStencil {
format: self.target_info.depth_format,
depth_read_only: false, // TODO optimize?
stencil_read_only: true,
}),
2022-04-24 01:57:27 +00:00
sample_count: 1,
multiview: None,
});
2022-05-08 21:42:30 +00:00
let vertices = &data.frame_data.vertices;
let indices = &data.frame_data.indices;
2022-04-24 01:57:27 +00:00
cmds.set_pipeline(&self.pipeline);
cmds.set_bind_group(0, data.bind_viewport, &[]);
2022-05-08 21:42:30 +00:00
cmds.set_vertex_buffer(0, vertices.as_ref().slice(..));
cmds.set_index_buffer(indices.as_ref().slice(..), wgpu::IndexFormat::Uint32);
2022-04-24 01:57:27 +00:00
2022-05-08 21:42:30 +00:00
let index_range = 0..(indices.len() as u32);
2022-04-24 01:57:27 +00:00
cmds.draw_indexed(index_range, 0, 0..1);
Some(cmds.finish(&wgpu::RenderBundleDescriptor::default()))
}
}