FXAA.glsl 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  2. // Version 2, December 2004
  3. // Copyright (C) 2013 mudlord
  4. // Everyone is permitted to copy and distribute verbatim or modified
  5. // copies of this license document, and changing it is allowed as long
  6. // as the name is changed.
  7. // DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  8. // TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  9. // 0. You just DO WHAT THE FUCK YOU WANT TO.
  10. #define FXAA_REDUCE_MIN (1.0/ 128.0)
  11. #define FXAA_REDUCE_MUL (1.0 / 8.0)
  12. #define FXAA_SPAN_MAX 8.0
  13. float4 applyFXAA(float2 fragCoord)
  14. {
  15. float4 color;
  16. float2 inverseVP = GetInvResolution();
  17. float3 rgbNW = SampleLocation((fragCoord + float2(-1.0, -1.0)) * inverseVP).xyz;
  18. float3 rgbNE = SampleLocation((fragCoord + float2(1.0, -1.0)) * inverseVP).xyz;
  19. float3 rgbSW = SampleLocation((fragCoord + float2(-1.0, 1.0)) * inverseVP).xyz;
  20. float3 rgbSE = SampleLocation((fragCoord + float2(1.0, 1.0)) * inverseVP).xyz;
  21. float3 rgbM = SampleLocation(fragCoord * inverseVP).xyz;
  22. float3 luma = float3(0.299, 0.587, 0.114);
  23. float lumaNW = dot(rgbNW, luma);
  24. float lumaNE = dot(rgbNE, luma);
  25. float lumaSW = dot(rgbSW, luma);
  26. float lumaSE = dot(rgbSE, luma);
  27. float lumaM = dot(rgbM, luma);
  28. float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  29. float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  30. float2 dir;
  31. dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  32. dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  33. float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
  34. (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
  35. float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
  36. dir = min(float2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
  37. max(float2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
  38. dir * rcpDirMin)) * inverseVP;
  39. float3 rgbA = 0.5 * (
  40. SampleLocation(fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz +
  41. SampleLocation(fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz);
  42. float3 rgbB = rgbA * 0.5 + 0.25 * (
  43. SampleLocation(fragCoord * inverseVP + dir * -0.5).xyz +
  44. SampleLocation(fragCoord * inverseVP + dir * 0.5).xyz);
  45. float lumaB = dot(rgbB, luma);
  46. if ((lumaB < lumaMin) || (lumaB > lumaMax))
  47. color = float4(rgbA, 1.0);
  48. else
  49. color = float4(rgbB, 1.0);
  50. return color;
  51. }
  52. void main()
  53. {
  54. SetOutput(applyFXAA(GetCoordinates() * GetResolution()));
  55. }