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

Depth offset for lines & points #2052

Merged
merged 2 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 39 additions & 7 deletions crates/re_renderer/examples/2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,48 @@ impl framework::Example for Render2D {
std::iter::repeat(re_renderer::PickingLayerInstanceId::default()),
);

// Pile stuff to test for overlap handling
// Pile stuff to test for overlap handling.
// Do in individual batches to test depth offset.
{
let mut batch = line_strip_builder.batch("overlapping objects");
for i in 0..10 {
let x = 5.0 * i as f32 + 20.0;
let num_lines = 20_i16;
let y_range = 700.0..780.0;

// Cycle through which line is on top.
let top_line = ((time.seconds_since_startup() * 6.0) as i16 % (num_lines * 2 - 1)
- num_lines)
.abs();
for i in 0..num_lines {
let depth_offset = if i < top_line { i } else { top_line * 2 - i };
let mut batch = line_strip_builder
.batch(format!("overlapping objects {i}"))
.depth_offset(depth_offset);

let x = 15.0 * i as f32 + 20.0;
batch
.add_segment_2d(glam::vec2(x, 700.0), glam::vec2(x, 780.0))
.color(Hsva::new(10.0 / i as f32, 1.0, 0.5, 1.0).into())
.radius(Size::new_points(10.0));
.add_segment_2d(glam::vec2(x, y_range.start), glam::vec2(x, y_range.end))
.color(Hsva::new(0.25 / num_lines as f32 * i as f32, 1.0, 0.5, 1.0).into())
.radius(Size::new_points(10.0))
.flags(LineStripFlags::FLAG_COLOR_GRADIENT);
}

let num_points = 8;
let size = Size::new_points(3.0);
point_cloud_builder
.batch("points overlapping with lines")
.depth_offset(5)
.add_points_2d(
num_points,
(0..num_points).map(|i| {
glam::vec2(
30.0 * i as f32 + 20.0,
y_range.start
+ (y_range.end - y_range.start) / num_points as f32 * i as f32,
)
}),
std::iter::repeat(size),
std::iter::repeat(Color32::WHITE),
std::iter::repeat(re_renderer::PickingLayerInstanceId::default()),
);
}

let line_strip_draw_data = line_strip_builder.to_draw_data(re_ctx).unwrap();
Expand Down
4 changes: 3 additions & 1 deletion crates/re_renderer/shader/lines.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#import <./utils/flags.wgsl>
#import <./utils/size.wgsl>
#import <./utils/srgb.wgsl>
#import <./utils/depth_offset.wgsl>

@group(1) @binding(0)
var line_strip_texture: texture_2d<f32>;
Expand All @@ -28,6 +29,7 @@ struct BatchUniformBuffer {
world_from_obj: Mat4,
outline_mask_ids: UVec2,
picking_layer_object_id: UVec2,
depth_offset: f32,
};
@group(2) @binding(0)
var<uniform> batch: BatchUniformBuffer;
Expand Down Expand Up @@ -262,7 +264,7 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {

// Output, transform to projection space and done.
var out: VertexOut;
out.position = frame.projection_from_world * Vec4(pos, 1.0);
out.position = apply_depth_offset(frame.projection_from_world * Vec4(pos, 1.0), batch.depth_offset);
out.position_world = pos;
out.center_position = center_position;
out.round_cap_circle_center = round_cap_circle_center;
Expand Down
6 changes: 4 additions & 2 deletions crates/re_renderer/shader/point_cloud.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import <./utils/flags.wgsl>
#import <./utils/size.wgsl>
#import <./utils/sphere_quad.wgsl>
#import <./utils/depth_offset.wgsl>

@group(1) @binding(0)
var position_data_texture: texture_2d<f32>;
Expand All @@ -26,7 +27,8 @@ var<uniform> draw_data: DrawDataUniformBuffer;
struct BatchUniformBuffer {
world_from_obj: Mat4,
flags: u32,
_padding: UVec2, // UVec3 would take its own 4xf32 row, UVec2 is on the same as flags
depth_offset: f32,
_padding: UVec2,
outline_mask: UVec2,
picking_layer_object_id: UVec2,
};
Expand Down Expand Up @@ -101,7 +103,7 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {

// Output, transform to projection space and done.
var out: VertexOut;
out.position = frame.projection_from_world * Vec4(quad.pos_in_world, 1.0);
out.position = apply_depth_offset(frame.projection_from_world * Vec4(quad.pos_in_world, 1.0), batch.depth_offset);
out.color = point_data.color;
out.radius = quad.point_resolved_radius;
out.world_position = quad.pos_in_world;
Expand Down
12 changes: 10 additions & 2 deletions crates/re_renderer/src/line_strip_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
renderer::{
LineBatchInfo, LineDrawData, LineDrawDataError, LineStripFlags, LineStripInfo, LineVertex,
},
Color32, DebugLabel, OutlineMaskPreference, PickingLayerInstanceId, PickingLayerObjectId,
RenderContext, Size,
Color32, DebugLabel, DepthOffset, OutlineMaskPreference, PickingLayerInstanceId,
PickingLayerObjectId, RenderContext, Size,
};

/// Builder for a vector of line strips, making it easy to create [`crate::renderer::LineDrawData`].
Expand Down Expand Up @@ -70,6 +70,7 @@ impl LineStripSeriesBuilder {
overall_outline_mask_ids: OutlineMaskPreference::NONE,
additional_outline_mask_ids_vertex_ranges: Vec::new(),
picking_object_id: PickingLayerObjectId::default(),
depth_offset: 0,
});

LineBatchBuilder(self)
Expand Down Expand Up @@ -163,6 +164,13 @@ impl<'a> LineBatchBuilder<'a> {
self
}

/// Sets the depth offset for the entire batch.
#[inline]
pub fn depth_offset(mut self, depth_offset: DepthOffset) -> Self {
self.batch_mut().depth_offset = depth_offset;
self
}

/// Adds a 3D series of line connected points.
pub fn add_strip(&mut self, points: impl Iterator<Item = glam::Vec3>) -> LineStripBuilder<'_> {
let old_strip_count = self.0.strips.len();
Expand Down
11 changes: 10 additions & 1 deletion crates/re_renderer/src/point_cloud_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::{
PointCloudBatchFlags, PointCloudBatchInfo, PointCloudDrawData, PointCloudDrawDataError,
PointCloudVertex,
},
Color32, DebugLabel, OutlineMaskPreference, PickingLayerInstanceId, RenderContext, Size,
Color32, DebugLabel, DepthOffset, OutlineMaskPreference, PickingLayerInstanceId, RenderContext,
Size,
};

/// Builder for point clouds, making it easy to create [`crate::renderer::PointCloudDrawData`].
Expand Down Expand Up @@ -69,6 +70,7 @@ impl PointCloudBuilder {
overall_outline_mask_ids: OutlineMaskPreference::NONE,
additional_outline_mask_ids_vertex_ranges: Vec::new(),
picking_object_id: Default::default(),
depth_offset: 0,
});

PointCloudBatchBuilder(self)
Expand Down Expand Up @@ -140,6 +142,13 @@ impl<'a> PointCloudBatchBuilder<'a> {
self
}

/// Sets the depth offset for the entire batch.
#[inline]
pub fn depth_offset(mut self, depth_offset: DepthOffset) -> Self {
self.batch_mut().depth_offset = depth_offset;
self
}

/// Add several 3D points
///
/// Returns a `PointBuilder` which can be used to set the colors, radii, and user-data for the points.
Expand Down
14 changes: 11 additions & 3 deletions crates/re_renderer/src/renderer/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ use crate::{
BindGroupDesc, BindGroupEntry, BindGroupLayoutDesc, GpuBindGroup, GpuBindGroupLayoutHandle,
GpuRenderPipelineHandle, PipelineLayoutDesc, PoolError, RenderPipelineDesc, TextureDesc,
},
Color32, DebugLabel, LineStripSeriesBuilder, OutlineMaskPreference, PickingLayerObjectId,
PickingLayerProcessor,
Color32, DebugLabel, DepthOffset, LineStripSeriesBuilder, OutlineMaskPreference,
PickingLayerObjectId, PickingLayerProcessor,
};

use super::{
Expand Down Expand Up @@ -174,7 +174,9 @@ pub mod gpu_data {
pub outline_mask_ids: wgpu_buffer_types::UVec2,
pub picking_object_id: PickingLayerObjectId,

pub end_padding: [wgpu_buffer_types::PaddingRow; 16 - 5],
pub depth_offset: wgpu_buffer_types::F32RowPadded,

pub end_padding: [wgpu_buffer_types::PaddingRow; 16 - 6],
}
}

Expand Down Expand Up @@ -272,6 +274,9 @@ pub struct LineBatchInfo {

/// Picking object id that applies for the entire batch.
pub picking_object_id: PickingLayerObjectId,

/// Depth offset applied after projection.
pub depth_offset: DepthOffset,
}

/// Style information for a line strip.
Expand Down Expand Up @@ -368,6 +373,7 @@ impl LineDrawData {
overall_outline_mask_ids: OutlineMaskPreference::NONE,
picking_object_id: PickingLayerObjectId::default(),
additional_outline_mask_ids_vertex_ranges: Vec::new(),
depth_offset: 0,
}]
} else {
batches
Expand Down Expand Up @@ -620,6 +626,7 @@ impl LineDrawData {
.unwrap_or_default()
.into(),
picking_object_id: batch_info.picking_object_id,
depth_offset: (batch_info.depth_offset as f32).into(),
end_padding: Default::default(),
}),
);
Expand All @@ -640,6 +647,7 @@ impl LineDrawData {
world_from_obj: batch_info.world_from_obj.into(),
outline_mask_ids: mask.0.unwrap_or_default().into(),
picking_object_id: batch_info.picking_object_id,
depth_offset: (batch_info.depth_offset as f32).into(),
end_padding: Default::default(),
})
})
Expand Down
28 changes: 21 additions & 7 deletions crates/re_renderer/src/renderer/point_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{num::NonZeroU64, ops::Range};
use crate::{
allocator::create_and_fill_uniform_buffer_batch,
draw_phases::{DrawPhase, OutlineMaskProcessor, PickingLayerObjectId, PickingLayerProcessor},
include_shader_module, DebugLabel, OutlineMaskPreference, PointCloudBuilder,
include_shader_module, DebugLabel, DepthOffset, OutlineMaskPreference, PointCloudBuilder,
};
use bitflags::bitflags;
use bytemuck::Zeroable as _;
Expand Down Expand Up @@ -75,12 +75,16 @@ mod gpu_data {
pub end_padding: [wgpu_buffer_types::PaddingRow; 16 - 1],
}

/// Uniform buffer that changes for every batch of points.
/// Uniform buffer that changes for every 1 of points.
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
#[repr(C, align(256))]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct BatchUniformBuffer {
pub world_from_obj: wgpu_buffer_types::Mat4,
pub flags: wgpu_buffer_types::U32RowPadded, // PointCloudBatchFlags

pub flags: u32, // PointCloudBatchFlags
pub depth_offset: f32,
pub padding: [f32; 2],
Wumpf marked this conversation as resolved.
Show resolved Hide resolved

pub outline_mask_ids: wgpu_buffer_types::UVec2,
pub picking_object_id: PickingLayerObjectId,

Expand Down Expand Up @@ -141,6 +145,9 @@ pub struct PointCloudBatchInfo {

/// Picking object id that applies for the entire batch.
pub picking_object_id: PickingLayerObjectId,

/// Depth offset applied after projection.
pub depth_offset: DepthOffset,
}

/// Description of a point cloud.
Expand Down Expand Up @@ -209,6 +216,7 @@ impl PointCloudDrawData {
overall_outline_mask_ids: OutlineMaskPreference::NONE,
additional_outline_mask_ids_vertex_ranges: Vec::new(),
picking_object_id: Default::default(),
depth_offset: 0,
}];
let batches = if batches.is_empty() {
&fallback_batches
Expand Down Expand Up @@ -400,14 +408,17 @@ impl PointCloudDrawData {
.iter()
.map(|batch_info| gpu_data::BatchUniformBuffer {
world_from_obj: batch_info.world_from_obj.into(),
flags: batch_info.flags.bits.into(),
flags: batch_info.flags.bits,
outline_mask_ids: batch_info
.overall_outline_mask_ids
.0
.unwrap_or_default()
.into(),
end_padding: Default::default(),
picking_object_id: batch_info.picking_object_id,
depth_offset: batch_info.depth_offset as f32,

padding: [0.0, 0.0],
end_padding: Default::default(),
}),
);

Expand All @@ -425,10 +436,13 @@ impl PointCloudDrawData {
.iter()
.map(|(_, mask)| gpu_data::BatchUniformBuffer {
world_from_obj: batch_info.world_from_obj.into(),
flags: batch_info.flags.bits.into(),
flags: batch_info.flags.bits,
outline_mask_ids: mask.0.unwrap_or_default().into(),
end_padding: Default::default(),
picking_object_id: batch_info.picking_object_id,
depth_offset: batch_info.depth_offset as f32,

padding: [0.0, 0.0],
end_padding: Default::default(),
})
})
.collect::<Vec<_>>()
Expand Down