FXAA3.glsl 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. //----------------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 2014, 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. //----------------------------------------------------------------------------------
  30. /*============================================================================
  31. NVIDIA FXAA 3.11 by TIMOTHY LOTTES
  32. ------------------------------------------------------------------------------
  33. Modified for Urho3D
  34. ============================================================================*/
  35. /*==========================================================================*/
  36. //
  37. // Urho3D specific preparations
  38. //
  39. /*--------------------------------------------------------------------------*/
  40. #include "Uniforms.glsl"
  41. #include "Samplers.glsl"
  42. #include "Transform.glsl"
  43. #include "ScreenPos.glsl"
  44. varying vec2 vScreenPos;
  45. /*==========================================================================*/
  46. //
  47. // Shader Requirements
  48. //
  49. /*--------------------------------------------------------------------------*/
  50. #define FXAA_FAST_PIXEL_OFFSET 0
  51. /*--------------------------------------------------------------------------*/
  52. #if (FXAA_FAST_PIXEL_OFFSET == 1)
  53. #define Fxaa_vec2 ivec2
  54. #else
  55. #define Fxaa_vec2 vec2
  56. #endif
  57. /*============================================================================
  58. FXAA QUALITY - TUNING KNOBS
  59. ------------------------------------------------------------------------------
  60. NOTE the other tuning knobs are now in the shader function inputs!
  61. ============================================================================*/
  62. #ifndef FXAA_QUALITY_PRESET
  63. //
  64. // Choose the quality preset.
  65. // This needs to be compiled into the shader as it effects code.
  66. // Best option to include multiple presets is to
  67. // in each shader define the preset, then include this file.
  68. //
  69. // OPTIONS
  70. // -----------------------------------------------------------------------
  71. // 10 to 15 - default medium dither (10=fastest, 15=highest quality)
  72. // 20 to 29 - less dither, more expensive (20=fastest, 29=highest quality)
  73. // 39 - no dither, very expensive
  74. //
  75. // NOTES
  76. // -----------------------------------------------------------------------
  77. // 12 = slightly faster then FXAA 3.9 and higher edge quality (default)
  78. // 13 = about same speed as FXAA 3.9 and better than 12
  79. // 23 = closest to FXAA 3.9 visually and performance wise
  80. // _ = the lowest digit is directly related to performance
  81. // _ = the highest digit is directly related to style
  82. //
  83. #define FXAA_QUALITY_PRESET 12
  84. #endif
  85. /*============================================================================
  86. FXAA QUALITY - PRESETS
  87. ============================================================================*/
  88. /*============================================================================
  89. FXAA QUALITY - MEDIUM DITHER PRESETS
  90. ============================================================================*/
  91. #if (FXAA_QUALITY_PRESET == 10)
  92. #define FXAA_QUALITY_PS 3
  93. #define FXAA_QUALITY_P0 1.5
  94. #define FXAA_QUALITY_P1 3.0
  95. #define FXAA_QUALITY_P2 12.0
  96. #endif
  97. /*--------------------------------------------------------------------------*/
  98. #if (FXAA_QUALITY_PRESET == 11)
  99. #define FXAA_QUALITY_PS 4
  100. #define FXAA_QUALITY_P0 1.0
  101. #define FXAA_QUALITY_P1 1.5
  102. #define FXAA_QUALITY_P2 3.0
  103. #define FXAA_QUALITY_P3 12.0
  104. #endif
  105. /*--------------------------------------------------------------------------*/
  106. #if (FXAA_QUALITY_PRESET == 12)
  107. #define FXAA_QUALITY_PS 5
  108. #define FXAA_QUALITY_P0 1.0
  109. #define FXAA_QUALITY_P1 1.5
  110. #define FXAA_QUALITY_P2 2.0
  111. #define FXAA_QUALITY_P3 4.0
  112. #define FXAA_QUALITY_P4 12.0
  113. #endif
  114. /*--------------------------------------------------------------------------*/
  115. #if (FXAA_QUALITY_PRESET == 13)
  116. #define FXAA_QUALITY_PS 6
  117. #define FXAA_QUALITY_P0 1.0
  118. #define FXAA_QUALITY_P1 1.5
  119. #define FXAA_QUALITY_P2 2.0
  120. #define FXAA_QUALITY_P3 2.0
  121. #define FXAA_QUALITY_P4 4.0
  122. #define FXAA_QUALITY_P5 12.0
  123. #endif
  124. /*--------------------------------------------------------------------------*/
  125. #if (FXAA_QUALITY_PRESET == 14)
  126. #define FXAA_QUALITY_PS 7
  127. #define FXAA_QUALITY_P0 1.0
  128. #define FXAA_QUALITY_P1 1.5
  129. #define FXAA_QUALITY_P2 2.0
  130. #define FXAA_QUALITY_P3 2.0
  131. #define FXAA_QUALITY_P4 2.0
  132. #define FXAA_QUALITY_P5 4.0
  133. #define FXAA_QUALITY_P6 12.0
  134. #endif
  135. /*--------------------------------------------------------------------------*/
  136. #if (FXAA_QUALITY_PRESET == 15)
  137. #define FXAA_QUALITY_PS 8
  138. #define FXAA_QUALITY_P0 1.0
  139. #define FXAA_QUALITY_P1 1.5
  140. #define FXAA_QUALITY_P2 2.0
  141. #define FXAA_QUALITY_P3 2.0
  142. #define FXAA_QUALITY_P4 2.0
  143. #define FXAA_QUALITY_P5 2.0
  144. #define FXAA_QUALITY_P6 4.0
  145. #define FXAA_QUALITY_P7 12.0
  146. #endif
  147. /*============================================================================
  148. FXAA QUALITY - LOW DITHER PRESETS
  149. ============================================================================*/
  150. #if (FXAA_QUALITY_PRESET == 20)
  151. #define FXAA_QUALITY_PS 3
  152. #define FXAA_QUALITY_P0 1.5
  153. #define FXAA_QUALITY_P1 2.0
  154. #define FXAA_QUALITY_P2 8.0
  155. #endif
  156. /*--------------------------------------------------------------------------*/
  157. #if (FXAA_QUALITY_PRESET == 21)
  158. #define FXAA_QUALITY_PS 4
  159. #define FXAA_QUALITY_P0 1.0
  160. #define FXAA_QUALITY_P1 1.5
  161. #define FXAA_QUALITY_P2 2.0
  162. #define FXAA_QUALITY_P3 8.0
  163. #endif
  164. /*--------------------------------------------------------------------------*/
  165. #if (FXAA_QUALITY_PRESET == 22)
  166. #define FXAA_QUALITY_PS 5
  167. #define FXAA_QUALITY_P0 1.0
  168. #define FXAA_QUALITY_P1 1.5
  169. #define FXAA_QUALITY_P2 2.0
  170. #define FXAA_QUALITY_P3 2.0
  171. #define FXAA_QUALITY_P4 8.0
  172. #endif
  173. /*--------------------------------------------------------------------------*/
  174. #if (FXAA_QUALITY_PRESET == 23)
  175. #define FXAA_QUALITY_PS 6
  176. #define FXAA_QUALITY_P0 1.0
  177. #define FXAA_QUALITY_P1 1.5
  178. #define FXAA_QUALITY_P2 2.0
  179. #define FXAA_QUALITY_P3 2.0
  180. #define FXAA_QUALITY_P4 2.0
  181. #define FXAA_QUALITY_P5 8.0
  182. #endif
  183. /*--------------------------------------------------------------------------*/
  184. #if (FXAA_QUALITY_PRESET == 24)
  185. #define FXAA_QUALITY_PS 7
  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 3.0
  192. #define FXAA_QUALITY_P6 8.0
  193. #endif
  194. /*--------------------------------------------------------------------------*/
  195. #if (FXAA_QUALITY_PRESET == 25)
  196. #define FXAA_QUALITY_PS 8
  197. #define FXAA_QUALITY_P0 1.0
  198. #define FXAA_QUALITY_P1 1.5
  199. #define FXAA_QUALITY_P2 2.0
  200. #define FXAA_QUALITY_P3 2.0
  201. #define FXAA_QUALITY_P4 2.0
  202. #define FXAA_QUALITY_P5 2.0
  203. #define FXAA_QUALITY_P6 4.0
  204. #define FXAA_QUALITY_P7 8.0
  205. #endif
  206. /*--------------------------------------------------------------------------*/
  207. #if (FXAA_QUALITY_PRESET == 26)
  208. #define FXAA_QUALITY_PS 9
  209. #define FXAA_QUALITY_P0 1.0
  210. #define FXAA_QUALITY_P1 1.5
  211. #define FXAA_QUALITY_P2 2.0
  212. #define FXAA_QUALITY_P3 2.0
  213. #define FXAA_QUALITY_P4 2.0
  214. #define FXAA_QUALITY_P5 2.0
  215. #define FXAA_QUALITY_P6 2.0
  216. #define FXAA_QUALITY_P7 4.0
  217. #define FXAA_QUALITY_P8 8.0
  218. #endif
  219. /*--------------------------------------------------------------------------*/
  220. #if (FXAA_QUALITY_PRESET == 27)
  221. #define FXAA_QUALITY_PS 10
  222. #define FXAA_QUALITY_P0 1.0
  223. #define FXAA_QUALITY_P1 1.5
  224. #define FXAA_QUALITY_P2 2.0
  225. #define FXAA_QUALITY_P3 2.0
  226. #define FXAA_QUALITY_P4 2.0
  227. #define FXAA_QUALITY_P5 2.0
  228. #define FXAA_QUALITY_P6 2.0
  229. #define FXAA_QUALITY_P7 2.0
  230. #define FXAA_QUALITY_P8 4.0
  231. #define FXAA_QUALITY_P9 8.0
  232. #endif
  233. /*--------------------------------------------------------------------------*/
  234. #if (FXAA_QUALITY_PRESET == 28)
  235. #define FXAA_QUALITY_PS 11
  236. #define FXAA_QUALITY_P0 1.0
  237. #define FXAA_QUALITY_P1 1.5
  238. #define FXAA_QUALITY_P2 2.0
  239. #define FXAA_QUALITY_P3 2.0
  240. #define FXAA_QUALITY_P4 2.0
  241. #define FXAA_QUALITY_P5 2.0
  242. #define FXAA_QUALITY_P6 2.0
  243. #define FXAA_QUALITY_P7 2.0
  244. #define FXAA_QUALITY_P8 2.0
  245. #define FXAA_QUALITY_P9 4.0
  246. #define FXAA_QUALITY_P10 8.0
  247. #endif
  248. /*--------------------------------------------------------------------------*/
  249. #if (FXAA_QUALITY_PRESET == 29)
  250. #define FXAA_QUALITY_PS 12
  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 2.0
  255. #define FXAA_QUALITY_P4 2.0
  256. #define FXAA_QUALITY_P5 2.0
  257. #define FXAA_QUALITY_P6 2.0
  258. #define FXAA_QUALITY_P7 2.0
  259. #define FXAA_QUALITY_P8 2.0
  260. #define FXAA_QUALITY_P9 2.0
  261. #define FXAA_QUALITY_P10 4.0
  262. #define FXAA_QUALITY_P11 8.0
  263. #endif
  264. /*============================================================================
  265. FXAA QUALITY - EXTREME QUALITY
  266. ============================================================================*/
  267. #if (FXAA_QUALITY_PRESET == 39)
  268. #define FXAA_QUALITY_PS 12
  269. #define FXAA_QUALITY_P0 1.0
  270. #define FXAA_QUALITY_P1 1.0
  271. #define FXAA_QUALITY_P2 1.0
  272. #define FXAA_QUALITY_P3 1.0
  273. #define FXAA_QUALITY_P4 1.0
  274. #define FXAA_QUALITY_P5 1.5
  275. #define FXAA_QUALITY_P6 2.0
  276. #define FXAA_QUALITY_P7 2.0
  277. #define FXAA_QUALITY_P8 2.0
  278. #define FXAA_QUALITY_P9 2.0
  279. #define FXAA_QUALITY_P10 4.0
  280. #define FXAA_QUALITY_P11 8.0
  281. #endif
  282. /*============================================================================
  283. Support Functions
  284. ============================================================================*/
  285. float CalcLuma(vec3 rgb)
  286. {
  287. vec3 luma = vec3(0.299, 0.587, 0.114);
  288. return dot(rgb, luma);
  289. }
  290. /*--------------------------------------------------------------------------*/
  291. #define FxaaTexTop(t, p) vec4(texture2DLod(t, p, 0.0).rgb, 1.0)
  292. #define LumaTop(t, p) CalcLuma(texture2DLod(t, p, 0.0).rgb)
  293. #if (FXAA_FAST_PIXEL_OFFSET == 1)
  294. #define LumaOff(t, p, o, r) CalcLuma(texture2DLodOffset(t, p, 0.0, o).rgb)
  295. #else
  296. #define LumaOff(t, p, o, r) CalcLuma(texture2DLod(t, p + (o * r), 0.0).rgb)
  297. #endif
  298. /*============================================================================
  299. FXAA3 QUALITY - PC
  300. ============================================================================*/
  301. vec4 FxaaPixelShader(
  302. //
  303. // Use noperspective interpolation here (turn off perspective interpolation).
  304. // {xy} = center of pixel
  305. vec2 pos,
  306. //
  307. // Input color texture.
  308. // {rgb_} = color in linear or perceptual color space
  309. // if (FXAA_GREEN_AS_LUMA == 0)
  310. // {__a} = luma in perceptual color space (not linear)
  311. sampler2D tex,
  312. //
  313. // Only used on FXAA Quality.
  314. // This must be from a constant/uniform.
  315. // {x_} = 1.0/screenWidthInPixels
  316. // {_y} = 1.0/screenHeightInPixels
  317. vec2 fxaaQualityRcpFrame,
  318. //
  319. // Only used on FXAA Quality.
  320. // This used to be the FXAA_QUALITY_SUBPIX define.
  321. // It is here now to allow easier tuning.
  322. // Choose the amount of sub-pixel aliasing removal.
  323. // This can effect sharpness.
  324. // 1.00 - upper limit (softer)
  325. // 0.75 - default amount of filtering
  326. // 0.50 - lower limit (sharper, less sub-pixel aliasing removal)
  327. // 0.25 - almost off
  328. // 0.00 - completely off
  329. float fxaaQualitySubpix,
  330. //
  331. // Only used on FXAA Quality.
  332. // This used to be the FXAA_QUALITY_EDGE_THRESHOLD define.
  333. // It is here now to allow easier tuning.
  334. // The minimum amount of local contrast required to apply algorithm.
  335. // 0.333 - too little (faster)
  336. // 0.250 - low quality
  337. // 0.166 - default
  338. // 0.125 - high quality
  339. // 0.063 - overkill (slower)
  340. float fxaaQualityEdgeThreshold,
  341. //
  342. // Only used on FXAA Quality.
  343. // This used to be the FXAA_QUALITY_EDGE_THRESHOLD_MIN define.
  344. // It is here now to allow easier tuning.
  345. // Trims the algorithm from processing darks.
  346. // 0.0833 - upper limit (default, the start of visible unfiltered edges)
  347. // 0.0625 - high quality (faster)
  348. // 0.0312 - visible limit (slower)
  349. // Special notes when using FXAA_GREEN_AS_LUMA,
  350. // Likely want to set this to zero.
  351. // As colors that are mostly not-green
  352. // will appear very dark in the green channel!
  353. // Tune by looking at mostly non-green content,
  354. // then start at zero and increase until aliasing is a problem.
  355. float fxaaQualityEdgeThresholdMin
  356. ) {
  357. /*--------------------------------------------------------------------------*/
  358. vec2 posM;
  359. posM.x = pos.x;
  360. posM.y = pos.y;
  361. vec4 rgbyM = FxaaTexTop(tex, posM);
  362. rgbyM.y = CalcLuma(rgbyM.rgb);
  363. #define lumaM rgbyM.y
  364. float lumaS = LumaOff(tex, posM, Fxaa_vec2( 0, 1), fxaaQualityRcpFrame.xy);
  365. float lumaE = LumaOff(tex, posM, Fxaa_vec2( 1, 0), fxaaQualityRcpFrame.xy);
  366. float lumaN = LumaOff(tex, posM, Fxaa_vec2( 0,-1), fxaaQualityRcpFrame.xy);
  367. float lumaW = LumaOff(tex, posM, Fxaa_vec2(-1, 0), fxaaQualityRcpFrame.xy);
  368. /*--------------------------------------------------------------------------*/
  369. float maxSM = max(lumaS, lumaM);
  370. float minSM = min(lumaS, lumaM);
  371. float maxESM = max(lumaE, maxSM);
  372. float minESM = min(lumaE, minSM);
  373. float maxWN = max(lumaN, lumaW);
  374. float minWN = min(lumaN, lumaW);
  375. float rangeMax = max(maxWN, maxESM);
  376. float rangeMin = min(minWN, minESM);
  377. float rangeMaxScaled = rangeMax * fxaaQualityEdgeThreshold;
  378. float range = rangeMax - rangeMin;
  379. float rangeMaxClamped = max(fxaaQualityEdgeThresholdMin, rangeMaxScaled);
  380. bool earlyExit = range < rangeMaxClamped;
  381. /*--------------------------------------------------------------------------*/
  382. if(earlyExit)
  383. return FxaaTexTop(tex, pos);
  384. /*--------------------------------------------------------------------------*/
  385. float lumaNW = LumaOff(tex, posM, Fxaa_vec2(-1,-1), fxaaQualityRcpFrame.xy);
  386. float lumaSE = LumaOff(tex, posM, Fxaa_vec2( 1, 1), fxaaQualityRcpFrame.xy);
  387. float lumaNE = LumaOff(tex, posM, Fxaa_vec2( 1,-1), fxaaQualityRcpFrame.xy);
  388. float lumaSW = LumaOff(tex, posM, Fxaa_vec2(-1, 1), fxaaQualityRcpFrame.xy);
  389. /*--------------------------------------------------------------------------*/
  390. float lumaNS = lumaN + lumaS;
  391. float lumaWE = lumaW + lumaE;
  392. float subpixRcpRange = 1.0/range;
  393. float subpixNSWE = lumaNS + lumaWE;
  394. float edgeHorz1 = (-2.0 * lumaM) + lumaNS;
  395. float edgeVert1 = (-2.0 * lumaM) + lumaWE;
  396. /*--------------------------------------------------------------------------*/
  397. float lumaNESE = lumaNE + lumaSE;
  398. float lumaNWNE = lumaNW + lumaNE;
  399. float edgeHorz2 = (-2.0 * lumaE) + lumaNESE;
  400. float edgeVert2 = (-2.0 * lumaN) + lumaNWNE;
  401. /*--------------------------------------------------------------------------*/
  402. float lumaNWSW = lumaNW + lumaSW;
  403. float lumaSWSE = lumaSW + lumaSE;
  404. float edgeHorz4 = (abs(edgeHorz1) * 2.0) + abs(edgeHorz2);
  405. float edgeVert4 = (abs(edgeVert1) * 2.0) + abs(edgeVert2);
  406. float edgeHorz3 = (-2.0 * lumaW) + lumaNWSW;
  407. float edgeVert3 = (-2.0 * lumaS) + lumaSWSE;
  408. float edgeHorz = abs(edgeHorz3) + edgeHorz4;
  409. float edgeVert = abs(edgeVert3) + edgeVert4;
  410. /*--------------------------------------------------------------------------*/
  411. float subpixNWSWNESE = lumaNWSW + lumaNESE;
  412. float lengthSign = fxaaQualityRcpFrame.x;
  413. bool horzSpan = edgeHorz >= edgeVert;
  414. float subpixA = subpixNSWE * 2.0 + subpixNWSWNESE;
  415. /*--------------------------------------------------------------------------*/
  416. if(!horzSpan) lumaN = lumaW;
  417. if(!horzSpan) lumaS = lumaE;
  418. if(horzSpan) lengthSign = fxaaQualityRcpFrame.y;
  419. float subpixB = (subpixA * (1.0/12.0)) - lumaM;
  420. /*--------------------------------------------------------------------------*/
  421. float gradientN = lumaN - lumaM;
  422. float gradientS = lumaS - lumaM;
  423. float lumaNN = lumaN + lumaM;
  424. float lumaSS = lumaS + lumaM;
  425. bool pairN = abs(gradientN) >= abs(gradientS);
  426. float gradient = max(abs(gradientN), abs(gradientS));
  427. if(pairN) lengthSign = -lengthSign;
  428. float subpixC = clamp((abs(subpixB) * subpixRcpRange), 0.0, 1.0);
  429. /*--------------------------------------------------------------------------*/
  430. vec2 posB;
  431. posB.x = posM.x;
  432. posB.y = posM.y;
  433. vec2 offNP;
  434. offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x;
  435. offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y;
  436. if(!horzSpan) posB.x += lengthSign * 0.5;
  437. if( horzSpan) posB.y += lengthSign * 0.5;
  438. /*--------------------------------------------------------------------------*/
  439. vec2 posN;
  440. posN.x = posB.x - offNP.x * FXAA_QUALITY_P0;
  441. posN.y = posB.y - offNP.y * FXAA_QUALITY_P0;
  442. vec2 posP;
  443. posP.x = posB.x + offNP.x * FXAA_QUALITY_P0;
  444. posP.y = posB.y + offNP.y * FXAA_QUALITY_P0;
  445. float subpixD = ((-2.0)*subpixC) + 3.0;
  446. float lumaEndN = LumaTop(tex, posN);
  447. float subpixE = subpixC * subpixC;
  448. float lumaEndP = LumaTop(tex, posP);
  449. /*--------------------------------------------------------------------------*/
  450. if(!pairN) lumaNN = lumaSS;
  451. float gradientScaled = gradient * 1.0/4.0;
  452. float lumaMM = lumaM - lumaNN * 0.5;
  453. float subpixF = subpixD * subpixE;
  454. bool lumaMLTZero = lumaMM < 0.0;
  455. /*--------------------------------------------------------------------------*/
  456. lumaEndN -= lumaNN * 0.5;
  457. lumaEndP -= lumaNN * 0.5;
  458. bool doneN = abs(lumaEndN) >= gradientScaled;
  459. bool doneP = abs(lumaEndP) >= gradientScaled;
  460. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P1;
  461. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P1;
  462. bool doneNP = (!doneN) || (!doneP);
  463. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P1;
  464. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P1;
  465. /*--------------------------------------------------------------------------*/
  466. if(doneNP) {
  467. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  468. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  469. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  470. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  471. doneN = abs(lumaEndN) >= gradientScaled;
  472. doneP = abs(lumaEndP) >= gradientScaled;
  473. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P2;
  474. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P2;
  475. doneNP = (!doneN) || (!doneP);
  476. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P2;
  477. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P2;
  478. /*--------------------------------------------------------------------------*/
  479. #if (FXAA_QUALITY_PS > 3)
  480. if(doneNP) {
  481. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  482. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  483. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  484. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  485. doneN = abs(lumaEndN) >= gradientScaled;
  486. doneP = abs(lumaEndP) >= gradientScaled;
  487. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P3;
  488. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P3;
  489. doneNP = (!doneN) || (!doneP);
  490. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P3;
  491. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P3;
  492. /*--------------------------------------------------------------------------*/
  493. #if (FXAA_QUALITY_PS > 4)
  494. if(doneNP) {
  495. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  496. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  497. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  498. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  499. doneN = abs(lumaEndN) >= gradientScaled;
  500. doneP = abs(lumaEndP) >= gradientScaled;
  501. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P4;
  502. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P4;
  503. doneNP = (!doneN) || (!doneP);
  504. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P4;
  505. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P4;
  506. /*--------------------------------------------------------------------------*/
  507. #if (FXAA_QUALITY_PS > 5)
  508. if(doneNP) {
  509. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  510. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  511. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  512. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  513. doneN = abs(lumaEndN) >= gradientScaled;
  514. doneP = abs(lumaEndP) >= gradientScaled;
  515. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P5;
  516. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P5;
  517. doneNP = (!doneN) || (!doneP);
  518. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P5;
  519. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P5;
  520. /*--------------------------------------------------------------------------*/
  521. #if (FXAA_QUALITY_PS > 6)
  522. if(doneNP) {
  523. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  524. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  525. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  526. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  527. doneN = abs(lumaEndN) >= gradientScaled;
  528. doneP = abs(lumaEndP) >= gradientScaled;
  529. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P6;
  530. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P6;
  531. doneNP = (!doneN) || (!doneP);
  532. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P6;
  533. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P6;
  534. /*--------------------------------------------------------------------------*/
  535. #if (FXAA_QUALITY_PS > 7)
  536. if(doneNP) {
  537. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  538. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  539. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  540. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  541. doneN = abs(lumaEndN) >= gradientScaled;
  542. doneP = abs(lumaEndP) >= gradientScaled;
  543. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P7;
  544. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P7;
  545. doneNP = (!doneN) || (!doneP);
  546. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P7;
  547. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P7;
  548. /*--------------------------------------------------------------------------*/
  549. #if (FXAA_QUALITY_PS > 8)
  550. if(doneNP) {
  551. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  552. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  553. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  554. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  555. doneN = abs(lumaEndN) >= gradientScaled;
  556. doneP = abs(lumaEndP) >= gradientScaled;
  557. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P8;
  558. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P8;
  559. doneNP = (!doneN) || (!doneP);
  560. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P8;
  561. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P8;
  562. /*--------------------------------------------------------------------------*/
  563. #if (FXAA_QUALITY_PS > 9)
  564. if(doneNP) {
  565. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  566. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  567. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  568. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  569. doneN = abs(lumaEndN) >= gradientScaled;
  570. doneP = abs(lumaEndP) >= gradientScaled;
  571. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P9;
  572. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P9;
  573. doneNP = (!doneN) || (!doneP);
  574. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P9;
  575. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P9;
  576. /*--------------------------------------------------------------------------*/
  577. #if (FXAA_QUALITY_PS > 10)
  578. if(doneNP) {
  579. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  580. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  581. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  582. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  583. doneN = abs(lumaEndN) >= gradientScaled;
  584. doneP = abs(lumaEndP) >= gradientScaled;
  585. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P10;
  586. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P10;
  587. doneNP = (!doneN) || (!doneP);
  588. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P10;
  589. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P10;
  590. /*--------------------------------------------------------------------------*/
  591. #if (FXAA_QUALITY_PS > 11)
  592. if(doneNP) {
  593. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  594. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  595. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  596. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  597. doneN = abs(lumaEndN) >= gradientScaled;
  598. doneP = abs(lumaEndP) >= gradientScaled;
  599. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P11;
  600. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P11;
  601. doneNP = (!doneN) || (!doneP);
  602. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P11;
  603. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P11;
  604. /*--------------------------------------------------------------------------*/
  605. #if (FXAA_QUALITY_PS > 12)
  606. if(doneNP) {
  607. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  608. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  609. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  610. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  611. doneN = abs(lumaEndN) >= gradientScaled;
  612. doneP = abs(lumaEndP) >= gradientScaled;
  613. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P12;
  614. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P12;
  615. doneNP = (!doneN) || (!doneP);
  616. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P12;
  617. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P12;
  618. /*--------------------------------------------------------------------------*/
  619. }
  620. #endif
  621. /*--------------------------------------------------------------------------*/
  622. }
  623. #endif
  624. /*--------------------------------------------------------------------------*/
  625. }
  626. #endif
  627. /*--------------------------------------------------------------------------*/
  628. }
  629. #endif
  630. /*--------------------------------------------------------------------------*/
  631. }
  632. #endif
  633. /*--------------------------------------------------------------------------*/
  634. }
  635. #endif
  636. /*--------------------------------------------------------------------------*/
  637. }
  638. #endif
  639. /*--------------------------------------------------------------------------*/
  640. }
  641. #endif
  642. /*--------------------------------------------------------------------------*/
  643. }
  644. #endif
  645. /*--------------------------------------------------------------------------*/
  646. }
  647. #endif
  648. /*--------------------------------------------------------------------------*/
  649. }
  650. /*--------------------------------------------------------------------------*/
  651. float dstN = posM.x - posN.x;
  652. float dstP = posP.x - posM.x;
  653. if(!horzSpan) dstN = posM.y - posN.y;
  654. if(!horzSpan) dstP = posP.y - posM.y;
  655. /*--------------------------------------------------------------------------*/
  656. bool goodSpanN = (lumaEndN < 0.0) != lumaMLTZero;
  657. float spanLength = (dstP + dstN);
  658. bool goodSpanP = (lumaEndP < 0.0) != lumaMLTZero;
  659. float spanLengthRcp = 1.0/spanLength;
  660. /*--------------------------------------------------------------------------*/
  661. bool directionN = dstN < dstP;
  662. float dst = min(dstN, dstP);
  663. bool goodSpan = directionN ? goodSpanN : goodSpanP;
  664. float subpixG = subpixF * subpixF;
  665. float pixelOffset = (dst * (-spanLengthRcp)) + 0.5;
  666. float subpixH = subpixG * fxaaQualitySubpix;
  667. /*--------------------------------------------------------------------------*/
  668. float pixelOffsetGood = goodSpan ? pixelOffset : 0.0;
  669. float pixelOffsetSubpix = max(pixelOffsetGood, subpixH);
  670. if(!horzSpan) posM.x += pixelOffsetSubpix * lengthSign;
  671. if( horzSpan) posM.y += pixelOffsetSubpix * lengthSign;
  672. return FxaaTexTop(tex, posM);
  673. }
  674. /*==========================================================================*/
  675. /*============================================================================
  676. Urho3D Vertex- and Pixelshader
  677. ============================================================================*/
  678. void VS()
  679. {
  680. mat4 modelMatrix = iModelMatrix;
  681. vec3 worldPos = GetWorldPos(modelMatrix);
  682. gl_Position = GetClipPos(worldPos);
  683. vScreenPos = GetScreenPosPreDiv(gl_Position);
  684. }
  685. void PS()
  686. {
  687. vec2 rcpFrame = vec2(cGBufferInvSize.x, cGBufferInvSize.y);
  688. gl_FragColor = FxaaPixelShader(
  689. vScreenPos, // vec2 pos,
  690. sDiffMap, // sampler2D tex,
  691. rcpFrame, // vec2 fxaaQualityRcpFrame,
  692. 0.75, // float fxaaQualitySubpix,
  693. 0.166, // float fxaaQualityEdgeThreshold,
  694. 0.0833 // float fxaaQualityEdgeThresholdMin
  695. );
  696. }