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

Refactor: remove GpuTexture2DHandle::invalid #1866

Merged
merged 5 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 13 additions & 9 deletions crates/re_renderer/examples/2d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ecolor::Hsva;
use re_renderer::{
renderer::{
ColormappedTexture, LineStripFlags, RectangleDrawData, TextureFilterMag, TextureFilterMin,
TexturedRect,
ColormappedTexture, LineStripFlags, RectangleDrawData, RectangleOptions, TextureFilterMag,
TextureFilterMin, TexturedRect,
},
resource_managers::{GpuTexture2DHandle, Texture2DCreationDesc},
view_builder::{self, Projection, TargetConfiguration, ViewBuilder},
Expand Down Expand Up @@ -197,9 +197,11 @@ impl framework::Example for Render2D {
colormapped_texture: ColormappedTexture::from_unorm_srgba(
self.rerun_logo_texture.clone(),
),
texture_filter_magnification: TextureFilterMag::Nearest,
texture_filter_minification: TextureFilterMin::Linear,
..Default::default()
options: RectangleOptions {
texture_filter_magnification: TextureFilterMag::Nearest,
texture_filter_minification: TextureFilterMin::Linear,
..Default::default()
},
},
TexturedRect {
top_left_corner_position: glam::vec3(
Expand All @@ -213,10 +215,12 @@ impl framework::Example for Render2D {
colormapped_texture: ColormappedTexture::from_unorm_srgba(
self.rerun_logo_texture.clone(),
),
texture_filter_magnification: TextureFilterMag::Linear,
texture_filter_minification: TextureFilterMin::Linear,
depth_offset: 1,
..Default::default()
options: RectangleOptions {
texture_filter_magnification: TextureFilterMag::Linear,
texture_filter_minification: TextureFilterMin::Linear,
depth_offset: 1,
..Default::default()
},
},
],
)
Expand Down
14 changes: 8 additions & 6 deletions crates/re_renderer/examples/depth_cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use macaw::IsoTransform;
use re_renderer::{
renderer::{
ColormappedTexture, DepthCloud, DepthCloudDepthData, DepthCloudDrawData, DepthClouds,
DrawData, GenericSkyboxDrawData, RectangleDrawData, TexturedRect,
DrawData, GenericSkyboxDrawData, RectangleDrawData, RectangleOptions, TexturedRect,
},
resource_managers::{GpuTexture2DHandle, Texture2DCreationDesc},
view_builder::{self, Projection, ViewBuilder},
Expand Down Expand Up @@ -327,11 +327,13 @@ impl framework::Example for RenderDepthClouds {
extent_u: world_from_model.transform_vector3(-glam::Vec3::X),
extent_v: world_from_model.transform_vector3(-glam::Vec3::Y),
colormapped_texture: ColormappedTexture::from_unorm_srgba(albedo_handle.clone()),
texture_filter_magnification: re_renderer::renderer::TextureFilterMag::Nearest,
texture_filter_minification: re_renderer::renderer::TextureFilterMin::Linear,
multiplicative_tint: Rgba::from_white_alpha(0.5),
depth_offset: -1,
..Default::default()
options: RectangleOptions {
texture_filter_magnification: re_renderer::renderer::TextureFilterMag::Nearest,
texture_filter_minification: re_renderer::renderer::TextureFilterMin::Linear,
multiplicative_tint: Rgba::from_white_alpha(0.5),
depth_offset: -1,
..Default::default()
},
}],
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/re_renderer/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl GpuMesh {
.iter()
.zip(uniform_buffer_bindings.into_iter())
{
let texture = ctx.texture_manager_2d.get(&material.albedo)?;
let texture = ctx.texture_manager_2d.get(&material.albedo);
let bind_group = pools.bind_groups.alloc(
device,
pools,
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub use test_triangle::TestTriangleDrawData;

mod rectangles;
pub use rectangles::{
ColorMapper, ColormappedTexture, RectangleDrawData, TextureFilterMag, TextureFilterMin,
TexturedRect,
ColorMapper, ColormappedTexture, RectangleDrawData, RectangleOptions, TextureFilterMag,
TextureFilterMin, TexturedRect,
};

mod mesh_renderer;
Expand Down
69 changes: 37 additions & 32 deletions crates/re_renderer/src/renderer/rectangles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,6 @@ pub enum ColorMapper {
Texture(GpuTexture2DHandle),
}

impl Default for ColormappedTexture {
fn default() -> Self {
Self {
texture: GpuTexture2DHandle::invalid(),
range: [0.0, 1.0],
gamma: 1.0,
color_mapper: None,
}
}
}

impl ColormappedTexture {
pub fn from_unorm_srgba(texture: GpuTexture2DHandle) -> Self {
Self {
Expand All @@ -120,6 +109,10 @@ pub struct TexturedRect {
/// Texture that fills the rectangle
pub colormapped_texture: ColormappedTexture,

pub options: RectangleOptions,
}

pub struct RectangleOptions {
pub texture_filter_magnification: TextureFilterMag,
pub texture_filter_minification: TextureFilterMin,

Expand All @@ -132,13 +125,9 @@ pub struct TexturedRect {
pub outline_mask: OutlineMaskPreference,
}

impl Default for TexturedRect {
impl Default for RectangleOptions {
fn default() -> Self {
Self {
top_left_corner_position: glam::Vec3::ZERO,
extent_u: glam::Vec3::ZERO,
extent_v: glam::Vec3::ZERO,
colormapped_texture: Default::default(),
texture_filter_magnification: TextureFilterMag::Nearest,
texture_filter_minification: TextureFilterMin::Linear,
multiplicative_tint: Rgba::WHITE,
Expand Down Expand Up @@ -177,7 +166,7 @@ pub enum RectangleError {
mod gpu_data {
use crate::wgpu_buffer_types;

use super::{ColorMapper, RectangleError};
use super::{ColorMapper, RectangleError, TexturedRect};

// Keep in sync with mirror in rectangle.wgsl

Expand Down Expand Up @@ -225,12 +214,28 @@ mod gpu_data {
) -> Result<Self, RectangleError> {
let texture_info = texture_format.describe();

let TexturedRect {
top_left_corner_position,
extent_u,
extent_v,
colormapped_texture,
options,
} = rectangle;

let super::ColormappedTexture {
texture: _,
range,
gamma,
color_mapper,
} = &rectangle.colormapped_texture;
} = colormapped_texture;

let super::RectangleOptions {
texture_filter_magnification: _,
texture_filter_minification: _,
multiplicative_tint,
depth_offset,
outline_mask,
} = options;

let sample_type = match texture_info.sample_type {
wgpu::TextureSampleType::Float { .. } => {
Expand Down Expand Up @@ -276,14 +281,14 @@ mod gpu_data {
}

Ok(Self {
top_left_corner_position: rectangle.top_left_corner_position.into(),
top_left_corner_position: (*top_left_corner_position).into(),
colormap_function,
extent_u: rectangle.extent_u.into(),
extent_u: (*extent_u).into(),
sample_type,
extent_v: rectangle.extent_v.into(),
depth_offset: rectangle.depth_offset as f32,
multiplicative_tint: rectangle.multiplicative_tint,
outline_mask: rectangle.outline_mask.0.unwrap_or_default().into(),
extent_v: (*extent_v).into(),
depth_offset: *depth_offset as f32,
multiplicative_tint: *multiplicative_tint,
outline_mask: outline_mask.0.unwrap_or_default().into(),
range_min_max: (*range).into(),
color_mapper: color_mapper_int,
gamma: *gamma,
Expand Down Expand Up @@ -330,15 +335,15 @@ impl RectangleDrawData {
});
}

// TODO(emilk): continue on error (skipping just that rectangle)?
let textures: Vec<_> = rectangles
.iter()
.map(|rectangle| {
ctx.texture_manager_2d
.get(&rectangle.colormapped_texture.texture)
})
.try_collect()?;
.collect();

// TODO(emilk): continue on error (skipping just that rectangle)?
let uniform_buffers: Vec<_> = izip!(rectangles, &textures)
.map(|(rect, texture)| {
gpu_data::UniformBuffer::from_textured_rect(rect, &texture.creation_desc.format)
Expand All @@ -355,20 +360,20 @@ impl RectangleDrawData {
for (rectangle, uniform_buffer, texture) in
izip!(rectangles, uniform_buffer_bindings, textures)
{
let options = &rectangle.options;
let sampler = ctx.gpu_resources.samplers.get_or_create(
&ctx.device,
&SamplerDesc {
label: format!(
"rectangle sampler mag {:?} min {:?}",
rectangle.texture_filter_magnification,
rectangle.texture_filter_minification
options.texture_filter_magnification, options.texture_filter_minification
)
.into(),
mag_filter: match rectangle.texture_filter_magnification {
mag_filter: match options.texture_filter_magnification {
TextureFilterMag::Linear => wgpu::FilterMode::Linear,
TextureFilterMag::Nearest => wgpu::FilterMode::Nearest,
},
min_filter: match rectangle.texture_filter_minification {
min_filter: match options.texture_filter_minification {
TextureFilterMin::Linear => wgpu::FilterMode::Linear,
TextureFilterMin::Nearest => wgpu::FilterMode::Nearest,
},
Expand Down Expand Up @@ -414,7 +419,7 @@ impl RectangleDrawData {
let colormap_texture = if let Some(ColorMapper::Texture(handle)) =
&rectangle.colormapped_texture.color_mapper
{
let colormap_texture = ctx.texture_manager_2d.get(handle)?;
let colormap_texture = ctx.texture_manager_2d.get(handle);
if colormap_texture.creation_desc.format != wgpu::TextureFormat::Rgba8UnormSrgb {
return Err(RectangleError::UnsupportedColormapTextureFormat(
colormap_texture.creation_desc.format,
Expand Down Expand Up @@ -443,7 +448,7 @@ impl RectangleDrawData {
layout: rectangle_renderer.bind_group_layout,
},
),
draw_outline_mask: rectangle.outline_mask.is_some(),
draw_outline_mask: rectangle.options.outline_mask.is_some(),
});
}

Expand Down
40 changes: 15 additions & 25 deletions crates/re_renderer/src/resource_managers/texture_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,23 @@ use crate::{
DebugLabel,
};

use super::ResourceManagerError;

/// Handle to a 2D resource.
///
/// Currently, this is solely a more strongly typed regular gpu texture handle.
/// Since all textures have "long lived" behavior (no temp allocation, alive until unused),
/// there is no difference as with buffer reliant data like meshes or most contents of draw-data.
#[derive(Clone)]
pub struct GpuTexture2DHandle(Option<GpuTexture>);
pub struct GpuTexture2DHandle(GpuTexture);

impl GpuTexture2DHandle {
pub fn invalid() -> Self {
Self(None)
}

/// Width of the texture, defaults to 1 if invalid since fallback textures are typically one pixel.
/// Width of the texture.
pub fn width(&self) -> u32 {
self.0.as_ref().map_or(1, |t| t.texture.width())
self.0.texture.width()
}

/// Height of the texture, defaults to 1 if invalid since fallback textures are typically one pixel.
/// Height of the texture.
pub fn height(&self) -> u32 {
self.0.as_ref().map_or(1, |t| t.texture.height())
self.0.texture.height()
}
}

Expand Down Expand Up @@ -213,27 +207,27 @@ impl TextureManager2D {

/// Returns a single pixel white pixel with an rgba8unorm format.
pub fn white_texture_unorm(&self) -> &GpuTexture {
self.white_texture_unorm.0.as_ref().unwrap()
&self.white_texture_unorm.0
}

/// Returns a single zero pixel with format [`wgpu::TextureFormat::Rgba8Unorm`].
pub fn zeroed_texture_float(&self) -> &GpuTexture {
self.zeroed_texture_float.0.as_ref().unwrap()
&self.zeroed_texture_float.0
}

/// Returns a single zero pixel with format [`wgpu::TextureFormat::Depth16Unorm`].
pub fn zeroed_texture_depth(&self) -> &GpuTexture {
self.zeroed_texture_depth.0.as_ref().unwrap()
&self.zeroed_texture_depth.0
}

/// Returns a single zero pixel with format [`wgpu::TextureFormat::Rgba8Sint`].
pub fn zeroed_texture_sint(&self) -> &GpuTexture {
self.zeroed_texture_sint.0.as_ref().unwrap()
&self.zeroed_texture_sint.0
}

/// Returns a single zero pixel with format [`wgpu::TextureFormat::Rgba8Uint`].
pub fn zeroed_texture_uint(&self) -> &GpuTexture {
self.zeroed_texture_uint.0.as_ref().unwrap()
&self.zeroed_texture_uint.0
}

fn create_and_upload_texture(
Expand Down Expand Up @@ -292,17 +286,13 @@ impl TextureManager2D {

// TODO(andreas): mipmap generation

GpuTexture2DHandle(Some(texture))
GpuTexture2DHandle(texture)
}

/// Retrieves gpu handle.
#[allow(clippy::unused_self)]
pub fn get(&self, handle: &GpuTexture2DHandle) -> Result<GpuTexture, ResourceManagerError> {
handle
.0
.as_ref()
.ok_or(ResourceManagerError::NullHandle)
.map(|h| h.clone())
pub fn get(&self, handle: &GpuTexture2DHandle) -> GpuTexture {
handle.0.clone()
}

pub(crate) fn begin_frame(&mut self, _frame_index: u64) {
Expand All @@ -319,7 +309,7 @@ fn create_zero_texture(
format: wgpu::TextureFormat,
) -> GpuTexture2DHandle {
// Wgpu zeros out new textures automatically
GpuTexture2DHandle(Some(texture_pool.alloc(
GpuTexture2DHandle(texture_pool.alloc(
device,
&TextureDesc {
label: format!("zeroed pixel {format:?}").into(),
Expand All @@ -334,5 +324,5 @@ fn create_zero_texture(
dimension: wgpu::TextureDimension::D2,
usage: wgpu::TextureUsages::TEXTURE_BINDING,
},
)))
))
}
23 changes: 12 additions & 11 deletions crates/re_viewer/src/gpu_bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use tensor_to_gpu::tensor_to_gpu;
use egui::mutex::Mutex;

use re_renderer::{
renderer::ColormappedTexture,
renderer::{ColormappedTexture, RectangleOptions},
resource_managers::{GpuTexture2DHandle, Texture2DCreationDesc},
RenderContext, ViewBuilder,
};
Expand Down Expand Up @@ -162,17 +162,18 @@ pub fn render_image(
extent_u: glam::Vec3::X * space_rect.width(),
extent_v: glam::Vec3::Y * space_rect.height(),
colormapped_texture,
texture_filter_magnification: match texture_options.magnification {
egui::TextureFilter::Nearest => TextureFilterMag::Nearest,
egui::TextureFilter::Linear => TextureFilterMag::Linear,
options: RectangleOptions {
texture_filter_magnification: match texture_options.magnification {
egui::TextureFilter::Nearest => TextureFilterMag::Nearest,
egui::TextureFilter::Linear => TextureFilterMag::Linear,
},
texture_filter_minification: match texture_options.minification {
egui::TextureFilter::Nearest => TextureFilterMin::Nearest,
egui::TextureFilter::Linear => TextureFilterMin::Linear,
},
multiplicative_tint: egui::Rgba::WHITE,
..Default::default()
},
texture_filter_minification: match texture_options.minification {
egui::TextureFilter::Nearest => TextureFilterMin::Nearest,
egui::TextureFilter::Linear => TextureFilterMin::Linear,
},
multiplicative_tint: egui::Rgba::WHITE,
depth_offset: 0,
outline_mask: Default::default(),
};

// ------------------------------------------------------------------------
Expand Down
Loading