Last active
October 19, 2020 05:28
-
-
Save JuanDiegoMontoya/95a4353e2cec9934b63df235d3fa4cc4 to your computer and use it in GitHub Desktop.
Simple abstraction for immutable buffers using DSA in OpenGL 4.5+.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Graphics/GraphicsIncludes.h> | |
#include <Graphics/StaticBuffer.h> | |
StaticBuffer::StaticBuffer(const void* data, GLuint size, GLbitfield glflags) | |
{ | |
glCreateBuffers(1, &rendererID_); | |
glNamedBufferStorage(rendererID_, size, data, glflags); | |
} | |
StaticBuffer::StaticBuffer(const StaticBuffer& other) | |
{ | |
glCreateBuffers(1, &rendererID_); | |
GLint size{}; | |
glGetNamedBufferParameteriv(other.rendererID_, GL_BUFFER_SIZE, &size); | |
glCopyNamedBufferSubData(other.rendererID_, rendererID_, 0, 0, size); | |
} | |
StaticBuffer::StaticBuffer(StaticBuffer&& other) noexcept | |
{ | |
rendererID_ = std::exchange(other.rendererID_, 0); | |
} | |
StaticBuffer::~StaticBuffer() | |
{ | |
glDeleteBuffers(1, &rendererID_); | |
} | |
void StaticBuffer::SubData(const void* data, GLuint size, GLuint offset) | |
{ | |
glNamedBufferSubData(rendererID_, offset, size, data); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
// general-purpose immutable buffer storage | |
class StaticBuffer | |
{ | |
public: | |
StaticBuffer(const void* data, GLuint size, GLbitfield glflags = GL_DYNAMIC_STORAGE_BIT); | |
// copies another buffer's data store and contents | |
StaticBuffer(const StaticBuffer& other); | |
StaticBuffer(StaticBuffer&& other) noexcept; | |
~StaticBuffer(); | |
StaticBuffer& operator=(const StaticBuffer&) = delete; | |
StaticBuffer& operator=(StaticBuffer&&) = delete; | |
bool operator==(const StaticBuffer&) const = default; | |
// updates a subset of the buffer's data store | |
void SubData(const void* data, GLuint size, GLuint offset = 0); | |
template<GLuint Target> | |
void Bind() const | |
{ | |
glBindBuffer(Target, rendererID_); | |
} | |
template<GLuint Target> | |
void Unbind() const | |
{ | |
glBindBuffer(Target, 0); | |
} | |
// for when this class doesn't offer enough functionality | |
// Note: constness is shallow! | |
GLuint GetID() const { return rendererID_; } | |
private: | |
GLuint rendererID_ = 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment