diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index c3a3030..8dcd0b0 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -11,10 +11,6 @@ namespace xna { impl = unew(); } - GraphicsAdapter::~GraphicsAdapter() { - impl = nullptr; - } - uptr GraphicsAdapter::DefaultAdapter() { IDXGIFactory1* pFactory = nullptr; @@ -38,28 +34,6 @@ namespace xna { return nullptr; } - void GraphicsAdapter::Adapters(std::vector>& adapters){ - IDXGIFactory1* pFactory = nullptr; - - if FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory)) - Exception::Throw(ExMessage::CreateComponent); - - IDXGIAdapter1* pAdapter = nullptr; - UINT count = 0; - - for (; pFactory->EnumAdapters1(count, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++count) { - auto adp = snew(); - - adp->impl->_index = count; - adp->impl->dxadapter = pAdapter; - - adapters.push_back(adp); - } - - pFactory->Release(); - pFactory = nullptr; - } - void GraphicsAdapter::Adapters(std::vector>& adapters) { IDXGIFactory1* pFactory = nullptr; @@ -180,7 +154,7 @@ namespace xna { if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) { const auto currentSurface = static_cast(f); - DXGI_FORMAT format = DxHelpers::ConvertSurfaceToDXGIFORMAT(currentSurface); + DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(currentSurface); UINT numModes = 0; pOutput->GetDisplayModeList(format, 0, &numModes, nullptr); @@ -211,7 +185,7 @@ namespace xna { UINT bufferOffset = 0; if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { - DXGI_FORMAT format = DxHelpers::ConvertSurfaceToDXGIFORMAT(surfaceFormat); + DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(surfaceFormat); UINT numModes = 0; @@ -273,7 +247,7 @@ namespace xna { if (adapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { for (size_t f = 0; f < SURFACE_FORMAT_COUNT; ++f) { const auto currentSurface = static_cast(f); - DXGI_FORMAT format = DxHelpers::ConvertSurfaceToDXGIFORMAT(currentSurface); + DXGI_FORMAT format = DxHelpers::SurfaceFormatToDx(currentSurface); UINT num = 0; pOutput->GetDisplayModeList(format, 0, &num, nullptr); diff --git a/framework/platform-dx/blendstate.cpp b/framework/platform-dx/blendstate.cpp index 2dd4544..acbd994 100644 --- a/framework/platform-dx/blendstate.cpp +++ b/framework/platform-dx/blendstate.cpp @@ -3,16 +3,96 @@ #include "xna/platform-dx/dx.hpp" namespace xna { - BlendState::BlendState() : GraphicsResource(nullptr) { - impl = unew(); - } + 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::ConvertBlend(Blend::One); + impl->dxDescription.RenderTarget[0].DestBlend = DxHelpers::ConvertBlend(Blend::One); + impl->dxDescription.RenderTarget[0].BlendOp = DxHelpers::ConvertOperation(BlendFunction::Add); + impl->dxDescription.RenderTarget[0].SrcBlendAlpha = DxHelpers::ConvertBlend(Blend::One); + impl->dxDescription.RenderTarget[0].DestBlendAlpha = DxHelpers::ConvertBlend(Blend::One); + impl->dxDescription.RenderTarget[0].BlendOpAlpha = DxHelpers::ConvertOperation(BlendFunction::Add); + impl->dxDescription.RenderTarget[0].RenderTargetWriteMask = DxHelpers::ConvertColorWrite(ColorWriteChannels::All); } - BlendState::~BlendState() { - impl = nullptr; + BlendFunction BlendState::AlphaBlendFunction() const { + return DxHelpers::ConvertOperationDx(impl->dxDescription.RenderTarget[0].BlendOpAlpha); + } + + void BlendState::AlphaBlendFunction(BlendFunction value) { + impl->dxDescription.RenderTarget[0].BlendOpAlpha = DxHelpers::ConvertOperation(value); + } + + Blend BlendState::AlphaDestinationBlend() const { + return DxHelpers::ConvertBlendDx(impl->dxDescription.RenderTarget[0].DestBlendAlpha); + } + + void BlendState::AlphaDestinationBlend(Blend value) { + impl->dxDescription.RenderTarget[0].DestBlendAlpha = DxHelpers::ConvertBlend(value); + } + + Blend BlendState::AlphaSourceBlend() const { + return DxHelpers::ConvertBlendDx(impl->dxDescription.RenderTarget[0].SrcBlendAlpha); + } + + void BlendState::AlphaSourceBlend(Blend value) { + impl->dxDescription.RenderTarget[0].SrcBlendAlpha = DxHelpers::ConvertBlend(value); + } + + BlendFunction BlendState::ColorBlendFunction() const { + return DxHelpers::ConvertOperationDx(impl->dxDescription.RenderTarget[0].BlendOp); + } + + void BlendState::ColorBlendFunction(BlendFunction value) { + impl->dxDescription.RenderTarget[0].BlendOp = DxHelpers::ConvertOperation(value); + } + + Blend BlendState::ColorDestinationBlend() const { + return DxHelpers::ConvertBlendDx(impl->dxDescription.RenderTarget[0].DestBlend); + } + + void BlendState::ColorDestinationBlend(Blend value) { + impl->dxDescription.RenderTarget[0].DestBlend = DxHelpers::ConvertBlend(value); + } + + Blend BlendState::ColorSourceBlend() const { + return DxHelpers::ConvertBlendDx(impl->dxDescription.RenderTarget[0].SrcBlend); + } + + void BlendState::ColorSourceBlend(Blend value) { + impl->dxDescription.RenderTarget[0].SrcBlend = DxHelpers::ConvertBlend(value); + } + + 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(impl->sampleMask); + } + + void BlendState::MultiSampleMast(Int value) { + impl->sampleMask = static_cast(value); } bool BlendState::Initialize() @@ -45,13 +125,13 @@ namespace xna { if (!impl->dxBlendState) { Exception::Throw(ExMessage::UnintializedComponent); } - - m_device->impl->_context->OMSetBlendState( - impl->dxBlendState, - impl->blendFactor, + + m_device->impl->_context->OMSetBlendState( + impl->dxBlendState, + impl->blendFactor, impl->sampleMask); - return true; + return true; } void BlendState::AlphaToCoverageEnable(bool value) { @@ -78,15 +158,15 @@ namespace xna { uptr BlendState::Opaque() { auto blendState = unew(); blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; - blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO; - blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_DEST_ALPHA; + 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; return blendState; } uptr BlendState::AlphaBlend() { - auto blendState = std::unique_ptr(new BlendState()); + auto blendState = std::unique_ptr(); 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; @@ -96,7 +176,7 @@ namespace xna { } uptr BlendState::Additive() { - auto blendState = std::unique_ptr(new BlendState()); + auto blendState = std::unique_ptr(); 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; @@ -106,7 +186,7 @@ namespace xna { } uptr BlendState::NonPremultiplied() { - auto blendState = std::unique_ptr(new BlendState()); + auto blendState = std::unique_ptr(); 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; diff --git a/framework/platform-dx/sprite.cpp b/framework/platform-dx/sprite.cpp index 15776be..95276ab 100644 --- a/framework/platform-dx/sprite.cpp +++ b/framework/platform-dx/sprite.cpp @@ -142,7 +142,7 @@ namespace xna { return; DxSpriteSortMode sort; - DxHelpers::ConvertSpriteSort(sortMode, sort); + DxHelpers::SpriteSortToDx(sortMode, sort); const auto& t = transformMatrix; DxMatrix matrix = DxMatrix( diff --git a/framework/platform-dx/swapchain.cpp b/framework/platform-dx/swapchain.cpp index a131568..82469a1 100644 --- a/framework/platform-dx/swapchain.cpp +++ b/framework/platform-dx/swapchain.cpp @@ -59,7 +59,7 @@ namespace xna { impl->dxDescription.Width = static_cast(parameters->BackBufferWidth); impl->dxDescription.Height = static_cast(parameters->BackBufferHeight); - impl->dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(parameters->BackBufferFormat); + impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(parameters->BackBufferFormat); impl->dxDescription.SampleDesc.Count = 1; impl->dxDescription.SampleDesc.Quality = 0; impl->dxDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; diff --git a/framework/platform-dx/texture.cpp b/framework/platform-dx/texture.cpp index 4925138..d4b9c29 100644 --- a/framework/platform-dx/texture.cpp +++ b/framework/platform-dx/texture.cpp @@ -122,7 +122,7 @@ namespace xna { impl->dxDescription.Width = static_cast(width); impl->dxDescription.Height = static_cast(height); impl->dxDescription.MipLevels = static_cast(mipMap); - impl->dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(format); + impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(format); } HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data) diff --git a/inc/xna/enums.hpp b/inc/xna/enums.hpp index 9a4568b..8417701 100644 --- a/inc/xna/enums.hpp +++ b/inc/xna/enums.hpp @@ -119,7 +119,8 @@ namespace xna { Green, Blue, Alpha, - All + All, + None }; enum class ContainmentType { diff --git a/inc/xna/graphics/adapter.hpp b/inc/xna/graphics/adapter.hpp index a70c446..a68fc84 100644 --- a/inc/xna/graphics/adapter.hpp +++ b/inc/xna/graphics/adapter.hpp @@ -4,26 +4,41 @@ #include "../default.hpp" namespace xna { + //Provides methods to retrieve and manipulate graphics adapters. class GraphicsAdapter { public: GraphicsAdapter(); - ~GraphicsAdapter(); + //Retrieves a string used for presentation to the user. String Description() const; + //Retrieves a value that is used to help identify a particular chip set. Uint DeviceId() const; + //Retrieves a string that contains the device name. String DeviceName() const; + //Determines if this instance of GraphicsAdapter is the default adapter. bool IsDefaultAdapter() const; + //Retrieves the handle of the monitor intptr_t MonitorHandle() const; + //Retrieves a value used to help identify the revision level of a particular chip set. Uint Revision() const; + //Retrieves a value used to identify the subsystem. Uint SubSystemId() const; + //Retrieves a value used to identify the manufacturer. Uint VendorId() const; + uptr SupportedDisplayModes() const; uptr SupportedDisplayModes(SurfaceFormat surfaceFormat) const; + + //Gets the current display mode. sptr CurrentDisplayMode(); + + //Gets the current display mode. void CurrentDisplayMode(SurfaceFormat surfaceFormat, Uint width, Uint height); + //Gets the default adapter. static uptr DefaultAdapter(); - static void Adapters(std::vector>& adapters); + + //Collection of available adapters on the system. static void Adapters(std::vector>& adapters); public: diff --git a/inc/xna/graphics/blendstate.hpp b/inc/xna/graphics/blendstate.hpp index 8affba0..722c0f1 100644 --- a/inc/xna/graphics/blendstate.hpp +++ b/inc/xna/graphics/blendstate.hpp @@ -3,24 +3,73 @@ #include "../default.hpp" #include "gresource.hpp" +#include "../common/color.hpp" namespace xna { struct BlendRenderTarget; + //Contains blend state for the device. class BlendState : public GraphicsResource { public: BlendState(); BlendState(sptr const& device); - ~BlendState() override; - bool Initialize() ; - void AlphaToCoverageEnable(bool value) ; - void IndependentBlendEnable(bool value) ; + + //Gets or sets the arithmetic operation when blending alpha values. The default is BlendFunction.Add. + BlendFunction AlphaBlendFunction() const; + //Gets or sets the arithmetic operation when blending alpha values. The default is BlendFunction.Add. + void AlphaBlendFunction(BlendFunction value); + //Gets or sets the blend factor for the destination alpha, which is the percentage of the destination alpha included in the blended result. The default is Blend.One. + Blend AlphaDestinationBlend() const; + //Gets or sets the blend factor for the destination alpha, which is the percentage of the destination alpha included in the blended result. The default is Blend.One. + void AlphaDestinationBlend(Blend value); + //Gets or sets the alpha blend factor. The default is Blend.One. + Blend AlphaSourceBlend() const; + //Gets or sets the alpha blend factor. The default is Blend.One. + void AlphaSourceBlend(Blend value); + + //Gets or sets the arithmetic operation when blending color values. The default is BlendFunction.Add. + BlendFunction ColorBlendFunction() const; + //Gets or sets the arithmetic operation when blending color values. The default is BlendFunction.Add. + void ColorBlendFunction(BlendFunction value); + //Gets or sets the blend factor for the destination color. The default is Blend.One. + Blend ColorDestinationBlend() const; + //Gets or sets the blend factor for the destination color. The default is Blend.One. + void ColorDestinationBlend(Blend value); + //Gets or sets the blend factor for the source color. The default is Blend.One. + Blend ColorSourceBlend() const; + //Gets or sets the blend factor for the source color. The default is Blend.One. + void ColorSourceBlend(Blend value); + + //Gets or sets the four-component (RGBA) blend factor for alpha blending. + Color BlendFactor() const; + //Gets or sets the four-component (RGBA) blend factor for alpha blending. + void BlendFactor(Color const& value); + + //Gets or sets a bitmask which defines which samples can be written during multisampling. The default is 0xffffffff. + Int MultiSampleMask() const; + //Gets or sets a bitmask which defines which samples can be written during multisampling. The default is 0xffffffff. + void MultiSampleMast(Int value); + + //Specifies whether to use alpha-to-coverage as a multisampling technique when setting a pixel to a render target. + void AlphaToCoverageEnable(bool value); + //Specifies whether to enable independent blending in simultaneous render targets + void IndependentBlendEnable(bool value); + void RenderTargets(std::vector const& value); + bool Initialize(); bool Apply(); + //A built-in state object with settings for opaque blend, + //that is overwriting the source with the destination data. static uptr Opaque(); + //A built-in state object with settings for alpha blend, + //that is blending the source and destination data using alpha. static uptr AlphaBlend(); + //A built-in state object with settings for additive blend, + //that is adding the destination data to the source data without using alpha. 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: diff --git a/inc/xna/platform-dx/dx.hpp b/inc/xna/platform-dx/dx.hpp index bd19353..450039b 100644 --- a/inc/xna/platform-dx/dx.hpp +++ b/inc/xna/platform-dx/dx.hpp @@ -101,11 +101,11 @@ namespace xna { //==============================================// struct DxHelpers { - static constexpr void ConvertSpriteSort(SpriteSortMode value, DirectX::SpriteSortMode& target) { + static constexpr void SpriteSortToDx(SpriteSortMode value, DirectX::SpriteSortMode& target) { target = static_cast(static_cast(value)); } - static constexpr DXGI_FORMAT ConvertSurfaceToDXGIFORMAT(SurfaceFormat format) + static constexpr DXGI_FORMAT SurfaceFormatToDx(SurfaceFormat format) { switch (format) { @@ -199,6 +199,47 @@ namespace xna { } } + static constexpr Blend ConvertBlendDx(D3D11_BLEND blend) { + switch (blend) { + case D3D11_BLEND_ZERO: + return Blend::Zero; + case D3D11_BLEND_ONE: + return Blend::One; + case D3D11_BLEND_SRC_COLOR: + return Blend::SourceColor; + case D3D11_BLEND_INV_SRC_COLOR: + return Blend::InverseSourceColor; + case D3D11_BLEND_SRC_ALPHA: + return Blend::SourceAlpha; + case D3D11_BLEND_INV_SRC_ALPHA: + return Blend::InverseSourceAlpha; + case D3D11_BLEND_DEST_ALPHA: + return Blend::DestinationAlpha; + case D3D11_BLEND_INV_DEST_ALPHA: + return Blend::InverseDestinationAlpha; + case D3D11_BLEND_DEST_COLOR: + return Blend::DestinationColor; + case D3D11_BLEND_INV_DEST_COLOR: + return Blend::InverseDestinationColor; + case D3D11_BLEND_SRC_ALPHA_SAT: + return Blend::SourceAlphaSaturation; + case D3D11_BLEND_BLEND_FACTOR: + return Blend::BlendFactor; + case D3D11_BLEND_INV_BLEND_FACTOR: + return Blend::InverseBlendFactor; + case D3D11_BLEND_SRC1_COLOR: + return Blend::Source1Color; + case D3D11_BLEND_INV_SRC1_COLOR: + return Blend::InverseSource1Color; + case D3D11_BLEND_SRC1_ALPHA: + return Blend::Source1Alpha; + case D3D11_BLEND_INV_SRC1_ALPHA: + return Blend::InverseSource1Alpha; + default: + return Blend::Zero; + } + } + static constexpr D3D11_BLEND ConvertBlend(Blend blend) { switch (blend) { @@ -245,6 +286,10 @@ namespace xna { return static_cast(static_cast(op) + 1); } + static constexpr BlendOperation ConvertOperationDx(D3D11_BLEND_OP op) { + return static_cast(static_cast(op) - 1); + } + static constexpr D3D11_COLOR_WRITE_ENABLE ConvertColorWrite(ColorWriteChannels colorWrite) { switch (colorWrite) { @@ -257,7 +302,7 @@ namespace xna { case xna::ColorWriteChannels::Alpha: return D3D11_COLOR_WRITE_ENABLE_ALPHA; case xna::ColorWriteChannels::All: - return D3D11_COLOR_WRITE_ENABLE_ALL; + return D3D11_COLOR_WRITE_ENABLE_ALL; default: return D3D11_COLOR_WRITE_ENABLE_ALL; }