Triangulated fill outlines (Metal) (#1880)

This commit is contained in:
Stefan Karschti
2023-11-24 14:19:41 +02:00
committed by GitHub
parent 09e16d78d8
commit 893c97f20e
21 changed files with 821 additions and 68 deletions

View File

@@ -5,21 +5,44 @@
#include <mbgl/gfx/vertex_vector.hpp>
#include <mbgl/gfx/index_vector.hpp>
#include <mbgl/programs/fill_program.hpp>
#include <mbgl/programs/line_program.hpp>
namespace mbgl {
namespace gfx {
/// Generate fill buffers, without outline
void generateFillBuffers(const GeometryCollection& geometry,
gfx::VertexVector<FillLayoutVertex>& vertices,
SegmentVector<FillAttributes>& triangleSegments,
gfx::IndexVector<Triangles>& triangles);
gfx::VertexVector<FillLayoutVertex>& fillVertices,
gfx::IndexVector<Triangles>& fillIndexes,
SegmentVector<FillAttributes>& fillSegments);
/// Generate fill and outline buffers, with the outline composed of line primitives.
void generateFillAndOutineBuffers(const GeometryCollection& geometry,
gfx::VertexVector<FillLayoutVertex>& vertices,
SegmentVector<FillAttributes>& lineSegments,
gfx::IndexVector<gfx::Lines>& lines,
SegmentVector<FillAttributes>& triangleSegments,
gfx::IndexVector<gfx::Triangles>& triangles);
gfx::IndexVector<gfx::Triangles>& fillIndexes,
SegmentVector<FillAttributes>& fillSegments,
gfx::IndexVector<gfx::Lines>& lineIndexes,
SegmentVector<FillAttributes>& lineSegments);
/// Generate fill and outline buffers, where the outlines are built with triangle primitives
void generateFillAndOutineBuffers(const GeometryCollection& geometry,
gfx::VertexVector<FillLayoutVertex>& fillVertices,
gfx::IndexVector<gfx::Triangles>& fillIndexes,
SegmentVector<FillAttributes>& fillSegments,
gfx::VertexVector<LineLayoutVertex>& lineVertices,
gfx::IndexVector<gfx::Triangles>& lineIndexes,
SegmentVector<LineAttributes>& lineSegments);
/// Generate fill and outline buffers, where the outlines are built both with triangle primitives AND with simple lines
void generateFillAndOutineBuffers(const GeometryCollection& geometry,
gfx::VertexVector<FillLayoutVertex>& fillVertices,
gfx::IndexVector<gfx::Triangles>& fillIndexes,
SegmentVector<FillAttributes>& fillSegments,
gfx::VertexVector<LineLayoutVertex>& lineVertices,
gfx::IndexVector<gfx::Triangles>& lineIndexes,
SegmentVector<LineAttributes>& lineSegments,
gfx::IndexVector<gfx::Lines>& basicLineIndexes,
SegmentVector<FillAttributes>& basicLineSegments);
} // namespace gfx
} // namespace mbgl

View File

@@ -0,0 +1,119 @@
// Generated code, do not modify this file!
#pragma once
#include <mbgl/shaders/shader_source.hpp>
namespace mbgl {
namespace shaders {
template <>
struct ShaderSource<BuiltIn::LineBasicShader, gfx::Backend::Type::OpenGL> {
static constexpr const char* name = "LineBasicShader";
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
// there are also "special" normals that have a bigger length (of up to 126 in
// this case).
// #define scale 63.0
#define scale 0.015873016
layout (location = 0) in vec2 a_pos_normal;
layout (location = 1) in vec4 a_data;
layout (std140) uniform LineBasicUBO {
highp mat4 u_matrix;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
};
layout (std140) uniform LineBasicPropertiesUBO {
highp vec4 u_color;
lowp float u_opacity;
mediump float u_width;
highp vec2 pad1;
};
out vec2 v_normal;
out float v_width;
out float v_gamma_scale;
out highp float v_linesofar;
void main() {
// the distance over which the line edge fades out.
// Retina devices need a smaller distance to avoid aliasing.
float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0;
vec2 a_extrude = a_data.xy - 128.0;
v_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * 2.0;
vec2 pos = floor(a_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
mediump vec2 normal = a_pos_normal - 2.0 * pos;
normal.y = normal.y * 2.0 - 1.0;
v_normal = normal;
// these transformations used to be applied in the JS and native code bases.
// moved them into the shader for clarity and simplicity.
float halfwidth = u_width / 2.0;
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.
mediump vec2 dist = outset * a_extrude * scale;
vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0);
gl_Position = u_matrix * vec4(pos, 0.0, 1.0) + projected_extrude;
// calculate how much the perspective view squishes or stretches the extrude
float extrude_length_without_perspective = length(dist);
float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_units_to_pixels);
v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective;
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 u_device_pixel_ratio;
};
layout (std140) uniform LineBasicPropertiesUBO {
highp vec4 u_color;
lowp float u_opacity;
mediump float u_width;
highp vec2 pad1;
};
in float v_width;
in vec2 v_normal;
in float v_gamma_scale;
void main() {
// Calculate the distance of the pixel from the line in pixels.
float dist = length(v_normal) * v_width;
// Calculate the antialiasing fade factor. This is either when fading in
// the line in case of an offset line (v_width2.t) or when fading out
// (v_width2.s)
float blur2 = (1.0 / u_device_pixel_ratio) * v_gamma_scale;
float alpha = clamp(min(dist + blur2, v_width - dist) / blur2, 0.0, 1.0);
fragColor = u_color * (alpha * u_opacity);
#ifdef OVERDRAW_INSPECTOR
fragColor = vec4(1.0);
#endif
}
)";
};
} // namespace shaders
} // namespace mbgl

View File

@@ -84,6 +84,16 @@ struct alignas(16) LineSDFPropertiesUBO {
};
static_assert(sizeof(LineSDFPropertiesUBO) % 16 == 0);
using LineBasicUBO = LineUBO;
struct alignas(16) LineBasicPropertiesUBO {
Color color;
float opacity;
float width;
float pad1, pad2;
};
static_assert(sizeof(LineBasicPropertiesUBO) % 16 == 0);
/// Property interpolation UBOs
struct alignas(16) LineInterpolationUBO {
float color_t;

View File

@@ -123,6 +123,13 @@ struct alignas(16) LineUBO {
float device_pixel_ratio;
};
struct alignas(16) LineBasicUBO {
float4x4 matrix;
float2 units_to_pixels;
float ratio;
float device_pixel_ratio;
};
struct alignas(16) LineGradientUBO {
float4x4 matrix;
float2 units_to_pixels;
@@ -140,6 +147,13 @@ 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;

View File

@@ -480,5 +480,87 @@ 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 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;
float gamma_scale;
float4 color;
float opacity;
};
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 / line.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 = extrude_length_without_perspective / extrude_length_with_perspective,
.color = props.color,
.opacity = props.opacity,
};
}
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
device const LineBasicUBO& line [[buffer(2)]]) {
// 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 / line.device_pixel_ratio) * in.gamma_scale;
const float alpha = clamp(min(dist + blur2, in.width2 - dist) / blur2, 0.0, 1.0);
return half4(in.color * (alpha * in.opacity));
}
)";
};
} // namespace shaders
} // namespace mbgl

View File

@@ -15,6 +15,7 @@
#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_fill_pattern.hpp>
#include <mbgl/shaders/gl/drawable_fill_outline_pattern.hpp>
#include <mbgl/shaders/gl/drawable_fill_extrusion.hpp>

View File

@@ -21,6 +21,7 @@ enum class BuiltIn {
LinePatternShader,
LineSDFShader,
LineShader,
LineBasicShader,
FillPatternShader,
FillOutlinePatternShader,
FillExtrusionShader,