Skip to content

Commit

Permalink
Batch skinned meshes on platforms where storage buffers are available. (
Browse files Browse the repository at this point in the history
#16599)

This commit makes skinned meshes batchable on platforms other than WebGL
2. On supported platforms, it replaces the two uniform buffers used for
joint matrices with a pair of storage buffers containing all matrices
for all skinned meshes packed together. The indices into the buffer are
stored in the mesh uniform and mesh input uniform. The GPU mesh
preprocessing step copies the indices in if that step is enabled.

On the `many_foxes` demo, I observed a frame time decrease from 15.470ms
to 11.935ms. This is the result of reducing the `submit_graph_commands`
time from an average of 5.45ms to 0.489ms, an 11x speedup in that
portion of rendering.

![Screenshot 2024-12-01
192838](https://github.com/user-attachments/assets/7d2db997-8939-466e-8b9e-050d4a6a78ee)

This is what the profile looks like for `many_foxes` after these
changes.

![Screenshot 2024-12-01
193026](https://github.com/user-attachments/assets/68983fc3-01b8-41fd-835e-3d93cb65d0fa)

---------

Co-authored-by: François Mockers <[email protected]>
  • Loading branch information
pcwalton and mockersf authored Dec 10, 2024
1 parent 7ed1f32 commit 3188e5a
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 72 deletions.
9 changes: 8 additions & 1 deletion crates/bevy_pbr/src/meshlet/instance_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,14 @@ impl InstanceManager {
return;
};

let mesh_uniform = MeshUniform::new(&transforms, 0, mesh_material_binding_id.slot, None);
let mesh_uniform = MeshUniform::new(
&transforms,
0,
mesh_material_binding_id.slot,
None,
None,
None,
);

// Append instance data
self.instances.push((
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ pub struct PrepassPipeline<M: Material> {
pub deferred_material_vertex_shader: Option<Handle<Shader>>,
pub deferred_material_fragment_shader: Option<Handle<Shader>>,
pub material_pipeline: MaterialPipeline<M>,

/// Whether skins will use uniform buffers on account of storage buffers
/// being unavailable on this platform.
pub skins_use_uniform_buffers: bool,

pub depth_clip_control_supported: bool,
_marker: PhantomData<M>,
}
Expand Down Expand Up @@ -345,6 +350,7 @@ impl<M: Material> FromWorld for PrepassPipeline<M> {
},
material_layout: M::bind_group_layout(render_device),
material_pipeline: world.resource::<MaterialPipeline<M>>().clone(),
skins_use_uniform_buffers: skin::skins_use_uniform_buffers(render_device),
depth_clip_control_supported,
_marker: PhantomData,
}
Expand Down Expand Up @@ -521,6 +527,7 @@ where
&key.mesh_key,
&mut shader_defs,
&mut vertex_attributes,
self.skins_use_uniform_buffers,
);
bind_group_layouts.insert(1, bind_group);

Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_pbr/src/prepass/prepass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ fn vertex(vertex_no_morph: Vertex) -> VertexOutput {
let mesh_world_from_local = mesh_functions::get_world_from_local(vertex_no_morph.instance_index);

#ifdef SKINNED
var world_from_local = skinning::skin_model(vertex.joint_indices, vertex.joint_weights);
var world_from_local = skinning::skin_model(
vertex.joint_indices,
vertex.joint_weights,
vertex_no_morph.instance_index
);
#else // SKINNED
// Use vertex_no_morph.instance_index instead of vertex.instance_index to work around a wgpu dx12 bug.
// See https://github.com/gfx-rs/naga/issues/2416
Expand Down Expand Up @@ -142,6 +146,7 @@ fn vertex(vertex_no_morph: Vertex) -> VertexOutput {
let prev_model = skinning::skin_prev_model(
prev_vertex.joint_indices,
prev_vertex.joint_weights,
vertex_no_morph.instance_index
);
#else // HAS_PREVIOUS_SKIN
let prev_model = mesh_functions::get_previous_world_from_local(prev_vertex.instance_index);
Expand Down
Loading

0 comments on commit 3188e5a

Please sign in to comment.