FXAAShader.js 45 KB

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