Skip to content

Commit

Permalink
Replace macro with compile-time string for all metal shaders.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcristici committed Jan 8, 2025
1 parent 3717ffa commit c17ee79
Show file tree
Hide file tree
Showing 15 changed files with 385 additions and 284 deletions.
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
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
34 changes: 19 additions & 15 deletions include/mbgl/shaders/mtl/debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#include <mbgl/shaders/debug_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 DEBUG_SHADER_PRELUDE \
R"(
using mbgl::util::operator""_cts;

constexpr auto debugShaderPrelude = R"(
enum {
idDebugUBO = drawableReservedUBOCount,
Expand All @@ -26,19 +28,9 @@ struct alignas(16) DebugUBO {
};
static_assert(sizeof(DebugUBO) == 6 * 16, "wrong size");
)"

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 constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 1> textures;
)"_cts;

static constexpr auto source = DEBUG_SHADER_PRELUDE R"(
constexpr auto debugShaderSource = debugShaderPrelude + R"(
struct VertexStage {
short2 pos [[attribute(debugUBOCount + 0)]];
Expand Down Expand Up @@ -73,7 +65,19 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]],
float4 color = mix(debug.color, overlay_color, overlay_color.a);
return half4(color);
}
)";
)"_cts;

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 constexpr std::array<AttributeInfo, 0> instanceAttributes{};
static const std::array<TextureInfo, 1> textures;

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

} // namespace shaders
Expand Down
Loading

0 comments on commit c17ee79

Please sign in to comment.