Fxaa.glsl 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*============================================================================
  2. NVIDIA FXAA 3.11 by TIMOTHY LOTTES
  3. ------------------------------------------------------------------------------
  4. COPYRIGHT (C) 2010, 2011 NVIDIA CORPORATION. ALL RIGHTS RESERVED.
  5. ------------------------------------------------------------------------------
  6. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
  7. *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
  8. OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA
  10. OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR
  12. LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION,
  13. OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE
  14. THIS SOFTWARE, EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  15. DAMAGES.
  16. ------------------------------------------------------------------------------
  17. INTEGRATION CHECKLIST
  18. ------------------------------------------------------------------------------
  19. (1.)
  20. In the shader source, setup defines for the desired configuration.
  21. When providing multiple shaders (for different presets),
  22. simply setup the defines differently in multiple files.
  23. Example,
  24. #define FXAA_PC 1
  25. #define FXAA_HLSL_5 1
  26. #define FXAA_QUALITY_PRESET 12
  27. Or,
  28. #define FXAA_360 1
  29. Or,
  30. #define FXAA_PS3 1
  31. Etc.
  32. (2.)
  33. Then include this file,
  34. #include "Fxaa3_11.h"
  35. (3.)
  36. Then call the FXAA pixel shader from within your desired shader.
  37. Look at the FXAA Quality FxaaPixelShader() for docs on inputs.
  38. As for FXAA 3.11 all inputs for all shaders are the same
  39. to enable easy porting between platforms.
  40. return FxaaPixelShader(...);
  41. (4.)
  42. Insure pass prior to FXAA outputs RGBL (see next section).
  43. Or use,
  44. #define FXAA_GREEN_AS_LUMA 1
  45. (5.)
  46. Setup engine to provide the following constants
  47. which are used in the FxaaPixelShader() inputs,
  48. FxaaFloat2 fxaaQualityRcpFrame,
  49. FxaaFloat4 fxaaConsoleRcpFrameOpt,
  50. FxaaFloat4 fxaaConsoleRcpFrameOpt2,
  51. FxaaFloat4 fxaaConsole360RcpFrameOpt2,
  52. FxaaFloat fxaaQualitySubpix,
  53. FxaaFloat fxaaQualityEdgeThreshold,
  54. FxaaFloat fxaaQualityEdgeThresholdMin,
  55. FxaaFloat fxaaConsoleEdgeSharpness,
  56. FxaaFloat fxaaConsoleEdgeThreshold,
  57. FxaaFloat fxaaConsoleEdgeThresholdMin,
  58. FxaaFloat4 fxaaConsole360ConstDir
  59. Look at the FXAA Quality FxaaPixelShader() for docs on inputs.
  60. (6.)
  61. Have FXAA vertex shader run as a full screen triangle,
  62. and output "pos" and "fxaaConsolePosPos"
  63. such that inputs in the pixel shader provide,
  64. // {xy} = center of pixel
  65. FxaaFloat2 pos,
  66. // {xy_} = upper left of pixel
  67. // {_zw} = lower right of pixel
  68. FxaaFloat4 fxaaConsolePosPos,
  69. (7.)
  70. Insure the texture sampler(s) used by FXAA are set to bilinear filtering.
  71. ------------------------------------------------------------------------------
  72. INTEGRATION - RGBL AND COLORSPACE
  73. ------------------------------------------------------------------------------
  74. FXAA3 requires RGBL as input unless the following is set,
  75. #define FXAA_GREEN_AS_LUMA 1
  76. In which case the engine uses green in place of luma,
  77. and requires RGB input is in a non-linear colorspace.
  78. RGB should be LDR (low dynamic range).
  79. Specifically do FXAA after tonemapping.
  80. RGB data as returned by a texture fetch can be non-linear,
  81. or linear when FXAA_GREEN_AS_LUMA is not set.
  82. Note an "sRGB format" texture counts as linear,
  83. because the result of a texture fetch is linear data.
  84. Regular "RGBA8" textures in the sRGB colorspace are non-linear.
  85. If FXAA_GREEN_AS_LUMA is not set,
  86. luma must be stored in the alpha channel prior to running FXAA.
  87. This luma should be in a perceptual space (could be gamma 2.0).
  88. Example pass before FXAA where output is gamma 2.0 encoded,
  89. color.rgb = ToneMap(color.rgb); // linear color output
  90. color.rgb = sqrt(color.rgb); // gamma 2.0 color output
  91. return color;
  92. To use FXAA,
  93. color.rgb = ToneMap(color.rgb); // linear color output
  94. color.rgb = sqrt(color.rgb); // gamma 2.0 color output
  95. color.a = dot(color.rgb, FxaaFloat3(0.299, 0.587, 0.114)); // compute luma
  96. return color;
  97. Another example where output is linear encoded,
  98. say for instance writing to an sRGB formated render target,
  99. where the render target does the conversion back to sRGB after blending,
  100. color.rgb = ToneMap(color.rgb); // linear color output
  101. return color;
  102. To use FXAA,
  103. color.rgb = ToneMap(color.rgb); // linear color output
  104. color.a = sqrt(dot(color.rgb, FxaaFloat3(0.299, 0.587, 0.114))); // compute luma
  105. return color;
  106. Getting luma correct is required for the algorithm to work correctly.
  107. ------------------------------------------------------------------------------
  108. BEING LINEARLY CORRECT?
  109. ------------------------------------------------------------------------------
  110. Applying FXAA to a framebuffer with linear RGB color will look worse.
  111. This is very counter intuitive, but happends to be true in this case.
  112. The reason is because dithering artifacts will be more visiable
  113. in a linear colorspace.
  114. ------------------------------------------------------------------------------
  115. COMPLEX INTEGRATION
  116. ------------------------------------------------------------------------------
  117. Q. What if the engine is blending into RGB before wanting to run FXAA?
  118. A. In the last opaque pass prior to FXAA,
  119. have the pass write out luma into alpha.
  120. Then blend into RGB only.
  121. FXAA should be able to run ok
  122. assuming the blending pass did not any add aliasing.
  123. This should be the common case for particles and common blending passes.
  124. A. Or use FXAA_GREEN_AS_LUMA.
  125. ============================================================================*/
  126. #version 430 core
  127. layout(local_size_x = 16, local_size_y = 16) in;
  128. layout(rgba8, binding = 0, set = 3) uniform image2D imgOutput;
  129. layout(binding = 1, set = 2) uniform sampler2D inputImage;
  130. layout(binding = 2) uniform invResolution
  131. {
  132. vec2 invResolution_data;
  133. };
  134. #define FXAA_QUALITY_PRESET 12
  135. #define FXAA_GREEN_AS_LUMA 1
  136. #define FXAA_PC 1
  137. #define FXAA_GLSL_130 1
  138. /*============================================================================
  139. INTEGRATION KNOBS
  140. /*==========================================================================*/
  141. #ifndef FXAA_PC
  142. //
  143. // FXAA Quality
  144. // The high quality PC algorithm.
  145. //
  146. #define FXAA_PC 0
  147. #endif
  148. /*--------------------------------------------------------------------------*/
  149. #ifndef FXAA_GLSL_120
  150. #define FXAA_GLSL_120 0
  151. #endif
  152. /*--------------------------------------------------------------------------*/
  153. #ifndef FXAA_GLSL_130
  154. #define FXAA_GLSL_130 0
  155. #endif
  156. /*==========================================================================*/
  157. #ifndef FXAA_GREEN_AS_LUMA
  158. //
  159. // For those using non-linear color,
  160. // and either not able to get luma in alpha, or not wanting to,
  161. // this enables FXAA to run using green as a proxy for luma.
  162. // So with this enabled, no need to pack luma in alpha.
  163. //
  164. // This will turn off AA on anything which lacks some amount of green.
  165. // Pure red and blue or combination of only R and B, will get no AA.
  166. //
  167. // Might want to lower the settings for both,
  168. // fxaaConsoleEdgeThresholdMin
  169. // fxaaQualityEdgeThresholdMin
  170. // In order to insure AA does not get turned off on colors
  171. // which contain a minor amount of green.
  172. //
  173. // 1 = On.
  174. // 0 = Off.
  175. //
  176. #define FXAA_GREEN_AS_LUMA 0
  177. #endif
  178. /*--------------------------------------------------------------------------*/
  179. #ifndef FXAA_EARLY_EXIT
  180. //
  181. // Controls algorithm's early exit path.
  182. // On PS3 turning this ON adds 2 cycles to the shader.
  183. // On 360 turning this OFF adds 10ths of a millisecond to the shader.
  184. // Turning this off on console will result in a more blurry image.
  185. // So this defaults to on.
  186. //
  187. // 1 = On.
  188. // 0 = Off.
  189. //
  190. #define FXAA_EARLY_EXIT 1
  191. #endif
  192. /*--------------------------------------------------------------------------*/
  193. #ifndef FXAA_DISCARD
  194. //
  195. // Only valid for PC OpenGL currently.
  196. // Probably will not work when FXAA_GREEN_AS_LUMA = 1.
  197. //
  198. // 1 = Use discard on pixels which don't need AA.
  199. // For APIs which enable concurrent TEX+ROP from same surface.
  200. // 0 = Return unchanged color on pixels which don't need AA.
  201. //
  202. #define FXAA_DISCARD 0
  203. #endif
  204. /*--------------------------------------------------------------------------*/
  205. #ifndef FXAA_FAST_PIXEL_OFFSET
  206. //
  207. // Used for GLSL 120 only.
  208. //
  209. // 1 = GL API supports fast pixel offsets
  210. // 0 = do not use fast pixel offsets
  211. //
  212. #ifdef GL_EXT_gpu_shader4
  213. #define FXAA_FAST_PIXEL_OFFSET 1
  214. #endif
  215. #ifdef GL_NV_gpu_shader5
  216. #define FXAA_FAST_PIXEL_OFFSET 1
  217. #endif
  218. #ifdef GL_ARB_gpu_shader5
  219. #define FXAA_FAST_PIXEL_OFFSET 1
  220. #endif
  221. #ifndef FXAA_FAST_PIXEL_OFFSET
  222. #define FXAA_FAST_PIXEL_OFFSET 0
  223. #endif
  224. #endif
  225. /*--------------------------------------------------------------------------*/
  226. #ifndef FXAA_GATHER4_ALPHA
  227. //
  228. // 1 = API supports gather4 on alpha channel.
  229. // 0 = API does not support gather4 on alpha channel.
  230. //
  231. #if (FXAA_HLSL_5 == 1)
  232. #define FXAA_GATHER4_ALPHA 1
  233. #endif
  234. #ifdef GL_ARB_gpu_shader5
  235. #define FXAA_GATHER4_ALPHA 1
  236. #endif
  237. #ifdef GL_NV_gpu_shader5
  238. #define FXAA_GATHER4_ALPHA 1
  239. #endif
  240. #ifndef FXAA_GATHER4_ALPHA
  241. #define FXAA_GATHER4_ALPHA 0
  242. #endif
  243. #endif
  244. /*============================================================================
  245. FXAA QUALITY - TUNING KNOBS
  246. ------------------------------------------------------------------------------
  247. NOTE the other tuning knobs are now in the shader function inputs!
  248. ============================================================================*/
  249. #ifndef FXAA_QUALITY_PRESET
  250. //
  251. // Choose the quality preset.
  252. // This needs to be compiled into the shader as it effects code.
  253. // Best option to include multiple presets is to
  254. // in each shader define the preset, then include this file.
  255. //
  256. // OPTIONS
  257. // -----------------------------------------------------------------------
  258. // 10 to 15 - default medium dither (10=fastest, 15=highest quality)
  259. // 20 to 29 - less dither, more expensive (20=fastest, 29=highest quality)
  260. // 39 - no dither, very expensive
  261. //
  262. // NOTES
  263. // -----------------------------------------------------------------------
  264. // 12 = slightly faster then FXAA 3.9 and higher edge quality (default)
  265. // 13 = about same speed as FXAA 3.9 and better than 12
  266. // 23 = closest to FXAA 3.9 visually and performance wise
  267. // _ = the lowest digit is directly related to performance
  268. // _ = the highest digit is directly related to style
  269. //
  270. #define FXAA_QUALITY_PRESET 12
  271. #endif
  272. /*============================================================================
  273. FXAA QUALITY - PRESETS
  274. ============================================================================*/
  275. /*============================================================================
  276. FXAA QUALITY - MEDIUM DITHER PRESETS
  277. ============================================================================*/
  278. #if (FXAA_QUALITY_PRESET == 10)
  279. #define FXAA_QUALITY_PS 3
  280. #define FXAA_QUALITY_P0 1.5
  281. #define FXAA_QUALITY_P1 3.0
  282. #define FXAA_QUALITY_P2 12.0
  283. #endif
  284. /*--------------------------------------------------------------------------*/
  285. #if (FXAA_QUALITY_PRESET == 11)
  286. #define FXAA_QUALITY_PS 4
  287. #define FXAA_QUALITY_P0 1.0
  288. #define FXAA_QUALITY_P1 1.5
  289. #define FXAA_QUALITY_P2 3.0
  290. #define FXAA_QUALITY_P3 12.0
  291. #endif
  292. /*--------------------------------------------------------------------------*/
  293. #if (FXAA_QUALITY_PRESET == 12)
  294. #define FXAA_QUALITY_PS 5
  295. #define FXAA_QUALITY_P0 1.0
  296. #define FXAA_QUALITY_P1 1.5
  297. #define FXAA_QUALITY_P2 2.0
  298. #define FXAA_QUALITY_P3 4.0
  299. #define FXAA_QUALITY_P4 12.0
  300. #endif
  301. /*--------------------------------------------------------------------------*/
  302. #if (FXAA_QUALITY_PRESET == 13)
  303. #define FXAA_QUALITY_PS 6
  304. #define FXAA_QUALITY_P0 1.0
  305. #define FXAA_QUALITY_P1 1.5
  306. #define FXAA_QUALITY_P2 2.0
  307. #define FXAA_QUALITY_P3 2.0
  308. #define FXAA_QUALITY_P4 4.0
  309. #define FXAA_QUALITY_P5 12.0
  310. #endif
  311. /*--------------------------------------------------------------------------*/
  312. #if (FXAA_QUALITY_PRESET == 14)
  313. #define FXAA_QUALITY_PS 7
  314. #define FXAA_QUALITY_P0 1.0
  315. #define FXAA_QUALITY_P1 1.5
  316. #define FXAA_QUALITY_P2 2.0
  317. #define FXAA_QUALITY_P3 2.0
  318. #define FXAA_QUALITY_P4 2.0
  319. #define FXAA_QUALITY_P5 4.0
  320. #define FXAA_QUALITY_P6 12.0
  321. #endif
  322. /*--------------------------------------------------------------------------*/
  323. #if (FXAA_QUALITY_PRESET == 15)
  324. #define FXAA_QUALITY_PS 8
  325. #define FXAA_QUALITY_P0 1.0
  326. #define FXAA_QUALITY_P1 1.5
  327. #define FXAA_QUALITY_P2 2.0
  328. #define FXAA_QUALITY_P3 2.0
  329. #define FXAA_QUALITY_P4 2.0
  330. #define FXAA_QUALITY_P5 2.0
  331. #define FXAA_QUALITY_P6 4.0
  332. #define FXAA_QUALITY_P7 12.0
  333. #endif
  334. /*============================================================================
  335. FXAA QUALITY - LOW DITHER PRESETS
  336. ============================================================================*/
  337. #if (FXAA_QUALITY_PRESET == 20)
  338. #define FXAA_QUALITY_PS 3
  339. #define FXAA_QUALITY_P0 1.5
  340. #define FXAA_QUALITY_P1 2.0
  341. #define FXAA_QUALITY_P2 8.0
  342. #endif
  343. /*--------------------------------------------------------------------------*/
  344. #if (FXAA_QUALITY_PRESET == 21)
  345. #define FXAA_QUALITY_PS 4
  346. #define FXAA_QUALITY_P0 1.0
  347. #define FXAA_QUALITY_P1 1.5
  348. #define FXAA_QUALITY_P2 2.0
  349. #define FXAA_QUALITY_P3 8.0
  350. #endif
  351. /*--------------------------------------------------------------------------*/
  352. #if (FXAA_QUALITY_PRESET == 22)
  353. #define FXAA_QUALITY_PS 5
  354. #define FXAA_QUALITY_P0 1.0
  355. #define FXAA_QUALITY_P1 1.5
  356. #define FXAA_QUALITY_P2 2.0
  357. #define FXAA_QUALITY_P3 2.0
  358. #define FXAA_QUALITY_P4 8.0
  359. #endif
  360. /*--------------------------------------------------------------------------*/
  361. #if (FXAA_QUALITY_PRESET == 23)
  362. #define FXAA_QUALITY_PS 6
  363. #define FXAA_QUALITY_P0 1.0
  364. #define FXAA_QUALITY_P1 1.5
  365. #define FXAA_QUALITY_P2 2.0
  366. #define FXAA_QUALITY_P3 2.0
  367. #define FXAA_QUALITY_P4 2.0
  368. #define FXAA_QUALITY_P5 8.0
  369. #endif
  370. /*--------------------------------------------------------------------------*/
  371. #if (FXAA_QUALITY_PRESET == 24)
  372. #define FXAA_QUALITY_PS 7
  373. #define FXAA_QUALITY_P0 1.0
  374. #define FXAA_QUALITY_P1 1.5
  375. #define FXAA_QUALITY_P2 2.0
  376. #define FXAA_QUALITY_P3 2.0
  377. #define FXAA_QUALITY_P4 2.0
  378. #define FXAA_QUALITY_P5 3.0
  379. #define FXAA_QUALITY_P6 8.0
  380. #endif
  381. /*--------------------------------------------------------------------------*/
  382. #if (FXAA_QUALITY_PRESET == 25)
  383. #define FXAA_QUALITY_PS 8
  384. #define FXAA_QUALITY_P0 1.0
  385. #define FXAA_QUALITY_P1 1.5
  386. #define FXAA_QUALITY_P2 2.0
  387. #define FXAA_QUALITY_P3 2.0
  388. #define FXAA_QUALITY_P4 2.0
  389. #define FXAA_QUALITY_P5 2.0
  390. #define FXAA_QUALITY_P6 4.0
  391. #define FXAA_QUALITY_P7 8.0
  392. #endif
  393. /*--------------------------------------------------------------------------*/
  394. #if (FXAA_QUALITY_PRESET == 26)
  395. #define FXAA_QUALITY_PS 9
  396. #define FXAA_QUALITY_P0 1.0
  397. #define FXAA_QUALITY_P1 1.5
  398. #define FXAA_QUALITY_P2 2.0
  399. #define FXAA_QUALITY_P3 2.0
  400. #define FXAA_QUALITY_P4 2.0
  401. #define FXAA_QUALITY_P5 2.0
  402. #define FXAA_QUALITY_P6 2.0
  403. #define FXAA_QUALITY_P7 4.0
  404. #define FXAA_QUALITY_P8 8.0
  405. #endif
  406. /*--------------------------------------------------------------------------*/
  407. #if (FXAA_QUALITY_PRESET == 27)
  408. #define FXAA_QUALITY_PS 10
  409. #define FXAA_QUALITY_P0 1.0
  410. #define FXAA_QUALITY_P1 1.5
  411. #define FXAA_QUALITY_P2 2.0
  412. #define FXAA_QUALITY_P3 2.0
  413. #define FXAA_QUALITY_P4 2.0
  414. #define FXAA_QUALITY_P5 2.0
  415. #define FXAA_QUALITY_P6 2.0
  416. #define FXAA_QUALITY_P7 2.0
  417. #define FXAA_QUALITY_P8 4.0
  418. #define FXAA_QUALITY_P9 8.0
  419. #endif
  420. /*--------------------------------------------------------------------------*/
  421. #if (FXAA_QUALITY_PRESET == 28)
  422. #define FXAA_QUALITY_PS 11
  423. #define FXAA_QUALITY_P0 1.0
  424. #define FXAA_QUALITY_P1 1.5
  425. #define FXAA_QUALITY_P2 2.0
  426. #define FXAA_QUALITY_P3 2.0
  427. #define FXAA_QUALITY_P4 2.0
  428. #define FXAA_QUALITY_P5 2.0
  429. #define FXAA_QUALITY_P6 2.0
  430. #define FXAA_QUALITY_P7 2.0
  431. #define FXAA_QUALITY_P8 2.0
  432. #define FXAA_QUALITY_P9 4.0
  433. #define FXAA_QUALITY_P10 8.0
  434. #endif
  435. /*--------------------------------------------------------------------------*/
  436. #if (FXAA_QUALITY_PRESET == 29)
  437. #define FXAA_QUALITY_PS 12
  438. #define FXAA_QUALITY_P0 1.0
  439. #define FXAA_QUALITY_P1 1.5
  440. #define FXAA_QUALITY_P2 2.0
  441. #define FXAA_QUALITY_P3 2.0
  442. #define FXAA_QUALITY_P4 2.0
  443. #define FXAA_QUALITY_P5 2.0
  444. #define FXAA_QUALITY_P6 2.0
  445. #define FXAA_QUALITY_P7 2.0
  446. #define FXAA_QUALITY_P8 2.0
  447. #define FXAA_QUALITY_P9 2.0
  448. #define FXAA_QUALITY_P10 4.0
  449. #define FXAA_QUALITY_P11 8.0
  450. #endif
  451. /*============================================================================
  452. FXAA QUALITY - EXTREME QUALITY
  453. ============================================================================*/
  454. #if (FXAA_QUALITY_PRESET == 39)
  455. #define FXAA_QUALITY_PS 12
  456. #define FXAA_QUALITY_P0 1.0
  457. #define FXAA_QUALITY_P1 1.0
  458. #define FXAA_QUALITY_P2 1.0
  459. #define FXAA_QUALITY_P3 1.0
  460. #define FXAA_QUALITY_P4 1.0
  461. #define FXAA_QUALITY_P5 1.5
  462. #define FXAA_QUALITY_P6 2.0
  463. #define FXAA_QUALITY_P7 2.0
  464. #define FXAA_QUALITY_P8 2.0
  465. #define FXAA_QUALITY_P9 2.0
  466. #define FXAA_QUALITY_P10 4.0
  467. #define FXAA_QUALITY_P11 8.0
  468. #endif
  469. /*============================================================================
  470. API PORTING
  471. ============================================================================*/
  472. #if (FXAA_GLSL_120 == 1) || (FXAA_GLSL_130 == 1)
  473. #define FxaaBool bool
  474. #define FxaaDiscard discard
  475. #define FxaaFloat float
  476. #define FxaaFloat2 vec2
  477. #define FxaaFloat3 vec3
  478. #define FxaaFloat4 vec4
  479. #define FxaaHalf float
  480. #define FxaaHalf2 vec2
  481. #define FxaaHalf3 vec3
  482. #define FxaaHalf4 vec4
  483. #define FxaaInt2 ivec2
  484. #define FxaaSat(x) clamp(x, 0.0, 1.0)
  485. #define FxaaTex sampler2D
  486. #else
  487. #define FxaaBool bool
  488. #define FxaaDiscard clip(-1)
  489. #define FxaaFloat float
  490. #define FxaaFloat2 float2
  491. #define FxaaFloat3 float3
  492. #define FxaaFloat4 float4
  493. #define FxaaHalf half
  494. #define FxaaHalf2 half2
  495. #define FxaaHalf3 half3
  496. #define FxaaHalf4 half4
  497. #define FxaaSat(x) saturate(x)
  498. #endif
  499. /*--------------------------------------------------------------------------*/
  500. #if (FXAA_GLSL_120 == 1)
  501. // Requires,
  502. // #version 120
  503. // And at least,
  504. // #extension GL_EXT_gpu_shader4 : enable
  505. // (or set FXAA_FAST_PIXEL_OFFSET 1 to work like DX9)
  506. #define FxaaTexTop(t, p) texture2DLod(t, p, 0.0)
  507. #if (FXAA_FAST_PIXEL_OFFSET == 1)
  508. #define FxaaTexOff(t, p, o, r) texture2DLodOffset(t, p, 0.0, o)
  509. #else
  510. #define FxaaTexOff(t, p, o, r) texture2DLod(t, p + (o * r), 0.0)
  511. #endif
  512. #if (FXAA_GATHER4_ALPHA == 1)
  513. // use #extension GL_ARB_gpu_shader5 : enable
  514. #define FxaaTexAlpha4(t, p) textureGather(t, p, 3)
  515. #define FxaaTexOffAlpha4(t, p, o) textureGatherOffset(t, p, o, 3)
  516. #define FxaaTexGreen4(t, p) textureGather(t, p, 1)
  517. #define FxaaTexOffGreen4(t, p, o) textureGatherOffset(t, p, o, 1)
  518. #endif
  519. #endif
  520. /*--------------------------------------------------------------------------*/
  521. #if (FXAA_GLSL_130 == 1)
  522. // Requires "#version 130" or better
  523. #define FxaaTexTop(t, p) textureLod(t, p, 0.0)
  524. #define FxaaTexOff(t, p, o, r) textureLodOffset(t, p, 0.0, o)
  525. #if (FXAA_GATHER4_ALPHA == 1)
  526. // use #extension GL_ARB_gpu_shader5 : enable
  527. #define FxaaTexAlpha4(t, p) textureGather(t, p, 3)
  528. #define FxaaTexOffAlpha4(t, p, o) textureGatherOffset(t, p, o, 3)
  529. #define FxaaTexGreen4(t, p) textureGather(t, p, 1)
  530. #define FxaaTexOffGreen4(t, p, o) textureGatherOffset(t, p, o, 1)
  531. #endif
  532. #endif
  533. /*============================================================================
  534. GREEN AS LUMA OPTION SUPPORT FUNCTION
  535. ============================================================================*/
  536. #if (FXAA_GREEN_AS_LUMA == 0)
  537. FxaaFloat FxaaLuma(FxaaFloat4 rgba) { return rgba.w; }
  538. #else
  539. FxaaFloat FxaaLuma(FxaaFloat4 rgba) { return rgba.y; }
  540. #endif
  541. /*============================================================================
  542. FXAA3 QUALITY - PC
  543. ============================================================================*/
  544. #if (FXAA_PC == 1)
  545. /*--------------------------------------------------------------------------*/
  546. FxaaFloat4 FxaaPixelShader(
  547. //
  548. // Use noperspective interpolation here (turn off perspective interpolation).
  549. // {xy} = center of pixel
  550. FxaaFloat2 pos,
  551. //
  552. // Used only for FXAA Console, and not used on the 360 version.
  553. // Use noperspective interpolation here (turn off perspective interpolation).
  554. // {xy_} = upper left of pixel
  555. // {_zw} = lower right of pixel
  556. FxaaFloat4 fxaaConsolePosPos,
  557. //
  558. // Input color texture.
  559. // {rgb_} = color in linear or perceptual color space
  560. // if (FXAA_GREEN_AS_LUMA == 0)
  561. // {__a} = luma in perceptual color space (not linear)
  562. FxaaTex tex,
  563. //
  564. // Only used on the optimized 360 version of FXAA Console.
  565. // For everything but 360, just use the same input here as for "tex".
  566. // For 360, same texture, just alias with a 2nd sampler.
  567. // This sampler needs to have an exponent bias of -1.
  568. FxaaTex fxaaConsole360TexExpBiasNegOne,
  569. //
  570. // Only used on the optimized 360 version of FXAA Console.
  571. // For everything but 360, just use the same input here as for "tex".
  572. // For 360, same texture, just alias with a 3nd sampler.
  573. // This sampler needs to have an exponent bias of -2.
  574. FxaaTex fxaaConsole360TexExpBiasNegTwo,
  575. //
  576. // Only used on FXAA Quality.
  577. // This must be from a constant/uniform.
  578. // {x_} = 1.0/screenWidthInPixels
  579. // {_y} = 1.0/screenHeightInPixels
  580. FxaaFloat2 fxaaQualityRcpFrame,
  581. //
  582. // Only used on FXAA Console.
  583. // This must be from a constant/uniform.
  584. // This effects sub-pixel AA quality and inversely sharpness.
  585. // Where N ranges between,
  586. // N = 0.50 (default)
  587. // N = 0.33 (sharper)
  588. // {x__} = -N/screenWidthInPixels
  589. // {_y_} = -N/screenHeightInPixels
  590. // {_z_} = N/screenWidthInPixels
  591. // {__w} = N/screenHeightInPixels
  592. FxaaFloat4 fxaaConsoleRcpFrameOpt,
  593. //
  594. // Only used on FXAA Console.
  595. // Not used on 360, but used on PS3 and PC.
  596. // This must be from a constant/uniform.
  597. // {x__} = -2.0/screenWidthInPixels
  598. // {_y_} = -2.0/screenHeightInPixels
  599. // {_z_} = 2.0/screenWidthInPixels
  600. // {__w} = 2.0/screenHeightInPixels
  601. FxaaFloat4 fxaaConsoleRcpFrameOpt2,
  602. //
  603. // Only used on FXAA Console.
  604. // Only used on 360 in place of fxaaConsoleRcpFrameOpt2.
  605. // This must be from a constant/uniform.
  606. // {x__} = 8.0/screenWidthInPixels
  607. // {_y_} = 8.0/screenHeightInPixels
  608. // {_z_} = -4.0/screenWidthInPixels
  609. // {__w} = -4.0/screenHeightInPixels
  610. FxaaFloat4 fxaaConsole360RcpFrameOpt2,
  611. //
  612. // Only used on FXAA Quality.
  613. // This used to be the FXAA_QUALITY_SUBPIX define.
  614. // It is here now to allow easier tuning.
  615. // Choose the amount of sub-pixel aliasing removal.
  616. // This can effect sharpness.
  617. // 1.00 - upper limit (softer)
  618. // 0.75 - default amount of filtering
  619. // 0.50 - lower limit (sharper, less sub-pixel aliasing removal)
  620. // 0.25 - almost off
  621. // 0.00 - completely off
  622. FxaaFloat fxaaQualitySubpix,
  623. //
  624. // Only used on FXAA Quality.
  625. // This used to be the FXAA_QUALITY_EDGE_THRESHOLD define.
  626. // It is here now to allow easier tuning.
  627. // The minimum amount of local contrast required to apply algorithm.
  628. // 0.333 - too little (faster)
  629. // 0.250 - low quality
  630. // 0.166 - default
  631. // 0.125 - high quality
  632. // 0.063 - overkill (slower)
  633. FxaaFloat fxaaQualityEdgeThreshold,
  634. //
  635. // Only used on FXAA Quality.
  636. // This used to be the FXAA_QUALITY_EDGE_THRESHOLD_MIN define.
  637. // It is here now to allow easier tuning.
  638. // Trims the algorithm from processing darks.
  639. // 0.0833 - upper limit (default, the start of visible unfiltered edges)
  640. // 0.0625 - high quality (faster)
  641. // 0.0312 - visible limit (slower)
  642. // Special notes when using FXAA_GREEN_AS_LUMA,
  643. // Likely want to set this to zero.
  644. // As colors that are mostly not-green
  645. // will appear very dark in the green channel!
  646. // Tune by looking at mostly non-green content,
  647. // then start at zero and increase until aliasing is a problem.
  648. FxaaFloat fxaaQualityEdgeThresholdMin,
  649. //
  650. // Only used on FXAA Console.
  651. // This used to be the FXAA_CONSOLE_EDGE_SHARPNESS define.
  652. // It is here now to allow easier tuning.
  653. // This does not effect PS3, as this needs to be compiled in.
  654. // Use FXAA_CONSOLE_PS3_EDGE_SHARPNESS for PS3.
  655. // Due to the PS3 being ALU bound,
  656. // there are only three safe values here: 2 and 4 and 8.
  657. // These options use the shaders ability to a free *|/ by 2|4|8.
  658. // For all other platforms can be a non-power of two.
  659. // 8.0 is sharper (default!!!)
  660. // 4.0 is softer
  661. // 2.0 is really soft (good only for vector graphics inputs)
  662. FxaaFloat fxaaConsoleEdgeSharpness,
  663. //
  664. // Only used on FXAA Console.
  665. // This used to be the FXAA_CONSOLE_EDGE_THRESHOLD define.
  666. // It is here now to allow easier tuning.
  667. // This does not effect PS3, as this needs to be compiled in.
  668. // Use FXAA_CONSOLE_PS3_EDGE_THRESHOLD for PS3.
  669. // Due to the PS3 being ALU bound,
  670. // there are only two safe values here: 1/4 and 1/8.
  671. // These options use the shaders ability to a free *|/ by 2|4|8.
  672. // The console setting has a different mapping than the quality setting.
  673. // Other platforms can use other values.
  674. // 0.125 leaves less aliasing, but is softer (default!!!)
  675. // 0.25 leaves more aliasing, and is sharper
  676. FxaaFloat fxaaConsoleEdgeThreshold,
  677. //
  678. // Only used on FXAA Console.
  679. // This used to be the FXAA_CONSOLE_EDGE_THRESHOLD_MIN define.
  680. // It is here now to allow easier tuning.
  681. // Trims the algorithm from processing darks.
  682. // The console setting has a different mapping than the quality setting.
  683. // This only applies when FXAA_EARLY_EXIT is 1.
  684. // This does not apply to PS3,
  685. // PS3 was simplified to avoid more shader instructions.
  686. // 0.06 - faster but more aliasing in darks
  687. // 0.05 - default
  688. // 0.04 - slower and less aliasing in darks
  689. // Special notes when using FXAA_GREEN_AS_LUMA,
  690. // Likely want to set this to zero.
  691. // As colors that are mostly not-green
  692. // will appear very dark in the green channel!
  693. // Tune by looking at mostly non-green content,
  694. // then start at zero and increase until aliasing is a problem.
  695. FxaaFloat fxaaConsoleEdgeThresholdMin,
  696. //
  697. // Extra constants for 360 FXAA Console only.
  698. // Use zeros or anything else for other platforms.
  699. // These must be in physical constant registers and NOT immedates.
  700. // Immedates will result in compiler un-optimizing.
  701. // {xyzw} = float4(1.0, -1.0, 0.25, -0.25)
  702. FxaaFloat4 fxaaConsole360ConstDir
  703. ) {
  704. /*--------------------------------------------------------------------------*/
  705. FxaaFloat2 posM;
  706. posM.x = pos.x;
  707. posM.y = pos.y;
  708. #if (FXAA_GATHER4_ALPHA == 1)
  709. #if (FXAA_DISCARD == 0)
  710. FxaaFloat4 rgbyM = FxaaTexTop(tex, posM);
  711. #if (FXAA_GREEN_AS_LUMA == 0)
  712. #define lumaM rgbyM.w
  713. #else
  714. #define lumaM rgbyM.y
  715. #endif
  716. #endif
  717. #if (FXAA_GREEN_AS_LUMA == 0)
  718. FxaaFloat4 luma4A = FxaaTexAlpha4(tex, posM);
  719. FxaaFloat4 luma4B = FxaaTexOffAlpha4(tex, posM, FxaaInt2(-1, -1));
  720. #else
  721. FxaaFloat4 luma4A = FxaaTexGreen4(tex, posM);
  722. FxaaFloat4 luma4B = FxaaTexOffGreen4(tex, posM, FxaaInt2(-1, -1));
  723. #endif
  724. #if (FXAA_DISCARD == 1)
  725. #define lumaM luma4A.w
  726. #endif
  727. #define lumaE luma4A.z
  728. #define lumaS luma4A.x
  729. #define lumaSE luma4A.y
  730. #define lumaNW luma4B.w
  731. #define lumaN luma4B.z
  732. #define lumaW luma4B.x
  733. #else
  734. FxaaFloat4 rgbyM = FxaaTexTop(tex, posM);
  735. #if (FXAA_GREEN_AS_LUMA == 0)
  736. #define lumaM rgbyM.w
  737. #else
  738. #define lumaM rgbyM.y
  739. #endif
  740. FxaaFloat lumaS = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 0, 1), fxaaQualityRcpFrame.xy));
  741. FxaaFloat lumaE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1, 0), fxaaQualityRcpFrame.xy));
  742. FxaaFloat lumaN = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 0,-1), fxaaQualityRcpFrame.xy));
  743. FxaaFloat lumaW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 0), fxaaQualityRcpFrame.xy));
  744. #endif
  745. /*--------------------------------------------------------------------------*/
  746. FxaaFloat maxSM = max(lumaS, lumaM);
  747. FxaaFloat minSM = min(lumaS, lumaM);
  748. FxaaFloat maxESM = max(lumaE, maxSM);
  749. FxaaFloat minESM = min(lumaE, minSM);
  750. FxaaFloat maxWN = max(lumaN, lumaW);
  751. FxaaFloat minWN = min(lumaN, lumaW);
  752. FxaaFloat rangeMax = max(maxWN, maxESM);
  753. FxaaFloat rangeMin = min(minWN, minESM);
  754. FxaaFloat rangeMaxScaled = rangeMax * fxaaQualityEdgeThreshold;
  755. FxaaFloat range = rangeMax - rangeMin;
  756. FxaaFloat rangeMaxClamped = max(fxaaQualityEdgeThresholdMin, rangeMaxScaled);
  757. FxaaBool earlyExit = range < rangeMaxClamped;
  758. /*--------------------------------------------------------------------------*/
  759. if(earlyExit)
  760. #if (FXAA_DISCARD == 1)
  761. FxaaDiscard;
  762. #else
  763. return rgbyM;
  764. #endif
  765. /*--------------------------------------------------------------------------*/
  766. #if (FXAA_GATHER4_ALPHA == 0)
  767. FxaaFloat lumaNW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1,-1), fxaaQualityRcpFrame.xy));
  768. FxaaFloat lumaSE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1, 1), fxaaQualityRcpFrame.xy));
  769. FxaaFloat lumaNE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1,-1), fxaaQualityRcpFrame.xy));
  770. FxaaFloat lumaSW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 1), fxaaQualityRcpFrame.xy));
  771. #else
  772. FxaaFloat lumaNE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(1, -1), fxaaQualityRcpFrame.xy));
  773. FxaaFloat lumaSW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 1), fxaaQualityRcpFrame.xy));
  774. #endif
  775. /*--------------------------------------------------------------------------*/
  776. FxaaFloat lumaNS = lumaN + lumaS;
  777. FxaaFloat lumaWE = lumaW + lumaE;
  778. FxaaFloat subpixRcpRange = 1.0/range;
  779. FxaaFloat subpixNSWE = lumaNS + lumaWE;
  780. FxaaFloat edgeHorz1 = (-2.0 * lumaM) + lumaNS;
  781. FxaaFloat edgeVert1 = (-2.0 * lumaM) + lumaWE;
  782. /*--------------------------------------------------------------------------*/
  783. FxaaFloat lumaNESE = lumaNE + lumaSE;
  784. FxaaFloat lumaNWNE = lumaNW + lumaNE;
  785. FxaaFloat edgeHorz2 = (-2.0 * lumaE) + lumaNESE;
  786. FxaaFloat edgeVert2 = (-2.0 * lumaN) + lumaNWNE;
  787. /*--------------------------------------------------------------------------*/
  788. FxaaFloat lumaNWSW = lumaNW + lumaSW;
  789. FxaaFloat lumaSWSE = lumaSW + lumaSE;
  790. FxaaFloat edgeHorz4 = (abs(edgeHorz1) * 2.0) + abs(edgeHorz2);
  791. FxaaFloat edgeVert4 = (abs(edgeVert1) * 2.0) + abs(edgeVert2);
  792. FxaaFloat edgeHorz3 = (-2.0 * lumaW) + lumaNWSW;
  793. FxaaFloat edgeVert3 = (-2.0 * lumaS) + lumaSWSE;
  794. FxaaFloat edgeHorz = abs(edgeHorz3) + edgeHorz4;
  795. FxaaFloat edgeVert = abs(edgeVert3) + edgeVert4;
  796. /*--------------------------------------------------------------------------*/
  797. FxaaFloat subpixNWSWNESE = lumaNWSW + lumaNESE;
  798. FxaaFloat lengthSign = fxaaQualityRcpFrame.x;
  799. FxaaBool horzSpan = edgeHorz >= edgeVert;
  800. FxaaFloat subpixA = subpixNSWE * 2.0 + subpixNWSWNESE;
  801. /*--------------------------------------------------------------------------*/
  802. if(!horzSpan) lumaN = lumaW;
  803. if(!horzSpan) lumaS = lumaE;
  804. if(horzSpan) lengthSign = fxaaQualityRcpFrame.y;
  805. FxaaFloat subpixB = (subpixA * (1.0/12.0)) - lumaM;
  806. /*--------------------------------------------------------------------------*/
  807. FxaaFloat gradientN = lumaN - lumaM;
  808. FxaaFloat gradientS = lumaS - lumaM;
  809. FxaaFloat lumaNN = lumaN + lumaM;
  810. FxaaFloat lumaSS = lumaS + lumaM;
  811. FxaaBool pairN = abs(gradientN) >= abs(gradientS);
  812. FxaaFloat gradient = max(abs(gradientN), abs(gradientS));
  813. if(pairN) lengthSign = -lengthSign;
  814. FxaaFloat subpixC = FxaaSat(abs(subpixB) * subpixRcpRange);
  815. /*--------------------------------------------------------------------------*/
  816. FxaaFloat2 posB;
  817. posB.x = posM.x;
  818. posB.y = posM.y;
  819. FxaaFloat2 offNP;
  820. offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x;
  821. offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y;
  822. if(!horzSpan) posB.x += lengthSign * 0.5;
  823. if( horzSpan) posB.y += lengthSign * 0.5;
  824. /*--------------------------------------------------------------------------*/
  825. FxaaFloat2 posN;
  826. posN.x = posB.x - offNP.x * FXAA_QUALITY_P0;
  827. posN.y = posB.y - offNP.y * FXAA_QUALITY_P0;
  828. FxaaFloat2 posP;
  829. posP.x = posB.x + offNP.x * FXAA_QUALITY_P0;
  830. posP.y = posB.y + offNP.y * FXAA_QUALITY_P0;
  831. FxaaFloat subpixD = ((-2.0)*subpixC) + 3.0;
  832. FxaaFloat lumaEndN = FxaaLuma(FxaaTexTop(tex, posN));
  833. FxaaFloat subpixE = subpixC * subpixC;
  834. FxaaFloat lumaEndP = FxaaLuma(FxaaTexTop(tex, posP));
  835. /*--------------------------------------------------------------------------*/
  836. if(!pairN) lumaNN = lumaSS;
  837. FxaaFloat gradientScaled = gradient * 1.0/4.0;
  838. FxaaFloat lumaMM = lumaM - lumaNN * 0.5;
  839. FxaaFloat subpixF = subpixD * subpixE;
  840. FxaaBool lumaMLTZero = lumaMM < 0.0;
  841. /*--------------------------------------------------------------------------*/
  842. lumaEndN -= lumaNN * 0.5;
  843. lumaEndP -= lumaNN * 0.5;
  844. FxaaBool doneN = abs(lumaEndN) >= gradientScaled;
  845. FxaaBool doneP = abs(lumaEndP) >= gradientScaled;
  846. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P1;
  847. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P1;
  848. FxaaBool doneNP = (!doneN) || (!doneP);
  849. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P1;
  850. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P1;
  851. /*--------------------------------------------------------------------------*/
  852. if(doneNP) {
  853. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  854. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  855. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  856. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  857. doneN = abs(lumaEndN) >= gradientScaled;
  858. doneP = abs(lumaEndP) >= gradientScaled;
  859. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P2;
  860. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P2;
  861. doneNP = (!doneN) || (!doneP);
  862. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P2;
  863. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P2;
  864. /*--------------------------------------------------------------------------*/
  865. #if (FXAA_QUALITY_PS > 3)
  866. if(doneNP) {
  867. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  868. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  869. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  870. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  871. doneN = abs(lumaEndN) >= gradientScaled;
  872. doneP = abs(lumaEndP) >= gradientScaled;
  873. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P3;
  874. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P3;
  875. doneNP = (!doneN) || (!doneP);
  876. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P3;
  877. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P3;
  878. /*--------------------------------------------------------------------------*/
  879. #if (FXAA_QUALITY_PS > 4)
  880. if(doneNP) {
  881. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  882. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  883. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  884. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  885. doneN = abs(lumaEndN) >= gradientScaled;
  886. doneP = abs(lumaEndP) >= gradientScaled;
  887. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P4;
  888. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P4;
  889. doneNP = (!doneN) || (!doneP);
  890. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P4;
  891. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P4;
  892. /*--------------------------------------------------------------------------*/
  893. #if (FXAA_QUALITY_PS > 5)
  894. if(doneNP) {
  895. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  896. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  897. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  898. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  899. doneN = abs(lumaEndN) >= gradientScaled;
  900. doneP = abs(lumaEndP) >= gradientScaled;
  901. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P5;
  902. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P5;
  903. doneNP = (!doneN) || (!doneP);
  904. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P5;
  905. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P5;
  906. /*--------------------------------------------------------------------------*/
  907. #if (FXAA_QUALITY_PS > 6)
  908. if(doneNP) {
  909. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  910. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  911. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  912. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  913. doneN = abs(lumaEndN) >= gradientScaled;
  914. doneP = abs(lumaEndP) >= gradientScaled;
  915. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P6;
  916. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P6;
  917. doneNP = (!doneN) || (!doneP);
  918. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P6;
  919. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P6;
  920. /*--------------------------------------------------------------------------*/
  921. #if (FXAA_QUALITY_PS > 7)
  922. if(doneNP) {
  923. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  924. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  925. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  926. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  927. doneN = abs(lumaEndN) >= gradientScaled;
  928. doneP = abs(lumaEndP) >= gradientScaled;
  929. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P7;
  930. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P7;
  931. doneNP = (!doneN) || (!doneP);
  932. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P7;
  933. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P7;
  934. /*--------------------------------------------------------------------------*/
  935. #if (FXAA_QUALITY_PS > 8)
  936. if(doneNP) {
  937. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  938. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  939. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  940. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  941. doneN = abs(lumaEndN) >= gradientScaled;
  942. doneP = abs(lumaEndP) >= gradientScaled;
  943. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P8;
  944. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P8;
  945. doneNP = (!doneN) || (!doneP);
  946. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P8;
  947. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P8;
  948. /*--------------------------------------------------------------------------*/
  949. #if (FXAA_QUALITY_PS > 9)
  950. if(doneNP) {
  951. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  952. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  953. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  954. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  955. doneN = abs(lumaEndN) >= gradientScaled;
  956. doneP = abs(lumaEndP) >= gradientScaled;
  957. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P9;
  958. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P9;
  959. doneNP = (!doneN) || (!doneP);
  960. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P9;
  961. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P9;
  962. /*--------------------------------------------------------------------------*/
  963. #if (FXAA_QUALITY_PS > 10)
  964. if(doneNP) {
  965. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  966. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  967. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  968. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  969. doneN = abs(lumaEndN) >= gradientScaled;
  970. doneP = abs(lumaEndP) >= gradientScaled;
  971. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P10;
  972. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P10;
  973. doneNP = (!doneN) || (!doneP);
  974. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P10;
  975. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P10;
  976. /*--------------------------------------------------------------------------*/
  977. #if (FXAA_QUALITY_PS > 11)
  978. if(doneNP) {
  979. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  980. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  981. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  982. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  983. doneN = abs(lumaEndN) >= gradientScaled;
  984. doneP = abs(lumaEndP) >= gradientScaled;
  985. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P11;
  986. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P11;
  987. doneNP = (!doneN) || (!doneP);
  988. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P11;
  989. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P11;
  990. /*--------------------------------------------------------------------------*/
  991. #if (FXAA_QUALITY_PS > 12)
  992. if(doneNP) {
  993. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  994. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  995. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  996. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  997. doneN = abs(lumaEndN) >= gradientScaled;
  998. doneP = abs(lumaEndP) >= gradientScaled;
  999. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P12;
  1000. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P12;
  1001. doneNP = (!doneN) || (!doneP);
  1002. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P12;
  1003. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P12;
  1004. /*--------------------------------------------------------------------------*/
  1005. }
  1006. #endif
  1007. /*--------------------------------------------------------------------------*/
  1008. }
  1009. #endif
  1010. /*--------------------------------------------------------------------------*/
  1011. }
  1012. #endif
  1013. /*--------------------------------------------------------------------------*/
  1014. }
  1015. #endif
  1016. /*--------------------------------------------------------------------------*/
  1017. }
  1018. #endif
  1019. /*--------------------------------------------------------------------------*/
  1020. }
  1021. #endif
  1022. /*--------------------------------------------------------------------------*/
  1023. }
  1024. #endif
  1025. /*--------------------------------------------------------------------------*/
  1026. }
  1027. #endif
  1028. /*--------------------------------------------------------------------------*/
  1029. }
  1030. #endif
  1031. /*--------------------------------------------------------------------------*/
  1032. }
  1033. #endif
  1034. /*--------------------------------------------------------------------------*/
  1035. }
  1036. /*--------------------------------------------------------------------------*/
  1037. FxaaFloat dstN = posM.x - posN.x;
  1038. FxaaFloat dstP = posP.x - posM.x;
  1039. if(!horzSpan) dstN = posM.y - posN.y;
  1040. if(!horzSpan) dstP = posP.y - posM.y;
  1041. /*--------------------------------------------------------------------------*/
  1042. FxaaBool goodSpanN = (lumaEndN < 0.0) != lumaMLTZero;
  1043. FxaaFloat spanLength = (dstP + dstN);
  1044. FxaaBool goodSpanP = (lumaEndP < 0.0) != lumaMLTZero;
  1045. FxaaFloat spanLengthRcp = 1.0/spanLength;
  1046. /*--------------------------------------------------------------------------*/
  1047. FxaaBool directionN = dstN < dstP;
  1048. FxaaFloat dst = min(dstN, dstP);
  1049. FxaaBool goodSpan = directionN ? goodSpanN : goodSpanP;
  1050. FxaaFloat subpixG = subpixF * subpixF;
  1051. FxaaFloat pixelOffset = (dst * (-spanLengthRcp)) + 0.5;
  1052. FxaaFloat subpixH = subpixG * fxaaQualitySubpix;
  1053. /*--------------------------------------------------------------------------*/
  1054. FxaaFloat pixelOffsetGood = goodSpan ? pixelOffset : 0.0;
  1055. FxaaFloat pixelOffsetSubpix = max(pixelOffsetGood, subpixH);
  1056. if(!horzSpan) posM.x += pixelOffsetSubpix * lengthSign;
  1057. if( horzSpan) posM.y += pixelOffsetSubpix * lengthSign;
  1058. #if (FXAA_DISCARD == 1)
  1059. return FxaaTexTop(tex, posM);
  1060. #else
  1061. return FxaaFloat4(FxaaTexTop(tex, posM).xyz, lumaM);
  1062. #endif
  1063. }
  1064. /*==========================================================================*/
  1065. #endif
  1066. vec4 mainImage(vec2 fragCoord)
  1067. {
  1068. vec2 rcpFrame = 1./invResolution_data.xy;
  1069. vec2 uv2 = fragCoord.xy / invResolution_data.xy;
  1070. float fxaaQualitySubpix = 0.75; // [0..1], default 0.75
  1071. float fxaaQualityEdgeThreshold = 0.166; // [0.125..0.33], default 0.166
  1072. float fxaaQualityEdgeThresholdMin = 0.02;//0.0625; // ?
  1073. vec4 dummy4 = vec4(0.0,0.0,0.0,0.0);
  1074. float dummy1 = 0.0;
  1075. vec4 col = FxaaPixelShader(uv2, dummy4,
  1076. inputImage, inputImage, inputImage,
  1077. rcpFrame, dummy4, dummy4, dummy4,
  1078. fxaaQualitySubpix, fxaaQualityEdgeThreshold,
  1079. fxaaQualityEdgeThresholdMin,
  1080. dummy1, dummy1, dummy1, dummy4);
  1081. vec4 fragColor = vec4( col.xyz, 1. );
  1082. return fragColor;
  1083. }
  1084. void main()
  1085. {
  1086. ivec2 loc = ivec2(gl_GlobalInvocationID.x * 4, gl_GlobalInvocationID.y * 4);
  1087. for(int i = 0; i < 4; i++)
  1088. {
  1089. for(int j = 0; j < 4; j++)
  1090. {
  1091. ivec2 texelCoord = ivec2(loc.x + i, loc.y + j);
  1092. vec4 outColor = mainImage(texelCoord + vec2(0.5));
  1093. imageStore(imgOutput, texelCoord, outColor);
  1094. }
  1095. }
  1096. }