refactor vao object to use acutal OpenGL IDs rather than object pointers

This commit is contained in:
Konstantin Käfer
2014-08-15 13:37:19 +02:00
parent a81c2efba2
commit ba67fcd830
18 changed files with 123 additions and 77 deletions

View File

@@ -62,6 +62,10 @@ public:
}
}
inline GLuint getID() const {
return buffer;
}
protected:
// increase the buffer size by at least /required/ bytes.
inline void *addElement() {
@@ -72,7 +76,7 @@ protected:
while (length < pos + itemSize) length += defaultLength;
array = realloc(array, length);
if (array == nullptr) {
throw std::runtime_error("Buffer reallocation failed¯");
throw std::runtime_error("Buffer reallocation failed");
}
}
pos += itemSize;
@@ -106,7 +110,7 @@ private:
size_t length = 0;
// GL buffer ID
uint32_t buffer = 0;
GLuint buffer = 0;
};
}

View File

@@ -1,104 +1,74 @@
#ifndef MBGL_GEOMETRY_VAO
#define MBGL_GEOMETRY_VAO
#include <mbgl/shader/shader.hpp>
#include <mbgl/platform/gl.hpp>
#include <stdexcept>
namespace mbgl {
#if GL_ARB_vertex_array_object
class VertexArrayObject {
public:
template <typename Shader, typename VertexBuffer>
void bind(Shader& shader, VertexBuffer& vertex_buffer, char *offset) {
#if GL_ARB_vertex_array_object
if (!vao) {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
} else {
// We have been given the correct information.
glBindVertexArray(vao);
}
if (shader_ptr != &shader) {
if (shader_ptr != nullptr) {
throw std::runtime_error("shader rebind!");
}
#endif
vertex_buffer.bind();
inline void bind(Shader& shader, VertexBuffer &vertexBuffer, char *offset) {
bindVertexArrayObject();
if (bound_shader == 0) {
vertexBuffer.bind();
shader.bind(offset);
#if GL_ARB_vertex_array_object
shader_ptr = &shader;
vertex_buffer_ptr = &vertex_buffer;
elements_buffer_ptr = nullptr;
offset_ptr = offset;
} else if (vertex_buffer_ptr != &vertex_buffer) {
throw std::runtime_error("trying to bind VAO to another vertex buffer");
} else if (elements_buffer_ptr != nullptr) {
throw std::runtime_error("trying to bind VAO to another elements buffer");
} else if (offset_ptr != offset) {
throw std::runtime_error("trying to bind VAO to another offset");
storeBinding(shader, vertexBuffer.getID(), 0, offset);
} else {
verifyBinding(shader, vertexBuffer.getID(), 0, offset);
}
#endif
}
template <typename Shader, typename VertexBuffer, typename ElementsBuffer>
void bind(Shader& shader, VertexBuffer& vertex_buffer, ElementsBuffer& elements_buffer, char *offset) {
#if GL_ARB_vertex_array_object
if (!vao) {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
} else {
// We have been given the correct information.
glBindVertexArray(vao);
}
if (shader_ptr != &shader) {
if (shader_ptr != nullptr) {
throw std::runtime_error("shader rebind!");
}
#endif
vertex_buffer.bind();
elements_buffer.bind();
inline void bind(Shader& shader, VertexBuffer &vertexBuffer, ElementsBuffer &elementsBuffer, char *offset) {
bindVertexArrayObject();
if (bound_shader == 0) {
vertexBuffer.bind();
elementsBuffer.bind();
shader.bind(offset);
#if GL_ARB_vertex_array_object
shader_ptr = &shader;
vertex_buffer_ptr = &vertex_buffer;
elements_buffer_ptr = &elements_buffer;
offset_ptr = offset;
} else if (vertex_buffer_ptr != &vertex_buffer) {
throw std::runtime_error("trying to bind VAO to another vertex buffer");
} else if (elements_buffer_ptr != &elements_buffer) {
throw std::runtime_error("trying to bind VAO to another elements buffer");
} else if (offset_ptr != offset) {
throw std::runtime_error("trying to bind VAO to another offset");
storeBinding(shader, vertexBuffer.getID(), elementsBuffer.getID(), offset);
} else {
verifyBinding(shader, vertexBuffer.getID(), elementsBuffer.getID(), offset);
}
#endif
}
~VertexArrayObject() {
#if GL_ARB_vertex_array_object
if (vao) {
glDeleteVertexArrays(1, &vao);
}
#endif
}
~VertexArrayObject();
private:
#if GL_ARB_vertex_array_object
void bindVertexArrayObject();
void storeBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, char *offset);
void verifyBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, char *offset);
GLuint vao = 0;
// For debug reasons, we're storing the bind information so that we can
// detect errors and report
void *shader_ptr = nullptr;
void *vertex_buffer_ptr = nullptr;
void *elements_buffer_ptr = nullptr;
char *offset_ptr = 0;
#endif
GLuint bound_shader = 0;
const char *bound_shader_name = "";
GLuint bound_vertex_buffer = 0;
GLuint bound_elements_buffer = 0;
char *bound_offset = 0;
};
#else
class VertexArrayObject {
public:
template <typename Shader, typename Buffers>
void bind(Shader& shader, Buffers& buffers, char *offset) {
for (auto &buffer : buffers) {
buffer.bind();
}
shader.bind(offset);
}
};
#endif
}
#endif

View File

@@ -25,6 +25,11 @@ public:
*/
void bind();
/*
* Returns the OpenGL ID of the buffer object.
*/
uint32_t getID() const;
private:
const std::vector<vertex_type> array;
uint32_t buffer = 0;

View File

@@ -9,13 +9,18 @@ namespace mbgl {
class Shader : private util::noncopyable {
public:
Shader(const char *vertex, const char *fragment);
Shader(const char *name, const char *vertex, const char *fragment);
~Shader();
const char *name;
bool valid;
uint32_t program;
void setMatrix(const std::array<float, 16>& matrix);
inline uint32_t getID() const {
return program;
}
private:
bool compileShader(uint32_t *shader, uint32_t type, const char *source);