Fixed metal background render tests (#1864)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -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);
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
)";
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -109,7 +109,10 @@ 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 {
|
||||
@@ -117,8 +120,11 @@ void BackgroundLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintPara
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
@@ -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));
|
||||
|
||||
@@ -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 = {};
|
||||
|
||||
|
||||
@@ -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>
|
||||
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"},
|
||||
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, 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
|
||||
|
||||
Reference in New Issue
Block a user