summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/painter_debug.cpp
blob: 54286316985da05894ee8bd1d061b08ec2b6c333 (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
#include <mbgl/renderer/painter.hpp>
#include <mbgl/renderer/debug_bucket.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/tile/tile.hpp>
#include <mbgl/shader/shaders.hpp>
#include <mbgl/shader/fill_uniforms.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/gl/debugging.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/util/color.hpp>

namespace mbgl {

void Painter::renderTileDebug(const RenderTile& renderTile) {
    if (frame.debugOptions == MapDebugOptions::NoDebug)
        return;

    MBGL_DEBUG_GROUP(std::string { "debug " } + util::toString(renderTile.id));

    auto draw = [&] (Color color, auto subject) {
        context.draw({
            gl::DepthMode::disabled(),
            stencilModeForClipping(renderTile.clip),
            gl::ColorMode::unblended(),
            shaders->fill,
            FillColorUniforms::values(
                renderTile.matrix,
                1.0f,
                color,
                color,
                context.viewport.getCurrentValue().size
            ),
            subject
        });
    };

    if (frame.debugOptions & (MapDebugOptions::Timestamps | MapDebugOptions::ParseStatus)) {
        Tile& tile = renderTile.tile;
        if (!tile.debugBucket || tile.debugBucket->renderable != tile.isRenderable() ||
            tile.debugBucket->complete != tile.isComplete() ||
            !(tile.debugBucket->modified == tile.modified) ||
            !(tile.debugBucket->expires == tile.expires) ||
            tile.debugBucket->debugMode != frame.debugOptions) {
            tile.debugBucket = std::make_unique<DebugBucket>(
                tile.id, tile.isRenderable(), tile.isComplete(), tile.modified,
                tile.expires, frame.debugOptions, context);
        }

        const auto& vertexBuffer = tile.debugBucket->vertexBuffer;

        draw(Color::white(), gl::Unindexed<gl::Lines>(vertexBuffer, 4.0f * frame.pixelRatio));
        draw(Color::black(), gl::Unindexed<gl::Points>(vertexBuffer, 2.0f));
        draw(Color::black(), gl::Unindexed<gl::Lines>(vertexBuffer, 2.0f * frame.pixelRatio));
    }

    if (frame.debugOptions & MapDebugOptions::TileBorders) {
        draw(Color::red(), gl::Unindexed<gl::LineStrip>(tileLineStripVertexBuffer, 4.0f * frame.pixelRatio));
    }
}

#ifndef NDEBUG
void Painter::renderClipMasks(PaintParameters&) {
    context.setStencilMode(gl::StencilMode::disabled());
    context.setDepthMode(gl::DepthMode::disabled());
    context.setColorMode(gl::ColorMode::unblended());
    context.program = 0;

#if not MBGL_USE_GLES2
    context.pixelZoom = { 1, 1 };
    context.rasterPos = { -1, -1, 0, 0 };

    // Read the stencil buffer
    const auto viewport = context.viewport.getCurrentValue();
    auto pixels = std::make_unique<uint8_t[]>(viewport.size.width * viewport.size.height);
    MBGL_CHECK_ERROR(glReadPixels(
                viewport.x,           // GLint x
                viewport.y,           // GLint y
                viewport.size.width,  // GLsizei width
                viewport.size.height, // GLsizei height
                GL_STENCIL_INDEX,     // GLenum format
                GL_UNSIGNED_BYTE,     // GLenum type
                pixels.get()          // GLvoid * data
                ));

    // Scale the Stencil buffer to cover the entire color space.
    auto it = pixels.get();
    auto end = it + viewport.size.width * viewport.size.height;
    const auto factor = 255.0f / *std::max_element(it, end);
    for (; it != end; ++it) {
        *it *= factor;
    }

    MBGL_CHECK_ERROR(glWindowPos2i(viewport.x, viewport.y));
    MBGL_CHECK_ERROR(glDrawPixels(viewport.size.width, viewport.size.height, GL_LUMINANCE,
                                  GL_UNSIGNED_BYTE, pixels.get()));
#endif // MBGL_USE_GLES2
}

void Painter::renderDepthBuffer(PaintParameters&) {
    context.setStencilMode(gl::StencilMode::disabled());
    context.setDepthMode(gl::DepthMode::disabled());
    context.setColorMode(gl::ColorMode::unblended());
    context.program = 0;

#if not MBGL_USE_GLES2
    context.pixelZoom = { 1, 1 };
    context.rasterPos = { -1, -1, 0, 0 };

    // Read the stencil buffer
    const auto viewport = context.viewport.getCurrentValue();
    auto pixels = std::make_unique<uint8_t[]>(viewport.size.width * viewport.size.height);

    const double base = 1.0 / (1.0 - depthRangeSize);
    glPixelTransferf(GL_DEPTH_SCALE, base);
    glPixelTransferf(GL_DEPTH_BIAS, 1.0 - base);

    MBGL_CHECK_ERROR(glReadPixels(
                viewport.x,           // GLint x
                viewport.y,           // GLint y
                viewport.size.width,  // GLsizei width
                viewport.size.height, // GLsizei height
                GL_DEPTH_COMPONENT,   // GLenum format
                GL_UNSIGNED_BYTE,     // GLenum type
                pixels.get()          // GLvoid * data
                ));

    MBGL_CHECK_ERROR(glWindowPos2i(viewport.x, viewport.y));
    MBGL_CHECK_ERROR(glDrawPixels(viewport.size.width, viewport.size.height, GL_LUMINANCE,
                                  GL_UNSIGNED_BYTE, pixels.get()));
#endif // MBGL_USE_GLES2
}
#endif // NDEBUG

} // namespace mbgl