From d521e955106c9a6a5cfb9a28344d35c9dfb5819c Mon Sep 17 00:00:00 2001 From: Jakub Pelc Date: Mon, 20 May 2024 10:57:14 +0200 Subject: [PATCH] HiSilicon fix: software clipping for line layer --- src/render/draw_line.ts | 2 +- src/shaders/line.fragment.glsl | 14 ++++++++++++++ src/shaders/line.vertex.glsl | 6 ++++++ src/shaders/line_gradient.fragment.glsl | 10 ++++++++++ src/shaders/line_gradient.vertex.glsl | 6 ++++++ src/shaders/line_pattern.fragment.glsl | 10 ++++++++++ src/shaders/line_pattern.vertex.glsl | 6 ++++++ src/shaders/line_sdf.fragment.glsl | 10 ++++++++++ src/shaders/line_sdf.vertex.glsl | 6 ++++++ 9 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/render/draw_line.ts b/src/render/draw_line.ts index 175d46d4f1..bffc919e46 100644 --- a/src/render/draw_line.ts +++ b/src/render/draw_line.ts @@ -119,7 +119,7 @@ export function drawLine(painter: Painter, sourceCache: SourceCache, layer: Line } program.draw(context, gl.TRIANGLES, depthMode, - painter.stencilModeForClipping(coord), colorMode, CullFaceMode.backCCW, uniformValues, terrainData, projectionData, + painter.stencilModeForClipping(coord), colorMode, CullFaceMode.disabled, uniformValues, terrainData, projectionData, layer.id, bucket.layoutVertexBuffer, bucket.indexBuffer, bucket.segments, layer.paint, painter.transform.zoom, programConfiguration, bucket.layoutVertexBuffer2); diff --git a/src/shaders/line.fragment.glsl b/src/shaders/line.fragment.glsl index 4ea11c11a4..2bd82dc828 100644 --- a/src/shaders/line.fragment.glsl +++ b/src/shaders/line.fragment.glsl @@ -3,6 +3,9 @@ uniform lowp float u_device_pixel_ratio; in vec2 v_width2; in vec2 v_normal; in float v_gamma_scale; +#ifdef GLOBE +in float v_depth; +#endif #pragma mapbox: define highp vec4 color #pragma mapbox: define lowp float blur @@ -24,6 +27,17 @@ void main() { fragColor = color * (alpha * opacity); + #ifdef GLOBE + if (v_depth > 1.0) { + // Hides lines 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 the line layer's geometry (actually drawn as polygons) is complex and partly resolved in the shader, + // so we can't easily ensure that all triangles have the proper winding order in the vertex buffer creation step. + // Thus we render line geometry without face culling, and clip the lines manually here. + discard; + } + #endif + #ifdef OVERDRAW_INSPECTOR fragColor = vec4(1.0); #endif diff --git a/src/shaders/line.vertex.glsl b/src/shaders/line.vertex.glsl index 8842d61dc9..92a7b38779 100644 --- a/src/shaders/line.vertex.glsl +++ b/src/shaders/line.vertex.glsl @@ -18,6 +18,9 @@ out vec2 v_normal; out vec2 v_width2; out float v_gamma_scale; out highp float v_linesofar; +#ifdef GLOBE +out float v_depth; +#endif #pragma mapbox: define highp vec4 color #pragma mapbox: define lowp float blur @@ -77,6 +80,9 @@ void main() { vec4 projected_no_extrude = projectTile(pos + offset2 / u_ratio * adjustedThickness + u_translation); vec4 projected_with_extrude = projectTile(pos + offset2 / u_ratio * adjustedThickness + u_translation + dist / u_ratio * adjustedThickness); gl_Position = projected_with_extrude; + #ifdef GLOBE + v_depth = gl_Position.z / gl_Position.w; + #endif // calculate how much the perspective view squishes or stretches the extrude #ifdef TERRAIN3D diff --git a/src/shaders/line_gradient.fragment.glsl b/src/shaders/line_gradient.fragment.glsl index fb4221f48e..6c730726de 100644 --- a/src/shaders/line_gradient.fragment.glsl +++ b/src/shaders/line_gradient.fragment.glsl @@ -5,6 +5,9 @@ in vec2 v_width2; in vec2 v_normal; in float v_gamma_scale; in highp vec2 v_uv; +#ifdef GLOBE +in float v_depth; +#endif #pragma mapbox: define lowp float blur #pragma mapbox: define lowp float opacity @@ -28,6 +31,13 @@ void main() { fragColor = color * (alpha * opacity); + #ifdef GLOBE + if (v_depth > 1.0) { + // See comment in line.fragment.glsl + discard; + } + #endif + #ifdef OVERDRAW_INSPECTOR fragColor = vec4(1.0); #endif diff --git a/src/shaders/line_gradient.vertex.glsl b/src/shaders/line_gradient.vertex.glsl index 61c2530604..08a9ae92fc 100644 --- a/src/shaders/line_gradient.vertex.glsl +++ b/src/shaders/line_gradient.vertex.glsl @@ -21,6 +21,9 @@ out vec2 v_normal; out vec2 v_width2; out float v_gamma_scale; out highp vec2 v_uv; +#ifdef GLOBE +out float v_depth; +#endif #pragma mapbox: define lowp float blur #pragma mapbox: define lowp float opacity @@ -80,6 +83,9 @@ void main() { vec4 projected_no_extrude = projectTile(pos + offset2 / u_ratio * adjustedThickness + u_translation); vec4 projected_with_extrude = projectTile(pos + offset2 / u_ratio * adjustedThickness + u_translation + dist / u_ratio * adjustedThickness); gl_Position = projected_with_extrude; + #ifdef GLOBE + v_depth = gl_Position.z / gl_Position.w; + #endif // calculate how much the perspective view squishes or stretches the extrude #ifdef TERRAIN3D diff --git a/src/shaders/line_pattern.fragment.glsl b/src/shaders/line_pattern.fragment.glsl index 2ab4bd7023..a2288aa433 100644 --- a/src/shaders/line_pattern.fragment.glsl +++ b/src/shaders/line_pattern.fragment.glsl @@ -13,6 +13,9 @@ in vec2 v_width2; in float v_linesofar; in float v_gamma_scale; in float v_width; +#ifdef GLOBE +in float v_depth; +#endif #pragma mapbox: define lowp vec4 pattern_from #pragma mapbox: define lowp vec4 pattern_to @@ -71,6 +74,13 @@ void main() { fragColor = color * alpha * opacity; + #ifdef GLOBE + if (v_depth > 1.0) { + // See comment in line.fragment.glsl + discard; + } + #endif + #ifdef OVERDRAW_INSPECTOR fragColor = vec4(1.0); #endif diff --git a/src/shaders/line_pattern.vertex.glsl b/src/shaders/line_pattern.vertex.glsl index 8acab715b9..4ecae36366 100644 --- a/src/shaders/line_pattern.vertex.glsl +++ b/src/shaders/line_pattern.vertex.glsl @@ -23,6 +23,9 @@ out vec2 v_width2; out float v_linesofar; out float v_gamma_scale; out float v_width; +#ifdef GLOBE +out float v_depth; +#endif #pragma mapbox: define lowp float blur #pragma mapbox: define lowp float opacity @@ -89,6 +92,9 @@ void main() { vec4 projected_no_extrude = projectTile(pos + offset2 / u_ratio * adjustedThickness + u_translation); vec4 projected_with_extrude = projectTile(pos + offset2 / u_ratio * adjustedThickness + u_translation + dist / u_ratio * adjustedThickness); gl_Position = projected_with_extrude; + #ifdef GLOBE + v_depth = gl_Position.z / gl_Position.w; + #endif // calculate how much the perspective view squishes or stretches the extrude #ifdef TERRAIN3D diff --git a/src/shaders/line_sdf.fragment.glsl b/src/shaders/line_sdf.fragment.glsl index 3cbd2c9b66..aa0ad05c01 100644 --- a/src/shaders/line_sdf.fragment.glsl +++ b/src/shaders/line_sdf.fragment.glsl @@ -9,6 +9,9 @@ in vec2 v_width2; in vec2 v_tex_a; in vec2 v_tex_b; in float v_gamma_scale; +#ifdef GLOBE +in float v_depth; +#endif #pragma mapbox: define highp vec4 color #pragma mapbox: define lowp float blur @@ -39,6 +42,13 @@ void main() { fragColor = color * (alpha * opacity); + #ifdef GLOBE + if (v_depth > 1.0) { + // See comment in line.fragment.glsl + discard; + } + #endif + #ifdef OVERDRAW_INSPECTOR fragColor = vec4(1.0); #endif diff --git a/src/shaders/line_sdf.vertex.glsl b/src/shaders/line_sdf.vertex.glsl index 2c7edcc163..8c6c345b66 100644 --- a/src/shaders/line_sdf.vertex.glsl +++ b/src/shaders/line_sdf.vertex.glsl @@ -27,6 +27,9 @@ out vec2 v_width2; out vec2 v_tex_a; out vec2 v_tex_b; out float v_gamma_scale; +#ifdef GLOBE +out float v_depth; +#endif #pragma mapbox: define highp vec4 color #pragma mapbox: define lowp float blur @@ -87,6 +90,9 @@ void main() { vec4 projected_no_extrude = projectTile(pos + offset2 / u_ratio * adjustedThickness + u_translation); vec4 projected_with_extrude = projectTile(pos + offset2 / u_ratio * adjustedThickness + u_translation + dist / u_ratio * adjustedThickness); gl_Position = projected_with_extrude; + #ifdef GLOBE + v_depth = gl_Position.z / gl_Position.w; + #endif // calculate how much the perspective view squishes or stretches the extrude #ifdef TERRAIN3D