Skip to content

Commit

Permalink
renderer: fix the groupby logic in mesh instancing (#1657)
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc authored Mar 21, 2023
1 parent 9ef34dd commit 28feae8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions crates/re_renderer/src/renderer/mesh_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use std::sync::Arc;

use itertools::Itertools as _;
use ahash::{HashMap, HashMapExt};
use smallvec::smallvec;

use crate::{
include_file,
mesh::{gpu_data::MaterialUniformBuffer, mesh_vertices, GpuMesh, Mesh},
renderer::OutlineMaskProcessor,
resource_managers::GpuMeshHandle,
resource_managers::{GpuMeshHandle, ResourceHandle},
view_builder::ViewBuilder,
wgpu_resources::{
BindGroupLayoutDesc, BufferDesc, GpuBindGroupLayoutHandle, GpuBuffer,
Expand Down Expand Up @@ -175,6 +175,16 @@ impl MeshDrawData {
},
);

let mut instances_by_mesh: HashMap<_, Vec<_>> = HashMap::new();
for instance in instances {
if !matches!(instance.gpu_mesh, ResourceHandle::Invalid) {
instances_by_mesh
.entry(&instance.gpu_mesh)
.or_insert_with(|| Vec::with_capacity(instances.len()))
.push(instance);
}
}

let mut batches = Vec::new();
{
let mut instance_buffer_staging = ctx
Expand All @@ -188,13 +198,12 @@ impl MeshDrawData {

let mesh_manager = ctx.mesh_manager.read();
let mut num_processed_instances = 0;
// TODO(#1530) This grouping doesn't seem to do its job correctly. We're not actually batching correctly right now in all cases.
for (mesh, instances) in &instances.iter().group_by(|instance| &instance.gpu_mesh) {
for (mesh, mut instances) in instances_by_mesh {
let mut count = 0;
let mut count_with_outlines = 0;

// Put all instances with outlines at the start of the instance buffer range.
let instances = instances.sorted_by(|a, b| {
instances.sort_by(|a, b| {
a.outline_mask_ids
.is_none()
.cmp(&b.outline_mask_ids.is_none())
Expand Down Expand Up @@ -232,7 +241,8 @@ impl MeshDrawData {
}
num_processed_instances += count;

// We resolve the meshes here already, so the actual draw call doesn't need to know about the MeshManager.
// We resolve the meshes here already, so the actual draw call doesn't need to
// know about the MeshManager.
let mesh = mesh_manager.get(mesh)?;
batches.push(MeshBatch {
mesh: mesh.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use slotmap::{Key, SlotMap};
use crate::wgpu_resources::PoolError;

/// Handle to a resource that is stored in a resource manager.
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum ResourceHandle<InnerHandle: slotmap::Key> {
/// Resource handle that keeps the resource alive as long as there are handles.
///
Expand Down

1 comment on commit 28feae8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: 28feae8 Previous: 9ef34dd Ratio
datastore/insert/batch/rects/insert 556565 ns/iter (± 3153) 554819 ns/iter (± 3514) 1.00
datastore/latest_at/batch/rects/query 1840 ns/iter (± 2) 1860 ns/iter (± 24) 0.99
datastore/latest_at/missing_components/primary 286 ns/iter (± 0) 286 ns/iter (± 0) 1
datastore/latest_at/missing_components/secondaries 437 ns/iter (± 0) 436 ns/iter (± 2) 1.00
datastore/range/batch/rects/query 153102 ns/iter (± 262) 150385 ns/iter (± 890) 1.02
mono_points_arrow/generate_message_bundles 45774556 ns/iter (± 830948) 44201145 ns/iter (± 1129924) 1.04
mono_points_arrow/generate_messages 126152367 ns/iter (± 1023987) 125549171 ns/iter (± 1402042) 1.00
mono_points_arrow/encode_log_msg 154072970 ns/iter (± 986536) 155463925 ns/iter (± 1038482) 0.99
mono_points_arrow/encode_total 325793414 ns/iter (± 1895128) 327028761 ns/iter (± 2220078) 1.00
mono_points_arrow/decode_log_msg 177869955 ns/iter (± 1043507) 174313449 ns/iter (± 1051137) 1.02
mono_points_arrow/decode_message_bundles 63990326 ns/iter (± 969352) 63583482 ns/iter (± 1019295) 1.01
mono_points_arrow/decode_total 241486157 ns/iter (± 2052056) 235978689 ns/iter (± 1574296) 1.02
batch_points_arrow/generate_message_bundles 328149 ns/iter (± 405) 327455 ns/iter (± 1327) 1.00
batch_points_arrow/generate_messages 6459 ns/iter (± 13) 6446 ns/iter (± 44) 1.00
batch_points_arrow/encode_log_msg 362892 ns/iter (± 24192) 355026 ns/iter (± 1816) 1.02
batch_points_arrow/encode_total 706869 ns/iter (± 2305) 713064 ns/iter (± 3080) 0.99
batch_points_arrow/decode_log_msg 341243 ns/iter (± 864) 344545 ns/iter (± 1024) 0.99
batch_points_arrow/decode_message_bundles 2069 ns/iter (± 6) 2067 ns/iter (± 14) 1.00
batch_points_arrow/decode_total 354764 ns/iter (± 1003) 353690 ns/iter (± 907) 1.00
arrow_mono_points/insert 6114055245 ns/iter (± 23490028) 6162557603 ns/iter (± 14292562) 0.99
arrow_mono_points/query 1755773 ns/iter (± 8480) 1777972 ns/iter (± 9981) 0.99
arrow_batch_points/insert 2655560 ns/iter (± 10503) 2633594 ns/iter (± 23125) 1.01
arrow_batch_points/query 16177 ns/iter (± 23) 16183 ns/iter (± 68) 1.00
arrow_batch_vecs/insert 42313 ns/iter (± 72) 42392 ns/iter (± 181) 1.00
arrow_batch_vecs/query 388855 ns/iter (± 698) 389389 ns/iter (± 1588) 1.00
tuid/Tuid::random 34 ns/iter (± 0) 34 ns/iter (± 0) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.