summaryrefslogtreecommitdiff
path: root/src/mbgl/gl
diff options
context:
space:
mode:
authorMolly Lloyd <molly@mapbox.com>2017-03-01 11:28:31 -0800
committerJohn Firebaugh <john.firebaugh@gmail.com>2017-03-08 15:23:25 -0800
commit692f5e0a9f5321ee2932a976a8eb9e0a83fc3352 (patch)
treecb9ff13dc30455f1700eeb691dc155d0981c4116 /src/mbgl/gl
parent3afae825c7b2f95e58cbcb85c59857f2253c945e (diff)
downloadqtlocation-mapboxgl-692f5e0a9f5321ee2932a976a8eb9e0a83fc3352.tar.gz
Pack min + max into one attribute :muscle:
Some devices supported by Mapbox GL provide only 8 vertex attributes; this change packs existing attributes to get us just under that limit. For properties using a composite function, pack the min and max values into a single attribute with two logical components instead of using two separate attributes and buffers. Special logic is included for color attributes, whose integer components must be packed into the available bits of floating-point attributes. (We don't have access to ivec types in GL ES 2.0.) For source functions, continue to bind just a one-component attribute even though the GLSL type is vec2 (or vec4 for colors). The type-checking done by gl::Attribute is relaxed slightly to accommodate this.
Diffstat (limited to 'src/mbgl/gl')
-rw-r--r--src/mbgl/gl/attribute.cpp2
-rw-r--r--src/mbgl/gl/attribute.hpp46
2 files changed, 35 insertions, 13 deletions
diff --git a/src/mbgl/gl/attribute.cpp b/src/mbgl/gl/attribute.cpp
index 2c16dac3fc..ff313fdcd0 100644
--- a/src/mbgl/gl/attribute.cpp
+++ b/src/mbgl/gl/attribute.cpp
@@ -38,7 +38,7 @@ void VariableAttributeBinding<T, N>::bind(Context& context,
MBGL_CHECK_ERROR(glEnableVertexAttribArray(location));
MBGL_CHECK_ERROR(glVertexAttribPointer(
location,
- static_cast<GLint>(N),
+ static_cast<GLint>(attributeSize),
static_cast<GLenum>(DataTypeOf<T>),
static_cast<GLboolean>(IsNormalized<T>),
static_cast<GLsizei>(vertexSize),
diff --git a/src/mbgl/gl/attribute.hpp b/src/mbgl/gl/attribute.hpp
index 6300ebb56b..43e2c2d794 100644
--- a/src/mbgl/gl/attribute.hpp
+++ b/src/mbgl/gl/attribute.hpp
@@ -17,10 +17,12 @@ class VariableAttributeBinding {
public:
VariableAttributeBinding(BufferID vertexBuffer_,
std::size_t vertexSize_,
- std::size_t attributeOffset_)
+ std::size_t attributeOffset_,
+ std::size_t attributeSize_ = N)
: vertexBuffer(vertexBuffer_),
vertexSize(vertexSize_),
- attributeOffset(attributeOffset_)
+ attributeOffset(attributeOffset_),
+ attributeSize(attributeSize_)
{}
void bind(Context&, AttributeLocation, optional<VariableAttributeBinding<T, N>>&, std::size_t vertexOffset) const;
@@ -29,13 +31,15 @@ public:
const VariableAttributeBinding& rhs) {
return lhs.vertexBuffer == rhs.vertexBuffer
&& lhs.vertexSize == rhs.vertexSize
- && lhs.attributeOffset == rhs.attributeOffset;
+ && lhs.attributeOffset == rhs.attributeOffset
+ && lhs.attributeSize == rhs.attributeSize;
}
private:
BufferID vertexBuffer;
std::size_t vertexSize;
std::size_t attributeOffset;
+ std::size_t attributeSize;
};
template <class T, std::size_t N>
@@ -58,9 +62,16 @@ private:
std::array<T, N> value;
};
+/*
+ gl::Attribute<T,N> manages the binding of a constant value or vertex buffer to a GL program attribute.
+ - T is the underlying primitive type (exposed as Attribute<T,N>::ValueType)
+ - N is the number of components in the attribute declared in the shader (exposed as Attribute<T,N>::Dimensions)
+*/
template <class T, std::size_t N>
class Attribute {
public:
+ using ValueType = T;
+ static constexpr size_t Dimensions = N;
using Value = std::array<T, N>;
using VariableBinding = VariableAttributeBinding<T, N>;
@@ -72,6 +83,25 @@ public:
ConstantBinding,
VariableBinding>;
+ /*
+ Create a variable (i.e. data-driven) binding for this attribute. The `attributeSize`
+ parameter may be used to override the number of components available in the buffer for
+ each vertex. Thus, a buffer with only one float for each vertex can be bound to a
+ `vec2` attribute
+ */
+ template <class Vertex, class DrawMode>
+ static VariableBinding variableBinding(const VertexBuffer<Vertex, DrawMode>& buffer,
+ std::size_t attributeIndex,
+ std::size_t attributeSize = N) {
+ static_assert(std::is_standard_layout<Vertex>::value, "vertex type must use standard layout");
+ return VariableBinding {
+ buffer.buffer,
+ sizeof(Vertex),
+ Vertex::attributeOffsets[attributeIndex],
+ attributeSize
+ };
+ }
+
static void bind(Context& context,
const Location& location,
optional<VariableBinding>& oldBinding,
@@ -223,15 +253,7 @@ public:
template <class DrawMode>
static Bindings allVariableBindings(const VertexBuffer<Vertex, DrawMode>& buffer) {
- static_assert(std::is_standard_layout<Vertex>::value, "vertex type must use standard layout");
-
- return Bindings {
- typename As::VariableBinding {
- buffer.buffer,
- sizeof(Vertex),
- Vertex::attributeOffsets[Index<As>]
- }...
- };
+ return Bindings { As::variableBinding(buffer, Index<As>)... };
}
static void bind(Context& context,