FXAAShader.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. import {
  2. Vector2
  3. } from '../../../build/three.module.js';
  4. /**
  5. * NVIDIA FXAA by Timothy Lottes
  6. * http://timothylottes.blogspot.com/2011/06/fxaa3-source-released.html
  7. * - WebGL port by @supereggbert
  8. * http://www.glge.org/demos/fxaa/
  9. */
  10. const FXAAShader = {
  11. uniforms: {
  12. 'tDiffuse': { value: null },
  13. 'resolution': { value: new Vector2( 1 / 1024, 1 / 512 ) }
  14. },
  15. vertexShader:
  16. `varying vec2 vUv;
  17. void main() {
  18. vUv = uv;
  19. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  20. }`,
  21. fragmentShader:
  22. // 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. export { FXAAShader };