diff --git a/src/shaders/fill_outline.fragment.glsl b/src/shaders/fill_outline.fragment.glsl index f13b94edac..a3e6d8eb54 100644 --- a/src/shaders/fill_outline.fragment.glsl +++ b/src/shaders/fill_outline.fragment.glsl @@ -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 @@ -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 diff --git a/src/shaders/fill_outline.vertex.glsl b/src/shaders/fill_outline.vertex.glsl index 2a269237fa..ea38c928ea 100644 --- a/src/shaders/fill_outline.vertex.glsl +++ b/src/shaders/fill_outline.vertex.glsl @@ -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 @@ -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 } diff --git a/src/shaders/fill_outline_pattern.fragment.glsl b/src/shaders/fill_outline_pattern.fragment.glsl index 07984c7174..4d0d5a28b9 100644 --- a/src/shaders/fill_outline_pattern.fragment.glsl +++ b/src/shaders/fill_outline_pattern.fragment.glsl @@ -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 @@ -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 diff --git a/src/shaders/fill_outline_pattern.vertex.glsl b/src/shaders/fill_outline_pattern.vertex.glsl index 955f033c7f..c1b49bc27c 100644 --- a/src/shaders/fill_outline_pattern.vertex.glsl +++ b/src/shaders/fill_outline_pattern.vertex.glsl @@ -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 @@ -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 }