mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-11-29 11:05:36 -05:00
138 lines
5.9 KiB
Text
138 lines
5.9 KiB
Text
#extension GL_ARB_shader_texture_lod : require
|
|
vec4 xll_tex2Dlod(sampler2D s, vec4 coord) {
|
|
return texture2DLod( s, coord.xy, coord.w);
|
|
}
|
|
uniform sampler2D _MainTex;
|
|
uniform vec4 _MainTex_TexelSize;
|
|
vec4 FxaaTexOff( in sampler2D tex, in vec2 pos, in vec2 off, in vec2 rcpFrame );
|
|
vec4 FxaaTexLod0( in sampler2D tex, in vec2 pos );
|
|
float FxaaLuma( in vec3 rgb );
|
|
vec3 FxaaLerp3( in vec3 a, in vec3 b, in float amountOfA );
|
|
vec3 FxaaFilterReturn( in vec3 rgb );
|
|
vec3 FxaaPixelShader( in vec2 pos, in sampler2D tex, in vec2 rcpFrame );
|
|
vec4 xlat_main( in vec2 uv );
|
|
vec4 FxaaTexOff( in sampler2D tex, in vec2 pos, in vec2 off, in vec2 rcpFrame ) {
|
|
return xll_tex2Dlod( tex, vec4( (pos.xy + (off * rcpFrame)), 0.0, 0.0));
|
|
}
|
|
vec4 FxaaTexLod0( in sampler2D tex, in vec2 pos ) {
|
|
return xll_tex2Dlod( tex, vec4( pos.xy, 0.0, 0.0));
|
|
}
|
|
float FxaaLuma( in vec3 rgb ) {
|
|
return ((rgb.y * (0.587 / 0.299)) + rgb.x);
|
|
}
|
|
vec3 FxaaLerp3( in vec3 a, in vec3 b, in float amountOfA ) {
|
|
return ((vec3( (-amountOfA), (-amountOfA), (-amountOfA)) * b) + ((a * vec3( amountOfA, amountOfA, amountOfA)) + b));
|
|
}
|
|
vec3 FxaaFilterReturn( in vec3 rgb ) {
|
|
return rgb;
|
|
}
|
|
vec3 FxaaPixelShader( in vec2 pos, in sampler2D tex, in vec2 rcpFrame ) {
|
|
vec3 rgbN = FxaaTexOff( tex, pos.xy, vec2( 0.0, -1.0), rcpFrame).xyz;
|
|
vec3 rgbW = FxaaTexOff( tex, pos.xy, vec2( -1.0, 0.0), rcpFrame).xyz;
|
|
vec3 rgbM = FxaaTexOff( tex, pos.xy, vec2( 0.0, 0.0), rcpFrame).xyz;
|
|
vec3 rgbE = FxaaTexOff( tex, pos.xy, vec2( 1.0, 0.0), rcpFrame).xyz;
|
|
vec3 rgbS = FxaaTexOff( tex, pos.xy, vec2( 0.0, 1.0), rcpFrame).xyz;
|
|
float lumaN = FxaaLuma( rgbN);
|
|
float lumaW = FxaaLuma( rgbW);
|
|
float lumaM = FxaaLuma( rgbM);
|
|
float lumaE = FxaaLuma( rgbE);
|
|
float lumaS = FxaaLuma( rgbS);
|
|
float rangeMin = min( lumaM, min( min( lumaN, lumaW), min( lumaS, lumaE)));
|
|
float rangeMax = max( lumaM, max( max( lumaN, lumaW), max( lumaS, lumaE)));
|
|
float range = (rangeMax - rangeMin);
|
|
if ((range < max( (1.0 / 24.0), (rangeMax * (1.0 / 8.0))))){
|
|
return FxaaFilterReturn( rgbM);
|
|
}
|
|
vec3 rgbL = ((((rgbN + rgbW) + rgbM) + rgbE) + rgbS);
|
|
float lumaL = ((((lumaN + lumaW) + lumaE) + lumaS) * 0.25);
|
|
float rangeL = abs((lumaL - lumaM));
|
|
float blendL = (max( 0.0, ((rangeL / range) - (1.0 / 4.0))) * (1.0 / (1.0 - (1.0 / 4.0))));
|
|
blendL = min( (3.0 / 4.0), blendL);
|
|
vec3 rgbNW = FxaaTexOff( tex, pos.xy, vec2( -1.0, -1.0), rcpFrame).xyz;
|
|
vec3 rgbNE = FxaaTexOff( tex, pos.xy, vec2( 1.0, -1.0), rcpFrame).xyz;
|
|
vec3 rgbSW = FxaaTexOff( tex, pos.xy, vec2( -1.0, 1.0), rcpFrame).xyz;
|
|
vec3 rgbSE = FxaaTexOff( tex, pos.xy, vec2( 1.0, 1.0), rcpFrame).xyz;
|
|
rgbL += (((rgbNW + rgbNE) + rgbSW) + rgbSE);
|
|
rgbL *= vec3( (1.0 / 9.0), (1.0 / 9.0), (1.0 / 9.0));
|
|
float lumaNW = FxaaLuma( rgbNW);
|
|
float lumaNE = FxaaLuma( rgbNE);
|
|
float lumaSW = FxaaLuma( rgbSW);
|
|
float lumaSE = FxaaLuma( rgbSE);
|
|
float edgeVert = ((abs((((0.25 * lumaNW) + ((-0.5) * lumaN)) + (0.25 * lumaNE))) + abs((((0.5 * lumaW) + ((-1.0) * lumaM)) + (0.5 * lumaE)))) + abs((((0.25 * lumaSW) + ((-0.5) * lumaS)) + (0.25 * lumaSE))));
|
|
float edgeHorz = ((abs((((0.25 * lumaNW) + ((-0.5) * lumaW)) + (0.25 * lumaSW))) + abs((((0.5 * lumaN) + ((-1.0) * lumaM)) + (0.5 * lumaS)))) + abs((((0.25 * lumaNE) + ((-0.5) * lumaE)) + (0.25 * lumaSE))));
|
|
bool horzSpan = (edgeHorz >= edgeVert);
|
|
float lengthSign = (( horzSpan ) ? ( (-rcpFrame.y) ) : ( (-rcpFrame.x) ));
|
|
if ((!horzSpan)){
|
|
lumaN = lumaW;
|
|
}
|
|
if ((!horzSpan)){
|
|
lumaS = lumaE;
|
|
}
|
|
float gradientN = abs((lumaN - lumaM));
|
|
float gradientS = abs((lumaS - lumaM));
|
|
lumaN = ((lumaN + lumaM) * 0.5);
|
|
lumaS = ((lumaS + lumaM) * 0.5);
|
|
bool pairN = (gradientN >= gradientS);
|
|
if ((!pairN)){
|
|
lumaN = lumaS;
|
|
}
|
|
if ((!pairN)){
|
|
gradientN = gradientS;
|
|
}
|
|
if ((!pairN)){
|
|
lengthSign *= (-1.0);
|
|
}
|
|
vec2 posN;
|
|
posN.x = (pos.x + (( horzSpan ) ? ( 0.0 ) : ( (lengthSign * 0.5) )));
|
|
posN.y = (pos.y + (( horzSpan ) ? ( (lengthSign * 0.5) ) : ( 0.0 )));
|
|
gradientN *= (1.0 / 4.0);
|
|
vec2 posP = posN;
|
|
vec2 offNP = (( horzSpan ) ? ( vec2( rcpFrame.x, 0.0) ) : ( vec2( 0.0, rcpFrame.y) ));
|
|
float lumaEndN = lumaN;
|
|
float lumaEndP = lumaN;
|
|
bool doneN = false;
|
|
bool doneP = false;
|
|
posN += (offNP * vec2( (-1.0), (-1.0)));
|
|
posP += (offNP * vec2( 1.0, 1.0));
|
|
int i = 0;
|
|
for ( ; (i < 16); (i++)) {
|
|
if ((!doneN)){
|
|
lumaEndN = FxaaLuma( FxaaTexLod0( tex, posN.xy).xyz);
|
|
}
|
|
if ((!doneP)){
|
|
lumaEndP = FxaaLuma( FxaaTexLod0( tex, posP.xy).xyz);
|
|
}
|
|
doneN = (doneN || (abs((lumaEndN - lumaN)) >= gradientN));
|
|
doneP = (doneP || (abs((lumaEndP - lumaN)) >= gradientN));
|
|
if ((doneN && doneP)){
|
|
break;
|
|
}
|
|
if ((!doneN)){
|
|
posN -= offNP;
|
|
}
|
|
if ((!doneP)){
|
|
posP += offNP;
|
|
}
|
|
}
|
|
float dstN = (( horzSpan ) ? ( (pos.x - posN.x) ) : ( (pos.y - posN.y) ));
|
|
float dstP = (( horzSpan ) ? ( (posP.x - pos.x) ) : ( (posP.y - pos.y) ));
|
|
bool directionN = (dstN < dstP);
|
|
lumaEndN = (( directionN ) ? ( lumaEndN ) : ( lumaEndP ));
|
|
if ((((lumaM - lumaN) < 0.0) == ((lumaEndN - lumaN) < 0.0))){
|
|
lengthSign = 0.0;
|
|
}
|
|
float spanLength = (dstP + dstN);
|
|
dstN = (( directionN ) ? ( dstN ) : ( dstP ));
|
|
float subPixelOffset = ((0.5 + (dstN * ((-1.0) / spanLength))) * lengthSign);
|
|
vec3 rgbF = FxaaTexLod0( tex, vec2( (pos.x + (( horzSpan ) ? ( 0.0 ) : ( subPixelOffset ))), (pos.y + (( horzSpan ) ? ( subPixelOffset ) : ( 0.0 ))))).xyz;
|
|
return FxaaFilterReturn( FxaaLerp3( rgbL, rgbF, blendL));
|
|
}
|
|
vec4 xlat_main( in vec2 uv ) {
|
|
return vec4( FxaaPixelShader( uv.xy, _MainTex, _MainTex_TexelSize.xy).xyz, 0.0);
|
|
}
|
|
varying vec2 xlv_TEXCOORD0;
|
|
void main() {
|
|
vec4 xl_retval;
|
|
xl_retval = xlat_main( vec2(xlv_TEXCOORD0));
|
|
gl_FragData[0] = vec4(xl_retval);
|
|
}
|