Skip to content

Commit 6fea51e

Browse files
Create frs_rcas.fsh
Add AMD FSR1
1 parent 4f85bb8 commit 6fea51e

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

assets/shaders/frs_rcas.fsh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
// Set precision for OpenGL ES
24+
#ifdef GL_ES
25+
precision mediump float;
26+
precision mediump int;
27+
#endif
28+
29+
// Texture sampler for input image
30+
uniform sampler2D sampler0;
31+
32+
// Texture coordinate delta (1.0 / viewport size)
33+
uniform vec2 u_texelDelta;
34+
35+
// User settings:
36+
// u_setting.x = sharpness value (0.0 to 1.0)
37+
uniform vec4 u_setting;
38+
39+
// Interpolated texture coordinates from vertex shader
40+
varying vec2 v_texcoord0;
41+
42+
// Split-screen divider width for comparison views
43+
const float lineWidth = 0.005;
44+
45+
// Luminance coefficients based on Rec. 709 standard
46+
// Used for converting RGB to perceptually weighted luminance
47+
const vec3 lumCoef = vec3(0.2126, 0.7152, 0.0722);
48+
49+
// RCAS (Robust Contrast Adaptive Sharpening) constants
50+
// Peak negative lobe strength and its inverse
51+
const float rcasPeak = 8.0 - 3.0; // Peak negative lobe strength
52+
const float rcasInvPeak = 1.0 / rcasPeak; // Inverse of peak strength
53+
54+
// Small epsilon value for numerical stability
55+
// Matches AMD's reference implementation
56+
const float FSR_EPS = 0.0001;
57+
58+
// Cross-shaped sampling pattern offsets (N, W, E, S)
59+
// Used for 4-tap cross sampling around center pixel
60+
const vec2 crossOffsets[4] = vec2[4](
61+
vec2( 0.0, -1.0), // North
62+
vec2(-1.0, 0.0), // West
63+
vec2( 1.0, 0.0), // East
64+
vec2( 0.0, 1.0) // South
65+
);
66+
67+
// Vanilla RCAS kernel implementation (no edge-aware weighting)
68+
// Performs contrast adaptive sharpening on the input texture
69+
vec4 FsrRcasVanilla(vec2 uv) {
70+
// Sample center pixel and convert to luminance
71+
vec3 C = texture2D(sampler0, uv).rgb;
72+
float CL = dot(C, lumCoef);
73+
74+
// Sample the 4 cross neighbors and convert to luminance
75+
vec3 N = texture2D(sampler0, uv + crossOffsets[0] * u_texelDelta).rgb; // North
76+
vec3 W = texture2D(sampler0, uv + crossOffsets[1] * u_texelDelta).rgb; // West
77+
vec3 E = texture2D(sampler0, uv + crossOffsets[2] * u_texelDelta).rgb; // East
78+
vec3 S = texture2D(sampler0, uv + crossOffsets[3] * u_texelDelta).rgb; // South
79+
80+
float NL = dot(N, lumCoef); // North luminance
81+
float WL = dot(W, lumCoef); // West luminance
82+
float EL = dot(E, lumCoef); // East luminance
83+
float SL = dot(S, lumCoef); // South luminance
84+
85+
// Calculate adaptive amplification factor to prevent oversharpening
86+
// Uses min/max range analysis to determine safe sharpening strength
87+
vec3 minRGB = min(min(min(N, W), min(E, S)), C); // Minimum RGB in 5-tap neighborhood
88+
vec3 maxRGB = max(max(max(N, W), max(E, S)), C); // Maximum RGB in 5-tap neighborhood
89+
vec3 invMax = 1.0 / (maxRGB + FSR_EPS); // Inverse of maximum (with epsilon)
90+
vec3 amp = clamp(min(minRGB, 2.0 - maxRGB) * invMax, 0.0, 1.0); // Amplification factor
91+
amp = inversesqrt(amp + FSR_EPS); // Inverse square root for non-linearity
92+
93+
// Calculate sharpening weight based on amplification
94+
float w = -rcasInvPeak / dot(amp, lumCoef);
95+
96+
// Compute sharpened luminance using contrast adaptive formula
97+
float sumL = NL + WL + EL + SL; // Sum of neighbor luminances
98+
float invDen = 1.0 / (4.0 * w + 1.0); // Inverse denominator
99+
float sharpL = clamp((sumL * w + CL) * invDen, 0.0, 1.0); // Sharpened luminance
100+
101+
// Reconstruct color by preserving chroma (hue/saturation)
102+
// This prevents color shifts during sharpening
103+
vec3 chroma = C - vec3(CL); // Extract chroma (color without brightness)
104+
vec3 sharpColor = chroma + vec3(sharpL); // Apply sharpened luminance to chroma
105+
106+
// Blend between original and sharpened based on user sharpness setting
107+
// u_setting.x controls the blend: 0.0 = original, 1.0 = fully sharpened
108+
vec3 outColor = mix(C, sharpColor, u_setting.x);
109+
110+
return vec4(outColor, 1.0);
111+
}
112+
113+
// Main fragment shader entry point
114+
void main() {
115+
// Apply RCAS sharpening to the current fragment
116+
gl_FragColor = FsrRcasVanilla(v_texcoord0);
117+
}

0 commit comments

Comments
 (0)