Skip to content

Commit

Permalink
Using indirect draw for wireframe
Browse files Browse the repository at this point in the history
  • Loading branch information
gents83 committed Mar 12, 2024
1 parent b4c88a8 commit 728aeb7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crates/graphics/src/passes/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Pass for VisibilityBufferPass {
&render_context.webgpu.device,
"visibility_pass",
);
pass.indirect_indexed_draw(render_context, &buffers, draw_commands_type, render_pass);
pass.indirect_indexed_draw(render_context, draw_commands_type, render_pass);
}
}
}
Expand Down
27 changes: 15 additions & 12 deletions crates/graphics/src/passes/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct WireframePass {
vertices: Vec<DebugVertex>,
indices: Vec<u32>,
instances: Vec<DrawIndexedCommand>,
instance_count: u32,
instance_count: usize,
listener: Listener,
}
unsafe impl Send for WireframePass {}
Expand Down Expand Up @@ -123,7 +123,6 @@ impl Pass for WireframePass {
..Default::default()
},
)
.bind_buffer(&mut self.instance_count, Some("DebugInstance Count"))
.set_vertex_buffer(
VextexBindingType::Vertex,
&mut self.vertices,
Expand All @@ -134,6 +133,7 @@ impl Pass for WireframePass {
&mut self.instances,
Some("DebugInstances"),
)
.bind_buffer(&mut self.instance_count, Some("DebugInstance Count"))
.set_index_buffer(&mut self.indices, Some("DebugIndices"));

let vertex_layout = DebugVertex::descriptor(0);
Expand Down Expand Up @@ -179,14 +179,19 @@ impl Pass for WireframePass {
&render_context.webgpu.device,
"wireframe_pass",
);
pass.draw_indexed(render_context, render_pass, &self.instances);

pass.indirect_draw(
render_context,
&self.instances,
&self.instance_count,
render_pass,
);
}
}
}

impl WireframePass {
fn add_mesh(
render_context: &RenderContext,
commands: &mut Vec<DrawIndexedCommand>,
vertices: &mut Vec<DebugVertex>,
indices: &mut Vec<u32>,
Expand All @@ -207,8 +212,6 @@ impl WireframePass {
});
}
indices.extend_from_slice(&mesh_data.indices);
indices.mark_as_dirty(render_context);
vertices.mark_as_dirty(render_context);
}
fn process_messages(&mut self, render_context: &RenderContext) {
inox_profiler::scoped_profile!("WireframePass::process_messages");
Expand All @@ -235,7 +238,6 @@ impl WireframePass {

let mesh_data = create_line(start, end, color);
Self::add_mesh(
render_context,
&mut self.instances,
&mut self.vertices,
&mut self.indices,
Expand All @@ -247,7 +249,6 @@ impl WireframePass {

let mesh_data = create_cube_from_min_max(min, max, color);
Self::add_mesh(
render_context,
&mut self.instances,
&mut self.vertices,
&mut self.indices,
Expand All @@ -260,7 +261,6 @@ impl WireframePass {
let mesh_data =
create_colored_quad([min.x, min.y, max.x, max.y].into(), z, color);
Self::add_mesh(
render_context,
&mut self.instances,
&mut self.vertices,
&mut self.indices,
Expand All @@ -272,7 +272,6 @@ impl WireframePass {

let mesh_data = create_arrow(position, direction, color);
Self::add_mesh(
render_context,
&mut self.instances,
&mut self.vertices,
&mut self.indices,
Expand All @@ -284,7 +283,6 @@ impl WireframePass {

let mesh_data = create_sphere(position, radius, 16, 8, color);
Self::add_mesh(
render_context,
&mut self.instances,
&mut self.vertices,
&mut self.indices,
Expand All @@ -303,13 +301,18 @@ impl WireframePass {
mesh_data.aabb_max = matrix.rotate_point(mesh_data.aabb_max);
}
Self::add_mesh(
render_context,
&mut self.instances,
&mut self.vertices,
&mut self.indices,
mesh_data,
);
}
});
self.indices.mark_as_dirty(render_context);
self.vertices.mark_as_dirty(render_context);
self.instances.mark_as_dirty(render_context);

self.instance_count = self.instances.len();
self.instance_count.mark_as_dirty(render_context);
}
}
39 changes: 36 additions & 3 deletions crates/render/src/resources/render_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,38 @@ impl RenderPass {
);
});
}
pub fn indirect_draw(
&self,
render_context: &RenderContext,
instances: &Vec<DrawIndexedCommand>,
instance_count: &usize,
mut render_pass: wgpu::RenderPass,
) {
inox_profiler::scoped_profile!("render_pass::indirect_draw");

if is_indirect_mode_enabled() && self.render_mode == RenderMode::Indirect {
inox_profiler::gpu_scoped_profile!(
&mut render_pass,
&render_context.webgpu.device,
"render_pass::multi_draw_indexed_indirect_count",
);
let buffers = render_context.buffers();
let indirect_buffer = buffers.get(&(instances.buffer_id())).unwrap();
let count_buffer = buffers.get(&instance_count.buffer_id()).unwrap();
let mut render_pass = render_pass;
render_pass.multi_draw_indexed_indirect_count(
indirect_buffer.gpu_buffer().unwrap(),
0,
count_buffer.gpu_buffer().unwrap(),
0,
instances.len() as _,
);
return;
}
//TODO: use debug_log_once
//inox_log::debug_log!("Unable to use indirect_draw - using normal draw_indexed");
self.draw_indexed(render_context, render_pass, instances);
}

pub fn draw_meshlets(&self, render_context: &RenderContext, mut render_pass: wgpu::RenderPass) {
inox_profiler::scoped_profile!("render_pass::draw_meshlets");
Expand Down Expand Up @@ -483,12 +515,11 @@ impl RenderPass {
});
}

pub fn indirect_indexed_draw<'a>(
pub fn indirect_indexed_draw(
&self,
render_context: &RenderContext,
buffers: &'a HashMap<BufferId, BufferRef>,
draw_commands_type: DrawCommandType,
mut render_pass: wgpu::RenderPass<'a>,
mut render_pass: wgpu::RenderPass,
) {
inox_profiler::scoped_profile!("render_pass::indirect_draw");

Expand All @@ -504,6 +535,7 @@ impl RenderPass {
if let Some(commands) = commands.map.get(&draw_commands_type) {
if !commands.commands.is_empty() {
let commands_id = commands.commands.buffer_id();
let buffers = render_context.buffers();
if let Some(commands_buffer) = buffers.get(&commands_id) {
let count_id = commands.counter.buffer_id();
if let Some(count_buffer) = buffers.get(&count_id) {
Expand All @@ -512,6 +544,7 @@ impl RenderPass {
&render_context.webgpu.device,
"render_pass::multi_draw_indexed_indirect_count",
);
let mut render_pass = render_pass;
render_pass.multi_draw_indexed_indirect_count(
commands_buffer.gpu_buffer().unwrap(),
0,
Expand Down

0 comments on commit 728aeb7

Please sign in to comment.