diff options
Diffstat (limited to 'src/mbgl/shader')
41 files changed, 753 insertions, 181 deletions
diff --git a/src/mbgl/shader/circle_shader.cpp b/src/mbgl/shader/circle_shader.cpp index 9e294f8d76..c43c435e1b 100644 --- a/src/mbgl/shader/circle_shader.cpp +++ b/src/mbgl/shader/circle_shader.cpp @@ -9,7 +9,8 @@ CircleShader::CircleShader(gl::Context& context, Defines defines) : Shader(shaders::circle::name, shaders::circle::vertex, shaders::circle::fragment, - context, defines) { + context, defines), + uniformsState(CircleUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/circle_shader.hpp b/src/mbgl/shader/circle_shader.hpp index c2c4053ba4..1c67fd02f3 100644 --- a/src/mbgl/shader/circle_shader.hpp +++ b/src/mbgl/shader/circle_shader.hpp @@ -2,8 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> -#include <mbgl/util/color.hpp> +#include <mbgl/shader/circle_uniforms.hpp> namespace mbgl { @@ -14,17 +13,11 @@ public: CircleShader(gl::Context&, Defines defines = None); using VertexType = CircleVertex; + using UniformsType = CircleUniforms; gl::Attribute<int16_t, 2> a_pos = {"a_pos", *this}; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<std::array<float, 2>> u_extrude_scale = {"u_extrude_scale", *this}; - gl::Uniform<float> u_devicepixelratio = {"u_devicepixelratio", *this}; - gl::Uniform<Color> u_color = {"u_color", *this}; - gl::Uniform<float> u_radius = {"u_radius", *this}; - gl::Uniform<float> u_blur = {"u_blur", *this}; - gl::Uniform<float> u_opacity = {"u_opacity", *this}; - gl::Uniform<int32_t> u_scale_with_map = {"u_scale_with_map", *this}; + typename CircleUniforms::State uniformsState; }; } // namespace mbgl diff --git a/src/mbgl/shader/circle_uniforms.hpp b/src/mbgl/shader/circle_uniforms.hpp new file mode 100644 index 0000000000..c77c0daaba --- /dev/null +++ b/src/mbgl/shader/circle_uniforms.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include <mbgl/shader/uniforms.hpp> + +namespace mbgl { + +namespace uniforms { +MBGL_DEFINE_UNIFORM_SCALAR(float, u_radius); +MBGL_DEFINE_UNIFORM_SCALAR(bool, u_scale_with_map); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_devicepixelratio); +} // namespace uniforms + +struct CircleUniforms : gl::Uniforms< + uniforms::u_matrix, + uniforms::u_opacity, + uniforms::u_color, + uniforms::u_radius, + uniforms::u_blur, + uniforms::u_scale_with_map, + uniforms::u_extrude_scale, + uniforms::u_devicepixelratio> {}; + +} // namespace mbgl diff --git a/src/mbgl/shader/circle_vertex.hpp b/src/mbgl/shader/circle_vertex.hpp index 4fce49f137..62e6e2da5b 100644 --- a/src/mbgl/shader/circle_vertex.hpp +++ b/src/mbgl/shader/circle_vertex.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/attribute.hpp> -#include <array> +#include <vector> #include <cstdint> namespace mbgl { @@ -28,7 +28,7 @@ namespace gl { template <class Shader> struct AttributeBindings<Shader, CircleVertex> { - std::array<AttributeBinding, 1> operator()(const Shader& shader) { + std::vector<AttributeBinding> operator()(const Shader& shader) { return {{ MBGL_MAKE_ATTRIBUTE_BINDING(CircleVertex, shader, a_pos) }}; diff --git a/src/mbgl/shader/collision_box_shader.cpp b/src/mbgl/shader/collision_box_shader.cpp index d61c849cd1..f12327102a 100644 --- a/src/mbgl/shader/collision_box_shader.cpp +++ b/src/mbgl/shader/collision_box_shader.cpp @@ -9,7 +9,9 @@ CollisionBoxShader::CollisionBoxShader(gl::Context& context) : Shader(shaders::collision_box::name, shaders::collision_box::vertex, shaders::collision_box::fragment, - context) { + context), + uniformsState(CollisionBoxUniforms::state(*this)) +{ } } // namespace mbgl diff --git a/src/mbgl/shader/collision_box_shader.hpp b/src/mbgl/shader/collision_box_shader.hpp index 2f5c506168..337de77150 100644 --- a/src/mbgl/shader/collision_box_shader.hpp +++ b/src/mbgl/shader/collision_box_shader.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> +#include <mbgl/shader/collision_box_uniforms.hpp> namespace mbgl { @@ -13,15 +13,13 @@ public: CollisionBoxShader(gl::Context&); using VertexType = CollisionBoxVertex; + using UniformsType = CollisionBoxUniforms; gl::Attribute<int16_t, 2> a_pos = {"a_pos", *this}; gl::Attribute<int16_t, 2> a_extrude = {"a_extrude", *this}; gl::Attribute<uint8_t, 2> a_data = {"a_data", *this}; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<float> u_scale = {"u_scale", *this}; - gl::Uniform<float> u_zoom = {"u_zoom", *this}; - gl::Uniform<float> u_maxzoom = {"u_maxzoom", *this}; + typename CollisionBoxUniforms::State uniformsState; }; } // namespace mbgl diff --git a/src/mbgl/shader/collision_box_uniforms.hpp b/src/mbgl/shader/collision_box_uniforms.hpp new file mode 100644 index 0000000000..f195cc9a6a --- /dev/null +++ b/src/mbgl/shader/collision_box_uniforms.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include <mbgl/shader/uniforms.hpp> + +namespace mbgl { + +namespace uniforms { +MBGL_DEFINE_UNIFORM_SCALAR(float, u_scale); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_maxzoom); +} // namespace uniforms + +struct CollisionBoxUniforms : gl::Uniforms< + uniforms::u_matrix, + uniforms::u_scale, + uniforms::u_zoom, + uniforms::u_maxzoom> {}; + +} // namespace mbgl diff --git a/src/mbgl/shader/collision_box_vertex.hpp b/src/mbgl/shader/collision_box_vertex.hpp index ba72b1c0ee..563b6403bf 100644 --- a/src/mbgl/shader/collision_box_vertex.hpp +++ b/src/mbgl/shader/collision_box_vertex.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/attribute.hpp> -#include <array> +#include <vector> #include <cstdint> #include <cmath> @@ -30,7 +30,7 @@ namespace gl { template <class Shader> struct AttributeBindings<Shader, CollisionBoxVertex> { - std::array<AttributeBinding, 3> operator()(const Shader& shader) { + std::vector<AttributeBinding> operator()(const Shader& shader) { return {{ MBGL_MAKE_ATTRIBUTE_BINDING(CollisionBoxVertex, shader, a_pos), MBGL_MAKE_ATTRIBUTE_BINDING(CollisionBoxVertex, shader, a_extrude), diff --git a/src/mbgl/shader/fill_outline_pattern_shader.cpp b/src/mbgl/shader/fill_outline_pattern_shader.cpp index b03921d384..7b9cf81c6d 100644 --- a/src/mbgl/shader/fill_outline_pattern_shader.cpp +++ b/src/mbgl/shader/fill_outline_pattern_shader.cpp @@ -9,7 +9,8 @@ FillOutlinePatternShader::FillOutlinePatternShader(gl::Context& context, Defines : Shader(shaders::fill_outline_pattern::name, shaders::fill_outline_pattern::vertex, shaders::fill_outline_pattern::fragment, - context, defines) { + context, defines), + uniformsState(FillPatternUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/fill_outline_pattern_shader.hpp b/src/mbgl/shader/fill_outline_pattern_shader.hpp index 630e6a7ce8..c76b93c355 100644 --- a/src/mbgl/shader/fill_outline_pattern_shader.hpp +++ b/src/mbgl/shader/fill_outline_pattern_shader.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> +#include <mbgl/shader/fill_uniforms.hpp> namespace mbgl { @@ -13,25 +13,11 @@ public: FillOutlinePatternShader(gl::Context&, Defines defines = None); using VertexType = FillVertex; + using UniformsType = FillPatternUniforms; gl::Attribute<int16_t, 2> a_pos = {"a_pos", *this}; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_tl_a = {"u_pattern_tl_a", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_br_a = {"u_pattern_br_a", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_tl_b = {"u_pattern_tl_b", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_br_b = {"u_pattern_br_b", *this}; - gl::Uniform<float> u_opacity = {"u_opacity", *this}; - gl::Uniform<float> u_mix = {"u_mix", *this}; - gl::Uniform<float> u_scale_a = {"u_scale_a", *this}; - gl::Uniform<float> u_scale_b = {"u_scale_b", *this}; - gl::Uniform<float> u_tile_units_to_pixels = {"u_tile_units_to_pixels", *this}; - gl::Uniform<int32_t> u_image = {"u_image", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_size_a = {"u_pattern_size_a", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_size_b = {"u_pattern_size_b", *this}; - gl::Uniform<std::array<float, 2>> u_pixel_coord_upper = {"u_pixel_coord_upper", *this}; - gl::Uniform<std::array<float, 2>> u_pixel_coord_lower = {"u_pixel_coord_lower", *this}; - gl::Uniform<std::array<float, 2>> u_world = {"u_world", *this}; + typename FillPatternUniforms::State uniformsState; }; } // namespace mbgl diff --git a/src/mbgl/shader/fill_outline_shader.cpp b/src/mbgl/shader/fill_outline_shader.cpp index 6e6d8c2239..f3d8ac5f5b 100644 --- a/src/mbgl/shader/fill_outline_shader.cpp +++ b/src/mbgl/shader/fill_outline_shader.cpp @@ -9,7 +9,8 @@ FillOutlineShader::FillOutlineShader(gl::Context& context, Defines defines) : Shader(shaders::fill_outline::name, shaders::fill_outline::vertex, shaders::fill_outline::fragment, - context, defines) { + context, defines), + uniformsState(FillColorUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/fill_outline_shader.hpp b/src/mbgl/shader/fill_outline_shader.hpp index c20bc187d3..4194f7c8c0 100644 --- a/src/mbgl/shader/fill_outline_shader.hpp +++ b/src/mbgl/shader/fill_outline_shader.hpp @@ -2,8 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> -#include <mbgl/util/color.hpp> +#include <mbgl/shader/fill_uniforms.hpp> namespace mbgl { @@ -14,13 +13,11 @@ public: FillOutlineShader(gl::Context&, Defines defines = None); using VertexType = FillVertex; + using UniformsType = FillColorUniforms; gl::Attribute<int16_t, 2> a_pos = {"a_pos", *this}; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<Color> u_outline_color = {"u_outline_color", *this}; - gl::Uniform<float> u_opacity = {"u_opacity", *this}; - gl::Uniform<std::array<float, 2>> u_world = {"u_world", *this}; + typename FillColorUniforms::State uniformsState; }; } // namespace mbgl diff --git a/src/mbgl/shader/fill_pattern_shader.cpp b/src/mbgl/shader/fill_pattern_shader.cpp index 60be6d79ad..b3cd2c595c 100644 --- a/src/mbgl/shader/fill_pattern_shader.cpp +++ b/src/mbgl/shader/fill_pattern_shader.cpp @@ -9,7 +9,8 @@ FillPatternShader::FillPatternShader(gl::Context& context, Defines defines) : Shader(shaders::fill_pattern::name, shaders::fill_pattern::vertex, shaders::fill_pattern::fragment, - context, defines) { + context, defines), + uniformsState(FillPatternUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/fill_pattern_shader.hpp b/src/mbgl/shader/fill_pattern_shader.hpp index 36be538000..92a16ee45a 100644 --- a/src/mbgl/shader/fill_pattern_shader.hpp +++ b/src/mbgl/shader/fill_pattern_shader.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> +#include <mbgl/shader/fill_uniforms.hpp> namespace mbgl { @@ -13,24 +13,11 @@ public: FillPatternShader(gl::Context&, Defines defines = None); using VertexType = FillVertex; + using UniformsType = FillPatternUniforms; gl::Attribute<int16_t, 2> a_pos = {"a_pos", *this}; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_tl_a = {"u_pattern_tl_a", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_br_a = {"u_pattern_br_a", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_tl_b = {"u_pattern_tl_b", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_br_b = {"u_pattern_br_b", *this}; - gl::Uniform<float> u_opacity = {"u_opacity", *this}; - gl::Uniform<float> u_mix = {"u_mix", *this}; - gl::Uniform<float> u_scale_a = {"u_scale_a", *this}; - gl::Uniform<float> u_scale_b = {"u_scale_b", *this}; - gl::Uniform<float> u_tile_units_to_pixels = {"u_tile_units_to_pixels", *this}; - gl::Uniform<int32_t> u_image = {"u_image", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_size_a = {"u_pattern_size_a", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_size_b = {"u_pattern_size_b", *this}; - gl::Uniform<std::array<float, 2>> u_pixel_coord_upper = {"u_pixel_coord_upper", *this}; - gl::Uniform<std::array<float, 2>> u_pixel_coord_lower = {"u_pixel_coord_lower", *this}; + typename FillPatternUniforms::State uniformsState; }; } // namespace mbgl diff --git a/src/mbgl/shader/fill_shader.cpp b/src/mbgl/shader/fill_shader.cpp index 7026bb2f1c..59028d7384 100644 --- a/src/mbgl/shader/fill_shader.cpp +++ b/src/mbgl/shader/fill_shader.cpp @@ -9,7 +9,8 @@ FillShader::FillShader(gl::Context& context, Defines defines) : Shader(shaders::fill::name, shaders::fill::vertex, shaders::fill::fragment, - context, defines) { + context, defines), + uniformsState(FillColorUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/fill_shader.hpp b/src/mbgl/shader/fill_shader.hpp index 1240b73aa2..a7f3b8e2f9 100644 --- a/src/mbgl/shader/fill_shader.hpp +++ b/src/mbgl/shader/fill_shader.hpp @@ -2,8 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> -#include <mbgl/util/color.hpp> +#include <mbgl/shader/fill_uniforms.hpp> namespace mbgl { @@ -14,12 +13,11 @@ public: FillShader(gl::Context&, Defines defines = None); using VertexType = FillVertex; + using UniformsType = FillColorUniforms; gl::Attribute<int16_t, 2> a_pos = {"a_pos", *this}; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<Color> u_color = {"u_color", *this}; - gl::Uniform<float> u_opacity = {"u_opacity", *this}; + typename FillColorUniforms::State uniformsState; }; } // namespace mbgl diff --git a/src/mbgl/shader/fill_uniforms.cpp b/src/mbgl/shader/fill_uniforms.cpp new file mode 100644 index 0000000000..368cb12fcf --- /dev/null +++ b/src/mbgl/shader/fill_uniforms.cpp @@ -0,0 +1,45 @@ +#include <mbgl/shader/fill_uniforms.hpp> +#include <mbgl/sprite/sprite_atlas.hpp> +#include <mbgl/style/property_evaluator.hpp> +#include <mbgl/tile/tile_id.hpp> +#include <mbgl/map/transform_state.hpp> + +namespace mbgl { + +using namespace style; + +FillPatternUniforms::Values +FillPatternUniforms::values(mat4 matrix, + float opacity, + Size framebufferSize, + const SpriteAtlasPosition& a, + const SpriteAtlasPosition& b, + const Faded<std::string>& fading, + const UnwrappedTileID& tileID, + const TransformState& state) +{ + int32_t tileSizeAtNearestZoom = util::tileSize * state.zoomScale(state.getIntegerZoom() - tileID.canonical.z); + int32_t pixelX = tileSizeAtNearestZoom * (tileID.canonical.x + tileID.wrap * state.zoomScale(tileID.canonical.z)); + int32_t pixelY = tileSizeAtNearestZoom * tileID.canonical.y; + + return FillPatternUniforms::Values { + matrix, + opacity, + framebufferSize, + a.tl, + a.br, + b.tl, + b.br, + a.size, + b.size, + fading.fromScale, + fading.toScale, + fading.t, + 0, + std::array<float, 2> {{ float(pixelX >> 16), float(pixelY >> 16) }}, + std::array<float, 2> {{ float(pixelX & 0xFFFF), float(pixelY & 0xFFFF) }}, + 1.0f / tileID.pixelsToTileUnits(1.0f, state.getIntegerZoom()) + }; +} + +} // namespace mbgl diff --git a/src/mbgl/shader/fill_uniforms.hpp b/src/mbgl/shader/fill_uniforms.hpp new file mode 100644 index 0000000000..a596da0ea9 --- /dev/null +++ b/src/mbgl/shader/fill_uniforms.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include <mbgl/shader/uniforms.hpp> +#include <mbgl/util/mat4.hpp> +#include <mbgl/util/size.hpp> + +#include <string> + +namespace mbgl { + +namespace uniforms { +MBGL_DEFINE_UNIFORM_SCALAR(Size, u_world); +MBGL_DEFINE_UNIFORM_SCALAR(Color, u_outline_color); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_scale_a); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_scale_b); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_tile_units_to_pixels); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_pixel_coord_upper); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_pixel_coord_lower); +} // namespace uniforms + +struct FillColorUniforms : gl::Uniforms< + uniforms::u_matrix, + uniforms::u_opacity, + uniforms::u_color, + uniforms::u_outline_color, + uniforms::u_world> {}; + +class SpriteAtlasPosition; +class UnwrappedTileID; +class TransformState; + +namespace style { +template <class> class Faded; +} // namespace style + +struct FillPatternUniforms : gl::Uniforms< + uniforms::u_matrix, + uniforms::u_opacity, + uniforms::u_world, + uniforms::u_pattern_tl_a, + uniforms::u_pattern_br_a, + uniforms::u_pattern_tl_b, + uniforms::u_pattern_br_b, + uniforms::u_pattern_size_a, + uniforms::u_pattern_size_b, + uniforms::u_scale_a, + uniforms::u_scale_b, + uniforms::u_mix, + uniforms::u_image, + uniforms::u_pixel_coord_upper, + uniforms::u_pixel_coord_lower, + uniforms::u_tile_units_to_pixels> { + static Values values(mat4 matrix, + float opacity, + Size framebufferSize, + const SpriteAtlasPosition&, + const SpriteAtlasPosition&, + const style::Faded<std::string>&, + const UnwrappedTileID&, + const TransformState&); +}; + +} // namespace mbgl diff --git a/src/mbgl/shader/fill_vertex.hpp b/src/mbgl/shader/fill_vertex.hpp index 1b8130382a..86b83d28e5 100644 --- a/src/mbgl/shader/fill_vertex.hpp +++ b/src/mbgl/shader/fill_vertex.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/attribute.hpp> -#include <array> +#include <vector> #include <cstdint> namespace mbgl { @@ -19,7 +19,7 @@ namespace gl { template <class Shader> struct AttributeBindings<Shader, FillVertex> { - std::array<AttributeBinding, 1> operator()(const Shader& shader) { + std::vector<AttributeBinding> operator()(const Shader& shader) { return {{ MBGL_MAKE_ATTRIBUTE_BINDING(FillVertex, shader, a_pos) }}; diff --git a/src/mbgl/shader/line_pattern_shader.cpp b/src/mbgl/shader/line_pattern_shader.cpp index e6bc32a5c9..134e619b22 100644 --- a/src/mbgl/shader/line_pattern_shader.cpp +++ b/src/mbgl/shader/line_pattern_shader.cpp @@ -9,7 +9,8 @@ LinePatternShader::LinePatternShader(gl::Context& context, Defines defines) : Shader(shaders::line_pattern::name, shaders::line_pattern::vertex, shaders::line_pattern::fragment, - context, defines) { + context, defines), + uniformsState(LinePatternUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/line_pattern_shader.hpp b/src/mbgl/shader/line_pattern_shader.hpp index 1bd6085c8b..57fb7b6d61 100644 --- a/src/mbgl/shader/line_pattern_shader.hpp +++ b/src/mbgl/shader/line_pattern_shader.hpp @@ -1,8 +1,8 @@ #pragma once #include <mbgl/gl/shader.hpp> -#include <mbgl/gl/uniform.hpp> #include <mbgl/gl/attribute.hpp> +#include <mbgl/shader/line_uniforms.hpp> namespace mbgl { @@ -13,29 +13,12 @@ public: LinePatternShader(gl::Context&, Defines defines = None); using VertexType = LineVertex; + using UniformsType = LinePatternUniforms; gl::Attribute<int16_t, 2> a_pos = { "a_pos", *this }; gl::Attribute<uint8_t, 4> a_data = { "a_data", *this }; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<float> u_linewidth = {"u_linewidth", *this}; - gl::Uniform<float> u_gapwidth = {"u_gapwidth", *this}; - gl::Uniform<float> u_antialiasing = {"u_antialiasing", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_size_a = {"u_pattern_size_a", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_tl_a = {"u_pattern_tl_a", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_br_a = {"u_pattern_br_a", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_size_b = {"u_pattern_size_b", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_tl_b = {"u_pattern_tl_b", *this}; - gl::Uniform<std::array<float, 2>> u_pattern_br_b = {"u_pattern_br_b", *this}; - gl::Uniform<float> u_ratio = {"u_ratio", *this}; - gl::Uniform<float> u_point = {"u_point", *this}; - gl::Uniform<float> u_blur = {"u_blur", *this}; - gl::Uniform<float> u_fade = {"u_fade", *this}; - gl::Uniform<float> u_opacity = {"u_opacity", *this}; - gl::Uniform<float> u_extra = {"u_extra", *this}; - gl::Uniform<float> u_offset = {"u_offset", *this}; - gl::Uniform<int32_t> u_image = {"u_image", *this}; - gl::UniformMatrix<2> u_antialiasingmatrix = {"u_antialiasingmatrix", *this}; + typename LinePatternUniforms::State uniformsState; }; } // namespace mbgl diff --git a/src/mbgl/shader/line_sdf_shader.cpp b/src/mbgl/shader/line_sdf_shader.cpp index dd724365ea..d290c16676 100644 --- a/src/mbgl/shader/line_sdf_shader.cpp +++ b/src/mbgl/shader/line_sdf_shader.cpp @@ -9,7 +9,8 @@ LineSDFShader::LineSDFShader(gl::Context& context, Defines defines) : Shader(shaders::line_sdf::name, shaders::line_sdf::vertex, shaders::line_sdf::fragment, - context, defines) { + context, defines), + uniformsState(LineSDFUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/line_sdf_shader.hpp b/src/mbgl/shader/line_sdf_shader.hpp index d74e42e50f..b6cced7ae7 100644 --- a/src/mbgl/shader/line_sdf_shader.hpp +++ b/src/mbgl/shader/line_sdf_shader.hpp @@ -2,8 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> -#include <mbgl/util/color.hpp> +#include <mbgl/shader/line_uniforms.hpp> namespace mbgl { @@ -14,28 +13,12 @@ public: LineSDFShader(gl::Context&, Defines defines = None); using VertexType = LineVertex; + using UniformsType = LineSDFUniforms; gl::Attribute<int16_t, 2> a_pos = { "a_pos", *this }; gl::Attribute<uint8_t, 4> a_data = { "a_data", *this }; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<Color> u_color = {"u_color", *this}; - gl::Uniform<float> u_opacity = {"u_opacity", *this}; - gl::Uniform<float> u_linewidth = {"u_linewidth", *this}; - gl::Uniform<float> u_gapwidth = {"u_gapwidth", *this}; - gl::Uniform<float> u_antialiasing = {"u_antialiasing", *this}; - gl::Uniform<float> u_ratio = {"u_ratio", *this}; - gl::Uniform<float> u_blur = {"u_blur", *this}; - gl::Uniform<std::array<float, 2>> u_patternscale_a = { "u_patternscale_a", *this}; - gl::Uniform<float> u_tex_y_a = {"u_tex_y_a", *this}; - gl::Uniform<std::array<float, 2>> u_patternscale_b = { "u_patternscale_b", *this}; - gl::Uniform<float> u_tex_y_b = {"u_tex_y_b", *this}; - gl::Uniform<int32_t> u_image = {"u_image", *this}; - gl::Uniform<float> u_sdfgamma = {"u_sdfgamma", *this}; - gl::Uniform<float> u_mix = {"u_mix", *this}; - gl::Uniform<float> u_extra = {"u_extra", *this}; - gl::Uniform<float> u_offset = {"u_offset", *this}; - gl::UniformMatrix<2> u_antialiasingmatrix = {"u_antialiasingmatrix", *this}; + typename LineSDFUniforms::State uniformsState; }; diff --git a/src/mbgl/shader/line_shader.cpp b/src/mbgl/shader/line_shader.cpp index 4e934cd60c..07227070be 100644 --- a/src/mbgl/shader/line_shader.cpp +++ b/src/mbgl/shader/line_shader.cpp @@ -9,7 +9,8 @@ LineShader::LineShader(gl::Context& context, Defines defines) : Shader(shaders::line::name, shaders::line::vertex, shaders::line::fragment, - context, defines) { + context, defines), + uniformsState(LineColorUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/line_shader.hpp b/src/mbgl/shader/line_shader.hpp index 79991e1883..eafaf15bef 100644 --- a/src/mbgl/shader/line_shader.hpp +++ b/src/mbgl/shader/line_shader.hpp @@ -2,8 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> -#include <mbgl/util/color.hpp> +#include <mbgl/shader/line_uniforms.hpp> namespace mbgl { @@ -14,21 +13,12 @@ public: LineShader(gl::Context&, Defines defines = None); using VertexType = LineVertex; + using UniformsType = LineColorUniforms; gl::Attribute<int16_t, 2> a_pos = { "a_pos", *this }; gl::Attribute<uint8_t, 4> a_data = { "a_data", *this }; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<Color> u_color = {"u_color", *this}; - gl::Uniform<float> u_opacity = {"u_opacity", *this}; - gl::Uniform<float> u_ratio = {"u_ratio", *this}; - gl::Uniform<float> u_linewidth = {"u_linewidth", *this}; - gl::Uniform<float> u_gapwidth = {"u_gapwidth", *this}; - gl::Uniform<float> u_antialiasing = {"u_antialiasing", *this}; - gl::Uniform<float> u_blur = {"u_blur", *this}; - gl::Uniform<float> u_extra = {"u_extra", *this}; - gl::Uniform<float> u_offset = {"u_offset", *this}; - gl::UniformMatrix<2> u_antialiasingmatrix = {"u_antialiasingmatrix", *this}; + typename LineColorUniforms::State uniformsState; }; diff --git a/src/mbgl/shader/line_uniforms.cpp b/src/mbgl/shader/line_uniforms.cpp new file mode 100644 index 0000000000..f1d73e20eb --- /dev/null +++ b/src/mbgl/shader/line_uniforms.cpp @@ -0,0 +1,133 @@ +#include <mbgl/shader/line_uniforms.hpp> +#include <mbgl/style/layers/line_layer_properties.hpp> +#include <mbgl/renderer/render_tile.hpp> +#include <mbgl/map/transform_state.hpp> +#include <mbgl/util/mat2.hpp> +#include <mbgl/sprite/sprite_atlas.hpp> +#include <mbgl/geometry/line_atlas.hpp> + +namespace mbgl { + +template <class Values, class...Args> +Values makeValues(const style::LinePaintProperties& properties, + float pixelRatio, + const RenderTile& tile, + const TransformState& state, + Args&&... args) { + // the distance over which the line edge fades out. + // Retina devices need a smaller distance to avoid aliasing. + float antialiasing = 1.0 / pixelRatio; + + mat2 antialiasingMatrix; + matrix::identity(antialiasingMatrix); + matrix::scale(antialiasingMatrix, antialiasingMatrix, 1.0, std::cos(state.getPitch())); + matrix::rotate(antialiasingMatrix, antialiasingMatrix, state.getAngle()); + + // calculate how much longer the real world distance is at the top of the screen + // than at the middle of the screen. + float topedgelength = std::sqrt(std::pow(state.getSize().height, 2.0f) / 4.0f * (1.0f + std::pow(state.getAltitude(), 2.0f))); + float x = state.getSize().height / 2.0f * std::tan(state.getPitch()); + + return Values { + tile.translatedMatrix(properties.lineTranslate.value, + properties.lineTranslateAnchor.value, + state), + properties.lineOpacity.value, + properties.lineWidth.value / 2, + properties.lineGapWidth.value / 2, + properties.lineBlur.value + antialiasing, + -properties.lineOffset.value, + antialiasing / 2, + antialiasingMatrix, + 1.0f / tile.id.pixelsToTileUnits(1.0, state.getZoom()), + (topedgelength + x) / topedgelength - 1.0f, + std::forward<Args>(args)... + }; +} + +LineColorUniforms::Values +LineColorUniforms::values(const style::LinePaintProperties& properties, + float pixelRatio, + const RenderTile& tile, + const TransformState& state) { + return makeValues<LineColorUniforms::Values>( + properties, + pixelRatio, + tile, + state, + properties.lineColor.value + ); +} + +LineSDFUniforms::Values +LineSDFUniforms::values(const style::LinePaintProperties& properties, + float pixelRatio, + const RenderTile& tile, + const TransformState& state, + const LinePatternPos& posA, + const LinePatternPos& posB, + float dashLineWidth, + float atlasWidth) { + const float widthA = posA.width * properties.lineDasharray.value.fromScale * dashLineWidth; + const float widthB = posB.width * properties.lineDasharray.value.toScale * dashLineWidth; + + std::array<float, 2> scaleA {{ + 1.0f / tile.id.pixelsToTileUnits(widthA, state.getIntegerZoom()), + -posA.height / 2.0f + }}; + + std::array<float, 2> scaleB {{ + 1.0f / tile.id.pixelsToTileUnits(widthB, state.getIntegerZoom()), + -posB.height / 2.0f + }}; + + return makeValues<LineSDFUniforms::Values>( + properties, + pixelRatio, + tile, + state, + properties.lineColor.value, + scaleA, + scaleB, + posA.y, + posB.y, + properties.lineDasharray.value.t, + atlasWidth / (std::min(widthA, widthB) * 256.0f * pixelRatio) / 2.0f, + 0 + ); +} + +LinePatternUniforms::Values +LinePatternUniforms::values(const style::LinePaintProperties& properties, + float pixelRatio, + const RenderTile& tile, + const TransformState& state, + const SpriteAtlasPosition& posA, + const SpriteAtlasPosition& posB) { + std::array<float, 2> sizeA {{ + tile.id.pixelsToTileUnits(posA.size[0] * properties.linePattern.value.fromScale, state.getIntegerZoom()), + posA.size[1] + }}; + + std::array<float, 2> sizeB {{ + tile.id.pixelsToTileUnits(posB.size[0] * properties.linePattern.value.toScale, state.getIntegerZoom()), + posB.size[1] + }}; + + return makeValues<LinePatternUniforms::Values>( + properties, + pixelRatio, + tile, + state, + posA.tl, + posA.br, + posB.tl, + posB.br, + sizeA, + sizeB, + properties.linePattern.value.t, + 0 + ); +} + +} // namespace mbgl diff --git a/src/mbgl/shader/line_uniforms.hpp b/src/mbgl/shader/line_uniforms.hpp new file mode 100644 index 0000000000..8a9c5310b5 --- /dev/null +++ b/src/mbgl/shader/line_uniforms.hpp @@ -0,0 +1,109 @@ +#pragma once + +#include <mbgl/shader/uniforms.hpp> + +namespace mbgl { + +namespace style { +class LinePaintProperties; +} // namespace style + +class RenderTile; +class TransformState; +class LinePatternPos; +class SpriteAtlasPosition; + +namespace uniforms { +MBGL_DEFINE_UNIFORM_SCALAR(float, u_ratio); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_linewidth); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_gapwidth); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_antialiasing); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_extra); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_offset); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_tex_y_a); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_tex_y_b); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_sdfgamma); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_fade); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_patternscale_a); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_patternscale_b); +MBGL_DEFINE_UNIFORM_MATRIX(double, 2, u_antialiasingmatrix); +} // namespace uniforms + +struct LineColorUniforms : gl::Uniforms< + uniforms::u_matrix, + uniforms::u_opacity, + uniforms::u_linewidth, + uniforms::u_gapwidth, + uniforms::u_blur, + uniforms::u_offset, + uniforms::u_antialiasing, + uniforms::u_antialiasingmatrix, + uniforms::u_ratio, + uniforms::u_extra, + uniforms::u_color> +{ + static Values values(const style::LinePaintProperties&, + float pixelRatio, + const RenderTile&, + const TransformState&); +}; + +struct LineSDFUniforms : gl::Uniforms< + uniforms::u_matrix, + uniforms::u_opacity, + uniforms::u_linewidth, + uniforms::u_gapwidth, + uniforms::u_blur, + uniforms::u_offset, + uniforms::u_antialiasing, + uniforms::u_antialiasingmatrix, + uniforms::u_ratio, + uniforms::u_extra, + uniforms::u_color, + uniforms::u_patternscale_a, + uniforms::u_patternscale_b, + uniforms::u_tex_y_a, + uniforms::u_tex_y_b, + uniforms::u_mix, + uniforms::u_sdfgamma, + uniforms::u_image> +{ + static Values values(const style::LinePaintProperties&, + float pixelRatio, + const RenderTile&, + const TransformState&, + const LinePatternPos& posA, + const LinePatternPos& posB, + float dashLineWidth, + float atlasWidth); +}; + +struct LinePatternUniforms : gl::Uniforms< + uniforms::u_matrix, + uniforms::u_opacity, + uniforms::u_linewidth, + uniforms::u_gapwidth, + uniforms::u_blur, + uniforms::u_offset, + uniforms::u_antialiasing, + uniforms::u_antialiasingmatrix, + uniforms::u_ratio, + uniforms::u_extra, + uniforms::u_pattern_tl_a, + uniforms::u_pattern_br_a, + uniforms::u_pattern_tl_b, + uniforms::u_pattern_br_b, + uniforms::u_pattern_size_a, + uniforms::u_pattern_size_b, + uniforms::u_fade, + uniforms::u_image> +{ + static Values values(const style::LinePaintProperties&, + float pixelRatio, + const RenderTile&, + const TransformState&, + const SpriteAtlasPosition& posA, + const SpriteAtlasPosition& posB); +}; + +} // namespace mbgl diff --git a/src/mbgl/shader/line_vertex.hpp b/src/mbgl/shader/line_vertex.hpp index 086100810e..6d299c5197 100644 --- a/src/mbgl/shader/line_vertex.hpp +++ b/src/mbgl/shader/line_vertex.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/attribute.hpp> -#include <array> +#include <vector> #include <cstdint> #include <cmath> @@ -60,7 +60,7 @@ namespace gl { template <class Shader> struct AttributeBindings<Shader, LineVertex> { - std::array<AttributeBinding, 2> operator()(const Shader& shader) { + std::vector<AttributeBinding> operator()(const Shader& shader) { return {{ MBGL_MAKE_ATTRIBUTE_BINDING(LineVertex, shader, a_pos), MBGL_MAKE_ATTRIBUTE_BINDING(LineVertex, shader, a_data) diff --git a/src/mbgl/shader/raster_shader.cpp b/src/mbgl/shader/raster_shader.cpp index 34b2bdf47b..17d6b246a1 100644 --- a/src/mbgl/shader/raster_shader.cpp +++ b/src/mbgl/shader/raster_shader.cpp @@ -9,7 +9,8 @@ RasterShader::RasterShader(gl::Context& context, Defines defines) : Shader(shaders::raster::name, shaders::raster::vertex, shaders::raster::fragment, - context, defines) { + context, defines), + uniformsState(RasterUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/raster_shader.hpp b/src/mbgl/shader/raster_shader.hpp index 9633fd5fa0..977556a097 100644 --- a/src/mbgl/shader/raster_shader.hpp +++ b/src/mbgl/shader/raster_shader.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> +#include <mbgl/shader/raster_uniforms.hpp> namespace mbgl { @@ -13,23 +13,12 @@ public: RasterShader(gl::Context&, Defines defines = None); using VertexType = RasterVertex; + using UniformsType = RasterUniforms; gl::Attribute<int16_t, 2> a_pos = { "a_pos", *this }; gl::Attribute<uint16_t, 2> a_texture_pos = { "a_texture_pos", *this }; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<int32_t> u_image0 = {"u_image0", *this}; - gl::Uniform<int32_t> u_image1 = {"u_image1", *this}; - gl::Uniform<float> u_opacity0 = {"u_opacity0", *this}; - gl::Uniform<float> u_opacity1 = {"u_opacity1", *this}; - gl::Uniform<float> u_buffer_scale = {"u_buffer_scale", *this}; - gl::Uniform<float> u_brightness_low = {"u_brightness_low", *this}; - gl::Uniform<float> u_brightness_high = {"u_brightness_high", *this}; - gl::Uniform<float> u_saturation_factor = {"u_saturation_factor", *this}; - gl::Uniform<float> u_contrast_factor = {"u_contrast_factor", *this}; - gl::Uniform<std::array<float, 3>> u_spin_weights = {"u_spin_weights", *this}; - gl::Uniform<std::array<float, 2>> u_tl_parent = {"u_tl_parent", *this}; - gl::Uniform<float> u_scale_parent = {"u_scale_parent", *this}; + typename RasterUniforms::State uniformsState; }; } // namespace mbgl diff --git a/src/mbgl/shader/raster_uniforms.hpp b/src/mbgl/shader/raster_uniforms.hpp new file mode 100644 index 0000000000..0a81b95c66 --- /dev/null +++ b/src/mbgl/shader/raster_uniforms.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include <mbgl/shader/uniforms.hpp> + +namespace mbgl { + +namespace uniforms { +MBGL_DEFINE_UNIFORM_SCALAR(gl::TextureUnit, u_image0); +MBGL_DEFINE_UNIFORM_SCALAR(gl::TextureUnit, u_image1); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_opacity0); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_opacity1); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_buffer_scale); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_brightness_low); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_brightness_high); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_saturation_factor); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_contrast_factor); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_scale_parent); +MBGL_DEFINE_UNIFORM_VECTOR(float, 3, u_spin_weights); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_tl_parent); +} // namespace uniforms + +struct RasterUniforms : gl::Uniforms< + uniforms::u_matrix, + uniforms::u_image0, + uniforms::u_image1, + uniforms::u_opacity0, + uniforms::u_opacity1, + uniforms::u_brightness_low, + uniforms::u_brightness_high, + uniforms::u_saturation_factor, + uniforms::u_contrast_factor, + uniforms::u_spin_weights, + uniforms::u_buffer_scale, + uniforms::u_scale_parent, + uniforms::u_tl_parent> {}; + +} // namespace mbgl diff --git a/src/mbgl/shader/raster_vertex.hpp b/src/mbgl/shader/raster_vertex.hpp index 70e08c609d..889405d6cd 100644 --- a/src/mbgl/shader/raster_vertex.hpp +++ b/src/mbgl/shader/raster_vertex.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/attribute.hpp> -#include <array> +#include <vector> #include <cstdint> namespace mbgl { @@ -27,7 +27,7 @@ namespace gl { template <class Shader> struct AttributeBindings<Shader, RasterVertex> { - std::array<AttributeBinding, 2> operator()(const Shader& shader) { + std::vector<AttributeBinding> operator()(const Shader& shader) { return {{ MBGL_MAKE_ATTRIBUTE_BINDING(RasterVertex, shader, a_pos), MBGL_MAKE_ATTRIBUTE_BINDING(RasterVertex, shader, a_texture_pos) diff --git a/src/mbgl/shader/shaders.hpp b/src/mbgl/shader/shaders.hpp index 937ee85f44..56b063ce29 100644 --- a/src/mbgl/shader/shaders.hpp +++ b/src/mbgl/shader/shaders.hpp @@ -48,11 +48,6 @@ public: SymbolSDFShader symbolGlyph; CollisionBoxShader collisionBox; - - gl::VertexArrayObject coveringPlainArray; - gl::VertexArrayObject coveringRasterArray; - gl::VertexArrayObject backgroundPatternArray; - gl::VertexArrayObject backgroundArray; }; } // namespace mbgl diff --git a/src/mbgl/shader/symbol_icon_shader.cpp b/src/mbgl/shader/symbol_icon_shader.cpp index 2655a1c9b5..66fce84d83 100644 --- a/src/mbgl/shader/symbol_icon_shader.cpp +++ b/src/mbgl/shader/symbol_icon_shader.cpp @@ -9,7 +9,8 @@ SymbolIconShader::SymbolIconShader(gl::Context& context, Defines defines) : Shader(shaders::symbol_icon::name, shaders::symbol_icon::vertex, shaders::symbol_icon::fragment, - context, defines) { + context, defines), + uniformsState(SymbolIconUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/symbol_icon_shader.hpp b/src/mbgl/shader/symbol_icon_shader.hpp index 5281beb60c..95b669e8f7 100644 --- a/src/mbgl/shader/symbol_icon_shader.hpp +++ b/src/mbgl/shader/symbol_icon_shader.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> +#include <mbgl/shader/symbol_uniforms.hpp> namespace mbgl { @@ -13,20 +13,14 @@ public: SymbolIconShader(gl::Context&, Defines defines = None); using VertexType = SymbolVertex; + using UniformsType = SymbolIconUniforms; gl::Attribute<int16_t, 2> a_pos = { "a_pos", *this }; gl::Attribute<int16_t, 2> a_offset = { "a_offset", *this }; gl::Attribute<uint16_t, 2> a_texture_pos = { "a_texture_pos", *this }; gl::Attribute<uint8_t, 4> a_data = { "a_data", *this }; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<std::array<float, 2>> u_extrude_scale = {"u_extrude_scale", *this}; - gl::Uniform<float> u_zoom = {"u_zoom", *this}; - gl::Uniform<float> u_opacity = {"u_opacity", *this}; - gl::Uniform<std::array<float, 2>> u_texsize = {"u_texsize", *this}; - gl::Uniform<int32_t> u_rotate_with_map = {"u_rotate_with_map", *this}; - gl::Uniform<int32_t> u_texture = {"u_texture", *this}; - gl::Uniform<int32_t> u_fadetexture = {"u_fadetexture", *this}; + typename SymbolIconUniforms::State uniformsState; }; } // namespace mbgl diff --git a/src/mbgl/shader/symbol_sdf_shader.cpp b/src/mbgl/shader/symbol_sdf_shader.cpp index 63c3bd5a4a..b8dffbab11 100644 --- a/src/mbgl/shader/symbol_sdf_shader.cpp +++ b/src/mbgl/shader/symbol_sdf_shader.cpp @@ -9,7 +9,8 @@ SymbolSDFShader::SymbolSDFShader(gl::Context& context, Defines defines) : Shader(shaders::symbol_sdf::name, shaders::symbol_sdf::vertex, shaders::symbol_sdf::fragment, - context, defines) { + context, defines), + uniformsState(SymbolSDFUniforms::state(*this)) { } } // namespace mbgl diff --git a/src/mbgl/shader/symbol_sdf_shader.hpp b/src/mbgl/shader/symbol_sdf_shader.hpp index 8d3b3df939..b7c12811c0 100644 --- a/src/mbgl/shader/symbol_sdf_shader.hpp +++ b/src/mbgl/shader/symbol_sdf_shader.hpp @@ -2,8 +2,7 @@ #include <mbgl/gl/shader.hpp> #include <mbgl/gl/attribute.hpp> -#include <mbgl/gl/uniform.hpp> -#include <mbgl/util/color.hpp> +#include <mbgl/shader/symbol_uniforms.hpp> namespace mbgl { @@ -14,27 +13,14 @@ public: SymbolSDFShader(gl::Context&, Defines defines = None); using VertexType = SymbolVertex; + using UniformsType = SymbolSDFUniforms; gl::Attribute<int16_t, 2> a_pos = { "a_pos", *this }; gl::Attribute<int16_t, 2> a_offset = { "a_offset", *this }; gl::Attribute<uint16_t, 2> a_texture_pos = { "a_texture_pos", *this }; gl::Attribute<uint8_t, 4> a_data = { "a_data", *this }; - gl::UniformMatrix<4> u_matrix = {"u_matrix", *this}; - gl::Uniform<std::array<float, 2>> u_extrude_scale = {"u_extrude_scale", *this}; - gl::Uniform<Color> u_color = {"u_color", *this}; - gl::Uniform<float> u_opacity = {"u_opacity", *this}; - gl::Uniform<std::array<float, 2>> u_texsize = {"u_texsize", *this}; - gl::Uniform<float> u_buffer = {"u_buffer", *this}; - gl::Uniform<float> u_gamma = {"u_gamma", *this}; - gl::Uniform<float> u_zoom = {"u_zoom", *this}; - gl::Uniform<float> u_pitch = {"u_pitch", *this}; - gl::Uniform<float> u_bearing = {"u_bearing", *this}; - gl::Uniform<float> u_aspect_ratio = {"u_aspect_ratio", *this}; - gl::Uniform<int32_t> u_rotate_with_map = {"u_rotate_with_map", *this}; - gl::Uniform<int32_t> u_pitch_with_map = {"u_pitch_with_map", *this}; - gl::Uniform<int32_t> u_texture = {"u_texture", *this}; - gl::Uniform<int32_t> u_fadetexture = {"u_fadetexture", *this}; + typename SymbolSDFUniforms::State uniformsState; }; } // namespace mbgl diff --git a/src/mbgl/shader/symbol_uniforms.cpp b/src/mbgl/shader/symbol_uniforms.cpp new file mode 100644 index 0000000000..cd9f33de71 --- /dev/null +++ b/src/mbgl/shader/symbol_uniforms.cpp @@ -0,0 +1,143 @@ +#include <mbgl/shader/symbol_uniforms.hpp> +#include <mbgl/renderer/render_tile.hpp> +#include <mbgl/map/transform_state.hpp> +#include <mbgl/style/layers/symbol_layer_impl.hpp> + +namespace mbgl { + +using namespace style; + +template <class Values, class...Args> +Values makeValues(const style::SymbolPropertyValues& values, + const std::array<uint16_t, 2>& texsize, + const std::array<float, 2>& pixelsToGLUnits, + const RenderTile& tile, + const TransformState& state, + Args&&... args) { + std::array<float, 2> extrudeScale; + + const float scale = values.paintSize / values.sdfScale; + if (values.pitchAlignment == AlignmentType::Map) { + extrudeScale.fill(tile.id.pixelsToTileUnits(1, state.getZoom()) * scale); + } else { + extrudeScale = {{ + pixelsToGLUnits[0] * scale * state.getAltitude(), + pixelsToGLUnits[1] * scale * state.getAltitude() + }}; + } + + // adjust min/max zooms for variable font sies + float zoomAdjust = std::log(values.paintSize / values.layoutSize) / std::log(2); + + return Values { + tile.translatedMatrix(values.translate, + values.translateAnchor, + state), + values.opacity, + extrudeScale, + std::array<float, 2> {{ texsize[0] / 4.0f, texsize[1] / 4.0f }}, + (state.getZoom() - zoomAdjust) * 10.0f, + values.rotationAlignment == AlignmentType::Map, + 0, + 1, + std::forward<Args>(args)... + }; +} + +SymbolIconUniforms::Values +SymbolIconUniforms::values(const style::SymbolPropertyValues& values, + const std::array<uint16_t, 2>& texsize, + const std::array<float, 2>& pixelsToGLUnits, + const RenderTile& tile, + const TransformState& state) +{ + return makeValues<SymbolIconUniforms::Values>( + values, + texsize, + pixelsToGLUnits, + tile, + state + ); +} + +static SymbolSDFUniforms::Values makeSDFValues(const style::SymbolPropertyValues& values, + const std::array<uint16_t, 2>& texsize, + const std::array<float, 2>& pixelsToGLUnits, + const RenderTile& tile, + const TransformState& state, + float pixelRatio, + Color color, + float buffer, + float gammaAdjust) +{ + // The default gamma value has to be adjust for the current pixelratio so that we're not + // drawing blurry font on retina screens. + const float gammaBase = 0.105 * values.sdfScale / values.paintSize / pixelRatio; + const float gammaScale = values.pitchAlignment == AlignmentType::Map + ? 1.0 / std::cos(state.getPitch()) + : 1.0; + + return makeValues<SymbolSDFUniforms::Values>( + values, + texsize, + pixelsToGLUnits, + tile, + state, + color, + buffer, + (gammaBase + gammaAdjust) * gammaScale, + state.getPitch(), + -1.0f * state.getAngle(), + (state.getSize().width * 1.0f) / (state.getSize().height * 1.0f), + values.pitchAlignment == AlignmentType::Map + ); +} + +SymbolSDFUniforms::Values +SymbolSDFUniforms::haloValues(const style::SymbolPropertyValues& values, + const std::array<uint16_t, 2>& texsize, + const std::array<float, 2>& pixelsToGLUnits, + const RenderTile& tile, + const TransformState& state, + float pixelRatio) +{ + const float scale = values.paintSize / values.sdfScale; + const float sdfPx = 8.0f; + const float blurOffset = 1.19f; + const float haloOffset = 6.0f; + + return makeSDFValues( + values, + texsize, + pixelsToGLUnits, + tile, + state, + pixelRatio, + values.haloColor, + (haloOffset - values.haloWidth / scale) / sdfPx, + values.haloBlur * blurOffset / scale / sdfPx + ); +} + +SymbolSDFUniforms::Values +SymbolSDFUniforms::foregroundValues(const style::SymbolPropertyValues& values, + const std::array<uint16_t, 2>& texsize, + const std::array<float, 2>& pixelsToGLUnits, + const RenderTile& tile, + const TransformState& state, + float pixelRatio) +{ + return makeSDFValues( + values, + texsize, + pixelsToGLUnits, + tile, + state, + pixelRatio, + values.color, + (256.0f - 64.0f) / 256.0f, + 0 + ); +} + +} // namespace mbgl diff --git a/src/mbgl/shader/symbol_uniforms.hpp b/src/mbgl/shader/symbol_uniforms.hpp new file mode 100644 index 0000000000..dc1b25004f --- /dev/null +++ b/src/mbgl/shader/symbol_uniforms.hpp @@ -0,0 +1,76 @@ +#pragma once + +#include <mbgl/shader/uniforms.hpp> + +#include <array> + +namespace mbgl { + +namespace style { +class SymbolPropertyValues; +} // namespace style + +class RenderTile; +class TransformState; + +namespace uniforms { +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_texsize); +MBGL_DEFINE_UNIFORM_SCALAR(bool, u_rotate_with_map); +MBGL_DEFINE_UNIFORM_SCALAR(bool, u_pitch_with_map); +MBGL_DEFINE_UNIFORM_SCALAR(gl::TextureUnit, u_texture); +MBGL_DEFINE_UNIFORM_SCALAR(gl::TextureUnit, u_fadetexture); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_buffer); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_gamma); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_aspect_ratio); +} // namespace uniforms + +struct SymbolIconUniforms : gl::Uniforms< + uniforms::u_matrix, + uniforms::u_opacity, + uniforms::u_extrude_scale, + uniforms::u_texsize, + uniforms::u_zoom, + uniforms::u_rotate_with_map, + uniforms::u_texture, + uniforms::u_fadetexture> +{ + static Values values(const style::SymbolPropertyValues&, + const std::array<uint16_t, 2>& texsize, + const std::array<float, 2>& pixelsToGLUnits, + const RenderTile&, + const TransformState&); +}; + +struct SymbolSDFUniforms : gl::Uniforms< + uniforms::u_matrix, + uniforms::u_opacity, + uniforms::u_extrude_scale, + uniforms::u_texsize, + uniforms::u_zoom, + uniforms::u_rotate_with_map, + uniforms::u_texture, + uniforms::u_fadetexture, + uniforms::u_color, + uniforms::u_buffer, + uniforms::u_gamma, + uniforms::u_pitch, + uniforms::u_bearing, + uniforms::u_aspect_ratio, + uniforms::u_pitch_with_map> +{ + static Values haloValues(const style::SymbolPropertyValues&, + const std::array<uint16_t, 2>& texsize, + const std::array<float, 2>& pixelsToGLUnits, + const RenderTile&, + const TransformState&, + float pixelRatio); + + static Values foregroundValues(const style::SymbolPropertyValues&, + const std::array<uint16_t, 2>& texsize, + const std::array<float, 2>& pixelsToGLUnits, + const RenderTile&, + const TransformState&, + float pixelRatio); +}; + +} // namespace mbgl diff --git a/src/mbgl/shader/symbol_vertex.hpp b/src/mbgl/shader/symbol_vertex.hpp index 4eba86f946..4bb6c82a60 100644 --- a/src/mbgl/shader/symbol_vertex.hpp +++ b/src/mbgl/shader/symbol_vertex.hpp @@ -2,7 +2,7 @@ #include <mbgl/gl/attribute.hpp> -#include <array> +#include <vector> #include <cstdint> #include <cmath> @@ -40,7 +40,7 @@ namespace gl { template <class Shader> struct AttributeBindings<Shader, SymbolVertex> { - std::array<AttributeBinding, 4> operator()(const Shader& shader) { + std::vector<AttributeBinding> operator()(const Shader& shader) { return {{ MBGL_MAKE_ATTRIBUTE_BINDING(SymbolVertex, shader, a_pos), MBGL_MAKE_ATTRIBUTE_BINDING(SymbolVertex, shader, a_offset), diff --git a/src/mbgl/shader/uniforms.hpp b/src/mbgl/shader/uniforms.hpp new file mode 100644 index 0000000000..e0c5a0d361 --- /dev/null +++ b/src/mbgl/shader/uniforms.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include <mbgl/gl/uniform.hpp> +#include <mbgl/util/color.hpp> + +namespace mbgl { +namespace uniforms { + +// Uniforms common to several shaders. + +MBGL_DEFINE_UNIFORM_MATRIX(double, 4, u_matrix); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_opacity); +MBGL_DEFINE_UNIFORM_SCALAR(Color, u_color); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_blur); + +MBGL_DEFINE_UNIFORM_SCALAR(float, u_zoom); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_pitch); +MBGL_DEFINE_UNIFORM_SCALAR(float, u_bearing); + +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_extrude_scale); + +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_pattern_tl_a); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_pattern_br_a); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_pattern_tl_b); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_pattern_br_b); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_pattern_size_a); +MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_pattern_size_b); + +MBGL_DEFINE_UNIFORM_SCALAR(float, u_mix); +MBGL_DEFINE_UNIFORM_SCALAR(gl::TextureUnit, u_image); + +} // namespace uniforms +} // namespace mbgl |