Metal render target (#1484)
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Tim Sylvester <tim@wetdogweather.com> Co-authored-by: mwilsnd <mwilsnd@gmail.com> Co-authored-by: Stefan Karschti <stefan.karschti@gmail.com> Co-authored-by: Bart Louwers <bart@emeel.net> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: mwilsnd <53413200+mwilsnd@users.noreply.github.com> Co-authored-by: Alex Cristici <alex@skoon.energy> Co-authored-by: Bart Louwers <bart.louwers@gmail.com> Co-authored-by: Wei Chen <alanchenboy@gmail.com> Co-authored-by: Siarhei Fedartsou <siarhei.fedartsou@gmail.com> Co-authored-by: tdcosta100 <tdcosta100@gmail.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
#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
|
||||
@@ -54,24 +54,6 @@ public:
|
||||
MTLTexturePtr createMetalTexture(MTLTextureDescriptorPtr textureDescriptor) const;
|
||||
MTLSamplerStatePtr createMetalSamplerState(MTLSamplerDescriptorPtr samplerDescriptor) const;
|
||||
|
||||
/*
|
||||
Framebuffer createFramebuffer(const gfx::Renderbuffer<gfx::RenderbufferPixelType::RGBA>&,
|
||||
const gfx::Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>&);
|
||||
Framebuffer createFramebuffer(const gfx::Renderbuffer<gfx::RenderbufferPixelType::RGBA>&);
|
||||
Framebuffer createFramebuffer(const gfx::Texture&,
|
||||
const gfx::Renderbuffer<gfx::RenderbufferPixelType::DepthStencil>&);
|
||||
Framebuffer createFramebuffer(const gfx::Texture&);
|
||||
Framebuffer createFramebuffer(const gfx::Texture&, const gfx::Renderbuffer<gfx::RenderbufferPixelType::Depth>&);
|
||||
|
||||
template <typename Image,
|
||||
gfx::TexturePixelType format = Image::channels == 4 ? gfx::TexturePixelType::RGBA
|
||||
: gfx::TexturePixelType::Alpha>
|
||||
Image readFramebuffer(const Size size, bool flip = true) {
|
||||
static_assert(Image::channels == (format == gfx::TexturePixelType::RGBA ? 4 : 1), "image format mismatch");
|
||||
return {size, readFramebuffer(size, format, flip)};
|
||||
}
|
||||
*/
|
||||
|
||||
// Actually remove the objects we marked as abandoned with the above methods.
|
||||
void performCleanup() override {}
|
||||
|
||||
|
||||
23
include/mbgl/mtl/offscreen_texture.hpp
Normal file
23
include/mbgl/mtl/offscreen_texture.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/gfx/offscreen_texture.hpp>
|
||||
#include <mbgl/gfx/types.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace mtl {
|
||||
|
||||
class Context;
|
||||
class Texture2D;
|
||||
|
||||
class OffscreenTexture final : public gfx::OffscreenTexture {
|
||||
public:
|
||||
OffscreenTexture(Context&, Size size, gfx::TextureChannelDataType type = gfx::TextureChannelDataType::UnsignedByte);
|
||||
|
||||
bool isRenderable() override;
|
||||
|
||||
PremultipliedImage readStillImage() override;
|
||||
const gfx::Texture2DPtr& getTexture() override;
|
||||
};
|
||||
|
||||
} // namespace mtl
|
||||
} // namespace mbgl
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <Foundation/NSSharedPtr.hpp>
|
||||
#include <Metal/MTLPixelFormat.hpp>
|
||||
#include <Metal/MTLTexture.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
@@ -44,6 +45,11 @@ public:
|
||||
|
||||
bool needsUpload() const noexcept override { return !!image; };
|
||||
|
||||
public:
|
||||
gfx::Texture2D& setUsage(MTL::TextureUsage usage_) noexcept;
|
||||
|
||||
MTL::Texture* getMetalTexture() const noexcept;
|
||||
|
||||
void updateSamplerConfiguration() noexcept;
|
||||
|
||||
/// @brief Bind this texture to the specified location
|
||||
@@ -67,6 +73,7 @@ private:
|
||||
Size size{0, 0};
|
||||
gfx::TexturePixelType pixelFormat{gfx::TexturePixelType::RGBA};
|
||||
gfx::TextureChannelDataType channelType{gfx::TextureChannelDataType::UnsignedByte};
|
||||
MTL::TextureUsage usage{MTL::TextureUsageShaderRead};
|
||||
SamplerState samplerState{};
|
||||
|
||||
std::shared_ptr<PremultipliedImage> image{nullptr};
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/gfx/types.hpp>
|
||||
#include <mbgl/util/size.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
@@ -8,7 +11,9 @@
|
||||
namespace mbgl {
|
||||
|
||||
namespace gfx {
|
||||
class Context;
|
||||
class Texture2D;
|
||||
class OffscreenTexture;
|
||||
class UploadPass;
|
||||
using Texture2DPtr = std::shared_ptr<Texture2D>;
|
||||
} // namespace gfx
|
||||
@@ -23,13 +28,11 @@ using LayerGroupBasePtr = std::shared_ptr<LayerGroupBase>;
|
||||
/// Render target class
|
||||
class RenderTarget {
|
||||
public:
|
||||
virtual ~RenderTarget() = default;
|
||||
|
||||
/// Set the render target texture
|
||||
void setTexture(const gfx::Texture2DPtr& texture_) { texture = std::move(texture_); };
|
||||
RenderTarget(gfx::Context& context, const Size size, const gfx::TextureChannelDataType type);
|
||||
~RenderTarget();
|
||||
|
||||
/// Get the render target texture
|
||||
const gfx::Texture2DPtr& getTexture() const { return texture; };
|
||||
const gfx::Texture2DPtr& getTexture();
|
||||
|
||||
/// @brief Add a layer group to the render target
|
||||
/// @param replace Flag to replace if exists
|
||||
@@ -54,13 +57,14 @@ public:
|
||||
void visitLayerGroups(std::function<void(const LayerGroupBase&)>) const;
|
||||
|
||||
/// Upload the layer groups
|
||||
virtual void upload(gfx::UploadPass& uploadPass) = 0;
|
||||
void upload(gfx::UploadPass& uploadPass);
|
||||
|
||||
/// Render the layer groups
|
||||
virtual void render(RenderOrchestrator&, const RenderTree&, PaintParameters&) = 0;
|
||||
void render(RenderOrchestrator&, const RenderTree&, PaintParameters&);
|
||||
|
||||
protected:
|
||||
gfx::Texture2DPtr texture;
|
||||
gfx::Context& context;
|
||||
std::unique_ptr<gfx::OffscreenTexture> offscreenTexture;
|
||||
using LayerGroupMap = std::map<int32_t, LayerGroupBasePtr>;
|
||||
LayerGroupMap layerGroupsByLayerIndex;
|
||||
};
|
||||
|
||||
41
include/mbgl/shaders/heatmap_layer_ubo.hpp
Normal file
41
include/mbgl/shaders/heatmap_layer_ubo.hpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/shaders/layer_ubo.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace shaders {
|
||||
|
||||
struct alignas(16) HeatmapDrawableUBO {
|
||||
std::array<float, 4 * 4> matrix;
|
||||
float extrude_scale;
|
||||
std::array<float, 3> padding;
|
||||
};
|
||||
static_assert(sizeof(HeatmapDrawableUBO) % 16 == 0);
|
||||
|
||||
struct alignas(16) HeatmapEvaluatedPropsUBO {
|
||||
float weight;
|
||||
float radius;
|
||||
float intensity;
|
||||
float padding;
|
||||
};
|
||||
static_assert(sizeof(HeatmapEvaluatedPropsUBO) % 16 == 0);
|
||||
|
||||
struct alignas(16) HeatmapInterpolateUBO {
|
||||
float weight_t;
|
||||
float radius_t;
|
||||
std::array<float, 2> padding;
|
||||
};
|
||||
static_assert(sizeof(HeatmapInterpolateUBO) % 16 == 0);
|
||||
|
||||
struct alignas(16) HeatmapPermutationUBO {
|
||||
/* 0 */ Attribute weight;
|
||||
/* 8 */ Attribute radius;
|
||||
/* 16 */ bool overdrawInspector;
|
||||
/* 17 */ uint8_t pad1, pad2, pad3;
|
||||
/* 20 */ float pad4, pad5, pad6;
|
||||
/* 32 */
|
||||
};
|
||||
static_assert(sizeof(HeatmapPermutationUBO) == 2 * 16);
|
||||
|
||||
} // namespace shaders
|
||||
} // namespace mbgl
|
||||
19
include/mbgl/shaders/heatmap_texture_layer_ubo.hpp
Normal file
19
include/mbgl/shaders/heatmap_texture_layer_ubo.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/shaders/layer_ubo.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace shaders {
|
||||
|
||||
struct alignas(16) HeatmapTextureDrawableUBO {
|
||||
std::array<float, 4 * 4> matrix;
|
||||
std::array<float, 2> world;
|
||||
float opacity;
|
||||
// overdrawInspector is used only in Metal, while in GL this is a 4 bytes empty padding.
|
||||
bool overdrawInspector;
|
||||
uint8_t pad1, pad2, pad3;
|
||||
};
|
||||
static_assert(sizeof(HeatmapTextureDrawableUBO) % 16 == 0);
|
||||
|
||||
} // namespace shaders
|
||||
} // namespace mbgl
|
||||
145
include/mbgl/shaders/mtl/heatmap.hpp
Normal file
145
include/mbgl/shaders/mtl/heatmap.hpp
Normal file
@@ -0,0 +1,145 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/shaders/heatmap_layer_ubo.hpp>
|
||||
#include <mbgl/shaders/shader_source.hpp>
|
||||
#include <mbgl/shaders/mtl/common.hpp>
|
||||
#include <mbgl/shaders/mtl/shader_program.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace shaders {
|
||||
|
||||
template <>
|
||||
struct ShaderSource<BuiltIn::HeatmapShader, gfx::Backend::Type::Metal> {
|
||||
static constexpr auto name = "HeatmapShader";
|
||||
static constexpr auto vertexMainFunction = "vertexMain";
|
||||
static constexpr auto fragmentMainFunction = "fragmentMain";
|
||||
|
||||
static constexpr AttributeInfo attributes[] = {
|
||||
{0, gfx::AttributeDataType::Short2, 1, "a_pos"},
|
||||
{1, gfx::AttributeDataType::Float2, 1, "a_weight"},
|
||||
{2, gfx::AttributeDataType::Float2, 1, "a_radius"},
|
||||
};
|
||||
static constexpr UniformBlockInfo uniforms[] = {
|
||||
MLN_MTL_UNIFORM_BLOCK(3, true, false, HeatmapDrawableUBO),
|
||||
MLN_MTL_UNIFORM_BLOCK(4, true, true, HeatmapEvaluatedPropsUBO),
|
||||
MLN_MTL_UNIFORM_BLOCK(5, true, false, HeatmapInterpolateUBO),
|
||||
MLN_MTL_UNIFORM_BLOCK(6, true, true, HeatmapPermutationUBO),
|
||||
MLN_MTL_UNIFORM_BLOCK(7, true, false, ExpressionInputsUBO),
|
||||
};
|
||||
static constexpr TextureInfo textures[] = {};
|
||||
|
||||
static constexpr auto source = R"(
|
||||
|
||||
struct VertexStage {
|
||||
short2 pos [[attribute(0)]];
|
||||
float2 weight [[attribute(1)]];
|
||||
float2 radius [[attribute(2)]];
|
||||
};
|
||||
|
||||
struct FragmentStage {
|
||||
float4 position [[position, invariant]];
|
||||
float weight;
|
||||
float2 extrude;
|
||||
};
|
||||
|
||||
struct alignas(16) HeatmapDrawableUBO {
|
||||
float4x4 matrix;
|
||||
float extrude_scale;
|
||||
float pad1;
|
||||
float2 pad2;
|
||||
};
|
||||
|
||||
struct alignas(16) HeatmapEvaluatedPropsUBO {
|
||||
float weight;
|
||||
float radius;
|
||||
float intensity;
|
||||
float pad1;
|
||||
};
|
||||
|
||||
struct alignas(16) HeatmapInterpolateUBO {
|
||||
float weight_t;
|
||||
float radius_t;
|
||||
float2 pad1;
|
||||
};
|
||||
|
||||
struct alignas(16) HeatmapPermutationUBO {
|
||||
Attribute weight;
|
||||
Attribute radius;
|
||||
bool overdrawInspector;
|
||||
uint8_t pad1, pad2, pad3;
|
||||
float pad4, pad5, pad6;
|
||||
};
|
||||
|
||||
// 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)
|
||||
constant const float ZERO = 1.0 / 255.0 / 16.0;
|
||||
|
||||
// Gaussian kernel coefficient: 1 / sqrt(2 * PI)
|
||||
#define GAUSS_COEF 0.3989422804014327
|
||||
|
||||
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
|
||||
device const HeatmapDrawableUBO& drawable [[buffer(3)]],
|
||||
device const HeatmapEvaluatedPropsUBO& props [[buffer(4)]],
|
||||
device const HeatmapInterpolateUBO& interp [[buffer(5)]],
|
||||
device const HeatmapPermutationUBO& permutation [[buffer(6)]],
|
||||
device const ExpressionInputsUBO& expr [[buffer(7)]]) {
|
||||
|
||||
const auto weight = valueFor(permutation.weight, props.weight, vertx.weight, interp.weight_t, expr);
|
||||
const auto radius = valueFor(permutation.radius, props.radius, vertx.radius, interp.radius_t, expr);
|
||||
|
||||
// unencode the extrusion vector that we snuck into the a_pos vector
|
||||
float2 unscaled_extrude = float2(glMod(float2(vertx.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 / props.intensity / GAUSS_COEF)) / 3.0;
|
||||
|
||||
// Pass the varying in units of radius
|
||||
float2 extrude = S * unscaled_extrude;
|
||||
|
||||
// Scale by radius and the zoom-based scale factor to produce actual
|
||||
// mesh position
|
||||
float2 scaled_extrude = extrude * radius * drawable.extrude_scale;
|
||||
|
||||
// multiply a_pos by 0.5, since we had it * 2 in order to sneak
|
||||
// in extrusion data
|
||||
float4 pos = float4(floor(float2(vertx.pos) * 0.5) + scaled_extrude, 0, 1);
|
||||
|
||||
float4 position = drawable.matrix * pos;
|
||||
|
||||
return {
|
||||
.position = position,
|
||||
.weight = weight,
|
||||
.extrude = extrude,
|
||||
};
|
||||
}
|
||||
|
||||
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
|
||||
device const HeatmapEvaluatedPropsUBO& props [[buffer(4)]],
|
||||
device const HeatmapPermutationUBO& permutation [[buffer(6)]]) {
|
||||
|
||||
if (permutation.overdrawInspector) {
|
||||
return half4(1.0);
|
||||
}
|
||||
|
||||
// Kernel density estimation with a Gaussian kernel of size 5x5
|
||||
float d = -0.5 * 3.0 * 3.0 * dot(in.extrude, in.extrude);
|
||||
float val = in.weight * props.intensity * GAUSS_COEF * exp(d);
|
||||
|
||||
return half4(val, 1.0, 1.0, 1.0);
|
||||
}
|
||||
)";
|
||||
};
|
||||
|
||||
} // namespace shaders
|
||||
} // namespace mbgl
|
||||
77
include/mbgl/shaders/mtl/heatmap_texture.hpp
Normal file
77
include/mbgl/shaders/mtl/heatmap_texture.hpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/shaders/heatmap_texture_layer_ubo.hpp>
|
||||
#include <mbgl/shaders/shader_source.hpp>
|
||||
#include <mbgl/shaders/mtl/common.hpp>
|
||||
#include <mbgl/shaders/mtl/shader_program.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace shaders {
|
||||
|
||||
template <>
|
||||
struct ShaderSource<BuiltIn::HeatmapTextureShader, gfx::Backend::Type::Metal> {
|
||||
static constexpr auto name = "HeatmapTextureShader";
|
||||
static constexpr auto vertexMainFunction = "vertexMain";
|
||||
static constexpr auto fragmentMainFunction = "fragmentMain";
|
||||
|
||||
static constexpr AttributeInfo attributes[] = {
|
||||
{0, gfx::AttributeDataType::Short2, 1, "a_pos"},
|
||||
};
|
||||
static constexpr UniformBlockInfo uniforms[] = {
|
||||
MLN_MTL_UNIFORM_BLOCK(1, true, true, HeatmapTextureDrawableUBO),
|
||||
};
|
||||
static constexpr TextureInfo textures[] = {
|
||||
{0, "u_image"},
|
||||
{1, "u_color_ramp"},
|
||||
};
|
||||
static constexpr auto source = R"(
|
||||
|
||||
struct VertexStage {
|
||||
short2 pos [[attribute(0)]];
|
||||
};
|
||||
|
||||
struct FragmentStage {
|
||||
float4 position [[position, invariant]];
|
||||
float2 pos;
|
||||
};
|
||||
|
||||
struct alignas(16) HeatmapTextureDrawableUBO {
|
||||
float4x4 matrix;
|
||||
float2 world;
|
||||
float opacity;
|
||||
bool overdrawInspector;
|
||||
uint8_t pad1, pad2, pad3;
|
||||
};
|
||||
|
||||
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
|
||||
device const HeatmapTextureDrawableUBO& drawable [[buffer(1)]]) {
|
||||
|
||||
const float2 pos = float2(vertx.pos);
|
||||
const float4 position = drawable.matrix * float4(pos * drawable.world, 0, 1);
|
||||
|
||||
return {
|
||||
.position = position,
|
||||
.pos = pos,
|
||||
};
|
||||
}
|
||||
|
||||
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
|
||||
device const HeatmapTextureDrawableUBO& drawable [[buffer(1)]],
|
||||
texture2d<float, access::sample> image [[texture(0)]],
|
||||
texture2d<float, access::sample> color_ramp [[texture(1)]],
|
||||
sampler image_sampler [[sampler(0)]],
|
||||
sampler color_ramp_sampler [[sampler(1)]]) {
|
||||
|
||||
if (drawable.overdrawInspector) {
|
||||
return half4(0.0);
|
||||
}
|
||||
|
||||
float t = image.sample(image_sampler, in.pos).r;
|
||||
float4 color = color_ramp.sample(color_ramp_sampler, float2(t, 0.5));
|
||||
return half4(color * drawable.opacity);
|
||||
}
|
||||
)";
|
||||
};
|
||||
|
||||
} // namespace shaders
|
||||
} // namespace mbgl
|
||||
Reference in New Issue
Block a user