#pragma once #include #include #include #include namespace mbgl { namespace shaders { template <> struct ShaderSource { static constexpr auto name = "LineGradientShader"; static constexpr auto vertexMainFunction = "vertexMain"; static constexpr auto fragmentMainFunction = "fragmentMain"; static const std::array attributes; static constexpr std::array instanceAttributes{}; static const std::array uniforms; static const std::array textures; static constexpr auto source = R"( struct VertexStage { short2 pos_normal [[attribute(0)]]; uchar4 data [[attribute(1)]]; float2 blur [[attribute(2)]]; float2 opacity [[attribute(3)]]; float2 gapwidth [[attribute(4)]]; float2 offset [[attribute(5)]]; float2 width [[attribute(6)]]; }; struct FragmentStage { float4 position [[position, invariant]]; float2 width2; half2 normal; half gamma_scale; float lineprogress; #if !defined(HAS_UNIFORM_u_blur) half blur; #endif #if !defined(HAS_UNIFORM_u_opacity) half opacity; #endif }; FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]], device const LineDynamicUBO& dynamic [[buffer(7)]], device const LineGradientUBO& line [[buffer(8)]], device const LineGradientPropertiesUBO& props [[buffer(9)]], device const LineGradientInterpolationUBO& interp [[buffer(10)]]) { #if !defined(HAS_UNIFORM_u_blur) const auto blur = unpack_mix_float(vertx.blur, interp.blur_t); #endif #if !defined(HAS_UNIFORM_u_opacity) const auto opacity = unpack_mix_float(vertx.opacity, interp.opacity_t); #endif #if defined(HAS_UNIFORM_u_gapwidth) const auto gapwidth = props.gapwidth; #else const auto gapwidth = unpack_mix_float(vertx.gapwidth, interp.gapwidth_t) / 2; #endif #if defined(HAS_UNIFORM_u_offset) const auto offset = props.offset; #else const auto offset = unpack_mix_float(vertx.offset, interp.offset_t) * -1; #endif #if defined(HAS_UNIFORM_u_width) const auto width = props.width; #else const auto width = unpack_mix_float(vertx.width, interp.width_t); #endif // the distance over which the line edge fades out. // Retina devices need a smaller distance to avoid aliasing. const float ANTIALIASING = 1.0 / DEVICE_PIXEL_RATIO / 2.0; const float2 a_extrude = float2(vertx.data.xy) - 128.0; const float a_direction = fmod(float(vertx.data.z), 4.0) - 1.0; const float v_lineprogress = (floor(float(vertx.data.z) / 4.0) + vertx.data.w * 64.0) * 2.0 / MAX_LINE_DISTANCE; const float2 pos = floor(float2(vertx.pos_normal) * 0.5); // x is 1 if it's a round cap, 0 otherwise // y is 1 if the normal points up, and -1 if it points down // We store these in the least significant bit of a_pos_normal const float2 normal = float2(vertx.pos_normal) - 2.0 * pos; const float2 v_normal = float2(normal.x, normal.y * 2.0 - 1.0); const float halfwidth = width / 2.0; const float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0); const float outset = gapwidth + halfwidth * (gapwidth > 0.0 ? 2.0 : 1.0) + (halfwidth == 0.0 ? 0.0 : ANTIALIASING); // Scale the extrusion vector down to a normal and then up by the line width of this vertex. const float2 dist = outset * a_extrude * LINE_NORMAL_SCALE; // Calculate the offset when drawing a line that is to the side of the actual line. // We do this by creating a vector that points towards the extrude, but rotate // it when we're drawing round end points (a_direction = -1 or 1) since their // extrude vector points in another direction. const float u = 0.5 * a_direction; const float t = 1.0 - abs(u); const float2 offset2 = offset * a_extrude * LINE_NORMAL_SCALE * v_normal.y * float2x2(t, -u, u, t); const float4 projected_extrude = line.matrix * float4(dist / line.ratio, 0.0, 0.0); const float4 position = line.matrix * float4(pos + offset2 / line.ratio, 0.0, 1.0) + projected_extrude; // calculate how much the perspective view squishes or stretches the extrude const float extrude_length_without_perspective = length(dist); const float extrude_length_with_perspective = length(projected_extrude.xy / position.w * dynamic.units_to_pixels); return { .position = position, .width2 = float2(outset, inset), .normal = half2(v_normal), .gamma_scale = half(extrude_length_without_perspective / extrude_length_with_perspective), .lineprogress = v_lineprogress, #if !defined(HAS_UNIFORM_u_blur) .blur = half(blur), #endif #if !defined(HAS_UNIFORM_u_opacity) .opacity = half(opacity), #endif }; } half4 fragment fragmentMain(FragmentStage in [[stage_in]], device const LineGradientUBO& line [[buffer(8)]], device const LineGradientPropertiesUBO& props [[buffer(9)]], texture2d gradientTexture [[texture(0)]]) { #if defined(OVERDRAW_INSPECTOR) return half4(1.0); #endif #if defined(HAS_UNIFORM_u_blur) const float blur = props.blur; #else const float blur = float(in.blur); #endif #if defined(HAS_UNIFORM_u_opacity) const float opacity = props.opacity; #else const float opacity = float(in.opacity); #endif // Calculate the distance of the pixel from the line in pixels. const float dist = length(in.normal) * in.width2.x; // Calculate the antialiasing fade factor. This is either when fading in the // line in case of an offset line (v_width2.y) or when fading out (v_width2.x) const float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * in.gamma_scale; const float alpha = clamp(min(dist - (in.width2.y - blur2), in.width2.x - dist) / blur2, 0.0, 1.0); // For gradient lines, v_lineprogress is the ratio along the entire line, // scaled to [0, 2^15), and the gradient ramp is stored in a texture. constexpr sampler sampler2d(coord::normalized, filter::linear); const float4 color = gradientTexture.sample(sampler2d, float2(in.lineprogress, 0.5)); return half4(color * (alpha * opacity)); } )"; }; } // namespace shaders } // namespace mbgl