FXAAEffect.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 = GetAssembly(typeof(FXAAEffect)).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 = GetAssembly(typeof(Effect)).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. private static Assembly GetAssembly(Type type)
  91. {
  92. #if W10
  93. return type.GetTypeInfo().Assembly;
  94. #else
  95. return type.Assembly;
  96. #endif
  97. }
  98. #endregion
  99. #region Public Properties
  100. /// <summary>
  101. /// Choose the amount of sub-pixel aliasing removal.
  102. /// This can effect sharpness.
  103. /// 1.00 - upper limit (softer)
  104. /// 0.75 - default amount of filtering
  105. /// 0.50 - lower limit (sharper, less sub-pixel aliasing removal)
  106. /// 0.25 - almost off
  107. /// 0.00 - completely off
  108. /// </summary>
  109. public float SubPixelAliasingRemoval
  110. {
  111. get { return subPixelAliasingRemovalParam.GetValueSingle(); }
  112. set { subPixelAliasingRemovalParam.SetValue(value); }
  113. }
  114. /// <summary>
  115. /// The minimum amount of local contrast required to apply algorithm.
  116. /// 0.333 - too little (faster)
  117. /// 0.250 - low quality
  118. /// 0.166 - default
  119. /// 0.125 - high quality
  120. /// 0.063 - overkill (slower)
  121. /// </summary>
  122. public float EdgeThreshold
  123. {
  124. get { return edgeThresholdParam.GetValueSingle(); }
  125. set { edgeThresholdParam.SetValue(value); }
  126. }
  127. /// <summary>
  128. /// Trims the algorithm from processing darks.
  129. /// 0.0833 - upper limit (default, the start of visible unfiltered edges)
  130. /// 0.0625 - high quality (faster)
  131. /// 0.0312 - visible limit (slower)
  132. /// Special notes when using FXAA_GREEN_AS_LUMA,
  133. /// Likely want to set this to zero.
  134. /// As colors that are mostly not-green
  135. /// will appear very dark in the green channel!
  136. /// Tune by looking at mostly non-green content,
  137. /// then start at zero and increase until aliasing is a problem.
  138. /// </summary>
  139. public float EdgeThresholdMin
  140. {
  141. get { return edgeThresholdMinParam.GetValueSingle(); }
  142. set { edgeThresholdMinParam.SetValue(value); }
  143. }
  144. public float ConsoleEdgeSharpness
  145. {
  146. get { return consoleEdgeSharpnessParam.GetValueSingle(); }
  147. set { consoleEdgeSharpnessParam.SetValue(value); }
  148. }
  149. public float ConsoleEdgeThreshold
  150. {
  151. get { return consoleEdgeThresholdParam.GetValueSingle(); }
  152. set { consoleEdgeThresholdParam.SetValue(value); }
  153. }
  154. public float ConsoleEdgeThresholdMin
  155. {
  156. get { return consoleEdgeThresholdMinParam.GetValueSingle(); }
  157. set { consoleEdgeThresholdMinParam.SetValue(value); }
  158. }
  159. public Vector2 InverseViewportSize
  160. {
  161. get { return inverseViewportSizeParam.GetValueVector2(); }
  162. set { inverseViewportSizeParam.SetValue(value); }
  163. }
  164. public Vector4 ConsoleSharpness
  165. {
  166. get { return consoleSharpnessParam.GetValueVector4(); }
  167. set { consoleSharpnessParam.SetValue(value); }
  168. }
  169. public Vector4 ConsoleOpt1
  170. {
  171. get { return consoleOpt1Param.GetValueVector4(); }
  172. set { consoleOpt1Param.SetValue(value); }
  173. }
  174. public Vector4 ConsoleOpt2
  175. {
  176. get { return consoleOpt2Param.GetValueVector4(); }
  177. set { consoleOpt2Param.SetValue(value); }
  178. }
  179. public Matrix Projection
  180. {
  181. get { return projectionParam.GetValueMatrix(); }
  182. set { projectionParam.SetValue(value); }
  183. }
  184. public Matrix View
  185. {
  186. get { return viewParam.GetValueMatrix(); }
  187. set { viewParam.SetValue(value); }
  188. }
  189. public Matrix World
  190. {
  191. get { return worldParam.GetValueMatrix(); }
  192. set { worldParam.SetValue(value); }
  193. }
  194. public bool AntialiasingEnabled
  195. {
  196. get { return (techniqueIndex & FXAA) == FXAA; }
  197. set
  198. {
  199. techniqueIndex = (value) ? (techniqueIndex|FXAA) : (techniqueIndex&~FXAA);
  200. CurrentTechnique = Techniques[techniqueIndex];
  201. }
  202. }
  203. #endregion
  204. #region Methods
  205. public FXAAEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  206. {
  207. CacheEffectParameters(null);
  208. SubPixelAliasingRemoval = 0.75f;
  209. EdgeThreshold = 0.166f;
  210. EdgeThresholdMin = 0.0833f;
  211. ConsoleEdgeSharpness = 8.0f;
  212. ConsoleEdgeThreshold = 0.125f;
  213. ConsoleEdgeThresholdMin = 0f;
  214. AntialiasingEnabled = true;
  215. }
  216. protected FXAAEffect(FXAAEffect cloneSource)
  217. : base(cloneSource)
  218. {
  219. CacheEffectParameters(cloneSource);
  220. AntialiasingEnabled = cloneSource.AntialiasingEnabled;
  221. }
  222. /// <param name="N">
  223. /// This effects sub-pixel AA quality and inversely sharpness.
  224. /// Where N ranges between,
  225. /// N = 0.50 (default)
  226. /// N = 0.33 (sharper)
  227. /// </param>
  228. public void SetDefaultParameters(int width, int height, float N = 0.5f)
  229. {
  230. Matrix projection = Matrix.CreateOrthographicOffCenter(0, width, height, 0, 0, 1);
  231. #if XNA
  232. Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
  233. #else
  234. Matrix halfPixelOffset = Matrix.Identity;
  235. #endif
  236. World = Matrix.Identity;
  237. View = Matrix.Identity;
  238. Projection = halfPixelOffset * projection;
  239. InverseViewportSize = new Vector2(1f / width, 1f / height);
  240. ConsoleSharpness = new Vector4(
  241. -N / width, -N / height,
  242. N / width, N / height);
  243. ConsoleOpt1 = new Vector4(
  244. -2.0f / width, -2.0f / height,
  245. 2.0f / width, 2.0f / height);
  246. ConsoleOpt2 = new Vector4(
  247. 8.0f / width, 8.0f / height,
  248. -4.0f / width, -4.0f / height);
  249. }
  250. public override Effect Clone()
  251. {
  252. return new FXAAEffect(this);
  253. }
  254. void CacheEffectParameters(FXAAEffect cloneSource)
  255. {
  256. subPixelAliasingRemovalParam = Parameters["SubPixelAliasingRemoval"];
  257. edgeThresholdParam = Parameters["EdgeThreshold"];
  258. edgeThresholdMinParam = Parameters["EdgeThresholdMin"];
  259. consoleEdgeSharpnessParam = Parameters["ConsoleEdgeSharpness"];
  260. consoleEdgeThresholdParam = Parameters["ConsoleEdgeThreshold"];
  261. consoleEdgeThresholdMinParam = Parameters["ConsoleEdgeThresholdMin"];
  262. inverseViewportSizeParam = Parameters["InverseViewportSize"];
  263. consoleSharpnessParam = Parameters["ConsoleSharpness"];
  264. consoleOpt1Param = Parameters["ConsoleOpt1"];
  265. consoleOpt2Param = Parameters["ConsoleOpt2"];
  266. projectionParam = Parameters["Projection"];
  267. viewParam = Parameters["View"];
  268. worldParam = Parameters["World"];
  269. }
  270. #endregion
  271. }
  272. }