diff --git a/includes/xna-dx/implementations.hpp b/includes/xna-dx/implementations.hpp index 33ed481..ef82b23 100644 --- a/includes/xna-dx/implementations.hpp +++ b/includes/xna-dx/implementations.hpp @@ -4,7 +4,7 @@ #include "headers.hpp" namespace xna { - struct GraphicsAdapter::ImplementationBase::PlatformImplementation { + struct GraphicsAdapterImpl { comptr Adapter() const { return adapter; } @@ -20,16 +20,6 @@ namespace xna { comptr factory; }; - struct SpriteFont::PlatformImplementation { - uptr dxSpriteFont{ nullptr }; - }; - - struct SpriteBatch::PlatformImplementation { - sptr dxSpriteBatch = nullptr; - comptr dxInputLayout = nullptr; - sptr dxEffectBuffer = nullptr; - }; - struct BlendRenderTarget { bool Enabled{ true }; Blend Source{ Blend::SourceAlpha }; @@ -43,13 +33,25 @@ namespace xna { constexpr BlendRenderTarget() = default; }; - struct BlendState::PlatformImplementation { - comptr dxBlendState = nullptr; - D3D11_BLEND_DESC dxDescription{}; - float blendFactor[4]{ 1.0F, 1.0F, 1.0F, 1.0F }; - UINT sampleMask{ 0xffffffff }; + struct BlendStateImplementation { + friend class BlendState; + + D3D11_BLEND_DESC Description{}; + float BlendFactor[4]{ 1.0F, 1.0F, 1.0F, 1.0F }; + UINT SampleMask{ 0xffffffff }; + comptr BlendState; }; + struct SpriteFont::PlatformImplementation { + uptr dxSpriteFont{ nullptr }; + }; + + struct SpriteBatch::PlatformImplementation { + sptr dxSpriteBatch = nullptr; + comptr dxInputLayout = nullptr; + sptr dxEffectBuffer = nullptr; + }; + struct DepthStencilState::PlatformImplementation { comptr dxDepthStencil = nullptr; D3D11_DEPTH_STENCIL_DESC dxDescription{}; diff --git a/includes/xna/graphics/adapter.hpp b/includes/xna/graphics/adapter.hpp index 9bfa1af..77ef9e5 100644 --- a/includes/xna/graphics/adapter.hpp +++ b/includes/xna/graphics/adapter.hpp @@ -1,15 +1,17 @@ #ifndef XNA_GRAPHICS_ADAPTER_HPP #define XNA_GRAPHICS_ADAPTER_HPP -#include "displaymode.hpp" #include "../platform.hpp" -#include +#include "displaymode.hpp" #include +#include #include namespace xna { + + struct GraphicsAdapterImpl; //Provides methods to retrieve and manipulate graphics adapters. - class GraphicsAdapter : public ImplementationBase { + class GraphicsAdapter : public ImplementationBase { public: //Collection of available adapters on the system. static void Adapters(std::vector>& adapters); diff --git a/includes/xna/graphics/blendstate.hpp b/includes/xna/graphics/blendstate.hpp index ba65f71..e3c6d0b 100644 --- a/includes/xna/graphics/blendstate.hpp +++ b/includes/xna/graphics/blendstate.hpp @@ -4,12 +4,14 @@ #include "../common/color.hpp" #include "../default.hpp" #include "gresource.hpp" +#include "../platform.hpp" namespace xna { struct BlendRenderTarget; + struct BlendStateImplementation; //Contains blend state for the device. - class BlendState : public GraphicsResource { + class BlendState : public GraphicsResource, public ImplementationBase { public: BlendState(); BlendState(sptr const& device); @@ -70,11 +72,7 @@ namespace xna { static uptr Additive(); //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. - static uptr NonPremultiplied(); - - public: - struct PlatformImplementation; - uptr impl = nullptr; + static uptr NonPremultiplied(); }; using PBlendState = sptr; diff --git a/includes/xna/platform.hpp b/includes/xna/platform.hpp index 8d2943e..e41e3f2 100644 --- a/includes/xna/platform.hpp +++ b/includes/xna/platform.hpp @@ -4,9 +4,10 @@ #include namespace xna { - struct ImplementationBase { - struct PlatformImplementation; - std::unique_ptr Implementation; + template struct ImplementationBase { + virtual ~ImplementationBase() {} + + std::unique_ptr Implementation; }; } diff --git a/sources/framework-dx/adapter.cpp b/sources/framework-dx/adapter.cpp index f7625c9..d24df7d 100644 --- a/sources/framework-dx/adapter.cpp +++ b/sources/framework-dx/adapter.cpp @@ -8,7 +8,7 @@ namespace xna { static sptr getSupportedDisplayModes(comptr& dxAdapter); GraphicsAdapter::GraphicsAdapter() { - Implementation = unew(); + Implementation = unew(); } uptr GraphicsAdapter::DefaultAdapter() { diff --git a/sources/framework-dx/blendstate.cpp b/sources/framework-dx/blendstate.cpp index 30e38cb..3e98284 100644 --- a/sources/framework-dx/blendstate.cpp +++ b/sources/framework-dx/blendstate.cpp @@ -6,73 +6,73 @@ namespace xna { BlendState::BlendState() : BlendState(nullptr) {} BlendState::BlendState(sptr const& device) : GraphicsResource(device) { - impl = unew(); - impl->dxDescription.AlphaToCoverageEnable = false; - impl->dxDescription.IndependentBlendEnable = false; - impl->dxDescription.RenderTarget[0].BlendEnable = true; - 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); + Implementation = unew(); + 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); } 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) { - impl->dxDescription.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(value); + Implementation->Description.RenderTarget[0].BlendOpAlpha = DxHelpers::BlendOperationToDx(value); } 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) { - impl->dxDescription.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(value); + Implementation->Description.RenderTarget[0].DestBlendAlpha = DxHelpers::BlendToDx(value); } 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) { - impl->dxDescription.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(value); + Implementation->Description.RenderTarget[0].SrcBlendAlpha = DxHelpers::BlendToDx(value); } 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) { - impl->dxDescription.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(value); + Implementation->Description.RenderTarget[0].BlendOp = DxHelpers::BlendOperationToDx(value); } 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) { - impl->dxDescription.RenderTarget[0].DestBlend = DxHelpers::BlendToDx(value); + Implementation->Description.RenderTarget[0].DestBlend = DxHelpers::BlendToDx(value); } 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) { - impl->dxDescription.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(value); + Implementation->Description.RenderTarget[0].SrcBlend = DxHelpers::BlendToDx(value); } Color BlendState::BlendFactor() const { auto color = Color( - impl->blendFactor[0], - impl->blendFactor[1], - impl->blendFactor[2], - impl->blendFactor[3] + Implementation->BlendFactor[0], + Implementation->BlendFactor[1], + Implementation->BlendFactor[2], + Implementation->BlendFactor[3] ); return color; @@ -81,18 +81,18 @@ namespace xna { 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; + Implementation->BlendFactor[0] = v4.X; + Implementation->BlendFactor[1] = v4.Y; + Implementation->BlendFactor[2] = v4.Z; + Implementation->BlendFactor[3] = v4.W; } Int BlendState::MultiSampleMask() const { - return static_cast(impl->sampleMask); + return static_cast(Implementation->SampleMask); } void BlendState::MultiSampleMast(Int value) { - impl->sampleMask = static_cast(value); + Implementation->SampleMask = static_cast(value); } bool BlendState::Initialize() @@ -101,13 +101,13 @@ namespace xna { Exception::Throw(Exception::UNABLE_TO_INITIALIZE); } - if (impl->dxBlendState) { - impl->dxBlendState = nullptr; + if (Implementation->BlendState) { + Implementation->BlendState = nullptr; } const auto hr = m_device->impl->_device->CreateBlendState( - &impl->dxDescription, - impl->dxBlendState.GetAddressOf()); + &Implementation->Description, + Implementation->BlendState.GetAddressOf()); if (FAILED(hr)) { Exception::Throw(Exception::FAILED_TO_CREATE); @@ -121,75 +121,75 @@ namespace xna { Exception::Throw(Exception::FAILED_TO_APPLY); } - if (!impl->dxBlendState) { + if (!Implementation->BlendState) { Initialize(); } m_device->impl->_context->OMSetBlendState( - impl->dxBlendState.Get(), - impl->blendFactor, - impl->sampleMask); + Implementation->BlendState.Get(), + Implementation->BlendFactor, + Implementation->SampleMask); return true; } void BlendState::AlphaToCoverageEnable(bool value) { - impl->dxDescription.AlphaToCoverageEnable = value; + Implementation->Description.AlphaToCoverageEnable = value; } void BlendState::IndependentBlendEnable(bool value) { - impl->dxDescription.IndependentBlendEnable = value; + Implementation->Description.IndependentBlendEnable = value; } void BlendState::RenderTargets(std::vector const& value) { for (size_t i = 0; i < value.size() && i < 8; ++i) { - impl->dxDescription.RenderTarget[i].BlendEnable = value[i].Enabled; - 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); + 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); } } uptr BlendState::Opaque() { auto blendState = unew(); - 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_ZERO; - blendState->impl->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; + 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; return blendState; } uptr BlendState::AlphaBlend() { auto blendState = unew(); - 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; + 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; return blendState; } uptr BlendState::Additive() { auto blendState = unew(); - 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; + 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; return blendState; } uptr BlendState::NonPremultiplied() { auto blendState = unew(); - 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; + 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; return blendState; } diff --git a/sources/framework-dx/sprite.cpp b/sources/framework-dx/sprite.cpp index 6bc352d..6639941 100644 --- a/sources/framework-dx/sprite.cpp +++ b/sources/framework-dx/sprite.cpp @@ -159,7 +159,7 @@ namespace xna { impl->dxSpriteBatch->Begin( _sortMode, - blendState ? blendState->impl->dxBlendState.Get() : nullptr, + blendState ? blendState->Implementation->BlendState.Get() : nullptr, samplerState ? samplerState->impl->_samplerState.Get() : nullptr, depthStencil ? depthStencil->impl->dxDepthStencil.Get() : nullptr, rasterizerState ? rasterizerState->impl->dxRasterizerState.Get() : nullptr,