Files
WiVRn/server/compositor/shaders/foveation.comp
T

138 lines
3.9 KiB
Plaintext

/*
* WiVRn VR streaming
* Copyright (C) 2026 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/>.
*/
// Copyright 2021-2022, Collabora Ltd.
// Author: Jakob Bornecrantz <jakob@collabora.com>
// SPDX-License-Identifier: BSL-1.0
#version 460
#extension GL_KHR_shader_subgroup_basic: require
#extension GL_KHR_shader_subgroup_shuffle_relative: require
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
#define MAX_DIM 4097
#define MAX_SAMPLES 4
layout(set = 0, binding = 0) uniform sampler2D source[2];
layout(set = 0, binding = 1, std430) buffer restrict Config
{
// foveation parameters
uint x[2*MAX_DIM];
uint y[2*MAX_DIM];
// output parameters
uint alpha_width;
bool alpha;
} ubo;
layout(set = 0, binding = 2) uniform writeonly restrict image2DArray luma;
layout(set = 0, binding = 3) uniform writeonly restrict image2DArray chroma;
const mat3 color_space = mat3(
// R G B
/* Y */ 0.2126, 0.7152, 0.0722,
/* Cb*/-0.1146, -0.3854, 0.5,
/* Cr*/ 0.5 , -0.4542, -0.0458);
vec3 rgb_to_ycbcr(vec3 color)
{
return transpose(color_space) * color + vec3(0, 0.5, 0.5);
}
float from_linear_to_srgb_channel(float value)
{
if (value < 0.0031308) {
return 12.92 * value;
} else {
return 1.055 * pow(value, 1.0 / 2.4) - 0.055;
}
}
vec3 from_linear_to_srgb(vec3 linear_rgb)
{
return vec3(
from_linear_to_srgb_channel(linear_rgb.r),
from_linear_to_srgb_channel(linear_rgb.g),
from_linear_to_srgb_channel(linear_rgb.b)
);
}
void main()
{
uint iz = gl_GlobalInvocationID.z;
// In order to know how subgroup are mapped, we have to do the swizzling ourselves
// in the work group, consider pixels in the following order:
//
// 0 1 4 5 8 9 12 13
// 2 3 6 7 10 11 14 15
// 16 17 20 21 24 25 28 29
// 18 19 22 23 26 27 30 31
// ...
uint id = gl_SubgroupID * gl_SubgroupSize + gl_SubgroupInvocationID;
uint quad = id / 4;
id -= 4 * quad;
uint iy = gl_WorkGroupID.y * gl_WorkGroupSize.y + 2 * uint(quad / (gl_WorkGroupSize.y / 2)) + id / 2;
uint ymin = ubo.y[MAX_DIM * iz + iy];
uint ymax = ubo.y[MAX_DIM * iz + iy + 1];
if (ymin > ymax)
{
uint tmp = ymin;
ymin = ymax;
ymax = tmp;
}
uint ix = gl_WorkGroupID.x * gl_WorkGroupSize.x + 2 * (quad % (gl_WorkGroupSize.y / 2)) + (id % 2);
uint xmin = ubo.x[MAX_DIM * iz + ix];
uint xmax = ubo.x[MAX_DIM * iz + ix + 1];
uint xstep = (xmax - xmin + MAX_SAMPLES - 1) / MAX_SAMPLES;
uint ystep = (ymax - ymin + MAX_SAMPLES - 1) / MAX_SAMPLES;
vec4 colour = vec4(0);
uint count = 0;
for (uint y = ymin ; y < ymax; y += ystep) {
for (uint x = xmin ; x < xmax; x += xstep) {
colour += texelFetch(source[iz], ivec2(x, y), 0);
++count;
}
}
colour /= max(count, 1);
colour.xyz = rgb_to_ycbcr(from_linear_to_srgb(colour.rgb));
imageStore(luma, ivec3(ix, iy, iz), vec4(colour.x));
vec4 sum = colour + subgroupShuffleDown(colour, 1) + subgroupShuffleDown(colour, 2) + subgroupShuffleDown(colour, 3);
if (id == 0)
{
// actual chroma
imageStore(chroma, ivec3(ix/2, iy/2, iz), vec4(sum.y, sum.z, 0, 0) / 4);
if (ubo.alpha)
{
// alpha in luma
int offset = int(ubo.alpha_width * iz);
imageStore(luma, ivec3(offset + ix/2, iy/2, 2), vec4(sum.w, 0, 0, 0) / 4);
imageStore(chroma, ivec3(offset + ix/2, iy/2, 2), vec4(0.5, 0.5, 0, 0));
}
}
}