Heatmap layer (#1230)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Alex Cristici
2023-07-03 16:50:10 +03:00
committed by Bart Louwers
parent e631bc3c42
commit 813ddb024a
46 changed files with 1247 additions and 26 deletions

View File

@@ -5,15 +5,25 @@
#include <mbgl/renderer/render_static_data.hpp>
#include <mbgl/programs/programs.hpp>
#include <mbgl/tile/tile.hpp>
#include <mbgl/style/layers/heatmap_layer.hpp>
#include <mbgl/style/layers/heatmap_layer_impl.hpp>
#include <mbgl/geometry/feature_index.hpp>
#include <mbgl/gfx/context.hpp>
#include <mbgl/gfx/cull_face_mode.hpp>
#include <mbgl/gfx/render_pass.hpp>
#include <mbgl/gfx/context.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/util/intersection_tests.hpp>
#if MLN_DRAWABLE_RENDERER
#include <mbgl/renderer/layers/heatmap_layer_tweaker.hpp>
#include <mbgl/renderer/layers/heatmap_texture_layer_tweaker.hpp>
#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/renderer/render_target.hpp>
#include <mbgl/shaders/shader_program_base.hpp>
#include <mbgl/gfx/drawable_builder.hpp>
#include <mbgl/gfx/shader_group.hpp>
#include <mbgl/gfx/shader_registry.hpp>
#endif
namespace mbgl {
using namespace style;
@@ -29,8 +39,9 @@ inline const HeatmapLayer::Impl& impl_cast(const Immutable<Layer::Impl>& impl) {
RenderHeatmapLayer::RenderHeatmapLayer(Immutable<HeatmapLayer::Impl> _impl)
: RenderLayer(makeMutable<HeatmapLayerProperties>(std::move(_impl))),
unevaluated(impl_cast(baseImpl).paint.untransitioned()),
colorRamp({256, 1}) {}
unevaluated(impl_cast(baseImpl).paint.untransitioned()) {
colorRamp = std::make_shared<PremultipliedImage>(Size(256, 1));
}
RenderHeatmapLayer::~RenderHeatmapLayer() = default;
@@ -47,6 +58,15 @@ void RenderHeatmapLayer::evaluate(const PropertyEvaluationParameters& parameters
: RenderPass::None;
properties->renderPasses = mbgl::underlying_type(passes);
evaluatedProperties = std::move(properties);
#if MLN_DRAWABLE_RENDERER
if (renderTarget) {
renderTarget->getLayerGroup(0)->setLayerTweaker(std::make_shared<HeatmapLayerTweaker>(evaluatedProperties));
}
if (layerGroup) {
layerGroup->setLayerTweaker(std::make_shared<HeatmapTextureLayerTweaker>(evaluatedProperties));
}
#endif
}
bool RenderHeatmapLayer::hasTransition() const {
@@ -59,7 +79,7 @@ bool RenderHeatmapLayer::hasCrossfade() const {
void RenderHeatmapLayer::upload(gfx::UploadPass& uploadPass) {
if (!colorRampTexture) {
colorRampTexture = uploadPass.createTexture(colorRamp, gfx::TextureChannelDataType::UnsignedByte);
colorRampTexture = uploadPass.createTexture(*colorRamp, gfx::TextureChannelDataType::UnsignedByte);
}
}
@@ -180,14 +200,14 @@ void RenderHeatmapLayer::updateColorRamp() {
colorValue = HeatmapLayer::getDefaultHeatmapColor();
}
const auto length = colorRamp.bytes();
const auto length = (*colorRamp).bytes();
for (uint32_t i = 0; i < length; i += 4) {
const auto color = colorValue.evaluate(static_cast<double>(i) / length);
colorRamp.data[i + 0] = static_cast<uint8_t>(std::floor(color.r * 255.f));
colorRamp.data[i + 1] = static_cast<uint8_t>(std::floor(color.g * 255.f));
colorRamp.data[i + 2] = static_cast<uint8_t>(std::floor(color.b * 255.f));
colorRamp.data[i + 3] = static_cast<uint8_t>(std::floor(color.a * 255.f));
(*colorRamp).data[i + 0] = static_cast<uint8_t>(std::floor(color.r * 255.f));
(*colorRamp).data[i + 1] = static_cast<uint8_t>(std::floor(color.g * 255.f));
(*colorRamp).data[i + 2] = static_cast<uint8_t>(std::floor(color.b * 255.f));
(*colorRamp).data[i + 3] = static_cast<uint8_t>(std::floor(color.a * 255.f));
}
if (colorRampTexture) {
@@ -209,4 +229,266 @@ bool RenderHeatmapLayer::queryIntersectsFeature(const GeometryCoordinates& query
return false;
}
#if MLN_DRAWABLE_RENDERER
void activateRenderTarget(const RenderTargetPtr& renderTarget_, bool activate, UniqueChangeRequestVec& changes) {
if (renderTarget_) {
if (activate) {
// The RenderTree has determined this render target should be included in the renderable set for a frame
changes.emplace_back(std::make_unique<AddRenderTargetRequest>(renderTarget_));
} else {
// The RenderTree is informing us we should not render anything
changes.emplace_back(std::make_unique<RemoveRenderTargetRequest>(renderTarget_));
}
}
}
void RenderHeatmapLayer::markLayerRenderable(bool willRender, UniqueChangeRequestVec& changes) {
RenderLayer::markLayerRenderable(willRender, changes);
activateRenderTarget(renderTarget, willRender, changes);
}
void RenderHeatmapLayer::removeTile(RenderPass renderPass, const OverscaledTileID& tileID) {
auto* tileLayerGroup = static_cast<TileLayerGroup*>(renderTarget->getLayerGroup(0).get());
stats.drawablesRemoved += tileLayerGroup->removeDrawables(renderPass, tileID).size();
}
void RenderHeatmapLayer::removeAllDrawables() {
RenderLayer::removeAllDrawables();
if (renderTarget) {
stats.drawablesRemoved += renderTarget->getLayerGroup(0)->getDrawableCount();
renderTarget->getLayerGroup(0)->clearDrawables();
}
}
struct alignas(16) HeatmapInterpolateUBO {
float weight_t;
float radius_t;
std::array<float, 2> padding;
};
static_assert(sizeof(HeatmapInterpolateUBO) % 16 == 0);
static const std::string HeatmapShaderGroupName = "HeatmapShader";
static const std::string HeatmapTextureShaderGroupName = "HeatmapTextureShader";
static constexpr std::string_view HeatmapInterpolateUBOName = "HeatmapInterpolateUBO";
void RenderHeatmapLayer::update(gfx::ShaderRegistry& shaders,
gfx::Context& context,
const TransformState& state,
[[maybe_unused]] const RenderTree& renderTree,
[[maybe_unused]] UniqueChangeRequestVec& changes) {
std::unique_lock<std::mutex> guard(mutex);
if (!renderTiles || renderTiles->empty()) {
removeAllDrawables();
return;
}
const auto& viewportSize = state.getSize();
const auto size = Size{viewportSize.width / 2, viewportSize.height / 2};
// Set up a render target
if (!renderTarget) {
renderTarget = context.createRenderTarget(size, gfx::TextureChannelDataType::HalfFloat);
if (!renderTarget) {
return;
}
activateRenderTarget(renderTarget, isRenderable, changes);
// Set up tile layer group
auto tileLayerGroup = context.createTileLayerGroup(0, /*initialCapacity=*/64, getID());
if (!tileLayerGroup) {
return;
}
tileLayerGroup->setLayerTweaker(std::make_shared<HeatmapLayerTweaker>(evaluatedProperties));
renderTarget->addLayerGroup(tileLayerGroup, /*canReplace=*/true);
}
if (renderTarget->getTexture()->getSize() != size) {
renderTarget->getTexture()->setSize(size);
}
auto* tileLayerGroup = static_cast<TileLayerGroup*>(renderTarget->getLayerGroup(0).get());
if (!heatmapShaderGroup) {
heatmapShaderGroup = shaders.getShaderGroup(HeatmapShaderGroupName);
}
if (!heatmapShaderGroup) {
removeAllDrawables();
return;
}
std::unordered_set<OverscaledTileID> newTileIDs(renderTiles->size());
std::transform(renderTiles->begin(),
renderTiles->end(),
std::inserter(newTileIDs, newTileIDs.begin()),
[](const auto& renderTile) -> OverscaledTileID { return renderTile.get().getOverscaledTileID(); });
std::unique_ptr<gfx::DrawableBuilder> heatmapBuilder;
std::vector<gfx::DrawablePtr> newTiles;
gfx::VertexAttributeArray heatmapVertexAttrs;
auto renderPass = RenderPass::Translucent;
if (!(mbgl::underlying_type(renderPass) & evaluatedProperties->renderPasses)) {
return;
}
tileLayerGroup->observeDrawables([&](gfx::UniqueDrawable& drawable) {
const auto tileID = drawable->getTileID();
if (tileID && newTileIDs.find(*tileID) == newTileIDs.end()) {
// remove it
drawable.reset();
++stats.drawablesRemoved;
}
});
const auto& evaluated = static_cast<const HeatmapLayerProperties&>(*evaluatedProperties).evaluated;
for (const RenderTile& tile : *renderTiles) {
const auto& tileID = tile.getOverscaledTileID();
const LayerRenderData* renderData = getRenderDataForPass(tile, renderPass);
if (!renderData) {
removeTile(renderPass, tileID);
continue;
}
auto& bucket = static_cast<HeatmapBucket&>(*renderData->bucket);
const auto& paintPropertyBinders = bucket.paintPropertyBinders.at(getID());
float zoom = static_cast<float>(state.getZoom());
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}};
tileLayerGroup->observeDrawables(renderPass, tileID, [&](gfx::Drawable& drawable) {
drawable.mutableUniformBuffers().createOrUpdate(HeatmapInterpolateUBOName, &interpolateUBO, context);
});
if (tileLayerGroup->getDrawableCount(renderPass, tileID) > 0) {
continue;
}
heatmapVertexAttrs.clear();
auto propertiesAsUniforms = heatmapVertexAttrs.readDataDrivenPaintProperties<HeatmapWeight, HeatmapRadius>(
paintPropertyBinders, evaluated);
auto heatmapShader = heatmapShaderGroup->getOrCreateShader(context, propertiesAsUniforms);
if (!heatmapShader) {
continue;
}
std::vector<std::array<int16_t, 2>> rawVerts;
const auto buildVertices = [&]() {
const std::vector<gfx::VertexVector<gfx::detail::VertexType<gfx::AttributeType<int16_t, 2>>>::Vertex>&
verts = bucket.vertices.vector();
if (rawVerts.size() < verts.size()) {
rawVerts.resize(verts.size());
std::transform(verts.begin(), verts.end(), rawVerts.begin(), [](const auto& x) { return x.a1; });
}
};
heatmapBuilder = context.createDrawableBuilder("heatmap");
heatmapBuilder->setShader(std::static_pointer_cast<gfx::ShaderProgramBase>(heatmapShader));
heatmapBuilder->setDepthType((renderPass == RenderPass::Opaque) ? gfx::DepthMaskType::ReadWrite
: gfx::DepthMaskType::ReadOnly);
heatmapBuilder->setColorMode(gfx::ColorMode::additive());
heatmapBuilder->setCullFaceMode(gfx::CullFaceMode::disabled());
heatmapBuilder->setRenderPass(renderPass);
heatmapBuilder->setVertexAttributes(heatmapVertexAttrs);
buildVertices();
heatmapBuilder->addVertices(rawVerts, 0, rawVerts.size());
heatmapBuilder->setSegments(
gfx::Triangles(), bucket.triangles.vector(), bucket.segments.data(), bucket.segments.size());
heatmapBuilder->flush();
for (auto& drawable : heatmapBuilder->clearDrawables()) {
drawable->setTileID(tileID);
drawable->mutableUniformBuffers().createOrUpdate(HeatmapInterpolateUBOName, &interpolateUBO, context);
tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable));
++stats.drawablesAdded;
}
}
// Set up texture layer group
if (!layerGroup) {
auto layerGroup_ = context.createLayerGroup(layerIndex, /*initialCapacity=*/1, getID());
if (!layerGroup_) {
return;
}
layerGroup_->setLayerTweaker(std::make_shared<HeatmapTextureLayerTweaker>(evaluatedProperties));
setLayerGroup(std::move(layerGroup_), changes);
}
auto* textureLayerGroup = static_cast<LayerGroup*>(layerGroup.get());
if (!heatmapTextureShader) {
heatmapTextureShader = context.getGenericShader(shaders, HeatmapTextureShaderGroupName);
}
if (!heatmapTextureShader) {
removeAllDrawables();
return;
}
textureLayerGroup->clearDrawables();
std::unique_ptr<gfx::DrawableBuilder> heatmapTextureBuilder;
std::vector<std::array<int16_t, 2>> textureRawVerts;
const auto buildTextureVertices = [&]() {
const std::vector<gfx::VertexVector<gfx::detail::VertexType<gfx::AttributeType<int16_t, 2>>>::Vertex> verts =
RenderStaticData::heatmapTextureVertices().vector();
if (textureRawVerts.size() < verts.size()) {
textureRawVerts.resize(verts.size());
std::transform(verts.begin(), verts.end(), textureRawVerts.begin(), [](const auto& x) { return x.a1; });
}
};
heatmapTextureBuilder = context.createDrawableBuilder("heatmapTexture");
heatmapTextureBuilder->setShader(heatmapTextureShader);
heatmapTextureBuilder->setDepthType((renderPass == RenderPass::Opaque) ? gfx::DepthMaskType::ReadWrite
: gfx::DepthMaskType::ReadOnly);
heatmapTextureBuilder->setColorMode(gfx::ColorMode::alphaBlended());
heatmapTextureBuilder->setCullFaceMode(gfx::CullFaceMode::disabled());
heatmapTextureBuilder->setRenderPass(renderPass);
buildTextureVertices();
heatmapTextureBuilder->addVertices(textureRawVerts, 0, textureRawVerts.size());
if (segments.empty()) {
segments = RenderStaticData::heatmapTextureSegments();
}
heatmapTextureBuilder->setSegments(
gfx::Triangles(), RenderStaticData::quadTriangleIndices().vector(), segments.data(), segments.size());
auto imageLocation = heatmapTextureShader->getSamplerLocation("u_image");
if (imageLocation.has_value()) {
heatmapTextureBuilder->setTexture(renderTarget->getTexture(), imageLocation.value());
}
auto colorRampLocation = heatmapTextureShader->getSamplerLocation("u_color_ramp");
if (colorRampLocation.has_value()) {
std::shared_ptr<gfx::Texture2D> texture = context.createTexture2D();
texture->setImage(colorRamp);
texture->setSamplerConfiguration(
{gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp});
heatmapTextureBuilder->setTexture(texture, colorRampLocation.value());
}
heatmapTextureBuilder->flush();
for (auto& drawable : heatmapTextureBuilder->clearDrawables()) {
textureLayerGroup->addDrawable(std::move(drawable));
++stats.drawablesAdded;
}
}
#endif // MLN_DRAWABLE_RENDERER
} // namespace mbgl