summaryrefslogtreecommitdiff
path: root/src/mbgl/gfx/attribute.hpp
blob: ec34a69a9af6607c683e8c18139275fc76afead9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma once

#include <mbgl/util/type_list.hpp>
#include <mbgl/util/indexed_tuple.hpp>

#include <array>
#include <type_traits>
#include <cstddef>

#define MBGL_DEFINE_ATTRIBUTE(type_, n_, name_)                                                    \
    struct name_ {                                                                                 \
        using Type = ::mbgl::gfx::AttributeType<type_, n_>;                                        \
        static auto name() {                                                                       \
            return #name_;                                                                         \
        }                                                                                          \
    }

#define MBGL_VERTEX_ALIGN __attribute__((aligned(4)))

namespace mbgl {
namespace gfx {

template <typename T, std::size_t N>
class AttributeType {
public:
    using ValueType = T;
    static constexpr size_t Dimensions = N;
    using Value = std::array<T, N>;
};

struct VertexDescriptor {
    const uint8_t size;
    const uint8_t count;
    const uint8_t offsets[5];
};

// Attribute binding requires member offsets. The only standard way to
// obtain an offset is the offsetof macro. The offsetof macro has defined
// behavior only for standard layout types. That rules out std::tuple and
// any other solution that relies on chained inheritance. Manually implemented
// variadic specialization looks like the only solution. Fortunately, we
// only use a maximum of five attributes.

namespace detail {

template <class...>
struct Vertex;

template <class A1>
struct Vertex<A1> {
    using Type = Vertex<A1>;
    A1 a1;
} MBGL_VERTEX_ALIGN;

template <class A1, class A2>
struct Vertex<A1, A2> {
    using Type = Vertex<A1, A2>;
    A1 a1;
    A2 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;
} 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;
} 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;
} MBGL_VERTEX_ALIGN;

template <class>
struct Descriptor;

template <class A1>
struct Descriptor<Vertex<A1>> {
    using Type = 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),
    }};
};

template <class A1, class A2>
struct Descriptor<Vertex<A1, A2>> {
    using Type = 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),
    }};
};

template <class A1, class A2, class A3>
struct Descriptor<Vertex<A1, A2, A3>> {
    using Type = 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),
    }};
};

template <class A1, class A2, class A3, class A4>
struct Descriptor<Vertex<A1, A2, A3, A4>> {
    using Type = 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),
    }};
};

template <class A1, class A2, class A3, class A4, class A5>
struct Descriptor<Vertex<A1, A2, A3, A4, A5>> {
    using Type = 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),
    }};
};

template <class... As>
struct Vertex<TypeList<As...>> {
    using Type = Vertex<typename As::Type::Value...>;
};

} // namespace detail

template <class A>
using Vertex = typename detail::Vertex<A>::Type;

template <class V>
constexpr const VertexDescriptor vertexDescriptor = detail::Descriptor<V>::Data;

} // namespace gfx
} // namespace mbgl