From da12db2c473e4e99f01c8e1f3db2f7e709496a50 Mon Sep 17 00:00:00 2001 From: Danilo Date: Wed, 24 Apr 2024 14:40:14 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20BlendState?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/enums.hpp | 9 +++++- framework/platform/blendstate-dx.cpp | 44 +++++++++++++------------- framework/platform/blendstate-dx.hpp | 32 +++++++++---------- framework/platform/constbuffer-dx.hpp | 1 + framework/platform/spritebatch-dx.cpp | 2 +- framework/platform/vertexbuffer-dx.cpp | 3 ++ framework/platform/vertexbuffer-dx.hpp | 2 +- 7 files changed, 52 insertions(+), 41 deletions(-) diff --git a/framework/enums.hpp b/framework/enums.hpp index 24bcf54..720756f 100644 --- a/framework/enums.hpp +++ b/framework/enums.hpp @@ -67,6 +67,13 @@ namespace xna { using BlendOperation = BlendFunction; + enum class BufferUsage { + Static, + Dynamic, + Immutable, + Staging + }; + enum class Buttons { A = 4096, // 0x00001000 B = 8192, // 0x00002000 @@ -107,7 +114,7 @@ namespace xna { Blue, Alpha, All - }; + }; enum class ComparisonFunction { Never, diff --git a/framework/platform/blendstate-dx.cpp b/framework/platform/blendstate-dx.cpp index eeb315b..319418b 100644 --- a/framework/platform/blendstate-dx.cpp +++ b/framework/platform/blendstate-dx.cpp @@ -9,12 +9,12 @@ namespace xna { return false; } - if (_blendState) { - _blendState->Release(); - _blendState = nullptr; + if (dxBlendState) { + dxBlendState->Release(); + dxBlendState = nullptr; } - const auto hr = device._device->CreateBlendState(&_description, &_blendState); + const auto hr = device._device->CreateBlendState(&dxDescription, &dxBlendState); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -30,52 +30,52 @@ namespace xna { return false; } - if (!_blendState) { + if (!dxBlendState) { const auto init = Initialize(device, err); if (!init) return false; } - device._context->OMSetBlendState(_blendState, blendFactor, sampleMask); + device._context->OMSetBlendState(dxBlendState, blendFactor, sampleMask); return true; } PBlendState IBlendState::Opaque() { auto blendState = New(); - blendState->_description.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; - blendState->_description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO; - blendState->_description.RenderTarget[0].DestBlend = D3D11_BLEND_DEST_ALPHA; - blendState->_description.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; + blendState->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; + blendState->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO; + blendState->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_DEST_ALPHA; + blendState->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; return blendState; } PBlendState IBlendState::AlphaBlend() { auto blendState = New(); - blendState->_description.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; - blendState->_description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; - blendState->_description.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; - blendState->_description.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; + blendState->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; + blendState->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE; + blendState->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; + blendState->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; return blendState; } PBlendState IBlendState::Additive() { auto blendState = New(); - blendState->_description.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; - blendState->_description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; - blendState->_description.RenderTarget[0].DestBlend = D3D11_BLEND_ONE; - blendState->_description.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE; + blendState->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; + blendState->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; + blendState->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_ONE; + blendState->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE; return blendState; } PBlendState IBlendState::NonPremultiplied() { auto blendState = New(); - blendState->_description.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; - blendState->_description.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; - blendState->_description.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; - blendState->_description.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; + blendState->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; + blendState->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; + blendState->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; + blendState->dxDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; return blendState; } diff --git a/framework/platform/blendstate-dx.hpp b/framework/platform/blendstate-dx.hpp index 830ca53..dfadf37 100644 --- a/framework/platform/blendstate-dx.hpp +++ b/framework/platform/blendstate-dx.hpp @@ -23,41 +23,41 @@ namespace xna { BlendState() = default; virtual ~BlendState() override { - if (_blendState) { - _blendState->Release(); - _blendState = nullptr; + if (dxBlendState) { + dxBlendState->Release(); + dxBlendState = nullptr; } } virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override; virtual constexpr void AlphaToCoverageEnable(bool value) override { - _description.AlphaToCoverageEnable = value; + dxDescription.AlphaToCoverageEnable = value; } virtual constexpr void IndependentBlendEnable(bool value) override { - _description.IndependentBlendEnable = value; + dxDescription.IndependentBlendEnable = value; } virtual void RenderTargets(std::vector const& value) override { for (size_t i = 0; i < value.size() && i < 8; ++i) { - _description.RenderTarget[i].BlendEnable = value[i].Enabled; - _description.RenderTarget[i].SrcBlend = ConvertBlend(value[i].Source); - _description.RenderTarget[i].DestBlend = ConvertBlend(value[i].Destination); - _description.RenderTarget[i].BlendOp = ConvertOperation(value[i].Operation); - _description.RenderTarget[i].SrcBlendAlpha = ConvertBlend(value[i].SourceAlpha); - _description.RenderTarget[i].DestBlendAlpha = ConvertBlend(value[i].DestinationAlpha); - _description.RenderTarget[i].BlendOpAlpha = ConvertOperation(value[i].OperationAlpha); - _description.RenderTarget[i].RenderTargetWriteMask = ConvertColorWrite(value[i].WriteMask); + dxDescription.RenderTarget[i].BlendEnable = value[i].Enabled; + dxDescription.RenderTarget[i].SrcBlend = ConvertBlend(value[i].Source); + dxDescription.RenderTarget[i].DestBlend = ConvertBlend(value[i].Destination); + dxDescription.RenderTarget[i].BlendOp = ConvertOperation(value[i].Operation); + dxDescription.RenderTarget[i].SrcBlendAlpha = ConvertBlend(value[i].SourceAlpha); + dxDescription.RenderTarget[i].DestBlendAlpha = ConvertBlend(value[i].DestinationAlpha); + dxDescription.RenderTarget[i].BlendOpAlpha = ConvertOperation(value[i].OperationAlpha); + dxDescription.RenderTarget[i].RenderTargetWriteMask = ConvertColorWrite(value[i].WriteMask); } } virtual bool Apply(GraphicsDevice& device, xna_error_nullarg) override; public: - ID3D11BlendState* _blendState{ nullptr }; - D3D11_BLEND_DESC _description{}; + ID3D11BlendState* dxBlendState{ nullptr }; + D3D11_BLEND_DESC dxDescription{}; float blendFactor[4] { 1.0F, 1.0F, 1.0F, 1.0F }; - UINT sampleMask{ 0xffffffff }; + UINT sampleMask{ 0xffffffff }; public: static constexpr D3D11_BLEND ConvertBlend(Blend blend) { diff --git a/framework/platform/constbuffer-dx.hpp b/framework/platform/constbuffer-dx.hpp index b7a775d..6f46ab2 100644 --- a/framework/platform/constbuffer-dx.hpp +++ b/framework/platform/constbuffer-dx.hpp @@ -4,6 +4,7 @@ #include "../graphics/constbuffer.hpp" #include "../common/matrix.hpp" #include "dxheaders.hpp" +#include namespace xna { class ConstantBuffer : public IConstantBuffer { diff --git a/framework/platform/spritebatch-dx.cpp b/framework/platform/spritebatch-dx.cpp index bdbea45..325854d 100644 --- a/framework/platform/spritebatch-dx.cpp +++ b/framework/platform/spritebatch-dx.cpp @@ -40,7 +40,7 @@ namespace xna { _dxspriteBatch->Begin( sort, - blendState ? blendState->_blendState : nullptr, + blendState ? blendState->dxBlendState : nullptr, samplerState ? samplerState->_samplerState : nullptr, depthStencil ? depthStencil->_depthStencil : nullptr, rasterizerState ? rasterizerState->_rasterizerState : nullptr, diff --git a/framework/platform/vertexbuffer-dx.cpp b/framework/platform/vertexbuffer-dx.cpp index ea69e2e..66171b7 100644 --- a/framework/platform/vertexbuffer-dx.cpp +++ b/framework/platform/vertexbuffer-dx.cpp @@ -1,5 +1,6 @@ #include "vertexbuffer-dx.hpp" #include "device-dx.hpp" +#include namespace xna { bool VertexBuffer::Initialize(GraphicsDevice& device, xna_error_ptr_arg) @@ -14,6 +15,8 @@ namespace xna { _buffer = nullptr; } + + const auto hr = device._device->CreateBuffer( &_description, &_subResource, diff --git a/framework/platform/vertexbuffer-dx.hpp b/framework/platform/vertexbuffer-dx.hpp index a9f84d5..fb4afcd 100644 --- a/framework/platform/vertexbuffer-dx.hpp +++ b/framework/platform/vertexbuffer-dx.hpp @@ -2,8 +2,8 @@ #define XNA_PLATFORM_VERTEXBUFFER_DX_HPP #include "../graphics/vertexbuffer.hpp" -#include "dxheaders.hpp" #include "../graphics/vertexposition.hpp" +#include "dxheaders.hpp" namespace xna { class VertexBuffer : public IVertexBuffer {