Skip to content

Commit

Permalink
UBO fixes (#2008)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
alexcristici and pre-commit-ci[bot] committed Jan 11, 2024
1 parent 43e52d0 commit d332088
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 53 deletions.
1 change: 1 addition & 0 deletions shaders/drawable.fill_outline.vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
layout (std140) uniform FillOutlineDrawableUBO {
highp mat4 u_matrix;
highp vec2 u_world;
highp vec2 pad;
};
layout (std140) uniform FillOutlineEvaluatedPropsUBO {
highp vec4 u_outline_color;
Expand Down
140 changes: 91 additions & 49 deletions src/mbgl/renderer/layers/render_line_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,50 +485,92 @@ void RenderLineLayer::update(gfx::ShaderRegistry& shaders,

// interpolation UBOs
const float zoom = static_cast<float>(state.getZoom());
const LineInterpolationUBO lineInterpolationUBO{
/*color_t =*/std::get<0>(paintPropertyBinders.get<LineColor>()->interpolationFactor(zoom)),
/*blur_t =*/std::get<0>(paintPropertyBinders.get<LineBlur>()->interpolationFactor(zoom)),
/*opacity_t =*/std::get<0>(paintPropertyBinders.get<LineOpacity>()->interpolationFactor(zoom)),
/*gapwidth_t =*/std::get<0>(paintPropertyBinders.get<LineGapWidth>()->interpolationFactor(zoom)),
/*offset_t =*/std::get<0>(paintPropertyBinders.get<LineOffset>()->interpolationFactor(zoom)),
/*width_t =*/std::get<0>(paintPropertyBinders.get<LineWidth>()->interpolationFactor(zoom)),
0,
0};
const LineGradientInterpolationUBO lineGradientInterpolationUBO{
/*blur_t =*/std::get<0>(paintPropertyBinders.get<LineBlur>()->interpolationFactor(zoom)),
/*opacity_t =*/std::get<0>(paintPropertyBinders.get<LineOpacity>()->interpolationFactor(zoom)),
/*gapwidth_t =*/std::get<0>(paintPropertyBinders.get<LineGapWidth>()->interpolationFactor(zoom)),
/*offset_t =*/std::get<0>(paintPropertyBinders.get<LineOffset>()->interpolationFactor(zoom)),
/*width_t =*/std::get<0>(paintPropertyBinders.get<LineWidth>()->interpolationFactor(zoom)),
0,
0,
0};
const LinePatternInterpolationUBO linePatternInterpolationUBO{
/*blur_t =*/std::get<0>(paintPropertyBinders.get<LineBlur>()->interpolationFactor(zoom)),
/*opacity_t =*/std::get<0>(paintPropertyBinders.get<LineOpacity>()->interpolationFactor(zoom)),
/*offset_t =*/std::get<0>(paintPropertyBinders.get<LineOffset>()->interpolationFactor(zoom)),
/*gapwidth_t =*/std::get<0>(paintPropertyBinders.get<LineGapWidth>()->interpolationFactor(zoom)),
/*width_t =*/std::get<0>(paintPropertyBinders.get<LineWidth>()->interpolationFactor(zoom)),
/*pattern_from_t =*/std::get<0>(paintPropertyBinders.get<LinePattern>()->interpolationFactor(zoom)),
/*pattern_to_t =*/std::get<1>(paintPropertyBinders.get<LinePattern>()->interpolationFactor(zoom)),
0};
const LineSDFInterpolationUBO lineSDFInterpolationUBO{
/*color_t =*/std::get<0>(paintPropertyBinders.get<LineColor>()->interpolationFactor(zoom)),
/*blur_t =*/std::get<0>(paintPropertyBinders.get<LineBlur>()->interpolationFactor(zoom)),
/*opacity_t =*/std::get<0>(paintPropertyBinders.get<LineOpacity>()->interpolationFactor(zoom)),
/*gapwidth_t =*/std::get<0>(paintPropertyBinders.get<LineGapWidth>()->interpolationFactor(zoom)),
/*offset_t =*/std::get<0>(paintPropertyBinders.get<LineOffset>()->interpolationFactor(zoom)),
/*width_t =*/std::get<0>(paintPropertyBinders.get<LineWidth>()->interpolationFactor(zoom)),
/*floorwidth_t =*/std::get<0>(paintPropertyBinders.get<LineFloorWidth>()->interpolationFactor(zoom)),
0};

std::optional<LineInterpolationUBO> lineInterpolationUBO = std::nullopt;
std::optional<LineGradientInterpolationUBO> lineGradientInterpolationUBO = std::nullopt;
std::optional<LinePatternInterpolationUBO> linePatternInterpolationUBO = std::nullopt;
std::optional<LineSDFInterpolationUBO> lineSDFInterpolationUBO = std::nullopt;

auto getLineInterpolationUBO = [&]() -> const LineInterpolationUBO& {
if (!lineInterpolationUBO) {
lineInterpolationUBO = {
/*color_t =*/std::get<0>(paintPropertyBinders.get<LineColor>()->interpolationFactor(zoom)),
/*blur_t =*/std::get<0>(paintPropertyBinders.get<LineBlur>()->interpolationFactor(zoom)),
/*opacity_t =*/std::get<0>(paintPropertyBinders.get<LineOpacity>()->interpolationFactor(zoom)),
/*gapwidth_t =*/std::get<0>(paintPropertyBinders.get<LineGapWidth>()->interpolationFactor(zoom)),
/*offset_t =*/std::get<0>(paintPropertyBinders.get<LineOffset>()->interpolationFactor(zoom)),
/*width_t =*/std::get<0>(paintPropertyBinders.get<LineWidth>()->interpolationFactor(zoom)),
0,
0};
}

return *lineInterpolationUBO;
};

auto getLineGradientInterpolationUBO = [&]() -> const LineGradientInterpolationUBO& {
if (!lineGradientInterpolationUBO) {
lineGradientInterpolationUBO = {
/*blur_t =*/std::get<0>(paintPropertyBinders.get<LineBlur>()->interpolationFactor(zoom)),
/*opacity_t =*/std::get<0>(paintPropertyBinders.get<LineOpacity>()->interpolationFactor(zoom)),
/*gapwidth_t =*/std::get<0>(paintPropertyBinders.get<LineGapWidth>()->interpolationFactor(zoom)),
/*offset_t =*/std::get<0>(paintPropertyBinders.get<LineOffset>()->interpolationFactor(zoom)),
/*width_t =*/std::get<0>(paintPropertyBinders.get<LineWidth>()->interpolationFactor(zoom)),
0,
0,
0};
}

return *lineGradientInterpolationUBO;
};

auto getLinePatternInterpolationUBO = [&]() -> const LinePatternInterpolationUBO& {
if (!linePatternInterpolationUBO) {
linePatternInterpolationUBO = {
/*blur_t =*/std::get<0>(paintPropertyBinders.get<LineBlur>()->interpolationFactor(zoom)),
/*opacity_t =*/std::get<0>(paintPropertyBinders.get<LineOpacity>()->interpolationFactor(zoom)),
/*offset_t =*/std::get<0>(paintPropertyBinders.get<LineOffset>()->interpolationFactor(zoom)),
/*gapwidth_t =*/std::get<0>(paintPropertyBinders.get<LineGapWidth>()->interpolationFactor(zoom)),
/*width_t =*/std::get<0>(paintPropertyBinders.get<LineWidth>()->interpolationFactor(zoom)),
/*pattern_from_t =*/std::get<0>(paintPropertyBinders.get<LinePattern>()->interpolationFactor(zoom)),
/*pattern_to_t =*/std::get<1>(paintPropertyBinders.get<LinePattern>()->interpolationFactor(zoom)),
0};
}

return *linePatternInterpolationUBO;
};

auto getLineSDFInterpolationUBO = [&]() -> const LineSDFInterpolationUBO& {
if (!lineSDFInterpolationUBO) {
lineSDFInterpolationUBO = {
/*color_t =*/std::get<0>(paintPropertyBinders.get<LineColor>()->interpolationFactor(zoom)),
/*blur_t =*/std::get<0>(paintPropertyBinders.get<LineBlur>()->interpolationFactor(zoom)),
/*opacity_t =*/std::get<0>(paintPropertyBinders.get<LineOpacity>()->interpolationFactor(zoom)),
/*gapwidth_t =*/std::get<0>(paintPropertyBinders.get<LineGapWidth>()->interpolationFactor(zoom)),
/*offset_t =*/std::get<0>(paintPropertyBinders.get<LineOffset>()->interpolationFactor(zoom)),
/*width_t =*/std::get<0>(paintPropertyBinders.get<LineWidth>()->interpolationFactor(zoom)),
/*floorwidth_t =*/
std::get<0>(paintPropertyBinders.get<LineFloorWidth>()->interpolationFactor(zoom)),
0};
}

return *lineSDFInterpolationUBO;
};

// tile dependent properties UBOs:
std::optional<LinePatternTilePropertiesUBO> linePatternTilePropertiesUBO = std::nullopt;
const auto& linePatternValue = evaluated.get<LinePattern>().constantOr(Faded<expression::Image>{"", ""});
const std::optional<ImagePosition> patternPosA = tile.getPattern(linePatternValue.from.id());
const std::optional<ImagePosition> patternPosB = tile.getPattern(linePatternValue.to.id());
const LinePatternTilePropertiesUBO linePatternTilePropertiesUBO{
/*pattern_from =*/patternPosA ? util::cast<float>(patternPosA->tlbr()) : std::array<float, 4>{0},
/*pattern_to =*/patternPosB ? util::cast<float>(patternPosB->tlbr()) : std::array<float, 4>{0}};

auto getLinePatternTilePropertiesUBO = [&]() -> const LinePatternTilePropertiesUBO& {
if (!linePatternTilePropertiesUBO) {
linePatternTilePropertiesUBO = {
/*pattern_from =*/patternPosA ? util::cast<float>(patternPosA->tlbr()) : std::array<float, 4>{0},
/*pattern_to =*/patternPosB ? util::cast<float>(patternPosB->tlbr()) : std::array<float, 4>{0}};
}

return *linePatternTilePropertiesUBO;
};

auto updateExisting = [&](gfx::Drawable& drawable) {
if (drawable.getLayerTweaker() != layerTweaker) {
Expand All @@ -542,25 +584,25 @@ void RenderLineLayer::update(gfx::ShaderRegistry& shaders,

// simple line interpolation UBO
if (shaderUniforms.get(idLineInterpolationUBOName)) {
drawableUniforms.createOrUpdate(idLineInterpolationUBOName, &lineInterpolationUBO, context);
drawableUniforms.createOrUpdate(idLineInterpolationUBOName, &getLineInterpolationUBO(), context);
}
// gradient line interpolation UBO
else if (shaderUniforms.get(idLineGradientInterpolationUBOName)) {
drawableUniforms.createOrUpdate(
idLineGradientInterpolationUBOName, &lineGradientInterpolationUBO, context);
idLineGradientInterpolationUBOName, &getLineGradientInterpolationUBO(), context);
}
// pattern line interpolation UBO
else if (shaderUniforms.get(idLinePatternInterpolationUBOName)) {
// interpolation
drawableUniforms.createOrUpdate(
idLinePatternInterpolationUBOName, &linePatternInterpolationUBO, context);
idLinePatternInterpolationUBOName, &getLinePatternInterpolationUBO(), context);
// tile properties
drawableUniforms.createOrUpdate(
idLinePatternTilePropertiesUBOName, &linePatternTilePropertiesUBO, context);
idLinePatternTilePropertiesUBOName, &getLinePatternTilePropertiesUBO(), context);
}
// SDF line interpolation UBO
else if (shaderUniforms.get(idLineSDFInterpolationUBOName)) {
drawableUniforms.createOrUpdate(idLineSDFInterpolationUBOName, &lineSDFInterpolationUBO, context);
drawableUniforms.createOrUpdate(idLineSDFInterpolationUBOName, &getLineSDFInterpolationUBO(), context);
}

// TODO: vertex attributes or `propertiesAsUniforms` updated, is that needed?
Expand Down Expand Up @@ -608,7 +650,7 @@ void RenderLineLayer::update(gfx::ShaderRegistry& shaders,
drawable->setLayerTweaker(layerTweaker);
drawable->setData(std::make_unique<gfx::LineDrawableData>(cap));
drawable->mutableUniformBuffers().createOrUpdate(
idLineSDFInterpolationUBOName, &lineSDFInterpolationUBO, context);
idLineSDFInterpolationUBOName, &getLineSDFInterpolationUBO(), context);

tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable));
++stats.drawablesAdded;
Expand Down Expand Up @@ -665,9 +707,9 @@ void RenderLineLayer::update(gfx::ShaderRegistry& shaders,
drawable->setTileID(tileID);
drawable->setLayerTweaker(layerTweaker);
drawable->mutableUniformBuffers().createOrUpdate(
idLinePatternInterpolationUBOName, &linePatternInterpolationUBO, context);
idLinePatternInterpolationUBOName, &getLinePatternInterpolationUBO(), context);
drawable->mutableUniformBuffers().createOrUpdate(
idLinePatternTilePropertiesUBOName, &linePatternTilePropertiesUBO, context);
idLinePatternTilePropertiesUBOName, &getLinePatternTilePropertiesUBO(), context);

tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable));
++stats.drawablesAdded;
Expand Down Expand Up @@ -717,7 +759,7 @@ void RenderLineLayer::update(gfx::ShaderRegistry& shaders,
drawable->setTileID(tileID);
drawable->setLayerTweaker(layerTweaker);
drawable->mutableUniformBuffers().createOrUpdate(
idLineGradientInterpolationUBOName, &lineGradientInterpolationUBO, context);
idLineGradientInterpolationUBOName, &getLineGradientInterpolationUBO(), context);

tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable));
++stats.drawablesAdded;
Expand Down Expand Up @@ -754,7 +796,7 @@ void RenderLineLayer::update(gfx::ShaderRegistry& shaders,
drawable->setTileID(tileID);
drawable->setLayerTweaker(layerTweaker);
drawable->mutableUniformBuffers().createOrUpdate(
idLineInterpolationUBOName, &lineInterpolationUBO, context);
idLineInterpolationUBOName, &getLineInterpolationUBO(), context);

tileLayerGroup->addDrawable(renderPass, tileID, std::move(drawable));
++stats.drawablesAdded;
Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/shaders/mtl/line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const std::array<AttributeInfo, 8> ShaderSource<BuiltIn::LineShader, gfx::Backen
AttributeInfo{7, gfx::AttributeDataType::Float2, 1, "a_width"},
};
const std::array<UniformBlockInfo, 4> ShaderSource<BuiltIn::LineShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{8, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"},
UniformBlockInfo{8, true, false, sizeof(LineDynamicUBO), "LineDynamicUBO"},
UniformBlockInfo{9, true, true, sizeof(LineUBO), "LineUBO"},
UniformBlockInfo{10, true, true, sizeof(LinePropertiesUBO), "LinePropertiesUBO"},
UniformBlockInfo{11, true, false, sizeof(LineInterpolationUBO), "LineInterpolationUBO"},
Expand All @@ -33,7 +33,7 @@ const std::array<AttributeInfo, 9> ShaderSource<BuiltIn::LinePatternShader, gfx:
AttributeInfo{8, gfx::AttributeDataType::UShort4, 1, "a_pattern_to"},
};
const std::array<UniformBlockInfo, 5> ShaderSource<BuiltIn::LinePatternShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{9, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"},
UniformBlockInfo{9, true, false, sizeof(LineDynamicUBO), "LineDynamicUBO"},
UniformBlockInfo{10, true, true, sizeof(LinePatternUBO), "LinePatternUBO"},
UniformBlockInfo{11, true, true, sizeof(LinePatternPropertiesUBO), "LinePatternPropertiesUBO"},
UniformBlockInfo{12, true, false, sizeof(LinePatternInterpolationUBO), "LinePatternInterpolationUBO"},
Expand All @@ -55,7 +55,7 @@ const std::array<AttributeInfo, 9> ShaderSource<BuiltIn::LineSDFShader, gfx::Bac
AttributeInfo{8, gfx::AttributeDataType::Float2, 1, "a_floorwidth"},
};
const std::array<UniformBlockInfo, 4> ShaderSource<BuiltIn::LineSDFShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{9, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"},
UniformBlockInfo{9, true, false, sizeof(LineDynamicUBO), "LineDynamicUBO"},
UniformBlockInfo{10, true, true, sizeof(LineSDFUBO), "LineSDFUBO"},
UniformBlockInfo{11, true, true, sizeof(LineSDFPropertiesUBO), "LineSDFPropertiesUBO"},
UniformBlockInfo{12, true, false, sizeof(LineSDFInterpolationUBO), "LineSDFInterpolationUBO"},
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/shaders/mtl/line_gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const std::array<AttributeInfo, 7> ShaderSource<BuiltIn::LineGradientShader, gfx
AttributeInfo{6, gfx::AttributeDataType::Float2, 1, "a_width"},
};
const std::array<UniformBlockInfo, 4> ShaderSource<BuiltIn::LineGradientShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{7, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"},
UniformBlockInfo{7, true, false, sizeof(LineDynamicUBO), "LineDynamicUBO"},
UniformBlockInfo{8, true, true, sizeof(LineGradientUBO), "LineGradientUBO"},
UniformBlockInfo{9, true, true, sizeof(LineGradientPropertiesUBO), "LineGradientPropertiesUBO"},
UniformBlockInfo{10, true, false, sizeof(LineGradientInterpolationUBO), "LineGradientInterpolationUBO"},
Expand Down

0 comments on commit d332088

Please sign in to comment.