[core] Return to static, per-segment approach to VAOs

This is safer now -- it can be an implementation detail of Segment/Context. And the typing of SegmentVector now ensures that the attributes and program match.
This commit is contained in:
John Firebaugh
2016-11-14 11:57:02 -08:00
parent 377d0fb0d2
commit 9d2be04461
3 changed files with 10 additions and 42 deletions

View File

@@ -6,7 +6,7 @@
#include <mbgl/util/std.hpp>
#include <mbgl/platform/log.hpp>
#include <boost/functional/hash.hpp>
#include <cstring>
namespace mbgl {
namespace gl {
@@ -434,15 +434,6 @@ PrimitiveType Context::operator()(const TriangleStrip&) {
return PrimitiveType::TriangleStrip;
}
std::size_t Context::VertexArrayObjectHash::operator()(const VertexArrayObjectKey& key) const {
std::size_t seed = 0;
boost::hash_combine(seed, std::get<0>(key));
boost::hash_combine(seed, std::get<1>(key));
boost::hash_combine(seed, std::get<2>(key));
boost::hash_combine(seed, std::get<3>(key));
return seed;
}
void Context::setDepthMode(const DepthMode& depth) {
if (depth.func == DepthMode::Always && !depth.mask) {
depthTest = false;
@@ -503,23 +494,15 @@ void Context::draw(const Drawable& drawable) {
return true;
}
VertexArrayObjectKey vaoKey {
drawable.program,
drawable.vertexBuffer,
drawable.indexBuffer,
segment.vertexOffset
};
auto it = vaos.find(vaoKey);
if (it != vaos.end()) {
vertexArrayObject = it->second;
if (segment.vao) {
vertexArrayObject = *segment.vao;
return false;
}
VertexArrayID id = 0;
MBGL_CHECK_ERROR(gl::GenVertexArrays(1, &id));
vertexArrayObject = id;
vaos.emplace(vaoKey, UniqueVertexArray(std::move(id), { this }));
segment.vao = UniqueVertexArray(std::move(id), { this });
// If we are initializing a new VAO, we need to force the buffers
// to be rebound. VAOs don't inherit the existing buffer bindings.
@@ -555,9 +538,6 @@ void Context::performCleanup() {
if (program == id) {
program.setDirty();
}
mbgl::util::erase_if(vaos, [&] (const VertexArrayObjectMap::value_type& kv) {
return std::get<0>(kv.first) == id;
});
MBGL_CHECK_ERROR(glDeleteProgram(id));
}
abandonedPrograms.clear();
@@ -574,10 +554,6 @@ void Context::performCleanup() {
} else if (elementBuffer == id) {
elementBuffer.setDirty();
}
mbgl::util::erase_if(vaos, [&] (const VertexArrayObjectMap::value_type& kv) {
return std::get<1>(kv.first) == id
|| std::get<2>(kv.first) == id;
});
}
MBGL_CHECK_ERROR(glDeleteBuffers(int(abandonedBuffers.size()), abandonedBuffers.data()));
abandonedBuffers.clear();

View File

@@ -233,20 +233,6 @@ private:
std::vector<VertexArrayID> abandonedVertexArrays;
std::vector<FramebufferID> abandonedFramebuffers;
std::vector<RenderbufferID> abandonedRenderbuffers;
using VertexArrayObjectKey = std::tuple<
ProgramID, // Program
BufferID, // Vertex buffer
BufferID, // Index buffer
std::size_t // Vertex buffer offset
>;
struct VertexArrayObjectHash {
std::size_t operator()(const VertexArrayObjectKey&) const;
};
using VertexArrayObjectMap = std::unordered_map<VertexArrayObjectKey, UniqueVertexArray, VertexArrayObjectHash>;
VertexArrayObjectMap vaos;
};
} // namespace gl

View File

@@ -1,5 +1,7 @@
#pragma once
#include <mbgl/util/optional.hpp>
#include <cstddef>
namespace mbgl {
@@ -21,6 +23,10 @@ public:
std::size_t vertexLength;
std::size_t indexLength;
private:
friend class Context;
mutable optional<UniqueVertexArray> vao;
};
template <class Attributes>