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

@@ -22,6 +22,7 @@ enum class RenderPass : uint8_t;
namespace gfx {
class ColorMode;
class CullFaceMode;
enum class DepthMaskType : bool;
class DrawableTweaker;
@@ -134,6 +135,9 @@ public:
const gfx::CullFaceMode& getCullFaceMode() const;
void setCullFaceMode(const gfx::CullFaceMode&);
const gfx::ColorMode& getColorMode() const;
void setColorMode(const gfx::ColorMode&);
/// Get the number of vertexes
std::size_t getVertexCount() const { return getVertexAttributes().getMaxCount(); }

View File

@@ -76,6 +76,9 @@ public:
DepthMaskType getDepthType() const { return depthType; }
void setDepthType(DepthMaskType value) { depthType = value; }
const gfx::ColorMode& getColorMode() const;
void setColorMode(const gfx::ColorMode& value);
const gfx::CullFaceMode& getCullFaceMode() const;
void setCullFaceMode(const gfx::CullFaceMode& value);

View File

@@ -0,0 +1,29 @@
#pragma once
#include <mbgl/renderer/render_target.hpp>
#include <mbgl/gfx/types.hpp>
#include <mbgl/gl/object.hpp>
#include <mbgl/gl/types.hpp>
#include <mbgl/util/size.hpp>
namespace mbgl {
namespace gl {
class Context;
using UniqueFramebufferPtr = std::shared_ptr<UniqueFramebuffer>;
class RenderTargetGL final : public RenderTarget {
public:
RenderTargetGL(Context& context, const Size size, const gfx::TextureChannelDataType type);
~RenderTargetGL() override;
void upload(gfx::UploadPass& uploadPass) override;
void render(RenderOrchestrator&, const RenderTree&, PaintParameters&) override;
private:
Context& glContext;
UniqueFramebufferPtr framebuffer;
};
} // namespace gl
} // namespace mbgl

View File

@@ -11,11 +11,13 @@ class ChangeRequest;
class LayerGroupBase;
class TileLayerGroup;
class RenderOrchestrator;
class RenderTarget;
using ChangeRequestPtr = std::shared_ptr<ChangeRequest>;
using UniqueChangeRequest = std::unique_ptr<ChangeRequest>;
using UniqueChangeRequestVec = std::vector<UniqueChangeRequest>;
using LayerGroupBasePtr = std::shared_ptr<LayerGroupBase>;
using RenderTargetPtr = std::shared_ptr<RenderTarget>;
namespace gfx {
class Drawable;
@@ -89,4 +91,30 @@ protected:
int32_t newLayerIndex;
};
/**
Add a new render target to the scene
*/
class AddRenderTargetRequest : public ChangeRequest {
public:
AddRenderTargetRequest(RenderTargetPtr renderTarget_);
void execute(RenderOrchestrator &) override;
protected:
RenderTargetPtr renderTarget;
};
/**
Remove a render target from the scene
*/
class RemoveRenderTargetRequest : public ChangeRequest {
public:
RemoveRenderTargetRequest(const RenderTargetPtr &renderTarget_);
void execute(RenderOrchestrator &) override;
protected:
const RenderTargetPtr &renderTarget;
};
} // namespace mbgl

View File

@@ -0,0 +1,45 @@
#pragma once
#include <string>
#include <memory>
#include <map>
namespace mbgl {
namespace gfx {
class Texture2D;
class UploadPass;
using Texture2DPtr = std::shared_ptr<Texture2D>;
} // namespace gfx
class LayerGroupBase;
class PaintParameters;
class RenderOrchestrator;
class RenderTree;
using LayerGroupBasePtr = std::shared_ptr<LayerGroupBase>;
class RenderTarget {
public:
virtual ~RenderTarget() = default;
void setTexture(const gfx::Texture2DPtr& texture_) { texture = std::move(texture_); };
const gfx::Texture2DPtr& getTexture() const { return texture; };
bool addLayerGroup(LayerGroupBasePtr, bool replace);
bool removeLayerGroup(const int32_t layerIndex);
size_t numLayerGroups() const noexcept;
const LayerGroupBasePtr& getLayerGroup(const int32_t layerIndex) const;
void observeLayerGroups(std::function<void(LayerGroupBase&)>);
void observeLayerGroups(std::function<void(const LayerGroupBase&)>) const;
virtual void upload(gfx::UploadPass& uploadPass) = 0;
virtual void render(RenderOrchestrator&, const RenderTree&, PaintParameters&) = 0;
protected:
gfx::Texture2DPtr texture;
using LayerGroupMap = std::map<int32_t, LayerGroupBasePtr>;
LayerGroupMap layerGroupsByLayerIndex;
};
} // namespace mbgl

View File

@@ -0,0 +1,127 @@
// Generated code, do not modify this file!
#pragma once
#include <mbgl/shaders/shader_source.hpp>
namespace mbgl {
namespace shaders {
template <>
struct ShaderSource<BuiltIn::HeatmapShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "HeatmapShader";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
out vec2 v_extrude;
layout (std140) uniform HeatmapDrawableUBO {
highp mat4 u_matrix;
highp float u_extrude_scale;
lowp float pad1_;
lowp vec2 pad2_;
};
layout (std140) uniform HeatmapEvaluatedPropsUBO {
highp float u_weight;
highp float u_radius;
highp float u_intensity;
lowp float pad0_;
};
layout (std140) uniform HeatmapInterpolateUBO {
lowp float u_weight_t;
lowp float u_radius_t;
lowp vec2 pad3_;
};
#ifndef HAS_UNIFORM_u_weight
layout (location = 1) in highp vec2 a_weight;
out highp float weight;
#endif
#ifndef HAS_UNIFORM_u_radius
layout (location = 2) in mediump vec2 a_radius;
#endif
// Effective "0" in the kernel density texture to adjust the kernel size to;
// this empirically chosen number minimizes artifacts on overlapping kernels
// for typical heatmap cases (assuming clustered source)
const highp float ZERO = 1.0 / 255.0 / 16.0;
// Gaussian kernel coefficient: 1 / sqrt(2 * PI)
#define GAUSS_COEF 0.3989422804014327
void main(void) {
#ifndef HAS_UNIFORM_u_weight
weight = unpack_mix_vec2(a_weight, u_weight_t);
#else
highp float weight = u_weight;
#endif
#ifndef HAS_UNIFORM_u_radius
mediump float radius = unpack_mix_vec2(a_radius, u_radius_t);
#else
mediump float radius = u_radius;
#endif
// unencode the extrusion vector that we snuck into the a_pos vector
vec2 unscaled_extrude = vec2(mod(a_pos, 2.0) * 2.0 - 1.0);
// This 'extrude' comes in ranging from [-1, -1], to [1, 1]. We'll use
// it to produce the vertices of a square mesh framing the point feature
// we're adding to the kernel density texture. We'll also pass it as
// a varying, so that the fragment shader can determine the distance of
// each fragment from the point feature.
// Before we do so, we need to scale it up sufficiently so that the
// kernel falls effectively to zero at the edge of the mesh.
// That is, we want to know S such that
// weight * u_intensity * GAUSS_COEF * exp(-0.5 * 3.0^2 * S^2) == ZERO
// Which solves to:
// S = sqrt(-2.0 * log(ZERO / (weight * u_intensity * GAUSS_COEF))) / 3.0
float S = sqrt(-2.0 * log(ZERO / weight / u_intensity / GAUSS_COEF)) / 3.0;
// Pass the varying in units of radius
v_extrude = S * unscaled_extrude;
// Scale by radius and the zoom-based scale factor to produce actual
// mesh position
vec2 extrude = v_extrude * radius * u_extrude_scale;
// multiply a_pos by 0.5, since we had it * 2 in order to sneak
// in extrusion data
vec4 pos = vec4(floor(a_pos * 0.5) + extrude, 0, 1);
gl_Position = u_matrix * pos;
}
)";
static constexpr const char* fragment = R"(in vec2 v_extrude;
layout (std140) uniform HeatmapEvaluatedPropsUBO {
highp float u_weight;
highp float u_radius;
highp float u_intensity;
lowp float pad0_;
};
#ifndef HAS_UNIFORM_u_weight
in highp float weight;
#endif
// Gaussian kernel coefficient: 1 / sqrt(2 * PI)
#define GAUSS_COEF 0.3989422804014327
void main() {
#ifdef HAS_UNIFORM_u_weight
highp float weight = u_weight;
#endif
// Kernel density estimation with a Gaussian kernel of size 5x5
float d = -0.5 * 3.0 * 3.0 * dot(v_extrude, v_extrude);
float val = weight * u_intensity * GAUSS_COEF * exp(d);
fragColor = vec4(val, 1.0, 1.0, 1.0);
#ifdef OVERDRAW_INSPECTOR
fragColor = vec4(1.0);
#endif
}
)";
};
} // namespace shaders
} // namespace mbgl

View File

@@ -0,0 +1,52 @@
// Generated code, do not modify this file!
#pragma once
#include <mbgl/shaders/shader_source.hpp>
namespace mbgl {
namespace shaders {
template <>
struct ShaderSource<BuiltIn::HeatmapTextureShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "HeatmapTextureShader";
static constexpr const char* vertex = R"(layout (location = 0) in vec2 a_pos;
out vec2 v_pos;
layout (std140) uniform HeatmapTextureDrawableUBO {
highp mat4 u_matrix;
highp vec2 u_world;
highp float u_opacity;
lowp float pad0_;
};
void main() {
gl_Position = u_matrix * vec4(a_pos * u_world, 0, 1);
v_pos.x = a_pos.x;
v_pos.y = 1.0 - a_pos.y;
}
)";
static constexpr const char* fragment = R"(in vec2 v_pos;
uniform sampler2D u_image;
uniform sampler2D u_color_ramp;
layout (std140) uniform HeatmapTextureDrawableUBO {
highp mat4 u_matrix;
highp vec2 u_world;
highp float u_opacity;
lowp float pad0_;
};
void main() {
float t = texture(u_image, v_pos).r;
vec4 color = texture(u_color_ramp, vec2(t, 0.5));
fragColor = color * u_opacity;
#ifdef OVERDRAW_INSPECTOR
fragColor = vec4(0.0);
#endif
}
)";
};
} // namespace shaders
} // namespace mbgl

View File

@@ -14,6 +14,8 @@
#include <mbgl/shaders/gl/drawable_line.hpp>
#include <mbgl/shaders/gl/drawable_fill_pattern.hpp>
#include <mbgl/shaders/gl/drawable_fill_outline_pattern.hpp>
#include <mbgl/shaders/gl/drawable_heatmap.hpp>
#include <mbgl/shaders/gl/drawable_heatmap_texture.hpp>
#include <mbgl/shaders/gl/drawable_raster.hpp>
#include <mbgl/shaders/gl/drawable_symbol_icon.hpp>
#include <mbgl/shaders/gl/drawable_symbol_sdf_icon.hpp>

View File

@@ -20,11 +20,14 @@ enum class BuiltIn {
LineShader,
FillPatternShader,
FillOutlinePatternShader,
HeatmapShader,
HeatmapTextureShader,
RasterShader,
SymbolIconShader,
SymbolSDFTextShader,
SymbolSDFIconShader,
SymbolTextAndIconShader,
Prelude,
BackgroundProgram,
BackgroundPatternProgram,