Skip to content

Commit

Permalink
Fixed metal background render tests (#1864)
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] authored Nov 15, 2023
1 parent 37a9c08 commit f24055a
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 23 deletions.
9 changes: 7 additions & 2 deletions include/mbgl/shaders/background_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ static_assert(sizeof(BackgroundDrawableUBO) % 16 == 0);
struct alignas(16) BackgroundLayerUBO {
/* 0 */ Color color;
/* 16 */ float opacity;
/* 20 */ float pad1, pad2, pad3;
// overdrawInspector is used only in Metal, while in GL this is a 16 bytes empty padding.
/* 20 */ bool overdrawInspector;
/* 21 */ uint8_t pad1, pad2, pad3;
/* 24 */ float pad4, pad5;
/* 32 */
};
static_assert(sizeof(BackgroundLayerUBO) == 32);
Expand All @@ -35,7 +38,9 @@ struct alignas(16) BackgroundPatternLayerUBO {
/* 80 */ float scale_b;
/* 84 */ float mix;
/* 88 */ float opacity;
/* 92 */ float pad;
// overdrawInspector is used only in Metal, while in GL this is a 4 bytes empty padding.
/* 92 */ bool overdrawInspector;
/* 93 */ uint8_t pad1, pad2, pad3;
/* 96 */
};
static_assert(sizeof(BackgroundPatternLayerUBO) == 96);
Expand Down
20 changes: 11 additions & 9 deletions include/mbgl/shaders/mtl/background.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,26 @@ struct alignas(16) BackgroundDrawableUBO {
};
struct alignas(16) BackgroundLayerUBO {
float4 color;
float opacity, pad1, pad2, pad3;
float opacity;
bool overdrawInspector;
uint8_t pad1, pad2, pad3;
float pad4, pad5;
};
FragmentStage vertex vertexMain(VertexStage in [[stage_in]],
device const BackgroundLayerUBO& layerUBO [[buffer(1)]],
device const BackgroundDrawableUBO& drawableUBO [[buffer(2)]]) {
device const BackgroundDrawableUBO& drawableUBO [[buffer(1)]]) {
return {
.position = drawableUBO.matrix * float4(float2(in.position.xy), 0, 1)
};
}
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
device const BackgroundLayerUBO& layerUBO [[buffer(1)]]) {
#ifdef OVERDRAW_INSPECTOR
return half4(1.0);
#else
return half4(float4(layerUBO.color.rgb, layerUBO.color.a * layerUBO.opacity));
#endif
device const BackgroundLayerUBO& layerUBO [[buffer(2)]]) {
if (layerUBO.overdrawInspector) {
return half4(0.0);
}
return half4(layerUBO.color * layerUBO.opacity);
}
)";
};
Expand Down
80 changes: 77 additions & 3 deletions include/mbgl/shaders/mtl/background_pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,84 @@ namespace mbgl {
namespace shaders {

template <>
struct ShaderSource<BuiltIn::BackgroundPatternShader, gfx::Backend::Type::Metal>
: public ShaderSource<BuiltIn::BackgroundShader, gfx::Backend::Type::Metal> {
struct ShaderSource<BuiltIn::BackgroundPatternShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "BackgroundPatternShader";
static const std::array<UniformBlockInfo, 3> uniforms;
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";

static const std::array<AttributeInfo, 1> attributes;
static const std::array<UniformBlockInfo, 2> uniforms;
static const std::array<TextureInfo, 1> textures;

static constexpr auto source = R"(
#include <metal_stdlib>
using namespace metal;
struct VertexStage {
short2 position [[attribute(0)]];
};
struct FragmentStage {
float4 position [[position, invariant]];
float2 pos_a;
float2 pos_b;
};
struct alignas(16) BackgroundDrawableUBO {
float4x4 matrix;
};
struct alignas(16) BackgroundLayerUBO {
float2 pattern_tl_a;
float2 pattern_br_a;
float2 pattern_tl_b;
float2 pattern_br_b;
float2 texsize;
float2 pattern_size_a;
float2 pattern_size_b;
float2 pixel_coord_upper;
float2 pixel_coord_lower;
float tile_units_to_pixels;
float scale_a;
float scale_b;
float mix;
float opacity;
bool overdrawInspector;
uint8_t pad1, pad2, pad3;
};
FragmentStage vertex vertexMain(VertexStage in [[stage_in]],
device const BackgroundDrawableUBO& drawableUBO [[buffer(1)]],
device const BackgroundLayerUBO& layerUBO [[buffer(2)]]) {
const float2 pos = float2(in.position);
float2 pos_a = get_pattern_pos(layerUBO.pixel_coord_upper, layerUBO.pixel_coord_lower, layerUBO.scale_a * layerUBO.pattern_size_a, layerUBO.tile_units_to_pixels, pos);
float2 pos_b = get_pattern_pos(layerUBO.pixel_coord_upper, layerUBO.pixel_coord_lower, layerUBO.scale_b * layerUBO.pattern_size_b, layerUBO.tile_units_to_pixels, pos);
return {
.position = drawableUBO.matrix * float4(float2(in.position.xy), 0, 1),
.pos_a = pos_a,
.pos_b = pos_b
};
}
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
device const BackgroundLayerUBO& layerUBO [[buffer(2)]],
texture2d<float, access::sample> image [[texture(0)]],
sampler image_sampler [[sampler(0)]]) {
if (layerUBO.overdrawInspector) {
return half4(0.0);
}
float2 imagecoord = glMod(in.pos_a, 1.0);
float2 pos = mix(layerUBO.pattern_tl_a / layerUBO.texsize, layerUBO.pattern_br_a / layerUBO.texsize, imagecoord);
float4 color1 = image.sample(image_sampler, pos);
float2 imagecoord_b = glMod(in.pos_b, 1.0);
float2 pos2 = mix(layerUBO.pattern_tl_b / layerUBO.texsize, layerUBO.pattern_br_b / layerUBO.texsize, imagecoord_b);
float4 color2 = image.sample(image_sampler, pos2);
return half4(mix(color1, color2, layerUBO.mix) * layerUBO.opacity);
}
)";
};

} // namespace shaders
Expand Down
10 changes: 8 additions & 2 deletions src/mbgl/renderer/layers/background_layer_tweaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,22 @@ void BackgroundLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintPara
/* .scale_b = */ crossfade.toScale,
/* .mix = */ crossfade.t,
/* .opacity = */ evaluated.get<BackgroundOpacity>(),
/* .pad = */ 0,
/* .overdrawInspector = */ overdrawInspector,
/* .pad1/2/3 = */ 0,
0,
0,
};
uniforms.createOrUpdate(idBackgroundLayerUBOName, &layerUBO, context);
} else {
// UBOs can be shared
if (!backgroundLayerBuffer) {
const BackgroundLayerUBO layerUBO = {/* .color = */ evaluated.get<BackgroundColor>(),
/* .opacity = */ evaluated.get<BackgroundOpacity>(),
/* .pad = */ 0,
/* .overdrawInspector = */ overdrawInspector,
/* .pad1/2/3 = */ 0,
0,
0,
/* .pad4/5 = */ 0,
0};
backgroundLayerBuffer = context.createUniformBuffer(&layerUBO, sizeof(layerUBO));
}
Expand Down
4 changes: 3 additions & 1 deletion src/mbgl/renderer/layers/render_background_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <mbgl/gfx/drawable_builder.hpp>
#include <mbgl/renderer/change_request.hpp>
#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/renderer/update_parameters.hpp>
#include <mbgl/shaders/shader_program_base.hpp>
#endif

Expand Down Expand Up @@ -216,7 +217,7 @@ static constexpr std::string_view BackgroundPatternShaderName = "BackgroundPatte
void RenderBackgroundLayer::update(gfx::ShaderRegistry& shaders,
gfx::Context& context,
const TransformState& state,
const std::shared_ptr<UpdateParameters>&,
const std::shared_ptr<UpdateParameters>& updateParameters,
[[maybe_unused]] const RenderTree& renderTree,
[[maybe_unused]] UniqueChangeRequestVec& changes) {
std::unique_lock<std::mutex> guard(mutex);
Expand Down Expand Up @@ -261,6 +262,7 @@ void RenderBackgroundLayer::update(gfx::ShaderRegistry& shaders,
layerTweaker = std::make_shared<BackgroundLayerTweaker>(getID(), evaluatedProperties);
layerGroup->addLayerTweaker(layerTweaker);
}
layerTweaker->enableOverdrawInspector(!!(updateParameters->debugOptions & MapDebugOptions::Overdraw));

if (!hasPattern && !plainShader) {
plainShader = context.getGenericShader(shaders, std::string(BackgroundPlainShaderName));
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/shaders/mtl/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const std::array<AttributeInfo, 1> ShaderSource<BuiltIn::BackgroundShader, gfx::
AttributeInfo{0, gfx::AttributeDataType::Float3, 1, "a_pos"},
};
const std::array<UniformBlockInfo, 2> ShaderSource<BuiltIn::BackgroundShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{1, true, true, sizeof(BackgroundLayerUBO), "BackgroundLayerUBO"},
UniformBlockInfo{2, true, false, sizeof(BackgroundDrawableUBO), "BackgroundDrawableUBO"},
UniformBlockInfo{1, true, false, sizeof(BackgroundDrawableUBO), "BackgroundDrawableUBO"},
UniformBlockInfo{2, false, true, sizeof(BackgroundLayerUBO), "BackgroundLayerUBO"},
};
const std::array<TextureInfo, 0> ShaderSource<BuiltIn::BackgroundShader, gfx::Backend::Type::Metal>::textures = {};

Expand Down
14 changes: 10 additions & 4 deletions src/mbgl/shaders/mtl/background_pattern.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#include <mbgl/shaders/mtl/background_pattern.hpp>
#include <mbgl/shaders/background_layer_ubo.hpp>

namespace mbgl {
namespace shaders {

const std::array<UniformBlockInfo, 3>
const std::array<AttributeInfo, 1>
ShaderSource<BuiltIn::BackgroundPatternShader, gfx::Backend::Type::Metal>::attributes = {
AttributeInfo{0, gfx::AttributeDataType::Float3, 1, "a_pos"},
};
const std::array<UniformBlockInfo, 2>
ShaderSource<BuiltIn::BackgroundPatternShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{1, sizeof(BackgroundLayerUBO), true, false, "BackgroundLayerUBO"},
UniformBlockInfo{2, sizeof(BackgroundDrawableUBO), true, false, "BackgroundDrawableUBO"},
UniformBlockInfo{3, sizeof(BackgroundPatternLayerUBO), true, false, "BackgroundLayerUBO"},
UniformBlockInfo{1, true, false, sizeof(BackgroundDrawableUBO), "BackgroundDrawableUBO"},
UniformBlockInfo{2, true, true, sizeof(BackgroundPatternLayerUBO), "BackgroundLayerUBO"},
};
const std::array<TextureInfo, 1> ShaderSource<BuiltIn::BackgroundPatternShader, gfx::Backend::Type::Metal>::textures = {
TextureInfo{0, "u_image"}};

} // namespace shaders
} // namespace mbgl

0 comments on commit f24055a

Please sign in to comment.