Make StringIndexer not a singleton

This commit is contained in:
Bart Louwers
2023-10-04 12:47:17 +00:00
parent 43bafd2819
commit 0a81468939
33 changed files with 186 additions and 196 deletions

View File

@@ -384,7 +384,7 @@ public:
auto& attributeNameID = DataDrivenPaintProperty::AttributeNameIDs[attrIndex];
if (!attributeNameID) {
static const std::string attributePrefix = "a_";
attributeNameID = StringIndexer::get(attributePrefix + attributeName.data());
attributeNameID = stringIndexer().get(attributePrefix + attributeName.data());
}
if (auto& attr = getOrAdd(*attributeNameID)) {

View File

@@ -15,36 +15,30 @@ using StringIdentity = size_t;
class StringIndexer {
public:
StringIndexer();
StringIndexer(StringIndexer const&) = delete;
StringIndexer(StringIndexer&&) = delete;
void operator=(StringIndexer const&) = delete;
static StringIdentity get(std::string_view);
static std::string get(const StringIdentity id);
static void clear();
static size_t size();
protected:
StringIndexer();
~StringIndexer() = default;
using MapType = std::unordered_map<std::string_view, StringIdentity>;
using VectorType = std::vector<std::size_t>;
using BufferType = std::vector<char>;
StringIdentity get(std::string_view);
static StringIndexer& instance() {
static StringIndexer inst;
return inst;
}
std::string get(const StringIdentity id);
MapType stringToIdentity;
VectorType identityToString;
BufferType buffer;
void clear();
size_t size();
protected:
std::unordered_map<std::string_view, StringIdentity> stringToIdentity;
std::vector<StringIdentity> identityToString;
std::vector<char> buffer;
std::shared_mutex sharedMutex;
};
/// StringIndexer singleton
StringIndexer& stringIndexer();
} // namespace mbgl

View File

@@ -12,7 +12,7 @@ namespace gfx {
DrawableBuilder::DrawableBuilder(std::string name_)
: name(std::move(name_)),
vertexAttrNameId(StringIndexer::get("a_pos")),
vertexAttrNameId(stringIndexer().get("a_pos")),
renderPass(mbgl::RenderPass::Opaque),
impl(std::make_unique<Impl>()) {}

View File

@@ -124,7 +124,7 @@ void DrawableGL::bindUniformBuffers() const {
using namespace std::string_literals;
const auto currTileID = getTileID() ? util::toString(*getTileID()) : "<no tile>";
Log::Error(Event::General,
"bindUniformBuffers: UBO "s + std::string(StringIndexer::get(element.first)) +
"bindUniformBuffers: UBO "s + std::string(stringIndexer().get(element.first)) +
" not found for " + util::toString(getID()) + " / " + getName() + " / " + currTileID +
". skipping.");
assert(false);

View File

@@ -51,7 +51,7 @@ public:
gfx::CullFaceMode cullFaceMode;
GLfloat pointSize = 0.0f;
StringIdentity idVertexAttrName = StringIndexer::get("a_pos");
StringIdentity idVertexAttrName = stringIndexer().get("a_pos");
};
struct DrawableGL::DrawSegmentGL final : public gfx::Drawable::DrawSegment {

View File

@@ -260,7 +260,7 @@ gfx::AttributeBindingArray UploadPass::buildAttributeBindings(
// something else, the binding is invalid
// TODO: throw?
Log::Warning(Event::General,
"Got " + util::toString(rawData.size()) + " bytes for attribute '" + StringIndexer::get(id) +
"Got " + util::toString(rawData.size()) + " bytes for attribute '" + stringIndexer().get(id) +
"' (" + util::toString(defaultGL.getIndex()) + "), expected " + util::toString(stride) +
" or " + util::toString(stride * vertexCount));
return;

View File

@@ -311,7 +311,7 @@ void Drawable::setVertices(std::vector<uint8_t>&& data, std::size_t count, gfx::
using namespace std::string_literals;
Log::Warning(
Event::General,
"Vertex attribute type mismatch: "s + name + " / " + StringIndexer::get(impl->idVertexAttrName));
"Vertex attribute type mismatch: "s + name + " / " + stringIndexer().get(impl->idVertexAttrName));
assert(false);
}
}
@@ -380,7 +380,7 @@ void Drawable::bindUniformBuffers(const RenderPass& renderPass) const {
const auto tileID = getTileID() ? util::toString(*getTileID()) : "<no tile>";
const auto tileLabel = util::toString(getID()) + "/" + getName() + "/" + tileID;
Log::Error(Event::General,
"bindUniformBuffers: UBO "s + StringIndexer::get(element.first) + " not found on " +
"bindUniformBuffers: UBO "s + stringIndexer().get(element.first) + " not found on " +
tileLabel + ". skipping.");
assert(false);
continue;

View File

@@ -52,7 +52,7 @@ public:
gfx::StencilMode stencilMode;
gfx::CullFaceMode cullFaceMode;
// GLfloat pointSize = 0.0f;
StringIdentity idVertexAttrName = StringIndexer::get("a_pos");
StringIdentity idVertexAttrName = stringIndexer().get("a_pos");
gfx::UniqueVertexBufferResource noBindingBuffer;
};

View File

@@ -19,9 +19,9 @@ using namespace shaders;
#if !defined(NDEBUG)
constexpr auto BackgroundPatternShaderName = "BackgroundPatternShader";
#endif
static const StringIdentity idBackgroundDrawableUBOName = StringIndexer::get("BackgroundDrawableUBO");
static const StringIdentity idBackgroundLayerUBOName = StringIndexer::get("BackgroundLayerUBO");
static const StringIdentity idTexUniformName = StringIndexer::get("u_image");
static const StringIdentity idBackgroundDrawableUBOName = stringIndexer().get("BackgroundDrawableUBO");
static const StringIdentity idBackgroundLayerUBOName = stringIndexer().get("BackgroundLayerUBO");
static const StringIdentity idTexUniformName = stringIndexer().get("u_image");
void BackgroundLayerTweaker::execute(LayerGroupBase& layerGroup, const RenderTree&, const PaintParameters& parameters) {
const auto& state = parameters.state;

View File

@@ -22,12 +22,12 @@ namespace mbgl {
using namespace style;
using namespace shaders;
static const StringIdentity idCircleDrawableUBOName = StringIndexer::get("CircleDrawableUBO");
static const StringIdentity idCirclePaintParamsUBOName = StringIndexer::get("CirclePaintParamsUBO");
static const StringIdentity idCircleEvaluatedPropsUBOName = StringIndexer::get("CircleEvaluatedPropsUBO");
static const StringIdentity idCircleDrawableUBOName = stringIndexer().get("CircleDrawableUBO");
static const StringIdentity idCirclePaintParamsUBOName = stringIndexer().get("CirclePaintParamsUBO");
static const StringIdentity idCircleEvaluatedPropsUBOName = stringIndexer().get("CircleEvaluatedPropsUBO");
static const StringIdentity idExpressionInputsUBOName = StringIndexer::get("ExpressionInputsUBO");
static const StringIdentity idCirclePermutationUBOName = StringIndexer::get("CirclePermutationUBO");
static const StringIdentity idExpressionInputsUBOName = stringIndexer().get("ExpressionInputsUBO");
static const StringIdentity idCirclePermutationUBOName = stringIndexer().get("CirclePermutationUBO");
void CircleLayerTweaker::execute(LayerGroupBase& layerGroup,
const RenderTree& renderTree,

View File

@@ -31,8 +31,8 @@ static_assert(sizeof(CollisionUBO) == 20 * 4);
} // namespace
const StringIdentity CollisionLayerTweaker::idCollisionCircleUBOName = StringIndexer::get(CollisionCircleUBOName);
const StringIdentity CollisionLayerTweaker::idCollisionBoxUBOName = StringIndexer::get(CollisionBoxUBOName);
const StringIdentity CollisionLayerTweaker::idCollisionCircleUBOName = stringIndexer().get(CollisionCircleUBOName);
const StringIdentity CollisionLayerTweaker::idCollisionBoxUBOName = stringIndexer().get(CollisionBoxUBOName);
void CollisionLayerTweaker::execute(LayerGroupBase& layerGroup,
const RenderTree& renderTree,

View File

@@ -22,17 +22,17 @@ using namespace shaders;
using namespace style;
namespace {
const StringIdentity idFillExtrusionDrawableUBOName = StringIndexer::get("FillExtrusionDrawableUBO");
const StringIdentity idFillExtrusionDrawablePropsUBOName = StringIndexer::get("FillExtrusionDrawablePropsUBO");
const StringIdentity idExpressionInputsUBOName = StringIndexer::get("ExpressionInputsUBO");
const StringIdentity idFillExtrusionPermutationUBOName = StringIndexer::get("FillExtrusionPermutationUBO");
const StringIdentity idTexImageName = StringIndexer::get("u_image");
const StringIdentity idFillExtrusionDrawableUBOName = stringIndexer().get("FillExtrusionDrawableUBO");
const StringIdentity idFillExtrusionDrawablePropsUBOName = stringIndexer().get("FillExtrusionDrawablePropsUBO");
const StringIdentity idExpressionInputsUBOName = stringIndexer().get("ExpressionInputsUBO");
const StringIdentity idFillExtrusionPermutationUBOName = stringIndexer().get("FillExtrusionPermutationUBO");
const StringIdentity idTexImageName = stringIndexer().get("u_image");
} // namespace
const StringIdentity FillExtrusionLayerTweaker::idFillExtrusionTilePropsUBOName = StringIndexer::get(
const StringIdentity FillExtrusionLayerTweaker::idFillExtrusionTilePropsUBOName = stringIndexer().get(
"FillExtrusionDrawableTilePropsUBO");
const StringIdentity FillExtrusionLayerTweaker::idFillExtrusionInterpolateUBOName = StringIndexer::get(
const StringIdentity FillExtrusionLayerTweaker::idFillExtrusionInterpolateUBOName = stringIndexer().get(
"FillExtrusionInterpolateUBO");
void FillExtrusionLayerTweaker::execute(LayerGroupBase& layerGroup,

View File

@@ -24,40 +24,41 @@ namespace mbgl {
using namespace style;
static const StringIdentity idFillDrawableUBOName = StringIndexer::get("FillDrawableUBO");
static const StringIdentity idFillDrawablePropsUBOName = StringIndexer::get("FillDrawablePropsUBO");
static const StringIdentity idFillEvaluatedPropsUBOName = StringIndexer::get("FillEvaluatedPropsUBO");
static const StringIdentity idFillPermutationUBOName = StringIndexer::get("FillPermutationUBO");
static const StringIdentity idFillDrawableUBOName = stringIndexer().get("FillDrawableUBO");
static const StringIdentity idFillDrawablePropsUBOName = stringIndexer().get("FillDrawablePropsUBO");
static const StringIdentity idFillEvaluatedPropsUBOName = stringIndexer().get("FillEvaluatedPropsUBO");
static const StringIdentity idFillPermutationUBOName = stringIndexer().get("FillPermutationUBO");
const StringIdentity FillLayerTweaker::idFillTilePropsUBOName = StringIndexer::get("FillDrawableTilePropsUBO");
const StringIdentity FillLayerTweaker::idFillInterpolateUBOName = StringIndexer::get("FillInterpolateUBO");
const StringIdentity FillLayerTweaker::idFillOutlineInterpolateUBOName = StringIndexer::get(
const StringIdentity FillLayerTweaker::idFillTilePropsUBOName = stringIndexer().get("FillDrawableTilePropsUBO");
const StringIdentity FillLayerTweaker::idFillInterpolateUBOName = stringIndexer().get("FillInterpolateUBO");
const StringIdentity FillLayerTweaker::idFillOutlineInterpolateUBOName = stringIndexer().get(
"FillOutlineInterpolateUBO");
static const StringIdentity idFillOutlineDrawableUBOName = StringIndexer::get("FillOutlineDrawableUBO");
static const StringIdentity idFillOutlineEvaluatedPropsUBOName = StringIndexer::get("FillOutlineEvaluatedPropsUBO");
static const StringIdentity idFillOutlinePermutationUBOName = StringIndexer::get("FillOutlinePermutationUBO");
static const StringIdentity idFillOutlineDrawableUBOName = stringIndexer().get("FillOutlineDrawableUBO");
static const StringIdentity idFillOutlineEvaluatedPropsUBOName = stringIndexer().get("FillOutlineEvaluatedPropsUBO");
static const StringIdentity idFillOutlinePermutationUBOName = stringIndexer().get("FillOutlinePermutationUBO");
static const StringIdentity idFillOutlineInterpolateUBOName = StringIndexer::get("FillOutlineInterpolateUBO");
static const StringIdentity idFillOutlineInterpolateUBOName = stringIndexer().get("FillOutlineInterpolateUBO");
static const StringIdentity idFillPatternDrawableUBOName = StringIndexer::get("FillPatternDrawableUBO");
static const StringIdentity idFillPatternPermutationUBOName = StringIndexer::get("FillPatternPermutationUBO");
static const StringIdentity idFillPatternInterpolateUBOName = StringIndexer::get("FillPatternInterpolateUBO");
static const StringIdentity idFillPatternEvaluatedPropsUBOName = StringIndexer::get("FillPatternEvaluatedPropsUBO");
static const StringIdentity idFillPatternTilePropsUBOName = StringIndexer::get("FillPatternTilePropsUBO");
static const StringIdentity idFillPatternDrawableUBOName = stringIndexer().get("FillPatternDrawableUBO");
static const StringIdentity idFillPatternPermutationUBOName = stringIndexer().get("FillPatternPermutationUBO");
static const StringIdentity idFillPatternInterpolateUBOName = stringIndexer().get("FillPatternInterpolateUBO");
static const StringIdentity idFillPatternEvaluatedPropsUBOName = stringIndexer().get("FillPatternEvaluatedPropsUBO");
static const StringIdentity idFillPatternTilePropsUBOName = stringIndexer().get("FillPatternTilePropsUBO");
static const StringIdentity idFillOutlinePatternDrawableUBOName = StringIndexer::get("FillOutlinePatternDrawableUBO");
static const StringIdentity idFillOutlinePatternPermutationUBOName = StringIndexer::get(
static const StringIdentity idFillOutlinePatternDrawableUBOName = stringIndexer().get("FillOutlinePatternDrawableUBO");
static const StringIdentity idFillOutlinePatternPermutationUBOName = stringIndexer().get(
"FillOutlinePatternPermutationUBO");
static const StringIdentity idFillOutlinePatternInterpolateUBOName = StringIndexer::get(
static const StringIdentity idFillOutlinePatternInterpolateUBOName = stringIndexer().get(
"FillOutlinePatternInterpolateUBO");
static const StringIdentity idFillOutlinePatternEvaluatedPropsUBOName = StringIndexer::get(
static const StringIdentity idFillOutlinePatternEvaluatedPropsUBOName = stringIndexer().get(
"FillOutlinePatternEvaluatedPropsUBO");
static const StringIdentity idFillOutlinePatternTilePropsUBOName = StringIndexer::get("FillOutlinePatternTilePropsUBO");
static const StringIdentity idFillOutlinePatternTilePropsUBOName = stringIndexer().get(
"FillOutlinePatternTilePropsUBO");
static const StringIdentity idExpressionInputsUBOName = StringIndexer::get("ExpressionInputsUBO");
static const StringIdentity idExpressionInputsUBOName = stringIndexer().get("ExpressionInputsUBO");
static const StringIdentity idTexImageName = StringIndexer::get("u_image");
static const StringIdentity idTexImageName = stringIndexer().get("u_image");
using namespace shaders;
void FillLayerTweaker::execute(LayerGroupBase& layerGroup,

View File

@@ -20,10 +20,10 @@ namespace mbgl {
using namespace style;
using namespace shaders;
static const StringIdentity idHeatmapDrawableUBOName = StringIndexer::get("HeatmapDrawableUBO");
static const StringIdentity idHeatmapEvaluatedPropsUBOName = StringIndexer::get("HeatmapEvaluatedPropsUBO");
static const StringIdentity idHeatmapPermutationUBOName = StringIndexer::get("HeatmapPermutationUBO");
static const StringIdentity idExpressionInputsUBOName = StringIndexer::get("ExpressionInputsUBO");
static const StringIdentity idHeatmapDrawableUBOName = stringIndexer().get("HeatmapDrawableUBO");
static const StringIdentity idHeatmapEvaluatedPropsUBOName = stringIndexer().get("HeatmapEvaluatedPropsUBO");
static const StringIdentity idHeatmapPermutationUBOName = stringIndexer().get("HeatmapPermutationUBO");
static const StringIdentity idExpressionInputsUBOName = stringIndexer().get("ExpressionInputsUBO");
void HeatmapLayerTweaker::execute(LayerGroupBase& layerGroup,
const RenderTree& renderTree,

View File

@@ -16,7 +16,7 @@ namespace mbgl {
using namespace style;
using namespace shaders;
static const StringIdentity idHeatmapTextureDrawableUBOName = StringIndexer::get("HeatmapTextureDrawableUBO");
static const StringIdentity idHeatmapTextureDrawableUBOName = stringIndexer().get("HeatmapTextureDrawableUBO");
void HeatmapTextureLayerTweaker::execute(LayerGroupBase& layerGroup,
[[maybe_unused]] const RenderTree& renderTree,

View File

@@ -15,8 +15,8 @@ namespace mbgl {
using namespace style;
using namespace shaders;
static const StringIdentity idHillshadeDrawableUBOName = StringIndexer::get("HillshadeDrawableUBO");
static const StringIdentity idHillshadeEvaluatedPropsUBOName = StringIndexer::get("HillshadeEvaluatedPropsUBO");
static const StringIdentity idHillshadeDrawableUBOName = stringIndexer().get("HillshadeDrawableUBO");
static const StringIdentity idHillshadeEvaluatedPropsUBOName = stringIndexer().get("HillshadeEvaluatedPropsUBO");
std::array<float, 2> getLatRange(const UnwrappedTileID& id) {
const LatLng latlng0 = LatLng(id);

View File

@@ -16,7 +16,7 @@ namespace mbgl {
using namespace style;
using namespace shaders;
static const StringIdentity idHillshadePrepareDrawableUBOName = StringIndexer::get("HillshadePrepareDrawableUBO");
static const StringIdentity idHillshadePrepareDrawableUBOName = stringIndexer().get("HillshadePrepareDrawableUBO");
const std::array<float, 4>& getUnpackVector(Tileset::DEMEncoding encoding) {
// https://www.mapbox.com/help/access-elevation-data/#mapbox-terrain-rgb

View File

@@ -24,18 +24,18 @@ namespace mbgl {
using namespace style;
using namespace shaders;
static const StringIdentity idLineUBOName = StringIndexer::get("LineUBO");
static const StringIdentity idLinePropertiesUBOName = StringIndexer::get("LinePropertiesUBO");
static const StringIdentity idLineGradientUBOName = StringIndexer::get("LineGradientUBO");
static const StringIdentity idLineGradientPropertiesUBOName = StringIndexer::get("LineGradientPropertiesUBO");
static const StringIdentity idLinePatternUBOName = StringIndexer::get("LinePatternUBO");
static const StringIdentity idLinePatternPropertiesUBOName = StringIndexer::get("LinePatternPropertiesUBO");
static const StringIdentity idLineSDFUBOName = StringIndexer::get("LineSDFUBO");
static const StringIdentity idLineSDFPropertiesUBOName = StringIndexer::get("LineSDFPropertiesUBO");
static const StringIdentity idTexImageName = StringIndexer::get("u_image");
static const StringIdentity idLineUBOName = stringIndexer().get("LineUBO");
static const StringIdentity idLinePropertiesUBOName = stringIndexer().get("LinePropertiesUBO");
static const StringIdentity idLineGradientUBOName = stringIndexer().get("LineGradientUBO");
static const StringIdentity idLineGradientPropertiesUBOName = stringIndexer().get("LineGradientPropertiesUBO");
static const StringIdentity idLinePatternUBOName = stringIndexer().get("LinePatternUBO");
static const StringIdentity idLinePatternPropertiesUBOName = stringIndexer().get("LinePatternPropertiesUBO");
static const StringIdentity idLineSDFUBOName = stringIndexer().get("LineSDFUBO");
static const StringIdentity idLineSDFPropertiesUBOName = stringIndexer().get("LineSDFPropertiesUBO");
static const StringIdentity idTexImageName = stringIndexer().get("u_image");
static const StringIdentity idExpressionInputsUBOName = StringIndexer::get("ExpressionInputsUBO");
static const StringIdentity idLinePermutationUBOName = StringIndexer::get("LinePermutationUBO");
static const StringIdentity idExpressionInputsUBOName = stringIndexer().get("ExpressionInputsUBO");
static const StringIdentity idLinePermutationUBOName = stringIndexer().get("LinePermutationUBO");
void LineLayerTweaker::execute(LayerGroupBase& layerGroup,
const RenderTree& renderTree,

View File

@@ -17,7 +17,7 @@ namespace mbgl {
using namespace style;
using namespace shaders;
static const StringIdentity idRasterDrawableUBOName = StringIndexer::get("RasterDrawableUBO");
static const StringIdentity idRasterDrawableUBOName = stringIndexer().get("RasterDrawableUBO");
void RasterLayerTweaker::execute([[maybe_unused]] LayerGroupBase& layerGroup,
[[maybe_unused]] const RenderTree& renderTree,

View File

@@ -271,8 +271,8 @@ bool RenderCircleLayer::queryIntersectsFeature(const GeometryCoordinates& queryG
namespace {
constexpr auto CircleShaderGroupName = "CircleShader";
const StringIdentity idCircleInterpolateUBOName = StringIndexer::get("CircleInterpolateUBO");
const StringIdentity idVertexAttribName = StringIndexer::get("a_pos");
const StringIdentity idCircleInterpolateUBOName = stringIndexer().get("CircleInterpolateUBO");
const StringIdentity idVertexAttribName = stringIndexer().get("a_pos");
} // namespace

View File

@@ -39,10 +39,10 @@ using namespace shaders;
namespace {
#if MLN_DRAWABLE_RENDERER
const StringIdentity idPosAttribName = StringIndexer::get("a_pos");
const StringIdentity idNormAttribName = StringIndexer::get("a_normal_ed");
const StringIdentity idPosAttribName = stringIndexer().get("a_pos");
const StringIdentity idNormAttribName = stringIndexer().get("a_normal_ed");
const StringIdentity idIconTextureName = StringIndexer::get("u_image");
const StringIdentity idIconTextureName = stringIndexer().get("u_image");
#endif // MLN_DRAWABLE_RENDERER

View File

@@ -46,14 +46,14 @@ constexpr auto FillOutlineShaderName = "FillOutlineShader";
constexpr auto FillPatternShaderName = "FillPatternShader";
constexpr auto FillOutlinePatternShaderName = "FillOutlinePatternShader";
const StringIdentity idFillOutlineInterpolateUBOName = StringIndexer::get("FillOutlineInterpolateUBO");
const StringIdentity idFillPatternInterpolateUBOName = StringIndexer::get("FillPatternInterpolateUBO");
const StringIdentity idFillPatternTilePropsUBOName = StringIndexer::get("FillPatternTilePropsUBO");
const StringIdentity idFillOutlinePatternInterpolateUBOName = StringIndexer::get("FillOutlinePatternInterpolateUBO");
const StringIdentity idFillOutlinePatternTilePropsUBOName = StringIndexer::get("FillOutlinePatternTilePropsUBO");
const StringIdentity idFillOutlineInterpolateUBOName = stringIndexer().get("FillOutlineInterpolateUBO");
const StringIdentity idFillPatternInterpolateUBOName = stringIndexer().get("FillPatternInterpolateUBO");
const StringIdentity idFillPatternTilePropsUBOName = stringIndexer().get("FillPatternTilePropsUBO");
const StringIdentity idFillOutlinePatternInterpolateUBOName = stringIndexer().get("FillOutlinePatternInterpolateUBO");
const StringIdentity idFillOutlinePatternTilePropsUBOName = stringIndexer().get("FillOutlinePatternTilePropsUBO");
const StringIdentity idPosAttribName = StringIndexer::get("a_pos");
const StringIdentity idIconTextureName = StringIndexer::get("u_image");
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<style::Layer::Impl>& impl) {

View File

@@ -280,10 +280,10 @@ namespace {
constexpr auto HeatmapShaderGroupName = "HeatmapShader";
constexpr auto HeatmapTextureShaderGroupName = "HeatmapTextureShader";
const StringIdentity idHeatmapInterpolateUBOName = StringIndexer::get("HeatmapInterpolateUBO");
const StringIdentity idVertexAttribName = StringIndexer::get("a_pos");
const StringIdentity idTexImageName = StringIndexer::get("u_image");
const StringIdentity idTexColorRampName = StringIndexer::get("u_color_ramp");
const StringIdentity idHeatmapInterpolateUBOName = stringIndexer().get("HeatmapInterpolateUBO");
const StringIdentity idVertexAttribName = stringIndexer().get("a_pos");
const StringIdentity idTexImageName = stringIndexer().get("u_image");
const StringIdentity idTexColorRampName = stringIndexer().get("u_color_ramp");
} // namespace

View File

@@ -299,9 +299,9 @@ void RenderHillshadeLayer::removeRenderTargets(UniqueChangeRequestVec& changes)
static const std::string HillshadePrepareShaderGroupName = "HillshadePrepareShader";
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");
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,

View File

@@ -47,8 +47,8 @@ inline const LineLayer::Impl& impl_cast(const Immutable<style::Layer::Impl>& imp
#if MLN_DRAWABLE_RENDERER
const StringIdentity idVertexAttribName = StringIndexer::get("a_pos_normal");
const StringIdentity idDataAttribName = StringIndexer::get("a_data");
const StringIdentity idVertexAttribName = stringIndexer().get("a_pos_normal");
const StringIdentity idDataAttribName = stringIndexer().get("a_data");
#endif // MLN_DRAWABLE_RENDERER
@@ -351,15 +351,15 @@ float RenderLineLayer::getLineWidth(const GeometryTileFeature& feature,
#if MLN_DRAWABLE_RENDERER
/// Property interpolation UBOs
static const StringIdentity idLineInterpolationUBOName = StringIndexer::get("LineInterpolationUBO");
static const StringIdentity idLineGradientInterpolationUBOName = StringIndexer::get("LineGradientInterpolationUBO");
static const StringIdentity idLinePatternInterpolationUBOName = StringIndexer::get("LinePatternInterpolationUBO");
static const StringIdentity idLineSDFInterpolationUBOName = StringIndexer::get("LineSDFInterpolationUBO");
static const StringIdentity idLineInterpolationUBOName = stringIndexer().get("LineInterpolationUBO");
static const StringIdentity idLineGradientInterpolationUBOName = stringIndexer().get("LineGradientInterpolationUBO");
static const StringIdentity idLinePatternInterpolationUBOName = stringIndexer().get("LinePatternInterpolationUBO");
static const StringIdentity idLineSDFInterpolationUBOName = stringIndexer().get("LineSDFInterpolationUBO");
/// Evaluated properties that depend on the tile
static const StringIdentity idLinePatternTilePropertiesUBOName = StringIndexer::get("LinePatternTilePropertiesUBO");
static const StringIdentity idLinePatternTilePropertiesUBOName = stringIndexer().get("LinePatternTilePropertiesUBO");
static const StringIdentity idLineImageUniformName = StringIndexer::get("u_image");
static const StringIdentity idLineImageUniformName = stringIndexer().get("u_image");
void RenderLineLayer::update(gfx::ShaderRegistry& shaders,
gfx::Context& context,

View File

@@ -246,10 +246,10 @@ void RenderRasterLayer::layerIndexChanged(int32_t newLayerIndex, UniqueChangeReq
changeLayerIndex(imageLayerGroup, newLayerIndex, changes);
}
static const StringIdentity idPosAttribName = StringIndexer::get("a_pos");
static const StringIdentity idTexturePosAttribName = StringIndexer::get("a_texture_pos");
static const StringIdentity idTexImage0Name = StringIndexer::get("u_image0");
static const StringIdentity idTexImage1Name = StringIndexer::get("u_image1");
static const StringIdentity idPosAttribName = stringIndexer().get("a_pos");
static const StringIdentity idTexturePosAttribName = stringIndexer().get("a_texture_pos");
static const StringIdentity idTexImage0Name = stringIndexer().get("u_image0");
static const StringIdentity idTexImage1Name = stringIndexer().get("u_image1");
void RenderRasterLayer::update(gfx::ShaderRegistry& shaders,
gfx::Context& context,

View File

@@ -748,13 +748,13 @@ SymbolDrawableTilePropsUBO buildTileUBO(const SymbolBucket& bucket,
};
}
const StringIdentity idDataAttibName = StringIndexer::get("a_data");
const StringIdentity idPosOffsetAttribName = StringIndexer::get("a_pos_offset");
const StringIdentity idPixOffsetAttribName = StringIndexer::get("a_pixeloffset");
const StringIdentity idProjPosAttribName = StringIndexer::get("a_projected_pos");
const StringIdentity idFadeOpacityAttribName = StringIndexer::get("a_fade_opacity");
const StringIdentity idTexUniformName = StringIndexer::get("u_texture");
const StringIdentity idTexIconUniformName = StringIndexer::get("u_texture_icon");
const StringIdentity idDataAttibName = stringIndexer().get("a_data");
const StringIdentity idPosOffsetAttribName = stringIndexer().get("a_pos_offset");
const StringIdentity idPixOffsetAttribName = stringIndexer().get("a_pixeloffset");
const StringIdentity idProjPosAttribName = stringIndexer().get("a_projected_pos");
const StringIdentity idFadeOpacityAttribName = stringIndexer().get("a_fade_opacity");
const StringIdentity idTexUniformName = stringIndexer().get("u_texture");
const StringIdentity idTexIconUniformName = stringIndexer().get("u_texture_icon");
std::vector<std::string> updateTileAttributes(const SymbolBucket::Buffer& buffer,
const bool isText,
@@ -862,11 +862,11 @@ void updateTileDrawable(gfx::Drawable& drawable,
drawable.setVertexAttributes(std::move(attribs));
}
const StringIdentity idCollisionPosAttribName = StringIndexer::get("a_pos");
const StringIdentity idCollisionAnchorPosAttribName = StringIndexer::get("a_anchor_pos");
const StringIdentity idCollisionExtrudeAttribName = StringIndexer::get("a_extrude");
const StringIdentity idCollisionPlacedAttribName = StringIndexer::get("a_placed");
const StringIdentity idCollisionShiftAttribName = StringIndexer::get("a_shift");
const StringIdentity idCollisionPosAttribName = stringIndexer().get("a_pos");
const StringIdentity idCollisionAnchorPosAttribName = stringIndexer().get("a_anchor_pos");
const StringIdentity idCollisionExtrudeAttribName = stringIndexer().get("a_extrude");
const StringIdentity idCollisionPlacedAttribName = stringIndexer().get("a_placed");
const StringIdentity idCollisionShiftAttribName = stringIndexer().get("a_shift");
gfx::VertexAttributeArray getCollisionVertexAttributes(const SymbolBucket::CollisionBuffer& buffer, bool staticCopy) {
gfx::VertexAttributeArray vertexAttrs;
@@ -1139,7 +1139,7 @@ void RenderSymbolLayer::update(gfx::ShaderRegistry& shaders,
const auto& collisionBox = isText ? bucket.textCollisionBox : bucket.iconCollisionBox;
if (const auto shader = std::static_pointer_cast<gfx::ShaderProgramBase>(
collisionBoxGroup->getOrCreateShader(
context, {}, StringIndexer::get(idCollisionPosAttribName)))) {
context, {}, stringIndexer().get(idCollisionPosAttribName)))) {
collisionBuilder->setDrawableName(layerCollisionPrefix + suffix + "box");
collisionBuilder->setShader(shader);
addVertices(collisionBox->vertices().vector(), staticVertexAndAttributes);
@@ -1157,7 +1157,7 @@ void RenderSymbolLayer::update(gfx::ShaderRegistry& shaders,
const auto& collisionCircle = isText ? bucket.textCollisionCircle : bucket.iconCollisionCircle;
if (const auto shader = std::static_pointer_cast<gfx::ShaderProgramBase>(
collisionCircleGroup->getOrCreateShader(
context, {}, StringIndexer::get(idCollisionPosAttribName)))) {
context, {}, stringIndexer().get(idCollisionPosAttribName)))) {
collisionBuilder->setDrawableName(layerCollisionPrefix + suffix + "circle");
collisionBuilder->setShader(shader);
addVertices(collisionCircle->vertices().vector(), staticVertexAndAttributes);
@@ -1355,7 +1355,7 @@ void RenderSymbolLayer::update(gfx::ShaderRegistry& shaders,
return;
}
const auto shader = std::static_pointer_cast<gfx::ShaderProgramBase>(shaderGroup->getOrCreateShader(
context, propertiesAsUniforms, StringIndexer::get(idPosOffsetAttribName)));
context, propertiesAsUniforms, stringIndexer().get(idPosOffsetAttribName)));
if (!shader) {
return;
}

View File

@@ -43,10 +43,10 @@ std::array<float, 2> toArray(const Size& s) {
return util::cast<float>(std::array<uint32_t, 2>{s.width, s.height});
}
const StringIdentity idTexUniformName = StringIndexer::get("u_texture");
const StringIdentity idTexIconUniformName = StringIndexer::get("u_texture_icon");
const StringIdentity idExpressionInputsUBOName = StringIndexer::get("ExpressionInputsUBO");
const StringIdentity idSymbolPermutationUBOName = StringIndexer::get("SymbolPermutationUBO");
const StringIdentity idTexUniformName = stringIndexer().get("u_texture");
const StringIdentity idTexIconUniformName = stringIndexer().get("u_texture_icon");
const StringIdentity idExpressionInputsUBOName = stringIndexer().get("ExpressionInputsUBO");
const StringIdentity idSymbolPermutationUBOName = stringIndexer().get("SymbolPermutationUBO");
SymbolDrawablePaintUBO buildPaintUBO(bool isText, const SymbolPaintProperties::PossiblyEvaluated& evaluated) {
return {
@@ -63,12 +63,12 @@ SymbolDrawablePaintUBO buildPaintUBO(bool isText, const SymbolPaintProperties::P
} // namespace
const StringIdentity SymbolLayerTweaker::idSymbolDrawableUBOName = StringIndexer::get("SymbolDrawableUBO");
const StringIdentity SymbolLayerTweaker::idSymbolDynamicUBOName = StringIndexer::get("SymbolDynamicUBO");
const StringIdentity SymbolLayerTweaker::idSymbolDrawablePaintUBOName = StringIndexer::get("SymbolDrawablePaintUBO");
const StringIdentity SymbolLayerTweaker::idSymbolDrawableTilePropsUBOName = StringIndexer::get(
const StringIdentity SymbolLayerTweaker::idSymbolDrawableUBOName = stringIndexer().get("SymbolDrawableUBO");
const StringIdentity SymbolLayerTweaker::idSymbolDynamicUBOName = stringIndexer().get("SymbolDynamicUBO");
const StringIdentity SymbolLayerTweaker::idSymbolDrawablePaintUBOName = stringIndexer().get("SymbolDrawablePaintUBO");
const StringIdentity SymbolLayerTweaker::idSymbolDrawableTilePropsUBOName = stringIndexer().get(
"SymbolDrawableTilePropsUBO");
const StringIdentity SymbolLayerTweaker::idSymbolDrawableInterpolateUBOName = StringIndexer::get(
const StringIdentity SymbolLayerTweaker::idSymbolDrawableInterpolateUBOName = stringIndexer().get(
"SymbolDrawableInterpolateUBO");
void SymbolLayerTweaker::execute(LayerGroupBase& layerGroup,

View File

@@ -67,8 +67,8 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr
}
// create a builder
static const StringIdentity idVertexAttribName = StringIndexer::get("a_pos");
static const StringIdentity idDebugUBOName = StringIndexer::get("DebugUBO");
static const StringIdentity idVertexAttribName = stringIndexer().get("a_pos");
static const StringIdentity idDebugUBOName = stringIndexer().get("DebugUBO");
std::unique_ptr<gfx::DrawableBuilder> builder = context.createDrawableBuilder("debug-builder");
builder->setShader(debugShader);
builder->setRenderPass(renderPass);
@@ -109,7 +109,7 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr
texture->setImage(emptyImage);
texture->setSamplerConfiguration(
{gfx::TextureFilterType::Linear, gfx::TextureWrapType::Clamp, gfx::TextureWrapType::Clamp});
static const StringIdentity idDebugOverlayUniformName = StringIndexer::get("u_overlay");
static const StringIdentity idDebugOverlayUniformName = stringIndexer().get("u_overlay");
samplerLocation = debugShader->getSamplerLocation(idDebugOverlayUniformName);
}
assert(samplerLocation.has_value());

View File

@@ -158,7 +158,7 @@ std::shared_ptr<ShaderProgramGL> ShaderProgramGL::create(Context& context,
MBGL_CHECK_ERROR(glGetActiveUniformBlockiv(program, index, GL_UNIFORM_BLOCK_DATA_SIZE, &size));
assert(length > 0 && size > 0);
MBGL_CHECK_ERROR(glUniformBlockBinding(program, index, binding));
uniformBlocks.add(StringIndexer::get(name.data()), index, size);
uniformBlocks.add(stringIndexer().get(name.data()), index, size);
}
SamplerLocationMap samplerLocations;
@@ -178,7 +178,7 @@ std::shared_ptr<ShaderProgramGL> ShaderProgramGL::create(Context& context,
GLint location = MBGL_CHECK_ERROR(glGetUniformLocation(program, name.data()));
assert(location != -1);
if (location != -1) {
samplerLocations[StringIndexer::get(name.data())] = location;
samplerLocations[stringIndexer().get(name.data())] = location;
}
}
}
@@ -198,7 +198,7 @@ std::shared_ptr<ShaderProgramGL> ShaderProgramGL::create(Context& context,
continue;
}
const GLint location = MBGL_CHECK_ERROR(glGetAttribLocation(program, name.data()));
addAttr(attrs, StringIndexer::get(name.data()), location, length, size, glType);
addAttr(attrs, stringIndexer().get(name.data()), location, length, size, glType);
}
return std::make_shared<ShaderProgramGL>(

View File

@@ -182,7 +182,7 @@ void ShaderProgram::initAttribute(const shaders::AttributeInfo& info) {
[&](auto, const gfx::VertexAttribute& attrib) { assert(attrib.getIndex() != index); });
uniformBlocks.visit([&](auto, const gfx::UniformBlock& block) { assert(block.getIndex() != index); });
#endif
vertexAttributes.add(StringIndexer::get(info.name), index, info.dataType, info.count);
vertexAttributes.add(stringIndexer().get(info.name), index, info.dataType, info.count);
}
void ShaderProgram::initUniformBlock(const shaders::UniformBlockInfo& info) {
@@ -193,7 +193,7 @@ void ShaderProgram::initUniformBlock(const shaders::UniformBlockInfo& info) {
[&](auto, const gfx::VertexAttribute& attrib) { assert(attrib.getIndex() != index); });
uniformBlocks.visit([&](auto, const gfx::UniformBlock& block) { assert(block.getIndex() != index); });
#endif
if (const auto& block_ = uniformBlocks.add(StringIndexer::get(info.name), index, info.size)) {
if (const auto& block_ = uniformBlocks.add(stringIndexer().get(info.name), index, info.size)) {
auto& block = static_cast<UniformBlock&>(*block_);
block.setBindVertex(info.vertex);
block.setBindFragment(info.fragment);
@@ -201,7 +201,7 @@ void ShaderProgram::initUniformBlock(const shaders::UniformBlockInfo& info) {
}
void ShaderProgram::initTexture(const shaders::TextureInfo& info) {
textureBindings[StringIndexer::get(info.name.data())] = info.index;
textureBindings[stringIndexer().get(info.name.data())] = info.index;
}
} // namespace mtl

View File

@@ -19,10 +19,7 @@ StringIndexer::StringIndexer() {
StringIdentity StringIndexer::get(std::string_view string) {
{
std::shared_lock<std::shared_mutex> readerLock(instance().sharedMutex);
const auto& stringToIdentity = instance().stringToIdentity;
[[maybe_unused]] const auto& identityToString = instance().identityToString;
std::shared_lock<std::shared_mutex> readerLock(sharedMutex);
assert(stringToIdentity.size() == identityToString.size());
if (const auto it = stringToIdentity.find(string); it != stringToIdentity.end()) {
@@ -31,15 +28,11 @@ StringIdentity StringIndexer::get(std::string_view string) {
}
{
std::unique_lock<std::shared_mutex> writerLock(instance().sharedMutex);
auto& stringToIdentity = instance().stringToIdentity;
auto& identityToString = instance().identityToString;
std::unique_lock<std::shared_mutex> writerLock(sharedMutex);
assert(stringToIdentity.size() == identityToString.size());
if (const auto it = stringToIdentity.find(string); it == stringToIdentity.end()) {
// this writer to insert
auto& buffer = instance().buffer;
const auto previousCapacity = buffer.capacity();
const StringIdentity id = identityToString.size();
@@ -67,30 +60,32 @@ StringIdentity StringIndexer::get(std::string_view string) {
}
std::string StringIndexer::get(const StringIdentity id) {
std::shared_lock<std::shared_mutex> readerLock(instance().sharedMutex);
std::shared_lock<std::shared_mutex> readerLock(sharedMutex);
const auto& identityToString = instance().identityToString;
assert(id < identityToString.size());
return id < identityToString.size() ? (instance().buffer.data() + identityToString[id]) : empty;
return id < identityToString.size() ? (buffer.data() + identityToString[id]) : empty;
}
void StringIndexer::clear() {
std::unique_lock<std::shared_mutex> writerLock(instance().sharedMutex);
std::unique_lock<std::shared_mutex> writerLock(sharedMutex);
instance().stringToIdentity.clear();
instance().identityToString.clear();
instance().buffer.clear();
stringToIdentity.clear();
identityToString.clear();
buffer.clear();
}
size_t StringIndexer::size() {
std::shared_lock<std::shared_mutex> readerLock(instance().sharedMutex);
std::shared_lock<std::shared_mutex> readerLock(sharedMutex);
[[maybe_unused]] auto& stringToIdentity = instance().stringToIdentity;
auto& identityToString = instance().identityToString;
assert(stringToIdentity.size() == identityToString.size());
return identityToString.size();
}
StringIndexer& stringIndexer() {
static StringIndexer inst;
return inst;
}
} // namespace mbgl

View File

@@ -7,46 +7,46 @@
using namespace mbgl;
TEST(StringIndexer, AddStrings) {
StringIndexer::clear();
EXPECT_EQ(StringIndexer::size(), 0);
StringIndexer strIndexer;
EXPECT_EQ(strIndexer.size(), 0);
const auto id1 = StringIndexer::get("test string1");
const auto id1 = strIndexer.get("test string1");
EXPECT_EQ(id1, 0);
EXPECT_EQ(StringIndexer::size(), 1);
EXPECT_EQ(strIndexer.size(), 1);
const std::string_view s2 = "test string2";
const auto id2 = StringIndexer::get(s2);
const auto id2 = strIndexer.get(s2);
EXPECT_EQ(id2, 1);
EXPECT_EQ(StringIndexer::size(), 2);
EXPECT_EQ(strIndexer.size(), 2);
}
TEST(StringIndexer, GetString) {
StringIndexer::clear();
EXPECT_EQ(StringIndexer::size(), 0);
StringIndexer strIndexer;
EXPECT_EQ(strIndexer.size(), 0);
constexpr auto str = "test string1";
const auto id1 = StringIndexer::get(str);
const auto id1 = strIndexer.get(str);
EXPECT_EQ(id1, 0);
EXPECT_EQ(StringIndexer::size(), 1);
EXPECT_EQ(strIndexer.size(), 1);
const auto str1 = StringIndexer::get(id1);
const auto str1 = strIndexer.get(id1);
EXPECT_EQ(str, str1);
const auto str2 = str;
const auto id2 = StringIndexer::get(str2);
const auto id2 = strIndexer.get(str2);
EXPECT_EQ(id1, id2);
}
TEST(StringIndexer, Reallocate) {
StringIndexer::clear();
EXPECT_EQ(StringIndexer::size(), 0);
StringIndexer strIndexer;
EXPECT_EQ(strIndexer.size(), 0);
constexpr auto str = "reallocate test string1";
const auto id1 = StringIndexer::get(str);
const auto id1 = strIndexer.get(str);
EXPECT_EQ(id1, 0);
EXPECT_EQ(StringIndexer::size(), 1);
EXPECT_EQ(strIndexer.size(), 1);
// force reallocation. assume capacity: 100 strings, 100 * 32 bytes buffer space
constexpr auto N = 1000;
@@ -55,31 +55,31 @@ TEST(StringIndexer, Reallocate) {
return "1234567890"s + "1234567890"s + "1234567890"s + "---" + std::to_string(i);
};
for (auto i = 1; i < N; ++i) {
StringIndexer::get(string_for_i(i));
strIndexer.get(string_for_i(i));
}
EXPECT_EQ(StringIndexer::size(), N);
EXPECT_EQ(strIndexer.size(), N);
const auto str1 = StringIndexer::get(id1);
const auto str1 = strIndexer.get(id1);
EXPECT_EQ(str, str1);
for (auto i = 1; i < N; ++i) {
const auto strN1 = StringIndexer::get(i);
const auto strN1 = strIndexer.get(i);
EXPECT_EQ(strN1, string_for_i(i));
}
}
TEST(StringIndexer, GetOOBIdentity) {
StringIndexer::clear();
EXPECT_EQ(StringIndexer::size(), 0);
StringIndexer strIndexer;
EXPECT_EQ(strIndexer.size(), 0);
std::string str;
#ifndef NDEBUG
EXPECT_DEATH_IF_SUPPORTED(str = StringIndexer::get(42), "id < identityToString.size()");
EXPECT_DEATH_IF_SUPPORTED(str = strIndexer.get(42), "id < identityToString.size()");
#endif
EXPECT_TRUE(str.empty());
#ifndef NDEBUG
EXPECT_DEATH_IF_SUPPORTED(str = StringIndexer::get(-1), "id < identityToString.size()");
EXPECT_DEATH_IF_SUPPORTED(str = strIndexer.get(-1), "id < identityToString.size()");
#endif
EXPECT_TRUE(str.empty());
}