mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
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).
304 lines
14 KiB
Plaintext
304 lines
14 KiB
Plaintext
#version 450
|
|
// Qualcomm Snapdragon Game Super Resolution 2 (SGSR 2) — 3-Pass Compute: Pass 3 (Upscale)
|
|
// Based directly on official quic/snapdragon-gsr sgsr/v2/include/glsl_3_pass_cs/sgsr2_upscale.comp
|
|
// (Copyright (c) 2024, Qualcomm Innovation Center, Inc. SPDX-License-Identifier: BSD-3-Clause)
|
|
float FastLanczos(float base) {
|
|
float y = base - 1.0;
|
|
float y2 = y * y;
|
|
float y_temp = 0.75 * y + y2;
|
|
return y_temp * y2;
|
|
}
|
|
|
|
vec3 DecodeColor(uint sample32) {
|
|
uint x11 = sample32 >> 21u;
|
|
uint y11 = sample32 & (2047u << 10u);
|
|
uint z10 = sample32 & 1023u;
|
|
vec3 samplecolor;
|
|
samplecolor.x = (float(x11) * (1.0 / 2047.5));
|
|
samplecolor.y = (float(y11) * (4.76953602e-7)) - 0.5;
|
|
samplecolor.z = (float(z10) * (1.0 / 1023.5)) - 0.5;
|
|
return samplecolor;
|
|
}
|
|
|
|
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;
|
|
uint dep_base;
|
|
uint mv_base;
|
|
uint react_base;
|
|
float sharpness;
|
|
vec2 pad0;
|
|
} params;
|
|
|
|
layout(set = 0, binding = 1) uniform sampler2D PrevHistoryOutput;
|
|
layout(set = 0, binding = 2) uniform sampler2D MotionDepthClipAlphaBuffer;
|
|
layout(set = 0, binding = 3) uniform usampler2D YCoCgColor;
|
|
layout(set = 0, binding = 4, rgba16f) writeonly uniform image2D HistoryOutput;
|
|
layout(set = 0, binding = 5, rgba16f) writeonly uniform image2D SceneColorOutput;
|
|
layout(set = 0, binding = 6) uniform sampler2D InputExposure;
|
|
|
|
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);
|
|
}
|
|
|
|
void main() {
|
|
ivec2 gid = ivec2(gl_GlobalInvocationID.xy);
|
|
if (gid.x >= int(params.displaySize.x) || gid.y >= int(params.displaySize.y)) return;
|
|
|
|
float resetF = ((params.flags & 8u) != 0u) ? 1.0 : 0.0;
|
|
float Biasmax_viewportXScale = min(float(params.displaySize.x) / float(params.renderSize.x), 1.99);
|
|
float scalefactor = min(20.0, pow((float(params.displaySize.x) / float(params.renderSize.x)) *
|
|
(float(params.displaySize.y) / float(params.renderSize.y)), 3.0));
|
|
vec2 HistoryInfoViewportSizeInverse = params.displaySizeRcp;
|
|
vec2 HistoryInfoViewportSize = vec2(params.displaySize);
|
|
vec2 InputJitter = params.jitterOffset;
|
|
vec2 InputInfoViewportSize = vec2(params.renderSize);
|
|
vec2 Hruv = (vec2(gid) + vec2(0.5)) * HistoryInfoViewportSizeInverse;
|
|
|
|
vec2 Jitteruv;
|
|
Jitteruv.x = clamp(Hruv.x + (InputJitter.x * params.renderSizeRcp.x), 0.0, 1.0);
|
|
Jitteruv.y = clamp(Hruv.y + (InputJitter.y * params.renderSizeRcp.y), 0.0, 1.0);
|
|
|
|
ivec2 r_lim = ivec2(params.renderSize) - ivec2(1);
|
|
ivec2 InputPos = clamp(ivec2(Jitteruv * InputInfoViewportSize), ivec2(0), r_lim);
|
|
|
|
float alphab = texelFetch(MotionDepthClipAlphaBuffer, InputPos, 0).w;
|
|
vec3 mda = textureLod(MotionDepthClipAlphaBuffer, Jitteruv, 0.0).xyz;
|
|
vec2 Motion = mda.xy;
|
|
|
|
vec2 PrevUV_raw = vec2(-0.5 * Motion.x + Hruv.x, -0.5 * Motion.y + Hruv.y);
|
|
bool offscreen = (PrevUV_raw.x < 0.0 || PrevUV_raw.x > 1.0 || PrevUV_raw.y < 0.0 || PrevUV_raw.y > 1.0);
|
|
vec2 PrevUV = clamp(PrevUV_raw, vec2(0.0), vec2(1.0));
|
|
|
|
float depthfactor = mda.z;
|
|
float history_value = fract(alphab);
|
|
float alphamask = clamp((alphab - history_value) * 0.001, 0.0, 1.0);
|
|
history_value *= 2.0;
|
|
|
|
vec4 History = textureLod(PrevHistoryOutput, PrevUV, 0.0);
|
|
vec3 HistoryColor = History.xyz;
|
|
float Historyw = History.w;
|
|
float Wfactor = max(clamp(abs(Historyw), 0.0, 1.0), alphamask);
|
|
|
|
// Upsample and compute variance bounding box (Qualcomm SGSR 2 9-tap FastLanczos on R32UI YCoCg)
|
|
vec4 Upsampledcw = vec4(0.0);
|
|
float kernelfactor = clamp(Wfactor + resetF, 0.0, 1.0);
|
|
float biasmax = Biasmax_viewportXScale - Biasmax_viewportXScale * kernelfactor;
|
|
float biasmin = max(1.0, 0.3 + 0.3 * biasmax);
|
|
float biasfactor = max(0.25 * depthfactor, kernelfactor);
|
|
float kernelbias = mix(biasmax, biasmin, biasfactor);
|
|
float motion_viewport_len = length(Motion * HistoryInfoViewportSize);
|
|
float curvebias = mix(-2.0, -3.0, clamp(motion_viewport_len * 0.02, 0.0, 1.0));
|
|
|
|
vec3 rectboxcenter = vec3(0.0);
|
|
vec3 rectboxvar = vec3(0.0);
|
|
float rectboxweight = 0.0;
|
|
vec2 srcpos = vec2(InputPos) + vec2(0.5) - InputJitter;
|
|
vec2 srcOutputPos = Hruv * InputInfoViewportSize;
|
|
|
|
kernelbias *= 0.5;
|
|
float kernelbias2 = kernelbias * kernelbias;
|
|
vec2 srcpos_srcOutputPos = srcpos - srcOutputPos;
|
|
|
|
ivec2 InputPosBtmRight = min(ivec2(1) + InputPos, r_lim);
|
|
vec2 gatherCoord = vec2(InputPos) * params.renderSizeRcp;
|
|
uint btmRight = texelFetch(YCoCgColor, InputPosBtmRight, 0).x;
|
|
uvec4 topleft = textureGather(YCoCgColor, gatherCoord);
|
|
uvec2 topRight = textureGather(YCoCgColor, gatherCoord + vec2(params.renderSizeRcp.x, 0.0)).yz;
|
|
uvec2 bottomLeft = textureGather(YCoCgColor, gatherCoord + vec2(0.0, params.renderSizeRcp.y)).xy;
|
|
|
|
vec3 rectboxmin;
|
|
vec3 rectboxmax;
|
|
{
|
|
rectboxmin = DecodeColor(btmRight);
|
|
vec2 baseoffset = srcpos_srcOutputPos + vec2(1.0, 1.0);
|
|
float baseoffset_dot = dot(baseoffset, baseoffset);
|
|
float weight = FastLanczos(clamp(baseoffset_dot * kernelbias2, 0.0, 1.0));
|
|
Upsampledcw += vec4(rectboxmin * weight, weight);
|
|
float boxweight = exp(baseoffset_dot * curvebias);
|
|
rectboxmax = rectboxmin;
|
|
vec3 wsample = rectboxmin * boxweight;
|
|
rectboxcenter = wsample;
|
|
rectboxvar = rectboxmin * wsample;
|
|
rectboxweight = boxweight;
|
|
}
|
|
{
|
|
vec3 samplecolor = DecodeColor(bottomLeft.x);
|
|
vec2 baseoffset = srcpos_srcOutputPos + vec2(-1.0, 1.0);
|
|
float baseoffset_dot = dot(baseoffset, baseoffset);
|
|
float weight = FastLanczos(clamp(baseoffset_dot * kernelbias2, 0.0, 1.0));
|
|
Upsampledcw += vec4(samplecolor * weight, weight);
|
|
float boxweight = exp(baseoffset_dot * curvebias);
|
|
rectboxmin = min(rectboxmin, samplecolor);
|
|
rectboxmax = max(rectboxmax, samplecolor);
|
|
vec3 wsample = samplecolor * boxweight;
|
|
rectboxcenter += wsample;
|
|
rectboxvar += samplecolor * wsample;
|
|
rectboxweight += boxweight;
|
|
}
|
|
{
|
|
vec3 samplecolor = DecodeColor(bottomLeft.y);
|
|
vec2 baseoffset = srcpos_srcOutputPos + vec2(0.0, 1.0);
|
|
float baseoffset_dot = dot(baseoffset, baseoffset);
|
|
float weight = FastLanczos(clamp(baseoffset_dot * kernelbias2, 0.0, 1.0));
|
|
Upsampledcw += vec4(samplecolor * weight, weight);
|
|
float boxweight = exp(baseoffset_dot * curvebias);
|
|
rectboxmin = min(rectboxmin, samplecolor);
|
|
rectboxmax = max(rectboxmax, samplecolor);
|
|
vec3 wsample = samplecolor * boxweight;
|
|
rectboxcenter += wsample;
|
|
rectboxvar += samplecolor * wsample;
|
|
rectboxweight += boxweight;
|
|
}
|
|
{
|
|
vec3 samplecolor = DecodeColor(topRight.x);
|
|
vec2 baseoffset = srcpos_srcOutputPos + vec2(1.0, 0.0);
|
|
float baseoffset_dot = dot(baseoffset, baseoffset);
|
|
float weight = FastLanczos(clamp(baseoffset_dot * kernelbias2, 0.0, 1.0));
|
|
Upsampledcw += vec4(samplecolor * weight, weight);
|
|
float boxweight = exp(baseoffset_dot * curvebias);
|
|
rectboxmin = min(rectboxmin, samplecolor);
|
|
rectboxmax = max(rectboxmax, samplecolor);
|
|
vec3 wsample = samplecolor * boxweight;
|
|
rectboxcenter += wsample;
|
|
rectboxvar += samplecolor * wsample;
|
|
rectboxweight += boxweight;
|
|
}
|
|
{
|
|
vec3 samplecolor = DecodeColor(topRight.y);
|
|
vec2 baseoffset = srcpos_srcOutputPos + vec2(1.0, -1.0);
|
|
float baseoffset_dot = dot(baseoffset, baseoffset);
|
|
float weight = FastLanczos(clamp(baseoffset_dot * kernelbias2, 0.0, 1.0));
|
|
Upsampledcw += vec4(samplecolor * weight, weight);
|
|
float boxweight = exp(baseoffset_dot * curvebias);
|
|
rectboxmin = min(rectboxmin, samplecolor);
|
|
rectboxmax = max(rectboxmax, samplecolor);
|
|
vec3 wsample = samplecolor * boxweight;
|
|
rectboxcenter += wsample;
|
|
rectboxvar += samplecolor * wsample;
|
|
rectboxweight += boxweight;
|
|
}
|
|
{
|
|
vec3 samplecolor = DecodeColor(topleft.x);
|
|
vec2 baseoffset = srcpos_srcOutputPos + vec2(-1.0, 0.0);
|
|
float baseoffset_dot = dot(baseoffset, baseoffset);
|
|
float weight = FastLanczos(clamp(baseoffset_dot * kernelbias2, 0.0, 1.0));
|
|
Upsampledcw += vec4(samplecolor * weight, weight);
|
|
float boxweight = exp(baseoffset_dot * curvebias);
|
|
rectboxmin = min(rectboxmin, samplecolor);
|
|
rectboxmax = max(rectboxmax, samplecolor);
|
|
vec3 wsample = samplecolor * boxweight;
|
|
rectboxcenter += wsample;
|
|
rectboxvar += samplecolor * wsample;
|
|
rectboxweight += boxweight;
|
|
}
|
|
{
|
|
vec3 samplecolor = DecodeColor(topleft.y);
|
|
vec2 baseoffset = srcpos_srcOutputPos;
|
|
float baseoffset_dot = dot(baseoffset, baseoffset);
|
|
float weight = FastLanczos(clamp(baseoffset_dot * kernelbias2, 0.0, 1.0));
|
|
Upsampledcw += vec4(samplecolor * weight, weight);
|
|
float boxweight = exp(baseoffset_dot * curvebias);
|
|
rectboxmin = min(rectboxmin, samplecolor);
|
|
rectboxmax = max(rectboxmax, samplecolor);
|
|
vec3 wsample = samplecolor * boxweight;
|
|
rectboxcenter += wsample;
|
|
rectboxvar += samplecolor * wsample;
|
|
rectboxweight += boxweight;
|
|
}
|
|
{
|
|
vec3 samplecolor = DecodeColor(topleft.z);
|
|
vec2 baseoffset = srcpos_srcOutputPos + vec2(0.0, -1.0);
|
|
float baseoffset_dot = dot(baseoffset, baseoffset);
|
|
float weight = FastLanczos(clamp(baseoffset_dot * kernelbias2, 0.0, 1.0));
|
|
Upsampledcw += vec4(samplecolor * weight, weight);
|
|
float boxweight = exp(baseoffset_dot * curvebias);
|
|
rectboxmin = min(rectboxmin, samplecolor);
|
|
rectboxmax = max(rectboxmax, samplecolor);
|
|
vec3 wsample = samplecolor * boxweight;
|
|
rectboxcenter += wsample;
|
|
rectboxvar += samplecolor * wsample;
|
|
rectboxweight += boxweight;
|
|
}
|
|
{
|
|
vec3 samplecolor = DecodeColor(topleft.w);
|
|
vec2 baseoffset = srcpos_srcOutputPos + vec2(-1.0, -1.0);
|
|
float baseoffset_dot = dot(baseoffset, baseoffset);
|
|
float weight = FastLanczos(clamp(baseoffset_dot * kernelbias2, 0.0, 1.0));
|
|
Upsampledcw += vec4(samplecolor * weight, weight);
|
|
float boxweight = exp(baseoffset_dot * curvebias);
|
|
rectboxmin = min(rectboxmin, samplecolor);
|
|
rectboxmax = max(rectboxmax, samplecolor);
|
|
vec3 wsample = samplecolor * boxweight;
|
|
rectboxcenter += wsample;
|
|
rectboxvar += samplecolor * wsample;
|
|
rectboxweight += boxweight;
|
|
}
|
|
|
|
rectboxweight = 1.0 / max(rectboxweight, 1e-6);
|
|
rectboxcenter *= rectboxweight;
|
|
rectboxvar *= rectboxweight;
|
|
rectboxvar = sqrt(abs(rectboxvar - rectboxcenter * rectboxcenter));
|
|
|
|
Upsampledcw.xyz = clamp(Upsampledcw.xyz / max(Upsampledcw.w, 1e-5), rectboxmin - vec3(0.05), rectboxmax + vec3(0.05));
|
|
Upsampledcw.w = Upsampledcw.w * (1.0 / 3.0);
|
|
|
|
float tcontribute = history_value * clamp(rectboxvar.x * 10.0, 0.0, 1.0);
|
|
float OneMinusWfactor = 1.0 - Wfactor;
|
|
tcontribute = tcontribute * OneMinusWfactor;
|
|
|
|
float baseupdate = OneMinusWfactor - OneMinusWfactor * depthfactor;
|
|
baseupdate = min(baseupdate, mix(baseupdate, Upsampledcw.w * 10.0, clamp(10.0 * motion_viewport_len, 0.0, 1.0)));
|
|
baseupdate = min(baseupdate, mix(baseupdate, Upsampledcw.w, clamp(motion_viewport_len * 0.05, 0.0, 1.0)));
|
|
float basealpha = baseupdate;
|
|
|
|
const float EPSILON = 1.192e-07;
|
|
float boxscale = max(depthfactor, clamp(motion_viewport_len * 0.05, 0.0, 1.0));
|
|
float boxsize = mix(scalefactor, 1.0, boxscale);
|
|
vec3 sboxvar = rectboxvar * boxsize;
|
|
vec3 boxmin = rectboxcenter - sboxvar;
|
|
vec3 boxmax = rectboxcenter + sboxvar;
|
|
rectboxmax = min(rectboxmax, boxmax);
|
|
rectboxmin = max(rectboxmin, boxmin);
|
|
|
|
vec3 clampedcolor = clamp(HistoryColor, rectboxmin, rectboxmax);
|
|
float lerpcontribution = (any(greaterThan(rectboxmin, HistoryColor)) || any(greaterThan(HistoryColor, rectboxmax))) ? tcontribute : 1.0;
|
|
lerpcontribution = lerpcontribution - lerpcontribution * sqrt(alphamask);
|
|
HistoryColor = mix(clampedcolor, HistoryColor, clamp(lerpcontribution, 0.0, 1.0));
|
|
float basemin = min(basealpha, 0.1);
|
|
basealpha = mix(basemin, basealpha, clamp(lerpcontribution, 0.0, 1.0));
|
|
|
|
float alphasum = max(EPSILON, basealpha + Upsampledcw.w);
|
|
float alpha = clamp(Upsampledcw.w / alphasum + resetF + (offscreen ? 1.0 : 0.0), 0.0, 1.0);
|
|
Upsampledcw.xyz = mix(HistoryColor, Upsampledcw.xyz, alpha);
|
|
|
|
imageStore(HistoryOutput, gid, vec4(Upsampledcw.xyz, Wfactor));
|
|
|
|
// YCoCg -> RGB
|
|
float x_z = Upsampledcw.x - Upsampledcw.z;
|
|
vec3 rgb = max(vec3(x_z + Upsampledcw.y, Upsampledcw.x + Upsampledcw.z, x_z - Upsampledcw.y), vec3(0.0));
|
|
|
|
// Exact inverse of Qualcomm's reversible max-RGB tonemap + exposure undo
|
|
float compMax = min(max(rgb.x, max(rgb.y, rgb.z)), 0.9999);
|
|
float inv_expo = 1.0 / getExposure();
|
|
float scale = inv_expo / ((1.0 + 1.0 / 65504.0) - compMax);
|
|
vec3 out_lin = clamp(rgb * scale, vec3(0.0), vec3(64000.0));
|
|
imageStore(SceneColorOutput, gid, vec4(out_lin, 1.0));
|
|
}
|