Always do yuv conversion in vulkan

This commit is contained in:
Patrick Nicolas
2024-02-18 16:15:55 +01:00
parent 4d99392ced
commit e385e459ee
28 changed files with 1663 additions and 1502 deletions
+75
View File
@@ -0,0 +1,75 @@
#version 450
layout(binding = 0, rgba8) uniform readonly image2D rgb;
layout(binding = 1, r8) uniform writeonly image2D luminance;
#define SEMIPLANAR
#ifdef SEMIPLANAR
layout(binding = 2, rg8) uniform writeonly image2D chroma_uv;
#else
layout(binding = 2, r8) uniform writeonly image2D chroma_u;
layout(binding = 3, r8) uniform writeonly image2D chroma_v;
#endif
layout(push_constant) uniform PushConstants
{
mat3 color_space;
}
pcs;
layout(constant_id = 0) const bool full_range = true;
layout(local_size_x = 16, local_size_y = 16) in;
vec3 rgb_to_ycbcr(vec3 color)
{
vec3 yuv = transpose(pcs.color_space) * color;
if (full_range)
{
return yuv + vec3(0, 0.5, 0.5);
}
else
{
// The matrix multiplication gives us Y in [0, 1] and Cb and Cr in [-0.5, 0.5].
// This converts to "MPEG" or "Narrow" in the range [16, 235] and [16, 240].
return vec3(
(219.0 * yuv.x + 16.0) / 256.0,
(224.0 * yuv.y + 128.0) / 256.0,
(224.0 * yuv.z + 128.0) / 256.0);
}
}
void main()
{
vec2 self_id = gl_GlobalInvocationID.xy;
ivec2 coords = ivec2(self_id.x * 2, self_id.y * 2);
ivec2 chroma_coords = coords / 2;
int j, k;
vec2 uvs[4];
for (k = 0; k < 2; k += 1)
{
for (j = 0; j < 2; j += 1)
{
ivec2 texel_coords = coords + ivec2(j, k);
vec4 texel = imageLoad(rgb, texel_coords);
vec3 yuv = rgb_to_ycbcr(texel.rgb);
imageStore(luminance, texel_coords, vec4(yuv.x));
int i = k * 2 + j;
uvs[i] = yuv.yz;
}
}
vec2 uv = mix(mix(uvs[0], uvs[1], 0.5), mix(uvs[2], uvs[3], 0.5), 0.5);
#ifdef SEMIPLANAR
imageStore(chroma_uv, chroma_coords, vec4(uv.x, uv.y, 0, 0));
#else
imageStore(chroma_u, chroma_coords, vec4(u.x));
imageStore(chroma_v, chroma_coords, vec4(v.y));
#endif
}
@@ -1,46 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#version 450
layout (binding = 0) uniform sampler2D image;
layout (location = 0) in vec2 pos;
layout (location = 0) out vec2 uv;
float gamma(float x)
{
if (x <= 0.0031308)
return 12.92 * x;
else
return 1.055 * pow(x, 1/2.4) - 0.055;
}
vec4 gamma4(vec4 x)
{
return vec4(gamma(x.r), gamma(x.g), gamma(x.b), gamma(x.a));
}
void main() {
vec4 rgba = gamma4(texture(image, pos));
uv.x = 0.5 + rgba.r * -0.1146 + rgba.g * -0.3854 + rgba.b * 0.5;
uv.y = 0.5 + rgba.r * 0.5 + rgba.g * -0.4542 + rgba.b * -0.0458;
}
@@ -1,27 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#version 450
layout (location = 0) out vec2 outUV;
void main() {
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); // [(0, 0), (2, 0), (0, 2)]
gl_Position = vec4(outUV * 2.0f + -1.0f, 0.0f, 1.0f); // [(-1, -1), (3, -1), (-1, 3)]
}
@@ -1,44 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#version 450
layout (binding = 0) uniform sampler2D image;
layout (location = 0) in vec2 pos;
layout (location = 0) out float y;
float gamma(float x)
{
if (x <= 0.0031308)
return 12.92 * x;
else
return 1.055 * pow(x, 1/2.4) - 0.055;
}
vec4 gamma4(vec4 x)
{
return vec4(gamma(x.r), gamma(x.g), gamma(x.b), gamma(x.a));
}
void main() {
vec4 rgba = gamma4(texture(image, pos));
y = rgba.r * 0.2126 + rgba.g * 0.7152 + rgba.b * 0.0722;
}