From 0510712d6e508024c19a15a3ac8f40e1578d2cc6 Mon Sep 17 00:00:00 2001 From: Light Date: Mon, 14 Jun 2021 17:02:56 +0430 Subject: [PATCH] IndexBuffer - IndexBuffer now creates it's own indices if non is provided --- Engine/src/Engine/Graphics/Buffers.cpp | 15 +++++---- Engine/src/Engine/Graphics/Buffers.h | 4 +-- Engine/src/Engine/Graphics/Renderer.cpp | 23 ++----------- .../GraphicsAPI/DirectX/dxBuffers.cpp | 33 +++++++++++++++++-- .../Platform/GraphicsAPI/DirectX/dxBuffers.h | 4 +-- .../Platform/GraphicsAPI/OpenGL/glBuffers.cpp | 33 +++++++++++++++++-- .../Platform/GraphicsAPI/OpenGL/glBuffers.h | 4 +-- 7 files changed, 79 insertions(+), 37 deletions(-) diff --git a/Engine/src/Engine/Graphics/Buffers.cpp b/Engine/src/Engine/Graphics/Buffers.cpp index fefe474..f97cf4d 100644 --- a/Engine/src/Engine/Graphics/Buffers.cpp +++ b/Engine/src/Engine/Graphics/Buffers.cpp @@ -11,15 +11,15 @@ namespace Light { - VertexBuffer* VertexBuffer::Create(unsigned int stride, unsigned int count, float* vertices, void* sharedContext) + VertexBuffer* VertexBuffer::Create(float* vertices, unsigned int stride, unsigned int count, void* sharedContext) { switch (GraphicsContext::GetGraphicsAPI()) { case GraphicsAPI::OpenGL: - return new glVertexBuffer(count, vertices); + return new glVertexBuffer(vertices, count); case GraphicsAPI::DirectX: - return new dxVertexBuffer(count, stride, vertices, sharedContext); + return new dxVertexBuffer(vertices, stride, count, sharedContext); default: LT_ENGINE_ASSERT(false, "VertexBuffer::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); @@ -27,15 +27,16 @@ namespace Light { } } - IndexBuffer* IndexBuffer::Create(unsigned int count, unsigned int* indices, void* sharedContext) + IndexBuffer* IndexBuffer::Create(unsigned int* indices, unsigned int count, void* sharedContext) { switch (GraphicsContext::GetGraphicsAPI()) { case GraphicsAPI::OpenGL: - return new glIndexBuffer(count, indices); + return new glIndexBuffer(indices, count); + case GraphicsAPI::DirectX: LT_WIN( - return new dxIndexBuffer(count, indices, sharedContext); - ) + return new dxIndexBuffer(indices, count, sharedContext);) + default: LT_ENGINE_ASSERT(false, "IndexBuffer::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); return nullptr; diff --git a/Engine/src/Engine/Graphics/Buffers.h b/Engine/src/Engine/Graphics/Buffers.h index d4b48bf..898c5cd 100644 --- a/Engine/src/Engine/Graphics/Buffers.h +++ b/Engine/src/Engine/Graphics/Buffers.h @@ -7,7 +7,7 @@ namespace Light { class VertexBuffer { public: - static VertexBuffer* Create(unsigned int stride, unsigned int count, float* vertices, void* sharedContext); + static VertexBuffer* Create(float* vertices, unsigned int stride, unsigned int count, void* sharedContext); virtual void* Map() = 0; virtual void UnMap() = 0; @@ -22,7 +22,7 @@ namespace Light { class IndexBuffer { public: - static IndexBuffer* Create(unsigned int count, unsigned int* indices, void* sharedContext); + static IndexBuffer* Create(unsigned int* indices, unsigned int count, void* sharedContext); virtual void Bind() = 0; virtual void UnBind() = 0; diff --git a/Engine/src/Engine/Graphics/Renderer.cpp b/Engine/src/Engine/Graphics/Renderer.cpp index 61b3b16..9adf95d 100644 --- a/Engine/src/Engine/Graphics/Renderer.cpp +++ b/Engine/src/Engine/Graphics/Renderer.cpp @@ -15,27 +15,10 @@ namespace Light { s_Context = this; // QUADRENDERER // - unsigned int offset = 0; - unsigned int* indices = new unsigned int[LT_MAX_QUAD * 6]; - - for (int i = 0; i < LT_MAX_QUAD * 6; i += 6) - { - indices[i + 0] = offset + 0; - indices[i + 1] = offset + 1; - indices[i + 2] = offset + 2; - - indices[i + 3] = offset + 2; - indices[i + 4] = offset + 3; - indices[i + 5] = offset + 0; - - offset += 4; - } - m_QuadRenderer.shader = std::unique_ptr(Shader::Create("res/vertex.vertex", "res/fragment.fragment", m_SharedContext)); - m_QuadRenderer.vertexBuffer = std::unique_ptr(VertexBuffer::Create(sizeof(QuadRendererProgram::QuadVertexData), LT_MAX_QUAD * 4, nullptr, m_SharedContext)); + m_QuadRenderer.vertexBuffer = std::unique_ptr(VertexBuffer::Create(nullptr, sizeof(QuadRendererProgram::QuadVertexData), LT_MAX_QUAD * 4, m_SharedContext)); m_QuadRenderer.vertexLayout = std::unique_ptr(VertexLayout::Create(m_QuadRenderer.vertexBuffer.get(), m_QuadRenderer.shader.get(), { { "POSITION", VertexElementType::Float3 },{ "COLOR", VertexElementType::Float4 } }, m_SharedContext)); - m_QuadRenderer.indexBuffer = std::unique_ptr(IndexBuffer::Create(LT_MAX_QUAD * 6, indices, m_SharedContext)); - delete[] indices; + m_QuadRenderer.indexBuffer = std::unique_ptr(IndexBuffer::Create(nullptr, LT_MAX_QUAD * 3, m_SharedContext)); // QUADRENDERER // } @@ -60,7 +43,7 @@ namespace Light { m_QuadRenderer.Map(); } - // local + // locals const float xMin = position.x; const float yMin = position.y; const float xMax = position.x + size.x; diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.cpp index 47ba1b7..1b7fa7c 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.cpp +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.cpp @@ -5,7 +5,7 @@ namespace Light { - dxVertexBuffer::dxVertexBuffer(unsigned int count, unsigned int stride, float* vertices, void* sharedContext) + dxVertexBuffer::dxVertexBuffer(float* vertices, unsigned int stride, unsigned int count, void* sharedContext) : m_Stride(stride) { HRESULT hr; @@ -54,7 +54,7 @@ namespace Light { } - dxIndexBuffer::dxIndexBuffer(unsigned int count, unsigned int* indices, void* sharedContext) + dxIndexBuffer::dxIndexBuffer(unsigned int* indices, unsigned int count, void* sharedContext) { HRESULT hr; @@ -64,6 +64,32 @@ namespace Light { m_Device = dxContext->device; m_DeviceContext = dxContext->deviceContext; + bool hasIndices = !!indices; + if (!hasIndices) + { + if (count % 6 != 0) + { + LT_ENGINE_WARN("dxIndexBuffer::dxIndexBuffer: count should be divisible by 6 when no indices is provided"); + LT_ENGINE_WARN("dxIndexBuffer::dxIndexBuffer: adding {} to count -> {}", (6 - (count % 6)), count + (6 - (count % 6))); + count = count + (6 - (count % 6)); + } + + indices = new unsigned int[count]; + unsigned int offset = 0; + for (unsigned int i = 0; i < count; i += 6) + { + indices[i + 0] = offset + 0; + indices[i + 1] = offset + 1; + indices[i + 2] = offset + 2; + + indices[i + 3] = offset + 2; + indices[i + 4] = offset + 3; + indices[i + 5] = offset + 0; + + offset += 4; + } + } + D3D11_BUFFER_DESC bufferDesc = { 0 }; D3D11_SUBRESOURCE_DATA sd = { 0 }; bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; @@ -75,6 +101,9 @@ namespace Light { sd.pSysMem = indices; DXC(m_Device->CreateBuffer(&bufferDesc, &sd, &m_Buffer)); + + if (!hasIndices) + delete[] indices; } dxIndexBuffer::~dxIndexBuffer() diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.h index 8546fdf..b906e4a 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.h +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxBuffers.h @@ -21,7 +21,7 @@ namespace Light { unsigned int m_Stride; public: - dxVertexBuffer(unsigned int count, unsigned int stride, float* vertices, void* sharedContext); + dxVertexBuffer(float* vertices, unsigned int stride, unsigned int count, void* sharedContext); ~dxVertexBuffer(); void* Map() override; @@ -39,7 +39,7 @@ namespace Light { Microsoft::WRL::ComPtr m_Device; Microsoft::WRL::ComPtr m_DeviceContext; public: - dxIndexBuffer(unsigned int count, unsigned int* indices, void* sharedContext); + dxIndexBuffer(unsigned int* indices, unsigned int count, void* sharedContext); ~dxIndexBuffer(); void Bind() override; diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glBuffers.cpp b/Engine/src/Platform/GraphicsAPI/OpenGL/glBuffers.cpp index dd140fe..584114a 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glBuffers.cpp +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glBuffers.cpp @@ -5,7 +5,7 @@ namespace Light { - glVertexBuffer::glVertexBuffer(unsigned int count, float* vertices) + glVertexBuffer::glVertexBuffer(float* vertices, unsigned int count) { glCreateBuffers(1, &m_BufferID); glNamedBufferData(m_BufferID, count * sizeof(float), vertices, GL_DYNAMIC_DRAW); @@ -36,10 +36,39 @@ namespace Light { glBindBuffer(GL_ARRAY_BUFFER, NULL); } - glIndexBuffer::glIndexBuffer(unsigned int count, unsigned int* indices) + glIndexBuffer::glIndexBuffer(unsigned int* indices, unsigned int count) { + bool hasIndices = !!indices; + if (!hasIndices) + { + if (count % 6 != 0) + { + LT_ENGINE_WARN("glIndexBuffer::glIndexBuffer: count should be divisible by 6 when no indices is provided"); + LT_ENGINE_WARN("glIndexBuffer::glIndexBuffer: adding {} to count -> {}", (6 - (count % 6)), count + (6 - (count % 6))); + count = count + (6 - (count % 6)); + } + + indices = new unsigned int[count]; + unsigned int offset = 0; + for (unsigned int i = 0; i < count; i += 6) + { + indices[i + 0] = offset + 0; + indices[i + 1] = offset + 1; + indices[i + 2] = offset + 2; + + indices[i + 3] = offset + 2; + indices[i + 4] = offset + 3; + indices[i + 5] = offset + 0; + + offset += 4; + } + } + glCreateBuffers(1, &m_BufferID); glNamedBufferData(m_BufferID, count * sizeof(unsigned int), indices, GL_STATIC_DRAW); + + if (!hasIndices) + delete[] indices; } glIndexBuffer::~glIndexBuffer() diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glBuffers.h b/Engine/src/Platform/GraphicsAPI/OpenGL/glBuffers.h index 7307e9a..7673766 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glBuffers.h +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glBuffers.h @@ -11,7 +11,7 @@ namespace Light { unsigned int m_BufferID; public: - glVertexBuffer(unsigned int count, float* vertices); + glVertexBuffer(float* vertices, unsigned int count); ~glVertexBuffer(); void* Map() override; @@ -27,7 +27,7 @@ namespace Light { unsigned int m_BufferID; public: - glIndexBuffer(unsigned int count, unsigned int* indices); + glIndexBuffer(unsigned int* indices, unsigned int count); ~glIndexBuffer(); void Bind() override;