summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/fill_bucket.cpp
blob: ff976c245076dcf1f482db511a570530562edf44 (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
#include <mbgl/renderer/fill_bucket.hpp>
#include <mbgl/geometry/fill_buffer.hpp>
#include <mbgl/layer/fill_layer.hpp>
#include <mbgl/geometry/elements_buffer.hpp>
#include <mbgl/renderer/painter.hpp>
#include <mbgl/shader/plain_shader.hpp>
#include <mbgl/shader/pattern_shader.hpp>
#include <mbgl/shader/outline_shader.hpp>
#include <mbgl/platform/gl.hpp>
#include <mbgl/platform/log.hpp>

#include <cassert>

struct geometry_too_long_exception : std::exception {};

using namespace mbgl;

void *FillBucket::alloc(void *, unsigned int size) {
    return ::malloc(size);
}

void *FillBucket::realloc(void *, void *ptr, unsigned int size) {
    return ::realloc(ptr, size);
}

void FillBucket::free(void *, void *ptr) {
    ::free(ptr);
}

FillBucket::FillBucket()
    : allocator(new TESSalloc{
          &alloc,
          &realloc,
          &free,
          nullptr, // userData
          64,      // meshEdgeBucketSize
          64,      // meshVertexBucketSize
          32,      // meshFaceBucketSize
          64,      // dictNodeBucketSize
          8,       // regionBucketSize
          128,     // extraVertices allocated for the priority queue.
      }),
      tesselator(tessNewTess(allocator)) {
    assert(tesselator);
}

FillBucket::~FillBucket() {
    if (tesselator) {
        tessDeleteTess(tesselator);
    }
    if (allocator) {
        delete allocator;
    }
}

void FillBucket::addGeometry(const GeometryCollection& geometryCollection) {
    for (auto& line_ : geometryCollection) {
        for (auto& v : line_) {
            line.emplace_back(v.x, v.y);
        }
        if (!line.empty()) {
            clipper.AddPath(line, ClipperLib::ptSubject, true);
            line.clear();
            hasVertices = true;
        }
    }

    tessellate();
}

void FillBucket::tessellate() {
    if (!hasVertices) {
        return;
    }
    hasVertices = false;

    std::vector<std::vector<ClipperLib::IntPoint>> polygons;
    clipper.Execute(ClipperLib::ctUnion, polygons, ClipperLib::pftEvenOdd, ClipperLib::pftEvenOdd);
    clipper.Clear();

    if (polygons.empty()) {
        return;
    }

    GLsizei total_vertex_count = 0;
    for (const auto& polygon : polygons) {
        total_vertex_count += polygon.size();
    }

    if (total_vertex_count > 65536) {
        throw geometry_too_long_exception();
    }

    if (lineGroups.empty() || (lineGroups.back()->vertex_length + total_vertex_count > 65535)) {
        // Move to a new group because the old one can't hold the geometry.
        lineGroups.emplace_back(std::make_unique<LineGroup>());
    }

    assert(lineGroups.back());
    LineGroup& lineGroup = *lineGroups.back();
    GLsizei lineIndex = lineGroup.vertex_length;

    for (const auto& polygon : polygons) {
        const GLsizei group_count = static_cast<GLsizei>(polygon.size());
        assert(group_count >= 3);

        std::vector<TESSreal> clipped_line;
        for (const auto& pt : polygon) {
            clipped_line.push_back(pt.X);
            clipped_line.push_back(pt.Y);
            vertexBuffer.add(pt.X, pt.Y);
        }

        for (GLsizei i = 0; i < group_count; i++) {
            const GLsizei prev_i = (i == 0 ? group_count : i) - 1;
            lineElementsBuffer.add(lineIndex + prev_i, lineIndex + i);
        }

        lineIndex += group_count;

        tessAddContour(tesselator, vertexSize, clipped_line.data(), stride, (int)clipped_line.size() / vertexSize);
    }

    lineGroup.elements_length += total_vertex_count;

    if (tessTesselate(tesselator, TESS_WINDING_ODD, TESS_POLYGONS, vertices_per_group, vertexSize, 0)) {
        const TESSreal *vertices = tessGetVertices(tesselator);
        const GLsizei vertex_count = tessGetVertexCount(tesselator);
        TESSindex *vertex_indices = const_cast<TESSindex *>(tessGetVertexIndices(tesselator));
        const TESSindex *elements = tessGetElements(tesselator);
        const int triangle_count = tessGetElementCount(tesselator);

        for (GLsizei i = 0; i < vertex_count; ++i) {
            if (vertex_indices[i] == TESS_UNDEF) {
                vertexBuffer.add(::round(vertices[i * 2]), ::round(vertices[i * 2 + 1]));
                vertex_indices[i] = (TESSindex)total_vertex_count;
                total_vertex_count++;
            }
        }

        if (triangleGroups.empty() || (triangleGroups.back()->vertex_length + total_vertex_count > 65535)) {
            // Move to a new group because the old one can't hold the geometry.
            triangleGroups.emplace_back(std::make_unique<TriangleGroup>());
        }

        // We're generating triangle fans, so we always start with the first
        // coordinate in this polygon.
        assert(triangleGroups.back());
        TriangleGroup& triangleGroup = *triangleGroups.back();
        GLsizei triangleIndex = triangleGroup.vertex_length;

        for (int i = 0; i < triangle_count; ++i) {
            const TESSindex *element_group = &elements[i * vertices_per_group];

            if (element_group[0] != TESS_UNDEF && element_group[1] != TESS_UNDEF && element_group[2] != TESS_UNDEF) {
                const TESSindex a = vertex_indices[element_group[0]];
                const TESSindex b = vertex_indices[element_group[1]];
                const TESSindex c = vertex_indices[element_group[2]];

                if (a != TESS_UNDEF && b != TESS_UNDEF && c != TESS_UNDEF) {
                    triangleElementsBuffer.add(triangleIndex + a, triangleIndex + b, triangleIndex + c);
                } else {
#if defined(DEBUG)
                    // TODO: We're missing a vertex that was not part of the line.
                    Log::Error(Event::OpenGL, "undefined element buffer");
#endif
                }
            } else {
#if defined(DEBUG)
                Log::Error(Event::OpenGL, "undefined element buffer");
#endif
            }
        }

        triangleGroup.vertex_length += total_vertex_count;
        triangleGroup.elements_length += triangle_count;
    } else {
#if defined(DEBUG)
        Log::Error(Event::OpenGL, "tessellation failed");
#endif
    }

    // We're adding the total vertex count *after* we added additional vertices
    // in the tessellation step. They won't be part of the actual lines, but
    // we need to skip over them anyway if we draw the next group.
    lineGroup.vertex_length += total_vertex_count;
}

void FillBucket::upload() {
    vertexBuffer.upload();
    triangleElementsBuffer.upload();
    lineElementsBuffer.upload();

    // From now on, we're going to render during the opaque and translucent pass.
    uploaded = true;
}

void FillBucket::render(Painter& painter,
                        const StyleLayer& layer,
                        const TileID& id,
                        const mat4& matrix) {
    painter.renderFill(*this, *layer.as<FillLayer>(), id, matrix);
}

bool FillBucket::hasData() const {
    return !triangleGroups.empty() || !lineGroups.empty();
}

void FillBucket::drawElements(PlainShader& shader) {
    GLbyte* vertex_index = BUFFER_OFFSET(0);
    GLbyte* elements_index = BUFFER_OFFSET(0);
    for (auto& group : triangleGroups) {
        assert(group);
        group->array[0].bind(shader, vertexBuffer, triangleElementsBuffer, vertex_index);
        MBGL_CHECK_ERROR(glDrawElements(GL_TRIANGLES, group->elements_length * 3, GL_UNSIGNED_SHORT, elements_index));
        vertex_index += group->vertex_length * vertexBuffer.itemSize;
        elements_index += group->elements_length * triangleElementsBuffer.itemSize;
    }
}

void FillBucket::drawElements(PatternShader& shader) {
    GLbyte* vertex_index = BUFFER_OFFSET(0);
    GLbyte* elements_index = BUFFER_OFFSET(0);
    for (auto& group : triangleGroups) {
        assert(group);
        group->array[1].bind(shader, vertexBuffer, triangleElementsBuffer, vertex_index);
        MBGL_CHECK_ERROR(glDrawElements(GL_TRIANGLES, group->elements_length * 3, GL_UNSIGNED_SHORT, elements_index));
        vertex_index += group->vertex_length * vertexBuffer.itemSize;
        elements_index += group->elements_length * triangleElementsBuffer.itemSize;
    }
}

void FillBucket::drawVertices(OutlineShader& shader) {
    GLbyte* vertex_index = BUFFER_OFFSET(0);
    GLbyte* elements_index = BUFFER_OFFSET(0);
    for (auto& group : lineGroups) {
        assert(group);
        group->array[0].bind(shader, vertexBuffer, lineElementsBuffer, vertex_index);
        MBGL_CHECK_ERROR(glDrawElements(GL_LINES, group->elements_length * 2, GL_UNSIGNED_SHORT, elements_index));
        vertex_index += group->vertex_length * vertexBuffer.itemSize;
        elements_index += group->elements_length * lineElementsBuffer.itemSize;
    }
}