summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/indexed_primitives.hpp
blob: c75fe61b18ee580e794b1e882f07a1ec7dc75b3f (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
#pragma once

#include <mbgl/gl/vertex_buffer.hpp>
#include <mbgl/gl/index_buffer.hpp>
#include <mbgl/gl/segment.hpp>

#include <stdexcept>

namespace mbgl {

template <class DrawMode, class LayoutVertex, class AttributeLayout>
class Drawable;

template <class DrawMode, class LayoutVertex, class AttributeLayout>
class IndexedPrimitives {
public:
    void add(std::initializer_list<LayoutVertex> v,
             std::initializer_list<std::array<uint16_t, DrawMode::bufferGroupSize>> i) {
        // Check that all vertices fit into the current segment, if not, create a new one.
        if (segmentInfo.empty() || segmentInfo.back().vertexLength + v.size() > std::numeric_limits<uint16_t>::max()) {
            segmentInfo.emplace_back(vertices.vertexSize(), indices.indexSize());
        }

        auto& segment = segmentInfo.back();
        const uint16_t offset = segment.vertexLength;

        for (const auto& primitive : i) {
            for (uint16_t index : primitive) {
                // Check that the index references a vertex supplied in this list and that it is not
                // out of bounds.
                if (index >= v.size()) {
                    throw std::out_of_range("primitive contains indices outside its vertex group");
                }
            }

            // Insert all indices into the list.
            addIndices(primitive, offset, std::make_index_sequence<DrawMode::bufferGroupSize>());
        }

        // Insert all vertices into the list.
        for (const auto& vertex : v) {
            vertices.emplace_back(vertex);
        }

        // Update the current segment statistics.
        segment.vertexLength += v.size();
        segment.indexLength += i.size() * DrawMode::bufferGroupSize;
    }

    // Accessors for test suite.
    const gl::VertexVector<LayoutVertex>& getVertices() const {
        return vertices;
    }

    const gl::IndexVector<DrawMode>& getIndices() const {
        return indices;
    }

    const gl::SegmentInfoVector& getSegmentInfo() const {
        return segmentInfo;
    }

private:
    // Helper function for expanding primitives.
    template <std::size_t N, std::size_t... I>
    void addIndices(std::array<uint16_t, N> primitive, const uint16_t offset, std::index_sequence<I...>) {
        // Adds the current offset to the indices.
        indices.emplace_back((std::get<I>(primitive) + offset)...);
    }

private:
    friend class Drawable<DrawMode, LayoutVertex, AttributeLayout>;
    gl::VertexVector<LayoutVertex> vertices;
    gl::IndexVector<DrawMode> indices;
    gl::SegmentInfoVector segmentInfo;
};

} // namespace mbgl