From 0fe9fee1adee297fcd6860384ee6c4eccb0bd8c8 Mon Sep 17 00:00:00 2001 From: Alex Cristici Date: Sun, 15 Dec 2024 22:52:26 +0200 Subject: [PATCH] Vulkan. --- include/mbgl/gfx/context.hpp | 2 +- include/mbgl/mtl/context.hpp | 2 +- include/mbgl/shaders/layer_ubo.hpp | 2 +- .../mbgl/shaders/mtl/custom_symbol_icon.hpp | 2 +- include/mbgl/shaders/shader_defines.hpp | 2 + include/mbgl/shaders/vulkan/background.hpp | 29 +++- include/mbgl/shaders/vulkan/circle.hpp | 13 +- include/mbgl/shaders/vulkan/common.hpp | 4 +- include/mbgl/shaders/vulkan/fill.hpp | 117 +++++++++++-- .../mbgl/shaders/vulkan/fill_extrusion.hpp | 40 ++++- include/mbgl/shaders/vulkan/heatmap.hpp | 13 +- include/mbgl/shaders/vulkan/hillshade.hpp | 27 ++- include/mbgl/shaders/vulkan/line.hpp | 89 ++++++++-- .../shaders/vulkan/location_indicator.hpp | 2 +- include/mbgl/shaders/vulkan/raster.hpp | 13 +- include/mbgl/shaders/vulkan/symbol.hpp | 84 +++++++-- include/mbgl/vulkan/context.hpp | 7 +- include/mbgl/vulkan/descriptor_set.hpp | 16 +- include/mbgl/vulkan/uniform_buffer.hpp | 9 +- src/mbgl/gl/context.cpp | 2 +- src/mbgl/gl/context.hpp | 2 +- src/mbgl/mtl/context.cpp | 2 +- .../layers/background_layer_tweaker.cpp | 4 +- .../renderer/layers/circle_layer_tweaker.cpp | 2 +- .../layers/fill_extrusion_layer_tweaker.cpp | 4 +- .../renderer/layers/fill_layer_tweaker.cpp | 7 +- .../renderer/layers/heatmap_layer_tweaker.cpp | 4 +- .../layers/hillshade_layer_tweaker.cpp | 4 +- .../renderer/layers/line_layer_tweaker.cpp | 6 +- .../renderer/layers/raster_layer_tweaker.cpp | 2 +- .../renderer/layers/symbol_layer_tweaker.cpp | 4 +- src/mbgl/renderer/renderer_impl.cpp | 20 ++- .../renderer/sources/render_tile_source.cpp | 163 +++++++++--------- .../style/layers/custom_drawable_layer.cpp | 88 ++++++---- src/mbgl/vulkan/buffer_resource.cpp | 4 +- src/mbgl/vulkan/context.cpp | 50 ++++-- src/mbgl/vulkan/descriptor_set.cpp | 34 ++-- src/mbgl/vulkan/drawable.cpp | 7 + src/mbgl/vulkan/drawable_impl.hpp | 2 +- src/mbgl/vulkan/layer_group.cpp | 2 +- src/mbgl/vulkan/tile_layer_group.cpp | 2 +- src/mbgl/vulkan/uniform_buffer.cpp | 8 +- 42 files changed, 636 insertions(+), 260 deletions(-) diff --git a/include/mbgl/gfx/context.hpp b/include/mbgl/gfx/context.hpp index 8e21265098f..921374d4281 100644 --- a/include/mbgl/gfx/context.hpp +++ b/include/mbgl/gfx/context.hpp @@ -125,7 +125,7 @@ class Context { /// @param data The data to copy, may be `nullptr` /// @param size The size of the buffer /// @param persistent Performance hint, optimize for few or many uses - virtual UniformBufferPtr createUniformBuffer(const void* data, std::size_t size, bool persistent = false) = 0; + virtual UniformBufferPtr createUniformBuffer(const void* data, std::size_t size, bool persistent = false, bool ssbo = false) = 0; /// Get the generic shader with the specified name virtual gfx::ShaderProgramBasePtr getGenericShader(gfx::ShaderRegistry&, const std::string& name) = 0; diff --git a/include/mbgl/mtl/context.hpp b/include/mbgl/mtl/context.hpp index 6934ced790f..a6349faf942 100644 --- a/include/mbgl/mtl/context.hpp +++ b/include/mbgl/mtl/context.hpp @@ -83,7 +83,7 @@ class Context final : public gfx::Context { void reduceMemoryUsage() override {} gfx::UniqueDrawableBuilder createDrawableBuilder(std::string name) override; - gfx::UniformBufferPtr createUniformBuffer(const void* data, std::size_t size, bool persisten) override; + gfx::UniformBufferPtr createUniformBuffer(const void* data, std::size_t size, bool persistent = false, bool ssbo = false) override; gfx::ShaderProgramBasePtr getGenericShader(gfx::ShaderRegistry&, const std::string& name) override; diff --git a/include/mbgl/shaders/layer_ubo.hpp b/include/mbgl/shaders/layer_ubo.hpp index fd9b91a3e05..1f3d97887c0 100644 --- a/include/mbgl/shaders/layer_ubo.hpp +++ b/include/mbgl/shaders/layer_ubo.hpp @@ -75,7 +75,7 @@ enum { drawableReservedUBOCount }; -#define MLN_UBO_CONSOLIDATION MLN_RENDER_BACKEND_METAL +#define MLN_UBO_CONSOLIDATION (MLN_RENDER_BACKEND_METAL || MLN_RENDER_BACKEND_VULKAN) } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/mtl/custom_symbol_icon.hpp b/include/mbgl/shaders/mtl/custom_symbol_icon.hpp index a52674c5e1b..82581e74d37 100644 --- a/include/mbgl/shaders/mtl/custom_symbol_icon.hpp +++ b/include/mbgl/shaders/mtl/custom_symbol_icon.hpp @@ -29,7 +29,7 @@ struct alignas(16) CustomSymbolIconDrawableUBO { /* 108 */ float pad3; /* 112 */ }; -static_assert(sizeof(CustomSymbolIconParametersUBO) == 7 * 16, "wrong size"); +static_assert(sizeof(CustomSymbolIconDrawableUBO) == 7 * 16, "wrong size"); )" diff --git a/include/mbgl/shaders/shader_defines.hpp b/include/mbgl/shaders/shader_defines.hpp index 692c688a188..557921146e2 100644 --- a/include/mbgl/shaders/shader_defines.hpp +++ b/include/mbgl/shaders/shader_defines.hpp @@ -101,6 +101,7 @@ enum { wideVectorDrawableUBOCount }; +static constexpr auto layerSSBOStartId = globalUBOCount; static constexpr auto layerUBOStartId = std::max({static_cast(backgroundDrawableUBOCount), static_cast(circleDrawableUBOCount), static_cast(clippingMaskDrawableUBOCount), @@ -228,6 +229,7 @@ static constexpr auto maxUBOCountPerShader = std::max({static_cast(backg static_cast(symbolUBOCount), static_cast(wideVectorUBOCount)}); +static constexpr auto maxSSBOCountPerLayer = maxUBOCountPerDrawable; static constexpr auto maxUBOCountPerLayer = maxUBOCountPerShader - layerUBOStartId; // Texture defines diff --git a/include/mbgl/shaders/vulkan/background.hpp b/include/mbgl/shaders/vulkan/background.hpp index a1946e249a5..2fa7df3478d 100644 --- a/include/mbgl/shaders/vulkan/background.hpp +++ b/include/mbgl/shaders/vulkan/background.hpp @@ -26,11 +26,23 @@ struct ShaderSource { layout(location = 0) in ivec2 in_position; -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idBackgroundDrawableUBO) uniform BackgroundDrawableUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct BackgroundDrawableUBO { mat4 matrix; -} drawable; + vec4 pad1; + vec4 pad2; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idBackgroundDrawableUBO) readonly buffer BackgroundDrawableUBOVector { + BackgroundDrawableUBO drawable_ubo[]; +} drawableVector; void main() { + const BackgroundDrawableUBO drawable = drawableVector.drawable_ubo[constant.ubo_index]; + gl_Position = drawable.matrix * vec4(in_position, 0.0, 1.0); applySurfaceTransform(); } @@ -70,7 +82,11 @@ struct ShaderSource { layout(location = 0) in ivec2 in_position; layout(location = 1) in ivec2 in_texture_position; -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idHillshadeDrawableUBO) uniform HillshadeDrawableUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct HillshadeDrawableUBO { mat4 matrix; -} drawable; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idHillshadeDrawableUBO) readonly buffer HillshadeDrawableUBOVector { + HillshadeDrawableUBO drawable_ubo[]; +} drawableVector; layout(location = 0) out vec2 frag_position; void main() { + const HillshadeDrawableUBO drawable = drawableVector.drawable_ubo[constant.ubo_index]; gl_Position = drawable.matrix * vec4(in_position, 0.0, 1.0); applySurfaceTransform(); @@ -49,10 +58,18 @@ void main() { layout(location = 0) in vec2 frag_position; layout(location = 0) out vec4 out_color; -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idHillshadeTilePropsUBO) uniform HillshadeTilePropsUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct HillshadeTilePropsUBO { vec2 latrange; vec2 light; -} tileProps; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idHillshadeTilePropsUBO) readonly buffer HillshadeTilePropsUBOVector { + HillshadeTilePropsUBO tile_props_ubo[]; +} tilePropsVector; layout(set = LAYER_SET_INDEX, binding = idHillshadeEvaluatedPropsUBO) uniform HillshadeEvaluatedPropsUBO { vec4 highlight; @@ -69,6 +86,8 @@ void main() { return; #endif + const HillshadeTilePropsUBO tileProps = tilePropsVector.tile_props_ubo[constant.ubo_index]; + vec4 pixel = texture(image_sampler, frag_position); vec2 deriv = pixel.rg * 2.0 - 1.0; diff --git a/include/mbgl/shaders/vulkan/line.hpp b/include/mbgl/shaders/vulkan/line.hpp index 4dc3cdbd601..20fd60d2fdd 100644 --- a/include/mbgl/shaders/vulkan/line.hpp +++ b/include/mbgl/shaders/vulkan/line.hpp @@ -52,7 +52,11 @@ layout(location = 6) in vec2 in_offset; layout(location = 7) in vec2 in_width; #endif -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idLineDrawableUBO) uniform LineDrawableUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct LineDrawableUBO { mat4 matrix; mediump float ratio; // Interpolations @@ -63,7 +67,13 @@ layout(set = DRAWABLE_UBO_SET_INDEX, binding = idLineDrawableUBO) uniform LineDr float offset_t; float width_t; float pad1; -} drawable; + vec4 pad2; + vec4 pad3; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idLineDrawableUBO) readonly buffer LineDrawableUBOVector { + LineDrawableUBO drawable_ubo[]; +} drawableVector; layout(set = LAYER_SET_INDEX, binding = idLineEvaluatedPropsUBO) uniform LineEvaluatedPropsUBO { vec4 color; @@ -94,6 +104,7 @@ layout(location = 5) out lowp float frag_opacity; #endif void main() { + const LineDrawableUBO drawable = drawableVector.drawable_ubo[constant.ubo_index]; #ifndef HAS_UNIFORM_u_color frag_color = unpack_mix_color(in_color, drawable.color_t); @@ -275,7 +286,11 @@ layout(location = 5) in vec2 in_offset; layout(location = 6) in vec2 in_width; #endif -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idLineDrawableUBO) uniform LineGradientDrawableUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct LineGradientDrawableUBO { mat4 matrix; mediump float ratio; // Interpolations @@ -286,7 +301,13 @@ layout(set = DRAWABLE_UBO_SET_INDEX, binding = idLineDrawableUBO) uniform LineGr float width_t; float pad1; float pad2; -} drawable; + vec4 pad3; + vec4 pad4; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idLineDrawableUBO) readonly buffer LineGradientDrawableUBOVector { + LineGradientDrawableUBO drawable_ubo[]; +} drawableVector; layout(set = LAYER_SET_INDEX, binding = idLineEvaluatedPropsUBO) uniform LineEvaluatedPropsUBO { vec4 color; @@ -314,6 +335,7 @@ layout(location = 5) out lowp float frag_opacity; #endif void main() { + const LineGradientDrawableUBO drawable = drawableVector.drawable_ubo[constant.ubo_index]; #ifndef HAS_UNIFORM_u_blur frag_blur = unpack_mix_float(in_blur, drawable.blur_t); @@ -497,7 +519,11 @@ layout(location = 7) in uvec4 in_pattern_from; layout(location = 8) in uvec4 in_pattern_to; #endif -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idLineDrawableUBO) uniform LinePatternDrawableUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct LinePatternDrawableUBO { mat4 matrix; float ratio; // Interpolations @@ -508,7 +534,13 @@ layout(set = DRAWABLE_UBO_SET_INDEX, binding = idLineDrawableUBO) uniform LinePa float width_t; float pattern_from_t; float pattern_to_t; -} drawable; + vec4 pad1; + vec4 pad2; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idLineDrawableUBO) readonly buffer LinePatternDrawableUBOVector { + LinePatternDrawableUBO drawable_ubo[]; +} drawableVector; layout(set = LAYER_SET_INDEX, binding = idLineEvaluatedPropsUBO) uniform LineEvaluatedPropsUBO { vec4 color; @@ -544,6 +576,7 @@ layout(location = 7) out mediump vec4 frag_pattern_to; #endif void main() { + const LinePatternDrawableUBO drawable = drawableVector.drawable_ubo[constant.ubo_index]; #ifndef HAS_UNIFORM_u_blur frag_blur = unpack_mix_float(in_blur, drawable.blur_t); @@ -652,14 +685,22 @@ layout(location = 7) in mediump vec4 frag_pattern_to; layout(location = 0) out vec4 out_color; -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idLineTilePropsUBO) uniform LinePatternTilePropertiesUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct LinePatternTilePropertiesUBO { vec4 pattern_from; vec4 pattern_to; vec4 scale; vec2 texsize; float fade; float pad1; -} tileProps; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idLineTilePropsUBO) readonly buffer LinePatternTilePropertiesUBOVector { + LinePatternTilePropertiesUBO tile_props_ubo[]; +} tilePropsVector; layout(set = LAYER_SET_INDEX, binding = idLineEvaluatedPropsUBO) uniform LineEvaluatedPropsUBO { vec4 color; @@ -682,6 +723,8 @@ void main() { return; #endif + const LinePatternTilePropertiesUBO tileProps = tilePropsVector.tile_props_ubo[constant.ubo_index]; + #ifdef HAS_UNIFORM_u_blur const lowp float blur = props.blur; #else @@ -792,7 +835,11 @@ layout(location = 7) in vec2 in_width; layout(location = 8) in vec2 in_floorwidth; #endif -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idLineDrawableUBO) uniform LineSDFDrawableUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct LineSDFDrawableUBO { mat4 matrix; vec2 patternscale_a; vec2 patternscale_b; @@ -809,7 +856,11 @@ layout(set = DRAWABLE_UBO_SET_INDEX, binding = idLineDrawableUBO) uniform LineSD float floorwidth_t; float pad1; float pad2; -} drawable; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idLineDrawableUBO) readonly buffer LineSDFDrawableUBOVector { + LineSDFDrawableUBO drawable_ubo[]; +} drawableVector; layout(set = LAYER_SET_INDEX, binding = idLineEvaluatedPropsUBO) uniform LineEvaluatedPropsUBO { vec4 color; @@ -846,6 +897,7 @@ layout(location = 8) out mediump float frag_floorwidth; #endif void main() { + const LineSDFDrawableUBO drawable = drawableVector.drawable_ubo[constant.ubo_index]; #ifndef HAS_UNIFORM_u_color frag_color = unpack_mix_color(in_color, intdrawableerp.color_t); @@ -961,12 +1013,23 @@ layout(location = 8) in mediump float frag_floorwidth; layout(location = 0) out vec4 out_color; -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idLineTilePropsUBO) uniform LineSDFInterpolationUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct LineSDFTilePropsUBO { float sdfgamma; float mix; float pad1; float pad2; -} tileProps; + vec4 pad3; + vec4 pad4; + vec4 pad5; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idLineTilePropsUBO) readonly buffer LineSDFTilePropsUBOVector { + LineSDFTilePropsUBO tile_props_ubo[]; +} tilePropsVector; layout(set = LAYER_SET_INDEX, binding = idLineEvaluatedPropsUBO) uniform LineEvaluatedPropsUBO { vec4 color; @@ -989,6 +1052,8 @@ void main() { return; #endif + const LineSDFTilePropsUBO tileProps = tilePropsVector.tile_props_ubo[constant.ubo_index]; + #ifdef HAS_UNIFORM_u_color const lowp vec4 color = props.color; #else diff --git a/include/mbgl/shaders/vulkan/location_indicator.hpp b/include/mbgl/shaders/vulkan/location_indicator.hpp index 5a06107e559..a1181fdc9b2 100644 --- a/include/mbgl/shaders/vulkan/location_indicator.hpp +++ b/include/mbgl/shaders/vulkan/location_indicator.hpp @@ -57,7 +57,7 @@ struct ShaderSource instanceAttributes{}; static const std::array textures; - static constexpr auto vertex = R"( + static constexpr auto vertex = LOCATION_INDICATOR_SHADER_PRELUDE R"( layout(location = 0) in vec2 in_position; layout(location = 1) in vec2 in_texcoord; diff --git a/include/mbgl/shaders/vulkan/raster.hpp b/include/mbgl/shaders/vulkan/raster.hpp index cb0b89a1487..6727ec0dc31 100644 --- a/include/mbgl/shaders/vulkan/raster.hpp +++ b/include/mbgl/shaders/vulkan/raster.hpp @@ -27,9 +27,17 @@ struct ShaderSource { layout(location = 0) in ivec2 in_position; layout(location = 1) in ivec2 in_texture_position; -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idRasterDrawableUBO) uniform RasterDrawableUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct RasterDrawableUBO { mat4 matrix; -} drawable; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idRasterDrawableUBO) readonly buffer RasterDrawableUBOVector { + RasterDrawableUBO drawable_ubo[]; +} drawableVector; layout(set = LAYER_SET_INDEX, binding = idRasterEvaluatedPropsUBO) uniform RasterEvaluatedPropsUBO { vec4 spin_weights; @@ -50,6 +58,7 @@ layout(location = 0) out vec2 frag_position0; layout(location = 1) out vec2 frag_position1; void main() { + const RasterDrawableUBO drawable = drawableVector.drawable_ubo[constant.ubo_index]; gl_Position = drawable.matrix * vec4(in_position, 0, 1); applySurfaceTransform(); diff --git a/include/mbgl/shaders/vulkan/symbol.hpp b/include/mbgl/shaders/vulkan/symbol.hpp index dfca8285b51..c41550bcf34 100644 --- a/include/mbgl/shaders/vulkan/symbol.hpp +++ b/include/mbgl/shaders/vulkan/symbol.hpp @@ -35,7 +35,11 @@ layout(location = 4) in float in_fade_opacity; layout(location = 5) in vec2 in_opacity; #endif -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idSymbolDrawableUBO) uniform SymbolDrawableUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct SymbolDrawableUBO { mat4 matrix; mat4 label_plane_matrix; mat4 coord_matrix; @@ -58,13 +62,17 @@ layout(set = DRAWABLE_UBO_SET_INDEX, binding = idSymbolDrawableUBO) uniform Symb float opacity_t; float halo_width_t; float halo_blur_t; - float pad1; -} drawable; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idSymbolDrawableUBO) readonly buffer SymbolDrawableUBOVector { + SymbolDrawableUBO drawable_ubo[]; +} drawableVector; layout(location = 0) out mediump vec2 frag_tex; layout(location = 1) out mediump float frag_opacity; void main() { + const SymbolDrawableUBO drawable = drawableVector.drawable_ubo[constant.ubo_index]; const vec2 a_pos = in_pos_offset.xy; const vec2 a_offset = in_pos_offset.zw; @@ -143,12 +151,20 @@ layout(location = 1) in mediump float frag_opacity; layout(location = 0) out vec4 out_color; -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idSymbolTilePropsUBO) uniform SymbolTilePropsUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct SymbolTilePropsUBO { bool is_text; bool is_halo; float gamma_scale; float pad1; -} tileProps; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idSymbolTilePropsUBO) readonly buffer SymbolTilePropsUBOVector { + SymbolTilePropsUBO tile_props_ubo[]; +} tilePropsVector; layout(set = LAYER_SET_INDEX, binding = idSymbolEvaluatedPropsUBO) uniform SymbolEvaluatedPropsUBO { vec4 text_fill_color; @@ -173,6 +189,8 @@ void main() { return; #endif + const SymbolTilePropsUBO tileProps = tilePropsVector.tile_props_ubo[constant.ubo_index]; + #if defined(HAS_UNIFORM_u_opacity) const float opacity = (tileProps.is_text ? props.text_opacity : props.icon_opacity) * frag_opacity; #else @@ -220,7 +238,11 @@ layout(location = 8) in vec2 in_halo_width; layout(location = 9) in vec2 in_halo_blur; #endif -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idSymbolDrawableUBO) uniform SymbolDrawableUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct SymbolDrawableUBO { mat4 matrix; mat4 label_plane_matrix; mat4 coord_matrix; @@ -243,8 +265,11 @@ layout(set = DRAWABLE_UBO_SET_INDEX, binding = idSymbolDrawableUBO) uniform Symb float opacity_t; float halo_width_t; float halo_blur_t; - float pad1; -} drawable; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idSymbolDrawableUBO) readonly buffer SymbolDrawableUBOVector { + SymbolDrawableUBO drawable_ubo[]; +} drawableVector; layout(location = 0) out mediump vec2 frag_tex; layout(location = 1) out mediump float frag_fade_opacity; @@ -272,6 +297,7 @@ layout(location = 8) out mediump float frag_halo_blur; #endif void main() { + const SymbolDrawableUBO drawable = drawableVector.drawable_ubo[constant.ubo_index]; const vec2 a_pos = in_pos_offset.xy; const vec2 a_offset = in_pos_offset.zw; @@ -389,12 +415,20 @@ layout(location = 8) in mediump float frag_halo_blur; layout(location = 0) out vec4 out_color; -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idSymbolTilePropsUBO) uniform SymbolTilePropsUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct SymbolTilePropsUBO { bool is_text; bool is_halo; float gamma_scale; float pad1; -} tileProps; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idSymbolTilePropsUBO) readonly buffer SymbolTilePropsUBOVector { + SymbolTilePropsUBO tile_props_ubo[]; +} tilePropsVector; layout(set = LAYER_SET_INDEX, binding = idSymbolEvaluatedPropsUBO) uniform SymbolEvaluatedPropsUBO { vec4 text_fill_color; @@ -420,6 +454,8 @@ void main() { return; #endif + const SymbolTilePropsUBO tileProps = tilePropsVector.tile_props_ubo[constant.ubo_index]; + #if defined(HAS_UNIFORM_u_fill_color) const vec4 fill_color = tileProps.is_text ? props.text_fill_color : props.icon_fill_color; #else @@ -498,7 +534,11 @@ layout(location = 7) in vec2 in_halo_width; layout(location = 8) in vec2 in_halo_blur; #endif -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idSymbolDrawableUBO) uniform SymbolDrawableUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct SymbolDrawableUBO { mat4 matrix; mat4 label_plane_matrix; mat4 coord_matrix; @@ -521,8 +561,11 @@ layout(set = DRAWABLE_UBO_SET_INDEX, binding = idSymbolDrawableUBO) uniform Symb float opacity_t; float halo_width_t; float halo_blur_t; - float pad1; -} drawable; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idSymbolDrawableUBO) readonly buffer SymbolDrawableUBOVector { + SymbolDrawableUBO drawable_ubo[]; +} drawableVector; layout(location = 0) out mediump vec2 frag_tex; layout(location = 1) out mediump float frag_fade_opacity; @@ -552,6 +595,7 @@ layout(location = 8) out mediump float frag_halo_blur; layout(location = 9) out int frag_is_icon; void main() { + const SymbolDrawableUBO drawable = drawableVector.drawable_ubo[constant.ubo_index]; const vec2 a_pos = in_pos_offset.xy; const vec2 a_offset = in_pos_offset.zw; @@ -674,12 +718,20 @@ layout(location = 9) flat in int frag_is_icon; layout(location = 0) out vec4 out_color; -layout(set = DRAWABLE_UBO_SET_INDEX, binding = idSymbolTilePropsUBO) uniform SymbolTilePropsUBO { +layout(push_constant) uniform Constants { + int ubo_index; +} constant; + +struct SymbolTilePropsUBO { bool is_text; bool is_halo; float gamma_scale; float pad1; -} tileProps; +}; + +layout(std140, set = LAYER_SET_INDEX, binding = idSymbolTilePropsUBO) readonly buffer SymbolTilePropsUBOVector { + SymbolTilePropsUBO tile_props_ubo[]; +} tilePropsVector; layout(set = LAYER_SET_INDEX, binding = idSymbolEvaluatedPropsUBO) uniform SymbolEvaluatedPropsUBO { vec4 text_fill_color; @@ -705,6 +757,8 @@ void main() { return; #endif + const SymbolTilePropsUBO tileProps = tilePropsVector.tile_props_ubo[constant.ubo_index]; + #if defined(HAS_UNIFORM_u_fill_color) const vec4 fill_color = tileProps.is_text ? props.text_fill_color : props.icon_fill_color; #else diff --git a/include/mbgl/vulkan/context.hpp b/include/mbgl/vulkan/context.hpp index 3208b3d9dc7..eb6ea156631 100644 --- a/include/mbgl/vulkan/context.hpp +++ b/include/mbgl/vulkan/context.hpp @@ -74,7 +74,7 @@ class Context final : public gfx::Context { void reduceMemoryUsage() override {} gfx::UniqueDrawableBuilder createDrawableBuilder(std::string name) override; - gfx::UniformBufferPtr createUniformBuffer(const void* data, std::size_t size, bool persistent) override; + gfx::UniformBufferPtr createUniformBuffer(const void* data, std::size_t size, bool persistent, bool ssbo = false) override; gfx::ShaderProgramBasePtr getGenericShader(gfx::ShaderRegistry&, const std::string& name) override; @@ -135,6 +135,7 @@ class Context final : public gfx::Context { const std::unique_ptr& getDummyVertexBuffer(); const std::unique_ptr& getDummyUniformBuffer(); + const std::unique_ptr& getDummyStorageBuffer(); const std::unique_ptr& getDummyTexture(); const vk::DescriptorSetLayout& getDescriptorSetLayout(DescriptorSetType type); @@ -175,7 +176,8 @@ class Context final : public gfx::Context { void buildImageDescriptorSetLayout(); void buildUniformDescriptorSetLayout(vk::UniqueDescriptorSetLayout& layout, - size_t uniformStartId, + size_t startId, + size_t storageCount, size_t uniformCount, const std::string& name); @@ -187,6 +189,7 @@ class Context final : public gfx::Context { std::unique_ptr dummyVertexBuffer; std::unique_ptr dummyUniformBuffer; + std::unique_ptr dummyStorageBuffer; std::unique_ptr dummyTexture2D; vk::UniqueDescriptorSetLayout globalUniformDescriptorSetLayout; vk::UniqueDescriptorSetLayout layerUniformDescriptorSetLayout; diff --git a/include/mbgl/vulkan/descriptor_set.hpp b/include/mbgl/vulkan/descriptor_set.hpp index fff737a7d91..6582216c956 100644 --- a/include/mbgl/vulkan/descriptor_set.hpp +++ b/include/mbgl/vulkan/descriptor_set.hpp @@ -30,7 +30,9 @@ struct DescriptorPoolGrowable { }; const uint32_t maxSets{0}; - const uint32_t descriptorsPerSet{0}; + const uint32_t descriptorStoragePerSet{0}; + const uint32_t descriptorUniformsPerSet{0}; + const uint32_t descriptorTexturesPerSet{0}; const float growFactor{1.5f}; std::vector pools; @@ -39,9 +41,15 @@ struct DescriptorPoolGrowable { PoolInfo& current() { return pools[currentPoolIndex]; } DescriptorPoolGrowable() = default; - DescriptorPoolGrowable(uint32_t maxSets_, uint32_t descriptorsPerSet_, float growFactor_ = 1.5f) + DescriptorPoolGrowable(uint32_t maxSets_, + uint32_t descriptorStoragePerSet_, + uint32_t descriptorUniformsPerSet_, + uint32_t descriptorTexturesPerSet_, + float growFactor_ = 1.5f) : maxSets(maxSets_), - descriptorsPerSet(descriptorsPerSet_), + descriptorStoragePerSet(descriptorStoragePerSet_), + descriptorUniformsPerSet(descriptorUniformsPerSet_), + descriptorTexturesPerSet(descriptorTexturesPerSet_), growFactor(growFactor_) {} }; @@ -72,7 +80,7 @@ class UniformDescriptorSet : public DescriptorSet { UniformDescriptorSet(Context& context_, DescriptorSetType type_); virtual ~UniformDescriptorSet() = default; - void update(const gfx::UniformBufferArray& uniforms, uint32_t uniformStartIndex, uint32_t descriptorBindingCount); + void update(const gfx::UniformBufferArray& uniforms, uint32_t descriptorStartIndex, uint32_t descriptorStorageCount, uint32_t descriptorUniformCount); }; class ImageDescriptorSet : public DescriptorSet { diff --git a/include/mbgl/vulkan/uniform_buffer.hpp b/include/mbgl/vulkan/uniform_buffer.hpp index 83854bee3a2..643f293e955 100644 --- a/include/mbgl/vulkan/uniform_buffer.hpp +++ b/include/mbgl/vulkan/uniform_buffer.hpp @@ -30,10 +30,12 @@ class UniformBufferArray final : public gfx::UniformBufferArray { UniformBufferArray() = delete; UniformBufferArray(DescriptorSetType descriptorSetType_, uint32_t descriptorStartIndex_, - uint32_t descriptorBindingCount_) + uint32_t descriptorStorageCount_, + uint32_t descriptorUniformCount_) : descriptorSetType(descriptorSetType_), descriptorStartIndex(descriptorStartIndex_), - descriptorBindingCount(descriptorBindingCount_) {} + descriptorStorageCount(descriptorStorageCount_), + descriptorUniformCount(descriptorUniformCount_) {} UniformBufferArray(UniformBufferArray&& other) : gfx::UniformBufferArray(std::move(other)) {} @@ -66,7 +68,8 @@ class UniformBufferArray final : public gfx::UniformBufferArray { const DescriptorSetType descriptorSetType{DescriptorSetType::DrawableUniform}; const uint32_t descriptorStartIndex{0}; - const uint32_t descriptorBindingCount{0}; + const uint32_t descriptorStorageCount{0}; + const uint32_t descriptorUniformCount{0}; std::unique_ptr descriptorSet; }; diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index 432592612a2..6e95f3df7d5 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -611,7 +611,7 @@ gfx::UniqueDrawableBuilder Context::createDrawableBuilder(std::string name) { return std::make_unique(std::move(name)); } -gfx::UniformBufferPtr Context::createUniformBuffer(const void* data, std::size_t size, bool /*persistent*/) { +gfx::UniformBufferPtr Context::createUniformBuffer(const void* data, std::size_t size, bool /*persistent*/, bool /*ssbo*/) { MLN_TRACE_FUNC(); return std::make_shared(data, size, *uboAllocator); diff --git a/src/mbgl/gl/context.hpp b/src/mbgl/gl/context.hpp index ae3b50cf84b..0b42d58f844 100644 --- a/src/mbgl/gl/context.hpp +++ b/src/mbgl/gl/context.hpp @@ -120,7 +120,7 @@ class Context final : public gfx::Context { #if MLN_DRAWABLE_RENDERER gfx::UniqueDrawableBuilder createDrawableBuilder(std::string name) override; - gfx::UniformBufferPtr createUniformBuffer(const void* data, std::size_t size, bool persistent) override; + gfx::UniformBufferPtr createUniformBuffer(const void* data, std::size_t size, bool persistent = false, bool ssbo = false) override; gfx::ShaderProgramBasePtr getGenericShader(gfx::ShaderRegistry&, const std::string& name) override; diff --git a/src/mbgl/mtl/context.cpp b/src/mbgl/mtl/context.cpp index 675d5c8d120..afa1756b0ad 100644 --- a/src/mbgl/mtl/context.cpp +++ b/src/mbgl/mtl/context.cpp @@ -199,7 +199,7 @@ gfx::UniqueDrawableBuilder Context::createDrawableBuilder(std::string name) { return std::make_unique(std::move(name)); } -gfx::UniformBufferPtr Context::createUniformBuffer(const void* data, std::size_t size, bool persistent) { +gfx::UniformBufferPtr Context::createUniformBuffer(const void* data, std::size_t size, bool persistent, bool /*ssbo*/) { return std::make_shared( createBuffer(data, size, gfx::BufferUsageType::StaticDraw, /*isIndexBuffer=*/false, persistent)); } diff --git a/src/mbgl/renderer/layers/background_layer_tweaker.cpp b/src/mbgl/renderer/layers/background_layer_tweaker.cpp index 769a5818eb7..dbfca5ef36c 100644 --- a/src/mbgl/renderer/layers/background_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/background_layer_tweaker.cpp @@ -92,7 +92,9 @@ void BackgroundLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintPara const auto matrix = getTileMatrix( tileID, parameters, {0.f, 0.f}, TranslateAnchorType::Viewport, false, false, drawable); +#if !MLN_UBO_CONSOLIDATION auto& drawableUniforms = drawable.mutableUniformBuffers(); +#endif if (hasPattern) { if (const auto& tex = parameters.patternAtlas.texture()) { @@ -146,7 +148,7 @@ void BackgroundLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintPara #if MLN_UBO_CONSOLIDATION const size_t drawableUBOVectorSize = sizeof(BackgroundDrawableUnionUBO) * drawableUBOVector.size(); if (!drawableUniformBuffer || drawableUniformBuffer->getSize() < drawableUBOVectorSize) { - drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false); + drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false, true); } else { drawableUniformBuffer->update(drawableUBOVector.data(), drawableUBOVectorSize); } diff --git a/src/mbgl/renderer/layers/circle_layer_tweaker.cpp b/src/mbgl/renderer/layers/circle_layer_tweaker.cpp index 1a2f461a810..3b3ac5f6f50 100644 --- a/src/mbgl/renderer/layers/circle_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/circle_layer_tweaker.cpp @@ -118,7 +118,7 @@ void CircleLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamete #if MLN_UBO_CONSOLIDATION const size_t drawableUBOVectorSize = sizeof(CircleDrawableUBO) * drawableUBOVector.size(); if (!drawableUniformBuffer || drawableUniformBuffer->getSize() < drawableUBOVectorSize) { - drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false); + drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false, true); } else { drawableUniformBuffer->update(drawableUBOVector.data(), drawableUBOVectorSize); } diff --git a/src/mbgl/renderer/layers/fill_extrusion_layer_tweaker.cpp b/src/mbgl/renderer/layers/fill_extrusion_layer_tweaker.cpp index cf4d8374f1b..044452fabfd 100644 --- a/src/mbgl/renderer/layers/fill_extrusion_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/fill_extrusion_layer_tweaker.cpp @@ -155,14 +155,14 @@ void FillExtrusionLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintP #if MLN_UBO_CONSOLIDATION const size_t drawableUBOVectorSize = sizeof(FillExtrusionDrawableUBO) * drawableUBOVector.size(); if (!drawableUniformBuffer || drawableUniformBuffer->getSize() < drawableUBOVectorSize) { - drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false); + drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false, true); } else { drawableUniformBuffer->update(drawableUBOVector.data(), drawableUBOVectorSize); } const size_t tilePropsUBOVectorSize = sizeof(FillExtrusionTilePropsUBO) * tilePropsUBOVector.size(); if (!tilePropsUniformBuffer || tilePropsUniformBuffer->getSize() < tilePropsUBOVectorSize) { - tilePropsUniformBuffer = context.createUniformBuffer(tilePropsUBOVector.data(), tilePropsUBOVectorSize, false); + tilePropsUniformBuffer = context.createUniformBuffer(tilePropsUBOVector.data(), tilePropsUBOVectorSize, false, true); } else { tilePropsUniformBuffer->update(tilePropsUBOVector.data(), tilePropsUBOVectorSize); } diff --git a/src/mbgl/renderer/layers/fill_layer_tweaker.cpp b/src/mbgl/renderer/layers/fill_layer_tweaker.cpp index 5060fd91bc5..8530629f11d 100644 --- a/src/mbgl/renderer/layers/fill_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/fill_layer_tweaker.cpp @@ -101,8 +101,9 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters textureSize = tex->getSize(); } +#if !MLN_UBO_CONSOLIDATION auto& drawableUniforms = drawable.mutableUniformBuffers(); - +#endif switch (static_cast(drawable.getType())) { case RenderFillLayer::FillVariant::Fill: { #if MLN_UBO_CONSOLIDATION @@ -251,14 +252,14 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters #if MLN_UBO_CONSOLIDATION const size_t drawableUBOVectorSize = sizeof(FillDrawableUnionUBO) * drawableUBOVector.size(); if (!drawableUniformBuffer || drawableUniformBuffer->getSize() < drawableUBOVectorSize) { - drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false); + drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false, true); } else { drawableUniformBuffer->update(drawableUBOVector.data(), drawableUBOVectorSize); } const size_t tilePropsUBOVectorSize = sizeof(FillTilePropsUnionUBO) * tilePropsUBOVector.size(); if (!tilePropsUniformBuffer || tilePropsUniformBuffer->getSize() < tilePropsUBOVectorSize) { - tilePropsUniformBuffer = context.createUniformBuffer(tilePropsUBOVector.data(), tilePropsUBOVectorSize, false); + tilePropsUniformBuffer = context.createUniformBuffer(tilePropsUBOVector.data(), tilePropsUBOVectorSize, false, true); } else { tilePropsUniformBuffer->update(tilePropsUBOVector.data(), tilePropsUBOVectorSize); } diff --git a/src/mbgl/renderer/layers/heatmap_layer_tweaker.cpp b/src/mbgl/renderer/layers/heatmap_layer_tweaker.cpp index 7cbff4afabe..5572a8ba1ac 100644 --- a/src/mbgl/renderer/layers/heatmap_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/heatmap_layer_tweaker.cpp @@ -93,12 +93,12 @@ void HeatmapLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamet #if MLN_UBO_CONSOLIDATION const size_t drawableUBOVectorSize = sizeof(HeatmapDrawableUBO) * drawableUBOVector.size(); if (!drawableUniformBuffer || drawableUniformBuffer->getSize() < drawableUBOVectorSize) { - drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false); + drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false, true); } else { drawableUniformBuffer->update(drawableUBOVector.data(), drawableUBOVectorSize); } - layerUniforms.set(idSymbolDrawableUBO, drawableUniformBuffer); + layerUniforms.set(idHeatmapDrawableUBO, drawableUniformBuffer); #endif } diff --git a/src/mbgl/renderer/layers/hillshade_layer_tweaker.cpp b/src/mbgl/renderer/layers/hillshade_layer_tweaker.cpp index ac486d9a00a..db085271f14 100644 --- a/src/mbgl/renderer/layers/hillshade_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/hillshade_layer_tweaker.cpp @@ -100,14 +100,14 @@ void HillshadeLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParam auto& context = parameters.context; const size_t drawableUBOVectorSize = sizeof(HillshadeDrawableUBO) * drawableUBOVector.size(); if (!drawableUniformBuffer || drawableUniformBuffer->getSize() < drawableUBOVectorSize) { - drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false); + drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false, true); } else { drawableUniformBuffer->update(drawableUBOVector.data(), drawableUBOVectorSize); } const size_t tilePropsUBOVectorSize = sizeof(HillshadeTilePropsUBO) * tilePropsUBOVector.size(); if (!tilePropsUniformBuffer || tilePropsUniformBuffer->getSize() < tilePropsUBOVectorSize) { - tilePropsUniformBuffer = context.createUniformBuffer(tilePropsUBOVector.data(), tilePropsUBOVectorSize, false); + tilePropsUniformBuffer = context.createUniformBuffer(tilePropsUBOVector.data(), tilePropsUBOVectorSize, false, true); } else { tilePropsUniformBuffer->update(tilePropsUBOVector.data(), tilePropsUBOVectorSize); } diff --git a/src/mbgl/renderer/layers/line_layer_tweaker.cpp b/src/mbgl/renderer/layers/line_layer_tweaker.cpp index 87b7b432487..87596e1f7c6 100644 --- a/src/mbgl/renderer/layers/line_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/line_layer_tweaker.cpp @@ -187,7 +187,9 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters const auto matrix = getTileMatrix( tileID, parameters, translation, anchor, nearClipped, inViewportPixelUnits, drawable); +#if !MLN_UBO_CONSOLIDATION auto& drawableUniforms = drawable.mutableUniformBuffers(); +#endif switch (static_cast(drawable.getType())) { case LineType::Simple: { #if MLN_UBO_CONSOLIDATION @@ -362,14 +364,14 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters #if MLN_UBO_CONSOLIDATION const size_t drawableUBOVectorSize = sizeof(LineDrawableUnionUBO) * drawableUBOVector.size(); if (!drawableUniformBuffer || drawableUniformBuffer->getSize() < drawableUBOVectorSize) { - drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false); + drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false, true); } else { drawableUniformBuffer->update(drawableUBOVector.data(), drawableUBOVectorSize); } const size_t tilePropsUBOVectorSize = sizeof(LineTilePropsUnionUBO) * tilePropsUBOVector.size(); if (!tilePropsUniformBuffer || tilePropsUniformBuffer->getSize() < tilePropsUBOVectorSize) { - tilePropsUniformBuffer = context.createUniformBuffer(tilePropsUBOVector.data(), tilePropsUBOVectorSize, false); + tilePropsUniformBuffer = context.createUniformBuffer(tilePropsUBOVector.data(), tilePropsUBOVectorSize, false, true); } else { tilePropsUniformBuffer->update(tilePropsUBOVector.data(), tilePropsUBOVectorSize); } diff --git a/src/mbgl/renderer/layers/raster_layer_tweaker.cpp b/src/mbgl/renderer/layers/raster_layer_tweaker.cpp index 6b7fb5b1ea1..0303017fc19 100644 --- a/src/mbgl/renderer/layers/raster_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/raster_layer_tweaker.cpp @@ -119,7 +119,7 @@ void RasterLayerTweaker::execute([[maybe_unused]] LayerGroupBase& layerGroup, auto& context = parameters.context; const size_t drawableUBOVectorSize = sizeof(RasterDrawableUBO) * drawableUBOVector.size(); if (!drawableUniformBuffer || drawableUniformBuffer->getSize() < drawableUBOVectorSize) { - drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false); + drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false, true); } else { drawableUniformBuffer->update(drawableUBOVector.data(), drawableUBOVectorSize); } diff --git a/src/mbgl/renderer/layers/symbol_layer_tweaker.cpp b/src/mbgl/renderer/layers/symbol_layer_tweaker.cpp index 4455fc9626d..e2e7a8ea781 100644 --- a/src/mbgl/renderer/layers/symbol_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/symbol_layer_tweaker.cpp @@ -203,14 +203,14 @@ void SymbolLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamete #if MLN_UBO_CONSOLIDATION const size_t drawableUBOVectorSize = sizeof(SymbolDrawableUBO) * drawableUBOVector.size(); if (!drawableUniformBuffer || drawableUniformBuffer->getSize() < drawableUBOVectorSize) { - drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false); + drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false, true); } else { drawableUniformBuffer->update(drawableUBOVector.data(), drawableUBOVectorSize); } const size_t tilePropsUBOVectorSize = sizeof(SymbolTilePropsUBO) * tilePropsUBOVector.size(); if (!tilePropsUniformBuffer || tilePropsUniformBuffer->getSize() < tilePropsUBOVectorSize) { - tilePropsUniformBuffer = context.createUniformBuffer(tilePropsUBOVector.data(), tilePropsUBOVectorSize, false); + tilePropsUniformBuffer = context.createUniformBuffer(tilePropsUBOVector.data(), tilePropsUBOVectorSize, false, true); } else { tilePropsUniformBuffer->update(tilePropsUBOVector.data(), tilePropsUBOVectorSize); } diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp index ff10be6dd62..bd3631d4af0 100644 --- a/src/mbgl/renderer/renderer_impl.cpp +++ b/src/mbgl/renderer/renderer_impl.cpp @@ -251,17 +251,20 @@ void Renderer::Impl::render(const RenderTree& renderTree, const auto debugGroup = uploadPass->createDebugGroup("layerGroup-upload"); #endif + // Update the debug layer groups + orchestrator.updateDebugLayerGroups(renderTree, parameters); + // Tweakers are run in the upload pass so they can set up uniforms. parameters.currentLayer = 0; orchestrator.visitLayerGroups([&](LayerGroupBase& layerGroup) { layerGroup.runTweakers(renderTree, parameters); parameters.currentLayer++; }); - orchestrator.visitDebugLayerGroups( - [&](LayerGroupBase& layerGroup) { layerGroup.runTweakers(renderTree, parameters); }); - - // Update the debug layer groups - orchestrator.updateDebugLayerGroups(renderTree, parameters); + parameters.currentLayer = 0; + orchestrator.visitDebugLayerGroups([&](LayerGroupBase& layerGroup) { + layerGroup.runTweakers(renderTree, parameters); + parameters.currentLayer++; + }); // Give the layers a chance to upload orchestrator.visitLayerGroups([&](LayerGroupBase& layerGroup) { layerGroup.upload(*uploadPass); }); @@ -458,8 +461,11 @@ void Renderer::Impl::render(const RenderTree& renderTree, // Renders debug overlays. { const auto debugGroup(parameters.renderPass->createDebugGroup("debug")); - orchestrator.visitDebugLayerGroups( - [&](LayerGroupBase& layerGroup) { layerGroup.render(orchestrator, parameters); }); + parameters.currentLayer = 0; + orchestrator.visitDebugLayerGroups([&](LayerGroupBase& layerGroup) { + layerGroup.render(orchestrator, parameters); + parameters.currentLayer++; + }); } }; #endif // MLN_DRAWABLE_RENDERER diff --git a/src/mbgl/renderer/sources/render_tile_source.cpp b/src/mbgl/renderer/sources/render_tile_source.cpp index 82a48a63d92..5a0ebe4b8a1 100644 --- a/src/mbgl/renderer/sources/render_tile_source.cpp +++ b/src/mbgl/renderer/sources/render_tile_source.cpp @@ -56,7 +56,7 @@ void TileSourceRenderItem::render(PaintParameters& parameters) const { #if MLN_DRAWABLE_RENDERER -#if MLN_RENDER_BACKEND_VULKAN +#if MLN_ENABLE_POLYLINE_DRAWABLES class PolylineLayerImpl : public Layer::Impl { public: PolylineLayerImpl() @@ -88,14 +88,15 @@ class PolylineLayerTweaker : public LayerTweaker { public: PolylineLayerTweaker(const shaders::LineEvaluatedPropsUBO& properties) : LayerTweaker("debug-polyline", makeMutable()), - linePropertiesUBO(properties) {} + propsUBO(properties) {} void execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) override { + auto& context = parameters.context; auto& layerUniforms = layerGroup.mutableUniformBuffers(); - layerUniforms.createOrUpdate(idLineEvaluatedPropsUBO, &linePropertiesUBO, parameters.context); + layerUniforms.createOrUpdate(idLineEvaluatedPropsUBO, &propsUBO, context); // We would need to set up `idLineExpressionUBO` if the expression mask isn't empty - assert(linePropertiesUBO.expressionMask == LineExpressionMask::None); + assert(propsUBO.expressionMask == LineExpressionMask::None); const LineExpressionUBO exprUBO = { /* .color = */ nullptr, @@ -106,11 +107,66 @@ class PolylineLayerTweaker : public LayerTweaker { /* .width = */ nullptr, /* .floorWidth = */ nullptr, }; - layerUniforms.createOrUpdate(idLineExpressionUBO, &exprUBO, parameters.context); + layerUniforms.createOrUpdate(idLineExpressionUBO, &exprUBO, context); + +#if MLN_UBO_CONSOLIDATION + int i = 0; + std::vector drawableUBOVector(layerGroup.getDrawableCount()); +#endif + visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) { + if (!drawable.getTileID().has_value()) { + return; + } + + const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped(); + const auto zoom = parameters.state.getZoom(); + mat4 tileMatrix; + parameters.state.matrixFor(/*out*/ tileMatrix, tileID); + + const auto matrix = LayerTweaker::getTileMatrix( + tileID, parameters, {{0, 0}}, style::TranslateAnchorType::Viewport, false, false, drawable, false); + +#if MLN_UBO_CONSOLIDATION + drawableUBOVector[i].lineDrawableUBO = { +#else + const shaders::LineDrawableUBO drawableUBO = { +#endif + /* .matrix = */ util::cast(matrix), + /* .ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom), + + /* .color_t = */ 0.f, + /* .blur_t = */ 0.f, + /* .opacity_t = */ 0.f, + /* .gapwidth_t = */ 0.f, + /* .offset_t = */ 0.f, + /* .width_t = */ 0.f, + /* .pad1 = */ 0}; + +#if MLN_UBO_CONSOLIDATION + drawable.setUBOIndex(i++); +#else + auto& drawableUniforms = drawable.mutableUniformBuffers(); + drawableUniforms.createOrUpdate(idLineDrawableUBO, &drawableUBO, context); +#endif + }); + +#if MLN_UBO_CONSOLIDATION + const size_t drawableUBOVectorSize = sizeof(LineDrawableUnionUBO) * drawableUBOVector.size(); + if (!drawableUniformBuffer || drawableUniformBuffer->getSize() < drawableUBOVectorSize) { + drawableUniformBuffer = context.createUniformBuffer(drawableUBOVector.data(), drawableUBOVectorSize, false, true); + } else { + drawableUniformBuffer->update(drawableUBOVector.data(), drawableUBOVectorSize); + } + layerUniforms.set(idLineDrawableUBO, drawableUniformBuffer); +#endif } private: - shaders::LineEvaluatedPropsUBO linePropertiesUBO; + shaders::LineEvaluatedPropsUBO propsUBO; + +#if MLN_UBO_CONSOLIDATION + gfx::UniformBufferPtr drawableUniformBuffer; +#endif }; #endif @@ -264,64 +320,6 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr // function to add polylines drawable const auto addPolylineDrawable = [&](TileLayerGroup* tileLayerGroup, const RenderTile& tile) { - class PolylineDrawableTweaker : public gfx::DrawableTweaker { - public: - PolylineDrawableTweaker(const shaders::LineEvaluatedPropsUBO& properties) - : linePropertiesUBO(properties) {} - ~PolylineDrawableTweaker() override = default; - - void init(gfx::Drawable&) override {} - - void execute(gfx::Drawable& drawable, const PaintParameters& parameters) override { - if (!drawable.getTileID().has_value()) { - return; - } - - const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped(); - const auto zoom = parameters.state.getZoom(); - mat4 tileMatrix; - parameters.state.matrixFor(/*out*/ tileMatrix, tileID); - - const auto matrix = LayerTweaker::getTileMatrix( - tileID, parameters, {{0, 0}}, style::TranslateAnchorType::Viewport, false, false, drawable, false); - - const shaders::LineDrawableUBO drawableUBO = { - /* .matrix = */ util::cast(matrix), - /* .ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom), - - /* .color_t = */ 0.f, - /* .blur_t = */ 0.f, - /* .opacity_t = */ 0.f, - /* .gapwidth_t = */ 0.f, - /* .offset_t = */ 0.f, - /* .width_t = */ 0.f, - /* .pad1 = */ 0}; - auto& drawableUniforms = drawable.mutableUniformBuffers(); - drawableUniforms.createOrUpdate(idLineDrawableUBO, &drawableUBO, parameters.context); - -#if !MLN_RENDER_BACKEND_VULKAN - drawableUniforms.createOrUpdate(idLineEvaluatedPropsUBO, &linePropertiesUBO, parameters.context); - - // We would need to set up `idLineExpressionUBO` if the expression mask isn't empty - assert(linePropertiesUBO.expressionMask == LineExpressionMask::None); - - const LineExpressionUBO exprUBO = { - /* .color = */ nullptr, - /* .blur = */ nullptr, - /* .opacity = */ nullptr, - /* .gapwidth = */ nullptr, - /* .offset = */ nullptr, - /* .width = */ nullptr, - /* .floorWidth = */ nullptr, - }; - drawableUniforms.createOrUpdate(idLineExpressionUBO, &exprUBO, parameters.context); -#endif - }; - - private: - shaders::LineEvaluatedPropsUBO linePropertiesUBO; - }; - GeometryCoordinates coords{{0, 0}, {util::EXTENT, 0}, {util::EXTENT, util::EXTENT}, {0, util::EXTENT}, {0, 0}}; gfx::PolylineGeneratorOptions options; options.type = FeatureType::Polygon; @@ -332,24 +330,17 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr } polylineBuilder->addPolyline(coords, options); -#if MLN_RENDER_BACKEND_VULKAN - if (!layerTweaker) { - layerTweaker = std::make_shared(linePropertiesUBO); - layerTweaker->execute(*tileLayerGroup, parameters); - tileLayerGroup->addLayerTweaker(layerTweaker); - } -#endif - - // create line tweaker; - auto tweaker = std::make_shared(linePropertiesUBO); - // finish polylineBuilder->flush(context); for (auto& drawable : polylineBuilder->clearDrawables()) { drawable->setTileID(tile.getOverscaledTileID()); - drawable->addTweaker(tweaker); tileLayerGroup->addDrawable(renderPass, tile.getOverscaledTileID(), std::move(drawable)); } + + if (!layerTweaker) { + layerTweaker = std::make_shared(linePropertiesUBO); + tileLayerGroup->addLayerTweaker(layerTweaker); + } }; #endif @@ -432,16 +423,20 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr const auto& debugBucket = tile.debugBucket; if (!debugBucket) continue; - const DebugUBO debugUBO = {/* .matrix = */ util::cast(tile.matrix), - /* .color = */ Color::red(), - /* .overlay_scale = */ 1.0f, - /* .pad1 = */ 0, - /* .pad2 = */ 0, - /* .pad3 = */ 0}; - if (0 == updateDrawables(tileLayerGroup, tileID, debugUBO) && tile.getNeedsRendering()) { #if MLN_ENABLE_POLYLINE_DRAWABLES + if (0 == tileLayerGroup->getDrawableCount(renderPass, tileID) && tile.getNeedsRendering()) { addPolylineDrawable(tileLayerGroup, tile); + } #else + const DebugUBO debugUBO = { + /* .matrix = */ util::cast(tile.matrix), + /* .color = */ Color::red(), + /* .overlay_scale = */ 1.0f, + /* .pad1 = */ 0, + /* .pad2 = */ 0, + /* .pad3 = */ 0}; + + if (0 == updateDrawables(tileLayerGroup, tileID, debugUBO) && tile.getNeedsRendering()) { addDrawable(tileLayerGroup, tileID, debugUBO, @@ -449,8 +444,8 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr vertices, indexes, segments); -#endif } +#endif } } else { // if tile borders are not required, erase layer group diff --git a/src/mbgl/style/layers/custom_drawable_layer.cpp b/src/mbgl/style/layers/custom_drawable_layer.cpp index 351258aa618..45477520a95 100644 --- a/src/mbgl/style/layers/custom_drawable_layer.cpp +++ b/src/mbgl/style/layers/custom_drawable_layer.cpp @@ -90,7 +90,7 @@ const LayerTypeInfo* CustomDrawableLayer::Impl::staticTypeInfo() noexcept { class LineDrawableTweaker : public gfx::DrawableTweaker { public: LineDrawableTweaker(const shaders::LineEvaluatedPropsUBO& properties) - : linePropertiesUBO(properties) {} + : propsUBO(properties) {} ~LineDrawableTweaker() override = default; void init(gfx::Drawable&) override {} @@ -108,26 +108,44 @@ class LineDrawableTweaker : public gfx::DrawableTweaker { const auto matrix = LayerTweaker::getTileMatrix( tileID, parameters, {{0, 0}}, style::TranslateAnchorType::Viewport, false, false, drawable, false); - const shaders::LineDrawableUBO drawableUBO = {/* .matrix = */ util::cast(matrix), - /* .ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom), - - /* .color_t = */ 0.f, - /* .blur_t = */ 0.f, - /* .opacity_t = */ 0.f, - /* .gapwidth_t = */ 0.f, - /* .offset_t = */ 0.f, - /* .width_t = */ 0.f, - /* .pad1 = */ 0}; +#if MLN_UBO_CONSOLIDATION + shaders::LineDrawableUnionUBO drawableUBO; + drawableUBO.lineDrawableUBO = { +#else + const shaders::LineDrawableUBO drawableUBO = { +#endif + /* .matrix = */ util::cast(matrix), + /* .ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom), + + /* .color_t = */ 0.f, + /* .blur_t = */ 0.f, + /* .opacity_t = */ 0.f, + /* .gapwidth_t = */ 0.f, + /* .offset_t = */ 0.f, + /* .width_t = */ 0.f, + /* .pad1 = */ 0 + }; auto& drawableUniforms = drawable.mutableUniformBuffers(); - drawableUniforms.createOrUpdate(idLineDrawableUBO, &drawableUBO, parameters.context); - drawableUniforms.createOrUpdate(idLineEvaluatedPropsUBO, &linePropertiesUBO, parameters.context); + drawableUniforms.createOrUpdate(idLineDrawableUBO, &drawableUBO, parameters.context, true); + drawableUniforms.createOrUpdate(idLineEvaluatedPropsUBO, &propsUBO, parameters.context); // We would need to set up `idLineExpressionUBO` if the expression mask isn't empty - assert(linePropertiesUBO.expressionMask == LineExpressionMask::None); + assert(propsUBO.expressionMask == LineExpressionMask::None); + + const LineExpressionUBO exprUBO = { + /* .color = */ nullptr, + /* .blur = */ nullptr, + /* .opacity = */ nullptr, + /* .gapwidth = */ nullptr, + /* .offset = */ nullptr, + /* .width = */ nullptr, + /* .floorWidth = */ nullptr, + }; + drawableUniforms.createOrUpdate(idLineExpressionUBO, &exprUBO, parameters.context); }; private: - shaders::LineEvaluatedPropsUBO linePropertiesUBO; + shaders::LineEvaluatedPropsUBO propsUBO; }; class WideVectorDrawableTweaker : public gfx::DrawableTweaker { @@ -216,22 +234,31 @@ class FillDrawableTweaker : public gfx::DrawableTweaker { const auto matrix = LayerTweaker::getTileMatrix( tileID, parameters, {{0, 0}}, style::TranslateAnchorType::Viewport, false, false, drawable, false); - const shaders::FillDrawableUBO fillDrawableUBO = {/* .matrix = */ util::cast(matrix), - - /* .color_t = */ 0.f, - /* .opacity_t = */ 0.f, - /* .pad1 = */ 0, - /* .pad2 = */ 0}; +#if MLN_UBO_CONSOLIDATION + shaders::FillDrawableUnionUBO drawableUBO; + drawableUBO.fillDrawableUBO = { +#else + const shaders::FillDrawableUBO drawableUBO = { +#endif + /* .matrix = */ util::cast(matrix), + + /* .color_t = */ 0.f, + /* .opacity_t = */ 0.f, + /* .pad1 = */ 0, + /* .pad2 = */ 0 + }; - const shaders::FillEvaluatedPropsUBO fillPropertiesUBO = {/* .color = */ color, - /* .outline_color = */ Color::white(), - /* .opacity = */ opacity, - /* .fade = */ 0.f, - /* .from_scale = */ 0.f, - /* .to_scale = */ 0.f}; + const shaders::FillEvaluatedPropsUBO propsUBO = { + /* .color = */ color, + /* .outline_color = */ Color::white(), + /* .opacity = */ opacity, + /* .fade = */ 0.f, + /* .from_scale = */ 0.f, + /* .to_scale = */ 0.f + }; auto& drawableUniforms = drawable.mutableUniformBuffers(); - drawableUniforms.createOrUpdate(idFillDrawableUBO, &fillDrawableUBO, parameters.context); - drawableUniforms.createOrUpdate(idFillEvaluatedPropsUBO, &fillPropertiesUBO, parameters.context); + drawableUniforms.createOrUpdate(idFillDrawableUBO, &drawableUBO, parameters.context); + drawableUniforms.createOrUpdate(idFillEvaluatedPropsUBO, &propsUBO, parameters.context); }; private: @@ -396,7 +423,6 @@ bool CustomDrawableLayerHost::Interface::addPolyline(const GeometryCoordinates& } bool CustomDrawableLayerHost::Interface::addFill(const GeometryCollection& geometry) { - return true; // build fill if (!updateBuilder(BuilderType::Fill, "custom-fill", fillShaderDefault())) return false; @@ -492,7 +518,7 @@ bool CustomDrawableLayerHost::Interface::addSymbol(const GeometryCoordinate& poi builder->setTexture(symbolOptions.texture, idCustomSymbolImageTexture); } - // create fill tweaker + // create symbol tweaker auto tweaker = std::make_shared(symbolOptions); builder->addTweaker(tweaker); diff --git a/src/mbgl/vulkan/buffer_resource.cpp b/src/mbgl/vulkan/buffer_resource.cpp index 2f9ccb4a45b..bff075701c5 100644 --- a/src/mbgl/vulkan/buffer_resource.cpp +++ b/src/mbgl/vulkan/buffer_resource.cpp @@ -56,13 +56,13 @@ BufferResource::BufferResource( std::size_t totalSize = size; // TODO -> check avg minUniformBufferOffsetAlignment vs individual buffers - if (usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) { + if (usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT || usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) { const auto& backend = context.getBackend(); const auto& deviceProps = backend.getDeviceProperties(); const auto& align = deviceProps.limits.minUniformBufferOffsetAlignment; bufferWindowSize = (size + align - 1) & ~(align - 1); - assert(bufferWindowSize != 0); + //assert(bufferWindowSize != 0); totalSize = bufferWindowSize * backend.getMaxFrames(); } diff --git a/src/mbgl/vulkan/context.cpp b/src/mbgl/vulkan/context.cpp index 1b6f97b9f5b..5e0dacd4f2b 100644 --- a/src/mbgl/vulkan/context.cpp +++ b/src/mbgl/vulkan/context.cpp @@ -51,7 +51,7 @@ class RenderbufferResource : public gfx::RenderbufferResource { Context::Context(RendererBackend& backend_) : gfx::Context(vulkan::maximumVertexBindingCount), backend(backend_), - globalUniformBuffers(DescriptorSetType::Global, 0, shaders::globalUBOCount) { + globalUniformBuffers(DescriptorSetType::Global, 0, 0, shaders::globalUBOCount) { if (glslangRefCount++ == 0) { glslang::InitializeProcess(); } @@ -73,19 +73,21 @@ void Context::initFrameResources() { const auto& device = backend.getDevice(); const auto frameCount = backend.getMaxFrames(); - descriptorPoolMap.emplace(DescriptorSetType::Global, - DescriptorPoolGrowable(globalDescriptorPoolSize, shaders::globalUBOCount)); + descriptorPoolMap.emplace( + DescriptorSetType::Global, + DescriptorPoolGrowable(globalDescriptorPoolSize, 0, shaders::globalUBOCount, 0)); - descriptorPoolMap.emplace(DescriptorSetType::Layer, - DescriptorPoolGrowable(layerDescriptorPoolSize, shaders::maxUBOCountPerLayer)); + descriptorPoolMap.emplace( + DescriptorSetType::Layer, + DescriptorPoolGrowable(layerDescriptorPoolSize, shaders::maxSSBOCountPerLayer, shaders::maxUBOCountPerLayer, 0)); descriptorPoolMap.emplace( DescriptorSetType::DrawableUniform, - DescriptorPoolGrowable(drawableUniformDescriptorPoolSize, shaders::maxUBOCountPerDrawable)); + DescriptorPoolGrowable(drawableUniformDescriptorPoolSize, 0, shaders::maxUBOCountPerDrawable, 0)); descriptorPoolMap.emplace( DescriptorSetType::DrawableImage, - DescriptorPoolGrowable(drawableImageDescriptorPoolSize, shaders::maxTextureCountPerShader)); + DescriptorPoolGrowable(drawableImageDescriptorPoolSize, 0, 0, shaders::maxTextureCountPerShader)); // command buffers const vk::CommandBufferAllocateInfo allocateInfo( @@ -113,13 +115,15 @@ void Context::initFrameResources() { (void)getDummyTexture(); buildUniformDescriptorSetLayout( - globalUniformDescriptorSetLayout, 0, shaders::globalUBOCount, "GlobalUniformDescriptorSetLayout"); + globalUniformDescriptorSetLayout, 0, 0, shaders::globalUBOCount, "GlobalUniformDescriptorSetLayout"); buildUniformDescriptorSetLayout(layerUniformDescriptorSetLayout, - shaders::layerUBOStartId, + shaders::globalUBOCount, + shaders::maxSSBOCountPerLayer, shaders::maxUBOCountPerLayer, "LayerUniformDescriptorSetLayout"); buildUniformDescriptorSetLayout(drawableUniformDescriptorSetLayout, shaders::globalUBOCount, + 0, shaders::maxUBOCountPerDrawable, "DrawableUniformDescriptorSetLayout"); buildImageDescriptorSetLayout(); @@ -357,8 +361,8 @@ gfx::UniqueDrawableBuilder Context::createDrawableBuilder(std::string name) { return std::make_unique(std::move(name)); } -gfx::UniformBufferPtr Context::createUniformBuffer(const void* data, std::size_t size, bool persistent) { - return std::make_shared(createBuffer(data, size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, persistent)); +gfx::UniformBufferPtr Context::createUniformBuffer(const void* data, std::size_t size, bool persistent, bool ssbo) { + return std::make_shared(createBuffer(data, size, ssbo ? VK_BUFFER_USAGE_STORAGE_BUFFER_BIT : VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, persistent)); } gfx::ShaderProgramBasePtr Context::getGenericShader(gfx::ShaderRegistry& shaders, const std::string& name) { @@ -572,6 +576,12 @@ const std::unique_ptr& Context::getDummyUniformBuffer() { return dummyUniformBuffer; } +const std::unique_ptr& Context::getDummyStorageBuffer() { + if (!dummyStorageBuffer) + dummyStorageBuffer = std::make_unique(*this, nullptr, 16, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, false); + return dummyStorageBuffer; +} + const std::unique_ptr& Context::getDummyTexture() { if (!dummyTexture2D) { const Size size(2, 2); @@ -590,23 +600,26 @@ const std::unique_ptr& Context::getDummyTexture() { } void Context::buildUniformDescriptorSetLayout(vk::UniqueDescriptorSetLayout& layout, - size_t uniformStartId, + size_t startId, + size_t storageCount, size_t uniformCount, const std::string& name) { std::vector bindings; - for (size_t i = 0; i < uniformCount; ++i) { + for (size_t i = 0; i < storageCount + uniformCount; ++i) { auto stageFlags = vk::ShaderStageFlags(); - if (uniformStartId + i != shaders::idDrawableReservedFragmentOnlyUBO) { + if (startId + i != shaders::idDrawableReservedFragmentOnlyUBO) { stageFlags |= vk::ShaderStageFlagBits::eVertex; } - if (uniformStartId + i != shaders::idDrawableReservedVertexOnlyUBO) { + if (startId + i != shaders::idDrawableReservedVertexOnlyUBO) { stageFlags |= vk::ShaderStageFlagBits::eFragment; } + const auto descriptorType = i < storageCount ? vk::DescriptorType::eStorageBuffer : vk::DescriptorType::eUniformBuffer; + bindings.push_back(vk::DescriptorSetLayoutBinding() .setBinding(static_cast(i)) .setStageFlags(stageFlags) - .setDescriptorType(vk::DescriptorType::eUniformBuffer) + .setDescriptorType(descriptorType) .setDescriptorCount(1)); } @@ -661,6 +674,9 @@ DescriptorPoolGrowable& Context::getDescriptorPool(DescriptorSetType type) { const vk::UniquePipelineLayout& Context::getGeneralPipelineLayout() { if (generalPipelineLayout) return generalPipelineLayout; + const auto stages = vk::ShaderStageFlags() | vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment; + const auto pushConstant = vk::PushConstantRange().setSize(sizeof(uint32_t)).setStageFlags(stages); + const std::vector layouts = { globalUniformDescriptorSetLayout.get(), layerUniformDescriptorSetLayout.get(), @@ -669,7 +685,7 @@ const vk::UniquePipelineLayout& Context::getGeneralPipelineLayout() { }; generalPipelineLayout = backend.getDevice()->createPipelineLayoutUnique( - vk::PipelineLayoutCreateInfo().setSetLayouts(layouts)); + vk::PipelineLayoutCreateInfo().setPushConstantRanges(pushConstant).setSetLayouts(layouts)); backend.setDebugName(generalPipelineLayout.get(), "PipelineLayout_general"); diff --git a/src/mbgl/vulkan/descriptor_set.cpp b/src/mbgl/vulkan/descriptor_set.cpp index f27f0a23c3e..8e13302b6f0 100644 --- a/src/mbgl/vulkan/descriptor_set.cpp +++ b/src/mbgl/vulkan/descriptor_set.cpp @@ -36,10 +36,6 @@ void DescriptorSet::createDescriptorPool(DescriptorPoolGrowable& growablePool) { const uint32_t maxSets = static_cast(growablePool.maxSets * std::pow(growablePool.growFactor, growablePool.pools.size())); - const vk::DescriptorPoolSize size = {type != DescriptorSetType::DrawableImage - ? vk::DescriptorType::eUniformBuffer - : vk::DescriptorType::eCombinedImageSampler, - maxSets * growablePool.descriptorsPerSet}; #ifdef USE_DESCRIPTOR_POOL_RESET const auto poolFlags = vk::DescriptorPoolCreateFlags(); @@ -47,7 +43,19 @@ void DescriptorSet::createDescriptorPool(DescriptorPoolGrowable& growablePool) { const auto poolFlags = vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet; #endif - const auto descriptorPoolInfo = vk::DescriptorPoolCreateInfo(poolFlags).setPoolSizes(size).setMaxSets(maxSets); + + std::vector sizes; + if (growablePool.descriptorStoragePerSet > 0) { + sizes.emplace_back(vk::DescriptorPoolSize(vk::DescriptorType::eStorageBuffer, maxSets * growablePool.descriptorStoragePerSet)); + } + if (growablePool.descriptorUniformsPerSet > 0) { + sizes.emplace_back(vk::DescriptorPoolSize(vk::DescriptorType::eUniformBuffer, maxSets * growablePool.descriptorUniformsPerSet)); + } + if (growablePool.descriptorTexturesPerSet > 0) { + sizes.emplace_back(vk::DescriptorPoolSize(vk::DescriptorType::eCombinedImageSampler, maxSets * growablePool.descriptorTexturesPerSet)); + } + + const auto descriptorPoolInfo = vk::DescriptorPoolCreateInfo(poolFlags).setPoolSizes(sizes).setMaxSets(maxSets); growablePool.pools.emplace_back(device->createDescriptorPoolUnique(descriptorPoolInfo), maxSets); growablePool.currentPoolIndex = static_cast(growablePool.pools.size() - 1); @@ -131,8 +139,9 @@ UniformDescriptorSet::UniformDescriptorSet(Context& context_, DescriptorSetType : DescriptorSet(context_, type_) {} void UniformDescriptorSet::update(const gfx::UniformBufferArray& uniforms, - uint32_t uniformStartIndex, - uint32_t descriptorBindingCount) { + uint32_t descriptorStartIndex, + uint32_t descriptorStorageCount, + uint32_t descriptorUniformCount) { MLN_TRACE_FUNC(); allocate(); @@ -144,25 +153,28 @@ void UniformDescriptorSet::update(const gfx::UniformBufferArray& uniforms, const auto& device = context.getBackend().getDevice(); - for (size_t index = 0; index < descriptorBindingCount; ++index) { + for (size_t index = 0; index < descriptorStorageCount + descriptorUniformCount; ++index) { vk::DescriptorBufferInfo descriptorBufferInfo; - if (const auto& uniformBuffer = uniforms.get(uniformStartIndex + index)) { + if (const auto& uniformBuffer = uniforms.get(descriptorStartIndex + index)) { const auto& uniformBufferImpl = static_cast(*uniformBuffer); const auto& bufferResource = uniformBufferImpl.getBufferResource(); descriptorBufferInfo.setBuffer(bufferResource.getVulkanBuffer()) .setOffset(bufferResource.getVulkanBufferOffset()) .setRange(bufferResource.getSizeInBytes()); } else { - descriptorBufferInfo.setBuffer(context.getDummyUniformBuffer()->getVulkanBuffer()) + const auto& dummyBuffer = index < descriptorStorageCount ? context.getDummyStorageBuffer() : context.getDummyUniformBuffer(); + descriptorBufferInfo.setBuffer(dummyBuffer->getVulkanBuffer()) .setOffset(0) .setRange(VK_WHOLE_SIZE); } + const auto descriptorType = index < descriptorStorageCount ? vk::DescriptorType::eStorageBuffer : vk::DescriptorType::eUniformBuffer; + const auto writeDescriptorSet = vk::WriteDescriptorSet() .setBufferInfo(descriptorBufferInfo) .setDescriptorCount(1) - .setDescriptorType(vk::DescriptorType::eUniformBuffer) + .setDescriptorType(descriptorType) .setDstBinding(static_cast(index)) .setDstSet(descriptorSets[frameIndex]); diff --git a/src/mbgl/vulkan/drawable.cpp b/src/mbgl/vulkan/drawable.cpp index 64dcf681ac0..e3b0cf45fc7 100644 --- a/src/mbgl/vulkan/drawable.cpp +++ b/src/mbgl/vulkan/drawable.cpp @@ -253,6 +253,13 @@ void Drawable::draw(PaintParameters& parameters) const { if (!bindAttributes(encoder)) return; if (!bindDescriptors(encoder)) return; + commandBuffer->pushConstants( + context.getGeneralPipelineLayout().get(), + vk::ShaderStageFlags() | vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, + 0, + sizeof(uboIndex), + &uboIndex); + if (is3D) { impl->pipelineInfo.setDepthMode(impl->depthFor3D); impl->pipelineInfo.setStencilMode(impl->stencilFor3D); diff --git a/src/mbgl/vulkan/drawable_impl.hpp b/src/mbgl/vulkan/drawable_impl.hpp index b484ef533c1..432b93211b6 100644 --- a/src/mbgl/vulkan/drawable_impl.hpp +++ b/src/mbgl/vulkan/drawable_impl.hpp @@ -24,7 +24,7 @@ using namespace platform; class Drawable::Impl final { public: Impl() - : uniformBuffers(DescriptorSetType::DrawableUniform, shaders::globalUBOCount, shaders::maxUBOCountPerDrawable) { + : uniformBuffers(DescriptorSetType::DrawableUniform, shaders::globalUBOCount, 0, shaders::maxUBOCountPerDrawable) { } ~Impl() = default; diff --git a/src/mbgl/vulkan/layer_group.cpp b/src/mbgl/vulkan/layer_group.cpp index f86d556e037..0bbefe9f2ef 100644 --- a/src/mbgl/vulkan/layer_group.cpp +++ b/src/mbgl/vulkan/layer_group.cpp @@ -16,7 +16,7 @@ namespace vulkan { LayerGroup::LayerGroup(int32_t layerIndex_, std::size_t initialCapacity, std::string name_) : mbgl::LayerGroup(layerIndex_, initialCapacity, std::move(name_)), - uniformBuffers(DescriptorSetType::Layer, shaders::layerUBOStartId, shaders::maxUBOCountPerLayer) {} + uniformBuffers(DescriptorSetType::Layer, shaders::globalUBOCount, shaders::maxSSBOCountPerLayer, shaders::maxUBOCountPerLayer) {} void LayerGroup::upload(gfx::UploadPass& uploadPass) { if (!enabled) { diff --git a/src/mbgl/vulkan/tile_layer_group.cpp b/src/mbgl/vulkan/tile_layer_group.cpp index ac56147305e..16246a98942 100644 --- a/src/mbgl/vulkan/tile_layer_group.cpp +++ b/src/mbgl/vulkan/tile_layer_group.cpp @@ -17,7 +17,7 @@ namespace vulkan { TileLayerGroup::TileLayerGroup(int32_t layerIndex_, std::size_t initialCapacity, std::string name_) : mbgl::TileLayerGroup(layerIndex_, initialCapacity, std::move(name_)), - uniformBuffers(DescriptorSetType::Layer, shaders::layerUBOStartId, shaders::maxUBOCountPerLayer) {} + uniformBuffers(DescriptorSetType::Layer, shaders::globalUBOCount, shaders::maxSSBOCountPerLayer, shaders::maxUBOCountPerLayer) {} void TileLayerGroup::upload(gfx::UploadPass& uploadPass) { if (!enabled || !getDrawableCount()) { diff --git a/src/mbgl/vulkan/uniform_buffer.cpp b/src/mbgl/vulkan/uniform_buffer.cpp index b01e0facc90..6a4ee1e0e16 100644 --- a/src/mbgl/vulkan/uniform_buffer.cpp +++ b/src/mbgl/vulkan/uniform_buffer.cpp @@ -26,17 +26,17 @@ UniformBuffer::~UniformBuffer() { } void UniformBuffer::update(const void* data, std::size_t size_) { - assert(size == size_); + /*assert(size == size_); if (size != size_ || size != buffer.getSizeInBytes()) { Log::Error( Event::General, "Mismatched size given to UBO update, expected " + std::to_string(size) + ", got " + std::to_string(size_)); return; - } + }*/ buffer.getContext().renderingStats().numUniformUpdates++; buffer.getContext().renderingStats().uniformUpdateBytes += size_; - buffer.update(data, size, /*offset=*/0); + buffer.update(data, size_, /*offset=*/0); } const std::shared_ptr& UniformBufferArray::set(const size_t id, @@ -73,7 +73,7 @@ void UniformBufferArray::bindDescriptorSets(CommandEncoder& encoder) { descriptorSet = std::make_unique(encoder.getContext(), descriptorSetType); } - descriptorSet->update(*this, descriptorStartIndex, descriptorBindingCount); + descriptorSet->update(*this, descriptorStartIndex, descriptorStorageCount, descriptorUniformCount); descriptorSet->bind(encoder); }