#pragma clang diagnostic ignored "-Wmissing-prototypes"

#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct Uniforms
{
    float2 inv_working_size;
    uint pair_count;
    uint mode;
    float2 direction;
    float inv_downsample_factor;
    float _pad0;
    float4 kernel0[32];
};

struct main0_out
{
    float4 out_color [[color(0)]];
};

struct main0_in
{
    float2 p_local [[user(locn0)]];
    float4 f_color [[user(locn1)]];
    float2 f_half_size_ppx [[user(locn2), flat]];
    float4 f_radii_ppx [[user(locn3), flat]];
    float f_half_feather_ppx [[user(locn4), flat]];
};

static inline __attribute__((always_inline))
float3 blur_sample(thread const float2& uv, constant Uniforms& _108, texture2d<float> blur_input_tex, sampler blur_input_texSmplr)
{
    float3 color = blur_input_tex.sample(blur_input_texSmplr, uv).xyz * _108.kernel0[0].x;
    float2 axis_step = _108.direction * _108.inv_working_size;
    for (uint i = 1u; i < _108.pair_count; i++)
    {
        float w = _108.kernel0[i].x;
        float off = _108.kernel0[i].y;
        float2 step_uv = axis_step * off;
        color += (blur_input_tex.sample(blur_input_texSmplr, (uv - step_uv)).xyz * w);
        color += (blur_input_tex.sample(blur_input_texSmplr, (uv + step_uv)).xyz * w);
    }
    return color;
}

static inline __attribute__((always_inline))
float sdRoundedBox(thread const float2& p, thread const float2& b, thread const float4& r)
{
    float2 _36;
    if (p.x > 0.0)
    {
        _36 = r.xy;
    }
    else
    {
        _36 = r.zw;
    }
    float2 rxy = _36;
    float _50;
    if (p.y > 0.0)
    {
        _50 = rxy.x;
    }
    else
    {
        _50 = rxy.y;
    }
    float rr = _50;
    float2 q = abs(p) - b;
    if (rr == 0.0)
    {
        return fast::max(q.x, q.y);
    }
    q += float2(rr);
    return (fast::min(fast::max(q.x, q.y), 0.0) + length(fast::max(q, float2(0.0)))) - rr;
}

static inline __attribute__((always_inline))
float sdf_alpha(thread const float& d, thread const float& h)
{
    return 1.0 - smoothstep(-h, h, d);
}

fragment main0_out main0(main0_in in [[stage_in]], constant Uniforms& _108 [[buffer(0)]], texture2d<float> blur_input_tex [[texture(0)]], sampler blur_input_texSmplr [[sampler(0)]], float4 gl_FragCoord [[position]])
{
    main0_out out = {};
    if (_108.mode == 0u)
    {
        float2 uv = gl_FragCoord.xy * _108.inv_working_size;
        float2 param = uv;
        float3 color = blur_sample(param, _108, blur_input_tex, blur_input_texSmplr);
        out.out_color = float4(color, 1.0);
        return out;
    }
    float2 param_1 = in.p_local;
    float2 param_2 = in.f_half_size_ppx;
    float4 param_3 = in.f_radii_ppx;
    float d = sdRoundedBox(param_1, param_2, param_3);
    if (d > in.f_half_feather_ppx)
    {
        discard_fragment();
    }
    float grad_magnitude = fast::max(fwidth(d), 9.9999999747524270787835121154785e-07);
    float d_n = d / grad_magnitude;
    float h_n = in.f_half_feather_ppx / grad_magnitude;
    float2 uv_1 = (gl_FragCoord.xy * _108.inv_downsample_factor) * _108.inv_working_size;
    float3 color_1 = blur_input_tex.sample(blur_input_texSmplr, uv_1).xyz;
    float3 tinted = mix(color_1, color_1 * in.f_color.xyz, float3(in.f_color.w));
    float param_4 = d_n;
    float param_5 = h_n;
    float coverage = sdf_alpha(param_4, param_5);
    out.out_color = float4(tinted * coverage, coverage);
    return out;
}

