diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-02-14 22:31:50 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-02-18 13:27:35 +0200 |
commit | 93cfca4d4e3294e777d0654d5b7812a35a9ab064 (patch) | |
tree | 3cbd79f037cbaa882cd155bfdd33c7e5fe3b16fc | |
parent | 124173080f35d4a53e24790c0ad2cf2687d35836 (diff) | |
download | qtlocation-mapboxgl-93cfca4d4e3294e777d0654d5b7812a35a9ab064.tar.gz |
[gl] Added mbgl::gl::VAOHolder
-rw-r--r-- | src/mbgl/geometry/vao.cpp | 16 | ||||
-rw-r--r-- | src/mbgl/geometry/vao.hpp | 10 | ||||
-rw-r--r-- | src/mbgl/gl/gl_object_store.cpp | 27 | ||||
-rw-r--r-- | src/mbgl/gl/gl_object_store.hpp | 16 |
4 files changed, 38 insertions, 31 deletions
diff --git a/src/mbgl/geometry/vao.cpp b/src/mbgl/geometry/vao.cpp index db4d233887..964ac31a2f 100644 --- a/src/mbgl/geometry/vao.cpp +++ b/src/mbgl/geometry/vao.cpp @@ -11,19 +11,11 @@ void VertexArrayObject::Unbind() { MBGL_CHECK_ERROR(gl::BindVertexArray(0)); } -void VertexArrayObject::Delete(GLsizei n, const GLuint* arrays) { - if (!gl::DeleteVertexArrays) return; - MBGL_CHECK_ERROR(gl::DeleteVertexArrays(n, arrays)); -} - VertexArrayObject::VertexArrayObject() { } VertexArrayObject::~VertexArrayObject() { - if (!gl::DeleteVertexArrays) return; - if (vao) { - util::ThreadContext::getGLObjectStore()->abandonVAO(vao); - } + if (vao) util::ThreadContext::getGLObjectStore()->abandon(std::move(vao)); } void VertexArrayObject::bindVertexArrayObject() { @@ -36,10 +28,8 @@ void VertexArrayObject::bindVertexArrayObject() { return; } - if (!vao) { - MBGL_CHECK_ERROR(gl::GenVertexArrays(1, &vao)); - } - MBGL_CHECK_ERROR(gl::BindVertexArray(vao)); + if (!vao) vao.create(); + MBGL_CHECK_ERROR(gl::BindVertexArray(vao.getID())); } void VertexArrayObject::verifyBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, diff --git a/src/mbgl/geometry/vao.hpp b/src/mbgl/geometry/vao.hpp index 1b3f43806a..cab258cb5c 100644 --- a/src/mbgl/geometry/vao.hpp +++ b/src/mbgl/geometry/vao.hpp @@ -3,16 +3,18 @@ #include <mbgl/shader/shader.hpp> #include <mbgl/gl/gl.hpp> +#include <mbgl/gl/gl_object_store.hpp> #include <mbgl/util/noncopyable.hpp> #include <stdexcept> namespace mbgl { +class Shader; + class VertexArrayObject : public util::noncopyable { public: static void Unbind(); - static void Delete(GLsizei n, const GLuint* arrays); VertexArrayObject(); ~VertexArrayObject(); @@ -46,8 +48,8 @@ public: } } - inline GLuint getID() const { - return vao; + GLuint getID() const { + return vao.getID(); } private: @@ -55,7 +57,7 @@ private: void storeBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, GLbyte *offset); void verifyBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, GLbyte *offset); - GLuint vao = 0; + gl::VAOHolder vao; // For debug reasons, we're storing the bind information so that we can // detect errors and report diff --git a/src/mbgl/gl/gl_object_store.cpp b/src/mbgl/gl/gl_object_store.cpp index addf1cc972..62d92f3100 100644 --- a/src/mbgl/gl/gl_object_store.cpp +++ b/src/mbgl/gl/gl_object_store.cpp @@ -1,8 +1,5 @@ #include <mbgl/gl/gl_object_store.hpp> #include <mbgl/util/thread_context.hpp> -#include <mbgl/util/thread.hpp> -#include <mbgl/geometry/vao.hpp> -#include <mbgl/gl/gl.hpp> #include <cassert> @@ -74,10 +71,17 @@ void TexturePoolHolder::reset() { ids.fill(0); } +void VAOHolder::create() { + if (id) return; + assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); + MBGL_CHECK_ERROR(gl::GenVertexArrays(1, &id)); +} -void GLObjectStore::abandonVAO(GLuint vao) { +void VAOHolder::reset() { + if (!id) return; assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); - abandonedVAOs.emplace_back(vao); + MBGL_CHECK_ERROR(gl::DeleteVertexArrays(1, &id)); + id = 0; } void GLObjectStore::abandon(BufferHolder&& buffer) { @@ -95,18 +99,17 @@ void GLObjectStore::abandon(TexturePoolHolder&& texture) { abandonedTexturePools.push_back(std::move(texture)); } -void GLObjectStore::performCleanup() { +void GLObjectStore::abandon(VAOHolder&& vao) { assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); + abandonedVAOs.push_back(std::move(vao)); +} - if (!abandonedVAOs.empty()) { - MBGL_CHECK_ERROR(VertexArrayObject::Delete(static_cast<GLsizei>(abandonedVAOs.size()), - abandonedVAOs.data())); - abandonedVAOs.clear(); - } - +void GLObjectStore::performCleanup() { + assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); abandonedBuffers.clear(); abandonedTextures.clear(); abandonedTexturePools.clear(); + abandonedVAOs.clear(); } } // namespace gl diff --git a/src/mbgl/gl/gl_object_store.hpp b/src/mbgl/gl/gl_object_store.hpp index 6a115df0cb..684400d443 100644 --- a/src/mbgl/gl/gl_object_store.hpp +++ b/src/mbgl/gl/gl_object_store.hpp @@ -98,10 +98,22 @@ private: std::array<GLuint, TextureMax> ids; }; +class VAOHolder : public GLHolder { +public: + VAOHolder() = default; + ~VAOHolder() { reset(); } + + VAOHolder(VAOHolder&& o) noexcept : GLHolder(std::move(o)) {} + VAOHolder& operator=(VAOHolder&& o) noexcept { GLHolder::operator=(std::move(o)); return *this; } + + void create(); + void reset(); +}; + class GLObjectStore : private util::noncopyable { public: // Mark OpenGL objects for deletion - void abandonVAO(GLuint vao); + void abandon(VAOHolder&&); void abandon(BufferHolder&&); void abandon(TextureHolder&&); void abandon(TexturePoolHolder&&); @@ -113,7 +125,7 @@ public: private: // We split the holder objects in separate containers because each // GLHolder-derived object can vary in size. - std::vector<GLuint> abandonedVAOs; + std::vector<VAOHolder> abandonedVAOs; std::vector<BufferHolder> abandonedBuffers; std::vector<TextureHolder> abandonedTextures; std::vector<TexturePoolHolder> abandonedTexturePools; |