fxaa.glslf 43 KB

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