IndexBuffer

- IndexBuffer now creates it's own
      indices if non is provided
This commit is contained in:
Light 2021-06-14 17:02:56 +04:30
parent cd9747ccfe
commit 0510712d6e
7 changed files with 79 additions and 37 deletions

View file

@ -11,15 +11,15 @@
namespace Light { 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()) switch (GraphicsContext::GetGraphicsAPI())
{ {
case GraphicsAPI::OpenGL: case GraphicsAPI::OpenGL:
return new glVertexBuffer(count, vertices); return new glVertexBuffer(vertices, count);
case GraphicsAPI::DirectX: case GraphicsAPI::DirectX:
return new dxVertexBuffer(count, stride, vertices, sharedContext); return new dxVertexBuffer(vertices, stride, count, sharedContext);
default: default:
LT_ENGINE_ASSERT(false, "VertexBuffer::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); 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()) switch (GraphicsContext::GetGraphicsAPI())
{ {
case GraphicsAPI::OpenGL: case GraphicsAPI::OpenGL:
return new glIndexBuffer(count, indices); return new glIndexBuffer(indices, count);
case GraphicsAPI::DirectX: LT_WIN( case GraphicsAPI::DirectX: LT_WIN(
return new dxIndexBuffer(count, indices, sharedContext); return new dxIndexBuffer(indices, count, sharedContext);)
)
default: default:
LT_ENGINE_ASSERT(false, "IndexBuffer::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); LT_ENGINE_ASSERT(false, "IndexBuffer::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());
return nullptr; return nullptr;

View file

@ -7,7 +7,7 @@ namespace Light {
class VertexBuffer class VertexBuffer
{ {
public: 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* Map() = 0;
virtual void UnMap() = 0; virtual void UnMap() = 0;
@ -22,7 +22,7 @@ namespace Light {
class IndexBuffer class IndexBuffer
{ {
public: 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 Bind() = 0;
virtual void UnBind() = 0; virtual void UnBind() = 0;

View file

@ -15,27 +15,10 @@ namespace Light {
s_Context = this; s_Context = this;
// QUADRENDERER // // 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>(Shader::Create("res/vertex.vertex", "res/fragment.fragment", m_SharedContext)); m_QuadRenderer.shader = std::unique_ptr<Shader>(Shader::Create("res/vertex.vertex", "res/fragment.fragment", m_SharedContext));
m_QuadRenderer.vertexBuffer = std::unique_ptr<VertexBuffer>(VertexBuffer::Create(sizeof(QuadRendererProgram::QuadVertexData), LT_MAX_QUAD * 4, nullptr, m_SharedContext)); m_QuadRenderer.vertexBuffer = std::unique_ptr<VertexBuffer>(VertexBuffer::Create(nullptr, sizeof(QuadRendererProgram::QuadVertexData), LT_MAX_QUAD * 4, m_SharedContext));
m_QuadRenderer.vertexLayout = std::unique_ptr<VertexLayout>(VertexLayout::Create(m_QuadRenderer.vertexBuffer.get(), m_QuadRenderer.shader.get(), { { "POSITION", VertexElementType::Float3 },{ "COLOR", VertexElementType::Float4 } }, m_SharedContext)); m_QuadRenderer.vertexLayout = std::unique_ptr<VertexLayout>(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>(IndexBuffer::Create(LT_MAX_QUAD * 6, indices, m_SharedContext)); m_QuadRenderer.indexBuffer = std::unique_ptr<IndexBuffer>(IndexBuffer::Create(nullptr, LT_MAX_QUAD * 3, m_SharedContext));
delete[] indices;
// QUADRENDERER // // QUADRENDERER //
} }
@ -60,7 +43,7 @@ namespace Light {
m_QuadRenderer.Map(); m_QuadRenderer.Map();
} }
// local // locals
const float xMin = position.x; const float xMin = position.x;
const float yMin = position.y; const float yMin = position.y;
const float xMax = position.x + size.x; const float xMax = position.x + size.x;

View file

@ -5,7 +5,7 @@
namespace Light { 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) : m_Stride(stride)
{ {
HRESULT hr; 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; HRESULT hr;
@ -64,6 +64,32 @@ namespace Light {
m_Device = dxContext->device; m_Device = dxContext->device;
m_DeviceContext = dxContext->deviceContext; 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_BUFFER_DESC bufferDesc = { 0 };
D3D11_SUBRESOURCE_DATA sd = { 0 }; D3D11_SUBRESOURCE_DATA sd = { 0 };
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
@ -75,6 +101,9 @@ namespace Light {
sd.pSysMem = indices; sd.pSysMem = indices;
DXC(m_Device->CreateBuffer(&bufferDesc, &sd, &m_Buffer)); DXC(m_Device->CreateBuffer(&bufferDesc, &sd, &m_Buffer));
if (!hasIndices)
delete[] indices;
} }
dxIndexBuffer::~dxIndexBuffer() dxIndexBuffer::~dxIndexBuffer()

View file

@ -21,7 +21,7 @@ namespace Light {
unsigned int m_Stride; unsigned int m_Stride;
public: public:
dxVertexBuffer(unsigned int count, unsigned int stride, float* vertices, void* sharedContext); dxVertexBuffer(float* vertices, unsigned int stride, unsigned int count, void* sharedContext);
~dxVertexBuffer(); ~dxVertexBuffer();
void* Map() override; void* Map() override;
@ -39,7 +39,7 @@ namespace Light {
Microsoft::WRL::ComPtr<ID3D11Device> m_Device; Microsoft::WRL::ComPtr<ID3D11Device> m_Device;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_DeviceContext; Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_DeviceContext;
public: public:
dxIndexBuffer(unsigned int count, unsigned int* indices, void* sharedContext); dxIndexBuffer(unsigned int* indices, unsigned int count, void* sharedContext);
~dxIndexBuffer(); ~dxIndexBuffer();
void Bind() override; void Bind() override;

View file

@ -5,7 +5,7 @@
namespace Light { namespace Light {
glVertexBuffer::glVertexBuffer(unsigned int count, float* vertices) glVertexBuffer::glVertexBuffer(float* vertices, unsigned int count)
{ {
glCreateBuffers(1, &m_BufferID); glCreateBuffers(1, &m_BufferID);
glNamedBufferData(m_BufferID, count * sizeof(float), vertices, GL_DYNAMIC_DRAW); glNamedBufferData(m_BufferID, count * sizeof(float), vertices, GL_DYNAMIC_DRAW);
@ -36,10 +36,39 @@ namespace Light {
glBindBuffer(GL_ARRAY_BUFFER, NULL); 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); glCreateBuffers(1, &m_BufferID);
glNamedBufferData(m_BufferID, count * sizeof(unsigned int), indices, GL_STATIC_DRAW); glNamedBufferData(m_BufferID, count * sizeof(unsigned int), indices, GL_STATIC_DRAW);
if (!hasIndices)
delete[] indices;
} }
glIndexBuffer::~glIndexBuffer() glIndexBuffer::~glIndexBuffer()

View file

@ -11,7 +11,7 @@ namespace Light {
unsigned int m_BufferID; unsigned int m_BufferID;
public: public:
glVertexBuffer(unsigned int count, float* vertices); glVertexBuffer(float* vertices, unsigned int count);
~glVertexBuffer(); ~glVertexBuffer();
void* Map() override; void* Map() override;
@ -27,7 +27,7 @@ namespace Light {
unsigned int m_BufferID; unsigned int m_BufferID;
public: public:
glIndexBuffer(unsigned int count, unsigned int* indices); glIndexBuffer(unsigned int* indices, unsigned int count);
~glIndexBuffer(); ~glIndexBuffer();
void Bind() override; void Bind() override;