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

BGR(A) image format support #7238

Merged
merged 8 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ enum ColorModel: ubyte{

/// Red, Green, Blue, Alpha
RGBA = 3,

/// Blue, Green, Red
BGR,

/// Blue, Green, Red, Alpha
BGRA,
}
4 changes: 2 additions & 2 deletions crates/store/re_types/src/archetypes/image_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ impl Image {

let is_shape_correct = match color_model {
ColorModel::L => non_empty_dim_inds.len() == 2,
ColorModel::RGB => {
ColorModel::RGB | ColorModel::BGR => {
non_empty_dim_inds.len() == 3 && shape[non_empty_dim_inds[2]].size == 3
}
ColorModel::RGBA => {
ColorModel::RGBA | ColorModel::BGRA => {
non_empty_dim_inds.len() == 3 && shape[non_empty_dim_inds[2]].size == 4
}
};
Expand Down
16 changes: 15 additions & 1 deletion crates/store/re_types/src/datatypes/color_model.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions crates/store/re_types/src/datatypes/color_model_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ impl ColorModel {
pub fn num_channels(self) -> usize {
match self {
Self::L => 1,
Self::RGB => 3,
Self::RGBA => 4,
Self::RGB | Self::BGR => 3,
Self::RGBA | Self::BGRA => 4,
}
}

/// Do we have an alpha channel?
#[inline]
pub fn has_alpha(&self) -> bool {
match self {
Self::L | Self::RGB => false,
Self::RGBA => true,
Self::L | Self::RGB | Self::BGR => false,
Self::RGBA | Self::BGRA => true,
}
}
}
46 changes: 46 additions & 0 deletions crates/viewer/re_data_ui/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,52 @@ fn image_pixel_value_ui(
None
}
}

ColorModel::BGR => {
if let Some([b, g, r]) = {
if let [Some(b), Some(g), Some(r)] = [
image.get_xyc(x, y, 0),
image.get_xyc(x, y, 1),
image.get_xyc(x, y, 2),
] {
Some([r, g, b])
} else {
None
}
} {
match (b, g, r) {
(TensorElement::U8(b), TensorElement::U8(g), TensorElement::U8(r)) => {
Some(format!("B: {b}, G: {g}, R: {r}, #{b:02X}{g:02X}{r:02X}"))
}
_ => Some(format!("B: {b}, G: {g}, R: {r}")),
}
} else {
None
}
}

ColorModel::BGRA => {
if let (Some(b), Some(g), Some(r), Some(a)) = (
image.get_xyc(x, y, 0),
image.get_xyc(x, y, 1),
image.get_xyc(x, y, 2),
image.get_xyc(x, y, 3),
) {
match (b, g, r, a) {
(
TensorElement::U8(b),
TensorElement::U8(g),
TensorElement::U8(r),
TensorElement::U8(a),
) => Some(format!(
"B: {b}, G: {g}, R: {r}, A: {a}, #{r:02X}{g:02X}{b:02X}{a:02X}"
)),
_ => Some(format!("B: {b}, G: {g}, R: {r}, A: {a}")),
}
} else {
None
}
}
},
};

Expand Down
3 changes: 3 additions & 0 deletions crates/viewer/re_renderer/shader/rectangle.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ struct UniformBuffer {

/// Boolean: multiply RGB with alpha before filtering
multiply_rgb_with_alpha: u32,

/// Boolean: swizzle RGBA to BGRA
bgra_to_rgba: u32,
};

@group(1) @binding(0)
Expand Down
5 changes: 5 additions & 0 deletions crates/viewer/re_renderer/shader/rectangle_fs.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ fn decode_color(sampled_value: vec4f) -> vec4f {
// Normalize the value first, otherwise premultiplying alpha and linear space conversion won't make sense.
var rgba = normalize_range(sampled_value);

// BGR(A) -> RGB(A)
if rect_info.bgra_to_rgba != 0u {
rgba = rgba.bgra;
}

// Convert to linear space
if rect_info.decode_srgb != 0u {
if all(vec3f(0.0) <= rgba.rgb) && all(rgba.rgb <= vec3f(1.0)) {
Expand Down
11 changes: 9 additions & 2 deletions crates/viewer/re_renderer/src/renderer/rectangles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub enum TextureFilterMin {
pub enum ShaderDecoding {
Nv12,
Yuy2,

/// BGR(A)->RGB(A) conversion is done in the shader.
/// (as opposed to doing it via ``)
Bgr,
}

/// Describes a texture and how to map it to a color.
Expand Down Expand Up @@ -151,7 +155,7 @@ impl ColormappedTexture {
let [width, height] = self.texture.width_height();
[width / 2, height]
}
_ => self.texture.width_height(),
Some(ShaderDecoding::Bgr) | None => self.texture.width_height(),
}
}
}
Expand Down Expand Up @@ -275,7 +279,8 @@ mod gpu_data {

decode_srgb: u32,
multiply_rgb_with_alpha: u32,
_row_padding: [u32; 2],
bgra_to_rgba: u32,
_row_padding: [u32; 1],

_end_padding: [wgpu_buffer_types::PaddingRow; 16 - 7],
}
Expand Down Expand Up @@ -362,6 +367,7 @@ mod gpu_data {
super::TextureFilterMag::Linear => FILTER_BILINEAR,
super::TextureFilterMag::Nearest => FILTER_NEAREST,
};
let bgra_to_rgba = shader_decoding == &Some(super::ShaderDecoding::Bgr);

Ok(Self {
top_left_corner_position: (*top_left_corner_position).into(),
Expand All @@ -379,6 +385,7 @@ mod gpu_data {
magnification_filter,
decode_srgb: *decode_srgb as _,
multiply_rgb_with_alpha: *multiply_rgb_with_alpha as _,
bgra_to_rgba: bgra_to_rgba as _,
_row_padding: Default::default(),
_end_padding: Default::default(),
})
Expand Down
66 changes: 50 additions & 16 deletions crates/viewer/re_space_view_spatial/src/mesh_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,19 @@ impl LoadedMesh {
re_math::BoundingBox::from_points(vertex_positions.iter().copied())
};

let albedo = if let (Some(albedo_texture_buffer), Some(albedo_texture_format)) =
(&albedo_texture_buffer, albedo_texture_format)
{
let image_info = ImageInfo {
buffer_row_id: RowId::ZERO, // unused
buffer: albedo_texture_buffer.0.clone(),
format: albedo_texture_format.0,
kind: re_types::image::ImageKind::Color,
colormap: None,
};
re_viewer_context::gpu_bridge::get_or_create_texture(render_ctx, texture_key, || {
let debug_name = "mesh albedo texture";
texture_creation_desc_from_color_image(&image_info, debug_name)
})?
} else {
let albedo = try_get_or_create_albedo_texture(
albedo_texture_buffer,
albedo_texture_format,
render_ctx,
texture_key,
&name,
)
.unwrap_or_else(|| {
render_ctx
.texture_manager_2d
.white_texture_unorm_handle()
.clone()
};
});

let mesh = re_renderer::mesh::Mesh {
label: name.clone().into(),
Expand Down Expand Up @@ -211,3 +204,44 @@ impl LoadedMesh {
self.bbox
}
}

fn try_get_or_create_albedo_texture(
albedo_texture_buffer: &Option<re_types::components::ImageBuffer>,
albedo_texture_format: &Option<re_types::components::ImageFormat>,
render_ctx: &RenderContext,
texture_key: u64,
name: &str,
) -> Option<re_renderer::resource_managers::GpuTexture2D> {
let (Some(albedo_texture_buffer), Some(albedo_texture_format)) =
(&albedo_texture_buffer, albedo_texture_format)
else {
return None;
};

let image_info = ImageInfo {
buffer_row_id: RowId::ZERO, // unused
buffer: albedo_texture_buffer.0.clone(),
format: albedo_texture_format.0,
kind: re_types::image::ImageKind::Color,
colormap: None,
};

if re_viewer_context::gpu_bridge::required_shader_decode(albedo_texture_format).is_some() {
re_log::warn_once!("Mesh can't yet handle encoded image formats like NV12 & YUY2 or BGR(A) formats without a channel type other than U8. Ignoring the texture at {name:?}.");
return None;
}

let texture =
re_viewer_context::gpu_bridge::get_or_create_texture(render_ctx, texture_key, || {
let debug_name = "mesh albedo texture";
texture_creation_desc_from_color_image(&image_info, debug_name)
});

match texture {
Ok(texture) => Some(texture),
Err(err) => {
re_log::warn_once!("Failed to create mesh albedo texture for {name:?}: {err}");
None
}
}
}
Loading
Loading