Files
DroidDeck/tools/upscale/layer/shaders/fsr4_builtin_post_b.comp
T
The412Banner 7972c18e50 upscale: import the NeuralScale Vulkan upscaler layer sources unchanged
Verbatim copy of app/src/main/cpp/fsr4npu/ (C sources, headers, shaders,
weight pack scripts) from michaelisomer-cyber/Winlator-NeuralScale at
a0ab6b0f770e1a6786b4e68def46b4edb8baafd0 (GPL-3.0, fork of
StevenMXZ/Winlator-Ludashi). Not built yet; the DroidDeck port follows.
2026-11-29 22:18:41 -05:00

93 lines
3.1 KiB
Plaintext

#version 450
// Winlator Ludashi Built-in AMD FSR 4.0.2 Post-Pass B (RCAS + inverse mu-law -> RGBA16F image)
layout(local_size_x = 8, local_size_y = 8) in;
layout(set = 0, binding = 0, std430) readonly buffer InC { uint inc[]; };
layout(set = 0, binding = 1, rgba16f) writeonly uniform image2D u_out_img;
layout(push_constant) uniform PC {
vec2 u_jitter;
vec2 u_uv_scale;
float bfloor;
float expo;
float sharp;
uint OS_W;
uint OS_H;
uint rw;
uint rh;
uint pad0;
} pc;
const float FSR_RCAS_LIMIT = 0.25 - 1.0 / 16.0;
vec3 removeMuLaw(vec3 rgb) {
return max((exp(8.51788 * rgb) - 1.0) / 150.0, vec3(0.0));
}
shared uint s_t[10 * 10 * 2];
vec3 load3T(ivec2 p, ivec2 base) {
int lx = clamp(clamp(p.x, 0, int(pc.OS_W) - 1) - base.x, 0, 9);
int ly = clamp(clamp(p.y, 0, int(pc.OS_H) - 1) - base.y, 0, 9);
uint idx = (uint(ly) * 10u + uint(lx)) * 2u;
return max(vec3(unpackHalf2x16(s_t[idx]), unpackHalf2x16(s_t[idx + 1u]).x), vec3(0.0));
}
float rcpMed(float x) {
float b = intBitsToFloat(0x7ef19fff - floatBitsToInt(x));
return b * (-b * x + 2.0);
}
void main() {
ivec2 base = ivec2(gl_WorkGroupID.xy) * 8 - 1;
for (uint i = gl_LocalInvocationIndex; i < 200u; i += 64u) {
uint t = i >> 1; uint w = i & 1u;
ivec2 p = ivec2(clamp(base.x + int(t % 10u), 0, int(pc.OS_W) - 1),
clamp(base.y + int(t / 10u), 0, int(pc.OS_H) - 1));
s_t[i] = inc[(uint(p.y) * pc.OS_W + uint(p.x)) * 2u + w];
}
barrier();
uvec2 tid = gl_GlobalInvocationID.xy;
if (tid.x >= pc.OS_W || tid.y >= pc.OS_H) return;
ivec2 sp = ivec2(tid);
vec3 b = load3T(sp + ivec2(0, -1), base);
vec3 d = load3T(sp + ivec2(-1, 0), base);
vec3 e = load3T(sp, base);
vec3 f = load3T(sp + ivec2(1, 0), base);
vec3 h = load3T(sp + ivec2(0, 1), base);
float bL = b.b * 0.5 + (b.r * 0.5 + b.g);
float dL = d.b * 0.5 + (d.r * 0.5 + d.g);
float eL = e.b * 0.5 + (e.r * 0.5 + e.g);
float fL = f.b * 0.5 + (f.r * 0.5 + f.g);
float hL = h.b * 0.5 + (h.r * 0.5 + h.g);
float nz = 0.25 * bL + 0.25 * dL + 0.25 * fL + 0.25 * hL - eL;
float lmin = min(min(bL, min(dL, eL)), min(fL, hL));
float lmax = max(max(bL, max(dL, eL)), max(fL, hL));
nz = clamp(abs(nz) * rcpMed(max(lmax - lmin, 1e-9)), 0.0, 1.0);
nz = -0.5 * nz + 1.0;
vec3 mn4 = min(min(b, min(d, f)), h);
vec3 mx4 = max(max(b, max(d, f)), h);
float lll = clamp(eL / max(min(min(bL, dL), min(fL, hL)), 1e-9), 0.0, 1.0);
vec3 hitMin = mn4 / max(4.0 * mx4, vec3(1e-6)) * lll;
vec3 den4 = 4.0 * mn4 - 4.0;
vec3 strip = step(abs(den4), vec3(0.0));
vec3 hitMax = (1.0 - mx4) / (den4 - strip * vec3(1e-9));
hitMax = mix(hitMax, vec3(-1e9), strip);
vec3 lobe3 = max(-hitMin, hitMax);
float lobe = max(-FSR_RCAS_LIMIT, min(max(max(lobe3.r, lobe3.g), lobe3.b), 0.0));
lobe *= exp2(2.0 * pc.sharp - 2.0);
lobe *= nz;
float rcpL = rcpMed(4.0 * lobe + 1.0);
vec3 pix = (lobe * (b + d + h + f) + e) * rcpL;
vec3 fc = clamp(removeMuLaw(pix) / max(pc.expo, 1e-6), vec3(0.0), vec3(64000.0));
imageStore(u_out_img, sp, vec4(fc, 1.0));
}