FXAAShader.js 45 KB

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