From 60d106588d3238fef004b2a56429d737e956c700 Mon Sep 17 00:00:00 2001 From: Danilo Date: Mon, 20 May 2024 10:41:40 -0300 Subject: [PATCH] Implementa DxHelpers --- framework/platform/adapter-dx.cpp | 11 +- framework/platform/blendstate-dx.cpp | 15 +-- framework/platform/swapchain-dx.cpp | 3 +- framework/platform/texture-dx.cpp | 3 +- inc/platform-dx/dxhelpers.hpp | 165 +++++++++++++++++++++++++++ inc/platform-dx/implementations.hpp | 162 +------------------------- 6 files changed, 185 insertions(+), 174 deletions(-) create mode 100644 inc/platform-dx/dxhelpers.hpp diff --git a/framework/platform/adapter-dx.cpp b/framework/platform/adapter-dx.cpp index 2e835f9..4e96676 100644 --- a/framework/platform/adapter-dx.cpp +++ b/framework/platform/adapter-dx.cpp @@ -3,6 +3,7 @@ #include "graphics/adapter.hpp" #include "platform-dx/displaymode-dx.hpp" #include "platform-dx/dxheaders.hpp" +#include "platform-dx/dxhelpers.hpp" namespace xna { static size_t getDisplayModesCount(IDXGIAdapter* adapter); @@ -181,7 +182,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 = GraphicsAdapter::PlatformImplementation::ConvertSurfaceToDXGIFORMAT(currentSurface); + DXGI_FORMAT format = DxHelpers::ConvertSurfaceToDXGIFORMAT(currentSurface); UINT numModes = 0; pOutput->GetDisplayModeList(format, 0, &numModes, nullptr); @@ -212,7 +213,7 @@ namespace xna { UINT bufferOffset = 0; if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { - DXGI_FORMAT format = GraphicsAdapter::PlatformImplementation::ConvertSurfaceToDXGIFORMAT(surfaceFormat); + DXGI_FORMAT format = DxHelpers::ConvertSurfaceToDXGIFORMAT(surfaceFormat); UINT numModes = 0; @@ -274,7 +275,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 = GraphicsAdapter::PlatformImplementation::ConvertSurfaceToDXGIFORMAT(currentSurface); + DXGI_FORMAT format = DxHelpers::ConvertSurfaceToDXGIFORMAT(currentSurface); UINT num = 0; pOutput->GetDisplayModeList(format, 0, &num, nullptr); @@ -303,14 +304,14 @@ namespace xna { description._scaling = static_cast(modedesc.Scaling); description._scanlineOrdering = static_cast(modedesc.ScanlineOrdering); - if (pDisplay && pDisplay->_width == modedesc.Width && pDisplay->_height == modedesc.Height && pDisplay->_format == GraphicsAdapter::PlatformImplementation::ConvertDXGIFORMATToSurface(modedesc.Format)) { + if (pDisplay && pDisplay->_width == modedesc.Width && pDisplay->_height == modedesc.Height && pDisplay->_format == DxHelpers::ConvertDXGIFORMATToSurface(modedesc.Format)) { pDisplay->_descriptions.push_back(description); } else { pDisplay = New(); pDisplay->_width = modedesc.Width; pDisplay->_height = modedesc.Height; - pDisplay->_format = GraphicsAdapter::PlatformImplementation::ConvertDXGIFORMATToSurface(modedesc.Format); + pDisplay->_format = DxHelpers::ConvertDXGIFORMATToSurface(modedesc.Format); pDisplay->_descriptions.push_back(description); displayList.push_back(pDisplay); } diff --git a/framework/platform/blendstate-dx.cpp b/framework/platform/blendstate-dx.cpp index 2259604..625c3e7 100644 --- a/framework/platform/blendstate-dx.cpp +++ b/framework/platform/blendstate-dx.cpp @@ -2,6 +2,7 @@ #include "graphics/gresource.hpp" #include "platform-dx/device-dx.hpp" #include "platform-dx/dxheaders.hpp" +#include "platform-dx/dxhelpers.hpp" #include "graphics/blendstate.hpp" #include "platform-dx/implementations.hpp" @@ -72,13 +73,13 @@ namespace xna { 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 = PlatformImplementation::ConvertBlend(value[i].Source); - impl->dxDescription.RenderTarget[i].DestBlend = PlatformImplementation::ConvertBlend(value[i].Destination); - impl->dxDescription.RenderTarget[i].BlendOp = PlatformImplementation::ConvertOperation(value[i].Operation); - impl->dxDescription.RenderTarget[i].SrcBlendAlpha = PlatformImplementation::ConvertBlend(value[i].SourceAlpha); - impl->dxDescription.RenderTarget[i].DestBlendAlpha = PlatformImplementation::ConvertBlend(value[i].DestinationAlpha); - impl->dxDescription.RenderTarget[i].BlendOpAlpha = PlatformImplementation::ConvertOperation(value[i].OperationAlpha); - impl->dxDescription.RenderTarget[i].RenderTargetWriteMask = PlatformImplementation::ConvertColorWrite(value[i].WriteMask); + impl->dxDescription.RenderTarget[i].SrcBlend = DxHelpers::ConvertBlend(value[i].Source); + impl->dxDescription.RenderTarget[i].DestBlend = DxHelpers::ConvertBlend(value[i].Destination); + impl->dxDescription.RenderTarget[i].BlendOp = DxHelpers::ConvertOperation(value[i].Operation); + impl->dxDescription.RenderTarget[i].SrcBlendAlpha = DxHelpers::ConvertBlend(value[i].SourceAlpha); + impl->dxDescription.RenderTarget[i].DestBlendAlpha = DxHelpers::ConvertBlend(value[i].DestinationAlpha); + impl->dxDescription.RenderTarget[i].BlendOpAlpha = DxHelpers::ConvertOperation(value[i].OperationAlpha); + impl->dxDescription.RenderTarget[i].RenderTargetWriteMask = DxHelpers::ConvertColorWrite(value[i].WriteMask); } } diff --git a/framework/platform/swapchain-dx.cpp b/framework/platform/swapchain-dx.cpp index fa3d3ca..36cfd10 100644 --- a/framework/platform/swapchain-dx.cpp +++ b/framework/platform/swapchain-dx.cpp @@ -1,5 +1,6 @@ #include "platform-dx/swapchain-dx.hpp" #include "platform-dx/device-dx.hpp" +#include "platform-dx/dxhelpers.hpp" #include "graphics/adapter.hpp" #include "platform-dx/implementations.hpp" @@ -48,7 +49,7 @@ namespace xna { dxDescription.Width = static_cast(parameters.backBufferWidth); dxDescription.Height = static_cast(parameters.backBufferHeight); - dxDescription.Format = GraphicsAdapter::PlatformImplementation::ConvertSurfaceToDXGIFORMAT(parameters.backBufferFormat); + dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(parameters.backBufferFormat); dxDescription.SampleDesc.Count = 1; dxDescription.SampleDesc.Quality = 0; dxDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; diff --git a/framework/platform/texture-dx.cpp b/framework/platform/texture-dx.cpp index caf208a..15c516c 100644 --- a/framework/platform/texture-dx.cpp +++ b/framework/platform/texture-dx.cpp @@ -2,6 +2,7 @@ #include "platform-dx/device-dx.hpp" #include "graphics/adapter.hpp" #include "platform-dx/implementations.hpp" +#include "platform-dx/dxhelpers.hpp" namespace xna { sptr Texture2D::FromStream(GraphicsDevice& device, String const& fileName, xna_error_ptr_arg) @@ -112,7 +113,7 @@ namespace xna { dxDescription.Width = static_cast(width); dxDescription.Height = static_cast(height); dxDescription.MipLevels = static_cast(mipMap); - dxDescription.Format = GraphicsAdapter::PlatformImplementation::ConvertSurfaceToDXGIFORMAT(format); + dxDescription.Format = DxHelpers::ConvertSurfaceToDXGIFORMAT(format); } void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount, xna_error_ptr_arg) diff --git a/inc/platform-dx/dxhelpers.hpp b/inc/platform-dx/dxhelpers.hpp new file mode 100644 index 0000000..0bc73d4 --- /dev/null +++ b/inc/platform-dx/dxhelpers.hpp @@ -0,0 +1,165 @@ +#include "dxheaders.hpp" +#include "graphics/blendstate.hpp" +#include "graphics/adapter.hpp" + +namespace xna { + struct DxHelpers { + static constexpr DXGI_FORMAT ConvertSurfaceToDXGIFORMAT(SurfaceFormat format) + { + switch (format) + { + case SurfaceFormat::Color://21 + return DXGI_FORMAT_R8G8B8A8_UNORM; + case SurfaceFormat::Bgr565: //23 + return DXGI_FORMAT_B5G6R5_UNORM; + case SurfaceFormat::Bgra5551://25 + return DXGI_FORMAT_B5G5R5A1_UNORM; + case SurfaceFormat::Bgra4444://26 + return DXGI_FORMAT_B4G4R4A4_UNORM; + case SurfaceFormat::Dxt1://827611204 + return DXGI_FORMAT_BC1_UNORM; + case SurfaceFormat::Dxt3://861165636 + return DXGI_FORMAT_BC2_UNORM; + case SurfaceFormat::Dxt5://894720068 + return DXGI_FORMAT_BC3_UNORM; + case SurfaceFormat::NormalizedByte2://60 + return DXGI_FORMAT_R8G8_SNORM; + case SurfaceFormat::NormalizedByte4://63 + return DXGI_FORMAT_R8G8B8A8_SNORM; + case SurfaceFormat::Rgba1010102://31 + return DXGI_FORMAT_R10G10B10A2_UNORM; + case SurfaceFormat::Rg32://34 + return DXGI_FORMAT_R16G16_UNORM; + case SurfaceFormat::Rgba64://36 + return DXGI_FORMAT_R16G16B16A16_UNORM; + case SurfaceFormat::Alpha8://28 + return DXGI_FORMAT_A8_UNORM; + case SurfaceFormat::Single://114 + return DXGI_FORMAT_R32_FLOAT; + case SurfaceFormat::Vector2://115 + return DXGI_FORMAT_R32G32_FLOAT; + case SurfaceFormat::Vector4://116 + return DXGI_FORMAT_R32G32B32A32_FLOAT; + case SurfaceFormat::HalfSingle://111 + return DXGI_FORMAT_R16_FLOAT; + case SurfaceFormat::HalfVector2://112 + return DXGI_FORMAT_R16G16_FLOAT; + case SurfaceFormat::HalfVector4://113 + return DXGI_FORMAT_R16G16B16A16_FLOAT; + case SurfaceFormat::HdrBlendable://113 + return DXGI_FORMAT_R16G16B16A16_FLOAT; + default://0 + return DXGI_FORMAT_UNKNOWN; + } + } + + static constexpr SurfaceFormat ConvertDXGIFORMATToSurface(DXGI_FORMAT format) { + switch (format) + { + case DXGI_FORMAT_R8G8B8A8_UNORM: + case DXGI_FORMAT_B8G8R8A8_UNORM: + return SurfaceFormat::Color; + case DXGI_FORMAT_B5G6R5_UNORM: + return SurfaceFormat::Bgr565; + case DXGI_FORMAT_B5G5R5A1_UNORM: + return SurfaceFormat::Bgra5551; + case DXGI_FORMAT_B4G4R4A4_UNORM: + return SurfaceFormat::Bgra4444; + case DXGI_FORMAT_BC2_UNORM: + return SurfaceFormat::Dxt3; + case DXGI_FORMAT_BC3_UNORM: + return SurfaceFormat::Dxt5; + case DXGI_FORMAT_R8G8_SNORM: + return SurfaceFormat::NormalizedByte2; + case DXGI_FORMAT_R8G8B8A8_SNORM: + return SurfaceFormat::NormalizedByte4; + case DXGI_FORMAT_R10G10B10A2_UNORM: + return SurfaceFormat::Rgba1010102; + case DXGI_FORMAT_R16G16_UNORM: + return SurfaceFormat::Rg32; + case DXGI_FORMAT_R16G16B16A16_UNORM: + return SurfaceFormat::Rgba64; + case DXGI_FORMAT_A8_UNORM: + return SurfaceFormat::Alpha8; + case DXGI_FORMAT_R32_FLOAT: + return SurfaceFormat::Single; + case DXGI_FORMAT_R32G32_FLOAT: + return SurfaceFormat::Vector2; + case DXGI_FORMAT_R32G32B32A32_FLOAT: + return SurfaceFormat::Vector4; + case DXGI_FORMAT_R16_FLOAT: + return SurfaceFormat::HalfSingle; + case DXGI_FORMAT_R16G16_FLOAT: + return SurfaceFormat::HalfVector2; + case DXGI_FORMAT_R16G16B16A16_FLOAT: + return SurfaceFormat::HalfVector4; + default: + return SurfaceFormat::Unknown; + } + } + + static constexpr D3D11_BLEND ConvertBlend(Blend blend) { + switch (blend) + { + case xna::Blend::Zero: + return D3D11_BLEND_ZERO; + case xna::Blend::One: + return D3D11_BLEND_ONE; + case xna::Blend::SourceColor: + return D3D11_BLEND_SRC_COLOR; + case xna::Blend::InverseSourceColor: + return D3D11_BLEND_INV_SRC_COLOR; + case xna::Blend::SourceAlpha: + return D3D11_BLEND_SRC_ALPHA; + case xna::Blend::InverseSourceAlpha: + return D3D11_BLEND_INV_SRC_ALPHA; + case xna::Blend::DestinationAlpha: + return D3D11_BLEND_DEST_ALPHA; + case xna::Blend::InverseDestinationAlpha: + return D3D11_BLEND_INV_DEST_ALPHA; + case xna::Blend::DestinationColor: + return D3D11_BLEND_DEST_COLOR; + case xna::Blend::InverseDestinationColor: + return D3D11_BLEND_INV_DEST_COLOR; + case xna::Blend::SourceAlphaSaturation: + return D3D11_BLEND_SRC_ALPHA_SAT; + case xna::Blend::BlendFactor: + return D3D11_BLEND_BLEND_FACTOR; + case xna::Blend::InverseBlendFactor: + return D3D11_BLEND_INV_BLEND_FACTOR; + case xna::Blend::Source1Color: + return D3D11_BLEND_SRC1_COLOR; + case xna::Blend::InverseSource1Color: + return D3D11_BLEND_INV_SRC1_COLOR; + case xna::Blend::Source1Alpha: + return D3D11_BLEND_SRC1_ALPHA; + case xna::Blend::InverseSource1Alpha: + return D3D11_BLEND_INV_SRC1_ALPHA; + default: + return D3D11_BLEND_ZERO; + } + } + + static constexpr D3D11_BLEND_OP ConvertOperation(BlendOperation op) { + return static_cast(static_cast(op) + 1); + } + + static constexpr D3D11_COLOR_WRITE_ENABLE ConvertColorWrite(ColorWriteChannels colorWrite) { + switch (colorWrite) + { + case xna::ColorWriteChannels::Red: + return D3D11_COLOR_WRITE_ENABLE_RED; + case xna::ColorWriteChannels::Green: + return D3D11_COLOR_WRITE_ENABLE_GREEN; + case xna::ColorWriteChannels::Blue: + return D3D11_COLOR_WRITE_ENABLE_BLUE; + case xna::ColorWriteChannels::Alpha: + return D3D11_COLOR_WRITE_ENABLE_ALPHA; + case xna::ColorWriteChannels::All: + return D3D11_COLOR_WRITE_ENABLE_ALL; + default: + return D3D11_COLOR_WRITE_ENABLE_ALL; + } + } + }; +} \ No newline at end of file diff --git a/inc/platform-dx/implementations.hpp b/inc/platform-dx/implementations.hpp index 3abb21c..b565119 100644 --- a/inc/platform-dx/implementations.hpp +++ b/inc/platform-dx/implementations.hpp @@ -33,101 +33,7 @@ namespace xna { sptr _currentDisplayMode = nullptr; public: - bool GetOutput(UINT slot, IDXGIOutput*& output); - - static constexpr DXGI_FORMAT ConvertSurfaceToDXGIFORMAT(SurfaceFormat format) - { - switch (format) - { - case SurfaceFormat::Color://21 - return DXGI_FORMAT_R8G8B8A8_UNORM; - case SurfaceFormat::Bgr565: //23 - return DXGI_FORMAT_B5G6R5_UNORM; - case SurfaceFormat::Bgra5551://25 - return DXGI_FORMAT_B5G5R5A1_UNORM; - case SurfaceFormat::Bgra4444://26 - return DXGI_FORMAT_B4G4R4A4_UNORM; - case SurfaceFormat::Dxt1://827611204 - return DXGI_FORMAT_BC1_UNORM; - case SurfaceFormat::Dxt3://861165636 - return DXGI_FORMAT_BC2_UNORM; - case SurfaceFormat::Dxt5://894720068 - return DXGI_FORMAT_BC3_UNORM; - case SurfaceFormat::NormalizedByte2://60 - return DXGI_FORMAT_R8G8_SNORM; - case SurfaceFormat::NormalizedByte4://63 - return DXGI_FORMAT_R8G8B8A8_SNORM; - case SurfaceFormat::Rgba1010102://31 - return DXGI_FORMAT_R10G10B10A2_UNORM; - case SurfaceFormat::Rg32://34 - return DXGI_FORMAT_R16G16_UNORM; - case SurfaceFormat::Rgba64://36 - return DXGI_FORMAT_R16G16B16A16_UNORM; - case SurfaceFormat::Alpha8://28 - return DXGI_FORMAT_A8_UNORM; - case SurfaceFormat::Single://114 - return DXGI_FORMAT_R32_FLOAT; - case SurfaceFormat::Vector2://115 - return DXGI_FORMAT_R32G32_FLOAT; - case SurfaceFormat::Vector4://116 - return DXGI_FORMAT_R32G32B32A32_FLOAT; - case SurfaceFormat::HalfSingle://111 - return DXGI_FORMAT_R16_FLOAT; - case SurfaceFormat::HalfVector2://112 - return DXGI_FORMAT_R16G16_FLOAT; - case SurfaceFormat::HalfVector4://113 - return DXGI_FORMAT_R16G16B16A16_FLOAT; - case SurfaceFormat::HdrBlendable://113 - return DXGI_FORMAT_R16G16B16A16_FLOAT; - default://0 - return DXGI_FORMAT_UNKNOWN; - } - } - - static constexpr SurfaceFormat ConvertDXGIFORMATToSurface(DXGI_FORMAT format) { - switch (format) - { - case DXGI_FORMAT_R8G8B8A8_UNORM: - case DXGI_FORMAT_B8G8R8A8_UNORM: - return SurfaceFormat::Color; - case DXGI_FORMAT_B5G6R5_UNORM: - return SurfaceFormat::Bgr565; - case DXGI_FORMAT_B5G5R5A1_UNORM: - return SurfaceFormat::Bgra5551; - case DXGI_FORMAT_B4G4R4A4_UNORM: - return SurfaceFormat::Bgra4444; - case DXGI_FORMAT_BC2_UNORM: - return SurfaceFormat::Dxt3; - case DXGI_FORMAT_BC3_UNORM: - return SurfaceFormat::Dxt5; - case DXGI_FORMAT_R8G8_SNORM: - return SurfaceFormat::NormalizedByte2; - case DXGI_FORMAT_R8G8B8A8_SNORM: - return SurfaceFormat::NormalizedByte4; - case DXGI_FORMAT_R10G10B10A2_UNORM: - return SurfaceFormat::Rgba1010102; - case DXGI_FORMAT_R16G16_UNORM: - return SurfaceFormat::Rg32; - case DXGI_FORMAT_R16G16B16A16_UNORM: - return SurfaceFormat::Rgba64; - case DXGI_FORMAT_A8_UNORM: - return SurfaceFormat::Alpha8; - case DXGI_FORMAT_R32_FLOAT: - return SurfaceFormat::Single; - case DXGI_FORMAT_R32G32_FLOAT: - return SurfaceFormat::Vector2; - case DXGI_FORMAT_R32G32B32A32_FLOAT: - return SurfaceFormat::Vector4; - case DXGI_FORMAT_R16_FLOAT: - return SurfaceFormat::HalfSingle; - case DXGI_FORMAT_R16G16_FLOAT: - return SurfaceFormat::HalfVector2; - case DXGI_FORMAT_R16G16B16A16_FLOAT: - return SurfaceFormat::HalfVector4; - default: - return SurfaceFormat::Unknown; - } - } + bool GetOutput(UINT slot, IDXGIOutput*& output); }; struct BlendRenderTarget { @@ -154,71 +60,7 @@ namespace xna { ID3D11BlendState* dxBlendState = nullptr; D3D11_BLEND_DESC dxDescription{}; float blendFactor[4]{ 1.0F, 1.0F, 1.0F, 1.0F }; - UINT sampleMask{ 0xffffffff }; - - static constexpr D3D11_BLEND ConvertBlend(Blend blend) { - switch (blend) - { - case xna::Blend::Zero: - return D3D11_BLEND_ZERO; - case xna::Blend::One: - return D3D11_BLEND_ONE; - case xna::Blend::SourceColor: - return D3D11_BLEND_SRC_COLOR; - case xna::Blend::InverseSourceColor: - return D3D11_BLEND_INV_SRC_COLOR; - case xna::Blend::SourceAlpha: - return D3D11_BLEND_SRC_ALPHA; - case xna::Blend::InverseSourceAlpha: - return D3D11_BLEND_INV_SRC_ALPHA; - case xna::Blend::DestinationAlpha: - return D3D11_BLEND_DEST_ALPHA; - case xna::Blend::InverseDestinationAlpha: - return D3D11_BLEND_INV_DEST_ALPHA; - case xna::Blend::DestinationColor: - return D3D11_BLEND_DEST_COLOR; - case xna::Blend::InverseDestinationColor: - return D3D11_BLEND_INV_DEST_COLOR; - case xna::Blend::SourceAlphaSaturation: - return D3D11_BLEND_SRC_ALPHA_SAT; - case xna::Blend::BlendFactor: - return D3D11_BLEND_BLEND_FACTOR; - case xna::Blend::InverseBlendFactor: - return D3D11_BLEND_INV_BLEND_FACTOR; - case xna::Blend::Source1Color: - return D3D11_BLEND_SRC1_COLOR; - case xna::Blend::InverseSource1Color: - return D3D11_BLEND_INV_SRC1_COLOR; - case xna::Blend::Source1Alpha: - return D3D11_BLEND_SRC1_ALPHA; - case xna::Blend::InverseSource1Alpha: - return D3D11_BLEND_INV_SRC1_ALPHA; - default: - return D3D11_BLEND_ZERO; - } - } - - static constexpr D3D11_BLEND_OP ConvertOperation(BlendOperation op) { - return static_cast(static_cast(op) + 1); - } - - static constexpr D3D11_COLOR_WRITE_ENABLE ConvertColorWrite(ColorWriteChannels colorWrite) { - switch (colorWrite) - { - case xna::ColorWriteChannels::Red: - return D3D11_COLOR_WRITE_ENABLE_RED; - case xna::ColorWriteChannels::Green: - return D3D11_COLOR_WRITE_ENABLE_GREEN; - case xna::ColorWriteChannels::Blue: - return D3D11_COLOR_WRITE_ENABLE_BLUE; - case xna::ColorWriteChannels::Alpha: - return D3D11_COLOR_WRITE_ENABLE_ALPHA; - case xna::ColorWriteChannels::All: - return D3D11_COLOR_WRITE_ENABLE_ALL; - default: - return D3D11_COLOR_WRITE_ENABLE_ALL; - } - } + UINT sampleMask{ 0xffffffff }; }; struct ConstantBuffer::PlatformImplementation {