mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-07 06:35:40 +12:00
Add UBO support to opengl.hpp
This commit is contained in:
parent
2cf4563533
commit
85a17c3fcd
1 changed files with 68 additions and 26 deletions
94
third_party/opengl/opengl.hpp
vendored
94
third_party/opengl/opengl.hpp
vendored
|
@ -430,36 +430,36 @@ namespace OpenGL {
|
||||||
glDispatchCompute(groupsX, groupsY, groupsZ);
|
glDispatchCompute(groupsX, groupsY, groupsZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VertexBuffer {
|
struct VertexBuffer {
|
||||||
GLuint m_handle = 0;
|
GLuint m_handle = 0;
|
||||||
|
|
||||||
void create() {
|
void create() {
|
||||||
if (m_handle == 0) {
|
if (m_handle == 0) {
|
||||||
glGenBuffers(1, &m_handle);
|
glGenBuffers(1, &m_handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void createFixedSize(GLsizei size, GLenum usage = GL_DYNAMIC_DRAW) {
|
void createFixedSize(GLsizei size, GLenum usage = GL_DYNAMIC_DRAW) {
|
||||||
create();
|
create();
|
||||||
bind();
|
bind();
|
||||||
glBufferData(GL_ARRAY_BUFFER, size, nullptr, usage);
|
glBufferData(GL_ARRAY_BUFFER, size, nullptr, usage);
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexBuffer(bool shouldCreate = false) {
|
VertexBuffer(bool shouldCreate = false) {
|
||||||
if (shouldCreate) {
|
if (shouldCreate) {
|
||||||
create();
|
create();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef OPENGL_DESTRUCTORS
|
#ifdef OPENGL_DESTRUCTORS
|
||||||
~VertexBuffer() { free(); }
|
~VertexBuffer() { free(); }
|
||||||
#endif
|
#endif
|
||||||
GLuint handle() const { return m_handle; }
|
GLuint handle() const { return m_handle; }
|
||||||
bool exists() const { return m_handle != 0; }
|
bool exists() const { return m_handle != 0; }
|
||||||
void bind() const { glBindBuffer(GL_ARRAY_BUFFER, m_handle); }
|
void bind() const { glBindBuffer(GL_ARRAY_BUFFER, m_handle); }
|
||||||
void free() { glDeleteBuffers(1, &m_handle); }
|
void free() { glDeleteBuffers(1, &m_handle); }
|
||||||
|
|
||||||
// Reallocates the buffer on every call. Prefer the sub version if possible.
|
// Reallocates the buffer on every call. Prefer the sub version if possible.
|
||||||
template <typename VertType>
|
template <typename VertType>
|
||||||
void bufferVerts(VertType* vertices, int vertCount, GLenum usage = GL_DYNAMIC_DRAW) {
|
void bufferVerts(VertType* vertices, int vertCount, GLenum usage = GL_DYNAMIC_DRAW) {
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(VertType) * vertCount, vertices, usage);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(VertType) * vertCount, vertices, usage);
|
||||||
|
@ -471,7 +471,7 @@ namespace OpenGL {
|
||||||
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(VertType) * vertCount, vertices);
|
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(VertType) * vertCount, vertices);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If C++20 is available, add overloads that take std::span instead of raw pointers
|
// If C++20 is available, add overloads that take std::span instead of raw pointers
|
||||||
#ifdef OPENGL_HAVE_CPP20
|
#ifdef OPENGL_HAVE_CPP20
|
||||||
template <typename VertType>
|
template <typename VertType>
|
||||||
void bufferVerts(std::span<const VertType> vertices, GLenum usage = GL_DYNAMIC_DRAW) {
|
void bufferVerts(std::span<const VertType> vertices, GLenum usage = GL_DYNAMIC_DRAW) {
|
||||||
|
@ -485,6 +485,48 @@ namespace OpenGL {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct UniformBuffer {
|
||||||
|
GLuint m_handle = 0;
|
||||||
|
|
||||||
|
void create() {
|
||||||
|
if (m_handle == 0) {
|
||||||
|
glGenBuffers(1, &m_handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void createFixedSize(GLsizei size, GLenum usage = GL_DYNAMIC_DRAW) {
|
||||||
|
create();
|
||||||
|
bind();
|
||||||
|
glBufferData(GL_UNIFORM_BUFFER, size, nullptr, usage);
|
||||||
|
}
|
||||||
|
|
||||||
|
UniformBuffer(bool shouldCreate = false) {
|
||||||
|
if (shouldCreate) {
|
||||||
|
create();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef OPENGL_DESTRUCTORS
|
||||||
|
~UniformBuffer() { free(); }
|
||||||
|
#endif
|
||||||
|
GLuint handle() const { return m_handle; }
|
||||||
|
bool exists() const { return m_handle != 0; }
|
||||||
|
void bind() const { glBindBuffer(GL_UNIFORM_BUFFER, m_handle); }
|
||||||
|
void free() { glDeleteBuffers(1, &m_handle); }
|
||||||
|
|
||||||
|
// Reallocates the buffer on every call. Prefer the sub version if possible.
|
||||||
|
template <typename UniformType>
|
||||||
|
void buffer(const UniformType& uniformData, GLenum usage = GL_DYNAMIC_DRAW) {
|
||||||
|
glBufferData(GL_UNIFORM_BUFFER, sizeof(uniformData), &uniformData, usage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only use if you used createFixedSize
|
||||||
|
template <typename UniformType>
|
||||||
|
void bufferSub(const UniformType& uniformData, int vertCount, GLintptr offset = 0) {
|
||||||
|
glBufferSubData(GL_UNIFORM_BUFFER, offset, sizeof(uniformData), &uniformData);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
enum DepthFunc {
|
enum DepthFunc {
|
||||||
Never = GL_NEVER, // Depth test never passes
|
Never = GL_NEVER, // Depth test never passes
|
||||||
Always = GL_ALWAYS, // Depth test always passes
|
Always = GL_ALWAYS, // Depth test always passes
|
||||||
|
@ -693,4 +735,4 @@ namespace OpenGL {
|
||||||
|
|
||||||
using Rect = Rectangle<GLuint>;
|
using Rect = Rectangle<GLuint>;
|
||||||
|
|
||||||
} // end namespace OpenGL
|
} // end namespace OpenGL
|
||||||
|
|
Loading…
Add table
Reference in a new issue