summaryrefslogtreecommitdiff
path: root/src/mbgl/gl/segment.hpp
blob: fe0658bf8eaa2a93279f26a58a3d4daf9091284d (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
#pragma once

#include <mbgl/gl/context.hpp>
#include <mbgl/gl/vertex_buffer.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/logging.hpp>

#include <cstddef>
#include <vector>

namespace mbgl {
namespace gl {

template <class Attributes>
class Segment {
public:
    Segment(std::size_t vertexOffset_,
            std::size_t indexOffset_,
            std::size_t vertexLength_ = 0,
            std::size_t indexLength_ = 0)
        : vertexOffset(vertexOffset_),
          indexOffset(indexOffset_),
          vertexLength(vertexLength_),
          indexLength(indexLength_) {}

    const std::size_t vertexOffset;
    const std::size_t indexOffset;

    std::size_t vertexLength;
    std::size_t indexLength;

    void bind(Context& context,
              BufferID indexBuffer_,
              const typename Attributes::Locations& attributeLocations,
              const typename Attributes::Bindings& attributeBindings_) const {
        if (context.supportsVertexArrays()) {
            if (!vao) {
                vao = context.createVertexArray();
                context.vertexBuffer.setDirty();
            }
            context.vertexArrayObject = *vao;
            if (indexBuffer != indexBuffer_) {
                indexBuffer = indexBuffer_;
                context.elementBuffer.setDirty();
                context.elementBuffer = indexBuffer_;
            }
        } else {
            // No VAO support. Force attributes to be rebound.
            context.elementBuffer = indexBuffer_;
            attributeBindings = {};
        }

        Attributes::bind(context,
                         attributeLocations,
                         attributeBindings,
                         attributeBindings_,
                         vertexOffset);
    }

private:
    mutable optional<UniqueVertexArray> vao;
    mutable optional<BufferID> indexBuffer;
    mutable typename Attributes::Bindings attributeBindings;
};

template <class Attributes>
class SegmentVector : public std::vector<Segment<Attributes>> {
public:
    SegmentVector() = default;
};

} // namespace gl
} // namespace mbgl