Skip to content

Commit

Permalink
Removed compile-time string and split shader code in prelude and source.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcristici committed Jan 9, 2025
1 parent c17ee79 commit b136147
Show file tree
Hide file tree
Showing 18 changed files with 319 additions and 462 deletions.
54 changes: 22 additions & 32 deletions include/mbgl/shaders/mtl/background.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
#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 {

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

constexpr auto backgroundShaderCommon = R"(
constexpr auto backgroundShaderPrelude = R"(
enum {
idBackgroundDrawableUBO = idDrawableReservedVertexOnlyUBO,
Expand Down Expand Up @@ -74,12 +71,20 @@ union BackgroundDrawableUnionUBO {
BackgroundPatternDrawableUBO backgroundPatternDrawableUBO;
};
)"_cts;
)";

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

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

#include <metal_stdlib>
using namespace metal;
static constexpr auto prelude = backgroundShaderPrelude;
static constexpr auto source = R"(
struct VertexStage {
short2 position [[attribute(backgroundUBOCount + 0)]];
Expand Down Expand Up @@ -108,25 +113,22 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]],
return half4(props.color * props.opacity);
}
)"_cts;
)";
};


template <>
struct ShaderSource<BuiltIn::BackgroundShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "BackgroundShader";
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, 0> textures;

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

constexpr auto backgroundPatternShaderSource = backgroundShaderCommon + R"(
static const std::array<TextureInfo, 1> textures;

#include <metal_stdlib>
using namespace metal;
static constexpr auto prelude = backgroundShaderPrelude;
static constexpr auto source = R"(
struct VertexStage {
short2 position [[attribute(backgroundUBOCount + 0)]];
Expand Down Expand Up @@ -182,19 +184,7 @@ 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
32 changes: 14 additions & 18 deletions include/mbgl/shaders/mtl/circle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
#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 {

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

constexpr auto circleShaderPrelude = R"(
enum {
Expand Down Expand Up @@ -53,9 +50,20 @@ 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";

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

constexpr auto circleShaderSource = circleShaderPrelude + R"(
static constexpr auto prelude = circleShaderPrelude;
static constexpr auto source = R"(
struct VertexStage {
short2 position [[attribute(circleUBOCount + 0)]];
Expand Down Expand Up @@ -244,19 +252,7 @@ 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
35 changes: 14 additions & 21 deletions include/mbgl/shaders/mtl/clipping_mask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

#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 @@ -21,9 +18,6 @@ static_assert(sizeof(ClipUBO) == 5 * 16);

constexpr auto clippingMaskShaderPrelude = R"(
#include <metal_stdlib>
using namespace metal;
enum {
idClippingMaskUBO = idDrawableReservedVertexOnlyUBO,
clippingMaskUBOCount = drawableReservedUBOCount
Expand All @@ -39,9 +33,20 @@ 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";

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

constexpr auto clippingMaskShaderSource = clippingMaskShaderPrelude + R"(
static constexpr auto prelude = clippingMaskShaderPrelude;
static constexpr auto source = R"(
struct VertexStage {
short2 position [[attribute(clippingMaskUBOCount + 0)]];
Expand All @@ -64,19 +69,7 @@ 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
49 changes: 22 additions & 27 deletions include/mbgl/shaders/mtl/collision.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
#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 {

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

constexpr auto collisionShaderCommon = R"(
constexpr auto collisionShaderPrelude = R"(
enum {
idCollisionDrawableUBO = idDrawableReservedVertexOnlyUBO,
Expand All @@ -32,9 +29,20 @@ struct alignas(16) CollisionTilePropsUBO {
};
static_assert(sizeof(CollisionTilePropsUBO) == 16, "wrong size");
)"_cts;
)";

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;

constexpr auto collisionBoxShaderSource = collisionShaderCommon + R"(
static constexpr auto prelude = collisionShaderPrelude;
static constexpr auto source = R"(
struct VertexStage {
short2 pos [[attribute(collisionUBOCount + 0)]];
Expand Down Expand Up @@ -94,22 +102,21 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]]) {
return half4(color);
}
)"_cts;
)";
};

template <>
struct ShaderSource<BuiltIn::CollisionBoxShader, gfx::Backend::Type::Metal> {
static constexpr auto name = "CollisionBoxShader";
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, 5> attributes;
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 = collisionBoxShaderSource.as_string_view();
};

constexpr auto collisionCircleShaderSource = collisionShaderCommon + R"(
static constexpr auto prelude = collisionShaderPrelude;
static constexpr auto source = R"(
struct VertexStage {
short2 pos [[attribute(collisionUBOCount + 0)]];
Expand Down Expand Up @@ -189,19 +196,7 @@ 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
32 changes: 14 additions & 18 deletions include/mbgl/shaders/mtl/custom_symbol_icon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
#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 {

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

constexpr auto customSymbolIconShaderPrelude = R"(
enum {
Expand All @@ -33,9 +30,20 @@ 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";

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

constexpr auto customSymbolIconShaderSource = customSymbolIconShaderPrelude + R"(
static constexpr auto prelude = customSymbolIconShaderPrelude;
static constexpr auto source = R"(
struct VertexStage {
float2 a_pos [[attribute(customSymbolUBOCount + 0)]];
Expand Down Expand Up @@ -100,19 +108,7 @@ 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

0 comments on commit b136147

Please sign in to comment.