FXAAShader.js 45 KB

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