From 28dae3df603ceadba8b61c22112f4f8918a68a29 Mon Sep 17 00:00:00 2001 From: Danilo Date: Thu, 11 Apr 2024 10:38:56 -0300 Subject: [PATCH] Implementa Vertex e IndexBuffer --- framework/CMakeLists.txt | 2 +- framework/common/color.hpp | 18 ++++++++ framework/forward.hpp | 6 +++ framework/graphics/constbuffer.hpp | 0 framework/graphics/indexbuffer.hpp | 14 ++++++ framework/graphics/vertexbuffer.hpp | 14 ++++++ framework/graphics/vertexposition.hpp | 61 ++++++++++++++++++++++++++ framework/platform/dxheaders.hpp | 3 +- framework/platform/indexbuffer-dx.cpp | 47 ++++++++++++++++++++ framework/platform/indexbuffer-dx.hpp | 40 +++++++++++++++++ framework/platform/vertexbuffer-dx.cpp | 29 ++++++++++++ framework/platform/vertexbuffer-dx.hpp | 43 ++++++++++++++++++ framework/xnaerror.hpp | 1 + 13 files changed, 276 insertions(+), 2 deletions(-) create mode 100644 framework/common/color.hpp create mode 100644 framework/graphics/constbuffer.hpp create mode 100644 framework/graphics/indexbuffer.hpp create mode 100644 framework/graphics/vertexbuffer.hpp create mode 100644 framework/graphics/vertexposition.hpp create mode 100644 framework/platform/indexbuffer-dx.cpp create mode 100644 framework/platform/indexbuffer-dx.hpp create mode 100644 framework/platform/vertexbuffer-dx.cpp create mode 100644 framework/platform/vertexbuffer-dx.hpp diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index e290be2..8e41e60 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -3,7 +3,7 @@ # # Add source to this project's executable. -add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizer-dx.cpp") +add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizer-dx.cpp" "platform/vertexbuffer-dx.cpp" "platform/indexbuffer-dx.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET xna PROPERTY CXX_STANDARD 20) diff --git a/framework/common/color.hpp b/framework/common/color.hpp new file mode 100644 index 0000000..cba2c67 --- /dev/null +++ b/framework/common/color.hpp @@ -0,0 +1,18 @@ +#ifndef XNA_COMMON_COLOR_HPP +#define XNA_COMMON_COLOR_HPP + +#include "../default.hpp" + +namespace xna { + struct Color { + constexpr Color() = default; + + Uint packedValue{0}; + + constexpr bool operator==(const Color& other) const { + return packedValue == other.packedValue; + } + }; +} + +#endif \ No newline at end of file diff --git a/framework/forward.hpp b/framework/forward.hpp index d9a2353..37ffd85 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -79,6 +79,8 @@ namespace xna { //Graphics class BlendState; using PBlendState = std::shared_ptr; + class ConstantBuffer; + using PConstantBuffer = std::shared_ptr; class DisplayMode; using PDisplayMode = std::shared_ptr; class DisplayModeCollection; @@ -103,8 +105,12 @@ namespace xna { using PRasterizerState = std::shared_ptr; class Shader; using PShader = std::shared_ptr; + class VertexBuffer; + using PVertexBuffer = std::shared_ptr; class VertexInputLayout; using PVertexInputLayout = std::shared_ptr; + struct VertexPositionColor; + using PVertexPositionColor = std::shared_ptr; class VertexShader; using pVertexShader = std::shared_ptr; struct Viewport; diff --git a/framework/graphics/constbuffer.hpp b/framework/graphics/constbuffer.hpp new file mode 100644 index 0000000..e69de29 diff --git a/framework/graphics/indexbuffer.hpp b/framework/graphics/indexbuffer.hpp new file mode 100644 index 0000000..342bd96 --- /dev/null +++ b/framework/graphics/indexbuffer.hpp @@ -0,0 +1,14 @@ +#ifndef XNA_GRAPHICS_INDEXBUFFER_HPP +#define XNA_GRAPHICS_INDEXBUFFER_HPP + +#include "../default.hpp" + +namespace xna { + class IIndexBuffer { + public: + virtual ~IIndexBuffer() {} + virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) = 0; + }; +} + +#endif \ No newline at end of file diff --git a/framework/graphics/vertexbuffer.hpp b/framework/graphics/vertexbuffer.hpp new file mode 100644 index 0000000..01f8d6b --- /dev/null +++ b/framework/graphics/vertexbuffer.hpp @@ -0,0 +1,14 @@ +#ifndef XNA_GRAPHICS_VERTEXBUFFER_HPP +#define XNA_GRAPHICS_VERTEXBUFFER_HPP + +#include "../default.hpp" + +namespace xna { + class IVertexBuffer { + public: + virtual ~IVertexBuffer(){} + virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) = 0; + }; +} + +#endif \ No newline at end of file diff --git a/framework/graphics/vertexposition.hpp b/framework/graphics/vertexposition.hpp new file mode 100644 index 0000000..c6c301e --- /dev/null +++ b/framework/graphics/vertexposition.hpp @@ -0,0 +1,61 @@ +#ifndef XNA_GRAPHICS_VERTEXPOSITION_HPP +#define XNA_GRAPHICS_VERTEXPOSITION_HPP + +#include "../common/vectors.hpp" +#include "../common/color.hpp" +#include "../default.hpp" + +namespace xna { + struct VertexPositionColor { + Vector3 position{}; + Color color{}; + + constexpr VertexPositionColor() = default; + + constexpr bool operator==(const VertexPositionColor& other) const { + return position == other.position && color == other.color; + } + }; + + struct VertexPositionTexture { + Vector3 position{}; + Vector2 textureCoordinate{}; + + constexpr VertexPositionTexture() = default; + + constexpr bool operator==(const VertexPositionTexture& other) const { + return position == other.position + && textureCoordinate == other.textureCoordinate; + } + }; + + struct VertexPositionColorTexture { + Vector3 position{}; + Vector2 textureCoodinate{}; + Color color{}; + + constexpr VertexPositionColorTexture() = default; + + constexpr bool operator==(const VertexPositionColorTexture& other) const { + return position == other.position + && textureCoodinate == other.textureCoodinate + && color == other.color; + } + }; + + struct VertexPositionNormalTexture { + Vector3 position{}; + Vector3 normal{}; + Vector2 textureCoodinate{}; + + constexpr VertexPositionNormalTexture() = default; + + bool operator==(const VertexPositionNormalTexture& other) const { + return position == other.position + && normal == other.normal + && textureCoodinate == other.textureCoodinate; + } + }; +} + +#endif \ No newline at end of file diff --git a/framework/platform/dxheaders.hpp b/framework/platform/dxheaders.hpp index 133be21..02ce37a 100644 --- a/framework/platform/dxheaders.hpp +++ b/framework/platform/dxheaders.hpp @@ -1,3 +1,4 @@ #include "dxgi.h" #include "d3d11.h" -#include \ No newline at end of file +#include +#include \ No newline at end of file diff --git a/framework/platform/indexbuffer-dx.cpp b/framework/platform/indexbuffer-dx.cpp new file mode 100644 index 0000000..529f423 --- /dev/null +++ b/framework/platform/indexbuffer-dx.cpp @@ -0,0 +1,47 @@ +#include "indexbuffer-dx.hpp" +#include "device-dx.hpp" + +namespace xna { + bool IndexBuffer::Initialize(GraphicsDevice& device, xna_error_ptr_arg) + { + if (!device._device) { + xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL); + return false; + } + + if (_buffer) { + _buffer->Release(); + _buffer = nullptr; + } + + //TODO: refatorar isso aqui + int IndicesPerSprite = 4; + int MaxBatchSize = 4096; + int VerticesPerSprite = 4; + + std::vector indices; + indices.reserve(MaxBatchSize * IndicesPerSprite); + + for (short i = 0; i < (MaxBatchSize * VerticesPerSprite); i += VerticesPerSprite) + { + indices.push_back(i); + indices.push_back(i + 1); + indices.push_back(i + 2); + + indices.push_back(i + 1); + indices.push_back(i + 3); + indices.push_back(i + 2); + } + + _initialData.pSysMem = &indices.front(); + + const auto hr = device._device->CreateBuffer(&_description, &_initialData, &_buffer); + + if (FAILED(hr)) { + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/framework/platform/indexbuffer-dx.hpp b/framework/platform/indexbuffer-dx.hpp new file mode 100644 index 0000000..299f6ce --- /dev/null +++ b/framework/platform/indexbuffer-dx.hpp @@ -0,0 +1,40 @@ +#ifndef XNA_PLATFORM_INDEXBUFFER_DX_HPP +#define XNA_PLATFORM_INDEXBUFFER_DX_HPP + +#include "../graphics/indexbuffer.hpp" +#include "dxheaders.hpp" + +namespace xna { + class IndexBuffer : public IIndexBuffer { + public: + IndexBuffer() { + _description.Usage = D3D11_USAGE_DEFAULT; + _description.BindFlags = D3D11_BIND_INDEX_BUFFER; + + } + + IndexBuffer(size_t size) { + _description.ByteWidth = static_cast(size); + _description.Usage = D3D11_USAGE_DEFAULT; + _description.BindFlags = D3D11_BIND_INDEX_BUFFER; + } + + IndexBuffer(D3D11_BUFFER_DESC desc) : _description(desc){} + + virtual ~IndexBuffer() override { + if (_buffer) { + _buffer->Release(); + _buffer = nullptr; + } + } + + virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override; + + public: + D3D11_BUFFER_DESC _description; + ID3D11Buffer* _buffer = nullptr; + D3D11_SUBRESOURCE_DATA _initialData{} + }; +} + +#endif \ No newline at end of file diff --git a/framework/platform/vertexbuffer-dx.cpp b/framework/platform/vertexbuffer-dx.cpp new file mode 100644 index 0000000..fd37f62 --- /dev/null +++ b/framework/platform/vertexbuffer-dx.cpp @@ -0,0 +1,29 @@ +#include "vertexbuffer-dx.hpp" +#include "device-dx.hpp" + +namespace xna { + bool VertexBuffer::Initialize(GraphicsDevice& device, xna_error_ptr_arg) + { + if (!device._device) { + xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL); + return false; + } + + if (_buffer) { + _buffer->Release(); + _buffer = nullptr; + } + + const auto hr = device._device->CreateBuffer( + &_description, + _useInitialData ? &_initialData : nullptr, + &_buffer); + + if (FAILED(hr)) { + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/framework/platform/vertexbuffer-dx.hpp b/framework/platform/vertexbuffer-dx.hpp new file mode 100644 index 0000000..b906905 --- /dev/null +++ b/framework/platform/vertexbuffer-dx.hpp @@ -0,0 +1,43 @@ +#ifndef XNA_PLATFORM_VERTEXBUFFER_DX_HPP +#define XNA_PLATFORM_VERTEXBUFFER_DX_HPP + +#include "../graphics/vertexbuffer.hpp" +#include "dxheaders.hpp" +#include "../graphics/vertexposition.hpp" + +namespace xna { + class VertexBuffer : public IVertexBuffer { + public: + VertexBuffer() { + _description.Usage = D3D11_USAGE_DYNAMIC; + _description.BindFlags = D3D11_BIND_VERTEX_BUFFER; + _description.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + } + + VertexBuffer(size_t size) { + _description.ByteWidth = static_cast(size); + _description.Usage = D3D11_USAGE_DYNAMIC; + _description.BindFlags = D3D11_BIND_VERTEX_BUFFER; + _description.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + } + + VertexBuffer(D3D11_BUFFER_DESC desc) : _description(desc){} + + virtual ~VertexBuffer() override { + if (_buffer) { + _buffer->Release(); + _buffer = nullptr; + } + } + + virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override; + + public: + D3D11_BUFFER_DESC _description{}; + D3D11_SUBRESOURCE_DATA _initialData{}; + bool _useInitialData{ false }; + ID3D11Buffer* _buffer = nullptr; + }; +} + +#endif \ No newline at end of file diff --git a/framework/xnaerror.hpp b/framework/xnaerror.hpp index 437df37..ea401c9 100644 --- a/framework/xnaerror.hpp +++ b/framework/xnaerror.hpp @@ -7,6 +7,7 @@ namespace xna { ARGUMENT_OUT_OF_RANGE, ARGUMENT_IS_NULL, INVALID_OPERATION, + FAILED_OPERATION, OVERFLOW_OPERATION, NULL_CAST, BAD_CAST,