From 522edf20a0a2f98cfe95e252893be4c497a9be4a Mon Sep 17 00:00:00 2001 From: Danilo Date: Wed, 22 May 2024 17:43:29 -0300 Subject: [PATCH] Corrige Shader --- framework/platform/shader-dx.cpp | 30 +++++++++------- inc/graphics/shader.hpp | 34 ++++++++++++++++-- inc/platform-dx/implementations.hpp | 23 ++++++++++++ inc/platform-dx/shader-dx.hpp | 56 ----------------------------- inc/platform-dx/xna-dx.hpp | 1 - 5 files changed, 72 insertions(+), 72 deletions(-) delete mode 100644 inc/platform-dx/shader-dx.hpp diff --git a/framework/platform/shader-dx.cpp b/framework/platform/shader-dx.cpp index e41d09a..88e0653 100644 --- a/framework/platform/shader-dx.cpp +++ b/framework/platform/shader-dx.cpp @@ -1,10 +1,10 @@ -#include "platform-dx/shader-dx.hpp" #include "platform-dx/device-dx.hpp" #include "graphics/buffer.hpp" #include "platform-dx/implementations.hpp" +#include "graphics/shader.hpp" namespace xna { - HRESULT Shader::CompileFromFile(_In_ LPCWSTR srcFile, _In_ LPCSTR entryPoint, _In_ LPCSTR profile, _Outptr_ ID3DBlob** blob) + static HRESULT shaderCompileFromFile(_In_ LPCWSTR srcFile, _In_ LPCSTR entryPoint, _In_ LPCSTR profile, _Outptr_ ID3DBlob** blob) { //https://learn.microsoft.com/en-us/windows/win32/direct3d11/how-to--compile-a-shader @@ -48,23 +48,29 @@ namespace xna { return hr; } + bool Shader::CompileFromFile(WString srcFile, String entryPoint, String profile, DataBuffer& blob) { + const auto hr = shaderCompileFromFile(srcFile.c_str(), entryPoint.c_str(), profile.c_str(), &blob.impl->_blob); + + return SUCCEEDED(hr); + } + bool VertexShader::Initialize(DataBuffer& buffer, xna_error_ptr_arg) { - if (!m_device || !m_device->_device || !buffer.impl->_blob) { + if (!impl || !m_device || !m_device->_device || !buffer.impl->_blob) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - if (_vertexShader) { - _vertexShader->Release(); - _vertexShader = nullptr; + if (impl->_vertexShader) { + impl->_vertexShader->Release(); + impl->_vertexShader = nullptr; } const auto hr = m_device->_device->CreateVertexShader( buffer.impl->_blob->GetBufferPointer(), buffer.impl->_blob->GetBufferSize(), NULL, - &_vertexShader); + &impl->_vertexShader); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); @@ -76,21 +82,21 @@ namespace xna { bool PixelShader::Initialize(DataBuffer& buffer, xna_error_ptr_arg) { - if (!m_device || !m_device->_device || !buffer.impl->_blob) { + if (!impl || !m_device || !m_device->_device || !buffer.impl->_blob) { xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); return false; } - if (_pixelShader) { - _pixelShader->Release(); - _pixelShader = nullptr; + if (impl->_pixelShader) { + impl->_pixelShader->Release(); + impl->_pixelShader = nullptr; } const auto hr = m_device->_device->CreatePixelShader( buffer.impl->_blob->GetBufferPointer(), buffer.impl->_blob->GetBufferSize(), NULL, - &_pixelShader); + &impl->_pixelShader); if (FAILED(hr)) { xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); diff --git a/inc/graphics/shader.hpp b/inc/graphics/shader.hpp index ba91705..f497b21 100644 --- a/inc/graphics/shader.hpp +++ b/inc/graphics/shader.hpp @@ -2,12 +2,40 @@ #define XNA_GRAPHICS_SHADER_HPP #include "../default.hpp" +#include "gresource.hpp" namespace xna { - class IShader { + class Shader : public GraphicsResource { public: - virtual ~IShader() {} - virtual bool Initialize(DataBuffer& buffer, xna_error_nullarg) = 0; + Shader(); + Shader(sptr const& device); + ~Shader(); + bool Initialize(DataBuffer& buffer, xna_error_nullarg); + static bool CompileFromFile(WString srcFile, String entryPoint, String profile, DataBuffer& blob); + }; + + class VertexShader : public Shader { + public: + VertexShader(); + VertexShader(sptr const& device); + ~VertexShader(); + bool Initialize(DataBuffer& buffer, xna_error_nullarg); + + public: + struct PlatformImplementation; + uptr impl = nullptr; + }; + + class PixelShader : public Shader { + public: + PixelShader(); + PixelShader(sptr const& device); + ~PixelShader(); + bool Initialize(DataBuffer& buffer, xna_error_nullarg); + + public: + struct PlatformImplementation; + uptr impl = nullptr; }; } diff --git a/inc/platform-dx/implementations.hpp b/inc/platform-dx/implementations.hpp index 0d4d79f..7deb2f6 100644 --- a/inc/platform-dx/implementations.hpp +++ b/inc/platform-dx/implementations.hpp @@ -14,6 +14,7 @@ #include "graphics/presentparams.hpp" #include "platform-dx/rendertarget-dx.hpp" #include "platform-dx/swapchain-dx.hpp" +#include "graphics/shader.hpp" namespace xna { struct SpriteFont::PlatformImplementation { @@ -227,4 +228,26 @@ namespace xna { ID3D11SamplerState* _samplerState = nullptr; D3D11_SAMPLER_DESC _description{}; }; + + struct VertexShader::PlatformImplementation { + ~PlatformImplementation() { + if (_vertexShader) { + _vertexShader->Release(); + _vertexShader = nullptr; + } + } + + ID3D11VertexShader* _vertexShader = nullptr; + }; + + struct PixelShader::PlatformImplementation { + ~PlatformImplementation() { + if (_pixelShader) { + _pixelShader->Release(); + _pixelShader = nullptr; + } + } + + ID3D11PixelShader* _pixelShader = nullptr; + }; } \ No newline at end of file diff --git a/inc/platform-dx/shader-dx.hpp b/inc/platform-dx/shader-dx.hpp deleted file mode 100644 index 28b3c17..0000000 --- a/inc/platform-dx/shader-dx.hpp +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef XNA_PLATFORM_SHADER_DX_HPP -#define XNA_PLATFORM_SHADER_DX_HPP - -#include "../graphics/shader.hpp" -#include "../graphics/gresource.hpp" -#include "graphics/buffer.hpp" -#include "dxheaders.hpp" - -namespace xna { - class Shader : public IShader, public GraphicsResource { - public: - Shader(sptr const& device) : GraphicsResource(device){} - - virtual ~Shader() override {} - - virtual bool Initialize(DataBuffer& buffer, xna_error_nullarg) override {} - - static HRESULT CompileFromFile(_In_ LPCWSTR srcFile, _In_ LPCSTR entryPoint, _In_ LPCSTR profile, _Outptr_ ID3DBlob** blob); - }; - - class VertexShader : public Shader { - public: - VertexShader(sptr const& device) : Shader(device){} - - virtual ~VertexShader() override { - if (_vertexShader) { - _vertexShader->Release(); - _vertexShader = nullptr; - } - } - - virtual bool Initialize(DataBuffer& buffer, xna_error_nullarg) override; - - public: - ID3D11VertexShader* _vertexShader = nullptr; - }; - - class PixelShader : public Shader { - public: - PixelShader(sptr const& device) : Shader(device) {} - - virtual ~PixelShader() override { - if (_pixelShader) { - _pixelShader->Release(); - _pixelShader = nullptr; - } - } - - virtual bool Initialize(DataBuffer& buffer, xna_error_nullarg) override; - - public: - ID3D11PixelShader* _pixelShader = nullptr; - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/xna-dx.hpp b/inc/platform-dx/xna-dx.hpp index 2cf941f..73a448e 100644 --- a/inc/platform-dx/xna-dx.hpp +++ b/inc/platform-dx/xna-dx.hpp @@ -8,7 +8,6 @@ #include "gdevicemanager-dx.hpp" #include "init-dx.hpp" #include "rendertarget-dx.hpp" -#include "shader-dx.hpp" #include "soundeffect-dx.hpp" #include "swapchain-dx.hpp" #include "texture-dx.hpp"