Skip to content

Commit

Permalink
Line tweaker (#2050)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSylvester authored Jan 25, 2024
1 parent ec9afb5 commit e7d6f6a
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 152 deletions.
3 changes: 3 additions & 0 deletions src/mbgl/renderer/layers/background_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ void BackgroundLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintPara
}
layerGroup.setEnabled(true);

// properties are re-evaluated every time
propertiesUpdated = false;

std::optional<uint32_t> samplerLocation{};
visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
assert(drawable.getTileID());
Expand Down
12 changes: 4 additions & 8 deletions src/mbgl/renderer/layers/circle_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,14 @@ void CircleLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamete
/* .padding = */ 0,
0,
0};

if (!paintParamsUniformBuffer) {
paintParamsUniformBuffer = context.createUniformBuffer(&paintParamsUBO, sizeof(paintParamsUBO));
} else {
paintParamsUniformBuffer->update(&paintParamsUBO, sizeof(CirclePaintParamsUBO));
}
context.emplaceOrUpdateUniformBuffer(paintParamsUniformBuffer, &paintParamsUBO);

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

// Updated only with evaluated properties
if (!evaluatedPropsUniformBuffer) {
if (!evaluatedPropsUniformBuffer || propertiesUpdated) {
const CircleEvaluatedPropsUBO evaluatedPropsUBO = {
/* .color = */ constOrDefault<CircleColor>(evaluated),
/* .stroke_color = */ constOrDefault<CircleStrokeColor>(evaluated),
Expand All @@ -69,8 +64,9 @@ void CircleLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamete
/* .scale_with_map = */ scaleWithMap,
/* .pitch_with_map = */ pitchWithMap,
/* .padding = */ 0};
evaluatedPropsUniformBuffer = context.createUniformBuffer(&evaluatedPropsUBO, sizeof(evaluatedPropsUBO));
context.emplaceOrUpdateUniformBuffer(evaluatedPropsUniformBuffer, &evaluatedPropsUBO);
}
propertiesUpdated = false;

visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
assert(drawable.getTileID() || !"Circles only render with tiles");
Expand Down
7 changes: 2 additions & 5 deletions src/mbgl/renderer/layers/fill_extrusion_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ void FillExtrusionLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintP
/* .pad = */ 0,
0,
0};
if (!propsBuffer) {
propsBuffer = context.createUniformBuffer(&paramsUBO, sizeof(paramsUBO));
} else {
propsBuffer->update(&paramsUBO, sizeof(paramsUBO));
}
context.emplaceOrUpdateUniformBuffer(propsBuffer, &paramsUBO);
propertiesUpdated = false;

visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
if (!drawable.getTileID() || !checkTweakDrawable(drawable)) {
Expand Down
27 changes: 15 additions & 12 deletions src/mbgl/renderer/layers/heatmap_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static const StringIdentity idHeatmapEvaluatedPropsUBOName = stringIndexer().get

void HeatmapLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) {
auto& context = parameters.context;
const auto zoom = parameters.state.getZoom();
const auto& evaluated = static_cast<const HeatmapLayerProperties&>(*evaluatedProperties).evaluated;

if (layerGroup.empty()) {
Expand All @@ -36,17 +37,17 @@ void HeatmapLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamet
const auto debugGroup = parameters.encoder->createDebugGroup(label.c_str());
#endif

const auto zoom = parameters.state.getZoom();

if (!evaluatedPropsUniformBuffer) {
const HeatmapEvaluatedPropsUBO evaluatedPropsUBO = {
/* .weight = */ evaluated.get<HeatmapWeight>().constantOr(HeatmapWeight::defaultValue()),
/* .radius = */ evaluated.get<HeatmapRadius>().constantOr(HeatmapRadius::defaultValue()),
/* .intensity = */ evaluated.get<HeatmapIntensity>(),
/* .padding = */ 0};
evaluatedPropsUniformBuffer = parameters.context.createUniformBuffer(&evaluatedPropsUBO,
sizeof(evaluatedPropsUBO));
}
const auto getPropsBuffer = [&]() -> auto& {
if (!evaluatedPropsUniformBuffer || propertiesUpdated) {
const HeatmapEvaluatedPropsUBO evaluatedPropsUBO = {
/* .weight = */ evaluated.get<HeatmapWeight>().constantOr(HeatmapWeight::defaultValue()),
/* .radius = */ evaluated.get<HeatmapRadius>().constantOr(HeatmapRadius::defaultValue()),
/* .intensity = */ evaluated.get<HeatmapIntensity>(),
/* .padding = */ 0};
parameters.context.emplaceOrUpdateUniformBuffer(evaluatedPropsUniformBuffer, &evaluatedPropsUBO);
}
return evaluatedPropsUniformBuffer;
};

visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
if (!drawable.getTileID() || !checkTweakDrawable(drawable)) {
Expand All @@ -56,7 +57,7 @@ void HeatmapLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamet
const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped();

auto& uniforms = drawable.mutableUniformBuffers();
uniforms.addOrReplace(idHeatmapEvaluatedPropsUBOName, evaluatedPropsUniformBuffer);
uniforms.addOrReplace(idHeatmapEvaluatedPropsUBOName, getPropsBuffer());

constexpr bool nearClipped = false;
constexpr bool inViewportPixelUnits = false;
Expand All @@ -69,6 +70,8 @@ void HeatmapLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamet

uniforms.createOrUpdate(idHeatmapDrawableUBOName, &drawableUBO, context);
});

propertiesUpdated = false;
}

} // namespace mbgl
30 changes: 17 additions & 13 deletions src/mbgl/renderer/layers/heatmap_texture_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,27 @@ void HeatmapTextureLayerTweaker::execute(LayerGroupBase& layerGroup, const Paint
const auto debugGroup = parameters.encoder->createDebugGroup(label.c_str());
#endif

const auto getDrawableUBO = [&]() -> auto& {
if (!drawableBuffer) {
const auto& size = parameters.staticData.backendSize;
mat4 viewportMat;
matrix::ortho(viewportMat, 0, size.width, size.height, 0, -1, 1);
const HeatmapTextureDrawableUBO drawableUBO = {
/* .matrix = */ util::cast<float>(viewportMat),
/* .world = */ {static_cast<float>(size.width), static_cast<float>(size.height)},
/* .opacity = */ evaluated.get<HeatmapOpacity>(),
/* .pad1 = */ 0,
};
parameters.context.emplaceOrUpdateUniformBuffer(drawableBuffer, &drawableUBO);
}
return drawableBuffer;
};

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

const auto& size = parameters.staticData.backendSize;
mat4 viewportMat;
matrix::ortho(viewportMat, 0, size.width, size.height, 0, -1, 1);
const HeatmapTextureDrawableUBO drawableUBO = {
/* .matrix = */ util::cast<float>(viewportMat),
/* .world = */ {static_cast<float>(size.width), static_cast<float>(size.height)},
/* .opacity = */ evaluated.get<HeatmapOpacity>(),
/* .pad1 = */ 0,
};

drawable.mutableUniformBuffers().createOrUpdate(
idHeatmapTextureDrawableUBOName, &drawableUBO, parameters.context);
drawable.mutableUniformBuffers().addOrReplace(idHeatmapTextureDrawableUBOName, getDrawableUBO());
});
}

Expand Down
1 change: 1 addition & 0 deletions src/mbgl/renderer/layers/heatmap_texture_layer_tweaker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class HeatmapTextureLayerTweaker : public LayerTweaker {
void execute(LayerGroupBase&, const PaintParameters&) override;

protected:
gfx::UniformBufferPtr drawableBuffer;
};

} // namespace mbgl
34 changes: 21 additions & 13 deletions src/mbgl/renderer/layers/hillshade_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace mbgl {
using namespace style;
using namespace shaders;

static const StringIdentity idHillshadeDrawableUBOName = stringIndexer().get("HillshadeDrawableUBO");
static const StringIdentity idHillshadeEvaluatedPropsUBOName = stringIndexer().get("HillshadeEvaluatedPropsUBO");
namespace {
const StringIdentity idHillshadeDrawableUBOName = stringIndexer().get("HillshadeDrawableUBO");
const StringIdentity idHillshadeEvaluatedPropsUBOName = stringIndexer().get("HillshadeEvaluatedPropsUBO");

std::array<float, 2> getLatRange(const UnwrappedTileID& id) {
const LatLng latlng0 = LatLng(id);
Expand All @@ -27,10 +28,12 @@ std::array<float, 2> getLatRange(const UnwrappedTileID& id) {
std::array<float, 2> getLight(const PaintParameters& parameters,
const HillshadePaintProperties::PossiblyEvaluated& evaluated) {
float azimuthal = util::deg2radf(evaluated.get<HillshadeIlluminationDirection>());
if (evaluated.get<HillshadeIlluminationAnchor>() == HillshadeIlluminationAnchorType::Viewport)
if (evaluated.get<HillshadeIlluminationAnchor>() == HillshadeIlluminationAnchorType::Viewport) {
azimuthal = azimuthal - static_cast<float>(parameters.state.getBearing());
}
return {{evaluated.get<HillshadeExaggeration>(), azimuthal}};
}
} // namespace

void HillshadeLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) {
const auto& evaluated = static_cast<const HillshadeLayerProperties&>(*evaluatedProperties).evaluated;
Expand All @@ -44,13 +47,16 @@ void HillshadeLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParam
const auto debugGroup = parameters.encoder->createDebugGroup(label.c_str());
#endif

if (!evaluatedPropsUniformBuffer) {
HillshadeEvaluatedPropsUBO evaluatedPropsUBO = {/* .highlight = */ evaluated.get<HillshadeHighlightColor>(),
/* .shadow = */ evaluated.get<HillshadeShadowColor>(),
/* .accent = */ evaluated.get<HillshadeAccentColor>()};
evaluatedPropsUniformBuffer = parameters.context.createUniformBuffer(&evaluatedPropsUBO,
sizeof(evaluatedPropsUBO));
}
const auto getPropsBuffer = [&]() -> auto& {
if (!evaluatedPropsUniformBuffer || propertiesUpdated) {
const HillshadeEvaluatedPropsUBO evaluatedPropsUBO = {
/* .highlight = */ evaluated.get<HillshadeHighlightColor>(),
/* .shadow = */ evaluated.get<HillshadeShadowColor>(),
/* .accent = */ evaluated.get<HillshadeAccentColor>()};
parameters.context.emplaceOrUpdateUniformBuffer(evaluatedPropsUniformBuffer, &evaluatedPropsUBO);
}
return evaluatedPropsUniformBuffer;
};

visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
if (!drawable.getTileID() || !checkTweakDrawable(drawable)) {
Expand All @@ -59,16 +65,18 @@ void HillshadeLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParam

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

drawable.mutableUniformBuffers().addOrReplace(idHillshadeEvaluatedPropsUBOName, evaluatedPropsUniformBuffer);
auto& uniforms = drawable.mutableUniformBuffers();
uniforms.addOrReplace(idHillshadeEvaluatedPropsUBOName, getPropsBuffer());

const auto matrix = getTileMatrix(
tileID, parameters, {0.f, 0.f}, TranslateAnchorType::Viewport, false, false, drawable, true);
HillshadeDrawableUBO drawableUBO = {/* .matrix = */ util::cast<float>(matrix),
/* .latrange = */ getLatRange(tileID),
/* .light = */ getLight(parameters, evaluated)};

drawable.mutableUniformBuffers().createOrUpdate(idHillshadeDrawableUBOName, &drawableUBO, parameters.context);
uniforms.createOrUpdate(idHillshadeDrawableUBOName, &drawableUBO, parameters.context);
});

propertiesUpdated = false;
}

} // namespace mbgl
19 changes: 10 additions & 9 deletions src/mbgl/renderer/layers/hillshade_prepare_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ namespace mbgl {
using namespace style;
using namespace shaders;

static const StringIdentity idHillshadePrepareDrawableUBOName = stringIndexer().get("HillshadePrepareDrawableUBO");
namespace {
const StringIdentity idHillshadePrepareDrawableUBOName = stringIndexer().get("HillshadePrepareDrawableUBO");

const std::array<float, 4>& getUnpackVector(Tileset::DEMEncoding encoding) {
// https://www.mapbox.com/help/access-elevation-data/#mapbox-terrain-rgb
static const std::array<float, 4> unpackMapbox = {{6553.6f, 25.6f, 0.1f, 10000.0f}};
// https://aws.amazon.com/public-datasets/terrain/
static const std::array<float, 4> unpackTerrarium = {{256.0f, 1.0f, 1.0f / 256.0f, 32768.0f}};
// https://www.mapbox.com/help/access-elevation-data/#mapbox-terrain-rgb
constexpr std::array<float, 4> unpackMapbox = {{6553.6f, 25.6f, 0.1f, 10000.0f}};

return encoding == Tileset::DEMEncoding::Terrarium ? unpackTerrarium : unpackMapbox;
// https://aws.amazon.com/public-datasets/terrain/
constexpr std::array<float, 4> unpackTerrarium = {{256.0f, 1.0f, 1.0f / 256.0f, 32768.0f}};

constexpr const std::array<float, 4>& getUnpackVector(const Tileset::DEMEncoding encoding) {
return (encoding == Tileset::DEMEncoding::Terrarium) ? unpackTerrarium : unpackMapbox;
}
} // namespace

void HillshadePrepareLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) {
// const auto& evaluated = static_cast<const HillshadeLayerProperties&>(*evaluatedProperties).evaluated;

if (layerGroup.empty()) {
return;
}
Expand Down
37 changes: 20 additions & 17 deletions src/mbgl/renderer/layers/line_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ static const StringIdentity idTexImageName = stringIndexer().get("u_image");

void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) {
auto& context = parameters.context;
const auto zoom = parameters.state.getZoom();
const auto& evaluated = static_cast<const LineLayerProperties&>(*evaluatedProperties).evaluated;
const auto& crossfade = static_cast<const LineLayerProperties&>(*evaluatedProperties).crossfade;

const auto zoom = parameters.state.getZoom();
// Each property UBO is updated at most once if new evaluated properties were set
bool simplePropertiesUpdated = propertiesUpdated;
bool gradientPropertiesUpdated = propertiesUpdated;
bool patternPropertiesUpdated = propertiesUpdated;
bool sdfPropertiesUpdated = propertiesUpdated;
propertiesUpdated = false;

const auto getLinePropsBuffer = [&]() {
if (!linePropertiesBuffer) {
if (!linePropertiesBuffer || simplePropertiesUpdated) {
const LinePropertiesUBO linePropertiesUBO{
/*color =*/evaluated.get<LineColor>().constantOr(LineColor::defaultValue()),
/*blur =*/evaluated.get<LineBlur>().constantOr(LineBlur::defaultValue()),
Expand All @@ -55,12 +61,13 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
0,
0,
0};
linePropertiesBuffer = context.createUniformBuffer(&linePropertiesUBO, sizeof(linePropertiesUBO));
context.emplaceOrUpdateUniformBuffer(linePropertiesBuffer, &linePropertiesUBO);
simplePropertiesUpdated = false;
}
return linePropertiesBuffer;
};
const auto getLineGradientPropsBuffer = [&]() {
if (!lineGradientPropertiesBuffer) {
if (!lineGradientPropertiesBuffer || gradientPropertiesUpdated) {
const LineGradientPropertiesUBO lineGradientPropertiesUBO{
/*blur =*/evaluated.get<LineBlur>().constantOr(LineBlur::defaultValue()),
/*opacity =*/evaluated.get<LineOpacity>().constantOr(LineOpacity::defaultValue()),
Expand All @@ -70,13 +77,13 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
0,
0,
0};
lineGradientPropertiesBuffer = context.createUniformBuffer(&lineGradientPropertiesUBO,
sizeof(lineGradientPropertiesUBO));
context.emplaceOrUpdateUniformBuffer(lineGradientPropertiesBuffer, &lineGradientPropertiesUBO);
gradientPropertiesUpdated = false;
}
return lineGradientPropertiesBuffer;
};
const auto getLinePatternPropsBuffer = [&]() {
if (!linePatternPropertiesBuffer) {
if (!linePatternPropertiesBuffer || patternPropertiesUpdated) {
const LinePatternPropertiesUBO linePatternPropertiesUBO{
/*blur =*/evaluated.get<LineBlur>().constantOr(LineBlur::defaultValue()),
/*opacity =*/evaluated.get<LineOpacity>().constantOr(LineOpacity::defaultValue()),
Expand All @@ -86,13 +93,13 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
0,
0,
0};
linePatternPropertiesBuffer = context.createUniformBuffer(&linePatternPropertiesUBO,
sizeof(linePatternPropertiesUBO));
context.emplaceOrUpdateUniformBuffer(linePatternPropertiesBuffer, &linePatternPropertiesUBO);
patternPropertiesUpdated = false;
}
return linePatternPropertiesBuffer;
};
const auto getLineSDFPropsBuffer = [&]() {
if (!lineSDFPropertiesBuffer) {
if (!lineSDFPropertiesBuffer || sdfPropertiesUpdated) {
const LineSDFPropertiesUBO lineSDFPropertiesUBO{
/*color =*/evaluated.get<LineColor>().constantOr(LineColor::defaultValue()),
/*blur =*/evaluated.get<LineBlur>().constantOr(LineBlur::defaultValue()),
Expand All @@ -103,19 +110,15 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
/*floorwidth =*/evaluated.get<LineFloorWidth>().constantOr(LineFloorWidth::defaultValue()),
0,
0};
lineSDFPropertiesBuffer = context.createUniformBuffer(&lineSDFPropertiesUBO, sizeof(lineSDFPropertiesUBO));
context.emplaceOrUpdateUniformBuffer(lineSDFPropertiesBuffer, &lineSDFPropertiesUBO);
sdfPropertiesUpdated = false;
}
return lineSDFPropertiesBuffer;
};

const LineDynamicUBO dynamicUBO = {
/*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, 0, 0};

if (!dynamicBuffer) {
dynamicBuffer = parameters.context.createUniformBuffer(&dynamicUBO, sizeof(dynamicUBO));
} else {
dynamicBuffer->update(&dynamicUBO, sizeof(dynamicUBO));
}
context.emplaceOrUpdateUniformBuffer(dynamicBuffer, &dynamicUBO);

visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
const auto shader = drawable.getShader();
Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/renderer/layers/raster_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ void RasterLayerTweaker::execute([[maybe_unused]] LayerGroupBase& layerGroup,
[[maybe_unused]] const PaintParameters& parameters) {
const auto& evaluated = static_cast<const RasterLayerProperties&>(*evaluatedProperties).evaluated;

propertiesUpdated = false;

visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
if (!checkTweakDrawable(drawable)) {
return;
Expand Down
5 changes: 2 additions & 3 deletions src/mbgl/renderer/layers/render_background_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ void RenderBackgroundLayer::evaluate(const PropertyEvaluationParameters& paramet
evaluatedProperties = std::move(properties);

#if MLN_DRAWABLE_RENDERER
if (layerGroup) {
auto newTweaker = std::make_shared<BackgroundLayerTweaker>(getID(), evaluatedProperties);
replaceTweaker(layerTweaker, std::move(newTweaker), {layerGroup});
if (layerTweaker) {
layerTweaker->updateProperties(evaluatedProperties);
}
#endif
}
Expand Down
5 changes: 2 additions & 3 deletions src/mbgl/renderer/layers/render_circle_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ void RenderCircleLayer::evaluate(const PropertyEvaluationParameters& parameters)
evaluatedProperties = std::move(properties);

#if MLN_DRAWABLE_RENDERER
if (layerGroup) {
auto newTweaker = std::make_shared<CircleLayerTweaker>(getID(), evaluatedProperties);
replaceTweaker(layerTweaker, std::move(newTweaker), {layerGroup});
if (layerTweaker) {
layerTweaker->updateProperties(evaluatedProperties);
}
#endif // MLN_DRAWABLE_RENDERER
}
Expand Down
Loading

0 comments on commit e7d6f6a

Please sign in to comment.