[core] Reduce memory requirements of VertexArrays

This commit is contained in:
John Firebaugh
2017-07-10 12:19:23 -07:00
parent de91e68090
commit cf83f117f0
4 changed files with 34 additions and 12 deletions

View File

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

View File

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

View File

@@ -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,

View File

@@ -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: