FXAAShader.js 58 KB

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