Skip to content

Commit

Permalink
client gfx refactor (11): wgpu stream
Browse files Browse the repository at this point in the history
  • Loading branch information
zmerp authored and failboat committed Aug 17, 2024
1 parent ae03f6f commit 31e627e
Show file tree
Hide file tree
Showing 12 changed files with 401 additions and 343 deletions.
6 changes: 3 additions & 3 deletions alvr/client_core/resources/lobby_quad.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct VertexOutput {
@group(0) @binding(1) var hud_sampler: sampler;

struct PushConstant {
transform: mat4x4f, // alignment
transform: mat4x4f,
object_type: u32,
floor_side: f32,
}
Expand All @@ -28,7 +28,7 @@ fn vertex_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {

@fragment
fn fragment_main(@location(0) uv: vec2f) -> @location(0) vec4f {
if pc.object_type == 0 {
if pc.object_type == 0 { // Ground
let world_xz = (uv - 0.5) * pc.floor_side;

let ground_center = vec3f(0.0, 0.0, 0.0);
Expand Down Expand Up @@ -73,7 +73,7 @@ fn fragment_main(@location(0) uv: vec2f) -> @location(0) vec4f {
}

return vec4f(out_color, 1.0);
} else {
} else { // HUD
let mask = textureSample(hud_texture, hud_sampler, uv).a;

return vec4<f32>(1.0, 1.0, 1.0, mask);
Expand Down
60 changes: 60 additions & 0 deletions alvr/client_core/resources/stream.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// todo: use expression directly when supported in naga
const DIV12: f32 = 0.0773993808;// 1.0 / 12.92
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;

var<push_constant> view_idx: u32;

struct VertexOutput {
@builtin(position) position: vec4f,
@location(0) uv: vec2f,
}

@group(0) @binding(0) var stream_texture: texture_2d<f32>;
@group(0) @binding(1) var stream_sampler: sampler;

@vertex
fn vertex_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
var result: VertexOutput;

let screen_uv = vec2f(f32(vertex_index & 1), f32(vertex_index >> 1));
result.position = vec4f((screen_uv - vec2f(0.5, 0.5)) * 2.0, 0.0, 1.0);
result.uv = vec2f((screen_uv.x + f32(view_idx)) / 2.0, screen_uv.y);

return result;
}

@fragment
fn fragment_main(@location(0) uv: vec2f) -> @location(0) vec4f {
var result: vec3f = textureSample(stream_texture, stream_sampler, uv).rgb;

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

if ENABLE_SRGB_CORRECTION {
let condition = vec3f(f32(result.r < THRESHOLD), f32(result.g < THRESHOLD), f32(result.b < THRESHOLD));
let lowValues = result * DIV12;
let highValues = pow((result + vec3f(0.055)) * DIV1, GAMMA);
result = condition * lowValues + (1.0 - condition) * highValues;
}

if ENCODING_GAMMA != 0.0 {
let enc_condition = vec3f(f32(result.r < 0.0), f32(result.g < 0.0), f32(result.b < 0.0));
let enc_lowValues = result;
let enc_highValues = pow(result, vec3f(ENCODING_GAMMA));
result = enc_condition * enc_lowValues + (1.0 - enc_condition) * enc_highValues;
}

return vec4f(result, 1.0);
}
41 changes: 0 additions & 41 deletions alvr/client_core/resources/stream_fragment.glsl

This file was deleted.

11 changes: 0 additions & 11 deletions alvr/client_core/resources/stream_vertex.glsl

This file was deleted.

1 change: 1 addition & 0 deletions alvr/client_core/src/c_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ pub unsafe extern "C" fn alvr_start_stream_opengl(config: AlvrStreamConfig) {
GRAPHICS_CONTEXT.with_borrow(|c| c.as_ref().unwrap().clone()),
view_resolution,
swapchain_textures,
glow::RGBA8,
foveated_encoding,
true,
false, // TODO: limited range fix config
Expand Down
25 changes: 16 additions & 9 deletions alvr/client_core/src/graphics/lobby.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{GraphicsContext, RenderViewInput};
use super::{GraphicsContext, SDR_FORMAT};
use alvr_common::{
glam::{Mat4, UVec2, Vec3, Vec4},
Fov, Pose,
Expand All @@ -17,7 +17,7 @@ use wgpu::{
PrimitiveTopology, PushConstantRange, RenderPass, RenderPassColorAttachment,
RenderPassDescriptor, RenderPipeline, RenderPipelineDescriptor, SamplerBindingType,
SamplerDescriptor, ShaderModuleDescriptor, ShaderStages, StoreOp, Texture, TextureAspect,
TextureFormat, TextureSampleType, TextureView, TextureViewDimension, VertexState,
TextureSampleType, TextureView, TextureViewDimension, VertexState,
};

const FLOOR_SIDE: f32 = 300.0;
Expand Down Expand Up @@ -76,7 +76,7 @@ fn projection_from_fov(fov: Fov) -> Mat4 {
.transpose()
}

fn pipeline(
fn create_pipeline(
device: &Device,
label: &str,
bind_group_layouts: &[&BindGroupLayout],
Expand Down Expand Up @@ -113,7 +113,7 @@ fn pipeline(
entry_point: "fragment_main",
compilation_options: Default::default(),
targets: &[Some(ColorTargetState {
format: TextureFormat::Rgba8Unorm,
format: SDR_FORMAT,
blend: Some(BlendState {
color: BlendComponent {
src_factor: BlendFactor::SrcAlpha,
Expand All @@ -133,6 +133,12 @@ fn pipeline(
})
}

pub struct RenderViewInput {
pub pose: Pose,
pub fov: Fov,
pub swapchain_index: u32,
}

pub struct LobbyRenderer {
context: Rc<GraphicsContext>,
quad_pipeline: RenderPipeline,
Expand All @@ -151,7 +157,8 @@ impl LobbyRenderer {
) -> Self {
let device = &context.device;

let hud_texture = super::create_texture(device, UVec2::ONE * HUD_TEXTURE_SIDE as u32);
let hud_texture =
super::create_texture(device, UVec2::ONE * HUD_TEXTURE_SIDE as u32, SDR_FORMAT);

let bind_group_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: None,
Expand All @@ -175,7 +182,7 @@ impl LobbyRenderer {
],
});

let quad_pipeline = pipeline(
let quad_pipeline = create_pipeline(
device,
"lobby_quad",
&[&bind_group_layout],
Expand All @@ -184,7 +191,7 @@ impl LobbyRenderer {
PrimitiveTopology::TriangleStrip,
);

let line_pipeline = pipeline(
let line_pipeline = create_pipeline(
device,
"lobby_line",
&[],
Expand Down Expand Up @@ -217,8 +224,8 @@ impl LobbyRenderer {
});

let render_targets = [
super::create_gl_swapchain(device, &swapchain_textures[0], view_resolution),
super::create_gl_swapchain(device, &swapchain_textures[1], view_resolution),
super::create_gl_swapchain(device, &swapchain_textures[0], view_resolution, SDR_FORMAT),
super::create_gl_swapchain(device, &swapchain_textures[1], view_resolution, SDR_FORMAT),
];

let this = Self {
Expand Down
Loading

0 comments on commit 31e627e

Please sign in to comment.