Skip to content

Commit

Permalink
Move UBO updates from render layers to tweakers (#2703)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSylvester authored Sep 10, 2024
1 parent f74f4ff commit 29319a7
Show file tree
Hide file tree
Showing 24 changed files with 566 additions and 660 deletions.
17 changes: 16 additions & 1 deletion include/mbgl/gfx/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@

namespace mbgl {

class Bucket;
class Color;
class PaintParameters;
class LayerTweaker;
class PaintParameters;
class PaintPropertyBindersBase;
enum class RenderPass : uint8_t;
class RenderTile;

using LayerTweakerPtr = std::shared_ptr<LayerTweaker>;
using RenderTiles = std::shared_ptr<const std::vector<std::reference_wrapper<const RenderTile>>>;

namespace gfx {

Expand Down Expand Up @@ -245,6 +249,17 @@ class Drawable {
/// Set origin point
void setOrigin(std::optional<Point<double>> p) { origin = std::move(p); }

/// Get the property binders used for property updates
PaintPropertyBindersBase* getBinders();
const PaintPropertyBindersBase* getBinders() const;

/// Set the property binders used for property updates
void setBinders(std::shared_ptr<Bucket>, PaintPropertyBindersBase*);

const RenderTile* getRenderTile() const;
const std::shared_ptr<Bucket>& getBucket() const;
void setRenderTile(Immutable<std::vector<RenderTile>>, const RenderTile*);

const std::chrono::duration<double> createTime = util::MonotonicTimer::now();

protected:
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/renderer/layer_tweaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class LayerProperties;
enum class TranslateAnchorType : bool;
} // namespace style

class TransformState;
class LayerGroupBase;
class PaintParameters;
class RenderTree;
class TransformState;
class UnwrappedTileID;

/**
Expand Down
8 changes: 4 additions & 4 deletions include/mbgl/util/containers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

namespace mbgl {
#if MLN_USE_UNORDERED_DENSE
template <typename Key, typename Value>
using unordered_map = ankerl::unordered_dense::map<Key, Value>;
template <typename Key, typename Value, typename Hash = std::hash<Key>>
using unordered_map = ankerl::unordered_dense::map<Key, Value, Hash>;

template <typename Value>
using unordered_set = ankerl::unordered_dense::set<Value>;
#else
template <typename Key, typename Value>
using unordered_map = std::unordered_map<Key, Value>;
template <typename Key, typename Value, typename Hash = std::hash<Key>>
using unordered_map = std::unordered_map<Key, Value, Hash>;

template <typename Value>
using unordered_set = std::unordered_set<Value>;
Expand Down
33 changes: 33 additions & 0 deletions src/mbgl/gfx/drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
#include <mbgl/gfx/index_vector.hpp>
#include <mbgl/gfx/types.hpp>
#include <mbgl/renderer/render_pass.hpp>
#include <mbgl/renderer/render_tile.hpp>

namespace mbgl {
namespace gfx {

struct Drawable::Impl {
gfx::ColorMode colorMode = gfx::ColorMode::disabled();
gfx::CullFaceMode cullFaceMode = gfx::CullFaceMode::disabled();

std::shared_ptr<Bucket> bucket;
PaintPropertyBindersBase* binders = nullptr; // owned by `bucket`

Immutable<std::vector<RenderTile>> renderTiles = makeMutable<std::vector<RenderTile>>();
const RenderTile* renderTile = nullptr; // owned by `renderTiles`
};

Drawable::Drawable(std::string name_)
Expand Down Expand Up @@ -57,5 +64,31 @@ void Drawable::setTexture(std::shared_ptr<gfx::Texture2D> texture, size_t id) {
textures[id] = std::move(texture);
}

PaintPropertyBindersBase* Drawable::getBinders() {
return impl->binders;
}
const PaintPropertyBindersBase* Drawable::getBinders() const {
return impl->binders;
}

/// Set the property binders used for property updates
void Drawable::setBinders(std::shared_ptr<Bucket> bucket_, PaintPropertyBindersBase* binders_) {
impl->bucket = std::move(bucket_);
impl->binders = binders_;
}

const RenderTile* Drawable::getRenderTile() const {
return impl->renderTile;
}

const std::shared_ptr<Bucket>& Drawable::getBucket() const {
return impl->bucket;
}

void Drawable::setRenderTile(Immutable<std::vector<RenderTile>> renderTiles_, const RenderTile* tile_) {
impl->renderTiles = std::move(renderTiles_);
impl->renderTile = tile_;
}

} // namespace gfx
} // namespace mbgl
25 changes: 23 additions & 2 deletions src/mbgl/renderer/layers/circle_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <mbgl/gfx/context.hpp>
#include <mbgl/gfx/drawable.hpp>
#include <mbgl/programs/circle_program.hpp>
#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/render_tree.hpp>
Expand Down Expand Up @@ -34,7 +35,7 @@ void CircleLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamete
const auto debugGroup = parameters.encoder->createDebugGroup(label.c_str());
#endif

const auto zoom = parameters.state.getZoom();
const auto zoom = static_cast<float>(parameters.state.getZoom());
const bool pitchWithMap = evaluated.get<CirclePitchAlignment>() == AlignmentType::Map;
const bool scaleWithMap = evaluated.get<CirclePitchScale>() == CirclePitchScaleType::Map;

Expand Down Expand Up @@ -64,14 +65,20 @@ void CircleLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamete
}
const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped();

auto* binders = static_cast<CircleProgram::Binders*>(drawable.getBinders());
if (!binders) {
assert(false);
return;
}

const auto& translation = evaluated.get<CircleTranslate>();
const auto anchor = evaluated.get<CircleTranslateAnchor>();
constexpr bool inViewportPixelUnits = false; // from RenderTile::translatedMatrix
constexpr bool nearClipped = false;
const auto matrix = getTileMatrix(
tileID, parameters, translation, anchor, nearClipped, inViewportPixelUnits, drawable);

const auto pixelsToTileUnits = tileID.pixelsToTileUnits(1.0f, static_cast<float>(zoom));
const auto pixelsToTileUnits = tileID.pixelsToTileUnits(1.0f, zoom);
const auto extrudeScale = pitchWithMap ? std::array<float, 2>{pixelsToTileUnits, pixelsToTileUnits}
: parameters.pixelsToGLUnits;

Expand All @@ -82,6 +89,20 @@ void CircleLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamete

auto& drawableUniforms = drawable.mutableUniformBuffers();
drawableUniforms.createOrUpdate(idCircleDrawableUBO, &drawableUBO, context);

const CircleInterpolateUBO interpolateUBO = {
/* .color_t = */ std::get<0>(binders->get<CircleColor>()->interpolationFactor(zoom)),
/* .radius_t = */ std::get<0>(binders->get<CircleRadius>()->interpolationFactor(zoom)),
/* .blur_t = */ std::get<0>(binders->get<CircleBlur>()->interpolationFactor(zoom)),
/* .opacity_t = */ std::get<0>(binders->get<CircleOpacity>()->interpolationFactor(zoom)),
/* .stroke_color_t = */
std::get<0>(binders->get<CircleStrokeColor>()->interpolationFactor(zoom)),
/* .stroke_width_t = */
std::get<0>(binders->get<CircleStrokeWidth>()->interpolationFactor(zoom)),
/* .stroke_opacity_t = */
std::get<0>(binders->get<CircleStrokeOpacity>()->interpolationFactor(zoom)),
/* .padding = */ 0};
drawableUniforms.createOrUpdate(idCircleInterpolateUBO, &interpolateUBO, context);
});
}

Expand Down
38 changes: 36 additions & 2 deletions src/mbgl/renderer/layers/fill_extrusion_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
#include <mbgl/gfx/renderer_backend.hpp>
#include <mbgl/programs/fill_extrusion_program.hpp>
#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/renderer/render_tree.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/paint_property_binder.hpp>
#include <mbgl/shaders/fill_extrusion_layer_ubo.hpp>
#include <mbgl/shaders/shader_program_base.hpp>
#include <mbgl/style/layers/fill_extrusion_layer_properties.hpp>
#include <mbgl/util/convert.hpp>
#include <mbgl/util/std.hpp>

#if MLN_RENDER_BACKEND_METAL
#include <mbgl/shaders/mtl/fill_extrusion.hpp>
Expand Down Expand Up @@ -62,11 +61,22 @@ void FillExtrusionLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintP

propertiesUpdated = false;

const auto zoom = static_cast<float>(parameters.state.getZoom());
const auto defPattern = mbgl::Faded<expression::Image>{"", ""};
const auto fillPatternValue = evaluated.get<FillExtrusionPattern>().constantOr(defPattern);

visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
if (!drawable.getTileID() || !checkTweakDrawable(drawable)) {
return;
}

auto* binders = static_cast<FillExtrusionProgram::Binders*>(drawable.getBinders());
const auto* tile = drawable.getRenderTile();
if (!binders || !tile) {
assert(false);
return;
}

const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped();
const auto& translation = evaluated.get<FillExtrusionTranslate>();
const auto anchor = evaluated.get<FillExtrusionTranslateAnchor>();
Expand All @@ -90,6 +100,13 @@ void FillExtrusionLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintP
textureSize = tex->getSize();
}

const auto hasPattern = !!drawable.getType();
const auto patternPosA = tile->getPattern(fillPatternValue.from.id());
const auto patternPosB = tile->getPattern(fillPatternValue.to.id());
if (hasPattern) {
binders->setPatternParameters(patternPosA, patternPosB, crossfade);
}

const FillExtrusionDrawableUBO drawableUBO = {
/* .matrix = */ util::cast<float>(matrix),
/* .texsize = */ {static_cast<float>(textureSize.width), static_cast<float>(textureSize.height)},
Expand All @@ -98,8 +115,25 @@ void FillExtrusionLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintP
/* .height_factor = */ heightFactor,
/* .tile_ratio = */ tileRatio};

const FillExtrusionInterpolateUBO interpUBO = {
/* .base_t = */ std::get<0>(binders->get<FillExtrusionBase>()->interpolationFactor(zoom)),
/* .height_t = */ std::get<0>(binders->get<FillExtrusionHeight>()->interpolationFactor(zoom)),
/* .color_t = */ std::get<0>(binders->get<FillExtrusionColor>()->interpolationFactor(zoom)),
/* .pattern_from_t = */ std::get<0>(binders->get<FillExtrusionPattern>()->interpolationFactor(zoom)),
/* .pattern_to_t = */ std::get<0>(binders->get<FillExtrusionPattern>()->interpolationFactor(zoom)),
/* .pad = */ 0,
0,
0};

const FillExtrusionTilePropsUBO tilePropsUBO = {
/* pattern_from = */ patternPosA ? util::cast<float>(patternPosA->tlbr()) : std::array<float, 4>{0},
/* pattern_to = */ patternPosB ? util::cast<float>(patternPosB->tlbr()) : std::array<float, 4>{0},
};

auto& drawableUniforms = drawable.mutableUniformBuffers();
drawableUniforms.createOrUpdate(idFillExtrusionDrawableUBO, &drawableUBO, context);
drawableUniforms.createOrUpdate(idFillExtrusionTilePropsUBO, &tilePropsUBO, context);
drawableUniforms.createOrUpdate(idFillExtrusionInterpolateUBO, &interpUBO, context);
});
}

Expand Down
59 changes: 58 additions & 1 deletion src/mbgl/renderer/layers/fill_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mbgl/gfx/renderer_backend.hpp>
#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/renderer/layers/render_fill_layer.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/renderer/render_tree.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/paint_property_binder.hpp>
Expand Down Expand Up @@ -53,6 +54,7 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters

const auto& translation = evaluated.get<FillTranslate>();
const auto anchor = evaluated.get<FillTranslateAnchor>();
const auto zoom = static_cast<float>(parameters.state.getZoom());
const auto intZoom = parameters.state.getIntegerZoom();

visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
Expand All @@ -62,9 +64,20 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters

const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped();

auto* binders = static_cast<FillProgram::Binders*>(drawable.getBinders());
const auto* tile = drawable.getRenderTile();
if (!binders || !tile) {
assert(false);
return;
}

const auto& fillPatternValue = evaluated.get<FillPattern>().constantOr(Faded<expression::Image>{"", ""});
const auto patternPosA = tile->getPattern(fillPatternValue.from.id());
const auto patternPosB = tile->getPattern(fillPatternValue.to.id());
binders->setPatternParameters(patternPosA, patternPosB, crossfade);

constexpr bool inViewportPixelUnits = false; // from RenderTile::translatedMatrix
constexpr bool nearClipped = false;

const auto matrix = getTileMatrix(
tileID, parameters, translation, anchor, nearClipped, inViewportPixelUnits, drawable);

Expand All @@ -88,11 +101,27 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
case RenderFillLayer::FillVariant::Fill: {
const FillDrawableUBO drawableUBO = {/*.matrix=*/util::cast<float>(matrix)};
drawableUniforms.createOrUpdate(idFillDrawableUBO, &drawableUBO, context);

const auto fillInterpolateUBO = FillInterpolateUBO{
/* .color_t = */ std::get<0>(binders->get<FillColor>()->interpolationFactor(zoom)),
/* .opacity_t = */ std::get<0>(binders->get<FillOpacity>()->interpolationFactor(zoom)),
0,
0,
};
drawableUniforms.createOrUpdate(idFillInterpolateUBO, &fillInterpolateUBO, context);
break;
}
case RenderFillLayer::FillVariant::FillOutline: {
const FillOutlineDrawableUBO drawableUBO = {/*.matrix=*/util::cast<float>(matrix)};
drawableUniforms.createOrUpdate(idFillDrawableUBO, &drawableUBO, context);

const auto fillOutlineInterpolateUBO = FillOutlineInterpolateUBO{
/* .color_t = */ std::get<0>(binders->get<FillOutlineColor>()->interpolationFactor(zoom)),
/* .opacity_t = */ std::get<0>(binders->get<FillOpacity>()->interpolationFactor(zoom)),
0,
0,
};
drawableUniforms.createOrUpdate(idFillInterpolateUBO, &fillOutlineInterpolateUBO, context);
break;
}
case RenderFillLayer::FillVariant::FillPattern: {
Expand All @@ -105,6 +134,20 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
0,
};
drawableUniforms.createOrUpdate(idFillDrawableUBO, &drawableUBO, context);

const auto fillPatternInterpolateUBO = FillPatternInterpolateUBO{
/* .pattern_from_t = */ std::get<0>(binders->get<FillPattern>()->interpolationFactor(zoom)),
/* .pattern_to_t = */ std::get<0>(binders->get<FillPattern>()->interpolationFactor(zoom)),
/* .opacity_t = */ std::get<0>(binders->get<FillOpacity>()->interpolationFactor(zoom)),
0,
};
drawableUniforms.createOrUpdate(idFillInterpolateUBO, &fillPatternInterpolateUBO, context);

const auto fillPatternTilePropsUBO = FillPatternTilePropsUBO{
/* pattern_from = */ patternPosA ? util::cast<float>(patternPosA->tlbr()) : std::array<float, 4>{0},
/* pattern_to = */ patternPosB ? util::cast<float>(patternPosB->tlbr()) : std::array<float, 4>{0},
};
drawableUniforms.createOrUpdate(idFillTilePropsUBO, &fillPatternTilePropsUBO, context);
break;
}
case RenderFillLayer::FillVariant::FillOutlinePattern: {
Expand All @@ -116,6 +159,20 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
/*.tile_ratio = */ tileRatio,
0};
drawableUniforms.createOrUpdate(idFillDrawableUBO, &drawableUBO, context);

const auto fillOutlinePatternInterpolateUBO = FillPatternInterpolateUBO{
/* .pattern_from_t = */ std::get<0>(binders->get<FillPattern>()->interpolationFactor(zoom)),
/* .pattern_to_t = */ std::get<0>(binders->get<FillPattern>()->interpolationFactor(zoom)),
/* .opacity_t = */ std::get<0>(binders->get<FillOpacity>()->interpolationFactor(zoom)),
0,
};
drawableUniforms.createOrUpdate(idFillInterpolateUBO, &fillOutlinePatternInterpolateUBO, context);

const auto fillOutlinePatternTilePropsUBO = FillOutlinePatternTilePropsUBO{
/* pattern_from = */ patternPosA ? util::cast<float>(patternPosA->tlbr()) : std::array<float, 4>{0},
/* pattern_to = */ patternPosB ? util::cast<float>(patternPosB->tlbr()) : std::array<float, 4>{0},
};
drawableUniforms.createOrUpdate(idFillTilePropsUBO, &fillOutlinePatternTilePropsUBO, context);
break;
}
case RenderFillLayer::FillVariant::FillOutlineTriangulated: {
Expand Down
Loading

0 comments on commit 29319a7

Please sign in to comment.