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

196 lines
7.6 KiB
C++
Raw Permalink Normal View History

2024-06-03 21:55:09 -03:00
#include "xna/graphics/blendstate.hpp"
#include "xna/graphics/gresource.hpp"
2024-09-06 22:23:32 -03:00
#include "xna-dx/framework.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) {
Implementation = unew<BlendStateImplementation>();
Implementation->Description.AlphaToCoverageEnable = false;
Implementation->Description.IndependentBlendEnable = false;
Implementation->Description.RenderTarget[0].BlendEnable = true;
Implementation->Description.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(Blend::One);
Implementation->Description.RenderTarget[0].DestBlend = DxHelpers::BlendToDx(Blend::One);
Implementation->Description.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(BlendFunction::Add);
Implementation->Description.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(Blend::One);
Implementation->Description.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(Blend::One);
Implementation->Description.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(BlendFunction::Add);
Implementation->Description.RenderTarget[0].RenderTargetWriteMask = DxHelpers::ColorWriteChannelsToDx(ColorWriteChannels::All);
2024-06-22 22:05:35 -03:00
}
BlendFunction BlendState::AlphaBlendFunction() const {
return DxHelpers::BlendOperationToXna(Implementation->Description.RenderTarget[0].BlendOpAlpha);
2024-06-22 22:05:35 -03:00
}
void BlendState::AlphaBlendFunction(BlendFunction value) {
Implementation->Description.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(value);
2024-06-22 22:05:35 -03:00
}
Blend BlendState::AlphaDestinationBlend() const {
return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].DestBlendAlpha);
2024-06-22 22:05:35 -03:00
}
void BlendState::AlphaDestinationBlend(Blend value) {
Implementation->Description.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(value);
2024-06-22 22:05:35 -03:00
}
Blend BlendState::AlphaSourceBlend() const {
return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].SrcBlendAlpha);
2024-06-22 22:05:35 -03:00
}
void BlendState::AlphaSourceBlend(Blend value) {
Implementation->Description.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(value);
2024-06-22 22:05:35 -03:00
}
BlendFunction BlendState::ColorBlendFunction() const {
return DxHelpers::BlendOperationToXna(Implementation->Description.RenderTarget[0].BlendOp);
2024-06-22 22:05:35 -03:00
}
void BlendState::ColorBlendFunction(BlendFunction value) {
Implementation->Description.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(value);
2024-06-22 22:05:35 -03:00
}
Blend BlendState::ColorDestinationBlend() const {
return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].DestBlend);
2024-06-22 22:05:35 -03:00
}
void BlendState::ColorDestinationBlend(Blend value) {
Implementation->Description.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 {
return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].SrcBlend);
2024-06-22 22:05:35 -03:00
}
void BlendState::ColorSourceBlend(Blend value) {
Implementation->Description.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(value);
2024-06-22 22:05:35 -03:00
}
Color BlendState::BlendFactor() const {
auto color = Color(
Implementation->BlendFactor[0],
Implementation->BlendFactor[1],
Implementation->BlendFactor[2],
Implementation->BlendFactor[3]
2024-06-22 22:05:35 -03:00
);
return color;
}
void BlendState::BlendFactor(Color const& value) {
auto v4 = value.ToVector4();
Implementation->BlendFactor[0] = v4.X;
Implementation->BlendFactor[1] = v4.Y;
Implementation->BlendFactor[2] = v4.Z;
Implementation->BlendFactor[3] = v4.W;
2024-06-22 22:05:35 -03:00
}
Int BlendState::MultiSampleMask() const {
return static_cast<Int>(Implementation->SampleMask);
2024-06-22 22:05:35 -03:00
}
void BlendState::MultiSampleMask(Int value) {
Implementation->SampleMask = static_cast<UINT>(value);
2024-05-19 21:22:34 -03:00
}
bool BlendState::Initialize()
2024-04-13 21:04:12 -03:00
{
if (!BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) {
throw csharp::InvalidOperationException();
2024-04-13 21:04:12 -03:00
}
2024-03-18 15:41:46 -03:00
if (Implementation->BlendState) {
Implementation->BlendState = nullptr;
}
2024-03-18 15:41:46 -03:00
const auto hr = BaseGraphicsDevice->Implementation->Device->CreateBlendState(
&Implementation->Description,
Implementation->BlendState.GetAddressOf());
2024-03-18 15:41:46 -03:00
2024-04-13 21:04:12 -03:00
if (FAILED(hr)) {
throw csharp::InvalidOperationException();
2024-04-13 21:04:12 -03:00
}
return true;
}
bool BlendState::Apply() {
if (!BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Context) {
throw csharp::InvalidOperationException();
2024-04-13 21:04:12 -03:00
}
if (!Implementation->BlendState) {
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
BaseGraphicsDevice->Implementation->Context->OMSetBlendState(
Implementation->BlendState.Get(),
Implementation->BlendFactor,
Implementation->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) {
Implementation->Description.AlphaToCoverageEnable = value;
2024-05-19 21:22:34 -03:00
}
void BlendState::IndependentBlendEnable(bool value) {
Implementation->Description.IndependentBlendEnable = value;
2024-05-19 21:22:34 -03:00
}
void BlendState::RenderTargets(BlendRenderTarget const* value, size_t size) {
2024-11-13 14:23:27 -03:00
for (size_t i = 0; i < size && i < Implementation->MAX_RENDER_TARGETS; ++i) {
Implementation->Description.RenderTarget[i].BlendEnable = value[i].Enabled;
Implementation->Description.RenderTarget[i].SrcBlend = DxHelpers::BlendToDx(value[i].Source);
Implementation->Description.RenderTarget[i].DestBlend = DxHelpers::BlendToDx(value[i].Destination);
Implementation->Description.RenderTarget[i].BlendOp = DxHelpers::BlendOperationToDx(value[i].Operation);
Implementation->Description.RenderTarget[i].SrcBlendAlpha = DxHelpers::BlendToDx(value[i].SourceAlpha);
Implementation->Description.RenderTarget[i].DestBlendAlpha = DxHelpers::BlendToDx(value[i].DestinationAlpha);
Implementation->Description.RenderTarget[i].BlendOpAlpha = DxHelpers::BlendOperationToDx(value[i].OperationAlpha);
Implementation->Description.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>();
blendState->Implementation->Description.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
blendState->Implementation->Description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendState->Implementation->Description.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO;
blendState->Implementation->Description.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>();
blendState->Implementation->Description.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
blendState->Implementation->Description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendState->Implementation->Description.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendState->Implementation->Description.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>();
blendState->Implementation->Description.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendState->Implementation->Description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendState->Implementation->Description.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
blendState->Implementation->Description.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>();
blendState->Implementation->Description.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendState->Implementation->Description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendState->Implementation->Description.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendState->Implementation->Description.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
}