Skip to content

Commit 80d78cd

Browse files
Full range wgpu partial fix (#2597)
* Full range HDR works? * Fix full range for non-HDR * Cleanup * Adjust this note * Fix CI * Fix server * Return fractions
1 parent 08e1509 commit 80d78cd

File tree

7 files changed

+29
-14
lines changed

7 files changed

+29
-14
lines changed

alvr/client_core/resources/staging_fragment.glsl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33

44
uniform samplerExternalOES tex;
55

6+
// Convert from limited colors to full
7+
const float LIMITED_MIN = 16.0 / 255.0;
8+
const float LIMITED_MAX = 235.0 / 255.0;
9+
610
in vec2 uv;
711
out vec4 out_color;
812

913
void main() {
10-
out_color = texture(tex, uv);
14+
vec3 color = texture(tex, uv).rgb;
15+
#ifdef FIX_LIMITED_RANGE
16+
color = LIMITED_MIN + ((LIMITED_MAX - LIMITED_MIN) * color);
17+
#endif
18+
out_color = vec4(color, 1.0);
1119
}

alvr/client_core/resources/stream.wgsl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ const DIV1: f32 = 0.94786729857; // 1.0 / 1.055
44
const THRESHOLD: f32 = 0.04045;
55
const GAMMA: vec3f = vec3f(2.4);
66

7-
// Convert from limited colors to full
8-
const LIMITED_MIN: f32 = 0.06274509803; // 16.0 / 255.0
9-
const LIMITED_MAX: f32 = 0.92156862745; // 235.0 / 255.0
10-
11-
override FIX_LIMITED_RANGE: bool;
127
override ENABLE_SRGB_CORRECTION: bool;
138
override ENCODING_GAMMA: f32;
149

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

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

121-
if FIX_LIMITED_RANGE {
122-
// For some reason, the encoder shifts full-range color into the negatives and over one.
123-
color = LIMITED_MIN + ((LIMITED_MAX - LIMITED_MIN) * color);
124-
}
125-
126116
if ENABLE_SRGB_CORRECTION {
127117
let condition = vec3f(f32(color.r < THRESHOLD), f32(color.g < THRESHOLD), f32(color.b < THRESHOLD));
128118
let lowValues = color * DIV12;

alvr/client_core/src/graphics/staging.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,24 @@ impl StagingRenderer {
6262
context: Rc<GraphicsContext>,
6363
staging_textures: [gl::Texture; 2],
6464
view_resolution: UVec2,
65+
fix_limited_range: bool,
6566
) -> Self {
6667
let gl = &context.gl_context;
6768
context.make_current();
6869

70+
// Add #defines into the shader after the first line
71+
let mut frag_lines: Vec<&str> = include_str!("../../resources/staging_fragment.glsl")
72+
.lines()
73+
.collect();
74+
if fix_limited_range {
75+
frag_lines.insert(1, "#line 0 1\n#define FIX_LIMITED_RANGE");
76+
}
77+
let frag_str = frag_lines.join("\n");
78+
6979
let program = create_program(
7080
gl,
7181
include_str!("../../resources/staging_vertex.glsl"),
72-
include_str!("../../resources/staging_fragment.glsl"),
82+
frag_str.as_str(),
7383
);
7484

7585
unsafe {

alvr/client_core/src/graphics/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ impl StreamRenderer {
8484
"ENABLE_SRGB_CORRECTION".into(),
8585
enable_srgb_correction.into(),
8686
),
87-
("FIX_LIMITED_RANGE".into(), fix_limited_range.into()),
8887
("ENCODING_GAMMA".into(), encoding_gamma.into()),
8988
]);
9089

@@ -203,6 +202,7 @@ impl StreamRenderer {
203202
Rc::clone(&context),
204203
staging_textures_gl.try_into().unwrap(),
205204
staging_resolution,
205+
fix_limited_range,
206206
);
207207

208208
Self {

alvr/client_openxr/src/stream.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const DECODER_MAX_TIMEOUT_MULTIPLIER: f32 = 0.8;
3333
pub struct ParsedStreamConfig {
3434
pub view_resolution: UVec2,
3535
pub refresh_rate_hint: f32,
36+
pub use_full_range: bool,
3637
pub encoding_gamma: f32,
3738
pub enable_hdr: bool,
3839
pub passthrough: Option<PassthroughMode>,
@@ -50,6 +51,7 @@ impl ParsedStreamConfig {
5051
Self {
5152
view_resolution: config.negotiated_config.view_resolution,
5253
refresh_rate_hint: config.negotiated_config.refresh_rate_hint,
54+
use_full_range: config.negotiated_config.use_full_range,
5355
encoding_gamma: config.negotiated_config.encoding_gamma,
5456
enable_hdr: config.negotiated_config.enable_hdr,
5557
passthrough: config.settings.video.passthrough.as_option().cloned(),
@@ -180,7 +182,7 @@ impl StreamContext {
180182
format,
181183
config.foveated_encoding_config.clone(),
182184
platform != Platform::Lynx && !((platform.is_pico()) && config.enable_hdr),
183-
!config.enable_hdr,
185+
config.use_full_range && !config.enable_hdr, // TODO: figure out why HDR doesn't need the limited range hackfix in staging?
184186
config.encoding_gamma,
185187
config.passthrough.clone(),
186188
);

alvr/packets/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ pub struct NegotiatedStreamingConfig {
129129
// This is needed to detect when to use SteamVR hand trackers. This does NOT imply if multimodal
130130
// input is supported
131131
pub use_multimodal_protocol: bool,
132+
pub use_full_range: bool,
132133
pub encoding_gamma: f32,
133134
pub enable_hdr: bool,
134135
pub wired: bool,
@@ -173,6 +174,8 @@ pub fn decode_stream_config(packet: &StreamConfigPacket) -> Result<StreamConfig>
173174
.unwrap_or_else(|_| settings.video.foveated_encoding.enabled());
174175
let use_multimodal_protocol =
175176
json::from_value(negotiated_json["use_multimodal_protocol"].clone()).unwrap_or(false);
177+
let use_full_range = json::from_value(negotiated_json["use_full_range"].clone())
178+
.unwrap_or(settings.video.encoder_config.use_full_range);
176179
let encoding_gamma = json::from_value(negotiated_json["encoding_gamma"].clone()).unwrap_or(1.0);
177180
let enable_hdr = json::from_value(negotiated_json["enable_hdr"].clone()).unwrap_or(false);
178181
let wired = json::from_value(negotiated_json["wired"].clone())?;
@@ -186,6 +189,7 @@ pub fn decode_stream_config(packet: &StreamConfigPacket) -> Result<StreamConfig>
186189
game_audio_sample_rate,
187190
enable_foveated_encoding,
188191
use_multimodal_protocol,
192+
use_full_range,
189193
encoding_gamma,
190194
enable_hdr,
191195
wired,

alvr/server_core/src/connection.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ fn connection_pipeline(
766766
game_audio_sample_rate,
767767
enable_foveated_encoding,
768768
use_multimodal_protocol: streaming_caps.multimodal_protocol,
769+
use_full_range,
769770
encoding_gamma,
770771
enable_hdr,
771772
wired,

0 commit comments

Comments
 (0)