diff --git a/include/mbgl/gfx/drawable.hpp b/include/mbgl/gfx/drawable.hpp index 2041f6405..2353c66c6 100644 --- a/include/mbgl/gfx/drawable.hpp +++ b/include/mbgl/gfx/drawable.hpp @@ -34,18 +34,8 @@ using Texture2DPtr = std::shared_ptr; class Drawable { public: - struct TextureAttachment { - /// @brief A Texture2D instance - std::shared_ptr texture{nullptr}; - /// @brief A sampler location to bind the texture to - int32_t location{0}; - - TextureAttachment() = delete; - TextureAttachment(std::shared_ptr tex, int32_t loc) - : texture(std::move(tex)), - location(loc) {} - }; - using Textures = std::vector; + /// @brief Map from sampler location to texture info + using Textures = std::unordered_map; protected: Drawable(std::string name); @@ -97,20 +87,24 @@ public: /// @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 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. /// @param texture Texture2D instance /// @param location A sampler location in the shader being used with this drawable. - void setTexture(std::shared_ptr texture, int32_t location); + void setTexture(gfx::Texture2DPtr texture, int32_t location); - using TexSourceFunc = std::function; + using TexSourceFunc = std::function; - /// @brief Provide a function to get the current texture - void setTextureSource(int32_t location, TexSourceFunc); - const TexSourceFunc& getTextureSource(int32_t location) const; + /// @brief Provide a function to get the current textures + void setTextureSource(TexSourceFunc value) { textureSource = std::move(value); } + const TexSourceFunc& getTextureSource() const { return textureSource; } /// @brief Provide all texture sources at once void setTextureSources(std::vector); @@ -189,7 +183,7 @@ protected: std::unique_ptr impl; Textures textures; - std::vector textureSources; + TexSourceFunc textureSource; std::vector tweakers; }; diff --git a/include/mbgl/gfx/drawable_builder.hpp b/include/mbgl/gfx/drawable_builder.hpp index 3047274b4..8bcfbf946 100644 --- a/include/mbgl/gfx/drawable_builder.hpp +++ b/include/mbgl/gfx/drawable_builder.hpp @@ -113,10 +113,8 @@ public: /// @param location A sampler location in the shader being used. void setTexture(const gfx::Texture2DPtr&, int32_t location); - using TexSourceFunc = std::function; - /// @brief Provide a function to get the current texture - void setTextureSource(int32_t location, TexSourceFunc); + void setTextureSource(Drawable::TexSourceFunc value) { textureSource = std::move(value); } /// Add a triangle void addTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2); @@ -177,7 +175,7 @@ protected: ColorAttrMode colorAttrMode = ColorAttrMode::PerVertex; VertexAttributeArray vertexAttrs; gfx::Drawable::Textures textures; - std::vector textureSources; + Drawable::TexSourceFunc textureSource; struct Impl; std::unique_ptr impl; diff --git a/src/mbgl/gfx/drawable.cpp b/src/mbgl/gfx/drawable.cpp index aaa3b1ee3..44207af28 100644 --- a/src/mbgl/gfx/drawable.cpp +++ b/src/mbgl/gfx/drawable.cpp @@ -27,40 +27,19 @@ void Drawable::setCullFaceMode(const gfx::CullFaceMode& value) { impl->cullFaceMode = value; } +static const gfx::Texture2DPtr noTexture; + +const gfx::Texture2DPtr& Drawable::getTexture(int32_t location) const { + const auto hit = textures.find(location); + return (hit != textures.end()) ? hit->second : noTexture; +} + void Drawable::setTexture(std::shared_ptr texture, int32_t location) { - for (auto& tex : textures) { - if (tex.location == location) { - tex.texture = std::move(texture); - return; - } - } - textures.emplace_back(std::move(texture), location); + textures.insert(std::make_pair(location, gfx::Texture2DPtr{})).first->second = std::move(texture); } void Drawable::removeTexture(int32_t location) { - for (auto it = textures.begin(); it != textures.end(); ++it) { - if (it->location == location) { - textures.erase(it); - return; - } - } -} - -/// @brief Provide a function to get the current texture -void Drawable::setTextureSource(int32_t location, TexSourceFunc source) { - textureSources.resize(std::max(textureSources.size(), static_cast(location + 1))); - textureSources[location] = std::move(source); -} - -static const Drawable::TexSourceFunc noSource; - -const Drawable::TexSourceFunc& Drawable::getTextureSource(int32_t location) const { - return (static_cast(location) < textureSources.size()) ? textureSources[location] : noSource; -} - -/// @brief Provide all texture sources at once -void Drawable::setTextureSources(std::vector sources) { - textureSources = std::move(sources); + textures.erase(location); } } // namespace gfx diff --git a/src/mbgl/gfx/drawable_builder.cpp b/src/mbgl/gfx/drawable_builder.cpp index a7a134bbe..b6d943db9 100644 --- a/src/mbgl/gfx/drawable_builder.cpp +++ b/src/mbgl/gfx/drawable_builder.cpp @@ -50,7 +50,7 @@ void DrawableBuilder::flush() { draw->setCullFaceMode(impl->cullFaceMode); draw->setShader(shader); draw->setTextures(textures); - draw->setTextureSources(textureSources); + draw->setTextureSource(textureSource); if (auto drawAttrs = getVertexAttributes().clone()) { vertexAttrs.observeAttributes([&](const std::string& iName, const VertexAttribute& iAttr) { @@ -104,18 +104,7 @@ void DrawableBuilder::resetDrawPriority(DrawPriority value) { } void DrawableBuilder::setTexture(const std::shared_ptr& texture, int32_t location) { - for (auto& tex : textures) { - if (tex.location == location) { - tex.texture = texture; - return; - } - } - textures.emplace_back(texture, location); -} - -void DrawableBuilder::setTextureSource(int32_t location, TexSourceFunc source) { - textureSources.resize(std::max(textureSources.size(), static_cast(location + 1))); - textureSources[location] = std::move(source); + textures.insert(std::make_pair(location, gfx::Texture2DPtr{})).first->second = 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/gl/drawable_gl.cpp b/src/mbgl/gl/drawable_gl.cpp index 0098637b0..13e427aac 100644 --- a/src/mbgl/gl/drawable_gl.cpp +++ b/src/mbgl/gl/drawable_gl.cpp @@ -183,14 +183,14 @@ void DrawableGL::upload(gfx::UploadPass& uploadPass) { }; } - for (std::size_t i = 0; i < textureSources.size(); ++i) { - if (const auto& source = textureSources[i]) { - setTexture(source(), static_cast(i)); + if (const auto src = getTextureSource()) { + for (auto& pair : src()) { + setTexture(std::move(pair.second), pair.first); } } const bool texturesNeedUpload = std::any_of( - textures.begin(), textures.end(), [](const auto& tex) { return tex.texture->needsUpload(); }); + textures.begin(), textures.end(), [](const auto& pair) { return pair.second && pair.second->needsUpload(); }); if (texturesNeedUpload) { uploadTextures(); @@ -198,21 +198,28 @@ void DrawableGL::upload(gfx::UploadPass& uploadPass) { } void DrawableGL::uploadTextures() const { - for (const auto& tex : textures) { - std::static_pointer_cast(tex.texture)->upload(); + for (const auto& pair : textures) { + if (const auto& tex = pair.second) { + std::static_pointer_cast(tex)->upload(); + } } } void DrawableGL::bindTextures() const { int32_t unit = 0; - for (const auto& tex : textures) { - std::static_pointer_cast(tex.texture)->bind(tex.location, unit++); + for (const auto& pair : textures) { + if (const auto& tex = pair.second) { + const auto& location = pair.first; + std::static_pointer_cast(tex)->bind(location, unit++); + } } } void DrawableGL::unbindTextures() const { - for (const auto& tex : textures) { - std::static_pointer_cast(tex.texture)->unbind(); + for (const auto& pair : textures) { + if (const auto& tex = pair.second) { + std::static_pointer_cast(tex)->unbind(); + } } } diff --git a/src/mbgl/renderer/layers/fill_layer_tweaker.cpp b/src/mbgl/renderer/layers/fill_layer_tweaker.cpp index d1d0f9ac6..5799b5d94 100644 --- a/src/mbgl/renderer/layers/fill_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/fill_layer_tweaker.cpp @@ -97,17 +97,12 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup, Size textureSize = {0, 0}; if (const auto shader = drawable.getShader()) { if (const auto index = shader->getSamplerLocation("u_image")) { - const auto& textures = drawable.getTextures(); - const auto src = drawable.getTextureSource(*index); - gfx::Texture2DPtr tex = src ? src() : nullptr; - if (!tex && textures.size() > *index) { - tex = textures[*index].texture; - } - if (tex) { + if (const auto& tex = drawable.getTexture(*index)) { textureSize = tex->getSize(); } } } + const FillDrawableUBO drawableUBO = { /*.matrix=*/util::cast(matrix), /*.scale=*/{pixelRatio, tileRatio, crossfade.fromScale, crossfade.toScale}, diff --git a/src/mbgl/renderer/layers/render_fill_layer.cpp b/src/mbgl/renderer/layers/render_fill_layer.cpp index b67fb1879..64347771c 100644 --- a/src/mbgl/renderer/layers/render_fill_layer.cpp +++ b/src/mbgl/renderer/layers/render_fill_layer.cpp @@ -554,7 +554,9 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders, if (const auto& atlases = tile.getAtlasTextures()) { if (const auto samplerLocation = fillShader->getSamplerLocation("u_image")) { - patternBuilder->setTextureSource(*samplerLocation, [=]() { return atlases->icon; }); + patternBuilder->setTextureSource([=]() { + return gfx::Drawable::Textures{{*samplerLocation, atlases->icon}}; + }); } } @@ -571,7 +573,9 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders, if (const auto& atlases = tile.getAtlasTextures()) { if (const auto samplerLocation = outlineShader->getSamplerLocation("u_image")) { - outlinePatternBuilder->setTextureSource(*samplerLocation, [=]() { return atlases->icon; }); + outlinePatternBuilder->setTextureSource([=]() { + return gfx::Drawable::Textures{{*samplerLocation, atlases->icon}}; + }); } }