지금까지 혼합 연산자와 혼합 계수를 알아보았다. 응용 프로그램에서 이를 설정하려면 어떻게 해야 할까? 이와 같은 절차를 따르면 된다.
D3D11_BLEND_DESC 구조체는 다음과 같다.
typedef struct D3D11_BLEND_DESC
{
BOOL AlphaToCoverageEnable;
BOOL IndependentBlendEnable;
D3D11_RENDER_TARGET_BLEND_DESC RenderTarget[ 8 ];
} D3D11_BLEND_DESC;
위의 제3 매개변수의 자료형인 D3D11_RENDER_TARGET_BLEND_DESC 구조체의 정의는 다음과 같다.
typedef struct D3D11_RENDER_TARGET_BLEND_DESC
{
BOOL BlendEnable; // 기본값 : FALSE.
D3D11_BLEND SrcBlend; // 기본값 : D3D11_BLEND_ONE.
D3D11_BLEND DestBlend; // 기본값 : D3D11_BLEND_ZERO.
D3D11_BLEND_OP BlendOp; // 기본값 : D3D11_BLEND_OP_ADD.
D3D11_BLEND SrcBlendAlpha; // 기본값 : D3D11_BLEND_ONE.
D3D11_BLEND DestBlendAlpha; // 기본값 : D3D11_BLEND_ZERO.
D3D11_BLEND_OP BlendOpAlpha; // 기본값 : D3D11_BLEND_OP_ADD.
UINT8 RenderTargetWriteMask; // 기본값 : D3D11_COLOR_WRITE_ENABLE_ALL.
} D3D11_RENDER_TARGET_BLEND_DESC;
enum D3D11_COLOR_WRITE_ENABLE
{
D3D11_COLOR_WRITE_ENABLE_RED = 1,
D3D11_COLOR_WRITE_ENABLE_GREEN = 2,
D3D11_COLOR_WRITE_ENABLE_BLUE = 4,
D3D11_COLOR_WRITE_ENABLE_ALPHA = 8,
D3D11_COLOR_WRITE_ENABLE_ALL = ( ( ( D3D11_COLOR_WRITE_ENABLE_RED | D3D11_COLOR_WRITE_ENABLE_GREEN ) | D3D11_COLOR_WRITE_ENABLE_BLUE ) | D3D11_COLOR_WRITE_ENABLE_ALPHA )
} D3D11_COLOR_WRITE_ENABLE;
이 플래그들은 혼합의 결과를 후면 버퍼의 어떤 색상 채널들에 기록할 것인지를 결정한다. 예를 들어 D3D11_COLOR_WRITE_ENABLE_ALPHA를 지정하면 결과가 RGB 채널들에는 기록되지 않고 알파 채널에만 기록된다. 비활성화한 경우에는 픽셀 셰이더가 돌려준 색상에 그 어떤 쓰기 마스크도 적용되지 않는다. 고급 기술이다.
D3D11_BLEND_DESC를 지정하였으면, ID3D11Device::CreateBlendState 메서드를 호출한다.
HRESULT ID3D11Device::CreateBlendState(
const D3D11_BLEND_DESC *pBlendStateDesc,
ID3D11BlendState **ppBlendState);
이를 통해 ID3D11BlendState 객체를 생성하였으면, 출력 병합기 단계에 묶어야 한다.
void ID3D11DeviceContext::OMSetBlendState(
ID3D11BlendState *pBlendState,
const FLOAT BlendFactor,
UINT SampleMask);
만약 제1 매개변수에 널 값을 지정해서 호출하면 기본 혼합 상태가 적용된다.
다음은 혼합 설정 예이다.
D3D11_BLEND_DESC desc;
ZeroMemory(&desc, sizeof(D3D11_BLEND_DESC));
D3D11_COLOR_WRITE_ENABLE
desc.AlphaToCoverageEnable = false;
desc.RenderTarget[0].BlendEnable = true;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
HRESULT hr = md3dDevice->CreateBlendState(&desc, &blendState);
assert(SUCCEEDED(hr));
md3dImmediateContext->OMSetBlendState(blendState, nullptr, 0xffffffff);