Skip to content

Commit

Permalink
HiSilicon fix: software clipping of polygon outlines
Browse files Browse the repository at this point in the history
  • Loading branch information
kubapelc committed May 20, 2024
1 parent 266897d commit 98167ba
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/shaders/fill_outline.fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
in vec2 v_pos;
#ifdef GLOBE
in float v_depth;
#endif

#pragma mapbox: define highp vec4 outline_color
#pragma mapbox: define lowp float opacity
Expand All @@ -11,6 +14,15 @@ void main() {
float alpha = 1.0 - smoothstep(0.0, 1.0, dist);
fragColor = outline_color * (alpha * opacity);

#ifdef GLOBE
if (v_depth > 1.0) {
// Hides polygon outlines that are visible on the backfacing side of the globe.
// This is needed, because some hardware seems to apply glDepthRange first and then apply clipping, which is the wrong order.
// Other layers fix this by using backface culling, but that is unavailable for line primitives, so we clip the lines in software here.
discard;
}
#endif

#ifdef OVERDRAW_INSPECTOR
fragColor = vec4(1.0);
#endif
Expand Down
6 changes: 6 additions & 0 deletions src/shaders/fill_outline.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ uniform vec2 u_fill_translate;
in vec2 a_pos;

out vec2 v_pos;
#ifdef GLOBE
out float v_depth;
#endif

#pragma mapbox: define highp vec4 outline_color
#pragma mapbox: define lowp float opacity
Expand All @@ -15,4 +18,7 @@ void main() {
gl_Position = projectTile(a_pos + u_fill_translate);

v_pos = (gl_Position.xy / gl_Position.w + 1.0) / 2.0 * u_world;
#ifdef GLOBE
v_depth = gl_Position.z / gl_Position.w;
#endif
}
11 changes: 10 additions & 1 deletion src/shaders/fill_outline_pattern.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ uniform float u_fade;
in vec2 v_pos_a;
in vec2 v_pos_b;
in vec2 v_pos;
#ifdef GLOBE
in float v_depth;
#endif

#pragma mapbox: define lowp float opacity
#pragma mapbox: define lowp vec4 pattern_from
Expand Down Expand Up @@ -34,9 +37,15 @@ void main() {
float dist = length(v_pos - gl_FragCoord.xy);
float alpha = 1.0 - smoothstep(0.0, 1.0, dist);


fragColor = mix(color1, color2, u_fade) * alpha * opacity;

#ifdef GLOBE
if (v_depth > 1.0) {
// See comment in fill_outline.fragment.glsl
discard;
}
#endif

#ifdef OVERDRAW_INSPECTOR
fragColor = vec4(1.0);
#endif
Expand Down
6 changes: 6 additions & 0 deletions src/shaders/fill_outline_pattern.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ in vec2 a_pos;
out vec2 v_pos_a;
out vec2 v_pos_b;
out vec2 v_pos;
#ifdef GLOBE
out float v_depth;
#endif

#pragma mapbox: define lowp float opacity
#pragma mapbox: define lowp vec4 pattern_from
Expand Down Expand Up @@ -41,4 +44,7 @@ void main() {
v_pos_b = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, toScale * display_size_b, tileRatio, a_pos);

v_pos = (gl_Position.xy / gl_Position.w + 1.0) / 2.0 * u_world;
#ifdef GLOBE
v_depth = gl_Position.z / gl_Position.w;
#endif
}

0 comments on commit 98167ba

Please sign in to comment.