diff options
Diffstat (limited to 'src/mbgl/gfx/attribute.hpp')
-rw-r--r-- | src/mbgl/gfx/attribute.hpp | 115 |
1 files changed, 80 insertions, 35 deletions
diff --git a/src/mbgl/gfx/attribute.hpp b/src/mbgl/gfx/attribute.hpp index ec34a69a9a..2f33d3e484 100644 --- a/src/mbgl/gfx/attribute.hpp +++ b/src/mbgl/gfx/attribute.hpp @@ -1,5 +1,6 @@ #pragma once +#include <mbgl/gfx/types.hpp> #include <mbgl/util/type_list.hpp> #include <mbgl/util/indexed_tuple.hpp> @@ -20,18 +21,62 @@ namespace mbgl { namespace gfx { +namespace { + +template <typename, std::size_t> constexpr AttributeDataType AttributeDataTypeOf = AttributeDataType::Invalid; +template <> constexpr AttributeDataType AttributeDataTypeOf<int8_t, 1> = AttributeDataType::Byte; +template <> constexpr AttributeDataType AttributeDataTypeOf<int8_t, 2> = AttributeDataType::Byte2; +template <> constexpr AttributeDataType AttributeDataTypeOf<int8_t, 3> = AttributeDataType::Byte3; +template <> constexpr AttributeDataType AttributeDataTypeOf<int8_t, 4> = AttributeDataType::Byte4; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint8_t, 1> = AttributeDataType::UByte; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint8_t, 2> = AttributeDataType::UByte2; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint8_t, 3> = AttributeDataType::UByte3; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint8_t, 4> = AttributeDataType::UByte4; +template <> constexpr AttributeDataType AttributeDataTypeOf<int16_t, 1> = AttributeDataType::Short; +template <> constexpr AttributeDataType AttributeDataTypeOf<int16_t, 2> = AttributeDataType::Short2; +template <> constexpr AttributeDataType AttributeDataTypeOf<int16_t, 3> = AttributeDataType::Short3; +template <> constexpr AttributeDataType AttributeDataTypeOf<int16_t, 4> = AttributeDataType::Short4; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint16_t, 1> = AttributeDataType::UShort; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint16_t, 2> = AttributeDataType::UShort2; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint16_t, 3> = AttributeDataType::UShort3; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint16_t, 4> = AttributeDataType::UShort4; +template <> constexpr AttributeDataType AttributeDataTypeOf<int32_t, 1> = AttributeDataType::Int; +template <> constexpr AttributeDataType AttributeDataTypeOf<int32_t, 2> = AttributeDataType::Int2; +template <> constexpr AttributeDataType AttributeDataTypeOf<int32_t, 3> = AttributeDataType::Int3; +template <> constexpr AttributeDataType AttributeDataTypeOf<int32_t, 4> = AttributeDataType::Int4; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint32_t, 1> = AttributeDataType::UInt; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint32_t, 2> = AttributeDataType::UInt2; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint32_t, 3> = AttributeDataType::UInt3; +template <> constexpr AttributeDataType AttributeDataTypeOf<uint32_t, 4> = AttributeDataType::UInt4; +template <> constexpr AttributeDataType AttributeDataTypeOf<float, 1> = AttributeDataType::Float; +template <> constexpr AttributeDataType AttributeDataTypeOf<float, 2> = AttributeDataType::Float2; +template <> constexpr AttributeDataType AttributeDataTypeOf<float, 3> = AttributeDataType::Float3; +template <> constexpr AttributeDataType AttributeDataTypeOf<float, 4> = AttributeDataType::Float4; + +} // namespace + template <typename T, std::size_t N> class AttributeType { public: - using ValueType = T; + using ElementType = T; + static constexpr AttributeDataType DataType = AttributeDataTypeOf<T, N>; static constexpr size_t Dimensions = N; using Value = std::array<T, N>; }; +struct AttributeDescriptor { + AttributeDataType dataType; + uint8_t offset; +}; + +inline bool operator==(const AttributeDescriptor& lhs, const AttributeDescriptor& rhs) { + return lhs.dataType == rhs.dataType && lhs.offset == rhs.offset; +} + struct VertexDescriptor { - const uint8_t size; - const uint8_t count; - const uint8_t offsets[5]; + uint8_t stride; + uint8_t count; + AttributeDescriptor attributes[5]; }; // Attribute binding requires member offsets. The only standard way to @@ -49,41 +94,41 @@ struct Vertex; template <class A1> struct Vertex<A1> { using Type = Vertex<A1>; - A1 a1; + typename A1::Value a1; } MBGL_VERTEX_ALIGN; template <class A1, class A2> struct Vertex<A1, A2> { using Type = Vertex<A1, A2>; - A1 a1; - A2 a2; + typename A1::Value a1; + typename A2::Value a2; } MBGL_VERTEX_ALIGN; template <class A1, class A2, class A3> struct Vertex<A1, A2, A3> { using Type = Vertex<A1, A2, A3>; - A1 a1; - A2 a2; - A3 a3; + typename A1::Value a1; + typename A2::Value a2; + typename A3::Value a3; } MBGL_VERTEX_ALIGN; template <class A1, class A2, class A3, class A4> struct Vertex<A1, A2, A3, A4> { using Type = Vertex<A1, A2, A3, A4>; - A1 a1; - A2 a2; - A3 a3; - A4 a4; + typename A1::Value a1; + typename A2::Value a2; + typename A3::Value a3; + typename A4::Value a4; } MBGL_VERTEX_ALIGN; template <class A1, class A2, class A3, class A4, class A5> struct Vertex<A1, A2, A3, A4, A5> { using Type = Vertex<A1, A2, A3, A4, A5>; - A1 a1; - A2 a2; - A3 a3; - A4 a4; - A5 a5; + typename A1::Value a1; + typename A2::Value a2; + typename A3::Value a3; + typename A4::Value a4; + typename A5::Value a5; } MBGL_VERTEX_ALIGN; template <class> @@ -95,7 +140,7 @@ struct Descriptor<Vertex<A1>> { static_assert(sizeof(Type) < 256, "vertex type must be smaller than 256 bytes"); static_assert(std::is_standard_layout<Type>::value, "vertex type must use standard layout"); static constexpr const VertexDescriptor Data = { sizeof(Type), 1, { - offsetof(Type, a1), + { A1::DataType, offsetof(Type, a1) }, }}; }; @@ -105,8 +150,8 @@ struct Descriptor<Vertex<A1, A2>> { static_assert(sizeof(Type) < 256, "vertex type must be smaller than 256 bytes"); static_assert(std::is_standard_layout<Type>::value, "vertex type must use standard layout"); static constexpr const VertexDescriptor Data = { sizeof(Type), 2, { - offsetof(Type, a1), - offsetof(Type, a2), + { A1::DataType, offsetof(Type, a1) }, + { A2::DataType, offsetof(Type, a2) }, }}; }; @@ -116,9 +161,9 @@ struct Descriptor<Vertex<A1, A2, A3>> { static_assert(sizeof(Type) < 256, "vertex type must be smaller than 256 bytes"); static_assert(std::is_standard_layout<Type>::value, "vertex type must use standard layout"); static constexpr const VertexDescriptor Data = { sizeof(Type), 3, { - offsetof(Type, a1), - offsetof(Type, a2), - offsetof(Type, a3), + { A1::DataType, offsetof(Type, a1) }, + { A2::DataType, offsetof(Type, a2) }, + { A3::DataType, offsetof(Type, a3) }, }}; }; @@ -128,10 +173,10 @@ struct Descriptor<Vertex<A1, A2, A3, A4>> { static_assert(sizeof(Type) < 256, "vertex type must be smaller than 256 bytes"); static_assert(std::is_standard_layout<Type>::value, "vertex type must use standard layout"); static constexpr const VertexDescriptor Data = { sizeof(Type), 4, { - offsetof(Type, a1), - offsetof(Type, a2), - offsetof(Type, a3), - offsetof(Type, a4), + { A1::DataType, offsetof(Type, a1) }, + { A2::DataType, offsetof(Type, a2) }, + { A3::DataType, offsetof(Type, a3) }, + { A4::DataType, offsetof(Type, a4) }, }}; }; @@ -141,17 +186,17 @@ struct Descriptor<Vertex<A1, A2, A3, A4, A5>> { static_assert(sizeof(Type) < 256, "vertex type must be smaller than 256 bytes"); static_assert(std::is_standard_layout<Type>::value, "vertex type must use standard layout"); static constexpr const VertexDescriptor Data = { sizeof(Type), 5, { - offsetof(Type, a1), - offsetof(Type, a2), - offsetof(Type, a3), - offsetof(Type, a4), - offsetof(Type, a5), + { A1::DataType, offsetof(Type, a1) }, + { A2::DataType, offsetof(Type, a2) }, + { A3::DataType, offsetof(Type, a3) }, + { A4::DataType, offsetof(Type, a4) }, + { A5::DataType, offsetof(Type, a5) }, }}; }; template <class... As> struct Vertex<TypeList<As...>> { - using Type = Vertex<typename As::Type::Value...>; + using Type = Vertex<typename As::Type...>; }; } // namespace detail |