From 612b256821d379d86e47bd90fc29ccf922b57173 Mon Sep 17 00:00:00 2001 From: Alex Cristici Date: Thu, 15 Feb 2024 21:28:20 +0200 Subject: [PATCH] Texture by index instead of map (#2107) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- CMakeLists.txt | 2 +- bazel/core.bzl | 2 +- include/mbgl/gfx/drawable.hpp | 22 ++--- include/mbgl/gfx/drawable_atlases_tweaker.hpp | 14 +-- include/mbgl/gfx/drawable_builder.hpp | 12 +-- include/mbgl/gfx/uniform_block.hpp | 2 +- include/mbgl/gfx/uniform_buffer.hpp | 3 +- include/mbgl/mtl/drawable.hpp | 1 + include/mbgl/shaders/gl/shader_group_gl.hpp | 2 +- include/mbgl/shaders/gl/shader_info.hpp | 81 +++++++++++------ include/mbgl/shaders/gl/shader_program_gl.hpp | 10 +-- include/mbgl/shaders/mtl/shader_program.hpp | 9 +- .../{ubo_max_count.hpp => shader_defines.hpp} | 68 ++++++++++++++ include/mbgl/shaders/shader_program_base.hpp | 2 +- src/mbgl/gfx/drawable.cpp | 17 ++-- src/mbgl/gfx/drawable_atlases_tweaker.cpp | 19 ++-- src/mbgl/gfx/drawable_builder.cpp | 14 ++- src/mbgl/gfx/uniform_block.cpp | 3 +- src/mbgl/gfx/uniform_buffer.cpp | 3 +- src/mbgl/gl/drawable_gl.cpp | 23 ++--- src/mbgl/gl/drawable_gl_builder.cpp | 2 +- src/mbgl/mtl/drawable.cpp | 26 +++--- src/mbgl/mtl/drawable_builder.cpp | 2 +- .../layers/background_layer_tweaker.cpp | 24 ++--- .../layers/fill_extrusion_layer_tweaker.cpp | 14 +-- .../renderer/layers/fill_layer_tweaker.cpp | 11 +-- .../renderer/layers/line_layer_tweaker.cpp | 21 ++--- .../layers/render_fill_extrusion_layer.cpp | 4 +- .../renderer/layers/render_fill_layer.cpp | 3 +- .../renderer/layers/render_heatmap_layer.cpp | 21 ++--- .../layers/render_hillshade_layer.cpp | 29 ++---- .../layers/render_hillshade_layer.hpp | 1 - .../renderer/layers/render_line_layer.cpp | 48 +++++----- .../renderer/layers/render_raster_layer.cpp | 16 ++-- .../renderer/layers/render_raster_layer.hpp | 2 - .../renderer/layers/render_symbol_layer.cpp | 10 +-- .../renderer/layers/symbol_layer_tweaker.cpp | 17 ++-- .../renderer/sources/render_tile_source.cpp | 7 +- src/mbgl/shaders/gl/shader_info.cpp | 89 ++++++++++++++++++- src/mbgl/shaders/gl/shader_program_gl.cpp | 53 +++-------- src/mbgl/shaders/mtl/background_pattern.cpp | 2 +- src/mbgl/shaders/mtl/debug.cpp | 2 +- src/mbgl/shaders/mtl/fill.cpp | 4 +- .../shaders/mtl/fill_extrusion_pattern.cpp | 2 +- src/mbgl/shaders/mtl/heatmap_texture.cpp | 4 +- src/mbgl/shaders/mtl/hillshade.cpp | 2 +- src/mbgl/shaders/mtl/hillshade_prepare.cpp | 2 +- src/mbgl/shaders/mtl/line.cpp | 4 +- src/mbgl/shaders/mtl/line_gradient.cpp | 2 +- src/mbgl/shaders/mtl/raster.cpp | 4 +- src/mbgl/shaders/mtl/shader_program.cpp | 18 ++-- src/mbgl/shaders/mtl/symbol_icon.cpp | 2 +- src/mbgl/shaders/mtl/symbol_sdf.cpp | 2 +- src/mbgl/shaders/mtl/symbol_text_and_icon.cpp | 4 +- 54 files changed, 416 insertions(+), 347 deletions(-) rename include/mbgl/shaders/{ubo_max_count.hpp => shader_defines.hpp} (59%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 944ea2823..8bba574fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1143,8 +1143,8 @@ if(MLN_WITH_OPENGL) ${PROJECT_SOURCE_DIR}/include/mbgl/shaders/layer_ubo.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/shaders/line_layer_ubo.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/shaders/raster_layer_ubo.hpp + ${PROJECT_SOURCE_DIR}/include/mbgl/shaders/shader_defines.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/shaders/symbol_layer_ubo.hpp - ${PROJECT_SOURCE_DIR}/include/mbgl/shaders/ubo_max_count.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/shader_program_gl.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/gl/drawable_gl.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/gl/drawable_gl_builder.hpp diff --git a/bazel/core.bzl b/bazel/core.bzl index 9a756fa84..b09873d25 100644 --- a/bazel/core.bzl +++ b/bazel/core.bzl @@ -990,9 +990,9 @@ MLN_DRAWABLES_HEADERS = [ "include/mbgl/shaders/layer_ubo.hpp", "include/mbgl/shaders/line_layer_ubo.hpp", "include/mbgl/shaders/raster_layer_ubo.hpp", + "include/mbgl/shaders/shader_defines.hpp", "include/mbgl/shaders/shader_program_base.hpp", "include/mbgl/shaders/symbol_layer_ubo.hpp", - "include/mbgl/shaders/ubo_max_count.hpp", "include/mbgl/util/identity.hpp", "include/mbgl/util/suppress_copies.hpp", "include/mbgl/style/layers/custom_drawable_layer.hpp", diff --git a/include/mbgl/gfx/drawable.hpp b/include/mbgl/gfx/drawable.hpp index 621e16f8f..42143d84e 100644 --- a/include/mbgl/gfx/drawable.hpp +++ b/include/mbgl/gfx/drawable.hpp @@ -44,8 +44,8 @@ using VertexAttributeArrayPtr = std::shared_ptr; class Drawable { public: - /// @brief Map from sampler location to texture info - using Textures = mbgl::unordered_map; + /// @brief Array of textures to bind + using Textures = std::array; protected: Drawable(std::string name); @@ -99,26 +99,18 @@ public: /// Set line width void setLineWidth(int32_t value) { lineWidth = value; } - /// @brief Remove an attached texture from this drawable at the given sampler location - /// @param location Texture sampler location - void removeTexture(int32_t location); - - /// @brief Return the textures attached to this drawable - /// @return Texture and sampler location pairs - const Textures& getTextures() const { return textures; }; - - /// @brief Get the texture at the given sampler location. - const gfx::Texture2DPtr& getTexture(int32_t location) const; + /// @brief Get the texture at the given internal ID. + const gfx::Texture2DPtr& getTexture(size_t id) const; /// @brief Set the collection of textures bound to this drawable /// @param textures_ A Textures collection to set void setTextures(const Textures& textures_) noexcept { textures = textures_; } void setTextures(Textures&& textures_) noexcept { textures = std::move(textures_); } - /// @brief Attach the given texture to this drawable at the given sampler location. + /// @brief Attach the given texture to this drawable at the given internal ID. /// @param texture Texture2D instance - /// @param location A sampler location in the shader being used with this drawable. - void setTexture(gfx::Texture2DPtr texture, int32_t location); + /// @param id Internal ID of the texture. + void setTexture(gfx::Texture2DPtr texture, size_t id); /// Whether the drawble should be drawn bool getEnabled() const { return enabled; } diff --git a/include/mbgl/gfx/drawable_atlases_tweaker.hpp b/include/mbgl/gfx/drawable_atlases_tweaker.hpp index 63e123845..5d74eb45b 100644 --- a/include/mbgl/gfx/drawable_atlases_tweaker.hpp +++ b/include/mbgl/gfx/drawable_atlases_tweaker.hpp @@ -24,22 +24,22 @@ class Drawable; class DrawableAtlasesTweaker : public gfx::DrawableTweaker { public: DrawableAtlasesTweaker(TileAtlasTexturesPtr atlases_, - const std::optional iconNameId_, - const std::optional glyphNameId_, + const std::optional iconTextureId_, + const std::optional glyphTextureId_, bool isText_, const bool sdfIcons_, const style::AlignmentType rotationAlignment_, const bool iconScaled_, const bool textSizeIsZoomConstant_) : atlases(std::move(atlases_)), - iconNameId(iconNameId_), - glyphNameId(glyphNameId_), + iconTextureId(iconTextureId_), + glyphTextureId(glyphTextureId_), isText(isText_), sdfIcons(sdfIcons_), rotationAlignment(rotationAlignment_), iconScaled(iconScaled_), textSizeIsZoomConstant(textSizeIsZoomConstant_) { - assert(iconNameId_ != glyphNameId_); + assert(iconTextureId_ != glyphTextureId_); } ~DrawableAtlasesTweaker() override = default; @@ -51,8 +51,8 @@ protected: void setupTextures(Drawable&, const bool); TileAtlasTexturesPtr atlases; - std::optional iconNameId; - std::optional glyphNameId; + std::optional iconTextureId; + std::optional glyphTextureId; bool isText; const bool sdfIcons; diff --git a/include/mbgl/gfx/drawable_builder.hpp b/include/mbgl/gfx/drawable_builder.hpp index 6ce94f852..3ec548416 100644 --- a/include/mbgl/gfx/drawable_builder.hpp +++ b/include/mbgl/gfx/drawable_builder.hpp @@ -140,13 +140,13 @@ public: /// The attribute names for vertex/position attributes void setVertexAttrNameId(const StringIdentity id) { vertexAttrNameId = id; } - /// @brief Attach the given texture at the given sampler location. - /// @param texture Texture2D instance - /// @param location A sampler location in the shader being used. - void setTexture(const gfx::Texture2DPtr&, int32_t location); + /// @brief Get the texture at the given internal ID. + const gfx::Texture2DPtr& getTexture(size_t id) const; - /// Direct access to the current texture set - const gfx::Drawable::Textures& getTextures() const { return textures; } + /// @brief Attach the given texture at the given internal ID. + /// @param texture Texture2D instance + /// @param id Internal ID of the texture. + void setTexture(const gfx::Texture2DPtr&, size_t id); /// Add a tweaker to emitted drawable void addTweaker(DrawableTweakerPtr value) { tweakers.emplace_back(std::move(value)); } diff --git a/include/mbgl/gfx/uniform_block.hpp b/include/mbgl/gfx/uniform_block.hpp index d93d49acf..70f267a6c 100644 --- a/include/mbgl/gfx/uniform_block.hpp +++ b/include/mbgl/gfx/uniform_block.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace mbgl { namespace gfx { diff --git a/include/mbgl/gfx/uniform_buffer.hpp b/include/mbgl/gfx/uniform_buffer.hpp index 8b21ee69e..5ae273563 100644 --- a/include/mbgl/gfx/uniform_buffer.hpp +++ b/include/mbgl/gfx/uniform_buffer.hpp @@ -1,7 +1,6 @@ #pragma once -#include -#include +#include namespace mbgl { namespace gfx { diff --git a/include/mbgl/mtl/drawable.hpp b/include/mbgl/mtl/drawable.hpp index ddeaccb14..83370b782 100644 --- a/include/mbgl/mtl/drawable.hpp +++ b/include/mbgl/mtl/drawable.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include diff --git a/include/mbgl/shaders/gl/shader_group_gl.hpp b/include/mbgl/shaders/gl/shader_group_gl.hpp index 1c64db0c3..10677538e 100644 --- a/include/mbgl/shaders/gl/shader_group_gl.hpp +++ b/include/mbgl/shaders/gl/shader_group_gl.hpp @@ -55,9 +55,9 @@ public: auto& glContext = static_cast(context); shader = ShaderProgramGL::create(glContext, programParameters, - shaderName, firstAttribName, shaders::ShaderInfo::uniformBlocks, + shaders::ShaderInfo::textures, vert, frag, additionalDefines); diff --git a/include/mbgl/shaders/gl/shader_info.hpp b/include/mbgl/shaders/gl/shader_info.hpp index 980e4faa1..7baba27c9 100644 --- a/include/mbgl/shaders/gl/shader_info.hpp +++ b/include/mbgl/shaders/gl/shader_info.hpp @@ -15,132 +15,163 @@ struct UniformBlockInfo { std::size_t binding; }; +struct TextureInfo { + TextureInfo(std::string_view name, std::size_t id); + std::string_view name; + std::size_t id; +}; + template struct ShaderInfo; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; -}; - -template <> -struct ShaderInfo { - static const std::vector uniformBlocks; -}; - -template <> -struct ShaderInfo { - static const std::vector uniformBlocks; -}; - -template <> -struct ShaderInfo { - static const std::vector uniformBlocks; -}; - -template <> -struct ShaderInfo { - static const std::vector uniformBlocks; -}; - -template <> -struct ShaderInfo { - static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; +}; + +template <> +struct ShaderInfo { + static const std::vector uniformBlocks; + static const std::vector textures; +}; + +template <> +struct ShaderInfo { + static const std::vector uniformBlocks; + static const std::vector textures; +}; + +template <> +struct ShaderInfo { + static const std::vector uniformBlocks; + static const std::vector textures; +}; + +template <> +struct ShaderInfo { + static const std::vector uniformBlocks; + static const std::vector textures; +}; + +template <> +struct ShaderInfo { + static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; template <> struct ShaderInfo { static const std::vector uniformBlocks; + static const std::vector textures; }; } // namespace shaders diff --git a/include/mbgl/shaders/gl/shader_program_gl.hpp b/include/mbgl/shaders/gl/shader_program_gl.hpp index 4836a730e..f1739f2d3 100644 --- a/include/mbgl/shaders/gl/shader_program_gl.hpp +++ b/include/mbgl/shaders/gl/shader_program_gl.hpp @@ -17,13 +17,13 @@ namespace gl { class ShaderProgramGL final : public gfx::ShaderProgramBase { public: - using SamplerLocationMap = std::unordered_map; + using SamplerLocationArray = std::array, shaders::maxTextureCountPerShader>; ShaderProgramGL(UniqueProgram&& glProgram_); ShaderProgramGL(UniqueProgram&&, UniformBlockArrayGL&& uniformBlocks, VertexAttributeArrayGL&& attributes, - SamplerLocationMap&& samplerLocations); + SamplerLocationArray&& samplerLocations); ShaderProgramGL(ShaderProgramGL&& other); ~ShaderProgramGL() noexcept override = default; @@ -32,14 +32,14 @@ public: static std::shared_ptr create(Context&, const ProgramParameters& programParameters, - const std::string& name, const std::string_view firstAttribName, const std::vector& uniformBlocksInfo, + const std::vector& texturesInfo, const std::string& vertexSource, const std::string& fragmentSource, const std::string& additionalDefines = "") noexcept(false); - std::optional getSamplerLocation(const StringIdentity id) const override; + std::optional getSamplerLocation(const size_t id) const override; const gfx::UniformBlockArray& getUniformBlocks() const override { return uniformBlocks; } @@ -55,7 +55,7 @@ protected: UniformBlockArrayGL uniformBlocks; VertexAttributeArrayGL vertexAttributes; - SamplerLocationMap samplerLocations; + SamplerLocationArray samplerLocations; }; } // namespace gl diff --git a/include/mbgl/shaders/mtl/shader_program.hpp b/include/mbgl/shaders/mtl/shader_program.hpp index bea1380c9..e11d47779 100644 --- a/include/mbgl/shaders/mtl/shader_program.hpp +++ b/include/mbgl/shaders/mtl/shader_program.hpp @@ -29,10 +29,9 @@ struct UniformBlockInfo { std::size_t id; }; struct TextureInfo { - TextureInfo(std::size_t index, std::string_view name); + TextureInfo(std::size_t index, std::size_t id); std::size_t index; - std::string_view name; - StringIdentity nameID; + std::size_t id; }; } // namespace shaders namespace mtl { @@ -56,7 +55,7 @@ public: const MTLVertexDescriptorPtr&, const gfx::ColorMode& colorMode) const; - std::optional getSamplerLocation(const StringIdentity id) const override; + std::optional getSamplerLocation(const size_t id) const override; const gfx::VertexAttributeArray& getVertexAttributes() const override { return vertexAttributes; } @@ -74,7 +73,7 @@ protected: MTLFunctionPtr fragmentFunction; UniformBlockArray uniformBlocks; VertexAttributeArray vertexAttributes; - std::unordered_map textureBindings; + std::array, shaders::maxTextureCountPerShader> textureBindings; }; } // namespace mtl diff --git a/include/mbgl/shaders/ubo_max_count.hpp b/include/mbgl/shaders/shader_defines.hpp similarity index 59% rename from include/mbgl/shaders/ubo_max_count.hpp rename to include/mbgl/shaders/shader_defines.hpp index 32f262097..9ee9fc0a9 100644 --- a/include/mbgl/shaders/ubo_max_count.hpp +++ b/include/mbgl/shaders/shader_defines.hpp @@ -40,5 +40,73 @@ static constexpr auto maxUBOCountPerShader = std::max({static_cast(backg static_cast(rasterUBOCount), static_cast(symbolUBOCount)}); +enum { + idBackgroundImageTexture, + backgroundTextureCount +}; + +enum { + circleTextureCount +}; + +enum { + collisionTextureCount +}; + +enum { + idDebugOverlayTexture, + debugTextureCount +}; + +enum { + idFillExtrusionImageTexture, + fillExtrusionTextureCount +}; + +enum { + idFillImageTexture, + fillTextureCount +}; + +enum { + idHeatmapImageTexture, + idHeatmapColorRampTexture, + heatmapTextureCount +}; + +enum { + idHillshadeImageTexture, + hillshadeTextureCount +}; + +enum { + idLineImageTexture, + lineTextureCount +}; + +enum { + idRasterImage0Texture, + idRasterImage1Texture, + rasterTextureCount +}; + +enum { + idSymbolImageTexture, + idSymbolImageIconTexture, + symbolTextureCount +}; + +static constexpr auto maxTextureCountPerShader = std::max({static_cast(backgroundTextureCount), + static_cast(circleTextureCount), + static_cast(collisionTextureCount), + static_cast(debugTextureCount), + static_cast(fillTextureCount), + static_cast(fillExtrusionTextureCount), + static_cast(heatmapTextureCount), + static_cast(hillshadeTextureCount), + static_cast(lineTextureCount), + static_cast(rasterTextureCount), + static_cast(symbolTextureCount)}); + } // namespace shaders } // namespace mbgl diff --git a/include/mbgl/shaders/shader_program_base.hpp b/include/mbgl/shaders/shader_program_base.hpp index acdb80127..ff7ff22dd 100644 --- a/include/mbgl/shaders/shader_program_base.hpp +++ b/include/mbgl/shaders/shader_program_base.hpp @@ -33,7 +33,7 @@ public: /// @brief Gets the sampler location /// @param name uniform name - virtual std::optional getSamplerLocation(const StringIdentity) const = 0; + virtual std::optional getSamplerLocation(const size_t) const = 0; /// Get the available uniform blocks attached to this shader virtual const gfx::UniformBlockArray& getUniformBlocks() const = 0; diff --git a/src/mbgl/gfx/drawable.cpp b/src/mbgl/gfx/drawable.cpp index fd9441549..69dd98022 100644 --- a/src/mbgl/gfx/drawable.cpp +++ b/src/mbgl/gfx/drawable.cpp @@ -45,17 +45,16 @@ void Drawable::setIndexData(std::vector indexes, std::vectorsecond : noTexture; +const gfx::Texture2DPtr& Drawable::getTexture(size_t id) const { + return (id < textures.size()) ? textures[id] : noTexture; } -void Drawable::setTexture(std::shared_ptr texture, int32_t location) { - textures.insert(std::make_pair(location, gfx::Texture2DPtr{})).first->second = std::move(texture); -} - -void Drawable::removeTexture(int32_t location) { - textures.erase(location); +void Drawable::setTexture(std::shared_ptr texture, size_t id) { + assert(id < textures.size()); + if (id >= textures.size()) { + return; + } + textures[id] = std::move(texture); } } // namespace gfx diff --git a/src/mbgl/gfx/drawable_atlases_tweaker.cpp b/src/mbgl/gfx/drawable_atlases_tweaker.cpp index 120c37ed1..cba50660b 100644 --- a/src/mbgl/gfx/drawable_atlases_tweaker.cpp +++ b/src/mbgl/gfx/drawable_atlases_tweaker.cpp @@ -8,16 +8,9 @@ namespace mbgl { namespace gfx { -namespace { -std::optional getSamplerLocation(const gfx::ShaderProgramBasePtr& shader, - const std::optional& nameId) { - return nameId ? shader->getSamplerLocation(*nameId) : std::nullopt; -} -} // namespace - void DrawableAtlasesTweaker::setupTextures(gfx::Drawable& drawable, const bool linearFilterForIcons) { if (const auto& shader = drawable.getShader()) { - if (const auto samplerLocation = getSamplerLocation(shader, glyphNameId)) { + if (glyphTextureId) { if (atlases) { atlases->glyph->setSamplerConfiguration( {TextureFilterType::Linear, TextureWrapType::Clamp, TextureWrapType::Clamp}); @@ -26,12 +19,12 @@ void DrawableAtlasesTweaker::setupTextures(gfx::Drawable& drawable, const bool l TextureWrapType::Clamp, TextureWrapType::Clamp}); } - if (const auto iconSamplerLocation = getSamplerLocation(shader, iconNameId)) { - assert(*samplerLocation != *iconSamplerLocation); - drawable.setTexture(atlases ? atlases->glyph : nullptr, *samplerLocation); - drawable.setTexture(atlases ? atlases->icon : nullptr, *iconSamplerLocation); + if (iconTextureId && shader->getSamplerLocation(*iconTextureId)) { + assert(*glyphTextureId != *iconTextureId); + drawable.setTexture(atlases ? atlases->glyph : nullptr, *glyphTextureId); + drawable.setTexture(atlases ? atlases->icon : nullptr, *iconTextureId); } else { - drawable.setTexture(atlases ? (isText ? atlases->glyph : atlases->icon) : nullptr, *samplerLocation); + drawable.setTexture(atlases ? (isText ? atlases->glyph : atlases->icon) : nullptr, *glyphTextureId); } } } diff --git a/src/mbgl/gfx/drawable_builder.cpp b/src/mbgl/gfx/drawable_builder.cpp index e34dafcaf..15fcc0bcf 100644 --- a/src/mbgl/gfx/drawable_builder.cpp +++ b/src/mbgl/gfx/drawable_builder.cpp @@ -102,8 +102,18 @@ void DrawableBuilder::resetDrawPriority(DrawPriority value) { } } -void DrawableBuilder::setTexture(const std::shared_ptr& texture, int32_t location) { - textures.insert(std::make_pair(location, gfx::Texture2DPtr{})).first->second = std::move(texture); +static const gfx::Texture2DPtr noTexture; + +const gfx::Texture2DPtr& DrawableBuilder::getTexture(size_t id) const { + return (id < textures.size()) ? textures[id] : noTexture; +} + +void DrawableBuilder::setTexture(const std::shared_ptr& texture, size_t id) { + assert(id < textures.size()); + if (id >= textures.size()) { + return; + } + textures[id] = std::move(texture); } void DrawableBuilder::addTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2) { diff --git a/src/mbgl/gfx/uniform_block.cpp b/src/mbgl/gfx/uniform_block.cpp index c80230711..472d2dbfa 100644 --- a/src/mbgl/gfx/uniform_block.cpp +++ b/src/mbgl/gfx/uniform_block.cpp @@ -21,8 +21,7 @@ UniformBlockArray& UniformBlockArray::operator=(const UniformBlockArray& other) } const std::unique_ptr& UniformBlockArray::get(const size_t id) const { - const auto& result = (id < uniformBlockVector.size()) ? uniformBlockVector[id] : nullref; - return (result != nullptr) ? result : nullref; + return (id < uniformBlockVector.size()) ? uniformBlockVector[id] : nullref; } const std::unique_ptr& UniformBlockArray::set(const size_t id, const size_t index, std::size_t size) { diff --git a/src/mbgl/gfx/uniform_buffer.cpp b/src/mbgl/gfx/uniform_buffer.cpp index 8370eee4c..5f357b999 100644 --- a/src/mbgl/gfx/uniform_buffer.cpp +++ b/src/mbgl/gfx/uniform_buffer.cpp @@ -23,8 +23,7 @@ UniformBufferArray& UniformBufferArray::operator=(const UniformBufferArray& othe } const std::shared_ptr& UniformBufferArray::get(const size_t id) const { - const auto& result = (id < uniformBufferVector.size()) ? uniformBufferVector[id] : nullref; - return (result != nullptr) ? result : nullref; + return (id < uniformBufferVector.size()) ? uniformBufferVector[id] : nullref; } const std::shared_ptr& UniformBufferArray::set(const size_t id, diff --git a/src/mbgl/gl/drawable_gl.cpp b/src/mbgl/gl/drawable_gl.cpp index 33499312c..5b3ce61d4 100644 --- a/src/mbgl/gl/drawable_gl.cpp +++ b/src/mbgl/gl/drawable_gl.cpp @@ -223,7 +223,7 @@ void DrawableGL::upload(gfx::UploadPass& uploadPass) { } const bool texturesNeedUpload = std::any_of( - textures.begin(), textures.end(), [](const auto& pair) { return pair.second && pair.second->needsUpload(); }); + textures.begin(), textures.end(), [](const auto& texture) { return texture && texture->needsUpload(); }); if (texturesNeedUpload) { uploadTextures(); @@ -245,27 +245,28 @@ gfx::StencilMode DrawableGL::makeStencilMode(PaintParameters& parameters) const } void DrawableGL::uploadTextures() const { - for (const auto& pair : textures) { - if (const auto& tex = pair.second) { - std::static_pointer_cast(tex)->upload(); + for (const auto& texture : textures) { + if (texture) { + texture->upload(); } } } void DrawableGL::bindTextures() const { int32_t unit = 0; - for (const auto& pair : textures) { - if (const auto& tex = pair.second) { - const auto& location = pair.first; - std::static_pointer_cast(tex)->bind(location, unit++); + for (size_t id = 0; id < textures.size(); id++) { + if (const auto& texture = textures[id]) { + if (const auto& location = shader->getSamplerLocation(id)) { + static_cast(*texture).bind(static_cast(*location), unit++); + } } } } void DrawableGL::unbindTextures() const { - for (const auto& pair : textures) { - if (const auto& tex = pair.second) { - std::static_pointer_cast(tex)->unbind(); + for (const auto& texture : textures) { + if (texture) { + static_cast(*texture).unbind(); } } } diff --git a/src/mbgl/gl/drawable_gl_builder.cpp b/src/mbgl/gl/drawable_gl_builder.cpp index f1269e9e3..1b43506c9 100644 --- a/src/mbgl/gl/drawable_gl_builder.cpp +++ b/src/mbgl/gl/drawable_gl_builder.cpp @@ -43,7 +43,7 @@ void DrawableGLBuilder::init() { drawableGL.setIndexData(std::move(impl->sharedIndexes), std::move(impl->segments)); impl->clear(); - textures.clear(); + textures.fill(nullptr); } } // namespace gl diff --git a/src/mbgl/mtl/drawable.cpp b/src/mbgl/mtl/drawable.cpp index 865a22b89..e3380b472 100644 --- a/src/mbgl/mtl/drawable.cpp +++ b/src/mbgl/mtl/drawable.cpp @@ -386,27 +386,29 @@ void Drawable::bindUniformBuffers(RenderPass& renderPass) const noexcept { } void Drawable::bindTextures(RenderPass& renderPass) const noexcept { - for (const auto& pair : textures) { - if (const auto& tex = pair.second) { - const auto& location = pair.first; - static_cast(*tex).bind(renderPass, location); + for (size_t id = 0; id < textures.size(); id++) { + if (const auto& texture = textures[id]) { + if (const auto& location = shader->getSamplerLocation(id)) { + static_cast(*texture).bind(renderPass, static_cast(*location)); + } } } } void Drawable::unbindTextures(RenderPass& renderPass) const noexcept { - for (const auto& pair : textures) { - if (const auto& tex = pair.second) { - const auto& location = pair.first; - static_cast(*tex).unbind(renderPass, location); + for (size_t id = 0; id < textures.size(); id++) { + if (const auto& texture = textures[id]) { + if (const auto& location = shader->getSamplerLocation(id)) { + static_cast(*texture).unbind(renderPass, static_cast(*location)); + } } } } void Drawable::uploadTextures(UploadPass&) const noexcept { - for (const auto& pair : textures) { - if (const auto& tex = pair.second) { - static_cast(*tex).upload(); + for (const auto& texture : textures) { + if (texture) { + texture->upload(); } } } @@ -584,7 +586,7 @@ void Drawable::upload(gfx::UploadPass& uploadPass_) { } const bool texturesNeedUpload = std::any_of( - textures.begin(), textures.end(), [](const auto& pair) { return pair.second && pair.second->needsUpload(); }); + textures.begin(), textures.end(), [](const auto& texture) { return texture && texture->needsUpload(); }); if (texturesNeedUpload) { uploadTextures(uploadPass); diff --git a/src/mbgl/mtl/drawable_builder.cpp b/src/mbgl/mtl/drawable_builder.cpp index 00d52f55b..632c6063d 100644 --- a/src/mbgl/mtl/drawable_builder.cpp +++ b/src/mbgl/mtl/drawable_builder.cpp @@ -42,7 +42,7 @@ void DrawableBuilder::init() { drawable.setIndexData(std::move(impl->sharedIndexes), std::move(impl->segments)); impl->clear(); - textures.clear(); + textures.fill(nullptr); } } // namespace mtl diff --git a/src/mbgl/renderer/layers/background_layer_tweaker.cpp b/src/mbgl/renderer/layers/background_layer_tweaker.cpp index 1fb49074f..a7eb61f93 100644 --- a/src/mbgl/renderer/layers/background_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/background_layer_tweaker.cpp @@ -9,7 +9,6 @@ #include #include #include -#include namespace mbgl { @@ -20,8 +19,6 @@ using namespace shaders; constexpr auto BackgroundPatternShaderName = "BackgroundPatternShader"; #endif -static const StringIdentity idTexUniformName = stringIndexer().get("u_image"); - void BackgroundLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) { const auto& state = parameters.state; auto& context = parameters.context; @@ -53,7 +50,6 @@ void BackgroundLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintPara // properties are re-evaluated every time propertiesUpdated = false; - std::optional samplerLocation{}; visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) { assert(drawable.getTileID()); if (!drawable.getTileID() || !checkTweakDrawable(drawable)) { @@ -61,9 +57,8 @@ void BackgroundLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintPara } // We assume that drawables don't change between pattern and non-pattern. - const auto& shader = drawable.getShader(); - assert(hasPattern == - (shader == context.getGenericShader(parameters.shaders, std::string(BackgroundPatternShaderName)))); + assert(hasPattern == (drawable.getShader() == + context.getGenericShader(parameters.shaders, std::string(BackgroundPatternShaderName)))); const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped(); const auto matrix = getTileMatrix( @@ -75,17 +70,10 @@ void BackgroundLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintPara uniforms.createOrUpdate(idBackgroundDrawableUBO, &drawableUBO, context); if (hasPattern) { - if (!samplerLocation.has_value()) { - samplerLocation = shader->getSamplerLocation(idTexUniformName); - if (const auto& tex = parameters.patternAtlas.texture()) { - tex->setSamplerConfiguration( - {gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp}); - } - } - if (samplerLocation.has_value()) { - if (const auto& tex = parameters.patternAtlas.texture()) { - drawable.setTexture(tex, samplerLocation.value()); - } + if (const auto& tex = parameters.patternAtlas.texture()) { + tex->setSamplerConfiguration( + {gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp}); + drawable.setTexture(tex, idBackgroundImageTexture); } // from BackgroundPatternProgram::layoutUniformValues diff --git a/src/mbgl/renderer/layers/fill_extrusion_layer_tweaker.cpp b/src/mbgl/renderer/layers/fill_extrusion_layer_tweaker.cpp index b65cb9072..f93da13d0 100644 --- a/src/mbgl/renderer/layers/fill_extrusion_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/fill_extrusion_layer_tweaker.cpp @@ -14,7 +14,6 @@ #include #include #include -#include #if MLN_RENDER_BACKEND_METAL #include @@ -26,11 +25,6 @@ namespace mbgl { using namespace shaders; using namespace style; -namespace { -const StringIdentity idTexImageName = stringIndexer().get("u_image"); - -} // namespace - void FillExtrusionLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) { auto& context = parameters.context; const auto& props = static_cast(*evaluatedProperties); @@ -95,12 +89,8 @@ void FillExtrusionLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintP const auto heightFactor = static_cast(-numTiles / util::tileSize_D / 8.0); Size textureSize = {0, 0}; - if (const auto shader = drawable.getShader()) { - if (const auto index = shader->getSamplerLocation(idTexImageName)) { - if (const auto& tex = drawable.getTexture(*index)) { - textureSize = tex->getSize(); - } - } + if (const auto& tex = drawable.getTexture(idFillExtrusionImageTexture)) { + textureSize = tex->getSize(); } const FillExtrusionDrawableUBO drawableUBO = { diff --git a/src/mbgl/renderer/layers/fill_layer_tweaker.cpp b/src/mbgl/renderer/layers/fill_layer_tweaker.cpp index d8d614add..bfb7020b2 100644 --- a/src/mbgl/renderer/layers/fill_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/fill_layer_tweaker.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #if MLN_RENDER_BACKEND_METAL #include @@ -24,8 +23,6 @@ namespace mbgl { using namespace style; - -static const StringIdentity idTexImageName = stringIndexer().get("u_image"); using namespace shaders; void FillLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) { @@ -135,12 +132,8 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters const int32_t pixelY = tileSizeAtNearestZoom * tileID.canonical.y; Size textureSize = {0, 0}; - if (const auto shader = drawable.getShader()) { - if (const auto index = shader->getSamplerLocation(idTexImageName)) { - if (const auto& tex = drawable.getTexture(*index)) { - textureSize = tex->getSize(); - } - } + if (const auto& tex = drawable.getTexture(idFillImageTexture)) { + textureSize = tex->getSize(); } auto& uniforms = drawable.mutableUniformBuffers(); diff --git a/src/mbgl/renderer/layers/line_layer_tweaker.cpp b/src/mbgl/renderer/layers/line_layer_tweaker.cpp index 8f2fc7d4b..0ebde50af 100644 --- a/src/mbgl/renderer/layers/line_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/line_layer_tweaker.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -25,8 +24,6 @@ namespace mbgl { using namespace style; using namespace shaders; -static const StringIdentity idTexImageName = stringIndexer().get("u_image"); - void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) { auto& context = parameters.context; const auto zoom = parameters.state.getZoom(); @@ -164,10 +161,8 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters case LineType::Pattern: { Size textureSize{0, 0}; - if (const auto index = shader->getSamplerLocation(idTexImageName)) { - if (const auto& texture = drawable.getTexture(index.value())) { - textureSize = texture->getSize(); - } + if (const auto& texture = drawable.getTexture(idLineImageTexture)) { + textureSize = texture->getSize(); } const LinePatternUBO linePatternUBO{ /*matrix =*/util::cast(matrix), @@ -198,13 +193,11 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters lineData.linePatternCap); // texture - if (const auto index = shader->getSamplerLocation(idTexImageName)) { - if (!drawable.getTexture(index.value())) { - const auto& texture = dashPatternTexture.getTexture(); - drawable.setEnabled(!!texture); - if (texture) { - drawable.setTexture(texture, index.value()); - } + if (!drawable.getTexture(idLineImageTexture)) { + const auto& texture = dashPatternTexture.getTexture(); + drawable.setEnabled(!!texture); + if (texture) { + drawable.setTexture(texture, idLineImageTexture); } } diff --git a/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp b/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp index 269ba752d..84db4916c 100644 --- a/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp +++ b/src/mbgl/renderer/layers/render_fill_extrusion_layer.cpp @@ -42,8 +42,6 @@ namespace { const StringIdentity idPosAttribName = stringIndexer().get("a_pos"); const StringIdentity idNormAttribName = stringIndexer().get("a_normal_ed"); -const StringIdentity idIconTextureName = stringIndexer().get("u_image"); - #endif // MLN_DRAWABLE_RENDERER inline const FillExtrusionLayer::Impl& impl_cast(const Immutable& impl) { @@ -460,7 +458,7 @@ void RenderFillExtrusionLayer::update(gfx::ShaderRegistry& shaders, if (const auto& atlases = tile.getAtlasTextures()) { tweaker = std::make_shared(atlases, std::nullopt, - idIconTextureName, + idFillExtrusionImageTexture, /*isText=*/false, false, style::AlignmentType::Auto, diff --git a/src/mbgl/renderer/layers/render_fill_layer.cpp b/src/mbgl/renderer/layers/render_fill_layer.cpp index 3e115f6cc..361d183ab 100644 --- a/src/mbgl/renderer/layers/render_fill_layer.cpp +++ b/src/mbgl/renderer/layers/render_fill_layer.cpp @@ -51,7 +51,6 @@ constexpr auto FillPatternShaderName = "FillPatternShader"; constexpr auto FillOutlinePatternShaderName = "FillOutlinePatternShader"; const StringIdentity idPosAttribName = stringIndexer().get("a_pos"); -const StringIdentity idIconTextureName = stringIndexer().get("u_image"); #endif // MLN_DRAWABLE_RENDERER inline const FillLayer::Impl& impl_cast(const Immutable& impl) { @@ -598,7 +597,7 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders, atlasTweaker = std::make_shared( atlases, std::nullopt, - idIconTextureName, + idFillImageTexture, /*isText*/ false, /*sdfIcons*/ true, // to force linear filter /*rotationAlignment_*/ AlignmentType::Auto, diff --git a/src/mbgl/renderer/layers/render_heatmap_layer.cpp b/src/mbgl/renderer/layers/render_heatmap_layer.cpp index 0541bd67a..9b7ecabc0 100644 --- a/src/mbgl/renderer/layers/render_heatmap_layer.cpp +++ b/src/mbgl/renderer/layers/render_heatmap_layer.cpp @@ -279,8 +279,6 @@ constexpr auto HeatmapShaderGroupName = "HeatmapShader"; constexpr auto HeatmapTextureShaderGroupName = "HeatmapTextureShader"; const StringIdentity idVertexAttribName = stringIndexer().get("a_pos"); -const StringIdentity idTexImageName = stringIndexer().get("u_image"); -const StringIdentity idTexColorRampName = stringIndexer().get("u_color_ramp"); } // namespace @@ -529,18 +527,13 @@ void RenderHeatmapLayer::update(gfx::ShaderRegistry& shaders, heatmapTextureBuilder->setSegments( gfx::Triangles(), RenderStaticData::quadTriangleIndices().vector(), segments.data(), segments.size()); - auto imageLocation = heatmapTextureShader->getSamplerLocation(idTexImageName); - if (imageLocation.has_value()) { - heatmapTextureBuilder->setTexture(renderTarget->getTexture(), imageLocation.value()); - } - auto colorRampLocation = heatmapTextureShader->getSamplerLocation(idTexColorRampName); - if (colorRampLocation.has_value()) { - std::shared_ptr texture = context.createTexture2D(); - texture->setImage(colorRamp); - texture->setSamplerConfiguration( - {gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp}); - heatmapTextureBuilder->setTexture(std::move(texture), colorRampLocation.value()); - } + heatmapTextureBuilder->setTexture(renderTarget->getTexture(), idHeatmapImageTexture); + + std::shared_ptr texture = context.createTexture2D(); + texture->setImage(colorRamp); + texture->setSamplerConfiguration( + {gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp}); + heatmapTextureBuilder->setTexture(std::move(texture), idHeatmapColorRampTexture); heatmapTextureBuilder->flush(context); diff --git a/src/mbgl/renderer/layers/render_hillshade_layer.cpp b/src/mbgl/renderer/layers/render_hillshade_layer.cpp index 2eb129880..f98e3f4ac 100644 --- a/src/mbgl/renderer/layers/render_hillshade_layer.cpp +++ b/src/mbgl/renderer/layers/render_hillshade_layer.cpp @@ -30,6 +30,7 @@ namespace mbgl { using namespace style; +using namespace shaders; namespace { @@ -296,7 +297,6 @@ static const std::string HillshadeShaderGroupName = "HillshadeShader"; static const StringIdentity idPosAttribName = stringIndexer().get("a_pos"); static const StringIdentity idTexturePosAttribName = stringIndexer().get("a_texture_pos"); -static const StringIdentity idTexImageName = stringIndexer().get("u_image"); void RenderHillshadeLayer::update(gfx::ShaderRegistry& shaders, gfx::Context& context, @@ -336,10 +336,6 @@ void RenderHillshadeLayer::update(gfx::ShaderRegistry& shaders, return; } - if (!hillshadeImageLocation) { - hillshadeImageLocation = hillshadeShader->getSamplerLocation(idTexImageName); - } - auto renderPass = RenderPass::Translucent; if (!(mbgl::underlying_type(renderPass) & evaluatedProperties->renderPasses)) { return; @@ -433,14 +429,11 @@ void RenderHillshadeLayer::update(gfx::ShaderRegistry& shaders, hillshadePrepareBuilder->setSegments( gfx::Triangles(), staticDataIndices.vector(), staticDataSegments.data(), staticDataSegments.size()); - auto imageLocation = hillshadePrepareShader->getSamplerLocation(idTexImageName); - if (imageLocation.has_value()) { - std::shared_ptr texture = context.createTexture2D(); - texture->setImage(bucket.getDEMData().getImagePtr()); - texture->setSamplerConfiguration( - {gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp}); - hillshadePrepareBuilder->setTexture(texture, imageLocation.value()); - } + std::shared_ptr texture = context.createTexture2D(); + texture->setImage(bucket.getDEMData().getImagePtr()); + texture->setSamplerConfiguration( + {gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp}); + hillshadePrepareBuilder->setTexture(texture, idHillshadeImageTexture); hillshadePrepareBuilder->flush(context); @@ -515,10 +508,7 @@ void RenderHillshadeLayer::update(gfx::ShaderRegistry& shaders, drawSegments.emplace_back(hillshadeBuilder->createSegment(gfx::Triangles(), std::move(segCopy))); } drawable.setIndexData(indices->vector(), std::move(drawSegments)); - - if (hillshadeImageLocation) { - drawable.setTexture(bucket.renderTarget->getTexture(), *hillshadeImageLocation); - } + drawable.setTexture(bucket.renderTarget->getTexture(), idHillshadeImageTexture); return true; }; if (updateTile(renderPass, tileID, std::move(updateExisting))) { @@ -533,10 +523,7 @@ void RenderHillshadeLayer::update(gfx::ShaderRegistry& shaders, hillshadeBuilder->setVertexAttributes(buildVertexAttributes()); hillshadeBuilder->setRawVertices({}, vertices->elements(), gfx::AttributeDataType::Short2); hillshadeBuilder->setSegments(gfx::Triangles(), indices->vector(), segments->data(), segments->size()); - - if (hillshadeImageLocation) { - hillshadeBuilder->setTexture(bucket.renderTarget->getTexture(), *hillshadeImageLocation); - } + hillshadeBuilder->setTexture(bucket.renderTarget->getTexture(), idHillshadeImageTexture); hillshadeBuilder->flush(context); diff --git a/src/mbgl/renderer/layers/render_hillshade_layer.hpp b/src/mbgl/renderer/layers/render_hillshade_layer.hpp index 83fa357ed..59390317e 100644 --- a/src/mbgl/renderer/layers/render_hillshade_layer.hpp +++ b/src/mbgl/renderer/layers/render_hillshade_layer.hpp @@ -75,7 +75,6 @@ private: #if MLN_DRAWABLE_RENDERER gfx::ShaderProgramBasePtr hillshadePrepareShader; gfx::ShaderProgramBasePtr hillshadeShader; - std::optional hillshadeImageLocation; std::vector activatedRenderTargets; using HillshadeVertexVector = gfx::VertexVector; diff --git a/src/mbgl/renderer/layers/render_line_layer.cpp b/src/mbgl/renderer/layers/render_line_layer.cpp index 6d21a77f2..918a4782f 100644 --- a/src/mbgl/renderer/layers/render_line_layer.cpp +++ b/src/mbgl/renderer/layers/render_line_layer.cpp @@ -345,8 +345,6 @@ float RenderLineLayer::getLineWidth(const GeometryTileFeature& feature, } #if MLN_DRAWABLE_RENDERER -static const StringIdentity idLineImageUniformName = stringIndexer().get("u_image"); - void RenderLineLayer::update(gfx::ShaderRegistry& shaders, gfx::Context& context, const TransformState& state, @@ -679,7 +677,7 @@ void RenderLineLayer::update(gfx::ShaderRegistry& shaders, iconTweaker = std::make_shared( atlases, std::nullopt, - idLineImageUniformName, + idLineImageTexture, /*isText*/ false, /*sdfIcons*/ true, // to force linear filter /*rotationAlignment_*/ AlignmentType::Auto, @@ -727,33 +725,31 @@ void RenderLineLayer::update(gfx::ShaderRegistry& shaders, addAttributes(*builder, bucket, std::move(vertexAttrs)); // texture - if (const auto samplerLocation = builder->getShader()->getSamplerLocation(idLineImageUniformName)) { - if (!colorRampTexture2D && colorRamp->valid()) { - // create texture. to be reused for all the tiles of the layer - colorRampTexture2D = context.createTexture2D(); - colorRampTexture2D->setImage(colorRamp); - colorRampTexture2D->setSamplerConfiguration( - {gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp}); - } + if (!colorRampTexture2D && colorRamp->valid()) { + // create texture. to be reused for all the tiles of the layer + colorRampTexture2D = context.createTexture2D(); + colorRampTexture2D->setImage(colorRamp); + colorRampTexture2D->setSamplerConfiguration( + {gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp}); + } - if (colorRampTexture2D) { - builder->setTexture(colorRampTexture2D, samplerLocation.value()); + if (colorRampTexture2D) { + builder->setTexture(colorRampTexture2D, idLineImageTexture); - // segments - setSegments(builder, bucket); + // segments + setSegments(builder, bucket); - // finish - builder->flush(context); - for (auto& drawable : builder->clearDrawables()) { - drawable->setType(mbgl::underlying_type(LineLayerTweaker::LineType::Gradient)); - drawable->setTileID(tileID); - drawable->setLayerTweaker(layerTweaker); - drawable->mutableUniformBuffers().createOrUpdate( - idLineGradientInterpolationUBO, &getLineGradientInterpolationUBO(), context); + // finish + builder->flush(context); + for (auto& drawable : builder->clearDrawables()) { + drawable->setType(mbgl::underlying_type(LineLayerTweaker::LineType::Gradient)); + drawable->setTileID(tileID); + drawable->setLayerTweaker(layerTweaker); + drawable->mutableUniformBuffers().createOrUpdate( + idLineGradientInterpolationUBO, &getLineGradientInterpolationUBO(), context); - tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable)); - ++stats.drawablesAdded; - } + tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable)); + ++stats.drawablesAdded; } } } else { diff --git a/src/mbgl/renderer/layers/render_raster_layer.cpp b/src/mbgl/renderer/layers/render_raster_layer.cpp index 58d043c70..e495030cb 100644 --- a/src/mbgl/renderer/layers/render_raster_layer.cpp +++ b/src/mbgl/renderer/layers/render_raster_layer.cpp @@ -26,6 +26,7 @@ namespace mbgl { using namespace style; +using namespace shaders; namespace { @@ -273,8 +274,6 @@ void RenderRasterLayer::update(gfx::ShaderRegistry& shaders, if (!rasterShader) { return; } - rasterSampler0 = rasterShader->getSamplerLocation(idTexImage0Name); - rasterSampler1 = rasterShader->getSamplerLocation(idTexImage1Name); } if (!layerTweaker) { @@ -311,7 +310,7 @@ void RenderRasterLayer::update(gfx::ShaderRegistry& shaders, }; const auto setTextures = [&](gfx::UniqueDrawableBuilder& builder, RasterBucket& bucket) { - if (bucket.image && (rasterSampler0 || rasterSampler1)) { + if (bucket.image) { if (!bucket.texture2d) { if (auto tex = context.createTexture2D()) { tex->setImage(bucket.image); @@ -327,12 +326,8 @@ void RenderRasterLayer::update(gfx::ShaderRegistry& shaders, bucket.texture2d->setSamplerConfiguration( {filter, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp}); - if (rasterSampler0) { - builder->setTexture(bucket.texture2d, *rasterSampler0); - } - if (rasterSampler1) { - builder->setTexture(bucket.texture2d, *rasterSampler1); - } + builder->setTexture(bucket.texture2d, idRasterImage0Texture); + builder->setTexture(bucket.texture2d, idRasterImage1Texture); } } }; @@ -498,7 +493,8 @@ void RenderRasterLayer::update(gfx::ShaderRegistry& shaders, builder = createBuilder(); } - if (bucket.image && builder->getTextures().size() == 0) { + if (bucket.image && !builder->getTexture(idRasterImage0Texture) && + !builder->getTexture(idRasterImage1Texture)) { setTextures(builder, bucket); }; diff --git a/src/mbgl/renderer/layers/render_raster_layer.hpp b/src/mbgl/renderer/layers/render_raster_layer.hpp index 361392d82..105407f85 100644 --- a/src/mbgl/renderer/layers/render_raster_layer.hpp +++ b/src/mbgl/renderer/layers/render_raster_layer.hpp @@ -66,8 +66,6 @@ private: #if MLN_DRAWABLE_RENDERER gfx::ShaderProgramBasePtr rasterShader; - std::optional rasterSampler0; - std::optional rasterSampler1; LayerGroupPtr imageLayerGroup; using RasterVertexVector = gfx::VertexVector; diff --git a/src/mbgl/renderer/layers/render_symbol_layer.cpp b/src/mbgl/renderer/layers/render_symbol_layer.cpp index f35b745d0..ac3535b3d 100644 --- a/src/mbgl/renderer/layers/render_symbol_layer.cpp +++ b/src/mbgl/renderer/layers/render_symbol_layer.cpp @@ -766,8 +766,6 @@ const auto idPosOffsetAttribName = stringIndexer().get(posOffsetAttribName); const auto idPixOffsetAttribName = stringIndexer().get("a_pixeloffset"); const auto idProjPosAttribName = stringIndexer().get("a_projected_pos"); const auto idFadeOpacityAttribName = stringIndexer().get("a_fade_opacity"); -const auto idTexUniformName = stringIndexer().get("u_texture"); -const auto idTexIconUniformName = stringIndexer().get("u_texture_icon"); void updateTileAttributes(const SymbolBucket::Buffer& buffer, const bool isText, @@ -1293,8 +1291,8 @@ void RenderSymbolLayer::update(gfx::ShaderRegistry& shaders, const bool textSizeIsZoomConstant = bucket.textSizeBinder->evaluateForZoom(static_cast(state.getZoom())).isZoomConstant; tileInfo.textTweaker = std::make_shared(atlases, - idTexIconUniformName, - idTexUniformName, + idSymbolImageIconTexture, + idSymbolImageTexture, isText, false, values.rotationAlignment, @@ -1304,8 +1302,8 @@ void RenderSymbolLayer::update(gfx::ShaderRegistry& shaders, if (!isText && !tileInfo.iconTweaker) { const bool iconScaled = layout.get().constantOr(1.0) != 1.0 || bucket.iconsNeedLinear; tileInfo.iconTweaker = std::make_shared(atlases, - idTexIconUniformName, - idTexUniformName, + idSymbolImageIconTexture, + idSymbolImageTexture, isText, sdfIcons, values.rotationAlignment, diff --git a/src/mbgl/renderer/layers/symbol_layer_tweaker.cpp b/src/mbgl/renderer/layers/symbol_layer_tweaker.cpp index 120e9c20b..fd0d1b25d 100644 --- a/src/mbgl/renderer/layers/symbol_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/symbol_layer_tweaker.cpp @@ -29,13 +29,9 @@ using namespace shaders; namespace { -Size getTexSize(const gfx::Drawable& drawable, const StringIdentity nameId) { - if (const auto& shader = drawable.getShader()) { - if (const auto index = shader->getSamplerLocation(nameId)) { - if (const auto& tex = drawable.getTexture(*index)) { - return tex->getSize(); - } - } +Size getTexSize(const gfx::Drawable& drawable, const size_t texId) { + if (const auto& tex = drawable.getTexture(texId)) { + return tex->getSize(); } return {0, 0}; } @@ -44,9 +40,6 @@ std::array toArray(const Size& s) { return util::cast(std::array{s.width, s.height}); } -const StringIdentity idTexUniformName = stringIndexer().get("u_texture"); -const StringIdentity idTexIconUniformName = stringIndexer().get("u_texture_icon"); - SymbolDrawablePaintUBO buildPaintUBO(bool isText, const SymbolPaintProperties::PossiblyEvaluated& evaluated) { return { /*.fill_color=*/isText ? constOrDefault(evaluated) : constOrDefault(evaluated), @@ -152,8 +145,8 @@ void SymbolLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParamete /*.label_plane_matrix=*/util::cast(labelPlaneMatrix), /*.coord_matrix=*/util::cast(glCoordMatrix), - /*.texsize=*/toArray(getTexSize(drawable, idTexUniformName)), - /*.texsize_icon=*/toArray(getTexSize(drawable, idTexIconUniformName)), + /*.texsize=*/toArray(getTexSize(drawable, idSymbolImageTexture)), + /*.texsize_icon=*/toArray(getTexSize(drawable, idSymbolImageIconTexture)), /*.gamma_scale=*/gammaScale, /*.rotate_symbol=*/rotateInShader, diff --git a/src/mbgl/renderer/sources/render_tile_source.cpp b/src/mbgl/renderer/sources/render_tile_source.cpp index 959dbff43..a88e6741c 100644 --- a/src/mbgl/renderer/sources/render_tile_source.cpp +++ b/src/mbgl/renderer/sources/render_tile_source.cpp @@ -131,18 +131,13 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr // create texture. to be reused for all the tiles of the debug layers auto texture = context.createTexture2D(); - std::optional samplerLocation; { std::array data{{0, 0, 0, 0}}; auto emptyImage = std::make_shared(Size(1, 1), data.data(), data.size()); texture->setImage(emptyImage); texture->setSamplerConfiguration( {gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp}); - static const StringIdentity idDebugOverlayUniformName = stringIndexer().get("u_overlay"); - samplerLocation = debugShader->getSamplerLocation(idDebugOverlayUniformName); } - assert(samplerLocation.has_value()); - if (!samplerLocation.has_value()) return; // function to update existing tile drawables with UBO value. return number of updated drawables const auto updateDrawables = @@ -172,7 +167,7 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr debugBuilder->addVertices(verts, 0, verts.size()); debugBuilder->setSegments(mode, indexes, segments.data(), segments.size()); // texture - debugBuilder->setTexture(texture, samplerLocation.value()); + debugBuilder->setTexture(texture, idDebugOverlayTexture); // finish debugBuilder->flush(context); diff --git a/src/mbgl/shaders/gl/shader_info.cpp b/src/mbgl/shaders/gl/shader_info.cpp index 5d90eb2fc..637c26db1 100644 --- a/src/mbgl/shaders/gl/shader_info.cpp +++ b/src/mbgl/shaders/gl/shader_info.cpp @@ -1,6 +1,6 @@ #include -#include +#include namespace mbgl { namespace shaders { @@ -10,51 +10,76 @@ UniformBlockInfo::UniformBlockInfo(std::string_view name_, std::size_t id_) id(id_), binding(id_) {} +TextureInfo::TextureInfo(std::string_view name_, std::size_t id_) + : name(name_), + id(id_) {} + +// Background const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"BackgroundDrawableUBO", idBackgroundDrawableUBO}, UniformBlockInfo{"BackgroundLayerUBO", idBackgroundLayerUBO}, }; +const std::vector ShaderInfo::textures = {}; +// Background Pattern const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"BackgroundDrawableUBO", idBackgroundDrawableUBO}, UniformBlockInfo{"BackgroundLayerUBO", idBackgroundLayerUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_image", idBackgroundImageTexture}, +}; +// Circle const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"CircleDrawableUBO", idCircleDrawableUBO}, UniformBlockInfo{"CirclePaintParamsUBO", idCirclePaintParamsUBO}, UniformBlockInfo{"CircleEvaluatedPropsUBO", idCircleEvaluatedPropsUBO}, UniformBlockInfo{"CircleInterpolateUBO", idCircleInterpolateUBO}, }; +const std::vector ShaderInfo::textures = {}; +// Collision Box const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"CollisionBoxUBO", idCollisionUBO}, }; +const std::vector ShaderInfo::textures = {}; +// Collision Circle const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"CollisionCircleUBO", idCollisionUBO}, }; +const std::vector ShaderInfo::textures = {}; +// Debug const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"DebugUBO", idDebugUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_overlay", idDebugOverlayTexture}, +}; +// Fill const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"FillDrawableUBO", idFillDrawableUBO}, UniformBlockInfo{"FillEvaluatedPropsUBO", idFillEvaluatedPropsUBO}, UniformBlockInfo{"FillInterpolateUBO", idFillInterpolateUBO}, }; +const std::vector ShaderInfo::textures = {}; +// Fill Outline const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"FillOutlineDrawableUBO", idFillOutlineDrawableUBO}, UniformBlockInfo{"FillOutlineEvaluatedPropsUBO", idFillOutlineEvaluatedPropsUBO}, UniformBlockInfo{"FillOutlineInterpolateUBO", idFillOutlineInterpolateUBO}, }; +const std::vector ShaderInfo::textures = {}; +// Fill Pattern const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"FillPatternDrawableUBO", idFillPatternDrawableUBO}, @@ -62,7 +87,11 @@ const std::vector ShaderInfo ShaderInfo::textures = { + TextureInfo{"u_image", idFillImageTexture}, +}; +// Fill Outline Pattern const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"FillOutlinePatternDrawableUBO", idFillOutlinePatternDrawableUBO}, @@ -70,7 +99,11 @@ const std::vector UniformBlockInfo{"FillOutlinePatternEvaluatedPropsUBO", idFillOutlinePatternEvaluatedPropsUBO}, UniformBlockInfo{"FillOutlinePatternInterpolateUBO", idFillOutlinePatternInterpolateUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_image", idFillImageTexture}, +}; +// Fill Extrusion const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"FillExtrusionDrawableUBO", idFillExtrusionDrawableUBO}, @@ -78,7 +111,9 @@ const std::vector UniformBlockInfo{"FillExtrusionDrawableTilePropsUBO", idFillExtrusionDrawableTilePropsUBO}, UniformBlockInfo{"FillExtrusionInterpolateUBO", idFillExtrusionInterpolateUBO}, }; +const std::vector ShaderInfo::textures = {}; +// Fill Extrusion Pattern const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"FillExtrusionDrawableUBO", idFillExtrusionDrawableUBO}, @@ -86,28 +121,47 @@ const std::vector UniformBlockInfo{"FillExtrusionDrawableTilePropsUBO", idFillExtrusionDrawableTilePropsUBO}, UniformBlockInfo{"FillExtrusionInterpolateUBO", idFillExtrusionInterpolateUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_image", idFillExtrusionImageTexture}, +}; +// Heatmap const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"HeatmapDrawableUBO", idHeatmapDrawableUBO}, UniformBlockInfo{"HeatmapEvaluatedPropsUBO", idHeatmapEvaluatedPropsUBO}, UniformBlockInfo{"HeatmapInterpolateUBO", idHeatmapInterpolateUBO}, }; +const std::vector ShaderInfo::textures = {}; +// Heatmap Texture const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"HeatmapTextureDrawableUBO", idHeatmapTextureDrawableUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_image", idHeatmapImageTexture}, + TextureInfo{"u_color_ramp", idHeatmapColorRampTexture}, +}; +// Hillshade Prepare const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"HillshadePrepareDrawableUBO", idHillshadePrepareDrawableUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_image", idHillshadeImageTexture}, +}; +// Hillshade const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"HillshadeDrawableUBO", idHillshadeDrawableUBO}, UniformBlockInfo{"HillshadeEvaluatedPropsUBO", idHillshadeEvaluatedPropsUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_image", idHillshadeImageTexture}, +}; +// Line Gradient const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"LineDynamicUBO", idLineDynamicUBO}, @@ -115,7 +169,11 @@ const std::vector ShaderInfo ShaderInfo::textures = { + TextureInfo{"u_image", idLineImageTexture}, +}; +// Line Pattern const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"LineDynamicUBO", idLineDynamicUBO}, @@ -124,30 +182,47 @@ const std::vector ShaderInfo ShaderInfo::textures = { + TextureInfo{"u_image", idLineImageTexture}, +}; +// Line SDF const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"LineDynamicUBO", idLineDynamicUBO}, UniformBlockInfo{"LineSDFUBO", idLineSDFUBO}, UniformBlockInfo{"LineSDFPropertiesUBO", idLineSDFPropertiesUBO}, UniformBlockInfo{"LineSDFInterpolationUBO", idLineSDFInterpolationUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_image", idLineImageTexture}, +}; +// Line const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"LineDynamicUBO", idLineDynamicUBO}, UniformBlockInfo{"LineUBO", idLineUBO}, UniformBlockInfo{"LinePropertiesUBO", idLinePropertiesUBO}, UniformBlockInfo{"LineInterpolationUBO", idLineInterpolationUBO}, }; +const std::vector ShaderInfo::textures = {}; +// Line Basic const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"LineBasicUBO", idLineBasicUBO}, UniformBlockInfo{"LineBasicPropertiesUBO", idLineBasicPropertiesUBO}, }; +const std::vector ShaderInfo::textures = {}; +// Raster const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"RasterDrawableUBO", idRasterDrawableUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_image0", idRasterImage0Texture}, + TextureInfo{"u_image1", idRasterImage0Texture}, +}; +// Symbol Icon const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"SymbolDrawableUBO", idSymbolDrawableUBO}, UniformBlockInfo{"SymbolDynamicUBO", idSymbolDynamicUBO}, @@ -155,7 +230,11 @@ const std::vector ShaderInfo ShaderInfo::textures = { + TextureInfo{"u_texture", idSymbolImageTexture}, +}; +// Symbol SDF const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"SymbolDrawableUBO", idSymbolDrawableUBO}, @@ -164,7 +243,11 @@ const std::vector UniformBlockInfo{"SymbolDrawableTilePropsUBO", idSymbolDrawableTilePropsUBO}, UniformBlockInfo{"SymbolDrawableInterpolateUBO", idSymbolDrawableInterpolateUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_texture", idSymbolImageTexture}, +}; +// Symbol Text & Icon const std::vector ShaderInfo::uniformBlocks = { UniformBlockInfo{"SymbolDrawableUBO", idSymbolDrawableUBO}, @@ -173,6 +256,10 @@ const std::vector UniformBlockInfo{"SymbolDrawableTilePropsUBO", idSymbolDrawableTilePropsUBO}, UniformBlockInfo{"SymbolDrawableInterpolateUBO", idSymbolDrawableInterpolateUBO}, }; +const std::vector ShaderInfo::textures = { + TextureInfo{"u_texture", idSymbolImageTexture}, + TextureInfo{"u_texture_icon", idSymbolImageIconTexture}, +}; } // namespace shaders } // namespace mbgl diff --git a/src/mbgl/shaders/gl/shader_program_gl.cpp b/src/mbgl/shaders/gl/shader_program_gl.cpp index 0e3ce2f41..2183bf91b 100644 --- a/src/mbgl/shaders/gl/shader_program_gl.cpp +++ b/src/mbgl/shaders/gl/shader_program_gl.cpp @@ -91,7 +91,7 @@ ShaderProgramGL::ShaderProgramGL(UniqueProgram&& glProgram_) ShaderProgramGL::ShaderProgramGL(UniqueProgram&& program, UniformBlockArrayGL&& uniformBlocks_, VertexAttributeArrayGL&& attributes_, - SamplerLocationMap&& samplerLocations_) + SamplerLocationArray&& samplerLocations_) : ShaderProgramBase(), glProgram(std::move(program)), uniformBlocks(std::move(uniformBlocks_)), @@ -105,20 +105,16 @@ ShaderProgramGL::ShaderProgramGL(ShaderProgramGL&& other) vertexAttributes(std::move(other.vertexAttributes)), samplerLocations(std::move(other.samplerLocations)) {} -std::optional ShaderProgramGL::getSamplerLocation(const StringIdentity id) const { - std::optional result{}; - if (auto it = samplerLocations.find(id); it != samplerLocations.end()) { - result = it->second; - } - return result; +std::optional ShaderProgramGL::getSamplerLocation(const size_t id) const { + return (id < samplerLocations.size()) ? samplerLocations[id] : std::nullopt; } std::shared_ptr ShaderProgramGL::create( Context& context, const ProgramParameters& programParameters, - const std::string& /*name*/, const std::string_view firstAttribName, const std::vector& uniformBlocksInfo, + const std::vector& texturesInfo, const std::string& vertexSource, const std::string& fragmentSource, const std::string& additionalDefines) noexcept(false) { @@ -140,17 +136,7 @@ std::shared_ptr ShaderProgramGL::create( fragmentSource.c_str()}); auto program = context.createProgram(vertProg, fragProg, firstAttribName.data()); - // GLES3.1 - // GLint numAttribs; - // glGetProgramInterfaceiv(program, GL_PROGRAM_INPUT, GL_ACTIVE_RESOURCES, &numAttribs); - UniformBlockArrayGL uniformBlocks; - - GLint count = 0; - GLint maxLength = 0; - MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &count)); - MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &maxLength)); - for (const auto& blockInfo : uniformBlocksInfo) { GLint index = MBGL_CHECK_ERROR(glGetUniformBlockIndex(program, blockInfo.name.data())); GLint size = 0; @@ -161,34 +147,21 @@ std::shared_ptr ShaderProgramGL::create( uniformBlocks.set(blockInfo.id, binding, size); } - SamplerLocationMap samplerLocations; - GLint numActiveUniforms = 0; - MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &numActiveUniforms)); - MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLength)); - auto name = std::vector(maxLength); - for (GLint index = 0; index < numActiveUniforms; ++index) { - GLsizei actualLength = 0; - GLint size = 0; - GLenum type = GL_ZERO; - - MBGL_CHECK_ERROR(glGetActiveUniform(program, index, maxLength, &actualLength, &size, &type, name.data())); - - if (type == GL_SAMPLER_2D) { - // This uniform is a texture sampler - GLint location = MBGL_CHECK_ERROR(glGetUniformLocation(program, name.data())); - assert(location != -1); - if (location != -1) { - samplerLocations[stringIndexer().get(name.data())] = location; - } + SamplerLocationArray samplerLocations; + for (const auto& textureInfo : texturesInfo) { + GLint location = MBGL_CHECK_ERROR(glGetUniformLocation(program, textureInfo.name.data())); + assert(location != -1); + if (location != -1) { + samplerLocations[textureInfo.id] = location; } } VertexAttributeArrayGL attrs; - - count = 0; + GLint count = 0; + GLint maxLength = 0; MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &count)); MBGL_CHECK_ERROR(glGetProgramiv(program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength)); - name.resize(maxLength); + auto name = std::vector(maxLength); for (GLint index = 0; index < count; ++index) { GLsizei length = 0; // "number of characters actually written in name (excluding the null terminator)" GLint size = 0; // "size of the attribute variable, in units of the type returned in type" diff --git a/src/mbgl/shaders/mtl/background_pattern.cpp b/src/mbgl/shaders/mtl/background_pattern.cpp index 1cdb89b0d..aa6a7c704 100644 --- a/src/mbgl/shaders/mtl/background_pattern.cpp +++ b/src/mbgl/shaders/mtl/background_pattern.cpp @@ -14,7 +14,7 @@ const std::array UniformBlockInfo{2, true, true, sizeof(BackgroundPatternLayerUBO), idBackgroundLayerUBO}, }; const std::array ShaderSource::textures = { - TextureInfo{0, "u_image"}}; + TextureInfo{0, idBackgroundImageTexture}}; } // namespace shaders } // namespace mbgl diff --git a/src/mbgl/shaders/mtl/debug.cpp b/src/mbgl/shaders/mtl/debug.cpp index 8694c6a21..017c0edb1 100644 --- a/src/mbgl/shaders/mtl/debug.cpp +++ b/src/mbgl/shaders/mtl/debug.cpp @@ -10,7 +10,7 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_overlay"}, + TextureInfo{0, idDebugOverlayTexture}, }; } // namespace shaders diff --git a/src/mbgl/shaders/mtl/fill.cpp b/src/mbgl/shaders/mtl/fill.cpp index 3b124bbaa..05ad0f24a 100644 --- a/src/mbgl/shaders/mtl/fill.cpp +++ b/src/mbgl/shaders/mtl/fill.cpp @@ -40,7 +40,7 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_image"}, + TextureInfo{0, idFillImageTexture}, }; const std::array @@ -59,7 +59,7 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_image"}, + TextureInfo{0, idFillImageTexture}, }; } // namespace shaders diff --git a/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp b/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp index 7f4b301f4..3bd415ae7 100644 --- a/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp +++ b/src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp @@ -22,7 +22,7 @@ const std::array }; const std::array ShaderSource::textures = { - TextureInfo{0, "u_image"}, + TextureInfo{0, idFillExtrusionImageTexture}, }; } // namespace shaders diff --git a/src/mbgl/shaders/mtl/heatmap_texture.cpp b/src/mbgl/shaders/mtl/heatmap_texture.cpp index 40a8e99cb..61f206750 100644 --- a/src/mbgl/shaders/mtl/heatmap_texture.cpp +++ b/src/mbgl/shaders/mtl/heatmap_texture.cpp @@ -12,8 +12,8 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_image"}, - TextureInfo{1, "u_color_ramp"}, + TextureInfo{0, idHeatmapImageTexture}, + TextureInfo{1, idHeatmapColorRampTexture}, }; } // namespace shaders diff --git a/src/mbgl/shaders/mtl/hillshade.cpp b/src/mbgl/shaders/mtl/hillshade.cpp index c4d9b1c7a..3bc21b853 100644 --- a/src/mbgl/shaders/mtl/hillshade.cpp +++ b/src/mbgl/shaders/mtl/hillshade.cpp @@ -12,7 +12,7 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_image"}, + TextureInfo{0, idHillshadeImageTexture}, }; } // namespace shaders diff --git a/src/mbgl/shaders/mtl/hillshade_prepare.cpp b/src/mbgl/shaders/mtl/hillshade_prepare.cpp index 717552aa2..5aa84fa93 100644 --- a/src/mbgl/shaders/mtl/hillshade_prepare.cpp +++ b/src/mbgl/shaders/mtl/hillshade_prepare.cpp @@ -13,7 +13,7 @@ const std::array UniformBlockInfo{2, true, true, sizeof(HillshadePrepareDrawableUBO), idHillshadePrepareDrawableUBO}, }; const std::array ShaderSource::textures = { - TextureInfo{0, "u_image"}, + TextureInfo{0, idHillshadeImageTexture}, }; } // namespace shaders diff --git a/src/mbgl/shaders/mtl/line.cpp b/src/mbgl/shaders/mtl/line.cpp index 805007aee..fed8f6dd5 100644 --- a/src/mbgl/shaders/mtl/line.cpp +++ b/src/mbgl/shaders/mtl/line.cpp @@ -40,7 +40,7 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_image"}, + TextureInfo{0, idLineImageTexture}, }; const std::array ShaderSource::attributes = { @@ -61,7 +61,7 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_image"}, + TextureInfo{0, idLineImageTexture}, }; const std::array ShaderSource::attributes = { diff --git a/src/mbgl/shaders/mtl/line_gradient.cpp b/src/mbgl/shaders/mtl/line_gradient.cpp index 131a6a1a5..a3a4a1aa7 100644 --- a/src/mbgl/shaders/mtl/line_gradient.cpp +++ b/src/mbgl/shaders/mtl/line_gradient.cpp @@ -19,7 +19,7 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_image"}, + TextureInfo{0, idLineImageTexture}, }; } // namespace shaders diff --git a/src/mbgl/shaders/mtl/raster.cpp b/src/mbgl/shaders/mtl/raster.cpp index cd0a21dd2..0bb98a615 100644 --- a/src/mbgl/shaders/mtl/raster.cpp +++ b/src/mbgl/shaders/mtl/raster.cpp @@ -11,8 +11,8 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_image0"}, - TextureInfo{1, "u_image1"}, + TextureInfo{0, idRasterImage0Texture}, + TextureInfo{1, idRasterImage1Texture}, }; } // namespace shaders diff --git a/src/mbgl/shaders/mtl/shader_program.cpp b/src/mbgl/shaders/mtl/shader_program.cpp index c75ef817e..ca13c4acd 100644 --- a/src/mbgl/shaders/mtl/shader_program.cpp +++ b/src/mbgl/shaders/mtl/shader_program.cpp @@ -38,10 +38,9 @@ shaders::UniformBlockInfo::UniformBlockInfo( size(size_), id(id_) {} -shaders::TextureInfo::TextureInfo(std::size_t index_, std::string_view name_) +shaders::TextureInfo::TextureInfo(std::size_t index_, std::size_t id_) : index(index_), - name(name_), - nameID(stringIndexer().get(name_)) {} + id(id_) {} namespace mtl { namespace { @@ -188,11 +187,8 @@ MTLRenderPipelineStatePtr ShaderProgram::getRenderPipelineState(const gfx::Rende return rps; } -std::optional ShaderProgram::getSamplerLocation(const StringIdentity id) const { - if (auto it = textureBindings.find(id); it != textureBindings.end()) { - return it->second; - } - return std::nullopt; +std::optional ShaderProgram::getSamplerLocation(const size_t id) const { + return (id < textureBindings.size()) ? textureBindings[id] : std::nullopt; } void ShaderProgram::initAttribute(const shaders::AttributeInfo& info) { @@ -222,7 +218,11 @@ void ShaderProgram::initUniformBlock(const shaders::UniformBlockInfo& info) { } void ShaderProgram::initTexture(const shaders::TextureInfo& info) { - textureBindings[stringIndexer().get(info.name.data())] = info.index; + assert(info.id < textureBindings.size()); + if (info.id >= textureBindings.size()) { + return; + } + textureBindings[info.id] = info.index; } } // namespace mtl diff --git a/src/mbgl/shaders/mtl/symbol_icon.cpp b/src/mbgl/shaders/mtl/symbol_icon.cpp index cfb82bb83..556a29aeb 100644 --- a/src/mbgl/shaders/mtl/symbol_icon.cpp +++ b/src/mbgl/shaders/mtl/symbol_icon.cpp @@ -22,7 +22,7 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_texture"}, + TextureInfo{0, idSymbolImageTexture}, }; } // namespace shaders diff --git a/src/mbgl/shaders/mtl/symbol_sdf.cpp b/src/mbgl/shaders/mtl/symbol_sdf.cpp index 074d706e4..f064ad620 100644 --- a/src/mbgl/shaders/mtl/symbol_sdf.cpp +++ b/src/mbgl/shaders/mtl/symbol_sdf.cpp @@ -28,7 +28,7 @@ const std::array ShaderSource ShaderSource::textures = { - TextureInfo{0, "u_texture"}, + TextureInfo{0, idSymbolImageTexture}, }; } // 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 afcd477ef..2cc4523ea 100644 --- a/src/mbgl/shaders/mtl/symbol_text_and_icon.cpp +++ b/src/mbgl/shaders/mtl/symbol_text_and_icon.cpp @@ -27,8 +27,8 @@ const std::array UniformBlockInfo{13, true, false, sizeof(SymbolDrawableInterpolateUBO), idSymbolDrawableInterpolateUBO}, }; const std::array ShaderSource::textures = { - TextureInfo{0, "u_texture"}, - TextureInfo{1, "u_texture_icon"}, + TextureInfo{0, idSymbolImageTexture}, + TextureInfo{1, idSymbolImageIconTexture}, }; } // namespace shaders