Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
146 lines
5.5 KiB
C++
146 lines
5.5 KiB
C++
#pragma once
|
|
|
|
#include <mbgl/shaders/hillshade_prepare_layer_ubo.hpp>
|
|
#include <mbgl/shaders/shader_source.hpp>
|
|
#include <mbgl/shaders/mtl/shader_program.hpp>
|
|
|
|
namespace mbgl {
|
|
namespace shaders {
|
|
|
|
#define HILLSHADE_PREPARE_SHADER_PRELUDE \
|
|
R"(
|
|
|
|
enum {
|
|
idHillshadePrepareDrawableUBO = idDrawableReservedVertexOnlyUBO,
|
|
idHillshadePrepareTilePropsUBO = drawableReservedUBOCount,
|
|
hillshadePrepareUBOCount
|
|
};
|
|
|
|
struct alignas(16) HillshadePrepareDrawableUBO {
|
|
/* 0 */ float4x4 matrix;
|
|
/* 64 */
|
|
};
|
|
static_assert(sizeof(HillshadePrepareDrawableUBO) == 4 * 16, "wrong size");
|
|
|
|
struct alignas(16) HillshadePrepareTilePropsUBO {
|
|
/* 0 */ float4 unpack;
|
|
/* 16 */ float2 dimension;
|
|
/* 24 */ float zoom;
|
|
/* 28 */ float maxzoom;
|
|
/* 32 */
|
|
};
|
|
static_assert(sizeof(HillshadePrepareTilePropsUBO) == 2 * 16, "wrong size");
|
|
|
|
)"
|
|
|
|
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 const std::array<AttributeInfo, 2> attributes;
|
|
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
|
|
static const std::array<TextureInfo, 1> textures;
|
|
|
|
static constexpr auto source = HILLSHADE_PREPARE_SHADER_PRELUDE R"(
|
|
struct VertexStage {
|
|
short2 pos [[attribute(hillshadePrepareUBOCount + 0)]];
|
|
short2 texture_pos [[attribute(hillshadePrepareUBOCount + 1)]];
|
|
};
|
|
|
|
struct FragmentStage {
|
|
float4 position [[position, invariant]];
|
|
float2 pos;
|
|
};
|
|
|
|
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
|
|
device const HillshadePrepareDrawableUBO& drawable [[buffer(idHillshadePrepareDrawableUBO)]],
|
|
device const HillshadePrepareTilePropsUBO& tileProps [[buffer(idHillshadePrepareTilePropsUBO)]]) {
|
|
|
|
const float4 position = drawable.matrix * float4(float2(vertx.pos), 0, 1);
|
|
|
|
float2 epsilon = 1.0 / tileProps.dimension;
|
|
float scale = (tileProps.dimension.x - 2.0) / tileProps.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 HillshadePrepareTilePropsUBO& tileProps [[buffer(idHillshadePrepareTilePropsUBO)]],
|
|
texture2d<float, access::sample> image [[texture(0)]],
|
|
sampler image_sampler [[sampler(0)]]) {
|
|
#if defined(OVERDRAW_INSPECTOR)
|
|
return half4(1.0);
|
|
#endif
|
|
|
|
float2 epsilon = 1.0 / tileProps.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, tileProps.unpack);
|
|
float b = getElevation(in.pos + float2(0, -epsilon.y), 0.0, image, image_sampler, tileProps.unpack);
|
|
float c = getElevation(in.pos + float2(epsilon.x, -epsilon.y), 0.0, image, image_sampler, tileProps.unpack);
|
|
float d = getElevation(in.pos + float2(-epsilon.x, 0), 0.0, image, image_sampler, tileProps.unpack);
|
|
//float e = getElevation(in.pos, 0.0, image, image_sampler, tileProps.unpack);
|
|
float f = getElevation(in.pos + float2(epsilon.x, 0), 0.0, image, image_sampler, tileProps.unpack);
|
|
float g = getElevation(in.pos + float2(-epsilon.x, epsilon.y), 0.0, image, image_sampler, tileProps.unpack);
|
|
float h = getElevation(in.pos + float2(0, epsilon.y), 0.0, image, image_sampler, tileProps.unpack);
|
|
float i = getElevation(in.pos + float2(epsilon.x, epsilon.y), 0.0, image, image_sampler, tileProps.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 = tileProps.zoom < 2.0 ? 0.4 : tileProps.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, (tileProps.zoom - tileProps.maxzoom) * exaggeration + 19.2562 - tileProps.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
|