Skip to content

Commit

Permalink
Refactor: remove GpuTexture2DHandle::invalid (#1866)
Browse files Browse the repository at this point in the history
* Refactor TexturedRect

* Remove GpuTexture2DHandle::invalid

* `GpuTexture2DHandle` is always valid

* spacing
  • Loading branch information
emilk authored Apr 17, 2023
1 parent f35efec commit ccc20c2
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 98 deletions.
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
73 changes: 39 additions & 34 deletions crates/re_renderer/src/renderer/rectangles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,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 @@ -122,6 +111,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 @@ -134,13 +127,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 @@ -179,7 +168,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 @@ -231,12 +220,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 @@ -281,24 +286,24 @@ mod gpu_data {
}
}

let minification_filter = match rectangle.texture_filter_minification {
let minification_filter = match rectangle.options.texture_filter_minification {
super::TextureFilterMin::Linear => FILTER_BILINEAR,
super::TextureFilterMin::Nearest => FILTER_NEAREST,
};
let magnification_filter = match rectangle.texture_filter_magnification {
let magnification_filter = match rectangle.options.texture_filter_magnification {
super::TextureFilterMag::Linear => FILTER_BILINEAR,
super::TextureFilterMag::Nearest => FILTER_NEAREST,
};

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 @@ -346,15 +351,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 @@ -371,20 +376,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 @@ -430,7 +435,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 @@ -459,7 +464,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()
}

/// Width and height of the texture.
Expand Down Expand Up @@ -218,27 +212,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 @@ -297,17 +291,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 @@ -324,7 +314,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 @@ -339,5 +329,5 @@ fn create_zero_texture(
dimension: wgpu::TextureDimension::D2,
usage: wgpu::TextureUsages::TEXTURE_BINDING,
},
)))
))
}
Loading

0 comments on commit ccc20c2

Please sign in to comment.