summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2017-07-10 12:19:23 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2017-07-17 09:19:02 -0700
commitcf83f117f0e3e338454ba1bc0e5a13b3b9565228 (patch)
treeb0fb18263d23349a124770cd0214689c84649d82
parentde91e68090a8b246eb161cc8dfdc5b0f40e5bdc0 (diff)
downloadqtlocation-mapboxgl-cf83f117f0e3e338454ba1bc0e5a13b3b9565228.tar.gz
[core] Reduce memory requirements of VertexArrays
-rw-r--r--src/mbgl/gl/attribute.hpp22
-rw-r--r--src/mbgl/gl/context.cpp5
-rw-r--r--src/mbgl/gl/types.hpp2
-rw-r--r--src/mbgl/gl/vertex_array.hpp17
4 files changed, 34 insertions, 12 deletions
diff --git a/src/mbgl/gl/attribute.hpp b/src/mbgl/gl/attribute.hpp
index ed4168c21f..a85b49c6a5 100644
--- a/src/mbgl/gl/attribute.hpp
+++ b/src/mbgl/gl/attribute.hpp
@@ -12,6 +12,7 @@
#include <functional>
#include <string>
#include <array>
+#include <limits>
namespace mbgl {
namespace gl {
@@ -30,12 +31,12 @@ template <> struct DataTypeOf<float> : std::integral_constant<DataType, DataT
class AttributeBinding {
public:
DataType attributeType;
- std::size_t attributeSize;
- std::size_t attributeOffset;
+ uint8_t attributeSize;
+ uint32_t attributeOffset;
BufferID vertexBuffer;
- std::size_t vertexSize;
- std::size_t vertexOffset;
+ uint32_t vertexSize;
+ uint32_t vertexOffset;
friend bool operator==(const AttributeBinding& lhs,
const AttributeBinding& rhs) {
@@ -71,20 +72,25 @@ public:
std::size_t attributeIndex,
std::size_t attributeSize = N) {
static_assert(std::is_standard_layout<Vertex>::value, "vertex type must use standard layout");
+ assert(attributeSize >= 1);
+ assert(attributeSize <= 4);
+ assert(Vertex::attributeOffsets[attributeIndex] <= std::numeric_limits<uint32_t>::max());
+ static_assert(sizeof(Vertex) <= std::numeric_limits<uint32_t>::max(), "vertex too large");
return AttributeBinding {
DataTypeOf<T>::value,
- attributeSize,
- Vertex::attributeOffsets[attributeIndex],
+ static_cast<uint8_t>(attributeSize),
+ static_cast<uint32_t>(Vertex::attributeOffsets[attributeIndex]),
buffer.buffer,
- sizeof(Vertex),
+ static_cast<uint32_t>(sizeof(Vertex)),
0,
};
}
static optional<Binding> offsetBinding(const optional<Binding>& binding, std::size_t vertexOffset) {
+ assert(vertexOffset <= std::numeric_limits<uint32_t>::max());
if (binding) {
AttributeBinding result = *binding;
- result.vertexOffset = vertexOffset;
+ result.vertexOffset = static_cast<uint32_t>(vertexOffset);
return result;
} else {
return binding;
diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp
index 2d7354a64e..19ea9a514f 100644
--- a/src/mbgl/gl/context.cpp
+++ b/src/mbgl/gl/context.cpp
@@ -233,11 +233,12 @@ VertexArray Context::createVertexArray() {
if (supportsVertexArrays()) {
VertexArrayID id = 0;
MBGL_CHECK_ERROR(vertexArray->genVertexArrays(1, &id));
- return { std::make_unique<VertexArrayState>(UniqueVertexArray(std::move(id), { this }), *this) };
+ UniqueVertexArray vao(std::move(id), { this });
+ return { UniqueVertexArrayState(new VertexArrayState(std::move(vao), *this), VertexArrayStateDeleter { true })};
} else {
// On GL implementations which do not support vertex arrays, attribute bindings are global state.
// So return a VertexArray which shares our global state tracking and whose deleter is a no-op.
- return { UniqueVertexArrayState(&globalVertexArrayState, [] (VertexArrayState*) {}) };
+ return { UniqueVertexArrayState(&globalVertexArrayState, VertexArrayStateDeleter { false }) };
}
}
diff --git a/src/mbgl/gl/types.hpp b/src/mbgl/gl/types.hpp
index c80399016e..e7d2ca449a 100644
--- a/src/mbgl/gl/types.hpp
+++ b/src/mbgl/gl/types.hpp
@@ -24,7 +24,7 @@ enum class ShaderType : uint32_t {
Fragment = 0x8B30
};
-enum class DataType : uint32_t {
+enum class DataType : uint16_t {
Byte = 0x1400,
UnsignedByte = 0x1401,
Short = 0x1402,
diff --git a/src/mbgl/gl/vertex_array.hpp b/src/mbgl/gl/vertex_array.hpp
index 9ccf48d9bd..46c67017bb 100644
--- a/src/mbgl/gl/vertex_array.hpp
+++ b/src/mbgl/gl/vertex_array.hpp
@@ -40,7 +40,22 @@ private:
}
};
-using UniqueVertexArrayState = std::unique_ptr<VertexArrayState, std::function<void (VertexArrayState*)>>;
+class VertexArrayStateDeleter {
+public:
+ VertexArrayStateDeleter(bool destroy_)
+ : destroy(destroy_) {}
+
+ void operator()(VertexArrayState* ptr) const {
+ if (destroy) {
+ delete ptr;
+ }
+ }
+
+private:
+ bool destroy;
+};
+
+using UniqueVertexArrayState = std::unique_ptr<VertexArrayState, VertexArrayStateDeleter>;
class VertexArray {
public: