summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/render_tile.cpp
blob: f1f6a4ac451a147553764e847a81e1c5c58ec218 (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
#include <mbgl/renderer/render_tile.hpp>

#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/buckets/debug_bucket.hpp>
#include <mbgl/renderer/render_source.hpp>
#include <mbgl/renderer/render_static_data.hpp>
#include <mbgl/programs/programs.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/gfx/cull_face_mode.hpp>
#include <mbgl/tile/tile.hpp>
#include <mbgl/tile/geometry_tile.hpp>
#include <mbgl/util/math.hpp>

namespace mbgl {

using namespace style;

RenderTile::RenderTile(UnwrappedTileID id_, Tile& tile_) : id(std::move(id_)), tile(tile_) {
}

RenderTile::~RenderTile() = default;

mat4 RenderTile::translateVtxMatrix(const mat4& tileMatrix,
                                    const std::array<float, 2>& translation,
                                    TranslateAnchorType anchor,
                                    const TransformState& state,
                                    const bool inViewportPixelUnits) const {
    if (translation[0] == 0 && translation[1] == 0) {
        return tileMatrix;
    }

    mat4 vtxMatrix;

    const float angle = inViewportPixelUnits ?
        (anchor == TranslateAnchorType::Map ? state.getBearing() : 0) :
        (anchor == TranslateAnchorType::Viewport ? -state.getBearing() : 0);

    Point<float> translate = util::rotate(Point<float>{ translation[0], translation[1] }, angle);

    if (inViewportPixelUnits) {
        matrix::translate(vtxMatrix, tileMatrix, translate.x, translate.y, 0);
    } else {
        matrix::translate(vtxMatrix, tileMatrix,
                          id.pixelsToTileUnits(translate.x, state.getZoom()),
                          id.pixelsToTileUnits(translate.y, state.getZoom()),
                          0);
    }

    return vtxMatrix;
}

mat4 RenderTile::translatedMatrix(const std::array<float, 2>& translation,
                                  TranslateAnchorType anchor,
                                  const TransformState& state) const {
    return translateVtxMatrix(matrix, translation, anchor, state, false);
}

mat4 RenderTile::translatedClipMatrix(const std::array<float, 2>& translation,
                                      TranslateAnchorType anchor,
                                      const TransformState& state) const {
    return translateVtxMatrix(nearClippedMatrix, translation, anchor, state, false);
}

const OverscaledTileID& RenderTile::getOverscaledTileID() const { return tile.id; }
bool RenderTile::holdForFade() const { return tile.holdForFade(); }

Bucket* RenderTile::getBucket(const style::Layer::Impl& impl) const {
    return tile.getBucket(impl);
}

const LayerRenderData* RenderTile::getLayerRenderData(const style::Layer::Impl& impl) const {
    return tile.getLayerRenderData(impl);
}

optional<ImagePosition> RenderTile::getPattern(const std::string& pattern) const {
    assert(tile.kind == Tile::Kind::Geometry);
    return static_cast<const GeometryTile&>(tile).getPattern(pattern);
}

const gfx::Texture& RenderTile::getGlyphAtlasTexture() const {
    assert(tile.kind == Tile::Kind::Geometry);
    assert(static_cast<const GeometryTile&>(tile).glyphAtlasTexture);
    return *(static_cast<const GeometryTile&>(tile).glyphAtlasTexture);
}

const gfx::Texture& RenderTile::getIconAtlasTexture() const {
    assert(tile.kind == Tile::Kind::Geometry);
    assert(static_cast<const GeometryTile&>(tile).iconAtlasTexture);
    return *(static_cast<const GeometryTile&>(tile).iconAtlasTexture);
}

std::shared_ptr<FeatureIndex> RenderTile::getFeatureIndex() const {
    assert(tile.kind == Tile::Kind::Geometry);
    return static_cast<const GeometryTile&>(tile).getFeatureIndex();
}

void RenderTile::setMask(TileMask&& mask) {
    tile.setMask(std::move(mask));
}

void RenderTile::upload(gfx::UploadPass& uploadPass) {
    tile.upload(uploadPass);

    if (debugBucket) {
        debugBucket->upload(uploadPass);
    }
}

void RenderTile::prepare(const SourcePrepareParameters& parameters) {
    if (parameters.debugOptions != MapDebugOptions::NoDebug &&
        (!debugBucket || debugBucket->renderable != tile.isRenderable() ||
         debugBucket->complete != tile.isComplete() ||
         !(debugBucket->modified == tile.modified) ||
         !(debugBucket->expires == tile.expires) ||
         debugBucket->debugMode != parameters.debugOptions)) {
        debugBucket = std::make_unique<DebugBucket>(
            tile.id, tile.isRenderable(), tile.isComplete(), tile.modified, tile.expires,
            parameters.debugOptions);
    } else if (parameters.debugOptions == MapDebugOptions::NoDebug) {
        debugBucket.reset();
    }

    // Calculate two matrices for this tile: matrix is the standard tile matrix; nearClippedMatrix
    // clips the near plane to 100 to save depth buffer precision
    const auto& transform = parameters.transform;
    transform.state.matrixFor(matrix, id);
    transform.state.matrixFor(nearClippedMatrix, id);
    matrix::multiply(matrix, transform.projMatrix, matrix);
    matrix::multiply(nearClippedMatrix, transform.nearClippedProjMatrix, nearClippedMatrix);
}

void RenderTile::finishRender(PaintParameters& parameters) {
    if (!tile.usedByRenderedLayers || parameters.debugOptions == MapDebugOptions::NoDebug)
        return;

    static const style::Properties<>::PossiblyEvaluated properties {};
    static const DebugProgram::Binders paintAttributeData(properties, 0);

    auto& program = parameters.programs.debug;

    if (parameters.debugOptions & (MapDebugOptions::Timestamps | MapDebugOptions::ParseStatus)) {
        assert(debugBucket);
        const auto allAttributeBindings = program.computeAllAttributeBindings(
            *debugBucket->vertexBuffer,
            paintAttributeData,
            properties
        );

        program.draw(
            parameters.context,
            *parameters.renderPass,
            gfx::Lines { 4.0f * parameters.pixelRatio },
            gfx::DepthMode::disabled(),
            gfx::StencilMode::disabled(),
            gfx::ColorMode::unblended(),
            gfx::CullFaceMode::disabled(),
            *debugBucket->indexBuffer,
            debugBucket->segments,
            program.computeAllUniformValues(
                DebugProgram::LayoutUniformValues {
                    uniforms::matrix::Value( matrix ),
                    uniforms::color::Value( Color::white() )
                },
                paintAttributeData,
                properties,
                parameters.state.getZoom()
            ),
            allAttributeBindings,
            DebugProgram::TextureBindings{},
            "__debug/" + debugBucket->drawScopeID + "/text-outline"
        );

        program.draw(
            parameters.context,
            *parameters.renderPass,
            gfx::Lines { 2.0f * parameters.pixelRatio },
            gfx::DepthMode::disabled(),
            gfx::StencilMode::disabled(),
            gfx::ColorMode::unblended(),
            gfx::CullFaceMode::disabled(),
            *debugBucket->indexBuffer,
            debugBucket->segments,
            program.computeAllUniformValues(
                DebugProgram::LayoutUniformValues {
                    uniforms::matrix::Value( matrix ),
                    uniforms::color::Value( Color::black() )
                },
                paintAttributeData,
                properties,
                parameters.state.getZoom()
            ),
            allAttributeBindings,
            DebugProgram::TextureBindings{},
            "__debug/" + debugBucket->drawScopeID + "/text"
        );
    }

    if (parameters.debugOptions & MapDebugOptions::TileBorders) {
        assert(debugBucket);
        parameters.programs.debug.draw(
            parameters.context,
            *parameters.renderPass,
            gfx::LineStrip { 4.0f * parameters.pixelRatio },
            gfx::DepthMode::disabled(),
            gfx::StencilMode::disabled(),
            gfx::ColorMode::unblended(),
            gfx::CullFaceMode::disabled(),
            *parameters.staticData.tileBorderIndexBuffer,
            parameters.staticData.tileBorderSegments,
            program.computeAllUniformValues(
                DebugProgram::LayoutUniformValues {
                    uniforms::matrix::Value( matrix ),
                    uniforms::color::Value( Color::red() )
                },
                paintAttributeData,
                properties,
                parameters.state.getZoom()
            ),
            program.computeAllAttributeBindings(
                *parameters.staticData.tileVertexBuffer,
                paintAttributeData,
                properties
            ),
            DebugProgram::TextureBindings{},
            "__debug/" + debugBucket->drawScopeID
        );
    }
}

} // namespace mbgl