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) {
|
2024-11-13 09:38:31 -03:00
|
|
|
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 {
|
2024-11-13 09:38:31 -03:00
|
|
|
return DxHelpers::BlendOperationToXna(Implementation->Description.RenderTarget[0].BlendOpAlpha);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlendState::AlphaBlendFunction(BlendFunction value) {
|
2024-11-13 09:38:31 -03:00
|
|
|
Implementation->Description.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(value);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Blend BlendState::AlphaDestinationBlend() const {
|
2024-11-13 09:38:31 -03:00
|
|
|
return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].DestBlendAlpha);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlendState::AlphaDestinationBlend(Blend value) {
|
2024-11-13 09:38:31 -03:00
|
|
|
Implementation->Description.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(value);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Blend BlendState::AlphaSourceBlend() const {
|
2024-11-13 09:38:31 -03:00
|
|
|
return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].SrcBlendAlpha);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlendState::AlphaSourceBlend(Blend value) {
|
2024-11-13 09:38:31 -03:00
|
|
|
Implementation->Description.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(value);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BlendFunction BlendState::ColorBlendFunction() const {
|
2024-11-13 09:38:31 -03:00
|
|
|
return DxHelpers::BlendOperationToXna(Implementation->Description.RenderTarget[0].BlendOp);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlendState::ColorBlendFunction(BlendFunction value) {
|
2024-11-13 09:38:31 -03:00
|
|
|
Implementation->Description.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(value);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Blend BlendState::ColorDestinationBlend() const {
|
2024-11-13 09:38:31 -03:00
|
|
|
return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].DestBlend);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlendState::ColorDestinationBlend(Blend value) {
|
2024-11-13 09:38:31 -03:00
|
|
|
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 {
|
2024-11-13 09:38:31 -03:00
|
|
|
return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].SrcBlend);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlendState::ColorSourceBlend(Blend value) {
|
2024-11-13 09:38:31 -03:00
|
|
|
Implementation->Description.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(value);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Color BlendState::BlendFactor() const {
|
|
|
|
auto color = Color(
|
2024-11-13 09:38:31 -03:00
|
|
|
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();
|
|
|
|
|
2024-11-13 09:38:31 -03:00
|
|
|
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 {
|
2024-11-13 09:38:31 -03:00
|
|
|
return static_cast<Int>(Implementation->SampleMask);
|
2024-06-22 22:05:35 -03:00
|
|
|
}
|
|
|
|
|
2024-11-13 10:25:22 -03:00
|
|
|
void BlendState::MultiSampleMask(Int value) {
|
2024-11-13 09:38:31 -03:00
|
|
|
Implementation->SampleMask = static_cast<UINT>(value);
|
2024-05-19 21:22:34 -03:00
|
|
|
}
|
|
|
|
|
2024-06-22 11:02:01 -03:00
|
|
|
bool BlendState::Initialize()
|
2024-04-13 21:04:12 -03:00
|
|
|
{
|
2024-11-14 21:44:46 -03:00
|
|
|
if (!BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Device) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-04-13 21:04:12 -03:00
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
2024-11-13 09:38:31 -03:00
|
|
|
if (Implementation->BlendState) {
|
|
|
|
Implementation->BlendState = nullptr;
|
2024-03-24 18:37:55 -03:00
|
|
|
}
|
2024-03-18 15:41:46 -03:00
|
|
|
|
2024-11-14 21:44:46 -03:00
|
|
|
const auto hr = BaseGraphicsDevice->Implementation->Device->CreateBlendState(
|
2024-11-13 09:38:31 -03:00
|
|
|
&Implementation->Description,
|
|
|
|
Implementation->BlendState.GetAddressOf());
|
2024-03-18 15:41:46 -03:00
|
|
|
|
2024-04-13 21:04:12 -03:00
|
|
|
if (FAILED(hr)) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-04-13 21:04:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-06-22 11:02:01 -03:00
|
|
|
bool BlendState::Apply() {
|
2024-11-14 21:44:46 -03:00
|
|
|
if (!BaseGraphicsDevice || !BaseGraphicsDevice->Implementation->Context) {
|
2024-12-02 19:02:51 -03:00
|
|
|
throw csharp::InvalidOperationException();
|
2024-04-13 21:04:12 -03:00
|
|
|
}
|
|
|
|
|
2024-11-13 09:38:31 -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
|
|
|
|
2024-11-14 21:44:46 -03:00
|
|
|
BaseGraphicsDevice->Implementation->Context->OMSetBlendState(
|
2024-11-13 09:38:31 -03:00
|
|
|
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) {
|
2024-11-13 09:38:31 -03:00
|
|
|
Implementation->Description.AlphaToCoverageEnable = value;
|
2024-05-19 21:22:34 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlendState::IndependentBlendEnable(bool value) {
|
2024-11-13 09:38:31 -03:00
|
|
|
Implementation->Description.IndependentBlendEnable = value;
|
2024-05-19 21:22:34 -03:00
|
|
|
}
|
2024-11-13 10:32:40 -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) {
|
2024-11-13 09:38:31 -03:00
|
|
|
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() {
|
2024-06-22 11:52:21 -03:00
|
|
|
auto blendState = unew<BlendState>();
|
2024-11-13 09:38:31 -03:00
|
|
|
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>();
|
2024-11-13 09:38:31 -03:00
|
|
|
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>();
|
2024-11-13 09:38:31 -03:00
|
|
|
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>();
|
2024-11-13 09:38:31 -03:00
|
|
|
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
|
|
|
}
|