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

58 lines
2.4 KiB
Markdown

---
title: "Color Blend State"
date: 2021-01-27T14:11:42-08:00
weight: 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](https://learnopengl.com/Advanced-OpenGL/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.
```cs
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](https://www.khronos.org/registry/vulkan/specs/1.2/html/chap28.html#framebuffer-logicop).
`BlendConstants` are used by the blend factors to produce result colors. You can read about that [here](https://www.khronos.org/registry/vulkan/specs/1.2/html/chap28.html#framebuffer-blendconstants).
Let's put it all together:
```cs
var myBlendConstants = new BlendConstants
{
R = 1f,
G = 1f,
B = 1f,
A = 1f
};
var myColorBlendState = new ColorBlendState
{
LogicOpEnable = false,
BlendConstants = myBlendConstants,
ColorTargetBlendStates = myColorTargetBlendStates
};
```