Metal debug shader (#1870)

This commit is contained in:
Alex Cristici
2023-11-15 23:37:59 +02:00
committed by GitHub
parent 08f24a68c8
commit 127506c135
8 changed files with 113 additions and 9 deletions

View File

@@ -1123,6 +1123,7 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/collision_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/layers/custom_drawable_layer.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/layermanager/custom_drawable_layer_factory.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/debug_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/fill_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/fill_extrusion_layer_ubo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/heatmap_layer_ubo.hpp
@@ -1195,6 +1196,7 @@ if(MBGL_WITH_METAL)
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/common.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/collision_box.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/collision_circle.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/debug.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/fill.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/fill_extrusion.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp
@@ -1239,6 +1241,7 @@ if(MBGL_WITH_METAL)
${PROJECT_SOURCE_DIR}src/mbgl/shaders/mtl/collision_box.cpp
${PROJECT_SOURCE_DIR}src/mbgl/shaders/mtl/collision_circle.cpp
${PROJECT_SOURCE_DIR}src/mbgl/shaders/mtl/clipping_mask.cpp
${PROJECT_SOURCE_DIR}src/mbgl/shaders/mtl/debug.cpp
${PROJECT_SOURCE_DIR}src/mbgl/shaders/mtl/fill.cpp
${PROJECT_SOURCE_DIR}src/mbgl/shaders/mtl/fill_extrusion.cpp
${PROJECT_SOURCE_DIR}src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp

View File

@@ -974,6 +974,7 @@ MLN_DRAWABLES_HEADERS = [
"include/mbgl/shaders/background_layer_ubo.hpp",
"include/mbgl/shaders/circle_layer_ubo.hpp",
"include/mbgl/shaders/collision_layer_ubo.hpp",
"include/mbgl/shaders/debug_layer_ubo.hpp",
"include/mbgl/shaders/fill_layer_ubo.hpp",
"include/mbgl/shaders/fill_extrusion_layer_ubo.hpp",
"include/mbgl/shaders/heatmap_layer_ubo.hpp",
@@ -1042,6 +1043,7 @@ MLN_DRAWABLES_MTL_SOURCE = [
"src/mbgl/shaders/mtl/collision_box.cpp",
"src/mbgl/shaders/mtl/collision_circle.cpp",
"src/mbgl/shaders/mtl/clipping_mask.cpp",
"src/mbgl/shaders/mtl/debug.cpp",
"src/mbgl/shaders/mtl/fill.cpp",
"src/mbgl/shaders/mtl/fill_extrusion.cpp",
"src/mbgl/shaders/mtl/fill_extrusion_pattern.cpp",
@@ -1084,6 +1086,7 @@ MLN_DRAWABLES_MTL_HEADERS = [
"include/mbgl/shaders/mtl/collision_box.hpp",
"include/mbgl/shaders/mtl/collision_circle.hpp",
"include/mbgl/shaders/mtl/common.hpp",
"include/mbgl/shaders/mtl/debug.hpp",
"include/mbgl/shaders/mtl/fill.hpp",
"include/mbgl/shaders/mtl/fill_extrusion.hpp",
"include/mbgl/shaders/mtl/fill_extrusion_pattern.hpp",

View File

@@ -0,0 +1,17 @@
#pragma once
#include <mbgl/shaders/layer_ubo.hpp>
namespace mbgl {
namespace shaders {
struct alignas(16) DebugUBO {
std::array<float, 4 * 4> matrix;
Color color;
float overlay_scale;
float pad1, pad2, pad3;
};
static_assert(sizeof(DebugUBO) % 16 == 0);
} // namespace shaders
} // namespace mbgl

View File

@@ -0,0 +1,67 @@
#pragma once
#include <mbgl/shaders/debug_layer_ubo.hpp>
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/shaders/mtl/common.hpp>
#include <mbgl/shaders/mtl/shader_program.hpp>
namespace mbgl {
namespace shaders {
template <>
struct ShaderSource<BuiltIn::DebugShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "DebugShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";
static const std::array<AttributeInfo, 1> attributes;
static const std::array<UniformBlockInfo, 1> uniforms;
static const std::array<TextureInfo, 1> textures;
static constexpr auto source = R"(
struct VertexStage {
short2 pos [[attribute(0)]];
};
struct FragmentStage {
float4 position [[position, invariant]];
float2 uv;
};
struct alignas(16) DebugUBO {
float4x4 matrix;
float4 color;
float overlay_scale;
float pad1, pad2, pad3;
};
FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
device const DebugUBO& debugUBO [[buffer(1)]]) {
const float4 position = debugUBO.matrix * float4(float2(vertx.pos) * debugUBO.overlay_scale, 0, 1);
// This vertex shader expects a EXTENT x EXTENT quad,
// The UV co-ordinates for the overlay texture can be calculated using that knowledge
float2 uv = float2(vertx.pos) / 8192.0;
return {
.position = position,
.uv = uv
};
}
half4 fragment fragmentMain(FragmentStage in [[stage_in]],
device const DebugUBO& debugUBO [[buffer(1)]],
texture2d<float, access::sample> overlay [[texture(0)]],
sampler overlay_sampler [[sampler(0)]]) {
float4 overlay_color = overlay.sample(overlay_sampler, in.uv);
float4 color = mix(debugUBO.color, overlay_color, overlay_color.a);
return half4(color);
}
)";
};
} // namespace shaders
} // namespace mbgl

View File

@@ -63,6 +63,8 @@ MTL::PrimitiveType getPrimitiveType(const gfx::DrawModeType type) noexcept {
return MTL::PrimitiveType::PrimitiveTypePoint;
case gfx::DrawModeType::Lines:
return MTL::PrimitiveType::PrimitiveTypeLine;
case gfx::DrawModeType::LineStrip:
return MTL::PrimitiveType::PrimitiveTypeLineStrip;
case gfx::DrawModeType::Triangles:
return MTL::PrimitiveType::PrimitiveTypeTriangle;
}

View File

@@ -17,6 +17,7 @@
#include <mbgl/shaders/mtl/clipping_mask.hpp>
#include <mbgl/shaders/mtl/collision_box.hpp>
#include <mbgl/shaders/mtl/collision_circle.hpp>
#include <mbgl/shaders/mtl/debug.hpp>
#include <mbgl/shaders/mtl/fill.hpp>
#include <mbgl/shaders/mtl/fill_extrusion.hpp>
#include <mbgl/shaders/mtl/fill_extrusion_pattern.hpp>
@@ -128,6 +129,7 @@ void RendererBackend::initShaders(gfx::ShaderRegistry& shaders, const ProgramPar
shaders::BuiltIn::ClippingMaskProgram,
shaders::BuiltIn::CollisionBoxShader,
shaders::BuiltIn::CollisionCircleShader,
shaders::BuiltIn::DebugShader,
shaders::BuiltIn::FillShader,
shaders::BuiltIn::FillOutlineShader,
shaders::BuiltIn::FillOutlinePatternShader,

View File

@@ -15,6 +15,7 @@
#include <mbgl/gfx/drawable_builder.hpp>
#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/renderer/render_static_data.hpp>
#include <mbgl/shaders/debug_layer_ubo.hpp>
#include <mbgl/shaders/shader_program_base.hpp>
#include <mbgl/util/convert.hpp>
#include <mbgl/util/string_indexer.hpp>
@@ -25,6 +26,7 @@
namespace mbgl {
using namespace style;
using namespace shaders;
void TileSourceRenderItem::upload(gfx::UploadPass& parameters) const {
for (auto& tile : *renderTiles) {
@@ -47,15 +49,6 @@ void TileSourceRenderItem::updateDebugDrawables(DebugLayerGroupMap& debugLayerGr
return;
}
// Debug UBO definition
struct alignas(16) DebugUBO {
std::array<float, 4 * 4> matrix;
Color color;
float overlay_scale;
float pad1, pad2, pad3;
};
static_assert(sizeof(DebugUBO) % 16 == 0);
// initialize
auto& context = parameters.context;
const auto renderPass = RenderPass::None;

View File

@@ -0,0 +1,17 @@
#include <mbgl/shaders/mtl/debug.hpp>
namespace mbgl {
namespace shaders {
const std::array<AttributeInfo, 1> ShaderSource<BuiltIn::DebugShader, gfx::Backend::Type::Metal>::attributes = {
AttributeInfo{0, gfx::AttributeDataType::Short2, 1, "a_pos"},
};
const std::array<UniformBlockInfo, 1> ShaderSource<BuiltIn::DebugShader, gfx::Backend::Type::Metal>::uniforms = {
UniformBlockInfo{1, true, true, sizeof(DebugUBO), "DebugUBO"},
};
const std::array<TextureInfo, 1> ShaderSource<BuiltIn::DebugShader, gfx::Backend::Type::Metal>::textures = {
TextureInfo{0, "u_overlay"},
};
} // namespace shaders
} // namespace mbgl