-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrcas.glsl
163 lines (147 loc) · 4.73 KB
/
rcas.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Original:https://www.shadertoy.com/view/stXSWB
by goingdigital
*/
uniform vec2 iResolution;
uniform float sharpness;
uniform sampler2D iChannel0;
// uniform sampler2D iChannel1;
out vec4 fragColor;
/*
* FidelityFX Super Resolution scales up a low resolution
* image, while adding fine detail.
*
* MIT Open License
*
* https://gpuopen.com/fsr
*
* Left: FSR processed
* Right: Original texture, bilinear interpolation
*
* Mouse at top: Sharpness 0 stops (maximum)
* Mouse at bottom: Sharpness 2 stops (minimum)
*
* It works in two passes-
* EASU upsamples the image with a clamped Lanczos kernel.
* RCAS sharpens the image at the target resolution.
*
* I needed to make a few changes to improve readability and
* WebGL compatibility in an algorithm I don't fully understand.
* Expect bugs.
*
* Shader not currently running for WebGL1 targets (eg. mobile Safari)
*
* There is kind of no point to using FSR in Shadertoy, as it renders buffers
* at full target resolution. But this might be useful for WebGL based demos
* running smaller-than-target render buffers.
*
* For sharpening with a full resolution render buffer,
* FidelityFX CAS is a better option.
* https://www.shadertoy.com/view/ftsXzM
*
* For readability and compatibility, these optimisations have been removed:
* * Fast approximate inverse and inversesqrt
* * textureGather fetches (not WebGL compatible)
* * Multiplying by reciprocal instead of division
*
* Apologies to AMD for the numerous slowdowns and errors I have introduced.
*
*/
/***** RCAS *****/
#define FSR_RCAS_LIMIT (0.25-(1.0/16.0))
//#define FSR_RCAS_DENOISE
// Input callback prototypes that need to be implemented by calling shader
vec4 FsrRcasLoadF(vec2 p);
//------------------------------------------------------------------------------------------------------------------------------
void FsrRcasCon(
out float con,
// The scale is {0.0 := maximum, to N>0, where N is the number of stops (halving) of the reduction of sharpness}.
float sharpness
){
// Transform from stops to linear value.
con = exp2(-sharpness);
}
vec3 FsrRcasF(
vec2 ip, // Integer pixel position in output.
float con
)
{
// Constant generated by RcasSetup().
// Algorithm uses minimal 3x3 pixel neighborhood.
// b
// d e f
// h
vec2 sp = vec2(ip);
vec3 b = FsrRcasLoadF(sp + vec2( 0,-1)).rgb;
vec3 d = FsrRcasLoadF(sp + vec2(-1, 0)).rgb;
vec3 e = FsrRcasLoadF(sp).rgb;
vec3 f = FsrRcasLoadF(sp+vec2( 1, 0)).rgb;
vec3 h = FsrRcasLoadF(sp+vec2( 0, 1)).rgb;
// Luma times 2.
float bL = b.g + .5 * (b.b + b.r);
float dL = d.g + .5 * (d.b + d.r);
float eL = e.g + .5 * (e.b + e.r);
float fL = f.g + .5 * (f.b + f.r);
float hL = h.g + .5 * (h.b + h.r);
// Noise detection.
float nz = .25 * (bL + dL + fL + hL) - eL;
nz=clamp(
abs(nz)
/(
max(max(bL,dL),max(eL,max(fL,hL)))
-min(min(bL,dL),min(eL,min(fL,hL)))
),
0., 1.
);
nz=1.-.5*nz;
// Min and max of ring.
vec3 mn4 = min(b, min(f, h));
vec3 mx4 = max(b, max(f, h));
// Immediate constants for peak range.
vec2 peakC = vec2(1., -4.);
// Limiters, these need to be high precision RCPs.
vec3 hitMin = mn4 / (4. * mx4);
vec3 hitMax = (peakC.x - mx4) / (4.* mn4 + peakC.y);
vec3 lobeRGB = max(-hitMin, hitMax);
float lobe = max(
-FSR_RCAS_LIMIT,
min(max(lobeRGB.r, max(lobeRGB.g, lobeRGB.b)), 0.)
)*con;
// Apply noise removal.
#ifdef FSR_RCAS_DENOISE
lobe *= nz;
#endif
// Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes.
return (lobe * (b + d + h + f) + e) / (4. * lobe + 1.);
}
vec4 FsrRcasLoadF(vec2 p) {
return texture(iChannel0,p/iResolution.xy);
}
void main()
{
vec4 fragCoord = gl_FragCoord;
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord.xy/iResolution.xy;
// Set up constants
float con;
// float sharpness = 0.2;
// float division = 0.5+.3*sin(iTime*.3);
// if (iMouse.z > 0.) {
// sharpness = 2.-2.*pow( iMouse.y / iResolution.y,.25);
// division = iMouse.x / iResolution.x;
// }
FsrRcasCon(con,sharpness);
// Perform RCAS pass
vec3 col = FsrRcasF(fragCoord.xy, con);
// Source image
// vec2 uv1;
// Bilinear interpolation
// uv1 = fragCoord.xy/iResolution.xy;
// Nearest pixel
// uv1 = (floor(vec2(textureSize(iChannel1,0))*fragCoord.xy/iResolution.xy)+.5)/vec2(textureSize(iChannel1,0));
// vec3 col_orig = texture(iChannel1,uv1).xyz;
// Comparison tool
// if (fragCoord.x/iResolution.x > division) col = col_orig;
// if (abs(fragCoord.x/iResolution.x - division)<.005) col = vec3(0);
fragColor = vec4(col,1);
}