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

Adiciona ImplementationBase em BlendState

This commit is contained in:
Danilo 2024-11-13 09:38:31 -03:00
parent 234a957484
commit 077caf7a05
7 changed files with 100 additions and 97 deletions

View File

@ -4,7 +4,7 @@
#include "headers.hpp" #include "headers.hpp"
namespace xna { namespace xna {
struct GraphicsAdapter::ImplementationBase::PlatformImplementation { struct GraphicsAdapterImpl {
comptr<IDXGIAdapter1> Adapter() const { comptr<IDXGIAdapter1> Adapter() const {
return adapter; return adapter;
} }
@ -20,16 +20,6 @@ namespace xna {
comptr<IDXGIFactory1> factory; comptr<IDXGIFactory1> factory;
}; };
struct SpriteFont::PlatformImplementation {
uptr<DirectX::SpriteFont> dxSpriteFont{ nullptr };
};
struct SpriteBatch::PlatformImplementation {
sptr<DirectX::SpriteBatch> dxSpriteBatch = nullptr;
comptr<ID3D11InputLayout> dxInputLayout = nullptr;
sptr<DirectX::DX11::IEffect> dxEffectBuffer = nullptr;
};
struct BlendRenderTarget { struct BlendRenderTarget {
bool Enabled{ true }; bool Enabled{ true };
Blend Source{ Blend::SourceAlpha }; Blend Source{ Blend::SourceAlpha };
@ -43,13 +33,25 @@ namespace xna {
constexpr BlendRenderTarget() = default; constexpr BlendRenderTarget() = default;
}; };
struct BlendState::PlatformImplementation { struct BlendStateImplementation {
comptr<ID3D11BlendState> dxBlendState = nullptr; friend class BlendState;
D3D11_BLEND_DESC dxDescription{};
float blendFactor[4]{ 1.0F, 1.0F, 1.0F, 1.0F }; D3D11_BLEND_DESC Description{};
UINT sampleMask{ 0xffffffff }; float BlendFactor[4]{ 1.0F, 1.0F, 1.0F, 1.0F };
UINT SampleMask{ 0xffffffff };
comptr<ID3D11BlendState> BlendState;
}; };
struct SpriteFont::PlatformImplementation {
uptr<DirectX::SpriteFont> dxSpriteFont{ nullptr };
};
struct SpriteBatch::PlatformImplementation {
sptr<DirectX::SpriteBatch> dxSpriteBatch = nullptr;
comptr<ID3D11InputLayout> dxInputLayout = nullptr;
sptr<DirectX::DX11::IEffect> dxEffectBuffer = nullptr;
};
struct DepthStencilState::PlatformImplementation { struct DepthStencilState::PlatformImplementation {
comptr<ID3D11DepthStencilState> dxDepthStencil = nullptr; comptr<ID3D11DepthStencilState> dxDepthStencil = nullptr;
D3D11_DEPTH_STENCIL_DESC dxDescription{}; D3D11_DEPTH_STENCIL_DESC dxDescription{};

View File

@ -1,15 +1,17 @@
#ifndef XNA_GRAPHICS_ADAPTER_HPP #ifndef XNA_GRAPHICS_ADAPTER_HPP
#define XNA_GRAPHICS_ADAPTER_HPP #define XNA_GRAPHICS_ADAPTER_HPP
#include "displaymode.hpp"
#include "../platform.hpp" #include "../platform.hpp"
#include <memory> #include "displaymode.hpp"
#include <cstdint> #include <cstdint>
#include <memory>
#include <string> #include <string>
namespace xna { namespace xna {
struct GraphicsAdapterImpl;
//Provides methods to retrieve and manipulate graphics adapters. //Provides methods to retrieve and manipulate graphics adapters.
class GraphicsAdapter : public ImplementationBase { class GraphicsAdapter : public ImplementationBase<GraphicsAdapterImpl> {
public: public:
//Collection of available adapters on the system. //Collection of available adapters on the system.
static void Adapters(std::vector<std::unique_ptr<GraphicsAdapter>>& adapters); static void Adapters(std::vector<std::unique_ptr<GraphicsAdapter>>& adapters);

View File

@ -4,12 +4,14 @@
#include "../common/color.hpp" #include "../common/color.hpp"
#include "../default.hpp" #include "../default.hpp"
#include "gresource.hpp" #include "gresource.hpp"
#include "../platform.hpp"
namespace xna { namespace xna {
struct BlendRenderTarget; struct BlendRenderTarget;
struct BlendStateImplementation;
//Contains blend state for the device. //Contains blend state for the device.
class BlendState : public GraphicsResource { class BlendState : public GraphicsResource, public ImplementationBase<BlendStateImplementation> {
public: public:
BlendState(); BlendState();
BlendState(sptr<GraphicsDevice> const& device); BlendState(sptr<GraphicsDevice> const& device);
@ -70,11 +72,7 @@ namespace xna {
static uptr<BlendState> Additive(); static uptr<BlendState> Additive();
//A built-in state object with settings for blending with non-premultipled alpha, //A built-in state object with settings for blending with non-premultipled alpha,
//that is blending source and destination data using alpha while assuming the color data contains no alpha information. //that is blending source and destination data using alpha while assuming the color data contains no alpha information.
static uptr<BlendState> NonPremultiplied(); static uptr<BlendState> NonPremultiplied();
public:
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
}; };
using PBlendState = sptr<BlendState>; using PBlendState = sptr<BlendState>;

View File

@ -4,9 +4,10 @@
#include <memory> #include <memory>
namespace xna { namespace xna {
struct ImplementationBase { template <typename T> struct ImplementationBase {
struct PlatformImplementation; virtual ~ImplementationBase() {}
std::unique_ptr<PlatformImplementation> Implementation;
std::unique_ptr<T> Implementation;
}; };
} }

View File

@ -8,7 +8,7 @@ namespace xna {
static sptr<DisplayModeCollection> getSupportedDisplayModes(comptr<IDXGIAdapter1>& dxAdapter); static sptr<DisplayModeCollection> getSupportedDisplayModes(comptr<IDXGIAdapter1>& dxAdapter);
GraphicsAdapter::GraphicsAdapter() { GraphicsAdapter::GraphicsAdapter() {
Implementation = unew<PlatformImplementation>(); Implementation = unew<GraphicsAdapterImpl>();
} }
uptr<GraphicsAdapter> GraphicsAdapter::DefaultAdapter() { uptr<GraphicsAdapter> GraphicsAdapter::DefaultAdapter() {

View File

@ -6,73 +6,73 @@ namespace xna {
BlendState::BlendState() : BlendState(nullptr) {} BlendState::BlendState() : BlendState(nullptr) {}
BlendState::BlendState(sptr<GraphicsDevice> const& device) : GraphicsResource(device) { BlendState::BlendState(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
impl = unew<PlatformImplementation>(); Implementation = unew<BlendStateImplementation>();
impl->dxDescription.AlphaToCoverageEnable = false; Implementation->Description.AlphaToCoverageEnable = false;
impl->dxDescription.IndependentBlendEnable = false; Implementation->Description.IndependentBlendEnable = false;
impl->dxDescription.RenderTarget[0].BlendEnable = true; Implementation->Description.RenderTarget[0].BlendEnable = true;
impl->dxDescription.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(Blend::One); Implementation->Description.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(Blend::One);
impl->dxDescription.RenderTarget[0].DestBlend = DxHelpers::BlendToDx(Blend::One); Implementation->Description.RenderTarget[0].DestBlend = DxHelpers::BlendToDx(Blend::One);
impl->dxDescription.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(BlendFunction::Add); Implementation->Description.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(BlendFunction::Add);
impl->dxDescription.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(Blend::One); Implementation->Description.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(Blend::One);
impl->dxDescription.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(Blend::One); Implementation->Description.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(Blend::One);
impl->dxDescription.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(BlendFunction::Add); Implementation->Description.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(BlendFunction::Add);
impl->dxDescription.RenderTarget[0].RenderTargetWriteMask = DxHelpers::ColorWriteChannelsToDx(ColorWriteChannels::All); Implementation->Description.RenderTarget[0].RenderTargetWriteMask = DxHelpers::ColorWriteChannelsToDx(ColorWriteChannels::All);
} }
BlendFunction BlendState::AlphaBlendFunction() const { BlendFunction BlendState::AlphaBlendFunction() const {
return DxHelpers::BlendOperationToXna(impl->dxDescription.RenderTarget[0].BlendOpAlpha); return DxHelpers::BlendOperationToXna(Implementation->Description.RenderTarget[0].BlendOpAlpha);
} }
void BlendState::AlphaBlendFunction(BlendFunction value) { void BlendState::AlphaBlendFunction(BlendFunction value) {
impl->dxDescription.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(value); Implementation->Description.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(value);
} }
Blend BlendState::AlphaDestinationBlend() const { Blend BlendState::AlphaDestinationBlend() const {
return DxHelpers::BlendToXna(impl->dxDescription.RenderTarget[0].DestBlendAlpha); return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].DestBlendAlpha);
} }
void BlendState::AlphaDestinationBlend(Blend value) { void BlendState::AlphaDestinationBlend(Blend value) {
impl->dxDescription.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(value); Implementation->Description.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(value);
} }
Blend BlendState::AlphaSourceBlend() const { Blend BlendState::AlphaSourceBlend() const {
return DxHelpers::BlendToXna(impl->dxDescription.RenderTarget[0].SrcBlendAlpha); return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].SrcBlendAlpha);
} }
void BlendState::AlphaSourceBlend(Blend value) { void BlendState::AlphaSourceBlend(Blend value) {
impl->dxDescription.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(value); Implementation->Description.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(value);
} }
BlendFunction BlendState::ColorBlendFunction() const { BlendFunction BlendState::ColorBlendFunction() const {
return DxHelpers::BlendOperationToXna(impl->dxDescription.RenderTarget[0].BlendOp); return DxHelpers::BlendOperationToXna(Implementation->Description.RenderTarget[0].BlendOp);
} }
void BlendState::ColorBlendFunction(BlendFunction value) { void BlendState::ColorBlendFunction(BlendFunction value) {
impl->dxDescription.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(value); Implementation->Description.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(value);
} }
Blend BlendState::ColorDestinationBlend() const { Blend BlendState::ColorDestinationBlend() const {
return DxHelpers::BlendToXna(impl->dxDescription.RenderTarget[0].DestBlend); return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].DestBlend);
} }
void BlendState::ColorDestinationBlend(Blend value) { void BlendState::ColorDestinationBlend(Blend value) {
impl->dxDescription.RenderTarget[0].DestBlend = DxHelpers::BlendToDx(value); Implementation->Description.RenderTarget[0].DestBlend = DxHelpers::BlendToDx(value);
} }
Blend BlendState::ColorSourceBlend() const { Blend BlendState::ColorSourceBlend() const {
return DxHelpers::BlendToXna(impl->dxDescription.RenderTarget[0].SrcBlend); return DxHelpers::BlendToXna(Implementation->Description.RenderTarget[0].SrcBlend);
} }
void BlendState::ColorSourceBlend(Blend value) { void BlendState::ColorSourceBlend(Blend value) {
impl->dxDescription.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(value); Implementation->Description.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(value);
} }
Color BlendState::BlendFactor() const { Color BlendState::BlendFactor() const {
auto color = Color( auto color = Color(
impl->blendFactor[0], Implementation->BlendFactor[0],
impl->blendFactor[1], Implementation->BlendFactor[1],
impl->blendFactor[2], Implementation->BlendFactor[2],
impl->blendFactor[3] Implementation->BlendFactor[3]
); );
return color; return color;
@ -81,18 +81,18 @@ namespace xna {
void BlendState::BlendFactor(Color const& value) { void BlendState::BlendFactor(Color const& value) {
auto v4 = value.ToVector4(); auto v4 = value.ToVector4();
impl->blendFactor[0] = v4.X; Implementation->BlendFactor[0] = v4.X;
impl->blendFactor[1] = v4.Y; Implementation->BlendFactor[1] = v4.Y;
impl->blendFactor[2] = v4.Z; Implementation->BlendFactor[2] = v4.Z;
impl->blendFactor[3] = v4.W; Implementation->BlendFactor[3] = v4.W;
} }
Int BlendState::MultiSampleMask() const { Int BlendState::MultiSampleMask() const {
return static_cast<Int>(impl->sampleMask); return static_cast<Int>(Implementation->SampleMask);
} }
void BlendState::MultiSampleMast(Int value) { void BlendState::MultiSampleMast(Int value) {
impl->sampleMask = static_cast<UINT>(value); Implementation->SampleMask = static_cast<UINT>(value);
} }
bool BlendState::Initialize() bool BlendState::Initialize()
@ -101,13 +101,13 @@ namespace xna {
Exception::Throw(Exception::UNABLE_TO_INITIALIZE); Exception::Throw(Exception::UNABLE_TO_INITIALIZE);
} }
if (impl->dxBlendState) { if (Implementation->BlendState) {
impl->dxBlendState = nullptr; Implementation->BlendState = nullptr;
} }
const auto hr = m_device->impl->_device->CreateBlendState( const auto hr = m_device->impl->_device->CreateBlendState(
&impl->dxDescription, &Implementation->Description,
impl->dxBlendState.GetAddressOf()); Implementation->BlendState.GetAddressOf());
if (FAILED(hr)) { if (FAILED(hr)) {
Exception::Throw(Exception::FAILED_TO_CREATE); Exception::Throw(Exception::FAILED_TO_CREATE);
@ -121,75 +121,75 @@ namespace xna {
Exception::Throw(Exception::FAILED_TO_APPLY); Exception::Throw(Exception::FAILED_TO_APPLY);
} }
if (!impl->dxBlendState) { if (!Implementation->BlendState) {
Initialize(); Initialize();
} }
m_device->impl->_context->OMSetBlendState( m_device->impl->_context->OMSetBlendState(
impl->dxBlendState.Get(), Implementation->BlendState.Get(),
impl->blendFactor, Implementation->BlendFactor,
impl->sampleMask); Implementation->SampleMask);
return true; return true;
} }
void BlendState::AlphaToCoverageEnable(bool value) { void BlendState::AlphaToCoverageEnable(bool value) {
impl->dxDescription.AlphaToCoverageEnable = value; Implementation->Description.AlphaToCoverageEnable = value;
} }
void BlendState::IndependentBlendEnable(bool value) { void BlendState::IndependentBlendEnable(bool value) {
impl->dxDescription.IndependentBlendEnable = value; Implementation->Description.IndependentBlendEnable = value;
} }
void BlendState::RenderTargets(std::vector<BlendRenderTarget> const& value) { void BlendState::RenderTargets(std::vector<BlendRenderTarget> const& value) {
for (size_t i = 0; i < value.size() && i < 8; ++i) { for (size_t i = 0; i < value.size() && i < 8; ++i) {
impl->dxDescription.RenderTarget[i].BlendEnable = value[i].Enabled; Implementation->Description.RenderTarget[i].BlendEnable = value[i].Enabled;
impl->dxDescription.RenderTarget[i].SrcBlend = DxHelpers::BlendToDx(value[i].Source); Implementation->Description.RenderTarget[i].SrcBlend = DxHelpers::BlendToDx(value[i].Source);
impl->dxDescription.RenderTarget[i].DestBlend = DxHelpers::BlendToDx(value[i].Destination); Implementation->Description.RenderTarget[i].DestBlend = DxHelpers::BlendToDx(value[i].Destination);
impl->dxDescription.RenderTarget[i].BlendOp = DxHelpers::BlendOperationToDx(value[i].Operation); Implementation->Description.RenderTarget[i].BlendOp = DxHelpers::BlendOperationToDx(value[i].Operation);
impl->dxDescription.RenderTarget[i].SrcBlendAlpha = DxHelpers::BlendToDx(value[i].SourceAlpha); Implementation->Description.RenderTarget[i].SrcBlendAlpha = DxHelpers::BlendToDx(value[i].SourceAlpha);
impl->dxDescription.RenderTarget[i].DestBlendAlpha = DxHelpers::BlendToDx(value[i].DestinationAlpha); Implementation->Description.RenderTarget[i].DestBlendAlpha = DxHelpers::BlendToDx(value[i].DestinationAlpha);
impl->dxDescription.RenderTarget[i].BlendOpAlpha = DxHelpers::BlendOperationToDx(value[i].OperationAlpha); Implementation->Description.RenderTarget[i].BlendOpAlpha = DxHelpers::BlendOperationToDx(value[i].OperationAlpha);
impl->dxDescription.RenderTarget[i].RenderTargetWriteMask = DxHelpers::ColorWriteChannelsToDx(value[i].WriteMask); Implementation->Description.RenderTarget[i].RenderTargetWriteMask = DxHelpers::ColorWriteChannelsToDx(value[i].WriteMask);
} }
} }
uptr<BlendState> BlendState::Opaque() { uptr<BlendState> BlendState::Opaque() {
auto blendState = unew<BlendState>(); auto blendState = unew<BlendState>();
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; blendState->Implementation->Description.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; blendState->Implementation->Description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO; blendState->Implementation->Description.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO;
blendState->impl->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; blendState->Implementation->Description.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
return blendState; return blendState;
} }
uptr<BlendState> BlendState::AlphaBlend() { uptr<BlendState> BlendState::AlphaBlend() {
auto blendState = unew<BlendState>(); auto blendState = unew<BlendState>();
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; blendState->Implementation->Description.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; blendState->Implementation->Description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; blendState->Implementation->Description.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; blendState->Implementation->Description.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
return blendState; return blendState;
} }
uptr<BlendState> BlendState::Additive() { uptr<BlendState> BlendState::Additive() {
auto blendState = unew<BlendState>(); auto blendState = unew<BlendState>();
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; blendState->Implementation->Description.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; blendState->Implementation->Description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_ONE; blendState->Implementation->Description.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
blendState->impl->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE; blendState->Implementation->Description.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
return blendState; return blendState;
} }
uptr<BlendState> BlendState::NonPremultiplied() { uptr<BlendState> BlendState::NonPremultiplied() {
auto blendState = unew<BlendState>(); auto blendState = unew<BlendState>();
blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; blendState->Implementation->Description.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; blendState->Implementation->Description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; blendState->Implementation->Description.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendState->impl->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; blendState->Implementation->Description.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
return blendState; return blendState;
} }

View File

@ -159,7 +159,7 @@ namespace xna {
impl->dxSpriteBatch->Begin( impl->dxSpriteBatch->Begin(
_sortMode, _sortMode,
blendState ? blendState->impl->dxBlendState.Get() : nullptr, blendState ? blendState->Implementation->BlendState.Get() : nullptr,
samplerState ? samplerState->impl->_samplerState.Get() : nullptr, samplerState ? samplerState->impl->_samplerState.Get() : nullptr,
depthStencil ? depthStencil->impl->dxDepthStencil.Get() : nullptr, depthStencil ? depthStencil->impl->dxDepthStencil.Get() : nullptr,
rasterizerState ? rasterizerState->impl->dxRasterizerState.Get() : nullptr, rasterizerState ? rasterizerState->impl->dxRasterizerState.Get() : nullptr,