Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallize Space View system evaluation #4460

Merged
merged 12 commits into from
Dec 11, 2023
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions crates/re_renderer/src/allocator/cpu_write_gpu_read_belt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub enum CpuWriteGpuReadError {
/// Note that the "vec like behavior" further encourages
/// * not leaving holes
/// * keeping writes sequential
pub struct CpuWriteGpuReadBuffer<T: bytemuck::Pod + 'static> {
pub struct CpuWriteGpuReadBuffer<T: bytemuck::Pod + Send + Sync> {
/// Write view into the relevant buffer portion.
///
/// UNSAFE: The lifetime is transmuted to be `'static`.
Expand All @@ -55,9 +55,23 @@ pub struct CpuWriteGpuReadBuffer<T: bytemuck::Pod + 'static> {
_type: std::marker::PhantomData<T>,
}

// BufferViewMut is not Send/Sync right now because `BufferMappedRange` trait does not implement Send/Sync.
// However, on native it is always Send/Sync, see https://github.com/gfx-rs/wgpu/issues/3795#issuecomment-1561189339
// and on web we're single threaded.
//
// https://github.com/gfx-rs/wgpu/issues/3795 is resolved, we force Send+Sync here.

#[allow(unsafe_code)]
// SAFETY: TODO(https://github.com/gfx-rs/wgpu/pull/4818): Upstream wgpu allows `wgpu::BufferViewMut` to be Send.
unsafe impl<T> Send for CpuWriteGpuReadBuffer<T> where T: bytemuck::Pod + Send + Sync {}

#[allow(unsafe_code)]
// SAFETY: TODO(https://github.com/gfx-rs/wgpu/pull/4818): Upstream wgpu allows `wgpu::BufferViewMut` to be Sync.
unsafe impl<T> Sync for CpuWriteGpuReadBuffer<T> where T: bytemuck::Pod + Send + Sync {}

impl<T> CpuWriteGpuReadBuffer<T>
where
T: bytemuck::Pod + 'static,
T: bytemuck::Pod + Send + Sync,
{
/// Memory as slice.
///
Expand Down Expand Up @@ -283,7 +297,7 @@ impl Chunk {
}

/// Caller needs to make sure that there is enough space.
fn allocate<T: bytemuck::Pod>(
fn allocate<T: bytemuck::Pod + Send + Sync>(
&mut self,
num_elements: usize,
size_in_bytes: u64,
Expand Down Expand Up @@ -419,7 +433,7 @@ impl CpuWriteGpuReadBelt {
}

/// Allocates a cpu writable buffer for `num_elements` instances of type `T`.
pub fn allocate<T: bytemuck::Pod>(
pub fn allocate<T: bytemuck::Pod + Send + Sync>(
&mut self,
device: &wgpu::Device,
buffer_pool: &GpuBufferPool,
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/src/allocator/uniform_buffer_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<T> UniformBufferAlignmentCheck<T> {
/// For subsequent frames, this will automatically not allocate any resources (thanks to our buffer pooling mechanism).
///
/// TODO(#1383): We could do this on a more complex stack allocator.
pub fn create_and_fill_uniform_buffer_batch<T: bytemuck::Pod>(
pub fn create_and_fill_uniform_buffer_batch<T: bytemuck::Pod + Send + Sync>(
ctx: &RenderContext,
label: DebugLabel,
content: impl ExactSizeIterator<Item = T>,
Expand Down Expand Up @@ -97,7 +97,7 @@ pub fn create_and_fill_uniform_buffer_batch<T: bytemuck::Pod>(
}

/// See [`create_and_fill_uniform_buffer`].
pub fn create_and_fill_uniform_buffer<T: bytemuck::Pod>(
pub fn create_and_fill_uniform_buffer<T: bytemuck::Pod + Send + Sync>(
ctx: &RenderContext,
label: DebugLabel,
content: T,
Expand Down
12 changes: 6 additions & 6 deletions crates/re_space_view_bar_chart/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use re_space_view::controls;
use re_types::datatypes::TensorBuffer;
use re_viewer_context::{
auto_color, SpaceViewClass, SpaceViewClassRegistryError, SpaceViewId,
SpaceViewSystemExecutionError, ViewContextCollection, ViewPartCollection, ViewQuery,
ViewerContext,
SpaceViewSystemExecutionError, ViewQuery, ViewerContext,
};

use super::view_part_system::BarChartViewPartSystem;
Expand Down Expand Up @@ -129,14 +128,15 @@ impl SpaceViewClass for BarChartSpaceView {
ui: &mut egui::Ui,
_state: &mut Self::State,
root_entity_properties: &EntityProperties,
_view_ctx: &ViewContextCollection,
parts: &ViewPartCollection,
_query: &ViewQuery<'_>,
_draw_data: Vec<re_renderer::QueueableDrawData>,
system_output: re_viewer_context::SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
use egui_plot::{Bar, BarChart, Legend, Plot};

let charts = &parts.get::<BarChartViewPartSystem>()?.charts;
let charts = &system_output
.view_systems
.get::<BarChartViewPartSystem>()?
.charts;

let zoom_both_axis = !ui.input(|i| i.modifiers.contains(controls::ASPECT_SCROLL_MODIFIER));

Expand Down
24 changes: 16 additions & 8 deletions crates/re_space_view_spatial/src/space_view_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use re_data_store::EntityProperties;
use re_log_types::EntityPath;
use re_viewer_context::{
AutoSpawnHeuristic, PerSystemEntities, SpaceViewClass, SpaceViewClassRegistryError,
SpaceViewId, SpaceViewSystemExecutionError, ViewContextCollection, ViewPartCollection,
ViewQuery, ViewerContext,
SpaceViewId, SpaceViewSystemExecutionError, ViewQuery, ViewerContext,
};

use crate::{
Expand Down Expand Up @@ -141,17 +140,26 @@ impl SpaceViewClass for SpatialSpaceView2D {
ui: &mut egui::Ui,
state: &mut Self::State,
_root_entity_properties: &EntityProperties,
view_ctx: &ViewContextCollection,
parts: &ViewPartCollection,
query: &ViewQuery<'_>,
draw_data: Vec<re_renderer::QueueableDrawData>,
system_output: re_viewer_context::SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
state.scene_bbox = calculate_bounding_box(parts, &mut state.scene_bbox_accum);
state.scene_num_primitives = view_ctx
state.scene_bbox =
calculate_bounding_box(&system_output.view_systems, &mut state.scene_bbox_accum);
state.scene_num_primitives = system_output
.context_systems
.get::<PrimitiveCounter>()?
.num_primitives
.load(std::sync::atomic::Ordering::Relaxed);

crate::ui_2d::view_2d(ctx, ui, state, view_ctx, parts, query, draw_data)
// TODO(andreas): Pass on system_output.
crate::ui_2d::view_2d(
ctx,
ui,
state,
&system_output.context_systems,
&system_output.view_systems,
query,
system_output.draw_data,
)
}
}
25 changes: 17 additions & 8 deletions crates/re_space_view_spatial/src/space_view_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use re_data_store::EntityProperties;
use re_log_types::EntityPath;
use re_viewer_context::{
AutoSpawnHeuristic, IdentifiedViewSystem as _, PerSystemEntities, SpaceViewClass,
SpaceViewClassRegistryError, SpaceViewId, SpaceViewSystemExecutionError, ViewContextCollection,
ViewPartCollection, ViewQuery, ViewerContext,
SpaceViewClassRegistryError, SpaceViewId, SpaceViewSystemExecutionError, ViewQuery,
ViewerContext,
};

use crate::{
Expand Down Expand Up @@ -116,19 +116,28 @@ impl SpaceViewClass for SpatialSpaceView3D {
ui: &mut egui::Ui,
state: &mut Self::State,
_root_entity_properties: &EntityProperties,
view_ctx: &ViewContextCollection,
parts: &ViewPartCollection,
query: &ViewQuery<'_>,
draw_data: Vec<re_renderer::QueueableDrawData>,
system_output: re_viewer_context::SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
re_tracing::profile_function!();

state.scene_bbox = calculate_bounding_box(parts, &mut state.scene_bbox_accum);
state.scene_num_primitives = view_ctx
state.scene_bbox =
calculate_bounding_box(&system_output.view_systems, &mut state.scene_bbox_accum);
state.scene_num_primitives = system_output
.context_systems
.get::<PrimitiveCounter>()?
.num_primitives
.load(std::sync::atomic::Ordering::Relaxed);

crate::ui_3d::view_3d(ctx, ui, state, view_ctx, parts, query, draw_data)
// TODO(andreas): Pass on system_output.
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
crate::ui_3d::view_3d(
ctx,
ui,
state,
&system_output.context_systems,
&system_output.view_systems,
query,
system_output.draw_data,
)
}
}
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/ui_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ pub fn view_2d(
ui_from_canvas,
&eye,
ui,
query.highlights,
&query.highlights,
SpatialSpaceViewKind::TwoD,
);

Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/ui_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ pub fn view_3d(
) -> Result<(), SpaceViewSystemExecutionError> {
re_tracing::profile_function!();

let highlights = query.highlights;
let highlights = &query.highlights;
let space_cameras = &parts.get::<CamerasPart>()?.space_cameras;
let view_coordinates = ctx
.store_db
Expand Down
8 changes: 3 additions & 5 deletions crates/re_space_view_tensor/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use re_types::{
use re_viewer_context::{
gpu_bridge, gpu_bridge::colormap_dropdown_button_ui, SpaceViewClass,
SpaceViewClassRegistryError, SpaceViewId, SpaceViewState, SpaceViewSystemExecutionError,
TensorStatsCache, ViewContextCollection, ViewPartCollection, ViewQuery, ViewerContext,
TensorStatsCache, ViewQuery, ViewerContext,
};

use crate::{tensor_dimension_mapper::dimension_mapping_ui, view_part_system::TensorSystem};
Expand Down Expand Up @@ -184,14 +184,12 @@ impl SpaceViewClass for TensorSpaceView {
ui: &mut egui::Ui,
state: &mut Self::State,
_root_entity_properties: &EntityProperties,
_view_ctx: &ViewContextCollection,
parts: &ViewPartCollection,
_query: &ViewQuery<'_>,
_draw_data: Vec<re_renderer::QueueableDrawData>,
system_output: re_viewer_context::SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
re_tracing::profile_function!();

let tensors = &parts.get::<TensorSystem>()?.tensors;
let tensors = &system_output.view_systems.get::<TensorSystem>()?.tensors;

if tensors.is_empty() {
ui.centered_and_justified(|ui| ui.label("(empty)"));
Expand Down
9 changes: 3 additions & 6 deletions crates/re_space_view_text_document/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use egui::Label;
use re_viewer_context::external::re_data_store::EntityProperties;
use re_viewer_context::{
external::re_log_types::EntityPath, SpaceViewClass, SpaceViewClassRegistryError, SpaceViewId,
SpaceViewState, SpaceViewSystemExecutionError, ViewContextCollection, ViewPartCollection,
ViewQuery, ViewerContext,
SpaceViewState, SpaceViewSystemExecutionError, ViewQuery, ViewerContext,
};

use crate::view_part_system::TextDocumentEntry;
Expand Down Expand Up @@ -99,12 +98,10 @@ impl SpaceViewClass for TextDocumentSpaceView {
ui: &mut egui::Ui,
state: &mut Self::State,
_root_entity_properties: &EntityProperties,
_view_ctx: &ViewContextCollection,
parts: &ViewPartCollection,
_query: &ViewQuery<'_>,
_draw_data: Vec<re_renderer::QueueableDrawData>,
system_output: re_viewer_context::SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
let text_document = parts.get::<TextDocumentSystem>()?;
let text_document = system_output.view_systems.get::<TextDocumentSystem>()?;

egui::Frame {
inner_margin: re_ui::ReUi::view_padding().into(),
Expand Down
8 changes: 3 additions & 5 deletions crates/re_space_view_text_log/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use re_types::components::TextLogLevel;
use re_viewer_context::{
level_to_rich_text, AutoSpawnHeuristic, PerSystemEntities, SpaceViewClass,
SpaceViewClassRegistryError, SpaceViewId, SpaceViewState, SpaceViewSystemExecutionError,
ViewContextCollection, ViewPartCollection, ViewQuery, ViewerContext,
ViewQuery, ViewerContext,
};

use super::view_part_system::{Entry, TextLogSystem};
Expand Down Expand Up @@ -136,13 +136,11 @@ impl SpaceViewClass for TextSpaceView {
ui: &mut egui::Ui,
state: &mut Self::State,
_root_entity_properties: &EntityProperties,
_view_ctx: &ViewContextCollection,
parts: &ViewPartCollection,
_query: &ViewQuery<'_>,
_draw_data: Vec<re_renderer::QueueableDrawData>,
system_output: re_viewer_context::SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
re_tracing::profile_function!();
let text = parts.get::<TextLogSystem>()?;
let text = system_output.view_systems.get::<TextLogSystem>()?;

// TODO(andreas): Should filter text entries in the part-system instead.
// this likely requires a way to pass state into a context.
Expand Down
9 changes: 3 additions & 6 deletions crates/re_space_view_time_series/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use re_viewer_context::external::re_data_store::{
};
use re_viewer_context::{
SpaceViewClass, SpaceViewClassRegistryError, SpaceViewId, SpaceViewState,
SpaceViewSystemExecutionError, ViewContextCollection, ViewPartCollection, ViewQuery,
ViewerContext,
SpaceViewSystemExecutionError, SystemExecutionOutput, ViewQuery, ViewerContext,
};

use crate::view_part_system::{PlotSeriesKind, TimeSeriesSystem};
Expand Down Expand Up @@ -159,10 +158,8 @@ impl SpaceViewClass for TimeSeriesSpaceView {
ui: &mut egui::Ui,
state: &mut Self::State,
root_entity_properties: &EntityProperties,
_view_ctx: &ViewContextCollection,
parts: &ViewPartCollection,
_query: &ViewQuery<'_>,
_draw_data: Vec<re_renderer::QueueableDrawData>,
system_output: SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
re_tracing::profile_function!();

Expand All @@ -177,7 +174,7 @@ impl SpaceViewClass for TimeSeriesSpaceView {

let timeline_name = timeline.name().to_string();

let time_series = parts.get::<TimeSeriesSystem>()?;
let time_series = system_output.view_systems.get::<TimeSeriesSystem>()?;

// Get the minimum time/X value for the entire plot…
let min_time = time_series.min_time.unwrap_or(0);
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl AppState {
if show_welcome {
welcome_screen.ui(ui, re_ui, rx, command_sender);
} else {
viewport.viewport_ui(ui, &mut ctx);
viewport.viewport_ui(ui, &ctx);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer_context/src/caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Caches {
}

/// A cache for memoizing things in order to speed up immediate mode UI & other immediate mode style things.
pub trait Cache: std::any::Any {
pub trait Cache: std::any::Any + Send + Sync {
/// Called once per frame to potentially flush the cache.
fn begin_frame(&mut self);

Expand Down
17 changes: 9 additions & 8 deletions crates/re_viewer_context/src/component_ui_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ pub enum UiVerbosity {

type ComponentUiCallback = Box<
dyn Fn(
&ViewerContext<'_>,
&mut egui::Ui,
UiVerbosity,
&LatestAtQuery,
&EntityPath,
&ComponentWithInstances,
&InstanceKey,
),
&ViewerContext<'_>,
&mut egui::Ui,
UiVerbosity,
&LatestAtQuery,
&EntityPath,
&ComponentWithInstances,
&InstanceKey,
) + Send
+ Sync,
>;

/// How to display components in a Ui.
Expand Down
4 changes: 2 additions & 2 deletions crates/re_viewer_context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub use space_view::{
SpaceViewClass, SpaceViewClassIdentifier, SpaceViewClassLayoutPriority, SpaceViewClassRegistry,
SpaceViewClassRegistryError, SpaceViewEntityHighlight, SpaceViewHighlights,
SpaceViewOutlineMasks, SpaceViewState, SpaceViewSystemExecutionError, SpaceViewSystemRegistry,
ViewContextCollection, ViewContextSystem, ViewPartCollection, ViewPartSystem, ViewQuery,
ViewSystemIdentifier,
SystemExecutionOutput, ViewContextCollection, ViewContextSystem, ViewPartCollection,
ViewPartSystem, ViewQuery, ViewSystemIdentifier,
};
pub use store_context::StoreContext;
pub use tensor::{TensorDecodeCache, TensorStats, TensorStatsCache};
Expand Down
Loading
Loading