#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace mbgl { using namespace style; namespace { struct alignas(16) SymbolDrawableUBO { /* 0 */ std::array matrix; /* 64 */ std::array label_plane_matrix; /* 128 */ std::array coord_matrix; /* 192 */ std::array texsize; /* 200 */ std::array texsize_icon; /* 208 */ float gamma_scale; /* 212 */ float device_pixel_ratio; /* 216 */ float camera_to_center_distance; /* 220 */ float pitch; /* 224 */ /*bool*/ int rotate_symbol; /* 228 */ float aspect_ratio; /* 232 */ std::array pad; /* 240 */ }; static_assert(sizeof(SymbolDrawableUBO) == 15 * 16); /// Dynamic UBO struct alignas(16) SymbolDynamicUBO { /* 0 */ float fade_change; /* 4 */ float pad1; /* 8 */ std::array pad2; /* 16 */ }; static_assert(sizeof(SymbolDynamicUBO) == 16); /// Evaluated properties that do not depend on the tile struct alignas(16) SymbolDrawablePaintUBO { /* 0 */ Color fill_color; /* 16 */ Color halo_color; /* 32 */ float opacity; /* 36 */ float halo_width; /* 40 */ float halo_blur; /* 44 */ float padding; /* 48 */ }; static_assert(sizeof(SymbolDrawablePaintUBO) == 3 * 16); Size getTexSize(const gfx::Drawable& drawable, const std::string_view name) { if (const auto& shader = drawable.getShader()) { if (const auto index = shader->getSamplerLocation(name)) { if (const auto& tex = drawable.getTexture(*index)) { return tex->getSize(); } } } return {0, 0}; } std::array toArray(const Size& s) { return util::cast(std::array{s.width, s.height}); } constexpr auto texUniformName = "u_texture"; constexpr auto texIconUniformName = "u_texture_icon"; SymbolDrawablePaintUBO buildPaintUBO(bool isText, const SymbolPaintProperties::PossiblyEvaluated& evaluated) { return { /*.fill_color=*/isText ? constOrDefault(evaluated) : constOrDefault(evaluated), /*.halo_color=*/ isText ? constOrDefault(evaluated) : constOrDefault(evaluated), /*.opacity=*/isText ? constOrDefault(evaluated) : constOrDefault(evaluated), /*.halo_width=*/ isText ? constOrDefault(evaluated) : constOrDefault(evaluated), /*.halo_blur=*/isText ? constOrDefault(evaluated) : constOrDefault(evaluated), /*.padding=*/0, }; } } // namespace void SymbolLayerTweaker::execute(LayerGroupBase& layerGroup, const RenderTree& renderTree, const PaintParameters& parameters) { auto& context = parameters.context; const auto& state = parameters.state; const auto& evaluated = static_cast(*evaluatedProperties).evaluated; if (layerGroup.empty()) { return; } #if !defined(NDEBUG) const auto label = layerGroup.getName() + "-update-uniforms"; const auto debugGroup = parameters.encoder->createDebugGroup(label.c_str()); #endif layerGroup.visitDrawables([&](gfx::Drawable& drawable) { if (!drawable.getTileID() || !drawable.getData()) { return; } const auto tileID = drawable.getTileID()->toUnwrapped(); const auto& symbolData = static_cast(*drawable.getData()); const auto isText = (symbolData.symbolType == SymbolType::Text); if (isText && !textPaintBuffer) { const auto props = buildPaintUBO(true, evaluated); textPaintBuffer = parameters.context.createUniformBuffer(&props, sizeof(props)); } if (!isText && !iconPaintBuffer) { const auto props = buildPaintUBO(false, evaluated); iconPaintBuffer = parameters.context.createUniformBuffer(&props, sizeof(props)); } // from RenderTile::translatedMatrix const auto translate = isText ? evaluated.get() : evaluated.get(); const auto anchor = isText ? evaluated.get() : evaluated.get(); constexpr bool nearClipped = false; constexpr bool inViewportPixelUnits = false; const auto matrix = getTileMatrix( tileID, renderTree, state, translate, anchor, nearClipped, inViewportPixelUnits); // from symbol_program, makeValues const auto currentZoom = static_cast(state.getZoom()); const float pixelsToTileUnits = tileID.pixelsToTileUnits(1.f, currentZoom); const bool pitchWithMap = symbolData.pitchAlignment == style::AlignmentType::Map; const bool rotateWithMap = symbolData.rotationAlignment == style::AlignmentType::Map; const bool alongLine = symbolData.placement != SymbolPlacementType::Point && symbolData.rotationAlignment == AlignmentType::Map; const bool hasVariablePlacement = symbolData.bucketVariablePlacement && (isText || symbolData.textFit != IconTextFitType::None); const mat4 labelPlaneMatrix = (alongLine || hasVariablePlacement) ? matrix::identity4() : getLabelPlaneMatrix( matrix, pitchWithMap, rotateWithMap, state, pixelsToTileUnits); const mat4 glCoordMatrix = getGlCoordMatrix(matrix, pitchWithMap, rotateWithMap, state, pixelsToTileUnits); const auto camDist = state.getCameraToCenterDistance(); const float gammaScale = (symbolData.pitchAlignment == AlignmentType::Map ? static_cast(std::cos(state.getPitch())) * camDist : 1.0f); // Line label rotation happens in `updateLineLabels`/`reprojectLineLabels`` // Pitched point labels are automatically rotated by the labelPlaneMatrix projection // Unpitched point labels need to have their rotation applied after projection const bool rotateInShader = rotateWithMap && !pitchWithMap && !alongLine; const SymbolDrawableUBO drawableUBO = { /*.matrix=*/util::cast(matrix), /*.label_plane_matrix=*/util::cast(labelPlaneMatrix), /*.coord_matrix=*/util::cast(glCoordMatrix), /*.texsize=*/toArray(getTexSize(drawable, texUniformName)), /*.texsize_icon=*/toArray(getTexSize(drawable, texIconUniformName)), /*.gamma_scale=*/gammaScale, /*.device_pixel_ratio=*/parameters.pixelRatio, /*.camera_to_center_distance=*/camDist, /*.pitch=*/static_cast(state.getPitch()), /*.rotate_symbol=*/rotateInShader, /*.aspect_ratio=*/state.getSize().aspectRatio(), /*.pad=*/{0}, }; const SymbolDynamicUBO dynamicUBO = {/*.fade_change=*/parameters.symbolFadeChange, /*.pad1=*/0, /*.pad2=*/{0, 0}}; auto& uniforms = drawable.mutableUniformBuffers(); uniforms.createOrUpdate(SymbolDrawableUBOName, &drawableUBO, context); uniforms.createOrUpdate(SymbolDynamicUBOName, &dynamicUBO, context); uniforms.addOrReplace(SymbolDrawablePaintUBOName, isText ? textPaintBuffer : iconPaintBuffer); }); } } // namespace mbgl