Skip to content

Commit

Permalink
HiSilicon fix: software clipping for line layer
Browse files Browse the repository at this point in the history
  • Loading branch information
kubapelc committed May 20, 2024
1 parent 98167ba commit d521e95
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/render/draw_line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 14 additions & 0 deletions src/shaders/line.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/shaders/line.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/shaders/line_gradient.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/shaders/line_gradient.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/shaders/line_pattern.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/shaders/line_pattern.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/shaders/line_sdf.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/shaders/line_sdf.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d521e95

Please sign in to comment.