Skip to content

Commit

Permalink
heatmap
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSylvester committed Aug 9, 2024
1 parent 5dd5f14 commit 362d627
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
23 changes: 18 additions & 5 deletions src/mbgl/renderer/layers/heatmap_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/heatmap_program.hpp>
#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/render_static_data.hpp>
Expand All @@ -21,7 +22,7 @@ using namespace shaders;

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

if (layerGroup.empty()) {
Expand Down Expand Up @@ -52,17 +53,29 @@ void HeatmapLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamet

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

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

constexpr bool nearClipped = false;
constexpr bool inViewportPixelUnits = false;
const auto matrix = getTileMatrix(
tileID, parameters, {0.f, 0.f}, TranslateAnchorType::Viewport, nearClipped, inViewportPixelUnits, drawable);
const HeatmapDrawableUBO drawableUBO = {
/* .matrix = */ util::cast<float>(matrix),
/* .extrude_scale = */ tileID.pixelsToTileUnits(1.0f, static_cast<float>(zoom)),
/* .padding = */ {0}};
const HeatmapDrawableUBO drawableUBO = {/* .matrix = */ util::cast<float>(matrix),
/* .extrude_scale = */ tileID.pixelsToTileUnits(1.0f, zoom),
/* .padding = */ {0}};

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

const HeatmapInterpolateUBO interpolateUBO = {
/* .weight_t = */ std::get<0>(binders->get<HeatmapWeight>()->interpolationFactor(zoom)),
/* .radius_t = */ std::get<0>(binders->get<HeatmapRadius>()->interpolationFactor(zoom)),
/* .padding = */ {0}};
drawableUniforms.createOrUpdate(idHeatmapInterpolateUBO, &interpolateUBO, context);
});
}

Expand Down
44 changes: 8 additions & 36 deletions src/mbgl/renderer/layers/render_heatmap_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ void RenderHeatmapLayer::update(gfx::ShaderRegistry& shaders,
continue;
}

const auto& bucket = static_cast<HeatmapBucket&>(*renderData->bucket);
auto& bucket = static_cast<HeatmapBucket&>(*renderData->bucket);
const auto vertexCount = bucket.vertices.elements();
auto& paintPropertyBinders = bucket.paintPropertyBinders.at(getID());

Expand All @@ -372,37 +372,14 @@ void RenderHeatmapLayer::update(gfx::ShaderRegistry& shaders,
}
setRenderTileBucketID(tileID, bucket.getID());

const float zoom = static_cast<float>(state.getZoom());

gfx::UniformBufferPtr interpolateBuffer;
const auto getInterpolateBuffer = [&]() {
if (!interpolateBuffer) {
const HeatmapInterpolateUBO interpolateUBO = {
/* .weight_t = */ std::get<0>(paintPropertyBinders.get<HeatmapWeight>()->interpolationFactor(zoom)),
/* .radius_t = */ std::get<0>(paintPropertyBinders.get<HeatmapRadius>()->interpolationFactor(zoom)),
/* .padding = */ {0}};
interpolateBuffer = context.createUniformBuffer(&interpolateUBO, sizeof(interpolateUBO), false);
}
return interpolateBuffer;
};

const auto updatedCount = tileLayerGroup->visitDrawables(renderPass, tileID, [&](gfx::Drawable& drawable) {
auto updateExisting = [&](gfx::Drawable& drawable) {
if (drawable.getLayerTweaker() != layerTweaker) {
// This drawable was produced on a previous style/bucket, and should not be updated.
return;
}

// We assume vertex attributes don't change, and so don't update them to avoid re-uploading
// drawable.setVertexAttributes(heatmapVertexAttrs);

auto& drawableUniforms = drawable.mutableUniformBuffers();

if (auto buffer = getInterpolateBuffer()) {
drawableUniforms.set(idHeatmapInterpolateUBO, std::move(buffer));
return false;
}
});

if (updatedCount > 0) {
return true;
};
if (updateTile(renderPass, tileID, std::move(updateExisting))) {
continue;
}

Expand Down Expand Up @@ -460,13 +437,8 @@ void RenderHeatmapLayer::update(gfx::ShaderRegistry& shaders,
for (auto& drawable : heatmapBuilder->clearDrawables()) {
drawable->setTileID(tileID);
drawable->setLayerTweaker(layerTweaker);

auto& drawableUniforms = drawable->mutableUniformBuffers();

if (auto buffer = getInterpolateBuffer()) {
drawableUniforms.set(idHeatmapInterpolateUBO, std::move(buffer));
}

drawable->setBinders(renderData->bucket, &paintPropertyBinders);
drawable->setRenderTile(renderTilesOwner, &tile);
tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable));
++stats.drawablesAdded;
}
Expand Down

0 comments on commit 362d627

Please sign in to comment.