Skip to content

Commit ccc20c2

Browse files
authored
Refactor: remove GpuTexture2DHandle::invalid (#1866)
* Refactor TexturedRect * Remove GpuTexture2DHandle::invalid * `GpuTexture2DHandle` is always valid * spacing
1 parent f35efec commit ccc20c2

File tree

10 files changed

+102
-98
lines changed

10 files changed

+102
-98
lines changed

crates/re_renderer/examples/2d.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use ecolor::Hsva;
22
use re_renderer::{
33
renderer::{
4-
ColormappedTexture, LineStripFlags, RectangleDrawData, TextureFilterMag, TextureFilterMin,
5-
TexturedRect,
4+
ColormappedTexture, LineStripFlags, RectangleDrawData, RectangleOptions, TextureFilterMag,
5+
TextureFilterMin, TexturedRect,
66
},
77
resource_managers::{GpuTexture2DHandle, Texture2DCreationDesc},
88
view_builder::{self, Projection, TargetConfiguration, ViewBuilder},
@@ -197,9 +197,11 @@ impl framework::Example for Render2D {
197197
colormapped_texture: ColormappedTexture::from_unorm_srgba(
198198
self.rerun_logo_texture.clone(),
199199
),
200-
texture_filter_magnification: TextureFilterMag::Nearest,
201-
texture_filter_minification: TextureFilterMin::Linear,
202-
..Default::default()
200+
options: RectangleOptions {
201+
texture_filter_magnification: TextureFilterMag::Nearest,
202+
texture_filter_minification: TextureFilterMin::Linear,
203+
..Default::default()
204+
},
203205
},
204206
TexturedRect {
205207
top_left_corner_position: glam::vec3(
@@ -213,10 +215,12 @@ impl framework::Example for Render2D {
213215
colormapped_texture: ColormappedTexture::from_unorm_srgba(
214216
self.rerun_logo_texture.clone(),
215217
),
216-
texture_filter_magnification: TextureFilterMag::Linear,
217-
texture_filter_minification: TextureFilterMin::Linear,
218-
depth_offset: 1,
219-
..Default::default()
218+
options: RectangleOptions {
219+
texture_filter_magnification: TextureFilterMag::Linear,
220+
texture_filter_minification: TextureFilterMin::Linear,
221+
depth_offset: 1,
222+
..Default::default()
223+
},
220224
},
221225
],
222226
)

crates/re_renderer/examples/depth_cloud.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use macaw::IsoTransform;
2121
use re_renderer::{
2222
renderer::{
2323
ColormappedTexture, DepthCloud, DepthCloudDepthData, DepthCloudDrawData, DepthClouds,
24-
DrawData, GenericSkyboxDrawData, RectangleDrawData, TexturedRect,
24+
DrawData, GenericSkyboxDrawData, RectangleDrawData, RectangleOptions, TexturedRect,
2525
},
2626
resource_managers::{GpuTexture2DHandle, Texture2DCreationDesc},
2727
view_builder::{self, Projection, ViewBuilder},
@@ -327,11 +327,13 @@ impl framework::Example for RenderDepthClouds {
327327
extent_u: world_from_model.transform_vector3(-glam::Vec3::X),
328328
extent_v: world_from_model.transform_vector3(-glam::Vec3::Y),
329329
colormapped_texture: ColormappedTexture::from_unorm_srgba(albedo_handle.clone()),
330-
texture_filter_magnification: re_renderer::renderer::TextureFilterMag::Nearest,
331-
texture_filter_minification: re_renderer::renderer::TextureFilterMin::Linear,
332-
multiplicative_tint: Rgba::from_white_alpha(0.5),
333-
depth_offset: -1,
334-
..Default::default()
330+
options: RectangleOptions {
331+
texture_filter_magnification: re_renderer::renderer::TextureFilterMag::Nearest,
332+
texture_filter_minification: re_renderer::renderer::TextureFilterMin::Linear,
333+
multiplicative_tint: Rgba::from_white_alpha(0.5),
334+
depth_offset: -1,
335+
..Default::default()
336+
},
335337
}],
336338
)
337339
.unwrap();

crates/re_renderer/src/mesh.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl GpuMesh {
286286
.iter()
287287
.zip(uniform_buffer_bindings.into_iter())
288288
{
289-
let texture = ctx.texture_manager_2d.get(&material.albedo)?;
289+
let texture = ctx.texture_manager_2d.get(&material.albedo);
290290
let bind_group = pools.bind_groups.alloc(
291291
device,
292292
pools,

crates/re_renderer/src/renderer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ pub use test_triangle::TestTriangleDrawData;
2323

2424
mod rectangles;
2525
pub use rectangles::{
26-
ColorMapper, ColormappedTexture, RectangleDrawData, TextureFilterMag, TextureFilterMin,
27-
TexturedRect,
26+
ColorMapper, ColormappedTexture, RectangleDrawData, RectangleOptions, TextureFilterMag,
27+
TextureFilterMin, TexturedRect,
2828
};
2929

3030
mod mesh_renderer;

crates/re_renderer/src/renderer/rectangles.rs

+39-34
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,6 @@ pub enum ColorMapper {
8787
Texture(GpuTexture2DHandle),
8888
}
8989

90-
impl Default for ColormappedTexture {
91-
fn default() -> Self {
92-
Self {
93-
texture: GpuTexture2DHandle::invalid(),
94-
range: [0.0, 1.0],
95-
gamma: 1.0,
96-
color_mapper: None,
97-
}
98-
}
99-
}
100-
10190
impl ColormappedTexture {
10291
pub fn from_unorm_srgba(texture: GpuTexture2DHandle) -> Self {
10392
Self {
@@ -122,6 +111,10 @@ pub struct TexturedRect {
122111
/// Texture that fills the rectangle
123112
pub colormapped_texture: ColormappedTexture,
124113

114+
pub options: RectangleOptions,
115+
}
116+
117+
pub struct RectangleOptions {
125118
pub texture_filter_magnification: TextureFilterMag,
126119
pub texture_filter_minification: TextureFilterMin,
127120

@@ -134,13 +127,9 @@ pub struct TexturedRect {
134127
pub outline_mask: OutlineMaskPreference,
135128
}
136129

137-
impl Default for TexturedRect {
130+
impl Default for RectangleOptions {
138131
fn default() -> Self {
139132
Self {
140-
top_left_corner_position: glam::Vec3::ZERO,
141-
extent_u: glam::Vec3::ZERO,
142-
extent_v: glam::Vec3::ZERO,
143-
colormapped_texture: Default::default(),
144133
texture_filter_magnification: TextureFilterMag::Nearest,
145134
texture_filter_minification: TextureFilterMin::Linear,
146135
multiplicative_tint: Rgba::WHITE,
@@ -179,7 +168,7 @@ pub enum RectangleError {
179168
mod gpu_data {
180169
use crate::wgpu_buffer_types;
181170

182-
use super::{ColorMapper, RectangleError};
171+
use super::{ColorMapper, RectangleError, TexturedRect};
183172

184173
// Keep in sync with mirror in rectangle.wgsl
185174

@@ -231,12 +220,28 @@ mod gpu_data {
231220
) -> Result<Self, RectangleError> {
232221
let texture_info = texture_format.describe();
233222

223+
let TexturedRect {
224+
top_left_corner_position,
225+
extent_u,
226+
extent_v,
227+
colormapped_texture,
228+
options,
229+
} = rectangle;
230+
234231
let super::ColormappedTexture {
235232
texture: _,
236233
range,
237234
gamma,
238235
color_mapper,
239-
} = &rectangle.colormapped_texture;
236+
} = colormapped_texture;
237+
238+
let super::RectangleOptions {
239+
texture_filter_magnification: _,
240+
texture_filter_minification: _,
241+
multiplicative_tint,
242+
depth_offset,
243+
outline_mask,
244+
} = options;
240245

241246
let sample_type = match texture_info.sample_type {
242247
wgpu::TextureSampleType::Float { .. } => {
@@ -281,24 +286,24 @@ mod gpu_data {
281286
}
282287
}
283288

284-
let minification_filter = match rectangle.texture_filter_minification {
289+
let minification_filter = match rectangle.options.texture_filter_minification {
285290
super::TextureFilterMin::Linear => FILTER_BILINEAR,
286291
super::TextureFilterMin::Nearest => FILTER_NEAREST,
287292
};
288-
let magnification_filter = match rectangle.texture_filter_magnification {
293+
let magnification_filter = match rectangle.options.texture_filter_magnification {
289294
super::TextureFilterMag::Linear => FILTER_BILINEAR,
290295
super::TextureFilterMag::Nearest => FILTER_NEAREST,
291296
};
292297

293298
Ok(Self {
294-
top_left_corner_position: rectangle.top_left_corner_position.into(),
299+
top_left_corner_position: (*top_left_corner_position).into(),
295300
colormap_function,
296-
extent_u: rectangle.extent_u.into(),
301+
extent_u: (*extent_u).into(),
297302
sample_type,
298-
extent_v: rectangle.extent_v.into(),
299-
depth_offset: rectangle.depth_offset as f32,
300-
multiplicative_tint: rectangle.multiplicative_tint,
301-
outline_mask: rectangle.outline_mask.0.unwrap_or_default().into(),
303+
extent_v: (*extent_v).into(),
304+
depth_offset: *depth_offset as f32,
305+
multiplicative_tint: *multiplicative_tint,
306+
outline_mask: outline_mask.0.unwrap_or_default().into(),
302307
range_min_max: (*range).into(),
303308
color_mapper: color_mapper_int,
304309
gamma: *gamma,
@@ -346,15 +351,15 @@ impl RectangleDrawData {
346351
});
347352
}
348353

349-
// TODO(emilk): continue on error (skipping just that rectangle)?
350354
let textures: Vec<_> = rectangles
351355
.iter()
352356
.map(|rectangle| {
353357
ctx.texture_manager_2d
354358
.get(&rectangle.colormapped_texture.texture)
355359
})
356-
.try_collect()?;
360+
.collect();
357361

362+
// TODO(emilk): continue on error (skipping just that rectangle)?
358363
let uniform_buffers: Vec<_> = izip!(rectangles, &textures)
359364
.map(|(rect, texture)| {
360365
gpu_data::UniformBuffer::from_textured_rect(rect, &texture.creation_desc.format)
@@ -371,20 +376,20 @@ impl RectangleDrawData {
371376
for (rectangle, uniform_buffer, texture) in
372377
izip!(rectangles, uniform_buffer_bindings, textures)
373378
{
379+
let options = &rectangle.options;
374380
let sampler = ctx.gpu_resources.samplers.get_or_create(
375381
&ctx.device,
376382
&SamplerDesc {
377383
label: format!(
378384
"rectangle sampler mag {:?} min {:?}",
379-
rectangle.texture_filter_magnification,
380-
rectangle.texture_filter_minification
385+
options.texture_filter_magnification, options.texture_filter_minification
381386
)
382387
.into(),
383-
mag_filter: match rectangle.texture_filter_magnification {
388+
mag_filter: match options.texture_filter_magnification {
384389
TextureFilterMag::Linear => wgpu::FilterMode::Linear,
385390
TextureFilterMag::Nearest => wgpu::FilterMode::Nearest,
386391
},
387-
min_filter: match rectangle.texture_filter_minification {
392+
min_filter: match options.texture_filter_minification {
388393
TextureFilterMin::Linear => wgpu::FilterMode::Linear,
389394
TextureFilterMin::Nearest => wgpu::FilterMode::Nearest,
390395
},
@@ -430,7 +435,7 @@ impl RectangleDrawData {
430435
let colormap_texture = if let Some(ColorMapper::Texture(handle)) =
431436
&rectangle.colormapped_texture.color_mapper
432437
{
433-
let colormap_texture = ctx.texture_manager_2d.get(handle)?;
438+
let colormap_texture = ctx.texture_manager_2d.get(handle);
434439
if colormap_texture.creation_desc.format != wgpu::TextureFormat::Rgba8UnormSrgb {
435440
return Err(RectangleError::UnsupportedColormapTextureFormat(
436441
colormap_texture.creation_desc.format,
@@ -459,7 +464,7 @@ impl RectangleDrawData {
459464
layout: rectangle_renderer.bind_group_layout,
460465
},
461466
),
462-
draw_outline_mask: rectangle.outline_mask.is_some(),
467+
draw_outline_mask: rectangle.options.outline_mask.is_some(),
463468
});
464469
}
465470

crates/re_renderer/src/resource_managers/texture_manager.rs

+15-25
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,23 @@ use crate::{
77
DebugLabel,
88
};
99

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

2018
impl GpuTexture2DHandle {
21-
pub fn invalid() -> Self {
22-
Self(None)
23-
}
24-
25-
/// Width of the texture, defaults to 1 if invalid since fallback textures are typically one pixel.
19+
/// Width of the texture.
2620
pub fn width(&self) -> u32 {
27-
self.0.as_ref().map_or(1, |t| t.texture.width())
21+
self.0.texture.width()
2822
}
2923

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

3529
/// Width and height of the texture.
@@ -218,27 +212,27 @@ impl TextureManager2D {
218212

219213
/// Returns a single pixel white pixel with an rgba8unorm format.
220214
pub fn white_texture_unorm(&self) -> &GpuTexture {
221-
self.white_texture_unorm.0.as_ref().unwrap()
215+
&self.white_texture_unorm.0
222216
}
223217

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

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

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

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

244238
fn create_and_upload_texture(
@@ -297,17 +291,13 @@ impl TextureManager2D {
297291

298292
// TODO(andreas): mipmap generation
299293

300-
GpuTexture2DHandle(Some(texture))
294+
GpuTexture2DHandle(texture)
301295
}
302296

303297
/// Retrieves gpu handle.
304298
#[allow(clippy::unused_self)]
305-
pub fn get(&self, handle: &GpuTexture2DHandle) -> Result<GpuTexture, ResourceManagerError> {
306-
handle
307-
.0
308-
.as_ref()
309-
.ok_or(ResourceManagerError::NullHandle)
310-
.map(|h| h.clone())
299+
pub fn get(&self, handle: &GpuTexture2DHandle) -> GpuTexture {
300+
handle.0.clone()
311301
}
312302

313303
pub(crate) fn begin_frame(&mut self, _frame_index: u64) {
@@ -324,7 +314,7 @@ fn create_zero_texture(
324314
format: wgpu::TextureFormat,
325315
) -> GpuTexture2DHandle {
326316
// Wgpu zeros out new textures automatically
327-
GpuTexture2DHandle(Some(texture_pool.alloc(
317+
GpuTexture2DHandle(texture_pool.alloc(
328318
device,
329319
&TextureDesc {
330320
label: format!("zeroed pixel {format:?}").into(),
@@ -339,5 +329,5 @@ fn create_zero_texture(
339329
dimension: wgpu::TextureDimension::D2,
340330
usage: wgpu::TextureUsages::TEXTURE_BINDING,
341331
},
342-
)))
332+
))
343333
}

0 commit comments

Comments
 (0)