mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
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.
298 lines
13 KiB
Plaintext
298 lines
13 KiB
Plaintext
#version 450
|
|
// Winlator Ludashi Built-in AMD FSR 4.0.2 Pre-Pass (Pass 0 + Feature Extraction)
|
|
// Samples game color at native rw x rh, dilates motion vectors across 3x3 closest depth
|
|
// with nearest filtering + MVJittered cancellation, runs AMD FSR 4 mu-law temporal
|
|
// reprojection + Pass-0 2x2 strided conv, and emits UFIXED_POINT_8 [1, 540, 960, 16]
|
|
// (offset=-128, scale=0.014884727) into the zero-copy AHardwareBuffer for Hexagon HTP.
|
|
layout(local_size_x = 8, local_size_y = 8) in;
|
|
|
|
layout(set = 0, binding = 0) uniform sampler2D u_color; // native color (linear)
|
|
layout(set = 0, binding = 1) uniform sampler2D u_mvec; // motion vectors (NEAREST)
|
|
layout(set = 0, binding = 2, std430) readonly buffer Weights { uint wb[]; }; // 256 weight words + 8 bias words
|
|
layout(set = 0, binding = 3, std430) readonly buffer RecPrev { uint recp[]; }; // RGBA8 @ OS_W x OS_H
|
|
layout(set = 0, binding = 4, std430) readonly buffer HistIn { uint hst[]; }; // fp16 RGBA mu-law @ OS_W x OS_H
|
|
layout(set = 0, binding = 5, std430) writeonly buffer OutQ { uint outq[]; }; // UFIXED_POINT_8 [LR_H][LR_W][16]
|
|
layout(set = 0, binding = 6, std430) writeonly buffer Repj { uint rpj[]; }; // fp16 RGBA mu-law @ OS_W x OS_H
|
|
layout(set = 0, binding = 7) uniform sampler2D u_depth; // depth buffer (NEAREST)
|
|
|
|
layout(push_constant) uniform PC {
|
|
vec2 u_jitter; // subpixel jitter in native rw x rh pixels
|
|
vec2 u_mv_scale; // scale applied to raw MV texel -> normalized UV delta
|
|
vec2 u_uv_scale; // (rw / color_tex_w, rh / color_tex_h)
|
|
vec2 u_jitter_cancel; // normalized UV jitter cancellation (0 if !MV_JITTERED)
|
|
float u_reset; // 1.0 on reset / first frame
|
|
float u_expo; // pre-exposure
|
|
uint OS_W; // 1920
|
|
uint OS_H; // 1080
|
|
uint LR_W; // 960
|
|
uint LR_H; // 540
|
|
uint rw; // native render width
|
|
uint rh; // native render height
|
|
uint flags; // bit 0: has_depth, bit 1: depth_inverted, bit 2: display_res_mv
|
|
uint pad0;
|
|
} pc;
|
|
|
|
vec3 loadHistRaw(uint x, uint y) {
|
|
uint base = (y * pc.OS_W + x) * 2u;
|
|
return vec3(unpackHalf2x16(hst[base]), unpackHalf2x16(hst[base + 1u]).x);
|
|
}
|
|
|
|
vec3 loadMulr(ivec2 p, ivec2 col_lim) {
|
|
p = clamp(p, ivec2(0), col_lim);
|
|
vec3 c = max(texelFetch(u_color, p, 0).rgb, vec3(0.0)) * pc.u_expo;
|
|
return max(vec3(0.1174) * log(vec3(1.0) + 150.0 * c), vec3(0.0));
|
|
}
|
|
|
|
float catmull(float f, uint k) {
|
|
if (k == 0u) return f * (-0.5 + f * (1.0 - 0.5 * f));
|
|
if (k == 1u) return 1.0 + f * f * (-2.5 + 1.5 * f);
|
|
if (k == 2u) return f * (0.5 + f * (2.0 - 1.5 * f));
|
|
return f * f * (-0.5 + 0.5 * f);
|
|
}
|
|
|
|
vec4 upscaled_current(uvec2 tid, ivec2 col_lim) {
|
|
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 pos_f = (vec2(tid) + vec2(0.5)) * ratio - vec2(0.5) + pc.u_jitter;
|
|
ivec2 pos_i = ivec2(floor(pos_f));
|
|
vec3 acc = vec3(0.0);
|
|
float wsum = 0.0;
|
|
float wmax = 0.0;
|
|
for (int dy = 0; dy < 2; ++dy) {
|
|
for (int dx = 0; dx < 2; ++dx) {
|
|
ivec2 pi = pos_i + ivec2(dx, dy);
|
|
vec2 d = (vec2(pi) - pos_f) * inv_ratio;
|
|
float w = exp2((dot(d, d) * (-0.5 / (0.47 * 0.47))) * 1.4426950409);
|
|
vec3 c = loadMulr(pi, col_lim);
|
|
acc += c * w;
|
|
wsum += w;
|
|
wmax = max(wmax, w);
|
|
}
|
|
}
|
|
return vec4(acc / max(wsum, 1e-6), wmax);
|
|
}
|
|
|
|
void gather_prep(float hx, float hy, out float wx[4], out float wy[4]) {
|
|
float sx = hx + 0.5; float sy = hy + 0.5;
|
|
float tx0 = floor(sx - 0.5); float ty0 = floor(sy - 0.5);
|
|
float fx = sx - (tx0 + 0.5); float fy = sy - (ty0 + 0.5);
|
|
for (int j = 0; j < 4; ++j) wx[j] = catmull(fx, uint(j));
|
|
for (int k = 0; k < 4; ++k) wy[k] = catmull(fy, uint(k));
|
|
}
|
|
|
|
vec4 recAt(ivec2 p) {
|
|
uint w = recp[uint(p.y) * pc.OS_W + uint(p.x)];
|
|
return vec4(float(w & 0xffu), float((w >> 8u) & 0xffu),
|
|
float((w >> 16u) & 0xffu), float(w >> 24u)) / 255.0;
|
|
}
|
|
|
|
vec4 recBilinear(float hx, float hy) {
|
|
float rx = clamp(hx, 0.0, float(pc.OS_W - 1u));
|
|
float ry = clamp(hy, 0.0, float(pc.OS_H - 1u));
|
|
int x0 = int(floor(rx)); int y0 = int(floor(ry));
|
|
float fx = rx - float(x0); float fy = ry - float(y0);
|
|
int x1 = min(x0 + 1, int(pc.OS_W) - 1);
|
|
int y1 = min(y0 + 1, int(pc.OS_H) - 1);
|
|
vec4 top = mix(recAt(ivec2(x0, y0)), recAt(ivec2(x1, y0)), fx);
|
|
vec4 bot = mix(recAt(ivec2(x0, y1)), recAt(ivec2(x1, y1)), fx);
|
|
return mix(top, bot, fy);
|
|
}
|
|
|
|
vec2 sampleDilatedVelocity(uvec2 p) {
|
|
ivec2 mv_sz = textureSize(u_mvec, 0);
|
|
ivec2 dep_sz = textureSize(u_depth, 0);
|
|
ivec2 r_lim = ivec2(int(pc.rw) - 1, int(pc.rh) - 1);
|
|
ivec2 np = clamp(ivec2((vec2(p) + vec2(0.5)) * vec2(pc.rw, pc.rh) / vec2(pc.LR_W, pc.LR_H)), ivec2(0), r_lim);
|
|
|
|
ivec2 best_q = np;
|
|
bool has_depth = (pc.flags & 1u) != 0u && dep_sz.x > 1 && dep_sz.y > 1;
|
|
bool dep_inv = (pc.flags & 2u) != 0u;
|
|
if (has_depth) {
|
|
ivec2 d_lim = min(r_lim, dep_sz - ivec2(1));
|
|
ivec2 dp_c = clamp(np, ivec2(0), d_lim);
|
|
float best_d = texelFetch(u_depth, dp_c, 0).r;
|
|
best_q = dp_c;
|
|
for (int dy = -1; dy <= 1; ++dy) {
|
|
for (int dx = -1; dx <= 1; ++dx) {
|
|
if (dx == 0 && dy == 0) continue;
|
|
ivec2 q = clamp(dp_c + ivec2(dx, dy), ivec2(0), d_lim);
|
|
float d = texelFetch(u_depth, q, 0).r;
|
|
bool closer = dep_inv ? (d > best_d) : (d < best_d);
|
|
if (closer) {
|
|
best_d = d;
|
|
best_q = q;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ivec2 mv_q;
|
|
if ((pc.flags & 4u) != 0u) {
|
|
// Display-resolution MVs
|
|
vec2 uv_n = (vec2(best_q) + vec2(0.5)) / vec2(pc.rw, pc.rh);
|
|
mv_q = clamp(ivec2(uv_n * vec2(mv_sz)), ivec2(0), mv_sz - ivec2(1));
|
|
} else {
|
|
ivec2 mv_lim = min(r_lim, mv_sz - ivec2(1));
|
|
mv_q = clamp(best_q, ivec2(0), mv_lim);
|
|
}
|
|
|
|
vec2 raw = texelFetch(u_mvec, mv_q, 0).xy;
|
|
return raw * pc.u_mv_scale + pc.u_jitter_cancel;
|
|
}
|
|
|
|
void feat_px(uvec2 tid, ivec2 col_lim, vec2 vel, bool mv_ok, bool onscreen, vec3 history,
|
|
out uint o0, out uint o1, out uint o2, out uint o3) {
|
|
vec4 upsc = upscaled_current(tid, col_lim);
|
|
vec3 cur = upsc.rgb;
|
|
float center_w = upsc.a;
|
|
|
|
float hx = float(tid.x) + vel.x * float(pc.OS_W);
|
|
float hy = float(tid.y) + vel.y * float(pc.OS_H);
|
|
|
|
vec4 rec = vec4(0.0);
|
|
if (pc.u_reset > 0.5) {
|
|
history = cur;
|
|
} else if (onscreen) {
|
|
rec = recBilinear(hx, hy);
|
|
}
|
|
|
|
float tsw = 0.1 * center_w;
|
|
vec3 rect = (pc.u_reset > 0.5) ? cur
|
|
: (mv_ok && onscreen ? (tsw * cur + (1.0 - tsw) * history) : cur);
|
|
|
|
float yin = cur.r * 0.2126 + cur.g * 0.7152 + cur.b * 0.0722;
|
|
float yh = rect.r * 0.2126 + rect.g * 0.7152 + rect.b * 0.0722;
|
|
float cb_in = (cur.b - yin) / 1.8556; float cr_in = (cur.r - yin) / 1.5748;
|
|
float cb_h = (rect.b - yh) / 1.8556; float cr_h = (rect.r - yh) / 1.5748;
|
|
float dist = sqrt((cb_in - cb_h) * (cb_in - cb_h) + (cr_in - cr_h) * (cr_in - cr_h) + 1e-6);
|
|
|
|
float n0 = 2.0 * yin - 1.0;
|
|
float n1 = 2.0 * yh - 1.0;
|
|
vec4 rn = 2.0 * rec - 1.0;
|
|
|
|
o0 = packHalf2x16(vec2(n0, n1));
|
|
o1 = packHalf2x16(vec2(dist, rn.x));
|
|
o2 = packHalf2x16(vec2(rn.y, rn.z));
|
|
o3 = packHalf2x16(vec2(rn.w, 0.0));
|
|
|
|
uint rbase = (tid.y * pc.OS_W + tid.x) * 2u;
|
|
rpj[rbase] = packHalf2x16(rect.xy);
|
|
rpj[rbase + 1u] = packHalf2x16(vec2(rect.z, 1.0));
|
|
}
|
|
|
|
vec2 w2(uint f, uint ky, uint kx, uint c2) {
|
|
return unpackHalf2x16(wb[f * 16u + (ky * 2u + kx) * 4u + c2]);
|
|
}
|
|
|
|
float biasAt(uint f) {
|
|
vec2 v = unpackHalf2x16(wb[256u + (f >> 1u)]);
|
|
return (f & 1u) == 0u ? v.x : v.y;
|
|
}
|
|
|
|
uint pack4u(int b0, int b1, int b2, int b3) {
|
|
return (uint(b0) & 0xffu)
|
|
| ((uint(b1) & 0xffu) << 8u)
|
|
| ((uint(b2) & 0xffu) << 16u)
|
|
| ((uint(b3) & 0xffu) << 24u);
|
|
}
|
|
|
|
// UFIXED_POINT_8 with offset = -128, scale = S_IN = 0.014884727075695992
|
|
const float RCP_S_IN = 67.18295843885989;
|
|
int quant1u(float acc, float bias) {
|
|
float v = (acc + bias) * RCP_S_IN + 128.0;
|
|
return int(clamp(roundEven(v), 0.0, 255.0));
|
|
}
|
|
|
|
void main() {
|
|
uvec2 p = gl_GlobalInvocationID.xy;
|
|
if (p.x >= pc.LR_W || p.y >= pc.LR_H) return;
|
|
|
|
ivec2 col_sz = textureSize(u_color, 0);
|
|
ivec2 col_lim = min(ivec2(int(pc.rw) - 1, int(pc.rh) - 1), col_sz - ivec2(1));
|
|
|
|
uint x0 = 2u * p.x, x1 = 2u * p.x + 1u;
|
|
uint y0 = 2u * p.y, y1 = 2u * p.y + 1u;
|
|
|
|
vec2 vel = sampleDilatedVelocity(p);
|
|
bool mv_ok = !isnan(vel.x) && !isnan(vel.y) && !isinf(vel.x) && !isinf(vel.y)
|
|
&& abs(vel.x) < 4.0 && abs(vel.y) < 4.0;
|
|
|
|
vec3 histA = vec3(0.0), histB = vec3(0.0), histC = vec3(0.0), histD = vec3(0.0);
|
|
bool onsA = false, onsB = false, onsC = false, onsD = false;
|
|
if (pc.u_reset <= 0.5 && mv_ok) {
|
|
float hxA = float(x0) + vel.x * float(pc.OS_W);
|
|
float hyA = float(y0) + vel.y * float(pc.OS_H);
|
|
float hxB = float(x1) + vel.x * float(pc.OS_W);
|
|
float hyB = float(y0) + vel.y * float(pc.OS_H);
|
|
float hxC = float(x0) + vel.x * float(pc.OS_W);
|
|
float hyC = float(y1) + vel.y * float(pc.OS_H);
|
|
float hxD = float(x1) + vel.x * float(pc.OS_W);
|
|
float hyD = float(y1) + vel.y * float(pc.OS_H);
|
|
onsA = hxA >= 0.0 && hxA <= float(pc.OS_W - 1u) && hyA >= 0.0 && hyA <= float(pc.OS_H - 1u);
|
|
onsB = hxB >= 0.0 && hxB <= float(pc.OS_W - 1u) && hyB >= 0.0 && hyB <= float(pc.OS_H - 1u);
|
|
onsC = hxC >= 0.0 && hxC <= float(pc.OS_W - 1u) && hyC >= 0.0 && hyC <= float(pc.OS_H - 1u);
|
|
onsD = hxD >= 0.0 && hxD <= float(pc.OS_W - 1u) && hyD >= 0.0 && hyD <= float(pc.OS_H - 1u);
|
|
if (onsA || onsB || onsC || onsD) {
|
|
float wxA[4], wyA[4], wxB[4], wyB[4], wxC[4], wyC[4], wxD[4], wyD[4];
|
|
gather_prep(hxA, hyA, wxA, wyA);
|
|
gather_prep(hxB, hyB, wxB, wyB);
|
|
gather_prep(hxC, hyC, wxC, wyC);
|
|
gather_prep(hxD, hyD, wxD, wyD);
|
|
int bx = int(floor(hxA)) - 1;
|
|
int by = int(floor(hyA)) - 1;
|
|
vec3 hA = vec3(0.0), hB = vec3(0.0), hC = vec3(0.0), hD = vec3(0.0);
|
|
float tA = 0.0, tB = 0.0, tC = 0.0, tD = 0.0;
|
|
for (int r = 0; r < 5; ++r) {
|
|
uint cy = uint(clamp(by + r, 0, int(pc.OS_H) - 1));
|
|
for (int c = 0; c < 5; ++c) {
|
|
vec3 v = loadHistRaw(uint(clamp(bx + c, 0, int(pc.OS_W) - 1)), cy);
|
|
if (r < 4 && c < 4 && onsA) { float w = wyA[r] * wxA[c]; tA += w; hA += v * w; }
|
|
if (r < 4 && c > 0 && onsB) { float w = wyB[r] * wxB[c - 1]; tB += w; hB += v * w; }
|
|
if (r > 0 && c < 4 && onsC) { float w = wyC[r - 1] * wxC[c]; tC += w; hC += v * w; }
|
|
if (r > 0 && c > 0 && onsD) { float w = wyD[r - 1] * wxD[c - 1]; tD += w; hD += v * w; }
|
|
}
|
|
}
|
|
if (onsA && abs(tA) > 1e-6) histA = max(hA / tA, vec3(0.0));
|
|
if (onsB && abs(tB) > 1e-6) histB = max(hB / tB, vec3(0.0));
|
|
if (onsC && abs(tC) > 1e-6) histC = max(hC / tC, vec3(0.0));
|
|
if (onsD && abs(tD) > 1e-6) histD = max(hD / tD, vec3(0.0));
|
|
}
|
|
}
|
|
|
|
uint A0, A1, A2, A3, B0, B1, B2, B3, C0, C1, C2, C3, D0, D1, D2, D3;
|
|
feat_px(uvec2(x0, y0), col_lim, vel, mv_ok, onsA, histA, A0, A1, A2, A3);
|
|
feat_px(uvec2(x1, y0), col_lim, vel, mv_ok, onsB, histB, B0, B1, B2, B3);
|
|
feat_px(uvec2(x0, y1), col_lim, vel, mv_ok, onsC, histC, C0, C1, C2, C3);
|
|
feat_px(uvec2(x1, y1), col_lim, vel, mv_ok, onsD, histD, D0, D1, D2, D3);
|
|
|
|
vec2 c00[4], c10[4], c01[4], c11[4];
|
|
c00[0] = unpackHalf2x16(A0); c00[1] = unpackHalf2x16(A1);
|
|
c00[2] = unpackHalf2x16(A2); c00[3] = unpackHalf2x16(A3);
|
|
c10[0] = unpackHalf2x16(B0); c10[1] = unpackHalf2x16(B1);
|
|
c10[2] = unpackHalf2x16(B2); c10[3] = unpackHalf2x16(B3);
|
|
c01[0] = unpackHalf2x16(C0); c01[1] = unpackHalf2x16(C1);
|
|
c01[2] = unpackHalf2x16(C2); c01[3] = unpackHalf2x16(C3);
|
|
c11[0] = unpackHalf2x16(D0); c11[1] = unpackHalf2x16(D1);
|
|
c11[2] = unpackHalf2x16(D2); c11[3] = unpackHalf2x16(D3);
|
|
|
|
int q[16];
|
|
for (uint f = 0u; f < 16u; ++f) {
|
|
vec2 acc = vec2(0.0);
|
|
acc += w2(f, 0u, 0u, 0u) * c00[0] + w2(f, 0u, 0u, 1u) * c00[1]
|
|
+ w2(f, 0u, 0u, 2u) * c00[2] + w2(f, 0u, 0u, 3u) * c00[3];
|
|
acc += w2(f, 1u, 0u, 0u) * c10[0] + w2(f, 1u, 0u, 1u) * c10[1]
|
|
+ w2(f, 1u, 0u, 2u) * c10[2] + w2(f, 1u, 0u, 3u) * c10[3];
|
|
acc += w2(f, 0u, 1u, 0u) * c01[0] + w2(f, 0u, 1u, 1u) * c01[1]
|
|
+ w2(f, 0u, 1u, 2u) * c01[2] + w2(f, 0u, 1u, 3u) * c01[3];
|
|
acc += w2(f, 1u, 1u, 0u) * c11[0] + w2(f, 1u, 1u, 1u) * c11[1]
|
|
+ w2(f, 1u, 1u, 2u) * c11[2] + w2(f, 1u, 1u, 3u) * c11[3];
|
|
q[f] = quant1u(acc.x + acc.y, biasAt(f));
|
|
}
|
|
|
|
uint base = (p.y * pc.LR_W + p.x) * 4u;
|
|
outq[base + 0u] = pack4u(q[0], q[1], q[2], q[3]);
|
|
outq[base + 1u] = pack4u(q[4], q[5], q[6], q[7]);
|
|
outq[base + 2u] = pack4u(q[8], q[9], q[10], q[11]);
|
|
outq[base + 3u] = pack4u(q[12], q[13], q[14], q[15]);
|
|
}
|