Metal setup merge (#1616)
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Alex Cristici <alex.cristici@gmail.com> Co-authored-by: mwilsnd <mwilsnd@gmail.com> Co-authored-by: Stefan Karschti <stefan.karschti@gmail.com> Co-authored-by: Bart Louwers <bart@emeel.net> Co-authored-by: Alex Cristici <alex@skoon.energy> Co-authored-by: Bart Louwers <bart.louwers@gmail.com> Co-authored-by: Wei Chen <alanchenboy@gmail.com> Co-authored-by: Siarhei Fedartsou <siarhei.fedartsou@gmail.com> Co-authored-by: tdcosta100 <tdcosta100@gmail.com>
This commit is contained in:
@@ -8,38 +8,21 @@
|
||||
#include <mbgl/renderer/render_tree.hpp>
|
||||
#include <mbgl/renderer/paint_parameters.hpp>
|
||||
#include <mbgl/renderer/paint_property_binder.hpp>
|
||||
#include <mbgl/shaders/shader_source.hpp>
|
||||
#include <mbgl/shaders/shader_program_base.hpp>
|
||||
#include <mbgl/shaders/fill_layer_ubo.hpp>
|
||||
#include <mbgl/style/layers/fill_layer_properties.hpp>
|
||||
#include <mbgl/util/convert.hpp>
|
||||
#include <mbgl/util/std.hpp>
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
#include <mbgl/shaders/mtl/fill.hpp>
|
||||
#endif
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
using namespace style;
|
||||
|
||||
struct alignas(16) FillDrawableUBO {
|
||||
/* 0 */ std::array<float, 4 * 4> matrix;
|
||||
/* 64 */ std::array<float, 4> scale;
|
||||
/* 80 */ std::array<float, 2> world;
|
||||
/* 88 */ std::array<float, 2> pixel_coord_upper;
|
||||
/* 96 */ std::array<float, 2> pixel_coord_lower;
|
||||
/* 104 */ std::array<float, 2> texsize;
|
||||
/* 112 */
|
||||
};
|
||||
static_assert(sizeof(FillDrawableUBO) == 112);
|
||||
|
||||
/// Evaluated properties that do not depend on the tile
|
||||
struct alignas(16) FillDrawablePropsUBO {
|
||||
/* 0 */ Color color;
|
||||
/* 16 */ Color outline_color;
|
||||
/* 32 */ float opacity;
|
||||
/* 36 */ float pad1, pad2, pad3;
|
||||
/* 48 */
|
||||
};
|
||||
static_assert(sizeof(FillDrawablePropsUBO) == 48);
|
||||
|
||||
static constexpr std::string_view FillDrawableUBOName = "FillDrawableUBO";
|
||||
static constexpr std::string_view FillDrawablePropsUBOName = "FillDrawablePropsUBO";
|
||||
using namespace shaders;
|
||||
|
||||
void FillLayerTweaker::execute(LayerGroupBase& layerGroup,
|
||||
const RenderTree& renderTree,
|
||||
@@ -58,24 +41,186 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup,
|
||||
const auto debugGroup = parameters.encoder->createDebugGroup(label.c_str());
|
||||
#endif
|
||||
|
||||
if (!propsBuffer) {
|
||||
const FillDrawablePropsUBO paramsUBO = {
|
||||
/* .color = */ evaluated.get<FillColor>().constantOr(FillColor::defaultValue()),
|
||||
/* .outline_color = */ evaluated.get<FillOutlineColor>().constantOr(FillOutlineColor::defaultValue()),
|
||||
/* .opacity = */ evaluated.get<FillOpacity>().constantOr(FillOpacity::defaultValue()),
|
||||
/* .padding = */ 0,
|
||||
0,
|
||||
0};
|
||||
propsBuffer = context.createUniformBuffer(¶msUBO, sizeof(paramsUBO));
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
const auto source = [this](const std::string_view& attrName) {
|
||||
return hasPropertyAsUniform(attrName) ? AttributeSource::Constant : AttributeSource::PerVertex;
|
||||
};
|
||||
#endif
|
||||
|
||||
bool fillUniformBufferUpdated = false;
|
||||
bool fillOutlineUniformBufferUpdated = false;
|
||||
bool fillPatternUniformBufferUpdated = false;
|
||||
bool fillOutlinePatternUniformBufferUpdated = false;
|
||||
|
||||
auto UpdateFillUniformBuffers = [&]() {
|
||||
if (fillUniformBufferUpdated) return;
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
using ShaderClass = ShaderSource<BuiltIn::FillShader, gfx::Backend::Type::Metal>;
|
||||
if (propertiesChanged || !fillPermutationUniformBuffer) {
|
||||
const FillPermutationUBO permutationUBO = {
|
||||
/* .color = */ {/*.source=*/source(ShaderClass::attributes[1].name), /*.expression=*/{}},
|
||||
/* .opacity = */ {/*.source=*/source(ShaderClass::attributes[2].name), /*.expression=*/{}},
|
||||
/* .overdrawInspector = */ overdrawInspector,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
|
||||
if (fillPermutationUniformBuffer) {
|
||||
fillPermutationUniformBuffer->update(&permutationUBO, sizeof(permutationUBO));
|
||||
} else {
|
||||
fillPermutationUniformBuffer = context.createUniformBuffer(&permutationUBO, sizeof(permutationUBO));
|
||||
}
|
||||
fillUniformBufferUpdated = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!fillPropsUniformBuffer) {
|
||||
const FillEvaluatedPropsUBO paramsUBO = {
|
||||
/* .color = */ evaluated.get<FillColor>().constantOr(FillColor::defaultValue()),
|
||||
/* .opacity = */ evaluated.get<FillOpacity>().constantOr(FillOpacity::defaultValue()),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
fillPropsUniformBuffer = context.createUniformBuffer(¶msUBO, sizeof(paramsUBO));
|
||||
}
|
||||
};
|
||||
|
||||
auto UpdateFillOutlineUniformBuffers = [&]() {
|
||||
if (fillOutlineUniformBufferUpdated) return;
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
using ShaderClass = ShaderSource<BuiltIn::FillOutlineShader, gfx::Backend::Type::Metal>;
|
||||
if (propertiesChanged || !fillOutlinePermutationUniformBuffer) {
|
||||
const FillOutlinePermutationUBO permutationUBO = {
|
||||
/* .outline_color = */ {/*.source=*/source(ShaderClass::attributes[1].name), /*.expression=*/{}},
|
||||
/* .opacity = */ {/*.source=*/source(ShaderClass::attributes[2].name), /*.expression=*/{}},
|
||||
/* .overdrawInspector = */ overdrawInspector,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
|
||||
if (fillOutlinePermutationUniformBuffer) {
|
||||
fillOutlinePermutationUniformBuffer->update(&permutationUBO, sizeof(permutationUBO));
|
||||
} else {
|
||||
fillOutlinePermutationUniformBuffer = context.createUniformBuffer(&permutationUBO,
|
||||
sizeof(permutationUBO));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!fillOutlinePropsUniformBuffer) {
|
||||
const FillOutlineEvaluatedPropsUBO paramsUBO = {
|
||||
/* .outline_color = */ evaluated.get<FillOutlineColor>().constantOr(FillOutlineColor::defaultValue()),
|
||||
/* .opacity = */ evaluated.get<FillOpacity>().constantOr(FillOpacity::defaultValue()),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
fillOutlinePropsUniformBuffer = context.createUniformBuffer(¶msUBO, sizeof(paramsUBO));
|
||||
}
|
||||
|
||||
fillOutlineUniformBufferUpdated = true;
|
||||
};
|
||||
|
||||
auto UpdateFillPatternUniformBuffers = [&]() {
|
||||
if (fillPatternUniformBufferUpdated) return;
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
using ShaderClass = ShaderSource<BuiltIn::FillOutlinePatternShader, gfx::Backend::Type::Metal>;
|
||||
if (propertiesChanged || !fillPatternPermutationUniformBuffer) {
|
||||
const FillPatternPermutationUBO permutationUBO = {
|
||||
/* .pattern_from = */ {/*.source=*/source(ShaderClass::attributes[1].name), /*.expression=*/{}},
|
||||
/* .pattern_to = */ {/*.source=*/source(ShaderClass::attributes[2].name), /*.expression=*/{}},
|
||||
/* .opacity = */ {/*.source=*/source(ShaderClass::attributes[3].name), /*.expression=*/{}},
|
||||
/* .overdrawInspector = */ overdrawInspector,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
|
||||
if (fillPatternPermutationUniformBuffer) {
|
||||
fillPatternPermutationUniformBuffer->update(&permutationUBO, sizeof(permutationUBO));
|
||||
} else {
|
||||
fillPatternPermutationUniformBuffer = context.createUniformBuffer(&permutationUBO,
|
||||
sizeof(permutationUBO));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!fillPatternPropsUniformBuffer) {
|
||||
const FillPatternEvaluatedPropsUBO paramsUBO = {
|
||||
/* .opacity = */ evaluated.get<FillOpacity>().constantOr(FillOpacity::defaultValue()),
|
||||
/* .fade = */ crossfade.t,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
fillPatternPropsUniformBuffer = context.createUniformBuffer(¶msUBO, sizeof(paramsUBO));
|
||||
}
|
||||
|
||||
fillPatternUniformBufferUpdated = true;
|
||||
};
|
||||
|
||||
auto UpdateFillOutlinePatternUniformBuffers = [&]() {
|
||||
if (fillOutlinePatternUniformBufferUpdated) return;
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
using ShaderClass = ShaderSource<BuiltIn::FillOutlinePatternShader, gfx::Backend::Type::Metal>;
|
||||
if (propertiesChanged || !fillOutlinePermutationUniformBuffer) {
|
||||
const FillOutlinePatternPermutationUBO permutationUBO = {
|
||||
/* .pattern_from = */ {/*.source=*/source(ShaderClass::attributes[1].name), /*.expression=*/{}},
|
||||
/* .pattern_to = */ {/*.source=*/source(ShaderClass::attributes[2].name), /*.expression=*/{}},
|
||||
/* .opacity = */ {/*.source=*/source(ShaderClass::attributes[3].name), /*.expression=*/{}},
|
||||
/* .overdrawInspector = */ overdrawInspector,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
|
||||
if (fillOutlinePatternPermutationUniformBuffer) {
|
||||
fillOutlinePatternPermutationUniformBuffer->update(&permutationUBO, sizeof(permutationUBO));
|
||||
} else {
|
||||
fillOutlinePatternPermutationUniformBuffer = context.createUniformBuffer(&permutationUBO,
|
||||
sizeof(permutationUBO));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!fillOutlinePatternPropsUniformBuffer) {
|
||||
const FillOutlinePatternEvaluatedPropsUBO paramsUBO = {
|
||||
/* .opacity = */ evaluated.get<FillOpacity>().constantOr(FillOpacity::defaultValue()),
|
||||
/* .fade = */ crossfade.t,
|
||||
0,
|
||||
0,
|
||||
};
|
||||
fillOutlinePatternPropsUniformBuffer = context.createUniformBuffer(¶msUBO, sizeof(paramsUBO));
|
||||
}
|
||||
|
||||
fillOutlinePatternUniformBufferUpdated = true;
|
||||
};
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
if (!expressionUniformBuffer) {
|
||||
const auto zoom = parameters.state.getZoom();
|
||||
const auto expressionUBO = buildExpressionUBO(zoom, parameters.frameCount);
|
||||
expressionUniformBuffer = context.createUniformBuffer(&expressionUBO, sizeof(expressionUBO));
|
||||
}
|
||||
#endif
|
||||
|
||||
layerGroup.visitDrawables([&](gfx::Drawable& drawable) {
|
||||
auto& uniforms = drawable.mutableUniformBuffers();
|
||||
uniforms.addOrReplace(FillDrawablePropsUBOName, propsBuffer);
|
||||
|
||||
if (!drawable.getTileID()) {
|
||||
return;
|
||||
}
|
||||
// auto& drawableType = drawable.getShader()->typeName();
|
||||
|
||||
const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped();
|
||||
|
||||
@@ -106,18 +251,80 @@ void FillLayerTweaker::execute(LayerGroupBase& layerGroup,
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!drawable.getTileID()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const FillDrawableUBO drawableUBO = {
|
||||
/*.matrix=*/util::cast<float>(matrix),
|
||||
/*.scale=*/{pixelRatio, tileRatio, crossfade.fromScale, crossfade.toScale},
|
||||
/*.world=*/{(float)renderableSize.width, (float)renderableSize.height},
|
||||
/*.pixel_coord_upper=*/{static_cast<float>(pixelX >> 16), static_cast<float>(pixelY >> 16)},
|
||||
/*.pixel_coord_lower=*/{static_cast<float>(pixelX & 0xFFFF), static_cast<float>(pixelY & 0xFFFF)},
|
||||
/*.texsize=*/{static_cast<float>(textureSize.width), static_cast<float>(textureSize.height)},
|
||||
};
|
||||
if (uniforms.get("FillInterpolateUBO")) {
|
||||
UpdateFillUniformBuffers();
|
||||
|
||||
uniforms.createOrUpdate(FillDrawableUBOName, &drawableUBO, context);
|
||||
uniforms.addOrReplace("FillEvaluatedPropsUBO", fillPropsUniformBuffer);
|
||||
const FillDrawableUBO drawableUBO = {/*.matrix=*/util::cast<float>(matrix)};
|
||||
|
||||
uniforms.createOrUpdate("FillDrawableUBO", &drawableUBO, context);
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
uniforms.addOrReplace("FillPermutationUBO", fillPermutationUniformBuffer);
|
||||
#endif // MLN_RENDER_BACKEND_METAL
|
||||
} else if (uniforms.get("FillOutlineInterpolateUBO")) {
|
||||
UpdateFillOutlineUniformBuffers();
|
||||
|
||||
uniforms.addOrReplace("FillOutlineEvaluatedPropsUBO", fillOutlinePropsUniformBuffer);
|
||||
const FillOutlineDrawableUBO drawableUBO = {
|
||||
/*.matrix=*/util::cast<float>(matrix),
|
||||
/*.world=*/{(float)renderableSize.width, (float)renderableSize.height},
|
||||
};
|
||||
|
||||
uniforms.createOrUpdate("FillOutlineDrawableUBO", &drawableUBO, context);
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
uniforms.addOrReplace("FillOutlinePermutationUBO", fillOutlinePermutationUniformBuffer);
|
||||
#endif // MLN_RENDER_BACKEND_METAL
|
||||
} else if (uniforms.get("FillPatternInterpolateUBO")) {
|
||||
UpdateFillPatternUniformBuffers();
|
||||
|
||||
uniforms.addOrReplace("FillPatternEvaluatedPropsUBO", fillPatternPropsUniformBuffer);
|
||||
const FillPatternDrawableUBO drawableUBO = {
|
||||
/*.matrix=*/util::cast<float>(matrix),
|
||||
/*.scale=*/{pixelRatio, tileRatio, crossfade.fromScale, crossfade.toScale},
|
||||
/*.pixel_coord_upper=*/{static_cast<float>(pixelX >> 16), static_cast<float>(pixelY >> 16)},
|
||||
/*.pixel_coord_lower=*/{static_cast<float>(pixelX & 0xFFFF), static_cast<float>(pixelY & 0xFFFF)},
|
||||
/*.texsize=*/{static_cast<float>(textureSize.width), static_cast<float>(textureSize.height)},
|
||||
0,
|
||||
0,
|
||||
};
|
||||
|
||||
uniforms.createOrUpdate("FillPatternDrawableUBO", &drawableUBO, context);
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
uniforms.addOrReplace("FillPatternPermutationUBO", fillPatternPermutationUniformBuffer);
|
||||
#endif // MLN_RENDER_BACKEND_METAL
|
||||
} else if (uniforms.get("FillOutlinePatternInterpolateUBO")) {
|
||||
UpdateFillOutlinePatternUniformBuffers();
|
||||
|
||||
uniforms.addOrReplace("FillOutlinePatternEvaluatedPropsUBO", fillOutlinePatternPropsUniformBuffer);
|
||||
const FillOutlinePatternDrawableUBO drawableUBO = {
|
||||
/*.matrix=*/util::cast<float>(matrix),
|
||||
/*.scale=*/{pixelRatio, tileRatio, crossfade.fromScale, crossfade.toScale},
|
||||
/*.world=*/{(float)renderableSize.width, (float)renderableSize.height},
|
||||
/*.pixel_coord_upper=*/{static_cast<float>(pixelX >> 16), static_cast<float>(pixelY >> 16)},
|
||||
/*.pixel_coord_lower=*/{static_cast<float>(pixelX & 0xFFFF), static_cast<float>(pixelY & 0xFFFF)},
|
||||
/*.texsize=*/{static_cast<float>(textureSize.width), static_cast<float>(textureSize.height)},
|
||||
};
|
||||
|
||||
uniforms.createOrUpdate("FillOutlinePatternDrawableUBO", &drawableUBO, context);
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
uniforms.addOrReplace("FillOutlinePatternPermutationUBO", fillOutlinePatternPermutationUniformBuffer);
|
||||
#endif // MLN_RENDER_BACKEND_METAL
|
||||
}
|
||||
|
||||
#if MLN_RENDER_BACKEND_METAL
|
||||
uniforms.addOrReplace("ExpressionInputsUBO", expressionUniformBuffer);
|
||||
#endif // MLN_RENDER_BACKEND_METAL
|
||||
});
|
||||
|
||||
propertiesChanged = false;
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
Reference in New Issue
Block a user