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

Full range wgpu partial fix #2597

Merged
merged 7 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 9 additions & 1 deletion alvr/client_core/resources/staging_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@

uniform samplerExternalOES tex;

// Convert from limited colors to full
const float LIMITED_MIN = 0.06274509803; // 16.0 / 255.0
const float LIMITED_MAX = 0.92156862745; // 235.0 / 255.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should restore the explicit fractions (in the comments). The decimal numbers were used because wgpu doesn't support expressions for constants.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be set now


in vec2 uv;
out vec4 out_color;

void main() {
out_color = texture(tex, uv);
vec3 color = texture(tex, uv).rgb;
#ifdef FIX_LIMITED_RANGE
color = LIMITED_MIN + ((LIMITED_MAX - LIMITED_MIN) * color);
#endif
out_color = vec4(color, 1.0);
}
10 changes: 0 additions & 10 deletions alvr/client_core/resources/stream.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ const DIV1: f32 = 0.94786729857; // 1.0 / 1.055
const THRESHOLD: f32 = 0.04045;
const GAMMA: vec3f = vec3f(2.4);

// Convert from limited colors to full
const LIMITED_MIN: f32 = 0.06274509803; // 16.0 / 255.0
const LIMITED_MAX: f32 = 0.92156862745; // 235.0 / 255.0

override FIX_LIMITED_RANGE: bool;
override ENABLE_SRGB_CORRECTION: bool;
override ENCODING_GAMMA: f32;

Expand Down Expand Up @@ -118,11 +113,6 @@ fn fragment_main(@location(0) uv: vec2f) -> @location(0) vec4f {

var color = textureSample(stream_texture, stream_sampler, corrected_uv).rgb;

if FIX_LIMITED_RANGE {
// For some reason, the encoder shifts full-range color into the negatives and over one.
color = LIMITED_MIN + ((LIMITED_MAX - LIMITED_MIN) * color);
}

if ENABLE_SRGB_CORRECTION {
let condition = vec3f(f32(color.r < THRESHOLD), f32(color.g < THRESHOLD), f32(color.b < THRESHOLD));
let lowValues = color * DIV12;
Expand Down
12 changes: 11 additions & 1 deletion alvr/client_core/src/graphics/staging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,24 @@ impl StagingRenderer {
context: Rc<GraphicsContext>,
staging_textures: [gl::Texture; 2],
view_resolution: UVec2,
fix_limited_range: bool,
) -> Self {
let gl = &context.gl_context;
context.make_current();

// Add #defines into the shader after the first line
let mut frag_lines: Vec<&str> = include_str!("../../resources/staging_fragment.glsl")
.lines()
.collect();
if fix_limited_range {
frag_lines.insert(1, "#line 0 1\n#define FIX_LIMITED_RANGE");
}
let frag_str = frag_lines.join("\n");
Comment on lines +70 to +77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kinda obscure but doesn't matter too much, we are going to remove the option for v21 and force full range


let program = create_program(
gl,
include_str!("../../resources/staging_vertex.glsl"),
include_str!("../../resources/staging_fragment.glsl"),
frag_str.as_str(),
);

unsafe {
Expand Down
2 changes: 1 addition & 1 deletion alvr/client_core/src/graphics/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ impl StreamRenderer {
"ENABLE_SRGB_CORRECTION".into(),
enable_srgb_correction.into(),
),
("FIX_LIMITED_RANGE".into(), fix_limited_range.into()),
("ENCODING_GAMMA".into(), encoding_gamma.into()),
]);

Expand Down Expand Up @@ -203,6 +202,7 @@ impl StreamRenderer {
Rc::clone(&context),
staging_textures_gl.try_into().unwrap(),
staging_resolution,
fix_limited_range,
);

Self {
Expand Down
4 changes: 3 additions & 1 deletion alvr/client_openxr/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const DECODER_MAX_TIMEOUT_MULTIPLIER: f32 = 0.8;
pub struct ParsedStreamConfig {
pub view_resolution: UVec2,
pub refresh_rate_hint: f32,
pub use_full_range: bool,
pub encoding_gamma: f32,
pub enable_hdr: bool,
pub passthrough: Option<PassthroughMode>,
Expand All @@ -50,6 +51,7 @@ impl ParsedStreamConfig {
Self {
view_resolution: config.negotiated_config.view_resolution,
refresh_rate_hint: config.negotiated_config.refresh_rate_hint,
use_full_range: config.negotiated_config.use_full_range,
encoding_gamma: config.negotiated_config.encoding_gamma,
enable_hdr: config.negotiated_config.enable_hdr,
passthrough: config.settings.video.passthrough.as_option().cloned(),
Expand Down Expand Up @@ -180,7 +182,7 @@ impl StreamContext {
format,
config.foveated_encoding_config.clone(),
platform != Platform::Lynx && !((platform.is_pico()) && config.enable_hdr),
!config.enable_hdr,
config.use_full_range && !config.enable_hdr, // TODO: figure out why HDR doesn't need the limited range hackfix in staging?
config.encoding_gamma,
config.passthrough.clone(),
);
Expand Down
4 changes: 4 additions & 0 deletions alvr/packets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub struct NegotiatedStreamingConfig {
// This is needed to detect when to use SteamVR hand trackers. This does NOT imply if multimodal
// input is supported
pub use_multimodal_protocol: bool,
pub use_full_range: bool,
pub encoding_gamma: f32,
pub enable_hdr: bool,
pub wired: bool,
Expand Down Expand Up @@ -173,6 +174,8 @@ pub fn decode_stream_config(packet: &StreamConfigPacket) -> Result<StreamConfig>
.unwrap_or_else(|_| settings.video.foveated_encoding.enabled());
let use_multimodal_protocol =
json::from_value(negotiated_json["use_multimodal_protocol"].clone()).unwrap_or(false);
let use_full_range = json::from_value(negotiated_json["use_full_range"].clone())
.unwrap_or(settings.video.encoder_config.use_full_range);
let encoding_gamma = json::from_value(negotiated_json["encoding_gamma"].clone()).unwrap_or(1.0);
let enable_hdr = json::from_value(negotiated_json["enable_hdr"].clone()).unwrap_or(false);
let wired = json::from_value(negotiated_json["wired"].clone())?;
Expand All @@ -186,6 +189,7 @@ pub fn decode_stream_config(packet: &StreamConfigPacket) -> Result<StreamConfig>
game_audio_sample_rate,
enable_foveated_encoding,
use_multimodal_protocol,
use_full_range,
encoding_gamma,
enable_hdr,
wired,
Expand Down
Loading