generated from Jonathan-Greve/GuildWarsMapBrowser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlendStateManager.h
104 lines (93 loc) · 3.96 KB
/
BlendStateManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#pragma once
enum class BlendState
{
Opaque,
AlphaBlend,
Additive,
Multiplicative,
Screen,
Subtractive
};
class BlendStateManager
{
public:
BlendStateManager(ID3D11Device* device, ID3D11DeviceContext* device_context)
: m_device(device)
, m_deviceContext(device_context)
{
CreateBlendState(device, D3D11_BLEND_ONE, D3D11_BLEND_ZERO, &m_opaque_blend_state); // Opaque
CreateBlendState(device, D3D11_BLEND_SRC_ALPHA, D3D11_BLEND_INV_SRC_ALPHA,
&m_alpha_blend_state); // Alpha Blend
CreateBlendState(device, D3D11_BLEND_ONE, D3D11_BLEND_ONE, &m_additive_blend_state); // Additive
CreateBlendState(device, D3D11_BLEND_ZERO, D3D11_BLEND_SRC_COLOR,
&m_multiplicative_blend_state); // Multiplicative
CreateBlendState(device, D3D11_BLEND_ONE, D3D11_BLEND_INV_SRC_COLOR, &m_screen_blend_state); // Screen
CreateBlendState(device, D3D11_BLEND_ONE, D3D11_BLEND_ONE, &m_subtractive_blend_state,
D3D11_BLEND_OP_REV_SUBTRACT); // Subtractive
}
ID3D11BlendState* GetBlendState(BlendState blendState)
{
switch (blendState)
{
case BlendState::Opaque:
return m_opaque_blend_state.Get();
case BlendState::AlphaBlend:
return m_alpha_blend_state.Get();
case BlendState::Additive:
return m_additive_blend_state.Get();
case BlendState::Multiplicative:
return m_multiplicative_blend_state.Get();
case BlendState::Screen:
return m_screen_blend_state.Get();
case BlendState::Subtractive:
return m_subtractive_blend_state.Get();
default:
return nullptr;
}
}
void SetBlendState(BlendState blendState, const float blendFactor[] = nullptr,
UINT sampleMask = 0xFFFFFFFF)
{
ID3D11BlendState* blendStatePtr = GetBlendState(blendState);
if (blendFactor == nullptr)
{
static float defaultBlendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f};
m_deviceContext->OMSetBlendState(blendStatePtr, defaultBlendFactor, sampleMask);
}
else
{
m_deviceContext->OMSetBlendState(blendStatePtr, blendFactor, sampleMask);
}
}
void ResetBlendState()
{
static float defaultBlendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f};
m_deviceContext->OMSetBlendState(nullptr, defaultBlendFactor, 0xFFFFFFFF);
}
private:
HRESULT CreateBlendState(ID3D11Device* device, D3D11_BLEND srcBlend, D3D11_BLEND destBlend,
Microsoft::WRL::ComPtr<ID3D11BlendState>* blendState,
D3D11_BLEND_OP blendOp = D3D11_BLEND_OP_ADD)
{
D3D11_BLEND_DESC blendDesc;
ZeroMemory(&blendDesc, sizeof(blendDesc));
blendDesc.RenderTarget[0].BlendEnable =
(srcBlend != D3D11_BLEND_ONE) || (destBlend != D3D11_BLEND_ZERO);
blendDesc.RenderTarget[0].SrcBlend = srcBlend;
blendDesc.RenderTarget[0].DestBlend = destBlend;
blendDesc.RenderTarget[0].BlendOp = blendOp;
blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
return device->CreateBlendState(&blendDesc, blendState->GetAddressOf());
}
ID3D11Device* m_device;
ID3D11DeviceContext* m_deviceContext;
Microsoft::WRL::ComPtr<ID3D11BlendState> m_opaque_blend_state;
Microsoft::WRL::ComPtr<ID3D11BlendState> m_alpha_blend_state;
Microsoft::WRL::ComPtr<ID3D11BlendState> m_additive_blend_state;
Microsoft::WRL::ComPtr<ID3D11BlendState> m_multiplicative_blend_state;
Microsoft::WRL::ComPtr<ID3D11BlendState> m_screen_blend_state;
Microsoft::WRL::ComPtr<ID3D11BlendState> m_subtractive_blend_state;
};