2025-07-10 13:29:03 +03:30
|
|
|
#include <renderer/dx/blender.hpp>
|
|
|
|
#include <renderer/dx/shared_context.hpp>
|
2021-07-09 11:56:31 +04:30
|
|
|
|
|
|
|
namespace Light {
|
|
|
|
|
2022-03-08 21:19:19 +03:30
|
|
|
dxBlender::dxBlender(Ref<dxSharedContext> sharedContext)
|
2025-07-05 14:23:01 +03:30
|
|
|
: m_context(sharedContext), m_factor_map { // constants
|
2022-03-08 21:19:19 +03:30
|
|
|
{ BlendFactor::ZERO, D3D11_BLEND_ZERO },
|
|
|
|
{ BlendFactor::ONE, D3D11_BLEND_ONE },
|
|
|
|
|
|
|
|
// source
|
|
|
|
{ BlendFactor::SRC_COLOR, D3D11_BLEND_SRC_COLOR },
|
|
|
|
{ BlendFactor::INVERSE_SRC_COLOR, D3D11_BLEND_INV_SRC_COLOR },
|
|
|
|
|
|
|
|
{ BlendFactor::SRC_ALPHA, D3D11_BLEND_SRC_ALPHA },
|
|
|
|
{ BlendFactor::INVERSE_SRC_ALPHA, D3D11_BLEND_INV_SRC_ALPHA },
|
|
|
|
|
|
|
|
// destination
|
|
|
|
{ BlendFactor::DST_COLOR, D3D11_BLEND_DEST_COLOR },
|
|
|
|
{ BlendFactor::INVERSE_DST_COLOR, D3D11_BLEND_INV_DEST_COLOR },
|
|
|
|
|
|
|
|
{ BlendFactor::DST_ALPHA, D3D11_BLEND_DEST_ALPHA },
|
|
|
|
{ BlendFactor::INVERSE_DST_ALPHA, D3D11_BLEND_INV_DEST_ALPHA },
|
|
|
|
|
|
|
|
// source1
|
|
|
|
{ BlendFactor::SRC1_COLOR, D3D11_BLEND_SRC1_COLOR },
|
|
|
|
{ BlendFactor::INVERSE_SRC1_COLOR, D3D11_BLEND_INV_SRC1_COLOR },
|
|
|
|
|
|
|
|
{ BlendFactor::SRC1_ALPHA, D3D11_BLEND_SRC1_ALPHA },
|
|
|
|
{ BlendFactor::INVERSE_SRC1_ALPHA, D3D11_BLEND_INV_SRC1_ALPHA }
|
|
|
|
}
|
2025-07-05 14:23:01 +03:30
|
|
|
, m_blend_state(nullptr)
|
|
|
|
, m_desc {}
|
2022-03-08 21:19:19 +03:30
|
|
|
{
|
|
|
|
// factor map
|
|
|
|
// blender desc
|
2025-07-05 14:23:01 +03:30
|
|
|
m_desc = {};
|
2022-03-08 21:19:19 +03:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
m_desc.RenderTarget[0].BlendEnable = true;
|
|
|
|
m_desc.RenderTarget[0].SrcBlend = D3D11_BLEND_ZERO;
|
|
|
|
m_desc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO;
|
|
|
|
m_desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
|
|
|
|
m_desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO;
|
|
|
|
m_desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
|
|
|
|
m_desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
|
|
|
|
m_desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
|
2022-03-08 21:19:19 +03:30
|
|
|
|
|
|
|
// create blend state
|
2025-07-06 14:02:50 +03:30
|
|
|
auto hr = HRESULT {};
|
2025-07-05 15:36:53 +03:30
|
|
|
dxc(m_context->get_device()->CreateBlendState(&m_desc, &m_blend_state));
|
2022-03-08 21:19:19 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxBlender::enable(BlendFactor srcFactor, BlendFactor dstFactor)
|
2022-03-08 21:19:19 +03:30
|
|
|
{
|
|
|
|
// update desc
|
2025-07-05 14:23:01 +03:30
|
|
|
m_desc.RenderTarget[0].BlendEnable = true;
|
|
|
|
m_desc.RenderTarget[0].SrcBlend = m_factor_map.at(srcFactor);
|
|
|
|
m_desc.RenderTarget[0].DestBlend = m_factor_map.at(dstFactor);
|
2022-03-08 21:19:19 +03:30
|
|
|
|
|
|
|
// re-create blind state
|
2025-07-06 14:02:50 +03:30
|
|
|
auto hr = HRESULT {};
|
2025-07-05 15:36:53 +03:30
|
|
|
dxc(m_context->get_device()->CreateBlendState(&m_desc, &m_blend_state));
|
2022-03-08 21:19:19 +03:30
|
|
|
|
|
|
|
// bind blend state
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->OMSetBlendState(m_blend_state.Get(), nullptr, 0x0000000f);
|
2022-03-08 21:19:19 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void dxBlender::disable()
|
2022-03-08 21:19:19 +03:30
|
|
|
{
|
|
|
|
// update desc
|
2025-07-05 14:23:01 +03:30
|
|
|
m_desc.RenderTarget[0].BlendEnable = false;
|
2022-03-08 21:19:19 +03:30
|
|
|
|
|
|
|
// re-create blind state
|
2025-07-06 14:02:50 +03:30
|
|
|
auto hr = HRESULT {};
|
2025-07-05 15:36:53 +03:30
|
|
|
dxc(m_context->get_device()->CreateBlendState(&m_desc, &m_blend_state));
|
2022-03-08 21:19:19 +03:30
|
|
|
|
|
|
|
// bind blend state
|
2025-07-05 15:36:53 +03:30
|
|
|
m_context->get_device_context()->OMSetBlendState(m_blend_state.Get(), nullptr, 0xffffffff);
|
2022-03-08 21:19:19 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
} // namespace Light
|