#version 450 // Qualcomm Snapdragon Game Super Resolution 2 (SGSR 2) — 3-Pass Compute: Pass 2 (Activate) // Based directly on official quic/snapdragon-gsr sgsr/v2/include/glsl_3_pass_cs/sgsr2_activate.comp // (Copyright (c) 2024, Qualcomm Innovation Center, Inc. SPDX-License-Identifier: BSD-3-Clause) #define EPSILON 1.19e-07 float DecodeColorY(uint sample32) { uint x11 = sample32 >> 21u; return float(x11) * (1.0 / 2047.5); } 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 InViewportSizeInverse; 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 usampler2D PrevLumaHistory; layout(set = 0, binding = 2) uniform sampler2D MotionDepthAlphaBuffer; layout(set = 0, binding = 3) uniform usampler2D YCoCgColor; layout(set = 0, binding = 4, rgba16f) writeonly uniform image2D MotionDepthClipAlphaBuffer; layout(set = 0, binding = 5, r32ui) writeonly uniform uimage2D LumaHistory; void main() { ivec2 p = ivec2(gl_GlobalInvocationID.xy); if (p.x >= int(params.renderSize.x) || p.y >= int(params.renderSize.y)) return; const ivec2 sampleOffset[4] = ivec2[4]( ivec2(-1, -1), ivec2(-1, +0), ivec2(+0, -1), ivec2(+0, +0) ); bool has_depth = (params.flags & 1u) != 0u; bool dep_inv = (params.flags & 2u) != 0u; bool is_reset = (params.flags & 8u) != 0u; vec2 ViewportUV = (vec2(p) + vec2(0.5)) * params.InViewportSizeInverse; uint luma_reference32 = texelFetch(YCoCgColor, p, 0).x; float luma_reference = DecodeColorY(luma_reference32); vec4 mda = texelFetch(MotionDepthAlphaBuffer, p, 0); float depth = mda.z; float alphamask = mda.w; vec2 motion = mda.xy; vec2 PrevUV = vec2(-0.5 * motion.x + ViewportUV.x, -0.5 * motion.y + ViewportUV.y); float depthclip = 0.0; bool depth_valid = has_depth && (dep_inv ? (depth > 1.0e-05) : (depth < 1.0 - 1.0e-05)); if (depth_valid) { vec2 Prevf_sample = PrevUV * vec2(params.renderSize) - 0.5; vec2 Prevfrac = Prevf_sample - floor(Prevf_sample); float OneMinusPrevfacx = 1.0 - Prevfrac.x; float Bilinweights[4] = float[4]( OneMinusPrevfacx - OneMinusPrevfacx * Prevfrac.y, Prevfrac.x - Prevfrac.x * Prevfrac.y, OneMinusPrevfacx * Prevfrac.y, Prevfrac.x * Prevfrac.y ); float diagonal_length = length(vec2(params.renderSize)); float Wdepth = 0.0; float Ksep = 1.37e-05; float Kfov = max(params.cameraFovAngleHor, 0.25); float Ksep_Kfov_diagonal = Ksep * Kfov * diagonal_length; for (int index = 0; index < 4; index += 2) { vec4 gPrevdepth = textureGatherOffset(MotionDepthAlphaBuffer, PrevUV, sampleOffset[index], 2); float tdepth1 = dep_inv ? max(gPrevdepth.x, gPrevdepth.y) : min(gPrevdepth.x, gPrevdepth.y); float tdepth2 = dep_inv ? max(gPrevdepth.z, gPrevdepth.w) : min(gPrevdepth.z, gPrevdepth.w); float fPrevdepth = dep_inv ? max(tdepth1, tdepth2) : min(tdepth1, tdepth2); float near_ref = dep_inv ? max(fPrevdepth, depth) : (1.0 - min(fPrevdepth, depth)); float Depthsep = Ksep_Kfov_diagonal * max(near_ref, 1e-4); float weight = Bilinweights[index]; Wdepth += clamp(Depthsep / (abs(fPrevdepth - depth) + EPSILON), 0.0, 1.0) * weight; vec2 gPrevdepth2 = textureGatherOffset(MotionDepthAlphaBuffer, PrevUV, sampleOffset[index + 1], 2).zw; fPrevdepth = dep_inv ? max(max(gPrevdepth2.x, gPrevdepth2.y), tdepth2) : min(min(gPrevdepth2.x, gPrevdepth2.y), tdepth2); near_ref = dep_inv ? max(fPrevdepth, depth) : (1.0 - min(fPrevdepth, depth)); Depthsep = Ksep_Kfov_diagonal * max(near_ref, 1e-4); weight = Bilinweights[index + 1]; Wdepth += clamp(Depthsep / (abs(fPrevdepth - depth) + EPSILON), 0.0, 1.0) * weight; } depthclip = clamp(1.0 - Wdepth, 0.0, 1.0); } vec2 current_luma_diff; uint prev_luma_diff_pack = textureGather(PrevLumaHistory, clamp(PrevUV, vec2(0.0), vec2(1.0)), 0).w; vec2 prev_luma_diff; prev_luma_diff.x = unpackHalf2x16(prev_luma_diff_pack >> 16u).x; prev_luma_diff.y = unpackHalf2x16(prev_luma_diff_pack & 0xFFFFu).x; bool enable = false; if (depthclip + float(is_reset ? 1u : 0u) < 0.1) { enable = (all(greaterThanEqual(PrevUV, vec2(0.0))) && all(lessThanEqual(PrevUV, vec2(1.0)))); } float luma_diff = luma_reference - prev_luma_diff.x; if (!enable) { current_luma_diff = vec2(0.0); } else { current_luma_diff.x = luma_reference; current_luma_diff.y = (prev_luma_diff.y != 0.0) ? ((sign(luma_diff) == sign(prev_luma_diff.y)) ? (sign(luma_diff) * min(abs(prev_luma_diff.y), abs(luma_diff))) : prev_luma_diff.y) : luma_diff; } alphamask = floor(alphamask) + 0.5 * float((current_luma_diff.x != 0.0) && (abs(current_luma_diff.y) != abs(luma_diff))); uint pack = (packHalf2x16(vec2(current_luma_diff.x, 0.0)) << 16u) | packHalf2x16(vec2(current_luma_diff.y, 0.0)); imageStore(LumaHistory, p, uvec4(pack)); imageStore(MotionDepthClipAlphaBuffer, p, vec4(motion, depthclip, alphamask)); }