1#extension GL_EXT_gpu_shader4 : enable 2 3uniform sampler2D m_Texture; 4uniform vec2 g_Resolution; 5 6uniform float m_VxOffset; 7uniform float m_SpanMax; 8uniform float m_ReduceMul; 9 10varying vec2 texCoord; 11varying vec4 posPos; 12 13#define FxaaTex(t, p) texture2D(t, p) 14 15#if __VERSION__ >= 130 16 #define OffsetVec(a, b) ivec2(a, b) 17 #define FxaaTexOff(t, p, o, r) textureOffset(t, p, o) 18#elif defined(GL_EXT_gpu_shader4) 19 #define OffsetVec(a, b) ivec2(a, b) 20 #define FxaaTexOff(t, p, o, r) texture2DLodOffset(t, p, 0.0, o) 21#else 22 #define OffsetVec(a, b) vec2(a, b) 23 #define FxaaTexOff(t, p, o, r) texture2D(t, p + o * r) 24#endif 25 26vec3 FxaaPixelShader( 27 vec4 posPos, // Output of FxaaVertexShader interpolated across screen. 28 sampler2D tex, // Input texture. 29 vec2 rcpFrame) // Constant {1.0/frameWidth, 1.0/frameHeight}. 30{ 31 32 #define FXAA_REDUCE_MIN (1.0/128.0) 33 //#define FXAA_REDUCE_MUL (1.0/8.0) 34 //#define FXAA_SPAN_MAX 8.0 35 36 vec3 rgbNW = FxaaTex(tex, posPos.zw).xyz; 37 vec3 rgbNE = FxaaTexOff(tex, posPos.zw, OffsetVec(1,0), rcpFrame.xy).xyz; 38 vec3 rgbSW = FxaaTexOff(tex, posPos.zw, OffsetVec(0,1), rcpFrame.xy).xyz; 39 vec3 rgbSE = FxaaTexOff(tex, posPos.zw, OffsetVec(1,1), rcpFrame.xy).xyz; 40 41 vec3 rgbM = FxaaTex(tex, posPos.xy).xyz; 42 43 vec3 luma = vec3(0.299, 0.587, 0.114); 44 float lumaNW = dot(rgbNW, luma); 45 float lumaNE = dot(rgbNE, luma); 46 float lumaSW = dot(rgbSW, luma); 47 float lumaSE = dot(rgbSE, luma); 48 float lumaM = dot(rgbM, luma); 49 50 float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); 51 float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); 52 53 vec2 dir; 54 dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); 55 dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); 56 57 float dirReduce = max( 58 (lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * m_ReduceMul), 59 FXAA_REDUCE_MIN); 60 float rcpDirMin = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce); 61 dir = min(vec2( m_SpanMax, m_SpanMax), 62 max(vec2(-m_SpanMax, -m_SpanMax), 63 dir * rcpDirMin)) * rcpFrame.xy; 64 65 vec3 rgbA = (1.0/2.0) * ( 66 FxaaTex(tex, posPos.xy + dir * vec2(1.0/3.0 - 0.5)).xyz + 67 FxaaTex(tex, posPos.xy + dir * vec2(2.0/3.0 - 0.5)).xyz); 68 vec3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * ( 69 FxaaTex(tex, posPos.xy + dir * vec2(0.0/3.0 - 0.5)).xyz + 70 FxaaTex(tex, posPos.xy + dir * vec2(3.0/3.0 - 0.5)).xyz); 71 72 float lumaB = dot(rgbB, luma); 73 74 if ((lumaB < lumaMin) || (lumaB > lumaMax)) 75 { 76 return rgbA; 77 } 78 else 79 { 80 return rgbB; 81 } 82} 83 84void main() 85{ 86 vec2 rcpFrame = vec2(1.0) / g_Resolution; 87 gl_FragColor = vec4(FxaaPixelShader(posPos, m_Texture, rcpFrame), 1.0); 88}