Skip to content

Commit

Permalink
remove chroma subsampling shader decode from rectangle shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Oct 8, 2024
1 parent a853a2e commit deec337
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 148 deletions.
2 changes: 1 addition & 1 deletion crates/viewer/re_data_ui/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn show_image_preview(
desired_size: egui::Vec2,
) -> Result<egui::Response, (egui::Response, anyhow::Error)> {
fn texture_size(colormapped_texture: &ColormappedTexture) -> Vec2 {
let [w, h] = colormapped_texture.width_height();
let [w, h] = colormapped_texture.texture.width_height();
egui::vec2(w as f32, h as f32)
}

Expand Down
73 changes: 0 additions & 73 deletions crates/viewer/re_renderer/shader/decodings.wgsl

This file was deleted.

17 changes: 0 additions & 17 deletions crates/viewer/re_renderer/shader/rectangle_fs.wgsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#import <./colormap.wgsl>
#import <./rectangle.wgsl>
#import <./utils/srgb.wgsl>
#import <./decodings.wgsl>

// WARNING! Adding anything else to this shader is very likely to push us over a size threshold that
// causes the failure reported in: https://github.com/rerun-io/rerun/issues/3931
// Make sure any changes are tested in Chrome on Linux using the Intel Mesa driver.

fn is_magnifying(pixel_coord: vec2f) -> bool {
return fwidth(pixel_coord.x) < 1.0;
Expand Down Expand Up @@ -86,10 +81,6 @@ fn fs_main(in: VertexOut) -> @location(0) vec4f {
texture_dimensions = vec2f(textureDimensions(texture_sint).xy);
} else if rect_info.sample_type == SAMPLE_TYPE_UINT {
texture_dimensions = vec2f(textureDimensions(texture_uint).xy);
} else if rect_info.sample_type == SAMPLE_TYPE_NV12 {
texture_dimensions = vec2f(textureDimensions(texture_uint).xy);
} else if rect_info.sample_type == SAMPLE_TYPE_YUY2 {
texture_dimensions = vec2f(textureDimensions(texture_uint).xy);
}

let coord = in.texcoord * texture_dimensions;
Expand Down Expand Up @@ -141,14 +132,6 @@ fn fs_main(in: VertexOut) -> @location(0) vec4f {
vec4f(textureLoad(texture_uint, v01_coord, 0)),
vec4f(textureLoad(texture_uint, v10_coord, 0)),
vec4f(textureLoad(texture_uint, v11_coord, 0)));
} else if rect_info.sample_type == SAMPLE_TYPE_NV12 || rect_info.sample_type == SAMPLE_TYPE_YUY2{
normalized_value = decode_color_and_filter_nearest_or_bilinear(
filter_nearest,
coord,
decode_nv12_or_yuy2(rect_info.sample_type, texture_uint, v00_coord),
decode_nv12_or_yuy2(rect_info.sample_type, texture_uint, v01_coord),
decode_nv12_or_yuy2(rect_info.sample_type, texture_uint, v10_coord),
decode_nv12_or_yuy2(rect_info.sample_type, texture_uint, v11_coord));
} else {
return ERROR_RGBA; // unknown sample type
}
Expand Down
27 changes: 2 additions & 25 deletions crates/viewer/re_renderer/src/renderer/rectangles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ pub enum TextureFilterMin {
/// Describes how the color information is encoded in the texture.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShaderDecoding {
Nv12,
Yuy2,

/// Do BGR(A)->RGB(A) conversion is in the shader.
Bgr,
}
Expand Down Expand Up @@ -145,17 +142,7 @@ impl ColormappedTexture {
}

pub fn width_height(&self) -> [u32; 2] {
match self.shader_decoding {
Some(ShaderDecoding::Nv12) => {
let [width, height] = self.texture.width_height();
[width, height * 2 / 3]
}
Some(ShaderDecoding::Yuy2) => {
let [width, height] = self.texture.width_height();
[width / 2, height]
}
Some(ShaderDecoding::Bgr) | None => self.texture.width_height(),
}
self.texture.width_height()
}
}

Expand Down Expand Up @@ -237,8 +224,6 @@ mod gpu_data {
const SAMPLE_TYPE_FLOAT: u32 = 1;
const SAMPLE_TYPE_SINT: u32 = 2;
const SAMPLE_TYPE_UINT: u32 = 3;
const SAMPLE_TYPE_NV12: u32 = 4;
const SAMPLE_TYPE_YUY2: u32 = 5;

// How do we do colormapping?
const COLOR_MAPPER_OFF_GRAYSCALE: u32 = 1;
Expand Down Expand Up @@ -318,15 +303,7 @@ mod gpu_data {
let sample_type = match texture_format.sample_type(None, None) {
Some(wgpu::TextureSampleType::Float { .. }) => SAMPLE_TYPE_FLOAT,
Some(wgpu::TextureSampleType::Sint) => SAMPLE_TYPE_SINT,
Some(wgpu::TextureSampleType::Uint) => {
if shader_decoding == &Some(super::ShaderDecoding::Nv12) {
SAMPLE_TYPE_NV12
} else if shader_decoding == &Some(super::ShaderDecoding::Yuy2) {
SAMPLE_TYPE_YUY2
} else {
SAMPLE_TYPE_UINT
}
}
Some(wgpu::TextureSampleType::Uint) => SAMPLE_TYPE_UINT,
_ => {
return Err(RectangleError::TextureFormatNotSupported(texture_format));
}
Expand Down
6 changes: 0 additions & 6 deletions crates/viewer/re_renderer/src/workspace_shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ pub fn init() {
fs.create_file(virtpath, content).unwrap();
}

{
let virtpath = Path::new("shader/decodings.wgsl");
let content = include_str!("../shader/decodings.wgsl").into();
fs.create_file(virtpath, content).unwrap();
}

{
let virtpath = Path::new("shader/depth_cloud.wgsl");
let content = include_str!("../shader/depth_cloud.wgsl").into();
Expand Down
2 changes: 1 addition & 1 deletion crates/viewer/re_space_view_tensor/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl TensorSpaceView {
&colormap,
gamma,
)?;
let [width, height] = colormapped_texture.width_height();
let [width, height] = colormapped_texture.texture.width_height();

let view_fit: ViewFit = ViewProperty::from_archetype::<TensorViewFit>(
ctx.blueprint_db(),
Expand Down
40 changes: 15 additions & 25 deletions crates/viewer/re_viewer_context/src/gpu_bridge/image_to_gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ fn color_image_to_gpu(
emath::Rangef::new(-1.0, 1.0)
} else if let Some(shader_decoding) = shader_decoding {
match shader_decoding {
ShaderDecoding::Nv12 | ShaderDecoding::Yuy2 => emath::Rangef::new(0.0, 1.0),
ShaderDecoding::Bgr => image_data_range_heuristic(image_stats, &image_format),
}
} else {
Expand All @@ -121,10 +120,8 @@ fn color_image_to_gpu(

let color_mapper = if let Some(shader_decoding) = shader_decoding {
match shader_decoding {
// We only have 1D color maps, therefore chroma downsampled and BGR formats can't have color maps.
ShaderDecoding::Bgr | ShaderDecoding::Nv12 | ShaderDecoding::Yuy2 => {
ColorMapper::OffRGB
}
// We only have 1D color maps, therefore BGR formats can't have color maps.
ShaderDecoding::Bgr => ColorMapper::OffRGB,
}
} else if texture_format.components() == 1 {
// TODO(andreas): support colormap property
Expand Down Expand Up @@ -193,6 +190,7 @@ pub fn image_data_range_heuristic(image_stats: &ImageStats, image_format: &Image
fn image_decode_srgb_gamma_heuristic(image_stats: &ImageStats, image_format: ImageFormat) -> bool {
if let Some(pixel_format) = image_format.pixel_format {
match pixel_format {
// Have to do the conversion because we don't use an `Srgb` texture format.
PixelFormat::NV12 | PixelFormat::YUY2 => true,
}
} else {
Expand All @@ -219,34 +217,26 @@ pub fn required_shader_decode(
image_format: &ImageFormat,
) -> Option<ShaderDecoding> {
let color_model = image_format.color_model();
match image_format.pixel_format {
Some(PixelFormat::NV12) => Some(ShaderDecoding::Nv12),
Some(PixelFormat::YUY2) => Some(ShaderDecoding::Yuy2),
None => {
if color_model == ColorModel::BGR || color_model == ColorModel::BGRA {
// U8 can be converted to RGBA without the shader's help since there's a format for it.
if image_format.datatype() == ChannelDatatype::U8
&& device_caps.support_bgra_textures()
{
None
} else {
Some(ShaderDecoding::Bgr)
}
} else {
None
}

if image_format.pixel_format.is_none() && color_model == ColorModel::BGR
|| color_model == ColorModel::BGRA
{
// U8 can be converted to RGBA without the shader's help since there's a format for it.
if image_format.datatype() == ChannelDatatype::U8 && device_caps.support_bgra_textures() {
None
} else {
Some(ShaderDecoding::Bgr)
}
} else {
None
}
}

/// Creates a [`Texture2DCreationDesc`] for creating a texture from an [`ImageInfo`].
///
/// The resulting texture has requirements as describe by [`required_shader_decode`].
///
/// TODO(andreas): The consumer needs to be aware of bgr and chroma downsampling conversions.
/// It would be much better if we had a separate `re_renderer`/gpu driven conversion pipeline for this
/// which would allow us to virtually extend over wgpu's texture formats.
/// This would allow us to seamlessly support e.g. NV12 on meshes without the mesh shader having to be updated.
/// TODO(andreas): The consumer needs to be aware of bgr conversions. Other conversions are already taken care of upon upload.
pub fn texture_creation_desc_from_color_image<'a>(
device_caps: &DeviceCaps,
image: &'a ImageInfo,
Expand Down

0 comments on commit deec337

Please sign in to comment.