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);

46
src/geometry/vao.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include <mbgl/geometry/vao.hpp>
namespace mbgl {
#if GL_ARB_vertex_array_object
VertexArrayObject::~VertexArrayObject() {
if (vao) {
glDeleteVertexArrays(1, &vao);
}
}
void VertexArrayObject::bindVertexArrayObject() {
if (!vao) {
glGenVertexArrays(1, &vao);
}
glBindVertexArray(vao);
}
void VertexArrayObject::verifyBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer,
char *offset) {
if (bound_shader != shader.getID()) {
throw std::runtime_error(std::string("trying to rebind VAO to another shader from " +
std::to_string(bound_shader) + "(" + bound_shader_name + ") to " +
std::to_string(shader.getID()) + "(" + shader.name + ")" ));
} else if (bound_offset != offset) {
throw std::runtime_error("trying to bind VAO to another offset");
} else if (bound_vertex_buffer != vertexBuffer) {
throw std::runtime_error("trying to bind VAO to another vertex buffer");
} else if (bound_elements_buffer != elementsBuffer) {
throw std::runtime_error("trying to bind VAO to another elements buffer");
}
}
void VertexArrayObject::storeBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer,
char *offset) {
bound_shader = shader.getID();
bound_shader_name = shader.name;
bound_offset = offset;
bound_vertex_buffer = vertexBuffer;
bound_elements_buffer = elementsBuffer;
}
#endif
}

View File

@@ -26,3 +26,7 @@ void VertexBuffer::bind() {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
}
}
uint32_t VertexBuffer::getID() const {
return buffer;
}

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
CompositeShader::CompositeShader()
: Shader(
"composite",
shaders[COMPOSITE_SHADER].vertex,
shaders[COMPOSITE_SHADER].fragment
) {

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
DotShader::DotShader()
: Shader(
"dot",
shaders[DOT_SHADER].vertex,
shaders[DOT_SHADER].fragment
) {

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
GaussianShader::GaussianShader()
: Shader(
"gaussian",
shaders[GAUSSIAN_SHADER].vertex,
shaders[GAUSSIAN_SHADER].fragment
) {

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
IconShader::IconShader()
: Shader(
"icon",
shaders[ICON_SHADER].vertex,
shaders[ICON_SHADER].fragment
) {

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
LineShader::LineShader()
: Shader(
"line",
shaders[LINE_SHADER].vertex,
shaders[LINE_SHADER].fragment
) {

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
LinejoinShader::LinejoinShader()
: Shader(
"linejoin",
shaders[LINEJOIN_SHADER].vertex,
shaders[LINEJOIN_SHADER].fragment
) {

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
OutlineShader::OutlineShader()
: Shader(
"outline",
shaders[OUTLINE_SHADER].vertex,
shaders[OUTLINE_SHADER].fragment
) {

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
PatternShader::PatternShader()
: Shader(
"pattern",
shaders[PATTERN_SHADER].vertex,
shaders[PATTERN_SHADER].fragment
) {

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
PlainShader::PlainShader()
: Shader(
"plain",
shaders[PLAIN_SHADER].vertex,
shaders[PLAIN_SHADER].fragment
) {

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
RasterShader::RasterShader()
: Shader(
"raster",
shaders[RASTER_SHADER].vertex,
shaders[RASTER_SHADER].fragment
) {

View File

@@ -8,8 +8,9 @@
using namespace mbgl;
Shader::Shader(const GLchar *vertSource, const GLchar *fragSource)
: valid(false),
Shader::Shader(const char *name, const GLchar *vertSource, const GLchar *fragSource)
: name(name),
valid(false),
program(0) {
util::timer timer("shader compilation", Event::Shader);

View File

@@ -8,6 +8,7 @@ using namespace mbgl;
TextShader::TextShader()
: Shader(
"text",
shaders[TEXT_SHADER].vertex,
shaders[TEXT_SHADER].fragment
) {