Fxaa3_11.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*============================================================================
  2. NVIDIA FXAA 3.11 by TIMOTHY LOTTES
  3. FXAA POST PROCESS INJECTION MODIFIED VERSION
  4. ------------------------------------------------------------------------------
  5. COPYRIGHT (C) 2010, 2011 NVIDIA CORPORATION. ALL RIGHTS RESERVED.
  6. ------------------------------------------------------------------------------
  7. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
  8. *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
  9. OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  10. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA
  11. OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR
  12. CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR
  13. LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION,
  14. OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE
  15. THIS SOFTWARE, EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. DAMAGES.
  17. ------------------------------------------------------------------------------
  18. INTEGRATION CHECKLIST
  19. ------------------------------------------------------------------------------
  20. (1.)
  21. In the shader source, setup defines for the desired configuration.
  22. When providing multiple shaders (for different presets),
  23. simply setup the defines differently in multiple files.
  24. Example,
  25. #define FXAA_HLSL_3 1
  26. Or,
  27. #define FXAA_HLSL_4 1
  28. Or,
  29. #define FXAA_HLSL_5 1
  30. Or,
  31. FXAA_GLSL_120
  32. Or,
  33. FXAA_GLSL_130
  34. (2.)
  35. Then include this file,
  36. #include "Fxaa3_11.h"
  37. (3.)
  38. Then call the FXAA pixel shader from within your desired shader.
  39. Look at the FXAA Quality FxaaPixelShader() for docs on inputs.
  40. As for FXAA 3.11 all inputs for all shaders are the same
  41. to enable easy porting between platforms.
  42. return FxaaPixelShader(...);
  43. (4.)
  44. Insure pass prior to FXAA outputs RGBL (see next section).
  45. (5.)
  46. Setup engine to provide the following constants
  47. which are used in the FxaaPixelShader() inputs,
  48. FxaaFloat2 fxaaQualityRcpFrame,
  49. FxaaFloat4 fxaaConsoleRcpFrameOpt2,
  50. FxaaFloat fxaaQualitySubpix,
  51. FxaaFloat fxaaQualityEdgeThreshold,
  52. FxaaFloat fxaaQualityEdgeThresholdMin
  53. Look at the FXAA Quality FxaaPixelShader() for docs on inputs.
  54. (6.)
  55. Have FXAA vertex shader run as a full screen triangle,
  56. and output "pos"
  57. // {xy} = center of pixel
  58. FxaaFloat2 pos,
  59. (7.)
  60. Insure the texture sampler(s) used by FXAA are set to bilinear filtering.
  61. ============================================================================*/
  62. /*============================================================================
  63. INTEGRATION KNOBS
  64. ============================================================================*/
  65. #ifndef FXAA_GLSL_120
  66. #define FXAA_GLSL_120 0
  67. #endif
  68. /*--------------------------------------------------------------------------*/
  69. #ifndef FXAA_GLSL_130
  70. #define FXAA_GLSL_130 0
  71. #endif
  72. /*--------------------------------------------------------------------------*/
  73. #ifndef FXAA_HLSL_3
  74. #define FXAA_HLSL_3 0
  75. #endif
  76. /*--------------------------------------------------------------------------*/
  77. #ifndef FXAA_HLSL_4
  78. #define FXAA_HLSL_4 0
  79. #endif
  80. /*--------------------------------------------------------------------------*/
  81. #ifndef FXAA_HLSL_5
  82. #define FXAA_HLSL_5 0
  83. #endif
  84. /*==========================================================================*/
  85. #ifndef FXAA_FAST_PIXEL_OFFSET
  86. //
  87. // Used for GLSL 120 only.
  88. //
  89. // 1 = GL API supports fast pixel offsets
  90. // 0 = do not use fast pixel offsets
  91. //
  92. #ifdef GL_EXT_gpu_shader4
  93. #define FXAA_FAST_PIXEL_OFFSET 1
  94. #endif
  95. #ifdef GL_NV_gpu_shader5
  96. #define FXAA_FAST_PIXEL_OFFSET 1
  97. #endif
  98. #ifdef GL_ARB_gpu_shader5
  99. #define FXAA_FAST_PIXEL_OFFSET 1
  100. #endif
  101. #ifndef FXAA_FAST_PIXEL_OFFSET
  102. #define FXAA_FAST_PIXEL_OFFSET 0
  103. #endif
  104. #endif
  105. /*--------------------------------------------------------------------------*/
  106. #ifndef FXAA_GATHER4_ALPHA
  107. //
  108. // 1 = API supports gather4 on alpha channel.
  109. // 0 = API does not support gather4 on alpha channel.
  110. //
  111. #if (FXAA_HLSL_5 == 1)
  112. #define FXAA_GATHER4_ALPHA 1
  113. #endif
  114. #ifdef GL_ARB_gpu_shader5
  115. #define FXAA_GATHER4_ALPHA 1
  116. #endif
  117. #ifdef GL_NV_gpu_shader5
  118. #define FXAA_GATHER4_ALPHA 1
  119. #endif
  120. #ifndef FXAA_GATHER4_ALPHA
  121. #define FXAA_GATHER4_ALPHA 0
  122. #endif
  123. #endif
  124. /*============================================================================
  125. FXAA QUALITY - TUNING KNOBS
  126. ------------------------------------------------------------------------------
  127. NOTE the other tuning knobs are now in the shader function inputs!
  128. ============================================================================*/
  129. #ifndef FXAA_QUALITY__PRESET
  130. // Choose the quality preset.
  131. // This needs to be compiled into the shader as it effects code.
  132. // Best option to include multiple presets is to
  133. // in each shader define the preset, then include this file.
  134. //
  135. // OPTIONS
  136. // -----------------------------------------------------------------------
  137. // 2 to 3 - default medium dither (10=fastest, 15=highest quality)
  138. // 4 to 5 - less dither, more expensive (20=fastest, 29=highest quality)
  139. // 6 - no dither, very expensive
  140. // 7 = slightly faster then FXAA 3.9 and higher edge quality (default)
  141. // 8 = about same speed as FXAA 3.9 and better than 12
  142. // 9 = closest to FXAA 3.9 visually and performance wise
  143. #define FXAA_QUALITY__PRESET 9
  144. #endif
  145. /*============================================================================
  146. FXAA QUALITY - PRESETS
  147. ============================================================================*/
  148. /*============================================================================
  149. FXAA QUALITY - MEDIUM DITHER PRESETS
  150. ============================================================================*/
  151. #if (FXAA_QUALITY__PRESET == 1)
  152. #define FXAA_QUALITY__PS 3
  153. #define FXAA_QUALITY__P0 1.5
  154. #define FXAA_QUALITY__P1 3.0
  155. #define FXAA_QUALITY__P2 12.0
  156. #endif
  157. /*--------------------------------------------------------------------------*/
  158. #if (FXAA_QUALITY__PRESET == 2)
  159. #define FXAA_QUALITY__PS 5
  160. #define FXAA_QUALITY__P0 1.0
  161. #define FXAA_QUALITY__P1 1.5
  162. #define FXAA_QUALITY__P2 2.0
  163. #define FXAA_QUALITY__P3 4.0
  164. #define FXAA_QUALITY__P4 12.0
  165. #endif
  166. /*--------------------------------------------------------------------------*/
  167. #if (FXAA_QUALITY__PRESET == 3)
  168. #define FXAA_QUALITY__PS 6
  169. #define FXAA_QUALITY__P0 1.0
  170. #define FXAA_QUALITY__P1 1.5
  171. #define FXAA_QUALITY__P2 2.0
  172. #define FXAA_QUALITY__P3 2.0
  173. #define FXAA_QUALITY__P4 4.0
  174. #define FXAA_QUALITY__P5 12.0
  175. #endif
  176. /*--------------------------------------------------------------------------*/
  177. #if (FXAA_QUALITY__PRESET == 4)
  178. #define FXAA_QUALITY__PS 8
  179. #define FXAA_QUALITY__P0 1.0
  180. #define FXAA_QUALITY__P1 1.5
  181. #define FXAA_QUALITY__P2 2.0
  182. #define FXAA_QUALITY__P3 2.0
  183. #define FXAA_QUALITY__P4 2.0
  184. #define FXAA_QUALITY__P5 2.0
  185. #define FXAA_QUALITY__P6 4.0
  186. #define FXAA_QUALITY__P7 12.0
  187. #endif
  188. /*============================================================================
  189. FXAA QUALITY - LOW DITHER PRESETS
  190. ============================================================================*/
  191. #if (FXAA_QUALITY__PRESET == 5)
  192. #define FXAA_QUALITY__PS 3
  193. #define FXAA_QUALITY__P0 1.5
  194. #define FXAA_QUALITY__P1 2.0
  195. #define FXAA_QUALITY__P2 8.0
  196. #endif
  197. /*--------------------------------------------------------------------------*/
  198. #if (FXAA_QUALITY__PRESET == 6)
  199. #define FXAA_QUALITY__PS 6
  200. #define FXAA_QUALITY__P0 1.0
  201. #define FXAA_QUALITY__P1 1.5
  202. #define FXAA_QUALITY__P2 2.0
  203. #define FXAA_QUALITY__P3 2.0
  204. #define FXAA_QUALITY__P4 2.0
  205. #define FXAA_QUALITY__P5 8.0
  206. #endif
  207. /*--------------------------------------------------------------------------*/
  208. #if (FXAA_QUALITY__PRESET == 7)
  209. #define FXAA_QUALITY__PS 11
  210. #define FXAA_QUALITY__P0 1.0
  211. #define FXAA_QUALITY__P1 1.5
  212. #define FXAA_QUALITY__P2 2.0
  213. #define FXAA_QUALITY__P3 2.0
  214. #define FXAA_QUALITY__P4 2.0
  215. #define FXAA_QUALITY__P5 2.0
  216. #define FXAA_QUALITY__P6 2.0
  217. #define FXAA_QUALITY__P7 2.0
  218. #define FXAA_QUALITY__P8 2.0
  219. #define FXAA_QUALITY__P9 4.0
  220. #define FXAA_QUALITY__P10 8.0
  221. #endif
  222. /*--------------------------------------------------------------------------*/
  223. #if (FXAA_QUALITY__PRESET == 8)
  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. /*============================================================================
  239. FXAA QUALITY - EXTREME QUALITY
  240. ============================================================================*/
  241. #if (FXAA_QUALITY__PRESET == 9)
  242. #define FXAA_QUALITY__PS 12
  243. #define FXAA_QUALITY__P0 1.0
  244. #define FXAA_QUALITY__P1 1.0
  245. #define FXAA_QUALITY__P2 1.0
  246. #define FXAA_QUALITY__P3 1.0
  247. #define FXAA_QUALITY__P4 1.0
  248. #define FXAA_QUALITY__P5 1.5
  249. #define FXAA_QUALITY__P6 2.0
  250. #define FXAA_QUALITY__P7 2.0
  251. #define FXAA_QUALITY__P8 2.0
  252. #define FXAA_QUALITY__P9 2.0
  253. #define FXAA_QUALITY__P10 4.0
  254. #define FXAA_QUALITY__P11 8.0
  255. #endif
  256. /*============================================================================
  257. API PORTING
  258. ============================================================================*/
  259. #if (FXAA_GLSL_120 == 1) || (FXAA_GLSL_130 == 1)
  260. #define FxaaBool bool
  261. #define FxaaDiscard discard
  262. #define FxaaFloat float
  263. #define FxaaFloat2 vec2
  264. #define FxaaFloat3 vec3
  265. #define FxaaFloat4 vec4
  266. #define FxaaHalf float
  267. #define FxaaHalf2 vec2
  268. #define FxaaHalf3 vec3
  269. #define FxaaHalf4 vec4
  270. #define FxaaInt2 ivec2
  271. #define FxaaSat(x) clamp(x, 0.0, 1.0)
  272. #define FxaaTex sampler2D
  273. #else
  274. #define FxaaBool bool
  275. #define FxaaDiscard clip(-1)
  276. #define FxaaFloat float
  277. #define FxaaFloat2 float2
  278. #define FxaaFloat3 float3
  279. #define FxaaFloat4 float4
  280. #define FxaaHalf half
  281. #define FxaaHalf2 half2
  282. #define FxaaHalf3 half3
  283. #define FxaaHalf4 half4
  284. #define FxaaSat(x) saturate(x)
  285. #endif
  286. /*--------------------------------------------------------------------------*/
  287. #if (FXAA_GLSL_120 == 1)
  288. // Requires,
  289. // #version 120
  290. // And at least,
  291. // #extension GL_EXT_gpu_shader4 : enable
  292. // (or set FXAA_FAST_PIXEL_OFFSET 1 to work like DX9)
  293. #define FxaaTexTop(t, p) texture2DLod(t, p, 0.0)
  294. #if (FXAA_FAST_PIXEL_OFFSET == 1)
  295. #define FxaaTexOff(t, p, o, r) texture2DLodOffset(t, p, 0.0, o)
  296. #else
  297. #define FxaaTexOff(t, p, o, r) texture2DLod(t, p + (o * r), 0.0)
  298. #endif
  299. #if (FXAA_GATHER4_ALPHA == 1)
  300. // use #extension GL_ARB_gpu_shader5 : enable
  301. #define FxaaTexAlpha4(t, p) textureGather(t, p, 3)
  302. #define FxaaTexOffAlpha4(t, p, o) textureGatherOffset(t, p, o, 3)
  303. #define FxaaTexGreen4(t, p) textureGather(t, p, 1)
  304. #define FxaaTexOffGreen4(t, p, o) textureGatherOffset(t, p, o, 1)
  305. #endif
  306. #endif
  307. /*--------------------------------------------------------------------------*/
  308. #if (FXAA_GLSL_130 == 1)
  309. // Requires "#version 130" or better
  310. #define FxaaTexTop(t, p) textureLod(t, p, 0.0)
  311. #define FxaaTexOff(t, p, o, r) textureLodOffset(t, p, 0.0, o)
  312. #if (FXAA_GATHER4_ALPHA == 1)
  313. // use #extension GL_ARB_gpu_shader5 : enable
  314. #define FxaaTexAlpha4(t, p) textureGather(t, p, 3)
  315. #define FxaaTexOffAlpha4(t, p, o) textureGatherOffset(t, p, o, 3)
  316. #define FxaaTexGreen4(t, p) textureGather(t, p, 1)
  317. #define FxaaTexOffGreen4(t, p, o) textureGatherOffset(t, p, o, 1)
  318. #endif
  319. #endif
  320. /*--------------------------------------------------------------------------*/
  321. #if (FXAA_HLSL_3 == 1) || (FXAA_360 == 1) || (FXAA_PS3 == 1)
  322. #define FxaaInt2 float2
  323. #define FxaaTex sampler2D
  324. #define FxaaTexTop(t, p) tex2Dlod(t, float4(p, 0.0, 0.0))
  325. #define FxaaTexOff(t, p, o, r) tex2Dlod(t, float4(p + (o * r), 0, 0))
  326. #endif
  327. /*--------------------------------------------------------------------------*/
  328. #if (FXAA_HLSL_4 == 1)
  329. #define FxaaInt2 int2
  330. struct FxaaTex { SamplerState smpl; Texture2D tex; };
  331. #define FxaaTexTop(t, p) t.tex.SampleLevel(t.smpl, p, 0.0)
  332. #define FxaaTexOff(t, p, o, r) t.tex.SampleLevel(t.smpl, p, 0.0, o)
  333. #endif
  334. /*--------------------------------------------------------------------------*/
  335. #if (FXAA_HLSL_5 == 1)
  336. #define FxaaInt2 int2
  337. struct FxaaTex { SamplerState smpl; Texture2D tex; };
  338. #define FxaaTexTop(t, p) t.tex.SampleLevel(t.smpl, p, 0.0)
  339. #define FxaaTexOff(t, p, o, r) t.tex.SampleLevel(t.smpl, p, 0.0, o)
  340. #define FxaaTexAlpha4(t, p) t.tex.GatherAlpha(t.smpl, p)
  341. #define FxaaTexOffAlpha4(t, p, o) t.tex.GatherAlpha(t.smpl, p, o)
  342. #define FxaaTexGreen4(t, p) t.tex.GatherGreen(t.smpl, p)
  343. #define FxaaTexOffGreen4(t, p, o) t.tex.GatherGreen(t.smpl, p, o)
  344. #endif
  345. /*============================================================================
  346. GREEN AS LUMA OPTION SUPPORT FUNCTION
  347. ============================================================================*/
  348. FxaaFloat FxaaLuma(FxaaFloat4 rgba) {
  349. rgba.w = dot(rgba.rgb, FxaaFloat3(0.299, 0.587, 0.114));
  350. return rgba.w; }
  351. /*============================================================================
  352. FXAA3 QUALITY - PC
  353. ============================================================================*/
  354. FxaaFloat4 FxaaPixelShader(
  355. //
  356. // Use noperspective interpolation here (turn off perspective interpolation).
  357. // {xy} = center of pixel
  358. FxaaFloat2 pos,
  359. //
  360. // Input color texture.
  361. // {rgb_} = color in linear or perceptual color space
  362. FxaaTex tex,
  363. //
  364. // This must be from a constant/uniform.
  365. // {x_} = 1.0/screenWidthInPixels
  366. // {_y} = 1.0/screenHeightInPixels
  367. FxaaFloat2 fxaaQualityRcpFrame,
  368. //
  369. // This must be from a constant/uniform.
  370. // {x___} = -2.0/screenWidthInPixels
  371. // {_y__} = -2.0/screenHeightInPixels
  372. // {__z_} = 2.0/screenWidthInPixels
  373. // {___w} = 2.0/screenHeightInPixels
  374. FxaaFloat4 fxaaConsoleRcpFrameOpt2,
  375. //
  376. // FXAA_QUALITY__SUBPIX
  377. // Choose the amount of sub-pixel aliasing removal.
  378. // This can effect sharpness.
  379. // 1.00 - upper limit (softer)
  380. // 0.75 - default amount of filtering
  381. // 0.50 - lower limit (sharper, less sub-pixel aliasing removal)
  382. // 0.25 - almost off
  383. // 0.00 - completely off
  384. FxaaFloat fxaaQualitySubpix,
  385. //
  386. // FXAA_QUALITY__EDGE_THRESHOLD
  387. // The minimum amount of local contrast required to apply algorithm.
  388. // 0.333 - too little (faster)
  389. // 0.250 - low quality
  390. // 0.166 - default
  391. // 0.125 - high quality
  392. // 0.063 - overkill (slower)
  393. FxaaFloat fxaaQualityEdgeThreshold,
  394. //
  395. // FXAA_QUALITY__EDGE_THRESHOLD_MIN
  396. // Trims the algorithm from processing darks.
  397. // 0.0833 - upper limit (default, the start of visible unfiltered edges)
  398. // 0.0625 - high quality (faster)
  399. // 0.0312 - visible limit (slower)
  400. FxaaFloat fxaaQualityEdgeThresholdMin
  401. ) {
  402. /*--------------------------------------------------------------------------*/
  403. FxaaFloat2 posM;
  404. posM.x = pos.x;
  405. posM.y = pos.y;
  406. #if (FXAA_GATHER4_ALPHA == 1)
  407. FxaaFloat4 luma4A = FxaaTexGreen4(tex, posM);
  408. FxaaFloat4 luma4B = FxaaTexOffGreen4(tex, posM, FxaaInt2(-1, -1));
  409. #define lumaE luma4A.z
  410. #define lumaS luma4A.x
  411. #define lumaSE luma4A.y
  412. #define lumaNW luma4B.w
  413. #define lumaN luma4B.z
  414. #define lumaW luma4B.x
  415. #else
  416. FxaaFloat4 rgbyM = FxaaTexTop(tex, posM);
  417. #define lumaM rgbyM.y
  418. FxaaFloat lumaS = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 0, 1), fxaaQualityRcpFrame.xy));
  419. FxaaFloat lumaE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1, 0), fxaaQualityRcpFrame.xy));
  420. FxaaFloat lumaN = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 0,-1), fxaaQualityRcpFrame.xy));
  421. FxaaFloat lumaW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 0), fxaaQualityRcpFrame.xy));
  422. #endif
  423. /*--------------------------------------------------------------------------*/
  424. FxaaFloat maxSM = max(lumaS, lumaM);
  425. FxaaFloat minSM = min(lumaS, lumaM);
  426. FxaaFloat maxESM = max(lumaE, maxSM);
  427. FxaaFloat minESM = min(lumaE, minSM);
  428. FxaaFloat maxWN = max(lumaN, lumaW);
  429. FxaaFloat minWN = min(lumaN, lumaW);
  430. FxaaFloat rangeMax = max(maxWN, maxESM);
  431. FxaaFloat rangeMin = min(minWN, minESM);
  432. FxaaFloat rangeMaxScaled = rangeMax * fxaaQualityEdgeThreshold;
  433. FxaaFloat range = rangeMax - rangeMin;
  434. FxaaFloat rangeMaxClamped = max(fxaaQualityEdgeThresholdMin, rangeMaxScaled);
  435. FxaaBool earlyExit = range < rangeMaxClamped;
  436. if(earlyExit)
  437. return rgbyM;
  438. /*--------------------------------------------------------------------------*/
  439. #if (FXAA_GATHER4_ALPHA == 0)
  440. FxaaFloat lumaNW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1,-1), fxaaQualityRcpFrame.xy));
  441. FxaaFloat lumaSE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1, 1), fxaaQualityRcpFrame.xy));
  442. FxaaFloat lumaNE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2( 1,-1), fxaaQualityRcpFrame.xy));
  443. FxaaFloat lumaSW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 1), fxaaQualityRcpFrame.xy));
  444. #else
  445. FxaaFloat lumaNE = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(1, -1), fxaaQualityRcpFrame.xy));
  446. FxaaFloat lumaSW = FxaaLuma(FxaaTexOff(tex, posM, FxaaInt2(-1, 1), fxaaQualityRcpFrame.xy));
  447. #endif
  448. /*--------------------------------------------------------------------------*/
  449. FxaaFloat lumaNS = lumaN + lumaS;
  450. FxaaFloat lumaWE = lumaW + lumaE;
  451. FxaaFloat subpixRcpRange = 1.0/range;
  452. FxaaFloat subpixNSWE = lumaNS + lumaWE;
  453. FxaaFloat edgeHorz1 = (-2.0 * lumaM) + lumaNS;
  454. FxaaFloat edgeVert1 = (-2.0 * lumaM) + lumaWE;
  455. /*--------------------------------------------------------------------------*/
  456. FxaaFloat lumaNESE = lumaNE + lumaSE;
  457. FxaaFloat lumaNWNE = lumaNW + lumaNE;
  458. FxaaFloat edgeHorz2 = (-2.0 * lumaE) + lumaNESE;
  459. FxaaFloat edgeVert2 = (-2.0 * lumaN) + lumaNWNE;
  460. /*--------------------------------------------------------------------------*/
  461. FxaaFloat lumaNWSW = lumaNW + lumaSW;
  462. FxaaFloat lumaSWSE = lumaSW + lumaSE;
  463. FxaaFloat edgeHorz4 = (abs(edgeHorz1) * 2.0) + abs(edgeHorz2);
  464. FxaaFloat edgeVert4 = (abs(edgeVert1) * 2.0) + abs(edgeVert2);
  465. FxaaFloat edgeHorz3 = (-2.0 * lumaW) + lumaNWSW;
  466. FxaaFloat edgeVert3 = (-2.0 * lumaS) + lumaSWSE;
  467. FxaaFloat edgeHorz = abs(edgeHorz3) + edgeHorz4;
  468. FxaaFloat edgeVert = abs(edgeVert3) + edgeVert4;
  469. /*--------------------------------------------------------------------------*/
  470. FxaaFloat subpixNWSWNESE = lumaNWSW + lumaNESE;
  471. FxaaFloat lengthSign = fxaaQualityRcpFrame.x;
  472. FxaaBool horzSpan = edgeHorz >= edgeVert;
  473. FxaaFloat subpixA = subpixNSWE * 2.0 + subpixNWSWNESE;
  474. /*--------------------------------------------------------------------------*/
  475. if(!horzSpan) lumaN = lumaW;
  476. if(!horzSpan) lumaS = lumaE;
  477. if(horzSpan) lengthSign = fxaaQualityRcpFrame.y;
  478. FxaaFloat subpixB = (subpixA * (1.0/12.0)) - lumaM;
  479. /*--------------------------------------------------------------------------*/
  480. FxaaFloat gradientN = lumaN - lumaM;
  481. FxaaFloat gradientS = lumaS - lumaM;
  482. FxaaFloat lumaNN = lumaN + lumaM;
  483. FxaaFloat lumaSS = lumaS + lumaM;
  484. FxaaBool pairN = abs(gradientN) >= abs(gradientS);
  485. FxaaFloat gradient = max(abs(gradientN), abs(gradientS));
  486. if(pairN) lengthSign = -lengthSign;
  487. FxaaFloat subpixC = FxaaSat(abs(subpixB) * subpixRcpRange);
  488. /*--------------------------------------------------------------------------*/
  489. FxaaFloat2 posB;
  490. posB.x = posM.x;
  491. posB.y = posM.y;
  492. FxaaFloat2 offNP;
  493. offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x;
  494. offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y;
  495. if(!horzSpan) posB.x += lengthSign * 0.5;
  496. if( horzSpan) posB.y += lengthSign * 0.5;
  497. /*--------------------------------------------------------------------------*/
  498. FxaaFloat2 posN;
  499. posN.x = posB.x - offNP.x * FXAA_QUALITY__P0;
  500. posN.y = posB.y - offNP.y * FXAA_QUALITY__P0;
  501. FxaaFloat2 posP;
  502. posP.x = posB.x + offNP.x * FXAA_QUALITY__P0;
  503. posP.y = posB.y + offNP.y * FXAA_QUALITY__P0;
  504. FxaaFloat subpixD = ((-2.0)*subpixC) + 3.0;
  505. FxaaFloat lumaEndN = FxaaLuma(FxaaTexTop(tex, posN));
  506. FxaaFloat subpixE = subpixC * subpixC;
  507. FxaaFloat lumaEndP = FxaaLuma(FxaaTexTop(tex, posP));
  508. /*--------------------------------------------------------------------------*/
  509. if(!pairN) lumaNN = lumaSS;
  510. FxaaFloat gradientScaled = gradient * 1.0/4.0;
  511. FxaaFloat lumaMM = lumaM - lumaNN * 0.5;
  512. FxaaFloat subpixF = subpixD * subpixE;
  513. FxaaBool lumaMLTZero = lumaMM < 0.0;
  514. /*--------------------------------------------------------------------------*/
  515. lumaEndN -= lumaNN * 0.5;
  516. lumaEndP -= lumaNN * 0.5;
  517. FxaaBool doneN = abs(lumaEndN) >= gradientScaled;
  518. FxaaBool doneP = abs(lumaEndP) >= gradientScaled;
  519. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P1;
  520. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P1;
  521. FxaaBool doneNP = (!doneN) || (!doneP);
  522. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P1;
  523. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P1;
  524. /*--------------------------------------------------------------------------*/
  525. if(doneNP) {
  526. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  527. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  528. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  529. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  530. doneN = abs(lumaEndN) >= gradientScaled;
  531. doneP = abs(lumaEndP) >= gradientScaled;
  532. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P2;
  533. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P2;
  534. doneNP = (!doneN) || (!doneP);
  535. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P2;
  536. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P2;
  537. /*--------------------------------------------------------------------------*/
  538. #if (FXAA_QUALITY__PS > 3)
  539. if(doneNP) {
  540. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  541. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  542. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  543. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  544. doneN = abs(lumaEndN) >= gradientScaled;
  545. doneP = abs(lumaEndP) >= gradientScaled;
  546. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P3;
  547. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P3;
  548. doneNP = (!doneN) || (!doneP);
  549. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P3;
  550. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P3;
  551. /*--------------------------------------------------------------------------*/
  552. #if (FXAA_QUALITY__PS > 4)
  553. if(doneNP) {
  554. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  555. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  556. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  557. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  558. doneN = abs(lumaEndN) >= gradientScaled;
  559. doneP = abs(lumaEndP) >= gradientScaled;
  560. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P4;
  561. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P4;
  562. doneNP = (!doneN) || (!doneP);
  563. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P4;
  564. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P4;
  565. /*--------------------------------------------------------------------------*/
  566. #if (FXAA_QUALITY__PS > 5)
  567. if(doneNP) {
  568. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  569. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  570. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  571. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  572. doneN = abs(lumaEndN) >= gradientScaled;
  573. doneP = abs(lumaEndP) >= gradientScaled;
  574. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P5;
  575. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P5;
  576. doneNP = (!doneN) || (!doneP);
  577. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P5;
  578. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P5;
  579. /*--------------------------------------------------------------------------*/
  580. #if (FXAA_QUALITY__PS > 6)
  581. if(doneNP) {
  582. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  583. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  584. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  585. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  586. doneN = abs(lumaEndN) >= gradientScaled;
  587. doneP = abs(lumaEndP) >= gradientScaled;
  588. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P6;
  589. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P6;
  590. doneNP = (!doneN) || (!doneP);
  591. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P6;
  592. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P6;
  593. /*--------------------------------------------------------------------------*/
  594. #if (FXAA_QUALITY__PS > 7)
  595. if(doneNP) {
  596. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  597. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  598. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  599. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  600. doneN = abs(lumaEndN) >= gradientScaled;
  601. doneP = abs(lumaEndP) >= gradientScaled;
  602. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P7;
  603. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P7;
  604. doneNP = (!doneN) || (!doneP);
  605. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P7;
  606. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P7;
  607. /*--------------------------------------------------------------------------*/
  608. #if (FXAA_QUALITY__PS > 8)
  609. if(doneNP) {
  610. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  611. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  612. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  613. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  614. doneN = abs(lumaEndN) >= gradientScaled;
  615. doneP = abs(lumaEndP) >= gradientScaled;
  616. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P8;
  617. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P8;
  618. doneNP = (!doneN) || (!doneP);
  619. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P8;
  620. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P8;
  621. /*--------------------------------------------------------------------------*/
  622. #if (FXAA_QUALITY__PS > 9)
  623. if(doneNP) {
  624. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  625. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  626. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  627. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  628. doneN = abs(lumaEndN) >= gradientScaled;
  629. doneP = abs(lumaEndP) >= gradientScaled;
  630. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P9;
  631. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P9;
  632. doneNP = (!doneN) || (!doneP);
  633. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P9;
  634. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P9;
  635. /*--------------------------------------------------------------------------*/
  636. #if (FXAA_QUALITY__PS > 10)
  637. if(doneNP) {
  638. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  639. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  640. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  641. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  642. doneN = abs(lumaEndN) >= gradientScaled;
  643. doneP = abs(lumaEndP) >= gradientScaled;
  644. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P10;
  645. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P10;
  646. doneNP = (!doneN) || (!doneP);
  647. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P10;
  648. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P10;
  649. /*--------------------------------------------------------------------------*/
  650. #if (FXAA_QUALITY__PS > 11)
  651. if(doneNP) {
  652. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  653. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  654. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  655. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  656. doneN = abs(lumaEndN) >= gradientScaled;
  657. doneP = abs(lumaEndP) >= gradientScaled;
  658. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P11;
  659. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P11;
  660. doneNP = (!doneN) || (!doneP);
  661. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P11;
  662. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P11;
  663. /*--------------------------------------------------------------------------*/
  664. #if (FXAA_QUALITY__PS > 12)
  665. if(doneNP) {
  666. if(!doneN) lumaEndN = FxaaLuma(FxaaTexTop(tex, posN.xy));
  667. if(!doneP) lumaEndP = FxaaLuma(FxaaTexTop(tex, posP.xy));
  668. if(!doneN) lumaEndN = lumaEndN - lumaNN * 0.5;
  669. if(!doneP) lumaEndP = lumaEndP - lumaNN * 0.5;
  670. doneN = abs(lumaEndN) >= gradientScaled;
  671. doneP = abs(lumaEndP) >= gradientScaled;
  672. if(!doneN) posN.x -= offNP.x * FXAA_QUALITY__P12;
  673. if(!doneN) posN.y -= offNP.y * FXAA_QUALITY__P12;
  674. doneNP = (!doneN) || (!doneP);
  675. if(!doneP) posP.x += offNP.x * FXAA_QUALITY__P12;
  676. if(!doneP) posP.y += offNP.y * FXAA_QUALITY__P12;
  677. }
  678. #endif
  679. }
  680. #endif
  681. }
  682. #endif
  683. }
  684. #endif
  685. }
  686. #endif
  687. }
  688. #endif
  689. }
  690. #endif
  691. }
  692. #endif
  693. }
  694. #endif
  695. }
  696. #endif
  697. }
  698. FxaaFloat dstN = posM.x - posN.x;
  699. FxaaFloat dstP = posP.x - posM.x;
  700. if(!horzSpan) dstN = posM.y - posN.y;
  701. if(!horzSpan) dstP = posP.y - posM.y;
  702. /*--------------------------------------------------------------------------*/
  703. FxaaBool goodSpanN = (lumaEndN < 0.0) != lumaMLTZero;
  704. FxaaFloat spanLength = (dstP + dstN);
  705. FxaaBool goodSpanP = (lumaEndP < 0.0) != lumaMLTZero;
  706. FxaaFloat spanLengthRcp = 1.0/spanLength;
  707. /*--------------------------------------------------------------------------*/
  708. FxaaBool directionN = dstN < dstP;
  709. FxaaFloat dst = min(dstN, dstP);
  710. FxaaBool goodSpan = directionN ? goodSpanN : goodSpanP;
  711. FxaaFloat subpixG = subpixF * subpixF;
  712. FxaaFloat pixelOffset = (dst * (-spanLengthRcp)) + 0.5;
  713. FxaaFloat subpixH = subpixG * fxaaQualitySubpix;
  714. /*--------------------------------------------------------------------------*/
  715. FxaaFloat pixelOffsetGood = goodSpan ? pixelOffset : 0.0;
  716. FxaaFloat pixelOffsetSubpix = max(pixelOffsetGood, subpixH);
  717. if(!horzSpan) posM.x += pixelOffsetSubpix * lengthSign;
  718. if( horzSpan) posM.y += pixelOffsetSubpix * lengthSign;
  719. return FxaaFloat4(FxaaTexTop(tex, posM).xyz, lumaM);
  720. }
  721. /*==========================================================================*/