Xbox Cloud Gaming app for PS5 Homebrew
C
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2026 RafaelNGP
"""Anime4K's mpv hooks (extern/anime4k, MIT) as Vulkan compute shaders.
Every convolution pass of Anime4K_Upscale_CNN_x2_<size>.glsl becomes
src/display/shaders/a4k_<size>_conv<N>.comp: same weights and body, its
texture reads (`<name>_texOff`) turned into clamped imageLoad()s, and the
result stored to a 16-bit float feature map. The depth-to-space pass is
not generated: a4k_d2s.comp writes it straight into the 4K picture.
Then run tools/gen-shaders.sh.
"""
import pathlib
import re
import sys
root = pathlib.Path(__file__).resolve().parent.parent
size = sys.argv[1] if len(sys.argv) > 1 else "S"
# "restore": Anime4K_Restore_CNN_S.glsl, four passes at the picture's size;
# the last adds its result to the picture and writes it back in place
# (a4k_r_conv3.comp: each pixel reads and writes only itself there).
restore = size == "restore"
src = (root / "extern/anime4k" / ("Anime4K_Restore_CNN_S.glsl" if restore else f"Anime4K_Upscale_CNN_x2_{size}.glsl")).read_text()
license_head = src[: src.index("//!DESC")]
passes = []
for block in src.split("//!DESC")[1:]:
lines = block.splitlines()
desc = lines[0].strip()
binds = [l.split()[1] for l in lines if l.startswith("//!BIND")]
save = next(l.split()[1] for l in lines if l.startswith("//!SAVE"))
body = "\n".join(l for l in lines[1:] if not l.startswith("//!"))
passes.append((desc, binds, save, body))
out_dir = root / "src/display/shaders"
generated = []
for n, (desc, binds, save, body) in enumerate(passes):
if "Depth-to-Space" in desc:
continue
if restore and binds[0] == "MAIN" and len(binds) == 2:
# The residual: features in, result + picture back into the picture.
name = binds[1]
body = body.replace(f"{name}_texOff(", "load_input(").replace("MAIN_tex(MAIN_pos)", "imageLoad(mainImage, pixel)")
shader = f"""#version 450
{license_head.strip()}
// Generated by tools/gen-anime4k.py from {desc} (inputs MAIN, {name}); do not edit.
layout(local_size_x = 8, local_size_y = 8) in;
layout(binding = 0, rgba16f) uniform readonly image2D inputImage;
layout(binding = 1, rgba8) uniform image2D mainImage;
ivec2 pixel;
vec4 load_input(vec2 offset) {{
ivec2 size = imageSize(inputImage);
return imageLoad(inputImage, clamp(pixel + ivec2(offset), ivec2(0), size - 1));
}}
{body.strip()}
void main() {{
pixel = ivec2(gl_GlobalInvocationID.xy);
ivec2 size = imageSize(mainImage);
if (pixel.x >= size.x || pixel.y >= size.y) return;
imageStore(mainImage, pixel, vec4(clamp(hook().rgb, 0.0, 1.0), 1.0));
}}
"""
path = out_dir / f"a4k_r_conv{len(generated)}.comp"
path.write_text(shader)
generated.append(path.name)
continue
if len(binds) != 1:
sys.exit(f"{desc}: expected one input, got {binds}")
name = binds[0]
# mpv: <name>_texOff(vec2(x, y)) samples the input x, y pixels away.
body = body.replace(f"{name}_texOff(", "load_input(")
shader = f"""#version 450
{license_head.strip()}
// Generated by tools/gen-anime4k.py from {desc} (input {name}); do not edit.
layout(local_size_x = 8, local_size_y = 8) in;
layout(binding = 0, {"rgba8" if name == "MAIN" else "rgba16f"}) uniform readonly image2D inputImage;
layout(binding = 1, rgba16f) uniform writeonly image2D outputImage;
ivec2 pixel;
vec4 load_input(vec2 offset) {{
ivec2 size = imageSize(inputImage);
return imageLoad(inputImage, clamp(pixel + ivec2(offset), ivec2(0), size - 1));
}}
{body.strip()}
void main() {{
pixel = ivec2(gl_GlobalInvocationID.xy);
ivec2 size = imageSize(outputImage);
if (pixel.x >= size.x || pixel.y >= size.y) return;
imageStore(outputImage, pixel, hook());
}}
"""
path = out_dir / (f"a4k_r_conv{len(generated)}.comp" if restore else f"a4k_{size.lower()}_conv{len(generated)}.comp")
path.write_text(shader)
generated.append(path.name)
print(" ".join(generated))