MoonWorks-docs/content/Graphics/Resources/GraphicsPipeline/ColorBlendState.md

2.4 KiB

title date weight
Color Blend State 2021-01-27T14:11:42-08:00 1

Color blend state is comprised of four fields - a logical operation, whether that logical operation is enabled, some blending constants, and a collection of color target blend states. Let's talk about the target states first.

Color Target Blend State

You might remember our old friend RenderTarget. A color target is just a render target in a color format. Our color target always has color information stored in it - the blend mode tells the renderer how to combine new pixel values with the original values to produce a new color.

BlendEnable is a bool. If we turn off blending entirely, the new pixel color will just overwrite any existing values. Simple!

ColorWriteMask tells the renderer which color channels it is allowed to write to.

Next we have AlphaBlendOp and ColorBlendOp, which are BlendOp values, and the rest of the fields are BlendFactor values. Rather than spend a bunch of time explaining the nuances of various blend modes, I will simply link you to a very thorough explanation of graphics blending.

Here is an example blend mode, which does something we call "additive blending". This kind of blending is very useful for lighting and particle effects.

var myColorTargetBlendState = new ColorTargetBlendState
{
    BlendEnable = true,
    ColorWriteMask = ColorComponentFlags.RGBA,
    AlphaBlendOp = BlendOp.Add,
    ColorBlendOp = BlendOp.Add,
    SourceColorBlendFactor = BlendFactor.SourceAlpha,
    SourceAlphaBlendFactor = BlendFactor.SourceAlpha,
    DestinationColorBlendFactor = BlendFactor.One,
    DestinationAlphaBlendFactor = BlendFactor.One
};

Next we have LogicOp. This lets you do bitwise combination of the original and new color. You can read about that in more detail over here.

BlendConstants are used by the blend factors to produce result colors. You can read about that here.

Let's put it all together:

var myBlendConstants = new BlendConstants
{
    R = 1f,
    G = 1f,
    B = 1f,
    A = 1f
};

var myColorBlendState = new ColorBlendState
{
    LogicOpEnable = false,
    BlendConstants = myBlendConstants,
    ColorTargetBlendStates = myColorTargetBlendStates
};