Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C
2.7 KB
Raw Blame
#version 450
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 RafaelNGP
// A decoded H.264 picture in NV12 (BT.709, limited range, 4:2:0 with the default
// chroma siting: on the even luma columns, between two luma rows) to RGB,
// debanded first.
layout(local_size_x = 8, local_size_y = 8) in;
layout(binding = 0) uniform sampler2D lumaTex;    // linear R8_UNORM
layout(binding = 1) uniform sampler2D chromaUVTex;// linear R8G8_UNORM
layout(binding = 2, rgba8) uniform writeonly image2D rgbOut;
layout(push_constant) uniform Constants {
    uint iterations;  // 0: no deband
    float threshold;  // largest step smoothed, in 0..1 units (divided by the iteration)
    float radius;     // in luma pixels (times the iteration)
    float grain;      // luma noise amplitude
    uint frame;       // moves the random pattern each frame
} pc;

vec2 lumaSize, chromaSize;

// Y, U, V at a luma position (pixel units, centres at .0).
vec3 yuvAt(vec2 q) {
    float y = texture(lumaTex, (q + 0.5) / lumaSize).r;
    // Chroma sample i sits on luma column 2i and halfway between luma rows
    // 2j and 2j + 1: in chroma texels, luma (x, y) is at (x / 2, y / 2 - 0.25).
    vec2 c = (vec2(q.x * 0.5, q.y * 0.5 - 0.25) + 0.5) / chromaSize;
    vec2 uv = texture(chromaUVTex, c).rg;
    return vec3(y, uv.r, uv.g);
}

float hash(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * 0.1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}

void main() {
    ivec2 p = ivec2(gl_GlobalInvocationID.xy);
    ivec2 size = imageSize(rgbOut);
    if (p.x >= size.x || p.y >= size.y) return;
    lumaSize = vec2(textureSize(lumaTex, 0));
    chromaSize = vec2(textureSize(chromaUVTex, 0));
    vec2 q = vec2(p);
    vec3 yuv = yuvAt(q);
    if (pc.iterations > 0u) {
        vec2 seed = q + float(pc.frame % 64u) * vec2(17.0, 31.0);
        for (uint i = 1u; i <= pc.iterations; ++i) {
            float fi = float(i);
            float dist = pc.radius * fi * hash(seed + fi * 13.7);
            float angle = 6.2831853 * hash(seed.yx + fi * 41.3);
            vec2 d = vec2(cos(angle), sin(angle)) * dist;
            vec3 avg = 0.25 * (yuvAt(q + d) + yuvAt(q - d) + yuvAt(q + vec2(-d.y, d.x)) + yuvAt(q + vec2(d.y, -d.x)));
            vec3 diff = abs(yuv - avg);
            yuv = mix(avg, yuv, greaterThan(diff, vec3(pc.threshold / fi)));
        }
        yuv.x += (hash(seed * 1.37 + 7.0) - 0.5) * pc.grain;
    }
    float l = (yuv.x - 16.0 / 255.0) * (255.0 / 219.0);
    float u = yuv.y - 128.0 / 255.0, v = yuv.z - 128.0 / 255.0;
    float s = 255.0 / 224.0;
    vec3 rgb = vec3(l + 1.5748 * s * v, l - 0.1873 * s * u - 0.4681 * s * v, l + 1.8556 * s * u);
    imageStore(rgbOut, p, vec4(clamp(rgb, 0.0, 1.0), 1.0));
}