Skip to content

Commit

Permalink
[NOX-76] add GLBuffer unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rafal Maziejuk <[email protected]>
  • Loading branch information
rafalmaziejuk committed Jun 7, 2024
1 parent b76983a commit eafecd0
Show file tree
Hide file tree
Showing 15 changed files with 205 additions and 204 deletions.
1 change: 1 addition & 0 deletions include/nox/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <nox/format.h>

#include <cstdint>
#include <vector>

namespace nox {

Expand Down
2 changes: 0 additions & 2 deletions include/nox/format.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <vector>

namespace nox {

enum class ImageFormat {
Expand Down
1 change: 1 addition & 0 deletions include/nox/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class NOX_EXPORT Texture {

[[nodiscard]] virtual TextureType getType() const = 0;
[[nodiscard]] virtual ImageFormat getFormat() const = 0;
[[nodiscard]] virtual Vector3D<uint32_t> getSize() const = 0;

public:
Texture(const Texture &) = delete;
Expand Down
61 changes: 30 additions & 31 deletions src/opengl/gl_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include "opengl/gl_vertex_array.h"

#include <glad/gl.h>

#include <algorithm>
#include "gl_buffer.h"

namespace nox {

Expand All @@ -32,60 +31,60 @@ GLenum mapVertexAttributeTypeToEnum(VertexAttributeType type) {
default: break;
}

NOX_ASSERT(false);
return GL_NONE;
}

} // namespace

bool GLBuffer::validateInput(const BufferDescriptor &descriptor) {
bool result = true;

result &= (descriptor.size > 0u);
result &= (descriptor.data != nullptr);

return result;
}

GLBuffer::GLBuffer(const BufferDescriptor &descriptor) {
auto flags = mapBufferUsageToBitfield(descriptor.usage);
glCreateBuffers(1, &m_handle);
glNamedBufferStorage(m_handle, descriptor.size, descriptor.data, flags);

constexpr auto immutableStorageBuffer = true;
if constexpr (immutableStorageBuffer) {
allocateImmutableStorage(descriptor.size, descriptor.data, flags);
}
}

GLBuffer::~GLBuffer() {
glDeleteBuffers(1, &m_handle);
}

bool GLVertexBuffer::validateInput(const VertexBufferDescriptor &descriptor) {
bool result = true;
void GLBuffer::allocateImmutableStorage(uint32_t size, const void *data, uint32_t flags) {
glNamedBufferStorage(m_handle, size, data, flags);
}

result &= GLBuffer::validateInput(descriptor);
result &= (!descriptor.vertexAttributes.empty());
std::unique_ptr<GLVertexBuffer> GLVertexBuffer::create(const VertexBufferDescriptor &descriptor, GLVertexArrayRegistry &registry) {
auto buffer = std::make_unique<GLVertexBuffer>(descriptor, registry);
NOX_ENSURE_RETURN_NULLPTR(buffer->getHandle() != 0u);

return result;
return buffer;
}

GLVertexBuffer::~GLVertexBuffer() {
auto &vertexArrayRegistry = GLVertexArrayRegistry::instance();
vertexArrayRegistry.unregisterVertexArray(m_vertexArrayIndex);
GLVertexBuffer::GLVertexBuffer(const VertexBufferDescriptor &descriptor, GLVertexArrayRegistry &registry)
: GLBuffer{descriptor},
m_rVertexArrayRegistry{registry},
m_vertexAttributes{descriptor.vertexAttributes} {
m_rVertexArrayRegistry.registerVertexArray(m_vertexAttributes);
auto &vertexArray = registry.getVertexArray(m_vertexAttributes);
vertexArray.setVertexBuffer(getHandle());
}

void GLVertexBuffer::setVertexArrayIndex(uint32_t index) {
m_vertexArrayIndex = index;
GLVertexBuffer::~GLVertexBuffer() {
m_rVertexArrayRegistry.unregisterVertexArray(m_vertexAttributes);
}

bool GLIndexBuffer::validateInput(const IndexBufferDescriptor &descriptor) {
bool result = true;

result &= GLBuffer::validateInput(descriptor);
std::unique_ptr<GLIndexBuffer> GLIndexBuffer::create(const IndexBufferDescriptor &descriptor) {
auto buffer = std::make_unique<GLIndexBuffer>(descriptor);
NOX_ENSURE_RETURN_NULLPTR(buffer->getHandle() != 0u);

return result;
return buffer;
}

void GLIndexBuffer::setIndexType(VertexAttributeFormat format) {
auto descriptor = getVertexAttributeFormatDescriptor(format);
m_indexType = mapVertexAttributeTypeToEnum(descriptor.vertexAttributeType);
GLIndexBuffer::GLIndexBuffer(const IndexBufferDescriptor &descriptor)
: GLBuffer{descriptor} {
auto formatDescriptor = getVertexAttributeFormatDescriptor(descriptor.format);
m_indexType = mapVertexAttributeTypeToEnum(formatDescriptor.vertexAttributeType);
}

} // namespace nox
26 changes: 14 additions & 12 deletions src/opengl/gl_buffer.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#include "opengl/gl_state.h"

#include <nox/buffer.h>

#include <memory>

namespace nox {

class GLVertexArrayRegistry;

class GLBuffer : public Buffer {
public:
explicit GLBuffer(const BufferDescriptor &descriptor);
Expand All @@ -14,35 +16,35 @@ class GLBuffer : public Buffer {
[[nodiscard]] uint32_t getHandle() const { return m_handle; }

protected:
[[nodiscard]] static bool validateInput(const BufferDescriptor &descriptor);
void allocateImmutableStorage(uint32_t size, const void *data, uint32_t flags);

private:
uint32_t m_handle{0u};
};

class GLVertexBuffer final : public GLBuffer {
public:
[[nodiscard]] static bool validateInput(const VertexBufferDescriptor &descriptor);
[[nodiscard]] static std::unique_ptr<GLVertexBuffer> create(const VertexBufferDescriptor &descriptor,
GLVertexArrayRegistry &registry);

using GLBuffer::GLBuffer;
GLVertexBuffer(const VertexBufferDescriptor &descriptor, GLVertexArrayRegistry &registry);
~GLVertexBuffer() override;

void setVertexArrayIndex(uint32_t index);
[[nodiscard]] uint32_t getVertexArrayIndex() const {
return m_vertexArrayIndex;
[[nodiscard]] const VertexAttributes &getVertexAttributes() const {
return m_vertexAttributes;
}

private:
uint32_t m_vertexArrayIndex{0u};
GLVertexArrayRegistry &m_rVertexArrayRegistry;
VertexAttributes m_vertexAttributes;
};

class GLIndexBuffer final : public GLBuffer {
public:
[[nodiscard]] static bool validateInput(const IndexBufferDescriptor &descriptor);
[[nodiscard]] static std::unique_ptr<GLIndexBuffer> create(const IndexBufferDescriptor &descriptor);

using GLBuffer::GLBuffer;
explicit GLIndexBuffer(const IndexBufferDescriptor &descriptor);

void setIndexType(VertexAttributeFormat format);
[[nodiscard]] uint32_t getIndexType() const {
return m_indexType;
}
Expand Down
14 changes: 6 additions & 8 deletions src/opengl/gl_command_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,21 @@ void GLCommandList::bindGraphicsPipelineState(const GraphicsPipelineState &pipel

void GLCommandList::bindVertexBuffer(const Buffer &buffer) {
const auto *vertexBuffer = static_cast<const GLVertexBuffer *>(&buffer);
auto vertexArrayIndex = vertexBuffer->getVertexArrayIndex();
const auto &vertexAttributes = vertexBuffer->getVertexAttributes();

auto &vertexArrayRegistry = GLVertexArrayRegistry::instance();
vertexArrayRegistry.setBoundVertexArrayIndex(vertexArrayIndex);
auto &registry = GLVertexArrayRegistry::instance();
registry.setBoundVertexArray(vertexAttributes);

const auto &vertexArray = vertexArrayRegistry.getVertexArray(vertexArrayIndex);
const auto &vertexArray = registry.getBoundVertexArray();
vertexArray.bind();
}

void GLCommandList::bindIndexBuffer(const Buffer &buffer) {
const auto *indexBuffer = static_cast<const GLIndexBuffer *>(&buffer);
m_state.indexType = indexBuffer->getIndexType();

auto &vertexArrayRegistry = GLVertexArrayRegistry::instance();
auto vertexArrayIndex = vertexArrayRegistry.getBoundVertexArrayIndex();

auto &vertexArray = vertexArrayRegistry.getVertexArray(vertexArrayIndex);
auto &registry = GLVertexArrayRegistry::instance();
auto &vertexArray = registry.getBoundVertexArray();
vertexArray.setIndexBuffer(indexBuffer->getHandle());
}

Expand Down
19 changes: 5 additions & 14 deletions src/opengl/gl_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,22 @@ std::unique_ptr<Swapchain> GLRenderer::createSwapchain(const SwapchainDescriptor
auto context = GLContext::create(descriptor.surfaceDescriptor);
NOX_ENSURE_RETURN_NULLPTR_MSG(context != nullptr, "Couldn't create context");

auto swapchain = GLSwapchain::create(descriptor, std::move(context));
auto swapchain = GLSwapchain::create(descriptor, std::move(context), GLVertexArrayRegistry::instance());
NOX_ENSURE_RETURN_NULLPTR_MSG(swapchain != nullptr, "Couldn't create swapchain");

return swapchain;
}

std::unique_ptr<Buffer> GLRenderer::createVertexBuffer(const VertexBufferDescriptor &descriptor) {
NOX_ASSERT(GLVertexBuffer::validateInput(descriptor));

auto &vertexArrayRegistry = GLVertexArrayRegistry::instance();
auto vertexArrayIndex = vertexArrayRegistry.registerVertexArray(descriptor.vertexAttributes);
auto &vertexArray = vertexArrayRegistry.getVertexArray(vertexArrayIndex);

auto buffer = std::make_unique<GLVertexBuffer>(descriptor);
buffer->setVertexArrayIndex(vertexArrayIndex);
vertexArray.setVertexBuffer(buffer->getHandle());
auto buffer = GLVertexBuffer::create(descriptor, GLVertexArrayRegistry::instance());
NOX_ENSURE_RETURN_NULLPTR_MSG(buffer != nullptr, "Couldn't create vertex buffer");

return buffer;
}

std::unique_ptr<Buffer> GLRenderer::createIndexBuffer(const IndexBufferDescriptor &descriptor) {
NOX_ASSERT(GLIndexBuffer::validateInput(descriptor));

auto buffer = std::make_unique<GLIndexBuffer>(descriptor);
buffer->setIndexType(descriptor.format);
auto buffer = GLIndexBuffer::create(descriptor);
NOX_ENSURE_RETURN_NULLPTR_MSG(buffer != nullptr, "Couldn't create index buffer");

return buffer;
}
Expand Down
Loading

0 comments on commit eafecd0

Please sign in to comment.