Skip to content

Commit

Permalink
[pure refactor] "namespace" flag parameters for linestrip & point clo…
Browse files Browse the repository at this point in the history
…ud shader flags (#2033)

* pseudo namespace point & line flags

* color gradient on lines is now opt-in, no longer inverting the flag meaning

* remove accidental color graident
  • Loading branch information
Wumpf committed May 4, 2023
1 parent 73aecfe commit a676bdb
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 80 deletions.
18 changes: 9 additions & 9 deletions crates/re_renderer/examples/2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ impl framework::Example for Render2D {
let mut line_batch = line_strip_builder.batch("line cap variations");
for (i, flags) in [
LineStripFlags::empty(),
LineStripFlags::CAP_START_ROUND,
LineStripFlags::CAP_END_ROUND,
LineStripFlags::CAP_START_TRIANGLE,
LineStripFlags::CAP_END_TRIANGLE,
LineStripFlags::CAP_START_ROUND | LineStripFlags::CAP_END_ROUND,
LineStripFlags::CAP_START_ROUND | LineStripFlags::CAP_END_TRIANGLE,
LineStripFlags::CAP_START_TRIANGLE | LineStripFlags::CAP_END_ROUND,
LineStripFlags::CAP_START_TRIANGLE | LineStripFlags::CAP_END_TRIANGLE,
LineStripFlags::FLAG_CAP_START_ROUND,
LineStripFlags::FLAG_CAP_END_ROUND,
LineStripFlags::FLAG_CAP_START_TRIANGLE,
LineStripFlags::FLAG_CAP_END_TRIANGLE,
LineStripFlags::FLAG_CAP_START_ROUND | LineStripFlags::FLAG_CAP_END_ROUND,
LineStripFlags::FLAG_CAP_START_ROUND | LineStripFlags::FLAG_CAP_END_TRIANGLE,
LineStripFlags::FLAG_CAP_START_TRIANGLE | LineStripFlags::FLAG_CAP_END_ROUND,
LineStripFlags::FLAG_CAP_START_TRIANGLE | LineStripFlags::FLAG_CAP_END_TRIANGLE,
]
.iter()
.enumerate()
Expand All @@ -119,7 +119,7 @@ impl framework::Example for Render2D {
line_batch
.add_segment_2d(glam::vec2(70.0, y), glam::vec2(400.0, y))
.radius(Size::new_scene(15.0))
.flags(*flags);
.flags(*flags | LineStripFlags::FLAG_COLOR_GRADIENT);
}
}

Expand Down
9 changes: 7 additions & 2 deletions crates/re_renderer/examples/multiview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fn build_lines(re_ctx: &mut RenderContext, seconds_since_startup: f32) -> LineDr
batch
.add_strip(lorenz_points.into_iter())
.color(Color32::from_rgb(255, 191, 0))
.flags(LineStripFlags::FLAG_COLOR_GRADIENT)
.radius(Size::new_points(1.0));

// Green Zig-Zag arrow
Expand All @@ -107,7 +108,11 @@ fn build_lines(re_ctx: &mut RenderContext, seconds_since_startup: f32) -> LineDr
)
.color(Color32::GREEN)
.radius(Size::new_scene(0.05))
.flags(LineStripFlags::CAP_END_TRIANGLE | LineStripFlags::CAP_START_ROUND);
.flags(
LineStripFlags::FLAG_COLOR_GRADIENT
| LineStripFlags::FLAG_CAP_END_TRIANGLE
| LineStripFlags::FLAG_CAP_START_ROUND,
);
}

// Blue spiral, rotating
Expand All @@ -125,7 +130,7 @@ fn build_lines(re_ctx: &mut RenderContext, seconds_since_startup: f32) -> LineDr
}))
.color(Color32::BLUE)
.radius(Size::new_scene(0.1))
.flags(LineStripFlags::CAP_END_TRIANGLE);
.flags(LineStripFlags::FLAG_CAP_END_TRIANGLE);

builder.to_draw_data(re_ctx).unwrap()
}
Expand Down
44 changes: 22 additions & 22 deletions crates/re_renderer/shader/lines.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ const LINE_STRIP_TEXTURE_SIZE: u32 = 256u;

// Flags
// See lines.rs#LineStripFlags
const CAP_END_TRIANGLE: u32 = 1u;
const CAP_END_ROUND: u32 = 2u;
const CAP_END_EXTEND_OUTWARDS: u32 = 4u;
const CAP_START_TRIANGLE: u32 = 8u;
const CAP_START_ROUND: u32 = 16u;
const CAP_START_EXTEND_OUTWARDS: u32 = 32u;
const NO_COLOR_GRADIENT: u32 = 64u;
const FORCE_ORTHO_SPANNING: u32 = 128u;
const FLAG_CAP_END_TRIANGLE: u32 = 1u;
const FLAG_CAP_END_ROUND: u32 = 2u;
const FLAG_CAP_END_EXTEND_OUTWARDS: u32 = 4u;
const FLAG_CAP_START_TRIANGLE: u32 = 8u;
const FLAG_CAP_START_ROUND: u32 = 16u;
const FLAG_CAP_START_EXTEND_OUTWARDS: u32 = 32u;
const FLAG_COLOR_GRADIENT: u32 = 64u;
const FLAG_FORCE_ORTHO_SPANNING: u32 = 128u;

// A lot of the attributes don't need to be interpolated across triangles.
// To document that and safe some time we mark them up with @interpolate(flat)
Expand Down Expand Up @@ -182,8 +182,8 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
// Compute quad_dir & correct center_position for triangle caps.
var quad_dir: Vec3;
var is_at_pointy_end = false;
let is_end_cap_triangle = is_cap_triangle && is_right_triangle && has_any_flag(strip_data.flags, CAP_END_TRIANGLE | CAP_END_ROUND);
let is_start_cap_triangle = is_cap_triangle && !is_right_triangle && has_any_flag(strip_data.flags, CAP_START_TRIANGLE | CAP_START_ROUND);
let is_end_cap_triangle = is_cap_triangle && is_right_triangle && has_any_flag(strip_data.flags, FLAG_CAP_END_TRIANGLE | FLAG_CAP_END_ROUND);
let is_start_cap_triangle = is_cap_triangle && !is_right_triangle && has_any_flag(strip_data.flags, FLAG_CAP_START_TRIANGLE | FLAG_CAP_START_ROUND);
if is_end_cap_triangle {
is_at_pointy_end = is_at_quad_end;
quad_dir = pos_data_quad_begin.pos - pos_data_quad_before.pos; // Go one pos data back.
Expand All @@ -201,7 +201,7 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
// Resolve radius.
// (slight inaccuracy: End caps are going to adjust their center_position)
var camera_ray: Ray;
if has_any_flag(strip_data.flags, FORCE_ORTHO_SPANNING) || is_camera_orthographic() {
if has_any_flag(strip_data.flags, FLAG_FORCE_ORTHO_SPANNING) || is_camera_orthographic() {
camera_ray = camera_ray_to_world_pos_orthographic(center_position);
} else {
camera_ray = camera_ray_to_world_pos_perspective(center_position);
Expand All @@ -210,18 +210,18 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
var strip_radius = unresolved_size_to_world(strip_data.unresolved_radius, camera_distance, frame.auto_size_lines);

// Make space for the end cap if this is either the cap itself or the cap follows right after/before this quad.
if !has_any_flag(strip_data.flags, CAP_END_EXTEND_OUTWARDS) &&
if !has_any_flag(strip_data.flags, FLAG_CAP_END_EXTEND_OUTWARDS) &&
(is_end_cap_triangle || (is_at_quad_end && pos_data_current.strip_index != pos_data_quad_after.strip_index)) {
var cap_length =
f32(has_any_flag(strip_data.flags, CAP_END_ROUND)) +
f32(has_any_flag(strip_data.flags, CAP_END_TRIANGLE)) * 4.0;
f32(has_any_flag(strip_data.flags, FLAG_CAP_END_ROUND)) +
f32(has_any_flag(strip_data.flags, FLAG_CAP_END_TRIANGLE)) * 4.0;
center_position -= quad_dir * (cap_length * strip_radius);
}
if !has_any_flag(strip_data.flags, CAP_START_EXTEND_OUTWARDS) &&
if !has_any_flag(strip_data.flags, FLAG_CAP_START_EXTEND_OUTWARDS) &&
(is_start_cap_triangle || (!is_at_quad_end && pos_data_current.strip_index != pos_data_quad_before.strip_index)) {
var cap_length =
f32(has_any_flag(strip_data.flags, CAP_START_ROUND)) +
f32(has_any_flag(strip_data.flags, CAP_START_TRIANGLE)) * 4.0;
f32(has_any_flag(strip_data.flags, FLAG_CAP_START_ROUND)) +
f32(has_any_flag(strip_data.flags, FLAG_CAP_START_TRIANGLE)) * 4.0;
center_position += quad_dir * (cap_length * strip_radius);
}

Expand All @@ -238,8 +238,8 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {

var active_radius = strip_radius;
// If this is a triangle cap, we blow up our ("virtual") quad by twice the size.
if (is_end_cap_triangle && has_any_flag(strip_data.flags, CAP_END_TRIANGLE)) ||
(is_start_cap_triangle && has_any_flag(strip_data.flags, CAP_START_TRIANGLE)) {
if (is_end_cap_triangle && has_any_flag(strip_data.flags, FLAG_CAP_END_TRIANGLE)) ||
(is_start_cap_triangle && has_any_flag(strip_data.flags, FLAG_CAP_START_TRIANGLE)) {
active_radius *= 2.0;
}

Expand Down Expand Up @@ -269,15 +269,15 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
out.color = strip_data.color;
out.active_radius = active_radius;
out.fragment_flags = strip_data.flags &
(NO_COLOR_GRADIENT | (u32(is_cap_triangle) * select(CAP_START_ROUND, CAP_END_ROUND, is_right_triangle)));
(FLAG_COLOR_GRADIENT | (u32(is_cap_triangle) * select(FLAG_CAP_START_ROUND, FLAG_CAP_END_ROUND, is_right_triangle)));
out.picking_instance_id = strip_data.picking_instance_id;

return out;
}

fn compute_coverage(in: VertexOut) -> f32 {
var coverage = 1.0;
if has_any_flag(in.fragment_flags, CAP_START_ROUND | CAP_END_ROUND) {
if has_any_flag(in.fragment_flags, FLAG_CAP_START_ROUND | FLAG_CAP_END_ROUND) {
let distance_to_skeleton = length(in.position_world - in.round_cap_circle_center);
let pixel_world_size = approx_pixel_world_size_at(length(in.position_world - frame.camera_position));

Expand All @@ -299,7 +299,7 @@ fn fs_main(in: VertexOut) -> @location(0) Vec4 {

// TODO(andreas): lighting setup
var shading = 1.0;
if !has_any_flag(in.fragment_flags, NO_COLOR_GRADIENT) { // TODO(andreas): Flip flag meaning.
if has_any_flag(in.fragment_flags, FLAG_COLOR_GRADIENT) {
let to_center = in.position_world - in.center_position;
let relative_distance_to_center_sq = dot(to_center, to_center) / (in.active_radius * in.active_radius);
shading = max(0.2, 1.0 - relative_distance_to_center_sq) * 0.9;
Expand Down
10 changes: 5 additions & 5 deletions crates/re_renderer/shader/point_cloud.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ var<uniform> batch: BatchUniformBuffer;

// Flags
// See point_cloud.rs#PointCloudBatchFlags
const ENABLE_SHADING: u32 = 1u;
const DRAW_AS_CIRCLES: u32 = 2u;
const FLAG_ENABLE_SHADING: u32 = 1u;
const FLAG_DRAW_AS_CIRCLES: u32 = 2u;

const TEXTURE_SIZE: u32 = 2048u;

Expand Down Expand Up @@ -97,7 +97,7 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {

// Span quad
let quad = sphere_or_circle_quad_span(vertex_idx, point_data.pos, point_data.unresolved_radius,
draw_data.radius_boost_in_ui_points, has_any_flag(batch.flags, DRAW_AS_CIRCLES));
draw_data.radius_boost_in_ui_points, has_any_flag(batch.flags, FLAG_DRAW_AS_CIRCLES));

// Output, transform to projection space and done.
var out: VertexOut;
Expand Down Expand Up @@ -126,7 +126,7 @@ fn circle_quad_coverage(world_position: Vec3, radius: f32, circle_center: Vec3)
}

fn coverage(world_position: Vec3, radius: f32, point_center: Vec3) -> f32 {
if is_camera_orthographic() || has_any_flag(batch.flags, DRAW_AS_CIRCLES) {
if is_camera_orthographic() || has_any_flag(batch.flags, FLAG_DRAW_AS_CIRCLES) {
return circle_quad_coverage(world_position, radius, point_center);
} else {
return sphere_quad_coverage(world_position, radius, point_center);
Expand All @@ -145,7 +145,7 @@ fn fs_main(in: VertexOut) -> @location(0) Vec4 {
// TODO(andreas): Proper shading
// TODO(andreas): This doesn't even use the sphere's world position for shading, the world position used here is flat!
var shading = 1.0;
if has_any_flag(batch.flags, ENABLE_SHADING) {
if has_any_flag(batch.flags, FLAG_ENABLE_SHADING) {
shading = max(0.4, sqrt(1.2 - distance(in.point_center, in.world_position) / in.radius)); // quick and dirty coloring
}
return vec4(in.color.rgb * shading, coverage);
Expand Down
19 changes: 9 additions & 10 deletions crates/re_renderer/src/line_strip_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@ impl LineStripSeriesBuilder {
}

pub fn default_box_flags() -> LineStripFlags {
LineStripFlags::CAP_END_ROUND
| LineStripFlags::CAP_START_ROUND
| LineStripFlags::NO_COLOR_GRADIENT
| LineStripFlags::CAP_END_EXTEND_OUTWARDS
| LineStripFlags::CAP_START_EXTEND_OUTWARDS
LineStripFlags::FLAG_CAP_END_ROUND
| LineStripFlags::FLAG_CAP_START_ROUND
| LineStripFlags::FLAG_CAP_END_EXTEND_OUTWARDS
| LineStripFlags::FLAG_CAP_START_EXTEND_OUTWARDS
}
}

Expand Down Expand Up @@ -306,14 +305,14 @@ impl<'a> LineBatchBuilder<'a> {
points: impl Iterator<Item = glam::Vec2>,
) -> LineStripBuilder<'_> {
self.add_strip(points.map(|p| p.extend(0.0)))
.flags(LineStripFlags::FORCE_ORTHO_SPANNING)
.flags(LineStripFlags::FLAG_FORCE_ORTHO_SPANNING)
}

/// Adds a single 2D line segment connecting two points. Uses autogenerated depth value.
#[inline]
pub fn add_segment_2d(&mut self, a: glam::Vec2, b: glam::Vec2) -> LineStripBuilder<'_> {
self.add_strip_2d([a, b].into_iter())
.flags(LineStripFlags::FORCE_ORTHO_SPANNING)
.flags(LineStripFlags::FLAG_FORCE_ORTHO_SPANNING)
}

/// Adds a series of unconnected 2D line segments.
Expand All @@ -325,7 +324,7 @@ impl<'a> LineBatchBuilder<'a> {
segments: impl Iterator<Item = (glam::Vec2, glam::Vec2)>,
) -> LineStripBuilder<'_> {
self.add_segments(segments.map(|(a, b)| (a.extend(0.0), b.extend(0.0))))
.flags(LineStripFlags::FORCE_ORTHO_SPANNING)
.flags(LineStripFlags::FLAG_FORCE_ORTHO_SPANNING)
}

/// Add 2D rectangle outlines.
Expand All @@ -344,7 +343,7 @@ impl<'a> LineBatchBuilder<'a> {
extent_u.extend(0.0),
extent_v.extend(0.0),
)
.flags(LineStripFlags::FORCE_ORTHO_SPANNING)
.flags(LineStripFlags::FLAG_FORCE_ORTHO_SPANNING)
}

/// Add 2D rectangle outlines with axis along X and Y.
Expand All @@ -362,7 +361,7 @@ impl<'a> LineBatchBuilder<'a> {
glam::Vec3::X * (max.x - min.x),
glam::Vec3::Y * (max.y - min.y),
)
.flags(LineStripFlags::FORCE_ORTHO_SPANNING)
.flags(LineStripFlags::FLAG_FORCE_ORTHO_SPANNING)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/src/point_cloud_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl PointCloudBuilder {
self.batches.push(PointCloudBatchInfo {
label: label.into(),
world_from_obj: glam::Affine3A::IDENTITY,
flags: PointCloudBatchFlags::ENABLE_SHADING,
flags: PointCloudBatchFlags::FLAG_ENABLE_SHADING,
point_count: 0,
overall_outline_mask_ids: OutlineMaskPreference::NONE,
additional_outline_mask_ids_vertex_ranges: Vec::new(),
Expand Down Expand Up @@ -244,7 +244,7 @@ impl<'a> PointCloudBatchBuilder<'a> {
colors,
picking_instance_ids,
)
.flags(PointCloudBatchFlags::DRAW_AS_CIRCLES)
.flags(PointCloudBatchFlags::FLAG_DRAW_AS_CIRCLES)
}

/// Adds (!) flags for this batch.
Expand Down
18 changes: 9 additions & 9 deletions crates/re_renderer/src/renderer/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,29 +207,29 @@ bitflags! {
#[derive(Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct LineStripFlags : u8 {
/// Puts a equilateral triangle at the end of the line strip (excludes other end caps).
const CAP_END_TRIANGLE = 0b0000_0001;
const FLAG_CAP_END_TRIANGLE = 0b0000_0001;

/// Adds a round cap at the end of a line strip (excludes other end caps).
const CAP_END_ROUND = 0b0000_0010;
const FLAG_CAP_END_ROUND = 0b0000_0010;

/// By default, line caps end at the last/first position of the the line strip.
/// This flag makes end caps extend outwards.
const CAP_END_EXTEND_OUTWARDS = 0b0000_0100;
const FLAG_CAP_END_EXTEND_OUTWARDS = 0b0000_0100;

/// Puts a equilateral triangle at the start of the line strip (excludes other start caps).
const CAP_START_TRIANGLE = 0b0000_1000;
const FLAG_CAP_START_TRIANGLE = 0b0000_1000;

/// Adds a round cap at the start of a line strip (excludes other start caps).
const CAP_START_ROUND = 0b0001_0000;
const FLAG_CAP_START_ROUND = 0b0001_0000;

/// By default, line caps end at the last/first position of the the line strip.
/// This flag makes end caps extend outwards.
const CAP_START_EXTEND_OUTWARDS = 0b0010_0000;
const FLAG_CAP_START_EXTEND_OUTWARDS = 0b0010_0000;

/// Disable color gradient which is on by default
/// Enable color gradient across the line.
///
/// TODO(andreas): Could be moved to per batch flags.
const NO_COLOR_GRADIENT = 0b0100_0000;
const FLAG_COLOR_GRADIENT = 0b0100_0000;

/// Forces spanning the line's quads as-if the camera was orthographic.
///
Expand All @@ -238,7 +238,7 @@ bitflags! {
/// Note that since distances to the camera are computed differently in orthographic mode, this changes how screen space sizes are computed.
///
/// TODO(andreas): Could be moved to per batch flags.
const FORCE_ORTHO_SPANNING = 0b1000_0000;
const FLAG_FORCE_ORTHO_SPANNING = 0b1000_0000;
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/src/renderer/point_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ bitflags! {
#[derive(Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct PointCloudBatchFlags : u32 {
/// If true, we shade all points in the batch like spheres.
const ENABLE_SHADING = 0b0001;
const FLAG_ENABLE_SHADING = 0b0001;

/// If true, draw 2D camera facing circles instead of spheres.
const DRAW_AS_CIRCLES = 0b0010;
const FLAG_DRAW_AS_CIRCLES = 0b0010;
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/re_viewer/src/ui/view_spatial/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl SceneSpatial {
.add_segment(*a, *b)
.radius(Size::AUTO)
.color(color)
.flags(re_renderer::renderer::LineStripFlags::FLAG_COLOR_GRADIENT)
// Select the entire object when clicking any of the lines.
.picking_instance_id(re_renderer::PickingLayerInstanceId(InstanceKey::SPLAT.0));
}
Expand Down
18 changes: 15 additions & 3 deletions crates/re_viewer/src/ui/view_spatial/scene/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ impl SceneSpatialPrimitives {
)
.radius(line_radius)
.color(AXIS_COLOR_X)
.flags(LineStripFlags::CAP_END_TRIANGLE | LineStripFlags::CAP_START_ROUND)
.flags(
LineStripFlags::FLAG_COLOR_GRADIENT
| LineStripFlags::FLAG_CAP_END_TRIANGLE
| LineStripFlags::FLAG_CAP_START_ROUND,
)
.picking_instance_id(picking_instance_id);
line_batch
.add_segment(
Expand All @@ -183,7 +187,11 @@ impl SceneSpatialPrimitives {
)
.radius(line_radius)
.color(AXIS_COLOR_Y)
.flags(LineStripFlags::CAP_END_TRIANGLE | LineStripFlags::CAP_START_ROUND)
.flags(
LineStripFlags::FLAG_COLOR_GRADIENT
| LineStripFlags::FLAG_CAP_END_TRIANGLE
| LineStripFlags::FLAG_CAP_START_ROUND,
)
.picking_instance_id(picking_instance_id);
line_batch
.add_segment(
Expand All @@ -192,7 +200,11 @@ impl SceneSpatialPrimitives {
)
.radius(line_radius)
.color(AXIS_COLOR_Z)
.flags(LineStripFlags::CAP_END_TRIANGLE | LineStripFlags::CAP_START_ROUND)
.flags(
LineStripFlags::FLAG_COLOR_GRADIENT
| LineStripFlags::FLAG_CAP_END_TRIANGLE
| LineStripFlags::FLAG_CAP_START_ROUND,
)
.picking_instance_id(picking_instance_id);
}
}

0 comments on commit a676bdb

Please sign in to comment.