Line layer UBO improvements (#1946)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Alex Cristici
2023-12-12 19:32:20 +02:00
committed by GitHub
parent dda4e4043c
commit 9fbcb031f0
22 changed files with 214 additions and 163 deletions

View File

@@ -33,6 +33,7 @@ static const StringIdentity idLinePatternUBOName = stringIndexer().get("LinePatt
static const StringIdentity idLinePatternPropertiesUBOName = stringIndexer().get("LinePatternPropertiesUBO");
static const StringIdentity idLineSDFUBOName = stringIndexer().get("LineSDFUBO");
static const StringIdentity idLineSDFPropertiesUBOName = stringIndexer().get("LineSDFPropertiesUBO");
static const StringIdentity idLineDynamicUBOName = stringIndexer().get("LineDynamicUBO");
static const StringIdentity idTexImageName = stringIndexer().get("u_image");
void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& parameters) {
@@ -107,6 +108,15 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
return lineSDFPropertiesBuffer;
};
const LineDynamicUBO dynamicUBO = {
/*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, 0, 0};
if (!dynamicBuffer) {
dynamicBuffer = parameters.context.createUniformBuffer(&dynamicUBO, sizeof(dynamicUBO));
} else {
dynamicBuffer->update(&dynamicUBO, sizeof(dynamicUBO));
}
visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
const auto shader = drawable.getShader();
if (!drawable.getTileID() || !shader || !checkTweakDrawable(drawable)) {
@@ -122,15 +132,16 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
const auto matrix = getTileMatrix(tileID, parameters, translation, anchor, nearClipped, inViewportPixelUnits);
const LineType type = static_cast<LineType>(drawable.getType());
uniforms.addOrReplace(idLineDynamicUBOName, dynamicBuffer);
const LineType type = static_cast<LineType>(drawable.getType());
switch (type) {
case LineType::Simple: {
const LineUBO lineUBO{
/*matrix = */ util::cast<float>(matrix),
/*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]},
/*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, static_cast<float>(zoom)),
0};
const LineUBO lineUBO{/*matrix = */ util::cast<float>(matrix),
/*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, static_cast<float>(zoom)),
0,
0,
0};
uniforms.createOrUpdate(idLineUBOName, &lineUBO, context);
// properties UBO
@@ -140,8 +151,9 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
case LineType::Gradient: {
const LineGradientUBO lineGradientUBO{
/*matrix = */ util::cast<float>(matrix),
/*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]},
/*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, static_cast<float>(zoom)),
0,
0,
0};
uniforms.createOrUpdate(idLineGradientUBOName, &lineGradientUBO, context);
@@ -158,16 +170,14 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
}
const LinePatternUBO linePatternUBO{
/*matrix =*/util::cast<float>(matrix),
/*scale =*/
{parameters.pixelRatio,
1 / tileID.pixelsToTileUnits(1, parameters.state.getIntegerZoom()),
crossfade.fromScale,
crossfade.toScale},
/*texsize =*/{static_cast<float>(textureSize.width), static_cast<float>(textureSize.height)},
/*units_to_pixels =*/{1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]},
/*ratio =*/1.0f / tileID.pixelsToTileUnits(1.0f, static_cast<float>(zoom)),
/*fade =*/crossfade.t,
0,
0};
/*fade =*/crossfade.t};
uniforms.createOrUpdate(idLinePatternUBOName, &linePatternUBO, context);
// properties UBO
@@ -200,19 +210,20 @@ void LineLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters
const float widthB = posB.width * crossfade.toScale;
const LineSDFUBO lineSDFUBO{
/* matrix = */ util::cast<float>(matrix),
{1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]},
/* patternscale_a = */
{1.0f / tileID.pixelsToTileUnits(widthA, parameters.state.getIntegerZoom()),
-posA.height / 2.0f},
/* patternscale_b = */
{1.0f / tileID.pixelsToTileUnits(widthB, parameters.state.getIntegerZoom()),
-posB.height / 2.0f},
/* ratio = */ 1.0f /
tileID.pixelsToTileUnits(1.0f, static_cast<float>(parameters.state.getZoom())),
/* ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, static_cast<float>(zoom)),
/* tex_y_a = */ posA.y,
/* tex_y_b = */ posB.y,
/* sdfgamma = */ static_cast<float>(dashPatternTexture.getSize().width) /
(std::min(widthA, widthB) * 256.0f * parameters.pixelRatio) / 2.0f,
/* mix = */ crossfade.t,
0,
0,
0};
uniforms.createOrUpdate(idLineSDFUBOName, &lineSDFUBO, context);

View File

@@ -35,6 +35,7 @@ protected:
gfx::UniformBufferPtr lineGradientPropertiesBuffer;
gfx::UniformBufferPtr linePatternPropertiesBuffer;
gfx::UniformBufferPtr lineSDFPropertiesBuffer;
gfx::UniformBufferPtr dynamicBuffer;
#if MLN_RENDER_BACKEND_METAL
gfx::UniformBufferPtr permutationUniformBuffer;

View File

@@ -210,13 +210,19 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr
const auto matrix = LayerTweaker::getTileMatrix(
tileID, parameters, {{0, 0}}, style::TranslateAnchorType::Viewport, false, false, false);
static const StringIdentity idLineUBOName = stringIndexer().get("LineUBO");
const shaders::LineUBO lineUBO{
/*matrix = */ util::cast<float>(matrix),
static const StringIdentity idLineDynamicUBOName = stringIndexer().get("LineDynamicUBO");
const shaders::LineDynamicUBO dynamicUBO = {
/*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]},
/*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom),
0,
0};
static const StringIdentity idLineUBOName = stringIndexer().get("LineUBO");
const shaders::LineUBO lineUBO{/*matrix = */ util::cast<float>(matrix),
/*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom),
0,
0,
0};
static const StringIdentity idLinePropertiesUBOName = stringIndexer().get("LinePropertiesUBO");
static const StringIdentity idLineInterpolationUBOName = stringIndexer().get("LineInterpolationUBO");
@@ -229,6 +235,7 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr
0,
0};
auto& uniforms = drawable.mutableUniformBuffers();
uniforms.createOrUpdate(idLineDynamicUBOName, &dynamicUBO, parameters.context);
uniforms.createOrUpdate(idLineUBOName, &lineUBO, parameters.context);
uniforms.createOrUpdate(idLinePropertiesUBOName, &linePropertiesUBO, parameters.context);
uniforms.createOrUpdate(idLineInterpolationUBOName, &lineInterpolationUBO, parameters.context);

View File

@@ -13,10 +13,11 @@ const std::array<AttributeInfo, 8> ShaderSource<BuiltIn::LineShader, gfx::Backen
AttributeInfo{6, gfx::AttributeDataType::Float2, 1, "a_offset"},
AttributeInfo{7, gfx::AttributeDataType::Float2, 1, "a_width"},
};
const std::array<UniformBlockInfo, 3> ShaderSource<BuiltIn::LineShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{8, true, true, sizeof(LineUBO), "LineUBO"},
UniformBlockInfo{9, true, true, sizeof(LinePropertiesUBO), "LinePropertiesUBO"},
UniformBlockInfo{10, true, false, sizeof(LineInterpolationUBO), "LineInterpolationUBO"},
const std::array<UniformBlockInfo, 4> ShaderSource<BuiltIn::LineShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{8, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"},
UniformBlockInfo{9, true, true, sizeof(LineUBO), "LineUBO"},
UniformBlockInfo{10, true, true, sizeof(LinePropertiesUBO), "LinePropertiesUBO"},
UniformBlockInfo{11, true, false, sizeof(LineInterpolationUBO), "LineInterpolationUBO"},
};
const std::array<TextureInfo, 0> ShaderSource<BuiltIn::LineShader, gfx::Backend::Type::Metal>::textures = {};
@@ -31,11 +32,12 @@ const std::array<AttributeInfo, 9> ShaderSource<BuiltIn::LinePatternShader, gfx:
AttributeInfo{7, gfx::AttributeDataType::UShort4, 1, "a_pattern_from"},
AttributeInfo{8, gfx::AttributeDataType::UShort4, 1, "a_pattern_to"},
};
const std::array<UniformBlockInfo, 4> ShaderSource<BuiltIn::LinePatternShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{9, true, true, sizeof(LinePatternUBO), "LinePatternUBO"},
UniformBlockInfo{10, true, true, sizeof(LinePatternPropertiesUBO), "LinePatternPropertiesUBO"},
UniformBlockInfo{11, true, false, sizeof(LinePatternInterpolationUBO), "LinePatternInterpolationUBO"},
UniformBlockInfo{12, true, true, sizeof(LinePatternTilePropertiesUBO), "LinePatternTilePropertiesUBO"},
const std::array<UniformBlockInfo, 5> ShaderSource<BuiltIn::LinePatternShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{9, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"},
UniformBlockInfo{10, true, true, sizeof(LinePatternUBO), "LinePatternUBO"},
UniformBlockInfo{11, true, true, sizeof(LinePatternPropertiesUBO), "LinePatternPropertiesUBO"},
UniformBlockInfo{12, true, false, sizeof(LinePatternInterpolationUBO), "LinePatternInterpolationUBO"},
UniformBlockInfo{13, true, true, sizeof(LinePatternTilePropertiesUBO), "LinePatternTilePropertiesUBO"},
};
const std::array<TextureInfo, 1> ShaderSource<BuiltIn::LinePatternShader, gfx::Backend::Type::Metal>::textures = {
TextureInfo{0, "u_image"},
@@ -52,10 +54,11 @@ const std::array<AttributeInfo, 9> ShaderSource<BuiltIn::LineSDFShader, gfx::Bac
AttributeInfo{7, gfx::AttributeDataType::Float2, 1, "a_width"},
AttributeInfo{8, gfx::AttributeDataType::Float2, 1, "a_floorwidth"},
};
const std::array<UniformBlockInfo, 3> ShaderSource<BuiltIn::LineSDFShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{9, true, true, sizeof(LineSDFUBO), "LineSDFUBO"},
UniformBlockInfo{10, true, true, sizeof(LineSDFPropertiesUBO), "LineSDFPropertiesUBO"},
UniformBlockInfo{11, true, false, sizeof(LineSDFInterpolationUBO), "LineSDFInterpolationUBO"},
const std::array<UniformBlockInfo, 4> ShaderSource<BuiltIn::LineSDFShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{9, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"},
UniformBlockInfo{10, true, true, sizeof(LineSDFUBO), "LineSDFUBO"},
UniformBlockInfo{11, true, true, sizeof(LineSDFPropertiesUBO), "LineSDFPropertiesUBO"},
UniformBlockInfo{12, true, false, sizeof(LineSDFInterpolationUBO), "LineSDFInterpolationUBO"},
};
const std::array<TextureInfo, 1> ShaderSource<BuiltIn::LineSDFShader, gfx::Backend::Type::Metal>::textures = {
TextureInfo{0, "u_image"},

View File

@@ -12,10 +12,11 @@ const std::array<AttributeInfo, 7> ShaderSource<BuiltIn::LineGradientShader, gfx
AttributeInfo{5, gfx::AttributeDataType::Float2, 1, "a_offset"},
AttributeInfo{6, gfx::AttributeDataType::Float2, 1, "a_width"},
};
const std::array<UniformBlockInfo, 3> ShaderSource<BuiltIn::LineGradientShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{7, true, true, sizeof(LineGradientUBO), "LineGradientUBO"},
UniformBlockInfo{8, true, true, sizeof(LineGradientPropertiesUBO), "LineGradientPropertiesUBO"},
UniformBlockInfo{9, true, false, sizeof(LineGradientInterpolationUBO), "LineGradientInterpolationUBO"},
const std::array<UniformBlockInfo, 4> ShaderSource<BuiltIn::LineGradientShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{7, true, false, sizeof(LineGradientUBO), "LineDynamicUBO"},
UniformBlockInfo{8, true, true, sizeof(LineGradientUBO), "LineGradientUBO"},
UniformBlockInfo{9, true, true, sizeof(LineGradientPropertiesUBO), "LineGradientPropertiesUBO"},
UniformBlockInfo{10, true, false, sizeof(LineGradientInterpolationUBO), "LineGradientInterpolationUBO"},
};
const std::array<TextureInfo, 1> ShaderSource<BuiltIn::LineGradientShader, gfx::Backend::Type::Metal>::textures = {
TextureInfo{0, "u_image"},

View File

@@ -97,12 +97,16 @@ public:
const auto matrix = LayerTweaker::getTileMatrix(
tileID, parameters, {{0, 0}}, style::TranslateAnchorType::Viewport, false, false, false);
static const StringIdentity idLineDynamicUBOName = stringIndexer().get("LineDynamicUBO");
const shaders::LineDynamicUBO dynamicUBO = {
/*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]}, 0, 0};
static const StringIdentity idLineUBOName = stringIndexer().get("LineUBO");
const shaders::LineUBO lineUBO{
/*matrix = */ util::cast<float>(matrix),
/*units_to_pixels = */ {1.0f / parameters.pixelsToGLUnits[0], 1.0f / parameters.pixelsToGLUnits[1]},
/*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom),
0};
const shaders::LineUBO lineUBO{/*matrix = */ util::cast<float>(matrix),
/*ratio = */ 1.0f / tileID.pixelsToTileUnits(1.0f, zoom),
0,
0,
0};
static const StringIdentity idLinePropertiesUBOName = stringIndexer().get("LinePropertiesUBO");
@@ -116,6 +120,7 @@ public:
0,
0};
auto& uniforms = drawable.mutableUniformBuffers();
uniforms.createOrUpdate(idLineDynamicUBOName, &dynamicUBO, parameters.context);
uniforms.createOrUpdate(idLineUBOName, &lineUBO, parameters.context);
uniforms.createOrUpdate(idLinePropertiesUBOName, &linePropertiesUBO, parameters.context);
uniforms.createOrUpdate(idLineInterpolationUBOName, &lineInterpolationUBO, parameters.context);