2
0

FXAAEffect.cs 9.9 KB

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