diff options
author | Leith Bade <leith@mapbox.com> | 2015-01-23 11:11:04 +1100 |
---|---|---|
committer | Leith Bade <leith@mapbox.com> | 2015-01-23 11:11:04 +1100 |
commit | 42d1972732a2f8cc59cf3c3041944b7831b4c97b (patch) | |
tree | 0b28f7476f805f55b67a621552c73b15dc7a358e /src/mbgl/shader | |
parent | 5234cd9f79d7aa1ac7843693f1f28f9d358edf2c (diff) | |
parent | 0c2ffc251c519af76a0ed6dd1925621ebe393e7b (diff) | |
download | qtlocation-mapboxgl-42d1972732a2f8cc59cf3c3041944b7831b4c97b.tar.gz |
Merge branch 'master' of github.com:mapbox/mapbox-gl-native into android-mason
Conflicts:
src/mbgl/renderer/painter_prerender.cpp
src/mbgl/renderer/prerendered_texture.cpp
src/mbgl/renderer/prerendered_texture.hpp
Diffstat (limited to 'src/mbgl/shader')
-rw-r--r-- | src/mbgl/shader/line.fragment.glsl | 8 | ||||
-rw-r--r-- | src/mbgl/shader/line.vertex.glsl | 3 | ||||
-rw-r--r-- | src/mbgl/shader/line_shader.hpp | 1 | ||||
-rw-r--r-- | src/mbgl/shader/linesdf.fragment.glsl | 23 | ||||
-rw-r--r-- | src/mbgl/shader/linesdf.vertex.glsl | 49 | ||||
-rw-r--r-- | src/mbgl/shader/linesdf_shader.cpp | 30 | ||||
-rw-r--r-- | src/mbgl/shader/linesdf_shader.hpp | 34 |
7 files changed, 136 insertions, 12 deletions
diff --git a/src/mbgl/shader/line.fragment.glsl b/src/mbgl/shader/line.fragment.glsl index f4ac1458b3..717c46e10d 100644 --- a/src/mbgl/shader/line.fragment.glsl +++ b/src/mbgl/shader/line.fragment.glsl @@ -2,10 +2,7 @@ uniform vec2 u_linewidth; uniform vec4 u_color; uniform float u_blur; -uniform vec2 u_dasharray; - varying vec2 v_normal; -varying float v_linesofar; void main() { // Calculate the distance of the pixel from the line in pixels. @@ -16,10 +13,5 @@ void main() { // (v_linewidth.s) float alpha = clamp(min(dist - (u_linewidth.t - u_blur), u_linewidth.s - dist) / u_blur, 0.0, 1.0); - // Calculate the antialiasing fade factor based on distance to the dash. - // Only affects alpha when line is dashed - float pos = mod(v_linesofar, u_dasharray.x + u_dasharray.y); - alpha *= max(step(0.0, -u_dasharray.y), clamp(min(pos, u_dasharray.x - pos), 0.0, 1.0)); - gl_FragColor = u_color * alpha; } diff --git a/src/mbgl/shader/line.vertex.glsl b/src/mbgl/shader/line.vertex.glsl index bf0b537e83..1f5432991c 100644 --- a/src/mbgl/shader/line.vertex.glsl +++ b/src/mbgl/shader/line.vertex.glsl @@ -20,11 +20,9 @@ uniform vec2 u_linewidth; uniform vec4 u_color; varying vec2 v_normal; -varying float v_linesofar; void main() { vec2 a_extrude = a_data.xy; - float a_linesofar = a_data.z * 128.0 + a_data.w; // We store the texture normals in the most insignificant bit // transform y so that 0 => -1 and 1 => 1 @@ -43,5 +41,4 @@ void main() { // because we're extruding the line in pixel space, regardless of the current // tile's zoom level. gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0.0, 1.0) + u_exmatrix * dist; - v_linesofar = a_linesofar * u_ratio; } diff --git a/src/mbgl/shader/line_shader.hpp b/src/mbgl/shader/line_shader.hpp index c6adc0ff8d..85152d6e0c 100644 --- a/src/mbgl/shader/line_shader.hpp +++ b/src/mbgl/shader/line_shader.hpp @@ -16,7 +16,6 @@ public: UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this}; Uniform<std::array<float, 4>> u_color = {"u_color", *this}; Uniform<std::array<float, 2>> u_linewidth = {"u_linewidth", *this}; - Uniform<std::array<float, 2>> u_dasharray = {"u_dasharray", *this}; Uniform<float> u_ratio = {"u_ratio", *this}; Uniform<float> u_blur = {"u_blur", *this}; diff --git a/src/mbgl/shader/linesdf.fragment.glsl b/src/mbgl/shader/linesdf.fragment.glsl new file mode 100644 index 0000000000..90a5eac3e2 --- /dev/null +++ b/src/mbgl/shader/linesdf.fragment.glsl @@ -0,0 +1,23 @@ +uniform vec2 u_linewidth; +uniform vec4 u_color; +uniform float u_blur; +uniform sampler2D u_image; +uniform float u_sdfgamma; + +varying vec2 v_normal; +varying vec2 v_tex; + +void main() { + // Calculate the distance of the pixel from the line in pixels. + float dist = length(v_normal) * u_linewidth.s; + + // Calculate the antialiasing fade factor. This is either when fading in + // the line in case of an offset line (v_linewidth.t) or when fading out + // (v_linewidth.s) + float alpha = clamp(min(dist - (u_linewidth.t - u_blur), u_linewidth.s - dist) / u_blur, 0.0, 1.0); + + float sdfdist = texture2D(u_image, v_tex).a; + alpha *= smoothstep(0.5 - u_sdfgamma, 0.5 + u_sdfgamma, sdfdist); + + gl_FragColor = u_color * alpha; +} diff --git a/src/mbgl/shader/linesdf.vertex.glsl b/src/mbgl/shader/linesdf.vertex.glsl new file mode 100644 index 0000000000..0ab6804ab5 --- /dev/null +++ b/src/mbgl/shader/linesdf.vertex.glsl @@ -0,0 +1,49 @@ +// floor(127 / 2) == 63.0 +// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is +// stored in a byte (-128..127). we scale regular normals up to length 63, but +// there are also "special" normals that have a bigger length (of up to 126 in +// this case). +// #define scale 63.0 +#define scale 0.015873016 + +attribute vec2 a_pos; +attribute vec4 a_data; + +// matrix is for the vertex position, exmatrix is for rotating and projecting +// the extrusion vector. +uniform mat4 u_matrix; +uniform mat4 u_exmatrix; + +// shared +uniform float u_ratio; +uniform vec2 u_linewidth; +uniform vec2 u_patternscale; +uniform float u_tex_y; + +varying vec2 v_normal; +varying vec2 v_tex; + +void main() { + vec2 a_extrude = a_data.xy; + float a_linesofar = a_data.z * 128.0 + a_data.w; + + // We store the texture normals in the most insignificant bit + // transform y so that 0 => -1 and 1 => 1 + // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap + // y is 1 if the normal points up, and -1 if it points down + vec2 normal = mod(a_pos, 2.0); + normal.y = sign(normal.y - 0.5); + v_normal = normal; + + // Scale the extrusion vector down to a normal and then up by the line width + // of this vertex. + vec4 dist = vec4(u_linewidth.s * a_extrude * scale, 0.0, 0.0); + + // Remove the texture normal bit of the position before scaling it with the + // model/view matrix. Add the extrusion vector *after* the model/view matrix + // because we're extruding the line in pixel space, regardless of the current + // tile's zoom level. + gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0.0, 1.0) + u_exmatrix * dist; + + v_tex = vec2(a_linesofar * u_patternscale.x, normal.y * u_patternscale.y + u_tex_y); +} diff --git a/src/mbgl/shader/linesdf_shader.cpp b/src/mbgl/shader/linesdf_shader.cpp new file mode 100644 index 0000000000..9802afb532 --- /dev/null +++ b/src/mbgl/shader/linesdf_shader.cpp @@ -0,0 +1,30 @@ +#include <mbgl/shader/linesdf_shader.hpp> +#include <mbgl/shader/shaders.hpp> +#include <mbgl/platform/gl.hpp> + +#include <cstdio> + +using namespace mbgl; + +LineSDFShader::LineSDFShader() + : Shader( + "line", + shaders[LINESDF_SHADER].vertex, + shaders[LINESDF_SHADER].fragment + ) { + if (!valid) { + fprintf(stderr, "invalid line shader\n"); + return; + } + + a_pos = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_pos")); + a_data = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_data")); +} + +void LineSDFShader::bind(char *offset) { + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset + 0)); + + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 4, GL_BYTE, false, 8, offset + 4)); +} diff --git a/src/mbgl/shader/linesdf_shader.hpp b/src/mbgl/shader/linesdf_shader.hpp new file mode 100644 index 0000000000..918ed5cbab --- /dev/null +++ b/src/mbgl/shader/linesdf_shader.hpp @@ -0,0 +1,34 @@ +#ifndef MBGL_SHADER_SHADER_LINESDF +#define MBGL_SHADER_SHADER_LINESDF + +#include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> + +namespace mbgl { + +class LineSDFShader : public Shader { +public: + LineSDFShader(); + + void bind(char *offset); + + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this}; + Uniform<std::array<float, 4>> u_color = {"u_color", *this}; + Uniform<std::array<float, 2>> u_linewidth = {"u_linewidth", *this}; + Uniform<float> u_ratio = {"u_ratio", *this}; + Uniform<float> u_blur = {"u_blur", *this}; + Uniform<std::array<float, 2>> u_patternscale = { "u_patternscale", *this }; + Uniform<float> u_tex_y = {"u_tex_y", *this}; + Uniform<int32_t> u_image = {"u_image", *this}; + Uniform<float> u_sdfgamma = {"u_sdfgamma", *this}; + +private: + int32_t a_pos = -1; + int32_t a_data = -1; +}; + + +} + +#endif |