Metal hillshade layer (#1546)
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: mwilsnd <mwilsnd@gmail.com> Co-authored-by: Tim Sylvester <tim@wetdogweather.com> Co-authored-by: mwilsnd <53413200+mwilsnd@users.noreply.github.com> Co-authored-by: Stefan Karschti <stefan.karschti@gmail.com> Co-authored-by: Bart Louwers <bart@emeel.net> 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:
@@ -1114,6 +1114,8 @@ if(MLN_WITH_OPENGL)
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/fill_extrusion_layer_ubo.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/heatmap_layer_ubo.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/heatmap_texture_layer_ubo.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/hillshade_layer_ubo.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/hillshade_prepare_layer_ubo.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/layer_ubo.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/line_layer_ubo.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/raster_layer_ubo.hpp
|
||||
@@ -1177,6 +1179,8 @@ if(MBGL_WITH_METAL)
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/heatmap.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/heatmap_texture.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/hillshade.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/hillshade_prepare.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/line.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/line_gradient.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/raster.hpp
|
||||
|
||||
@@ -965,6 +965,8 @@ MLN_DRAWABLES_HEADERS = [
|
||||
"include/mbgl/shaders/fill_extrusion_layer_ubo.hpp",
|
||||
"include/mbgl/shaders/heatmap_layer_ubo.hpp",
|
||||
"include/mbgl/shaders/heatmap_texture_layer_ubo.hpp",
|
||||
"include/mbgl/shaders/hillshade_layer_ubo.hpp",
|
||||
"include/mbgl/shaders/hillshade_prepare_layer_ubo.hpp",
|
||||
"include/mbgl/shaders/layer_ubo.hpp",
|
||||
"include/mbgl/shaders/line_layer_ubo.hpp",
|
||||
"include/mbgl/shaders/raster_layer_ubo.hpp",
|
||||
@@ -1049,6 +1051,8 @@ MLN_DRAWABLES_MTL_HEADERS = [
|
||||
"include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp",
|
||||
"include/mbgl/shaders/mtl/heatmap.hpp",
|
||||
"include/mbgl/shaders/mtl/heatmap_texture.hpp",
|
||||
"include/mbgl/shaders/mtl/hillshade.hpp",
|
||||
"include/mbgl/shaders/mtl/hillshade_prepare.hpp",
|
||||
"include/mbgl/shaders/mtl/line.hpp",
|
||||
"include/mbgl/shaders/mtl/line_gradient.hpp",
|
||||
"include/mbgl/shaders/mtl/raster.hpp",
|
||||
|
||||
@@ -45,7 +45,6 @@ public:
|
||||
|
||||
bool needsUpload() const noexcept override { return !!image; };
|
||||
|
||||
public:
|
||||
gfx::Texture2D& setUsage(MTL::TextureUsage usage_) noexcept;
|
||||
|
||||
MTL::Texture* getMetalTexture() const noexcept;
|
||||
|
||||
@@ -11,8 +11,6 @@ struct ShaderSource<BuiltIn::FillOutlineShader, gfx::Backend::Type::OpenGL> {
|
||||
static constexpr const char* vertex = R"(layout (std140) uniform FillOutlineDrawableUBO {
|
||||
highp mat4 u_matrix;
|
||||
highp vec2 u_world;
|
||||
highp float pad1;
|
||||
highp float pad2;
|
||||
};
|
||||
layout (std140) uniform FillOutlineEvaluatedPropsUBO {
|
||||
highp vec4 u_outline_color;
|
||||
|
||||
@@ -15,6 +15,10 @@ layout (std140) uniform HillshadeDrawableUBO {
|
||||
highp mat4 u_matrix;
|
||||
highp vec2 u_latrange;
|
||||
highp vec2 u_light;
|
||||
lowp float pad0_;
|
||||
lowp float pad1_;
|
||||
lowp float pad2_;
|
||||
lowp float pad3_;
|
||||
};
|
||||
|
||||
out vec2 v_pos;
|
||||
@@ -31,6 +35,10 @@ layout (std140) uniform HillshadeDrawableUBO {
|
||||
highp mat4 u_matrix;
|
||||
highp vec2 u_latrange;
|
||||
highp vec2 u_light;
|
||||
lowp float pad0_;
|
||||
lowp float pad1_;
|
||||
lowp float pad2_;
|
||||
lowp float pad3_;
|
||||
};
|
||||
|
||||
layout (std140) uniform HillshadeEvaluatedPropsUBO {
|
||||
|
||||
@@ -17,6 +17,10 @@ layout (std140) uniform HillshadePrepareDrawableUBO {
|
||||
highp vec2 u_dimension;
|
||||
highp float u_zoom;
|
||||
highp float u_maxzoom;
|
||||
lowp float pad0_;
|
||||
lowp float pad1_;
|
||||
lowp float pad2_;
|
||||
lowp float pad3_;
|
||||
};
|
||||
|
||||
out vec2 v_pos;
|
||||
@@ -42,6 +46,10 @@ layout (std140) uniform HillshadePrepareDrawableUBO {
|
||||
highp vec2 u_dimension;
|
||||
highp float u_zoom;
|
||||
highp float u_maxzoom;
|
||||
lowp float pad0_;
|
||||
lowp float pad1_;
|
||||
lowp float pad2_;
|
||||
lowp float pad3_;
|
||||
};
|
||||
|
||||
float getElevation(vec2 coord, float bias) {
|
||||
|
||||
27
include/mbgl/shaders/hillshade_layer_ubo.hpp
Normal file
27
include/mbgl/shaders/hillshade_layer_ubo.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/shaders/layer_ubo.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace shaders {
|
||||
|
||||
struct alignas(16) HillshadeDrawableUBO {
|
||||
std::array<float, 4 * 4> matrix;
|
||||
std::array<float, 2> latrange;
|
||||
std::array<float, 2> light;
|
||||
// overdrawInspector is used only in Metal, while in GL this is a 16 bytes empty padding.
|
||||
bool overdrawInspector;
|
||||
uint8_t pad1, pad2, pad3;
|
||||
float pad4, pad5, pad6;
|
||||
};
|
||||
static_assert(sizeof(HillshadeDrawableUBO) % 16 == 0);
|
||||
|
||||
struct alignas(16) HillshadeEvaluatedPropsUBO {
|
||||
Color highlight;
|
||||
Color shadow;
|
||||
Color accent;
|
||||
};
|
||||
static_assert(sizeof(HillshadeEvaluatedPropsUBO) % 16 == 0);
|
||||
|
||||
} // namespace shaders
|
||||
} // namespace mbgl
|
||||
22
include/mbgl/shaders/hillshade_prepare_layer_ubo.hpp
Normal file
22
include/mbgl/shaders/hillshade_prepare_layer_ubo.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/shaders/layer_ubo.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace shaders {
|
||||
|
||||
struct alignas(16) HillshadePrepareDrawableUBO {
|
||||
std::array<float, 4 * 4> matrix;
|
||||
std::array<float, 4> unpack;
|
||||
std::array<float, 2> dimension;
|
||||
float zoom;
|
||||
float maxzoom;
|
||||
// overdrawInspector is used only in Metal, while in GL this is a 16 bytes empty padding.
|
||||
bool overdrawInspector;
|
||||
uint8_t pad1, pad2, pad3;
|
||||
float pad4, pad5, pad6;
|
||||
};
|
||||
static_assert(sizeof(HillshadePrepareDrawableUBO) % 16 == 0);
|
||||
|
||||
} // namespace shaders
|
||||
} // namespace mbgl
|
||||
@@ -26,6 +26,10 @@ using namespace metal;
|
||||
template <typename T1, typename T2>
|
||||
inline auto glMod(T1 x, T2 y) { return x - y * metal::floor(x/y); }
|
||||
|
||||
float radians(float degrees) {
|
||||
return M_PI_F * degrees / 180.0;
|
||||
}
|
||||
|
||||
enum class AttributeSource : int32_t {
|
||||
Constant,
|
||||
PerVertex,
|
||||
|
||||
121
include/mbgl/shaders/mtl/hillshade.hpp
Normal file
121
include/mbgl/shaders/mtl/hillshade.hpp
Normal file
@@ -0,0 +1,121 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/shaders/hillshade_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::HillshadeShader, gfx::Backend::Type::Metal> {
|
||||
static constexpr auto name = "HillshadeShader";
|
||||
static constexpr auto vertexMainFunction = "vertexMain";
|
||||
static constexpr auto fragmentMainFunction = "fragmentMain";
|
||||
|
||||
static constexpr AttributeInfo attributes[] = {
|
||||
{0, gfx::AttributeDataType::Short2, 1, "a_pos"},
|
||||
{1, gfx::AttributeDataType::Short2, 1, "a_texture_pos"},
|
||||
};
|
||||
static constexpr UniformBlockInfo uniforms[] = {
|
||||
MLN_MTL_UNIFORM_BLOCK(2, true, true, HillshadeDrawableUBO),
|
||||
MLN_MTL_UNIFORM_BLOCK(3, false, true, HillshadeEvaluatedPropsUBO),
|
||||
};
|
||||
static constexpr TextureInfo textures[] = {
|
||||
{0, "u_image"},
|
||||
};
|
||||
|
||||
static constexpr auto source = R"(
|
||||
|
||||
struct VertexStage {
|
||||
short2 pos [[attribute(0)]];
|
||||
short2 texture_pos [[attribute(1)]];
|
||||
};
|
||||
|
||||
struct FragmentStage {
|
||||
float4 position [[position, invariant]];
|
||||
float2 pos;
|
||||
};
|
||||
|
||||
struct alignas(16) HillshadeDrawableUBO {
|
||||
float4x4 matrix;
|
||||
float2 latrange;
|
||||
float2 light;
|
||||
bool overdrawInspector;
|
||||
uint8_t pad1, pad2, pad3;
|
||||
float pad4, pad5, pad6;
|
||||
};
|
||||
|
||||
struct alignas(16) HillshadeEvaluatedPropsUBO {
|
||||
float4 highlight;
|
||||
float4 shadow;
|
||||
float4 accent;
|
||||
};
|
||||
|
||||
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
|
||||
device const HillshadeDrawableUBO& drawable [[buffer(2)]]) {
|
||||
|
||||
const float4 position = drawable.matrix * float4(float2(vertx.pos), 0, 1);
|
||||
float2 pos = float2(vertx.texture_pos) / 8192.0;
|
||||
pos.y = 1.0 - pos.y;
|
||||
|
||||
return {
|
||||
.position = position,
|
||||
.pos = pos,
|
||||
};
|
||||
}
|
||||
|
||||
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
|
||||
device const HillshadeDrawableUBO& drawable [[buffer(2)]],
|
||||
device const HillshadeEvaluatedPropsUBO& props [[buffer(3)]],
|
||||
texture2d<float, access::sample> image [[texture(0)]],
|
||||
sampler image_sampler [[sampler(0)]]) {
|
||||
|
||||
if (drawable.overdrawInspector) {
|
||||
return half4(1.0);
|
||||
}
|
||||
|
||||
float4 pixel = image.sample(image_sampler, in.pos);
|
||||
|
||||
float2 deriv = ((pixel.rg * 2.0) - 1.0);
|
||||
|
||||
// We divide the slope by a scale factor based on the cosin of the pixel's approximate latitude
|
||||
// to account for mercator projection distortion. see #4807 for details
|
||||
float scaleFactor = cos(radians((drawable.latrange[0] - drawable.latrange[1]) * in.pos.y + drawable.latrange[1]));
|
||||
// We also multiply the slope by an arbitrary z-factor of 1.25
|
||||
float slope = atan(1.25 * length(deriv) / scaleFactor);
|
||||
float aspect = deriv.x != 0.0 ? atan2(deriv.y, -deriv.x) : M_PI_F / 2.0 * (deriv.y > 0.0 ? 1.0 : -1.0);
|
||||
|
||||
float intensity = drawable.light.x;
|
||||
// We add PI to make this property match the global light object, which adds PI/2 to the light's azimuthal
|
||||
// position property to account for 0deg corresponding to north/the top of the viewport in the style spec
|
||||
// and the original shader was written to accept (-illuminationDirection - 90) as the azimuthal.
|
||||
float azimuth = drawable.light.y + M_PI_F;
|
||||
|
||||
// We scale the slope exponentially based on intensity, using a calculation similar to
|
||||
// the exponential interpolation function in the style spec:
|
||||
// https://github.com/mapbox/mapbox-gl-js/blob/master/src/style-spec/expression/definitions/interpolate.js#L217-L228
|
||||
// so that higher intensity values create more opaque hillshading.
|
||||
float base = 1.875 - intensity * 1.75;
|
||||
float maxValue = 0.5 * M_PI_F;
|
||||
float scaledSlope = intensity != 0.5 ? ((pow(base, slope) - 1.0) / (pow(base, maxValue) - 1.0)) * maxValue : slope;
|
||||
|
||||
// The accent color is calculated with the cosine of the slope while the shade color is calculated with the sine
|
||||
// so that the accent color's rate of change eases in while the shade color's eases out.
|
||||
float accent = cos(scaledSlope);
|
||||
// We multiply both the accent and shade color by a clamped intensity value
|
||||
// so that intensities >= 0.5 do not additionally affect the color values
|
||||
// while intensity values < 0.5 make the overall color more transparent.
|
||||
float4 accent_color = (1.0 - accent) * props.accent * clamp(intensity * 2.0, 0.0, 1.0);
|
||||
float shade = abs(glMod((aspect + azimuth) / M_PI_F + 0.5, 2.0) - 1.0);
|
||||
float4 shade_color = mix(props.shadow, props.highlight, shade) * sin(scaledSlope) * clamp(intensity * 2.0, 0.0, 1.0);
|
||||
float4 color = accent_color * (1.0 - shade_color.a) + shade_color;
|
||||
|
||||
return half4(color);
|
||||
}
|
||||
)";
|
||||
};
|
||||
|
||||
} // namespace shaders
|
||||
} // namespace mbgl
|
||||
138
include/mbgl/shaders/mtl/hillshade_prepare.hpp
Normal file
138
include/mbgl/shaders/mtl/hillshade_prepare.hpp
Normal file
@@ -0,0 +1,138 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/shaders/hillshade_prepare_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::HillshadePrepareShader, gfx::Backend::Type::Metal> {
|
||||
static constexpr auto name = "HillshadePrepareShader";
|
||||
static constexpr auto vertexMainFunction = "vertexMain";
|
||||
static constexpr auto fragmentMainFunction = "fragmentMain";
|
||||
|
||||
static constexpr AttributeInfo attributes[] = {
|
||||
{0, gfx::AttributeDataType::Short2, 1, "a_pos"},
|
||||
{1, gfx::AttributeDataType::Short2, 1, "a_texture_pos"},
|
||||
};
|
||||
static constexpr UniformBlockInfo uniforms[] = {
|
||||
MLN_MTL_UNIFORM_BLOCK(2, true, true, HillshadePrepareDrawableUBO),
|
||||
};
|
||||
static constexpr TextureInfo textures[] = {
|
||||
{0, "u_image"},
|
||||
};
|
||||
static constexpr auto source = R"(
|
||||
|
||||
struct VertexStage {
|
||||
short2 pos [[attribute(0)]];
|
||||
short2 texture_pos [[attribute(1)]];
|
||||
};
|
||||
|
||||
struct FragmentStage {
|
||||
float4 position [[position, invariant]];
|
||||
float2 pos;
|
||||
};
|
||||
|
||||
struct alignas(16) HillshadePrepareDrawableUBO {
|
||||
float4x4 matrix;
|
||||
float4 unpack;
|
||||
float2 dimension;
|
||||
float zoom;
|
||||
float maxzoom;
|
||||
bool overdrawInspector;
|
||||
uint8_t pad1, pad2, pad3;
|
||||
float pad4, pad5, pad6;
|
||||
};
|
||||
|
||||
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
|
||||
device const HillshadePrepareDrawableUBO& drawable [[buffer(2)]]) {
|
||||
|
||||
const float4 position = drawable.matrix * float4(float2(vertx.pos), 0, 1);
|
||||
|
||||
float2 epsilon = 1.0 / drawable.dimension;
|
||||
float scale = (drawable.dimension.x - 2.0) / drawable.dimension.x;
|
||||
const float2 pos = (float2(vertx.texture_pos) / 8192.0) * scale + epsilon;
|
||||
|
||||
return {
|
||||
.position = position,
|
||||
.pos = pos,
|
||||
};
|
||||
}
|
||||
|
||||
float getElevation(float2 coord, float bias, texture2d<float, access::sample> image, sampler image_sampler, float4 unpack) {
|
||||
// Convert encoded elevation value to meters
|
||||
float4 data = image.sample(image_sampler, coord) * 255.0;
|
||||
data.a = -1.0;
|
||||
return dot(data, unpack) / 4.0;
|
||||
}
|
||||
|
||||
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
|
||||
device const HillshadePrepareDrawableUBO& drawable [[buffer(2)]],
|
||||
texture2d<float, access::sample> image [[texture(0)]],
|
||||
sampler image_sampler [[sampler(0)]]) {
|
||||
|
||||
if (drawable.overdrawInspector) {
|
||||
return half4(1.0);
|
||||
}
|
||||
|
||||
float2 epsilon = 1.0 / drawable.dimension;
|
||||
|
||||
// queried pixels:
|
||||
// +-----------+
|
||||
// | | | |
|
||||
// | a | b | c |
|
||||
// | | | |
|
||||
// +-----------+
|
||||
// | | | |
|
||||
// | d | e | f |
|
||||
// | | | |
|
||||
// +-----------+
|
||||
// | | | |
|
||||
// | g | h | i |
|
||||
// | | | |
|
||||
// +-----------+
|
||||
|
||||
float a = getElevation(in.pos + float2(-epsilon.x, -epsilon.y), 0.0, image, image_sampler, drawable.unpack);
|
||||
float b = getElevation(in.pos + float2(0, -epsilon.y), 0.0, image, image_sampler, drawable.unpack);
|
||||
float c = getElevation(in.pos + float2(epsilon.x, -epsilon.y), 0.0, image, image_sampler, drawable.unpack);
|
||||
float d = getElevation(in.pos + float2(-epsilon.x, 0), 0.0, image, image_sampler, drawable.unpack);
|
||||
float e = getElevation(in.pos, 0.0, image, image_sampler, drawable.unpack);
|
||||
float f = getElevation(in.pos + float2(epsilon.x, 0), 0.0, image, image_sampler, drawable.unpack);
|
||||
float g = getElevation(in.pos + float2(-epsilon.x, epsilon.y), 0.0, image, image_sampler, drawable.unpack);
|
||||
float h = getElevation(in.pos + float2(0, epsilon.y), 0.0, image, image_sampler, drawable.unpack);
|
||||
float i = getElevation(in.pos + float2(epsilon.x, epsilon.y), 0.0, image, image_sampler, drawable.unpack);
|
||||
|
||||
// here we divide the x and y slopes by 8 * pixel size
|
||||
// where pixel size (aka meters/pixel) is:
|
||||
// circumference of the world / (pixels per tile * number of tiles)
|
||||
// which is equivalent to: 8 * 40075016.6855785 / (512 * pow(2, u_zoom))
|
||||
// which can be reduced to: pow(2, 19.25619978527 - u_zoom)
|
||||
// we want to vertically exaggerate the hillshading though, because otherwise
|
||||
// it is barely noticeable at low zooms. to do this, we multiply this by some
|
||||
// scale factor pow(2, (u_zoom - u_maxzoom) * a) where a is an arbitrary value
|
||||
// Here we use a=0.3 which works out to the expression below. see
|
||||
// nickidlugash's awesome breakdown for more info
|
||||
// https://github.com/mapbox/mapbox-gl-js/pull/5286#discussion_r148419556
|
||||
float exaggeration = drawable.zoom < 2.0 ? 0.4 : drawable.zoom < 4.5 ? 0.35 : 0.3;
|
||||
|
||||
float2 deriv = float2(
|
||||
(c + f + f + i) - (a + d + d + g),
|
||||
(g + h + h + i) - (a + b + b + c)
|
||||
) / pow(2.0, (drawable.zoom - drawable.maxzoom) * exaggeration + 19.2562 - drawable.zoom);
|
||||
|
||||
float4 color = clamp(float4(
|
||||
deriv.x / 2.0 + 0.5,
|
||||
deriv.y / 2.0 + 0.5,
|
||||
1.0,
|
||||
1.0), 0.0, 1.0);
|
||||
|
||||
return half4(color);
|
||||
}
|
||||
)";
|
||||
};
|
||||
|
||||
} // namespace shaders
|
||||
} // namespace mbgl
|
||||
@@ -5,6 +5,10 @@ layout (std140) uniform HillshadeDrawableUBO {
|
||||
highp mat4 u_matrix;
|
||||
highp vec2 u_latrange;
|
||||
highp vec2 u_light;
|
||||
lowp float pad0_;
|
||||
lowp float pad1_;
|
||||
lowp float pad2_;
|
||||
lowp float pad3_;
|
||||
};
|
||||
|
||||
layout (std140) uniform HillshadeEvaluatedPropsUBO {
|
||||
|
||||
@@ -5,6 +5,10 @@ layout (std140) uniform HillshadeDrawableUBO {
|
||||
highp mat4 u_matrix;
|
||||
highp vec2 u_latrange;
|
||||
highp vec2 u_light;
|
||||
lowp float pad0_;
|
||||
lowp float pad1_;
|
||||
lowp float pad2_;
|
||||
lowp float pad3_;
|
||||
};
|
||||
|
||||
out vec2 v_pos;
|
||||
|
||||
@@ -11,6 +11,10 @@ layout (std140) uniform HillshadePrepareDrawableUBO {
|
||||
highp vec2 u_dimension;
|
||||
highp float u_zoom;
|
||||
highp float u_maxzoom;
|
||||
lowp float pad0_;
|
||||
lowp float pad1_;
|
||||
lowp float pad2_;
|
||||
lowp float pad3_;
|
||||
};
|
||||
|
||||
float getElevation(vec2 coord, float bias) {
|
||||
|
||||
@@ -7,6 +7,10 @@ layout (std140) uniform HillshadePrepareDrawableUBO {
|
||||
highp vec2 u_dimension;
|
||||
highp float u_zoom;
|
||||
highp float u_maxzoom;
|
||||
lowp float pad0_;
|
||||
lowp float pad1_;
|
||||
lowp float pad2_;
|
||||
lowp float pad3_;
|
||||
};
|
||||
|
||||
out vec2 v_pos;
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
#include <mbgl/shaders/mtl/fill_extrusion_pattern.hpp>
|
||||
#include <mbgl/shaders/mtl/heatmap.hpp>
|
||||
#include <mbgl/shaders/mtl/heatmap_texture.hpp>
|
||||
#include <mbgl/shaders/mtl/hillshade.hpp>
|
||||
#include <mbgl/shaders/mtl/hillshade_prepare.hpp>
|
||||
#include <mbgl/shaders/mtl/line.hpp>
|
||||
#include <mbgl/shaders/mtl/line_gradient.hpp>
|
||||
#include <mbgl/shaders/mtl/fill.hpp>
|
||||
@@ -130,7 +132,8 @@ void RendererBackend::initShaders(gfx::ShaderRegistry& shaders, const ProgramPar
|
||||
shaders::BuiltIn::FillExtrusionPatternShader,
|
||||
shaders::BuiltIn::HeatmapShader,
|
||||
shaders::BuiltIn::HeatmapTextureShader,
|
||||
// shaders::BuiltIn::HillshadeShader,
|
||||
shaders::BuiltIn::HillshadeShader,
|
||||
shaders::BuiltIn::HillshadePrepareShader,
|
||||
shaders::BuiltIn::LineShader,
|
||||
shaders::BuiltIn::LineGradientShader,
|
||||
// shaders::BuiltIn::LineSDFShader,
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <mbgl/renderer/layer_group.hpp>
|
||||
#include <mbgl/renderer/paint_parameters.hpp>
|
||||
#include <mbgl/renderer/render_tree.hpp>
|
||||
#include <mbgl/shaders/hillshade_layer_ubo.hpp>
|
||||
#include <mbgl/style/layers/hillshade_layer_properties.hpp>
|
||||
#include <mbgl/util/convert.hpp>
|
||||
#include <mbgl/util/string_indexer.hpp>
|
||||
@@ -12,20 +13,7 @@
|
||||
namespace mbgl {
|
||||
|
||||
using namespace style;
|
||||
|
||||
struct alignas(16) HillshadeDrawableUBO {
|
||||
std::array<float, 4 * 4> matrix;
|
||||
std::array<float, 2> latrange;
|
||||
std::array<float, 2> light;
|
||||
};
|
||||
static_assert(sizeof(HillshadeDrawableUBO) % 16 == 0);
|
||||
|
||||
struct alignas(16) HillshadeEvaluatedPropsUBO {
|
||||
Color highlight;
|
||||
Color shadow;
|
||||
Color accent;
|
||||
};
|
||||
static_assert(sizeof(HillshadeEvaluatedPropsUBO) % 16 == 0);
|
||||
using namespace shaders;
|
||||
|
||||
static const StringIdentity idHillshadeDrawableUBOName = StringIndexer::get("HillshadeDrawableUBO");
|
||||
static const StringIdentity idHillshadeEvaluatedPropsUBOName = StringIndexer::get("HillshadeEvaluatedPropsUBO");
|
||||
@@ -77,7 +65,14 @@ void HillshadeLayerTweaker::execute(LayerGroupBase& layerGroup,
|
||||
tileID, renderTree, parameters.state, {0.f, 0.f}, TranslateAnchorType::Viewport, false, false, true);
|
||||
HillshadeDrawableUBO drawableUBO = {/* .matrix = */ util::cast<float>(matrix),
|
||||
/* .latrange = */ getLatRange(tileID),
|
||||
/* .light = */ getLight(parameters, evaluated)};
|
||||
/* .light = */ getLight(parameters, evaluated),
|
||||
/* .overdrawInspector = */ overdrawInspector,
|
||||
/* .pad1/2/3 = */ 0,
|
||||
0,
|
||||
0,
|
||||
/* .pad4/5/6 = */ 0,
|
||||
0,
|
||||
0};
|
||||
|
||||
drawable.mutableUniformBuffers().createOrUpdate(idHillshadeDrawableUBOName, &drawableUBO, parameters.context);
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <mbgl/renderer/layer_group.hpp>
|
||||
#include <mbgl/renderer/paint_parameters.hpp>
|
||||
#include <mbgl/renderer/render_tree.hpp>
|
||||
#include <mbgl/shaders/hillshade_prepare_layer_ubo.hpp>
|
||||
#include <mbgl/style/layers/hillshade_layer_properties.hpp>
|
||||
#include <mbgl/util/convert.hpp>
|
||||
#include <mbgl/util/string_indexer.hpp>
|
||||
@@ -13,15 +14,7 @@
|
||||
namespace mbgl {
|
||||
|
||||
using namespace style;
|
||||
|
||||
struct alignas(16) HillshadePrepareDrawableUBO {
|
||||
std::array<float, 4 * 4> matrix;
|
||||
std::array<float, 4> unpack;
|
||||
std::array<float, 2> dimension;
|
||||
float zoom;
|
||||
float maxzoom;
|
||||
};
|
||||
static_assert(sizeof(HillshadePrepareDrawableUBO) % 16 == 0);
|
||||
using namespace shaders;
|
||||
|
||||
static const StringIdentity idHillshadePrepareDrawableUBOName = StringIndexer::get("HillshadePrepareDrawableUBO");
|
||||
|
||||
@@ -56,7 +49,7 @@ void HillshadePrepareLayerTweaker::execute(LayerGroupBase& layerGroup,
|
||||
const auto& drawableData = static_cast<const gfx::HillshadePrepareDrawableData&>(*drawable.getData());
|
||||
|
||||
mat4 matrix;
|
||||
matrix::ortho(matrix, 0, util::EXTENT, -util::EXTENT, 0, 0, 1);
|
||||
matrix::ortho(matrix, 0, util::EXTENT, -util::EXTENT, 0, -1, 1);
|
||||
matrix::translate(matrix, matrix, 0, -util::EXTENT, 0);
|
||||
|
||||
HillshadePrepareDrawableUBO drawableUBO = {
|
||||
@@ -64,7 +57,14 @@ void HillshadePrepareLayerTweaker::execute(LayerGroupBase& layerGroup,
|
||||
/* .unpack = */ getUnpackVector(drawableData.encoding),
|
||||
/* .dimension = */ {static_cast<float>(drawableData.stride), static_cast<float>(drawableData.stride)},
|
||||
/* .zoom = */ static_cast<float>(tileID.canonical.z),
|
||||
/* .maxzoom = */ static_cast<float>(drawableData.maxzoom)};
|
||||
/* .maxzoom = */ static_cast<float>(drawableData.maxzoom),
|
||||
/* .overdrawInspector = */ overdrawInspector,
|
||||
/* .pad1/2/3 = */ 0,
|
||||
0,
|
||||
0,
|
||||
/* .pad4/5/6 = */ 0,
|
||||
0,
|
||||
0};
|
||||
|
||||
drawable.mutableUniformBuffers().createOrUpdate(
|
||||
idHillshadePrepareDrawableUBOName, &drawableUBO, parameters.context);
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <mbgl/renderer/layers/hillshade_prepare_layer_tweaker.hpp>
|
||||
#include <mbgl/renderer/layer_group.hpp>
|
||||
#include <mbgl/renderer/render_target.hpp>
|
||||
#include <mbgl/renderer/update_parameters.hpp>
|
||||
#include <mbgl/shaders/shader_program_base.hpp>
|
||||
#include <mbgl/gfx/drawable_builder.hpp>
|
||||
#include <mbgl/gfx/drawable_impl.hpp>
|
||||
@@ -72,9 +73,7 @@ void RenderHillshadeLayer::evaluate(const PropertyEvaluationParameters& paramete
|
||||
properties->renderPasses = mbgl::underlying_type(passes);
|
||||
evaluatedProperties = std::move(properties);
|
||||
#if MLN_DRAWABLE_RENDERER
|
||||
if (layerGroup) {
|
||||
layerGroup->setLayerTweaker(std::make_shared<HillshadeLayerTweaker>(getID(), evaluatedProperties));
|
||||
}
|
||||
updateLayerTweaker();
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -285,10 +284,20 @@ static const StringIdentity idPosAttribName = StringIndexer::get("a_pos");
|
||||
static const StringIdentity idTexturePosAttribName = StringIndexer::get("a_texture_pos");
|
||||
static const StringIdentity idTexImageName = StringIndexer::get("u_image");
|
||||
|
||||
#if MLN_DRAWABLE_RENDERER
|
||||
void RenderHillshadeLayer::updateLayerTweaker() {
|
||||
if (layerGroup) {
|
||||
tweaker = std::make_shared<HillshadeLayerTweaker>(getID(), evaluatedProperties);
|
||||
tweaker->enableOverdrawInspector(overdrawInspector);
|
||||
layerGroup->setLayerTweaker(tweaker);
|
||||
}
|
||||
}
|
||||
#endif // MLN_DRAWABLE_RENDERER
|
||||
|
||||
void RenderHillshadeLayer::update(gfx::ShaderRegistry& shaders,
|
||||
gfx::Context& context,
|
||||
[[maybe_unused]] const TransformState& state,
|
||||
const std::shared_ptr<UpdateParameters>&,
|
||||
const std::shared_ptr<UpdateParameters>& updateParameters,
|
||||
[[maybe_unused]] const RenderTree& renderTree,
|
||||
UniqueChangeRequestVec& changes) {
|
||||
std::unique_lock<std::mutex> guard(mutex);
|
||||
@@ -304,8 +313,8 @@ void RenderHillshadeLayer::update(gfx::ShaderRegistry& shaders,
|
||||
if (!layerGroup_) {
|
||||
return;
|
||||
}
|
||||
layerGroup_->setLayerTweaker(std::make_shared<HillshadeLayerTweaker>(getID(), evaluatedProperties));
|
||||
setLayerGroup(std::move(layerGroup_), changes);
|
||||
updateLayerTweaker();
|
||||
}
|
||||
|
||||
auto* tileLayerGroup = static_cast<TileLayerGroup*>(layerGroup.get());
|
||||
@@ -321,6 +330,14 @@ void RenderHillshadeLayer::update(gfx::ShaderRegistry& shaders,
|
||||
return;
|
||||
}
|
||||
|
||||
const bool overdraw = !!(updateParameters->debugOptions & MapDebugOptions::Overdraw);
|
||||
if (overdrawInspector != overdraw) {
|
||||
overdrawInspector = overdraw;
|
||||
if (tweaker) {
|
||||
tweaker->enableOverdrawInspector(overdrawInspector);
|
||||
}
|
||||
}
|
||||
|
||||
auto renderPass = RenderPass::Translucent;
|
||||
if (!(mbgl::underlying_type(renderPass) & evaluatedProperties->renderPasses)) {
|
||||
return;
|
||||
@@ -372,8 +389,9 @@ void RenderHillshadeLayer::update(gfx::ShaderRegistry& shaders,
|
||||
if (!singleTileLayerGroup) {
|
||||
return;
|
||||
}
|
||||
singleTileLayerGroup->setLayerTweaker(
|
||||
std::make_shared<HillshadePrepareLayerTweaker>(getID(), evaluatedProperties));
|
||||
const auto prepareTweaker = std::make_shared<HillshadePrepareLayerTweaker>(getID(), evaluatedProperties);
|
||||
prepareTweaker->enableOverdrawInspector(overdrawInspector);
|
||||
singleTileLayerGroup->setLayerTweaker(prepareTweaker);
|
||||
renderTarget->addLayerGroup(singleTileLayerGroup, /*replace=*/true);
|
||||
|
||||
gfx::VertexAttributeArray hillshadePrepareVertexAttrs;
|
||||
|
||||
@@ -9,8 +9,10 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class HillshadeProgram;
|
||||
class HillshadePrepareProgram;
|
||||
#if MLN_DRAWABLE_RENDERER
|
||||
class HillshadeLayerTweaker;
|
||||
using HillshadeLayerTweakerPtr = std::shared_ptr<HillshadeLayerTweaker>;
|
||||
#endif // MLN_DRAWABLE_RENDERER
|
||||
|
||||
class RenderHillshadeLayer : public RenderLayer {
|
||||
public:
|
||||
@@ -41,6 +43,10 @@ private:
|
||||
void render(PaintParameters&) override;
|
||||
#endif
|
||||
|
||||
#if MLN_DRAWABLE_RENDERER
|
||||
void updateLayerTweaker();
|
||||
#endif // MLN_DRAWABLE_RENDERER
|
||||
|
||||
void prepare(const LayerPrepareParameters&) override;
|
||||
|
||||
#if MLN_DRAWABLE_RENDERER
|
||||
@@ -68,6 +74,9 @@ private:
|
||||
|
||||
using HillshadeVertexVector = gfx::VertexVector<HillshadeLayoutVertex>;
|
||||
std::shared_ptr<HillshadeVertexVector> staticDataSharedVertices;
|
||||
|
||||
HillshadeLayerTweakerPtr tweaker;
|
||||
bool overdrawInspector = false;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user