Skip to content

Commit

Permalink
Misc improvements to BufferLayout.cpp
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavye Mathur <[email protected]>
  • Loading branch information
BhavyeMathur committed Feb 19, 2024
1 parent c1c2a38 commit 4c88751
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 56 deletions.
Binary file modified binaries/lib-macos/libgoopylib.dylib
Binary file not shown.
9 changes: 5 additions & 4 deletions src/core/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ namespace gp {

bind();

GP_OPENGL("glBufferData(target={0}, size={1}*{2}, data={3}, ...)", GP_BUFFER_TARGET, length, m_Layout.m_Stride, data);
glBufferData(_getBufferTarget(), length * m_Layout.m_Stride, data, _getBufferUsage());
GP_OPENGL("glBufferData(target={0}, size={1}*{2}, data={3}, ...)",
GP_BUFFER_TARGET, length, m_Layout.getStride(), data);
glBufferData(_getBufferTarget(), length * m_Layout.getStride(), data, _getBufferUsage());

m_Length = length;
}
Expand All @@ -107,8 +108,8 @@ namespace gp {

bind();
GP_OPENGL("glBufferSubData(target={0}, offset={1}*{2}, size={3}*{2}, data={4})",
GP_BUFFER_TARGET, offset, m_Layout.m_Stride, length, data);
glBufferSubData(_getBufferTarget(), offset * m_Layout.m_Stride, length * m_Layout.m_Stride, data);
GP_BUFFER_TARGET, offset, m_Layout.getStride(), length, data);
glBufferSubData(_getBufferTarget(), offset * m_Layout.getStride(), length * m_Layout.getStride(), data);
}

void Buffer::setData(const std::span<int32_t> data) {
Expand Down
71 changes: 41 additions & 30 deletions src/core/BufferLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <opengl.h>

namespace gp {
GLenum shaderOpenGLType(ShaderDataType type) {
GLenum shaderOpenGLType(const ShaderDataType type) {
switch (type) {
case ShaderDataType::Float:
case ShaderDataType::Float2:
Expand All @@ -26,15 +26,15 @@ namespace gp {
return GL_BYTE;

default:
GP_CORE_ERROR("Unrecognised Shader Type");
GP_CORE_ERROR("gp::shaderOpenGLType() Unrecognised Shader Type");
return 0;
}
}
}

// Buffer Layout Element
namespace gp {
int32_t shaderTypeSize(ShaderDataType type) {
int32_t shaderTypeSize(const ShaderDataType type) {
switch (type) {
case ShaderDataType::Float:
return 4;
Expand All @@ -59,17 +59,24 @@ namespace gp {
case ShaderDataType::Bool:
return 1;
default:
GP_CORE_ERROR("Unrecognised Shader Type");
GP_CORE_ERROR("gp::shaderTypeSize() Unrecognised Shader Type");
return 0;
}
}

BufferElement::BufferElement(ShaderDataType type, const char *name, bool normalized)
: m_Name(name),
m_Type(type),
m_Normalized(normalized),
m_Size(shaderTypeSize(type)),
m_Offset(0) {
BufferElement::BufferElement(const ShaderDataType type, const char *name, const bool normalized)
: m_Name(name),
m_Type(type),
m_Size(shaderTypeSize(type)),
m_Normalized(normalized) {
}

const char *BufferElement::getName() const {
return m_Name;
}

ShaderDataType BufferElement::getDataType() const {
return m_Type;
}

int32_t BufferElement::getCount() const {
Expand Down Expand Up @@ -103,42 +110,46 @@ namespace gp {
}
}

ShaderDataType BufferElement::getDataType() const {
return m_Type;
}

bool BufferElement::isNormalised() const {
return m_Normalized;
int32_t BufferElement::getSize() const {
return m_Size;
}

size_t BufferElement::getOffset() const {
int32_t BufferElement::getOffset() const {
return m_Offset;
}

const char *BufferElement::getName() const {
return m_Name;
bool BufferElement::isNormalized() const {
return m_Normalized;
}
}

// Buffer Layout
namespace gp {
BufferLayout::BufferLayout(BufferElement *elements, int32_t count) {
GP_CORE_INFO("Initializing BufferLayout");
BufferLayout::BufferLayout(const BufferElement *elements, const int32_t count) {
GP_CORE_INFO("gp::BufferLayout::BufferLayout({0}, count={1})", reinterpret_cast<std::uintptr_t>(elements), count);

const gp::BufferElement *end = &elements[count];
const BufferElement *end = &elements[count];

for (gp::BufferElement *element = elements; element != end; element++) {
for (const BufferElement *element = elements; element != end; element++) {
m_Elements.push_back(*element);
}

calculateOffsetAndStride();
_calculateOffsetAndStride();
}

BufferLayout::BufferLayout(std::initializer_list<BufferElement> elements)
: m_Elements(elements) {
GP_CORE_INFO("Initializing BufferLayout");
BufferLayout::BufferLayout(const std::initializer_list<BufferElement> elements)
: m_Elements{elements} {
GP_CORE_INFO("gp::BufferLayout::BufferLayout(count={0})", elements.size());

_calculateOffsetAndStride();
}

int32_t BufferLayout::getStride() const {
return m_Stride;
}

calculateOffsetAndStride();
int32_t BufferLayout::getCount() const {
return m_Count;
}

std::vector<BufferElement>::iterator BufferLayout::begin() {
Expand All @@ -157,13 +168,13 @@ namespace gp {
return m_Elements.end();
}

void BufferLayout::calculateOffsetAndStride() {
void BufferLayout::_calculateOffsetAndStride() {
m_Stride = 0;
m_Count = 0;

for (auto &element: m_Elements) {
element.m_Offset = m_Stride;
m_Stride += element.m_Size;
m_Stride += element.getSize();
m_Count += element.getCount();
}
}
Expand Down
33 changes: 18 additions & 15 deletions src/core/BufferLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,40 @@ namespace gp {
public:
BufferElement(ShaderDataType type, const char *name, bool normalized = false);

[[nodiscard]] int32_t getCount() const;
[[nodiscard]] const char *getName() const;

[[nodiscard]] ShaderDataType getDataType() const;

[[nodiscard]] bool isNormalised() const;
[[nodiscard]] int32_t getCount() const;

[[nodiscard]] size_t getOffset() const;
[[nodiscard]] int32_t getSize() const;

[[nodiscard]] const char *getName() const;
[[nodiscard]] int32_t getOffset() const;

[[nodiscard]] bool isNormalized() const;

private:
const char *m_Name;
ShaderDataType m_Type;
bool m_Normalized;

int32_t m_Size = 0;
size_t m_Offset = 0;
int32_t m_Offset = 0;
bool m_Normalized;
};
}

// Buffer Layout
namespace gp {
class GPAPI BufferLayout {
friend class VertexArray;

friend class Buffer;

public:
BufferLayout(BufferElement *elements, int32_t count);
BufferLayout(const BufferElement *elements, int32_t count);

BufferLayout(std::initializer_list<BufferElement> elements);

int32_t getStride() const;

int32_t getCount() const;

[[nodiscard]] std::vector<BufferElement>::iterator begin();

[[nodiscard]] std::vector<BufferElement>::iterator end();
Expand All @@ -62,11 +64,12 @@ namespace gp {

[[nodiscard]] std::vector<BufferElement>::const_iterator end() const;

void calculateOffsetAndStride();

private:
std::vector<BufferElement> m_Elements;
int32_t m_Stride = 0;
uint32_t m_Count = 0;
int32_t m_Count = 0;

std::vector<BufferElement> m_Elements;

void _calculateOffsetAndStride();
};
}
14 changes: 7 additions & 7 deletions src/core/VertexArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ namespace gp {

GP_OPENGL(
"glVertexAttribPointer({0}, size={1}, type=GL_FLOAT, normalised={2}, stride={3}, offset={4})",
attrIndex, element.getCount(), element.isNormalised(), layout.m_Stride, element.getOffset());
attrIndex, element.getCount(), element.isNormalised(), layout.getStride(), element.getOffset());
glVertexAttribPointer(attrIndex,
element.getCount(),
GL_FLOAT,
element.isNormalised() ? GL_TRUE : GL_FALSE,
layout.m_Stride,
element.isNormalized() ? GL_TRUE : GL_FALSE,
layout.getStride(),
(const void *) element.getOffset());
attrIndex++;
break;
Expand All @@ -139,21 +139,21 @@ namespace gp {

GP_OPENGL(
"glVertexAttribIPointer({0}, size={1}, type={2}, stride={3}, offset={4})",
attrIndex, element.getCount(), shaderOpenGLType(type), layout.m_Stride, element.getOffset());
attrIndex, element.getCount(), shaderOpenGLType(type), layout.getStride(), element.getOffset());
glVertexAttribIPointer(attrIndex,
element.getCount(),
shaderOpenGLType(type),
layout.m_Stride,
layout.getStride(),
(const void *) element.getOffset());
attrIndex++;
break;
}
case ShaderDataType::Mat3:
case ShaderDataType::Mat4: {
GLenum glType = shaderOpenGLType(type);
bool normalized = element.isNormalised() ? GL_TRUE : GL_FALSE;
bool normalized = element.isNormalized() ? GL_TRUE : GL_FALSE;
int32_t count = element.getCount();
int32_t stride = layout.m_Stride;
int32_t stride = layout.getStride();

for (int32_t i = 0; i < count; i++) {
GP_OPENGL("glEnableVertexAttribArray({0})", attrIndex);
Expand Down

0 comments on commit 4c88751

Please sign in to comment.