summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/attribute.hpp
blob: bc02b54bd2c572630fee57f0b7037f0ef1b8923a (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#pragma once

#include <mbgl/gl/types.hpp>
#include <mbgl/gl/segment.hpp>
#include <mbgl/util/ignore.hpp>
#include <mbgl/util/indexed_tuple.hpp>
#include <mbgl/util/variant.hpp>

#include <cstddef>
#include <vector>
#include <set>
#include <functional>

namespace mbgl {
namespace gl {

class DisabledAttribute {
public:
    void bind(Context&, AttributeLocation, std::size_t vertexOffset) const;

    friend bool operator==(const DisabledAttribute&,
                           const DisabledAttribute&) {
        return true;
    }
};

template <class T, std::size_t N>
class AttributeBinding {
public:
    AttributeBinding(BufferID vertexBuffer_,
                     std::size_t vertexSize_,
                     std::size_t attributeOffset_,
                     std::size_t attributeSize_ = N)
        : vertexBuffer(vertexBuffer_),
          vertexSize(vertexSize_),
          attributeOffset(attributeOffset_),
          attributeSize(attributeSize_)
        {}

    void bind(Context&, AttributeLocation, std::size_t vertexOffset) const;

    friend bool operator==(const AttributeBinding& lhs,
                           const AttributeBinding& rhs) {
        return lhs.vertexBuffer == rhs.vertexBuffer
            && lhs.vertexSize == rhs.vertexSize
            && lhs.attributeOffset == rhs.attributeOffset
            && lhs.attributeSize == rhs.attributeSize;
    }

private:
    BufferID vertexBuffer;
    std::size_t vertexSize;
    std::size_t attributeOffset;
    std::size_t attributeSize;
};

/*
    gl::Attribute<T,N> manages the binding of a 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 Location = AttributeLocation;

    using Binding = variant<
        DisabledAttribute,
        AttributeBinding<T, N>>;

    /*
        Create a 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 Binding binding(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 AttributeBinding<T, N> {
            buffer.buffer,
            sizeof(Vertex),
            Vertex::attributeOffsets[attributeIndex],
            attributeSize
        };
    }

    static void bind(Context& context,
                     const Location& location,
                     Binding& oldBinding,
                     const Binding& newBinding,
                     std::size_t vertexOffset) {
        if (oldBinding == newBinding) {
            return;
        }

        Binding::visit(newBinding, [&] (const auto& binding) {
            binding.bind(context, location, vertexOffset);
        });

        oldBinding = newBinding;
    }
};

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

namespace detail {

// 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.

template <class... As>
class Vertex;

template <>
class Vertex<> {
public:
    using VertexType = Vertex<>;
};

template <class A1>
class Vertex<A1> {
public:
    typename A1::Value a1;

    using VertexType = Vertex<A1>;
    static const std::size_t attributeOffsets[1];
};

template <class A1, class A2>
class Vertex<A1, A2> {
public:
    typename A1::Value a1;
    typename A2::Value a2;

    using VertexType = Vertex<A1, A2>;
    static const std::size_t attributeOffsets[2];
};

template <class A1, class A2, class A3>
class Vertex<A1, A2, A3> {
public:
    typename A1::Value a1;
    typename A2::Value a2;
    typename A3::Value a3;

    using VertexType = Vertex<A1, A2, A3>;
    static const std::size_t attributeOffsets[3];
};

template <class A1, class A2, class A3, class A4>
class Vertex<A1, A2, A3, A4> {
public:
    typename A1::Value a1;
    typename A2::Value a2;
    typename A3::Value a3;
    typename A4::Value a4;

    using VertexType = Vertex<A1, A2, A3, A4>;
    static const std::size_t attributeOffsets[4];
};

template <class A1, class A2, class A3, class A4, class A5>
class Vertex<A1, A2, A3, A4, A5> {
public:
    typename A1::Value a1;
    typename A2::Value a2;
    typename A3::Value a3;
    typename A4::Value a4;
    typename A5::Value a5;

    using VertexType = Vertex<A1, A2, A3, A4, A5>;
    static const std::size_t attributeOffsets[5];
};

template <class A1>
const std::size_t Vertex<A1>::attributeOffsets[1] = {
    offsetof(VertexType, a1)
};

template <class A1, class A2>
const std::size_t Vertex<A1, A2>::attributeOffsets[2] = {
    offsetof(VertexType, a1),
    offsetof(VertexType, a2)
};

template <class A1, class A2, class A3>
const std::size_t Vertex<A1, A2, A3>::attributeOffsets[3] = {
    offsetof(VertexType, a1),
    offsetof(VertexType, a2),
    offsetof(VertexType, a3)
};

template <class A1, class A2, class A3, class A4>
const std::size_t Vertex<A1, A2, A3, A4>::attributeOffsets[4] = {
    offsetof(VertexType, a1),
    offsetof(VertexType, a2),
    offsetof(VertexType, a3),
    offsetof(VertexType, a4)
};

template <class A1, class A2, class A3, class A4, class A5>
const std::size_t Vertex<A1, A2, A3, A4, A5>::attributeOffsets[5] = {
    offsetof(VertexType, a1),
    offsetof(VertexType, a2),
    offsetof(VertexType, a3),
    offsetof(VertexType, a4),
    offsetof(VertexType, a5)
};

} // namespace detail

AttributeLocation bindAttributeLocation(ProgramID, AttributeLocation, const char * name);
std::set<std::string> getActiveAttributes(ProgramID);

template <class... As>
class Attributes {
public:
    using Types = TypeList<As...>;
    using Locations = IndexedTuple<
        TypeList<As...>,
        TypeList<typename As::Type::Location...>>;
    using Bindings = IndexedTuple<
        TypeList<As...>,
        TypeList<typename As::Type::Binding...>>;
    using NamedLocations = std::vector<std::pair<const std::string, AttributeLocation>>;

    using Vertex = detail::Vertex<typename As::Type...>;

    static Locations bindLocations(const ProgramID& id) {
        std::set<std::string> activeAttributes = getActiveAttributes(id);

        AttributeLocation location = -1;
        auto bindAndIncrement = [&](const char* name) {
            location++;
            return bindAttributeLocation(id, location, name);
        };
        return Locations{ (activeAttributes.count(As::name()) ? bindAndIncrement(As::name())
                                                              : -1)... };
    }

    template <class Program>
    static Locations loadNamedLocations(const Program& program) {
        return Locations{ program.attributeLocation(As::name())... };
    }

    static NamedLocations getNamedLocations(const Locations& locations) {
        return NamedLocations{ { As::name(), locations.template get<As>() }... };
    }

    template <class DrawMode>
    static Bindings bindings(const VertexBuffer<Vertex, DrawMode>& buffer) {
        return Bindings { As::Type::binding(buffer, TypeIndex<As, As...>::value)... };
    }

    static void bind(Context& context,
                     const Locations& locations,
                     Bindings& oldBindings,
                     const Bindings& newBindings,
                     std::size_t vertexOffset) {
        util::ignore({ (As::Type::bind(context,
                                       locations.template get<As>(),
                                       oldBindings.template get<As>(),
                                       newBindings.template get<As>(),
                                       vertexOffset), 0)... });
    }
};

namespace detail {

template <class...>
struct ConcatenateAttributes;

template <class... As, class... Bs>
struct ConcatenateAttributes<TypeList<As...>, TypeList<Bs...>> {
    using Type = Attributes<As..., Bs...>;
};

} // namespace detail

template <class A, class B>
using ConcatenateAttributes = typename detail::ConcatenateAttributes<
    typename A::Types,
    typename B::Types>::Type;

} // namespace gl
} // namespace mbgl