/MonoGame.Framework/Graphics/States/TargetBlendState.cs

https://github.com/barbanish/MonoGame · C# · 140 lines · 99 code · 37 blank · 4 comment · 10 complexity · f788f5e998d4dc6038c452edbcacb4f8 MD5 · raw file

  1. // MonoGame - Copyright (C) The MonoGame Team
  2. // This file is subject to the terms and conditions defined in
  3. // file 'LICENSE.txt', which is part of this source code package.
  4. using System;
  5. namespace Microsoft.Xna.Framework.Graphics
  6. {
  7. public class TargetBlendState
  8. {
  9. internal TargetBlendState()
  10. {
  11. AlphaBlendFunction = BlendFunction.Add;
  12. AlphaDestinationBlend = Blend.Zero;
  13. AlphaSourceBlend = Blend.One;
  14. ColorBlendFunction = BlendFunction.Add;
  15. ColorDestinationBlend = Blend.Zero;
  16. ColorSourceBlend = Blend.One;
  17. ColorWriteChannels = ColorWriteChannels.All;
  18. }
  19. public BlendFunction AlphaBlendFunction { get; set; }
  20. public Blend AlphaDestinationBlend { get; set; }
  21. public Blend AlphaSourceBlend { get; set; }
  22. public BlendFunction ColorBlendFunction { get; set; }
  23. public Blend ColorDestinationBlend { get; set; }
  24. public Blend ColorSourceBlend { get; set; }
  25. public ColorWriteChannels ColorWriteChannels { get; set; }
  26. #if DIRECTX
  27. internal void GetState(ref SharpDX.Direct3D11.RenderTargetBlendDescription desc)
  28. {
  29. // We're blending if we're not in the opaque state.
  30. desc.IsBlendEnabled = !( ColorSourceBlend == Blend.One &&
  31. ColorDestinationBlend == Blend.Zero &&
  32. AlphaSourceBlend == Blend.One &&
  33. AlphaDestinationBlend == Blend.Zero);
  34. desc.BlendOperation = GetBlendOperation(ColorBlendFunction);
  35. desc.SourceBlend = GetBlendOption(ColorSourceBlend, false);
  36. desc.DestinationBlend = GetBlendOption(ColorDestinationBlend, false);
  37. desc.AlphaBlendOperation = GetBlendOperation(AlphaBlendFunction);
  38. desc.SourceAlphaBlend = GetBlendOption(AlphaSourceBlend, true);
  39. desc.DestinationAlphaBlend = GetBlendOption(AlphaDestinationBlend, true);
  40. desc.RenderTargetWriteMask = GetColorWriteMask(ColorWriteChannels);
  41. }
  42. static private SharpDX.Direct3D11.BlendOperation GetBlendOperation(BlendFunction blend)
  43. {
  44. switch (blend)
  45. {
  46. case BlendFunction.Add:
  47. return SharpDX.Direct3D11.BlendOperation.Add;
  48. case BlendFunction.Max:
  49. return SharpDX.Direct3D11.BlendOperation.Maximum;
  50. case BlendFunction.Min:
  51. return SharpDX.Direct3D11.BlendOperation.Minimum;
  52. case BlendFunction.ReverseSubtract:
  53. return SharpDX.Direct3D11.BlendOperation.ReverseSubtract;
  54. case BlendFunction.Subtract:
  55. return SharpDX.Direct3D11.BlendOperation.Subtract;
  56. default:
  57. throw new ArgumentException("Invalid blend function!");
  58. }
  59. }
  60. static private SharpDX.Direct3D11.BlendOption GetBlendOption(Blend blend, bool alpha)
  61. {
  62. switch (blend)
  63. {
  64. case Blend.BlendFactor:
  65. return SharpDX.Direct3D11.BlendOption.BlendFactor;
  66. case Blend.DestinationAlpha:
  67. return SharpDX.Direct3D11.BlendOption.DestinationAlpha;
  68. case Blend.DestinationColor:
  69. return alpha ? SharpDX.Direct3D11.BlendOption.DestinationAlpha : SharpDX.Direct3D11.BlendOption.DestinationColor;
  70. case Blend.InverseBlendFactor:
  71. return SharpDX.Direct3D11.BlendOption.InverseBlendFactor;
  72. case Blend.InverseDestinationAlpha:
  73. return SharpDX.Direct3D11.BlendOption.InverseDestinationAlpha;
  74. case Blend.InverseDestinationColor:
  75. return alpha ? SharpDX.Direct3D11.BlendOption.InverseDestinationAlpha : SharpDX.Direct3D11.BlendOption.InverseDestinationColor;
  76. case Blend.InverseSourceAlpha:
  77. return SharpDX.Direct3D11.BlendOption.InverseSourceAlpha;
  78. case Blend.InverseSourceColor:
  79. return alpha ? SharpDX.Direct3D11.BlendOption.InverseSourceAlpha : SharpDX.Direct3D11.BlendOption.InverseSourceColor;
  80. case Blend.One:
  81. return SharpDX.Direct3D11.BlendOption.One;
  82. case Blend.SourceAlpha:
  83. return SharpDX.Direct3D11.BlendOption.SourceAlpha;
  84. case Blend.SourceAlphaSaturation:
  85. return SharpDX.Direct3D11.BlendOption.SourceAlphaSaturate;
  86. case Blend.SourceColor:
  87. return alpha ? SharpDX.Direct3D11.BlendOption.SourceAlpha : SharpDX.Direct3D11.BlendOption.SourceColor;
  88. case Blend.Zero:
  89. return SharpDX.Direct3D11.BlendOption.Zero;
  90. default:
  91. throw new ArgumentException("Invalid blend!");
  92. }
  93. }
  94. static private SharpDX.Direct3D11.ColorWriteMaskFlags GetColorWriteMask(ColorWriteChannels mask)
  95. {
  96. return ((mask & ColorWriteChannels.Red) != 0 ? SharpDX.Direct3D11.ColorWriteMaskFlags.Red : 0) |
  97. ((mask & ColorWriteChannels.Green) != 0 ? SharpDX.Direct3D11.ColorWriteMaskFlags.Green : 0) |
  98. ((mask & ColorWriteChannels.Blue) != 0 ? SharpDX.Direct3D11.ColorWriteMaskFlags.Blue : 0) |
  99. ((mask & ColorWriteChannels.Alpha) != 0 ? SharpDX.Direct3D11.ColorWriteMaskFlags.Alpha : 0);
  100. }
  101. #endif
  102. }
  103. }