diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index cbb5e15..3e18b9b 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -19,7 +19,7 @@ add_executable (xna WIN32 "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizerstate-dx.cpp" -"platform/vertexbuffer-dx.cpp" + diff --git a/framework/platform/buffer.cpp b/framework/platform/buffer.cpp index 7366236..f18d74a 100644 --- a/framework/platform/buffer.cpp +++ b/framework/platform/buffer.cpp @@ -90,4 +90,35 @@ namespace xna { return true; } + + VertexBuffer::VertexBuffer() : GraphicsResource(nullptr) { + impl = uNew(); + } + + VertexBuffer::VertexBuffer(sptr const& device) : GraphicsResource(device) { + impl = uNew(); + } + + VertexBuffer::~VertexBuffer() { + impl = nullptr; + } + + bool VertexBuffer::Apply(xna_error_ptr_arg) { + if (!impl || !m_device || !m_device->_context) { + xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); + return false; + } + + if (!impl->dxBuffer) { + xna_error_apply(err, XnaErrorCode::UNINTIALIZED_RESOURCE); + return false; + } + + UINT stride = impl->size; + UINT offset = 0; + m_device->_context->IASetVertexBuffers(0, 1, + &impl->dxBuffer, &stride, &offset); + + return true; + } } \ No newline at end of file diff --git a/framework/platform/vertexbuffer-dx.cpp b/framework/platform/vertexbuffer-dx.cpp deleted file mode 100644 index 7b9df78..0000000 --- a/framework/platform/vertexbuffer-dx.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "platform-dx/vertexbuffer-dx.hpp" - -namespace xna { -} \ No newline at end of file diff --git a/inc/graphics/buffer.hpp b/inc/graphics/buffer.hpp index cc0e132..7b1f7e3 100644 --- a/inc/graphics/buffer.hpp +++ b/inc/graphics/buffer.hpp @@ -43,6 +43,20 @@ namespace xna { struct PlatformImplementation; uptr impl = nullptr; }; + + class VertexBuffer : public GraphicsResource { + public: + VertexBuffer(); + VertexBuffer(sptr const&); + ~VertexBuffer(); + template + bool Initialize(std::vector const& data, xna_error_nullarg); + bool Apply(xna_error_nullarg); + + public: + struct PlatformImplementation; + uptr impl = nullptr; + }; } #endif \ No newline at end of file diff --git a/inc/graphics/vertexbuffer.hpp b/inc/graphics/vertexbuffer.hpp deleted file mode 100644 index 7d81904..0000000 --- a/inc/graphics/vertexbuffer.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef XNA_GRAPHICS_VERTEXBUFFER_HPP -#define XNA_GRAPHICS_VERTEXBUFFER_HPP - -#include "../default.hpp" - -namespace xna { - class IVertexBuffer { - public: - virtual ~IVertexBuffer(){} - virtual bool Initialize(xna_error_nullarg) = 0; - virtual bool Apply(xna_error_nullarg) = 0; - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/implementations.hpp b/inc/platform-dx/implementations.hpp index 7631278..3da0495 100644 --- a/inc/platform-dx/implementations.hpp +++ b/inc/platform-dx/implementations.hpp @@ -302,6 +302,37 @@ namespace xna { ID3D11RenderTargetView* _renderTargetView = nullptr; D3D11_RENDER_TARGET_VIEW_DESC _renderTargetDesc{}; }; + + struct VertexBuffer::PlatformImplementation { + ~PlatformImplementation() { + if (dxBuffer) { + dxBuffer->Release(); + dxBuffer = nullptr; + } + } + + ID3D11Buffer* dxBuffer = nullptr; + UINT size{ 0 }; + }; + + template + inline bool VertexBuffer::Initialize(std::vector const& data, xna_error_ptr_arg) { + if (!impl || !m_device || !m_device->_device || data.empty()) { + xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); + return false; + } + + const auto hr = DirectX::CreateStaticBuffer(m_device->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_VERTEX_BUFFER, &impl->dxBuffer); + + if (FAILED(hr)) { + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); + return false; + } + + impl->size = sizeof(T); + + return true; + } } #endif \ No newline at end of file diff --git a/inc/platform-dx/shaders/pixel.hlsl b/inc/platform-dx/shaders/pixel.hlsl deleted file mode 100644 index d35f77b..0000000 --- a/inc/platform-dx/shaders/pixel.hlsl +++ /dev/null @@ -1,38 +0,0 @@ -/********************************************************************************** -// Pixel (Arquivo de Sombreamento) -// -// Criação: 11 Jul 2007 -// Atualização: 13 Ago 2021 -// Compilador: D3DCompiler -// -// Descrição: Define um pixel shader que apenas multiplica a cor do objeto -// pela cor da textura, depois de fazer uma amostragem linear -// ou anisotrópica -// -**********************************************************************************/ - - -Texture2D resource; - -SamplerState linearfilter -{ - Filter = MIN_MAG_MIP_LINEAR; -}; - -SamplerState anisotropic -{ - Filter = ANISOTROPIC; - MaxAnisotropy = 4; -}; - -struct pixelIn -{ - float4 Pos : SV_POSITION; - float4 Color : COLOR; - float2 Tex : TEXCOORD; -}; - -float4 main(pixelIn pIn) : SV_TARGET -{ - return resource.Sample(linearfilter, pIn.Tex) * pIn.Color; -} \ No newline at end of file diff --git a/inc/platform-dx/shaders/vertex.hlsl b/inc/platform-dx/shaders/vertex.hlsl deleted file mode 100644 index 4786602..0000000 --- a/inc/platform-dx/shaders/vertex.hlsl +++ /dev/null @@ -1,50 +0,0 @@ -/********************************************************************************** -// Vertex (Arquivo de Sombreamento) -// -// Criação: 11 Jul 2007 -// Atualização: 13 Ago 2021 -// Compilador: D3DCompiler -// -// Descrição: Define um vertex shader que apenas multiplica os vértices -// por uma matriz de transformação e projeção -// -**********************************************************************************/ - -// matriz de transformação e projeção -cbuffer ConstantBuffer -{ - float4x4 WorldViewProj; -} - -// estrutura dos vértices de entrada -struct VertexIn -{ - float3 Pos : POSITION; - float4 Color : COLOR; - float2 Tex : TEXCOORD; -}; - -// estrutura dos vértices de saída -struct VertexOut -{ - float4 Pos : SV_POSITION; - float4 Color : COLOR; - float2 Tex : TEXCOORD; -}; - -// programa principal do vertex shader -VertexOut main(VertexIn vIn) -{ - VertexOut vOut; - - // transforma vértices para coordenadas da tela - vOut.Pos = mul(float4(vIn.Pos, 1.0f), WorldViewProj); - - // mantém as cores inalteradas - vOut.Color = vIn.Color; - - // mantém as coordenadas da textura inalteradas - vOut.Tex = vIn.Tex; - - return vOut; -} \ No newline at end of file diff --git a/inc/platform-dx/vertexbuffer-dx.hpp b/inc/platform-dx/vertexbuffer-dx.hpp deleted file mode 100644 index fdc89bb..0000000 --- a/inc/platform-dx/vertexbuffer-dx.hpp +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef XNA_PLATFORM_VERTEXBUFFER_DX_HPP -#define XNA_PLATFORM_VERTEXBUFFER_DX_HPP - -#include "../graphics/vertexbuffer.hpp" -#include "../graphics/vertexposition.hpp" -#include "dxheaders.hpp" -#include -#include -#include "device-dx.hpp" -#include "../graphics/gresource.hpp" - -namespace xna { - template - class VertexBuffer : public IVertexBuffer, public GraphicsResource { - public: - constexpr VertexBuffer(GraphicsDevice* device) : GraphicsResource(device){} - - constexpr VertexBuffer(GraphicsDevice* device, std::vector const& vertices) : data(vertices), GraphicsResource(device) {} - - virtual ~VertexBuffer() override { - if (dxBuffer) { - dxBuffer->Release(); - dxBuffer = nullptr; - } - } - - virtual bool Initialize(xna_error_ptr_arg) override { - if (!m_device || !m_device->_device || data.empty()) { - xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); - return false; - } - - const auto hr = DirectX::CreateStaticBuffer(m_device->_device, data.data(), data.size(), sizeof(T), D3D11_BIND_VERTEX_BUFFER, &dxBuffer); - - if (FAILED(hr)) { - xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); - return false; - } - - return true; - } - - virtual bool Apply(xna_error_ptr_arg) override { - if (!m_device || !m_device->_context || !dxBuffer) { - xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); - return false; - } - - UINT stride = sizeof(T); - UINT offset = 0; - m_device->_context->IASetVertexBuffers(0, 1, - &dxBuffer, &stride, &offset); - - return true; - } - - public: - ID3D11Buffer* dxBuffer = nullptr; - std::vector data; - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/xna-dx.hpp b/inc/platform-dx/xna-dx.hpp index b355340..7e7157d 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 "soundeffect-dx.hpp" -#include "vertexbuffer-dx.hpp" #include "vertexinput-dx.hpp" #include "window-dx.hpp" #include "implementations.hpp" \ No newline at end of file diff --git a/inc/xna.hpp b/inc/xna.hpp index c599a6a..bcc2452 100644 --- a/inc/xna.hpp +++ b/inc/xna.hpp @@ -45,7 +45,6 @@ #include "graphics/sprite.hpp" #include "graphics/swapchain.hpp" #include "graphics/texture.hpp" -#include "graphics/vertexbuffer.hpp" #include "graphics/vertexinput.hpp" #include "graphics/vertexposition.hpp" #include "graphics/viewport.hpp"