comp: Cody style shaders

This commit is contained in:
Jakob Bornecrantz 2019-10-23 11:07:33 +01:00
parent 127e5e67b4
commit 742fe05dd8
6 changed files with 74 additions and 90 deletions

View file

@ -1,10 +1,11 @@
// Copyright 2019, Collabora, Ltd. // Copyright 2019, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
// Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com> // Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
#version 450 #version 450
layout (location = 0) out vec2 outUV; layout (location = 0) out vec2 out_uv;
layout (location = 1) out int outViewIndex; layout (location = 1) out int out_view_index;
layout (binding = 2, std140) uniform UBO layout (binding = 2, std140) uniform UBO
{ {
@ -26,9 +27,12 @@ void main()
ubo_vp.rot.zw, ubo_vp.rot.zw,
}; };
outViewIndex = ubo_vp.viewport_id; out_view_index = ubo_vp.viewport_id;
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); out_uv = 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) gl_Position = vec4(rot * (out_uv * 2.0f - 1.0f), 0.0f, 1.0f);
outUV.y = 1.0 - outUV.y;
if (ubo_vp.flip_y) {
out_uv.y = 1.0 - out_uv.y;
}
} }

View file

@ -9,48 +9,37 @@
#version 450 #version 450
layout (binding = 0) uniform sampler2D texSampler; layout (binding = 0) uniform sampler2D tex_sampler;
layout (binding = 1, std140) uniform UBO layout (binding = 1, std140) uniform UBO
{ {
// Distoriton coefficients (PanoTools model) [a,b,c,d] // Distoriton coefficients (PanoTools model) [a,b,c,d]
vec4 HmdWarpParam; vec4 hmd_warp_param;
// chromatic distortion post scaling // chromatic distortion post scaling
vec4 aberr; vec4 aberr;
// Position of lens center in m (usually eye_w/2, eye_h/2) // 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) // 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) // Distortion overall scale in m (usually ~eye_w/2)
float WarpScale; float warp_scale;
} ubo; } ubo;
layout (location = 0) in vec2 in_ruv;
layout (location = 0) in vec2 in_ruv; layout (location = 1) in vec2 in_guv;
layout (location = 1) in vec2 in_guv; layout (location = 2) in vec2 in_buv;
layout (location = 2) in vec2 in_buv;
layout (location = 0) out vec4 out_color; layout (location = 0) out vec4 out_color;
void main() void main()
{ {
float r = texture(texSampler, in_ruv).x; float r = texture(tex_sampler, in_ruv).x;
float g = texture(texSampler, in_guv).y; float g = texture(tex_sampler, in_guv).y;
float b = texture(texSampler, in_buv).z; float b = texture(tex_sampler, 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
out_color = vec4(r, g, b, 1.0); out_color = vec4(r, g, b, 1.0);
} }

View file

@ -5,13 +5,6 @@
#version 450 #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 layout (binding = 2, std140) uniform ubo
{ {
vec4 rot; vec4 rot;
@ -19,6 +12,12 @@ layout (binding = 2, std140) uniform ubo
bool flip_y; bool flip_y;
} ubo_vp; } 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 out gl_PerVertex
{ {
vec4 gl_Position; vec4 gl_Position;

View file

@ -1,17 +1,17 @@
// Copyright 2019, Collabora, Ltd. // Copyright 2019, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
// Author: Jakob Bornecrantz <jakob@collabora.com> // Author: Jakob Bornecrantz <jakob@collabora.com>
#version 450 #version 450
layout (binding = 0) uniform sampler2D texSampler; layout (binding = 0) uniform sampler2D tex_sampler;
layout (location = 0) in vec2 inUV; layout (location = 0) in vec2 in_uv;
layout (location = 1) flat in int inViewIndex; layout (location = 1) flat in int in_view_index;
layout (location = 0) out vec4 out_color;
layout (location = 0) out vec4 outColor;
void main() void main()
{ {
outColor = vec4(texture(texSampler, inUV).rgb, 1.0); out_color = vec4(texture(tex_sampler, in_uv).rgb, 1.0);
} }

View file

@ -5,76 +5,68 @@
// Author: James Sarrett <jsarrett@gmail.com> // Author: James Sarrett <jsarrett@gmail.com>
// Author: Bastiaan Olij <mux213@gmail.com> // Author: Bastiaan Olij <mux213@gmail.com>
// Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com> // Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
#version 450 #version 450
layout (binding = 0) uniform sampler2D texSampler; layout (binding = 0) uniform sampler2D tex_sampler;
layout (binding = 1, std140) uniform UBO layout (binding = 1, std140) uniform UBO
{ {
// Distoriton coefficients (PanoTools model) [a,b,c,d] // Distoriton coefficients (PanoTools model) [a,b,c,d]
vec4 HmdWarpParam; vec4 hmd_warp_param;
// chromatic distortion post scaling // chromatic distortion post scaling
vec4 aberr; vec4 aberr;
// Position of lens center in m (usually eye_w/2, eye_h/2) // 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) // 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) // Distortion overall scale in m (usually ~eye_w/2)
float WarpScale; float warp_scale;
} ubo; } ubo;
layout (location = 0) in vec2 inUV; layout (location = 0) in vec2 in_uv;
layout (location = 1) flat in int inViewIndex; layout (location = 1) flat in int in_view_index;
layout (location = 0) out vec4 out_color;
layout (location = 0) out vec4 outColor;
void main() 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 // scale for distortion model
// distortion model has r=1 being the largest circle inscribed (e.g. eye_w/2) // distortion model has r=1 being the largest circle inscribed (e.g. eye_w/2)
r /= ubo.WarpScale; r /= ubo.warp_scale;
// |r|**2 // |r|**2
float r_mag = length(r); float r_mag = length(r);
// offset for which fragment is sourced // offset for which fragment is sourced
vec2 r_displaced = r * ( vec2 r_displaced = r * (
ubo.HmdWarpParam.w + ubo.hmd_warp_param.w +
ubo.HmdWarpParam.z * r_mag + ubo.hmd_warp_param.z * r_mag +
ubo.HmdWarpParam.y * r_mag * r_mag + ubo.hmd_warp_param.y * r_mag * r_mag +
ubo.HmdWarpParam.x * r_mag * r_mag * r_mag ubo.hmd_warp_param.x * r_mag * r_mag * r_mag
); );
// back to world scale // back to world scale
r_displaced *= ubo.WarpScale; r_displaced *= ubo.warp_scale;
// back to viewport co-ord // back to viewport co-ord
vec2 tcR = (ubo.LensCenter[i] + ubo.aberr.r * r_displaced) / ubo.ViewportScale; vec2 tc_t = (ubo.lens_center[i] + ubo.aberr.r * r_displaced) / ubo.viewport_scale;
vec2 tcG = (ubo.LensCenter[i] + ubo.aberr.g * r_displaced) / ubo.ViewportScale; vec2 tc_g = (ubo.lens_center[i] + ubo.aberr.g * r_displaced) / ubo.viewport_scale;
vec2 tcB = (ubo.LensCenter[i] + ubo.aberr.b * r_displaced) / ubo.ViewportScale; vec2 tc_b = (ubo.lens_center[i] + ubo.aberr.b * r_displaced) / ubo.viewport_scale;
vec3 color = vec3( vec3 color = vec3(
texture(texSampler, tcR).r, texture(tex_sampler, tc_t).r,
texture(texSampler, tcG).g, texture(tex_sampler, tc_g).g,
texture(texSampler, tcB).b texture(tex_sampler, tc_b).b
); );
#if 0 out_color = vec4(color, 1.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);
} }

View file

@ -3,9 +3,10 @@
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
// Author: Philipp Zabel <philipp.zabel@gmail.com> // Author: Philipp Zabel <philipp.zabel@gmail.com>
// Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com> // Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
#version 450 #version 450
layout (binding = 0) uniform sampler2D texSampler; layout (binding = 0) uniform sampler2D tex_sampler;
layout (binding = 1, std140) uniform UBO layout (binding = 1, std140) uniform UBO
{ {
@ -16,20 +17,19 @@ layout (binding = 1, std140) uniform UBO
float grow_for_undistort; float grow_for_undistort;
} ubo; } ubo;
layout (location = 0) in vec2 inUV; layout (location = 0) in vec2 in_uv;
layout (location = 1) flat in int inViewIndex; layout (location = 1) flat in int in_view_index;
layout (location = 0) out vec4 out_color;
layout (location = 0) out vec4 outColor;
void main() void main()
{ {
const int i = inViewIndex; const int i = in_view_index;
const vec2 factor = 0.5 / (1.0 + ubo.grow_for_undistort) const vec2 factor = 0.5 / (1.0 + ubo.grow_for_undistort)
* vec2(1.0, ubo.aspect_x_over_y); * 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.y /= ubo.aspect_x_over_y;
texCoord -= ubo.center[i].xy; texCoord -= ubo.center[i].xy;
@ -44,18 +44,18 @@ void main()
const vec2 offset = vec2(0.5); const vec2 offset = vec2(0.5);
vec2 tcR = offset + (texCoord * d.r + ubo.center[i].xy) * factor; vec2 tc_r = offset + (texCoord * d.r + ubo.center[i].xy) * factor;
vec2 tcG = offset + (texCoord * d.g + ubo.center[i].xy) * factor; vec2 tc_g = offset + (texCoord * d.g + ubo.center[i].xy) * factor;
vec2 tcB = offset + (texCoord * d.b + ubo.center[i].xy) * factor; vec2 tc_b = offset + (texCoord * d.b + ubo.center[i].xy) * factor;
vec3 color = vec3( vec3 color = vec3(
texture(texSampler, tcR).r, texture(tex_sampler, tc_r).r,
texture(texSampler, tcG).g, texture(tex_sampler, tc_g).g,
texture(texSampler, tcB).b); texture(tex_sampler, tc_b).b);
if (r2 > ubo.undistort_r2_cutoff[i]) { if (r2 > ubo.undistort_r2_cutoff[i]) {
color *= 0.125; color *= 0.125;
} }
outColor = vec4(color, 1.0); out_color = vec4(color, 1.0);
} }