Skip to content

Commit

Permalink
faster point cloud population by not chaining iterators with default …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
Wumpf committed Apr 12, 2023
1 parent 987a774 commit 256517a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 58 deletions.
6 changes: 5 additions & 1 deletion crates/re_renderer/src/allocator/cpu_write_gpu_read_belt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ where
/// Pushes several elements into the buffer.
///
/// Panics if there are more elements than there is space in the buffer.
///
/// Returns number of elements pushed.
#[inline]
pub fn extend(&mut self, elements: impl Iterator<Item = T>) {
pub fn extend(&mut self, elements: impl Iterator<Item = T>) -> usize {
let num_written_before = self.num_written();
for element in elements {
self.push(element);
}
self.num_written() - num_written_before
}

/// Pushes a single element into the buffer and advances the write pointer.
Expand Down
59 changes: 38 additions & 21 deletions crates/re_renderer/src/point_cloud_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,27 +182,44 @@ impl<'a> PointCloudBatchBuilder<'a> {
}
self.batch_mut().point_count += num_points as u32;

// Expand iterators with default values.
let positions = positions
.chain(std::iter::repeat(glam::Vec3::ZERO))
.take(num_points);
let radii = radii.chain(std::iter::repeat(Size::AUTO)).take(num_points);
let colors = colors
.chain(std::iter::repeat(Color32::TRANSPARENT))
.take(num_points);
let picking_instance_ids = picking_instance_ids
.chain(std::iter::repeat(Default::default()))
.take(num_points);

self.0.vertices.extend(
positions
.zip(radii)
.map(|(position, radius)| PointCloudVertex { position, radius }),
);
self.0.color_buffer.extend(colors);
self.0
.picking_instance_ids_buffer
.extend(picking_instance_ids);
{
crate::profile_scope!("positions");
let num_before = self.0.vertices.len();
self.0.vertices.extend(
positions
.take(num_points)
.zip(radii.take(num_points))
.map(|(position, radius)| PointCloudVertex { position, radius }),
);
// Fill up with defaults. Doing this in a separate step is faster than chaining the iterator.
let num_default = (self.0.vertices.len() - num_before) - num_points;
self.0.vertices.extend(
std::iter::repeat(PointCloudVertex {
position: glam::Vec3::ZERO,
radius: Size::AUTO,
})
.take(num_default),
);
}
{
crate::profile_scope!("colors");
let num_written = self.0.color_buffer.extend(colors.take(num_points));
// Fill up with defaults. Doing this in a separate step is faster than chaining the iterator.
self.0
.color_buffer
.extend(std::iter::repeat(Color32::TRANSPARENT).take(num_written - num_points));
}
{
crate::profile_scope!("picking_instance_ids");
let num_written = self
.0
.picking_instance_ids_buffer
.extend(picking_instance_ids.take(num_points));
// Fill up with defaults. Doing this in a separate step is faster than chaining the iterator.
self.0.picking_instance_ids_buffer.extend(
std::iter::repeat(PickingLayerInstanceId::default()).take(num_written - num_points),
);
}

self
}
Expand Down
3 changes: 2 additions & 1 deletion crates/re_renderer/src/renderer/point_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub struct PointCloudBatchInfo {
}

/// Description of a point cloud.
#[derive(Clone)]
pub struct PointCloudVertex {
/// Connected points. Must be at least 2.
pub position: glam::Vec3,
Expand Down Expand Up @@ -225,7 +226,7 @@ impl PointCloudDrawData {
0
);

let vertices = if vertices.len() >= Self::MAX_NUM_POINTS {
let vertices = if vertices.len() > Self::MAX_NUM_POINTS {
re_log::error_once!(
"Reached maximum number of supported points. Clamping down to {}, passed were {}.
See also https://github.com/rerun-io/rerun/issues/957",
Expand Down
17 changes: 7 additions & 10 deletions crates/re_viewer/src/ui/view_spatial/scene/scene_part/points2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,13 @@ impl Points2DPart {
};

let mut point_range_builder = if properties.interactive {
let picking_instance_ids = {
crate::profile_scope!("instance_ids");
entity_view.iter_instance_keys()?.map(|instance_key| {
instance_key_to_picking_id(
instance_key,
entity_view,
entity_highlight.any_selection_highlight,
)
})
};
let picking_instance_ids = entity_view.iter_instance_keys()?.map(|instance_key| {
instance_key_to_picking_id(
instance_key,
entity_view,
entity_highlight.any_selection_highlight,
)
});
point_batch.add_points_2d(
entity_view.num_instances(),
point_positions,
Expand Down
44 changes: 19 additions & 25 deletions crates/re_viewer/src/ui/view_spatial/scene/scene_part/points3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,18 @@ impl Points3DPart {
let colors =
process_colors(entity_view, ent_path, &annotation_infos)?.collect::<Vec<_>>();

let instance_path_hashes_for_picking = {
crate::profile_scope!("instance_hashes");
entity_view
.iter_instance_keys()?
.map(|instance_key| {
instance_path_hash_for_picking(
ent_path,
instance_key,
entity_view,
properties,
entity_highlight.any_selection_highlight,
)
})
.collect::<Vec<_>>()
};
let instance_path_hashes_for_picking = entity_view
.iter_instance_keys()?
.map(|instance_key| {
instance_path_hash_for_picking(
ent_path,
instance_key,
entity_view,
properties,
entity_highlight.any_selection_highlight,
)
})
.collect::<Vec<_>>();

scene.ui.labels.extend(Self::process_labels(
entity_view,
Expand Down Expand Up @@ -136,16 +133,13 @@ impl Points3DPart {
};

let mut point_range_builder = if properties.interactive {
let picking_instance_ids = {
crate::profile_scope!("instance_ids");
entity_view.iter_instance_keys()?.map(|instance_key| {
instance_key_to_picking_id(
instance_key,
entity_view,
entity_highlight.any_selection_highlight,
)
})
};
let picking_instance_ids = entity_view.iter_instance_keys()?.map(|instance_key| {
instance_key_to_picking_id(
instance_key,
entity_view,
entity_highlight.any_selection_highlight,
)
});
point_batch.add_points(
entity_view.num_instances(),
point_positions,
Expand Down

0 comments on commit 256517a

Please sign in to comment.