diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2017-02-06 12:24:39 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2017-02-07 11:27:36 -0600 |
commit | 74eb8b360904be0c1d1818d0bf94a03fb0e8a687 (patch) | |
tree | 5a3403b84b3d2db483c85c62599ff36803f3adbb | |
parent | 9f2f7ddd12a4aaab0faddbe60cfb7c1e526fa7ff (diff) | |
download | qtlocation-mapboxgl-74eb8b360904be0c1d1818d0bf94a03fb0e8a687.tar.gz |
[core] Use cpp files for shader source
28 files changed, 2265 insertions, 2039 deletions
diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index e2a54daf01..a481249d61 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -182,18 +182,31 @@ set(MBGL_CORE_FILES src/mbgl/renderer/symbol_bucket.hpp # shaders + src/mbgl/shaders/circle.cpp src/mbgl/shaders/circle.hpp + src/mbgl/shaders/collision_box.cpp src/mbgl/shaders/collision_box.hpp + src/mbgl/shaders/debug.cpp src/mbgl/shaders/debug.hpp + src/mbgl/shaders/fill.cpp src/mbgl/shaders/fill.hpp + src/mbgl/shaders/fill_outline.cpp src/mbgl/shaders/fill_outline.hpp + src/mbgl/shaders/fill_outline_pattern.cpp src/mbgl/shaders/fill_outline_pattern.hpp + src/mbgl/shaders/fill_pattern.cpp src/mbgl/shaders/fill_pattern.hpp + src/mbgl/shaders/line.cpp src/mbgl/shaders/line.hpp + src/mbgl/shaders/line_pattern.cpp src/mbgl/shaders/line_pattern.hpp + src/mbgl/shaders/line_sdf.cpp src/mbgl/shaders/line_sdf.hpp + src/mbgl/shaders/raster.cpp src/mbgl/shaders/raster.hpp + src/mbgl/shaders/symbol_icon.cpp src/mbgl/shaders/symbol_icon.hpp + src/mbgl/shaders/symbol_sdf.cpp src/mbgl/shaders/symbol_sdf.hpp # sprite diff --git a/scripts/generate-shaders.js b/scripts/generate-shaders.js index 7660a6e66e..eb15ea907b 100755 --- a/scripts/generate-shaders.js +++ b/scripts/generate-shaders.js @@ -1,10 +1,10 @@ #!/usr/bin/env node -var path = require('path'); -var fs = require('fs'); +const path = require('path'); +const fs = require('fs'); -var inputPath = 'mapbox-gl-js/shaders'; -var outputPath = 'src/mbgl/shaders'; +const inputPath = 'mapbox-gl-js/shaders'; +const outputPath = 'src/mbgl/shaders'; require('./style-code'); @@ -34,8 +34,8 @@ require('./style-code'); } function vertexSource() { - var prelude = fs.readFileSync(path.join(inputPath, '_prelude.vertex.glsl')); - var source = fs.readFileSync(path.join(inputPath, shaderName + '.vertex.glsl'), 'utf8'); + const prelude = fs.readFileSync(path.join(inputPath, '_prelude.vertex.glsl')); + const source = fs.readFileSync(path.join(inputPath, shaderName + '.vertex.glsl'), 'utf8'); return prelude + applyPragmas(source, { define: [ "uniform lowp float a_{name}_t;", @@ -50,8 +50,8 @@ require('./style-code'); } function fragmentSource() { - var prelude = fs.readFileSync(path.join(inputPath, '_prelude.fragment.glsl')); - var source = fs.readFileSync(path.join(inputPath, shaderName + '.fragment.glsl'), 'utf8'); + const prelude = fs.readFileSync(path.join(inputPath, '_prelude.fragment.glsl')); + const source = fs.readFileSync(path.join(inputPath, shaderName + '.fragment.glsl'), 'utf8'); return prelude + applyPragmas(source, { define: [ "varying {precision} {type} {name};" @@ -61,24 +61,42 @@ require('./style-code'); }); } - var content = "#pragma once\n" + - "\n" + - "// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n" + - "\n" + - "#include <mbgl/gl/gl.hpp>\n" + - "\n" + - "namespace mbgl {\n" + - "namespace shaders {\n" + - "\n" + - "class " + shaderName + " {\n" + - "public:\n" + - " static constexpr const char* name = \"" + shaderName + "\";\n" + - " static constexpr const char* vertexSource = R\"MBGL_SHADER(\n" + vertexSource() + ")MBGL_SHADER\";\n" + - " static constexpr const char* fragmentSource = R\"MBGL_SHADER(\n" + fragmentSource() + ")MBGL_SHADER\";\n" + - "};\n" + - "\n" + - "} // namespace shaders\n" + - "} // namespace mbgl\n"; - - writeIfModified(path.join(outputPath, shaderName + '.hpp'), content); + writeIfModified(path.join(outputPath, `${shaderName}.hpp`), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#pragma once + +#include <mbgl/gl/gl.hpp> + +namespace mbgl { +namespace shaders { + +class ${shaderName} { +public: + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; +}; + +} // namespace shaders +} // namespace mbgl +`); + + writeIfModified(path.join(outputPath, `${shaderName}.cpp`), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/${shaderName}.hpp> + +namespace mbgl { +namespace shaders { + +const char* ${shaderName}::name = "${shaderName}"; +const char* ${shaderName}::vertexSource = R"MBGL_SHADER( +${vertexSource()} +)MBGL_SHADER"; +const char* ${shaderName}::fragmentSource = R"MBGL_SHADER( +${fragmentSource()} +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl +`); }); diff --git a/src/mbgl/shaders/circle.cpp b/src/mbgl/shaders/circle.cpp new file mode 100644 index 0000000000..592a883fb3 --- /dev/null +++ b/src/mbgl/shaders/circle.cpp @@ -0,0 +1,191 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/circle.hpp> + +namespace mbgl { +namespace shaders { + +const char* circle::name = "circle"; +const char* circle::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +uniform mat4 u_matrix; +uniform bool u_scale_with_map; +uniform vec2 u_extrude_scale; + +attribute vec2 a_pos; + +uniform lowp float a_color_t; +attribute lowp vec4 a_color_min; +attribute lowp vec4 a_color_max; +varying lowp vec4 color; +uniform lowp float a_radius_t; +attribute mediump float a_radius_min; +attribute mediump float a_radius_max; +varying mediump float radius; +uniform lowp float a_blur_t; +attribute lowp float a_blur_min; +attribute lowp float a_blur_max; +varying lowp float blur; +uniform lowp float a_opacity_t; +attribute lowp float a_opacity_min; +attribute lowp float a_opacity_max; +varying lowp float opacity; +uniform lowp float a_stroke_color_t; +attribute lowp vec4 a_stroke_color_min; +attribute lowp vec4 a_stroke_color_max; +varying lowp vec4 stroke_color; +uniform lowp float a_stroke_width_t; +attribute mediump float a_stroke_width_min; +attribute mediump float a_stroke_width_max; +varying mediump float stroke_width; +uniform lowp float a_stroke_opacity_t; +attribute lowp float a_stroke_opacity_min; +attribute lowp float a_stroke_opacity_max; +varying lowp float stroke_opacity; + +varying vec2 v_extrude; +varying lowp float v_antialiasblur; + +void main(void) { + color = mix(a_color_min, a_color_max, a_color_t); + radius = mix(a_radius_min, a_radius_max, a_radius_t); + blur = mix(a_blur_min, a_blur_max, a_blur_t); + opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); + stroke_color = mix(a_stroke_color_min, a_stroke_color_max, a_stroke_color_t); + stroke_width = mix(a_stroke_width_min, a_stroke_width_max, a_stroke_width_t); + stroke_opacity = mix(a_stroke_opacity_min, a_stroke_opacity_max, a_stroke_opacity_t); + + // unencode the extrusion vector that we snuck into the a_pos vector + v_extrude = vec2(mod(a_pos, 2.0) * 2.0 - 1.0); + + vec2 extrude = v_extrude * (radius + stroke_width) * u_extrude_scale; + // multiply a_pos by 0.5, since we had it * 2 in order to sneak + // in extrusion data + gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0, 1); + + if (u_scale_with_map) { + gl_Position.xy += extrude; + } else { + gl_Position.xy += extrude * gl_Position.w; + } + + // This is a minimum blur distance that serves as a faux-antialiasing for + // the circle. since blur is a ratio of the circle's size and the intent is + // to keep the blur at roughly 1px, the two are inversely related. + v_antialiasblur = 1.0 / DEVICE_PIXEL_RATIO / (radius + stroke_width); +} + +)MBGL_SHADER"; +const char* circle::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +varying lowp vec4 color; +varying mediump float radius; +varying lowp float blur; +varying lowp float opacity; +varying lowp vec4 stroke_color; +varying mediump float stroke_width; +varying lowp float stroke_opacity; + +varying vec2 v_extrude; +varying lowp float v_antialiasblur; + +void main() { + + + + + + + + + float extrude_length = length(v_extrude); + float antialiased_blur = -max(blur, v_antialiasblur); + + float opacity_t = smoothstep(0.0, antialiased_blur, extrude_length - 1.0); + + float color_t = stroke_width < 0.01 ? 0.0 : smoothstep( + antialiased_blur, + 0.0, + extrude_length - radius / (radius + stroke_width) + ); + + gl_FragColor = opacity_t * mix(color * opacity, stroke_color * stroke_opacity, color_t); + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/circle.hpp b/src/mbgl/shaders/circle.hpp index cafee6b526..51c3d41db2 100644 --- a/src/mbgl/shaders/circle.hpp +++ b/src/mbgl/shaders/circle.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,185 +9,9 @@ namespace shaders { class circle { public: - static constexpr const char* name = "circle"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -uniform mat4 u_matrix; -uniform bool u_scale_with_map; -uniform vec2 u_extrude_scale; - -attribute vec2 a_pos; - -uniform lowp float a_color_t; -attribute lowp vec4 a_color_min; -attribute lowp vec4 a_color_max; -varying lowp vec4 color; -uniform lowp float a_radius_t; -attribute mediump float a_radius_min; -attribute mediump float a_radius_max; -varying mediump float radius; -uniform lowp float a_blur_t; -attribute lowp float a_blur_min; -attribute lowp float a_blur_max; -varying lowp float blur; -uniform lowp float a_opacity_t; -attribute lowp float a_opacity_min; -attribute lowp float a_opacity_max; -varying lowp float opacity; -uniform lowp float a_stroke_color_t; -attribute lowp vec4 a_stroke_color_min; -attribute lowp vec4 a_stroke_color_max; -varying lowp vec4 stroke_color; -uniform lowp float a_stroke_width_t; -attribute mediump float a_stroke_width_min; -attribute mediump float a_stroke_width_max; -varying mediump float stroke_width; -uniform lowp float a_stroke_opacity_t; -attribute lowp float a_stroke_opacity_min; -attribute lowp float a_stroke_opacity_max; -varying lowp float stroke_opacity; - -varying vec2 v_extrude; -varying lowp float v_antialiasblur; - -void main(void) { - color = mix(a_color_min, a_color_max, a_color_t); - radius = mix(a_radius_min, a_radius_max, a_radius_t); - blur = mix(a_blur_min, a_blur_max, a_blur_t); - opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); - stroke_color = mix(a_stroke_color_min, a_stroke_color_max, a_stroke_color_t); - stroke_width = mix(a_stroke_width_min, a_stroke_width_max, a_stroke_width_t); - stroke_opacity = mix(a_stroke_opacity_min, a_stroke_opacity_max, a_stroke_opacity_t); - - // unencode the extrusion vector that we snuck into the a_pos vector - v_extrude = vec2(mod(a_pos, 2.0) * 2.0 - 1.0); - - vec2 extrude = v_extrude * (radius + stroke_width) * u_extrude_scale; - // multiply a_pos by 0.5, since we had it * 2 in order to sneak - // in extrusion data - gl_Position = u_matrix * vec4(floor(a_pos * 0.5), 0, 1); - - if (u_scale_with_map) { - gl_Position.xy += extrude; - } else { - gl_Position.xy += extrude * gl_Position.w; - } - - // This is a minimum blur distance that serves as a faux-antialiasing for - // the circle. since blur is a ratio of the circle's size and the intent is - // to keep the blur at roughly 1px, the two are inversely related. - v_antialiasblur = 1.0 / DEVICE_PIXEL_RATIO / (radius + stroke_width); -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -varying lowp vec4 color; -varying mediump float radius; -varying lowp float blur; -varying lowp float opacity; -varying lowp vec4 stroke_color; -varying mediump float stroke_width; -varying lowp float stroke_opacity; - -varying vec2 v_extrude; -varying lowp float v_antialiasblur; - -void main() { - - - - - - - - - float extrude_length = length(v_extrude); - float antialiased_blur = -max(blur, v_antialiasblur); - - float opacity_t = smoothstep(0.0, antialiased_blur, extrude_length - 1.0); - - float color_t = stroke_width < 0.01 ? 0.0 : smoothstep( - antialiased_blur, - 0.0, - extrude_length - radius / (radius + stroke_width) - ); - - gl_FragColor = opacity_t * mix(color * opacity, stroke_color * stroke_opacity, color_t); - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/collision_box.cpp b/src/mbgl/shaders/collision_box.cpp new file mode 100644 index 0000000000..6f2b9f3824 --- /dev/null +++ b/src/mbgl/shaders/collision_box.cpp @@ -0,0 +1,128 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/collision_box.hpp> + +namespace mbgl { +namespace shaders { + +const char* collision_box::name = "collision_box"; +const char* collision_box::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +attribute vec2 a_pos; +attribute vec2 a_extrude; +attribute vec2 a_data; + +uniform mat4 u_matrix; +uniform float u_scale; + +varying float v_max_zoom; +varying float v_placement_zoom; + +void main() { + gl_Position = u_matrix * vec4(a_pos + a_extrude / u_scale, 0.0, 1.0); + + v_max_zoom = a_data.x; + v_placement_zoom = a_data.y; +} + +)MBGL_SHADER"; +const char* collision_box::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +uniform float u_zoom; +uniform float u_maxzoom; + +varying float v_max_zoom; +varying float v_placement_zoom; + +void main() { + + float alpha = 0.5; + + gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0) * alpha; + + if (v_placement_zoom > u_zoom) { + gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0) * alpha; + } + + if (u_zoom >= v_max_zoom) { + gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0) * alpha * 0.25; + } + + if (v_placement_zoom >= u_maxzoom) { + gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0) * alpha * 0.2; + } +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/collision_box.hpp b/src/mbgl/shaders/collision_box.hpp index 36bc4e729b..7a31c919da 100644 --- a/src/mbgl/shaders/collision_box.hpp +++ b/src/mbgl/shaders/collision_box.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,122 +9,9 @@ namespace shaders { class collision_box { public: - static constexpr const char* name = "collision_box"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -attribute vec2 a_pos; -attribute vec2 a_extrude; -attribute vec2 a_data; - -uniform mat4 u_matrix; -uniform float u_scale; - -varying float v_max_zoom; -varying float v_placement_zoom; - -void main() { - gl_Position = u_matrix * vec4(a_pos + a_extrude / u_scale, 0.0, 1.0); - - v_max_zoom = a_data.x; - v_placement_zoom = a_data.y; -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -uniform float u_zoom; -uniform float u_maxzoom; - -varying float v_max_zoom; -varying float v_placement_zoom; - -void main() { - - float alpha = 0.5; - - gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0) * alpha; - - if (v_placement_zoom > u_zoom) { - gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0) * alpha; - } - - if (u_zoom >= v_max_zoom) { - gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0) * alpha * 0.25; - } - - if (v_placement_zoom >= u_maxzoom) { - gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0) * alpha * 0.2; - } -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/debug.cpp b/src/mbgl/shaders/debug.cpp new file mode 100644 index 0000000000..2659b2ca79 --- /dev/null +++ b/src/mbgl/shaders/debug.cpp @@ -0,0 +1,100 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/debug.hpp> + +namespace mbgl { +namespace shaders { + +const char* debug::name = "debug"; +const char* debug::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +attribute vec2 a_pos; + +uniform mat4 u_matrix; + +void main() { + gl_Position = u_matrix * vec4(a_pos, step(32767.0, a_pos.x), 1); +} + +)MBGL_SHADER"; +const char* debug::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +uniform lowp vec4 u_color; + +void main() { + gl_FragColor = u_color; +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/debug.hpp b/src/mbgl/shaders/debug.hpp index b0c1cf0e84..0eb3281f78 100644 --- a/src/mbgl/shaders/debug.hpp +++ b/src/mbgl/shaders/debug.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,94 +9,9 @@ namespace shaders { class debug { public: - static constexpr const char* name = "debug"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -attribute vec2 a_pos; - -uniform mat4 u_matrix; - -void main() { - gl_Position = u_matrix * vec4(a_pos, step(32767.0, a_pos.x), 1); -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -uniform lowp vec4 u_color; - -void main() { - gl_FragColor = u_color; -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/fill.cpp b/src/mbgl/shaders/fill.cpp new file mode 100644 index 0000000000..066adee447 --- /dev/null +++ b/src/mbgl/shaders/fill.cpp @@ -0,0 +1,120 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/fill.hpp> + +namespace mbgl { +namespace shaders { + +const char* fill::name = "fill"; +const char* fill::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +attribute vec2 a_pos; + +uniform mat4 u_matrix; + +uniform lowp float a_color_t; +attribute lowp vec4 a_color_min; +attribute lowp vec4 a_color_max; +varying lowp vec4 color; +uniform lowp float a_opacity_t; +attribute lowp float a_opacity_min; +attribute lowp float a_opacity_max; +varying lowp float opacity; + +void main() { + color = mix(a_color_min, a_color_max, a_color_t); + opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); + + gl_Position = u_matrix * vec4(a_pos, 0, 1); +} + +)MBGL_SHADER"; +const char* fill::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +varying lowp vec4 color; +varying lowp float opacity; + +void main() { + + + + gl_FragColor = color * opacity; + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill.hpp b/src/mbgl/shaders/fill.hpp index 0d735ceb73..6fb3843c91 100644 --- a/src/mbgl/shaders/fill.hpp +++ b/src/mbgl/shaders/fill.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,114 +9,9 @@ namespace shaders { class fill { public: - static constexpr const char* name = "fill"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -attribute vec2 a_pos; - -uniform mat4 u_matrix; - -uniform lowp float a_color_t; -attribute lowp vec4 a_color_min; -attribute lowp vec4 a_color_max; -varying lowp vec4 color; -uniform lowp float a_opacity_t; -attribute lowp float a_opacity_min; -attribute lowp float a_opacity_max; -varying lowp float opacity; - -void main() { - color = mix(a_color_min, a_color_max, a_color_t); - opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); - - gl_Position = u_matrix * vec4(a_pos, 0, 1); -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -varying lowp vec4 color; -varying lowp float opacity; - -void main() { - - - - gl_FragColor = color * opacity; - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/fill_outline.cpp b/src/mbgl/shaders/fill_outline.cpp new file mode 100644 index 0000000000..0f0f3806a9 --- /dev/null +++ b/src/mbgl/shaders/fill_outline.cpp @@ -0,0 +1,128 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/fill_outline.hpp> + +namespace mbgl { +namespace shaders { + +const char* fill_outline::name = "fill_outline"; +const char* fill_outline::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +attribute vec2 a_pos; + +uniform mat4 u_matrix; +uniform vec2 u_world; + +varying vec2 v_pos; + +uniform lowp float a_outline_color_t; +attribute lowp vec4 a_outline_color_min; +attribute lowp vec4 a_outline_color_max; +varying lowp vec4 outline_color; +uniform lowp float a_opacity_t; +attribute lowp float a_opacity_min; +attribute lowp float a_opacity_max; +varying lowp float opacity; + +void main() { + outline_color = mix(a_outline_color_min, a_outline_color_max, a_outline_color_t); + opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); + + gl_Position = u_matrix * vec4(a_pos, 0, 1); + v_pos = (gl_Position.xy / gl_Position.w + 1.0) / 2.0 * u_world; +} + +)MBGL_SHADER"; +const char* fill_outline::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +varying lowp vec4 outline_color; +varying lowp float opacity; + +varying vec2 v_pos; + +void main() { + + + + float dist = length(v_pos - gl_FragCoord.xy); + float alpha = smoothstep(1.0, 0.0, dist); + gl_FragColor = outline_color * (alpha * opacity); + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill_outline.hpp b/src/mbgl/shaders/fill_outline.hpp index 92e60b0328..3f2be5ac70 100644 --- a/src/mbgl/shaders/fill_outline.hpp +++ b/src/mbgl/shaders/fill_outline.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,122 +9,9 @@ namespace shaders { class fill_outline { public: - static constexpr const char* name = "fill_outline"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -attribute vec2 a_pos; - -uniform mat4 u_matrix; -uniform vec2 u_world; - -varying vec2 v_pos; - -uniform lowp float a_outline_color_t; -attribute lowp vec4 a_outline_color_min; -attribute lowp vec4 a_outline_color_max; -varying lowp vec4 outline_color; -uniform lowp float a_opacity_t; -attribute lowp float a_opacity_min; -attribute lowp float a_opacity_max; -varying lowp float opacity; - -void main() { - outline_color = mix(a_outline_color_min, a_outline_color_max, a_outline_color_t); - opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); - - gl_Position = u_matrix * vec4(a_pos, 0, 1); - v_pos = (gl_Position.xy / gl_Position.w + 1.0) / 2.0 * u_world; -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -varying lowp vec4 outline_color; -varying lowp float opacity; - -varying vec2 v_pos; - -void main() { - - - - float dist = length(v_pos - gl_FragCoord.xy); - float alpha = smoothstep(1.0, 0.0, dist); - gl_FragColor = outline_color * (alpha * opacity); - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/fill_outline_pattern.cpp b/src/mbgl/shaders/fill_outline_pattern.cpp new file mode 100644 index 0000000000..3921a83e6b --- /dev/null +++ b/src/mbgl/shaders/fill_outline_pattern.cpp @@ -0,0 +1,156 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/fill_outline_pattern.hpp> + +namespace mbgl { +namespace shaders { + +const char* fill_outline_pattern::name = "fill_outline_pattern"; +const char* fill_outline_pattern::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +uniform mat4 u_matrix; +uniform vec2 u_world; +uniform vec2 u_pattern_size_a; +uniform vec2 u_pattern_size_b; +uniform vec2 u_pixel_coord_upper; +uniform vec2 u_pixel_coord_lower; +uniform float u_scale_a; +uniform float u_scale_b; +uniform float u_tile_units_to_pixels; + +attribute vec2 a_pos; + +varying vec2 v_pos_a; +varying vec2 v_pos_b; +varying vec2 v_pos; + +uniform lowp float a_opacity_t; +attribute lowp float a_opacity_min; +attribute lowp float a_opacity_max; +varying lowp float opacity; + +void main() { + opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); + + gl_Position = u_matrix * vec4(a_pos, 0, 1); + + v_pos_a = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, u_scale_a * u_pattern_size_a, u_tile_units_to_pixels, a_pos); + v_pos_b = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, u_scale_b * u_pattern_size_b, u_tile_units_to_pixels, a_pos); + + v_pos = (gl_Position.xy / gl_Position.w + 1.0) / 2.0 * u_world; +} + +)MBGL_SHADER"; +const char* fill_outline_pattern::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +uniform vec2 u_pattern_tl_a; +uniform vec2 u_pattern_br_a; +uniform vec2 u_pattern_tl_b; +uniform vec2 u_pattern_br_b; +uniform float u_mix; + +uniform sampler2D u_image; + +varying vec2 v_pos_a; +varying vec2 v_pos_b; +varying vec2 v_pos; + +varying lowp float opacity; + +void main() { + + + vec2 imagecoord = mod(v_pos_a, 1.0); + vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord); + vec4 color1 = texture2D(u_image, pos); + + vec2 imagecoord_b = mod(v_pos_b, 1.0); + vec2 pos2 = mix(u_pattern_tl_b, u_pattern_br_b, imagecoord_b); + vec4 color2 = texture2D(u_image, pos2); + + // find distance to outline for alpha interpolation + + float dist = length(v_pos - gl_FragCoord.xy); + float alpha = smoothstep(1.0, 0.0, dist); + + + gl_FragColor = mix(color1, color2, u_mix) * alpha * opacity; + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill_outline_pattern.hpp b/src/mbgl/shaders/fill_outline_pattern.hpp index 6c6cc9c962..0aa73bd994 100644 --- a/src/mbgl/shaders/fill_outline_pattern.hpp +++ b/src/mbgl/shaders/fill_outline_pattern.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,150 +9,9 @@ namespace shaders { class fill_outline_pattern { public: - static constexpr const char* name = "fill_outline_pattern"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -uniform mat4 u_matrix; -uniform vec2 u_world; -uniform vec2 u_pattern_size_a; -uniform vec2 u_pattern_size_b; -uniform vec2 u_pixel_coord_upper; -uniform vec2 u_pixel_coord_lower; -uniform float u_scale_a; -uniform float u_scale_b; -uniform float u_tile_units_to_pixels; - -attribute vec2 a_pos; - -varying vec2 v_pos_a; -varying vec2 v_pos_b; -varying vec2 v_pos; - -uniform lowp float a_opacity_t; -attribute lowp float a_opacity_min; -attribute lowp float a_opacity_max; -varying lowp float opacity; - -void main() { - opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); - - gl_Position = u_matrix * vec4(a_pos, 0, 1); - - v_pos_a = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, u_scale_a * u_pattern_size_a, u_tile_units_to_pixels, a_pos); - v_pos_b = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, u_scale_b * u_pattern_size_b, u_tile_units_to_pixels, a_pos); - - v_pos = (gl_Position.xy / gl_Position.w + 1.0) / 2.0 * u_world; -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -uniform vec2 u_pattern_tl_a; -uniform vec2 u_pattern_br_a; -uniform vec2 u_pattern_tl_b; -uniform vec2 u_pattern_br_b; -uniform float u_mix; - -uniform sampler2D u_image; - -varying vec2 v_pos_a; -varying vec2 v_pos_b; -varying vec2 v_pos; - -varying lowp float opacity; - -void main() { - - - vec2 imagecoord = mod(v_pos_a, 1.0); - vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord); - vec4 color1 = texture2D(u_image, pos); - - vec2 imagecoord_b = mod(v_pos_b, 1.0); - vec2 pos2 = mix(u_pattern_tl_b, u_pattern_br_b, imagecoord_b); - vec4 color2 = texture2D(u_image, pos2); - - // find distance to outline for alpha interpolation - - float dist = length(v_pos - gl_FragCoord.xy); - float alpha = smoothstep(1.0, 0.0, dist); - - - gl_FragColor = mix(color1, color2, u_mix) * alpha * opacity; - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/fill_pattern.cpp b/src/mbgl/shaders/fill_pattern.cpp new file mode 100644 index 0000000000..822a0f7b8f --- /dev/null +++ b/src/mbgl/shaders/fill_pattern.cpp @@ -0,0 +1,145 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/fill_pattern.hpp> + +namespace mbgl { +namespace shaders { + +const char* fill_pattern::name = "fill_pattern"; +const char* fill_pattern::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +uniform mat4 u_matrix; +uniform vec2 u_pattern_size_a; +uniform vec2 u_pattern_size_b; +uniform vec2 u_pixel_coord_upper; +uniform vec2 u_pixel_coord_lower; +uniform float u_scale_a; +uniform float u_scale_b; +uniform float u_tile_units_to_pixels; + +attribute vec2 a_pos; + +varying vec2 v_pos_a; +varying vec2 v_pos_b; + +uniform lowp float a_opacity_t; +attribute lowp float a_opacity_min; +attribute lowp float a_opacity_max; +varying lowp float opacity; + +void main() { + opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); + + gl_Position = u_matrix * vec4(a_pos, 0, 1); + + v_pos_a = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, u_scale_a * u_pattern_size_a, u_tile_units_to_pixels, a_pos); + v_pos_b = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, u_scale_b * u_pattern_size_b, u_tile_units_to_pixels, a_pos); +} + +)MBGL_SHADER"; +const char* fill_pattern::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +uniform vec2 u_pattern_tl_a; +uniform vec2 u_pattern_br_a; +uniform vec2 u_pattern_tl_b; +uniform vec2 u_pattern_br_b; +uniform float u_mix; + +uniform sampler2D u_image; + +varying vec2 v_pos_a; +varying vec2 v_pos_b; + +varying lowp float opacity; + +void main() { + + + vec2 imagecoord = mod(v_pos_a, 1.0); + vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord); + vec4 color1 = texture2D(u_image, pos); + + vec2 imagecoord_b = mod(v_pos_b, 1.0); + vec2 pos2 = mix(u_pattern_tl_b, u_pattern_br_b, imagecoord_b); + vec4 color2 = texture2D(u_image, pos2); + + gl_FragColor = mix(color1, color2, u_mix) * opacity; + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill_pattern.hpp b/src/mbgl/shaders/fill_pattern.hpp index eeb0a80936..ca29e0eb99 100644 --- a/src/mbgl/shaders/fill_pattern.hpp +++ b/src/mbgl/shaders/fill_pattern.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,139 +9,9 @@ namespace shaders { class fill_pattern { public: - static constexpr const char* name = "fill_pattern"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -uniform mat4 u_matrix; -uniform vec2 u_pattern_size_a; -uniform vec2 u_pattern_size_b; -uniform vec2 u_pixel_coord_upper; -uniform vec2 u_pixel_coord_lower; -uniform float u_scale_a; -uniform float u_scale_b; -uniform float u_tile_units_to_pixels; - -attribute vec2 a_pos; - -varying vec2 v_pos_a; -varying vec2 v_pos_b; - -uniform lowp float a_opacity_t; -attribute lowp float a_opacity_min; -attribute lowp float a_opacity_max; -varying lowp float opacity; - -void main() { - opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); - - gl_Position = u_matrix * vec4(a_pos, 0, 1); - - v_pos_a = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, u_scale_a * u_pattern_size_a, u_tile_units_to_pixels, a_pos); - v_pos_b = get_pattern_pos(u_pixel_coord_upper, u_pixel_coord_lower, u_scale_b * u_pattern_size_b, u_tile_units_to_pixels, a_pos); -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -uniform vec2 u_pattern_tl_a; -uniform vec2 u_pattern_br_a; -uniform vec2 u_pattern_tl_b; -uniform vec2 u_pattern_br_b; -uniform float u_mix; - -uniform sampler2D u_image; - -varying vec2 v_pos_a; -varying vec2 v_pos_b; - -varying lowp float opacity; - -void main() { - - - vec2 imagecoord = mod(v_pos_a, 1.0); - vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord); - vec4 color1 = texture2D(u_image, pos); - - vec2 imagecoord_b = mod(v_pos_b, 1.0); - vec2 pos2 = mix(u_pattern_tl_b, u_pattern_br_b, imagecoord_b); - vec4 color2 = texture2D(u_image, pos2); - - gl_FragColor = mix(color1, color2, u_mix) * opacity; - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/line.cpp b/src/mbgl/shaders/line.cpp new file mode 100644 index 0000000000..59fa9f0cf2 --- /dev/null +++ b/src/mbgl/shaders/line.cpp @@ -0,0 +1,216 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/line.hpp> + +namespace mbgl { +namespace shaders { + +const char* line::name = "line"; +const char* line::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} + + +// the distance over which the line edge fades out. +// Retina devices need a smaller distance to avoid aliasing. +#define ANTIALIASING 1.0 / DEVICE_PIXEL_RATIO / 2.0 + +// 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; + +uniform mat4 u_matrix; +uniform mediump float u_ratio; +uniform mediump float u_width; +uniform vec2 u_gl_units_to_pixels; + +varying vec2 v_normal; +varying vec2 v_width2; +varying float v_gamma_scale; + +uniform lowp float a_color_t; +attribute lowp vec4 a_color_min; +attribute lowp vec4 a_color_max; +varying lowp vec4 color; +uniform lowp float a_blur_t; +attribute lowp float a_blur_min; +attribute lowp float a_blur_max; +varying lowp float blur; +uniform lowp float a_opacity_t; +attribute lowp float a_opacity_min; +attribute lowp float a_opacity_max; +varying lowp float opacity; +uniform lowp float a_gapwidth_t; +attribute mediump float a_gapwidth_min; +attribute mediump float a_gapwidth_max; +varying mediump float gapwidth; +uniform lowp float a_offset_t; +attribute lowp float a_offset_min; +attribute lowp float a_offset_max; +varying lowp float offset; + +void main() { + color = mix(a_color_min, a_color_max, a_color_t); + blur = mix(a_blur_min, a_blur_max, a_blur_t); + opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); + gapwidth = mix(a_gapwidth_min, a_gapwidth_max, a_gapwidth_t); + offset = mix(a_offset_min, a_offset_max, a_offset_t); + + vec2 a_extrude = a_data.xy - 128.0; + float a_direction = mod(a_data.z, 4.0) - 1.0; + + // 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 + mediump vec2 normal = mod(a_pos, 2.0); + normal.y = sign(normal.y - 0.5); + v_normal = normal; + + + // these transformations used to be applied in the JS and native code bases. + // moved them into the shader for clarity and simplicity. + gapwidth = gapwidth / 2.0; + float width = u_width / 2.0; + offset = -1.0 * offset; + + float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0); + float outset = gapwidth + width * (gapwidth > 0.0 ? 2.0 : 1.0) + ANTIALIASING; + + // Scale the extrusion vector down to a normal and then up by the line width + // of this vertex. + mediump vec2 dist = outset * a_extrude * scale; + + // Calculate the offset when drawing a line that is to the side of the actual line. + // We do this by creating a vector that points towards the extrude, but rotate + // it when we're drawing round end points (a_direction = -1 or 1) since their + // extrude vector points in another direction. + mediump float u = 0.5 * a_direction; + mediump float t = 1.0 - abs(u); + mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t); + + // Remove the texture normal bit to get the position + vec2 pos = floor(a_pos * 0.5); + + vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0); + gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude; + + // calculate how much the perspective view squishes or stretches the extrude + float extrude_length_without_perspective = length(dist); + float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_gl_units_to_pixels); + v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective; + + v_width2 = vec2(outset, inset); +} + +)MBGL_SHADER"; +const char* line::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +varying lowp vec4 color; +varying lowp float blur; +varying lowp float opacity; + +varying vec2 v_width2; +varying vec2 v_normal; +varying float v_gamma_scale; + +void main() { + + + + + // Calculate the distance of the pixel from the line in pixels. + float dist = length(v_normal) * v_width2.s; + + // Calculate the antialiasing fade factor. This is either when fading in + // the line in case of an offset line (v_width2.t) or when fading out + // (v_width2.s) + float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale; + float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0); + + gl_FragColor = color * (alpha * opacity); + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/line.hpp b/src/mbgl/shaders/line.hpp index 1ecef0a447..7477514761 100644 --- a/src/mbgl/shaders/line.hpp +++ b/src/mbgl/shaders/line.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,210 +9,9 @@ namespace shaders { class line { public: - static constexpr const char* name = "line"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} - - -// the distance over which the line edge fades out. -// Retina devices need a smaller distance to avoid aliasing. -#define ANTIALIASING 1.0 / DEVICE_PIXEL_RATIO / 2.0 - -// 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; - -uniform mat4 u_matrix; -uniform mediump float u_ratio; -uniform mediump float u_width; -uniform vec2 u_gl_units_to_pixels; - -varying vec2 v_normal; -varying vec2 v_width2; -varying float v_gamma_scale; - -uniform lowp float a_color_t; -attribute lowp vec4 a_color_min; -attribute lowp vec4 a_color_max; -varying lowp vec4 color; -uniform lowp float a_blur_t; -attribute lowp float a_blur_min; -attribute lowp float a_blur_max; -varying lowp float blur; -uniform lowp float a_opacity_t; -attribute lowp float a_opacity_min; -attribute lowp float a_opacity_max; -varying lowp float opacity; -uniform lowp float a_gapwidth_t; -attribute mediump float a_gapwidth_min; -attribute mediump float a_gapwidth_max; -varying mediump float gapwidth; -uniform lowp float a_offset_t; -attribute lowp float a_offset_min; -attribute lowp float a_offset_max; -varying lowp float offset; - -void main() { - color = mix(a_color_min, a_color_max, a_color_t); - blur = mix(a_blur_min, a_blur_max, a_blur_t); - opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); - gapwidth = mix(a_gapwidth_min, a_gapwidth_max, a_gapwidth_t); - offset = mix(a_offset_min, a_offset_max, a_offset_t); - - vec2 a_extrude = a_data.xy - 128.0; - float a_direction = mod(a_data.z, 4.0) - 1.0; - - // 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 - mediump vec2 normal = mod(a_pos, 2.0); - normal.y = sign(normal.y - 0.5); - v_normal = normal; - - - // these transformations used to be applied in the JS and native code bases. - // moved them into the shader for clarity and simplicity. - gapwidth = gapwidth / 2.0; - float width = u_width / 2.0; - offset = -1.0 * offset; - - float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0); - float outset = gapwidth + width * (gapwidth > 0.0 ? 2.0 : 1.0) + ANTIALIASING; - - // Scale the extrusion vector down to a normal and then up by the line width - // of this vertex. - mediump vec2 dist = outset * a_extrude * scale; - - // Calculate the offset when drawing a line that is to the side of the actual line. - // We do this by creating a vector that points towards the extrude, but rotate - // it when we're drawing round end points (a_direction = -1 or 1) since their - // extrude vector points in another direction. - mediump float u = 0.5 * a_direction; - mediump float t = 1.0 - abs(u); - mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t); - - // Remove the texture normal bit to get the position - vec2 pos = floor(a_pos * 0.5); - - vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0); - gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude; - - // calculate how much the perspective view squishes or stretches the extrude - float extrude_length_without_perspective = length(dist); - float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_gl_units_to_pixels); - v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective; - - v_width2 = vec2(outset, inset); -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -varying lowp vec4 color; -varying lowp float blur; -varying lowp float opacity; - -varying vec2 v_width2; -varying vec2 v_normal; -varying float v_gamma_scale; - -void main() { - - - - - // Calculate the distance of the pixel from the line in pixels. - float dist = length(v_normal) * v_width2.s; - - // Calculate the antialiasing fade factor. This is either when fading in - // the line in case of an offset line (v_width2.t) or when fading out - // (v_width2.s) - float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale; - float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0); - - gl_FragColor = color * (alpha * opacity); - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/line_pattern.cpp b/src/mbgl/shaders/line_pattern.cpp new file mode 100644 index 0000000000..7f2a31ee44 --- /dev/null +++ b/src/mbgl/shaders/line_pattern.cpp @@ -0,0 +1,233 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/line_pattern.hpp> + +namespace mbgl { +namespace shaders { + +const char* line_pattern::name = "line_pattern"; +const char* line_pattern::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +// 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 + +// We scale the distance before adding it to the buffers so that we can store +// long distances for long segments. Use this value to unscale the distance. +#define LINE_DISTANCE_SCALE 2.0 + +// the distance over which the line edge fades out. +// Retina devices need a smaller distance to avoid aliasing. +#define ANTIALIASING 1.0 / DEVICE_PIXEL_RATIO / 2.0 + +attribute vec2 a_pos; +attribute vec4 a_data; + +uniform mat4 u_matrix; +uniform mediump float u_ratio; +uniform mediump float u_width; +uniform vec2 u_gl_units_to_pixels; + +varying vec2 v_normal; +varying vec2 v_width2; +varying float v_linesofar; +varying float v_gamma_scale; + +uniform lowp float a_blur_t; +attribute lowp float a_blur_min; +attribute lowp float a_blur_max; +varying lowp float blur; +uniform lowp float a_opacity_t; +attribute lowp float a_opacity_min; +attribute lowp float a_opacity_max; +varying lowp float opacity; +uniform lowp float a_offset_t; +attribute lowp float a_offset_min; +attribute lowp float a_offset_max; +varying lowp float offset; +uniform lowp float a_gapwidth_t; +attribute mediump float a_gapwidth_min; +attribute mediump float a_gapwidth_max; +varying mediump float gapwidth; + +void main() { + blur = mix(a_blur_min, a_blur_max, a_blur_t); + opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); + offset = mix(a_offset_min, a_offset_max, a_offset_t); + gapwidth = mix(a_gapwidth_min, a_gapwidth_max, a_gapwidth_t); + + vec2 a_extrude = a_data.xy - 128.0; + float a_direction = mod(a_data.z, 4.0) - 1.0; + float a_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * LINE_DISTANCE_SCALE; + + // 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 + mediump vec2 normal = mod(a_pos, 2.0); + normal.y = sign(normal.y - 0.5); + v_normal = normal; + + // these transformations used to be applied in the JS and native code bases. + // moved them into the shader for clarity and simplicity. + gapwidth = gapwidth / 2.0; + float width = u_width / 2.0; + offset = -1.0 * offset; + + float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0); + float outset = gapwidth + width * (gapwidth > 0.0 ? 2.0 : 1.0) + ANTIALIASING; + + // Scale the extrusion vector down to a normal and then up by the line width + // of this vertex. + mediump vec2 dist = outset * a_extrude * scale; + + // Calculate the offset when drawing a line that is to the side of the actual line. + // We do this by creating a vector that points towards the extrude, but rotate + // it when we're drawing round end points (a_direction = -1 or 1) since their + // extrude vector points in another direction. + mediump float u = 0.5 * a_direction; + mediump float t = 1.0 - abs(u); + mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t); + + // Remove the texture normal bit to get the position + vec2 pos = floor(a_pos * 0.5); + + vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0); + gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude; + + // calculate how much the perspective view squishes or stretches the extrude + float extrude_length_without_perspective = length(dist); + float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_gl_units_to_pixels); + v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective; + + v_linesofar = a_linesofar; + v_width2 = vec2(outset, inset); +} + +)MBGL_SHADER"; +const char* line_pattern::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +uniform vec2 u_pattern_size_a; +uniform vec2 u_pattern_size_b; +uniform vec2 u_pattern_tl_a; +uniform vec2 u_pattern_br_a; +uniform vec2 u_pattern_tl_b; +uniform vec2 u_pattern_br_b; +uniform float u_fade; + +uniform sampler2D u_image; + +varying vec2 v_normal; +varying vec2 v_width2; +varying float v_linesofar; +varying float v_gamma_scale; + +varying lowp float blur; +varying lowp float opacity; + +void main() { + + + + // Calculate the distance of the pixel from the line in pixels. + float dist = length(v_normal) * v_width2.s; + + // Calculate the antialiasing fade factor. This is either when fading in + // the line in case of an offset line (v_width2.t) or when fading out + // (v_width2.s) + float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale; + float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0); + + float x_a = mod(v_linesofar / u_pattern_size_a.x, 1.0); + float x_b = mod(v_linesofar / u_pattern_size_b.x, 1.0); + float y_a = 0.5 + (v_normal.y * v_width2.s / u_pattern_size_a.y); + float y_b = 0.5 + (v_normal.y * v_width2.s / u_pattern_size_b.y); + vec2 pos_a = mix(u_pattern_tl_a, u_pattern_br_a, vec2(x_a, y_a)); + vec2 pos_b = mix(u_pattern_tl_b, u_pattern_br_b, vec2(x_b, y_b)); + + vec4 color = mix(texture2D(u_image, pos_a), texture2D(u_image, pos_b), u_fade); + + gl_FragColor = color * alpha * opacity; + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/line_pattern.hpp b/src/mbgl/shaders/line_pattern.hpp index 901d83702d..97d0261204 100644 --- a/src/mbgl/shaders/line_pattern.hpp +++ b/src/mbgl/shaders/line_pattern.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,227 +9,9 @@ namespace shaders { class line_pattern { public: - static constexpr const char* name = "line_pattern"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -// 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 - -// We scale the distance before adding it to the buffers so that we can store -// long distances for long segments. Use this value to unscale the distance. -#define LINE_DISTANCE_SCALE 2.0 - -// the distance over which the line edge fades out. -// Retina devices need a smaller distance to avoid aliasing. -#define ANTIALIASING 1.0 / DEVICE_PIXEL_RATIO / 2.0 - -attribute vec2 a_pos; -attribute vec4 a_data; - -uniform mat4 u_matrix; -uniform mediump float u_ratio; -uniform mediump float u_width; -uniform vec2 u_gl_units_to_pixels; - -varying vec2 v_normal; -varying vec2 v_width2; -varying float v_linesofar; -varying float v_gamma_scale; - -uniform lowp float a_blur_t; -attribute lowp float a_blur_min; -attribute lowp float a_blur_max; -varying lowp float blur; -uniform lowp float a_opacity_t; -attribute lowp float a_opacity_min; -attribute lowp float a_opacity_max; -varying lowp float opacity; -uniform lowp float a_offset_t; -attribute lowp float a_offset_min; -attribute lowp float a_offset_max; -varying lowp float offset; -uniform lowp float a_gapwidth_t; -attribute mediump float a_gapwidth_min; -attribute mediump float a_gapwidth_max; -varying mediump float gapwidth; - -void main() { - blur = mix(a_blur_min, a_blur_max, a_blur_t); - opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); - offset = mix(a_offset_min, a_offset_max, a_offset_t); - gapwidth = mix(a_gapwidth_min, a_gapwidth_max, a_gapwidth_t); - - vec2 a_extrude = a_data.xy - 128.0; - float a_direction = mod(a_data.z, 4.0) - 1.0; - float a_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * LINE_DISTANCE_SCALE; - - // 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 - mediump vec2 normal = mod(a_pos, 2.0); - normal.y = sign(normal.y - 0.5); - v_normal = normal; - - // these transformations used to be applied in the JS and native code bases. - // moved them into the shader for clarity and simplicity. - gapwidth = gapwidth / 2.0; - float width = u_width / 2.0; - offset = -1.0 * offset; - - float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0); - float outset = gapwidth + width * (gapwidth > 0.0 ? 2.0 : 1.0) + ANTIALIASING; - - // Scale the extrusion vector down to a normal and then up by the line width - // of this vertex. - mediump vec2 dist = outset * a_extrude * scale; - - // Calculate the offset when drawing a line that is to the side of the actual line. - // We do this by creating a vector that points towards the extrude, but rotate - // it when we're drawing round end points (a_direction = -1 or 1) since their - // extrude vector points in another direction. - mediump float u = 0.5 * a_direction; - mediump float t = 1.0 - abs(u); - mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t); - - // Remove the texture normal bit to get the position - vec2 pos = floor(a_pos * 0.5); - - vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0); - gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude; - - // calculate how much the perspective view squishes or stretches the extrude - float extrude_length_without_perspective = length(dist); - float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_gl_units_to_pixels); - v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective; - - v_linesofar = a_linesofar; - v_width2 = vec2(outset, inset); -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -uniform vec2 u_pattern_size_a; -uniform vec2 u_pattern_size_b; -uniform vec2 u_pattern_tl_a; -uniform vec2 u_pattern_br_a; -uniform vec2 u_pattern_tl_b; -uniform vec2 u_pattern_br_b; -uniform float u_fade; - -uniform sampler2D u_image; - -varying vec2 v_normal; -varying vec2 v_width2; -varying float v_linesofar; -varying float v_gamma_scale; - -varying lowp float blur; -varying lowp float opacity; - -void main() { - - - - // Calculate the distance of the pixel from the line in pixels. - float dist = length(v_normal) * v_width2.s; - - // Calculate the antialiasing fade factor. This is either when fading in - // the line in case of an offset line (v_width2.t) or when fading out - // (v_width2.s) - float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale; - float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0); - - float x_a = mod(v_linesofar / u_pattern_size_a.x, 1.0); - float x_b = mod(v_linesofar / u_pattern_size_b.x, 1.0); - float y_a = 0.5 + (v_normal.y * v_width2.s / u_pattern_size_a.y); - float y_b = 0.5 + (v_normal.y * v_width2.s / u_pattern_size_b.y); - vec2 pos_a = mix(u_pattern_tl_a, u_pattern_br_a, vec2(x_a, y_a)); - vec2 pos_b = mix(u_pattern_tl_b, u_pattern_br_b, vec2(x_b, y_b)); - - vec4 color = mix(texture2D(u_image, pos_a), texture2D(u_image, pos_b), u_fade); - - gl_FragColor = color * alpha * opacity; - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/line_sdf.cpp b/src/mbgl/shaders/line_sdf.cpp new file mode 100644 index 0000000000..011c1c3738 --- /dev/null +++ b/src/mbgl/shaders/line_sdf.cpp @@ -0,0 +1,239 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/line_sdf.hpp> + +namespace mbgl { +namespace shaders { + +const char* line_sdf::name = "line_sdf"; +const char* line_sdf::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +// 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 + +// We scale the distance before adding it to the buffers so that we can store +// long distances for long segments. Use this value to unscale the distance. +#define LINE_DISTANCE_SCALE 2.0 + +// the distance over which the line edge fades out. +// Retina devices need a smaller distance to avoid aliasing. +#define ANTIALIASING 1.0 / DEVICE_PIXEL_RATIO / 2.0 + +attribute vec2 a_pos; +attribute vec4 a_data; + +uniform mat4 u_matrix; +uniform mediump float u_ratio; +uniform vec2 u_patternscale_a; +uniform float u_tex_y_a; +uniform vec2 u_patternscale_b; +uniform float u_tex_y_b; +uniform vec2 u_gl_units_to_pixels; +uniform mediump float u_width; + +varying vec2 v_normal; +varying vec2 v_width2; +varying vec2 v_tex_a; +varying vec2 v_tex_b; +varying float v_gamma_scale; + +uniform lowp float a_color_t; +attribute lowp vec4 a_color_min; +attribute lowp vec4 a_color_max; +varying lowp vec4 color; +uniform lowp float a_blur_t; +attribute lowp float a_blur_min; +attribute lowp float a_blur_max; +varying lowp float blur; +uniform lowp float a_opacity_t; +attribute lowp float a_opacity_min; +attribute lowp float a_opacity_max; +varying lowp float opacity; +uniform lowp float a_gapwidth_t; +attribute mediump float a_gapwidth_min; +attribute mediump float a_gapwidth_max; +varying mediump float gapwidth; +uniform lowp float a_offset_t; +attribute lowp float a_offset_min; +attribute lowp float a_offset_max; +varying lowp float offset; + +void main() { + color = mix(a_color_min, a_color_max, a_color_t); + blur = mix(a_blur_min, a_blur_max, a_blur_t); + opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); + gapwidth = mix(a_gapwidth_min, a_gapwidth_max, a_gapwidth_t); + offset = mix(a_offset_min, a_offset_max, a_offset_t); + + vec2 a_extrude = a_data.xy - 128.0; + float a_direction = mod(a_data.z, 4.0) - 1.0; + float a_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * LINE_DISTANCE_SCALE; + + // 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 + mediump vec2 normal = mod(a_pos, 2.0); + normal.y = sign(normal.y - 0.5); + v_normal = normal; + + // these transformations used to be applied in the JS and native code bases. + // moved them into the shader for clarity and simplicity. + gapwidth = gapwidth / 2.0; + float width = u_width / 2.0; + offset = -1.0 * offset; + + float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0); + float outset = gapwidth + width * (gapwidth > 0.0 ? 2.0 : 1.0) + ANTIALIASING; + + // Scale the extrusion vector down to a normal and then up by the line width + // of this vertex. + mediump vec2 dist =outset * a_extrude * scale; + + // Calculate the offset when drawing a line that is to the side of the actual line. + // We do this by creating a vector that points towards the extrude, but rotate + // it when we're drawing round end points (a_direction = -1 or 1) since their + // extrude vector points in another direction. + mediump float u = 0.5 * a_direction; + mediump float t = 1.0 - abs(u); + mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t); + + // Remove the texture normal bit to get the position + vec2 pos = floor(a_pos * 0.5); + + vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0); + gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude; + + // calculate how much the perspective view squishes or stretches the extrude + float extrude_length_without_perspective = length(dist); + float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_gl_units_to_pixels); + v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective; + + v_tex_a = vec2(a_linesofar * u_patternscale_a.x, normal.y * u_patternscale_a.y + u_tex_y_a); + v_tex_b = vec2(a_linesofar * u_patternscale_b.x, normal.y * u_patternscale_b.y + u_tex_y_b); + + v_width2 = vec2(outset, inset); +} + +)MBGL_SHADER"; +const char* line_sdf::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +uniform sampler2D u_image; +uniform float u_sdfgamma; +uniform float u_mix; + +varying vec2 v_normal; +varying vec2 v_width2; +varying vec2 v_tex_a; +varying vec2 v_tex_b; +varying float v_gamma_scale; + +varying lowp vec4 color; +varying lowp float blur; +varying lowp float opacity; + +void main() { + + + + + // Calculate the distance of the pixel from the line in pixels. + float dist = length(v_normal) * v_width2.s; + + // Calculate the antialiasing fade factor. This is either when fading in + // the line in case of an offset line (v_width2.t) or when fading out + // (v_width2.s) + float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale; + float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0); + + float sdfdist_a = texture2D(u_image, v_tex_a).a; + float sdfdist_b = texture2D(u_image, v_tex_b).a; + float sdfdist = mix(sdfdist_a, sdfdist_b, u_mix); + alpha *= smoothstep(0.5 - u_sdfgamma, 0.5 + u_sdfgamma, sdfdist); + + gl_FragColor = color * (alpha * opacity); + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/line_sdf.hpp b/src/mbgl/shaders/line_sdf.hpp index 473a6ab650..7a738b9337 100644 --- a/src/mbgl/shaders/line_sdf.hpp +++ b/src/mbgl/shaders/line_sdf.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,233 +9,9 @@ namespace shaders { class line_sdf { public: - static constexpr const char* name = "line_sdf"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -// 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 - -// We scale the distance before adding it to the buffers so that we can store -// long distances for long segments. Use this value to unscale the distance. -#define LINE_DISTANCE_SCALE 2.0 - -// the distance over which the line edge fades out. -// Retina devices need a smaller distance to avoid aliasing. -#define ANTIALIASING 1.0 / DEVICE_PIXEL_RATIO / 2.0 - -attribute vec2 a_pos; -attribute vec4 a_data; - -uniform mat4 u_matrix; -uniform mediump float u_ratio; -uniform vec2 u_patternscale_a; -uniform float u_tex_y_a; -uniform vec2 u_patternscale_b; -uniform float u_tex_y_b; -uniform vec2 u_gl_units_to_pixels; -uniform mediump float u_width; - -varying vec2 v_normal; -varying vec2 v_width2; -varying vec2 v_tex_a; -varying vec2 v_tex_b; -varying float v_gamma_scale; - -uniform lowp float a_color_t; -attribute lowp vec4 a_color_min; -attribute lowp vec4 a_color_max; -varying lowp vec4 color; -uniform lowp float a_blur_t; -attribute lowp float a_blur_min; -attribute lowp float a_blur_max; -varying lowp float blur; -uniform lowp float a_opacity_t; -attribute lowp float a_opacity_min; -attribute lowp float a_opacity_max; -varying lowp float opacity; -uniform lowp float a_gapwidth_t; -attribute mediump float a_gapwidth_min; -attribute mediump float a_gapwidth_max; -varying mediump float gapwidth; -uniform lowp float a_offset_t; -attribute lowp float a_offset_min; -attribute lowp float a_offset_max; -varying lowp float offset; - -void main() { - color = mix(a_color_min, a_color_max, a_color_t); - blur = mix(a_blur_min, a_blur_max, a_blur_t); - opacity = mix(a_opacity_min, a_opacity_max, a_opacity_t); - gapwidth = mix(a_gapwidth_min, a_gapwidth_max, a_gapwidth_t); - offset = mix(a_offset_min, a_offset_max, a_offset_t); - - vec2 a_extrude = a_data.xy - 128.0; - float a_direction = mod(a_data.z, 4.0) - 1.0; - float a_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * LINE_DISTANCE_SCALE; - - // 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 - mediump vec2 normal = mod(a_pos, 2.0); - normal.y = sign(normal.y - 0.5); - v_normal = normal; - - // these transformations used to be applied in the JS and native code bases. - // moved them into the shader for clarity and simplicity. - gapwidth = gapwidth / 2.0; - float width = u_width / 2.0; - offset = -1.0 * offset; - - float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0); - float outset = gapwidth + width * (gapwidth > 0.0 ? 2.0 : 1.0) + ANTIALIASING; - - // Scale the extrusion vector down to a normal and then up by the line width - // of this vertex. - mediump vec2 dist =outset * a_extrude * scale; - - // Calculate the offset when drawing a line that is to the side of the actual line. - // We do this by creating a vector that points towards the extrude, but rotate - // it when we're drawing round end points (a_direction = -1 or 1) since their - // extrude vector points in another direction. - mediump float u = 0.5 * a_direction; - mediump float t = 1.0 - abs(u); - mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t); - - // Remove the texture normal bit to get the position - vec2 pos = floor(a_pos * 0.5); - - vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0); - gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude; - - // calculate how much the perspective view squishes or stretches the extrude - float extrude_length_without_perspective = length(dist); - float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_gl_units_to_pixels); - v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective; - - v_tex_a = vec2(a_linesofar * u_patternscale_a.x, normal.y * u_patternscale_a.y + u_tex_y_a); - v_tex_b = vec2(a_linesofar * u_patternscale_b.x, normal.y * u_patternscale_b.y + u_tex_y_b); - - v_width2 = vec2(outset, inset); -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -uniform sampler2D u_image; -uniform float u_sdfgamma; -uniform float u_mix; - -varying vec2 v_normal; -varying vec2 v_width2; -varying vec2 v_tex_a; -varying vec2 v_tex_b; -varying float v_gamma_scale; - -varying lowp vec4 color; -varying lowp float blur; -varying lowp float opacity; - -void main() { - - - - - // Calculate the distance of the pixel from the line in pixels. - float dist = length(v_normal) * v_width2.s; - - // Calculate the antialiasing fade factor. This is either when fading in - // the line in case of an offset line (v_width2.t) or when fading out - // (v_width2.s) - float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale; - float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0); - - float sdfdist_a = texture2D(u_image, v_tex_a).a; - float sdfdist_b = texture2D(u_image, v_tex_b).a; - float sdfdist = mix(sdfdist_a, sdfdist_b, u_mix); - alpha *= smoothstep(0.5 - u_sdfgamma, 0.5 + u_sdfgamma, sdfdist); - - gl_FragColor = color * (alpha * opacity); - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/raster.cpp b/src/mbgl/shaders/raster.cpp new file mode 100644 index 0000000000..ba0aa5cd05 --- /dev/null +++ b/src/mbgl/shaders/raster.cpp @@ -0,0 +1,150 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/raster.hpp> + +namespace mbgl { +namespace shaders { + +const char* raster::name = "raster"; +const char* raster::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +uniform mat4 u_matrix; +uniform vec2 u_tl_parent; +uniform float u_scale_parent; +uniform float u_buffer_scale; + +attribute vec2 a_pos; +attribute vec2 a_texture_pos; + +varying vec2 v_pos0; +varying vec2 v_pos1; + +void main() { + gl_Position = u_matrix * vec4(a_pos, 0, 1); + v_pos0 = (((a_texture_pos / 32767.0) - 0.5) / u_buffer_scale ) + 0.5; + v_pos1 = (v_pos0 * u_scale_parent) + u_tl_parent; +} + +)MBGL_SHADER"; +const char* raster::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +uniform float u_fade_t; +uniform float u_opacity; +uniform sampler2D u_image0; +uniform sampler2D u_image1; +varying vec2 v_pos0; +varying vec2 v_pos1; + +uniform float u_brightness_low; +uniform float u_brightness_high; + +uniform float u_saturation_factor; +uniform float u_contrast_factor; +uniform vec3 u_spin_weights; + +void main() { + + // read and cross-fade colors from the main and parent tiles + vec4 color0 = texture2D(u_image0, v_pos0); + vec4 color1 = texture2D(u_image1, v_pos1); + vec4 color = mix(color0, color1, u_fade_t); + color.a *= u_opacity; + vec3 rgb = color.rgb; + + // spin + rgb = vec3( + dot(rgb, u_spin_weights.xyz), + dot(rgb, u_spin_weights.zxy), + dot(rgb, u_spin_weights.yzx)); + + // saturation + float average = (color.r + color.g + color.b) / 3.0; + rgb += (average - rgb) * u_saturation_factor; + + // contrast + rgb = (rgb - 0.5) * u_contrast_factor + 0.5; + + // brightness + vec3 u_high_vec = vec3(u_brightness_low, u_brightness_low, u_brightness_low); + vec3 u_low_vec = vec3(u_brightness_high, u_brightness_high, u_brightness_high); + + gl_FragColor = vec4(mix(u_high_vec, u_low_vec, rgb) * color.a, color.a); + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/raster.hpp b/src/mbgl/shaders/raster.hpp index 6f65a7a79d..34f117e14a 100644 --- a/src/mbgl/shaders/raster.hpp +++ b/src/mbgl/shaders/raster.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,144 +9,9 @@ namespace shaders { class raster { public: - static constexpr const char* name = "raster"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -uniform mat4 u_matrix; -uniform vec2 u_tl_parent; -uniform float u_scale_parent; -uniform float u_buffer_scale; - -attribute vec2 a_pos; -attribute vec2 a_texture_pos; - -varying vec2 v_pos0; -varying vec2 v_pos1; - -void main() { - gl_Position = u_matrix * vec4(a_pos, 0, 1); - v_pos0 = (((a_texture_pos / 32767.0) - 0.5) / u_buffer_scale ) + 0.5; - v_pos1 = (v_pos0 * u_scale_parent) + u_tl_parent; -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -uniform float u_fade_t; -uniform float u_opacity; -uniform sampler2D u_image0; -uniform sampler2D u_image1; -varying vec2 v_pos0; -varying vec2 v_pos1; - -uniform float u_brightness_low; -uniform float u_brightness_high; - -uniform float u_saturation_factor; -uniform float u_contrast_factor; -uniform vec3 u_spin_weights; - -void main() { - - // read and cross-fade colors from the main and parent tiles - vec4 color0 = texture2D(u_image0, v_pos0); - vec4 color1 = texture2D(u_image1, v_pos1); - vec4 color = mix(color0, color1, u_fade_t); - color.a *= u_opacity; - vec3 rgb = color.rgb; - - // spin - rgb = vec3( - dot(rgb, u_spin_weights.xyz), - dot(rgb, u_spin_weights.zxy), - dot(rgb, u_spin_weights.yzx)); - - // saturation - float average = (color.r + color.g + color.b) / 3.0; - rgb += (average - rgb) * u_saturation_factor; - - // contrast - rgb = (rgb - 0.5) * u_contrast_factor + 0.5; - - // brightness - vec3 u_high_vec = vec3(u_brightness_low, u_brightness_low, u_brightness_low); - vec3 u_low_vec = vec3(u_brightness_high, u_brightness_high, u_brightness_high); - - gl_FragColor = vec4(mix(u_high_vec, u_low_vec, rgb) * color.a, color.a); - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/symbol_icon.cpp b/src/mbgl/shaders/symbol_icon.cpp new file mode 100644 index 0000000000..eca9342d54 --- /dev/null +++ b/src/mbgl/shaders/symbol_icon.cpp @@ -0,0 +1,142 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/symbol_icon.hpp> + +namespace mbgl { +namespace shaders { + +const char* symbol_icon::name = "symbol_icon"; +const char* symbol_icon::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +attribute vec2 a_pos; +attribute vec2 a_offset; +attribute vec2 a_texture_pos; +attribute vec4 a_data; + + +// matrix is for the vertex position. +uniform mat4 u_matrix; + +uniform mediump float u_zoom; +uniform bool u_rotate_with_map; +uniform vec2 u_extrude_scale; + +uniform vec2 u_texsize; + +varying vec2 v_tex; +varying vec2 v_fade_tex; + +void main() { + vec2 a_tex = a_texture_pos.xy; + mediump float a_labelminzoom = a_data[0]; + mediump vec2 a_zoom = a_data.pq; + mediump float a_minzoom = a_zoom[0]; + mediump float a_maxzoom = a_zoom[1]; + + // u_zoom is the current zoom level adjusted for the change in font size + mediump float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom)); + + vec2 extrude = u_extrude_scale * (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; + } else { + gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0); + } + + v_tex = a_tex / u_texsize; + v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0); +} + +)MBGL_SHADER"; +const char* symbol_icon::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +uniform sampler2D u_texture; +uniform sampler2D u_fadetexture; +uniform lowp float u_opacity; + +varying vec2 v_tex; +varying vec2 v_fade_tex; + +void main() { + lowp float alpha = texture2D(u_fadetexture, v_fade_tex).a * u_opacity; + gl_FragColor = texture2D(u_texture, v_tex) * alpha; + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/symbol_icon.hpp b/src/mbgl/shaders/symbol_icon.hpp index 17f4b36009..445c40f913 100644 --- a/src/mbgl/shaders/symbol_icon.hpp +++ b/src/mbgl/shaders/symbol_icon.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,136 +9,9 @@ namespace shaders { class symbol_icon { public: - static constexpr const char* name = "symbol_icon"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -attribute vec2 a_pos; -attribute vec2 a_offset; -attribute vec2 a_texture_pos; -attribute vec4 a_data; - - -// matrix is for the vertex position. -uniform mat4 u_matrix; - -uniform mediump float u_zoom; -uniform bool u_rotate_with_map; -uniform vec2 u_extrude_scale; - -uniform vec2 u_texsize; - -varying vec2 v_tex; -varying vec2 v_fade_tex; - -void main() { - vec2 a_tex = a_texture_pos.xy; - mediump float a_labelminzoom = a_data[0]; - mediump vec2 a_zoom = a_data.pq; - mediump float a_minzoom = a_zoom[0]; - mediump float a_maxzoom = a_zoom[1]; - - // u_zoom is the current zoom level adjusted for the change in font size - mediump float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom)); - - vec2 extrude = u_extrude_scale * (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; - } else { - gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0); - } - - v_tex = a_tex / u_texsize; - v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0); -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -uniform sampler2D u_texture; -uniform sampler2D u_fadetexture; -uniform lowp float u_opacity; - -varying vec2 v_tex; -varying vec2 v_fade_tex; - -void main() { - lowp float alpha = texture2D(u_fadetexture, v_fade_tex).a * u_opacity; - gl_FragColor = texture2D(u_texture, v_tex) * alpha; - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders diff --git a/src/mbgl/shaders/symbol_sdf.cpp b/src/mbgl/shaders/symbol_sdf.cpp new file mode 100644 index 0000000000..5258d792c9 --- /dev/null +++ b/src/mbgl/shaders/symbol_sdf.cpp @@ -0,0 +1,193 @@ +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include <mbgl/shaders/symbol_sdf.hpp> + +namespace mbgl { +namespace shaders { + +const char* symbol_sdf::name = "symbol_sdf"; +const char* symbol_sdf::vertexSource = R"MBGL_SHADER( +#ifdef GL_ES +precision highp float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif + +float evaluate_zoom_function_1(const vec4 values, const float t) { + if (t < 1.0) { + return mix(values[0], values[1], t); + } else if (t < 2.0) { + return mix(values[1], values[2], t - 1.0); + } else { + return mix(values[2], values[3], t - 2.0); + } +} +vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { + if (t < 1.0) { + return mix(value0, value1, t); + } else if (t < 2.0) { + return mix(value1, value2, t - 1.0); + } else { + return mix(value2, value3, t - 2.0); + } +} + +// The offset depends on how many pixels are between the world origin and the edge of the tile: +// vec2 offset = mod(pixel_coord, size) +// +// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. +// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. +// +// The pixel_coord is passed in as two 16 bit values: +// pixel_coord_upper = floor(pixel_coord / 2^16) +// pixel_coord_lower = mod(pixel_coord, 2^16) +// +// The offset is calculated in a series of steps that should preserve this precision: +vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, + const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { + + vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); + return (tile_units_to_pixels * pos + offset) / pattern_size; +} +const float PI = 3.141592653589793; + +attribute vec2 a_pos; +attribute vec2 a_offset; +attribute vec2 a_texture_pos; +attribute vec4 a_data; + + +// matrix is for the vertex position. +uniform mat4 u_matrix; + +uniform mediump 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 vec2 u_extrude_scale; + +uniform vec2 u_texsize; + +varying vec2 v_tex; +varying vec2 v_fade_tex; +varying float v_gamma_scale; + +void main() { + vec2 a_tex = a_texture_pos.xy; + mediump float a_labelminzoom = a_data[0]; + mediump vec2 a_zoom = a_data.pq; + mediump float a_minzoom = a_zoom[0]; + mediump float a_maxzoom = a_zoom[1]; + + // u_zoom is the current zoom level adjusted for the change in font size + mediump float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom)); + + // pitch-alignment: map + // rotation-alignment: map | viewport + if (u_pitch_with_map) { + lowp float angle = u_rotate_with_map ? (a_data[1] / 256.0 * 2.0 * PI) : u_bearing; + lowp float asin = sin(angle); + lowp float acos = cos(angle); + mat2 RotationMatrix = mat2(acos, asin, -1.0 * asin, acos); + vec2 offset = RotationMatrix * a_offset; + vec2 extrude = u_extrude_scale * (offset / 64.0); + gl_Position = u_matrix * vec4(a_pos + extrude, 0, 1); + gl_Position.z += z * 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_data[1] / 256.0 * 2.0 * PI; + + // 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); + mat2 RotationMatrix = mat2(acos, -1.0 * asin, asin, acos); + + vec2 offset = RotationMatrix * (vec2((1.0-pitchfactor)+(pitchfactor*cos(angle*2.0)), 1.0) * a_offset); + vec2 extrude = 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; + // pitch-alignment: viewport + // rotation-alignment: viewport + } else { + vec2 extrude = u_extrude_scale * (a_offset / 64.0); + gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0); + } + + v_gamma_scale = gl_Position.w; + + v_tex = a_tex / u_texsize; + v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0); +} + +)MBGL_SHADER"; +const char* symbol_sdf::fragmentSource = R"MBGL_SHADER( +#ifdef GL_ES +precision mediump float; +#else + +#if !defined(lowp) +#define lowp +#endif + +#if !defined(mediump) +#define mediump +#endif + +#if !defined(highp) +#define highp +#endif + +#endif +uniform sampler2D u_texture; +uniform sampler2D u_fadetexture; +uniform lowp vec4 u_color; +uniform lowp float u_opacity; +uniform lowp float u_buffer; +uniform lowp float u_gamma; + +varying vec2 v_tex; +varying vec2 v_fade_tex; +varying float v_gamma_scale; + +void main() { + lowp float dist = texture2D(u_texture, v_tex).a; + lowp float fade_alpha = texture2D(u_fadetexture, v_fade_tex).a; + lowp float gamma = u_gamma * v_gamma_scale; + lowp float alpha = smoothstep(u_buffer - gamma, u_buffer + gamma, dist) * fade_alpha; + + gl_FragColor = u_color * (alpha * u_opacity); + +#ifdef OVERDRAW_INSPECTOR + gl_FragColor = vec4(1.0); +#endif +} + +)MBGL_SHADER"; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/symbol_sdf.hpp b/src/mbgl/shaders/symbol_sdf.hpp index 67bcd86354..432f9f9313 100644 --- a/src/mbgl/shaders/symbol_sdf.hpp +++ b/src/mbgl/shaders/symbol_sdf.hpp @@ -1,7 +1,7 @@ -#pragma once - // NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. +#pragma once + #include <mbgl/gl/gl.hpp> namespace mbgl { @@ -9,187 +9,9 @@ namespace shaders { class symbol_sdf { public: - static constexpr const char* name = "symbol_sdf"; - static constexpr const char* vertexSource = R"MBGL_SHADER( -#ifdef GL_ES -precision highp float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif - -float evaluate_zoom_function_1(const vec4 values, const float t) { - if (t < 1.0) { - return mix(values[0], values[1], t); - } else if (t < 2.0) { - return mix(values[1], values[2], t - 1.0); - } else { - return mix(values[2], values[3], t - 2.0); - } -} -vec4 evaluate_zoom_function_4(const vec4 value0, const vec4 value1, const vec4 value2, const vec4 value3, const float t) { - if (t < 1.0) { - return mix(value0, value1, t); - } else if (t < 2.0) { - return mix(value1, value2, t - 1.0); - } else { - return mix(value2, value3, t - 2.0); - } -} - -// The offset depends on how many pixels are between the world origin and the edge of the tile: -// vec2 offset = mod(pixel_coord, size) -// -// At high zoom levels there are a ton of pixels between the world origin and the edge of the tile. -// The glsl spec only guarantees 16 bits of precision for highp floats. We need more than that. -// -// The pixel_coord is passed in as two 16 bit values: -// pixel_coord_upper = floor(pixel_coord / 2^16) -// pixel_coord_lower = mod(pixel_coord, 2^16) -// -// The offset is calculated in a series of steps that should preserve this precision: -vec2 get_pattern_pos(const vec2 pixel_coord_upper, const vec2 pixel_coord_lower, - const vec2 pattern_size, const float tile_units_to_pixels, const vec2 pos) { - - vec2 offset = mod(mod(mod(pixel_coord_upper, pattern_size) * 256.0, pattern_size) * 256.0 + pixel_coord_lower, pattern_size); - return (tile_units_to_pixels * pos + offset) / pattern_size; -} -const float PI = 3.141592653589793; - -attribute vec2 a_pos; -attribute vec2 a_offset; -attribute vec2 a_texture_pos; -attribute vec4 a_data; - - -// matrix is for the vertex position. -uniform mat4 u_matrix; - -uniform mediump 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 vec2 u_extrude_scale; - -uniform vec2 u_texsize; - -varying vec2 v_tex; -varying vec2 v_fade_tex; -varying float v_gamma_scale; - -void main() { - vec2 a_tex = a_texture_pos.xy; - mediump float a_labelminzoom = a_data[0]; - mediump vec2 a_zoom = a_data.pq; - mediump float a_minzoom = a_zoom[0]; - mediump float a_maxzoom = a_zoom[1]; - - // u_zoom is the current zoom level adjusted for the change in font size - mediump float z = 2.0 - step(a_minzoom, u_zoom) - (1.0 - step(a_maxzoom, u_zoom)); - - // pitch-alignment: map - // rotation-alignment: map | viewport - if (u_pitch_with_map) { - lowp float angle = u_rotate_with_map ? (a_data[1] / 256.0 * 2.0 * PI) : u_bearing; - lowp float asin = sin(angle); - lowp float acos = cos(angle); - mat2 RotationMatrix = mat2(acos, asin, -1.0 * asin, acos); - vec2 offset = RotationMatrix * a_offset; - vec2 extrude = u_extrude_scale * (offset / 64.0); - gl_Position = u_matrix * vec4(a_pos + extrude, 0, 1); - gl_Position.z += z * 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_data[1] / 256.0 * 2.0 * PI; - - // 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); - mat2 RotationMatrix = mat2(acos, -1.0 * asin, asin, acos); - - vec2 offset = RotationMatrix * (vec2((1.0-pitchfactor)+(pitchfactor*cos(angle*2.0)), 1.0) * a_offset); - vec2 extrude = 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; - // pitch-alignment: viewport - // rotation-alignment: viewport - } else { - vec2 extrude = u_extrude_scale * (a_offset / 64.0); - gl_Position = u_matrix * vec4(a_pos, 0, 1) + vec4(extrude, 0, 0); - } - - v_gamma_scale = gl_Position.w; - - v_tex = a_tex / u_texsize; - v_fade_tex = vec2(a_labelminzoom / 255.0, 0.0); -} -)MBGL_SHADER"; - static constexpr const char* fragmentSource = R"MBGL_SHADER( -#ifdef GL_ES -precision mediump float; -#else - -#if !defined(lowp) -#define lowp -#endif - -#if !defined(mediump) -#define mediump -#endif - -#if !defined(highp) -#define highp -#endif - -#endif -uniform sampler2D u_texture; -uniform sampler2D u_fadetexture; -uniform lowp vec4 u_color; -uniform lowp float u_opacity; -uniform lowp float u_buffer; -uniform lowp float u_gamma; - -varying vec2 v_tex; -varying vec2 v_fade_tex; -varying float v_gamma_scale; - -void main() { - lowp float dist = texture2D(u_texture, v_tex).a; - lowp float fade_alpha = texture2D(u_fadetexture, v_fade_tex).a; - lowp float gamma = u_gamma * v_gamma_scale; - lowp float alpha = smoothstep(u_buffer - gamma, u_buffer + gamma, dist) * fade_alpha; - - gl_FragColor = u_color * (alpha * u_opacity); - -#ifdef OVERDRAW_INSPECTOR - gl_FragColor = vec4(1.0); -#endif -} -)MBGL_SHADER"; + static const char* name; + static const char* vertexSource; + static const char* fragmentSource; }; } // namespace shaders |