Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #extension GL_EXT_gpu_shader4 : enable
- #define FXAA_PC 0
- /*==========================================================================*/
- #ifndef FXAA_PC
- //
- // FXAA Quality
- // The high quality PC algorithm.
- //
- #define FXAA_PC 0
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_PC_CONSOLE
- //
- // The console algorithm for PC is included
- // for developers targeting really low spec machines.
- //
- #define FXAA_PC_CONSOLE 1
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_GLSL_120
- #define FXAA_GLSL_120 0
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_GLSL_130
- #define FXAA_GLSL_130 1
- #endif
- /*==========================================================================*/
- #ifndef FXAA_EARLY_EXIT
- #define FXAA_EARLY_EXIT 0
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_DISCARD
- #define FXAA_DISCARD 1
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_LINEAR
- #define FXAA_LINEAR 0
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_FAST_PIXEL_OFFSET
- #ifdef GL_EXT_gpu_shader4
- #define FXAA_FAST_PIXEL_OFFSET 1
- #endif
- #ifdef GL_NV_gpu_shader5
- #define FXAA_FAST_PIXEL_OFFSET 1
- #endif
- #ifdef GL_ARB_gpu_shader5
- #define FXAA_FAST_PIXEL_OFFSET 1
- #endif
- #ifndef FXAA_FAST_PIXEL_OFFSET
- #define FXAA_FAST_PIXEL_OFFSET 0
- #endif
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_GATHER4_ALPHA
- #ifdef GL_ARB_gpu_shader5
- #define FXAA_GATHER4_ALPHA 1
- #endif
- #ifdef GL_NV_gpu_shader5
- #define FXAA_GATHER4_ALPHA 1
- #endif
- #ifndef FXAA_GATHER4_ALPHA
- #define FXAA_GATHER4_ALPHA 0
- #endif
- #endif
- /*============================================================================
- FXAA CONSOLE - TUNING KNOBS
- ============================================================================*/
- #ifndef FXAA_CONSOLE__EDGE_SHARPNESS
- #if 0
- #define FXAA_CONSOLE__EDGE_SHARPNESS 8.0
- #else
- #define FXAA_CONSOLE__EDGE_SHARPNESS 4.0
- #endif
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_CONSOLE__EDGE_THRESHOLD
- #if 0
- #define FXAA_CONSOLE__EDGE_THRESHOLD 0.125
- #else
- #define FXAA_CONSOLE__EDGE_THRESHOLD 0.25
- #endif
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_CONSOLE__EDGE_THRESHOLD_MIN
- #define FXAA_CONSOLE__EDGE_THRESHOLD_MIN 0.05
- #endif
- /*============================================================================
- FXAA QUALITY - TUNING KNOBS
- ============================================================================*/
- #ifndef FXAA_QUALITY__EDGE_THRESHOLD
- #define FXAA_QUALITY__EDGE_THRESHOLD (1.0/8.0)
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_QUALITY__EDGE_THRESHOLD_MIN
- #define FXAA_QUALITY__EDGE_THRESHOLD_MIN (1.0/16.0)
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_QUALITY__SUBPIX_CAP
- #define FXAA_QUALITY__SUBPIX_CAP (7.0/8.0)
- #endif
- /*--------------------------------------------------------------------------*/
- #ifndef FXAA_QUALITY__SUBPIX_TRIM
- #define FXAA_QUALITY__SUBPIX_TRIM (1.0/8.0)
- #endif
- /*============================================================================
- API PORTING
- ============================================================================*/
- #if FXAA_GLSL_120
- // Requires,
- // #version 120
- // And at least,
- // #extension GL_EXT_gpu_shader4 : enable
- // (or set FXAA_FAST_PIXEL_OFFSET 1 to work like DX9)
- #define half float
- #define half2 vec2
- #define half3 vec3
- #define half4 vec4
- #define int2 ivec2
- #define float2 vec2
- #define float3 vec3
- #define float4 vec4
- #define FxaaInt2 ivec2
- #define FxaaFloat2 vec2
- #define FxaaFloat3 vec3
- #define FxaaFloat4 vec4
- #define FxaaDiscard discard
- #define FxaaDot3(a, b) dot(a, b)
- #define FxaaSat(x) clamp(x, 0.0, 1.0)
- #define FxaaLerp(x,y,s) mix(x,y,s)
- #define FxaaTex sampler2D
- #define FxaaTexTop(t, p) texture2DLod(t, p, 0.0)
- #if (FXAA_FAST_PIXEL_OFFSET == 1)
- #define FxaaTexOff(t, p, o, r) texture2DLodOffset(t, p, 0.0, o)
- #else
- #define FxaaTexOff(t, p, o, r) texture2DLod(t, p + (o * r), 0.0)
- #endif
- #if (FXAA_GATHER4_ALPHA == 1)
- // use #extension GL_ARB_gpu_shader5 : enable
- #define FxaaTexAlpha4(t, p, r) textureGather(t, p, 3)
- #define FxaaTexOffAlpha4(t, p, o, r) textureGatherOffset(t, p, o, 3)
- #endif
- #endif
- /*--------------------------------------------------------------------------*/
- #if FXAA_GLSL_130
- // Requires "#version 130" or better
- #define half float
- #define half2 vec2
- #define half3 vec3
- #define half4 vec4
- #define int2 ivec2
- #define float2 vec2
- #define float3 vec3
- #define float4 vec4
- #define FxaaInt2 ivec2
- #define FxaaFloat2 vec2
- #define FxaaFloat3 vec3
- #define FxaaFloat4 vec4
- #define FxaaDiscard discard
- #define FxaaDot3(a, b) dot(a, b)
- #define FxaaSat(x) clamp(x, 0.0, 1.0)
- #define FxaaLerp(x,y,s) mix(x,y,s)
- #define FxaaTex sampler2D
- #define FxaaTexTop(t, p) textureLod(t, p, 0.0)
- #define FxaaTexOff(t, p, o, r) textureLodOffset(t, p, 0.0, o)
- #if (FXAA_GATHER4_ALPHA == 1)
- // use #extension GL_ARB_gpu_shader5 : enable
- #define FxaaTexAlpha4(t, p, r) textureGather(t, p, 3)
- #define FxaaTexOffAlpha4(t, p, o, r) textureGatherOffset(t, p, o, 3)
- #endif
- #endif
- /*============================================================================
- FXAA3 CONSOLE - PC PIXEL SHADER
- ------------------------------------------------------------------------------
- Using a modified version of the PS3 version here to best target old hardware.
- ============================================================================*/
- #if (FXAA_PC_CONSOLE == 1)
- /*--------------------------------------------------------------------------*/
- half4 FxaaPixelShader(
- // {xy} = center of pixel
- float2 pos,
- // {xy__} = upper left of pixel
- // {__zw} = lower right of pixel
- float4 posPos,
- // {rgb_} = color in linear or perceptual color space
- // {___a} = alpha output is junk value
- FxaaTex tex,
- // This must be from a constant/uniform.
- // {xy} = rcpFrame not used on PC version of FXAA Console
- float2 rcpFrame,
- // This must be from a constant/uniform.
- // {x___} = 2.0/screenWidthInPixels
- // {_y__} = 2.0/screenHeightInPixels
- // {__z_} = 0.5/screenWidthInPixels
- // {___w} = 0.5/screenHeightInPixels
- float4 rcpFrameOpt
- ) {
- /*--------------------------------------------------------------------------*/
- half4 dir;
- dir.y = 0.0;
- half4 lumaNe = FxaaTexTop(tex, posPos.zy);
- lumaNe.g += half(1.0/384.0);
- dir.x = -lumaNe.g;
- dir.z = -lumaNe.g;
- /*--------------------------------------------------------------------------*/
- half4 lumaSw = FxaaTexTop(tex, posPos.xw);
- dir.x += lumaSw.g;
- dir.z += lumaSw.g;
- /*--------------------------------------------------------------------------*/
- half4 lumaNw = FxaaTexTop(tex, posPos.xy);
- dir.x -= lumaNw.g;
- dir.z += lumaNw.g;
- /*--------------------------------------------------------------------------*/
- half4 lumaSe = FxaaTexTop(tex, posPos.zw);
- dir.x += lumaSe.g;
- dir.z -= lumaSe.g;
- /*==========================================================================*/
- #if (FXAA_EARLY_EXIT == 1)
- half4 rgbyM = FxaaTexTop(tex, pos.xy);
- /*--------------------------------------------------------------------------*/
- half lumaMin = min(min(lumaNw.g, lumaSw.g), min(lumaNe.g, lumaSe.g));
- half lumaMax = max(max(lumaNw.g, lumaSw.g), max(lumaNe.g, lumaSe.g));
- /*--------------------------------------------------------------------------*/
- half lumaMinM = min(lumaMin, rgbyM.g);
- half lumaMaxM = max(lumaMax, rgbyM.g);
- /*--------------------------------------------------------------------------*/
- if((lumaMaxM - lumaMinM) < max(FXAA_CONSOLE__EDGE_THRESHOLD_MIN, lumaMax * FXAA_CONSOLE__EDGE_THRESHOLD))
- #if (FXAA_DISCARD == 1)
- FxaaDiscard;
- #else
- return rgbyM;
- #endif
- #endif
- /*==========================================================================*/
- half4 dir1_pos;
- dir1_pos.xy = normalize(dir.xyz).xz;
- half dirAbsMinTimesC = min(abs(dir1_pos.x), abs(dir1_pos.y)) * half(FXAA_CONSOLE__EDGE_SHARPNESS);
- /*--------------------------------------------------------------------------*/
- half4 dir2_pos;
- dir2_pos.xy = clamp(dir1_pos.xy / dirAbsMinTimesC, half(-2.0), half(2.0));
- dir1_pos.zw = pos.xy;
- dir2_pos.zw = pos.xy;
- half4 temp1N;
- temp1N.xy = dir1_pos.zw - dir1_pos.xy * rcpFrameOpt.zw;
- /*--------------------------------------------------------------------------*/
- temp1N = FxaaTexTop(tex, temp1N.xy);
- half4 rgby1;
- rgby1.xy = dir1_pos.zw + dir1_pos.xy * rcpFrameOpt.zw;
- /*--------------------------------------------------------------------------*/
- rgby1 = FxaaTexTop(tex, rgby1.xy);
- rgby1 = (temp1N + rgby1) * 0.5;
- /*--------------------------------------------------------------------------*/
- half4 temp2N;
- temp2N.xy = dir2_pos.zw - dir2_pos.xy * rcpFrameOpt.xy;
- temp2N = FxaaTexTop(tex, temp2N.xy);
- /*--------------------------------------------------------------------------*/
- half4 rgby2;
- rgby2.xy = dir2_pos.zw + dir2_pos.xy * rcpFrameOpt.xy;
- rgby2 = FxaaTexTop(tex, rgby2.xy);
- rgby2 = (temp2N + rgby2) * 0.5;
- /*--------------------------------------------------------------------------*/
- #if (FXAA_EARLY_EXIT == 0)
- half lumaMin = min(min(lumaNw.g, lumaSw.g), min(lumaNe.g, lumaSe.g));
- half lumaMax = max(max(lumaNw.g, lumaSw.g), max(lumaNe.g, lumaSe.g));
- #endif
- rgby2 = (rgby2 + rgby1) * 0.5;
- /*--------------------------------------------------------------------------*/
- bool twoTapLt = rgby2.g < lumaMin;
- bool twoTapGt = rgby2.g > lumaMax;
- /*--------------------------------------------------------------------------*/
- if(twoTapLt || twoTapGt) rgby2 = rgby1;
- /*--------------------------------------------------------------------------*/
- return rgby2; }
- /*==========================================================================*/
- #endif
- /*============================================================================
- FXAA3 QUALITY - PC
- ============================================================================*/
- #if (FXAA_PC == 1)
- /*--------------------------------------------------------------------------*/
- float4 FxaaPixelShader(
- // {xy} = center of pixel
- float2 pos,
- // {xyzw} = not used on FXAA3 Quality
- float4 posPos,
- // {rgb_} = color in linear or perceptual color space
- // {___a} = luma in perceptual color space (not linear)
- FxaaTex tex,
- // This must be from a constant/uniform.
- // {x_} = 1.0/screenWidthInPixels
- // {_y} = 1.0/screenHeightInPixels
- float2 rcpFrame,
- // {xyzw} = not used on FXAA3 Quality
- float4 rcpFrameOpt
- ) {
- /*--------------------------------------------------------------------------*/
- #if (FXAA_GATHER4_ALPHA == 1)
- float4 luma4A = FxaaTexOffAlpha4(tex, pos.xy, FxaaInt2(-1, -1), rcpFrame.xy);
- #if (FXAA_DISCARD == 0)
- float4 rgbyM = FxaaTexTop(tex, pos.xy);
- #endif
- float4 luma4B = FxaaTexAlpha4(tex, pos.xy, rcpFrame.xy);
- float lumaNE = FxaaTexOff(tex, pos.xy, FxaaInt2(1, -1), rcpFrame.xy).g;
- float lumaSW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1, 1), rcpFrame.xy).g;
- float lumaNW = luma4A.g;
- float lumaN = luma4A.z;
- float lumaW = luma4A.x;
- float lumaM = luma4A.y;
- float lumaE = luma4B.z;
- float lumaS = luma4B.x;
- float lumaSE = luma4B.y;
- #else
- float lumaN = FxaaTexOff(tex, pos.xy, FxaaInt2(0, -1), rcpFrame.xy).g;
- float lumaW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1, 0), rcpFrame.xy).g;
- float4 rgbyM = FxaaTexTop(tex, pos.xy);
- float lumaE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1, 0), rcpFrame.xy).g;
- float lumaS = FxaaTexOff(tex, pos.xy, FxaaInt2( 0, 1), rcpFrame.xy).g;
- float lumaM = rgbyM.g;
- #endif
- /*--------------------------------------------------------------------------*/
- 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(FXAA_QUALITY__EDGE_THRESHOLD_MIN, rangeMax * FXAA_QUALITY__EDGE_THRESHOLD))
- #if (FXAA_DISCARD == 1)
- FxaaDiscard;
- #else
- return rgbyM;
- #endif
- /*--------------------------------------------------------------------------*/
- #if (FXAA_GATHER4_ALPHA == 0)
- float lumaNW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1,-1), rcpFrame.xy).g;
- float lumaNE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1,-1), rcpFrame.xy).g;
- float lumaSW = FxaaTexOff(tex, pos.xy, FxaaInt2(-1, 1), rcpFrame.xy).g;
- float lumaSE = FxaaTexOff(tex, pos.xy, FxaaInt2( 1, 1), rcpFrame.xy).g;
- #endif
- /*--------------------------------------------------------------------------*/
- #define FXAA_QUALITY__SUBPIX_TRIM_SCALE (1.0/(1.0 - FXAA_QUALITY__SUBPIX_TRIM))
- /*--------------------------------------------------------------------------*/
- float lumaL = (lumaN + lumaW + lumaE + lumaS) * 0.25;
- float rangeL = abs(lumaL - lumaM);
- float blendL = FxaaSat((rangeL / range) - FXAA_QUALITY__SUBPIX_TRIM) * FXAA_QUALITY__SUBPIX_TRIM_SCALE;
- blendL = min(FXAA_QUALITY__SUBPIX_CAP, blendL);
- /*--------------------------------------------------------------------------*/
- float edgeVert =
- abs(lumaNW + (-2.0 * lumaN) + lumaNE) +
- 2.0 * abs(lumaW + (-2.0 * lumaM) + lumaE ) +
- abs(lumaSW + (-2.0 * lumaS) + lumaSE);
- float edgeHorz =
- abs(lumaNW + (-2.0 * lumaW) + lumaSW) +
- 2.0 * abs(lumaN + (-2.0 * lumaM) + lumaS ) +
- abs(lumaNE + (-2.0 * lumaE) + 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;
- float2 posN;
- posN.x = pos.x + (horzSpan ? 0.0 : lengthSign * 0.5);
- posN.y = pos.y + (horzSpan ? lengthSign * 0.5 : 0.0);
- /*--------------------------------------------------------------------------*/
- #define FXAA_SEARCH_STEPS 6
- #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
- /*--------------------------------------------------------------------------*/
- gradientN *= FXAA_SEARCH_THRESHOLD;
- /*--------------------------------------------------------------------------*/
- float2 posP = posN;
- float2 offNP = horzSpan ?
- FxaaFloat2(rcpFrame.x, 0.0) :
- FxaaFloat2(0.0f, rcpFrame.y);
- float lumaEndN;
- float lumaEndP;
- bool doneN = false;
- bool doneP = false;
- posN += offNP * (-1.5);
- posP += offNP * ( 1.5);
- for(int i = 0; i < FXAA_SEARCH_STEPS; i++) {
- lumaEndN = FxaaTexTop(tex, posN.xy).g;
- lumaEndP = FxaaTexTop(tex, posP.xy).g;
- bool doneN2 = abs(lumaEndN - lumaN) >= gradientN;
- bool doneP2 = abs(lumaEndP - lumaN) >= gradientN;
- if(doneN2 && !doneN) posN += offNP;
- if(doneP2 && !doneP) posP -= offNP;
- if(doneN2 && doneP2) break;
- doneN = doneN2;
- doneP = doneP2;
- if(!doneN) posN -= offNP * 2.0;
- if(!doneP) posP += offNP * 2.0; }
- /*--------------------------------------------------------------------------*/
- 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));
- subPixelOffset += blendL * (1.0/8.0);
- subPixelOffset *= lengthSign;
- float3 rgbF = FxaaTexTop(tex, FxaaFloat2(
- pos.x + (horzSpan ? 0.0 : subPixelOffset),
- pos.y + (horzSpan ? subPixelOffset : 0.0))).xyz;
- /*--------------------------------------------------------------------------*/
- #if (FXAA_LINEAR == 1)
- lumaL *= lumaL;
- #endif
- float lumaF = dot(rgbF, float3(0.299, 0.587, 0.114)) + (1.0/(65536.0*256.0));
- float lumaB = FxaaLerp(lumaF, lumaL, blendL);
- float scale = min(4.0, lumaB/lumaF);
- rgbF *= scale;
- return float4(rgbF, lumaM); }
- /*==========================================================================*/
- #endif
- uniform sampler2D tex0;
- varying vec2 v_rcpFrame;
- varying vec4 v_rcpFrameOpt;
- varying vec2 v_pos;
- varying vec4 v_posPos;
- void main() {
- gl_FragColor = FxaaPixelShader(v_pos, v_posPos, tex0, v_rcpFrame, v_rcpFrameOpt);
- }
Advertisement
Add Comment
Please, Sign In to add comment