3 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGL
5 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
6 using OpenTK.Graphics.ES30;
8 using OpenTK.Graphics.OpenGL;
11 namespace SiliconStudio.
Paradox.Graphics
13 public partial class BlendState
15 private readonly
bool blendEnable;
16 private readonly BlendEquationMode blendEquationModeColor;
17 private readonly BlendEquationMode blendEquationModeAlpha;
18 private readonly BlendingFactorSrc blendFactorSrcColor;
19 private readonly BlendingFactorSrc blendFactorSrcAlpha;
20 private readonly BlendingFactorDest blendFactorDestColor;
21 private readonly BlendingFactorDest blendFactorDestAlpha;
22 private readonly uint blendEquationHash;
23 private readonly uint blendFuncHash;
24 internal readonly
bool[] EnabledColors =
new bool[4];
26 internal BlendState(GraphicsDevice device, BlendStateDescription blendStateDescription) : base(device)
28 Description = blendStateDescription;
30 for (
int i = 1; i < Description.RenderTargets.Length; ++i)
32 if (Description.RenderTargets[i].BlendEnable || Description.RenderTargets[i].ColorWriteChannels !=
ColorWriteChannels.All)
33 throw new NotSupportedException();
36 blendEnable = Description.RenderTargets[0].BlendEnable;
38 blendEquationModeColor = ToOpenGL(Description.RenderTargets[0].ColorBlendFunction);
39 blendEquationModeAlpha = ToOpenGL(Description.RenderTargets[0].AlphaBlendFunction);
40 blendFactorSrcColor = ToOpenGL(Description.RenderTargets[0].ColorSourceBlend);
41 blendFactorSrcAlpha = ToOpenGL(Description.RenderTargets[0].AlphaSourceBlend);
42 blendFactorDestColor = (BlendingFactorDest)ToOpenGL(Description.RenderTargets[0].ColorDestinationBlend);
43 blendFactorDestAlpha = (BlendingFactorDest)ToOpenGL(Description.RenderTargets[0].AlphaDestinationBlend);
44 EnabledColors[0] = (Description.RenderTargets[0].ColorWriteChannels & ColorWriteChannels.Red ) != 0;
45 EnabledColors[1] = (Description.RenderTargets[0].ColorWriteChannels & ColorWriteChannels.Green) != 0;
46 EnabledColors[2] = (Description.RenderTargets[0].ColorWriteChannels & ColorWriteChannels.Blue ) != 0;
47 EnabledColors[3] = (Description.RenderTargets[0].ColorWriteChannels & ColorWriteChannels.Alpha) != 0;
49 blendEquationHash = (uint)Description.RenderTargets[0].ColorBlendFunction
50 | ((uint)Description.RenderTargets[0].AlphaBlendFunction << 8);
52 blendFuncHash = (uint)Description.RenderTargets[0].ColorSourceBlend
53 | ((uint)Description.RenderTargets[0].AlphaSourceBlend << 8)
54 | ((uint)Description.RenderTargets[0].ColorDestinationBlend << 16)
55 | ((uint)Description.RenderTargets[0].AlphaDestinationBlend << 24);
59 protected internal override bool OnRecreate()
65 public static BlendEquationMode ToOpenGL(
BlendFunction blendFunction)
67 switch (blendFunction)
69 case BlendFunction.Subtract:
70 return BlendEquationMode.FuncSubtract;
71 case BlendFunction.Add:
72 return BlendEquationMode.FuncAdd;
73 #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
74 case BlendFunction.Max:
75 return BlendEquationMode.Max;
76 case BlendFunction.Min:
77 return BlendEquationMode.Min;
79 case BlendFunction.ReverseSubtract:
80 return BlendEquationMode.FuncReverseSubtract;
82 throw new NotSupportedException();
86 public static BlendingFactorSrc ToOpenGL(
Blend blend)
91 return BlendingFactorSrc.Zero;
93 return BlendingFactorSrc.One;
94 case Blend.SourceColor:
95 return (BlendingFactorSrc)BlendingFactorDest.SrcColor;
96 case Blend.InverseSourceColor:
97 return (BlendingFactorSrc)BlendingFactorDest.OneMinusSrcColor;
98 case Blend.SourceAlpha:
99 return BlendingFactorSrc.SrcAlpha;
100 case Blend.InverseSourceAlpha:
101 return BlendingFactorSrc.OneMinusSrcAlpha;
102 case Blend.DestinationAlpha:
103 return BlendingFactorSrc.DstAlpha;
104 case Blend.InverseDestinationAlpha:
105 return BlendingFactorSrc.OneMinusDstAlpha;
106 case Blend.DestinationColor:
107 return BlendingFactorSrc.DstColor;
108 case Blend.InverseDestinationColor:
109 return BlendingFactorSrc.OneMinusDstColor;
110 case Blend.SourceAlphaSaturate:
111 return BlendingFactorSrc.SrcAlphaSaturate;
112 case Blend.BlendFactor:
113 return BlendingFactorSrc.ConstantColor;
114 case Blend.InverseBlendFactor:
115 return BlendingFactorSrc.OneMinusConstantColor;
116 case Blend.SecondarySourceColor:
117 case Blend.InverseSecondarySourceColor:
118 case Blend.SecondarySourceAlpha:
119 case Blend.InverseSecondarySourceAlpha:
120 throw new NotSupportedException();
122 throw new ArgumentOutOfRangeException(
"blend");
126 public void Apply(BlendState oldBlendState)
130 if (blendEnable && !oldBlendState.blendEnable)
133 if (blendEquationHash != oldBlendState.blendEquationHash)
134 GL.BlendEquationSeparate(blendEquationModeColor, blendEquationModeAlpha);
136 if (blendFuncHash != oldBlendState.blendFuncHash)
137 GL.BlendFuncSeparate(blendFactorSrcColor, blendFactorDestColor, blendFactorSrcAlpha, blendFactorDestAlpha);
139 if (Description.RenderTargets[0].ColorWriteChannels != oldBlendState.Description.RenderTargets[0].ColorWriteChannels)
142 if(!blendEnable && oldBlendState.blendEnable)
146 internal void ApplyColorMask()
148 bool hasRenderTarget = GraphicsDevice.hasRenderTarget;
149 GL.ColorMask(hasRenderTarget && EnabledColors[0], hasRenderTarget && EnabledColors[1], hasRenderTarget && EnabledColors[2], hasRenderTarget && EnabledColors[3]);
BlendFunction
RGB or alpha blending operation.
ColorWriteChannels
Identify which components of each pixel of a render target are writable during blending.
Blend
Blend option. A blend option identifies the data source and an optional pre-blend operation...