-
Notifications
You must be signed in to change notification settings - Fork 369
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
Introduce image data conversion pipeline, taking over existing YUV conversions #7640
Merged
Merged
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c205d2a
first sketch of `transfer_image_data_to_texture`
Wumpf 2724b17
Make all texture manager operations go through new image-data-to-text…
Wumpf 00e1f67
chroma subsampling conversion setup
Wumpf fe9dedd
chroma subsampling converter now passes all the relevant data now to …
Wumpf a853a2e
chroma conversion shader has now parity with rectangle shader chroma …
Wumpf deec337
remove chroma subsampling shader decode from rectangle shader
Wumpf 3b81ae4
cleanup
Wumpf 4df5bc6
typo fixes
Wumpf f3efc9d
roll back unneeded changes
Wumpf 1c31cff
improve commentrary on expected_data_texture_format
Wumpf 7f3c538
use ERROR_RGBA
Wumpf 5dbfca5
todo item references
Wumpf d3552eb
collapse width & height into width_height in various places, clarify …
Wumpf 7e5963a
rename all things chroma subsampling
Wumpf 2c54928
resolve "this"-style comments
Wumpf 88be036
error improvements
Wumpf 97a61ef
more profiling scopes
Wumpf 0099cf9
remove unused/renamed file remnant
Wumpf 6b52176
typos and doc string fixes
Wumpf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
crates/viewer/re_renderer/shader/conversions/chroma_subsampling_converter.wgsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#import <../types.wgsl> | ||
#import <../screen_triangle_vertex.wgsl> | ||
|
||
struct UniformBuffer { | ||
format: u32, | ||
primaries: u32, | ||
target_texture_size: vec2u, | ||
}; | ||
|
||
@group(0) @binding(0) | ||
var<uniform> uniform_buffer: UniformBuffer; | ||
|
||
@group(0) @binding(1) | ||
var input_texture: texture_2d<u32>; | ||
|
||
|
||
const FORMAT_Y_UV = 0u; | ||
const FORMAT_YUYV16 = 1u; | ||
|
||
const PRIMARIES_BT601 = 0u; | ||
const PRIMARIES_BT709 = 1u; | ||
|
||
|
||
/// Returns sRGB from YUV color. | ||
/// | ||
/// This conversion mirrors the function in `crates/store/re_types/src/datatypes/tensor_data_ext.rs` | ||
/// | ||
/// Specifying the color standard should be exposed in the future [#3541](https://github.com/rerun-io/rerun/pull/3541) | ||
fn srgb_from_yuv(yuv: vec3f, primaries: u32) -> vec3f { | ||
// rescale YUV values | ||
let y = (yuv[0] - 16.0) / 219.0; | ||
let u = (yuv[1] - 128.0) / 224.0; | ||
let v = (yuv[2] - 128.0) / 224.0; | ||
|
||
var rgb: vec3f; | ||
|
||
switch (primaries) { | ||
// BT.601 (aka. SDTV, aka. Rec.601). wiki: https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion | ||
// Also note according to https://en.wikipedia.org/wiki/SRGB#sYCC_extended-gamut_transformation | ||
// > Although the RGB color primaries are based on BT.709, | ||
// > the equations for transformation from sRGB to sYCC and vice versa are based on BT.601. | ||
case PRIMARIES_BT601: { | ||
rgb.r = y + 1.402 * v; | ||
rgb.g = y - 0.344 * u - 0.714 * v; | ||
rgb.b = y + 1.772 * u; | ||
} | ||
|
||
// BT.709 (aka. HDTV, aka. Rec.709). wiki: https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.709_conversion | ||
case PRIMARIES_BT709: { | ||
rgb.r = y + 1.575 * v; | ||
rgb.g = y - 0.187 * u - 0.468 * v; | ||
rgb.b = y + 1.856 * u; | ||
} | ||
|
||
default: { | ||
rgb = vec3f(1.0, 0.0, 1.0); // Magenta to indicate trouble | ||
Wumpf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
return clamp(rgb, vec3f(0.0), vec3f(1.0)); | ||
} | ||
|
||
/// Extracts YUV data from a chroma subsampling encoded texture at specific coordinates. | ||
/// | ||
/// See also `enum ChromaSubsamplingPixelFormat` in `chroma_subsampling_converter.rs for a specification of | ||
/// the expected data layout. | ||
fn decode_chroma_subsampling_format_to_yuv(format: u32, texture: texture_2d<u32>, coords: vec2f) -> vec3f { | ||
let texture_dim = vec2f(textureDimensions(texture).xy); | ||
var yuv: vec3f; | ||
|
||
switch (format) { | ||
case FORMAT_Y_UV: { | ||
let uv_offset = u32(floor(texture_dim.y / 1.5)); | ||
let uv_row = u32(coords.y / 2); | ||
var uv_col = u32(coords.x / 2) * 2u; | ||
|
||
yuv[0] = f32(textureLoad(texture, vec2u(coords), 0).r); | ||
yuv[1] = f32(textureLoad(texture, vec2u(u32(uv_col), uv_offset + uv_row), 0).r); | ||
yuv[2] = f32(textureLoad(texture, vec2u((u32(uv_col) + 1u), uv_offset + uv_row), 0).r); | ||
} | ||
|
||
case FORMAT_YUYV16: { | ||
// texture is 2 * width * height | ||
// every 4 bytes is 2 pixels | ||
let uv_row = u32(coords.y); | ||
// multiply by 2 because the width is multiplied by 2 | ||
let y_col = u32(coords.x) * 2u; | ||
yuv[0] = f32(textureLoad(texture, vec2u(y_col, uv_row), 0).r); | ||
|
||
// at odd pixels we're in the second half of the yuyu block, offset back by 2 | ||
let uv_col = y_col - u32(coords.x % 2) * 2u; | ||
yuv[1] = f32(textureLoad(texture, vec2u(uv_col + 1u, uv_row), 0).r); | ||
yuv[2] = f32(textureLoad(texture, vec2u(uv_col + 3u, uv_row), 0).r); | ||
} | ||
|
||
default: { | ||
yuv = vec3f(0.0, 0.0, 0.0); | ||
} | ||
} | ||
|
||
return yuv; | ||
} | ||
|
||
@fragment | ||
fn fs_main(in: FragmentInput) -> @location(0) vec4f { | ||
let coords = vec2f(uniform_buffer.target_texture_size) * in.texcoord; | ||
|
||
let yuv = decode_chroma_subsampling_format_to_yuv(uniform_buffer.format, input_texture, coords); | ||
let rgb = srgb_from_yuv(yuv, uniform_buffer.primaries); | ||
|
||
return vec4f(rgb, 1.0); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
Comment on lines
-6
to
-8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the expectation is that this is no longer the case now. |
||
|
||
fn is_magnifying(pixel_coord: vec2f) -> bool { | ||
return fwidth(pixel_coord.x) < 1.0; | ||
|
@@ -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; | ||
|
@@ -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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wat, wgsl supports
switch
!? NICE! That could be useful incrates/viewer/re_renderer/shader/rectangle_fs.wgsl
too 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
learned that today as well 😄. Followed a hunch there - probably I've seen it in the meanwhile somewhere.
I think it didn't back when we started. But Naga & Tint (Dawn's compiler) are happy with it so full steam ahead!