Files
DroidDeck/tools/upscale/layer/shaders/sgsr2_convert.comp
T
The412Banner 74ece6c854 upscale: port Winlator-NeuralScale r5 (non-root parts)
Layer and bridges updated from Winlator-NeuralScale 71c140d9 (michaelisomer-cyber, GPL-3.0):
- full FSR 4.1 on the NPU (S8 graph, U8 fallback), compiled on the device per HTP arch
- ghosting fix: blank far depth when a game sends none, binding 11 bound with 62
- FSR 4.1.2 Lite S8/U8 zero-copy graphs
- Snapdragon GSR 2 on the GPU (new engine sgsr2_gpu)
- D3D11 DLSS through DXVK's interop (nvngx D3D11 entry points)

Left out on purpose: upstream's root-only performance/thermal manager, GPU clock floor and
bypass charging. Kept from DroidDeck: NPU detection by the app (DROIDDECK_UPSCALE_NPU, any HTP
generation incl. the phone's own QNN libraries) instead of upstream's SoC list, DD env names and
log tags, Intel XeSS 3 (runtime still fetched from a0ab6b0f, upstream dropped it in cb3ae04e).

FSR 4.0 on the NPU is gone upstream; the engine id becomes fsr4_1_full_npu and saved
fsr4_0_npu choices map to it. The full-FSR-4-on-Turnip guard is now opt-in
(DROIDDECK_UPSCALE_FULL_OFF_ON_TURNIP=1), since upstream's binding-11 fix targets that black frame.
Data files re-pinned to 71c140d9 (81 core + 8 NPU, sha256 checked).
2026-12-04 18:52:36 -05:00

139 lines
5.9 KiB
Plaintext

#version 450
// Qualcomm Snapdragon Game Super Resolution 2 (SGSR 2) — 3-Pass Compute: Pass 1 (Convert)
// Based directly on official quic/snapdragon-gsr sgsr/v2/include/glsl_3_pass_cs/sgsr2_convert.comp
// (Copyright (c) 2024, Qualcomm Innovation Center, Inc. SPDX-License-Identifier: BSD-3-Clause)
// Adapted for Winlator Vulkan 1.3 PC-game DLSS/FSR inputs (full-screen MVs, reverse-Z, reactive mask, exposure, subrects).
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
layout(std140, set = 0, binding = 0) uniform Params {
uvec2 renderSize;
uvec2 displaySize;
vec2 renderSizeRcp;
vec2 displaySizeRcp;
vec2 jitterOffset;
vec2 mvScale;
vec2 jitterCancel;
float preExposure;
float cameraFovAngleHor;
uint flags; // bit0: has_depth, 1: depth_inv, 2: disp_res_mv, 3: reset, 4: has_reactive, 5: has_expo_tex
uint col_base; // subrect_x | (subrect_y << 16)
uint dep_base;
uint mv_base;
uint react_base;
float sharpness;
vec2 pad0;
} params;
layout(set = 0, binding = 1) uniform sampler2D InputColor;
layout(set = 0, binding = 2) uniform sampler2D InputDepth;
layout(set = 0, binding = 3) uniform sampler2D InputVelocity;
layout(set = 0, binding = 4) uniform sampler2D InputReactive;
layout(set = 0, binding = 5) uniform sampler2D InputExposure;
layout(set = 0, binding = 6, r32ui) writeonly uniform uimage2D YCoCgColor;
layout(set = 0, binding = 7, rgba16f) writeonly uniform image2D MotionDepthAlphaBuffer;
ivec2 unpackBase(uint b) {
return ivec2(int(b & 0xFFFFu), int(b >> 16u));
}
float getExposure() {
float e = params.preExposure;
if ((params.flags & 32u) != 0u) {
float te = texelFetch(InputExposure, ivec2(0), 0).r;
if (!isnan(te) && !isinf(te) && te > 1e-4 && te < 1e4) e *= te;
}
return clamp(e, 1e-4, 1e4);
}
vec3 safeColor(vec3 c) {
if (any(isnan(c)) || any(isinf(c))) return vec3(0.0);
return clamp(c, vec3(0.0), vec3(64000.0));
}
vec2 fetchUVVelocity(ivec2 q, ivec2 mv_sz, ivec2 mv_base, ivec2 r_lim) {
ivec2 mv_q;
if ((params.flags & 4u) != 0u) {
vec2 uv_n = (vec2(q) + vec2(0.5)) * params.renderSizeRcp;
mv_q = clamp(mv_base + ivec2(uv_n * vec2(params.displaySize)), ivec2(0), mv_sz - ivec2(1));
} else {
mv_q = clamp(mv_base + clamp(q, ivec2(0), r_lim), ivec2(0), mv_sz - ivec2(1));
}
vec2 raw = texelFetch(InputVelocity, mv_q, 0).xy;
vec2 vel = raw * params.mvScale + params.jitterCancel;
if (isnan(vel.x) || isnan(vel.y) || isinf(vel.x) || isinf(vel.y) || abs(vel.x) > 4.0 || abs(vel.y) > 4.0) {
return vec2(0.0);
}
return vel;
}
void main() {
ivec2 p = ivec2(gl_GlobalInvocationID.xy);
if (p.x >= int(params.renderSize.x) || p.y >= int(params.renderSize.y)) return;
ivec2 r_lim = ivec2(params.renderSize) - ivec2(1);
ivec2 col_sz = textureSize(InputColor, 0);
ivec2 dep_sz = textureSize(InputDepth, 0);
ivec2 mv_sz = textureSize(InputVelocity, 0);
ivec2 c_base = unpackBase(params.col_base);
ivec2 d_base = unpackBase(params.dep_base);
ivec2 m_base = unpackBase(params.mv_base);
ivec2 re_base = unpackBase(params.react_base);
bool has_depth = (params.flags & 1u) != 0u && dep_sz.x > 1 && dep_sz.y > 1;
bool dep_inv = (params.flags & 2u) != 0u;
// 3x3 nearest-depth dilation (supports both standard-Z and reverse-Z)
ivec2 best_q = p;
float NearestZ = dep_inv ? 0.0 : 1.0;
if (has_depth) {
ivec2 d_max = dep_sz - ivec2(1);
NearestZ = texelFetch(InputDepth, clamp(d_base + p, ivec2(0), d_max), 0).x;
for (int dy = -1; dy <= 1; ++dy) {
for (int dx = -1; dx <= 1; ++dx) {
if (dx == 0 && dy == 0) continue;
ivec2 pq = clamp(p + ivec2(dx, dy), ivec2(0), r_lim);
float d = texelFetch(InputDepth, clamp(d_base + pq, ivec2(0), d_max), 0).x;
bool closer = dep_inv ? (d > NearestZ) : (d < NearestZ);
if (closer) {
NearestZ = d;
best_q = pq;
}
}
}
}
// Dilated UV velocity -> SGSR 2 NDC motion convention: PrevUV = ViewportUV - 0.5 * motion = ViewportUV + vel
vec2 vel_dilated = fetchUVVelocity(best_q, mv_sz, m_base, r_lim);
vec2 vel_center = fetchUVVelocity(p, mv_sz, m_base, r_lim);
vec2 motion = -2.0 * vel_dilated;
// Qualcomm SGSR 2 reversible max-RGB tonemap + 11-11-10 YCoCg packing into R32UI
float eff_expo = getExposure();
vec3 Colorrgb = safeColor(texelFetch(InputColor, clamp(c_base + p, ivec2(0), col_sz - ivec2(1)), 0).xyz) * eff_expo;
Colorrgb /= vec3(max(max(Colorrgb.x, Colorrgb.y), Colorrgb.z) + 1.0);
vec3 Colorycocg;
Colorycocg.x = 0.25 * (Colorrgb.x + 2.0 * Colorrgb.y + Colorrgb.z);
Colorycocg.y = clamp(0.5 * Colorrgb.x + 0.5 - 0.5 * Colorrgb.z, 0.0, 1.0);
Colorycocg.z = clamp(Colorycocg.x + Colorycocg.y - Colorrgb.x, 0.0, 1.0);
uint x11 = uint(clamp(Colorycocg.x, 0.0, 1.0) * 2047.5);
uint y11 = uint(clamp(Colorycocg.y, 0.0, 1.0) * 2047.5);
uint z10 = uint(clamp(Colorycocg.z, 0.0, 1.0) * 1023.5);
imageStore(YCoCgColor, p, uvec4((x11 << 21u) | (y11 << 10u) | z10));
// Reactive / translucency alpha_mask in Qualcomm's [0..1000] encoding (unpacked by * 0.001 in upscale)
float react_m = 0.0;
if ((params.flags & 16u) != 0u) {
ivec2 re_sz = textureSize(InputReactive, 0);
if (re_sz.x > 1 && re_sz.y > 1) {
react_m = clamp(texelFetch(InputReactive, clamp(re_base + p, ivec2(0), re_sz - ivec2(1)), 0).r, 0.0, 1.0);
}
}
// Also react mildly on sharp silhouette MV jumps to prevent trailing behind fast-moving characters
float mv_diff_px = length((vel_dilated - vel_center) * vec2(params.displaySize));
float alpha_mask = clamp(max(react_m * 0.85, clamp((mv_diff_px - 1.5) * 0.12, 0.0, 0.5)), 0.0, 1.0) * 1000.0;
imageStore(MotionDepthAlphaBuffer, p, vec4(motion, NearestZ, alpha_mask));
}