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

using namespace metal;

struct FragmentOut
{
    float4 gl_FragColor [[color(0)]];
};

struct FragmentIn
{
    float2 uv [[user(locn0)]];
    float4 color [[user(locn1)]];
};

fragment FragmentOut fragmentShader(FragmentIn in [[stage_in]], constant float& alpha_value_unif [[buffer(0)]], texture2d<float> tex_unif [[texture(0)]], sampler tex_unifSmplr [[sampler(0)]])
{
    FragmentOut out = {};
    out.gl_FragColor = tex_unif.sample(tex_unifSmplr, in.uv) * in.color;
    bool uvLessZero = in.uv.y < 0.0;
    bool uvGreaterEqualOne;
    if (!uvLessZero)
    {
        uvGreaterEqualOne = in.uv.y >= 1.0;
    }
    else
    {
        uvGreaterEqualOne = uvLessZero;
    }
    bool alphaUnThrough;
    if (!uvGreaterEqualOne)
    {
        alphaUnThrough = out.gl_FragColor.w < alpha_value_unif;
    }
    else
    {
        alphaUnThrough = uvGreaterEqualOne;
    }
    if (alphaUnThrough)
    {
        discard_fragment();
    }
    return out;
}

