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

Add radius boost for depth clouds on outline #1713

Merged
merged 8 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/re_renderer/examples/depth_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ impl RenderDepthClouds {

let world_from_obj = glam::Mat4::from_scale(glam::Vec3::splat(*scale));

let radius_boost_in_ui_points_for_outlines = 2.5;
let depth_cloud_draw_data = DepthCloudDrawData::new(
re_ctx,
radius_boost_in_ui_points_for_outlines,
&[DepthCloud {
world_from_obj,
depth_camera_intrinsics: *intrinsics,
Expand Down
19 changes: 17 additions & 2 deletions crates/re_renderer/shader/depth_cloud.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ fn compute_point_data(quad_idx: i32) -> PointData {
// ---

/// 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 Down Expand Up @@ -84,6 +86,20 @@ var<uniform> depth_cloud_info: DepthCloudInfo;
@group(1) @binding(1)
var depth_texture: texture_2d<f32>;

/// Keep in sync with `DrawDataUniformBuffer` in `depth_cloud.rs`.
///
/// Changes between the opaque and outline draw-phases.
struct DrawDataUniformBuffer {
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`
// if we wouldn't add padding here, which isn't available on WebGL.
_padding: Vec4,
};
@group(1) @binding(2)
var<uniform> draw_data: DrawDataUniformBuffer;

struct VertexOut {
@builtin(position) pos_in_clip: Vec4,
@location(0) pos_in_world: Vec3,
Expand All @@ -100,8 +116,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, draw_data.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
99 changes: 83 additions & 16 deletions crates/re_renderer/src/renderer/depth_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
//! The vertex shader backprojects the depth texture using the user-specified intrinsics, and then
//! behaves pretty much exactly like our point cloud renderer (see [`point_cloud.rs`]).

use std::num::NonZeroU64;

use smallvec::smallvec;

use crate::{
allocator::create_and_fill_uniform_buffer_batch,
allocator::{create_and_fill_uniform_buffer, create_and_fill_uniform_buffer_batch},
include_file,
renderer::OutlineMaskProcessor,
resource_managers::ResourceManagerError,
Expand All @@ -35,17 +37,20 @@ use super::{
// ---

mod gpu_data {
// - Keep in sync with mirror in depth_cloud.wgsl.
// - See `DepthCloud` for documentation.
use crate::wgpu_buffer_types;

/// Uniform buffer that is constant across all draw phases.
///
/// Keep in sync with mirror in `depth_cloud.wgsl.`
#[repr(C, align(256))]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct DepthCloudInfoUBO {
/// The extrinsics of the camera used for the projection.
pub world_from_obj: crate::wgpu_buffer_types::Mat4,
pub world_from_obj: wgpu_buffer_types::Mat4,

pub depth_camera_intrinsics: crate::wgpu_buffer_types::Mat3,
pub depth_camera_intrinsics: wgpu_buffer_types::Mat3,

pub outline_mask_id: crate::wgpu_buffer_types::UVec2,
pub outline_mask_id: wgpu_buffer_types::UVec2,

/// Multiplier to get world-space depth from whatever is in the texture.
pub world_depth_from_texture_value: f32,
Expand All @@ -59,7 +64,7 @@ mod gpu_data {
pub colormap: u32,
pub row_pad: [u32; 2],

pub end_padding: [crate::wgpu_buffer_types::PaddingRow; 16 - 4 - 3 - 1 - 1],
pub end_padding: [wgpu_buffer_types::PaddingRow; 16 - 4 - 3 - 1 - 1],
}

impl DepthCloudInfoUBO {
Expand Down Expand Up @@ -96,6 +101,16 @@ mod gpu_data {
}
}
}

/// Uniform buffer that changes between the opaque and outline draw-phases.
///
/// Keep in sync with mirror in `depth_cloud.wgsl.`
#[repr(C, align(256))]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct DrawDataUniformBuffer {
pub radius_boost_in_ui_points: wgpu_buffer_types::F32RowPadded,
pub end_padding: [wgpu_buffer_types::PaddingRow; 16 - 1],
}
}

/// The raw data from a depth texture.
Expand Down Expand Up @@ -159,7 +174,8 @@ pub struct DepthCloud {

#[derive(Clone)]
struct DepthCloudDrawInstance {
bind_group: GpuBindGroup,
bind_group_opaque: GpuBindGroup,
bind_group_outline: GpuBindGroup,
num_points: u32,
render_outline_mask: bool,
}
Expand All @@ -176,6 +192,7 @@ impl DrawData for DepthCloudDrawData {
impl DepthCloudDrawData {
pub fn new(
ctx: &mut RenderContext,
radius_boost_in_ui_points_for_outlines: f32,
depth_clouds: &[DepthCloud],
) -> Result<Self, ResourceManagerError> {
crate::profile_function!();
Expand All @@ -197,7 +214,7 @@ impl DepthCloudDrawData {
});
}

let depth_cloud_ubos = create_and_fill_uniform_buffer_batch(
let depth_cloud_ubo_bindings = create_and_fill_uniform_buffer_batch(
ctx,
"depth_cloud_ubos".into(),
depth_clouds
Expand All @@ -206,7 +223,10 @@ impl DepthCloudDrawData {
);

let mut instances = Vec::with_capacity(depth_clouds.len());
for (depth_cloud, ubo) in depth_clouds.iter().zip(depth_cloud_ubos.into_iter()) {
for (depth_cloud, ubo) in depth_clouds
.iter()
.zip(depth_cloud_ubo_bindings.into_iter())
{
let depth_texture = match &depth_cloud.depth_data {
DepthCloudDepthData::U16(data) => {
if cfg!(target_arch = "wasm32") {
Expand Down Expand Up @@ -245,20 +265,49 @@ impl DepthCloudDrawData {
),
};

instances.push(DepthCloudDrawInstance {
num_points: depth_cloud.depth_dimensions.x * depth_cloud.depth_dimensions.y,
bind_group: ctx.gpu_resources.bind_groups.alloc(
let mk_bind_group = |label, draw_data_uniform_buffer| {
ctx.gpu_resources.bind_groups.alloc(
&ctx.device,
&ctx.gpu_resources,
&BindGroupDesc {
label: "depth_cloud_bg".into(),
label,
entries: smallvec![
ubo,
ubo.clone(),
BindGroupEntry::DefaultTextureView(depth_texture.handle),
draw_data_uniform_buffer
],
layout: bg_layout,
},
)
};

let bind_group_outline = mk_bind_group(
"depth_cloud_outline_mask".into(),
create_and_fill_uniform_buffer(
ctx,
"PointCloudDrawData::DrawDataUniformBuffer_outline_mask".into(),
gpu_data::DrawDataUniformBuffer {
radius_boost_in_ui_points: radius_boost_in_ui_points_for_outlines.into(),
end_padding: Default::default(),
},
),
);
let bind_group_opaque = mk_bind_group(
"depth_cloud_bg".into(),
create_and_fill_uniform_buffer(
ctx,
"PointCloudDrawData::DrawDataUniformBuffer".into(),
Copy link
Member

Choose a reason for hiding this comment

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

I started naming those by their field names and I think that's better practice than what we had before. I.e it would be DepthCloudDrawInstance::bind_group_opaque here. Not consistent across everywhere though, jury is still out on this ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

I think you are in a better position to take a rename-pass over all of re_renderer

gpu_data::DrawDataUniformBuffer {
radius_boost_in_ui_points: 0.0.into(),
end_padding: Default::default(),
},
),
);

instances.push(DepthCloudDrawInstance {
num_points: depth_cloud.depth_dimensions.x * depth_cloud.depth_dimensions.y,
bind_group_opaque,
bind_group_outline,
render_outline_mask: depth_cloud.outline_mask_id.is_some(),
});
}
Expand Down Expand Up @@ -392,6 +441,18 @@ impl Renderer for DepthCloudRenderer {
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: NonZeroU64::new(std::mem::size_of::<
gpu_data::DrawDataUniformBuffer,
>() as _),
},
count: None,
},
],
},
);
Expand Down Expand Up @@ -492,7 +553,13 @@ impl Renderer for DepthCloudRenderer {
continue;
}

pass.set_bind_group(1, &instance.bind_group, &[]);
let bind_group = match phase {
DrawPhase::OutlineMask => &instance.bind_group_outline,
DrawPhase::Opaque => &instance.bind_group_opaque,
_ => unreachable!(),
};

pass.set_bind_group(1, bind_group, &[]);
pass.draw(0..instance.num_points * 6, 0..1);
}

Expand Down
8 changes: 4 additions & 4 deletions crates/re_renderer/src/renderer/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub mod gpu_data {
#[repr(C, align(256))]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct DrawDataUniformBuffer {
pub size_boost_in_points: wgpu_buffer_types::F32RowPadded,
pub radius_boost_in_ui_points: wgpu_buffer_types::F32RowPadded,
pub end_padding: [wgpu_buffer_types::PaddingRow; 16 - 1],
}

Expand Down Expand Up @@ -325,7 +325,7 @@ impl LineDrawData {
vertices: &[gpu_data::LineVertex],
strips: &[LineStripInfo],
batches: &[LineBatchInfo],
size_boost_in_points_for_outlines: f32,
radius_boost_in_ui_points_for_outlines: f32,
) -> Result<Self, LineDrawDataError> {
let mut renderers = ctx.renderers.write();
let line_renderer = renderers.get_or_create::<_, LineRenderer>(
Expand Down Expand Up @@ -512,11 +512,11 @@ impl LineDrawData {
"LineDrawData::DrawDataUniformBuffer".into(),
[
gpu_data::DrawDataUniformBuffer {
size_boost_in_points: 0.0.into(),
radius_boost_in_ui_points: 0.0.into(),
end_padding: Default::default(),
},
gpu_data::DrawDataUniformBuffer {
size_boost_in_points: size_boost_in_points_for_outlines.into(),
radius_boost_in_ui_points: radius_boost_in_ui_points_for_outlines.into(),
end_padding: Default::default(),
},
]
Expand Down
Loading