From 9f2f7ddd12a4aaab0faddbe60cfb7c1e526fa7ff Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 3 Feb 2017 13:51:51 -0800 Subject: [core] Check in generated shader code One step toward eliminating the node/npm dependency for platforms other than node. --- CMakeLists.txt | 1 - Makefile | 1 + cmake/core-files.cmake | 15 ++ cmake/core.cmake | 1 - cmake/shaders.cmake | 32 ---- scripts/build-shaders.js | 75 --------- scripts/generate-shaders.js | 84 ++++++++++ src/mbgl/programs/circle_program.hpp | 2 +- src/mbgl/programs/collision_box_program.hpp | 2 +- src/mbgl/programs/debug_program.hpp | 2 +- src/mbgl/programs/fill_program.hpp | 8 +- src/mbgl/programs/line_program.hpp | 6 +- src/mbgl/programs/raster_program.hpp | 2 +- src/mbgl/programs/symbol_program.hpp | 4 +- src/mbgl/shaders/circle.hpp | 194 ++++++++++++++++++++++ src/mbgl/shaders/collision_box.hpp | 131 +++++++++++++++ src/mbgl/shaders/debug.hpp | 103 ++++++++++++ src/mbgl/shaders/fill.hpp | 123 ++++++++++++++ src/mbgl/shaders/fill_outline.hpp | 131 +++++++++++++++ src/mbgl/shaders/fill_outline_pattern.hpp | 159 ++++++++++++++++++ src/mbgl/shaders/fill_pattern.hpp | 148 +++++++++++++++++ src/mbgl/shaders/line.hpp | 219 +++++++++++++++++++++++++ src/mbgl/shaders/line_pattern.hpp | 236 +++++++++++++++++++++++++++ src/mbgl/shaders/line_sdf.hpp | 242 ++++++++++++++++++++++++++++ src/mbgl/shaders/raster.hpp | 153 ++++++++++++++++++ src/mbgl/shaders/symbol_icon.hpp | 145 +++++++++++++++++ src/mbgl/shaders/symbol_sdf.hpp | 196 ++++++++++++++++++++++ 27 files changed, 2293 insertions(+), 122 deletions(-) delete mode 100644 cmake/shaders.cmake delete mode 100755 scripts/build-shaders.js create mode 100755 scripts/generate-shaders.js create mode 100644 src/mbgl/shaders/circle.hpp create mode 100644 src/mbgl/shaders/collision_box.hpp create mode 100644 src/mbgl/shaders/debug.hpp create mode 100644 src/mbgl/shaders/fill.hpp create mode 100644 src/mbgl/shaders/fill_outline.hpp create mode 100644 src/mbgl/shaders/fill_outline_pattern.hpp create mode 100644 src/mbgl/shaders/fill_pattern.hpp create mode 100644 src/mbgl/shaders/line.hpp create mode 100644 src/mbgl/shaders/line_pattern.hpp create mode 100644 src/mbgl/shaders/line_sdf.hpp create mode 100644 src/mbgl/shaders/raster.hpp create mode 100644 src/mbgl/shaders/symbol_icon.hpp create mode 100644 src/mbgl/shaders/symbol_sdf.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6265c05069..a48d544538 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,7 +68,6 @@ endif() include(platform/${MBGL_PLATFORM}/config.cmake) include(cmake/core-files.cmake) -include(cmake/shaders.cmake) include(cmake/core.cmake) if(COMMAND mbgl_platform_test) diff --git a/Makefile b/Makefile index dcfdc032ee..a1283c8695 100644 --- a/Makefile +++ b/Makefile @@ -633,6 +633,7 @@ android-javadoc: .PHONY: style-code style-code: node scripts/generate-style-code.js + node scripts/generate-shaders.js .PHONY: codestyle codestyle: diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index 1059e972ce..e2a54daf01 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -181,6 +181,21 @@ set(MBGL_CORE_FILES src/mbgl/renderer/symbol_bucket.cpp src/mbgl/renderer/symbol_bucket.hpp + # shaders + src/mbgl/shaders/circle.hpp + src/mbgl/shaders/collision_box.hpp + src/mbgl/shaders/debug.hpp + src/mbgl/shaders/fill.hpp + src/mbgl/shaders/fill_outline.hpp + src/mbgl/shaders/fill_outline_pattern.hpp + src/mbgl/shaders/fill_pattern.hpp + src/mbgl/shaders/line.hpp + src/mbgl/shaders/line_pattern.hpp + src/mbgl/shaders/line_sdf.hpp + src/mbgl/shaders/raster.hpp + src/mbgl/shaders/symbol_icon.hpp + src/mbgl/shaders/symbol_sdf.hpp + # sprite include/mbgl/sprite/sprite_image.hpp src/mbgl/sprite/sprite_atlas.cpp diff --git a/cmake/core.cmake b/cmake/core.cmake index 8d8a942d9f..f893e1571e 100644 --- a/cmake/core.cmake +++ b/cmake/core.cmake @@ -12,7 +12,6 @@ add_custom_command( add_custom_target(mbgl-headers DEPENDS ${MBGL_GENERATED}/include/mbgl/util/version.hpp - ${MBGL_SHADER_FILES} ) add_library(mbgl-core STATIC diff --git a/cmake/shaders.cmake b/cmake/shaders.cmake deleted file mode 100644 index a9ded80a6c..0000000000 --- a/cmake/shaders.cmake +++ /dev/null @@ -1,32 +0,0 @@ -function(add_shader VAR name) - set(shader_build_cmd ${NodeJS_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/build-shaders.js) - set(shader_file_prefix ${CMAKE_SOURCE_DIR}/mapbox-gl-js/shaders) - set(shader_source_prefix ${MBGL_GENERATED}/include/mbgl/shader) - - add_custom_command( - OUTPUT ${shader_source_prefix}/${name}.hpp - COMMAND ${shader_build_cmd} ${name} ${shader_file_prefix} ${shader_source_prefix} - DEPENDS npm-install - DEPENDS ${CMAKE_SOURCE_DIR}/scripts/build-shaders.js - DEPENDS ${shader_file_prefix}/${name}.vertex.glsl - DEPENDS ${shader_file_prefix}/${name}.fragment.glsl - DEPENDS ${shader_file_prefix}/_prelude.vertex.glsl - DEPENDS ${shader_file_prefix}/_prelude.fragment.glsl - VERBATIM - ) - set(${VAR} ${${VAR}} ${shader_source_prefix}/${name}.hpp PARENT_SCOPE) -endfunction() - -add_shader(MBGL_SHADER_FILES circle) -add_shader(MBGL_SHADER_FILES collision_box) -add_shader(MBGL_SHADER_FILES debug) -add_shader(MBGL_SHADER_FILES fill) -add_shader(MBGL_SHADER_FILES fill_outline) -add_shader(MBGL_SHADER_FILES fill_outline_pattern) -add_shader(MBGL_SHADER_FILES fill_pattern) -add_shader(MBGL_SHADER_FILES line) -add_shader(MBGL_SHADER_FILES line_pattern) -add_shader(MBGL_SHADER_FILES line_sdf) -add_shader(MBGL_SHADER_FILES raster) -add_shader(MBGL_SHADER_FILES symbol_icon) -add_shader(MBGL_SHADER_FILES symbol_sdf) diff --git a/scripts/build-shaders.js b/scripts/build-shaders.js deleted file mode 100755 index 00ced5f23b..0000000000 --- a/scripts/build-shaders.js +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env node - -var path = require('path'); -var fs = require('fs'); -var mkdirp = require('mkdirp'); - -var shaderName = process.argv[2]; -var inputPath = process.argv[3]; -var outputPath = process.argv[4]; - -if (!shaderName || !inputPath || !outputPath) { - console.warn('Not enough arguments.'); - console.warn('Usage: %s shaderName /path-to-shader-sources /output-path', path.basename(process.argv[1])); - process.exit(1); -} - -function applyPragmas(source, pragmas) { - return source.replace(/#pragma mapbox: ([\w]+) ([\w]+) ([\w]+) ([\w]+)/g, (match, operation, precision, type, name) => { - return pragmas[operation] - .join("\n") - .replace(/\{type\}/g, type) - .replace(/\{precision\}/g, precision) - .replace(/\{name\}/g, name); - }); -} - -function vertexSource() { - var prelude = fs.readFileSync(path.join(inputPath, '_prelude.vertex.glsl')); - var source = fs.readFileSync(path.join(inputPath, shaderName + '.vertex.glsl'), 'utf8'); - return prelude + applyPragmas(source, { - define: [ - "uniform lowp float a_{name}_t;", - "attribute {precision} {type} a_{name}_min;", - "attribute {precision} {type} a_{name}_max;", - "varying {precision} {type} {name};" - ], - initialize: [ - "{name} = mix(a_{name}_min, a_{name}_max, a_{name}_t);" - ] - }); -} - -function fragmentSource() { - var prelude = fs.readFileSync(path.join(inputPath, '_prelude.fragment.glsl')); - var source = fs.readFileSync(path.join(inputPath, shaderName + '.fragment.glsl'), 'utf8'); - return prelude + applyPragmas(source, { - define: [ - "varying {precision} {type} {name};" - ], - initialize: [ - ] - }); -} - -var content = "#pragma once\n" + -"\n" + -"// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n" + -"\n" + -"#include \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"; - -mkdirp.sync(outputPath); -fs.writeFileSync(path.join(outputPath, shaderName + '.hpp'), content); diff --git a/scripts/generate-shaders.js b/scripts/generate-shaders.js new file mode 100755 index 0000000000..7660a6e66e --- /dev/null +++ b/scripts/generate-shaders.js @@ -0,0 +1,84 @@ +#!/usr/bin/env node + +var path = require('path'); +var fs = require('fs'); + +var inputPath = 'mapbox-gl-js/shaders'; +var outputPath = 'src/mbgl/shaders'; + +require('./style-code'); + +[ + 'circle', + 'collision_box', + 'debug', + 'fill', + 'fill_outline', + 'fill_outline_pattern', + 'fill_pattern', + 'line', + 'line_pattern', + 'line_sdf', + 'raster', + 'symbol_icon', + 'symbol_sdf' +].forEach(function (shaderName) { + function applyPragmas(source, pragmas) { + return source.replace(/#pragma mapbox: ([\w]+) ([\w]+) ([\w]+) ([\w]+)/g, (match, operation, precision, type, name) => { + return pragmas[operation] + .join("\n") + .replace(/\{type\}/g, type) + .replace(/\{precision\}/g, precision) + .replace(/\{name\}/g, name); + }); + } + + function vertexSource() { + var prelude = fs.readFileSync(path.join(inputPath, '_prelude.vertex.glsl')); + var source = fs.readFileSync(path.join(inputPath, shaderName + '.vertex.glsl'), 'utf8'); + return prelude + applyPragmas(source, { + define: [ + "uniform lowp float a_{name}_t;", + "attribute {precision} {type} a_{name}_min;", + "attribute {precision} {type} a_{name}_max;", + "varying {precision} {type} {name};" + ], + initialize: [ + "{name} = mix(a_{name}_min, a_{name}_max, a_{name}_t);" + ] + }); + } + + function fragmentSource() { + var prelude = fs.readFileSync(path.join(inputPath, '_prelude.fragment.glsl')); + var source = fs.readFileSync(path.join(inputPath, shaderName + '.fragment.glsl'), 'utf8'); + return prelude + applyPragmas(source, { + define: [ + "varying {precision} {type} {name};" + ], + initialize: [ + ] + }); + } + + var content = "#pragma once\n" + + "\n" + + "// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n" + + "\n" + + "#include \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); +}); diff --git a/src/mbgl/programs/circle_program.hpp b/src/mbgl/programs/circle_program.hpp index 60a5faf649..8f056048b1 100644 --- a/src/mbgl/programs/circle_program.hpp +++ b/src/mbgl/programs/circle_program.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/mbgl/programs/collision_box_program.hpp b/src/mbgl/programs/collision_box_program.hpp index e59aa5ac8b..78ed6aa0c9 100644 --- a/src/mbgl/programs/collision_box_program.hpp +++ b/src/mbgl/programs/collision_box_program.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/mbgl/programs/debug_program.hpp b/src/mbgl/programs/debug_program.hpp index 6b3b479d24..de1666b4a8 100644 --- a/src/mbgl/programs/debug_program.hpp +++ b/src/mbgl/programs/debug_program.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include namespace mbgl { diff --git a/src/mbgl/programs/fill_program.hpp b/src/mbgl/programs/fill_program.hpp index 600e25bb46..5406a5999c 100644 --- a/src/mbgl/programs/fill_program.hpp +++ b/src/mbgl/programs/fill_program.hpp @@ -3,10 +3,10 @@ #include #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include #include diff --git a/src/mbgl/programs/line_program.hpp b/src/mbgl/programs/line_program.hpp index f4e3fe3d50..76329a463f 100644 --- a/src/mbgl/programs/line_program.hpp +++ b/src/mbgl/programs/line_program.hpp @@ -3,9 +3,9 @@ #include #include #include -#include -#include -#include +#include +#include +#include #include #include diff --git a/src/mbgl/programs/raster_program.hpp b/src/mbgl/programs/raster_program.hpp index 7b4c3842be..09cb94ac17 100644 --- a/src/mbgl/programs/raster_program.hpp +++ b/src/mbgl/programs/raster_program.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/mbgl/programs/symbol_program.hpp b/src/mbgl/programs/symbol_program.hpp index 0130255356..e0e90f0fa4 100644 --- a/src/mbgl/programs/symbol_program.hpp +++ b/src/mbgl/programs/symbol_program.hpp @@ -3,8 +3,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/mbgl/shaders/circle.hpp b/src/mbgl/shaders/circle.hpp new file mode 100644 index 0000000000..cafee6b526 --- /dev/null +++ b/src/mbgl/shaders/circle.hpp @@ -0,0 +1,194 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/collision_box.hpp b/src/mbgl/shaders/collision_box.hpp new file mode 100644 index 0000000000..36bc4e729b --- /dev/null +++ b/src/mbgl/shaders/collision_box.hpp @@ -0,0 +1,131 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/debug.hpp b/src/mbgl/shaders/debug.hpp new file mode 100644 index 0000000000..b0c1cf0e84 --- /dev/null +++ b/src/mbgl/shaders/debug.hpp @@ -0,0 +1,103 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill.hpp b/src/mbgl/shaders/fill.hpp new file mode 100644 index 0000000000..0d735ceb73 --- /dev/null +++ b/src/mbgl/shaders/fill.hpp @@ -0,0 +1,123 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill_outline.hpp b/src/mbgl/shaders/fill_outline.hpp new file mode 100644 index 0000000000..92e60b0328 --- /dev/null +++ b/src/mbgl/shaders/fill_outline.hpp @@ -0,0 +1,131 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill_outline_pattern.hpp b/src/mbgl/shaders/fill_outline_pattern.hpp new file mode 100644 index 0000000000..6c6cc9c962 --- /dev/null +++ b/src/mbgl/shaders/fill_outline_pattern.hpp @@ -0,0 +1,159 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/fill_pattern.hpp b/src/mbgl/shaders/fill_pattern.hpp new file mode 100644 index 0000000000..eeb0a80936 --- /dev/null +++ b/src/mbgl/shaders/fill_pattern.hpp @@ -0,0 +1,148 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/line.hpp b/src/mbgl/shaders/line.hpp new file mode 100644 index 0000000000..1ecef0a447 --- /dev/null +++ b/src/mbgl/shaders/line.hpp @@ -0,0 +1,219 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/line_pattern.hpp b/src/mbgl/shaders/line_pattern.hpp new file mode 100644 index 0000000000..901d83702d --- /dev/null +++ b/src/mbgl/shaders/line_pattern.hpp @@ -0,0 +1,236 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/line_sdf.hpp b/src/mbgl/shaders/line_sdf.hpp new file mode 100644 index 0000000000..473a6ab650 --- /dev/null +++ b/src/mbgl/shaders/line_sdf.hpp @@ -0,0 +1,242 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/raster.hpp b/src/mbgl/shaders/raster.hpp new file mode 100644 index 0000000000..6f65a7a79d --- /dev/null +++ b/src/mbgl/shaders/raster.hpp @@ -0,0 +1,153 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/symbol_icon.hpp b/src/mbgl/shaders/symbol_icon.hpp new file mode 100644 index 0000000000..17f4b36009 --- /dev/null +++ b/src/mbgl/shaders/symbol_icon.hpp @@ -0,0 +1,145 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl diff --git a/src/mbgl/shaders/symbol_sdf.hpp b/src/mbgl/shaders/symbol_sdf.hpp new file mode 100644 index 0000000000..67bcd86354 --- /dev/null +++ b/src/mbgl/shaders/symbol_sdf.hpp @@ -0,0 +1,196 @@ +#pragma once + +// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. + +#include + +namespace mbgl { +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"; +}; + +} // namespace shaders +} // namespace mbgl -- cgit v1.2.1