summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Loer <chris.loer@gmail.com>2017-05-16 11:50:24 -0700
committerChris Loer <chris.loer@gmail.com>2017-06-06 09:00:08 -0700
commit0e50db94e0ef09fc5104a54cfa0a14d6197b3468 (patch)
tree6ee5ffe92f7d0066667bb9c3d4d52be85282dc47
parent65177fb8fc76b8df7fbcf821ee9eb822a342135b (diff)
downloadqtlocation-mapboxgl-0e50db94e0ef09fc5104a54cfa0a14d6197b3468.tar.gz
[core] Re-generate shaders.
-rw-r--r--src/mbgl/shaders/collision_box.cpp36
-rw-r--r--src/mbgl/shaders/symbol_icon.cpp44
-rw-r--r--src/mbgl/shaders/symbol_sdf.cpp115
3 files changed, 137 insertions, 58 deletions
diff --git a/src/mbgl/shaders/collision_box.cpp b/src/mbgl/shaders/collision_box.cpp
index 5f733c6a1e..05f306ef65 100644
--- a/src/mbgl/shaders/collision_box.cpp
+++ b/src/mbgl/shaders/collision_box.cpp
@@ -8,44 +8,74 @@ namespace shaders {
const char* collision_box::name = "collision_box";
const char* collision_box::vertexSource = R"MBGL_SHADER(
attribute vec2 a_pos;
+attribute vec2 a_anchor_pos;
attribute vec2 a_extrude;
attribute vec2 a_data;
uniform mat4 u_matrix;
uniform float u_scale;
+uniform float u_pitch;
+uniform float u_collision_y_stretch;
+uniform float u_camera_to_center_distance;
varying float v_max_zoom;
varying float v_placement_zoom;
+varying float v_perspective_zoom_adjust;
+varying vec2 v_fade_tex;
void main() {
- gl_Position = u_matrix * vec4(a_pos + a_extrude / u_scale, 0.0, 1.0);
+ vec4 projectedPoint = u_matrix * vec4(a_anchor_pos, 0, 1);
+ highp float camera_to_anchor_distance = projectedPoint.w;
+ highp float collision_perspective_ratio = 1.0 + 0.5 * ((camera_to_anchor_distance / u_camera_to_center_distance) - 1.0);
+
+ highp float incidence_stretch = camera_to_anchor_distance / (u_camera_to_center_distance * cos(u_pitch));
+ highp float collision_adjustment = max(1.0, incidence_stretch / u_collision_y_stretch);
+
+ gl_Position = u_matrix * vec4(a_pos + a_extrude * collision_perspective_ratio * collision_adjustment / u_scale, 0.0, 1.0);
v_max_zoom = a_data.x;
v_placement_zoom = a_data.y;
+
+ v_perspective_zoom_adjust = log2(collision_perspective_ratio * collision_adjustment) * 10.0;
+ v_fade_tex = vec2((v_placement_zoom + v_perspective_zoom_adjust) / 255.0, 0.0);
}
)MBGL_SHADER";
const char* collision_box::fragmentSource = R"MBGL_SHADER(
uniform float u_zoom;
+// u_maxzoom is derived from the maximum scale considered by the CollisionTile
+// Labels with placement zoom greater than this value will not be placed,
+// regardless of perspective effects.
uniform float u_maxzoom;
+uniform sampler2D u_fadetexture;
+// v_max_zoom is a collision-box-specific value that controls when line-following
+// collision boxes are used.
varying float v_max_zoom;
varying float v_placement_zoom;
+varying float v_perspective_zoom_adjust;
+varying vec2 v_fade_tex;
void main() {
float alpha = 0.5;
+ // Green = no collisions, label is showing
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0) * alpha;
- if (v_placement_zoom > u_zoom) {
+ // Red = collision, label hidden
+ if (texture2D(u_fadetexture, v_fade_tex).a < 1.0) {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0) * alpha;
}
- if (u_zoom >= v_max_zoom) {
+ // Faded black = this collision box is not used at this zoom (for curved labels)
+ if (u_zoom >= v_max_zoom + v_perspective_zoom_adjust) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0) * alpha * 0.25;
}
+ // Faded blue = the placement scale for this label is beyond the CollisionTile
+ // max scale, so it's impossible for this label to show without collision detection
+ // being run again (the label's glyphs haven't even been added to the symbol bucket)
if (v_placement_zoom >= u_maxzoom) {
gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0) * alpha * 0.2;
}
diff --git a/src/mbgl/shaders/symbol_icon.cpp b/src/mbgl/shaders/symbol_icon.cpp
index b6fbed428e..031b9ce942 100644
--- a/src/mbgl/shaders/symbol_icon.cpp
+++ b/src/mbgl/shaders/symbol_icon.cpp
@@ -7,17 +7,20 @@ namespace shaders {
const char* symbol_icon::name = "symbol_icon";
const char* symbol_icon::vertexSource = R"MBGL_SHADER(
-
attribute vec4 a_pos_offset;
+attribute vec2 a_label_pos;
attribute vec4 a_data;
// icon-size data (see symbol_sdf.vertex.glsl for more)
attribute vec3 a_size;
uniform bool u_is_size_zoom_constant;
uniform bool u_is_size_feature_constant;
-uniform mediump float u_size_t; // used to interpolate between zoom stops when size is a composite function
-uniform mediump float u_size; // used when size is both zoom and feature constant
-uniform mediump float u_layout_size; // used when size is feature constant
+uniform highp float u_size_t; // used to interpolate between zoom stops when size is a composite function
+uniform highp float u_size; // used when size is both zoom and feature constant
+uniform highp float u_layout_size; // used when size is feature constant
+uniform highp float u_camera_to_center_distance;
+uniform highp float u_pitch;
+uniform highp float u_collision_y_stretch;
uniform lowp float a_opacity_t;
attribute lowp vec2 a_opacity;
@@ -27,7 +30,7 @@ varying lowp float opacity;
uniform mat4 u_matrix;
uniform bool u_is_text;
-uniform mediump float u_zoom;
+uniform highp float u_zoom;
uniform bool u_rotate_with_map;
uniform vec2 u_extrude_scale;
@@ -43,11 +46,11 @@ void main() {
vec2 a_offset = a_pos_offset.zw;
vec2 a_tex = a_data.xy;
- mediump vec2 label_data = unpack_float(a_data[2]);
- mediump float a_labelminzoom = label_data[0];
- mediump vec2 a_zoom = unpack_float(a_data[3]);
- mediump float a_minzoom = a_zoom[0];
- mediump float a_maxzoom = a_zoom[1];
+ highp vec2 label_data = unpack_float(a_data[2]);
+ highp float a_labelminzoom = label_data[0];
+ highp vec2 a_zoom = unpack_float(a_data[3]);
+ highp float a_minzoom = a_zoom[0];
+ highp float a_maxzoom = a_zoom[1];
float size;
// In order to accommodate placing labels around corners in
@@ -58,7 +61,7 @@ void main() {
// currently rendered zoom level if text-size is zoom-dependent.
// Thus, we compensate for this difference by calculating an adjustment
// based on the scale of rendered text size relative to layout text size.
- mediump float layoutSize;
+ highp float layoutSize;
if (!u_is_size_zoom_constant && !u_is_size_feature_constant) {
size = mix(a_size[0], a_size[1], u_size_t) / 10.0;
layoutSize = a_size[2] / 10.0;
@@ -75,12 +78,16 @@ void main() {
float fontScale = u_is_text ? size / 24.0 : size;
- mediump float zoomAdjust = log2(size / layoutSize);
- mediump float adjustedZoom = (u_zoom - zoomAdjust) * 10.0;
+ highp float zoomAdjust = log2(size / layoutSize);
+ highp float adjustedZoom = (u_zoom - zoomAdjust) * 10.0;
// result: z = 0 if a_minzoom <= adjustedZoom < a_maxzoom, and 1 otherwise
- mediump float z = 2.0 - step(a_minzoom, adjustedZoom) - (1.0 - step(a_maxzoom, adjustedZoom));
+ highp float z = 2.0 - step(a_minzoom, adjustedZoom) - (1.0 - step(a_maxzoom, adjustedZoom));
+
+ vec4 projectedPoint = u_matrix * vec4(a_label_pos, 0, 1);
+ highp float camera_to_anchor_distance = projectedPoint.w;
+ highp float perspective_ratio = 1.0 + 0.5*((camera_to_anchor_distance / u_camera_to_center_distance) - 1.0);
- vec2 extrude = fontScale * u_extrude_scale * (a_offset / 64.0);
+ vec2 extrude = fontScale * u_extrude_scale * perspective_ratio * (a_offset / 64.0);
if (u_rotate_with_map) {
gl_Position = u_matrix * vec4(a_pos + extrude, 0, 1);
gl_Position.z += z * gl_Position.w;
@@ -89,7 +96,12 @@ void main() {
}
v_tex = a_tex / u_texsize;
- v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0);
+ // See comments in symbol_sdf.vertex
+ highp float incidence_stretch = camera_to_anchor_distance / (u_camera_to_center_distance * cos(u_pitch));
+ highp float collision_adjustment = max(1.0, incidence_stretch / u_collision_y_stretch);
+
+ highp float perspective_zoom_adjust = log2(perspective_ratio * collision_adjustment) * 10.0;
+ v_fade_tex = vec2((a_labelminzoom + perspective_zoom_adjust) / 255.0, 0.0);
}
)MBGL_SHADER";
diff --git a/src/mbgl/shaders/symbol_sdf.cpp b/src/mbgl/shaders/symbol_sdf.cpp
index 194e624036..55c0404afb 100644
--- a/src/mbgl/shaders/symbol_sdf.cpp
+++ b/src/mbgl/shaders/symbol_sdf.cpp
@@ -9,7 +9,10 @@ const char* symbol_sdf::name = "symbol_sdf";
const char* symbol_sdf::vertexSource = R"MBGL_SHADER(
const float PI = 3.141592653589793;
+// NOTE: the a_data attribute in this shader is manually bound (see https://github.com/mapbox/mapbox-gl-js/issues/4607).
+// If removing or renaming a_data, revisit the manual binding in painter.js accordingly.
attribute vec4 a_pos_offset;
+attribute vec2 a_label_pos;
attribute vec4 a_data;
// contents of a_size vary based on the type of property value
@@ -23,9 +26,9 @@ attribute vec4 a_data;
attribute vec3 a_size;
uniform bool u_is_size_zoom_constant;
uniform bool u_is_size_feature_constant;
-uniform mediump float u_size_t; // used to interpolate between zoom stops when size is a composite function
-uniform mediump float u_size; // used when size is both zoom and feature constant
-uniform mediump float u_layout_size; // used when size is feature constant
+uniform highp float u_size_t; // used to interpolate between zoom stops when size is a composite function
+uniform highp float u_size; // used when size is both zoom and feature constant
+uniform highp float u_layout_size; // used when size is feature constant
uniform lowp float a_fill_color_t;
attribute highp vec4 a_fill_color;
@@ -47,12 +50,15 @@ varying lowp float halo_blur;
uniform mat4 u_matrix;
uniform bool u_is_text;
-uniform mediump float u_zoom;
+uniform highp float u_zoom;
uniform bool u_rotate_with_map;
uniform bool u_pitch_with_map;
-uniform mediump float u_pitch;
-uniform mediump float u_bearing;
-uniform mediump float u_aspect_ratio;
+uniform highp float u_pitch;
+uniform highp float u_bearing;
+uniform highp float u_aspect_ratio;
+uniform highp float u_camera_to_center_distance;
+uniform highp float u_max_camera_distance;
+uniform highp float u_collision_y_stretch;
uniform vec2 u_extrude_scale;
uniform vec2 u_texsize;
@@ -62,6 +68,18 @@ varying vec2 v_fade_tex;
varying float v_gamma_scale;
varying float v_size;
+// Used below to move the vertex out of the clip space for when the current
+// zoom is out of the glyph's zoom range.
+highp float clipUnusedGlyphAngles(const highp float render_size,
+ const highp float layout_size,
+ const highp float min_zoom,
+ const highp float max_zoom) {
+ highp float zoom_adjust = log2(render_size / layout_size);
+ highp float adjusted_zoom = (u_zoom - zoom_adjust) * 10.0;
+ // result: 0 if min_zoom <= adjusted_zoom < max_zoom, and 1 otherwise
+ return 2.0 - step(min_zoom, adjusted_zoom) - (1.0 - step(max_zoom, adjusted_zoom));
+}
+
void main() {
fill_color = unpack_mix_vec4(a_fill_color, a_fill_color_t);
halo_color = unpack_mix_vec4(a_halo_color, a_halo_color_t);
@@ -74,13 +92,12 @@ void main() {
vec2 a_tex = a_data.xy;
- mediump vec2 label_data = unpack_float(a_data[2]);
- mediump float a_labelminzoom = label_data[0];
- mediump float a_labelangle = label_data[1];
-
- mediump vec2 a_zoom = unpack_float(a_data[3]);
- mediump float a_minzoom = a_zoom[0];
- mediump float a_maxzoom = a_zoom[1];
+ highp vec2 label_data = unpack_float(a_data[2]);
+ highp float a_labelminzoom = label_data[0];
+ highp float a_lineangle = (label_data[1] / 256.0 * 2.0 * PI);
+ highp vec2 a_zoom = unpack_float(a_data[3]);
+ highp float a_minzoom = a_zoom[0];
+ highp float a_maxzoom = a_zoom[1];
// In order to accommodate placing labels around corners in
// symbol-placement: line, each glyph in a label could have multiple
@@ -90,7 +107,7 @@ void main() {
// currently rendered zoom level if text-size is zoom-dependent.
// Thus, we compensate for this difference by calculating an adjustment
// based on the scale of rendered text size relative to layout text size.
- mediump float layoutSize;
+ highp float layoutSize;
if (!u_is_size_zoom_constant && !u_is_size_feature_constant) {
v_size = mix(a_size[0], a_size[1], u_size_t) / 10.0;
layoutSize = a_size[2] / 10.0;
@@ -107,59 +124,79 @@ void main() {
float fontScale = u_is_text ? v_size / 24.0 : v_size;
- mediump float zoomAdjust = log2(v_size / layoutSize);
- mediump float adjustedZoom = (u_zoom - zoomAdjust) * 10.0;
- // result: z = 0 if a_minzoom <= adjustedZoom < a_maxzoom, and 1 otherwise
- // Used below to move the vertex out of the clip space for when the current
- // zoom is out of the glyph's zoom range.
- mediump float z = 2.0 - step(a_minzoom, adjustedZoom) - (1.0 - step(a_maxzoom, adjustedZoom));
+ vec4 projectedPoint = u_matrix * vec4(a_label_pos, 0, 1);
+ highp float camera_to_anchor_distance = projectedPoint.w;
+ highp float perspective_ratio = 1.0 + 0.5*((camera_to_anchor_distance / u_camera_to_center_distance) - 1.0);
// pitch-alignment: map
// rotation-alignment: map | viewport
if (u_pitch_with_map) {
- lowp float angle = u_rotate_with_map ? (a_labelangle / 256.0 * 2.0 * PI) : u_bearing;
- lowp float asin = sin(angle);
- lowp float acos = cos(angle);
+ highp float angle = u_rotate_with_map ? a_lineangle : u_bearing;
+ highp float asin = sin(angle);
+ highp float acos = cos(angle);
mat2 RotationMatrix = mat2(acos, asin, -1.0 * asin, acos);
vec2 offset = RotationMatrix * a_offset;
- vec2 extrude = fontScale * u_extrude_scale * (offset / 64.0);
+ vec2 extrude = fontScale * u_extrude_scale * perspective_ratio * (offset / 64.0);
+
gl_Position = u_matrix * vec4(a_pos + extrude, 0, 1);
- gl_Position.z += z * gl_Position.w;
+ gl_Position.z += clipUnusedGlyphAngles(v_size*perspective_ratio, layoutSize, a_minzoom, a_maxzoom) * gl_Position.w;
// pitch-alignment: viewport
// rotation-alignment: map
} else if (u_rotate_with_map) {
// foreshortening factor to apply on pitched maps
// as a label goes from horizontal <=> vertical in angle
// it goes from 0% foreshortening to up to around 70% foreshortening
- lowp float pitchfactor = 1.0 - cos(u_pitch * sin(u_pitch * 0.75));
-
- lowp float lineangle = a_labelangle / 256.0 * 2.0 * PI;
+ highp float pitchfactor = 1.0 - cos(u_pitch * sin(u_pitch * 0.75));
// use the lineangle to position points a,b along the line
// project the points and calculate the label angle in projected space
// this calculation allows labels to be rendered unskewed on pitched maps
vec4 a = u_matrix * vec4(a_pos, 0, 1);
- vec4 b = u_matrix * vec4(a_pos + vec2(cos(lineangle),sin(lineangle)), 0, 1);
- lowp float angle = atan((b[1]/b[3] - a[1]/a[3])/u_aspect_ratio, b[0]/b[3] - a[0]/a[3]);
- lowp float asin = sin(angle);
- lowp float acos = cos(angle);
+ vec4 b = u_matrix * vec4(a_pos + vec2(cos(a_lineangle), sin(a_lineangle)), 0, 1);
+ highp float angle = atan((b[1] / b[3] - a[1] / a[3]) / u_aspect_ratio, b[0] / b[3] - a[0] / a[3]);
+ highp float asin = sin(angle);
+ highp float acos = cos(angle);
mat2 RotationMatrix = mat2(acos, -1.0 * asin, asin, acos);
+ highp float foreshortening = (1.0 - pitchfactor) + (pitchfactor * cos(angle * 2.0));
+
+ vec2 offset = RotationMatrix * (vec2(foreshortening, 1.0) * a_offset);
+ vec2 extrude = fontScale * u_extrude_scale * perspective_ratio * (offset / 64.0);
- vec2 offset = RotationMatrix * (vec2((1.0-pitchfactor)+(pitchfactor*cos(angle*2.0)), 1.0) * a_offset);
- vec2 extrude = fontScale * u_extrude_scale * (offset / 64.0);
gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0);
- gl_Position.z += z * gl_Position.w;
+ gl_Position.z += clipUnusedGlyphAngles(v_size * perspective_ratio, layoutSize, a_minzoom, a_maxzoom) * gl_Position.w;
// pitch-alignment: viewport
// rotation-alignment: viewport
} else {
- vec2 extrude = fontScale * u_extrude_scale * (a_offset / 64.0);
+ vec2 extrude = fontScale * u_extrude_scale * perspective_ratio * (a_offset / 64.0);
gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0);
}
- v_gamma_scale = gl_Position.w;
+ gl_Position.z +=
+ step(u_max_camera_distance * u_camera_to_center_distance, camera_to_anchor_distance) * gl_Position.w;
+
+ v_gamma_scale = gl_Position.w / perspective_ratio;
v_tex = a_tex / u_texsize;
- v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0);
+ // incidence_stretch is the ratio of how much y space a label takes up on a tile while drawn perpendicular to the viewport vs
+ // how much space it would take up if it were drawn flat on the tile
+ // Using law of sines, camera_to_anchor/sin(ground_angle) = camera_to_center/sin(incidence_angle)
+ // sin(incidence_angle) = 1/incidence_stretch
+ // Incidence angle 90 -> head on, sin(incidence_angle) = 1, no incidence stretch
+ // Incidence angle 1 -> very oblique, sin(incidence_angle) =~ 0, lots of incidence stretch
+ // ground_angle = u_pitch + PI/2 -> sin(ground_angle) = cos(u_pitch)
+ // This 2D calculation is only exactly correct when gl_Position.x is in the center of the viewport,
+ // but it's a close enough approximation for our purposes
+ highp float incidence_stretch = camera_to_anchor_distance / (u_camera_to_center_distance * cos(u_pitch));
+ // incidence_stretch only applies to the y-axis, but without re-calculating the collision tile, we can't
+ // adjust the size of only one axis. So, we do a crude approximation at placement time to get the aspect ratio
+ // about right, and then do the rest of the adjustment here: there will be some extra padding on the x-axis,
+ // but hopefully not too much.
+ // Never make the adjustment less than 1.0: instead of allowing collisions on the x-axis, be conservative on
+ // the y-axis.
+ highp float collision_adjustment = max(1.0, incidence_stretch / u_collision_y_stretch);
+
+ highp float perspective_zoom_adjust = log2(perspective_ratio * collision_adjustment) * 10.0;
+ v_fade_tex = vec2((a_labelminzoom + perspective_zoom_adjust) / 255.0, 0.0);
}
)MBGL_SHADER";