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

196 lines
7.0 KiB
C++
Raw Normal View History

2024-06-03 21:55:09 -03:00
#include "xna/graphics/blendstate.hpp"
#include "xna/graphics/gresource.hpp"
2024-07-13 22:55:36 -03:00
#include "xna/xna-dx.hpp"
2024-03-18 15:41:46 -03:00
namespace xna {
2024-06-22 22:05:35 -03:00
BlendState::BlendState() : BlendState(nullptr) {}
2024-05-19 21:22:34 -03:00
BlendState::BlendState(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
impl = unew<PlatformImplementation>();
2024-06-22 22:05:35 -03:00
impl->dxDescription.AlphaToCoverageEnable = false;
impl->dxDescription.IndependentBlendEnable = false;
impl->dxDescription.RenderTarget[0].BlendEnable = true;
2024-06-25 22:04:09 -03:00
impl->dxDescription.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(Blend::One);
impl->dxDescription.RenderTarget[0].DestBlend = DxHelpers::BlendToDx(Blend::One);
impl->dxDescription.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(BlendFunction::Add);
impl->dxDescription.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(Blend::One);
impl->dxDescription.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(Blend::One);
impl->dxDescription.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(BlendFunction::Add);
impl->dxDescription.RenderTarget[0].RenderTargetWriteMask = DxHelpers::ColorWriteChannelsToDx(ColorWriteChannels::All);
2024-06-22 22:05:35 -03:00
}
BlendFunction BlendState::AlphaBlendFunction() const {
2024-06-25 22:04:09 -03:00
return DxHelpers::BlendOperationToXna(impl->dxDescription.RenderTarget[0].BlendOpAlpha);
2024-06-22 22:05:35 -03:00
}
void BlendState::AlphaBlendFunction(BlendFunction value) {
2024-06-25 22:04:09 -03:00
impl->dxDescription.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(value);
2024-06-22 22:05:35 -03:00
}
Blend BlendState::AlphaDestinationBlend() const {
2024-06-25 22:04:09 -03:00
return DxHelpers::BlendToXna(impl->dxDescription.RenderTarget[0].DestBlendAlpha);
2024-06-22 22:05:35 -03:00
}
void BlendState::AlphaDestinationBlend(Blend value) {
2024-06-25 22:04:09 -03:00
impl->dxDescription.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(value);
2024-06-22 22:05:35 -03:00
}
Blend BlendState::AlphaSourceBlend() const {
2024-06-25 22:04:09 -03:00
return DxHelpers::BlendToXna(impl->dxDescription.RenderTarget[0].SrcBlendAlpha);
2024-06-22 22:05:35 -03:00
}
void BlendState::AlphaSourceBlend(Blend value) {
2024-06-25 22:04:09 -03:00
impl->dxDescription.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(value);
2024-06-22 22:05:35 -03:00
}
BlendFunction BlendState::ColorBlendFunction() const {
2024-06-25 22:04:09 -03:00
return DxHelpers::BlendOperationToXna(impl->dxDescription.RenderTarget[0].BlendOp);
2024-06-22 22:05:35 -03:00
}
void BlendState::ColorBlendFunction(BlendFunction value) {
2024-06-25 22:04:09 -03:00
impl->dxDescription.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(value);
2024-06-22 22:05:35 -03:00
}
Blend BlendState::ColorDestinationBlend() const {
2024-06-25 22:04:09 -03:00
return DxHelpers::BlendToXna(impl->dxDescription.RenderTarget[0].DestBlend);
2024-06-22 22:05:35 -03:00
}
void BlendState::ColorDestinationBlend(Blend value) {
2024-06-25 22:04:09 -03:00
impl->dxDescription.RenderTarget[0].DestBlend = DxHelpers::BlendToDx(value);
2024-05-19 21:22:34 -03:00
}
2024-06-22 22:05:35 -03:00
Blend BlendState::ColorSourceBlend() const {
2024-06-25 22:04:09 -03:00
return DxHelpers::BlendToXna(impl->dxDescription.RenderTarget[0].SrcBlend);
2024-06-22 22:05:35 -03:00
}
void BlendState::ColorSourceBlend(Blend value) {
2024-06-25 22:04:09 -03:00
impl->dxDescription.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(value);
2024-06-22 22:05:35 -03:00
}
Color BlendState::BlendFactor() const {
auto color = Color(
impl->blendFactor[0],
impl->blendFactor[1],
impl->blendFactor[2],
impl->blendFactor[3]
);
return color;
}
void BlendState::BlendFactor(Color const& value) {
auto v4 = value.ToVector4();
impl->blendFactor[0] = v4.X;
impl->blendFactor[1] = v4.Y;
impl->blendFactor[2] = v4.Z;
impl->blendFactor[3] = v4.W;
}
Int BlendState::MultiSampleMask() const {
return static_cast<Int>(impl->sampleMask);
}
void BlendState::MultiSampleMast(Int value) {
impl->sampleMask = static_cast<UINT>(value);
2024-05-19 21:22:34 -03:00
}
bool BlendState::Initialize()
2024-04-13 21:04:12 -03:00
{
2024-05-24 22:26:10 -03:00
if (!m_device || !m_device->impl->_device) {
2024-07-06 12:20:54 -03:00
Exception::Throw(Exception::UNABLE_TO_INITIALIZE);
2024-04-13 21:04:12 -03:00
}
2024-03-18 15:41:46 -03:00
2024-05-19 21:22:34 -03:00
if (impl->dxBlendState) {
impl->dxBlendState = nullptr;
}
2024-03-18 15:41:46 -03:00
2024-05-24 22:26:10 -03:00
const auto hr = m_device->impl->_device->CreateBlendState(
2024-05-19 21:22:34 -03:00
&impl->dxDescription,
2024-06-25 17:06:37 -03:00
impl->dxBlendState.GetAddressOf());
2024-03-18 15:41:46 -03:00
2024-04-13 21:04:12 -03:00
if (FAILED(hr)) {
2024-07-06 12:20:54 -03:00
Exception::Throw(Exception::FAILED_TO_CREATE);
2024-04-13 21:04:12 -03:00
}
return true;
}
bool BlendState::Apply() {
2024-05-24 22:26:10 -03:00
if (!m_device || !m_device->impl->_context) {
2024-07-06 12:20:54 -03:00
Exception::Throw(Exception::FAILED_TO_APPLY);
2024-04-13 21:04:12 -03:00
}
2024-05-19 21:22:34 -03:00
if (!impl->dxBlendState) {
2024-08-02 16:40:06 -03:00
Initialize();
2024-04-13 21:04:12 -03:00
}
2024-06-22 22:05:35 -03:00
m_device->impl->_context->OMSetBlendState(
2024-06-25 17:06:37 -03:00
impl->dxBlendState.Get(),
2024-06-22 22:05:35 -03:00
impl->blendFactor,
2024-05-19 21:22:34 -03:00
impl->sampleMask);
2024-03-18 15:41:46 -03:00
2024-06-22 22:05:35 -03:00
return true;
2024-03-18 15:41:46 -03:00
}
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-06-25 22:04:09 -03:00
impl->dxDescription.RenderTarget[i].SrcBlend = DxHelpers::BlendToDx(value[i].Source);
impl->dxDescription.RenderTarget[i].DestBlend = DxHelpers::BlendToDx(value[i].Destination);
impl->dxDescription.RenderTarget[i].BlendOp = DxHelpers::BlendOperationToDx(value[i].Operation);
impl->dxDescription.RenderTarget[i].SrcBlendAlpha = DxHelpers::BlendToDx(value[i].SourceAlpha);
impl->dxDescription.RenderTarget[i].DestBlendAlpha = DxHelpers::BlendToDx(value[i].DestinationAlpha);
impl->dxDescription.RenderTarget[i].BlendOpAlpha = DxHelpers::BlendOperationToDx(value[i].OperationAlpha);
impl->dxDescription.RenderTarget[i].RenderTargetWriteMask = DxHelpers::ColorWriteChannelsToDx(value[i].WriteMask);
2024-05-19 21:22:34 -03:00
}
}
2024-04-25 14:51:33 -03:00
uptr<BlendState> BlendState::Opaque() {
auto blendState = unew<BlendState>();
2024-05-19 21:22:34 -03:00
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
2024-06-22 22:05:35 -03:00
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO;
2024-05-19 21:22:34 -03:00
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() {
2024-06-22 22:12:43 -03:00
auto blendState = unew<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() {
2024-06-22 22:12:43 -03:00
auto blendState = unew<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() {
2024-06-22 22:12:43 -03:00
auto blendState = unew<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
}