Skip to content

Commit

Permalink
Fill outline triangulated reorganized (maplibre#2266)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
alexcristici and pre-commit-ci[bot] authored Apr 16, 2024
1 parent a939c9a commit 8264a1c
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 373 deletions.
11 changes: 6 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1025,18 +1025,19 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_debug.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_fill.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_fill_outline.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_line.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_line_gradient.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_line_pattern.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_line_sdf.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_fill_outline_pattern.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_fill_pattern.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_fill_outline_pattern.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_fill_outline_triangulated.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_fill_extrusion.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_fill_extrusion_pattern.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_heatmap.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_heatmap_texture.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_hillshade_prepare.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_hillshade.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_line.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_line_gradient.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_line_pattern.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_line_sdf.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_raster.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_symbol_icon.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/drawable_symbol_sdf.hpp
Expand Down
4 changes: 2 additions & 2 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ MLN_GENERATED_OPENGL_SHADER_HEADERS = [
"include/mbgl/shaders/gl/drawable_debug.hpp",
"include/mbgl/shaders/gl/drawable_fill.hpp",
"include/mbgl/shaders/gl/drawable_fill_outline.hpp",
"include/mbgl/shaders/gl/drawable_fill_pattern.hpp",
"include/mbgl/shaders/gl/drawable_fill_outline_pattern.hpp",
"include/mbgl/shaders/gl/drawable_fill_outline_triangulated.hpp",
"include/mbgl/shaders/gl/drawable_fill_extrusion.hpp",
"include/mbgl/shaders/gl/drawable_fill_extrusion_pattern.hpp",
"include/mbgl/shaders/gl/drawable_fill_pattern.hpp",
"include/mbgl/shaders/gl/drawable_heatmap.hpp",
"include/mbgl/shaders/gl/drawable_heatmap_texture.hpp",
"include/mbgl/shaders/gl/drawable_hillshade_prepare.hpp",
Expand All @@ -83,7 +84,6 @@ MLN_GENERATED_OPENGL_SHADER_HEADERS = [
"include/mbgl/shaders/gl/drawable_line_pattern.hpp",
"include/mbgl/shaders/gl/drawable_line_sdf.hpp",
"include/mbgl/shaders/gl/drawable_line.hpp",
"include/mbgl/shaders/gl/drawable_line_basic.hpp",
"include/mbgl/shaders/gl/drawable_raster.hpp",
"include/mbgl/shaders/gl/drawable_symbol_icon.hpp",
"include/mbgl/shaders/gl/drawable_symbol_sdf.hpp",
Expand Down
25 changes: 25 additions & 0 deletions include/mbgl/shaders/fill_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,30 @@ enum {
fillOutlinePatternUBOCount
};

//
// Fill outline triangulated

struct alignas(16) FillOutlineTriangulatedDrawableUBO {
std::array<float, 4 * 4> matrix;
std::array<float, 2> units_to_pixels;
float ratio;
float pad;
};
static_assert(sizeof(FillOutlineTriangulatedDrawableUBO) % 16 == 0);

struct alignas(16) FillOutlineTriangulatedPropertiesUBO {
Color color;
float opacity;
float width;
float pad1, pad2;
};
static_assert(sizeof(FillOutlineTriangulatedPropertiesUBO) % 16 == 0);

enum {
idFillOutlineTriangulatedDrawableUBO,
idFillOutlineTriangulatedPropertiesUBO,
fillOutlineTriangulatedUBOCount
};

} // namespace shaders
} // namespace mbgl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace mbgl {
namespace shaders {

template <>
struct ShaderSource<BuiltIn::LineBasicShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "LineBasicShader";
struct ShaderSource<BuiltIn::FillOutlineTriangulatedShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "FillOutlineTriangulatedShader";
static constexpr const char* vertex = R"(// floor(127 / 2) == 63.0
// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is
// stored in a byte (-128..127). we scale regular normals up to length 63, but
Expand All @@ -19,18 +19,17 @@ struct ShaderSource<BuiltIn::LineBasicShader, gfx::Backend::Type::OpenGL> {
layout (location = 0) in vec2 a_pos_normal;
layout (location = 1) in vec4 a_data;
layout (std140) uniform LineBasicUBO {
layout (std140) uniform FillOutlineTriangulatedDrawableUBO {
highp mat4 u_matrix;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float pad0;
};
layout (std140) uniform LineBasicPropertiesUBO {
layout (std140) uniform FillOutlineTriangulatedPropertiesUBO {
highp vec4 u_color;
lowp float u_opacity;
mediump float u_width;
highp vec2 pad1;
};
Expand Down Expand Up @@ -77,18 +76,10 @@ void main() {
v_width = outset;
}
)";
static constexpr const char* fragment = R"(layout (std140) uniform LineBasicUBO {
highp mat4 u_matrix;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float pad0;
};
layout (std140) uniform LineBasicPropertiesUBO {
static constexpr const char* fragment = R"(layout (std140) uniform FillOutlineTriangulatedPropertiesUBO {
highp vec4 u_color;
lowp float u_opacity;
mediump float u_width;
highp vec2 pad1;
};
Expand Down
14 changes: 7 additions & 7 deletions include/mbgl/shaders/gl/shader_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ struct ShaderInfo<BuiltIn::FillOutlinePatternShader, gfx::Backend::Type::OpenGL>
static const std::vector<TextureInfo> textures;
};

template <>
struct ShaderInfo<BuiltIn::FillOutlineTriangulatedShader, gfx::Backend::Type::OpenGL> {
static const std::vector<AttributeInfo> attributes;
static const std::vector<UniformBlockInfo> uniformBlocks;
static const std::vector<TextureInfo> textures;
};

template <>
struct ShaderInfo<BuiltIn::FillExtrusionShader, gfx::Backend::Type::OpenGL> {
static const std::vector<AttributeInfo> attributes;
Expand Down Expand Up @@ -177,13 +184,6 @@ struct ShaderInfo<BuiltIn::LineShader, gfx::Backend::Type::OpenGL> {
static const std::vector<TextureInfo> textures;
};

template <>
struct ShaderInfo<BuiltIn::LineBasicShader, gfx::Backend::Type::OpenGL> {
static const std::vector<AttributeInfo> attributes;
static const std::vector<UniformBlockInfo> uniformBlocks;
static const std::vector<TextureInfo> textures;
};

template <>
struct ShaderInfo<BuiltIn::RasterShader, gfx::Backend::Type::OpenGL> {
static const std::vector<AttributeInfo> attributes;
Expand Down
25 changes: 0 additions & 25 deletions include/mbgl/shaders/line_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,30 +182,5 @@ enum {
lineSDFUBOCount
};

//
// Line basic

struct alignas(16) LineBasicUBO {
std::array<float, 4 * 4> matrix;
std::array<float, 2> units_to_pixels;
float ratio;
float pad;
};
static_assert(sizeof(LineBasicUBO) % 16 == 0);

struct alignas(16) LineBasicPropertiesUBO {
Color color;
float opacity;
float width;
float pad1, pad2;
};
static_assert(sizeof(LineBasicPropertiesUBO) % 16 == 0);

enum {
idLineBasicUBO,
idLineBasicPropertiesUBO,
lineBasicUBOCount
};

} // namespace shaders
} // namespace mbgl
14 changes: 0 additions & 14 deletions include/mbgl/shaders/mtl/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ struct alignas(16) LineUBO {
float pad1, pad2, pad3;
};
struct alignas(16) LineBasicUBO {
float4x4 matrix;
float2 units_to_pixels;
float ratio;
float pad;
};
struct alignas(16) LineGradientUBO {
float4x4 matrix;
float ratio;
Expand All @@ -88,13 +81,6 @@ struct alignas(16) LinePropertiesUBO {
float pad1, pad2, pad3;
};
struct alignas(16) LineBasicPropertiesUBO {
float4 color;
float opacity;
float width;
float pad1, pad2;
};
struct alignas(16) LineGradientPropertiesUBO {
float blur;
float opacity;
Expand Down
92 changes: 92 additions & 0 deletions include/mbgl/shaders/mtl/fill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,5 +518,97 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]],
)";
};

template <>
struct ShaderSource<BuiltIn::FillOutlineTriangulatedShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "FillOutlineTriangulatedShader";
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<UniformBlockInfo, 2> uniforms;
static const std::array<TextureInfo, 0> textures;

static constexpr auto source = R"(
struct VertexStage {
short2 pos_normal [[attribute(0)]];
uchar4 data [[attribute(1)]];
};
struct FragmentStage {
float4 position [[position, invariant]];
float width2;
float2 normal;
half gamma_scale;
};
struct alignas(16) FillOutlineTriangulatedDrawableUBO {
float4x4 matrix;
float2 units_to_pixels;
float ratio;
float pad;
};
struct alignas(16) FillOutlineTriangulatedPropertiesUBO {
float4 color;
float opacity;
float width;
float pad1, pad2;
};
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
device const FillOutlineTriangulatedDrawableUBO& drawable [[buffer(2)]],
device const FillOutlineTriangulatedPropertiesUBO& props [[buffer(3)]]) {
// the distance over which the line edge fades out.
// Retina devices need a smaller distance to avoid aliasing.
const float ANTIALIASING = 1.0 / DEVICE_PIXEL_RATIO / 2.0;
const float2 a_extrude = float2(vertx.data.xy) - 128.0;
const float2 pos = floor(float2(vertx.pos_normal) * 0.5);
// x is 1 if it's a round cap, 0 otherwise
// y is 1 if the normal points up, and -1 if it points down
// We store these in the least significant bit of a_pos_normal
const float2 normal = float2(vertx.pos_normal) - 2.0 * pos;
const float2 v_normal = float2(normal.x, normal.y * 2.0 - 1.0);
const float halfwidth = props.width / 2.0;
const float outset = halfwidth + (halfwidth == 0.0 ? 0.0 : ANTIALIASING);
// Scale the extrusion vector down to a normal and then up by the line width of this vertex.
const float2 dist = outset * a_extrude * LINE_NORMAL_SCALE;
const float4 projected_extrude = drawable.matrix * float4(dist / drawable.ratio, 0.0, 0.0);
const float4 position = drawable.matrix * float4(pos, 0.0, 1.0) + projected_extrude;
// calculate how much the perspective view squishes or stretches the extrude
const float extrude_length_without_perspective = length(dist);
const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * drawable.units_to_pixels);
return {
.position = position,
.width2 = outset,
.normal = v_normal,
.gamma_scale = half(extrude_length_without_perspective / extrude_length_with_perspective),
};
}
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
device const FillOutlineTriangulatedPropertiesUBO& props [[buffer(3)]]) {
// Calculate the distance of the pixel from the line in pixels.
const float dist = length(in.normal) * in.width2;
// Calculate the antialiasing fade factor. This is either when fading in the
// line in case of an offset line (`v_width2.y`) or when fading out (`v_width2.x`)
const float blur2 = (1.0 / DEVICE_PIXEL_RATIO) * in.gamma_scale;
const float alpha = clamp(min(dist + blur2, in.width2 - dist) / blur2, 0.0, 1.0);
return half4(props.color * (alpha * props.opacity));
}
)";
};

} // namespace shaders
} // namespace mbgl
79 changes: 0 additions & 79 deletions include/mbgl/shaders/mtl/line.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,84 +654,5 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]],
)";
};

template <>
struct ShaderSource<BuiltIn::LineBasicShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "LineBasicShader";
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<UniformBlockInfo, 2> uniforms;
static const std::array<TextureInfo, 0> textures;

static constexpr auto source = R"(
struct VertexStage {
short2 pos_normal [[attribute(0)]];
uchar4 data [[attribute(1)]];
};
struct FragmentStage {
float4 position [[position, invariant]];
float width2;
float2 normal;
half gamma_scale;
};
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
device const LineBasicUBO& line [[buffer(2)]],
device const LineBasicPropertiesUBO& props [[buffer(3)]]) {
// the distance over which the line edge fades out.
// Retina devices need a smaller distance to avoid aliasing.
const float ANTIALIASING = 1.0 / DEVICE_PIXEL_RATIO / 2.0;
const float2 a_extrude = float2(vertx.data.xy) - 128.0;
const float2 pos = floor(float2(vertx.pos_normal) * 0.5);
// x is 1 if it's a round cap, 0 otherwise
// y is 1 if the normal points up, and -1 if it points down
// We store these in the least significant bit of a_pos_normal
const float2 normal = float2(vertx.pos_normal) - 2.0 * pos;
const float2 v_normal = float2(normal.x, normal.y * 2.0 - 1.0);
const float halfwidth = props.width / 2.0;
const float outset = halfwidth + (halfwidth == 0.0 ? 0.0 : ANTIALIASING);
// Scale the extrusion vector down to a normal and then up by the line width of this vertex.
const float2 dist = outset * a_extrude * LINE_NORMAL_SCALE;
const float4 projected_extrude = line.matrix * float4(dist / line.ratio, 0.0, 0.0);
const float4 position = line.matrix * float4(pos, 0.0, 1.0) + projected_extrude;
// calculate how much the perspective view squishes or stretches the extrude
const float extrude_length_without_perspective = length(dist);
const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * line.units_to_pixels);
return {
.position = position,
.width2 = outset,
.normal = v_normal,
.gamma_scale = half(extrude_length_without_perspective / extrude_length_with_perspective),
};
}
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
device const LineBasicUBO& line [[buffer(2)]],
device const LineBasicPropertiesUBO& props [[buffer(3)]]) {
// Calculate the distance of the pixel from the line in pixels.
const float dist = length(in.normal) * in.width2;
// Calculate the antialiasing fade factor. This is either when fading in the
// line in case of an offset line (`v_width2.y`) or when fading out (`v_width2.x`)
const float blur2 = (1.0 / DEVICE_PIXEL_RATIO) * in.gamma_scale;
const float alpha = clamp(min(dist + blur2, in.width2 - dist) / blur2, 0.0, 1.0);
return half4(props.color * (alpha * props.opacity));
}
)";
};

} // namespace shaders
} // namespace mbgl
Loading

0 comments on commit 8264a1c

Please sign in to comment.