summaryrefslogtreecommitdiff
path: root/src/mbgl/gl
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/gl')
-rw-r--r--src/mbgl/gl/attribute.hpp42
-rw-r--r--src/mbgl/gl/context.cpp8
-rw-r--r--src/mbgl/gl/types.hpp10
-rw-r--r--src/mbgl/gl/value.cpp93
-rw-r--r--src/mbgl/gl/vertex_buffer.hpp5
5 files changed, 100 insertions, 58 deletions
diff --git a/src/mbgl/gl/attribute.hpp b/src/mbgl/gl/attribute.hpp
index 95a9942381..53ba71a89f 100644
--- a/src/mbgl/gl/attribute.hpp
+++ b/src/mbgl/gl/attribute.hpp
@@ -18,29 +18,17 @@
namespace mbgl {
namespace gl {
-template <class> struct DataTypeOf;
-template <> struct DataTypeOf< int8_t> : std::integral_constant<DataType, DataType::Byte> {};
-template <> struct DataTypeOf<uint8_t> : std::integral_constant<DataType, DataType::UnsignedByte> {};
-template <> struct DataTypeOf< int16_t> : std::integral_constant<DataType, DataType::Short> {};
-template <> struct DataTypeOf<uint16_t> : std::integral_constant<DataType, DataType::UnsignedShort> {};
-template <> struct DataTypeOf< int32_t> : std::integral_constant<DataType, DataType::Integer> {};
-template <> struct DataTypeOf<uint32_t> : std::integral_constant<DataType, DataType::UnsignedInteger> {};
-template <> struct DataTypeOf<float> : std::integral_constant<DataType, DataType::Float> {};
-
class AttributeBinding {
public:
- DataType attributeType;
- uint8_t attributeSize;
- uint32_t attributeOffset;
-
+ gfx::AttributeDescriptor attribute;
+ uint8_t vertexStride;
BufferID vertexBuffer;
- uint32_t vertexSize;
uint32_t vertexOffset;
friend bool operator==(const AttributeBinding& lhs,
const AttributeBinding& rhs) {
- return std::tie(lhs.attributeType, lhs.attributeSize, lhs.attributeOffset, lhs.vertexBuffer, lhs.vertexSize, lhs.vertexOffset)
- == std::tie(rhs.attributeType, rhs.attributeSize, rhs.attributeOffset, rhs.vertexBuffer, rhs.vertexSize, rhs.vertexOffset);
+ return std::tie(lhs.attribute, lhs.vertexStride, lhs.vertexBuffer, lhs.vertexOffset)
+ == std::tie(rhs.attribute, rhs.vertexStride, rhs.vertexBuffer, rhs.vertexOffset);
}
};
@@ -51,21 +39,13 @@ using AttributeBindingArray = std::vector<optional<AttributeBinding>>;
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 AttributeType, class Vertex>
-AttributeBinding attributeBinding(const VertexBuffer<Vertex>& buffer,
- std::size_t attributeIndex,
- std::size_t attributeSize = AttributeType::Dimensions) {
- 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<typename AttributeType::ValueType>::value,
- static_cast<uint8_t>(attributeSize),
- static_cast<uint32_t>(Vertex::attributeOffsets[attributeIndex]),
+template <std::size_t I, typename Vertex>
+AttributeBinding attributeBinding(const VertexBuffer<Vertex>& buffer) {
+ static_assert(I < gfx::VertexDescriptorOf<Vertex>::data.count, "vertex attribute index out of range");
+ return {
+ gfx::VertexDescriptorOf<Vertex>::data.attributes[I],
+ gfx::VertexDescriptorOf<Vertex>::data.stride,
buffer.buffer,
- static_cast<uint32_t>(sizeof(Vertex)),
0,
};
}
@@ -127,7 +107,7 @@ public:
}
static Bindings bindings(const VertexBuffer<gfx::Vertex<Types>>& buffer) {
- return Bindings { attributeBinding<typename As::Type>(buffer, TypeIndex<As, As...>::value)... };
+ return Bindings { attributeBinding<TypeIndex<As, As...>::value>(buffer)... };
}
static Bindings offsetBindings(const Bindings& bindings, std::size_t vertexOffset) {
diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp
index bcbb3ab66c..72d59737f3 100644
--- a/src/mbgl/gl/context.cpp
+++ b/src/mbgl/gl/context.cpp
@@ -17,14 +17,6 @@ using namespace platform;
static_assert(underlying_type(ShaderType::Vertex) == GL_VERTEX_SHADER, "OpenGL type mismatch");
static_assert(underlying_type(ShaderType::Fragment) == GL_FRAGMENT_SHADER, "OpenGL type mismatch");
-static_assert(underlying_type(DataType::Byte) == GL_BYTE, "OpenGL type mismatch");
-static_assert(underlying_type(DataType::UnsignedByte) == GL_UNSIGNED_BYTE, "OpenGL type mismatch");
-static_assert(underlying_type(DataType::Short) == GL_SHORT, "OpenGL type mismatch");
-static_assert(underlying_type(DataType::UnsignedShort) == GL_UNSIGNED_SHORT, "OpenGL type mismatch");
-static_assert(underlying_type(DataType::Integer) == GL_INT, "OpenGL type mismatch");
-static_assert(underlying_type(DataType::UnsignedInteger) == GL_UNSIGNED_INT, "OpenGL type mismatch");
-static_assert(underlying_type(DataType::Float) == GL_FLOAT, "OpenGL type mismatch");
-
#if not MBGL_USE_GLES2
static_assert(underlying_type(RenderbufferType::RGBA) == GL_RGBA8, "OpenGL type mismatch");
#else
diff --git a/src/mbgl/gl/types.hpp b/src/mbgl/gl/types.hpp
index 4c3033454f..22b7098100 100644
--- a/src/mbgl/gl/types.hpp
+++ b/src/mbgl/gl/types.hpp
@@ -32,16 +32,6 @@ enum class ShaderType : uint32_t {
Fragment = 0x8B30
};
-enum class DataType : uint16_t {
- Byte = 0x1400,
- UnsignedByte = 0x1401,
- Short = 0x1402,
- UnsignedShort = 0x1403,
- Integer = 0x1404,
- UnsignedInteger = 0x1405,
- Float = 0x1406
-};
-
enum class RenderbufferType : uint32_t {
RGBA = 0x8058,
DepthStencil = 0x88F0,
diff --git a/src/mbgl/gl/value.cpp b/src/mbgl/gl/value.cpp
index 4e56686103..b290cde50c 100644
--- a/src/mbgl/gl/value.cpp
+++ b/src/mbgl/gl/value.cpp
@@ -398,17 +398,102 @@ BindVertexArray::Type BindVertexArray::Get(const Context& context) {
const optional<AttributeBinding> VertexAttribute::Default {};
+namespace {
+
+GLenum vertexType(const gfx::AttributeDataType type) {
+ switch (type) {
+ case gfx::AttributeDataType::Byte:
+ case gfx::AttributeDataType::Byte2:
+ case gfx::AttributeDataType::Byte3:
+ case gfx::AttributeDataType::Byte4:
+ return GL_BYTE;
+ case gfx::AttributeDataType::UByte:
+ case gfx::AttributeDataType::UByte2:
+ case gfx::AttributeDataType::UByte3:
+ case gfx::AttributeDataType::UByte4:
+ return GL_UNSIGNED_BYTE;
+ case gfx::AttributeDataType::Short:
+ case gfx::AttributeDataType::Short2:
+ case gfx::AttributeDataType::Short3:
+ case gfx::AttributeDataType::Short4:
+ return GL_SHORT;
+ case gfx::AttributeDataType::UShort:
+ case gfx::AttributeDataType::UShort2:
+ case gfx::AttributeDataType::UShort3:
+ case gfx::AttributeDataType::UShort4:
+ return GL_UNSIGNED_SHORT;
+ case gfx::AttributeDataType::Int:
+ case gfx::AttributeDataType::Int2:
+ case gfx::AttributeDataType::Int3:
+ case gfx::AttributeDataType::Int4:
+ return GL_INT;
+ case gfx::AttributeDataType::UInt:
+ case gfx::AttributeDataType::UInt2:
+ case gfx::AttributeDataType::UInt3:
+ case gfx::AttributeDataType::UInt4:
+ return GL_UNSIGNED_INT;
+ case gfx::AttributeDataType::Float:
+ case gfx::AttributeDataType::Float2:
+ case gfx::AttributeDataType::Float3:
+ case gfx::AttributeDataType::Float4:
+ return GL_FLOAT;
+ default:
+ return GL_FLOAT;
+ }
+}
+
+GLint components(const gfx::AttributeDataType type) {
+ switch (type) {
+ case gfx::AttributeDataType::Byte:
+ case gfx::AttributeDataType::UByte:
+ case gfx::AttributeDataType::Short:
+ case gfx::AttributeDataType::UShort:
+ case gfx::AttributeDataType::Int:
+ case gfx::AttributeDataType::UInt:
+ case gfx::AttributeDataType::Float:
+ return 1;
+ case gfx::AttributeDataType::Byte2:
+ case gfx::AttributeDataType::UByte2:
+ case gfx::AttributeDataType::Short2:
+ case gfx::AttributeDataType::UShort2:
+ case gfx::AttributeDataType::Int2:
+ case gfx::AttributeDataType::UInt2:
+ case gfx::AttributeDataType::Float2:
+ return 2;
+ case gfx::AttributeDataType::Byte3:
+ case gfx::AttributeDataType::UByte3:
+ case gfx::AttributeDataType::Short3:
+ case gfx::AttributeDataType::UShort3:
+ case gfx::AttributeDataType::Int3:
+ case gfx::AttributeDataType::UInt3:
+ case gfx::AttributeDataType::Float3:
+ return 3;
+ case gfx::AttributeDataType::Byte4:
+ case gfx::AttributeDataType::UByte4:
+ case gfx::AttributeDataType::Short4:
+ case gfx::AttributeDataType::UShort4:
+ case gfx::AttributeDataType::Int4:
+ case gfx::AttributeDataType::UInt4:
+ case gfx::AttributeDataType::Float4:
+ return 4;
+ default:
+ return 0;
+ }
+}
+
+} // namespace
+
void VertexAttribute::Set(const optional<AttributeBinding>& binding, Context& context, AttributeLocation location) {
if (binding) {
context.vertexBuffer = binding->vertexBuffer;
MBGL_CHECK_ERROR(glEnableVertexAttribArray(location));
MBGL_CHECK_ERROR(glVertexAttribPointer(
location,
- static_cast<GLint>(binding->attributeSize),
- static_cast<GLenum>(binding->attributeType),
+ components(binding->attribute.dataType),
+ vertexType(binding->attribute.dataType),
static_cast<GLboolean>(false),
- static_cast<GLsizei>(binding->vertexSize),
- reinterpret_cast<GLvoid*>(binding->attributeOffset + (binding->vertexSize * binding->vertexOffset))));
+ static_cast<GLsizei>(binding->vertexStride),
+ reinterpret_cast<GLvoid*>(binding->attribute.offset + (binding->vertexStride * binding->vertexOffset))));
} else {
MBGL_CHECK_ERROR(glDisableVertexAttribArray(location));
}
diff --git a/src/mbgl/gl/vertex_buffer.hpp b/src/mbgl/gl/vertex_buffer.hpp
index 0b62b86c12..b65e7f9a01 100644
--- a/src/mbgl/gl/vertex_buffer.hpp
+++ b/src/mbgl/gl/vertex_buffer.hpp
@@ -2,17 +2,12 @@
#include <mbgl/gl/object.hpp>
-#include <vector>
-
namespace mbgl {
namespace gl {
template <class V>
class VertexBuffer {
public:
- using Vertex = V;
- static constexpr std::size_t vertexSize = sizeof(Vertex);
-
std::size_t vertexCount;
UniqueBuffer buffer;
};