1 #include <mbgl/renderer/paint_parameters.hpp>
2 #include <mbgl/renderer/update_parameters.hpp>
3 #include <mbgl/renderer/render_static_data.hpp>
4 #include <mbgl/map/transform_state.hpp>
5
6 namespace mbgl {
7
PaintParameters(gl::Context & context_,float pixelRatio_,GLContextMode contextMode_,RendererBackend & backend_,const UpdateParameters & updateParameters,const EvaluatedLight & evaluatedLight_,RenderStaticData & staticData_,ImageManager & imageManager_,LineAtlas & lineAtlas_)8 PaintParameters::PaintParameters(gl::Context& context_,
9 float pixelRatio_,
10 GLContextMode contextMode_,
11 RendererBackend& backend_,
12 const UpdateParameters& updateParameters,
13 const EvaluatedLight& evaluatedLight_,
14 RenderStaticData& staticData_,
15 ImageManager& imageManager_,
16 LineAtlas& lineAtlas_)
17 : context(context_),
18 backend(backend_),
19 state(updateParameters.transformState),
20 evaluatedLight(evaluatedLight_),
21 staticData(staticData_),
22 imageManager(imageManager_),
23 lineAtlas(lineAtlas_),
24 mapMode(updateParameters.mode),
25 debugOptions(updateParameters.debugOptions),
26 contextMode(contextMode_),
27 timePoint(updateParameters.timePoint),
28 pixelRatio(pixelRatio_),
29 #ifndef NDEBUG
30 programs((debugOptions & MapDebugOptions::Overdraw) ? staticData_.overdrawPrograms : staticData_.programs)
31 #else
32 programs(staticData_.programs)
33 #endif
34 {
35 // Update the default matrices to the current viewport dimensions.
36 state.getProjMatrix(projMatrix);
37
38 // Also compute a projection matrix that aligns with the current pixel grid, taking into account
39 // odd viewport sizes.
40 state.getProjMatrix(alignedProjMatrix, 1, true);
41
42 // Calculate a second projection matrix with the near plane clipped to 100 so as
43 // not to waste lots of depth buffer precision on very close empty space, for layer
44 // types (fill-extrusion) that use the depth buffer to emulate real-world space.
45 state.getProjMatrix(nearClippedProjMatrix, 100);
46
47 pixelsToGLUnits = {{ 2.0f / state.getSize().width, -2.0f / state.getSize().height }};
48
49 if (state.getViewportMode() == ViewportMode::FlippedY) {
50 pixelsToGLUnits[1] *= -1;
51 }
52 }
53
matrixForTile(const UnwrappedTileID & tileID,bool aligned) const54 mat4 PaintParameters::matrixForTile(const UnwrappedTileID& tileID, bool aligned) const {
55 mat4 matrix;
56 state.matrixFor(matrix, tileID);
57 matrix::multiply(matrix, aligned ? alignedProjMatrix : projMatrix, matrix);
58 return matrix;
59 }
60
depthModeForSublayer(uint8_t n,gl::DepthMode::Mask mask) const61 gl::DepthMode PaintParameters::depthModeForSublayer(uint8_t n, gl::DepthMode::Mask mask) const {
62 float nearDepth = ((1 + currentLayer) * numSublayers + n) * depthEpsilon;
63 float farDepth = nearDepth + depthRangeSize;
64 return gl::DepthMode { gl::DepthMode::LessEqual, mask, { nearDepth, farDepth } };
65 }
66
depthModeFor3D(gl::DepthMode::Mask mask) const67 gl::DepthMode PaintParameters::depthModeFor3D(gl::DepthMode::Mask mask) const {
68 return gl::DepthMode { gl::DepthMode::LessEqual, mask, { 0.0, 1.0 } };
69 }
70
stencilModeForClipping(const ClipID & id) const71 gl::StencilMode PaintParameters::stencilModeForClipping(const ClipID& id) const {
72 return gl::StencilMode {
73 gl::StencilMode::Equal { static_cast<uint32_t>(id.mask.to_ulong()) },
74 static_cast<int32_t>(id.reference.to_ulong()),
75 0,
76 gl::StencilMode::Keep,
77 gl::StencilMode::Keep,
78 gl::StencilMode::Replace
79 };
80 }
81
colorModeForRenderPass() const82 gl::ColorMode PaintParameters::colorModeForRenderPass() const {
83 if (debugOptions & MapDebugOptions::Overdraw) {
84 const float overdraw = 1.0f / 8.0f;
85 return gl::ColorMode {
86 gl::ColorMode::Add {
87 gl::ColorMode::ConstantColor,
88 gl::ColorMode::One
89 },
90 Color { overdraw, overdraw, overdraw, 0.0f },
91 gl::ColorMode::Mask { true, true, true, true }
92 };
93 } else if (pass == RenderPass::Translucent) {
94 return gl::ColorMode::alphaBlended();
95 } else {
96 return gl::ColorMode::unblended();
97 }
98 }
99
100 } // namespace mbgl
101