[gl] Added mbgl::gl::VAOHolder
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 GLObjectStore::abandonVAO(GLuint vao) {
|
||||
void VAOHolder::create() {
|
||||
if (id) return;
|
||||
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
|
||||
abandonedVAOs.emplace_back(vao);
|
||||
MBGL_CHECK_ERROR(gl::GenVertexArrays(1, &id));
|
||||
}
|
||||
|
||||
void VAOHolder::reset() {
|
||||
if (!id) return;
|
||||
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
|
||||
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::abandon(VAOHolder&& vao) {
|
||||
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
|
||||
abandonedVAOs.push_back(std::move(vao));
|
||||
}
|
||||
|
||||
void GLObjectStore::performCleanup() {
|
||||
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
|
||||
|
||||
if (!abandonedVAOs.empty()) {
|
||||
MBGL_CHECK_ERROR(VertexArrayObject::Delete(static_cast<GLsizei>(abandonedVAOs.size()),
|
||||
abandonedVAOs.data()));
|
||||
abandonedVAOs.clear();
|
||||
}
|
||||
|
||||
abandonedBuffers.clear();
|
||||
abandonedTextures.clear();
|
||||
abandonedTexturePools.clear();
|
||||
abandonedVAOs.clear();
|
||||
}
|
||||
|
||||
} // namespace gl
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user