FXAAEffect.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #region License
  2. // Copyright 2013-2016 Kastellanos Nikolaos
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.IO;
  18. using System.Reflection;
  19. using Microsoft.Xna.Framework;
  20. using Microsoft.Xna.Framework.Graphics;
  21. namespace nkast.Aether.Shaders
  22. {
  23. public class FXAAEffect : Effect , IEffectMatrices
  24. {
  25. #region Effect Parameters
  26. EffectParameter subPixelAliasingRemovalParam;
  27. EffectParameter edgeThresholdParam;
  28. EffectParameter edgeThresholdMinParam;
  29. EffectParameter consoleEdgeSharpnessParam;
  30. EffectParameter consoleEdgeThresholdParam;
  31. EffectParameter consoleEdgeThresholdMinParam;
  32. EffectParameter inverseViewportSizeParam;
  33. EffectParameter consoleSharpnessParam;
  34. EffectParameter consoleOpt1Param;
  35. EffectParameter consoleOpt2Param;
  36. EffectParameter projectionParam;
  37. EffectParameter viewParam;
  38. EffectParameter worldParam;
  39. #endregion
  40. #region Fields
  41. int techniqueIndex = 0;
  42. public const int FXAA = 0x00000001;
  43. internal static byte[] LoadEffectResource(GraphicsDevice graphicsDevice, string name)
  44. {
  45. name = GetResourceName(graphicsDevice, name);
  46. using (Stream stream = typeof(FXAAEffect).Assembly.GetManifestResourceStream(name))
  47. {
  48. byte[] bytecode = new byte[stream.Length];
  49. stream.Read(bytecode, 0, (int)stream.Length);
  50. return bytecode;
  51. }
  52. }
  53. private static string GetResourceName(GraphicsDevice graphicsDevice, string name)
  54. {
  55. string platformName = "";
  56. string version = "";
  57. #if XNA
  58. platformName = ".xna.WinHiDef";
  59. #else
  60. switch (graphicsDevice.Adapter.Backend)
  61. {
  62. case GraphicsBackend.DirectX11:
  63. platformName = ".dx11.fxo";
  64. break;
  65. case GraphicsBackend.OpenGL:
  66. case GraphicsBackend.GLES:
  67. case GraphicsBackend.WebGL:
  68. platformName = ".ogl.fxo";
  69. break;
  70. default:
  71. throw new NotSupportedException("platform");
  72. }
  73. // Detect version
  74. version = ".10";
  75. Version kniVersion = typeof(Effect).Assembly.GetName().Version;
  76. if (kniVersion.Major == 3)
  77. {
  78. if (kniVersion.Minor == 9)
  79. {
  80. version = ".10";
  81. }
  82. if (kniVersion.Minor == 10)
  83. {
  84. version = ".10";
  85. }
  86. }
  87. #endif
  88. return name + platformName + version;
  89. }
  90. #endregion
  91. #region Public Properties
  92. /// <summary>
  93. /// Choose the amount of sub-pixel aliasing removal.
  94. /// This can effect sharpness.
  95. /// 1.00 - upper limit (softer)
  96. /// 0.75 - default amount of filtering
  97. /// 0.50 - lower limit (sharper, less sub-pixel aliasing removal)
  98. /// 0.25 - almost off
  99. /// 0.00 - completely off
  100. /// </summary>
  101. public float SubPixelAliasingRemoval
  102. {
  103. get { return subPixelAliasingRemovalParam.GetValueSingle(); }
  104. set { subPixelAliasingRemovalParam.SetValue(value); }
  105. }
  106. /// <summary>
  107. /// The minimum amount of local contrast required to apply algorithm.
  108. /// 0.333 - too little (faster)
  109. /// 0.250 - low quality
  110. /// 0.166 - default
  111. /// 0.125 - high quality
  112. /// 0.063 - overkill (slower)
  113. /// </summary>
  114. public float EdgeThreshold
  115. {
  116. get { return edgeThresholdParam.GetValueSingle(); }
  117. set { edgeThresholdParam.SetValue(value); }
  118. }
  119. /// <summary>
  120. /// Trims the algorithm from processing darks.
  121. /// 0.0833 - upper limit (default, the start of visible unfiltered edges)
  122. /// 0.0625 - high quality (faster)
  123. /// 0.0312 - visible limit (slower)
  124. /// Special notes when using FXAA_GREEN_AS_LUMA,
  125. /// Likely want to set this to zero.
  126. /// As colors that are mostly not-green
  127. /// will appear very dark in the green channel!
  128. /// Tune by looking at mostly non-green content,
  129. /// then start at zero and increase until aliasing is a problem.
  130. /// </summary>
  131. public float EdgeThresholdMin
  132. {
  133. get { return edgeThresholdMinParam.GetValueSingle(); }
  134. set { edgeThresholdMinParam.SetValue(value); }
  135. }
  136. public float ConsoleEdgeSharpness
  137. {
  138. get { return consoleEdgeSharpnessParam.GetValueSingle(); }
  139. set { consoleEdgeSharpnessParam.SetValue(value); }
  140. }
  141. public float ConsoleEdgeThreshold
  142. {
  143. get { return consoleEdgeThresholdParam.GetValueSingle(); }
  144. set { consoleEdgeThresholdParam.SetValue(value); }
  145. }
  146. public float ConsoleEdgeThresholdMin
  147. {
  148. get { return consoleEdgeThresholdMinParam.GetValueSingle(); }
  149. set { consoleEdgeThresholdMinParam.SetValue(value); }
  150. }
  151. public Vector2 InverseViewportSize
  152. {
  153. get { return inverseViewportSizeParam.GetValueVector2(); }
  154. set { inverseViewportSizeParam.SetValue(value); }
  155. }
  156. public Vector4 ConsoleSharpness
  157. {
  158. get { return consoleSharpnessParam.GetValueVector4(); }
  159. set { consoleSharpnessParam.SetValue(value); }
  160. }
  161. public Vector4 ConsoleOpt1
  162. {
  163. get { return consoleOpt1Param.GetValueVector4(); }
  164. set { consoleOpt1Param.SetValue(value); }
  165. }
  166. public Vector4 ConsoleOpt2
  167. {
  168. get { return consoleOpt2Param.GetValueVector4(); }
  169. set { consoleOpt2Param.SetValue(value); }
  170. }
  171. public Matrix Projection
  172. {
  173. get { return projectionParam.GetValueMatrix(); }
  174. set { projectionParam.SetValue(value); }
  175. }
  176. public Matrix View
  177. {
  178. get { return viewParam.GetValueMatrix(); }
  179. set { viewParam.SetValue(value); }
  180. }
  181. public Matrix World
  182. {
  183. get { return worldParam.GetValueMatrix(); }
  184. set { worldParam.SetValue(value); }
  185. }
  186. public bool AntialiasingEnabled
  187. {
  188. get { return (techniqueIndex & FXAA) == FXAA; }
  189. set
  190. {
  191. techniqueIndex = (value) ? (techniqueIndex|FXAA) : (techniqueIndex&~FXAA);
  192. CurrentTechnique = Techniques[techniqueIndex];
  193. }
  194. }
  195. #endregion
  196. #region Methods
  197. public FXAAEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  198. {
  199. CacheEffectParameters(null);
  200. SubPixelAliasingRemoval = 0.75f;
  201. EdgeThreshold = 0.166f;
  202. EdgeThresholdMin = 0.0833f;
  203. ConsoleEdgeSharpness = 8.0f;
  204. ConsoleEdgeThreshold = 0.125f;
  205. ConsoleEdgeThresholdMin = 0f;
  206. AntialiasingEnabled = true;
  207. }
  208. protected FXAAEffect(FXAAEffect cloneSource)
  209. : base(cloneSource)
  210. {
  211. CacheEffectParameters(cloneSource);
  212. AntialiasingEnabled = cloneSource.AntialiasingEnabled;
  213. }
  214. /// <param name="N">
  215. /// This effects sub-pixel AA quality and inversely sharpness.
  216. /// Where N ranges between,
  217. /// N = 0.50 (default)
  218. /// N = 0.33 (sharper)
  219. /// </param>
  220. public void SetDefaultParameters(int width, int height, float N = 0.5f)
  221. {
  222. Matrix projection = Matrix.CreateOrthographicOffCenter(0, width, height, 0, 0, 1);
  223. #if XNA
  224. Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
  225. #else
  226. Matrix halfPixelOffset = Matrix.Identity;
  227. #endif
  228. World = Matrix.Identity;
  229. View = Matrix.Identity;
  230. Projection = halfPixelOffset * projection;
  231. InverseViewportSize = new Vector2(1f / width, 1f / height);
  232. ConsoleSharpness = new Vector4(
  233. -N / width, -N / height,
  234. N / width, N / height);
  235. ConsoleOpt1 = new Vector4(
  236. -2.0f / width, -2.0f / height,
  237. 2.0f / width, 2.0f / height);
  238. ConsoleOpt2 = new Vector4(
  239. 8.0f / width, 8.0f / height,
  240. -4.0f / width, -4.0f / height);
  241. }
  242. public override Effect Clone()
  243. {
  244. return new FXAAEffect(this);
  245. }
  246. void CacheEffectParameters(FXAAEffect cloneSource)
  247. {
  248. subPixelAliasingRemovalParam = Parameters["SubPixelAliasingRemoval"];
  249. edgeThresholdParam = Parameters["EdgeThreshold"];
  250. edgeThresholdMinParam = Parameters["EdgeThresholdMin"];
  251. consoleEdgeSharpnessParam = Parameters["ConsoleEdgeSharpness"];
  252. consoleEdgeThresholdParam = Parameters["ConsoleEdgeThreshold"];
  253. consoleEdgeThresholdMinParam = Parameters["ConsoleEdgeThresholdMin"];
  254. inverseViewportSizeParam = Parameters["InverseViewportSize"];
  255. consoleSharpnessParam = Parameters["ConsoleSharpness"];
  256. consoleOpt1Param = Parameters["ConsoleOpt1"];
  257. consoleOpt2Param = Parameters["ConsoleOpt2"];
  258. projectionParam = Parameters["Projection"];
  259. viewParam = Parameters["View"];
  260. worldParam = Parameters["World"];
  261. }
  262. #endregion
  263. }
  264. }