-
Notifications
You must be signed in to change notification settings - Fork 0
/
_fxaa.glsl
174 lines (164 loc) · 7.44 KB
/
_fxaa.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
164
165
166
167
168
169
170
171
172
173
174
// https://github.com/pyalot/webgl-deferred-irradiance-volumes/blob/master/src/antialias/fxaa3_11_preprocessed.shaderlib
// Honestly, this is just standard Nvidia FXAA 3.11
// Nothing much to add to the code..
float FxaaLuma(vec4 rgba) { return rgba.a; }
vec4 FxaaPixelShader(
vec2 pos
,sampler2D tex
,sampler2D lumaTex
,vec2 fxaaQualityRcpFrame
,float fxaaQualitySubpix
,float fxaaQualityEdgeThreshold
,float fxaaQualityEdgeThresholdMin
) {
vec2 posM;
posM.x = pos.x;
posM.y = pos.y;
vec4 rgbyM = texture(tex, posM, 0.0);
rgbyM.a = FxaaLuma(texture(lumaTex, posM, 0.0));
float lumaS = FxaaLuma(texture(lumaTex, posM + (vec2( 0.0, 1.0) * fxaaQualityRcpFrame.xy), 0.0));
float lumaE = FxaaLuma(texture(lumaTex, posM + (vec2( 1.0, 0.0) * fxaaQualityRcpFrame.xy), 0.0));
float lumaN = FxaaLuma(texture(lumaTex, posM + (vec2( 0.0,-1.0) * fxaaQualityRcpFrame.xy), 0.0));
float lumaW = FxaaLuma(texture(lumaTex, posM + (vec2(-1.0, 0.0) * fxaaQualityRcpFrame.xy), 0.0));
float maxSM = max(lumaS, rgbyM.y);
float minSM = min(lumaS, rgbyM.y);
float maxESM = max(lumaE, maxSM);
float minESM = min(lumaE, minSM);
float maxWN = max(lumaN, lumaW);
float minWN = min(lumaN, lumaW);
float rangeMax = max(maxWN, maxESM);
float rangeMin = min(minWN, minESM);
float rangeMaxScaled = rangeMax * fxaaQualityEdgeThreshold;
float range = rangeMax - rangeMin;
float rangeMaxClamped = max(fxaaQualityEdgeThresholdMin, rangeMaxScaled);
bool earlyExit = range < rangeMaxClamped;
if(earlyExit) return rgbyM;
float lumaNW = FxaaLuma(texture(lumaTex, posM + (vec2(-1.0,-1.0) * fxaaQualityRcpFrame.xy), 0.0));
float lumaSE = FxaaLuma(texture(lumaTex, posM + (vec2( 1.0, 1.0) * fxaaQualityRcpFrame.xy), 0.0));
float lumaNE = FxaaLuma(texture(lumaTex, posM + (vec2( 1.0,-1.0) * fxaaQualityRcpFrame.xy), 0.0));
float lumaSW = FxaaLuma(texture(lumaTex, posM + (vec2(-1.0, 1.0) * fxaaQualityRcpFrame.xy), 0.0));
float lumaNS = lumaN + lumaS;
float lumaWE = lumaW + lumaE;
float subpixRcpRange = 1.0/range;
float subpixNSWE = lumaNS + lumaWE;
float edgeHorz1 = (-2.0 * rgbyM.y) + lumaNS;
float edgeVert1 = (-2.0 * rgbyM.y) + lumaWE;
float lumaNESE = lumaNE + lumaSE;
float lumaNWNE = lumaNW + lumaNE;
float edgeHorz2 = (-2.0 * lumaE) + lumaNESE;
float edgeVert2 = (-2.0 * lumaN) + lumaNWNE;
float lumaNWSW = lumaNW + lumaSW;
float lumaSWSE = lumaSW + lumaSE;
float edgeHorz4 = (abs(edgeHorz1) * 2.0) + abs(edgeHorz2);
float edgeVert4 = (abs(edgeVert1) * 2.0) + abs(edgeVert2);
float edgeHorz3 = (-2.0 * lumaW) + lumaNWSW;
float edgeVert3 = (-2.0 * lumaS) + lumaSWSE;
float edgeHorz = abs(edgeHorz3) + edgeHorz4;
float edgeVert = abs(edgeVert3) + edgeVert4;
float subpixNWSWNESE = lumaNWSW + lumaNESE;
float lengthSign = fxaaQualityRcpFrame.x;
bool horzSpan = edgeHorz >= edgeVert;
float subpixA = subpixNSWE * 2.0 + subpixNWSWNESE;
if(!horzSpan) lumaN = lumaW;
if(!horzSpan) lumaS = lumaE;
if(horzSpan) lengthSign = fxaaQualityRcpFrame.y;
float subpixB = (subpixA * (1.0/12.0)) - rgbyM.y;
float gradientN = lumaN - rgbyM.y;
float gradientS = lumaS - rgbyM.y;
float lumaNN = lumaN + rgbyM.y;
float lumaSS = lumaS + rgbyM.y;
bool pairN = abs(gradientN) >= abs(gradientS);
float gradient = max(abs(gradientN), abs(gradientS));
if(pairN) lengthSign = -lengthSign;
float subpixC = clamp(abs(subpixB) * subpixRcpRange, 0.0, 1.0);
vec2 posB;
posB.x = posM.x;
posB.y = posM.y;
vec2 offNP;
offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x;
offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y;
if(!horzSpan) posB.x += lengthSign * 0.5;
if( horzSpan) posB.y += lengthSign * 0.5;
vec2 posN;
posN.x = posB.x - offNP.x * 1.0;
posN.y = posB.y - offNP.y * 1.0;
vec2 posP;
posP.x = posB.x + offNP.x * 1.0;
posP.y = posB.y + offNP.y * 1.0;
float subpixD = ((-2.0)*subpixC) + 3.0;
float lumaEndN = FxaaLuma(texture(lumaTex, posN, 0.0));
float subpixE = subpixC * subpixC;
float lumaEndP = FxaaLuma(texture(lumaTex, posP, 0.0));
if(!pairN) lumaNN = lumaSS;
float gradientScaled = gradient * 1.0/4.0;
float lumaMM = rgbyM.y - lumaNN * 0.5;
float subpixF = subpixD * subpixE;
bool lumaMLTZero = lumaMM < 0.0;
lumaEndN -= lumaNN * 0.5;
lumaEndP -= lumaNN * 0.5;
bool doneN = abs(lumaEndN) >= gradientScaled;
bool doneP = abs(lumaEndP) >= gradientScaled;
if(!doneN) posN.x -= offNP.x * 1.5;
if(!doneN) posN.y -= offNP.y * 1.5;
bool doneNP = (!doneN) || (!doneP);
if(!doneP) posP.x += offNP.x * 1.5;
if(!doneP) posP.y += offNP.y * 1.5;
if(doneNP) {
if(!doneN) lumaEndN = FxaaLuma(texture(lumaTex, posN.xy, 0.0));
if(!doneP) lumaEndP = FxaaLuma(texture(lumaTex, posP.xy, 0.0));
if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
doneN = abs(lumaEndN) >= gradientScaled;
doneP = abs(lumaEndP) >= gradientScaled;
if(!doneN) posN.x -= offNP.x * 2.0;
if(!doneN) posN.y -= offNP.y * 2.0;
doneNP = (!doneN) || (!doneP);
if(!doneP) posP.x += offNP.x * 2.0;
if(!doneP) posP.y += offNP.y * 2.0;
if(doneNP) {
if(!doneN) lumaEndN = FxaaLuma(texture(lumaTex, posN.xy, 0.0));
if(!doneP) lumaEndP = FxaaLuma(texture(lumaTex, posP.xy, 0.0));
if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
doneN = abs(lumaEndN) >= gradientScaled;
doneP = abs(lumaEndP) >= gradientScaled;
if(!doneN) posN.x -= offNP.x * 4.0;
if(!doneN) posN.y -= offNP.y * 4.0;
doneNP = (!doneN) || (!doneP);
if(!doneP) posP.x += offNP.x * 4.0;
if(!doneP) posP.y += offNP.y * 4.0;
if(doneNP) {
if(!doneN) lumaEndN = FxaaLuma(texture(lumaTex, posN.xy, 0.0));
if(!doneP) lumaEndP = FxaaLuma(texture(lumaTex, posP.xy, 0.0));
if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
doneN = abs(lumaEndN) >= gradientScaled;
doneP = abs(lumaEndP) >= gradientScaled;
if(!doneN) posN.x -= offNP.x * 12.0;
if(!doneN) posN.y -= offNP.y * 12.0;
doneNP = (!doneN) || (!doneP);
if(!doneP) posP.x += offNP.x * 12.0;
if(!doneP) posP.y += offNP.y * 12.0;
}
}
}
float dstN = posM.x - posN.x;
float dstP = posP.x - posM.x;
if(!horzSpan) dstN = posM.y - posN.y;
if(!horzSpan) dstP = posP.y - posM.y;
bool goodSpanN = (lumaEndN < 0.0) != lumaMLTZero;
float spanLength = (dstP + dstN);
bool goodSpanP = (lumaEndP < 0.0) != lumaMLTZero;
float spanLengthRcp = 1.0/spanLength;
bool directionN = dstN < dstP;
float dst = min(dstN, dstP);
bool goodSpan = directionN ? goodSpanN : goodSpanP;
float subpixG = subpixF * subpixF;
float pixelOffset = (dst * (-spanLengthRcp)) + 0.5;
float subpixH = subpixG * fxaaQualitySubpix;
float pixelOffsetGood = goodSpan ? pixelOffset : 0.0;
float pixelOffsetSubpix = max(pixelOffsetGood, subpixH);
if(!horzSpan) posM.x += pixelOffsetSubpix * lengthSign;
if( horzSpan) posM.y += pixelOffsetSubpix * lengthSign;
return vec4(texture(tex, posM, 0.0).xyz, rgbyM.y);
}