From fd49a892c7976d66f6bebab80b5cf6b13a049468 Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Mon, 30 Sep 2024 16:08:27 +0300 Subject: [PATCH 01/11] Added global/layer/drawable descriptor sets --- include/mbgl/gfx/drawable.hpp | 1 + include/mbgl/gfx/uniform_block.hpp | 2 + include/mbgl/gfx/uniform_buffer.hpp | 4 + include/mbgl/shaders/background_layer_ubo.hpp | 6 - include/mbgl/shaders/circle_layer_ubo.hpp | 7 - include/mbgl/shaders/collision_layer_ubo.hpp | 5 - .../shaders/custom_drawable_layer_ubo.hpp | 6 - include/mbgl/shaders/debug_layer_ubo.hpp | 5 - .../mbgl/shaders/fill_extrusion_layer_ubo.hpp | 8 - include/mbgl/shaders/fill_layer_ubo.hpp | 8 - include/mbgl/shaders/heatmap_layer_ubo.hpp | 7 - .../shaders/heatmap_texture_layer_ubo.hpp | 5 - include/mbgl/shaders/hillshade_layer_ubo.hpp | 6 - .../shaders/hillshade_prepare_layer_ubo.hpp | 5 - include/mbgl/shaders/line_layer_ubo.hpp | 9 - include/mbgl/shaders/mtl/circle.hpp | 6 +- include/mbgl/shaders/mtl/fill_extrusion.hpp | 4 +- .../shaders/mtl/fill_extrusion_pattern.hpp | 10 +- include/mbgl/shaders/mtl/heatmap.hpp | 6 +- include/mbgl/shaders/raster_layer_ubo.hpp | 6 - include/mbgl/shaders/shader_defines.hpp | 197 +++++++++++++++--- include/mbgl/shaders/symbol_layer_ubo.hpp | 8 - include/mbgl/shaders/vulkan/background.hpp | 12 +- include/mbgl/shaders/vulkan/circle.hpp | 28 +-- include/mbgl/shaders/vulkan/collision.hpp | 8 +- include/mbgl/shaders/vulkan/common.hpp | 12 +- include/mbgl/shaders/vulkan/debug.hpp | 6 +- include/mbgl/shaders/vulkan/fill.hpp | 118 +++++------ include/mbgl/shaders/vulkan/heatmap.hpp | 26 +-- include/mbgl/shaders/vulkan/hillshade.hpp | 14 +- include/mbgl/shaders/vulkan/line.hpp | 48 ++--- include/mbgl/shaders/vulkan/raster.hpp | 10 +- include/mbgl/shaders/vulkan/symbol.hpp | 48 ++--- include/mbgl/shaders/vulkan/widevector.hpp | 8 +- include/mbgl/shaders/widevector_ubo.hpp | 6 - include/mbgl/vulkan/context.hpp | 35 +++- include/mbgl/vulkan/layer_group.hpp | 6 +- include/mbgl/vulkan/render_pass.hpp | 3 - include/mbgl/vulkan/tile_layer_group.hpp | 4 - src/mbgl/gfx/uniform_block.cpp | 2 + src/mbgl/shaders/gl/shader_info.cpp | 8 +- src/mbgl/shaders/mtl/circle.cpp | 2 +- src/mbgl/shaders/mtl/fill_extrusion.cpp | 2 +- .../shaders/mtl/fill_extrusion_pattern.cpp | 2 +- src/mbgl/shaders/mtl/heatmap.cpp | 2 +- src/mbgl/shaders/vulkan/background.cpp | 2 + src/mbgl/shaders/vulkan/circle.cpp | 4 +- src/mbgl/shaders/vulkan/collision.cpp | 2 + src/mbgl/shaders/vulkan/debug.cpp | 2 + src/mbgl/shaders/vulkan/fill.cpp | 7 +- src/mbgl/shaders/vulkan/heatmap.cpp | 5 +- src/mbgl/shaders/vulkan/hillshade.cpp | 3 + src/mbgl/shaders/vulkan/line.cpp | 2 + src/mbgl/shaders/vulkan/raster.cpp | 2 + src/mbgl/shaders/vulkan/symbol.cpp | 3 + src/mbgl/shaders/vulkan/widevector.cpp | 2 + src/mbgl/vulkan/context.cpp | 135 ++++++++++-- src/mbgl/vulkan/drawable.cpp | 59 ++---- src/mbgl/vulkan/layer_group.cpp | 27 +-- src/mbgl/vulkan/render_pass.cpp | 10 - src/mbgl/vulkan/tile_layer_group.cpp | 39 +--- 61 files changed, 586 insertions(+), 449 deletions(-) diff --git a/include/mbgl/gfx/drawable.hpp b/include/mbgl/gfx/drawable.hpp index 11809b1d5da..93b92ba9ca1 100644 --- a/include/mbgl/gfx/drawable.hpp +++ b/include/mbgl/gfx/drawable.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include diff --git a/include/mbgl/gfx/uniform_block.hpp b/include/mbgl/gfx/uniform_block.hpp index 3f196b856f6..bbfd1f2fcb4 100644 --- a/include/mbgl/gfx/uniform_block.hpp +++ b/include/mbgl/gfx/uniform_block.hpp @@ -1,6 +1,8 @@ #pragma once #include +#include +#include namespace mbgl { namespace gfx { diff --git a/include/mbgl/gfx/uniform_buffer.hpp b/include/mbgl/gfx/uniform_buffer.hpp index 1aecd064b1a..03e90a8b56c 100644 --- a/include/mbgl/gfx/uniform_buffer.hpp +++ b/include/mbgl/gfx/uniform_buffer.hpp @@ -2,6 +2,10 @@ #include +#include +#include +#include + namespace mbgl { namespace gfx { diff --git a/include/mbgl/shaders/background_layer_ubo.hpp b/include/mbgl/shaders/background_layer_ubo.hpp index 0903dcbd436..9f31256e0a9 100644 --- a/include/mbgl/shaders/background_layer_ubo.hpp +++ b/include/mbgl/shaders/background_layer_ubo.hpp @@ -50,11 +50,5 @@ struct alignas(16) BackgroundPatternLayerUBO { }; static_assert(sizeof(BackgroundPatternLayerUBO) == 64); -enum { - idBackgroundDrawableUBO = globalUBOCount, - idBackgroundLayerUBO, - backgroundUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/circle_layer_ubo.hpp b/include/mbgl/shaders/circle_layer_ubo.hpp index f0618796966..9c347b807e1 100644 --- a/include/mbgl/shaders/circle_layer_ubo.hpp +++ b/include/mbgl/shaders/circle_layer_ubo.hpp @@ -46,12 +46,5 @@ struct alignas(16) CircleInterpolateUBO { }; static_assert(sizeof(CircleInterpolateUBO) % 16 == 0); -enum { - idCircleDrawableUBO = globalUBOCount, - idCircleEvaluatedPropsUBO, - idCircleInterpolateUBO, - circleUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/collision_layer_ubo.hpp b/include/mbgl/shaders/collision_layer_ubo.hpp index 89530becba0..d185fa0c031 100644 --- a/include/mbgl/shaders/collision_layer_ubo.hpp +++ b/include/mbgl/shaders/collision_layer_ubo.hpp @@ -14,10 +14,5 @@ struct alignas(16) CollisionUBO { static_assert(sizeof(CollisionUBO) % 16 == 0); static_assert(sizeof(CollisionUBO) == 80); -enum { - idCollisionUBO = globalUBOCount, - collisionUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/custom_drawable_layer_ubo.hpp b/include/mbgl/shaders/custom_drawable_layer_ubo.hpp index feeb3edc4ff..b022a52b90c 100644 --- a/include/mbgl/shaders/custom_drawable_layer_ubo.hpp +++ b/include/mbgl/shaders/custom_drawable_layer_ubo.hpp @@ -26,11 +26,5 @@ struct alignas(16) CustomSymbolIconParametersUBO { }; static_assert(sizeof(CustomSymbolIconParametersUBO) == 3 * 16); -enum { - idCustomSymbolDrawableUBO = globalUBOCount, - idCustomSymbolParametersUBO, - customSymbolUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/debug_layer_ubo.hpp b/include/mbgl/shaders/debug_layer_ubo.hpp index 57b4449b574..d0f6adffa38 100644 --- a/include/mbgl/shaders/debug_layer_ubo.hpp +++ b/include/mbgl/shaders/debug_layer_ubo.hpp @@ -13,10 +13,5 @@ struct alignas(16) DebugUBO { }; static_assert(sizeof(DebugUBO) % 16 == 0); -enum { - idDebugUBO = globalUBOCount, - debugUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/fill_extrusion_layer_ubo.hpp b/include/mbgl/shaders/fill_extrusion_layer_ubo.hpp index c9321718e96..2a748fbc305 100644 --- a/include/mbgl/shaders/fill_extrusion_layer_ubo.hpp +++ b/include/mbgl/shaders/fill_extrusion_layer_ubo.hpp @@ -57,13 +57,5 @@ struct alignas(16) FillExtrusionInterpolateUBO { }; static_assert(sizeof(FillExtrusionInterpolateUBO) == 2 * 16); -enum { - idFillExtrusionDrawableUBO = globalUBOCount, - idFillExtrusionPropsUBO, - idFillExtrusionTilePropsUBO, - idFillExtrusionInterpolateUBO, - fillExtrusionUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/fill_layer_ubo.hpp b/include/mbgl/shaders/fill_layer_ubo.hpp index 496d295029e..bcfd34ef933 100644 --- a/include/mbgl/shaders/fill_layer_ubo.hpp +++ b/include/mbgl/shaders/fill_layer_ubo.hpp @@ -116,13 +116,5 @@ struct alignas(16) FillEvaluatedPropsUBO { }; static_assert(sizeof(FillEvaluatedPropsUBO) == 3 * 16); -enum { - idFillDrawableUBO = globalUBOCount, - idFillTilePropsUBO, - idFillInterpolateUBO, - idFillEvaluatedPropsUBO, - fillUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/heatmap_layer_ubo.hpp b/include/mbgl/shaders/heatmap_layer_ubo.hpp index 840439148e8..68ac43ea8c9 100644 --- a/include/mbgl/shaders/heatmap_layer_ubo.hpp +++ b/include/mbgl/shaders/heatmap_layer_ubo.hpp @@ -27,12 +27,5 @@ struct alignas(16) HeatmapInterpolateUBO { }; static_assert(sizeof(HeatmapInterpolateUBO) % 16 == 0); -enum { - idHeatmapDrawableUBO = globalUBOCount, - idHeatmapEvaluatedPropsUBO, - idHeatmapInterpolateUBO, - heatmapUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/heatmap_texture_layer_ubo.hpp b/include/mbgl/shaders/heatmap_texture_layer_ubo.hpp index 4bf428be90e..a4888dbcf0e 100644 --- a/include/mbgl/shaders/heatmap_texture_layer_ubo.hpp +++ b/include/mbgl/shaders/heatmap_texture_layer_ubo.hpp @@ -12,10 +12,5 @@ struct alignas(16) HeatmapTexturePropsUBO { }; static_assert(sizeof(HeatmapTexturePropsUBO) % 16 == 0); -enum { - idHeatmapTexturePropsUBO = globalUBOCount, - heatmapTextureUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/hillshade_layer_ubo.hpp b/include/mbgl/shaders/hillshade_layer_ubo.hpp index 0ec6f40048b..77fd1a993ee 100644 --- a/include/mbgl/shaders/hillshade_layer_ubo.hpp +++ b/include/mbgl/shaders/hillshade_layer_ubo.hpp @@ -19,11 +19,5 @@ struct alignas(16) HillshadeEvaluatedPropsUBO { }; static_assert(sizeof(HillshadeEvaluatedPropsUBO) % 16 == 0); -enum { - idHillshadeDrawableUBO = globalUBOCount, - idHillshadeEvaluatedPropsUBO, - hillshadeUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/hillshade_prepare_layer_ubo.hpp b/include/mbgl/shaders/hillshade_prepare_layer_ubo.hpp index 5f4a6f925e8..57ac65f3e97 100644 --- a/include/mbgl/shaders/hillshade_prepare_layer_ubo.hpp +++ b/include/mbgl/shaders/hillshade_prepare_layer_ubo.hpp @@ -14,10 +14,5 @@ struct alignas(16) HillshadePrepareDrawableUBO { }; static_assert(sizeof(HillshadePrepareDrawableUBO) % 16 == 0); -enum { - idHillshadePrepareDrawableUBO = globalUBOCount, - hillshadePrepareUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/line_layer_ubo.hpp b/include/mbgl/shaders/line_layer_ubo.hpp index 29c09f26b47..82b22842ad5 100644 --- a/include/mbgl/shaders/line_layer_ubo.hpp +++ b/include/mbgl/shaders/line_layer_ubo.hpp @@ -143,14 +143,5 @@ struct alignas(16) LineEvaluatedPropsUBO { }; static_assert(sizeof(LineEvaluatedPropsUBO) % 16 == 0); -enum { - idLineDrawableUBO = globalUBOCount, - idLineInterpolationUBO, - idLineTilePropertiesUBO, - idLineEvaluatedPropsUBO, - idLineExpressionUBO, - lineUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/mtl/circle.hpp b/include/mbgl/shaders/mtl/circle.hpp index 182ff712e14..cc0525bc537 100644 --- a/include/mbgl/shaders/mtl/circle.hpp +++ b/include/mbgl/shaders/mtl/circle.hpp @@ -110,8 +110,8 @@ struct alignas(16) CircleInterpolateUBO { FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const GlobalPaintParamsUBO& paintParams [[buffer(0)]], device const CircleDrawableUBO& drawable [[buffer(1)]], - device const CircleEvaluatedPropsUBO& props [[buffer(2)]], - device const CircleInterpolateUBO& interp [[buffer(3)]]) { + device const CircleInterpolateUBO& interp [[buffer(2)]]), + device const CircleEvaluatedPropsUBO& props [[buffer(3)]] { #if defined(HAS_UNIFORM_u_radius) const auto radius = props.radius; @@ -189,7 +189,7 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], } half4 fragment fragmentMain(FragmentStage in [[stage_in]], - device const CircleEvaluatedPropsUBO& props [[buffer(2)]]) { + device const CircleEvaluatedPropsUBO& props [[buffer(3)]]) { #if defined(OVERDRAW_INSPECTOR) return half4(1.0); #endif diff --git a/include/mbgl/shaders/mtl/fill_extrusion.hpp b/include/mbgl/shaders/mtl/fill_extrusion.hpp index 2cbae0701b6..7339aa36bbf 100644 --- a/include/mbgl/shaders/mtl/fill_extrusion.hpp +++ b/include/mbgl/shaders/mtl/fill_extrusion.hpp @@ -47,8 +47,8 @@ struct FragmentOutput { FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const FillExtrusionDrawableUBO& drawable [[buffer(1)]], - device const FillExtrusionPropsUBO& props [[buffer(2)]], - device const FillExtrusionInterpolateUBO& interp [[buffer(4)]]) { + device const FillExtrusionInterpolateUBO& interp [[buffer(3)]]), + device const FillExtrusionPropsUBO& props [[buffer(4)]] { #if defined(HAS_UNIFORM_u_base) const auto base = props.light_position_base.w; diff --git a/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp b/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp index 7079e4f7ab6..c58b85c1a90 100644 --- a/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp +++ b/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp @@ -60,9 +60,9 @@ struct FragmentOutput { FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const GlobalPaintParamsUBO& paintParams [[buffer(0)]], device const FillExtrusionDrawableUBO& drawable [[buffer(1)]], - device const FillExtrusionPropsUBO& props [[buffer(2)]], - device const FillExtrusionTilePropsUBO& tileProps [[buffer(3)]], - device const FillExtrusionInterpolateUBO& interp [[buffer(4)]]) { + device const FillExtrusionTilePropsUBO& tileProps [[buffer(2)]], + device const FillExtrusionInterpolateUBO& interp [[buffer(3)]]), + device const FillExtrusionPropsUBO& props [[buffer(4)]], { #if defined(HAS_UNIFORM_u_base) const auto base = props.light_position_base.w; @@ -151,8 +151,8 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], fragment FragmentOutput fragmentMain(FragmentStage in [[stage_in]], device const FillExtrusionDrawableUBO& drawable [[buffer(1)]], - device const FillExtrusionPropsUBO& props [[buffer(2)]], - device const FillExtrusionTilePropsUBO& tileProps [[buffer(3)]], + device const FillExtrusionTilePropsUBO& tileProps [[buffer(2)]], + device const FillExtrusionPropsUBO& props [[buffer(4)]], texture2d image0 [[texture(0)]], sampler image0_sampler [[sampler(0)]]) { #if defined(OVERDRAW_INSPECTOR) diff --git a/include/mbgl/shaders/mtl/heatmap.hpp b/include/mbgl/shaders/mtl/heatmap.hpp index 5e613169a0c..df22bb0b49a 100644 --- a/include/mbgl/shaders/mtl/heatmap.hpp +++ b/include/mbgl/shaders/mtl/heatmap.hpp @@ -68,8 +68,8 @@ constant const float ZERO = 1.0 / 255.0 / 16.0; FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const HeatmapDrawableUBO& drawable [[buffer(1)]], - device const HeatmapEvaluatedPropsUBO& props [[buffer(2)]], - device const HeatmapInterpolateUBO& interp [[buffer(3)]]) { + device const HeatmapInterpolateUBO& interp [[buffer(2)]]), + device const HeatmapEvaluatedPropsUBO& props [[buffer(3)]] { #if defined(HAS_UNIFORM_u_weight) const auto weight = props.weight; @@ -117,7 +117,7 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], } half4 fragment fragmentMain(FragmentStage in [[stage_in]], - device const HeatmapEvaluatedPropsUBO& props [[buffer(2)]]) { + device const HeatmapEvaluatedPropsUBO& props [[buffer(3)]]) { #if defined(OVERDRAW_INSPECTOR) return half4(1.0); #endif diff --git a/include/mbgl/shaders/raster_layer_ubo.hpp b/include/mbgl/shaders/raster_layer_ubo.hpp index 789cb597b17..4e3c9a523c1 100644 --- a/include/mbgl/shaders/raster_layer_ubo.hpp +++ b/include/mbgl/shaders/raster_layer_ubo.hpp @@ -25,11 +25,5 @@ struct alignas(16) RasterEvaluatedPropsUBO { }; static_assert(sizeof(RasterEvaluatedPropsUBO) == 64); -enum { - idRasterDrawableUBO = globalUBOCount, - idRasterEvaluatedPropsUBO, - rasterUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/shader_defines.hpp b/include/mbgl/shaders/shader_defines.hpp index 937d6d49970..b187589afc4 100644 --- a/include/mbgl/shaders/shader_defines.hpp +++ b/include/mbgl/shaders/shader_defines.hpp @@ -1,48 +1,197 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include #include namespace mbgl { namespace shaders { -// UBO defines +// drawable UBOs + enum { idClippingMaskUBO = globalUBOCount, clippingMaskUBOCount }; -static constexpr auto maxUBOCountPerShader = std::max({static_cast(backgroundUBOCount), - static_cast(circleUBOCount), +enum { + idCommonUBO = globalUBOCount, + commonDrawableUBOCount +}; + +enum { + idCircleDrawableUBO = globalUBOCount, + idCircleInterpolateUBO, + circleDrawableUBOCount +}; + +enum { + idBackgroundDrawableUBO = globalUBOCount, + backgroundDrawableUBOCount +}; + +enum { + idCustomSymbolDrawableUBO = globalUBOCount, + idCustomSymbolParametersUBO, + customSymbolDrawableUBOCount +}; + +enum { + idCollisionUBO = globalUBOCount, + collisionDrawableUBOCount +}; + +enum { + idDebugUBO = globalUBOCount, + debugDrawableUBOCount +}; + +enum { + idFillDrawableUBO = globalUBOCount, + idFillTilePropsUBO, + idFillInterpolateUBO, + fillDrawableUBOCount +}; + +enum { + idFillExtrusionDrawableUBO = globalUBOCount, + idFillExtrusionTilePropsUBO, + idFillExtrusionInterpolateUBO, + fillExtrusionDrawableUBOCount +}; + +enum { + idHeatmapDrawableUBO = globalUBOCount, + idHeatmapInterpolateUBO, + heatmapDrawableUBOCount +}; + +enum { + idHillshadeDrawableUBO = globalUBOCount, + hillshadeDrawableUBOCount +}; + +enum { + idHillshadePrepareDrawableUBO = globalUBOCount, + hillshadePrepareDrawableUBOCount +}; + +enum { + idLineDrawableUBO = globalUBOCount, + idLineInterpolationUBO, + idLineTilePropertiesUBO, + lineDrawableUBOCount +}; + +enum { + idRasterDrawableUBO = globalUBOCount, + rasterDrawableUBOCount +}; + +enum { + idSymbolDrawableUBO = globalUBOCount, + idSymbolTilePropsUBO, + idSymbolInterpolateUBO, + symbolDrawableUBOCount +}; + +enum { + idWideVectorUniformsUBO = globalUBOCount, + idWideVectorUniformWideVecUBO, + wideVectorDrawableUBOCount +}; + +static constexpr auto layerUBOStartId = std::max({static_cast(circleDrawableUBOCount), + static_cast(backgroundDrawableUBOCount), + static_cast(customSymbolDrawableUBOCount), + static_cast(collisionDrawableUBOCount), + static_cast(commonDrawableUBOCount), + static_cast(debugDrawableUBOCount), + static_cast(fillDrawableUBOCount), + static_cast(fillExtrusionDrawableUBOCount), + static_cast(heatmapDrawableUBOCount), + static_cast(hillshadeDrawableUBOCount), + static_cast(hillshadePrepareDrawableUBOCount), + static_cast(lineDrawableUBOCount), + static_cast(rasterDrawableUBOCount), + static_cast(symbolDrawableUBOCount), + static_cast(wideVectorDrawableUBOCount)}); + +static constexpr auto maxUBOCountPerDrawable = layerUBOStartId - globalUBOCount; + +// layer UBOs + +#if MLN_RENDER_BACKEND_VULKAN +#define getLayerStartValue(packedValue) layerUBOStartId +#else +#define getLayerStartValue(packedValue) packedValue +#endif + +enum { + idCircleEvaluatedPropsUBO = getLayerStartValue(circleDrawableUBOCount), + circleUBOCount +}; + +enum { + idBackgroundLayerUBO = getLayerStartValue(backgroundDrawableUBOCount), + backgroundUBOCount +}; + +enum { + idFillEvaluatedPropsUBO = getLayerStartValue(fillDrawableUBOCount), + fillUBOCount +}; + +enum { + idFillExtrusionPropsUBO = getLayerStartValue(fillExtrusionDrawableUBOCount), + fillExtrusionUBOCount +}; + +enum { + idHeatmapEvaluatedPropsUBO = getLayerStartValue(heatmapDrawableUBOCount), + heatmapUBOCount +}; + +enum { + idHeatmapTexturePropsUBO = getLayerStartValue(globalUBOCount), + heatmapTextureUBOCount +}; + +enum { + idHillshadeEvaluatedPropsUBO = getLayerStartValue(hillshadeDrawableUBOCount), + hillshadeUBOCount +}; + +enum { + idLineEvaluatedPropsUBO = getLayerStartValue(lineDrawableUBOCount), + idLineExpressionUBO, + lineUBOCount +}; + +enum { + idRasterEvaluatedPropsUBO = getLayerStartValue(rasterDrawableUBOCount), + rasterUBOCount +}; + +enum { + idSymbolEvaluatedPropsUBO = getLayerStartValue(symbolDrawableUBOCount), + symbolUBOCount +}; + +#undef getLayerStartValue + +static constexpr auto maxUBOCountPerShader = std::max({static_cast(circleUBOCount), static_cast(clippingMaskUBOCount), - static_cast(collisionUBOCount), - static_cast(customSymbolUBOCount), - static_cast(debugUBOCount), static_cast(fillUBOCount), static_cast(fillExtrusionUBOCount), static_cast(heatmapUBOCount), static_cast(heatmapTextureUBOCount), static_cast(hillshadeUBOCount), - static_cast(hillshadePrepareUBOCount), static_cast(lineUBOCount), static_cast(rasterUBOCount), - static_cast(symbolUBOCount), - static_cast(wideVectorUBOCount)}); + static_cast(symbolUBOCount)}); + +static constexpr auto maxUBOCountPerLayer = maxUBOCountPerShader - layerUBOStartId; // Texture defines enum { diff --git a/include/mbgl/shaders/symbol_layer_ubo.hpp b/include/mbgl/shaders/symbol_layer_ubo.hpp index baf9b692844..ca1f6917397 100644 --- a/include/mbgl/shaders/symbol_layer_ubo.hpp +++ b/include/mbgl/shaders/symbol_layer_ubo.hpp @@ -65,13 +65,5 @@ struct alignas(16) SymbolEvaluatedPropsUBO { }; static_assert(sizeof(SymbolEvaluatedPropsUBO) == 6 * 16); -enum { - idSymbolDrawableUBO = globalUBOCount, - idSymbolTilePropsUBO, - idSymbolInterpolateUBO, - idSymbolEvaluatedPropsUBO, - symbolUBOCount -}; - } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/vulkan/background.hpp b/include/mbgl/shaders/vulkan/background.hpp index 1ca862ebfac..8bfb59514c7 100644 --- a/include/mbgl/shaders/vulkan/background.hpp +++ b/include/mbgl/shaders/vulkan/background.hpp @@ -19,7 +19,7 @@ struct ShaderSource { layout(location = 0) in ivec2 in_position; -layout(set = 0, binding = 1) uniform BackgroundDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform BackgroundDrawableUBO { mat4 matrix; } drawable; @@ -32,7 +32,7 @@ void main() { static constexpr auto fragment = R"( layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 2) uniform BackgroundLayerUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform BackgroundLayerUBO { vec4 color; float opacity; float pad1, pad2, pad3; @@ -63,7 +63,7 @@ struct ShaderSource { layout(location = 0) in ivec2 in_position; -layout(set = 0, binding = 1) uniform DebugUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform DebugUBO { mat4 matrix; vec4 color; float overlay_scale; @@ -43,14 +43,14 @@ void main() { layout(location = 0) in vec2 frag_uv; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform DebugUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform DebugUBO { mat4 matrix; vec4 color; float overlay_scale; float pad1, pad2, pad3; } debug; -layout(set = 1, binding = 0) uniform sampler2D image_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image_sampler; void main() { vec4 overlay_color = texture(image_sampler, frag_uv); diff --git a/include/mbgl/shaders/vulkan/fill.hpp b/include/mbgl/shaders/vulkan/fill.hpp index df20aa09782..ee378a09b30 100644 --- a/include/mbgl/shaders/vulkan/fill.hpp +++ b/include/mbgl/shaders/vulkan/fill.hpp @@ -27,11 +27,11 @@ layout(location = 1) in vec4 in_color; layout(location = 2) in vec2 in_opacity; #endif -layout(set = 0, binding = 1) uniform FillDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform FillDrawableUBO { mat4 matrix; } drawable; -layout(set = 0, binding = 3) uniform FillInterpolateUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform FillInterpolateUBO { float color_t; float opacity_t; } interp; @@ -71,7 +71,7 @@ layout(location = 1) in lowp float frag_opacity; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 4) uniform FillEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform FillEvaluatedPropsUBO { vec4 color; vec4 outline_color; float opacity; @@ -120,11 +120,11 @@ layout(location = 1) in vec4 in_color; layout(location = 2) in vec2 in_opacity; #endif -layout(set = 0, binding = 1) uniform FillDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform FillDrawableUBO { mat4 matrix; } drawable; -layout(set = 0, binding = 3) uniform FillInterpolateUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform FillInterpolateUBO { float color_t; float opacity_t; } interp; @@ -170,7 +170,7 @@ layout(location = 2) in vec2 frag_position; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 4) uniform FillEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform FillEvaluatedPropsUBO { vec4 color; vec4 outline_color; float opacity; @@ -230,7 +230,7 @@ layout(location = 2) in mediump uvec4 in_pattern_to; layout(location = 3) in vec2 in_opacity; #endif -layout(set = 0, binding = 1) uniform FillPatternDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform FillPatternDrawableUBO { mat4 matrix; vec2 pixel_coord_upper; vec2 pixel_coord_lower; @@ -239,18 +239,18 @@ layout(set = 0, binding = 1) uniform FillPatternDrawableUBO { float pad; } drawable; -layout(set = 0, binding = 2) uniform FillPatternTilePropsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform FillPatternTilePropsUBO { vec4 pattern_from; vec4 pattern_to; } tile; -layout(set = 0, binding = 3) uniform FillPatternInterpolateUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 2) uniform FillPatternInterpolateUBO { float pattern_from_t; float pattern_to_t; float opacity_t; } interp; -layout(set = 0, binding = 4) uniform FillEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform FillEvaluatedPropsUBO { vec4 color; vec4 outline_color; float opacity; @@ -332,7 +332,7 @@ layout(location = 4) in lowp float frag_opacity; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform FillPatternDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform FillPatternDrawableUBO { mat4 matrix; vec2 pixel_coord_upper; vec2 pixel_coord_lower; @@ -341,12 +341,12 @@ layout(set = 0, binding = 1) uniform FillPatternDrawableUBO { float pad; } drawable; -layout(set = 0, binding = 2) uniform FillPatternTilePropsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform FillPatternTilePropsUBO { vec4 pattern_from; vec4 pattern_to; } tile; -layout(set = 0, binding = 4) uniform FillEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform FillEvaluatedPropsUBO { vec4 color; vec4 outline_color; float opacity; @@ -355,7 +355,7 @@ layout(set = 0, binding = 4) uniform FillEvaluatedPropsUBO { float to_scale; } props; -layout(set = 1, binding = 0) uniform sampler2D image0_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image0_sampler; void main() { @@ -425,7 +425,7 @@ layout(location = 2) in mediump uvec4 in_pattern_to; layout(location = 3) in vec2 in_opacity; #endif -layout(set = 0, binding = 1) uniform FillOutlinePatternDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform FillOutlinePatternDrawableUBO { mat4 matrix; vec2 pixel_coord_upper; vec2 pixel_coord_lower; @@ -434,18 +434,18 @@ layout(set = 0, binding = 1) uniform FillOutlinePatternDrawableUBO { float pad; } drawable; -layout(set = 0, binding = 2) uniform FillOutlinePatternTilePropsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform FillOutlinePatternTilePropsUBO { vec4 pattern_from; vec4 pattern_to; } tile; -layout(set = 0, binding = 3) uniform FillOutlinePatternInterpolateUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 2) uniform FillOutlinePatternInterpolateUBO { float pattern_from_t; float pattern_to_t; float opacity_t; } interp; -layout(set = 0, binding = 4) uniform FillEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform FillEvaluatedPropsUBO { vec4 color; vec4 outline_color; float opacity; @@ -533,7 +533,7 @@ layout(location = 5) in lowp float frag_opacity; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform FillOutlinePatternDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform FillOutlinePatternDrawableUBO { mat4 matrix; vec2 pixel_coord_upper; vec2 pixel_coord_lower; @@ -542,12 +542,12 @@ layout(set = 0, binding = 1) uniform FillOutlinePatternDrawableUBO { float pad; } drawable; -layout(set = 0, binding = 2) uniform FillOutlinePatternTilePropsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform FillOutlinePatternTilePropsUBO { vec4 pattern_from; vec4 pattern_to; } tile; -layout(set = 0, binding = 4) uniform FillEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform FillEvaluatedPropsUBO { vec4 color; vec4 outline_color; float opacity; @@ -556,7 +556,7 @@ layout(set = 0, binding = 4) uniform FillEvaluatedPropsUBO { float to_scale; } props; -layout(set = 1, binding = 0) uniform sampler2D image0_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image0_sampler; void main() { @@ -618,7 +618,7 @@ struct ShaderSource { layout(location = 0) in ivec2 in_position; -layout(set = 0, binding = 1) uniform HeatmapTexturePropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform HeatmapTexturePropsUBO { mat4 matrix; float opacity; float pad1, pad2, pad3; @@ -173,14 +173,14 @@ void main() { layout(location = 0) in vec2 frag_position; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform HeatmapTexturePropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform HeatmapTexturePropsUBO { mat4 matrix; float opacity; float pad1, pad2, pad3; } props; -layout(set = 1, binding = 0) uniform sampler2D image_sampler; -layout(set = 1, binding = 1) uniform sampler2D color_ramp_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 1) uniform sampler2D color_ramp_sampler; void main() { diff --git a/include/mbgl/shaders/vulkan/hillshade.hpp b/include/mbgl/shaders/vulkan/hillshade.hpp index 3e2b5b9c33c..433532650d8 100644 --- a/include/mbgl/shaders/vulkan/hillshade.hpp +++ b/include/mbgl/shaders/vulkan/hillshade.hpp @@ -20,7 +20,7 @@ struct ShaderSource layout(location = 0) in ivec2 in_position; layout(location = 1) in ivec2 in_texture_position; -layout(set = 0, binding = 1) uniform HillshadePrepareDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform HillshadePrepareDrawableUBO { mat4 matrix; vec4 unpack; vec2 dimension; @@ -46,7 +46,7 @@ void main() { layout(location = 0) in vec2 frag_position; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform HillshadePrepareDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform HillshadePrepareDrawableUBO { mat4 matrix; vec4 unpack; vec2 dimension; @@ -54,7 +54,7 @@ layout(set = 0, binding = 1) uniform HillshadePrepareDrawableUBO { float maxzoom; } drawable; -layout(set = 1, binding = 0) uniform sampler2D image_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image_sampler; float getElevation(vec2 coord, float bias, sampler2D image_sampler, vec4 unpack) { // Convert encoded elevation value to meters @@ -138,7 +138,7 @@ struct ShaderSource { layout(location = 0) in ivec2 in_position; layout(location = 1) in ivec2 in_texture_position; -layout(set = 0, binding = 1) uniform HillshadeDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform HillshadeDrawableUBO { mat4 matrix; vec2 latrange; vec2 light; @@ -161,19 +161,19 @@ void main() { layout(location = 0) in vec2 frag_position; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform HillshadeDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform HillshadeDrawableUBO { mat4 matrix; vec2 latrange; vec2 light; } drawable; -layout(set = 0, binding = 2) uniform HillshadeEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform HillshadeEvaluatedPropsUBO { vec4 highlight; vec4 shadow; vec4 accent; } props; -layout(set = 1, binding = 0) uniform sampler2D image_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image_sampler; void main() { diff --git a/include/mbgl/shaders/vulkan/line.hpp b/include/mbgl/shaders/vulkan/line.hpp index 8069dc4172d..e0fc93f7aee 100644 --- a/include/mbgl/shaders/vulkan/line.hpp +++ b/include/mbgl/shaders/vulkan/line.hpp @@ -44,13 +44,13 @@ layout(location = 6) in vec2 in_offset; layout(location = 7) in vec2 in_width; #endif -layout(set = 0, binding = 1) uniform LineDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform LineDrawableUBO { mat4 matrix; mediump float ratio; float pad1, pad2, pad3; } drawable; -layout(set = 0, binding = 2) uniform LineInterpolationUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform LineInterpolationUBO { float color_t; float blur_t; float opacity_t; @@ -60,7 +60,7 @@ layout(set = 0, binding = 2) uniform LineInterpolationUBO { float pad1, pad2; } interp; -layout(set = 0, binding = 4) uniform LineEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform LineEvaluatedPropsUBO { vec4 color; float blur; float opacity; @@ -186,7 +186,7 @@ layout(location = 5) in lowp float frag_opacity; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 4) uniform LineEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform LineEvaluatedPropsUBO { vec4 color; float blur; float opacity; @@ -271,13 +271,13 @@ layout(location = 5) in vec2 in_offset; layout(location = 6) in vec2 in_width; #endif -layout(set = 0, binding = 1) uniform LineDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform LineDrawableUBO { mat4 matrix; mediump float ratio; float pad1, pad2, pad3; } drawable; -layout(set = 0, binding = 2) uniform LineInterpolationUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform LineInterpolationUBO { float color_t; float blur_t; float opacity_t; @@ -287,7 +287,7 @@ layout(set = 0, binding = 2) uniform LineInterpolationUBO { float pad1, pad2; } interp; -layout(set = 0, binding = 4) uniform LineEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform LineEvaluatedPropsUBO { vec4 color; float blur; float opacity; @@ -402,11 +402,11 @@ layout(location = 4) in lowp float frag_blur; layout(location = 5) in lowp float frag_opacity; #endif -layout(set = 1, binding = 0) uniform sampler2D image0_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image0_sampler; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 4) uniform LineEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform LineEvaluatedPropsUBO { vec4 color; float blur; float opacity; @@ -497,7 +497,7 @@ layout(location = 7) in uvec4 in_pattern_from; layout(location = 8) in uvec4 in_pattern_to; #endif -layout(set = 0, binding = 1) uniform LineDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform LineDrawableUBO { mat4 matrix; vec4 scale; vec2 texsize; @@ -505,7 +505,7 @@ layout(set = 0, binding = 1) uniform LineDrawableUBO { float fade; } drawable; -layout(set = 0, binding = 2) uniform LineInterpolationUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform LineInterpolationUBO { float blur_t; float opacity_t; float offset_t; @@ -516,12 +516,12 @@ layout(set = 0, binding = 2) uniform LineInterpolationUBO { float pad1; } interp; -layout(set = 0, binding = 3) uniform LinePatternTilePropertiesUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 2) uniform LinePatternTilePropertiesUBO { vec4 pattern_from; vec4 pattern_to; } tile; -layout(set = 0, binding = 4) uniform LineEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform LineEvaluatedPropsUBO { vec4 color; float blur; float opacity; @@ -663,7 +663,7 @@ layout(location = 7) in mediump vec4 frag_pattern_to; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform LineDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform LineDrawableUBO { mat4 matrix; vec4 scale; vec2 texsize; @@ -671,12 +671,12 @@ layout(set = 0, binding = 1) uniform LineDrawableUBO { float fade; } drawable; -layout(set = 0, binding = 3) uniform LinePatternTilePropertiesUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 2) uniform LinePatternTilePropertiesUBO { vec4 pattern_from; vec4 pattern_to; } tile; -layout(set = 0, binding = 4) uniform LineEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform LineEvaluatedPropsUBO { vec4 color; float blur; float opacity; @@ -688,7 +688,7 @@ layout(set = 0, binding = 4) uniform LineEvaluatedPropsUBO { float pad1; } props; -layout(set = 1, binding = 0) uniform sampler2D image0_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image0_sampler; void main() { @@ -808,7 +808,7 @@ layout(location = 7) in vec2 in_width; layout(location = 8) in vec2 in_floorwidth; #endif -layout(set = 0, binding = 1) uniform LineSDFDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform LineSDFDrawableUBO { mat4 matrix; vec2 patternscale_a; vec2 patternscale_b; @@ -820,7 +820,7 @@ layout(set = 0, binding = 1) uniform LineSDFDrawableUBO { float pad1, pad2, pad3; } drawable; -layout(set = 0, binding = 2) uniform LineSDFInterpolationUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform LineSDFInterpolationUBO { float color_t; float blur_t; float opacity_t; @@ -831,7 +831,7 @@ layout(set = 0, binding = 2) uniform LineSDFInterpolationUBO { float pad1; } interp; -layout(set = 0, binding = 4) uniform LineEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform LineEvaluatedPropsUBO { vec4 color; float blur; float opacity; @@ -982,7 +982,7 @@ layout(location = 8) in mediump float frag_floorwidth; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform LineSDFDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform LineSDFDrawableUBO { mat4 matrix; vec2 patternscale_a; vec2 patternscale_b; @@ -994,7 +994,7 @@ layout(set = 0, binding = 1) uniform LineSDFDrawableUBO { float pad1, pad2, pad3; } drawable; -layout(set = 0, binding = 2) uniform LineSDFInterpolationUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform LineSDFInterpolationUBO { float color_t; float blur_t; float opacity_t; @@ -1005,7 +1005,7 @@ layout(set = 0, binding = 2) uniform LineSDFInterpolationUBO { float pad1; } interp; -layout(set = 0, binding = 4) uniform LineEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform LineEvaluatedPropsUBO { vec4 color; float blur; float opacity; @@ -1017,7 +1017,7 @@ layout(set = 0, binding = 4) uniform LineEvaluatedPropsUBO { float pad1; } props; -layout(set = 1, binding = 0) uniform sampler2D image0_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image0_sampler; void main() { diff --git a/include/mbgl/shaders/vulkan/raster.hpp b/include/mbgl/shaders/vulkan/raster.hpp index 6e166cd3ec9..ed2d08957fe 100644 --- a/include/mbgl/shaders/vulkan/raster.hpp +++ b/include/mbgl/shaders/vulkan/raster.hpp @@ -20,11 +20,11 @@ struct ShaderSource { layout(location = 0) in ivec2 in_position; layout(location = 1) in ivec2 in_texture_position; -layout(set = 0, binding = 1) uniform RasterDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform RasterDrawableUBO { mat4 matrix; } drawable; -layout(set = 0, binding = 2) uniform RasterEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform RasterEvaluatedPropsUBO { vec4 spin_weights; vec2 tl_parent; float scale_parent; @@ -63,7 +63,7 @@ layout(location = 1) in vec2 frag_position1; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 2) uniform RasterEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform RasterEvaluatedPropsUBO { vec4 spin_weights; vec2 tl_parent; float scale_parent; @@ -77,8 +77,8 @@ layout(set = 0, binding = 2) uniform RasterEvaluatedPropsUBO { float pad1, pad2; } props; -layout(set = 1, binding = 0) uniform sampler2D image0_sampler; -layout(set = 1, binding = 1) uniform sampler2D image1_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image0_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 1) uniform sampler2D image1_sampler; void main() { diff --git a/include/mbgl/shaders/vulkan/symbol.hpp b/include/mbgl/shaders/vulkan/symbol.hpp index 5d4d623083e..72e7513b5c3 100644 --- a/include/mbgl/shaders/vulkan/symbol.hpp +++ b/include/mbgl/shaders/vulkan/symbol.hpp @@ -27,7 +27,7 @@ layout(location = 4) in float in_fade_opacity; layout(location = 5) in vec2 in_opacity; #endif -layout(set = 0, binding = 1) uniform SymbolDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform SymbolDrawableUBO { mat4 matrix; mat4 label_plane_matrix; mat4 coord_matrix; @@ -38,7 +38,7 @@ layout(set = 0, binding = 1) uniform SymbolDrawableUBO { vec2 pad; } drawable; -layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform SymbolTilePropsUBO { bool is_text; bool is_halo; bool pitch_with_map; @@ -49,7 +49,7 @@ layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { float padding; } tile; -layout(set = 0, binding = 3) uniform SymbolInterpolateUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 2) uniform SymbolInterpolateUBO { float fill_color_t; float halo_color_t; float opacity_t; @@ -140,7 +140,7 @@ layout(location = 1) in mediump float frag_opacity; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform SymbolTilePropsUBO { bool is_text; bool is_halo; bool pitch_with_map; @@ -151,7 +151,7 @@ layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { float padding; } tile; -layout(set = 0, binding = 4) uniform SymbolEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform SymbolEvaluatedPropsUBO { vec4 text_fill_color; vec4 text_halo_color; float text_opacity; @@ -166,7 +166,7 @@ layout(set = 0, binding = 4) uniform SymbolEvaluatedPropsUBO { float pad2; } props; -layout(set = 1, binding = 0) uniform sampler2D image0_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image0_sampler; void main() { #if defined(OVERDRAW_INSPECTOR) @@ -222,7 +222,7 @@ layout(location = 8) in vec2 in_halo_width; layout(location = 9) in vec2 in_halo_blur; #endif -layout(set = 0, binding = 1) uniform SymbolDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform SymbolDrawableUBO { mat4 matrix; mat4 label_plane_matrix; mat4 coord_matrix; @@ -233,7 +233,7 @@ layout(set = 0, binding = 1) uniform SymbolDrawableUBO { vec2 pad; } drawable; -layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform SymbolTilePropsUBO { bool is_text; bool is_halo; bool pitch_with_map; @@ -244,7 +244,7 @@ layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { float padding; } tile; -layout(set = 0, binding = 3) uniform SymbolInterpolateUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 2) uniform SymbolInterpolateUBO { float fill_color_t; float halo_color_t; float opacity_t; @@ -396,7 +396,7 @@ layout(location = 8) in mediump float frag_halo_blur; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform SymbolDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform SymbolDrawableUBO { mat4 matrix; mat4 label_plane_matrix; mat4 coord_matrix; @@ -407,7 +407,7 @@ layout(set = 0, binding = 1) uniform SymbolDrawableUBO { vec2 pad; } drawable; -layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform SymbolTilePropsUBO { bool is_text; bool is_halo; bool pitch_with_map; @@ -418,7 +418,7 @@ layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { float padding; } tile; -layout(set = 0, binding = 4) uniform SymbolEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform SymbolEvaluatedPropsUBO { vec4 text_fill_color; vec4 text_halo_color; float text_opacity; @@ -433,7 +433,7 @@ layout(set = 0, binding = 4) uniform SymbolEvaluatedPropsUBO { float pad2; } props; -layout(set = 1, binding = 0) uniform sampler2D image0_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image0_sampler; void main() { @@ -521,7 +521,7 @@ layout(location = 7) in vec2 in_halo_width; layout(location = 8) in vec2 in_halo_blur; #endif -layout(set = 0, binding = 1) uniform SymbolDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform SymbolDrawableUBO { mat4 matrix; mat4 label_plane_matrix; mat4 coord_matrix; @@ -532,7 +532,7 @@ layout(set = 0, binding = 1) uniform SymbolDrawableUBO { vec2 pad; } drawable; -layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform SymbolTilePropsUBO { bool is_text; bool is_halo; bool pitch_with_map; @@ -543,7 +543,7 @@ layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { float padding; } tile; -layout(set = 0, binding = 3) uniform SymbolInterpolateUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 2) uniform SymbolInterpolateUBO { float fill_color_t; float halo_color_t; float opacity_t; @@ -702,7 +702,7 @@ layout(location = 9) flat in int frag_is_icon; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform SymbolDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform SymbolDrawableUBO { mat4 matrix; mat4 label_plane_matrix; mat4 coord_matrix; @@ -713,7 +713,7 @@ layout(set = 0, binding = 1) uniform SymbolDrawableUBO { vec2 pad; } drawable; -layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform SymbolTilePropsUBO { bool is_text; bool is_halo; bool pitch_with_map; @@ -724,7 +724,7 @@ layout(set = 0, binding = 2) uniform SymbolTilePropsUBO { float padding; } tile; -layout(set = 0, binding = 4) uniform SymbolEvaluatedPropsUBO { +layout(set = LAYER_SET_INDEX, binding = 0) uniform SymbolEvaluatedPropsUBO { vec4 text_fill_color; vec4 text_halo_color; float text_opacity; @@ -739,8 +739,8 @@ layout(set = 0, binding = 4) uniform SymbolEvaluatedPropsUBO { float pad2; } props; -layout(set = 1, binding = 0) uniform sampler2D glyph_image; -layout(set = 1, binding = 1) uniform sampler2D icon_image; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D glyph_image; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 1) uniform sampler2D icon_image; void main() { #if defined(OVERDRAW_INSPECTOR) @@ -808,11 +808,11 @@ struct ShaderSource layout(location = 0) in vec2 in_position; layout(location = 1) in vec2 in_tex; -layout(set = 0, binding = 1) uniform CustomSymbolIconDrawableUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform CustomSymbolIconDrawableUBO { mat4 matrix; } drawable; -layout(set = 0, binding = 2) uniform CustomSymbolIconParametersUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform CustomSymbolIconParametersUBO { vec2 extrude_scale; vec2 anchor; float angle_degrees; @@ -872,7 +872,7 @@ void main() { layout(location = 0) in vec2 frag_tex; layout(location = 0) out vec4 out_color; -layout(set = 1, binding = 0) uniform sampler2D image_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image_sampler; void main() { diff --git a/include/mbgl/shaders/vulkan/widevector.hpp b/include/mbgl/shaders/vulkan/widevector.hpp index 55163b5f89b..5db551478fa 100644 --- a/include/mbgl/shaders/vulkan/widevector.hpp +++ b/include/mbgl/shaders/vulkan/widevector.hpp @@ -35,7 +35,7 @@ struct ShaderSource { #define WKSVertexLineCapButt 1 #define WKSVertexLineCapSquare 2 -layout(set = 0, binding = 1) uniform WideVectorUniformsUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform WideVectorUniformsUBO { mat4 mvpMatrix; mat4 mvpMatrixDiff; mat4 mvMatrix; @@ -45,7 +45,7 @@ layout(set = 0, binding = 1) uniform WideVectorUniformsUBO { vec2 frameSize; } uniforms; -layout(set = 0, binding = 2) uniform WideVectorUniformWideVecUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 1) uniform WideVectorUniformWideVecUBO { vec4 color; float w2; float offset; @@ -153,14 +153,14 @@ void main() { layout(location = 0) in vec2 frag_uv; layout(location = 0) out vec4 out_color; -layout(set = 0, binding = 1) uniform DebugUBO { +layout(set = DRAWABLE_UBO_SET_INDEX, binding = 0) uniform DebugUBO { mat4 matrix; vec4 color; float overlay_scale; float pad1, pad2, pad3; } debug; -layout(set = 1, binding = 0) uniform sampler2D image_sampler; +layout(set = DRAWABLE_IMAGE_SET_INDEX, binding = 0) uniform sampler2D image_sampler; void main() { vec4 overlay_color = texture(image_sampler, frag_uv); diff --git a/include/mbgl/shaders/widevector_ubo.hpp b/include/mbgl/shaders/widevector_ubo.hpp index 92a38dd2f58..5842a42f23e 100644 --- a/include/mbgl/shaders/widevector_ubo.hpp +++ b/include/mbgl/shaders/widevector_ubo.hpp @@ -31,12 +31,6 @@ struct alignas(16) WideVectorUniformWideVecUBO { }; static_assert(sizeof(WideVectorUniformWideVecUBO) % 16 == 0); -enum { - idWideVectorUniformsUBO = globalUBOCount, - idWideVectorUniformWideVecUBO, - wideVectorUBOCount -}; - struct VertexTriWideVecB { // x, y offset around the center std::array screenPos; diff --git a/include/mbgl/vulkan/context.hpp b/include/mbgl/vulkan/context.hpp index 621c9394661..55391716bd0 100644 --- a/include/mbgl/vulkan/context.hpp +++ b/include/mbgl/vulkan/context.hpp @@ -43,6 +43,14 @@ class Texture2D; using UniqueShaderProgram = std::unique_ptr; using UniqueVertexBufferResource = std::unique_ptr; +enum class DescriptorSetType : uint8_t { + Global, + Layer, + DrawableUniform, + DrawableImage, + Count, +}; + class Context final : public gfx::Context { public: Context(RendererBackend&); @@ -135,16 +143,34 @@ class Context final : public gfx::Context { const std::unique_ptr& getDummyVertexBuffer(); const std::unique_ptr& getDummyUniformBuffer(); const std::unique_ptr& getDummyTexture(); - const vk::UniqueDescriptorSetLayout& getUniformDescriptorSetLayout(); const vk::UniqueDescriptorSetLayout& getImageDescriptorSetLayout(); + const vk::UniqueDescriptorSetLayout& buildUniformDescriptorSetLayout(vk::UniqueDescriptorSetLayout& layout, + size_t uniformCount, + const std::string& name); + const std::vector& getDescriptorSetLayouts(); + const std::vector& getDrawableDescriptorSetLayouts(); + const vk::DescriptorSetLayout& getDescriptorSetLayout(DescriptorSetType type); const vk::UniquePipelineLayout& getGeneralPipelineLayout(); const vk::UniquePipelineLayout& getPushConstantPipelineLayout(); + void bindUniformDescriptorSet(DescriptorSetType type, + const gfx::UniformBufferArray& uniforms, + size_t startID, + size_t count, + bool fillGaps = true); + + void bindUniformDescriptorSet(DescriptorSetType type, + const vk::DescriptorSet& descriptorSet, + const gfx::UniformBufferArray& uniforms, + size_t startID, + size_t count, + bool fillGaps = true); + uint8_t getCurrentFrameResourceIndex() const { return frameResourceIndex; } const vk::UniqueDescriptorPool& getCurrentDescriptorPool() const; void enqueueDeletion(std::function&& function); - void submitOneTimeCommand(const std::function& function); + void submitOneTimeCommand(const std::function& function) const; void requestSurfaceUpdate() { surfaceUpdateRequested = true; } @@ -184,9 +210,12 @@ class Context final : public gfx::Context { std::unique_ptr dummyVertexBuffer; std::unique_ptr dummyUniformBuffer; std::unique_ptr dummyTexture2D; - vk::UniqueDescriptorSetLayout uniformDescriptorSetLayout; + vk::UniqueDescriptorSetLayout globalUniformDescriptorSetLayout; + vk::UniqueDescriptorSetLayout layerUniformDescriptorSetLayout; + vk::UniqueDescriptorSetLayout drawableUniformDescriptorSetLayout; vk::UniqueDescriptorSetLayout imageDescriptorSetLayout; std::vector descriptorSetLayouts; + std::vector drawableDescriptorSetLayouts; vk::UniquePipelineLayout generalPipelineLayout; vk::UniquePipelineLayout pushConstantPipelineLayout; diff --git a/include/mbgl/vulkan/layer_group.hpp b/include/mbgl/vulkan/layer_group.hpp index b94779483d8..ba867b81e52 100644 --- a/include/mbgl/vulkan/layer_group.hpp +++ b/include/mbgl/vulkan/layer_group.hpp @@ -9,7 +9,7 @@ class PaintParameters; namespace vulkan { -class RenderPass; +class Context; /** A layer group for non-tile-based drawables @@ -23,12 +23,8 @@ class LayerGroup : public mbgl::LayerGroup { void render(RenderOrchestrator&, PaintParameters&) override; const gfx::UniformBufferArray& getUniformBuffers() const override { return uniformBuffers; }; - gfx::UniformBufferArray& mutableUniformBuffers() override { return uniformBuffers; }; - void bindUniformBuffers(RenderPass&) const noexcept; - void unbindUniformBuffers(RenderPass&) const noexcept {} - protected: UniformBufferArray uniformBuffers; }; diff --git a/include/mbgl/vulkan/render_pass.hpp b/include/mbgl/vulkan/render_pass.hpp index ec23fe53ed7..e1c0377015d 100644 --- a/include/mbgl/vulkan/render_pass.hpp +++ b/include/mbgl/vulkan/render_pass.hpp @@ -25,9 +25,6 @@ class RenderPass final : public gfx::RenderPass { void addDebugSignpost(const char* name) override; - void bindVertex(const BufferResource&, std::size_t offset, std::size_t index, std::size_t size = 0); - void bindFragment(const BufferResource&, std::size_t offset, std::size_t index, std::size_t size = 0); - private: void pushDebugGroup(const char* name) override; void popDebugGroup() override; diff --git a/include/mbgl/vulkan/tile_layer_group.hpp b/include/mbgl/vulkan/tile_layer_group.hpp index 2b08d7a99f7..079d3af85cf 100644 --- a/include/mbgl/vulkan/tile_layer_group.hpp +++ b/include/mbgl/vulkan/tile_layer_group.hpp @@ -25,12 +25,8 @@ class TileLayerGroup : public mbgl::TileLayerGroup { void render(RenderOrchestrator&, PaintParameters&) override; const gfx::UniformBufferArray& getUniformBuffers() const override { return uniformBuffers; }; - gfx::UniformBufferArray& mutableUniformBuffers() override { return uniformBuffers; }; - void bindUniformBuffers(RenderPass&) const noexcept; - void unbindUniformBuffers(RenderPass&) const noexcept {} - protected: UniformBufferArray uniformBuffers; }; diff --git a/src/mbgl/gfx/uniform_block.cpp b/src/mbgl/gfx/uniform_block.cpp index 472d2dbfa0e..77a892193f6 100644 --- a/src/mbgl/gfx/uniform_block.cpp +++ b/src/mbgl/gfx/uniform_block.cpp @@ -1,5 +1,7 @@ #include +#include + namespace mbgl { namespace gfx { diff --git a/src/mbgl/shaders/gl/shader_info.cpp b/src/mbgl/shaders/gl/shader_info.cpp index 86c1fbfa589..b4bafceb79b 100644 --- a/src/mbgl/shaders/gl/shader_info.cpp +++ b/src/mbgl/shaders/gl/shader_info.cpp @@ -47,8 +47,8 @@ const std::vector ShaderInfo ShaderInfo::uniformBlocks = { UniformBlockInfo{"GlobalPaintParamsUBO", idGlobalPaintParamsUBO}, UniformBlockInfo{"CircleDrawableUBO", idCircleDrawableUBO}, - UniformBlockInfo{"CircleEvaluatedPropsUBO", idCircleEvaluatedPropsUBO}, UniformBlockInfo{"CircleInterpolateUBO", idCircleInterpolateUBO}, + UniformBlockInfo{"CircleEvaluatedPropsUBO", idCircleEvaluatedPropsUBO}, }; const std::vector ShaderInfo::attributes = { AttributeInfo{"a_pos", idCirclePosVertexAttribute}, @@ -200,9 +200,9 @@ const std::vector const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"FillExtrusionDrawableUBO", idFillExtrusionDrawableUBO}, - UniformBlockInfo{"FillExtrusionPropsUBO", idFillExtrusionPropsUBO}, UniformBlockInfo{"FillExtrusionTilePropsUBO", idFillExtrusionTilePropsUBO}, UniformBlockInfo{"FillExtrusionInterpolateUBO", idFillExtrusionInterpolateUBO}, + UniformBlockInfo{"FillExtrusionPropsUBO", idFillExtrusionPropsUBO}, }; const std::vector ShaderInfo::attributes = { AttributeInfo{"a_pos", idFillExtrusionPosVertexAttribute}, @@ -218,9 +218,9 @@ const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"GlobalPaintParamsUBO", idGlobalPaintParamsUBO}, UniformBlockInfo{"FillExtrusionDrawableUBO", idFillExtrusionDrawableUBO}, - UniformBlockInfo{"FillExtrusionPropsUBO", idFillExtrusionPropsUBO}, UniformBlockInfo{"FillExtrusionTilePropsUBO", idFillExtrusionTilePropsUBO}, UniformBlockInfo{"FillExtrusionInterpolateUBO", idFillExtrusionInterpolateUBO}, + UniformBlockInfo{"FillExtrusionPropsUBO", idFillExtrusionPropsUBO}, }; const std::vector ShaderInfo::attributes = { @@ -238,8 +238,8 @@ const std::vector ShaderInfo ShaderInfo::uniformBlocks = { UniformBlockInfo{"HeatmapDrawableUBO", idHeatmapDrawableUBO}, - UniformBlockInfo{"HeatmapEvaluatedPropsUBO", idHeatmapEvaluatedPropsUBO}, UniformBlockInfo{"HeatmapInterpolateUBO", idHeatmapInterpolateUBO}, + UniformBlockInfo{"HeatmapEvaluatedPropsUBO", idHeatmapEvaluatedPropsUBO}, }; const std::vector ShaderInfo::attributes = { AttributeInfo{"a_pos", idHeatmapPosVertexAttribute}, diff --git a/src/mbgl/shaders/mtl/circle.cpp b/src/mbgl/shaders/mtl/circle.cpp index abbfbda3a50..cc445df4267 100644 --- a/src/mbgl/shaders/mtl/circle.cpp +++ b/src/mbgl/shaders/mtl/circle.cpp @@ -6,8 +6,8 @@ namespace shaders { const std::array ShaderSource::uniforms = { UniformBlockInfo{true, false, sizeof(GlobalPaintParamsUBO), idGlobalPaintParamsUBO}, UniformBlockInfo{true, false, sizeof(CircleDrawableUBO), idCircleDrawableUBO}, - UniformBlockInfo{true, true, sizeof(CircleEvaluatedPropsUBO), idCircleEvaluatedPropsUBO}, UniformBlockInfo{true, false, sizeof(CircleInterpolateUBO), idCircleInterpolateUBO}, + UniformBlockInfo{true, true, sizeof(CircleEvaluatedPropsUBO), idCircleEvaluatedPropsUBO}, }; const std::array ShaderSource::attributes = { AttributeInfo{circleUBOCount + 0, gfx::AttributeDataType::Short2, idCirclePosVertexAttribute}, diff --git a/src/mbgl/shaders/mtl/fill_extrusion.cpp b/src/mbgl/shaders/mtl/fill_extrusion.cpp index 67d46d44915..6022bb3a3b6 100644 --- a/src/mbgl/shaders/mtl/fill_extrusion.cpp +++ b/src/mbgl/shaders/mtl/fill_extrusion.cpp @@ -6,8 +6,8 @@ namespace shaders { const std::array ShaderSource::uniforms = { UniformBlockInfo{true, false, sizeof(FillExtrusionDrawableUBO), idFillExtrusionDrawableUBO}, - UniformBlockInfo{true, false, sizeof(FillExtrusionPropsUBO), idFillExtrusionPropsUBO}, UniformBlockInfo{true, false, sizeof(FillExtrusionInterpolateUBO), idFillExtrusionInterpolateUBO}, + UniformBlockInfo{true, false, sizeof(FillExtrusionPropsUBO), idFillExtrusionPropsUBO}, }; const std::array ShaderSource::attributes = { AttributeInfo{fillExtrusionUBOCount + 0, gfx::AttributeDataType::Short2, idFillExtrusionPosVertexAttribute}, diff --git a/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp b/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp index 415831ff9b2..6b32276d8ed 100644 --- a/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp +++ b/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp @@ -8,9 +8,9 @@ const std::array ShaderSource::uniforms = { UniformBlockInfo{true, false, sizeof(GlobalPaintParamsUBO), idGlobalPaintParamsUBO}, UniformBlockInfo{true, true, sizeof(FillExtrusionDrawableUBO), idFillExtrusionDrawableUBO}, - UniformBlockInfo{true, true, sizeof(FillExtrusionPropsUBO), idFillExtrusionPropsUBO}, UniformBlockInfo{true, true, sizeof(FillExtrusionTilePropsUBO), idFillExtrusionTilePropsUBO}, UniformBlockInfo{true, false, sizeof(FillExtrusionInterpolateUBO), idFillExtrusionInterpolateUBO}, + UniformBlockInfo{true, true, sizeof(FillExtrusionPropsUBO), idFillExtrusionPropsUBO}, }; const std::array ShaderSource::attributes = { diff --git a/src/mbgl/shaders/mtl/heatmap.cpp b/src/mbgl/shaders/mtl/heatmap.cpp index c3952a7f71f..31a615e0c7e 100644 --- a/src/mbgl/shaders/mtl/heatmap.cpp +++ b/src/mbgl/shaders/mtl/heatmap.cpp @@ -5,8 +5,8 @@ namespace shaders { const std::array ShaderSource::uniforms = { UniformBlockInfo{true, false, sizeof(HeatmapDrawableUBO), idHeatmapDrawableUBO}, - UniformBlockInfo{true, true, sizeof(HeatmapEvaluatedPropsUBO), idHeatmapEvaluatedPropsUBO}, UniformBlockInfo{true, false, sizeof(HeatmapInterpolateUBO), idHeatmapInterpolateUBO}, + UniformBlockInfo{true, true, sizeof(HeatmapEvaluatedPropsUBO), idHeatmapEvaluatedPropsUBO}, }; const std::array ShaderSource::attributes = { AttributeInfo{heatmapUBOCount + 0, gfx::AttributeDataType::Short2, idHeatmapPosVertexAttribute}, diff --git a/src/mbgl/shaders/vulkan/background.cpp b/src/mbgl/shaders/vulkan/background.cpp index d79f21a7def..66d8de494f2 100644 --- a/src/mbgl/shaders/vulkan/background.cpp +++ b/src/mbgl/shaders/vulkan/background.cpp @@ -1,4 +1,6 @@ #include +#include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/vulkan/circle.cpp b/src/mbgl/shaders/vulkan/circle.cpp index 914fcbfa9b9..d10442520a8 100644 --- a/src/mbgl/shaders/vulkan/circle.cpp +++ b/src/mbgl/shaders/vulkan/circle.cpp @@ -1,4 +1,6 @@ #include +#include +#include namespace mbgl { namespace shaders { @@ -6,8 +8,8 @@ namespace shaders { const std::array ShaderSource::uniforms = { UniformBlockInfo{true, false, sizeof(GlobalPaintParamsUBO), idGlobalPaintParamsUBO}, UniformBlockInfo{true, false, sizeof(CircleDrawableUBO), idCircleDrawableUBO}, - UniformBlockInfo{true, true, sizeof(CircleEvaluatedPropsUBO), idCircleEvaluatedPropsUBO}, UniformBlockInfo{true, false, sizeof(CircleInterpolateUBO), idCircleInterpolateUBO}, + UniformBlockInfo{true, true, sizeof(CircleEvaluatedPropsUBO), idCircleEvaluatedPropsUBO}, }; const std::array ShaderSource::attributes = { AttributeInfo{0, gfx::AttributeDataType::Short2, idCirclePosVertexAttribute}, diff --git a/src/mbgl/shaders/vulkan/collision.cpp b/src/mbgl/shaders/vulkan/collision.cpp index 28bfff6f53d..733c8303147 100644 --- a/src/mbgl/shaders/vulkan/collision.cpp +++ b/src/mbgl/shaders/vulkan/collision.cpp @@ -1,4 +1,6 @@ #include +#include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/vulkan/debug.cpp b/src/mbgl/shaders/vulkan/debug.cpp index 42bd3d0f8bb..a48c2d00462 100644 --- a/src/mbgl/shaders/vulkan/debug.cpp +++ b/src/mbgl/shaders/vulkan/debug.cpp @@ -1,4 +1,6 @@ #include +#include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/vulkan/fill.cpp b/src/mbgl/shaders/vulkan/fill.cpp index 7ae42d8b6cc..dabd506de14 100644 --- a/src/mbgl/shaders/vulkan/fill.cpp +++ b/src/mbgl/shaders/vulkan/fill.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include namespace mbgl { namespace shaders { @@ -78,8 +81,8 @@ const std::array const std::array ShaderSource::uniforms = { UniformBlockInfo{true, false, sizeof(FillExtrusionDrawableUBO), idFillExtrusionDrawableUBO}, - UniformBlockInfo{true, false, sizeof(FillExtrusionPropsUBO), idFillExtrusionPropsUBO}, UniformBlockInfo{true, false, sizeof(FillExtrusionInterpolateUBO), idFillExtrusionInterpolateUBO}, + UniformBlockInfo{true, false, sizeof(FillExtrusionPropsUBO), idFillExtrusionPropsUBO}, }; const std::array ShaderSource::attributes = { @@ -94,9 +97,9 @@ const std::array ShaderSource::uniforms = { UniformBlockInfo{true, false, sizeof(GlobalPaintParamsUBO), idGlobalPaintParamsUBO}, UniformBlockInfo{true, true, sizeof(FillExtrusionDrawableUBO), idFillExtrusionDrawableUBO}, - UniformBlockInfo{true, true, sizeof(FillExtrusionPropsUBO), idFillExtrusionPropsUBO}, UniformBlockInfo{true, true, sizeof(FillExtrusionTilePropsUBO), idFillExtrusionTilePropsUBO}, UniformBlockInfo{true, false, sizeof(FillExtrusionInterpolateUBO), idFillExtrusionInterpolateUBO}, + UniformBlockInfo{true, true, sizeof(FillExtrusionPropsUBO), idFillExtrusionPropsUBO}, }; const std::array ShaderSource::attributes = { diff --git a/src/mbgl/shaders/vulkan/heatmap.cpp b/src/mbgl/shaders/vulkan/heatmap.cpp index 7ed3fcbf824..12c6a41c684 100644 --- a/src/mbgl/shaders/vulkan/heatmap.cpp +++ b/src/mbgl/shaders/vulkan/heatmap.cpp @@ -1,12 +1,15 @@ #include +#include +#include +#include namespace mbgl { namespace shaders { const std::array ShaderSource::uniforms = { UniformBlockInfo{true, false, sizeof(HeatmapDrawableUBO), idHeatmapDrawableUBO}, - UniformBlockInfo{true, true, sizeof(HeatmapEvaluatedPropsUBO), idHeatmapEvaluatedPropsUBO}, UniformBlockInfo{true, false, sizeof(HeatmapInterpolateUBO), idHeatmapInterpolateUBO}, + UniformBlockInfo{true, true, sizeof(HeatmapEvaluatedPropsUBO), idHeatmapEvaluatedPropsUBO}, }; const std::array ShaderSource::attributes = { AttributeInfo{0, gfx::AttributeDataType::Short2, idHeatmapPosVertexAttribute}, diff --git a/src/mbgl/shaders/vulkan/hillshade.cpp b/src/mbgl/shaders/vulkan/hillshade.cpp index a71502deab7..50dd92d0de2 100644 --- a/src/mbgl/shaders/vulkan/hillshade.cpp +++ b/src/mbgl/shaders/vulkan/hillshade.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/vulkan/line.cpp b/src/mbgl/shaders/vulkan/line.cpp index 80c541e725f..a40c38de3b1 100644 --- a/src/mbgl/shaders/vulkan/line.cpp +++ b/src/mbgl/shaders/vulkan/line.cpp @@ -1,4 +1,6 @@ #include +#include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/vulkan/raster.cpp b/src/mbgl/shaders/vulkan/raster.cpp index 9e7db414fa3..cab5e36b021 100644 --- a/src/mbgl/shaders/vulkan/raster.cpp +++ b/src/mbgl/shaders/vulkan/raster.cpp @@ -1,4 +1,6 @@ #include +#include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/vulkan/symbol.cpp b/src/mbgl/shaders/vulkan/symbol.cpp index ea886249499..3172f86aaa8 100644 --- a/src/mbgl/shaders/vulkan/symbol.cpp +++ b/src/mbgl/shaders/vulkan/symbol.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/vulkan/widevector.cpp b/src/mbgl/shaders/vulkan/widevector.cpp index 14c009ec870..a140b185881 100644 --- a/src/mbgl/shaders/vulkan/widevector.cpp +++ b/src/mbgl/shaders/vulkan/widevector.cpp @@ -1,4 +1,6 @@ #include +#include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/vulkan/context.cpp b/src/mbgl/vulkan/context.cpp index 25c0d322bc2..974a5d0fe17 100644 --- a/src/mbgl/vulkan/context.cpp +++ b/src/mbgl/vulkan/context.cpp @@ -131,7 +131,7 @@ void Context::enqueueDeletion(std::function&& function) { frameResources[frameResourceIndex].deletionQueue.push_back(std::move(function)); } -void Context::submitOneTimeCommand(const std::function& function) { +void Context::submitOneTimeCommand(const std::function& function) const { const vk::CommandBufferAllocateInfo allocateInfo( backend.getCommandPool().get(), vk::CommandBufferLevel::ePrimary, 1); @@ -386,7 +386,10 @@ void Context::clearStencilBuffer(int32_t) { assert(false); } -void Context::bindGlobalUniformBuffers(gfx::RenderPass&) const noexcept {} +void Context::bindGlobalUniformBuffers(gfx::RenderPass&) const noexcept { + const_cast(this)->bindUniformDescriptorSet( + DescriptorSetType::Global, globalUniformBuffers, 0, shaders::globalUBOCount); +} bool Context::renderTileClippingMasks(gfx::RenderPass& renderPass, RenderStaticData& staticData, @@ -513,27 +516,30 @@ const std::unique_ptr& Context::getDummyTexture() { return dummyTexture2D; } -const vk::UniqueDescriptorSetLayout& Context::getUniformDescriptorSetLayout() { - if (!uniformDescriptorSetLayout) { - std::vector bindings; - const auto stageFlags = vk::ShaderStageFlags() | vk::ShaderStageFlagBits::eVertex | - vk::ShaderStageFlagBits::eFragment; +const vk::UniqueDescriptorSetLayout& Context::buildUniformDescriptorSetLayout(vk::UniqueDescriptorSetLayout& layout, + size_t uniformCount, + const std::string& name) { + if (layout) { + return layout; + } - for (size_t i = 0; i < shaders::maxUBOCountPerShader; ++i) { - bindings.push_back(vk::DescriptorSetLayoutBinding() - .setBinding(i) - .setStageFlags(stageFlags) - .setDescriptorType(vk::DescriptorType::eUniformBuffer) - .setDescriptorCount(1)); - } + std::vector bindings; + const auto stageFlags = vk::ShaderStageFlags() | vk::ShaderStageFlagBits::eVertex | + vk::ShaderStageFlagBits::eFragment; - const auto descriptorSetLayoutCreateInfo = vk::DescriptorSetLayoutCreateInfo().setBindings(bindings); - uniformDescriptorSetLayout = backend.getDevice()->createDescriptorSetLayoutUnique( - descriptorSetLayoutCreateInfo); - backend.setDebugName(uniformDescriptorSetLayout.get(), "UniformDescriptorSetLayout"); + for (size_t i = 0; i < uniformCount; ++i) { + bindings.push_back(vk::DescriptorSetLayoutBinding() + .setBinding(i) + .setStageFlags(stageFlags) + .setDescriptorType(vk::DescriptorType::eUniformBuffer) + .setDescriptorCount(1)); } - return uniformDescriptorSetLayout; + const auto descriptorSetLayoutCreateInfo = vk::DescriptorSetLayoutCreateInfo().setBindings(bindings); + layout = backend.getDevice()->createDescriptorSetLayoutUnique(descriptorSetLayoutCreateInfo); + backend.setDebugName(layout.get(), name); + + return layout; } const vk::UniqueDescriptorSetLayout& Context::getImageDescriptorSetLayout() { @@ -558,12 +564,36 @@ const vk::UniqueDescriptorSetLayout& Context::getImageDescriptorSetLayout() { const std::vector& Context::getDescriptorSetLayouts() { if (descriptorSetLayouts.empty()) { - descriptorSetLayouts = {getUniformDescriptorSetLayout().get(), getImageDescriptorSetLayout().get()}; + descriptorSetLayouts = { + buildUniformDescriptorSetLayout( + globalUniformDescriptorSetLayout, shaders::globalUBOCount, "GlobalUniformDescriptorSetLayout") + .get(), + buildUniformDescriptorSetLayout( + layerUniformDescriptorSetLayout, shaders::maxUBOCountPerLayer, "LayerUniformDescriptorSetLayout") + .get(), + buildUniformDescriptorSetLayout( + drawableUniformDescriptorSetLayout, shaders::maxUBOCountPerDrawable, "DrawableUniformDescriptorSetLayout") + .get(), + getImageDescriptorSetLayout().get()}; } return descriptorSetLayouts; } +const std::vector& Context::getDrawableDescriptorSetLayouts() { + if (drawableDescriptorSetLayouts.empty()) { + drawableDescriptorSetLayouts = {drawableUniformDescriptorSetLayout.get(), imageDescriptorSetLayout.get()}; + } + + return drawableDescriptorSetLayouts; +} + +const vk::DescriptorSetLayout& Context::getDescriptorSetLayout(DescriptorSetType type) { + uint32_t index = static_cast(type); + assert(index >= 0 && index < static_cast(DescriptorSetType::Count)); + return getDescriptorSetLayouts()[index]; +} + const vk::UniquePipelineLayout& Context::getGeneralPipelineLayout() { if (generalPipelineLayout) return generalPipelineLayout; @@ -591,6 +621,71 @@ const vk::UniquePipelineLayout& Context::getPushConstantPipelineLayout() { return pushConstantPipelineLayout; } +void Context::bindUniformDescriptorSet(DescriptorSetType type, + const gfx::UniformBufferArray& uniforms, + size_t startID, + size_t count, + bool fillGaps) { + + const auto& device = getBackend().getDevice(); + const auto& descriptorPool = getCurrentDescriptorPool(); + const auto& descriptorSetLayout = getDescriptorSetLayout(type); + + const auto descriptorAllocInfo = + vk::DescriptorSetAllocateInfo().setDescriptorPool(*descriptorPool).setSetLayouts(descriptorSetLayout); + + const auto& descriptorSets = device->allocateDescriptorSets(descriptorAllocInfo); + + bindUniformDescriptorSet(type, descriptorSets[0], uniforms, startID, count); + +} + +void Context::bindUniformDescriptorSet(DescriptorSetType type, + const vk::DescriptorSet& descriptorSet, + const gfx::UniformBufferArray& uniforms, + size_t startID, + size_t count, + bool fillGaps) { + + assert(static_cast(type) < static_cast(DescriptorSetType::DrawableImage)); + + const auto& device = getBackend().getDevice(); + + for (size_t index = 0; index < count; ++index) { + vk::DescriptorBufferInfo descriptorBufferInfo; + + if (const auto& uniformBuffer = uniforms.get(startID + index)) { + const auto& uniformBufferImpl = static_cast(*uniformBuffer); + const auto& bufferResource = uniformBufferImpl.getBufferResource(); + descriptorBufferInfo.setBuffer(bufferResource.getVulkanBuffer()) + .setOffset(bufferResource.getVulkanBufferOffset()) + .setRange(bufferResource.getSizeInBytes()); + } else if (fillGaps) { + descriptorBufferInfo.setBuffer(getDummyUniformBuffer()->getVulkanBuffer()) + .setOffset(0) + .setRange(VK_WHOLE_SIZE); + } else { + continue; + } + + const auto writeDescriptorSet = vk::WriteDescriptorSet() + .setBufferInfo(descriptorBufferInfo) + .setDescriptorCount(1) + .setDescriptorType(vk::DescriptorType::eUniformBuffer) + .setDstBinding(index) + .setDstSet(descriptorSet); + + device->updateDescriptorSets(writeDescriptorSet, nullptr); + } + + auto& frame = frameResources[frameResourceIndex]; + frame.commandBuffer->bindDescriptorSets(vk::PipelineBindPoint::eGraphics, + getGeneralPipelineLayout().get(), + static_cast(type), + descriptorSet, + nullptr); +} + void Context::FrameResources::runDeletionQueue(const Context& context) { for (const auto& function : deletionQueue) function(context); diff --git a/src/mbgl/vulkan/drawable.cpp b/src/mbgl/vulkan/drawable.cpp index b310edf8ff0..808d52f1009 100644 --- a/src/mbgl/vulkan/drawable.cpp +++ b/src/mbgl/vulkan/drawable.cpp @@ -401,52 +401,26 @@ bool Drawable::bindDescriptors(CommandEncoder& encoder) const noexcept { auto& context = encoder.getContext(); const auto& device = context.getBackend().getDevice(); const auto& descriptorPool = context.getCurrentDescriptorPool(); - const auto& descriptorSetLayouts = context.getDescriptorSetLayouts(); + const auto& descriptorSetLayouts = context.getDrawableDescriptorSetLayouts(); const auto descriptorAllocInfo = vk::DescriptorSetAllocateInfo().setDescriptorPool(*descriptorPool).setSetLayouts(descriptorSetLayouts); const auto& drawableDescriptorSets = device->allocateDescriptorSets(descriptorAllocInfo); - const auto& uniformDescriptorSet = drawableDescriptorSets[0]; - - const auto updateUniformDescriptors = [&](const auto& buffer, bool fillGaps) { - for (size_t id = 0; id < buffer.allocatedSize(); ++id) { - vk::DescriptorBufferInfo descriptorBufferInfo; - - if (const auto& uniformBuffer = buffer.get(id)) { - const auto& uniformBufferImpl = static_cast(*uniformBuffer); - const auto& bufferResource = uniformBufferImpl.getBufferResource(); - descriptorBufferInfo.setBuffer(bufferResource.getVulkanBuffer()) - .setOffset(bufferResource.getVulkanBufferOffset()) - .setRange(bufferResource.getVulkanBufferSize()); - } else if (fillGaps) { - descriptorBufferInfo.setBuffer(context.getDummyUniformBuffer()->getVulkanBuffer()) - .setOffset(0) - .setRange(VK_WHOLE_SIZE); - } else { - continue; - } - const auto writeDescriptorSet = vk::WriteDescriptorSet() - .setBufferInfo(descriptorBufferInfo) - .setDescriptorCount(1) - .setDescriptorType(vk::DescriptorType::eUniformBuffer) - .setDstBinding(id) - .setDstSet(uniformDescriptorSet); + const auto& drawableUniformDescriptorSet = drawableDescriptorSets[0]; + const auto& drawableImageDescriptorSet = drawableDescriptorSets[1]; - device->updateDescriptorSets(writeDescriptorSet, nullptr); - } - }; - const auto& globalUniforms = context.getGlobalUniformBuffers(); - for (size_t i = 0; i < globalUniforms.allocatedSize(); ++i) { - if (globalUniforms.get(i)) impl->uniformBuffers.set(i, globalUniforms.get(i)); - } + const auto& uniformBufferArray = getUniformBuffers(); - updateUniformDescriptors(getUniformBuffers(), true); - - if (drawableDescriptorSets.size() >= 2) { - const auto& imageDescriptorSet = drawableDescriptorSets[1]; + context.bindUniformDescriptorSet(DescriptorSetType::DrawableUniform, + drawableUniformDescriptorSet, + uniformBufferArray, + shaders::globalUBOCount, + shaders::maxUBOCountPerDrawable); + // update image set + { for (size_t id = 0; id < shaders::maxTextureCountPerShader; ++id) { const auto& texture = id < textures.size() ? textures[id] : nullptr; auto& textureImpl = texture ? static_cast(*texture) : *context.getDummyTexture(); @@ -461,17 +435,18 @@ bool Drawable::bindDescriptors(CommandEncoder& encoder) const noexcept { .setDescriptorCount(1) .setDescriptorType(vk::DescriptorType::eCombinedImageSampler) .setDstBinding(id) - .setDstSet(imageDescriptorSet); + .setDstSet(drawableImageDescriptorSet); device->updateDescriptorSets(writeDescriptorSet, nullptr); } } - if (drawableDescriptorSets.empty()) return true; - const auto& commandBuffer = encoder.getCommandBuffer(); - commandBuffer->bindDescriptorSets( - vk::PipelineBindPoint::eGraphics, context.getGeneralPipelineLayout().get(), 0, drawableDescriptorSets, nullptr); + commandBuffer->bindDescriptorSets(vk::PipelineBindPoint::eGraphics, + context.getGeneralPipelineLayout().get(), + static_cast(DescriptorSetType::DrawableImage), + drawableImageDescriptorSet, + nullptr); return true; } diff --git a/src/mbgl/vulkan/layer_group.cpp b/src/mbgl/vulkan/layer_group.cpp index e5344c681bd..6a5f43c7739 100644 --- a/src/mbgl/vulkan/layer_group.cpp +++ b/src/mbgl/vulkan/layer_group.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -43,6 +44,7 @@ void LayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { #endif auto& renderPass = static_cast(*parameters.renderPass); + auto& context = renderPass.getEncoder().getContext(); bool bindUBOs = false; visitDrawables([&](gfx::Drawable& drawable) { @@ -55,34 +57,13 @@ void LayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { } if (!bindUBOs) { - bindUniformBuffers(renderPass); + context.bindUniformDescriptorSet( + DescriptorSetType::Layer, uniformBuffers, shaders::layerUBOStartId, shaders::maxUBOCountPerLayer); bindUBOs = true; } - auto& drawableUniforms = drawable.mutableUniformBuffers(); - for (size_t i = 0; i < uniformBuffers.allocatedSize(); ++i) { - if (uniformBuffers.get(i)) { - drawableUniforms.set(i, uniformBuffers.get(i)); - } - } - drawable.draw(parameters); }); - - if (bindUBOs) { - unbindUniformBuffers(renderPass); - } -} - -void LayerGroup::bindUniformBuffers(RenderPass& renderPass) const noexcept { - for (size_t id = 0; id < uniformBuffers.allocatedSize(); id++) { - const auto& uniformBuffer = uniformBuffers.get(id); - if (!uniformBuffer) continue; - const auto& buffer = static_cast(*uniformBuffer); - const auto& resource = buffer.getBufferResource(); - renderPass.bindVertex(resource, 0, id); - renderPass.bindFragment(resource, 0, id); - } } } // namespace vulkan diff --git a/src/mbgl/vulkan/render_pass.cpp b/src/mbgl/vulkan/render_pass.cpp index 5e95bb27710..f82588465fd 100644 --- a/src/mbgl/vulkan/render_pass.cpp +++ b/src/mbgl/vulkan/render_pass.cpp @@ -71,15 +71,5 @@ void RenderPass::addDebugSignpost(const char* name) { commandEncoder.getContext().getBackend().insertDebugLabel(commandEncoder.getCommandBuffer().get(), name); } -void RenderPass::bindVertex(const BufferResource& buf, std::size_t offset, std::size_t, std::size_t size) { - [[maybe_unused]] const auto actualSize = size ? size : buf.getSizeInBytes() - offset; - assert(actualSize <= buf.getSizeInBytes()); -} - -void RenderPass::bindFragment(const BufferResource& buf, std::size_t offset, std::size_t, std::size_t size) { - [[maybe_unused]] const auto actualSize = size ? size : buf.getSizeInBytes() - offset; - assert(actualSize <= buf.getSizeInBytes()); -} - } // namespace vulkan } // namespace mbgl diff --git a/src/mbgl/vulkan/tile_layer_group.cpp b/src/mbgl/vulkan/tile_layer_group.cpp index 766c42cb475..715f89c24d7 100644 --- a/src/mbgl/vulkan/tile_layer_group.cpp +++ b/src/mbgl/vulkan/tile_layer_group.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,7 @@ void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { } auto& renderPass = static_cast(*parameters.renderPass); + auto& context = renderPass.getEncoder().getContext(); // `stencilModeFor3D` uses a different stencil mask value each time its called, so if the // drawables in this layer use 3D stencil mode, we need to set it up here so that all the @@ -91,44 +93,23 @@ void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { } if (!bindUBOs) { - bindUniformBuffers(renderPass); + context.bindUniformDescriptorSet( + DescriptorSetType::Layer, uniformBuffers, shaders::layerUBOStartId, shaders::maxUBOCountPerLayer); bindUBOs = true; } - auto& drawableImpl = static_cast(drawable); - auto& drawableUniforms = drawable.mutableUniformBuffers(); - for (size_t id = 0; id < uniformBuffers.allocatedSize(); id++) { - const auto& uniformBuffer = uniformBuffers.get(id); - if (!uniformBuffer) continue; + if (features3d) { + auto& drawableImpl = static_cast(drawable); - if (features3d) { - drawableImpl.setDepthModeFor3D(drawableImpl.getEnableDepth() ? depthMode3d.value() - : gfx::DepthMode::disabled()); + const auto& depth = drawableImpl.getEnableDepth() ? depthMode3d.value() : gfx::DepthMode::disabled(); + drawableImpl.setDepthModeFor3D(depth); - drawableImpl.setStencilModeFor3D(drawableImpl.getEnableStencil() ? stencilMode3d.value() - : gfx::StencilMode::disabled()); - } - - drawableUniforms.set(id, uniformBuffer); + const auto& stencil = drawableImpl.getEnableStencil() ? stencilMode3d.value() : gfx::StencilMode::disabled(); + drawableImpl.setStencilModeFor3D(stencil); } drawable.draw(parameters); }); - - if (bindUBOs) { - unbindUniformBuffers(renderPass); - } -} - -void TileLayerGroup::bindUniformBuffers(RenderPass& renderPass) const noexcept { - for (size_t id = 0; id < uniformBuffers.allocatedSize(); id++) { - const auto& uniformBuffer = uniformBuffers.get(id); - if (!uniformBuffer) continue; - const auto& buffer = static_cast(*uniformBuffer); - const auto& resource = buffer.getBufferResource(); - renderPass.bindVertex(resource, 0, id); - renderPass.bindFragment(resource, 0, id); - } } } // namespace vulkan From 6cd3965020b4a885162c942b283a8d3a0f885813 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Oct 2024 17:54:39 +0000 Subject: [PATCH 02/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- include/mbgl/shaders/shader_defines.hpp | 2 +- src/mbgl/vulkan/context.cpp | 15 +++++---------- src/mbgl/vulkan/tile_layer_group.cpp | 3 ++- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/include/mbgl/shaders/shader_defines.hpp b/include/mbgl/shaders/shader_defines.hpp index b187589afc4..aca7fe35475 100644 --- a/include/mbgl/shaders/shader_defines.hpp +++ b/include/mbgl/shaders/shader_defines.hpp @@ -122,7 +122,7 @@ static constexpr auto maxUBOCountPerDrawable = layerUBOStartId - globalUBOCount; // layer UBOs #if MLN_RENDER_BACKEND_VULKAN -#define getLayerStartValue(packedValue) layerUBOStartId +#define getLayerStartValue(packedValue) layerUBOStartId #else #define getLayerStartValue(packedValue) packedValue #endif diff --git a/src/mbgl/vulkan/context.cpp b/src/mbgl/vulkan/context.cpp index 974a5d0fe17..52147eab333 100644 --- a/src/mbgl/vulkan/context.cpp +++ b/src/mbgl/vulkan/context.cpp @@ -571,8 +571,9 @@ const std::vector& Context::getDescriptorSetLayouts() { buildUniformDescriptorSetLayout( layerUniformDescriptorSetLayout, shaders::maxUBOCountPerLayer, "LayerUniformDescriptorSetLayout") .get(), - buildUniformDescriptorSetLayout( - drawableUniformDescriptorSetLayout, shaders::maxUBOCountPerDrawable, "DrawableUniformDescriptorSetLayout") + buildUniformDescriptorSetLayout(drawableUniformDescriptorSetLayout, + shaders::maxUBOCountPerDrawable, + "DrawableUniformDescriptorSetLayout") .get(), getImageDescriptorSetLayout().get()}; } @@ -621,12 +622,8 @@ const vk::UniquePipelineLayout& Context::getPushConstantPipelineLayout() { return pushConstantPipelineLayout; } -void Context::bindUniformDescriptorSet(DescriptorSetType type, - const gfx::UniformBufferArray& uniforms, - size_t startID, - size_t count, - bool fillGaps) { - +void Context::bindUniformDescriptorSet( + DescriptorSetType type, const gfx::UniformBufferArray& uniforms, size_t startID, size_t count, bool fillGaps) { const auto& device = getBackend().getDevice(); const auto& descriptorPool = getCurrentDescriptorPool(); const auto& descriptorSetLayout = getDescriptorSetLayout(type); @@ -637,7 +634,6 @@ void Context::bindUniformDescriptorSet(DescriptorSetType type, const auto& descriptorSets = device->allocateDescriptorSets(descriptorAllocInfo); bindUniformDescriptorSet(type, descriptorSets[0], uniforms, startID, count); - } void Context::bindUniformDescriptorSet(DescriptorSetType type, @@ -646,7 +642,6 @@ void Context::bindUniformDescriptorSet(DescriptorSetType type, size_t startID, size_t count, bool fillGaps) { - assert(static_cast(type) < static_cast(DescriptorSetType::DrawableImage)); const auto& device = getBackend().getDevice(); diff --git a/src/mbgl/vulkan/tile_layer_group.cpp b/src/mbgl/vulkan/tile_layer_group.cpp index 715f89c24d7..ff60fc34f30 100644 --- a/src/mbgl/vulkan/tile_layer_group.cpp +++ b/src/mbgl/vulkan/tile_layer_group.cpp @@ -104,7 +104,8 @@ void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { const auto& depth = drawableImpl.getEnableDepth() ? depthMode3d.value() : gfx::DepthMode::disabled(); drawableImpl.setDepthModeFor3D(depth); - const auto& stencil = drawableImpl.getEnableStencil() ? stencilMode3d.value() : gfx::StencilMode::disabled(); + const auto& stencil = drawableImpl.getEnableStencil() ? stencilMode3d.value() + : gfx::StencilMode::disabled(); drawableImpl.setStencilModeFor3D(stencil); } From c9157ccbec40c5f66b779b91d5756d523743a493 Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Wed, 2 Oct 2024 23:48:32 +0300 Subject: [PATCH 03/11] Allocate descriptor sets lazily --- CMakeLists.txt | 2 + include/mbgl/gfx/uniform_buffer.hpp | 5 +- include/mbgl/shaders/mtl/widevector.hpp | 15 +- include/mbgl/shaders/shader_defines.hpp | 2 +- include/mbgl/vulkan/context.hpp | 29 +--- include/mbgl/vulkan/descriptor_set.hpp | 55 +++++++ include/mbgl/vulkan/uniform_buffer.hpp | 25 +++- src/mbgl/shaders/mtl/background.cpp | 3 +- src/mbgl/shaders/mtl/background_pattern.cpp | 3 +- src/mbgl/shaders/mtl/circle.cpp | 1 + src/mbgl/shaders/mtl/collision_box.cpp | 11 +- src/mbgl/shaders/mtl/collision_circle.cpp | 9 +- src/mbgl/shaders/mtl/custom_symbol_icon.cpp | 5 +- src/mbgl/shaders/mtl/debug.cpp | 3 +- src/mbgl/shaders/mtl/fill.cpp | 1 + src/mbgl/shaders/mtl/fill_extrusion.cpp | 11 +- .../shaders/mtl/fill_extrusion_pattern.cpp | 1 + src/mbgl/shaders/mtl/heatmap.cpp | 1 + src/mbgl/shaders/mtl/heatmap_texture.cpp | 1 + src/mbgl/shaders/mtl/hillshade.cpp | 1 + src/mbgl/shaders/mtl/hillshade_prepare.cpp | 5 +- src/mbgl/shaders/mtl/line.cpp | 1 + src/mbgl/shaders/mtl/raster.cpp | 1 + src/mbgl/shaders/mtl/symbol_icon.cpp | 1 + src/mbgl/shaders/mtl/symbol_sdf.cpp | 1 + src/mbgl/shaders/mtl/symbol_text_and_icon.cpp | 1 + src/mbgl/vulkan/context.cpp | 115 +++------------ src/mbgl/vulkan/descriptor_set.cpp | 137 ++++++++++++++++++ src/mbgl/vulkan/drawable.cpp | 51 +------ src/mbgl/vulkan/drawable_impl.hpp | 9 +- src/mbgl/vulkan/layer_group.cpp | 8 +- src/mbgl/vulkan/tile_layer_group.cpp | 8 +- src/mbgl/vulkan/uniform_buffer.cpp | 30 ++++ 33 files changed, 347 insertions(+), 205 deletions(-) create mode 100644 include/mbgl/vulkan/descriptor_set.hpp create mode 100644 src/mbgl/vulkan/descriptor_set.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f9b40ce9b9b..b5f0f153ce1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1296,6 +1296,7 @@ if(MLN_WITH_VULKAN) ${PROJECT_SOURCE_DIR}/include/mbgl/vulkan/command_encoder.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/vulkan/context.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/vulkan/drawable.hpp + ${PROJECT_SOURCE_DIR}/include/mbgl/vulkan/descriptor_set.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/vulkan/drawable_builder.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/vulkan/index_buffer_resource.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/vulkan/layer_group.hpp @@ -1331,6 +1332,7 @@ if(MLN_WITH_VULKAN) ${PROJECT_SOURCE_DIR}/src/mbgl/vulkan/buffer_resource.cpp ${PROJECT_SOURCE_DIR}/src/mbgl/vulkan/command_encoder.cpp ${PROJECT_SOURCE_DIR}/src/mbgl/vulkan/context.cpp + ${PROJECT_SOURCE_DIR}/src/mbgl/vulkan/descriptor_set.cpp ${PROJECT_SOURCE_DIR}/src/mbgl/vulkan/drawable.cpp ${PROJECT_SOURCE_DIR}/src/mbgl/vulkan/drawable_builder.cpp ${PROJECT_SOURCE_DIR}/src/mbgl/vulkan/drawable_impl.hpp diff --git a/include/mbgl/gfx/uniform_buffer.hpp b/include/mbgl/gfx/uniform_buffer.hpp index 03e90a8b56c..d1acdc94ae2 100644 --- a/include/mbgl/gfx/uniform_buffer.hpp +++ b/include/mbgl/gfx/uniform_buffer.hpp @@ -62,11 +62,12 @@ class UniformBufferArray { const std::shared_ptr& get(const size_t id) const; /// Set a new uniform buffer element or replace the existing one. - const std::shared_ptr& set(const size_t id, std::shared_ptr uniformBuffer); + virtual const std::shared_ptr& set(const size_t id, std::shared_ptr uniformBuffer); /// Create and add a new buffer or update an existing one void createOrUpdate(const size_t id, const std::vector& data, gfx::Context&, bool persistent = false); - void createOrUpdate(const size_t id, const void* data, std::size_t size, gfx::Context&, bool persistent = false); + virtual void createOrUpdate( + const size_t id, const void* data, std::size_t size, gfx::Context&, bool persistent = false); template void createOrUpdate(const size_t id, const T* data, gfx::Context& context, bool persistent = false) requires(!std::is_pointer_v) diff --git a/include/mbgl/shaders/mtl/widevector.hpp b/include/mbgl/shaders/mtl/widevector.hpp index 2a49d68f583..59fa51f2afe 100644 --- a/include/mbgl/shaders/mtl/widevector.hpp +++ b/include/mbgl/shaders/mtl/widevector.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace mbgl { namespace shaders { @@ -19,14 +20,14 @@ struct ShaderSource { UniformBlockInfo{true, false, sizeof(WideVectorUniformWideVecUBO), idWideVectorUniformWideVecUBO}, }; static constexpr std::array attributes{ - AttributeInfo{wideVectorUBOCount + 0, gfx::AttributeDataType::Float3, idWideVectorScreenPos}, - AttributeInfo{wideVectorUBOCount + 1, gfx::AttributeDataType::Float4, idWideVectorColor}, - AttributeInfo{wideVectorUBOCount + 2, gfx::AttributeDataType::Int, idWideVectorIndex}}; + AttributeInfo{wideVectorDrawableUBOCount + 0, gfx::AttributeDataType::Float3, idWideVectorScreenPos}, + AttributeInfo{wideVectorDrawableUBOCount + 1, gfx::AttributeDataType::Float4, idWideVectorColor}, + AttributeInfo{wideVectorDrawableUBOCount + 2, gfx::AttributeDataType::Int, idWideVectorIndex}}; static constexpr std::array instanceAttributes{ - AttributeInfo{wideVectorUBOCount + 3, gfx::AttributeDataType::Float3, idWideVectorInstanceCenter}, - AttributeInfo{wideVectorUBOCount + 3, gfx::AttributeDataType::Float4, idWideVectorInstanceColor}, - AttributeInfo{wideVectorUBOCount + 3, gfx::AttributeDataType::Int, idWideVectorInstancePrevious}, - AttributeInfo{wideVectorUBOCount + 3, gfx::AttributeDataType::Int, idWideVectorInstanceNext}}; + AttributeInfo{wideVectorDrawableUBOCount + 3, gfx::AttributeDataType::Float3, idWideVectorInstanceCenter}, + AttributeInfo{wideVectorDrawableUBOCount + 3, gfx::AttributeDataType::Float4, idWideVectorInstanceColor}, + AttributeInfo{wideVectorDrawableUBOCount + 3, gfx::AttributeDataType::Int, idWideVectorInstancePrevious}, + AttributeInfo{wideVectorDrawableUBOCount + 3, gfx::AttributeDataType::Int, idWideVectorInstanceNext}}; static constexpr std::array textures{}; static constexpr auto source = R"( diff --git a/include/mbgl/shaders/shader_defines.hpp b/include/mbgl/shaders/shader_defines.hpp index b187589afc4..aca7fe35475 100644 --- a/include/mbgl/shaders/shader_defines.hpp +++ b/include/mbgl/shaders/shader_defines.hpp @@ -122,7 +122,7 @@ static constexpr auto maxUBOCountPerDrawable = layerUBOStartId - globalUBOCount; // layer UBOs #if MLN_RENDER_BACKEND_VULKAN -#define getLayerStartValue(packedValue) layerUBOStartId +#define getLayerStartValue(packedValue) layerUBOStartId #else #define getLayerStartValue(packedValue) packedValue #endif diff --git a/include/mbgl/vulkan/context.hpp b/include/mbgl/vulkan/context.hpp index 55391716bd0..a2bdcc0758d 100644 --- a/include/mbgl/vulkan/context.hpp +++ b/include/mbgl/vulkan/context.hpp @@ -43,14 +43,6 @@ class Texture2D; using UniqueShaderProgram = std::unique_ptr; using UniqueVertexBufferResource = std::unique_ptr; -enum class DescriptorSetType : uint8_t { - Global, - Layer, - DrawableUniform, - DrawableImage, - Count, -}; - class Context final : public gfx::Context { public: Context(RendererBackend&); @@ -149,26 +141,14 @@ class Context final : public gfx::Context { const std::string& name); const std::vector& getDescriptorSetLayouts(); - const std::vector& getDrawableDescriptorSetLayouts(); const vk::DescriptorSetLayout& getDescriptorSetLayout(DescriptorSetType type); const vk::UniquePipelineLayout& getGeneralPipelineLayout(); const vk::UniquePipelineLayout& getPushConstantPipelineLayout(); - void bindUniformDescriptorSet(DescriptorSetType type, - const gfx::UniformBufferArray& uniforms, - size_t startID, - size_t count, - bool fillGaps = true); - - void bindUniformDescriptorSet(DescriptorSetType type, - const vk::DescriptorSet& descriptorSet, - const gfx::UniformBufferArray& uniforms, - size_t startID, - size_t count, - bool fillGaps = true); + void bindDescriptorSet(DescriptorSetType type, const vk::DescriptorSet& descriptorSet); uint8_t getCurrentFrameResourceIndex() const { return frameResourceIndex; } - const vk::UniqueDescriptorPool& getCurrentDescriptorPool() const; + const vk::UniqueDescriptorPool& getGlobalDescriptorPool() const; void enqueueDeletion(std::function&& function); void submitOneTimeCommand(const std::function& function) const; @@ -177,7 +157,6 @@ class Context final : public gfx::Context { private: struct FrameResources { vk::UniqueCommandBuffer commandBuffer; - vk::UniqueDescriptorPool descriptorPool; vk::UniqueSemaphore surfaceSemaphore; vk::UniqueSemaphore frameSemaphore; @@ -186,12 +165,10 @@ class Context final : public gfx::Context { std::vector> deletionQueue; FrameResources(vk::UniqueCommandBuffer& cb, - vk::UniqueDescriptorPool&& dp, vk::UniqueSemaphore&& surf, vk::UniqueSemaphore&& frame, vk::UniqueFence&& flight) : commandBuffer(std::move(cb)), - descriptorPool(std::move(dp)), surfaceSemaphore(std::move(surf)), frameSemaphore(std::move(frame)), flightFrameFence(std::move(flight)) {} @@ -206,6 +183,7 @@ class Context final : public gfx::Context { RendererBackend& backend; vulkan::UniformBufferArray globalUniformBuffers; + vk::UniqueDescriptorPool globalDescriptorPool; std::unique_ptr dummyVertexBuffer; std::unique_ptr dummyUniformBuffer; @@ -215,7 +193,6 @@ class Context final : public gfx::Context { vk::UniqueDescriptorSetLayout drawableUniformDescriptorSetLayout; vk::UniqueDescriptorSetLayout imageDescriptorSetLayout; std::vector descriptorSetLayouts; - std::vector drawableDescriptorSetLayouts; vk::UniquePipelineLayout generalPipelineLayout; vk::UniquePipelineLayout pushConstantPipelineLayout; diff --git a/include/mbgl/vulkan/descriptor_set.hpp b/include/mbgl/vulkan/descriptor_set.hpp new file mode 100644 index 00000000000..db0a252939a --- /dev/null +++ b/include/mbgl/vulkan/descriptor_set.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include +#include +#include + +namespace mbgl { +namespace vulkan { + +class CommandEncoder; + +enum class DescriptorSetType : uint8_t { + Global, + Layer, + DrawableUniform, + DrawableImage, + Count, +}; + +class DescriptorSet { +public: + DescriptorSet(Context& context_, DescriptorSetType type_); + virtual ~DescriptorSet(); + + void allocate(); + + void markDirty(bool value = true); + void bind(CommandEncoder& encoder); + +protected: + Context& context; + DescriptorSetType type; + + std::vector dirty; + std::vector descriptorSets; +}; + +class UniformDescriptorSet : public DescriptorSet { +public: + UniformDescriptorSet(Context& context_, DescriptorSetType type_); + virtual ~UniformDescriptorSet() = default; + + void update(const gfx::UniformBufferArray& uniforms, uint32_t uniformStartIndex, uint32_t descriptorBindingCount); +}; + +class ImageDescriptorSet : public DescriptorSet { +public: + ImageDescriptorSet(Context& context_); + virtual ~ImageDescriptorSet() = default; + + void update(const std::array& textures); +}; + +} // namespace vulkan +} // namespace mbgl diff --git a/include/mbgl/vulkan/uniform_buffer.hpp b/include/mbgl/vulkan/uniform_buffer.hpp index 7ae14fd2212..99d8c8f8cf1 100644 --- a/include/mbgl/vulkan/uniform_buffer.hpp +++ b/include/mbgl/vulkan/uniform_buffer.hpp @@ -2,6 +2,7 @@ #include #include +#include namespace mbgl { namespace vulkan { @@ -26,7 +27,13 @@ class UniformBuffer final : public gfx::UniformBuffer { /// Stores a collection of uniform buffers by name class UniformBufferArray final : public gfx::UniformBufferArray { public: - UniformBufferArray() = default; + UniformBufferArray() = delete; + UniformBufferArray(DescriptorSetType type, uint32_t index, uint32_t count) + : descriptorSetType(type), + descriptorStartIndex(index), + descriptorBindingCount(count) + {} + UniformBufferArray(UniformBufferArray&& other) : gfx::UniformBufferArray(std::move(other)) {} UniformBufferArray(const UniformBufferArray&) = delete; @@ -40,10 +47,26 @@ class UniformBufferArray final : public gfx::UniformBufferArray { return *this; } + ~UniformBufferArray() = default; + + const std::shared_ptr& set(const size_t id, std::shared_ptr uniformBuffer); + + void createOrUpdate( + const size_t id, const void* data, std::size_t size, gfx::Context& context, bool persistent = false) override; + + void bindDescriptorSets(CommandEncoder& encoder); + void freeDescriptorSets() { descriptorSet.reset(); } + private: gfx::UniqueUniformBuffer copy(const gfx::UniformBuffer& buffer) override { return std::make_unique(static_cast(buffer).clone()); } + + const DescriptorSetType descriptorSetType{DescriptorSetType::DrawableUniform}; + const uint32_t descriptorStartIndex{0}; + const uint32_t descriptorBindingCount{0}; + + std::unique_ptr descriptorSet; }; } // namespace vulkan diff --git a/src/mbgl/shaders/mtl/background.cpp b/src/mbgl/shaders/mtl/background.cpp index 406e2b2d0a9..fb968c27d37 100644 --- a/src/mbgl/shaders/mtl/background.cpp +++ b/src/mbgl/shaders/mtl/background.cpp @@ -1,4 +1,5 @@ #include +#include #include namespace mbgl { @@ -9,7 +10,7 @@ const std::array ShaderSource ShaderSource::attributes = { - AttributeInfo{backgroundUBOCount + 0, gfx::AttributeDataType::Float3, idBackgroundPosVertexAttribute}, + AttributeInfo{backgroundDrawableUBOCount + 0, gfx::AttributeDataType::Float3, idBackgroundPosVertexAttribute}, }; const std::array ShaderSource::textures = {}; diff --git a/src/mbgl/shaders/mtl/background_pattern.cpp b/src/mbgl/shaders/mtl/background_pattern.cpp index 4346f7d7e12..af7913312f4 100644 --- a/src/mbgl/shaders/mtl/background_pattern.cpp +++ b/src/mbgl/shaders/mtl/background_pattern.cpp @@ -1,4 +1,5 @@ #include +#include #include namespace mbgl { @@ -12,7 +13,7 @@ const std::array }; const std::array ShaderSource::attributes = { - AttributeInfo{backgroundUBOCount + 0, gfx::AttributeDataType::Float3, idBackgroundPosVertexAttribute}, + AttributeInfo{backgroundDrawableUBOCount + 0, gfx::AttributeDataType::Float3, idBackgroundPosVertexAttribute}, }; const std::array ShaderSource::textures = { TextureInfo{0, idBackgroundImageTexture}}; diff --git a/src/mbgl/shaders/mtl/circle.cpp b/src/mbgl/shaders/mtl/circle.cpp index cc445df4267..7afbfa9ac67 100644 --- a/src/mbgl/shaders/mtl/circle.cpp +++ b/src/mbgl/shaders/mtl/circle.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/mtl/collision_box.cpp b/src/mbgl/shaders/mtl/collision_box.cpp index 2523a47da57..2eadf5bf09a 100644 --- a/src/mbgl/shaders/mtl/collision_box.cpp +++ b/src/mbgl/shaders/mtl/collision_box.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { @@ -7,11 +8,11 @@ const std::array ShaderSource ShaderSource::attributes = { - AttributeInfo{collisionUBOCount + 0, gfx::AttributeDataType::Short2, idCollisionPosVertexAttribute}, - AttributeInfo{collisionUBOCount + 1, gfx::AttributeDataType::Short2, idCollisionAnchorPosVertexAttribute}, - AttributeInfo{collisionUBOCount + 2, gfx::AttributeDataType::Short2, idCollisionExtrudeVertexAttribute}, - AttributeInfo{collisionUBOCount + 3, gfx::AttributeDataType::UShort2, idCollisionPlacedVertexAttribute}, - AttributeInfo{collisionUBOCount + 4, gfx::AttributeDataType::Float2, idCollisionShiftVertexAttribute}, + AttributeInfo{collisionDrawableUBOCount + 0, gfx::AttributeDataType::Short2, idCollisionPosVertexAttribute}, + AttributeInfo{collisionDrawableUBOCount + 1, gfx::AttributeDataType::Short2, idCollisionAnchorPosVertexAttribute}, + AttributeInfo{collisionDrawableUBOCount + 2, gfx::AttributeDataType::Short2, idCollisionExtrudeVertexAttribute}, + AttributeInfo{collisionDrawableUBOCount + 3, gfx::AttributeDataType::UShort2, idCollisionPlacedVertexAttribute}, + AttributeInfo{collisionDrawableUBOCount + 4, gfx::AttributeDataType::Float2, idCollisionShiftVertexAttribute}, }; const std::array ShaderSource::textures = {}; diff --git a/src/mbgl/shaders/mtl/collision_circle.cpp b/src/mbgl/shaders/mtl/collision_circle.cpp index 6a56729e247..fe1d3c51ee3 100644 --- a/src/mbgl/shaders/mtl/collision_circle.cpp +++ b/src/mbgl/shaders/mtl/collision_circle.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { @@ -9,10 +10,10 @@ const std::array }; const std::array ShaderSource::attributes = { - AttributeInfo{collisionUBOCount + 0, gfx::AttributeDataType::Short2, idCollisionPosVertexAttribute}, - AttributeInfo{collisionUBOCount + 1, gfx::AttributeDataType::Short2, idCollisionAnchorPosVertexAttribute}, - AttributeInfo{collisionUBOCount + 2, gfx::AttributeDataType::Short2, idCollisionExtrudeVertexAttribute}, - AttributeInfo{collisionUBOCount + 3, gfx::AttributeDataType::UShort2, idCollisionPlacedVertexAttribute}, + AttributeInfo{collisionDrawableUBOCount + 0, gfx::AttributeDataType::Short2, idCollisionPosVertexAttribute}, + AttributeInfo{collisionDrawableUBOCount + 1, gfx::AttributeDataType::Short2, idCollisionAnchorPosVertexAttribute}, + AttributeInfo{collisionDrawableUBOCount + 2, gfx::AttributeDataType::Short2, idCollisionExtrudeVertexAttribute}, + AttributeInfo{collisionDrawableUBOCount + 3, gfx::AttributeDataType::UShort2, idCollisionPlacedVertexAttribute}, }; const std::array ShaderSource::textures = {}; diff --git a/src/mbgl/shaders/mtl/custom_symbol_icon.cpp b/src/mbgl/shaders/mtl/custom_symbol_icon.cpp index 0b6713c7c6b..8cee478d67b 100644 --- a/src/mbgl/shaders/mtl/custom_symbol_icon.cpp +++ b/src/mbgl/shaders/mtl/custom_symbol_icon.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { @@ -11,8 +12,8 @@ const std::array const std::array ShaderSource::attributes = { // always attributes - AttributeInfo{customSymbolUBOCount + 0, gfx::AttributeDataType::Float2, idCustomSymbolPosVertexAttribute}, - AttributeInfo{customSymbolUBOCount + 1, gfx::AttributeDataType::Float2, idCustomSymbolTexVertexAttribute}, + AttributeInfo{customSymbolDrawableUBOCount + 0, gfx::AttributeDataType::Float2, idCustomSymbolPosVertexAttribute}, + AttributeInfo{customSymbolDrawableUBOCount + 1, gfx::AttributeDataType::Float2, idCustomSymbolTexVertexAttribute}, }; const std::array ShaderSource::textures = { TextureInfo{0, idCustomSymbolImageTexture}, diff --git a/src/mbgl/shaders/mtl/debug.cpp b/src/mbgl/shaders/mtl/debug.cpp index 0103f27081a..d1be225ad9a 100644 --- a/src/mbgl/shaders/mtl/debug.cpp +++ b/src/mbgl/shaders/mtl/debug.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { @@ -7,7 +8,7 @@ const std::array ShaderSource ShaderSource::attributes = { - AttributeInfo{debugUBOCount + 0, gfx::AttributeDataType::Short2, idDebugPosVertexAttribute}, + AttributeInfo{debugDrawableUBOCount + 0, gfx::AttributeDataType::Short2, idDebugPosVertexAttribute}, }; const std::array ShaderSource::textures = { TextureInfo{0, idDebugOverlayTexture}, diff --git a/src/mbgl/shaders/mtl/fill.cpp b/src/mbgl/shaders/mtl/fill.cpp index 6c881b36853..80f124f7127 100644 --- a/src/mbgl/shaders/mtl/fill.cpp +++ b/src/mbgl/shaders/mtl/fill.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/mtl/fill_extrusion.cpp b/src/mbgl/shaders/mtl/fill_extrusion.cpp index 6022bb3a3b6..c5a94b955c4 100644 --- a/src/mbgl/shaders/mtl/fill_extrusion.cpp +++ b/src/mbgl/shaders/mtl/fill_extrusion.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { @@ -10,11 +11,11 @@ const std::array ShaderSource ShaderSource::attributes = { - AttributeInfo{fillExtrusionUBOCount + 0, gfx::AttributeDataType::Short2, idFillExtrusionPosVertexAttribute}, - AttributeInfo{fillExtrusionUBOCount + 1, gfx::AttributeDataType::Short4, idFillExtrusionNormalEdVertexAttribute}, - AttributeInfo{fillExtrusionUBOCount + 2, gfx::AttributeDataType::Float4, idFillExtrusionColorVertexAttribute}, - AttributeInfo{fillExtrusionUBOCount + 3, gfx::AttributeDataType::Float, idFillExtrusionBaseVertexAttribute}, - AttributeInfo{fillExtrusionUBOCount + 4, gfx::AttributeDataType::Float, idFillExtrusionHeightVertexAttribute}, + AttributeInfo{fillExtrusionDrawableUBOCount + 0, gfx::AttributeDataType::Short2, idFillExtrusionPosVertexAttribute}, + AttributeInfo{fillExtrusionDrawableUBOCount + 1, gfx::AttributeDataType::Short4, idFillExtrusionNormalEdVertexAttribute}, + AttributeInfo{fillExtrusionDrawableUBOCount + 2, gfx::AttributeDataType::Float4, idFillExtrusionColorVertexAttribute}, + AttributeInfo{fillExtrusionDrawableUBOCount + 3, gfx::AttributeDataType::Float, idFillExtrusionBaseVertexAttribute}, + AttributeInfo{fillExtrusionDrawableUBOCount + 4, gfx::AttributeDataType::Float, idFillExtrusionHeightVertexAttribute}, }; const std::array ShaderSource::textures = {}; diff --git a/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp b/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp index 6b32276d8ed..5180d8da447 100644 --- a/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp +++ b/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/mtl/heatmap.cpp b/src/mbgl/shaders/mtl/heatmap.cpp index 31a615e0c7e..c63e3acd745 100644 --- a/src/mbgl/shaders/mtl/heatmap.cpp +++ b/src/mbgl/shaders/mtl/heatmap.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/mtl/heatmap_texture.cpp b/src/mbgl/shaders/mtl/heatmap_texture.cpp index b87c7f01198..b536695beae 100644 --- a/src/mbgl/shaders/mtl/heatmap_texture.cpp +++ b/src/mbgl/shaders/mtl/heatmap_texture.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/mtl/hillshade.cpp b/src/mbgl/shaders/mtl/hillshade.cpp index 079c8b309c1..fbd93012a8c 100644 --- a/src/mbgl/shaders/mtl/hillshade.cpp +++ b/src/mbgl/shaders/mtl/hillshade.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/mtl/hillshade_prepare.cpp b/src/mbgl/shaders/mtl/hillshade_prepare.cpp index 7465e2bb06f..2dc295cbbba 100644 --- a/src/mbgl/shaders/mtl/hillshade_prepare.cpp +++ b/src/mbgl/shaders/mtl/hillshade_prepare.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { @@ -9,8 +10,8 @@ const std::array }; const std::array ShaderSource::attributes = { - AttributeInfo{hillshadePrepareUBOCount + 0, gfx::AttributeDataType::Short2, idHillshadePosVertexAttribute}, - AttributeInfo{hillshadePrepareUBOCount + 1, gfx::AttributeDataType::Short2, idHillshadeTexturePosVertexAttribute}, + AttributeInfo{hillshadePrepareDrawableUBOCount + 0, gfx::AttributeDataType::Short2, idHillshadePosVertexAttribute}, + AttributeInfo{hillshadePrepareDrawableUBOCount + 1, gfx::AttributeDataType::Short2, idHillshadeTexturePosVertexAttribute}, }; const std::array ShaderSource::textures = { TextureInfo{0, idHillshadeImageTexture}, diff --git a/src/mbgl/shaders/mtl/line.cpp b/src/mbgl/shaders/mtl/line.cpp index 88aeda1a977..d91ba21a8db 100644 --- a/src/mbgl/shaders/mtl/line.cpp +++ b/src/mbgl/shaders/mtl/line.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/mtl/raster.cpp b/src/mbgl/shaders/mtl/raster.cpp index a7a28418c12..cc13205d432 100644 --- a/src/mbgl/shaders/mtl/raster.cpp +++ b/src/mbgl/shaders/mtl/raster.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/mtl/symbol_icon.cpp b/src/mbgl/shaders/mtl/symbol_icon.cpp index 7aa78e46aeb..d25af5b666b 100644 --- a/src/mbgl/shaders/mtl/symbol_icon.cpp +++ b/src/mbgl/shaders/mtl/symbol_icon.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/mtl/symbol_sdf.cpp b/src/mbgl/shaders/mtl/symbol_sdf.cpp index 1b7131d018c..e288aab5c14 100644 --- a/src/mbgl/shaders/mtl/symbol_sdf.cpp +++ b/src/mbgl/shaders/mtl/symbol_sdf.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/shaders/mtl/symbol_text_and_icon.cpp b/src/mbgl/shaders/mtl/symbol_text_and_icon.cpp index 3d8a0e1c140..226a051cf91 100644 --- a/src/mbgl/shaders/mtl/symbol_text_and_icon.cpp +++ b/src/mbgl/shaders/mtl/symbol_text_and_icon.cpp @@ -1,4 +1,5 @@ #include +#include namespace mbgl { namespace shaders { diff --git a/src/mbgl/vulkan/context.cpp b/src/mbgl/vulkan/context.cpp index 974a5d0fe17..e8cf29c8a4e 100644 --- a/src/mbgl/vulkan/context.cpp +++ b/src/mbgl/vulkan/context.cpp @@ -28,7 +28,7 @@ #include #ifndef MLN_VULKAN_DESCRIPTOR_POOL_SIZE -#define MLN_VULKAN_DESCRIPTOR_POOL_SIZE 10000 +#define MLN_VULKAN_DESCRIPTOR_POOL_SIZE 30000 #endif namespace mbgl { @@ -48,7 +48,8 @@ class RenderbufferResource : public gfx::RenderbufferResource { Context::Context(RendererBackend& backend_) : gfx::Context(vulkan::maximumVertexBindingCount), - backend(backend_) { + backend(backend_), + globalUniformBuffers(DescriptorSetType::Global, 0, shaders::globalUBOCount) { if (glslangRefCount++ == 0) { glslang::InitializeProcess(); } @@ -70,26 +71,28 @@ void Context::initFrameResources() { const auto& device = backend.getDevice(); const auto frameCount = backend.getMaxFrames(); - // command buffers - const vk::CommandBufferAllocateInfo allocateInfo( - backend.getCommandPool().get(), vk::CommandBufferLevel::ePrimary, frameCount); - - auto commandBuffers = backend.getDevice()->allocateCommandBuffersUnique(allocateInfo); - // descriptor pool info const std::vector poolSizes = { {vk::DescriptorType::eUniformBuffer, MLN_VULKAN_DESCRIPTOR_POOL_SIZE}, {vk::DescriptorType::eCombinedImageSampler, MLN_VULKAN_DESCRIPTOR_POOL_SIZE}, }; - const auto descriptorPoolInfo = vk::DescriptorPoolCreateInfo().setPoolSizes(poolSizes).setMaxSets( - MLN_VULKAN_DESCRIPTOR_POOL_SIZE); + const auto descriptorPoolInfo = vk::DescriptorPoolCreateInfo(vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet) + .setPoolSizes(poolSizes) + .setMaxSets(MLN_VULKAN_DESCRIPTOR_POOL_SIZE); + + globalDescriptorPool = device->createDescriptorPoolUnique(descriptorPoolInfo); + + // command buffers + const vk::CommandBufferAllocateInfo allocateInfo( + backend.getCommandPool().get(), vk::CommandBufferLevel::ePrimary, frameCount); + + auto commandBuffers = backend.getDevice()->allocateCommandBuffersUnique(allocateInfo); frameResources.reserve(frameCount); for (uint32_t index = 0; index < frameCount; ++index) { frameResources.emplace_back(commandBuffers[index], - device->createDescriptorPoolUnique(descriptorPoolInfo), device->createSemaphoreUnique({}), device->createSemaphoreUnique({}), device->createFenceUnique(vk::FenceCreateInfo(vk::FenceCreateFlagBits::eSignaled))); @@ -97,7 +100,6 @@ void Context::initFrameResources() { const auto& frame = frameResources.back(); backend.setDebugName(frame.commandBuffer.get(), "FrameCommandBuffer_" + std::to_string(index)); - backend.setDebugName(frame.descriptorPool.get(), "DescriptorPool_" + std::to_string(index)); backend.setDebugName(frame.frameSemaphore.get(), "FrameSemaphore_" + std::to_string(index)); backend.setDebugName(frame.surfaceSemaphore.get(), "SurfaceSemaphore_" + std::to_string(index)); backend.setDebugName(frame.flightFrameFence.get(), "FrameFence_" + std::to_string(index)); @@ -114,12 +116,14 @@ void Context::destroyResources() { frame.runDeletionQueue(*this); } + globalUniformBuffers.freeDescriptorSets(); + // all resources have unique handles frameResources.clear(); } -const vk::UniqueDescriptorPool& Context::getCurrentDescriptorPool() const { - return frameResources[frameResourceIndex].descriptorPool; +const vk::UniqueDescriptorPool& Context::getGlobalDescriptorPool() const { + return globalDescriptorPool; } void Context::enqueueDeletion(std::function&& function) { @@ -178,7 +182,6 @@ void Context::beginFrame() { // we wait for an idle device to recreate the swapchain // so it's a good opportunity to delete all queued items for (auto& frame : frameResources) { - device->resetDescriptorPool(frame.descriptorPool.get()); frame.runDeletionQueue(*this); } @@ -194,7 +197,6 @@ void Context::beginFrame() { waitFrame(); - device->resetDescriptorPool(getCurrentDescriptorPool().get()); frame.runDeletionQueue(*this); if (platformSurface) { @@ -386,9 +388,9 @@ void Context::clearStencilBuffer(int32_t) { assert(false); } -void Context::bindGlobalUniformBuffers(gfx::RenderPass&) const noexcept { - const_cast(this)->bindUniformDescriptorSet( - DescriptorSetType::Global, globalUniformBuffers, 0, shaders::globalUBOCount); +void Context::bindGlobalUniformBuffers(gfx::RenderPass& renderPass) const noexcept { + auto& renderPassImpl = static_cast(renderPass); + const_cast(this)->globalUniformBuffers.bindDescriptorSets(renderPassImpl.getEncoder()); } bool Context::renderTileClippingMasks(gfx::RenderPass& renderPass, @@ -580,17 +582,9 @@ const std::vector& Context::getDescriptorSetLayouts() { return descriptorSetLayouts; } -const std::vector& Context::getDrawableDescriptorSetLayouts() { - if (drawableDescriptorSetLayouts.empty()) { - drawableDescriptorSetLayouts = {drawableUniformDescriptorSetLayout.get(), imageDescriptorSetLayout.get()}; - } - - return drawableDescriptorSetLayouts; -} - const vk::DescriptorSetLayout& Context::getDescriptorSetLayout(DescriptorSetType type) { uint32_t index = static_cast(type); - assert(index >= 0 && index < static_cast(DescriptorSetType::Count)); + assert(index < static_cast(DescriptorSetType::Count)); return getDescriptorSetLayouts()[index]; } @@ -621,71 +615,6 @@ const vk::UniquePipelineLayout& Context::getPushConstantPipelineLayout() { return pushConstantPipelineLayout; } -void Context::bindUniformDescriptorSet(DescriptorSetType type, - const gfx::UniformBufferArray& uniforms, - size_t startID, - size_t count, - bool fillGaps) { - - const auto& device = getBackend().getDevice(); - const auto& descriptorPool = getCurrentDescriptorPool(); - const auto& descriptorSetLayout = getDescriptorSetLayout(type); - - const auto descriptorAllocInfo = - vk::DescriptorSetAllocateInfo().setDescriptorPool(*descriptorPool).setSetLayouts(descriptorSetLayout); - - const auto& descriptorSets = device->allocateDescriptorSets(descriptorAllocInfo); - - bindUniformDescriptorSet(type, descriptorSets[0], uniforms, startID, count); - -} - -void Context::bindUniformDescriptorSet(DescriptorSetType type, - const vk::DescriptorSet& descriptorSet, - const gfx::UniformBufferArray& uniforms, - size_t startID, - size_t count, - bool fillGaps) { - - assert(static_cast(type) < static_cast(DescriptorSetType::DrawableImage)); - - const auto& device = getBackend().getDevice(); - - for (size_t index = 0; index < count; ++index) { - vk::DescriptorBufferInfo descriptorBufferInfo; - - if (const auto& uniformBuffer = uniforms.get(startID + index)) { - const auto& uniformBufferImpl = static_cast(*uniformBuffer); - const auto& bufferResource = uniformBufferImpl.getBufferResource(); - descriptorBufferInfo.setBuffer(bufferResource.getVulkanBuffer()) - .setOffset(bufferResource.getVulkanBufferOffset()) - .setRange(bufferResource.getSizeInBytes()); - } else if (fillGaps) { - descriptorBufferInfo.setBuffer(getDummyUniformBuffer()->getVulkanBuffer()) - .setOffset(0) - .setRange(VK_WHOLE_SIZE); - } else { - continue; - } - - const auto writeDescriptorSet = vk::WriteDescriptorSet() - .setBufferInfo(descriptorBufferInfo) - .setDescriptorCount(1) - .setDescriptorType(vk::DescriptorType::eUniformBuffer) - .setDstBinding(index) - .setDstSet(descriptorSet); - - device->updateDescriptorSets(writeDescriptorSet, nullptr); - } - - auto& frame = frameResources[frameResourceIndex]; - frame.commandBuffer->bindDescriptorSets(vk::PipelineBindPoint::eGraphics, - getGeneralPipelineLayout().get(), - static_cast(type), - descriptorSet, - nullptr); -} - void Context::FrameResources::runDeletionQueue(const Context& context) { for (const auto& function : deletionQueue) function(context); diff --git a/src/mbgl/vulkan/descriptor_set.cpp b/src/mbgl/vulkan/descriptor_set.cpp new file mode 100644 index 00000000000..752b88b15b7 --- /dev/null +++ b/src/mbgl/vulkan/descriptor_set.cpp @@ -0,0 +1,137 @@ +#include + +#include +#include +#include + +#include + +namespace mbgl { +namespace vulkan { + +DescriptorSet::DescriptorSet(Context& context_, DescriptorSetType type_) + : context(context_), + type(type_) {} + +DescriptorSet::~DescriptorSet() { + context.enqueueDeletion([sets = std::move(descriptorSets)](const auto& context_) mutable { + context_.getBackend().getDevice()->freeDescriptorSets(context_.getGlobalDescriptorPool().get(), sets); + }); +} + +void DescriptorSet::allocate() { + if (!descriptorSets.empty()) { + return; + } + + const auto& device = context.getBackend().getDevice(); + const auto& descriptorPool = context.getGlobalDescriptorPool(); + const auto& descriptorSetLayout = context.getDescriptorSetLayout(type); + const std::vector layouts(context.getBackend().getMaxFrames(), descriptorSetLayout); + + const auto descriptorAllocInfo = + vk::DescriptorSetAllocateInfo().setDescriptorPool(*descriptorPool).setSetLayouts(layouts); + + descriptorSets = device->allocateDescriptorSets(descriptorAllocInfo); + + markDirty(true); +} + +void DescriptorSet::markDirty(bool value) { + dirty = std::vector(descriptorSets.size(), value); +} + +void DescriptorSet::bind(CommandEncoder& encoder) { + auto& commandBuffer = encoder.getCommandBuffer(); + + const uint8_t index = context.getCurrentFrameResourceIndex(); + + commandBuffer->bindDescriptorSets(vk::PipelineBindPoint::eGraphics, + context.getGeneralPipelineLayout().get(), + static_cast(type), + descriptorSets[index], + nullptr); +} + +UniformDescriptorSet::UniformDescriptorSet(Context& context_, DescriptorSetType type_) + : DescriptorSet(context_, type_) { + +} + +void UniformDescriptorSet::update(const gfx::UniformBufferArray& uniforms, + uint32_t uniformStartIndex, + uint32_t descriptorBindingCount) { + + allocate(); + + const uint8_t frameIndex = context.getCurrentFrameResourceIndex(); + if (!dirty[frameIndex]) { + return; + } + + const auto& device = context.getBackend().getDevice(); + + for (size_t index = 0; index < descriptorBindingCount; ++index) { + vk::DescriptorBufferInfo descriptorBufferInfo; + + if (const auto& uniformBuffer = uniforms.get(uniformStartIndex + 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()) + .setOffset(0) + .setRange(VK_WHOLE_SIZE); + } + + const auto writeDescriptorSet = vk::WriteDescriptorSet() + .setBufferInfo(descriptorBufferInfo) + .setDescriptorCount(1) + .setDescriptorType(vk::DescriptorType::eUniformBuffer) + .setDstBinding(index) + .setDstSet(descriptorSets[frameIndex]); + + device->updateDescriptorSets(writeDescriptorSet, nullptr); + } +} + +ImageDescriptorSet::ImageDescriptorSet(Context& context_) + : DescriptorSet(context_, DescriptorSetType::DrawableImage) { + +} + +void ImageDescriptorSet::update(const std::array& textures) { + + allocate(); + + const uint8_t frameIndex = context.getCurrentFrameResourceIndex(); + if (!dirty[frameIndex]) { + return; + } + + const auto& device = context.getBackend().getDevice(); + + for (size_t id = 0; id < shaders::maxTextureCountPerShader; ++id) { + const auto& texture = id < textures.size() ? textures[id] : nullptr; + auto& textureImpl = texture ? static_cast(*texture) : *context.getDummyTexture(); + + const auto descriptorImageInfo = vk::DescriptorImageInfo() + .setImageLayout(textureImpl.getVulkanImageLayout()) + .setImageView(textureImpl.getVulkanImageView().get()) + .setSampler(textureImpl.getVulkanSampler()); + + const auto writeDescriptorSet = vk::WriteDescriptorSet() + .setImageInfo(descriptorImageInfo) + .setDescriptorCount(1) + .setDescriptorType(vk::DescriptorType::eCombinedImageSampler) + .setDstBinding(id) + .setDstSet(descriptorSets[frameIndex]); + + device->updateDescriptorSets(writeDescriptorSet, nullptr); + } +} + +} // namespace vulkan +} // namespace mbgl diff --git a/src/mbgl/vulkan/drawable.cpp b/src/mbgl/vulkan/drawable.cpp index 808d52f1009..0babcd8ef3f 100644 --- a/src/mbgl/vulkan/drawable.cpp +++ b/src/mbgl/vulkan/drawable.cpp @@ -398,55 +398,16 @@ bool Drawable::bindAttributes(CommandEncoder& encoder) const noexcept { bool Drawable::bindDescriptors(CommandEncoder& encoder) const noexcept { if (!shader) return false; - auto& context = encoder.getContext(); - const auto& device = context.getBackend().getDevice(); - const auto& descriptorPool = context.getCurrentDescriptorPool(); - const auto& descriptorSetLayouts = context.getDrawableDescriptorSetLayouts(); - - const auto descriptorAllocInfo = - vk::DescriptorSetAllocateInfo().setDescriptorPool(*descriptorPool).setSetLayouts(descriptorSetLayouts); - - const auto& drawableDescriptorSets = device->allocateDescriptorSets(descriptorAllocInfo); - - const auto& drawableUniformDescriptorSet = drawableDescriptorSets[0]; - const auto& drawableImageDescriptorSet = drawableDescriptorSets[1]; - - const auto& uniformBufferArray = getUniformBuffers(); - - context.bindUniformDescriptorSet(DescriptorSetType::DrawableUniform, - drawableUniformDescriptorSet, - uniformBufferArray, - shaders::globalUBOCount, - shaders::maxUBOCountPerDrawable); + // bind uniforms + impl->uniformBuffers.bindDescriptorSets(encoder); // update image set - { - for (size_t id = 0; id < shaders::maxTextureCountPerShader; ++id) { - const auto& texture = id < textures.size() ? textures[id] : nullptr; - auto& textureImpl = texture ? static_cast(*texture) : *context.getDummyTexture(); - - const auto descriptorImageInfo = vk::DescriptorImageInfo() - .setImageLayout(textureImpl.getVulkanImageLayout()) - .setImageView(textureImpl.getVulkanImageView().get()) - .setSampler(textureImpl.getVulkanSampler()); - - const auto writeDescriptorSet = vk::WriteDescriptorSet() - .setImageInfo(descriptorImageInfo) - .setDescriptorCount(1) - .setDescriptorType(vk::DescriptorType::eCombinedImageSampler) - .setDstBinding(id) - .setDstSet(drawableImageDescriptorSet); - - device->updateDescriptorSets(writeDescriptorSet, nullptr); - } + if (!impl->imageDescriptorSet) { + impl->imageDescriptorSet = std::make_unique(encoder.getContext()); } - const auto& commandBuffer = encoder.getCommandBuffer(); - commandBuffer->bindDescriptorSets(vk::PipelineBindPoint::eGraphics, - context.getGeneralPipelineLayout().get(), - static_cast(DescriptorSetType::DrawableImage), - drawableImageDescriptorSet, - nullptr); + impl->imageDescriptorSet->update(textures); + impl->imageDescriptorSet->bind(encoder); return true; } diff --git a/src/mbgl/vulkan/drawable_impl.hpp b/src/mbgl/vulkan/drawable_impl.hpp index a057460e432..7d3b68d0640 100644 --- a/src/mbgl/vulkan/drawable_impl.hpp +++ b/src/mbgl/vulkan/drawable_impl.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -22,7 +23,11 @@ using namespace platform; class Drawable::Impl final { public: - Impl() = default; + Impl() + : uniformBuffers( + DescriptorSetType::DrawableUniform, shaders::globalUBOCount, shaders::maxUBOCountPerDrawable) + {} + ~Impl() = default; std::vector segments; @@ -47,6 +52,8 @@ class Drawable::Impl final { std::vector vulkanVertexBuffers; std::vector vulkanVertexOffsets; + + std::unique_ptr imageDescriptorSet; }; } // namespace vulkan diff --git a/src/mbgl/vulkan/layer_group.cpp b/src/mbgl/vulkan/layer_group.cpp index 6a5f43c7739..f86d556e037 100644 --- a/src/mbgl/vulkan/layer_group.cpp +++ b/src/mbgl/vulkan/layer_group.cpp @@ -15,7 +15,8 @@ namespace mbgl { namespace vulkan { LayerGroup::LayerGroup(int32_t layerIndex_, std::size_t initialCapacity, std::string name_) - : mbgl::LayerGroup(layerIndex_, initialCapacity, std::move(name_)) {} + : mbgl::LayerGroup(layerIndex_, initialCapacity, std::move(name_)), + uniformBuffers(DescriptorSetType::Layer, shaders::layerUBOStartId, shaders::maxUBOCountPerLayer) {} void LayerGroup::upload(gfx::UploadPass& uploadPass) { if (!enabled) { @@ -44,7 +45,7 @@ void LayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { #endif auto& renderPass = static_cast(*parameters.renderPass); - auto& context = renderPass.getEncoder().getContext(); + auto& encoder = renderPass.getEncoder(); bool bindUBOs = false; visitDrawables([&](gfx::Drawable& drawable) { @@ -57,8 +58,7 @@ void LayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { } if (!bindUBOs) { - context.bindUniformDescriptorSet( - DescriptorSetType::Layer, uniformBuffers, shaders::layerUBOStartId, shaders::maxUBOCountPerLayer); + uniformBuffers.bindDescriptorSets(encoder); bindUBOs = true; } diff --git a/src/mbgl/vulkan/tile_layer_group.cpp b/src/mbgl/vulkan/tile_layer_group.cpp index 715f89c24d7..c9fb7e9177f 100644 --- a/src/mbgl/vulkan/tile_layer_group.cpp +++ b/src/mbgl/vulkan/tile_layer_group.cpp @@ -16,7 +16,8 @@ namespace mbgl { namespace vulkan { TileLayerGroup::TileLayerGroup(int32_t layerIndex_, std::size_t initialCapacity, std::string name_) - : mbgl::TileLayerGroup(layerIndex_, initialCapacity, std::move(name_)) {} + : mbgl::TileLayerGroup(layerIndex_, initialCapacity, std::move(name_)), + uniformBuffers(DescriptorSetType::Layer, shaders::layerUBOStartId, shaders::maxUBOCountPerLayer) {} void TileLayerGroup::upload(gfx::UploadPass& uploadPass) { if (!enabled || !getDrawableCount()) { @@ -41,7 +42,7 @@ void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { } auto& renderPass = static_cast(*parameters.renderPass); - auto& context = renderPass.getEncoder().getContext(); + auto& encoder = renderPass.getEncoder(); // `stencilModeFor3D` uses a different stencil mask value each time its called, so if the // drawables in this layer use 3D stencil mode, we need to set it up here so that all the @@ -93,8 +94,7 @@ void TileLayerGroup::render(RenderOrchestrator&, PaintParameters& parameters) { } if (!bindUBOs) { - context.bindUniformDescriptorSet( - DescriptorSetType::Layer, uniformBuffers, shaders::layerUBOStartId, shaders::maxUBOCountPerLayer); + uniformBuffers.bindDescriptorSets(encoder); bindUBOs = true; } diff --git a/src/mbgl/vulkan/uniform_buffer.cpp b/src/mbgl/vulkan/uniform_buffer.cpp index 40ba0087822..9f5364f2d78 100644 --- a/src/mbgl/vulkan/uniform_buffer.cpp +++ b/src/mbgl/vulkan/uniform_buffer.cpp @@ -2,6 +2,7 @@ #include #include +#include #include @@ -38,5 +39,34 @@ void UniformBuffer::update(const void* data, std::size_t size_) { buffer.update(data, size, /*offset=*/0); } +const std::shared_ptr& UniformBufferArray::set(const size_t id, + std::shared_ptr uniformBuffer) { + if (descriptorSet) { + descriptorSet->markDirty(); + } + + return gfx::UniformBufferArray::set(id, uniformBuffer); +} + +void UniformBufferArray::createOrUpdate( + const size_t id, const void* data, std::size_t size, gfx::Context& context, bool persistent) { + if (descriptorSet) { + if (auto& ubo = get(id); !ubo || ubo->getSize() != size) { + descriptorSet->markDirty(); + } + } + + gfx::UniformBufferArray::createOrUpdate(id, data, size, context, persistent); +} + +void UniformBufferArray::bindDescriptorSets(CommandEncoder& encoder) { + if (!descriptorSet) { + descriptorSet = std::make_unique(encoder.getContext(), descriptorSetType); + } + + descriptorSet->update(*this, descriptorStartIndex, descriptorBindingCount); + descriptorSet->bind(encoder); +} + } // namespace vulkan } // namespace mbgl From 215e6311f0160894d676807e9d81737aa5e759cd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Oct 2024 20:56:40 +0000 Subject: [PATCH 04/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- include/mbgl/vulkan/uniform_buffer.hpp | 5 ++--- src/mbgl/shaders/mtl/collision_circle.cpp | 3 ++- src/mbgl/shaders/mtl/custom_symbol_icon.cpp | 10 +++++----- src/mbgl/shaders/mtl/fill_extrusion.cpp | 9 ++++++--- src/mbgl/shaders/mtl/hillshade_prepare.cpp | 3 ++- src/mbgl/vulkan/descriptor_set.cpp | 10 ++-------- src/mbgl/vulkan/drawable_impl.hpp | 5 ++--- 7 files changed, 21 insertions(+), 24 deletions(-) diff --git a/include/mbgl/vulkan/uniform_buffer.hpp b/include/mbgl/vulkan/uniform_buffer.hpp index 99d8c8f8cf1..1389ec5df71 100644 --- a/include/mbgl/vulkan/uniform_buffer.hpp +++ b/include/mbgl/vulkan/uniform_buffer.hpp @@ -31,9 +31,8 @@ class UniformBufferArray final : public gfx::UniformBufferArray { UniformBufferArray(DescriptorSetType type, uint32_t index, uint32_t count) : descriptorSetType(type), descriptorStartIndex(index), - descriptorBindingCount(count) - {} - + descriptorBindingCount(count) {} + UniformBufferArray(UniformBufferArray&& other) : gfx::UniformBufferArray(std::move(other)) {} UniformBufferArray(const UniformBufferArray&) = delete; diff --git a/src/mbgl/shaders/mtl/collision_circle.cpp b/src/mbgl/shaders/mtl/collision_circle.cpp index fe1d3c51ee3..bc407e1d235 100644 --- a/src/mbgl/shaders/mtl/collision_circle.cpp +++ b/src/mbgl/shaders/mtl/collision_circle.cpp @@ -11,7 +11,8 @@ const std::array const std::array ShaderSource::attributes = { AttributeInfo{collisionDrawableUBOCount + 0, gfx::AttributeDataType::Short2, idCollisionPosVertexAttribute}, - AttributeInfo{collisionDrawableUBOCount + 1, gfx::AttributeDataType::Short2, idCollisionAnchorPosVertexAttribute}, + AttributeInfo{ + collisionDrawableUBOCount + 1, gfx::AttributeDataType::Short2, idCollisionAnchorPosVertexAttribute}, AttributeInfo{collisionDrawableUBOCount + 2, gfx::AttributeDataType::Short2, idCollisionExtrudeVertexAttribute}, AttributeInfo{collisionDrawableUBOCount + 3, gfx::AttributeDataType::UShort2, idCollisionPlacedVertexAttribute}, }; diff --git a/src/mbgl/shaders/mtl/custom_symbol_icon.cpp b/src/mbgl/shaders/mtl/custom_symbol_icon.cpp index 8cee478d67b..8b104bd4497 100644 --- a/src/mbgl/shaders/mtl/custom_symbol_icon.cpp +++ b/src/mbgl/shaders/mtl/custom_symbol_icon.cpp @@ -9,11 +9,11 @@ const std::array UniformBlockInfo{true, false, sizeof(CustomSymbolIconDrawableUBO), idCustomSymbolDrawableUBO}, UniformBlockInfo{true, false, sizeof(CustomSymbolIconParametersUBO), idCustomSymbolParametersUBO}, }; -const std::array - ShaderSource::attributes = { - // always attributes - AttributeInfo{customSymbolDrawableUBOCount + 0, gfx::AttributeDataType::Float2, idCustomSymbolPosVertexAttribute}, - AttributeInfo{customSymbolDrawableUBOCount + 1, gfx::AttributeDataType::Float2, idCustomSymbolTexVertexAttribute}, +const std::array ShaderSource::attributes = { + // always attributes + AttributeInfo{customSymbolDrawableUBOCount + 0, gfx::AttributeDataType::Float2, idCustomSymbolPosVertexAttribute}, + AttributeInfo{customSymbolDrawableUBOCount + 1, gfx::AttributeDataType::Float2, idCustomSymbolTexVertexAttribute}, }; const std::array ShaderSource::textures = { TextureInfo{0, idCustomSymbolImageTexture}, diff --git a/src/mbgl/shaders/mtl/fill_extrusion.cpp b/src/mbgl/shaders/mtl/fill_extrusion.cpp index c5a94b955c4..0ddcb6de738 100644 --- a/src/mbgl/shaders/mtl/fill_extrusion.cpp +++ b/src/mbgl/shaders/mtl/fill_extrusion.cpp @@ -12,10 +12,13 @@ const std::array ShaderSource ShaderSource::attributes = { AttributeInfo{fillExtrusionDrawableUBOCount + 0, gfx::AttributeDataType::Short2, idFillExtrusionPosVertexAttribute}, - AttributeInfo{fillExtrusionDrawableUBOCount + 1, gfx::AttributeDataType::Short4, idFillExtrusionNormalEdVertexAttribute}, - AttributeInfo{fillExtrusionDrawableUBOCount + 2, gfx::AttributeDataType::Float4, idFillExtrusionColorVertexAttribute}, + AttributeInfo{ + fillExtrusionDrawableUBOCount + 1, gfx::AttributeDataType::Short4, idFillExtrusionNormalEdVertexAttribute}, + AttributeInfo{ + fillExtrusionDrawableUBOCount + 2, gfx::AttributeDataType::Float4, idFillExtrusionColorVertexAttribute}, AttributeInfo{fillExtrusionDrawableUBOCount + 3, gfx::AttributeDataType::Float, idFillExtrusionBaseVertexAttribute}, - AttributeInfo{fillExtrusionDrawableUBOCount + 4, gfx::AttributeDataType::Float, idFillExtrusionHeightVertexAttribute}, + AttributeInfo{ + fillExtrusionDrawableUBOCount + 4, gfx::AttributeDataType::Float, idFillExtrusionHeightVertexAttribute}, }; const std::array ShaderSource::textures = {}; diff --git a/src/mbgl/shaders/mtl/hillshade_prepare.cpp b/src/mbgl/shaders/mtl/hillshade_prepare.cpp index 2dc295cbbba..f5eea79ea21 100644 --- a/src/mbgl/shaders/mtl/hillshade_prepare.cpp +++ b/src/mbgl/shaders/mtl/hillshade_prepare.cpp @@ -11,7 +11,8 @@ const std::array const std::array ShaderSource::attributes = { AttributeInfo{hillshadePrepareDrawableUBOCount + 0, gfx::AttributeDataType::Short2, idHillshadePosVertexAttribute}, - AttributeInfo{hillshadePrepareDrawableUBOCount + 1, gfx::AttributeDataType::Short2, idHillshadeTexturePosVertexAttribute}, + AttributeInfo{ + hillshadePrepareDrawableUBOCount + 1, gfx::AttributeDataType::Short2, idHillshadeTexturePosVertexAttribute}, }; const std::array ShaderSource::textures = { TextureInfo{0, idHillshadeImageTexture}, diff --git a/src/mbgl/vulkan/descriptor_set.cpp b/src/mbgl/vulkan/descriptor_set.cpp index 752b88b15b7..10f041002a7 100644 --- a/src/mbgl/vulkan/descriptor_set.cpp +++ b/src/mbgl/vulkan/descriptor_set.cpp @@ -54,14 +54,11 @@ void DescriptorSet::bind(CommandEncoder& encoder) { } UniformDescriptorSet::UniformDescriptorSet(Context& context_, DescriptorSetType type_) - : DescriptorSet(context_, type_) { - -} + : DescriptorSet(context_, type_) {} void UniformDescriptorSet::update(const gfx::UniformBufferArray& uniforms, uint32_t uniformStartIndex, uint32_t descriptorBindingCount) { - allocate(); const uint8_t frameIndex = context.getCurrentFrameResourceIndex(); @@ -98,12 +95,9 @@ void UniformDescriptorSet::update(const gfx::UniformBufferArray& uniforms, } ImageDescriptorSet::ImageDescriptorSet(Context& context_) - : DescriptorSet(context_, DescriptorSetType::DrawableImage) { - -} + : DescriptorSet(context_, DescriptorSetType::DrawableImage) {} void ImageDescriptorSet::update(const std::array& textures) { - allocate(); const uint8_t frameIndex = context.getCurrentFrameResourceIndex(); diff --git a/src/mbgl/vulkan/drawable_impl.hpp b/src/mbgl/vulkan/drawable_impl.hpp index 7d3b68d0640..b484ef533c1 100644 --- a/src/mbgl/vulkan/drawable_impl.hpp +++ b/src/mbgl/vulkan/drawable_impl.hpp @@ -24,9 +24,8 @@ using namespace platform; class Drawable::Impl final { public: Impl() - : uniformBuffers( - DescriptorSetType::DrawableUniform, shaders::globalUBOCount, shaders::maxUBOCountPerDrawable) - {} + : uniformBuffers(DescriptorSetType::DrawableUniform, shaders::globalUBOCount, shaders::maxUBOCountPerDrawable) { + } ~Impl() = default; From 9beb898f61a0f8c161a29c9671082846130e3ead Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Thu, 3 Oct 2024 16:44:13 +0300 Subject: [PATCH 05/11] Fix compile error --- include/mbgl/vulkan/uniform_buffer.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/mbgl/vulkan/uniform_buffer.hpp b/include/mbgl/vulkan/uniform_buffer.hpp index 1389ec5df71..20323a32081 100644 --- a/include/mbgl/vulkan/uniform_buffer.hpp +++ b/include/mbgl/vulkan/uniform_buffer.hpp @@ -48,7 +48,8 @@ class UniformBufferArray final : public gfx::UniformBufferArray { ~UniformBufferArray() = default; - const std::shared_ptr& set(const size_t id, std::shared_ptr uniformBuffer); + const std::shared_ptr& set(const size_t id, + std::shared_ptr uniformBuffer) override; void createOrUpdate( const size_t id, const void* data, std::size_t size, gfx::Context& context, bool persistent = false) override; From a0d836f2580b5e31f6e6d9e361c192f1fe5896ef Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Mon, 7 Oct 2024 15:32:09 +0300 Subject: [PATCH 06/11] Fix metal shader compile errors --- include/mbgl/shaders/mtl/circle.hpp | 4 ++-- include/mbgl/shaders/mtl/fill_extrusion.hpp | 4 ++-- .../mbgl/shaders/mtl/fill_extrusion_pattern.hpp | 4 ++-- include/mbgl/shaders/mtl/heatmap.hpp | 4 ++-- include/mbgl/vulkan/uniform_buffer.hpp | 10 ++++++---- src/mbgl/vulkan/drawable.cpp | 15 +++++++++------ 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/include/mbgl/shaders/mtl/circle.hpp b/include/mbgl/shaders/mtl/circle.hpp index cc0525bc537..94b22fb1523 100644 --- a/include/mbgl/shaders/mtl/circle.hpp +++ b/include/mbgl/shaders/mtl/circle.hpp @@ -110,8 +110,8 @@ struct alignas(16) CircleInterpolateUBO { FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const GlobalPaintParamsUBO& paintParams [[buffer(0)]], device const CircleDrawableUBO& drawable [[buffer(1)]], - device const CircleInterpolateUBO& interp [[buffer(2)]]), - device const CircleEvaluatedPropsUBO& props [[buffer(3)]] { + device const CircleInterpolateUBO& interp [[buffer(2)]], + device const CircleEvaluatedPropsUBO& props [[buffer(3)]]) { #if defined(HAS_UNIFORM_u_radius) const auto radius = props.radius; diff --git a/include/mbgl/shaders/mtl/fill_extrusion.hpp b/include/mbgl/shaders/mtl/fill_extrusion.hpp index 7339aa36bbf..73440ccbc74 100644 --- a/include/mbgl/shaders/mtl/fill_extrusion.hpp +++ b/include/mbgl/shaders/mtl/fill_extrusion.hpp @@ -47,8 +47,8 @@ struct FragmentOutput { FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const FillExtrusionDrawableUBO& drawable [[buffer(1)]], - device const FillExtrusionInterpolateUBO& interp [[buffer(3)]]), - device const FillExtrusionPropsUBO& props [[buffer(4)]] { + device const FillExtrusionInterpolateUBO& interp [[buffer(3)]], + device const FillExtrusionPropsUBO& props [[buffer(4)]]) { #if defined(HAS_UNIFORM_u_base) const auto base = props.light_position_base.w; diff --git a/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp b/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp index c58b85c1a90..162d313c32b 100644 --- a/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp +++ b/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp @@ -61,8 +61,8 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const GlobalPaintParamsUBO& paintParams [[buffer(0)]], device const FillExtrusionDrawableUBO& drawable [[buffer(1)]], device const FillExtrusionTilePropsUBO& tileProps [[buffer(2)]], - device const FillExtrusionInterpolateUBO& interp [[buffer(3)]]), - device const FillExtrusionPropsUBO& props [[buffer(4)]], { + device const FillExtrusionInterpolateUBO& interp [[buffer(3)]], + device const FillExtrusionPropsUBO& props [[buffer(4)]]), { #if defined(HAS_UNIFORM_u_base) const auto base = props.light_position_base.w; diff --git a/include/mbgl/shaders/mtl/heatmap.hpp b/include/mbgl/shaders/mtl/heatmap.hpp index df22bb0b49a..e04ae35d126 100644 --- a/include/mbgl/shaders/mtl/heatmap.hpp +++ b/include/mbgl/shaders/mtl/heatmap.hpp @@ -68,8 +68,8 @@ constant const float ZERO = 1.0 / 255.0 / 16.0; FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const HeatmapDrawableUBO& drawable [[buffer(1)]], - device const HeatmapInterpolateUBO& interp [[buffer(2)]]), - device const HeatmapEvaluatedPropsUBO& props [[buffer(3)]] { + device const HeatmapInterpolateUBO& interp [[buffer(2)]], + device const HeatmapEvaluatedPropsUBO& props [[buffer(3)]]) { #if defined(HAS_UNIFORM_u_weight) const auto weight = props.weight; diff --git a/include/mbgl/vulkan/uniform_buffer.hpp b/include/mbgl/vulkan/uniform_buffer.hpp index 20323a32081..bda38209b61 100644 --- a/include/mbgl/vulkan/uniform_buffer.hpp +++ b/include/mbgl/vulkan/uniform_buffer.hpp @@ -28,10 +28,12 @@ class UniformBuffer final : public gfx::UniformBuffer { class UniformBufferArray final : public gfx::UniformBufferArray { public: UniformBufferArray() = delete; - UniformBufferArray(DescriptorSetType type, uint32_t index, uint32_t count) - : descriptorSetType(type), - descriptorStartIndex(index), - descriptorBindingCount(count) {} + UniformBufferArray(DescriptorSetType descriptorSetType_, + uint32_t descriptorStartIndex_, + uint32_t descriptorBindingCount_) + : descriptorSetType(descriptorSetType_), + descriptorStartIndex(descriptorStartIndex_), + descriptorBindingCount(descriptorBindingCount) {} UniformBufferArray(UniformBufferArray&& other) : gfx::UniformBufferArray(std::move(other)) {} diff --git a/src/mbgl/vulkan/drawable.cpp b/src/mbgl/vulkan/drawable.cpp index 6bca90a154b..a5a67581c0a 100644 --- a/src/mbgl/vulkan/drawable.cpp +++ b/src/mbgl/vulkan/drawable.cpp @@ -408,13 +408,16 @@ bool Drawable::bindDescriptors(CommandEncoder& encoder) const noexcept { // bind uniforms impl->uniformBuffers.bindDescriptorSets(encoder); - // update image set - if (!impl->imageDescriptorSet) { - impl->imageDescriptorSet = std::make_unique(encoder.getContext()); - } + const auto& shaderImpl = static_cast(*shader); + if (shaderImpl.hasTextures()) { + // update image set + if (!impl->imageDescriptorSet) { + impl->imageDescriptorSet = std::make_unique(encoder.getContext()); + } - impl->imageDescriptorSet->update(textures); - impl->imageDescriptorSet->bind(encoder); + impl->imageDescriptorSet->update(textures); + impl->imageDescriptorSet->bind(encoder); + } return true; } From ad7d4baeeb1195a5e6e13cd86ba27dc2af93744d Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Tue, 8 Oct 2024 17:14:34 +0300 Subject: [PATCH 07/11] Add layer tweaker for polyline drawables --- include/mbgl/vulkan/uniform_buffer.hpp | 2 +- src/mbgl/renderer/renderer_impl.cpp | 7 +- .../renderer/sources/render_tile_source.cpp | 93 ++++++++++++++++--- .../renderer/sources/render_tile_source.hpp | 4 + 4 files changed, 88 insertions(+), 18 deletions(-) diff --git a/include/mbgl/vulkan/uniform_buffer.hpp b/include/mbgl/vulkan/uniform_buffer.hpp index bda38209b61..83854bee3a2 100644 --- a/include/mbgl/vulkan/uniform_buffer.hpp +++ b/include/mbgl/vulkan/uniform_buffer.hpp @@ -33,7 +33,7 @@ class UniformBufferArray final : public gfx::UniformBufferArray { uint32_t descriptorBindingCount_) : descriptorSetType(descriptorSetType_), descriptorStartIndex(descriptorStartIndex_), - descriptorBindingCount(descriptorBindingCount) {} + descriptorBindingCount(descriptorBindingCount_) {} UniformBufferArray(UniformBufferArray&& other) : gfx::UniformBufferArray(std::move(other)) {} diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp index 2c4f23a935b..55c45515744 100644 --- a/src/mbgl/renderer/renderer_impl.cpp +++ b/src/mbgl/renderer/renderer_impl.cpp @@ -453,12 +453,7 @@ void Renderer::Impl::render(const RenderTree& renderTree, { const auto debugGroup(parameters.renderPass->createDebugGroup("debug")); orchestrator.visitDebugLayerGroups([&](LayerGroupBase& layerGroup) { - visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) { - for (const auto& tweaker : drawable.getTweakers()) { - tweaker->execute(drawable, parameters); - } - drawable.draw(parameters); - }); + layerGroup.render(orchestrator, parameters); }); } }; diff --git a/src/mbgl/renderer/sources/render_tile_source.cpp b/src/mbgl/renderer/sources/render_tile_source.cpp index e9831486d51..66209a05a00 100644 --- a/src/mbgl/renderer/sources/render_tile_source.cpp +++ b/src/mbgl/renderer/sources/render_tile_source.cpp @@ -55,6 +55,65 @@ void TileSourceRenderItem::render(PaintParameters& parameters) const { } #if MLN_DRAWABLE_RENDERER + +#if MLN_RENDER_BACKEND_VULKAN +class PolylineLayerImpl : public Layer::Impl { +public: + PolylineLayerImpl() + : Layer::Impl("", "") {} + bool hasLayoutDifference(const Layer::Impl&) const override { return false; } + void stringifyLayout(rapidjson::Writer&) const override {} + + const LayerTypeInfo* getTypeInfo() const noexcept final { return staticTypeInfo(); } + static const LayerTypeInfo* staticTypeInfo() noexcept { + const static LayerTypeInfo typeInfo{"debugPolyline", + LayerTypeInfo::Source::NotRequired, + LayerTypeInfo::Pass3D::NotRequired, + LayerTypeInfo::Layout::NotRequired, + LayerTypeInfo::FadingTiles::NotRequired, + LayerTypeInfo::CrossTileIndex::NotRequired, + LayerTypeInfo::TileKind::NotRequired}; + return &typeInfo; + } +}; + +class PolylineLayerProperties : public style::LayerProperties { +public: + PolylineLayerProperties() + : LayerProperties(makeMutable()) {} + expression::Dependency getDependencies() const noexcept override { return expression::Dependency::None; } +}; + +class PolylineLayerTweaker : public LayerTweaker { +public: + PolylineLayerTweaker(const shaders::LineEvaluatedPropsUBO& properties) + : linePropertiesUBO(properties), + LayerTweaker("debug-polyline", makeMutable()) {} + + void execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) override { + auto& layerUniforms = layerGroup.mutableUniformBuffers(); + layerUniforms.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, + }; + layerUniforms.createOrUpdate(idLineExpressionUBO, &exprUBO, parameters.context); + } + +private: + shaders::LineEvaluatedPropsUBO linePropertiesUBO; +}; +#endif + void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGroups, PaintParameters& parameters) const { if (!(parameters.debugOptions & @@ -108,7 +167,7 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr const auto createPolylineBuilder = [&](gfx::ShaderPtr shader) -> std::unique_ptr { std::unique_ptr builder = context.createDrawableBuilder("debug-polyline-builder"); builder->setShader(std::static_pointer_cast(shader)); - builder->setRenderPass(renderPass); + builder->setRenderPass(mbgl::RenderPass::Translucent); builder->setEnableDepth(false); builder->setColorMode(gfx::ColorMode::alphaBlended()); builder->setCullFaceMode(gfx::CullFaceMode::disabled()); @@ -193,6 +252,16 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr }; #if MLN_ENABLE_POLYLINE_DRAWABLES + const shaders::LineEvaluatedPropsUBO linePropertiesUBO = {/*color*/ Color::red(), + /*blur*/ 0.f, + /*opacity*/ 1.f, + /*gapwidth*/ 0.f, + /*offset*/ 0.f, + /*width*/ 4.f, + /*floorwidth*/ 0, + LineExpressionMask::None, + 0}; + // function to add polylines drawable const auto addPolylineDrawable = [&](TileLayerGroup* tileLayerGroup, const RenderTile& tile) { class PolylineDrawableTweaker : public gfx::DrawableTweaker { @@ -232,6 +301,8 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr auto& drawableUniforms = drawable.mutableUniformBuffers(); drawableUniforms.createOrUpdate(idLineDrawableUBO, &drawableUBO, parameters.context); drawableUniforms.createOrUpdate(idLineInterpolationUBO, &lineInterpolationUBO, 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 @@ -247,6 +318,7 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr /* floorWidth = */ nullptr, }; drawableUniforms.createOrUpdate(idLineExpressionUBO, &exprUBO, parameters.context); +#endif }; private: @@ -263,16 +335,15 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr } polylineBuilder->addPolyline(coords, options); - // create line tweaker - const shaders::LineEvaluatedPropsUBO linePropertiesUBO = {/*color*/ Color::red(), - /*blur*/ 0.f, - /*opacity*/ 1.f, - /*gapwidth*/ 0.f, - /*offset*/ 0.f, - /*width*/ 4.f, - /*floorwidth*/ 0, - LineExpressionMask::None, - 0}; +#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 diff --git a/src/mbgl/renderer/sources/render_tile_source.hpp b/src/mbgl/renderer/sources/render_tile_source.hpp index 1378ade4a1b..d1cbf8fc785 100644 --- a/src/mbgl/renderer/sources/render_tile_source.hpp +++ b/src/mbgl/renderer/sources/render_tile_source.hpp @@ -91,6 +91,8 @@ class RenderTileSetSource : public RenderTileSource { std::optional cachedTileset; }; +class PolylineLayerTweaker; + class TileSourceRenderItem : public RenderItem { public: TileSourceRenderItem(Immutable> renderTiles_, std::string name_) @@ -109,6 +111,8 @@ class TileSourceRenderItem : public RenderItem { Immutable> renderTiles; std::string name; + + mutable std::shared_ptr layerTweaker; }; } // namespace mbgl From 429a8c4279a98cc1bf949c0a2781c93cf1560214 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 14:15:58 +0000 Subject: [PATCH 08/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/mbgl/renderer/renderer_impl.cpp | 5 ++--- src/mbgl/renderer/sources/render_tile_source.cpp | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp index 55c45515744..918ca612ff6 100644 --- a/src/mbgl/renderer/renderer_impl.cpp +++ b/src/mbgl/renderer/renderer_impl.cpp @@ -452,9 +452,8 @@ 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); - }); + orchestrator.visitDebugLayerGroups( + [&](LayerGroupBase& layerGroup) { layerGroup.render(orchestrator, parameters); }); } }; #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 66209a05a00..598f7d7a4e3 100644 --- a/src/mbgl/renderer/sources/render_tile_source.cpp +++ b/src/mbgl/renderer/sources/render_tile_source.cpp @@ -342,7 +342,7 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr tileLayerGroup->addLayerTweaker(layerTweaker); } #endif - + // create line tweaker; auto tweaker = std::make_shared(linePropertiesUBO); From b7a39216072c5034627b7f704289283207af498c Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Tue, 8 Oct 2024 19:55:52 +0300 Subject: [PATCH 09/11] Fix compile error --- src/mbgl/renderer/sources/render_tile_source.cpp | 4 ++-- src/mbgl/shaders/mtl/fill_extrusion.cpp | 13 +++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/mbgl/renderer/sources/render_tile_source.cpp b/src/mbgl/renderer/sources/render_tile_source.cpp index 598f7d7a4e3..5fa4e8d3e50 100644 --- a/src/mbgl/renderer/sources/render_tile_source.cpp +++ b/src/mbgl/renderer/sources/render_tile_source.cpp @@ -87,8 +87,8 @@ class PolylineLayerProperties : public style::LayerProperties { class PolylineLayerTweaker : public LayerTweaker { public: PolylineLayerTweaker(const shaders::LineEvaluatedPropsUBO& properties) - : linePropertiesUBO(properties), - LayerTweaker("debug-polyline", makeMutable()) {} + : LayerTweaker("debug-polyline", makeMutable()), + linePropertiesUBO(properties) {} void execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) override { auto& layerUniforms = layerGroup.mutableUniformBuffers(); diff --git a/src/mbgl/shaders/mtl/fill_extrusion.cpp b/src/mbgl/shaders/mtl/fill_extrusion.cpp index 0ddcb6de738..4a329a7cd5e 100644 --- a/src/mbgl/shaders/mtl/fill_extrusion.cpp +++ b/src/mbgl/shaders/mtl/fill_extrusion.cpp @@ -11,14 +11,11 @@ const std::array ShaderSource ShaderSource::attributes = { - AttributeInfo{fillExtrusionDrawableUBOCount + 0, gfx::AttributeDataType::Short2, idFillExtrusionPosVertexAttribute}, - AttributeInfo{ - fillExtrusionDrawableUBOCount + 1, gfx::AttributeDataType::Short4, idFillExtrusionNormalEdVertexAttribute}, - AttributeInfo{ - fillExtrusionDrawableUBOCount + 2, gfx::AttributeDataType::Float4, idFillExtrusionColorVertexAttribute}, - AttributeInfo{fillExtrusionDrawableUBOCount + 3, gfx::AttributeDataType::Float, idFillExtrusionBaseVertexAttribute}, - AttributeInfo{ - fillExtrusionDrawableUBOCount + 4, gfx::AttributeDataType::Float, idFillExtrusionHeightVertexAttribute}, + AttributeInfo{fillExtrusionUBOCount + 0, gfx::AttributeDataType::Short2, idFillExtrusionPosVertexAttribute}, + AttributeInfo{fillExtrusionUBOCount + 1, gfx::AttributeDataType::Short4, idFillExtrusionNormalEdVertexAttribute}, + AttributeInfo{fillExtrusionUBOCount + 2, gfx::AttributeDataType::Float4, idFillExtrusionColorVertexAttribute}, + AttributeInfo{fillExtrusionUBOCount + 3, gfx::AttributeDataType::Float, idFillExtrusionBaseVertexAttribute}, + AttributeInfo{fillExtrusionUBOCount + 4, gfx::AttributeDataType::Float, idFillExtrusionHeightVertexAttribute}, }; const std::array ShaderSource::textures = {}; From 43c464e14d5aae6d9b0d2c3c616bd8e524c0a0f7 Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Wed, 9 Oct 2024 00:35:07 +0300 Subject: [PATCH 10/11] Fix mtl shaders --- include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp | 2 +- src/mbgl/shaders/mtl/background.cpp | 2 +- src/mbgl/shaders/mtl/background_pattern.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp b/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp index 162d313c32b..1f3de407bec 100644 --- a/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp +++ b/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp @@ -62,7 +62,7 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const FillExtrusionDrawableUBO& drawable [[buffer(1)]], device const FillExtrusionTilePropsUBO& tileProps [[buffer(2)]], device const FillExtrusionInterpolateUBO& interp [[buffer(3)]], - device const FillExtrusionPropsUBO& props [[buffer(4)]]), { + device const FillExtrusionPropsUBO& props [[buffer(4)]]) { #if defined(HAS_UNIFORM_u_base) const auto base = props.light_position_base.w; diff --git a/src/mbgl/shaders/mtl/background.cpp b/src/mbgl/shaders/mtl/background.cpp index fb968c27d37..e2556339c4f 100644 --- a/src/mbgl/shaders/mtl/background.cpp +++ b/src/mbgl/shaders/mtl/background.cpp @@ -10,7 +10,7 @@ const std::array ShaderSource ShaderSource::attributes = { - AttributeInfo{backgroundDrawableUBOCount + 0, gfx::AttributeDataType::Float3, idBackgroundPosVertexAttribute}, + AttributeInfo{backgroundUBOCount + 0, gfx::AttributeDataType::Float3, idBackgroundPosVertexAttribute}, }; const std::array ShaderSource::textures = {}; diff --git a/src/mbgl/shaders/mtl/background_pattern.cpp b/src/mbgl/shaders/mtl/background_pattern.cpp index af7913312f4..3eb91e51300 100644 --- a/src/mbgl/shaders/mtl/background_pattern.cpp +++ b/src/mbgl/shaders/mtl/background_pattern.cpp @@ -13,7 +13,7 @@ const std::array }; const std::array ShaderSource::attributes = { - AttributeInfo{backgroundDrawableUBOCount + 0, gfx::AttributeDataType::Float3, idBackgroundPosVertexAttribute}, + AttributeInfo{backgroundUBOCount + 0, gfx::AttributeDataType::Float3, idBackgroundPosVertexAttribute}, }; const std::array ShaderSource::textures = { TextureInfo{0, idBackgroundImageTexture}}; From c2b1c0bfaaf7383f49a38ade839bfc9f8555dcf0 Mon Sep 17 00:00:00 2001 From: Adrian Cojocaru Date: Wed, 9 Oct 2024 12:09:29 +0300 Subject: [PATCH 11/11] Move debug drawables to translucent render pass --- src/mbgl/renderer/sources/render_tile_source.cpp | 4 ++-- src/mbgl/vulkan/texture2d.cpp | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mbgl/renderer/sources/render_tile_source.cpp b/src/mbgl/renderer/sources/render_tile_source.cpp index 5fa4e8d3e50..52d2bc770df 100644 --- a/src/mbgl/renderer/sources/render_tile_source.cpp +++ b/src/mbgl/renderer/sources/render_tile_source.cpp @@ -123,7 +123,7 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr } auto& context = parameters.context; - const auto renderPass = RenderPass::None; + const auto renderPass = RenderPass::Translucent; auto& shaders = *parameters.staticData.shaders; // initialize debug builder @@ -167,7 +167,7 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr const auto createPolylineBuilder = [&](gfx::ShaderPtr shader) -> std::unique_ptr { std::unique_ptr builder = context.createDrawableBuilder("debug-polyline-builder"); builder->setShader(std::static_pointer_cast(shader)); - builder->setRenderPass(mbgl::RenderPass::Translucent); + builder->setRenderPass(renderPass); builder->setEnableDepth(false); builder->setColorMode(gfx::ColorMode::alphaBlended()); builder->setCullFaceMode(gfx::CullFaceMode::disabled()); diff --git a/src/mbgl/vulkan/texture2d.cpp b/src/mbgl/vulkan/texture2d.cpp index 86bcd8137b2..bd554e6f500 100644 --- a/src/mbgl/vulkan/texture2d.cpp +++ b/src/mbgl/vulkan/texture2d.cpp @@ -6,6 +6,8 @@ #include #include +#include + namespace mbgl { namespace vulkan {