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 cloud textures are now cached frame-to-frame #1913

Merged
merged 5 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 18 additions & 3 deletions crates/re_renderer/src/renderer/depth_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod gpu_data {
pub picking_layer_object_id: PickingLayerObjectId,

/// Multiplier to get world-space depth from whatever is in the texture.
pub world_depth_from_texture_value: f32,
pub world_depth_from_texture_depth: f32,

/// Point radius is calculated as world-space depth times this value.
pub point_radius_from_world_depth: f32,
Expand Down Expand Up @@ -88,7 +88,7 @@ mod gpu_data {
world_from_obj: (*world_from_obj).into(),
depth_camera_intrinsics: (*depth_camera_intrinsics).into(),
outline_mask_id: outline_mask_id.0.unwrap_or_default().into(),
world_depth_from_texture_value: *world_depth_from_texture_depth,
world_depth_from_texture_depth: *world_depth_from_texture_depth,
point_radius_from_world_depth: *point_radius_from_world_depth,
max_depth_in_world: *max_depth_in_world,
colormap: *colormap as u32,
Expand Down Expand Up @@ -189,11 +189,20 @@ impl DrawData for DepthCloudDrawData {
type Renderer = DepthCloudRenderer;
}

#[derive(thiserror::Error, Debug)]
pub enum DepthCloudDrawDataError {
#[error("Depth texture format was {0:?}, only F32 is supported")]
InvalidDepthTextureFormat(wgpu::TextureFormat),

#[error(transparent)]
ResourceManagerError(#[from] ResourceManagerError),
}

impl DepthCloudDrawData {
pub fn new(
ctx: &mut RenderContext,
depth_clouds: &DepthClouds,
) -> Result<Self, ResourceManagerError> {
) -> Result<Self, DepthCloudDrawDataError> {
crate::profile_function!();

let DepthClouds {
Expand Down Expand Up @@ -242,6 +251,12 @@ impl DepthCloudDrawData {
depth_cloud_ubo_binding_outlines,
depth_cloud_ubo_binding_opaque
) {
if depth_cloud.depth_texture.format() != wgpu::TextureFormat::R32Float {
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
return Err(DepthCloudDrawDataError::InvalidDepthTextureFormat(
depth_cloud.depth_texture.format(),
));
}

let mk_bind_group = |label, ubo: BindGroupEntry| {
ctx.gpu_resources.bind_groups.alloc(
&ctx.device,
Expand Down
19 changes: 8 additions & 11 deletions crates/re_viewer/src/ui/view_spatial/scene/scene_part/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,14 @@ impl ImagesPart {
return Err(format!("Couldn't fetch pinhole extrinsics at {pinhole_ent_path:?}"));
};

let (height, width) = (tensor.shape()[0].size, tensor.shape()[1].size);
let Some([height, width, _]) = tensor.image_height_width_channels() else {
return Err(format!("Tensor at {ent_path:?} is not an image"));
};
let dimensions = glam::UVec2::new(width as _, height as _);

// Ideally, we'd use the same key as for displaying the texture, but we might make other compromises regarding formats etc.!
// So to not couple this, we use a different key here
let depth_texture = {
// Ideally, we'd use the same key as for displaying the texture, but we might make other compromises regarding formats etc.!
// So to not couple this, we use a different key here
let texture_key = egui::util::hash((tensor.id(), "depth_cloud"));
let mut data_f32 = Vec::new();
ctx.render_ctx.texture_manager_2d.get_or_create_with(
Expand All @@ -294,9 +296,7 @@ impl ImagesPart {
// However, R16Unorm is behind a feature flag and Depth16Unorm doesn't work on WebGL (and is awkward as this is a depth buffer format!).
let data = match &tensor.data {
TensorData::U16(data) => {
data_f32.extend(
data.as_slice().iter().map(|d| *d as f32 / u16::MAX as f32),
);
data_f32.extend(data.as_slice().iter().map(|d| *d as f32));
bytemuck::cast_slice(&data_f32).into()
}
TensorData::F32(data) => bytemuck::cast_slice(data).into(),
Expand All @@ -321,10 +321,7 @@ impl ImagesPart {

let depth_from_world_scale = *properties.depth_from_world_scale.get();

let world_depth_from_data_depth = match &tensor.data {
TensorData::U16(_) => 65535.0,
_ => 1.0,
} / depth_from_world_scale;
let world_depth_from_texture_depth = 1.0 / depth_from_world_scale;

let colormap = match *properties.color_mapper.get() {
re_data_store::ColorMapper::Colormap(colormap) => match colormap {
Expand Down Expand Up @@ -360,7 +357,7 @@ impl ImagesPart {
scene.primitives.depth_clouds.clouds.push(DepthCloud {
world_from_obj,
depth_camera_intrinsics: intrinsics.image_from_cam.into(),
world_depth_from_texture_depth: world_depth_from_data_depth,
world_depth_from_texture_depth,
point_radius_from_world_depth,
max_depth_in_world: max_data_value / depth_from_world_scale,
depth_dimensions: dimensions,
Expand Down