From b13614779b0fe5e8636a523533ef0912006356a6 Mon Sep 17 00:00:00 2001 From: Alex Cristici Date: Thu, 9 Jan 2025 17:31:11 +0200 Subject: [PATCH] Removed compile-time string and split shader code in prelude and source. --- include/mbgl/shaders/mtl/background.hpp | 54 +++++------ include/mbgl/shaders/mtl/circle.hpp | 32 +++---- include/mbgl/shaders/mtl/clipping_mask.hpp | 35 +++---- include/mbgl/shaders/mtl/collision.hpp | 49 +++++----- .../mbgl/shaders/mtl/custom_symbol_icon.hpp | 32 +++---- include/mbgl/shaders/mtl/debug.hpp | 32 +++---- include/mbgl/shaders/mtl/fill.hpp | 95 +++++++++---------- include/mbgl/shaders/mtl/fill_extrusion.hpp | 51 +++++----- include/mbgl/shaders/mtl/heatmap.hpp | 32 +++---- include/mbgl/shaders/mtl/heatmap_texture.hpp | 32 +++---- include/mbgl/shaders/mtl/hillshade.hpp | 32 +++---- .../mbgl/shaders/mtl/hillshade_prepare.hpp | 32 +++---- include/mbgl/shaders/mtl/line.hpp | 80 ++++++++-------- include/mbgl/shaders/mtl/raster.hpp | 32 +++---- include/mbgl/shaders/mtl/shader_group.hpp | 3 +- include/mbgl/shaders/mtl/symbol.hpp | 66 ++++++------- include/mbgl/shaders/mtl/widevector.hpp | 32 +++---- include/mbgl/util/string.hpp | 60 ------------ 18 files changed, 319 insertions(+), 462 deletions(-) diff --git a/include/mbgl/shaders/mtl/background.hpp b/include/mbgl/shaders/mtl/background.hpp index 4f189312306..2457d740aea 100644 --- a/include/mbgl/shaders/mtl/background.hpp +++ b/include/mbgl/shaders/mtl/background.hpp @@ -3,14 +3,11 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - -constexpr auto backgroundShaderCommon = R"( +constexpr auto backgroundShaderPrelude = R"( enum { idBackgroundDrawableUBO = idDrawableReservedVertexOnlyUBO, @@ -74,12 +71,20 @@ union BackgroundDrawableUnionUBO { BackgroundPatternDrawableUBO backgroundPatternDrawableUBO; }; -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "BackgroundShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; -constexpr auto backgroundShaderSource = backgroundShaderCommon + R"( + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -#include -using namespace metal; + static constexpr auto prelude = backgroundShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 position [[attribute(backgroundUBOCount + 0)]]; @@ -108,25 +113,22 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(props.color * props.opacity); } -)"_cts; +)"; +}; + template <> -struct ShaderSource { - static constexpr auto name = "BackgroundShader"; +struct ShaderSource { + static constexpr auto name = "BackgroundPatternShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; static const std::array attributes; static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = backgroundShaderSource.as_string_view(); -}; - -constexpr auto backgroundPatternShaderSource = backgroundShaderCommon + R"( + static const std::array textures; -#include -using namespace metal; + static constexpr auto prelude = backgroundShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 position [[attribute(backgroundUBOCount + 0)]]; @@ -182,19 +184,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(mix(color1, color2, props.mix) * props.opacity); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "BackgroundPatternShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = backgroundPatternShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/circle.hpp b/include/mbgl/shaders/mtl/circle.hpp index 44c89e56df4..7575f39bd83 100644 --- a/include/mbgl/shaders/mtl/circle.hpp +++ b/include/mbgl/shaders/mtl/circle.hpp @@ -3,13 +3,10 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - constexpr auto circleShaderPrelude = R"( enum { @@ -53,9 +50,20 @@ struct alignas(16) CircleEvaluatedPropsUBO { }; static_assert(sizeof(CircleEvaluatedPropsUBO) == 4 * 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "CircleShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto circleShaderSource = circleShaderPrelude + R"( + static constexpr auto prelude = circleShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 position [[attribute(circleUBOCount + 0)]]; @@ -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 { - static constexpr auto name = "CircleShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = circleShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/clipping_mask.hpp b/include/mbgl/shaders/mtl/clipping_mask.hpp index 69c7b432829..e0f5d8b6173 100644 --- a/include/mbgl/shaders/mtl/clipping_mask.hpp +++ b/include/mbgl/shaders/mtl/clipping_mask.hpp @@ -2,13 +2,10 @@ #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - struct alignas(16) ClipUBO { /* 0 */ std::array matrix; /* 64 */ std::uint32_t stencil_ref; @@ -21,9 +18,6 @@ static_assert(sizeof(ClipUBO) == 5 * 16); constexpr auto clippingMaskShaderPrelude = R"( -#include -using namespace metal; - enum { idClippingMaskUBO = idDrawableReservedVertexOnlyUBO, clippingMaskUBOCount = drawableReservedUBOCount @@ -39,9 +33,20 @@ struct alignas(16) ClipUBO { }; static_assert(sizeof(ClipUBO) == 5 * 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "ClippingMaskProgram"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto clippingMaskShaderSource = clippingMaskShaderPrelude + R"( + static constexpr auto prelude = clippingMaskShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 position [[attribute(clippingMaskUBOCount + 0)]]; @@ -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 { - static constexpr auto name = "ClippingMaskProgram"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = clippingMaskShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/collision.hpp b/include/mbgl/shaders/mtl/collision.hpp index 4cc599af816..79a21da87db 100644 --- a/include/mbgl/shaders/mtl/collision.hpp +++ b/include/mbgl/shaders/mtl/collision.hpp @@ -3,14 +3,11 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - -constexpr auto collisionShaderCommon = R"( +constexpr auto collisionShaderPrelude = R"( enum { idCollisionDrawableUBO = idDrawableReservedVertexOnlyUBO, @@ -32,9 +29,20 @@ struct alignas(16) CollisionTilePropsUBO { }; static_assert(sizeof(CollisionTilePropsUBO) == 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "CollisionBoxShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto collisionBoxShaderSource = collisionShaderCommon + R"( + static constexpr auto prelude = collisionShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos [[attribute(collisionUBOCount + 0)]]; @@ -94,22 +102,21 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]]) { return half4(color); } -)"_cts; +)"; +}; template <> -struct ShaderSource { - static constexpr auto name = "CollisionBoxShader"; +struct ShaderSource { + static constexpr auto name = "CollisionCircleShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; - static const std::array attributes; + static const std::array attributes; static constexpr std::array instanceAttributes{}; static const std::array 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)]]; @@ -189,19 +196,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(opacity_t * color); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "CollisionCircleShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = collisionCircleShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/custom_symbol_icon.hpp b/include/mbgl/shaders/mtl/custom_symbol_icon.hpp index 8e8e037d70d..a497f9fdaa2 100644 --- a/include/mbgl/shaders/mtl/custom_symbol_icon.hpp +++ b/include/mbgl/shaders/mtl/custom_symbol_icon.hpp @@ -3,13 +3,10 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - constexpr auto customSymbolIconShaderPrelude = R"( enum { @@ -33,9 +30,20 @@ struct alignas(16) CustomSymbolIconDrawableUBO { }; static_assert(sizeof(CustomSymbolIconDrawableUBO) == 7 * 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "CustomSymbolIconShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto customSymbolIconShaderSource = customSymbolIconShaderPrelude + R"( + static constexpr auto prelude = customSymbolIconShaderPrelude; + static constexpr auto source = R"( struct VertexStage { float2 a_pos [[attribute(customSymbolUBOCount + 0)]]; @@ -100,19 +108,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(image.sample(image_sampler, float2(in.tex))); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "CustomSymbolIconShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = customSymbolIconShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/debug.hpp b/include/mbgl/shaders/mtl/debug.hpp index 7d9352c2f22..e252f38636a 100644 --- a/include/mbgl/shaders/mtl/debug.hpp +++ b/include/mbgl/shaders/mtl/debug.hpp @@ -3,13 +3,10 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - constexpr auto debugShaderPrelude = R"( enum { @@ -28,9 +25,20 @@ struct alignas(16) DebugUBO { }; static_assert(sizeof(DebugUBO) == 6 * 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "DebugShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto debugShaderSource = debugShaderPrelude + R"( + static constexpr auto prelude = debugShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos [[attribute(debugUBOCount + 0)]]; @@ -65,19 +73,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], float4 color = mix(debug.color, overlay_color, overlay_color.a); return half4(color); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "DebugShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = debugShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/fill.hpp b/include/mbgl/shaders/mtl/fill.hpp index b7cb6c9d70b..b507c380a3e 100644 --- a/include/mbgl/shaders/mtl/fill.hpp +++ b/include/mbgl/shaders/mtl/fill.hpp @@ -3,14 +3,11 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - -constexpr auto fillShaderCommon = R"( +constexpr auto fillShaderPrelude = R"( enum { idFillDrawableUBO = idDrawableReservedVertexOnlyUBO, @@ -141,9 +138,20 @@ union FillTilePropsUnionUBO { FillOutlinePatternTilePropsUBO fillOutlinePatternTilePropsUBO; }; -)"_cts; +)"; -constexpr auto fillShaderSource = fillShaderCommon + R"( +template <> +struct ShaderSource { + static constexpr auto name = "FillShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; + + static constexpr auto prelude = fillShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 position [[attribute(fillUBOCount + 0)]]; @@ -204,11 +212,12 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(color * opacity); } -)"_cts; +)"; +}; template <> -struct ShaderSource { - static constexpr auto name = "FillShader"; +struct ShaderSource { + static constexpr auto name = "FillOutlineShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; @@ -216,10 +225,8 @@ struct ShaderSource { static constexpr std::array instanceAttributes{}; static const std::array textures; - static constexpr auto source = fillShaderSource.as_string_view(); -}; - -constexpr auto fillOutlineShaderSource = fillShaderCommon + R"( + static constexpr auto prelude = fillShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 position [[attribute(fillUBOCount + 0)]]; @@ -284,22 +291,22 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(color * opacity); } -)"_cts; +)"; +}; + template <> -struct ShaderSource { - static constexpr auto name = "FillOutlineShader"; +struct ShaderSource { + static constexpr auto name = "FillPatternShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; - static const std::array attributes; + static const std::array attributes; static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = fillOutlineShaderSource.as_string_view(); -}; + static const std::array textures; -constexpr auto fillPatternShaderSource = fillShaderCommon + R"( + static constexpr auto prelude = fillShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 position [[attribute(fillUBOCount + 0)]]; @@ -428,11 +435,12 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(mix(color1, color2, props.fade) * opacity); } -)"_cts; +)"; +}; template <> -struct ShaderSource { - static constexpr auto name = "FillPatternShader"; +struct ShaderSource { + static constexpr auto name = "FillOutlinePatternShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; @@ -440,10 +448,8 @@ struct ShaderSource { static constexpr std::array instanceAttributes{}; static const std::array textures; - static constexpr auto source = fillPatternShaderSource.as_string_view(); -}; - -constexpr auto fillOutlinePatternShaderSource = fillShaderCommon + R"( + static constexpr auto prelude = fillShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 position [[attribute(fillUBOCount + 0)]]; @@ -584,22 +590,21 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(mix(color1, color2, props.fade) * opacity); } -)"_cts; +)"; +}; template <> -struct ShaderSource { - static constexpr auto name = "FillOutlinePatternShader"; +struct ShaderSource { + static constexpr auto name = "FillOutlineTriangulatedShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; - static const std::array attributes; + static const std::array attributes; static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = fillOutlinePatternShaderSource.as_string_view(); -}; + static const std::array textures; -constexpr auto fillOutlineTriangulatedShaderSource = fillShaderCommon + R"( + static constexpr auto prelude = fillShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos_normal [[attribute(fillUBOCount + 0)]]; @@ -668,19 +673,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(props.outline_color * (alpha * props.opacity)); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "FillOutlineTriangulatedShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = fillOutlineTriangulatedShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/fill_extrusion.hpp b/include/mbgl/shaders/mtl/fill_extrusion.hpp index 940f1c100b8..0ac9350ebd4 100644 --- a/include/mbgl/shaders/mtl/fill_extrusion.hpp +++ b/include/mbgl/shaders/mtl/fill_extrusion.hpp @@ -3,14 +3,11 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - -constexpr auto fillExtrusionShaderCommon = R"( +constexpr auto fillExtrusionShaderPrelude = R"( enum { idFillExtrusionDrawableUBO = idDrawableReservedVertexOnlyUBO, @@ -64,9 +61,20 @@ struct alignas(16) FillExtrusionPropsUBO { }; static_assert(sizeof(FillExtrusionPropsUBO) == 5 * 16, "wrong size"); -)"_cts; +)"; -constexpr auto fillExtrusionShaderSource = fillExtrusionShaderCommon + R"( +template <> +struct ShaderSource { + static constexpr auto name = "FillExtrusionShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; + + static constexpr auto prelude = fillExtrusionShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos [[attribute(fillExtrusionUBOCount + 0)]]; @@ -173,22 +181,21 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], fragment FragmentOutput fragmentMain(FragmentStage in [[stage_in]]) { return { in.color/*, in.position.z*/ }; } -)"_cts; +)"; +}; template <> -struct ShaderSource { - static constexpr auto name = "FillExtrusionShader"; +struct ShaderSource { + static constexpr auto name = "FillExtrusionPatternShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; - static const std::array attributes; + static const std::array attributes; static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = fillExtrusionShaderSource.as_string_view(); -}; + static const std::array textures; -constexpr auto fillExtrusionPatternShaderSource = fillExtrusionShaderCommon + R"( + static constexpr auto prelude = fillExtrusionShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos [[attribute(fillExtrusionUBOCount + 0)]]; @@ -360,19 +367,7 @@ fragment FragmentOutput fragmentMain(FragmentStage in [[stage_in]], return {half4(mix(color1, color2, props.fade) * in.lighting)/*, in.position.z*/}; } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "FillExtrusionPatternShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = fillExtrusionPatternShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/heatmap.hpp b/include/mbgl/shaders/mtl/heatmap.hpp index e791eef91a0..ead80d8ab33 100644 --- a/include/mbgl/shaders/mtl/heatmap.hpp +++ b/include/mbgl/shaders/mtl/heatmap.hpp @@ -3,13 +3,10 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - constexpr auto heatmapShaderPrelude = R"( enum { @@ -40,9 +37,20 @@ struct alignas(16) HeatmapEvaluatedPropsUBO { }; static_assert(sizeof(HeatmapEvaluatedPropsUBO) == 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "HeatmapShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto heatmapShaderSource = heatmapShaderPrelude + R"( + static constexpr auto prelude = heatmapShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos [[attribute(heatmapUBOCount + 0)]]; @@ -133,19 +141,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(val, 1.0, 1.0, 1.0); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "HeatmapShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = heatmapShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/heatmap_texture.hpp b/include/mbgl/shaders/mtl/heatmap_texture.hpp index f614b54517d..767b77847ed 100644 --- a/include/mbgl/shaders/mtl/heatmap_texture.hpp +++ b/include/mbgl/shaders/mtl/heatmap_texture.hpp @@ -3,13 +3,10 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - constexpr auto heatmapTextureShaderPrelude = R"( enum { @@ -27,9 +24,20 @@ struct alignas(16) HeatmapTexturePropsUBO { }; static_assert(sizeof(HeatmapTexturePropsUBO) == 5 * 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "HeatmapTextureShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto heatmapTextureShaderSource = heatmapTextureShaderPrelude + R"( + static constexpr auto prelude = heatmapTextureShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos [[attribute(heatmapTextureUBOCount + 0)]]; @@ -68,19 +76,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], float4 color = color_ramp.sample(color_ramp_sampler, float2(t, 0.5)); return half4(color * props.opacity); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "HeatmapTextureShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = heatmapTextureShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/hillshade.hpp b/include/mbgl/shaders/mtl/hillshade.hpp index 57984d1f033..f51f4b7a7ad 100644 --- a/include/mbgl/shaders/mtl/hillshade.hpp +++ b/include/mbgl/shaders/mtl/hillshade.hpp @@ -3,13 +3,10 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - constexpr auto hillshadeShaderPrelude = R"( enum { @@ -41,9 +38,20 @@ struct alignas(16) HillshadeEvaluatedPropsUBO { }; static_assert(sizeof(HillshadeEvaluatedPropsUBO) == 3 * 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "HillshadeShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto hillshadeShaderSource = hillshadeShaderPrelude + R"( + static constexpr auto prelude = hillshadeShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos [[attribute(hillshadeUBOCount + 0)]]; @@ -121,19 +129,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(color); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "HillshadeShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = hillshadeShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/hillshade_prepare.hpp b/include/mbgl/shaders/mtl/hillshade_prepare.hpp index 1f736503fad..6d7ddcf01a7 100644 --- a/include/mbgl/shaders/mtl/hillshade_prepare.hpp +++ b/include/mbgl/shaders/mtl/hillshade_prepare.hpp @@ -3,13 +3,10 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - constexpr auto hillshadePrepareShaderPrelude = R"( enum { @@ -33,9 +30,20 @@ struct alignas(16) HillshadePrepareTilePropsUBO { }; static_assert(sizeof(HillshadePrepareTilePropsUBO) == 2 * 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "HillshadePrepareShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto hillshadePrepareShaderSource = hillshadePrepareShaderPrelude + R"( + static constexpr auto prelude = hillshadePrepareShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos [[attribute(hillshadePrepareUBOCount + 0)]]; @@ -131,19 +139,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(color); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "HillshadePrepareShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = hillshadePrepareShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/line.hpp b/include/mbgl/shaders/mtl/line.hpp index 1f1991a3561..1cbb845903e 100644 --- a/include/mbgl/shaders/mtl/line.hpp +++ b/include/mbgl/shaders/mtl/line.hpp @@ -3,14 +3,11 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - -constexpr auto lineShaderCommon = R"( +constexpr auto lineShadePrelude = R"( enum { idLineDrawableUBO = idDrawableReservedVertexOnlyUBO, @@ -173,9 +170,20 @@ union LineTilePropsUnionUBO { LineSDFTilePropsUBO lineSDFTilePropsUBO; }; -)"_cts; +)"; -constexpr auto lineShaderSource = lineShaderCommon + R"( +template <> +struct ShaderSource { + static constexpr auto name = "LineShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; + + static constexpr auto prelude = lineShadePrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos_normal [[attribute(lineUBOCount + 0)]]; @@ -341,22 +349,22 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(color * (alpha * opacity)); } -)"_cts; +)"; +}; + template <> -struct ShaderSource { - static constexpr auto name = "LineShader"; +struct ShaderSource { + static constexpr auto name = "LineGradientShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; - static const std::array attributes; + static const std::array attributes; static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = lineShaderSource.as_string_view(); -}; + static const std::array textures; -constexpr auto lineGradientShaderSource = lineShaderCommon + R"( + static constexpr auto prelude = lineShadePrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos_normal [[attribute(lineUBOCount + 0)]]; @@ -509,22 +517,21 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(color * (alpha * opacity)); } -)"_cts; +)"; +}; template <> -struct ShaderSource { - static constexpr auto name = "LineGradientShader"; +struct ShaderSource { + static constexpr auto name = "LinePatternShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; - static const std::array attributes; + static const std::array attributes; static constexpr std::array instanceAttributes{}; static const std::array textures; - static constexpr auto source = lineGradientShaderSource.as_string_view(); -}; - -constexpr auto linePatternShaderSource = lineShaderCommon + R"( + static constexpr auto prelude = lineShadePrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos_normal [[attribute(lineUBOCount + 0)]]; @@ -747,11 +754,12 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(color * alpha * opacity); } -)"_cts; +)"; +}; template <> -struct ShaderSource { - static constexpr auto name = "LinePatternShader"; +struct ShaderSource { + static constexpr auto name = "LineSDFShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; @@ -759,10 +767,8 @@ struct ShaderSource { static constexpr std::array instanceAttributes{}; static const std::array textures; - static constexpr auto source = linePatternShaderSource.as_string_view(); -}; - -constexpr auto lineSDFShaderSource = lineShaderCommon + R"( + static constexpr auto prelude = lineShadePrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos_normal [[attribute(lineUBOCount + 0)]]; @@ -969,19 +975,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(color * (alpha * opacity)); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "LineSDFShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = lineSDFShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/raster.hpp b/include/mbgl/shaders/mtl/raster.hpp index 74167e8997f..399d0ca0b46 100644 --- a/include/mbgl/shaders/mtl/raster.hpp +++ b/include/mbgl/shaders/mtl/raster.hpp @@ -3,13 +3,10 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - constexpr auto rasterShaderPrelude = R"( enum { @@ -42,9 +39,20 @@ struct alignas(16) RasterEvaluatedPropsUBO { }; static_assert(sizeof(RasterEvaluatedPropsUBO) == 4 * 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "RasterShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto rasterShaderSource = rasterShaderPrelude + R"( + static constexpr auto prelude = rasterShaderPrelude; + static constexpr auto source = R"( struct VertexStage { short2 pos [[attribute(rasterUBOCount + 0)]]; @@ -123,19 +131,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(half3(mix(high_vec, low_vec, rgb) * color.a), color.a); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "RasterShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = rasterShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/shader_group.hpp b/include/mbgl/shaders/mtl/shader_group.hpp index 8492c362a0e..4688592070f 100644 --- a/include/mbgl/shaders/mtl/shader_group.hpp +++ b/include/mbgl/shaders/mtl/shader_group.hpp @@ -48,6 +48,7 @@ class ShaderGroup final : public ShaderGroupBase { std::string_view /*firstAttribName*/) override { using ShaderSource = shaders::ShaderSource; constexpr auto& name = ShaderSource::name; + constexpr auto& prelude = ShaderSource::prelude; constexpr auto& source = ShaderSource::source; constexpr auto& vertMain = ShaderSource::vertexMainFunction; constexpr auto& fragMain = ShaderSource::fragmentMainFunction; @@ -64,7 +65,7 @@ class ShaderGroup final : public ShaderGroupBase { auto& context = static_cast(gfxContext); // C++26 will allow operator+ with std::string and std::string_view - const auto shaderSource = std::string(shaders::prelude) + std::string(source); + const auto shaderSource = std::string(shaders::prelude) + std::string(prelude) + std::string(source); shader = context.createProgram( ShaderID, shaderName, shaderSource, vertMain, fragMain, programParameters, additionalDefines); assert(shader); diff --git a/include/mbgl/shaders/mtl/symbol.hpp b/include/mbgl/shaders/mtl/symbol.hpp index 6f1a0c41794..df3e754dc65 100644 --- a/include/mbgl/shaders/mtl/symbol.hpp +++ b/include/mbgl/shaders/mtl/symbol.hpp @@ -3,14 +3,11 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - -constexpr auto symbolShaderCommon = R"( +constexpr auto symbolShaderPrelude = R"( enum { idSymbolDrawableUBO = idDrawableReservedVertexOnlyUBO, @@ -73,9 +70,20 @@ struct alignas(16) SymbolEvaluatedPropsUBO { }; static_assert(sizeof(SymbolEvaluatedPropsUBO) == 6 * 16, "wrong size"); -)"_cts; +)"; + +template <> +struct ShaderSource { + static constexpr auto name = "SymbolIconShader"; + static constexpr auto vertexMainFunction = "vertexMain"; + static constexpr auto fragmentMainFunction = "fragmentMain"; + + static const std::array attributes; + static constexpr std::array instanceAttributes{}; + static const std::array textures; -constexpr auto symbolIconShaderSource = symbolShaderCommon + R"( + static constexpr auto prelude = symbolShaderPrelude; + static constexpr auto source = R"( struct VertexStage { float4 pos_offset [[attribute(symbolUBOCount + 0)]]; @@ -199,22 +207,21 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(image.sample(image_sampler, float2(in.tex)) * opacity); } -)"_cts; +)"; +}; template <> -struct ShaderSource { - static constexpr auto name = "SymbolIconShader"; +struct ShaderSource { + static constexpr auto name = "SymbolSDFIconShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; - static const std::array attributes; + static const std::array attributes; static constexpr std::array instanceAttributes{}; static const std::array textures; - static constexpr auto source = symbolIconShaderSource.as_string_view(); -}; - -constexpr auto symbolSDFIconShaderSource = symbolShaderCommon + R"( + static constexpr auto prelude = symbolShaderPrelude; + static constexpr auto source = R"( struct VertexStage { float4 pos_offset [[attribute(symbolUBOCount + 0)]]; @@ -409,22 +416,21 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(color * (alpha * opacity * in.fade_opacity)); } -)"_cts; +)"; +}; template <> -struct ShaderSource { - static constexpr auto name = "SymbolSDFIconShader"; +struct ShaderSource { + static constexpr auto name = "SymbolTextAndIconShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; - static const std::array attributes; + static const std::array attributes; static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = symbolSDFIconShaderSource.as_string_view(); -}; + static const std::array textures; -constexpr auto symbolTextAndIconShaderSource = symbolShaderCommon + R"( + static constexpr auto prelude = symbolShaderPrelude; + static constexpr auto source = R"( #define SDF 1.0 #define ICON 0.0 @@ -636,19 +642,7 @@ half4 fragment fragmentMain(FragmentStage in [[stage_in]], return half4(color * (alpha * opacity * in.fade_opacity)); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "SymbolTextAndIconShader"; - static constexpr auto vertexMainFunction = "vertexMain"; - static constexpr auto fragmentMainFunction = "fragmentMain"; - - static const std::array attributes; - static constexpr std::array instanceAttributes{}; - static const std::array textures; - - static constexpr auto source = symbolTextAndIconShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/shaders/mtl/widevector.hpp b/include/mbgl/shaders/mtl/widevector.hpp index 7b1612834d5..f312d2dc602 100644 --- a/include/mbgl/shaders/mtl/widevector.hpp +++ b/include/mbgl/shaders/mtl/widevector.hpp @@ -3,13 +3,10 @@ #include #include #include -#include namespace mbgl { namespace shaders { -using mbgl::util::operator""_cts; - constexpr auto wideVectorShaderPrelude = R"( enum { @@ -18,11 +15,20 @@ enum { wideVectorUBOCount }; -)"_cts; +)"; -constexpr auto wideVectorShaderSource = wideVectorShaderPrelude + R"( +template <> +struct ShaderSource { + static constexpr auto name = "WideVectorShader"; + static constexpr auto vertexMainFunction = "vertexTri_wideVecPerf"; + static constexpr auto fragmentMainFunction = "fragmentTri_wideVecPerf"; -#include + static const std::array attributes; + static const std::array instanceAttributes; + static const std::array textures; + + static constexpr auto prelude = wideVectorShaderPrelude; + static constexpr auto source = R"( namespace WhirlyKitShader { @@ -572,19 +578,7 @@ fragment float4 fragmentTri_wideVecPerf( return vert.color * float4(1,1,1,edgeAlpha * patternAlpha * roundAlpha); } -)"_cts; - -template <> -struct ShaderSource { - static constexpr auto name = "WideVectorShader"; - static constexpr auto vertexMainFunction = "vertexTri_wideVecPerf"; - static constexpr auto fragmentMainFunction = "fragmentTri_wideVecPerf"; - - static const std::array attributes; - static const std::array instanceAttributes; - static const std::array textures; - - static constexpr auto source = wideVectorShaderSource.as_string_view(); +)"; }; } // namespace shaders diff --git a/include/mbgl/util/string.hpp b/include/mbgl/util/string.hpp index a17bfe5c37b..ded4df9d081 100644 --- a/include/mbgl/util/string.hpp +++ b/include/mbgl/util/string.hpp @@ -7,7 +7,6 @@ #include #include #include -#include // Polyfill needed by Qt when building for Android with GCC #if defined(__ANDROID__) && defined(__GLIBCXX__) @@ -97,65 +96,6 @@ inline float stof(const std::string &str) { return std::stof(str); } -// https://gist.github.com/Baduit/63c4ea0f248451f7047c1b003c8335d5 -// Note: asked author to clarify license - -template -struct CompileTimeString { - constexpr CompileTimeString() noexcept = default; - - constexpr CompileTimeString(const char (&literal)[ArraySize]) noexcept { std::ranges::copy(literal, data); } - - template - constexpr auto operator+(const CompileTimeString &other) const noexcept { - CompileTimeString result; - std::ranges::copy(data, result.data); - std::ranges::copy(other.data, result.data + ArraySize - 1); - return result; - } - - // Don't count the \0 at the end - constexpr std::size_t size() const { return ArraySize - 1; } - - constexpr auto begin() noexcept { return std::begin(data); } - constexpr auto end() noexcept { return std::end(data); } - constexpr auto cbegin() const noexcept { return std::cbegin(data); } - constexpr auto cend() const noexcept { return std::cend(data); } - constexpr auto rbegin() noexcept { return std::rbegin(data); } - constexpr auto rend() noexcept { return std::rend(data); } - constexpr auto crbegin() const noexcept { return std::crbegin(data); } - constexpr auto crend() const noexcept { return std::crend(data); } - - template - constexpr bool operator==(const CompileTimeString &other) const { - return as_string_view() == other.as_string_view(); - } - - constexpr bool starts_with(std::string_view sv) const noexcept { return as_string_view().starts_with(sv); } - constexpr bool starts_with(char ch) const noexcept { return as_string_view().starts_with(ch); } - constexpr bool starts_with(const char *s) const { return as_string_view().starts_with(s); } - - template - constexpr bool starts_with(const CompileTimeString &other) const { - return starts_with(other.as_string_view()); - } - - // https://en.cppreference.com/w/cpp/language/reference - // https://en.cppreference.com/w/cpp/language/member_functions#Member_functions_with_ref-qualifier - constexpr operator std::string_view() const & { return as_string_view(); } - constexpr operator std::string_view() const && = delete; - - constexpr std::string_view as_string_view() const & { return std::string_view(data, ArraySize - 1); } - constexpr std::string_view as_string_view() const && = delete; - - char data[ArraySize]; -}; - -template -constexpr auto operator""_cts() { - return Str; -} - } // namespace util } // namespace mbgl