FXAA3.glsl 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. #ifdef GL3
  292. #define FxaaTexTop(t, p) vec4(textureLod(t, p, 0.0).rgb, 1.0)
  293. #define LumaTop(t, p) CalcLuma(textureLod(t, p, 0.0).rgb)
  294. #if (FXAA_FAST_PIXEL_OFFSET == 1)
  295. #define LumaOff(t, p, o, r) CalcLuma(textureLodOffset(t, p, 0.0, o).rgb)
  296. #else
  297. #define LumaOff(t, p, o, r) CalcLuma(textureLod(t, p + (o * r), 0.0).rgb)
  298. #endif
  299. #else
  300. #define FxaaTexTop(t, p) vec4(texture2DLod(t, p, 0.0).rgb, 1.0)
  301. #define LumaTop(t, p) CalcLuma(texture2DLod(t, p, 0.0).rgb)
  302. #if (FXAA_FAST_PIXEL_OFFSET == 1)
  303. #define LumaOff(t, p, o, r) CalcLuma(texture2DLodOffset(t, p, 0.0, o).rgb)
  304. #else
  305. #define LumaOff(t, p, o, r) CalcLuma(texture2DLod(t, p + (o * r), 0.0).rgb)
  306. #endif
  307. #endif
  308. /*============================================================================
  309. FXAA3 QUALITY - PC
  310. ============================================================================*/
  311. vec4 FxaaPixelShader(
  312. //
  313. // Use noperspective interpolation here (turn off perspective interpolation).
  314. // {xy} = center of pixel
  315. vec2 pos,
  316. //
  317. // Input color texture.
  318. // {rgb_} = color in linear or perceptual color space
  319. // if (FXAA_GREEN_AS_LUMA == 0)
  320. // {__a} = luma in perceptual color space (not linear)
  321. sampler2D tex,
  322. //
  323. // Only used on FXAA Quality.
  324. // This must be from a constant/uniform.
  325. // {x_} = 1.0/screenWidthInPixels
  326. // {_y} = 1.0/screenHeightInPixels
  327. vec2 fxaaQualityRcpFrame,
  328. //
  329. // Only used on FXAA Quality.
  330. // This used to be the FXAA_QUALITY_SUBPIX define.
  331. // It is here now to allow easier tuning.
  332. // Choose the amount of sub-pixel aliasing removal.
  333. // This can effect sharpness.
  334. // 1.00 - upper limit (softer)
  335. // 0.75 - default amount of filtering
  336. // 0.50 - lower limit (sharper, less sub-pixel aliasing removal)
  337. // 0.25 - almost off
  338. // 0.00 - completely off
  339. float fxaaQualitySubpix,
  340. //
  341. // Only used on FXAA Quality.
  342. // This used to be the FXAA_QUALITY_EDGE_THRESHOLD define.
  343. // It is here now to allow easier tuning.
  344. // The minimum amount of local contrast required to apply algorithm.
  345. // 0.333 - too little (faster)
  346. // 0.250 - low quality
  347. // 0.166 - default
  348. // 0.125 - high quality
  349. // 0.063 - overkill (slower)
  350. float fxaaQualityEdgeThreshold,
  351. //
  352. // Only used on FXAA Quality.
  353. // This used to be the FXAA_QUALITY_EDGE_THRESHOLD_MIN define.
  354. // It is here now to allow easier tuning.
  355. // Trims the algorithm from processing darks.
  356. // 0.0833 - upper limit (default, the start of visible unfiltered edges)
  357. // 0.0625 - high quality (faster)
  358. // 0.0312 - visible limit (slower)
  359. // Special notes when using FXAA_GREEN_AS_LUMA,
  360. // Likely want to set this to zero.
  361. // As colors that are mostly not-green
  362. // will appear very dark in the green channel!
  363. // Tune by looking at mostly non-green content,
  364. // then start at zero and increase until aliasing is a problem.
  365. float fxaaQualityEdgeThresholdMin
  366. ) {
  367. /*--------------------------------------------------------------------------*/
  368. vec2 posM;
  369. posM.x = pos.x;
  370. posM.y = pos.y;
  371. vec4 rgbyM = FxaaTexTop(tex, posM);
  372. rgbyM.y = CalcLuma(rgbyM.rgb);
  373. #define lumaM rgbyM.y
  374. float lumaS = LumaOff(tex, posM, Fxaa_vec2( 0, 1), fxaaQualityRcpFrame.xy);
  375. float lumaE = LumaOff(tex, posM, Fxaa_vec2( 1, 0), fxaaQualityRcpFrame.xy);
  376. float lumaN = LumaOff(tex, posM, Fxaa_vec2( 0,-1), fxaaQualityRcpFrame.xy);
  377. float lumaW = LumaOff(tex, posM, Fxaa_vec2(-1, 0), fxaaQualityRcpFrame.xy);
  378. /*--------------------------------------------------------------------------*/
  379. float maxSM = max(lumaS, lumaM);
  380. float minSM = min(lumaS, lumaM);
  381. float maxESM = max(lumaE, maxSM);
  382. float minESM = min(lumaE, minSM);
  383. float maxWN = max(lumaN, lumaW);
  384. float minWN = min(lumaN, lumaW);
  385. float rangeMax = max(maxWN, maxESM);
  386. float rangeMin = min(minWN, minESM);
  387. float rangeMaxScaled = rangeMax * fxaaQualityEdgeThreshold;
  388. float range = rangeMax - rangeMin;
  389. float rangeMaxClamped = max(fxaaQualityEdgeThresholdMin, rangeMaxScaled);
  390. bool earlyExit = range < rangeMaxClamped;
  391. /*--------------------------------------------------------------------------*/
  392. if(earlyExit)
  393. return FxaaTexTop(tex, pos);
  394. /*--------------------------------------------------------------------------*/
  395. float lumaNW = LumaOff(tex, posM, Fxaa_vec2(-1,-1), fxaaQualityRcpFrame.xy);
  396. float lumaSE = LumaOff(tex, posM, Fxaa_vec2( 1, 1), fxaaQualityRcpFrame.xy);
  397. float lumaNE = LumaOff(tex, posM, Fxaa_vec2( 1,-1), fxaaQualityRcpFrame.xy);
  398. float lumaSW = LumaOff(tex, posM, Fxaa_vec2(-1, 1), fxaaQualityRcpFrame.xy);
  399. /*--------------------------------------------------------------------------*/
  400. float lumaNS = lumaN + lumaS;
  401. float lumaWE = lumaW + lumaE;
  402. float subpixRcpRange = 1.0/range;
  403. float subpixNSWE = lumaNS + lumaWE;
  404. float edgeHorz1 = (-2.0 * lumaM) + lumaNS;
  405. float edgeVert1 = (-2.0 * lumaM) + lumaWE;
  406. /*--------------------------------------------------------------------------*/
  407. float lumaNESE = lumaNE + lumaSE;
  408. float lumaNWNE = lumaNW + lumaNE;
  409. float edgeHorz2 = (-2.0 * lumaE) + lumaNESE;
  410. float edgeVert2 = (-2.0 * lumaN) + lumaNWNE;
  411. /*--------------------------------------------------------------------------*/
  412. float lumaNWSW = lumaNW + lumaSW;
  413. float lumaSWSE = lumaSW + lumaSE;
  414. float edgeHorz4 = (abs(edgeHorz1) * 2.0) + abs(edgeHorz2);
  415. float edgeVert4 = (abs(edgeVert1) * 2.0) + abs(edgeVert2);
  416. float edgeHorz3 = (-2.0 * lumaW) + lumaNWSW;
  417. float edgeVert3 = (-2.0 * lumaS) + lumaSWSE;
  418. float edgeHorz = abs(edgeHorz3) + edgeHorz4;
  419. float edgeVert = abs(edgeVert3) + edgeVert4;
  420. /*--------------------------------------------------------------------------*/
  421. float subpixNWSWNESE = lumaNWSW + lumaNESE;
  422. float lengthSign = fxaaQualityRcpFrame.x;
  423. bool horzSpan = edgeHorz >= edgeVert;
  424. float subpixA = subpixNSWE * 2.0 + subpixNWSWNESE;
  425. /*--------------------------------------------------------------------------*/
  426. if(!horzSpan) lumaN = lumaW;
  427. if(!horzSpan) lumaS = lumaE;
  428. if(horzSpan) lengthSign = fxaaQualityRcpFrame.y;
  429. float subpixB = (subpixA * (1.0/12.0)) - lumaM;
  430. /*--------------------------------------------------------------------------*/
  431. float gradientN = lumaN - lumaM;
  432. float gradientS = lumaS - lumaM;
  433. float lumaNN = lumaN + lumaM;
  434. float lumaSS = lumaS + lumaM;
  435. bool pairN = abs(gradientN) >= abs(gradientS);
  436. float gradient = max(abs(gradientN), abs(gradientS));
  437. if(pairN) lengthSign = -lengthSign;
  438. float subpixC = clamp((abs(subpixB) * subpixRcpRange), 0.0, 1.0);
  439. /*--------------------------------------------------------------------------*/
  440. vec2 posB;
  441. posB.x = posM.x;
  442. posB.y = posM.y;
  443. vec2 offNP;
  444. offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x;
  445. offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y;
  446. if(!horzSpan) posB.x += lengthSign * 0.5;
  447. if( horzSpan) posB.y += lengthSign * 0.5;
  448. /*--------------------------------------------------------------------------*/
  449. vec2 posN;
  450. posN.x = posB.x - offNP.x * FXAA_QUALITY_P0;
  451. posN.y = posB.y - offNP.y * FXAA_QUALITY_P0;
  452. vec2 posP;
  453. posP.x = posB.x + offNP.x * FXAA_QUALITY_P0;
  454. posP.y = posB.y + offNP.y * FXAA_QUALITY_P0;
  455. float subpixD = ((-2.0)*subpixC) + 3.0;
  456. float lumaEndN = LumaTop(tex, posN);
  457. float subpixE = subpixC * subpixC;
  458. float lumaEndP = LumaTop(tex, posP);
  459. /*--------------------------------------------------------------------------*/
  460. if(!pairN) lumaNN = lumaSS;
  461. float gradientScaled = gradient * 1.0/4.0;
  462. float lumaMM = lumaM - lumaNN * 0.5;
  463. float subpixF = subpixD * subpixE;
  464. bool lumaMLTZero = lumaMM < 0.0;
  465. /*--------------------------------------------------------------------------*/
  466. lumaEndN -= lumaNN * 0.5;
  467. lumaEndP -= lumaNN * 0.5;
  468. bool doneN = abs(lumaEndN) >= gradientScaled;
  469. bool doneP = abs(lumaEndP) >= gradientScaled;
  470. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P1;
  471. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P1;
  472. bool doneNP = (!doneN) || (!doneP);
  473. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P1;
  474. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P1;
  475. /*--------------------------------------------------------------------------*/
  476. if(doneNP) {
  477. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  478. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  479. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  480. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  481. doneN = abs(lumaEndN) >= gradientScaled;
  482. doneP = abs(lumaEndP) >= gradientScaled;
  483. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P2;
  484. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P2;
  485. doneNP = (!doneN) || (!doneP);
  486. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P2;
  487. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P2;
  488. /*--------------------------------------------------------------------------*/
  489. #if (FXAA_QUALITY_PS > 3)
  490. if(doneNP) {
  491. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  492. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  493. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  494. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  495. doneN = abs(lumaEndN) >= gradientScaled;
  496. doneP = abs(lumaEndP) >= gradientScaled;
  497. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P3;
  498. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P3;
  499. doneNP = (!doneN) || (!doneP);
  500. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P3;
  501. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P3;
  502. /*--------------------------------------------------------------------------*/
  503. #if (FXAA_QUALITY_PS > 4)
  504. if(doneNP) {
  505. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  506. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  507. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  508. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  509. doneN = abs(lumaEndN) >= gradientScaled;
  510. doneP = abs(lumaEndP) >= gradientScaled;
  511. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P4;
  512. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P4;
  513. doneNP = (!doneN) || (!doneP);
  514. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P4;
  515. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P4;
  516. /*--------------------------------------------------------------------------*/
  517. #if (FXAA_QUALITY_PS > 5)
  518. if(doneNP) {
  519. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  520. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  521. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  522. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  523. doneN = abs(lumaEndN) >= gradientScaled;
  524. doneP = abs(lumaEndP) >= gradientScaled;
  525. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P5;
  526. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P5;
  527. doneNP = (!doneN) || (!doneP);
  528. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P5;
  529. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P5;
  530. /*--------------------------------------------------------------------------*/
  531. #if (FXAA_QUALITY_PS > 6)
  532. if(doneNP) {
  533. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  534. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  535. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  536. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  537. doneN = abs(lumaEndN) >= gradientScaled;
  538. doneP = abs(lumaEndP) >= gradientScaled;
  539. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P6;
  540. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P6;
  541. doneNP = (!doneN) || (!doneP);
  542. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P6;
  543. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P6;
  544. /*--------------------------------------------------------------------------*/
  545. #if (FXAA_QUALITY_PS > 7)
  546. if(doneNP) {
  547. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  548. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  549. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  550. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  551. doneN = abs(lumaEndN) >= gradientScaled;
  552. doneP = abs(lumaEndP) >= gradientScaled;
  553. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P7;
  554. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P7;
  555. doneNP = (!doneN) || (!doneP);
  556. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P7;
  557. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P7;
  558. /*--------------------------------------------------------------------------*/
  559. #if (FXAA_QUALITY_PS > 8)
  560. if(doneNP) {
  561. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  562. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  563. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  564. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  565. doneN = abs(lumaEndN) >= gradientScaled;
  566. doneP = abs(lumaEndP) >= gradientScaled;
  567. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P8;
  568. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P8;
  569. doneNP = (!doneN) || (!doneP);
  570. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P8;
  571. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P8;
  572. /*--------------------------------------------------------------------------*/
  573. #if (FXAA_QUALITY_PS > 9)
  574. if(doneNP) {
  575. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  576. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  577. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  578. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  579. doneN = abs(lumaEndN) >= gradientScaled;
  580. doneP = abs(lumaEndP) >= gradientScaled;
  581. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P9;
  582. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P9;
  583. doneNP = (!doneN) || (!doneP);
  584. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P9;
  585. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P9;
  586. /*--------------------------------------------------------------------------*/
  587. #if (FXAA_QUALITY_PS > 10)
  588. if(doneNP) {
  589. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  590. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  591. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  592. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  593. doneN = abs(lumaEndN) >= gradientScaled;
  594. doneP = abs(lumaEndP) >= gradientScaled;
  595. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P10;
  596. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P10;
  597. doneNP = (!doneN) || (!doneP);
  598. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P10;
  599. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P10;
  600. /*--------------------------------------------------------------------------*/
  601. #if (FXAA_QUALITY_PS > 11)
  602. if(doneNP) {
  603. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  604. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  605. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  606. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  607. doneN = abs(lumaEndN) >= gradientScaled;
  608. doneP = abs(lumaEndP) >= gradientScaled;
  609. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P11;
  610. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P11;
  611. doneNP = (!doneN) || (!doneP);
  612. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P11;
  613. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P11;
  614. /*--------------------------------------------------------------------------*/
  615. #if (FXAA_QUALITY_PS > 12)
  616. if(doneNP) {
  617. if(!doneN) lumaEndN = LumaTop(tex, posN.xy);
  618. if(!doneP) lumaEndP = LumaTop(tex, posP.xy);
  619. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  620. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  621. doneN = abs(lumaEndN) >= gradientScaled;
  622. doneP = abs(lumaEndP) >= gradientScaled;
  623. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY_P12;
  624. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY_P12;
  625. doneNP = (!doneN) || (!doneP);
  626. if(!doneP) posP.x += offNP.x * FXAA_QUALITY_P12;
  627. if(!doneP) posP.y += offNP.y * FXAA_QUALITY_P12;
  628. /*--------------------------------------------------------------------------*/
  629. }
  630. #endif
  631. /*--------------------------------------------------------------------------*/
  632. }
  633. #endif
  634. /*--------------------------------------------------------------------------*/
  635. }
  636. #endif
  637. /*--------------------------------------------------------------------------*/
  638. }
  639. #endif
  640. /*--------------------------------------------------------------------------*/
  641. }
  642. #endif
  643. /*--------------------------------------------------------------------------*/
  644. }
  645. #endif
  646. /*--------------------------------------------------------------------------*/
  647. }
  648. #endif
  649. /*--------------------------------------------------------------------------*/
  650. }
  651. #endif
  652. /*--------------------------------------------------------------------------*/
  653. }
  654. #endif
  655. /*--------------------------------------------------------------------------*/
  656. }
  657. #endif
  658. /*--------------------------------------------------------------------------*/
  659. }
  660. /*--------------------------------------------------------------------------*/
  661. float dstN = posM.x - posN.x;
  662. float dstP = posP.x - posM.x;
  663. if(!horzSpan) dstN = posM.y - posN.y;
  664. if(!horzSpan) dstP = posP.y - posM.y;
  665. /*--------------------------------------------------------------------------*/
  666. bool goodSpanN = (lumaEndN < 0.0) != lumaMLTZero;
  667. float spanLength = (dstP + dstN);
  668. bool goodSpanP = (lumaEndP < 0.0) != lumaMLTZero;
  669. float spanLengthRcp = 1.0/spanLength;
  670. /*--------------------------------------------------------------------------*/
  671. bool directionN = dstN < dstP;
  672. float dst = min(dstN, dstP);
  673. bool goodSpan = directionN ? goodSpanN : goodSpanP;
  674. float subpixG = subpixF * subpixF;
  675. float pixelOffset = (dst * (-spanLengthRcp)) + 0.5;
  676. float subpixH = subpixG * fxaaQualitySubpix;
  677. /*--------------------------------------------------------------------------*/
  678. float pixelOffsetGood = goodSpan ? pixelOffset : 0.0;
  679. float pixelOffsetSubpix = max(pixelOffsetGood, subpixH);
  680. if(!horzSpan) posM.x += pixelOffsetSubpix * lengthSign;
  681. if( horzSpan) posM.y += pixelOffsetSubpix * lengthSign;
  682. return FxaaTexTop(tex, posM);
  683. }
  684. /*==========================================================================*/
  685. /*============================================================================
  686. Urho3D Vertex- and Pixelshader
  687. ============================================================================*/
  688. void VS()
  689. {
  690. mat4 modelMatrix = iModelMatrix;
  691. vec3 worldPos = GetWorldPos(modelMatrix);
  692. gl_Position = GetClipPos(worldPos);
  693. vScreenPos = GetScreenPosPreDiv(gl_Position);
  694. }
  695. void PS()
  696. {
  697. vec2 rcpFrame = vec2(cGBufferInvSize.x, cGBufferInvSize.y);
  698. gl_FragColor = FxaaPixelShader(
  699. vScreenPos, // vec2 pos,
  700. sDiffMap, // sampler2D tex,
  701. rcpFrame, // vec2 fxaaQualityRcpFrame,
  702. 0.75, // float fxaaQualitySubpix,
  703. 0.166, // float fxaaQualityEdgeThreshold,
  704. 0.0833 // float fxaaQualityEdgeThresholdMin
  705. );
  706. }