Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace macro with run-time string concatenation #3124

Merged
merged 9 commits into from
Jan 10, 2025
48 changes: 28 additions & 20 deletions include/mbgl/shaders/mtl/background.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/shaders/background_layer_ubo.hpp>
#include <mbgl/shaders/mtl/shader_program.hpp>
#include <mbgl/util/string.hpp>

namespace mbgl {
namespace shaders {

#define BACKGROUND_SHADER_COMMON \
R"(
using mbgl::util::operator""_cts;

constexpr auto backgroundShaderCommon = R"(

enum {
idBackgroundDrawableUBO = idDrawableReservedVertexOnlyUBO,
Expand Down Expand Up @@ -72,19 +74,10 @@ union BackgroundDrawableUnionUBO {
BackgroundPatternDrawableUBO backgroundPatternDrawableUBO;
};

)"

template <>
struct ShaderSource<BuiltIn::BackgroundShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "BackgroundShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";
)"_cts;

static const std::array<AttributeInfo, 1> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 0> textures;
constexpr auto backgroundShaderSource = backgroundShaderCommon + R"(

static constexpr auto source = BACKGROUND_SHADER_COMMON R"(
#include <metal_stdlib>
using namespace metal;

Expand Down Expand Up @@ -115,20 +108,23 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]],

return half4(props.color * props.opacity);
}
)";
};
)"_cts;

template <>
struct ShaderSource<BuiltIn::BackgroundPatternShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "BackgroundPatternShader";
struct ShaderSource<BuiltIn::BackgroundShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "BackgroundShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";

static const std::array<AttributeInfo, 1> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 1> textures;
static const std::array<TextureInfo, 0> textures;

static constexpr auto source = backgroundShaderSource.as_string_view();
};

constexpr auto backgroundPatternShaderSource = backgroundShaderCommon + R"(

static constexpr auto source = BACKGROUND_SHADER_COMMON R"(
#include <metal_stdlib>
using namespace metal;

Expand Down Expand Up @@ -186,7 +182,19 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]],

return half4(mix(color1, color2, props.mix) * props.opacity);
}
)";
)"_cts;

template <>
struct ShaderSource<BuiltIn::BackgroundPatternShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "BackgroundPatternShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";

static const std::array<AttributeInfo, 1> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 1> textures;

static constexpr auto source = backgroundPatternShaderSource.as_string_view();
};

} // namespace shaders
Expand Down
33 changes: 19 additions & 14 deletions include/mbgl/shaders/mtl/circle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#include <mbgl/shaders/circle_layer_ubo.hpp>
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/shaders/mtl/shader_program.hpp>
#include <mbgl/util/string.hpp>

namespace mbgl {
namespace shaders {

#define CIRCLE_SHADER_PRELUDE \
R"(
using mbgl::util::operator""_cts;

constexpr auto circleShaderPrelude = R"(

enum {
idCircleDrawableUBO = idDrawableReservedVertexOnlyUBO,
Expand Down Expand Up @@ -51,19 +53,10 @@ struct alignas(16) CircleEvaluatedPropsUBO {
};
static_assert(sizeof(CircleEvaluatedPropsUBO) == 4 * 16, "wrong size");

)"
)"_cts;

template <>
struct ShaderSource<BuiltIn::CircleShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "CircleShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";
constexpr auto circleShaderSource = circleShaderPrelude + R"(

static const std::array<AttributeInfo, 8> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 0> textures;

static constexpr auto source = CIRCLE_SHADER_PRELUDE R"(
struct VertexStage {
short2 position [[attribute(circleUBOCount + 0)]];

Expand Down Expand Up @@ -251,7 +244,19 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]],

return half4(opacity_t * mix(color * opacity, stroke_color * stroke_opacity, color_t));
}
)";
)"_cts;

template <>
struct ShaderSource<BuiltIn::CircleShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "CircleShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";

static const std::array<AttributeInfo, 8> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 0> textures;

static constexpr auto source = circleShaderSource.as_string_view();
};

} // namespace shaders
Expand Down
33 changes: 19 additions & 14 deletions include/mbgl/shaders/mtl/clipping_mask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/shaders/mtl/shader_program.hpp>
#include <mbgl/util/string.hpp>

namespace mbgl {
namespace shaders {

using mbgl::util::operator""_cts;

struct alignas(16) ClipUBO {
/* 0 */ std::array<float, 4 * 4> matrix;
/* 64 */ std::uint32_t stencil_ref;
Expand All @@ -16,8 +19,7 @@ struct alignas(16) ClipUBO {
};
static_assert(sizeof(ClipUBO) == 5 * 16);

#define CLIPPING_MASK_SHADER_PRELUDE \
R"(
constexpr auto clippingMaskShaderPrelude = R"(

#include <metal_stdlib>
using namespace metal;
Expand All @@ -37,19 +39,10 @@ struct alignas(16) ClipUBO {
};
static_assert(sizeof(ClipUBO) == 5 * 16, "wrong size");

)"
)"_cts;

template <>
struct ShaderSource<BuiltIn::ClippingMaskProgram, gfx::Backend::Type::Metal> {
static constexpr auto name = "ClippingMaskProgram";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";
constexpr auto clippingMaskShaderSource = clippingMaskShaderPrelude + R"(

static const std::array<AttributeInfo, 1> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 0> textures;

static constexpr auto source = CLIPPING_MASK_SHADER_PRELUDE R"(
struct VertexStage {
short2 position [[attribute(clippingMaskUBOCount + 0)]];
};
Expand All @@ -71,7 +64,19 @@ FragmentStage vertex vertexMain(VertexStage in [[stage_in]],
half4 fragment fragmentMain(FragmentStage in [[stage_in]]) {
return half4(1.0);
}
)";
)"_cts;

template <>
struct ShaderSource<BuiltIn::ClippingMaskProgram, gfx::Backend::Type::Metal> {
static constexpr auto name = "ClippingMaskProgram";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";

static const std::array<AttributeInfo, 1> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 0> textures;

static constexpr auto source = clippingMaskShaderSource.as_string_view();
};

} // namespace shaders
Expand Down
48 changes: 27 additions & 21 deletions include/mbgl/shaders/mtl/collision.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#include <mbgl/shaders/collision_layer_ubo.hpp>
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/shaders/mtl/shader_program.hpp>
#include <mbgl/util/string.hpp>

namespace mbgl {
namespace shaders {

#define COLLISION_SHADER_COMMON \
R"(
using mbgl::util::operator""_cts;

constexpr auto collisionShaderCommon = R"(

enum {
idCollisionDrawableUBO = idDrawableReservedVertexOnlyUBO,
Expand All @@ -30,19 +32,9 @@ struct alignas(16) CollisionTilePropsUBO {
};
static_assert(sizeof(CollisionTilePropsUBO) == 16, "wrong size");

)"

template <>
struct ShaderSource<BuiltIn::CollisionBoxShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "CollisionBoxShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";

static const std::array<AttributeInfo, 5> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 0> textures;
)"_cts;

static constexpr auto source = COLLISION_SHADER_COMMON R"(
constexpr auto collisionBoxShaderSource = collisionShaderCommon + R"(

struct VertexStage {
short2 pos [[attribute(collisionUBOCount + 0)]];
Expand Down Expand Up @@ -102,20 +94,22 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]]) {

return half4(color);
}
)";
};
)"_cts;

template <>
struct ShaderSource<BuiltIn::CollisionCircleShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "CollisionCircleShader";
struct ShaderSource<BuiltIn::CollisionBoxShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "CollisionBoxShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";

static const std::array<AttributeInfo, 4> attributes;
static const std::array<AttributeInfo, 5> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 0> textures;

static constexpr auto source = COLLISION_SHADER_COMMON R"(
static constexpr auto source = collisionBoxShaderSource.as_string_view();
};

constexpr auto collisionCircleShaderSource = collisionShaderCommon + R"(

struct VertexStage {
short2 pos [[attribute(collisionUBOCount + 0)]];
Expand Down Expand Up @@ -195,7 +189,19 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]],

return half4(opacity_t * color);
}
)";
)"_cts;

template <>
struct ShaderSource<BuiltIn::CollisionCircleShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "CollisionCircleShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";

static const std::array<AttributeInfo, 4> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 0> textures;

static constexpr auto source = collisionCircleShaderSource.as_string_view();
};

} // namespace shaders
Expand Down
33 changes: 19 additions & 14 deletions include/mbgl/shaders/mtl/custom_symbol_icon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#include <mbgl/shaders/custom_drawable_layer_ubo.hpp>
#include <mbgl/shaders/shader_source.hpp>
#include <mbgl/shaders/mtl/shader_program.hpp>
#include <mbgl/util/string.hpp>

namespace mbgl {
namespace shaders {

#define CUSTOM_SYMBOL_ICON_SHADER_PRELUDE \
R"(
using mbgl::util::operator""_cts;

constexpr auto customSymbolIconShaderPrelude = R"(

enum {
idCustomSymbolDrawableUBO = idDrawableReservedVertexOnlyUBO,
Expand All @@ -31,19 +33,10 @@ struct alignas(16) CustomSymbolIconDrawableUBO {
};
static_assert(sizeof(CustomSymbolIconDrawableUBO) == 7 * 16, "wrong size");

)"
)"_cts;

template <>
struct ShaderSource<BuiltIn::CustomSymbolIconShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "CustomSymbolIconShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";
constexpr auto customSymbolIconShaderSource = customSymbolIconShaderPrelude + R"(

static const std::array<AttributeInfo, 2> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 1> textures;

static constexpr auto source = CUSTOM_SYMBOL_ICON_SHADER_PRELUDE R"(
struct VertexStage {
float2 a_pos [[attribute(customSymbolUBOCount + 0)]];
float2 a_tex [[attribute(customSymbolUBOCount + 1)]];
Expand Down Expand Up @@ -107,7 +100,19 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]],

return half4(image.sample(image_sampler, float2(in.tex)));
}
)";
)"_cts;

template <>
struct ShaderSource<BuiltIn::CustomSymbolIconShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "CustomSymbolIconShader";
static constexpr auto vertexMainFunction = "vertexMain";
static constexpr auto fragmentMainFunction = "fragmentMain";

static const std::array<AttributeInfo, 2> attributes;
static constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 1> textures;

static constexpr auto source = customSymbolIconShaderSource.as_string_view();
};

} // namespace shaders
Expand Down
Loading
Loading