From 71c8f6aa10368084bc0a365b15f004d0629a8b67 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 17 May 2017 18:19:48 -0700 Subject: [core] Move pattern coordinate division by texture size into shader This makes pattern usage more like icons, and will be necessary for data-driven *-pattern properties. --- src/mbgl/programs/fill_extrusion_program.cpp | 2 ++ src/mbgl/programs/fill_extrusion_program.hpp | 2 ++ src/mbgl/programs/fill_program.cpp | 2 ++ src/mbgl/programs/fill_program.hpp | 2 ++ src/mbgl/programs/line_program.cpp | 2 ++ src/mbgl/programs/line_program.hpp | 2 ++ src/mbgl/programs/symbol_program.cpp | 2 +- src/mbgl/programs/symbol_program.hpp | 1 - src/mbgl/programs/uniforms.hpp | 1 + src/mbgl/renderer/painters/painter_background.cpp | 1 + src/mbgl/renderer/painters/painter_fill.cpp | 1 + src/mbgl/renderer/painters/painter_fill_extrusion.cpp | 1 + src/mbgl/renderer/painters/painter_line.cpp | 1 + src/mbgl/shaders/fill_extrusion_pattern.cpp | 5 +++-- src/mbgl/shaders/fill_outline.cpp | 2 +- src/mbgl/shaders/fill_outline_pattern.cpp | 7 ++++--- src/mbgl/shaders/fill_pattern.cpp | 5 +++-- src/mbgl/shaders/line_pattern.cpp | 5 +++-- src/mbgl/sprite/sprite_atlas.cpp | 8 +++----- src/mbgl/sprite/sprite_atlas.hpp | 2 +- 20 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/mbgl/programs/fill_extrusion_program.cpp b/src/mbgl/programs/fill_extrusion_program.cpp index 63d1cbeb59..1b0f483b1a 100644 --- a/src/mbgl/programs/fill_extrusion_program.cpp +++ b/src/mbgl/programs/fill_extrusion_program.cpp @@ -45,6 +45,7 @@ FillExtrusionUniforms::values(mat4 matrix, FillExtrusionPatternUniforms::Values FillExtrusionPatternUniforms::values(mat4 matrix, + Size atlasSize, const SpriteAtlasElement& a, const SpriteAtlasElement& b, const Faded& fading, @@ -66,6 +67,7 @@ FillExtrusionPatternUniforms::values(mat4 matrix, uniforms::u_pattern_size_b::Value{ b.size }, uniforms::u_scale_a::Value{ fading.fromScale }, uniforms::u_scale_b::Value{ fading.toScale }, + uniforms::u_texsize::Value{ atlasSize }, uniforms::u_mix::Value{ fading.t }, uniforms::u_image::Value{ 0 }, uniforms::u_pixel_coord_upper::Value{ std::array{{ float(pixelX >> 16), float(pixelY >> 16) }} }, diff --git a/src/mbgl/programs/fill_extrusion_program.hpp b/src/mbgl/programs/fill_extrusion_program.hpp index 48fca44ee8..7f1c76a6ad 100644 --- a/src/mbgl/programs/fill_extrusion_program.hpp +++ b/src/mbgl/programs/fill_extrusion_program.hpp @@ -55,6 +55,7 @@ struct FillExtrusionPatternUniforms : gl::Uniforms< uniforms::u_pattern_size_b, uniforms::u_scale_a, uniforms::u_scale_b, + uniforms::u_texsize, uniforms::u_mix, uniforms::u_image, uniforms::u_pixel_coord_upper, @@ -66,6 +67,7 @@ struct FillExtrusionPatternUniforms : gl::Uniforms< uniforms::u_lightintensity> { static Values values(mat4, + Size atlasSize, const SpriteAtlasElement&, const SpriteAtlasElement&, const Faded&, diff --git a/src/mbgl/programs/fill_program.cpp b/src/mbgl/programs/fill_program.cpp index 4310f01164..13f7b00e35 100644 --- a/src/mbgl/programs/fill_program.cpp +++ b/src/mbgl/programs/fill_program.cpp @@ -13,6 +13,7 @@ static_assert(sizeof(FillLayoutVertex) == 4, "expected FillLayoutVertex size"); FillPatternUniforms::Values FillPatternUniforms::values(mat4 matrix, Size framebufferSize, + Size atlasSize, const SpriteAtlasElement& a, const SpriteAtlasElement& b, const Faded& fading, @@ -26,6 +27,7 @@ FillPatternUniforms::values(mat4 matrix, return FillPatternUniforms::Values { uniforms::u_matrix::Value{ matrix }, uniforms::u_world::Value{ framebufferSize }, + uniforms::u_texsize::Value{ atlasSize }, uniforms::u_pattern_tl_a::Value{ a.tl }, uniforms::u_pattern_br_a::Value{ a.br }, uniforms::u_pattern_tl_b::Value{ b.tl }, diff --git a/src/mbgl/programs/fill_program.hpp b/src/mbgl/programs/fill_program.hpp index 63751e740a..093485fc7f 100644 --- a/src/mbgl/programs/fill_program.hpp +++ b/src/mbgl/programs/fill_program.hpp @@ -33,6 +33,7 @@ struct FillUniforms : gl::Uniforms< struct FillPatternUniforms : gl::Uniforms< uniforms::u_matrix, uniforms::u_world, + uniforms::u_texsize, uniforms::u_pattern_tl_a, uniforms::u_pattern_br_a, uniforms::u_pattern_tl_b, @@ -49,6 +50,7 @@ struct FillPatternUniforms : gl::Uniforms< { static Values values(mat4 matrix, Size framebufferSize, + Size atlasSize, const SpriteAtlasElement&, const SpriteAtlasElement&, const Faded&, diff --git a/src/mbgl/programs/line_program.cpp b/src/mbgl/programs/line_program.cpp index e7e5c58e47..1e296963f2 100644 --- a/src/mbgl/programs/line_program.cpp +++ b/src/mbgl/programs/line_program.cpp @@ -88,6 +88,7 @@ LinePatternProgram::uniformValues(const LinePaintProperties::PossiblyEvaluated& const RenderTile& tile, const TransformState& state, const std::array& pixelsToGLUnits, + const Size atlasSize, const SpriteAtlasElement& posA, const SpriteAtlasElement& posB) { std::array sizeA {{ @@ -111,6 +112,7 @@ LinePatternProgram::uniformValues(const LinePaintProperties::PossiblyEvaluated& uniforms::u_pattern_br_b::Value{ posB.br }, uniforms::u_pattern_size_a::Value{ sizeA }, uniforms::u_pattern_size_b::Value{ sizeB }, + uniforms::u_texsize::Value{ atlasSize }, uniforms::u_fade::Value{ properties.get().t }, uniforms::u_image::Value{ 0 } ); diff --git a/src/mbgl/programs/line_program.hpp b/src/mbgl/programs/line_program.hpp index 9ad3882e72..6f6ceeb32b 100644 --- a/src/mbgl/programs/line_program.hpp +++ b/src/mbgl/programs/line_program.hpp @@ -112,6 +112,7 @@ class LinePatternProgram : public Program< uniforms::u_pattern_br_b, uniforms::u_pattern_size_a, uniforms::u_pattern_size_b, + uniforms::u_texsize, uniforms::u_fade, uniforms::u_image>, style::LinePaintProperties> @@ -123,6 +124,7 @@ public: const RenderTile&, const TransformState&, const std::array& pixelsToGLUnits, + Size atlasSize, const SpriteAtlasElement& posA, const SpriteAtlasElement& posB); }; diff --git a/src/mbgl/programs/symbol_program.cpp b/src/mbgl/programs/symbol_program.cpp index 1fb4ae5c5e..cdbd6b9713 100644 --- a/src/mbgl/programs/symbol_program.cpp +++ b/src/mbgl/programs/symbol_program.cpp @@ -51,7 +51,7 @@ Values makeValues(const bool isText, values.translateAnchor, state) }, uniforms::u_extrude_scale::Value{ extrudeScale }, - uniforms::u_texsize::Value{ std::array {{ float(texsize.width), float(texsize.height) }} }, + uniforms::u_texsize::Value{ texsize }, uniforms::u_zoom::Value{ float(state.getZoom()) }, uniforms::u_rotate_with_map::Value{ values.rotationAlignment == AlignmentType::Map }, uniforms::u_texture::Value{ 0 }, diff --git a/src/mbgl/programs/symbol_program.hpp b/src/mbgl/programs/symbol_program.hpp index f86b2d97b7..48928988e6 100644 --- a/src/mbgl/programs/symbol_program.hpp +++ b/src/mbgl/programs/symbol_program.hpp @@ -29,7 +29,6 @@ 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); diff --git a/src/mbgl/programs/uniforms.hpp b/src/mbgl/programs/uniforms.hpp index 972405d5a9..a2bfe396b1 100644 --- a/src/mbgl/programs/uniforms.hpp +++ b/src/mbgl/programs/uniforms.hpp @@ -18,6 +18,7 @@ MBGL_DEFINE_UNIFORM_SCALAR(float, u_pitch); MBGL_DEFINE_UNIFORM_SCALAR(float, u_bearing); MBGL_DEFINE_UNIFORM_SCALAR(Size, u_world); +MBGL_DEFINE_UNIFORM_SCALAR(Size, u_texsize); MBGL_DEFINE_UNIFORM_VECTOR(float, 2, u_extrude_scale); diff --git a/src/mbgl/renderer/painters/painter_background.cpp b/src/mbgl/renderer/painters/painter_background.cpp index 783880fe86..9c6df06094 100644 --- a/src/mbgl/renderer/painters/painter_background.cpp +++ b/src/mbgl/renderer/painters/painter_background.cpp @@ -42,6 +42,7 @@ void Painter::renderBackground(PaintParameters& parameters, const RenderBackgrou FillPatternUniforms::values( matrixForTile(tileID), context.viewport.getCurrentValue().size, + spriteAtlas->getSize(), *imagePosA, *imagePosB, background.get(), diff --git a/src/mbgl/renderer/painters/painter_fill.cpp b/src/mbgl/renderer/painters/painter_fill.cpp index 0b77474efe..e34b4107a9 100644 --- a/src/mbgl/renderer/painters/painter_fill.cpp +++ b/src/mbgl/renderer/painters/painter_fill.cpp @@ -49,6 +49,7 @@ void Painter::renderFill(PaintParameters& parameters, properties.get(), state), context.viewport.getCurrentValue().size, + spriteAtlas->getSize(), *imagePosA, *imagePosB, properties.get(), diff --git a/src/mbgl/renderer/painters/painter_fill_extrusion.cpp b/src/mbgl/renderer/painters/painter_fill_extrusion.cpp index 8e10cd7369..c7aede3aa6 100644 --- a/src/mbgl/renderer/painters/painter_fill_extrusion.cpp +++ b/src/mbgl/renderer/painters/painter_fill_extrusion.cpp @@ -46,6 +46,7 @@ void Painter::renderFillExtrusion(PaintParameters& parameters, tile.translatedClipMatrix(properties.get(), properties.get(), state), + spriteAtlas->getSize(), *imagePosA, *imagePosB, properties.get(), diff --git a/src/mbgl/renderer/painters/painter_line.cpp b/src/mbgl/renderer/painters/painter_line.cpp index dc99ef9aff..df627a76aa 100644 --- a/src/mbgl/renderer/painters/painter_line.cpp +++ b/src/mbgl/renderer/painters/painter_line.cpp @@ -75,6 +75,7 @@ void Painter::renderLine(PaintParameters& parameters, tile, state, pixelsToGLUnits, + spriteAtlas->getSize(), *posA, *posB)); diff --git a/src/mbgl/shaders/fill_extrusion_pattern.cpp b/src/mbgl/shaders/fill_extrusion_pattern.cpp index e2de5c20b2..49be73b713 100644 --- a/src/mbgl/shaders/fill_extrusion_pattern.cpp +++ b/src/mbgl/shaders/fill_extrusion_pattern.cpp @@ -73,6 +73,7 @@ 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 vec2 u_texsize; uniform float u_mix; uniform sampler2D u_image; @@ -89,11 +90,11 @@ void main() { vec2 imagecoord = mod(v_pos_a, 1.0); - vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord); + vec2 pos = mix(u_pattern_tl_a / u_texsize, u_pattern_br_a / u_texsize, 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); + vec2 pos2 = mix(u_pattern_tl_b / u_texsize, u_pattern_br_b / u_texsize, imagecoord_b); vec4 color2 = texture2D(u_image, pos2); vec4 mixedColor = mix(color1, color2, u_mix); diff --git a/src/mbgl/shaders/fill_outline.cpp b/src/mbgl/shaders/fill_outline.cpp index 3e160ac6c2..91b5db49ba 100644 --- a/src/mbgl/shaders/fill_outline.cpp +++ b/src/mbgl/shaders/fill_outline.cpp @@ -41,7 +41,7 @@ void main() { float dist = length(v_pos - gl_FragCoord.xy); - float alpha = smoothstep(1.0, 0.0, dist); + float alpha = 1.0 - smoothstep(0.0, 1.0, dist); gl_FragColor = outline_color * (alpha * opacity); #ifdef OVERDRAW_INSPECTOR diff --git a/src/mbgl/shaders/fill_outline_pattern.cpp b/src/mbgl/shaders/fill_outline_pattern.cpp index 5e38023382..6ee1f4c696 100644 --- a/src/mbgl/shaders/fill_outline_pattern.cpp +++ b/src/mbgl/shaders/fill_outline_pattern.cpp @@ -44,6 +44,7 @@ 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 vec2 u_texsize; uniform float u_mix; uniform sampler2D u_image; @@ -58,17 +59,17 @@ void main() { vec2 imagecoord = mod(v_pos_a, 1.0); - vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord); + vec2 pos = mix(u_pattern_tl_a / u_texsize, u_pattern_br_a / u_texsize, 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); + vec2 pos2 = mix(u_pattern_tl_b / u_texsize, u_pattern_br_b / u_texsize, 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); + float alpha = 1.0 - smoothstep(0.0, 1.0, dist); gl_FragColor = mix(color1, color2, u_mix) * alpha * opacity; diff --git a/src/mbgl/shaders/fill_pattern.cpp b/src/mbgl/shaders/fill_pattern.cpp index 0357fed40e..a81339bb25 100644 --- a/src/mbgl/shaders/fill_pattern.cpp +++ b/src/mbgl/shaders/fill_pattern.cpp @@ -40,6 +40,7 @@ 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 vec2 u_texsize; uniform float u_mix; uniform sampler2D u_image; @@ -53,11 +54,11 @@ void main() { vec2 imagecoord = mod(v_pos_a, 1.0); - vec2 pos = mix(u_pattern_tl_a, u_pattern_br_a, imagecoord); + vec2 pos = mix(u_pattern_tl_a / u_texsize, u_pattern_br_a / u_texsize, 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); + vec2 pos2 = mix(u_pattern_tl_b / u_texsize, u_pattern_br_b / u_texsize, imagecoord_b); vec4 color2 = texture2D(u_image, pos2); gl_FragColor = mix(color1, color2, u_mix) * opacity; diff --git a/src/mbgl/shaders/line_pattern.cpp b/src/mbgl/shaders/line_pattern.cpp index f52a8e2157..6eb9d67700 100644 --- a/src/mbgl/shaders/line_pattern.cpp +++ b/src/mbgl/shaders/line_pattern.cpp @@ -111,6 +111,7 @@ 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 vec2 u_texsize; uniform float u_fade; uniform sampler2D u_image; @@ -140,8 +141,8 @@ void main() { 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)); + vec2 pos_a = mix(u_pattern_tl_a / u_texsize, u_pattern_br_a / u_texsize, vec2(x_a, y_a)); + vec2 pos_b = mix(u_pattern_tl_b / u_texsize, u_pattern_br_b / u_texsize, vec2(x_b, y_b)); vec4 color = mix(texture2D(u_image, pos_a), texture2D(u_image, pos_b), u_fade); diff --git a/src/mbgl/sprite/sprite_atlas.cpp b/src/mbgl/sprite/sprite_atlas.cpp index cc69bccdbe..bcf35050ab 100644 --- a/src/mbgl/sprite/sprite_atlas.cpp +++ b/src/mbgl/sprite/sprite_atlas.cpp @@ -18,7 +18,7 @@ static constexpr uint32_t padding = 1; SpriteAtlasElement::SpriteAtlasElement(Rect rect_, const style::Image::Impl& image, - Size size_, float pixelRatio) + float pixelRatio) : pos(std::move(rect_)), sdf(image.sdf), relativePixelRatio(image.pixelRatio / pixelRatio), @@ -28,8 +28,8 @@ SpriteAtlasElement::SpriteAtlasElement(Rect rect_, const float w = image.image.size.width / pixelRatio; const float h = image.image.size.height / pixelRatio; - tl = {{ float(pos.x + padding) / size_.width, float(pos.y + padding) / size_.height }}; - br = {{ float(pos.x + padding + w) / size_.width, float(pos.y + padding + h) / size_.height }}; + tl = {{ float(pos.x + padding), float(pos.y + padding) }}; + br = {{ float(pos.x + padding + w), float(pos.y + padding + h) }}; } SpriteAtlas::SpriteAtlas(Size size_, float pixelRatio_) @@ -142,7 +142,6 @@ optional SpriteAtlas::getImage(const std::string& id, return SpriteAtlasElement { *(entry.*entryRect), *entry.image, - size, pixelRatio }; } @@ -164,7 +163,6 @@ optional SpriteAtlas::getImage(const std::string& id, return SpriteAtlasElement { rect, *entry.image, - size, pixelRatio }; } diff --git a/src/mbgl/sprite/sprite_atlas.hpp b/src/mbgl/sprite/sprite_atlas.hpp index 832922df5a..52749d389b 100644 --- a/src/mbgl/sprite/sprite_atlas.hpp +++ b/src/mbgl/sprite/sprite_atlas.hpp @@ -20,7 +20,7 @@ class Context; class SpriteAtlasElement { public: - SpriteAtlasElement(Rect, const style::Image::Impl&, Size size, float pixelRatio); + SpriteAtlasElement(Rect, const style::Image::Impl&, float pixelRatio); Rect pos; bool sdf; -- cgit v1.2.1