Files
DroidDeck/tools/upscale/layer/shaders/fsr4_builtin_post_a.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

116 lines
5.0 KiB
Plaintext

#version 450
// Winlator Ludashi Built-in AMD FSR 4.0.2 Post-Pass A
// Decodes UFIXED_POINT_8 [1, 1080, 1920, 8] (offset=-128, scale=0.30321157) from the
// zero-copy AHardwareBuffer produced by Hexagon HTP v81/v79 NPU, runs anisotropic 3x3
// Gaussian reconstruction directly over the game's FULL NATIVE rw x rh color buffer
// (preserving 100% of Quality/Balanced/DLAA detail!), applies the neural temporal blend
// with zero ghost-trail blend floor, and writes updated mu-law history + RGBA8 state.
layout(local_size_x = 8, local_size_y = 8) in;
layout(set = 0, binding = 0) uniform sampler2D u_color; // saved Frame N-1 native rw x rh color
layout(set = 0, binding = 1, std430) readonly buffer NpuIn { uint npu[]; }; // UFIXED_POINT_8 [OS_H][OS_W][8]
layout(set = 0, binding = 2, std430) readonly buffer Repj { uint rpj[]; }; // fp16 RGBA mu-law @ OS_W x OS_H
layout(set = 0, binding = 3, std430) writeonly buffer HistOut { uint hist[]; }; // fp16 RGBA mu-law @ OS_W x OS_H
layout(set = 0, binding = 4, std430) writeonly buffer RecOut { uint recout[]; }; // RGBA8 @ OS_W x OS_H
layout(push_constant) uniform PC {
vec2 u_jitter; // subpixel jitter in native rw x rh pixels (Frame N-1)
vec2 u_uv_scale;
float bfloor; // 0.0 (no forced ghosting trail)
float expo; // Frame N-1 pre-exposure
float sharp;
uint OS_W; // 1920
uint OS_H; // 1080
uint rw; // actual native render width of Frame N-1
uint rh; // actual native render height of Frame N-1
uint pad0;
} pc;
const float S_OUT = 0.303211569786;
int ubx(uint w, uint sh) {
return int((w >> sh) & 0xffu) - 128;
}
vec3 loadRepj(uvec2 p) {
uint base = (p.y * pc.OS_W + p.x) * 2u;
return vec3(unpackHalf2x16(rpj[base]), unpackHalf2x16(rpj[base + 1u]).x);
}
vec3 loadColorMu(ivec2 p, ivec2 lim) {
p = clamp(p, ivec2(0), lim);
vec3 c = max(texelFetch(u_color, p, 0).rgb, vec3(0.0)) * pc.expo;
return max(vec3(0.1174) * log(vec3(1.0) + 150.0 * c), vec3(0.0));
}
void main() {
uvec2 tid = gl_GlobalInvocationID.xy;
if (tid.x >= pc.OS_W || tid.y >= pc.OS_H) return;
ivec2 col_sz = textureSize(u_color, 0);
ivec2 lim = min(ivec2(int(pc.rw) - 1, int(pc.rh) - 1), col_sz - ivec2(1));
uint nbase = (tid.y * pc.OS_W + tid.x) * 2u;
uint w0 = npu[nbase];
uint w1 = npu[nbase + 1u];
float p0 = float(ubx(w0, 0u)) * S_OUT;
float p1 = float(ubx(w0, 8u)) * S_OUT;
float p2 = float(ubx(w0, 16u)) * S_OUT;
float p3 = float(ubx(w0, 24u)) * S_OUT;
vec4 rv = vec4(float(ubx(w1, 0u)), float(ubx(w1, 8u)),
float(ubx(w1, 16u)), float(ubx(w1, 24u))) * S_OUT;
vec4 rec = vec4(1.0) / (vec4(1.0) + exp2(-rv * 1.4426950409));
uvec4 rb = uvec4(clamp(floor(rec * 255.0 + 0.5), vec4(0.0), vec4(255.0)));
recout[tid.y * pc.OS_W + tid.x] =
(rb.x & 0xffu) | ((rb.y & 0xffu) << 8u) | ((rb.z & 0xffu) << 16u) | ((rb.w & 0xffu) << 24u);
float ep0 = exp2(p0 * 1.4426950409);
float em0 = exp2(-p0 * 1.4426950409);
float correlation = (ep0 - em0) / max(ep0 + em0, 1e-6);
float sfx = 2.0 / (1.0 + exp2(-p1 * 1.4426950409));
float sfy = 2.0 / (1.0 + exp2(-p2 * 1.4426950409));
// Exact native rw x rh subpixel mapping (matches 2x formula identically when rw=960, rh=540,
// and preserves full 720p/1080p detail on Quality / Balanced / DLAA!)
vec2 ratio = vec2(pc.rw, pc.rh) / vec2(pc.OS_W, pc.OS_H);
vec2 inv_ratio = vec2(pc.OS_W, pc.OS_H) / vec2(pc.rw, pc.rh);
vec2 lr_f = (vec2(tid) + vec2(0.5)) * ratio - vec2(0.5) + pc.u_jitter;
ivec2 lr_i = ivec2(roundEven(lr_f));
vec3 up = vec3(0.0);
float tw = 0.0;
vec3 n_min = vec3(1e9);
vec3 n_max = vec3(-1e9);
for (int dy = -1; dy <= 1; ++dy) {
float yd = (float(lr_i.y + dy) - lr_f.y) * inv_ratio.y;
for (int dx = -1; dx <= 1; ++dx) {
float xd = (float(lr_i.x + dx) - lr_f.x) * inv_ratio.x;
float gw = exp2((-0.5 / (0.47 * 0.47)) * 1.4426950409 *
(xd * xd * sfx * sfx + 2.0 * correlation * (xd * yd) * sfx * sfy +
yd * yd * sfy * sfy));
vec3 sample_m = loadColorMu(lr_i + ivec2(dx, dy), lim);
n_min = min(n_min, sample_m);
n_max = max(n_max, sample_m);
tw += gw;
up += sample_m * gw;
}
}
up /= max(tw, 1e-6);
float blend = 1.0 / (1.0 + exp2(-p3 * 1.4426950409));
vec3 rp = loadRepj(tid);
// Soft neighborhood guard in mu-law space prevents residual ghost trails on fast-moving
// disoccluded edges even if a game sends noisy/missing MVs.
vec3 margin = max((n_max - n_min) * 0.25, vec3(0.015));
vec3 rp_clamped = clamp(rp, n_min - margin, n_max + margin);
float bf = (max(max(rp.r, rp.g), rp.b) <= 0.0) ? 0.0 : pc.bfloor;
blend = max(blend, bf);
vec3 model_color = mix(up, rp_clamped, blend);
uint obase = (tid.y * pc.OS_W + tid.x) * 2u;
hist[obase] = packHalf2x16(model_color.xy);
hist[obase + 1u] = packHalf2x16(vec2(model_color.z, 1.0));
}