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