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: Add helper functions to GpuTexture2DHandle #1900

Merged
merged 4 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions crates/re_renderer/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,13 @@ impl GpuMesh {
.iter()
.zip(uniform_buffer_bindings.into_iter())
{
let texture = ctx.texture_manager_2d.get(&material.albedo);
let bind_group = pools.bind_groups.alloc(
device,
pools,
&BindGroupDesc {
label: material.label.clone(),
entries: smallvec![
BindGroupEntry::DefaultTextureView(texture.handle),
BindGroupEntry::DefaultTextureView(material.albedo.wgpu_handle()),
uniform_buffer_binding
],
layout: mesh_bind_group_layout,
Expand Down
38 changes: 12 additions & 26 deletions crates/re_renderer/src/renderer/rectangles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,8 @@ mod gpu_data {
}

impl UniformBuffer {
pub fn from_textured_rect(
rectangle: &super::TexturedRect,
texture_format: &wgpu::TextureFormat,
) -> Result<Self, RectangleError> {
pub fn from_textured_rect(rectangle: &super::TexturedRect) -> Result<Self, RectangleError> {
let texture_format = rectangle.colormapped_texture.texture.format();
let texture_info = texture_format.describe();

let TexturedRect {
Expand Down Expand Up @@ -245,7 +243,7 @@ mod gpu_data {

let sample_type = match texture_info.sample_type {
wgpu::TextureSampleType::Float { .. } => {
if super::is_float_filterable(texture_format) {
if super::is_float_filterable(&texture_format) {
SAMPLE_TYPE_FLOAT_FILTER
} else {
SAMPLE_TYPE_FLOAT_NOFILTER
Expand Down Expand Up @@ -351,19 +349,10 @@ impl RectangleDrawData {
});
}

let textures: Vec<_> = rectangles
.iter()
.map(|rectangle| {
ctx.texture_manager_2d
.get(&rectangle.colormapped_texture.texture)
})
.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)
})
let uniform_buffers: Vec<_> = rectangles
.iter()
.map(gpu_data::UniformBuffer::from_textured_rect)
.try_collect()?;

let uniform_buffer_bindings = create_and_fill_uniform_buffer_batch(
Expand All @@ -373,9 +362,7 @@ impl RectangleDrawData {
);

let mut instances = Vec::with_capacity(rectangles.len());
for (rectangle, uniform_buffer, texture) in
izip!(rectangles, uniform_buffer_bindings, textures)
{
for (rectangle, uniform_buffer) in izip!(rectangles, uniform_buffer_bindings) {
let options = &rectangle.options;
let sampler = ctx.gpu_resources.samplers.get_or_create(
&ctx.device,
Expand All @@ -398,6 +385,7 @@ impl RectangleDrawData {
},
);

let texture = rectangle.colormapped_texture.texture.texture();
let texture_format = texture.creation_desc.format;
let texture_description = texture_format.describe();
if texture_description.required_features != Default::default() {
Expand Down Expand Up @@ -435,13 +423,11 @@ 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);
if colormap_texture.creation_desc.format != wgpu::TextureFormat::Rgba8UnormSrgb {
return Err(RectangleError::UnsupportedColormapTextureFormat(
colormap_texture.creation_desc.format,
));
let format = handle.format();
if format != wgpu::TextureFormat::Rgba8UnormSrgb {
return Err(RectangleError::UnsupportedColormapTextureFormat(format));
}
colormap_texture.handle
handle.wgpu_handle()
} else {
ctx.texture_manager_2d.zeroed_texture_float().handle
};
Expand Down
24 changes: 18 additions & 6 deletions crates/re_renderer/src/resource_managers/texture_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,38 @@ use crate::{
pub struct GpuTexture2DHandle(GpuTexture);

impl GpuTexture2DHandle {
#[inline]
pub fn texture(&self) -> &GpuTexture {
&self.0
}
emilk marked this conversation as resolved.
Show resolved Hide resolved

#[inline]
pub fn wgpu_handle(&self) -> crate::wgpu_resources::GpuTextureHandle {
self.0.handle
}
emilk marked this conversation as resolved.
Show resolved Hide resolved

/// Width of the texture.
#[inline]
pub fn width(&self) -> u32 {
self.0.texture.width()
}

/// Height of the texture.
#[inline]
pub fn height(&self) -> u32 {
self.0.texture.height()
}

/// Width and height of the texture.
#[inline]
pub fn width_height(&self) -> [u32; 2] {
[self.width(), self.height()]
}

#[inline]
pub fn format(&self) -> wgpu::TextureFormat {
self.0.texture.format()
}
}

/// Data required to create a texture 2d resource.
Expand Down Expand Up @@ -294,12 +312,6 @@ impl TextureManager2D {
GpuTexture2DHandle(texture)
}

/// Retrieves gpu handle.
#[allow(clippy::unused_self)]
pub fn get(&self, handle: &GpuTexture2DHandle) -> GpuTexture {
handle.0.clone()
}

pub(crate) fn begin_frame(&mut self, _frame_index: u64) {
// Drop any textures that weren't accessed in the last frame
self.texture_cache
Expand Down
3 changes: 1 addition & 2 deletions crates/re_viewer/src/gpu_bridge/tensor_to_gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ fn color_tensor_to_gpu(
})
})?;

let gpu_texture = render_ctx.texture_manager_2d.get(&texture_handle);
let texture_format = gpu_texture.creation_desc.format;
let texture_format = texture_handle.format();

// Special casing for normalized textures used above:
let range = if matches!(
Expand Down