fxaa.frag 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* fxaa.frag -- Fragment shader for applying FXAA to the scene
  2. *
  3. * Copyright (c) 2025-2026 Le Juez Victor
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * For conditions of distribution and use, see accompanying LICENSE file.
  7. */
  8. /*
  9. * Portions of this code are derived from NVIDIA's FXAA technology.
  10. *
  11. * Copyright (c) 2011 NVIDIA Corporation. All rights reserved.
  12. *
  13. * TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
  14. * "AS IS" AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
  15. * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  17. * NVIDIA OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT,
  18. * OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR
  19. * LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION,
  20. * OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  21. * EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  22. */
  23. // TODO: Add quality preset option (need to recompile shader)
  24. #version 330 core
  25. /* === Varyings === */
  26. noperspective in vec2 vTexCoord;
  27. /* === Uniforms === */
  28. uniform sampler2D uSourceTex;
  29. uniform vec2 uSourceTexel;
  30. /* === Fragments === */
  31. out vec4 FragColor;
  32. /* === FXAA presets === */
  33. /*
  34. FXAA_PRESET - Choose compile-in knob preset 0-5.
  35. ------------------------------------------------------------------------------
  36. FXAA_EDGE_THRESHOLD - The minimum amount of local contrast required
  37. to apply algorithm.
  38. 1.0/3.0 - too little
  39. 1.0/4.0 - good start
  40. 1.0/8.0 - applies to more edges
  41. 1.0/16.0 - overkill
  42. ------------------------------------------------------------------------------
  43. FXAA_EDGE_THRESHOLD_MIN - Trims the algorithm from processing darks.
  44. Perf optimization.
  45. 1.0/32.0 - visible limit (smaller isn't visible)
  46. 1.0/16.0 - good compromise
  47. 1.0/12.0 - upper limit (seeing artifacts)
  48. ------------------------------------------------------------------------------
  49. FXAA_SEARCH_STEPS - Maximum number of search steps for end of span.
  50. ------------------------------------------------------------------------------
  51. FXAA_SEARCH_THRESHOLD - Controls when to stop searching.
  52. 1.0/4.0 - seems to be the best quality wise
  53. ------------------------------------------------------------------------------
  54. FXAA_SUBPIX_TRIM - Controls sub-pixel aliasing removal.
  55. 1.0/2.0 - low removal
  56. 1.0/3.0 - medium removal
  57. 1.0/4.0 - default removal
  58. 1.0/8.0 - high removal
  59. 0.0 - complete removal
  60. ------------------------------------------------------------------------------
  61. FXAA_SUBPIX_CAP - Insures fine detail is not completely removed.
  62. This is important for the transition of sub-pixel detail,
  63. like fences and wires.
  64. 3.0/4.0 - default (medium amount of filtering)
  65. 7.0/8.0 - high amount of filtering
  66. 1.0 - no capping of sub-pixel aliasing removal
  67. */
  68. #define FXAA_PRESET 5
  69. #if (FXAA_PRESET == 3)
  70. #define FXAA_EDGE_THRESHOLD (1.0/8.0)
  71. #define FXAA_EDGE_THRESHOLD_MIN (1.0/16.0)
  72. #define FXAA_SEARCH_STEPS 16
  73. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  74. #define FXAA_SUBPIX_CAP (3.0/4.0)
  75. #define FXAA_SUBPIX_TRIM (1.0/4.0)
  76. #endif
  77. #if (FXAA_PRESET == 4)
  78. #define FXAA_EDGE_THRESHOLD (1.0/8.0)
  79. #define FXAA_EDGE_THRESHOLD_MIN (1.0/24.0)
  80. #define FXAA_SEARCH_STEPS 24
  81. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  82. #define FXAA_SUBPIX_CAP (3.0/4.0)
  83. #define FXAA_SUBPIX_TRIM (1.0/4.0)
  84. #endif
  85. #if (FXAA_PRESET == 5)
  86. #define FXAA_EDGE_THRESHOLD (1.0/8.0)
  87. #define FXAA_EDGE_THRESHOLD_MIN (1.0/24.0)
  88. #define FXAA_SEARCH_STEPS 32
  89. #define FXAA_SEARCH_THRESHOLD (1.0/4.0)
  90. #define FXAA_SUBPIX_CAP (3.0/4.0)
  91. #define FXAA_SUBPIX_TRIM (1.0/4.0)
  92. #endif
  93. #define FXAA_SUBPIX_TRIM_SCALE (1.0/(1.0 - FXAA_SUBPIX_TRIM))
  94. /* === Helper Functions === */
  95. float FxaaLuma(vec3 rgb)
  96. {
  97. // Return the luma, the estimation of luminance from rgb inputs.
  98. // This approximates luma using one FMA instruction,
  99. // skipping normalization and tossing out blue.
  100. // FxaaLuma() will range 0.0 to 2.963210702.
  101. return rgb.y * (0.587/0.299) + rgb.x;
  102. }
  103. vec3 FxaaLerp3(vec3 a, vec3 b, float amountOfA)
  104. {
  105. return (vec3(-amountOfA) * b) + ((a * vec3(amountOfA)) + b);
  106. }
  107. vec4 FxaaTexOff(sampler2D tex, vec2 pos, ivec2 off, vec2 rcpFrame)
  108. {
  109. float x = pos.x + float(off.x) * rcpFrame.x;
  110. float y = pos.y + float(off.y) * rcpFrame.y;
  111. return texture(tex, vec2(x, y));
  112. }
  113. /* === Main function === */
  114. void main()
  115. {
  116. vec2 pos = vTexCoord;
  117. vec3 rgbN = FxaaTexOff(uSourceTex, pos.xy, ivec2( 0,-1), uSourceTexel).xyz;
  118. vec3 rgbW = FxaaTexOff(uSourceTex, pos.xy, ivec2(-1, 0), uSourceTexel).xyz;
  119. vec3 rgbM = FxaaTexOff(uSourceTex, pos.xy, ivec2( 0, 0), uSourceTexel).xyz;
  120. vec3 rgbE = FxaaTexOff(uSourceTex, pos.xy, ivec2( 1, 0), uSourceTexel).xyz;
  121. vec3 rgbS = FxaaTexOff(uSourceTex, pos.xy, ivec2( 0, 1), uSourceTexel).xyz;
  122. float lumaN = FxaaLuma(rgbN);
  123. float lumaW = FxaaLuma(rgbW);
  124. float lumaM = FxaaLuma(rgbM);
  125. float lumaE = FxaaLuma(rgbE);
  126. float lumaS = FxaaLuma(rgbS);
  127. float rangeMin = min(lumaM, min(min(lumaN, lumaW), min(lumaS, lumaE)));
  128. float rangeMax = max(lumaM, max(max(lumaN, lumaW), max(lumaS, lumaE)));
  129. float range = rangeMax - rangeMin;
  130. if(range < max(FXAA_EDGE_THRESHOLD_MIN, rangeMax * FXAA_EDGE_THRESHOLD)) {
  131. FragColor = vec4(rgbM, 1.0);
  132. return;
  133. }
  134. vec3 rgbL = rgbN + rgbW + rgbM + rgbE + rgbS;
  135. float lumaL = (lumaN + lumaW + lumaE + lumaS) * 0.25;
  136. float rangeL = abs(lumaL - lumaM);
  137. float blendL = max(0.0, (rangeL / range) - FXAA_SUBPIX_TRIM) * FXAA_SUBPIX_TRIM_SCALE;
  138. blendL = min(FXAA_SUBPIX_CAP, blendL);
  139. vec3 rgbNW = FxaaTexOff(uSourceTex, pos.xy, ivec2(-1,-1), uSourceTexel).xyz;
  140. vec3 rgbNE = FxaaTexOff(uSourceTex, pos.xy, ivec2( 1,-1), uSourceTexel).xyz;
  141. vec3 rgbSW = FxaaTexOff(uSourceTex, pos.xy, ivec2(-1, 1), uSourceTexel).xyz;
  142. vec3 rgbSE = FxaaTexOff(uSourceTex, pos.xy, ivec2( 1, 1), uSourceTexel).xyz;
  143. rgbL += (rgbNW + rgbNE + rgbSW + rgbSE);
  144. rgbL *= vec3(1.0/9.0);
  145. float lumaNW = FxaaLuma(rgbNW);
  146. float lumaNE = FxaaLuma(rgbNE);
  147. float lumaSW = FxaaLuma(rgbSW);
  148. float lumaSE = FxaaLuma(rgbSE);
  149. float edgeVert =
  150. abs((0.25 * lumaNW) + (-0.5 * lumaN) + (0.25 * lumaNE)) +
  151. abs((0.50 * lumaW ) + (-1.0 * lumaM) + (0.50 * lumaE )) +
  152. abs((0.25 * lumaSW) + (-0.5 * lumaS) + (0.25 * lumaSE));
  153. float edgeHorz =
  154. abs((0.25 * lumaNW) + (-0.5 * lumaW) + (0.25 * lumaSW)) +
  155. abs((0.50 * lumaN ) + (-1.0 * lumaM) + (0.50 * lumaS )) +
  156. abs((0.25 * lumaNE) + (-0.5 * lumaE) + (0.25 * lumaSE));
  157. bool horzSpan = edgeHorz >= edgeVert;
  158. float lengthSign = horzSpan ? -uSourceTexel.y : -uSourceTexel.x;
  159. if(!horzSpan) {
  160. lumaN = lumaW;
  161. lumaS = lumaE;
  162. }
  163. float gradientN = abs(lumaN - lumaM);
  164. float gradientS = abs(lumaS - lumaM);
  165. lumaN = (lumaN + lumaM) * 0.5;
  166. lumaS = (lumaS + lumaM) * 0.5;
  167. if (gradientN < gradientS) {
  168. lumaN = lumaS;
  169. lumaN = lumaS;
  170. gradientN = gradientS;
  171. lengthSign *= -1.0;
  172. }
  173. vec2 posN;
  174. posN.x = pos.x + (horzSpan ? 0.0 : lengthSign * 0.5);
  175. posN.y = pos.y + (horzSpan ? lengthSign * 0.5 : 0.0);
  176. gradientN *= FXAA_SEARCH_THRESHOLD;
  177. vec2 posP = posN;
  178. vec2 offNP = horzSpan ? vec2(uSourceTexel.x, 0.0) : vec2(0.0, uSourceTexel.y);
  179. float lumaEndN = lumaN;
  180. float lumaEndP = lumaN;
  181. bool doneN = false;
  182. bool doneP = false;
  183. posN += offNP * vec2(-1.0, -1.0);
  184. posP += offNP * vec2( 1.0, 1.0);
  185. for(int i = 0; i < FXAA_SEARCH_STEPS; i++) {
  186. if(!doneN) {
  187. lumaEndN = FxaaLuma(texture(uSourceTex, posN.xy).xyz);
  188. }
  189. if(!doneP) {
  190. lumaEndP = FxaaLuma(texture(uSourceTex, posP.xy).xyz);
  191. }
  192. doneN = doneN || (abs(lumaEndN - lumaN) >= gradientN);
  193. doneP = doneP || (abs(lumaEndP - lumaN) >= gradientN);
  194. if(doneN && doneP) {
  195. break;
  196. }
  197. if(!doneN) {
  198. posN -= offNP;
  199. }
  200. if(!doneP) {
  201. posP += offNP;
  202. }
  203. }
  204. float dstN = horzSpan ? pos.x - posN.x : pos.y - posN.y;
  205. float dstP = horzSpan ? posP.x - pos.x : posP.y - pos.y;
  206. bool directionN = dstN < dstP;
  207. lumaEndN = directionN ? lumaEndN : lumaEndP;
  208. if(((lumaM - lumaN) < 0.0) == ((lumaEndN - lumaN) < 0.0)) {
  209. lengthSign = 0.0;
  210. }
  211. float spanLength = (dstP + dstN);
  212. dstN = directionN ? dstN : dstP;
  213. float subPixelOffset = (0.5 + (dstN * (-1.0/spanLength))) * lengthSign;
  214. vec3 rgbF = texture(uSourceTex, vec2(
  215. pos.x + (horzSpan ? 0.0 : subPixelOffset),
  216. pos.y + (horzSpan ? subPixelOffset : 0.0))).xyz;
  217. FragColor = vec4(FxaaLerp3(rgbL, rgbF, blendL), 1.0);
  218. }