Skip to content

Commit

Permalink
Add radius boost for depth clouds on outline (#1713)
Browse files Browse the repository at this point in the history
* point_cloud.rs: remove code duplication and remove clones

* Implement outline size boost for depth clouds

* Pass in the arguments another way

* Only boost outline radius, not point

* Rename `size_boost_in_points` to `radius_boost_in_ui_points`

* Document the uniform buffers

* Combine the uniform buffers

* Create DepthClouds
  • Loading branch information
emilk authored Mar 27, 2023
1 parent 3390626 commit d42c13d
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 124 deletions.
29 changes: 16 additions & 13 deletions crates/re_renderer/examples/depth_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use itertools::Itertools;
use macaw::IsoTransform;
use re_renderer::{
renderer::{
DepthCloud, DepthCloudDepthData, DepthCloudDrawData, DrawData, GenericSkyboxDrawData,
RectangleDrawData, TexturedRect,
DepthCloud, DepthCloudDepthData, DepthCloudDrawData, DepthClouds, DrawData,
GenericSkyboxDrawData, RectangleDrawData, TexturedRect,
},
resource_managers::{GpuTexture2DHandle, Texture2DCreationDesc},
view_builder::{self, Projection, ViewBuilder},
Expand Down Expand Up @@ -172,17 +172,20 @@ impl RenderDepthClouds {

let depth_cloud_draw_data = DepthCloudDrawData::new(
re_ctx,
&[DepthCloud {
world_from_obj,
depth_camera_intrinsics: *intrinsics,
world_depth_from_data_depth: 1.0,
point_radius_from_world_depth: *point_radius_from_world_depth,
max_depth_in_world: 5.0,
depth_dimensions: depth.dimensions,
depth_data: depth.data.clone(),
colormap: re_renderer::ColorMap::ColorMapTurbo,
outline_mask_id: Default::default(),
}],
&DepthClouds {
clouds: vec![DepthCloud {
world_from_obj,
depth_camera_intrinsics: *intrinsics,
world_depth_from_data_depth: 1.0,
point_radius_from_world_depth: *point_radius_from_world_depth,
max_depth_in_world: 5.0,
depth_dimensions: depth.dimensions,
depth_data: depth.data.clone(),
colormap: re_renderer::ColorMap::ColorMapTurbo,
outline_mask_id: Default::default(),
}],
radius_boost_in_ui_points_for_outlines: 2.5,
},
)
.unwrap();

Expand Down
70 changes: 37 additions & 33 deletions crates/re_renderer/shader/depth_cloud.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,11 @@ struct PointData {
color: Vec4
}

// Backprojects the depth texture using the intrinsics passed in the uniform buffer.
fn compute_point_data(quad_idx: i32) -> PointData {
let wh = textureDimensions(depth_texture);
let texcoords = IVec2(quad_idx % wh.x, quad_idx / wh.x);

// TODO(cmc): expose knobs to linearize/normalize/flip/cam-to-plane depth.
let world_space_depth = depth_cloud_info.world_depth_from_texture_value * textureLoad(depth_texture, texcoords, 0).x;

// TODO(cmc): albedo textures
let color = Vec4(colormap_linear(depth_cloud_info.colormap, world_space_depth / depth_cloud_info.max_depth_in_world), 1.0);

// TODO(cmc): This assumes a pinhole camera; need to support other kinds at some point.
let intrinsics = depth_cloud_info.depth_camera_intrinsics;
let focal_length = Vec2(intrinsics[0][0], intrinsics[1][1]);
let offset = Vec2(intrinsics[2][0], intrinsics[2][1]);

let pos_in_obj = Vec3(
(Vec2(texcoords) - offset) * world_space_depth / focal_length,
world_space_depth,
);

let pos_in_world = depth_cloud_info.world_from_obj * Vec4(pos_in_obj, 1.0);

var data: PointData;
data.pos_in_world = pos_in_world.xyz;
data.unresolved_radius = depth_cloud_info.point_radius_from_world_depth * world_space_depth;
data.color = color;

return data;
}

// ---

/// Keep in sync with `DepthCloudInfoUBO` in `depth_cloud.rs`.
///
/// Same for all draw-phases.
struct DepthCloudInfo {
/// The extrinsincs of the camera used for the projection.
world_from_obj: Mat4,
Expand All @@ -76,6 +47,9 @@ struct DepthCloudInfo {

/// Configures color mapping mode, see `colormap.wgsl`.
colormap: u32,

/// Changes between the opaque and outline draw-phases.
radius_boost_in_ui_points: f32,
};

@group(1) @binding(0)
Expand All @@ -92,6 +66,37 @@ struct VertexOut {
@location(3) point_radius: f32,
};

// Backprojects the depth texture using the intrinsics passed in the uniform buffer.
fn compute_point_data(quad_idx: i32) -> PointData {
let wh = textureDimensions(depth_texture);
let texcoords = IVec2(quad_idx % wh.x, quad_idx / wh.x);

// TODO(cmc): expose knobs to linearize/normalize/flip/cam-to-plane depth.
let world_space_depth = depth_cloud_info.world_depth_from_texture_value * textureLoad(depth_texture, texcoords, 0).x;

// TODO(cmc): albedo textures
let color = Vec4(colormap_linear(depth_cloud_info.colormap, world_space_depth / depth_cloud_info.max_depth_in_world), 1.0);

// TODO(cmc): This assumes a pinhole camera; need to support other kinds at some point.
let intrinsics = depth_cloud_info.depth_camera_intrinsics;
let focal_length = Vec2(intrinsics[0][0], intrinsics[1][1]);
let offset = Vec2(intrinsics[2][0], intrinsics[2][1]);

let pos_in_obj = Vec3(
(Vec2(texcoords) - offset) * world_space_depth / focal_length,
world_space_depth,
);

let pos_in_world = depth_cloud_info.world_from_obj * Vec4(pos_in_obj, 1.0);

var data: PointData;
data.pos_in_world = pos_in_world.xyz;
data.unresolved_radius = depth_cloud_info.point_radius_from_world_depth * world_space_depth;
data.color = color;

return data;
}

@vertex
fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
let quad_idx = sphere_quad_index(vertex_idx);
Expand All @@ -100,8 +105,7 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
let point_data = compute_point_data(quad_idx);

// Span quad
// TODO(andreas): Implement outline-mask size boost for depth cloud as well.
let quad = sphere_quad_span(vertex_idx, point_data.pos_in_world, point_data.unresolved_radius, 0.0);
let quad = sphere_quad_span(vertex_idx, point_data.pos_in_world, point_data.unresolved_radius, depth_cloud_info.radius_boost_in_ui_points);

var out: VertexOut;
out.pos_in_clip = frame.projection_from_world * Vec4(quad.pos_in_world, 1.0);
Expand Down
6 changes: 3 additions & 3 deletions crates/re_renderer/shader/lines.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var line_strip_texture: texture_2d<f32>;
var position_data_texture: texture_2d<u32>;

struct DrawDataUniformBuffer {
size_boost_in_points: f32,
radius_boost_in_ui_points: f32,
// In actuality there is way more padding than this since we align all our uniform buffers to
// 256bytes in order to allow them to be buffer-suballocations.
// However, wgpu doesn't know this at this point and therefore requires `DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED`
Expand Down Expand Up @@ -200,8 +200,8 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
let camera_ray = camera_ray_to_world_pos(center_position);
let camera_distance = distance(camera_ray.origin, center_position);
var strip_radius = unresolved_size_to_world(strip_data.unresolved_radius, camera_distance, frame.auto_size_lines);
if draw_data.size_boost_in_points > 0.0 {
let size_boost = world_size_from_point_size(draw_data.size_boost_in_points, camera_distance);
if draw_data.radius_boost_in_ui_points > 0.0 {
let size_boost = world_size_from_point_size(draw_data.radius_boost_in_ui_points, camera_distance);
strip_radius += size_boost;
// Push out positions as well along the quad dir.
// This is especially important if there's no miters on a line-strip (TODO(#829)),
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/shader/point_cloud.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var position_data_texture: texture_2d<f32>;
var color_texture: texture_2d<f32>;

struct DrawDataUniformBuffer {
size_boost_in_points: f32,
radius_boost_in_ui_points: f32,
// In actuality there is way more padding than this since we align all our uniform buffers to
// 256bytes in order to allow them to be buffer-suballocations.
// However, wgpu doesn't know this at this point and therefore requires `DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED`
Expand Down Expand Up @@ -74,7 +74,7 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
let point_data = read_data(quad_idx);

// Span quad
let quad = sphere_quad_span(vertex_idx, point_data.pos, point_data.unresolved_radius, draw_data.size_boost_in_points);
let quad = sphere_quad_span(vertex_idx, point_data.pos, point_data.unresolved_radius, draw_data.radius_boost_in_ui_points);

// Output, transform to projection space and done.
var out: VertexOut;
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/shader/utils/sphere_quad.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ struct SphereQuadData {
/// Span a quad onto which perspective correct spheres can be drawn.
///
/// Spanning is done in perspective or orthographically depending of the state of the global cam.
fn sphere_quad_span(vertex_idx: u32, point_pos: Vec3, point_unresolved_radius: f32, size_boost_in_points: f32) -> SphereQuadData {
fn sphere_quad_span(vertex_idx: u32, point_pos: Vec3, point_unresolved_radius: f32, radius_boost_in_ui_points: f32) -> SphereQuadData {
// Resolve radius to a world size. We need the camera distance for this, which is useful later on.
let to_camera = frame.camera_position - point_pos;
let camera_distance = length(to_camera);
let radius = unresolved_size_to_world(point_unresolved_radius, camera_distance, frame.auto_size_points) +
world_size_from_point_size(size_boost_in_points, camera_distance);
world_size_from_point_size(radius_boost_in_ui_points, camera_distance);

// Basic properties of the vertex we're at.
let local_idx = vertex_idx % 6u;
Expand Down
12 changes: 6 additions & 6 deletions crates/re_renderer/src/line_strip_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct LineStripSeriesBuilder<PerStripUserData> {

pub batches: Vec<LineBatchInfo>,

pub(crate) size_boost_in_points_for_outlines: f32,
pub(crate) radius_boost_in_ui_points_for_outlines: f32,
}

impl<PerStripUserData> LineStripSeriesBuilder<PerStripUserData>
Expand All @@ -39,16 +39,16 @@ where
strips: Vec::with_capacity(RESERVE_SIZE),
strip_user_data: Vec::with_capacity(RESERVE_SIZE),
batches: Vec::with_capacity(16),
size_boost_in_points_for_outlines: 0.0,
radius_boost_in_ui_points_for_outlines: 0.0,
}
}

/// Boosts the size of the points by the given amount of ui-points for the purpose of drawing outlines.
pub fn size_boost_in_points_for_outlines(
pub fn radius_boost_in_ui_points_for_outlines(
mut self,
size_boost_in_points_for_outlines: f32,
radius_boost_in_ui_points_for_outlines: f32,
) -> Self {
self.size_boost_in_points_for_outlines = size_boost_in_points_for_outlines;
self.radius_boost_in_ui_points_for_outlines = radius_boost_in_ui_points_for_outlines;
self
}

Expand Down Expand Up @@ -93,7 +93,7 @@ where
&self.vertices,
&self.strips,
&self.batches,
self.size_boost_in_points_for_outlines,
self.radius_boost_in_ui_points_for_outlines,
)
.unwrap()
}
Expand Down
10 changes: 5 additions & 5 deletions crates/re_renderer/src/point_cloud_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct PointCloudBuilder<PerPointUserData> {

pub(crate) batches: Vec<PointCloudBatchInfo>,

pub(crate) size_boost_in_points_for_outlines: f32,
pub(crate) radius_boost_in_ui_points_for_outlines: f32,
}

impl<PerPointUserData> PointCloudBuilder<PerPointUserData>
Expand All @@ -39,16 +39,16 @@ where
color_buffer,
user_data: Vec::with_capacity(RESERVE_SIZE),
batches: Vec::with_capacity(16),
size_boost_in_points_for_outlines: 0.0,
radius_boost_in_ui_points_for_outlines: 0.0,
}
}

/// Boosts the size of the points by the given amount of ui-points for the purpose of drawing outlines.
pub fn size_boost_in_points_for_outlines(
pub fn radius_boost_in_ui_points_for_outlines(
mut self,
size_boost_in_points_for_outlines: f32,
radius_boost_in_ui_points_for_outlines: f32,
) -> Self {
self.size_boost_in_points_for_outlines = size_boost_in_points_for_outlines;
self.radius_boost_in_ui_points_for_outlines = radius_boost_in_ui_points_for_outlines;
self
}

Expand Down
Loading

1 comment on commit d42c13d

@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: d42c13d Previous: 3390626 Ratio
datastore/insert/batch/rects/insert 584674 ns/iter (± 1349) 600453 ns/iter (± 3693) 0.97
datastore/latest_at/batch/rects/query 1853 ns/iter (± 78) 1858 ns/iter (± 34) 1.00
datastore/latest_at/missing_components/primary 288 ns/iter (± 12) 295 ns/iter (± 2) 0.98
datastore/latest_at/missing_components/secondaries 445 ns/iter (± 1) 435 ns/iter (± 6) 1.02
datastore/range/batch/rects/query 152203 ns/iter (± 584) 152209 ns/iter (± 982) 1.00
mono_points_arrow/generate_message_bundles 29238717 ns/iter (± 1678578) 30510877 ns/iter (± 1104506) 0.96
mono_points_arrow/generate_messages 130720574 ns/iter (± 1483165) 129180999 ns/iter (± 1742998) 1.01
mono_points_arrow/encode_log_msg 164935632 ns/iter (± 1270014) 164849587 ns/iter (± 1191442) 1.00
mono_points_arrow/encode_total 334108007 ns/iter (± 2185852) 327434630 ns/iter (± 2848671) 1.02
mono_points_arrow/decode_log_msg 188533186 ns/iter (± 1121499) 184085765 ns/iter (± 1993974) 1.02
mono_points_arrow/decode_message_bundles 61657110 ns/iter (± 1703806) 59548850 ns/iter (± 1224050) 1.04
mono_points_arrow/decode_total 244437992 ns/iter (± 1754846) 240984242 ns/iter (± 2473122) 1.01
batch_points_arrow/generate_message_bundles 319583 ns/iter (± 756) 312256 ns/iter (± 3745) 1.02
batch_points_arrow/generate_messages 5897 ns/iter (± 17) 5773 ns/iter (± 72) 1.02
batch_points_arrow/encode_log_msg 352410 ns/iter (± 899) 351675 ns/iter (± 3046) 1.00
batch_points_arrow/encode_total 702749 ns/iter (± 1647) 685795 ns/iter (± 6090) 1.02
batch_points_arrow/decode_log_msg 343991 ns/iter (± 638) 345143 ns/iter (± 2810) 1.00
batch_points_arrow/decode_message_bundles 1583 ns/iter (± 7) 1533 ns/iter (± 20) 1.03
batch_points_arrow/decode_total 354071 ns/iter (± 1034) 351177 ns/iter (± 2941) 1.01
arrow_mono_points/insert 7135171987 ns/iter (± 25663603) 7068719477 ns/iter (± 209325033) 1.01
arrow_mono_points/query 1794210 ns/iter (± 7677) 1754215 ns/iter (± 19640) 1.02
arrow_batch_points/insert 3054701 ns/iter (± 18691) 3102805 ns/iter (± 29435) 0.98
arrow_batch_points/query 16495 ns/iter (± 64) 16338 ns/iter (± 229) 1.01
arrow_batch_vecs/insert 44813 ns/iter (± 120) 43602 ns/iter (± 488) 1.03
arrow_batch_vecs/query 388680 ns/iter (± 786) 390186 ns/iter (± 4412) 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.