From 742fe05dd82b9c67c5902b9443e0b2470fea7071 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Wed, 23 Oct 2019 11:07:33 +0100 Subject: [PATCH] comp: Cody style shaders --- src/xrt/compositor/shaders/distortion.vert | 18 ++++--- src/xrt/compositor/shaders/mesh.frag | 37 +++++--------- src/xrt/compositor/shaders/mesh.vert | 13 +++-- src/xrt/compositor/shaders/none.frag | 12 ++--- src/xrt/compositor/shaders/panotools.frag | 56 ++++++++++------------ src/xrt/compositor/shaders/vive.frag | 28 +++++------ 6 files changed, 74 insertions(+), 90 deletions(-) diff --git a/src/xrt/compositor/shaders/distortion.vert b/src/xrt/compositor/shaders/distortion.vert index 81b07fa82..f5a4d08f3 100644 --- a/src/xrt/compositor/shaders/distortion.vert +++ b/src/xrt/compositor/shaders/distortion.vert @@ -1,10 +1,11 @@ // Copyright 2019, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 // Author: Lubosz Sarnecki + #version 450 -layout (location = 0) out vec2 outUV; -layout (location = 1) out int outViewIndex; +layout (location = 0) out vec2 out_uv; +layout (location = 1) out int out_view_index; layout (binding = 2, std140) uniform UBO { @@ -26,9 +27,12 @@ void main() ubo_vp.rot.zw, }; - outViewIndex = ubo_vp.viewport_id; - outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); - gl_Position = vec4(rot * (outUV * 2.0f - 1.0f), 0.0f, 1.0f); - if (ubo_vp.flip_y) - outUV.y = 1.0 - outUV.y; + out_view_index = ubo_vp.viewport_id; + out_uv = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); + + gl_Position = vec4(rot * (out_uv * 2.0f - 1.0f), 0.0f, 1.0f); + + if (ubo_vp.flip_y) { + out_uv.y = 1.0 - out_uv.y; + } } diff --git a/src/xrt/compositor/shaders/mesh.frag b/src/xrt/compositor/shaders/mesh.frag index 7417edb2f..96496dbc8 100644 --- a/src/xrt/compositor/shaders/mesh.frag +++ b/src/xrt/compositor/shaders/mesh.frag @@ -9,48 +9,37 @@ #version 450 -layout (binding = 0) uniform sampler2D texSampler; +layout (binding = 0) uniform sampler2D tex_sampler; + layout (binding = 1, std140) uniform UBO { // Distoriton coefficients (PanoTools model) [a,b,c,d] - vec4 HmdWarpParam; + vec4 hmd_warp_param; // chromatic distortion post scaling vec4 aberr; // Position of lens center in m (usually eye_w/2, eye_h/2) - vec2 LensCenter[2]; + vec2 lens_center[2]; // Scale from texture co-ords to m (usually eye_w, eye_h) - vec2 ViewportScale; + vec2 viewport_scale; // Distortion overall scale in m (usually ~eye_w/2) - float WarpScale; + float warp_scale; } ubo; - -layout (location = 0) in vec2 in_ruv; -layout (location = 1) in vec2 in_guv; -layout (location = 2) in vec2 in_buv; - +layout (location = 0) in vec2 in_ruv; +layout (location = 1) in vec2 in_guv; +layout (location = 2) in vec2 in_buv; layout (location = 0) out vec4 out_color; + void main() { - float r = texture(texSampler, in_ruv).x; - float g = texture(texSampler, in_guv).y; - float b = texture(texSampler, in_buv).z; - -#if 0 - if (in_ruv.x < 0.0 || in_ruv.x > 1.0 || in_ruv.y < 0.0 || in_ruv.y > 1.0) { - color = vec3(1.0, 0.0, 1.0); - } else { - float t = floor(in_ruv.x * 16) + floor(in_ruv.y * 16); - bool isEven = mod(t, 2.0) == 0.0; - // color = color * float(isEven); - color = vec3(isEven, isEven, isEven); - } -#endif + float r = texture(tex_sampler, in_ruv).x; + float g = texture(tex_sampler, in_guv).y; + float b = texture(tex_sampler, in_buv).z; out_color = vec4(r, g, b, 1.0); } diff --git a/src/xrt/compositor/shaders/mesh.vert b/src/xrt/compositor/shaders/mesh.vert index be622f321..6ac094c85 100644 --- a/src/xrt/compositor/shaders/mesh.vert +++ b/src/xrt/compositor/shaders/mesh.vert @@ -5,13 +5,6 @@ #version 450 -layout (location = 0) in vec4 in_pos_ruv; -layout (location = 1) in vec4 in_guv_buv; - -layout (location = 0) out vec2 out_ruv; -layout (location = 1) out vec2 out_guv; -layout (location = 2) out vec2 out_buv; - layout (binding = 2, std140) uniform ubo { vec4 rot; @@ -19,6 +12,12 @@ layout (binding = 2, std140) uniform ubo bool flip_y; } ubo_vp; +layout (location = 0) in vec4 in_pos_ruv; +layout (location = 1) in vec4 in_guv_buv; +layout (location = 0) out vec2 out_ruv; +layout (location = 1) out vec2 out_guv; +layout (location = 2) out vec2 out_buv; + out gl_PerVertex { vec4 gl_Position; diff --git a/src/xrt/compositor/shaders/none.frag b/src/xrt/compositor/shaders/none.frag index df2ab5b8a..ba4e08d2d 100644 --- a/src/xrt/compositor/shaders/none.frag +++ b/src/xrt/compositor/shaders/none.frag @@ -1,17 +1,17 @@ // Copyright 2019, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 // Author: Jakob Bornecrantz + #version 450 -layout (binding = 0) uniform sampler2D texSampler; +layout (binding = 0) uniform sampler2D tex_sampler; -layout (location = 0) in vec2 inUV; -layout (location = 1) flat in int inViewIndex; - -layout (location = 0) out vec4 outColor; +layout (location = 0) in vec2 in_uv; +layout (location = 1) flat in int in_view_index; +layout (location = 0) out vec4 out_color; void main() { - outColor = vec4(texture(texSampler, inUV).rgb, 1.0); + out_color = vec4(texture(tex_sampler, in_uv).rgb, 1.0); } diff --git a/src/xrt/compositor/shaders/panotools.frag b/src/xrt/compositor/shaders/panotools.frag index 388179908..7e35e26e9 100644 --- a/src/xrt/compositor/shaders/panotools.frag +++ b/src/xrt/compositor/shaders/panotools.frag @@ -5,76 +5,68 @@ // Author: James Sarrett // Author: Bastiaan Olij // Author: Lubosz Sarnecki + #version 450 -layout (binding = 0) uniform sampler2D texSampler; +layout (binding = 0) uniform sampler2D tex_sampler; layout (binding = 1, std140) uniform UBO { // Distoriton coefficients (PanoTools model) [a,b,c,d] - vec4 HmdWarpParam; + vec4 hmd_warp_param; // chromatic distortion post scaling vec4 aberr; // Position of lens center in m (usually eye_w/2, eye_h/2) - vec2 LensCenter[2]; + vec2 lens_center[2]; // Scale from texture co-ords to m (usually eye_w, eye_h) - vec2 ViewportScale; + vec2 viewport_scale; // Distortion overall scale in m (usually ~eye_w/2) - float WarpScale; + float warp_scale; } ubo; -layout (location = 0) in vec2 inUV; -layout (location = 1) flat in int inViewIndex; - -layout (location = 0) out vec4 outColor; +layout (location = 0) in vec2 in_uv; +layout (location = 1) flat in int in_view_index; +layout (location = 0) out vec4 out_color; void main() { - const int i = inViewIndex; + const int i = in_view_index; - vec2 r = inUV * ubo.ViewportScale - ubo.LensCenter[i]; + vec2 r = in_uv * ubo.viewport_scale - ubo.lens_center[i]; // scale for distortion model // distortion model has r=1 being the largest circle inscribed (e.g. eye_w/2) - r /= ubo.WarpScale; + r /= ubo.warp_scale; // |r|**2 float r_mag = length(r); // offset for which fragment is sourced vec2 r_displaced = r * ( - ubo.HmdWarpParam.w + - ubo.HmdWarpParam.z * r_mag + - ubo.HmdWarpParam.y * r_mag * r_mag + - ubo.HmdWarpParam.x * r_mag * r_mag * r_mag + ubo.hmd_warp_param.w + + ubo.hmd_warp_param.z * r_mag + + ubo.hmd_warp_param.y * r_mag * r_mag + + ubo.hmd_warp_param.x * r_mag * r_mag * r_mag ); // back to world scale - r_displaced *= ubo.WarpScale; + r_displaced *= ubo.warp_scale; // back to viewport co-ord - vec2 tcR = (ubo.LensCenter[i] + ubo.aberr.r * r_displaced) / ubo.ViewportScale; - vec2 tcG = (ubo.LensCenter[i] + ubo.aberr.g * r_displaced) / ubo.ViewportScale; - vec2 tcB = (ubo.LensCenter[i] + ubo.aberr.b * r_displaced) / ubo.ViewportScale; + vec2 tc_t = (ubo.lens_center[i] + ubo.aberr.r * r_displaced) / ubo.viewport_scale; + vec2 tc_g = (ubo.lens_center[i] + ubo.aberr.g * r_displaced) / ubo.viewport_scale; + vec2 tc_b = (ubo.lens_center[i] + ubo.aberr.b * r_displaced) / ubo.viewport_scale; vec3 color = vec3( - texture(texSampler, tcR).r, - texture(texSampler, tcG).g, - texture(texSampler, tcB).b + texture(tex_sampler, tc_t).r, + texture(tex_sampler, tc_g).g, + texture(tex_sampler, tc_b).b ); -#if 0 - // No need to do this as the texture is clamped to black. - // Distortion cut-off. - if (tcG.x < 0.0 || tcG.x > 1.0 || tcG.y < 0.0 || tcG.y > 1.0) { - color *= 0.125; - } -#endif - - outColor = vec4(color, 1.0); + out_color = vec4(color, 1.0); } diff --git a/src/xrt/compositor/shaders/vive.frag b/src/xrt/compositor/shaders/vive.frag index cce47cf06..48063475c 100644 --- a/src/xrt/compositor/shaders/vive.frag +++ b/src/xrt/compositor/shaders/vive.frag @@ -3,9 +3,10 @@ // SPDX-License-Identifier: BSL-1.0 // Author: Philipp Zabel // Author: Lubosz Sarnecki + #version 450 -layout (binding = 0) uniform sampler2D texSampler; +layout (binding = 0) uniform sampler2D tex_sampler; layout (binding = 1, std140) uniform UBO { @@ -16,20 +17,19 @@ layout (binding = 1, std140) uniform UBO float grow_for_undistort; } ubo; -layout (location = 0) in vec2 inUV; -layout (location = 1) flat in int inViewIndex; - -layout (location = 0) out vec4 outColor; +layout (location = 0) in vec2 in_uv; +layout (location = 1) flat in int in_view_index; +layout (location = 0) out vec4 out_color; void main() { - const int i = inViewIndex; + const int i = in_view_index; const vec2 factor = 0.5 / (1.0 + ubo.grow_for_undistort) * vec2(1.0, ubo.aspect_x_over_y); - vec2 texCoord = 2.0 * inUV - vec2(1.0); + vec2 texCoord = 2.0 * in_uv - vec2(1.0); texCoord.y /= ubo.aspect_x_over_y; texCoord -= ubo.center[i].xy; @@ -44,18 +44,18 @@ void main() const vec2 offset = vec2(0.5); - vec2 tcR = offset + (texCoord * d.r + ubo.center[i].xy) * factor; - vec2 tcG = offset + (texCoord * d.g + ubo.center[i].xy) * factor; - vec2 tcB = offset + (texCoord * d.b + ubo.center[i].xy) * factor; + vec2 tc_r = offset + (texCoord * d.r + ubo.center[i].xy) * factor; + vec2 tc_g = offset + (texCoord * d.g + ubo.center[i].xy) * factor; + vec2 tc_b = offset + (texCoord * d.b + ubo.center[i].xy) * factor; vec3 color = vec3( - texture(texSampler, tcR).r, - texture(texSampler, tcG).g, - texture(texSampler, tcB).b); + texture(tex_sampler, tc_r).r, + texture(tex_sampler, tc_g).g, + texture(tex_sampler, tc_b).b); if (r2 > ubo.undistort_r2_cutoff[i]) { color *= 0.125; } - outColor = vec4(color, 1.0); + out_color = vec4(color, 1.0); }