Skip to content

Commit

Permalink
Move some code into MeshletGpuScene, skip unused material draws
Browse files Browse the repository at this point in the history
  • Loading branch information
JMS55 committed Oct 30, 2023
1 parent 411029e commit 419c707
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
31 changes: 23 additions & 8 deletions crates/bevy_pbr/src/meshlet/gpu_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use bevy_transform::components::GlobalTransform;
use bevy_utils::{tracing::error, EntityHashMap, HashMap};
use std::{
hash::Hash,
mem,
ops::{DerefMut, Range},
sync::Arc,
};
Expand Down Expand Up @@ -607,25 +608,39 @@ impl MeshletGpuScene {
&self.draw_bind_group_layout
}

pub fn resources_for_view(
&self,
pub fn resources_for_view<'a>(
&'a self,
view_entity: Entity,
pipeline_cache: &'a PipelineCache,
) -> (
u32,
Vec<Option<&(CachedRenderPipelineId, BindGroup)>>,
impl Iterator<Item = (u64, &'a BindGroup, &'a RenderPipeline)>,
Option<&BindGroup>,
Option<&BindGroup>,
Option<&Buffer>,
Option<&Buffer>,
) {
let mut materials = Vec::new();
for material_id in &self.material_order {
materials.push(self.materials.get(&(*material_id, view_entity)));
}
let material_draws = self
.material_order
.iter()
.enumerate()
.filter(|(_, material_id)| self.material_vertex_counts.contains_key(*material_id))
.map(move |(i, material_id)| (i, &self.materials[&(*material_id, view_entity)]))
.filter_map(|(i, (material_pipeline_id, material_bind_group))| {
pipeline_cache
.get_render_pipeline(*material_pipeline_id)
.map(|material_pipeline| {
(
(i * mem::size_of::<DrawIndexedIndirect>()) as u64,
material_bind_group,
material_pipeline,
)
})
});

(
self.scene_meshlet_count,
materials,
material_draws,
self.culling_bind_groups.get(&view_entity),
self.draw_bind_groups.get(&view_entity),
self.draw_index_buffer.as_ref(),
Expand Down
19 changes: 6 additions & 13 deletions crates/bevy_pbr/src/meshlet/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ impl ViewNode for MainMeshletOpaquePass3dNode {
};
let (
scene_meshlet_count,
materials,
material_draws,
Some(culling_bind_group),
Some(draw_bind_group),
Some(draw_index_buffer),
Some(draw_command_buffer),
) = gpu_scene.resources_for_view(graph.view_entity())
) = gpu_scene.resources_for_view(graph.view_entity(), pipeline_cache)
else {
return Ok(());
};
Expand Down Expand Up @@ -131,17 +131,10 @@ impl ViewNode for MainMeshletOpaquePass3dNode {
);
draw_pass.set_bind_group(1, draw_bind_group, &[]);

// TODO: Move this code into MeshletGpuScene::resources(), and have that return an iterator over
// (draw_offset, pipeline, material_bind_group)
// TODO: Skip indirect draw if 0 vertex count for material
for (i, material_resources) in materials.iter().enumerate() {
if let Some((pipeline_id, material_bind_group)) = material_resources {
if let Some(pipeline) = pipeline_cache.get_render_pipeline(*pipeline_id) {
draw_pass.set_bind_group(2, material_bind_group, &[]);
draw_pass.set_render_pipeline(pipeline);
draw_pass.draw_indexed_indirect(draw_command_buffer, i as u64 * 20);
}
}
for (material_draw_offset, material_bind_group, material_pipeline) in material_draws {
draw_pass.set_bind_group(2, material_bind_group, &[]);
draw_pass.set_render_pipeline(material_pipeline);
draw_pass.draw_indexed_indirect(draw_command_buffer, material_draw_offset);
}
}

Expand Down

0 comments on commit 419c707

Please sign in to comment.