Skip to content

Commit

Permalink
Fix error when logging segmentation image & annotation context on the…
Browse files Browse the repository at this point in the history
… same entity at once
  • Loading branch information
Wumpf committed May 28, 2024
1 parent 183ecf4 commit ca2338b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 50 deletions.
4 changes: 3 additions & 1 deletion crates/re_space_view_spatial/src/visualizers/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,14 @@ impl ImageVisualizer {
let tensor_stats = ctx
.cache
.entry(|c: &mut TensorStatsCache| c.entry(tensor_data_row_id, tensor));
let depth_texture = re_viewer_context::gpu_bridge::depth_tensor_to_gpu(
let depth_texture = re_viewer_context::gpu_bridge::tensor_to_gpu(
ctx.render_ctx,
&debug_name,
tensor_data_row_id,
tensor,
TensorDataMeaning::Depth,
&tensor_stats,
&ent_context.annotations,
)?;

let depth_from_world_scale = *properties.depth_from_world_scale;
Expand Down
5 changes: 1 addition & 4 deletions crates/re_viewer_context/src/gpu_bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ mod tensor_to_gpu;

pub use colormap::colormap_dropdown_button_ui;
pub use re_renderer_callback::new_renderer_callback;
pub use tensor_to_gpu::{
class_id_tensor_to_gpu, color_tensor_to_gpu, depth_tensor_to_gpu, tensor_to_gpu,
texture_height_width_channels,
};
pub use tensor_to_gpu::{tensor_to_gpu, texture_height_width_channels};

use crate::TensorStats;

Expand Down
107 changes: 62 additions & 45 deletions crates/re_viewer_context/src/gpu_bridge/tensor_to_gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ use super::{get_or_create_texture, try_get_or_create_texture};

// ----------------------------------------------------------------------------

enum TextureKeyUsage {
AnnotationContextColormap,
TensorData(TensorDataMeaning),
}

/// Returns a texture key for a given row id & usage.
///
/// Several textures may be created from the same row.
/// This makes sure that they all get different keys!
fn generate_texture_key(row_id: RowId, usage: TextureKeyUsage) -> u64 {
hash(row_id)
^ hash(match usage {
TextureKeyUsage::TensorData(meaning) => match meaning {
TensorDataMeaning::Unknown => 0x12345678,
TensorDataMeaning::ClassId => 0x23456789,
TensorDataMeaning::Depth => 0x34567890,
},
TextureKeyUsage::AnnotationContextColormap => 0x45678901,
})
}

/// Set up tensor for rendering on the GPU.
///
/// This will only upload the tensor if it isn't on the GPU already.
Expand All @@ -49,43 +70,37 @@ pub fn tensor_to_gpu(
tensor.shape()
));

let texture_key =
generate_texture_key(tensor_data_row_id, TextureKeyUsage::TensorData(meaning));

match meaning {
TensorDataMeaning::Unknown => color_tensor_to_gpu(
render_ctx,
debug_name,
tensor_data_row_id,
tensor,
tensor_stats,
),
TensorDataMeaning::Unknown => {
color_tensor_to_gpu(render_ctx, debug_name, texture_key, tensor, tensor_stats)
}
TensorDataMeaning::ClassId => class_id_tensor_to_gpu(
render_ctx,
debug_name,
tensor_data_row_id,
texture_key,
tensor,
tensor_stats,
annotations,
),
TensorDataMeaning::Depth => depth_tensor_to_gpu(
render_ctx,
debug_name,
tensor_data_row_id,
tensor,
tensor_stats,
),
TensorDataMeaning::Depth => {
depth_tensor_to_gpu(render_ctx, debug_name, texture_key, tensor, tensor_stats)
}
}
}

// ----------------------------------------------------------------------------
// Color textures:

pub fn color_tensor_to_gpu(
fn color_tensor_to_gpu(
render_ctx: &RenderContext,
debug_name: &str,
tensor_data_row_id: RowId,
texture_key: u64,
tensor: &DecodedTensor,
tensor_stats: &TensorStats,
) -> anyhow::Result<ColormappedTexture> {
let texture_key = hash(tensor_data_row_id);
let [height, width, depth] = texture_height_width_channels(tensor)?;

let texture_handle = try_get_or_create_texture(render_ctx, texture_key, || {
Expand Down Expand Up @@ -197,16 +212,20 @@ pub fn color_tensor_to_gpu(
// ----------------------------------------------------------------------------
// Textures with class_id annotations:

pub fn class_id_tensor_to_gpu(
fn class_id_tensor_to_gpu(
render_ctx: &RenderContext,
debug_name: &str,
tensor_data_row_id: RowId,
texture_key: u64,
tensor: &DecodedTensor,
tensor_stats: &TensorStats,
annotations: &Annotations,
) -> anyhow::Result<ColormappedTexture> {
re_tracing::profile_function!();
let texture_key = hash(tensor_data_row_id);

let colormap_key = generate_texture_key(
annotations.row_id(),
TextureKeyUsage::AnnotationContextColormap,
);

let [_height, _width, depth] = texture_height_width_channels(tensor)?;
anyhow::ensure!(
Expand All @@ -229,27 +248,26 @@ pub fn class_id_tensor_to_gpu(
let colormap_width = 256;
let colormap_height = (num_colors + colormap_width - 1) / colormap_width;

let colormap_texture_handle =
get_or_create_texture(render_ctx, hash(annotations.row_id()), || {
let data: Vec<u8> = (0..(colormap_width * colormap_height))
.flat_map(|id| {
let color = annotations
.resolved_class_description(Some(ClassId::from(id as u16)))
.annotation_info()
.color(None, DefaultColor::TransparentBlack);
color.to_array() // premultiplied!
})
.collect();

Texture2DCreationDesc {
label: "class_id_colormap".into(),
data: data.into(),
format: TextureFormat::Rgba8UnormSrgb,
width: colormap_width as u32,
height: colormap_height as u32,
}
})
.context("Failed to create class_id_colormap.")?;
let colormap_texture_handle = get_or_create_texture(render_ctx, colormap_key, || {
let data: Vec<u8> = (0..(colormap_width * colormap_height))
.flat_map(|id| {
let color = annotations
.resolved_class_description(Some(ClassId::from(id as u16)))
.annotation_info()
.color(None, DefaultColor::TransparentBlack);
color.to_array() // premultiplied!
})
.collect();

Texture2DCreationDesc {
label: "class_id_colormap".into(),
data: data.into(),
format: TextureFormat::Rgba8UnormSrgb,
width: colormap_width as u32,
height: colormap_height as u32,
}
})
.context("Failed to create class_id_colormap.")?;

let main_texture_handle = try_get_or_create_texture(render_ctx, texture_key, || {
general_texture_creation_desc_from_tensor(debug_name, tensor)
Expand All @@ -270,15 +288,14 @@ pub fn class_id_tensor_to_gpu(
// ----------------------------------------------------------------------------
// Depth textures:

pub fn depth_tensor_to_gpu(
fn depth_tensor_to_gpu(
render_ctx: &RenderContext,
debug_name: &str,
tensor_data_row_id: RowId,
texture_key: u64,
tensor: &DecodedTensor,
tensor_stats: &TensorStats,
) -> anyhow::Result<ColormappedTexture> {
re_tracing::profile_function!();
let texture_key = hash(tensor_data_row_id);

let [_height, _width, depth] = texture_height_width_channels(tensor)?;
anyhow::ensure!(
Expand Down

0 comments on commit ca2338b

Please sign in to comment.