FXAA3_11.glsl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /**
  2. * @license
  3. * Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of NVIDIA CORPORATION nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  25. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. // NVIDIA GameWorks Graphics Samples GitHub link: https://github.com/NVIDIAGameWorks/GraphicsSamples
  30. // Original FXAA 3.11 shader link: https://github.com/NVIDIAGameWorks/GraphicsSamples/blob/master/samples/es3-kepler/FXAA/FXAA3_11.h
  31. // Steps used to integrate into Cesium:
  32. // * The following defines are set:
  33. // #define FXAA_PC 1
  34. // #define FXAA_WEBGL_1 1
  35. // #define FXAA_GREEN_AS_LUMA 1
  36. // #define FXAA_EARLY_EXIT 1
  37. // #define FXAA_GLSL_120 1
  38. // * All other preprocessor directives besides the FXAA_QUALITY__P* directives were removed.
  39. // * Double underscores are invalid for preprocessor directives so replace them with a single underscore. Replace
  40. // /FXAA_QUALITY__P(.*)/g with /FXAA_QUALITY__P$1/.
  41. // * There are no implicit conversions from ivec* to vec* so replace:
  42. // #define FxaaInt2 ivec2
  43. // with
  44. // #define FxaaInt2 vec2
  45. // * The texture2DLod function is only available in vertex shaders so replace:
  46. // #define FxaaTexTop(t, p) texture2DLod(t, p, 0.0)
  47. // #define FxaaTexOff(t, p, o, r) texture2DLod(t, p + (o * r), 0.0)
  48. // with
  49. // #define FxaaTexTop(t, p) texture2D(t, p)
  50. // #define FxaaTexOff(t, p, o, r) texture2D(t, p + (o * r))
  51. // * FXAA_QUALITY_PRESET is prepended in the javascript code. We may want to expose that setting in the future.
  52. // * The following parameters to FxaaPixelShader are unused and can be removed:
  53. // fxaaConsolePosPos
  54. // fxaaConsoleRcpFrameOpt
  55. // fxaaConsoleRcpFrameOpt2
  56. // fxaaConsole360RcpFrameOpt2
  57. // fxaaConsoleEdgeSharpness
  58. // fxaaConsoleEdgeThreshold
  59. // fxaaConsoleEdgeThresholdMi
  60. // fxaaConsole360ConstDir
  61. //
  62. // Choose the quality preset.
  63. // This needs to be compiled into the shader as it effects code.
  64. // Best option to include multiple presets is to
  65. // in each shader define the preset, then include this file.
  66. //
  67. // OPTIONS
  68. // -----------------------------------------------------------------------
  69. // 10 to 15 - default medium dither (10=fastest, 15=highest quality)
  70. // 20 to 29 - less dither, more expensive (20=fastest, 29=highest quality)
  71. // 39 - no dither, very expensive
  72. //
  73. // NOTES
  74. // -----------------------------------------------------------------------
  75. // 12 = slightly faster then FXAA 3.9 and higher edge quality (default)
  76. // 13 = about same speed as FXAA 3.9 and better than 12
  77. // 23 = closest to FXAA 3.9 visually and performance wise
  78. // _ = the lowest digit is directly related to performance
  79. // _ = the highest digit is directly related to style
  80. //
  81. //#define FXAA_QUALITY_PRESET 12
  82. #if (FXAA_QUALITY_PRESET == 10)
  83. #define FXAA_QUALITY_PS 3
  84. #define FXAA_QUALITY_P0 1.5
  85. #define FXAA_QUALITY_P1 3.0
  86. #define FXAA_QUALITY_P2 12.0
  87. #endif
  88. #if (FXAA_QUALITY_PRESET == 11)
  89. #define FXAA_QUALITY_PS 4
  90. #define FXAA_QUALITY_P0 1.0
  91. #define FXAA_QUALITY_P1 1.5
  92. #define FXAA_QUALITY_P2 3.0
  93. #define FXAA_QUALITY_P3 12.0
  94. #endif
  95. #if (FXAA_QUALITY_PRESET == 12)
  96. #define FXAA_QUALITY_PS 5
  97. #define FXAA_QUALITY_P0 1.0
  98. #define FXAA_QUALITY_P1 1.5
  99. #define FXAA_QUALITY_P2 2.0
  100. #define FXAA_QUALITY_P3 4.0
  101. #define FXAA_QUALITY_P4 12.0
  102. #endif
  103. #if (FXAA_QUALITY_PRESET == 13)
  104. #define FXAA_QUALITY_PS 6
  105. #define FXAA_QUALITY_P0 1.0
  106. #define FXAA_QUALITY_P1 1.5
  107. #define FXAA_QUALITY_P2 2.0
  108. #define FXAA_QUALITY_P3 2.0
  109. #define FXAA_QUALITY_P4 4.0
  110. #define FXAA_QUALITY_P5 12.0
  111. #endif
  112. #if (FXAA_QUALITY_PRESET == 14)
  113. #define FXAA_QUALITY_PS 7
  114. #define FXAA_QUALITY_P0 1.0
  115. #define FXAA_QUALITY_P1 1.5
  116. #define FXAA_QUALITY_P2 2.0
  117. #define FXAA_QUALITY_P3 2.0
  118. #define FXAA_QUALITY_P4 2.0
  119. #define FXAA_QUALITY_P5 4.0
  120. #define FXAA_QUALITY_P6 12.0
  121. #endif
  122. #if (FXAA_QUALITY_PRESET == 15)
  123. #define FXAA_QUALITY_PS 8
  124. #define FXAA_QUALITY_P0 1.0
  125. #define FXAA_QUALITY_P1 1.5
  126. #define FXAA_QUALITY_P2 2.0
  127. #define FXAA_QUALITY_P3 2.0
  128. #define FXAA_QUALITY_P4 2.0
  129. #define FXAA_QUALITY_P5 2.0
  130. #define FXAA_QUALITY_P6 4.0
  131. #define FXAA_QUALITY_P7 12.0
  132. #endif
  133. #if (FXAA_QUALITY_PRESET == 20)
  134. #define FXAA_QUALITY_PS 3
  135. #define FXAA_QUALITY_P0 1.5
  136. #define FXAA_QUALITY_P1 2.0
  137. #define FXAA_QUALITY_P2 8.0
  138. #endif
  139. #if (FXAA_QUALITY_PRESET == 21)
  140. #define FXAA_QUALITY_PS 4
  141. #define FXAA_QUALITY_P0 1.0
  142. #define FXAA_QUALITY_P1 1.5
  143. #define FXAA_QUALITY_P2 2.0
  144. #define FXAA_QUALITY_P3 8.0
  145. #endif
  146. #if (FXAA_QUALITY_PRESET == 22)
  147. #define FXAA_QUALITY_PS 5
  148. #define FXAA_QUALITY_P0 1.0
  149. #define FXAA_QUALITY_P1 1.5
  150. #define FXAA_QUALITY_P2 2.0
  151. #define FXAA_QUALITY_P3 2.0
  152. #define FXAA_QUALITY_P4 8.0
  153. #endif
  154. #if (FXAA_QUALITY_PRESET == 23)
  155. #define FXAA_QUALITY_PS 6
  156. #define FXAA_QUALITY_P0 1.0
  157. #define FXAA_QUALITY_P1 1.5
  158. #define FXAA_QUALITY_P2 2.0
  159. #define FXAA_QUALITY_P3 2.0
  160. #define FXAA_QUALITY_P4 2.0
  161. #define FXAA_QUALITY_P5 8.0
  162. #endif
  163. #if (FXAA_QUALITY_PRESET == 24)
  164. #define FXAA_QUALITY_PS 7
  165. #define FXAA_QUALITY_P0 1.0
  166. #define FXAA_QUALITY_P1 1.5
  167. #define FXAA_QUALITY_P2 2.0
  168. #define FXAA_QUALITY_P3 2.0
  169. #define FXAA_QUALITY_P4 2.0
  170. #define FXAA_QUALITY_P5 3.0
  171. #define FXAA_QUALITY_P6 8.0
  172. #endif
  173. #if (FXAA_QUALITY_PRESET == 25)
  174. #define FXAA_QUALITY_PS 8
  175. #define FXAA_QUALITY_P0 1.0
  176. #define FXAA_QUALITY_P1 1.5
  177. #define FXAA_QUALITY_P2 2.0
  178. #define FXAA_QUALITY_P3 2.0
  179. #define FXAA_QUALITY_P4 2.0
  180. #define FXAA_QUALITY_P5 2.0
  181. #define FXAA_QUALITY_P6 4.0
  182. #define FXAA_QUALITY_P7 8.0
  183. #endif
  184. #if (FXAA_QUALITY_PRESET == 26)
  185. #define FXAA_QUALITY_PS 9
  186. #define FXAA_QUALITY_P0 1.0
  187. #define FXAA_QUALITY_P1 1.5
  188. #define FXAA_QUALITY_P2 2.0
  189. #define FXAA_QUALITY_P3 2.0
  190. #define FXAA_QUALITY_P4 2.0
  191. #define FXAA_QUALITY_P5 2.0
  192. #define FXAA_QUALITY_P6 2.0
  193. #define FXAA_QUALITY_P7 4.0
  194. #define FXAA_QUALITY_P8 8.0
  195. #endif
  196. #if (FXAA_QUALITY_PRESET == 27)
  197. #define FXAA_QUALITY_PS 10
  198. #define FXAA_QUALITY_P0 1.0
  199. #define FXAA_QUALITY_P1 1.5
  200. #define FXAA_QUALITY_P2 2.0
  201. #define FXAA_QUALITY_P3 2.0
  202. #define FXAA_QUALITY_P4 2.0
  203. #define FXAA_QUALITY_P5 2.0
  204. #define FXAA_QUALITY_P6 2.0
  205. #define FXAA_QUALITY_P7 2.0
  206. #define FXAA_QUALITY_P8 4.0
  207. #define FXAA_QUALITY_P9 8.0
  208. #endif
  209. #if (FXAA_QUALITY_PRESET == 28)
  210. #define FXAA_QUALITY_PS 11
  211. #define FXAA_QUALITY_P0 1.0
  212. #define FXAA_QUALITY_P1 1.5
  213. #define FXAA_QUALITY_P2 2.0
  214. #define FXAA_QUALITY_P3 2.0
  215. #define FXAA_QUALITY_P4 2.0
  216. #define FXAA_QUALITY_P5 2.0
  217. #define FXAA_QUALITY_P6 2.0
  218. #define FXAA_QUALITY_P7 2.0
  219. #define FXAA_QUALITY_P8 2.0
  220. #define FXAA_QUALITY_P9 4.0
  221. #define FXAA_QUALITY_P10 8.0
  222. #endif
  223. #if (FXAA_QUALITY_PRESET == 29)
  224. #define FXAA_QUALITY_PS 12
  225. #define FXAA_QUALITY_P0 1.0
  226. #define FXAA_QUALITY_P1 1.5
  227. #define FXAA_QUALITY_P2 2.0
  228. #define FXAA_QUALITY_P3 2.0
  229. #define FXAA_QUALITY_P4 2.0
  230. #define FXAA_QUALITY_P5 2.0
  231. #define FXAA_QUALITY_P6 2.0
  232. #define FXAA_QUALITY_P7 2.0
  233. #define FXAA_QUALITY_P8 2.0
  234. #define FXAA_QUALITY_P9 2.0
  235. #define FXAA_QUALITY_P10 4.0
  236. #define FXAA_QUALITY_P11 8.0
  237. #endif
  238. #if (FXAA_QUALITY_PRESET == 39)
  239. #define FXAA_QUALITY_PS 12
  240. #define FXAA_QUALITY_P0 1.0
  241. #define FXAA_QUALITY_P1 1.0
  242. #define FXAA_QUALITY_P2 1.0
  243. #define FXAA_QUALITY_P3 1.0
  244. #define FXAA_QUALITY_P4 1.0
  245. #define FXAA_QUALITY_P5 1.5
  246. #define FXAA_QUALITY_P6 2.0
  247. #define FXAA_QUALITY_P7 2.0
  248. #define FXAA_QUALITY_P8 2.0
  249. #define FXAA_QUALITY_P9 2.0
  250. #define FXAA_QUALITY_P10 4.0
  251. #define FXAA_QUALITY_P11 8.0
  252. #endif
  253. #define FxaaBool bool
  254. #define FxaaFloat float
  255. #define FxaaFloat2 vec2
  256. #define FxaaFloat3 vec3
  257. #define FxaaFloat4 vec4
  258. #define FxaaHalf float
  259. #define FxaaHalf2 vec2
  260. #define FxaaHalf3 vec3
  261. #define FxaaHalf4 vec4
  262. #define FxaaInt2 vec2
  263. #define FxaaTex sampler2D
  264. #define FxaaSat(x) clamp(x, 0.0, 1.0)
  265. #define FxaaTexTop(t, p) texture2D(t, p)
  266. #define FxaaTexOff(t, p, o, r) texture2D(t, p + (o * r))
  267. FxaaFloat FxaaLuma(FxaaFloat4 rgba) { return rgba.y; }
  268. FxaaFloat4 FxaaPixelShader(
  269. //
  270. // Use noperspective interpolation here (turn off perspective interpolation).
  271. // {xy} = center of pixel
  272. FxaaFloat2 pos,
  273. //
  274. // Input color texture.
  275. // {rgb_} = color in linear or perceptual color space
  276. // if (FXAA_GREEN_AS_LUMA == 0)
  277. // {___a} = luma in perceptual color space (not linear)
  278. FxaaTex tex,
  279. //
  280. // Only used on FXAA Quality.
  281. // This must be from a constant/uniform.
  282. // {x_} = 1.0/screenWidthInPixels
  283. // {_y} = 1.0/screenHeightInPixels
  284. FxaaFloat2 fxaaQualityRcpFrame,
  285. //
  286. // Only used on FXAA Quality.
  287. // This used to be the FXAA_QUALITY_SUBPIX define.
  288. // It is here now to allow easier tuning.
  289. // Choose the amount of sub-pixel aliasing removal.
  290. // This can effect sharpness.
  291. // 1.00 - upper limit (softer)
  292. // 0.75 - default amount of filtering
  293. // 0.50 - lower limit (sharper, less sub-pixel aliasing removal)
  294. // 0.25 - almost off
  295. // 0.00 - completely off
  296. FxaaFloat fxaaQualitySubpix,
  297. //
  298. // Only used on FXAA Quality.
  299. // This used to be the FXAA_QUALITY_EDGE_THRESHOLD define.
  300. // It is here now to allow easier tuning.
  301. // The minimum amount of local contrast required to apply algorithm.
  302. // 0.333 - too little (faster)
  303. // 0.250 - low quality
  304. // 0.166 - default
  305. // 0.125 - high quality
  306. // 0.063 - overkill (slower)
  307. FxaaFloat fxaaQualityEdgeThreshold,
  308. //
  309. // Only used on FXAA Quality.
  310. // This used to be the FXAA_QUALITY_EDGE_THRESHOLD_MIN define.
  311. // It is here now to allow easier tuning.
  312. // Trims the algorithm from processing darks.
  313. // 0.0833 - upper limit (default, the start of visible unfiltered edges)
  314. // 0.0625 - high quality (faster)
  315. // 0.0312 - visible limit (slower)
  316. // Special notes when using FXAA_GREEN_AS_LUMA,
  317. // Likely want to set this to zero.
  318. // As colors that are mostly not-green
  319. // will appear very dark in the green channel!
  320. // Tune by looking at mostly non-green content,
  321. // then start at zero and increase until aliasing is a problem.
  322. FxaaFloat fxaaQualityEdgeThresholdMin
  323. ) {
  324. /*--------------------------------------------------------------------------*/
  325. FxaaFloat2 posM;
  326. posM.x = pos.x;
  327. posM.y = pos.y;
  328. FxaaFloat4 rgbyM = FxaaTexTop(tex, posM);
  329. #define lumaM rgbyM.y
  330. FxaaFloat lumaS = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 0, 1), fxaaQualityRcpFrame.xy));
  331. FxaaFloat lumaE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1, 0), fxaaQualityRcpFrame.xy));
  332. FxaaFloat lumaN = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 0,-1), fxaaQualityRcpFrame.xy));
  333. FxaaFloat lumaW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 0), fxaaQualityRcpFrame.xy));
  334. /*--------------------------------------------------------------------------*/
  335. FxaaFloat maxSM = max(lumaS, lumaM);
  336. FxaaFloat minSM = min(lumaS, lumaM);
  337. FxaaFloat maxESM = max(lumaE, maxSM);
  338. FxaaFloat minESM = min(lumaE, minSM);
  339. FxaaFloat maxWN = max(lumaN, lumaW);
  340. FxaaFloat minWN = min(lumaN, lumaW);
  341. FxaaFloat rangeMax = max(maxWN, maxESM);
  342. FxaaFloat rangeMin = min(minWN, minESM);
  343. FxaaFloat rangeMaxScaled = rangeMax * fxaaQualityEdgeThreshold;
  344. FxaaFloat range = rangeMax - rangeMin;
  345. FxaaFloat rangeMaxClamped = max(fxaaQualityEdgeThresholdMin, rangeMaxScaled);
  346. FxaaBool earlyExit = range < rangeMaxClamped;
  347. /*--------------------------------------------------------------------------*/
  348. if(earlyExit)
  349. return rgbyM;
  350. /*--------------------------------------------------------------------------*/
  351. FxaaFloat lumaNW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1,-1), fxaaQualityRcpFrame.xy));
  352. FxaaFloat lumaSE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1, 1), fxaaQualityRcpFrame.xy));
  353. FxaaFloat lumaNE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1,-1), fxaaQualityRcpFrame.xy));
  354. FxaaFloat lumaSW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 1), fxaaQualityRcpFrame.xy));
  355. /*--------------------------------------------------------------------------*/
  356. FxaaFloat lumaNS = lumaN + lumaS;
  357. FxaaFloat lumaWE = lumaW + lumaE;
  358. FxaaFloat subpixRcpRange = 1.0/range;
  359. FxaaFloat subpixNSWE = lumaNS + lumaWE;
  360. FxaaFloat edgeHorz1 = (-2.0 * lumaM) + lumaNS;
  361. FxaaFloat edgeVert1 = (-2.0 * lumaM) + lumaWE;
  362. /*--------------------------------------------------------------------------*/
  363. FxaaFloat lumaNESE = lumaNE + lumaSE;
  364. FxaaFloat lumaNWNE = lumaNW + lumaNE;
  365. FxaaFloat edgeHorz2 = (-2.0 * lumaE) + lumaNESE;
  366. FxaaFloat edgeVert2 = (-2.0 * lumaN) + lumaNWNE;
  367. /*--------------------------------------------------------------------------*/
  368. FxaaFloat lumaNWSW = lumaNW + lumaSW;
  369. FxaaFloat lumaSWSE = lumaSW + lumaSE;
  370. FxaaFloat edgeHorz4 = (abs(edgeHorz1) * 2.0) + abs(edgeHorz2);
  371. FxaaFloat edgeVert4 = (abs(edgeVert1) * 2.0) + abs(edgeVert2);
  372. FxaaFloat edgeHorz3 = (-2.0 * lumaW) + lumaNWSW;
  373. FxaaFloat edgeVert3 = (-2.0 * lumaS) + lumaSWSE;
  374. FxaaFloat edgeHorz = abs(edgeHorz3) + edgeHorz4;
  375. FxaaFloat edgeVert = abs(edgeVert3) + edgeVert4;
  376. /*--------------------------------------------------------------------------*/
  377. FxaaFloat subpixNWSWNESE = lumaNWSW + lumaNESE;
  378. FxaaFloat lengthSign = fxaaQualityRcpFrame.x;
  379. FxaaBool horzSpan = edgeHorz >= edgeVert;
  380. FxaaFloat subpixA = subpixNSWE * 2.0 + subpixNWSWNESE;
  381. /*--------------------------------------------------------------------------*/
  382. if(!horzSpan) lumaN = lumaW;
  383. if(!horzSpan) lumaS = lumaE;
  384. if(horzSpan) lengthSign = fxaaQualityRcpFrame.y;
  385. FxaaFloat subpixB = (subpixA * (1.0/12.0)) - lumaM;
  386. /*--------------------------------------------------------------------------*/
  387. FxaaFloat gradientN = lumaN - lumaM;
  388. FxaaFloat gradientS = lumaS - lumaM;
  389. FxaaFloat lumaNN = lumaN + lumaM;
  390. FxaaFloat lumaSS = lumaS + lumaM;
  391. FxaaBool pairN = abs(gradientN) >= abs(gradientS);
  392. FxaaFloat gradient = max(abs(gradientN), abs(gradientS));
  393. if(pairN) lengthSign = -lengthSign;
  394. FxaaFloat subpixC = FxaaSat(abs(subpixB) * subpixRcpRange);
  395. /*--------------------------------------------------------------------------*/
  396. FxaaFloat2 posB;
  397. posB.x = posM.x;
  398. posB.y = posM.y;
  399. FxaaFloat2 offNP;
  400. offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x;
  401. offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y;
  402. if(!horzSpan) posB.x += lengthSign * 0.5;
  403. if( horzSpan) posB.y += lengthSign * 0.5;
  404. /*--------------------------------------------------------------------------*/
  405. FxaaFloat2 posN;
  406. posN.x = posB.x - offNP.x * FXAA_QUALITY_P0;
  407. posN.y = posB.y - offNP.y * FXAA_QUALITY_P0;
  408. FxaaFloat2 posP;
  409. posP.x = posB.x + offNP.x * FXAA_QUALITY_P0;
  410. posP.y = posB.y + offNP.y * FXAA_QUALITY_P0;
  411. FxaaFloat subpixD = ((-2.0)*subpixC) + 3.0;
  412. FxaaFloat lumaEndN = FxaaLuma(FxaaTexTop(tex, posN));
  413. FxaaFloat subpixE = subpixC * subpixC;
  414. FxaaFloat lumaEndP = FxaaLuma(FxaaTexTop(tex, posP));
  415. /*--------------------------------------------------------------------------*/
  416. if(!pairN) lumaNN = lumaSS;
  417. FxaaFloat gradientScaled = gradient * 1.0/4.0;
  418. FxaaFloat lumaMM = lumaM - lumaNN * 0.5;
  419. FxaaFloat subpixF = subpixD * subpixE;
  420. FxaaBool lumaMLTZero = lumaMM < 0.0;
  421. /*--------------------------------------------------------------------------*/
  422. lumaEndN -= lumaNN * 0.5;
  423. lumaEndP -= lumaNN * 0.5;
  424. FxaaBool doneN = abs(lumaEndN) >= gradientScaled;
  425. FxaaBool doneP = abs(lumaEndP) >= gradientScaled;
  426. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P1;
  427. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P1;
  428. FxaaBool doneNP = (!doneN) || (!doneP);
  429. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P1;
  430. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P1;
  431. /*--------------------------------------------------------------------------*/
  432. if(doneNP) {
  433. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  434. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  435. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  436. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  437. doneN = abs(lumaEndN) >= gradientScaled;
  438. doneP = abs(lumaEndP) >= gradientScaled;
  439. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P2;
  440. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P2;
  441. doneNP = (!doneN) || (!doneP);
  442. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P2;
  443. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P2;
  444. /*--------------------------------------------------------------------------*/
  445. #if (FXAA_QUALITY_PS > 3)
  446. if(doneNP) {
  447. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  448. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  449. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  450. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  451. doneN = abs(lumaEndN) >= gradientScaled;
  452. doneP = abs(lumaEndP) >= gradientScaled;
  453. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P3;
  454. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P3;
  455. doneNP = (!doneN) || (!doneP);
  456. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P3;
  457. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P3;
  458. /*--------------------------------------------------------------------------*/
  459. #if (FXAA_QUALITY_PS > 4)
  460. if(doneNP) {
  461. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  462. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  463. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  464. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  465. doneN = abs(lumaEndN) >= gradientScaled;
  466. doneP = abs(lumaEndP) >= gradientScaled;
  467. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P4;
  468. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P4;
  469. doneNP = (!doneN) || (!doneP);
  470. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P4;
  471. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P4;
  472. /*--------------------------------------------------------------------------*/
  473. #if (FXAA_QUALITY_PS > 5)
  474. if(doneNP) {
  475. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  476. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  477. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  478. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  479. doneN = abs(lumaEndN) >= gradientScaled;
  480. doneP = abs(lumaEndP) >= gradientScaled;
  481. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P5;
  482. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P5;
  483. doneNP = (!doneN) || (!doneP);
  484. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P5;
  485. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P5;
  486. /*--------------------------------------------------------------------------*/
  487. #if (FXAA_QUALITY_PS > 6)
  488. if(doneNP) {
  489. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  490. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  491. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  492. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  493. doneN = abs(lumaEndN) >= gradientScaled;
  494. doneP = abs(lumaEndP) >= gradientScaled;
  495. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P6;
  496. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P6;
  497. doneNP = (!doneN) || (!doneP);
  498. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P6;
  499. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P6;
  500. /*--------------------------------------------------------------------------*/
  501. #if (FXAA_QUALITY_PS > 7)
  502. if(doneNP) {
  503. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  504. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  505. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  506. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  507. doneN = abs(lumaEndN) >= gradientScaled;
  508. doneP = abs(lumaEndP) >= gradientScaled;
  509. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P7;
  510. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P7;
  511. doneNP = (!doneN) || (!doneP);
  512. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P7;
  513. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P7;
  514. /*--------------------------------------------------------------------------*/
  515. #if (FXAA_QUALITY_PS > 8)
  516. if(doneNP) {
  517. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  518. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  519. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  520. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  521. doneN = abs(lumaEndN) >= gradientScaled;
  522. doneP = abs(lumaEndP) >= gradientScaled;
  523. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P8;
  524. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P8;
  525. doneNP = (!doneN) || (!doneP);
  526. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P8;
  527. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P8;
  528. /*--------------------------------------------------------------------------*/
  529. #if (FXAA_QUALITY_PS > 9)
  530. if(doneNP) {
  531. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  532. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  533. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  534. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  535. doneN = abs(lumaEndN) >= gradientScaled;
  536. doneP = abs(lumaEndP) >= gradientScaled;
  537. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P9;
  538. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P9;
  539. doneNP = (!doneN) || (!doneP);
  540. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P9;
  541. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P9;
  542. /*--------------------------------------------------------------------------*/
  543. #if (FXAA_QUALITY_PS > 10)
  544. if(doneNP) {
  545. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  546. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  547. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  548. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  549. doneN = abs(lumaEndN) >= gradientScaled;
  550. doneP = abs(lumaEndP) >= gradientScaled;
  551. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P10;
  552. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P10;
  553. doneNP = (!doneN) || (!doneP);
  554. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P10;
  555. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P10;
  556. /*--------------------------------------------------------------------------*/
  557. #if (FXAA_QUALITY_PS > 11)
  558. if(doneNP) {
  559. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  560. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  561. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  562. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  563. doneN = abs(lumaEndN) >= gradientScaled;
  564. doneP = abs(lumaEndP) >= gradientScaled;
  565. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P11;
  566. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P11;
  567. doneNP = (!doneN) || (!doneP);
  568. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P11;
  569. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P11;
  570. /*--------------------------------------------------------------------------*/
  571. #if (FXAA_QUALITY_PS > 12)
  572. if(doneNP) {
  573. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  574. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  575. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  576. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  577. doneN = abs(lumaEndN) >= gradientScaled;
  578. doneP = abs(lumaEndP) >= gradientScaled;
  579. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P12;
  580. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P12;
  581. doneNP = (!doneN) || (!doneP);
  582. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P12;
  583. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P12;
  584. /*--------------------------------------------------------------------------*/
  585. }
  586. #endif
  587. /*--------------------------------------------------------------------------*/
  588. }
  589. #endif
  590. /*--------------------------------------------------------------------------*/
  591. }
  592. #endif
  593. /*--------------------------------------------------------------------------*/
  594. }
  595. #endif
  596. /*--------------------------------------------------------------------------*/
  597. }
  598. #endif
  599. /*--------------------------------------------------------------------------*/
  600. }
  601. #endif
  602. /*--------------------------------------------------------------------------*/
  603. }
  604. #endif
  605. /*--------------------------------------------------------------------------*/
  606. }
  607. #endif
  608. /*--------------------------------------------------------------------------*/
  609. }
  610. #endif
  611. /*--------------------------------------------------------------------------*/
  612. }
  613. #endif
  614. /*--------------------------------------------------------------------------*/
  615. }
  616. /*--------------------------------------------------------------------------*/
  617. FxaaFloat dstN = posM.x - posN.x;
  618. FxaaFloat dstP = posP.x - posM.x;
  619. if(!horzSpan) dstN = posM.y - posN.y;
  620. if(!horzSpan) dstP = posP.y - posM.y;
  621. /*--------------------------------------------------------------------------*/
  622. FxaaBool goodSpanN = (lumaEndN < 0.0) != lumaMLTZero;
  623. FxaaFloat spanLength = (dstP + dstN);
  624. FxaaBool goodSpanP = (lumaEndP < 0.0) != lumaMLTZero;
  625. FxaaFloat spanLengthRcp = 1.0/spanLength;
  626. /*--------------------------------------------------------------------------*/
  627. FxaaBool directionN = dstN < dstP;
  628. FxaaFloat dst = min(dstN, dstP);
  629. FxaaBool goodSpan = directionN ? goodSpanN : goodSpanP;
  630. FxaaFloat subpixG = subpixF * subpixF;
  631. FxaaFloat pixelOffset = (dst * (-spanLengthRcp)) + 0.5;
  632. FxaaFloat subpixH = subpixG * fxaaQualitySubpix;
  633. /*--------------------------------------------------------------------------*/
  634. FxaaFloat pixelOffsetGood = goodSpan ? pixelOffset : 0.0;
  635. FxaaFloat pixelOffsetSubpix = max(pixelOffsetGood, subpixH);
  636. if(!horzSpan) posM.x += pixelOffsetSubpix * lengthSign;
  637. if( horzSpan) posM.y += pixelOffsetSubpix * lengthSign;
  638. return FxaaFloat4(FxaaTexTop(tex, posM).xyz, lumaM);
  639. }