From 9fbcb031f019048f21fdfcb57b80f4451cdecfd9 Mon Sep 17 00:00:00 2001 From: Alex Cristici Date: Tue, 12 Dec 2023 19:32:20 +0200 Subject: [PATCH] Line layer UBO improvements (#1946) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/mbgl/shaders/gl/drawable_line.hpp | 23 ++++---- .../shaders/gl/drawable_line_gradient.hpp | 27 +++++----- .../mbgl/shaders/gl/drawable_line_pattern.hpp | 13 ++--- include/mbgl/shaders/gl/drawable_line_sdf.hpp | 20 +++---- include/mbgl/shaders/line_layer_ubo.hpp | 23 +++++--- include/mbgl/shaders/mtl/common.hpp | 11 ++-- include/mbgl/shaders/mtl/line.hpp | 54 +++++++++---------- include/mbgl/shaders/mtl/line_gradient.hpp | 15 +++--- shaders/drawable.line.fragment.glsl | 9 ++-- shaders/drawable.line.vertex.glsl | 14 +++-- shaders/drawable.line_gradient.fragment.glsl | 11 ++-- shaders/drawable.line_gradient.vertex.glsl | 16 +++--- shaders/drawable.line_pattern.fragment.glsl | 4 -- shaders/drawable.line_pattern.vertex.glsl | 9 ++-- shaders/drawable.line_sdf.fragment.glsl | 7 ++- shaders/drawable.line_sdf.vertex.glsl | 13 +++-- .../renderer/layers/line_layer_tweaker.cpp | 39 +++++++++----- .../renderer/layers/line_layer_tweaker.hpp | 1 + .../renderer/sources/render_tile_source.cpp | 15 ++++-- src/mbgl/shaders/mtl/line.cpp | 29 +++++----- src/mbgl/shaders/mtl/line_gradient.cpp | 9 ++-- .../style/layers/custom_drawable_layer.cpp | 15 ++++-- 22 files changed, 214 insertions(+), 163 deletions(-) diff --git a/include/mbgl/shaders/gl/drawable_line.hpp b/include/mbgl/shaders/gl/drawable_line.hpp index 6645eded1..e5dcdabe4 100644 --- a/include/mbgl/shaders/gl/drawable_line.hpp +++ b/include/mbgl/shaders/gl/drawable_line.hpp @@ -19,11 +19,15 @@ struct ShaderSource { layout (location = 0) in vec2 a_pos_normal; layout (location = 1) in vec4 a_data; +layout (std140) uniform LineDynamicUBO { + highp vec2 u_units_to_pixels; + lowp float pad0, pad1; +}; + layout (std140) uniform LineUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; mediump float u_ratio; - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LinePropertiesUBO { @@ -34,8 +38,8 @@ layout (std140) uniform LinePropertiesUBO { lowp float u_offset; mediump float u_width; - highp float pad1; - highp vec2 pad2; + highp float pad5; + highp vec2 pad6; }; layout (std140) uniform LineInterpolationUBO { @@ -46,7 +50,7 @@ layout (std140) uniform LineInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; - highp vec2 pad3; + highp vec2 pad7; }; out vec2 v_normal; @@ -160,9 +164,8 @@ mediump float width = u_width; )"; static constexpr const char* fragment = R"(layout (std140) uniform LineUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; mediump float u_ratio; - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LinePropertiesUBO { @@ -173,8 +176,8 @@ layout (std140) uniform LinePropertiesUBO { lowp float u_offset; mediump float u_width; - highp float pad1; - highp vec2 pad2; + highp float pad5; + highp vec2 pad6; }; layout (std140) uniform LineInterpolationUBO { @@ -185,7 +188,7 @@ layout (std140) uniform LineInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; - highp vec2 pad3; + highp vec2 pad7; }; in vec2 v_width2; diff --git a/include/mbgl/shaders/gl/drawable_line_gradient.hpp b/include/mbgl/shaders/gl/drawable_line_gradient.hpp index a41f945e4..6ba9b50e1 100644 --- a/include/mbgl/shaders/gl/drawable_line_gradient.hpp +++ b/include/mbgl/shaders/gl/drawable_line_gradient.hpp @@ -23,11 +23,15 @@ struct ShaderSource { layout (location = 0) in vec2 a_pos_normal; layout (location = 1) in vec4 a_data; +layout (std140) uniform LineDynamicUBO { + highp vec2 u_units_to_pixels; + lowp float pad0, pad1; +}; + layout (std140) uniform LineGradientUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; mediump float u_ratio; - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LineGradientPropertiesUBO { @@ -37,8 +41,8 @@ layout (std140) uniform LineGradientPropertiesUBO { lowp float u_offset; mediump float u_width; - highp float pad1; - highp vec2 pad2; + highp float pad5; + highp vec2 pad6; }; layout (std140) uniform LineGradientInterpolationUBO { @@ -48,8 +52,8 @@ layout (std140) uniform LineGradientInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; - highp float pad3; - highp vec2 pad4; + highp float pad7; + highp vec2 pad8; }; out vec2 v_normal; @@ -154,9 +158,8 @@ mediump float width = u_width; )"; static constexpr const char* fragment = R"(layout (std140) uniform LineGradientUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; mediump float u_ratio; - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LineGradientPropertiesUBO { @@ -166,8 +169,8 @@ layout (std140) uniform LineGradientPropertiesUBO { lowp float u_offset; mediump float u_width; - highp float pad1; - highp vec2 pad2; + highp float pad5; + highp vec2 pad6; }; layout (std140) uniform LineGradientInterpolationUBO { @@ -177,8 +180,8 @@ layout (std140) uniform LineGradientInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; - highp float pad3; - highp vec2 pad4; + highp float pad7; + highp vec2 pad8; }; uniform sampler2D u_image; diff --git a/include/mbgl/shaders/gl/drawable_line_pattern.hpp b/include/mbgl/shaders/gl/drawable_line_pattern.hpp index 6fe186c5b..d2fdbe3a9 100644 --- a/include/mbgl/shaders/gl/drawable_line_pattern.hpp +++ b/include/mbgl/shaders/gl/drawable_line_pattern.hpp @@ -23,16 +23,17 @@ struct ShaderSource { layout (location = 0) in vec2 a_pos_normal; layout (location = 1) in vec4 a_data; +layout (std140) uniform LineDynamicUBO { + highp vec2 u_units_to_pixels; + lowp float pad0, pad1; +}; + layout (std140) uniform LinePatternUBO { highp mat4 u_matrix; mediump vec4 u_scale; highp vec2 u_texsize; - highp vec2 u_units_to_pixels; mediump float u_ratio; highp float u_fade; - - lowp float pad0; - highp float pad1; }; layout (std140) uniform LinePatternPropertiesUBO { @@ -185,12 +186,8 @@ mediump vec4 pattern_to = u_pattern_to; highp mat4 u_matrix; mediump vec4 u_scale; highp vec2 u_texsize; - highp vec2 u_units_to_pixels; mediump float u_ratio; highp float u_fade; - - lowp float pad0; - highp float pad1; }; layout (std140) uniform LinePatternPropertiesUBO { diff --git a/include/mbgl/shaders/gl/drawable_line_sdf.hpp b/include/mbgl/shaders/gl/drawable_line_sdf.hpp index eecdcd5cc..52d39174d 100644 --- a/include/mbgl/shaders/gl/drawable_line_sdf.hpp +++ b/include/mbgl/shaders/gl/drawable_line_sdf.hpp @@ -23,9 +23,13 @@ struct ShaderSource { layout (location = 0) in vec2 a_pos_normal; layout (location = 1) in vec4 a_data; +layout (std140) uniform LineDynamicUBO { + highp vec2 u_units_to_pixels; + lowp float pad0, pad1; +}; + layout (std140) uniform LineSDFUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; highp vec2 u_patternscale_a; highp vec2 u_patternscale_b; mediump float u_ratio; @@ -33,8 +37,7 @@ layout (std140) uniform LineSDFUBO { highp float u_tex_y_b; highp float u_sdfgamma; highp float u_mix; - - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LineSDFPropertiesUBO { @@ -46,7 +49,7 @@ layout (std140) uniform LineSDFPropertiesUBO { mediump float u_width; lowp float u_floorwidth; - highp vec2 pad1; + highp vec2 pad5; }; layout (std140) uniform LineSDFInterpolationUBO { @@ -58,7 +61,7 @@ layout (std140) uniform LineSDFInterpolationUBO { lowp float u_width_t; lowp float u_floorwidth_t; - highp float pad2; + highp float pad6; }; out vec2 v_normal; @@ -186,7 +189,6 @@ lowp float floorwidth = u_floorwidth; static constexpr const char* fragment = R"( layout (std140) uniform LineSDFUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; highp vec2 u_patternscale_a; highp vec2 u_patternscale_b; mediump float u_ratio; @@ -195,7 +197,7 @@ layout (std140) uniform LineSDFUBO { highp float u_sdfgamma; highp float u_mix; - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LineSDFPropertiesUBO { @@ -207,7 +209,7 @@ layout (std140) uniform LineSDFPropertiesUBO { mediump float u_width; lowp float u_floorwidth; - highp vec2 pad1; + highp vec2 pad5; }; layout (std140) uniform LineSDFInterpolationUBO { @@ -219,7 +221,7 @@ layout (std140) uniform LineSDFInterpolationUBO { lowp float u_width_t; lowp float u_floorwidth_t; - highp float pad2; + highp float pad6; }; uniform sampler2D u_image; diff --git a/include/mbgl/shaders/line_layer_ubo.hpp b/include/mbgl/shaders/line_layer_ubo.hpp index 43859adf7..0b20b6d6e 100644 --- a/include/mbgl/shaders/line_layer_ubo.hpp +++ b/include/mbgl/shaders/line_layer_ubo.hpp @@ -5,11 +5,17 @@ namespace mbgl { namespace shaders { +struct alignas(16) LineDynamicUBO { + /* 0 */ std::array units_to_pixels; + /* 8 */ float pad1, pad2; + /* 16 */ +}; +static_assert(sizeof(LineDynamicUBO) == 16); + struct alignas(16) LineUBO { std::array matrix; - std::array units_to_pixels; float ratio; - float pad; + float pad1, pad2, pad3; }; static_assert(sizeof(LineUBO) % 16 == 0); @@ -40,10 +46,8 @@ struct alignas(16) LinePatternUBO { std::array matrix; std::array scale; std::array texsize; - std::array units_to_pixels; float ratio; float fade; - float pad1, pad2; }; static_assert(sizeof(LinePatternUBO) % 16 == 0); @@ -59,7 +63,6 @@ static_assert(sizeof(LinePatternPropertiesUBO) % 16 == 0); struct alignas(16) LineSDFUBO { std::array matrix; - std::array units_to_pixels; std::array patternscale_a; std::array patternscale_b; float ratio; @@ -67,7 +70,7 @@ struct alignas(16) LineSDFUBO { float tex_y_b; float sdfgamma; float mix; - float pad; + float pad1, pad2, pad3; }; static_assert(sizeof(LineSDFUBO) % 16 == 0); @@ -83,7 +86,13 @@ struct alignas(16) LineSDFPropertiesUBO { }; static_assert(sizeof(LineSDFPropertiesUBO) % 16 == 0); -using LineBasicUBO = LineUBO; +struct alignas(16) LineBasicUBO { + std::array matrix; + std::array units_to_pixels; + float ratio; + float pad; +}; +static_assert(sizeof(LineBasicUBO) % 16 == 0); struct alignas(16) LineBasicPropertiesUBO { Color color; diff --git a/include/mbgl/shaders/mtl/common.hpp b/include/mbgl/shaders/mtl/common.hpp index da318e2cf..0f33b5b61 100644 --- a/include/mbgl/shaders/mtl/common.hpp +++ b/include/mbgl/shaders/mtl/common.hpp @@ -54,11 +54,15 @@ float4 unpack_mix_color(const float4 packedColors, const float t) { decode_color(float2(packedColors[2], packedColors[3])), t); } +struct alignas(16) LineDynamicUBO { + float2 units_to_pixels; + float pad1, pad2; +}; + struct alignas(16) LineUBO { float4x4 matrix; - float2 units_to_pixels; float ratio; - float pad; + float pad1, pad2, pad3; }; struct alignas(16) LineBasicUBO { @@ -70,9 +74,8 @@ struct alignas(16) LineBasicUBO { struct alignas(16) LineGradientUBO { float4x4 matrix; - float2 units_to_pixels; float ratio; - float pad; + float pad1, pad2, pad3; }; struct alignas(16) LinePropertiesUBO { diff --git a/include/mbgl/shaders/mtl/line.hpp b/include/mbgl/shaders/mtl/line.hpp index c96569cb7..cbc63e1e3 100644 --- a/include/mbgl/shaders/mtl/line.hpp +++ b/include/mbgl/shaders/mtl/line.hpp @@ -15,7 +15,7 @@ struct ShaderSource { static constexpr auto fragmentMainFunction = "fragmentMain"; static const std::array attributes; - static const std::array uniforms; + static const std::array uniforms; static const std::array textures; static constexpr auto source = R"( @@ -61,9 +61,10 @@ struct FragmentStage { }; FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], - device const LineUBO& line [[buffer(8)]], - device const LinePropertiesUBO& props [[buffer(9)]], - device const LineInterpolationUBO& interp [[buffer(10)]]) { + device const LineDynamicUBO& dynamic [[buffer(8)]], + device const LineUBO& line [[buffer(9)]], + device const LinePropertiesUBO& props [[buffer(10)]], + device const LineInterpolationUBO& interp [[buffer(11)]]) { #if defined(HAS_UNIFORM_u_gapwidth) const auto gapwidth = props.gapwidth / 2; @@ -115,7 +116,7 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], // calculate how much the perspective view squishes or stretches the extrude const float extrude_length_without_perspective = length(dist); - const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * line.units_to_pixels); + const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * dynamic.units_to_pixels); return { .position = position, @@ -136,8 +137,8 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], } half4 fragment fragmentMain(FragmentStage in [[stage_in]], - device const LineUBO& line [[buffer(8)]], - device const LinePropertiesUBO& props [[buffer(9)]]) { + device const LineUBO& line [[buffer(9)]], + device const LinePropertiesUBO& props [[buffer(10)]]) { #if defined(OVERDRAW_INSPECTOR) return half4(1.0); #endif @@ -178,7 +179,7 @@ struct ShaderSource { static constexpr auto fragmentMainFunction = "fragmentMain"; static const std::array attributes; - static const std::array uniforms; + static const std::array uniforms; static const std::array textures; static constexpr auto source = R"( @@ -234,10 +235,8 @@ struct alignas(16) LinePatternUBO { float4x4 matrix; float4 scale; float2 texsize; - float2 units_to_pixels; float ratio; float fade; - float pad1, pad2; }; struct alignas(16) LinePatternPropertiesUBO { @@ -266,10 +265,11 @@ struct alignas(16) LinePatternTilePropertiesUBO { }; FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], - device const LinePatternUBO& line [[buffer(9)]], - device const LinePatternPropertiesUBO& props [[buffer(10)]], - device const LinePatternInterpolationUBO& interp [[buffer(11)]], - device const LinePatternTilePropertiesUBO& tileProps [[buffer(12)]]) { + device const LineDynamicUBO& dynamic [[buffer(9)]], + device const LinePatternUBO& line [[buffer(10)]], + device const LinePatternPropertiesUBO& props [[buffer(11)]], + device const LinePatternInterpolationUBO& interp [[buffer(12)]], + device const LinePatternTilePropertiesUBO& tileProps [[buffer(13)]]) { #if defined(HAS_UNIFORM_u_gapwidth) const auto gapwidth = props.gapwidth / 2; @@ -323,7 +323,7 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], // calculate how much the perspective view squishes or stretches the extrude const float extrude_length_without_perspective = length(dist); - const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * line.units_to_pixels); + const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * dynamic.units_to_pixels); return { .position = position, @@ -348,9 +348,9 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], } half4 fragment fragmentMain(FragmentStage in [[stage_in]], - device const LinePatternUBO& line [[buffer(9)]], - device const LinePatternPropertiesUBO& props [[buffer(10)]], - device const LinePatternTilePropertiesUBO& tileProps [[buffer(12)]], + device const LinePatternUBO& line [[buffer(10)]], + device const LinePatternPropertiesUBO& props [[buffer(11)]], + device const LinePatternTilePropertiesUBO& tileProps [[buffer(13)]], texture2d image0 [[texture(0)]], sampler image0_sampler [[sampler(0)]]) { #if defined(OVERDRAW_INSPECTOR) @@ -430,7 +430,7 @@ struct ShaderSource { static constexpr auto fragmentMainFunction = "fragmentMain"; static const std::array attributes; - static const std::array uniforms; + static const std::array uniforms; static const std::array textures; static constexpr auto source = R"( @@ -485,7 +485,6 @@ struct FragmentStage { struct alignas(16) LineSDFUBO { float4x4 matrix; - float2 units_to_pixels; float2 patternscale_a; float2 patternscale_b; float ratio; @@ -493,7 +492,7 @@ struct alignas(16) LineSDFUBO { float tex_y_b; float sdfgamma; float mix; - float pad; + float pad1, pad2, pad3; }; struct alignas(16) LineSDFPropertiesUBO { @@ -519,9 +518,10 @@ struct alignas(16) LineSDFInterpolationUBO { }; FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], - device const LineSDFUBO& line [[buffer(9)]], - device const LineSDFPropertiesUBO& props [[buffer(10)]], - device const LineSDFInterpolationUBO& interp [[buffer(11)]]) { + device const LineDynamicUBO& dynamic [[buffer(9)]], + device const LineSDFUBO& line [[buffer(10)]], + device const LineSDFPropertiesUBO& props [[buffer(11)]], + device const LineSDFInterpolationUBO& interp [[buffer(12)]]) { #if defined(HAS_UNIFORM_u_gapwidth) const auto gapwidth = props.gapwidth / 2; @@ -580,7 +580,7 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], // calculate how much the perspective view squishes or stretches the extrude const float extrude_length_without_perspective = length(dist); - const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * line.units_to_pixels); + const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * dynamic.units_to_pixels); return { .position = position, @@ -606,8 +606,8 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], } half4 fragment fragmentMain(FragmentStage in [[stage_in]], - device const LineSDFUBO& line [[buffer(9)]], - device const LineSDFPropertiesUBO& props [[buffer(10)]], + device const LineSDFUBO& line [[buffer(10)]], + device const LineSDFPropertiesUBO& props [[buffer(11)]], texture2d image0 [[texture(0)]], sampler image0_sampler [[sampler(0)]]) { #if defined(OVERDRAW_INSPECTOR) diff --git a/include/mbgl/shaders/mtl/line_gradient.hpp b/include/mbgl/shaders/mtl/line_gradient.hpp index b8cf813fc..28653583b 100644 --- a/include/mbgl/shaders/mtl/line_gradient.hpp +++ b/include/mbgl/shaders/mtl/line_gradient.hpp @@ -15,7 +15,7 @@ struct ShaderSource { static constexpr auto fragmentMainFunction = "fragmentMain"; static const std::array attributes; - static const std::array uniforms; + static const std::array uniforms; static const std::array textures; static constexpr auto source = R"( @@ -45,9 +45,10 @@ struct FragmentStage { }; FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], - device const LineGradientUBO& line [[buffer(7)]], - device const LineGradientPropertiesUBO& props [[buffer(8)]], - device const LineGradientInterpolationUBO& interp [[buffer(9)]]) { + device const LineDynamicUBO& dynamic [[buffer(7)]], + device const LineGradientUBO& line [[buffer(8)]], + device const LineGradientPropertiesUBO& props [[buffer(9)]], + device const LineGradientInterpolationUBO& interp [[buffer(10)]]) { #if !defined(HAS_UNIFORM_u_blur) const auto blur = unpack_mix_float(vertx.blur, interp.blur_t); @@ -106,7 +107,7 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], // calculate how much the perspective view squishes or stretches the extrude const float extrude_length_without_perspective = length(dist); - const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * line.units_to_pixels); + const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * dynamic.units_to_pixels); return { .position = position, @@ -125,8 +126,8 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], } half4 fragment fragmentMain(FragmentStage in [[stage_in]], - device const LineGradientUBO& line [[buffer(7)]], - device const LineGradientPropertiesUBO& props [[buffer(8)]], + device const LineGradientUBO& line [[buffer(8)]], + device const LineGradientPropertiesUBO& props [[buffer(9)]], texture2d gradientTexture [[texture(0)]]) { #if defined(OVERDRAW_INSPECTOR) return half4(1.0); diff --git a/shaders/drawable.line.fragment.glsl b/shaders/drawable.line.fragment.glsl index a8e354c96..bb7e9dbb3 100644 --- a/shaders/drawable.line.fragment.glsl +++ b/shaders/drawable.line.fragment.glsl @@ -1,8 +1,7 @@ layout (std140) uniform LineUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; mediump float u_ratio; - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LinePropertiesUBO { @@ -13,8 +12,8 @@ layout (std140) uniform LinePropertiesUBO { lowp float u_offset; mediump float u_width; - highp float pad1; - highp vec2 pad2; + highp float pad5; + highp vec2 pad6; }; layout (std140) uniform LineInterpolationUBO { @@ -25,7 +24,7 @@ layout (std140) uniform LineInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; - highp vec2 pad3; + highp vec2 pad7; }; in vec2 v_width2; diff --git a/shaders/drawable.line.vertex.glsl b/shaders/drawable.line.vertex.glsl index 169564ab8..b44a9bd3b 100644 --- a/shaders/drawable.line.vertex.glsl +++ b/shaders/drawable.line.vertex.glsl @@ -9,11 +9,15 @@ layout (location = 0) in vec2 a_pos_normal; layout (location = 1) in vec4 a_data; +layout (std140) uniform LineDynamicUBO { + highp vec2 u_units_to_pixels; + lowp float pad0, pad1; +}; + layout (std140) uniform LineUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; mediump float u_ratio; - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LinePropertiesUBO { @@ -24,8 +28,8 @@ layout (std140) uniform LinePropertiesUBO { lowp float u_offset; mediump float u_width; - highp float pad1; - highp vec2 pad2; + highp float pad5; + highp vec2 pad6; }; layout (std140) uniform LineInterpolationUBO { @@ -36,7 +40,7 @@ layout (std140) uniform LineInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; - highp vec2 pad3; + highp vec2 pad7; }; out vec2 v_normal; diff --git a/shaders/drawable.line_gradient.fragment.glsl b/shaders/drawable.line_gradient.fragment.glsl index 4e3c6856d..03b306401 100644 --- a/shaders/drawable.line_gradient.fragment.glsl +++ b/shaders/drawable.line_gradient.fragment.glsl @@ -1,8 +1,7 @@ layout (std140) uniform LineGradientUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; mediump float u_ratio; - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LineGradientPropertiesUBO { @@ -12,8 +11,8 @@ layout (std140) uniform LineGradientPropertiesUBO { lowp float u_offset; mediump float u_width; - highp float pad1; - highp vec2 pad2; + highp float pad5; + highp vec2 pad6; }; layout (std140) uniform LineGradientInterpolationUBO { @@ -23,8 +22,8 @@ layout (std140) uniform LineGradientInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; - highp float pad3; - highp vec2 pad4; + highp float pad7; + highp vec2 pad8; }; uniform sampler2D u_image; diff --git a/shaders/drawable.line_gradient.vertex.glsl b/shaders/drawable.line_gradient.vertex.glsl index e41ca7901..5e212cd59 100644 --- a/shaders/drawable.line_gradient.vertex.glsl +++ b/shaders/drawable.line_gradient.vertex.glsl @@ -13,11 +13,15 @@ layout (location = 0) in vec2 a_pos_normal; layout (location = 1) in vec4 a_data; +layout (std140) uniform LineDynamicUBO { + highp vec2 u_units_to_pixels; + lowp float pad0, pad1; +}; + layout (std140) uniform LineGradientUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; mediump float u_ratio; - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LineGradientPropertiesUBO { @@ -27,8 +31,8 @@ layout (std140) uniform LineGradientPropertiesUBO { lowp float u_offset; mediump float u_width; - highp float pad1; - highp vec2 pad2; + highp float pad5; + highp vec2 pad6; }; layout (std140) uniform LineGradientInterpolationUBO { @@ -38,8 +42,8 @@ layout (std140) uniform LineGradientInterpolationUBO { lowp float u_offset_t; lowp float u_width_t; - highp float pad3; - highp vec2 pad4; + highp float pad7; + highp vec2 pad8; }; out vec2 v_normal; diff --git a/shaders/drawable.line_pattern.fragment.glsl b/shaders/drawable.line_pattern.fragment.glsl index 30b5a7c0c..e049d70a4 100644 --- a/shaders/drawable.line_pattern.fragment.glsl +++ b/shaders/drawable.line_pattern.fragment.glsl @@ -2,12 +2,8 @@ layout (std140) uniform LinePatternUBO { highp mat4 u_matrix; mediump vec4 u_scale; highp vec2 u_texsize; - highp vec2 u_units_to_pixels; mediump float u_ratio; highp float u_fade; - - lowp float pad0; - highp float pad1; }; layout (std140) uniform LinePatternPropertiesUBO { diff --git a/shaders/drawable.line_pattern.vertex.glsl b/shaders/drawable.line_pattern.vertex.glsl index 600e8f3af..e69bdd4bc 100644 --- a/shaders/drawable.line_pattern.vertex.glsl +++ b/shaders/drawable.line_pattern.vertex.glsl @@ -13,16 +13,17 @@ layout (location = 0) in vec2 a_pos_normal; layout (location = 1) in vec4 a_data; +layout (std140) uniform LineDynamicUBO { + highp vec2 u_units_to_pixels; + lowp float pad0, pad1; +}; + layout (std140) uniform LinePatternUBO { highp mat4 u_matrix; mediump vec4 u_scale; highp vec2 u_texsize; - highp vec2 u_units_to_pixels; mediump float u_ratio; highp float u_fade; - - lowp float pad0; - highp float pad1; }; layout (std140) uniform LinePatternPropertiesUBO { diff --git a/shaders/drawable.line_sdf.fragment.glsl b/shaders/drawable.line_sdf.fragment.glsl index 64b2c406d..140b0c472 100644 --- a/shaders/drawable.line_sdf.fragment.glsl +++ b/shaders/drawable.line_sdf.fragment.glsl @@ -1,7 +1,6 @@ layout (std140) uniform LineSDFUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; highp vec2 u_patternscale_a; highp vec2 u_patternscale_b; mediump float u_ratio; @@ -10,7 +9,7 @@ layout (std140) uniform LineSDFUBO { highp float u_sdfgamma; highp float u_mix; - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LineSDFPropertiesUBO { @@ -22,7 +21,7 @@ layout (std140) uniform LineSDFPropertiesUBO { mediump float u_width; lowp float u_floorwidth; - highp vec2 pad1; + highp vec2 pad5; }; layout (std140) uniform LineSDFInterpolationUBO { @@ -34,7 +33,7 @@ layout (std140) uniform LineSDFInterpolationUBO { lowp float u_width_t; lowp float u_floorwidth_t; - highp float pad2; + highp float pad6; }; uniform sampler2D u_image; diff --git a/shaders/drawable.line_sdf.vertex.glsl b/shaders/drawable.line_sdf.vertex.glsl index cc41cf61b..7008e79e2 100644 --- a/shaders/drawable.line_sdf.vertex.glsl +++ b/shaders/drawable.line_sdf.vertex.glsl @@ -13,9 +13,13 @@ layout (location = 0) in vec2 a_pos_normal; layout (location = 1) in vec4 a_data; +layout (std140) uniform LineDynamicUBO { + highp vec2 u_units_to_pixels; + lowp float pad0, pad1; +}; + layout (std140) uniform LineSDFUBO { highp mat4 u_matrix; - highp vec2 u_units_to_pixels; highp vec2 u_patternscale_a; highp vec2 u_patternscale_b; mediump float u_ratio; @@ -23,8 +27,7 @@ layout (std140) uniform LineSDFUBO { highp float u_tex_y_b; highp float u_sdfgamma; highp float u_mix; - - lowp float pad0; + lowp float pad2, pad3, pad4; }; layout (std140) uniform LineSDFPropertiesUBO { @@ -36,7 +39,7 @@ layout (std140) uniform LineSDFPropertiesUBO { mediump float u_width; lowp float u_floorwidth; - highp vec2 pad1; + highp vec2 pad5; }; layout (std140) uniform LineSDFInterpolationUBO { @@ -48,7 +51,7 @@ layout (std140) uniform LineSDFInterpolationUBO { lowp float u_width_t; lowp float u_floorwidth_t; - highp float pad2; + highp float pad6; }; out vec2 v_normal; diff --git a/src/mbgl/renderer/layers/line_layer_tweaker.cpp b/src/mbgl/renderer/layers/line_layer_tweaker.cpp index 4f46e255e..bcb964300 100644 --- a/src/mbgl/renderer/layers/line_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/line_layer_tweaker.cpp @@ -33,6 +33,7 @@ static const StringIdentity idLinePatternUBOName = stringIndexer().get("LinePatt static const StringIdentity idLinePatternPropertiesUBOName = stringIndexer().get("LinePatternPropertiesUBO"); static const StringIdentity idLineSDFUBOName = stringIndexer().get("LineSDFUBO"); static const StringIdentity idLineSDFPropertiesUBOName = stringIndexer().get("LineSDFPropertiesUBO"); +static const StringIdentity idLineDynamicUBOName = stringIndexer().get("LineDynamicUBO"); static const StringIdentity idTexImageName = stringIndexer().get("u_image"); void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) { @@ -107,6 +108,15 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters return lineSDFPropertiesBuffer; }; + const LineDynamicUBO dynamicUBO = { + /*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, 0, 0}; + + if (!dynamicBuffer) { + dynamicBuffer = parameters.context.createUniformBuffer(&dynamicUBO, sizeof(dynamicUBO)); + } else { + dynamicBuffer->update(&dynamicUBO, sizeof(dynamicUBO)); + } + visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) { const auto shader = drawable.getShader(); if (!drawable.getTileID() || !shader || !checkTweakDrawable(drawable)) { @@ -122,15 +132,16 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters const auto matrix = getTileMatrix(tileID, parameters, translation, anchor, nearClipped, inViewportPixelUnits); - const LineType type = static_cast(drawable.getType()); + uniforms.addOrReplace(idLineDynamicUBOName, dynamicBuffer); + const LineType type = static_cast(drawable.getType()); switch (type) { case LineType::Simple: { - const LineUBO lineUBO{ - /*matrix = */ util::cast(matrix), - /*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, - /*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, static_cast(zoom)), - 0}; + const LineUBO lineUBO{/*matrix = */ util::cast(matrix), + /*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, static_cast(zoom)), + 0, + 0, + 0}; uniforms.createOrUpdate(idLineUBOName, &lineUBO, context); // properties UBO @@ -140,8 +151,9 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters case LineType::Gradient: { const LineGradientUBO lineGradientUBO{ /*matrix = */ util::cast(matrix), - /*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, /*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, static_cast(zoom)), + 0, + 0, 0}; uniforms.createOrUpdate(idLineGradientUBOName, &lineGradientUBO, context); @@ -158,16 +170,14 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters } const LinePatternUBO linePatternUBO{ /*matrix =*/util::cast(matrix), + /*scale =*/ {parameters.pixelRatio, 1 / tileID.pixelsToTileUnits(1, parameters.state.getIntegerZoom()), crossfade.fromScale, crossfade.toScale}, /*texsize =*/{static_cast(textureSize.width), static_cast(textureSize.height)}, - /*units_to_pixels =*/{1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, /*ratio =*/1.0f / tileID.pixelsToTileUnits(1.0f, static_cast(zoom)), - /*fade =*/crossfade.t, - 0, - 0}; + /*fade =*/crossfade.t}; uniforms.createOrUpdate(idLinePatternUBOName, &linePatternUBO, context); // properties UBO @@ -200,19 +210,20 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters const float widthB = posB.width * crossfade.toScale; const LineSDFUBO lineSDFUBO{ /* matrix = */ util::cast(matrix), - {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, + /* patternscale_a = */ {1.0f / tileID.pixelsToTileUnits(widthA, parameters.state.getIntegerZoom()), -posA.height / 2.0f}, /* patternscale_b = */ {1.0f / tileID.pixelsToTileUnits(widthB, parameters.state.getIntegerZoom()), -posB.height / 2.0f}, - /* ratio = */ 1.0f / - tileID.pixelsToTileUnits(1.0f, static_cast(parameters.state.getZoom())), + /* ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, static_cast(zoom)), /* tex_y_a = */ posA.y, /* tex_y_b = */ posB.y, /* sdfgamma = */ static_cast(dashPatternTexture.getSize().width) / (std::min(widthA, widthB) * 256.0f * parameters.pixelRatio) / 2.0f, /* mix = */ crossfade.t, + 0, + 0, 0}; uniforms.createOrUpdate(idLineSDFUBOName, &lineSDFUBO, context); diff --git a/src/mbgl/renderer/layers/line_layer_tweaker.hpp b/src/mbgl/renderer/layers/line_layer_tweaker.hpp index 75a0b6418..724cc88f1 100644 --- a/src/mbgl/renderer/layers/line_layer_tweaker.hpp +++ b/src/mbgl/renderer/layers/line_layer_tweaker.hpp @@ -35,6 +35,7 @@ protected: gfx::UniformBufferPtr lineGradientPropertiesBuffer; gfx::UniformBufferPtr linePatternPropertiesBuffer; gfx::UniformBufferPtr lineSDFPropertiesBuffer; + gfx::UniformBufferPtr dynamicBuffer; #if MLN_RENDER_BACKEND_METAL gfx::UniformBufferPtr permutationUniformBuffer; diff --git a/src/mbgl/renderer/sources/render_tile_source.cpp b/src/mbgl/renderer/sources/render_tile_source.cpp index da7e2b2d6..b05eb7567 100644 --- a/src/mbgl/renderer/sources/render_tile_source.cpp +++ b/src/mbgl/renderer/sources/render_tile_source.cpp @@ -210,13 +210,19 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr const auto matrix = LayerTweaker::getTileMatrix( tileID, parameters, {{0, 0}}, style::TranslateAnchorType::Viewport, false, false, false); - static const StringIdentity idLineUBOName = stringIndexer().get("LineUBO"); - const shaders::LineUBO lineUBO{ - /*matrix = */ util::cast(matrix), + static const StringIdentity idLineDynamicUBOName = stringIndexer().get("LineDynamicUBO"); + const shaders::LineDynamicUBO dynamicUBO = { /*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, - /*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom), + 0, 0}; + static const StringIdentity idLineUBOName = stringIndexer().get("LineUBO"); + const shaders::LineUBO lineUBO{/*matrix = */ util::cast(matrix), + /*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom), + 0, + 0, + 0}; + static const StringIdentity idLinePropertiesUBOName = stringIndexer().get("LinePropertiesUBO"); static const StringIdentity idLineInterpolationUBOName = stringIndexer().get("LineInterpolationUBO"); @@ -229,6 +235,7 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr 0, 0}; auto& uniforms = drawable.mutableUniformBuffers(); + uniforms.createOrUpdate(idLineDynamicUBOName, &dynamicUBO, parameters.context); uniforms.createOrUpdate(idLineUBOName, &lineUBO, parameters.context); uniforms.createOrUpdate(idLinePropertiesUBOName, &linePropertiesUBO, parameters.context); uniforms.createOrUpdate(idLineInterpolationUBOName, &lineInterpolationUBO, parameters.context); diff --git a/src/mbgl/shaders/mtl/line.cpp b/src/mbgl/shaders/mtl/line.cpp index 49bccbff4..d7412dd7e 100644 --- a/src/mbgl/shaders/mtl/line.cpp +++ b/src/mbgl/shaders/mtl/line.cpp @@ -13,10 +13,11 @@ const std::array ShaderSource ShaderSource::uniforms = { - UniformBlockInfo{8, true, true, sizeof(LineUBO), "LineUBO"}, - UniformBlockInfo{9, true, true, sizeof(LinePropertiesUBO), "LinePropertiesUBO"}, - UniformBlockInfo{10, true, false, sizeof(LineInterpolationUBO), "LineInterpolationUBO"}, +const std::array ShaderSource::uniforms = { + UniformBlockInfo{8, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"}, + UniformBlockInfo{9, true, true, sizeof(LineUBO), "LineUBO"}, + UniformBlockInfo{10, true, true, sizeof(LinePropertiesUBO), "LinePropertiesUBO"}, + UniformBlockInfo{11, true, false, sizeof(LineInterpolationUBO), "LineInterpolationUBO"}, }; const std::array ShaderSource::textures = {}; @@ -31,11 +32,12 @@ const std::array ShaderSource ShaderSource::uniforms = { - UniformBlockInfo{9, true, true, sizeof(LinePatternUBO), "LinePatternUBO"}, - UniformBlockInfo{10, true, true, sizeof(LinePatternPropertiesUBO), "LinePatternPropertiesUBO"}, - UniformBlockInfo{11, true, false, sizeof(LinePatternInterpolationUBO), "LinePatternInterpolationUBO"}, - UniformBlockInfo{12, true, true, sizeof(LinePatternTilePropertiesUBO), "LinePatternTilePropertiesUBO"}, +const std::array ShaderSource::uniforms = { + UniformBlockInfo{9, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"}, + UniformBlockInfo{10, true, true, sizeof(LinePatternUBO), "LinePatternUBO"}, + UniformBlockInfo{11, true, true, sizeof(LinePatternPropertiesUBO), "LinePatternPropertiesUBO"}, + UniformBlockInfo{12, true, false, sizeof(LinePatternInterpolationUBO), "LinePatternInterpolationUBO"}, + UniformBlockInfo{13, true, true, sizeof(LinePatternTilePropertiesUBO), "LinePatternTilePropertiesUBO"}, }; const std::array ShaderSource::textures = { TextureInfo{0, "u_image"}, @@ -52,10 +54,11 @@ const std::array ShaderSource ShaderSource::uniforms = { - UniformBlockInfo{9, true, true, sizeof(LineSDFUBO), "LineSDFUBO"}, - UniformBlockInfo{10, true, true, sizeof(LineSDFPropertiesUBO), "LineSDFPropertiesUBO"}, - UniformBlockInfo{11, true, false, sizeof(LineSDFInterpolationUBO), "LineSDFInterpolationUBO"}, +const std::array ShaderSource::uniforms = { + UniformBlockInfo{9, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"}, + UniformBlockInfo{10, true, true, sizeof(LineSDFUBO), "LineSDFUBO"}, + UniformBlockInfo{11, true, true, sizeof(LineSDFPropertiesUBO), "LineSDFPropertiesUBO"}, + UniformBlockInfo{12, true, false, sizeof(LineSDFInterpolationUBO), "LineSDFInterpolationUBO"}, }; const std::array ShaderSource::textures = { TextureInfo{0, "u_image"}, diff --git a/src/mbgl/shaders/mtl/line_gradient.cpp b/src/mbgl/shaders/mtl/line_gradient.cpp index 66b0ba7ce..9fcb5d65d 100644 --- a/src/mbgl/shaders/mtl/line_gradient.cpp +++ b/src/mbgl/shaders/mtl/line_gradient.cpp @@ -12,10 +12,11 @@ const std::array ShaderSource ShaderSource::uniforms = { - UniformBlockInfo{7, true, true, sizeof(LineGradientUBO), "LineGradientUBO"}, - UniformBlockInfo{8, true, true, sizeof(LineGradientPropertiesUBO), "LineGradientPropertiesUBO"}, - UniformBlockInfo{9, true, false, sizeof(LineGradientInterpolationUBO), "LineGradientInterpolationUBO"}, +const std::array ShaderSource::uniforms = { + UniformBlockInfo{7, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"}, + UniformBlockInfo{8, true, true, sizeof(LineGradientUBO), "LineGradientUBO"}, + UniformBlockInfo{9, true, true, sizeof(LineGradientPropertiesUBO), "LineGradientPropertiesUBO"}, + UniformBlockInfo{10, true, false, sizeof(LineGradientInterpolationUBO), "LineGradientInterpolationUBO"}, }; const std::array ShaderSource::textures = { TextureInfo{0, "u_image"}, diff --git a/src/mbgl/style/layers/custom_drawable_layer.cpp b/src/mbgl/style/layers/custom_drawable_layer.cpp index 5984e2093..374222285 100644 --- a/src/mbgl/style/layers/custom_drawable_layer.cpp +++ b/src/mbgl/style/layers/custom_drawable_layer.cpp @@ -97,12 +97,16 @@ public: const auto matrix = LayerTweaker::getTileMatrix( tileID, parameters, {{0, 0}}, style::TranslateAnchorType::Viewport, false, false, false); + static const StringIdentity idLineDynamicUBOName = stringIndexer().get("LineDynamicUBO"); + const shaders::LineDynamicUBO dynamicUBO = { + /*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, 0, 0}; + static const StringIdentity idLineUBOName = stringIndexer().get("LineUBO"); - const shaders::LineUBO lineUBO{ - /*matrix = */ util::cast(matrix), - /*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, - /*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom), - 0}; + const shaders::LineUBO lineUBO{/*matrix = */ util::cast(matrix), + /*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom), + 0, + 0, + 0}; static const StringIdentity idLinePropertiesUBOName = stringIndexer().get("LinePropertiesUBO"); @@ -116,6 +120,7 @@ public: 0, 0}; auto& uniforms = drawable.mutableUniformBuffers(); + uniforms.createOrUpdate(idLineDynamicUBOName, &dynamicUBO, parameters.context); uniforms.createOrUpdate(idLineUBOName, &lineUBO, parameters.context); uniforms.createOrUpdate(idLinePropertiesUBOName, &linePropertiesUBO, parameters.context); uniforms.createOrUpdate(idLineInterpolationUBOName, &lineInterpolationUBO, parameters.context);