AntiAliasing.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using nkast.Aether.Shaders;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. namespace Samples.FXAA
  6. {
  7. public class AntiAliasing
  8. {
  9. GraphicsDevice _graphicsDevice;
  10. SpriteBatch _spriteBatch;
  11. RenderTarget2D _renderTarget;
  12. internal FXAAEffect fxaaGreenLumaLowEffect;
  13. #if(WINDOWS || W10)
  14. internal FXAAEffect fxaaGreenLumaMediumEffect;
  15. internal FXAAEffect fxaaGreenLumaHighEffect;
  16. #endif
  17. public AntiAliasing(GraphicsDevice graphicsDevice)
  18. {
  19. _graphicsDevice = graphicsDevice;
  20. _spriteBatch = new SpriteBatch(_graphicsDevice);
  21. CreateEffect();
  22. ResizeRenderTarget(_graphicsDevice.Viewport);
  23. }
  24. private void CreateEffect()
  25. {
  26. Viewport viewport = _graphicsDevice.Viewport;
  27. try // try to create a 9_3 shader.
  28. {
  29. fxaaGreenLumaLowEffect = new FXAAGreenLumaLowEffect(_graphicsDevice);
  30. SetEffectParameters(fxaaGreenLumaLowEffect, 0.5f, viewport);
  31. }
  32. catch (Exception ex1) { }
  33. #if(WINDOWS || W10)
  34. try
  35. {
  36. if (_graphicsDevice.GraphicsProfile >= GraphicsProfile.HiDef)
  37. {
  38. fxaaGreenLumaMediumEffect = new FXAAGreenLumaMediumEffect(_graphicsDevice);
  39. fxaaGreenLumaHighEffect = new FXAAGreenLumaHighEffect(_graphicsDevice);
  40. SetEffectParameters(fxaaGreenLumaMediumEffect, 0.5f, viewport);
  41. SetEffectParameters(fxaaGreenLumaHighEffect, 0.5f, viewport);
  42. }
  43. }
  44. catch(Exception ex2) {}
  45. #endif
  46. return;
  47. }
  48. private void SetEffectParameters(FXAAEffect effect, float N, Viewport viewport)
  49. {
  50. effect.SetDefaultParameters(viewport.Width, viewport.Height);
  51. effect.AntialiasingEnabled = true;
  52. }
  53. private void ResizeRenderTarget(Viewport viewport)
  54. {
  55. int maxWidth = viewport.Width;
  56. int maxHeight = viewport.Height;
  57. int maxTexDim = (_graphicsDevice.GraphicsProfile>=GraphicsProfile.HiDef) ? 4096 : 2048;
  58. maxWidth = Math.Min(maxTexDim, maxWidth);
  59. maxHeight = Math.Min(maxTexDim, maxHeight);
  60. if (_renderTarget != null)
  61. {
  62. if (_renderTarget.Width == maxWidth && _renderTarget.Height == maxHeight) return;
  63. if (!_renderTarget.IsDisposed) _renderTarget.Dispose();
  64. _renderTarget = null;
  65. }
  66. if (_renderTarget == null)
  67. {
  68. _renderTarget = new RenderTarget2D(_graphicsDevice, maxWidth, maxHeight,
  69. false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);
  70. if (fxaaGreenLumaLowEffect!= null)
  71. {
  72. fxaaGreenLumaLowEffect.SetDefaultParameters(maxWidth, maxHeight);
  73. }
  74. if (_graphicsDevice.GraphicsProfile >= GraphicsProfile.HiDef)
  75. {
  76. fxaaGreenLumaMediumEffect.SetDefaultParameters(maxWidth, maxHeight);
  77. fxaaGreenLumaHighEffect.SetDefaultParameters(maxWidth, maxHeight);
  78. }
  79. }
  80. return;
  81. }
  82. public void SetRenderTarget(Viewport viewport, Color color)
  83. {
  84. ResizeRenderTarget(viewport);
  85. _graphicsDevice.SetRenderTarget(_renderTarget);
  86. _graphicsDevice.Clear(color);
  87. int width = viewport.Width;
  88. int height = viewport.Height;
  89. int maxTexDim = (_graphicsDevice.GraphicsProfile >= GraphicsProfile.HiDef) ? 4096 : 2048;
  90. int maxWidth = Math.Min(maxTexDim, width);
  91. int maxHeight = Math.Min(maxTexDim, height);
  92. _graphicsDevice.Viewport = new Viewport(0, 0, width, height);
  93. }
  94. public void DrawRenderTarget(int antiAliasingLevel, Viewport viewport, bool clearRenderTarget)
  95. {
  96. FXAAEffect fxaaEffect = fxaaGreenLumaLowEffect;
  97. #if(WINDOWS || W10)
  98. if (antiAliasingLevel == 1) fxaaEffect = fxaaGreenLumaLowEffect;
  99. if (antiAliasingLevel == 2) fxaaEffect = fxaaGreenLumaMediumEffect;
  100. if (antiAliasingLevel == 3) fxaaEffect = fxaaGreenLumaHighEffect;
  101. #endif
  102. fxaaEffect.AntialiasingEnabled = (antiAliasingLevel != 0);
  103. if(clearRenderTarget)
  104. _graphicsDevice.SetRenderTarget(null);
  105. Rectangle srcRect;
  106. srcRect = new Rectangle(0, 0, (int)(_renderTarget.Width), (int)(_renderTarget.Height));
  107. Viewport oldViewport = _graphicsDevice.Viewport;
  108. _graphicsDevice.Viewport = viewport;
  109. Rectangle destRect = new Rectangle(0, 0, viewport.Width, viewport.Height);
  110. fxaaEffect.CurrentTechnique.Passes[0].Apply();
  111. _spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.LinearClamp, null, null, fxaaEffect);
  112. _spriteBatch.Draw(_renderTarget, destRect, srcRect, Color.White);
  113. _spriteBatch.End();
  114. _graphicsDevice.Viewport = oldViewport;
  115. }
  116. }
  117. }