summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/paint_parameters.cpp
blob: 0bb66972a813a42469b01071ce467267db71abf4 (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
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/update_parameters.hpp>
#include <mbgl/renderer/render_static_data.hpp>
#include <mbgl/renderer/render_source.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/gfx/command_encoder.hpp>
#include <mbgl/gfx/render_pass.hpp>
#include <mbgl/gfx/cull_face_mode.hpp>
#include <mbgl/map/transform_state.hpp>

namespace mbgl {

TransformParameters::TransformParameters(const TransformState& state_)
    : state(state_) {
    // Update the default matrices to the current viewport dimensions.
    state.getProjMatrix(projMatrix);

    // Also compute a projection matrix that aligns with the current pixel grid, taking into account
    // odd viewport sizes.
    state.getProjMatrix(alignedProjMatrix, 1, true);

    // Calculate a second projection matrix with the near plane clipped to 100 so as
    // not to waste lots of depth buffer precision on very close empty space, for layer
    // types (fill-extrusion) that use the depth buffer to emulate real-world space.
    state.getProjMatrix(nearClippedProjMatrix, 100);
}

PaintParameters::PaintParameters(gfx::Context& context_,
                    float pixelRatio_,
                    gfx::RendererBackend& backend_,
                    const EvaluatedLight& evaluatedLight_,
                    MapMode mode_,
                    MapDebugOptions debugOptions_,
                    TimePoint timePoint_,
                    const TransformParameters& transformParams_,
                    RenderStaticData& staticData_,
                    LineAtlas& lineAtlas_,
                    PatternAtlas& patternAtlas_)
    : context(context_),
    backend(backend_),
    encoder(context.createCommandEncoder()),
    transformParams(transformParams_),
    state(transformParams_.state),
    evaluatedLight(evaluatedLight_),
    staticData(staticData_),
    lineAtlas(lineAtlas_),
    patternAtlas(patternAtlas_),
    mapMode(mode_),
    debugOptions(debugOptions_),
    timePoint(timePoint_),
    pixelRatio(pixelRatio_),
#ifndef NDEBUG
    programs((debugOptions & MapDebugOptions::Overdraw) ? staticData_.overdrawPrograms : staticData_.programs)
#else
    programs(staticData_.programs)
#endif
{
    pixelsToGLUnits = {{ 2.0f  / state.getSize().width, -2.0f / state.getSize().height }};

    if (state.getViewportMode() == ViewportMode::FlippedY) {
        pixelsToGLUnits[1] *= -1;
    }
}

PaintParameters::~PaintParameters() = default;

mat4 PaintParameters::matrixForTile(const UnwrappedTileID& tileID, bool aligned) const {
    mat4 matrix;
    state.matrixFor(matrix, tileID);
    matrix::multiply(matrix, aligned ? transformParams.alignedProjMatrix : transformParams.projMatrix, matrix);
    return matrix;
}

gfx::DepthMode PaintParameters::depthModeForSublayer(uint8_t n, gfx::DepthMaskType mask) const {
    float depth = depthRangeSize + ((1 + currentLayer) * numSublayers + n) * depthEpsilon;
    return gfx::DepthMode { gfx::DepthFunctionType::LessEqual, mask, { depth, depth } };
}

gfx::DepthMode PaintParameters::depthModeFor3D() const {
    return gfx::DepthMode{ gfx::DepthFunctionType::LessEqual,
                           gfx::DepthMaskType::ReadWrite,
                           { 0.0, depthRangeSize } };
}

void PaintParameters::clearStencil() {
    nextStencilID = 1;
    context.clearStencilBuffer(0b00000000);
}

namespace {

// Detects a difference in keys of renderTiles and tileClippingMaskIDs
bool tileIDsIdentical(const RenderTiles& renderTiles,
                      const std::map<UnwrappedTileID, int32_t>& tileClippingMaskIDs) {
    assert(std::is_sorted(renderTiles.begin(), renderTiles.end(),
                          [](const RenderTile& a, const RenderTile& b) { return a.id < b.id; }));
    if (renderTiles.size() != tileClippingMaskIDs.size()) {
        return false;
    }
    return std::equal(renderTiles.begin(), renderTiles.end(), tileClippingMaskIDs.begin(),
                      [](const RenderTile& a, const auto& b) { return a.id == b.first; });
}

} // namespace

void PaintParameters::renderTileClippingMasks(const RenderTiles& renderTiles) {
    if (renderTiles.empty() || tileIDsIdentical(renderTiles, tileClippingMaskIDs)) {
        // The current stencil mask is for this source already; no need to draw another one.
        return;
    }

    if (nextStencilID + renderTiles.size() > 256) {
        // we'll run out of fresh IDs so we need to clear and start from scratch
        clearStencil();
    }

    tileClippingMaskIDs.clear();

    auto& program = staticData.programs.clippingMask;
    const style::Properties<>::PossiblyEvaluated properties {};
    const ClippingMaskProgram::Binders paintAttributeData(properties, 0);

    for (const RenderTile& renderTile : renderTiles) {
        const int32_t stencilID = nextStencilID++;
        tileClippingMaskIDs.emplace(renderTile.id, stencilID);

        program.draw(
            context,
            *renderPass,
            gfx::Triangles(),
            gfx::DepthMode::disabled(),
            gfx::StencilMode {
                gfx::StencilMode::Always{},
                stencilID,
                0b11111111,
                gfx::StencilOpType::Keep,
                gfx::StencilOpType::Keep,
                gfx::StencilOpType::Replace
            },
            gfx::ColorMode::disabled(),
            gfx::CullFaceMode::disabled(),
            *staticData.quadTriangleIndexBuffer,
            staticData.tileTriangleSegments,
            program.computeAllUniformValues(
                ClippingMaskProgram::LayoutUniformValues {
                    uniforms::matrix::Value( matrixForTile(renderTile.id) ),
                },
                paintAttributeData,
                properties,
                state.getZoom()
            ),
            program.computeAllAttributeBindings(
                *staticData.tileVertexBuffer,
                paintAttributeData,
                properties
            ),
            ClippingMaskProgram::TextureBindings{},
            "clipping/" + util::toString(stencilID)
        );
    }
}

gfx::StencilMode PaintParameters::stencilModeForClipping(const UnwrappedTileID& tileID) const {
    auto it = tileClippingMaskIDs.find(tileID);
    assert(it != tileClippingMaskIDs.end());
    const int32_t id = it != tileClippingMaskIDs.end() ? it->second : 0b00000000;
    return gfx::StencilMode{ gfx::StencilMode::Equal{ 0b11111111 },
                             id,
                             0b00000000,
                             gfx::StencilOpType::Keep,
                             gfx::StencilOpType::Keep,
                             gfx::StencilOpType::Replace };
}

gfx::StencilMode PaintParameters::stencilModeFor3D() {
    if (nextStencilID + 1 > 256) {
        clearStencil();
    }

    // We're potentially destroying the stencil clipping mask in this pass. That means we'll have
    // to recreate it for the next source if any.
    tileClippingMaskIDs.clear();

    const int32_t id = nextStencilID++;
    return gfx::StencilMode{ gfx::StencilMode::NotEqual{ 0b11111111 },
                             id,
                             0b11111111,
                             gfx::StencilOpType::Keep,
                             gfx::StencilOpType::Keep,
                             gfx::StencilOpType::Replace };
}

gfx::ColorMode PaintParameters::colorModeForRenderPass() const {
    if (debugOptions & MapDebugOptions::Overdraw) {
        const float overdraw = 1.0f / 8.0f;
        return gfx::ColorMode {
            gfx::ColorMode::Add {
                gfx::ColorBlendFactorType::ConstantColor,
                gfx::ColorBlendFactorType::One
            },
            Color { overdraw, overdraw, overdraw, 0.0f },
            gfx::ColorMode::Mask { true, true, true, true }
        };
    } else if (pass == RenderPass::Translucent) {
        return gfx::ColorMode::alphaBlended();
    } else {
        return gfx::ColorMode::unblended();
    }
}

} // namespace mbgl