Skip to content

Commit

Permalink
Use GPU picking for line(like) primitives, fix interactive flags (#…
Browse files Browse the repository at this point in the history
…1829)

* line strip builder no longer has user data, exposes picking id instead (not implemented yet)
* handle interactive object property when evaluating picking code
* take line strip builder directly when building up line draw data
* finish implementing picking for lines
* remove unused iter_strips_with_vertices
* Simplify picking handling now that there are a lot less types. Labels & textured rects are always picked now, fixes #1021
  • Loading branch information
Wumpf committed Apr 13, 2023
1 parent 9716235 commit 780fe1c
Show file tree
Hide file tree
Showing 27 changed files with 635 additions and 788 deletions.
4 changes: 2 additions & 2 deletions crates/re_renderer/examples/2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl framework::Example for Render2D {
splits[0].resolution_in_pixel[1] as f32,
);

let mut line_strip_builder = LineStripSeriesBuilder::<()>::new(re_ctx);
let mut line_strip_builder = LineStripSeriesBuilder::new(re_ctx);

// Blue rect outline around the bottom right quarter.
{
Expand Down Expand Up @@ -182,7 +182,7 @@ impl framework::Example for Render2D {
}
}

let line_strip_draw_data = line_strip_builder.to_draw_data(re_ctx);
let line_strip_draw_data = line_strip_builder.to_draw_data(re_ctx).unwrap();
let point_draw_data = point_cloud_builder.to_draw_data(re_ctx).unwrap();

let image_scale = 4.0;
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/examples/depth_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl framework::Example for RenderDepthClouds {
let world_from_model = rotation * translation_center * scale;

let frame_draw_data = {
let mut builder = LineStripSeriesBuilder::<()>::new(re_ctx);
let mut builder = LineStripSeriesBuilder::new(re_ctx);
{
let mut line_batch = builder.batch("frame").world_from_obj(world_from_model);
line_batch.add_box_outline(glam::Affine3A::from_scale_rotation_translation(
Expand All @@ -321,7 +321,7 @@ impl framework::Example for RenderDepthClouds {
glam::Vec3::ONE * 0.5,
));
}
builder.to_draw_data(re_ctx)
builder.to_draw_data(re_ctx).unwrap()
};

let image_draw_data = RectangleDrawData::new(
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/examples/multiview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn build_lines(re_ctx: &mut RenderContext, seconds_since_startup: f32) -> LineDr
// Calculate some points that look nice for an animated line.
let lorenz_points = lorenz_points(seconds_since_startup);

let mut builder = LineStripSeriesBuilder::<()>::new(re_ctx);
let mut builder = LineStripSeriesBuilder::new(re_ctx);
{
let mut batch = builder.batch("lines without transform");

Expand Down Expand Up @@ -125,7 +125,7 @@ fn build_lines(re_ctx: &mut RenderContext, seconds_since_startup: f32) -> LineDr
.radius(Size::new_scene(0.1))
.flags(LineStripFlags::CAP_END_TRIANGLE);

builder.to_draw_data(re_ctx)
builder.to_draw_data(re_ctx).unwrap()
}

enum CameraControl {
Expand Down
22 changes: 16 additions & 6 deletions crates/re_renderer/shader/lines.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
var line_strip_texture: texture_2d<f32>;
@group(1) @binding(1)
var position_data_texture: texture_2d<u32>;
@group(1) @binding(2)
var picking_instance_id_texture: texture_2d<u32>;

struct DrawDataUniformBuffer {
radius_boost_in_ui_points: f32,
Expand All @@ -19,21 +21,22 @@ struct DrawDataUniformBuffer {
// if we wouldn't add padding here, which isn't available on WebGL.
_padding: Vec4,
};
@group(1) @binding(2)
@group(1) @binding(3)
var<uniform> draw_data: DrawDataUniformBuffer;

struct BatchUniformBuffer {
world_from_obj: Mat4,
outline_mask_ids: UVec2,
picking_layer_object_id: UVec2,
};
@group(2) @binding(0)
var<uniform> batch: BatchUniformBuffer;


// textureLoad needs i32 right now, so we use that with all sizes & indices to avoid casts
// https://github.com/gfx-rs/naga/issues/1997
const LINESTRIP_TEXTURE_SIZE: i32 = 512;
const POSITION_DATA_TEXTURE_SIZE: i32 = 256;
const POSITION_TEXTURE_SIZE: i32 = 512;
const LINE_STRIP_TEXTURE_SIZE: i32 = 256;

// Flags
// See lines.rs#LineStripFlags
Expand Down Expand Up @@ -69,20 +72,25 @@ struct VertexOut {

@location(5) @interpolate(flat)
fragment_flags: u32,

@location(6) @interpolate(flat)
picking_instance_id: UVec2,
};

struct LineStripData {
color: Vec4,
unresolved_radius: f32,
stippling: f32,
flags: u32,
picking_instance_id: UVec2,
}

// Read and unpack line strip data at a given location
fn read_strip_data(idx: u32) -> LineStripData {
// can be u32 once https://github.com/gfx-rs/naga/issues/1997 is solved
let idx = i32(idx);
var raw_data = textureLoad(position_data_texture, IVec2(idx % POSITION_DATA_TEXTURE_SIZE, idx / POSITION_DATA_TEXTURE_SIZE), 0).xy;
let coord = IVec2(idx % LINE_STRIP_TEXTURE_SIZE, idx / LINE_STRIP_TEXTURE_SIZE);
var raw_data = textureLoad(position_data_texture, coord, 0).xy;

var data: LineStripData;
data.color = linear_from_srgba(unpack4x8unorm_workaround(raw_data.x));
Expand All @@ -91,6 +99,7 @@ fn read_strip_data(idx: u32) -> LineStripData {
data.unresolved_radius = unpack2x16float(raw_data.y).y;
data.flags = ((raw_data.y >> 8u) & 0xFFu);
data.stippling = f32((raw_data.y >> 16u) & 0xFFu) * (1.0 / 255.0);
data.picking_instance_id = textureLoad(picking_instance_id_texture, coord, 0).rg;
return data;
}

Expand All @@ -103,7 +112,7 @@ struct PositionData {
fn read_position_data(idx: u32) -> PositionData {
// can be u32 once https://github.com/gfx-rs/naga/issues/1997 is solved
let idx = i32(idx);
var raw_data = textureLoad(line_strip_texture, IVec2(idx % LINESTRIP_TEXTURE_SIZE, idx / LINESTRIP_TEXTURE_SIZE), 0);
var raw_data = textureLoad(line_strip_texture, IVec2(idx % POSITION_TEXTURE_SIZE, idx / POSITION_TEXTURE_SIZE), 0);

var data: PositionData;
let pos_4d = batch.world_from_obj * Vec4(raw_data.xyz, 1.0);
Expand Down Expand Up @@ -262,6 +271,7 @@ fn vs_main(@builtin(vertex_index) vertex_idx: u32) -> VertexOut {
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)));
out.picking_instance_id = strip_data.picking_instance_id;

return out;
}
Expand Down Expand Up @@ -305,7 +315,7 @@ fn fs_main_picking_layer(in: VertexOut) -> @location(0) UVec4 {
if coverage < 0.5 {
discard;
}
return UVec4(0u, 0u, 0u, 0u); // TODO(andreas): Implement picking layer id pass-through.
return UVec4(batch.picking_layer_object_id, in.picking_instance_id);
}

@fragment
Expand Down
Loading

1 comment on commit 780fe1c

@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: 780fe1c Previous: 9716235 Ratio
datastore/num_rows=1000/num_instances=1000/packed=false/insert/default 2873030 ns/iter (± 129853) 2919746 ns/iter (± 117627) 0.98
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at/default 370 ns/iter (± 1) 371 ns/iter (± 2) 1.00
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/primary/default 261 ns/iter (± 0) 264 ns/iter (± 0) 0.99
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/secondaries/default 421 ns/iter (± 0) 421 ns/iter (± 0) 1
datastore/num_rows=1000/num_instances=1000/packed=false/range/default 2985175 ns/iter (± 46656) 3021808 ns/iter (± 77617) 0.99
datastore/num_rows=1000/num_instances=1000/gc/default 2370960 ns/iter (± 2279) 2372352 ns/iter (± 2752) 1.00
mono_points_arrow/generate_message_bundles 29060146 ns/iter (± 1353827) 30048583 ns/iter (± 860350) 0.97
mono_points_arrow/generate_messages 125684479 ns/iter (± 989448) 126572200 ns/iter (± 1053707) 0.99
mono_points_arrow/encode_log_msg 158910380 ns/iter (± 1953197) 157797886 ns/iter (± 2029752) 1.01
mono_points_arrow/encode_total 311555849 ns/iter (± 2255312) 313926959 ns/iter (± 2267498) 0.99
mono_points_arrow/decode_log_msg 189383114 ns/iter (± 974764) 190531535 ns/iter (± 836099) 0.99
mono_points_arrow/decode_message_bundles 72010385 ns/iter (± 1145748) 69927957 ns/iter (± 740183) 1.03
mono_points_arrow/decode_total 260372740 ns/iter (± 1636211) 258381263 ns/iter (± 2079058) 1.01
mono_points_arrow_batched/generate_message_bundles 19945855 ns/iter (± 1456346) 21567474 ns/iter (± 1560597) 0.92
mono_points_arrow_batched/generate_messages 4184226 ns/iter (± 188952) 4384761 ns/iter (± 243680) 0.95
mono_points_arrow_batched/encode_log_msg 1371697 ns/iter (± 6709) 1353438 ns/iter (± 7106) 1.01
mono_points_arrow_batched/encode_total 30228629 ns/iter (± 1816364) 29874763 ns/iter (± 1552578) 1.01
mono_points_arrow_batched/decode_log_msg 789668 ns/iter (± 4030) 783985 ns/iter (± 3504) 1.01
mono_points_arrow_batched/decode_message_bundles 7854339 ns/iter (± 214103) 7644076 ns/iter (± 200293) 1.03
mono_points_arrow_batched/decode_total 9190422 ns/iter (± 543822) 8612627 ns/iter (± 304594) 1.07
batch_points_arrow/generate_message_bundles 239208 ns/iter (± 408) 239275 ns/iter (± 594) 1.00
batch_points_arrow/generate_messages 5027 ns/iter (± 20) 5026 ns/iter (± 17) 1.00
batch_points_arrow/encode_log_msg 260553 ns/iter (± 1751) 262049 ns/iter (± 1974) 0.99
batch_points_arrow/encode_total 537133 ns/iter (± 10165) 529482 ns/iter (± 2881) 1.01
batch_points_arrow/decode_log_msg 212877 ns/iter (± 841) 212874 ns/iter (± 726) 1.00
batch_points_arrow/decode_message_bundles 1846 ns/iter (± 5) 1853 ns/iter (± 6) 1.00
batch_points_arrow/decode_total 219772 ns/iter (± 1257) 218081 ns/iter (± 677) 1.01
arrow_mono_points/insert 2565090747 ns/iter (± 6698397) 2543943526 ns/iter (± 5962253) 1.01
arrow_mono_points/query 1207419 ns/iter (± 10432) 1189291 ns/iter (± 13750) 1.02
arrow_batch_points/insert 1154077 ns/iter (± 9598) 1160594 ns/iter (± 4770) 0.99
arrow_batch_points/query 14465 ns/iter (± 87) 14751 ns/iter (± 57) 0.98
arrow_batch_vecs/insert 26432 ns/iter (± 65) 26293 ns/iter (± 49) 1.01
arrow_batch_vecs/query 325671 ns/iter (± 405) 325169 ns/iter (± 418) 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.