1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00
xn65/framework/platform/blendstate-dx.cpp

125 lines
4.4 KiB
C++
Raw Normal View History

2024-05-19 21:22:34 -03:00
#include "graphics/blendstate.hpp"
#include "graphics/gresource.hpp"
#include "platform-dx/device-dx.hpp"
2024-05-19 21:22:34 -03:00
#include "platform-dx/dxheaders.hpp"
2024-05-20 10:41:40 -03:00
#include "platform-dx/dxhelpers.hpp"
2024-05-19 21:22:34 -03:00
#include "graphics/blendstate.hpp"
#include "platform-dx/implementations.hpp"
2024-03-18 15:41:46 -03:00
namespace xna {
2024-05-19 21:22:34 -03:00
BlendState::BlendState() : GraphicsResource(nullptr) {
impl = uNew<PlatformImplementation>();
}
BlendState::BlendState(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
impl = uNew<PlatformImplementation>();
}
BlendState::~BlendState() {
impl = nullptr;
}
2024-04-25 14:51:33 -03:00
bool BlendState::Initialize(xna_error_ptr_arg)
2024-04-13 21:04:12 -03:00
{
2024-04-25 14:51:33 -03:00
if (!m_device || !m_device->_device) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
2024-04-13 21:04:12 -03:00
return false;
}
2024-03-18 15:41:46 -03:00
2024-05-19 21:22:34 -03:00
if (impl->dxBlendState) {
impl->dxBlendState->Release();
impl->dxBlendState = nullptr;
}
2024-03-18 15:41:46 -03:00
2024-05-19 21:22:34 -03:00
const auto hr = m_device->_device->CreateBlendState(
&impl->dxDescription,
&impl->dxBlendState);
2024-03-18 15:41:46 -03:00
2024-04-13 21:04:12 -03:00
if (FAILED(hr)) {
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
return false;
}
return true;
}
2024-04-25 14:51:33 -03:00
bool BlendState::Apply(xna_error_ptr_arg) {
if (!m_device || !m_device->_context) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
2024-04-13 21:04:12 -03:00
return false;
}
2024-05-19 21:22:34 -03:00
if (!impl->dxBlendState) {
2024-04-25 14:51:33 -03:00
xna_error_apply(err, XnaErrorCode::UNINTIALIZED_RESOURCE);
return false;
2024-04-13 21:04:12 -03:00
}
2024-04-24 11:44:41 -03:00
2024-05-19 21:22:34 -03:00
m_device->_context->OMSetBlendState(
impl->dxBlendState,
impl->blendFactor,
impl->sampleMask);
2024-03-18 15:41:46 -03:00
return true;
}
2024-03-21 16:01:47 -03:00
2024-05-19 21:22:34 -03:00
void BlendState::AlphaToCoverageEnable(bool value) {
impl->dxDescription.AlphaToCoverageEnable = value;
}
void BlendState::IndependentBlendEnable(bool value) {
impl->dxDescription.IndependentBlendEnable = value;
}
void BlendState::RenderTargets(std::vector<BlendRenderTarget> const& value) {
for (size_t i = 0; i < value.size() && i < 8; ++i) {
impl->dxDescription.RenderTarget[i].BlendEnable = value[i].Enabled;
2024-05-20 10:41:40 -03:00
impl->dxDescription.RenderTarget[i].SrcBlend = DxHelpers::ConvertBlend(value[i].Source);
impl->dxDescription.RenderTarget[i].DestBlend = DxHelpers::ConvertBlend(value[i].Destination);
impl->dxDescription.RenderTarget[i].BlendOp = DxHelpers::ConvertOperation(value[i].Operation);
impl->dxDescription.RenderTarget[i].SrcBlendAlpha = DxHelpers::ConvertBlend(value[i].SourceAlpha);
impl->dxDescription.RenderTarget[i].DestBlendAlpha = DxHelpers::ConvertBlend(value[i].DestinationAlpha);
impl->dxDescription.RenderTarget[i].BlendOpAlpha = DxHelpers::ConvertOperation(value[i].OperationAlpha);
impl->dxDescription.RenderTarget[i].RenderTargetWriteMask = DxHelpers::ConvertColorWrite(value[i].WriteMask);
2024-05-19 21:22:34 -03:00
}
}
2024-04-25 14:51:33 -03:00
uptr<BlendState> BlendState::Opaque() {
2024-05-19 21:22:34 -03:00
auto blendState = uNew<BlendState>();
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_DEST_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
2024-03-21 16:01:47 -03:00
return blendState;
}
2024-04-25 14:51:33 -03:00
uptr<BlendState> BlendState::AlphaBlend() {
auto blendState = std::unique_ptr<BlendState>(new BlendState());
2024-05-19 21:22:34 -03:00
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
2024-03-21 16:01:47 -03:00
return blendState;
}
2024-04-25 14:51:33 -03:00
uptr<BlendState> BlendState::Additive() {
auto blendState = std::unique_ptr<BlendState>(new BlendState());
2024-05-19 21:22:34 -03:00
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
2024-03-21 16:01:47 -03:00
return blendState;
}
2024-04-25 14:51:33 -03:00
uptr<BlendState> BlendState::NonPremultiplied() {
auto blendState = std::unique_ptr<BlendState>(new BlendState());
2024-05-19 21:22:34 -03:00
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
2024-03-21 16:01:47 -03:00
return blendState;
}
2024-03-18 15:41:46 -03:00
}