#include <metal_stdlib>
using namespace metal;

// shader c92c1c4c0a2fb839
// Shader dumped from BotW 208, using Cemu 2193a8c
// Based on c92c1c4c0a2fb839_0000000000001e49_ps.txt
// Used for: Camera Depth of Field Blur

constant float resXScale = float($width) / float($gameWidth);
constant float resYScale = float($height) / float($gameHeight);
#define SET_POSITION(_v)                                                       \
    out.position = _v;                                                         \
    out.position.z = (out.position.z + out.position.w) / 2.0
// start of shader inputs/outputs, predetermined by Cemu. Do not touch
struct SupportBuffer {
    int4 remapped[5];
    float2 fragCoordScale;
};

#define GET_FRAGCOORD()                                                        \
    float4(in.position.xy *supportBuffer.fragCoordScale.xy, in.position.z,     \
           1.0 / in.position.w)
struct FragmentIn {
    float4 position [[position]];
    float4 passParameterSem2 [[user(locn0)]];
};

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

// end of shader inputs/outputs
template <typename TextureT, typename CoordT>
float sampleCompareEmulate(TextureT tex, sampler samplr, CoordT coord,
                           float compareValue) {
    return compareValue < tex.sample(samplr, coord).x ? 1.0 : 0.0;
}
template <typename TextureT, typename CoordT>
float2 textureCalculateLod(TextureT tex, sampler samplr, CoordT coord) {
    float lod = tex.calculate_unclamped_lod(samplr, coord);
    return float2(floor(lod), fract(lod));
}
int clampFI32(int v) {
    if (v == 0x7FFFFFFF)
        return as_type<int>(1.0);
    else if (v == 0xFFFFFFFF)
        return as_type<int>(0.0);
    return as_type<int>(clamp(as_type<float>(v), 0.0, 1.0));
}
float mul_nonIEEE(float a, float b) {
    if (a == 0.0 || b == 0.0)
        return 0.0;
    return a * b;
}
fragment FragmentOut
main0(FragmentIn in [[stage_in]], float2 pointCoord [[point_coord]],
      bool frontFacing [[front_facing]],
      constant SupportBuffer &supportBuffer [[buffer(0)]],
      texture2d<float> tex0 [[texture(0)]], sampler samplr0 [[sampler(0)]],
      texture2d<float> tex1 [[texture(1)]], sampler samplr1 [[sampler(1)]],
      texture2d<float> tex2 [[texture(2)]], sampler samplr2 [[sampler(2)]]) {
    FragmentOut out;
    int4 R0i = int4(0);
    int4 R1i = int4(0);
    int4 R2i = int4(0);
    int4 R3i = int4(0);
    int4 R4i = int4(0);
    int4 R123i = int4(0);
    int4 R127i = int4(0);
    int backupReg0i, backupReg1i, backupReg2i, backupReg3i, backupReg4i;
    int PV0ix = 0, PV0iy = 0, PV0iz = 0, PV0iw = 0, PV1ix = 0, PV1iy = 0,
        PV1iz = 0, PV1iw = 0;
    int PS0i = 0, PS1i = 0;
    int4 tempi = int4(0);
    float tempResultf;
    int tempResulti;
    int4 ARi = int4(0);
    bool predResult = true;
    bool activeMaskStack[2];
    bool activeMaskStackC[3];
    activeMaskStack[0] = false;
    activeMaskStackC[0] = false;
    activeMaskStackC[1] = false;
    activeMaskStack[0] = true;
    activeMaskStackC[0] = true;
    activeMaskStackC[1] = true;
    R0i = as_type<int4>(in.passParameterSem2);
    if (activeMaskStackC[1] == true) {
        R1i.w = as_type<int>(tex0.sample(samplr0, float2(as_type<float>(R0i.x),
                                                         as_type<float>(R0i.y)))
                                 .x);
        R1i.z = as_type<int>(tex2.sample(samplr2, float2(as_type<float>(R0i.x),
                                                         as_type<float>(R0i.y)))
                                 .x);
    }
    if (activeMaskStackC[1] == true) {
        // 0
        R123i.x = as_type<int>(
            mul_nonIEEE(as_type<float>(R1i.w),
                        as_type<float>(supportBuffer.remapped[0].x)) +
            as_type<float>(supportBuffer.remapped[1].x));
        R123i.x = clampFI32(R123i.x);
        // 1
        R0i.z =
            as_type<int>(max(as_type<float>(R1i.z), as_type<float>(R123i.x)));
        // 2
        R1i.y = ((as_type<float>(R0i.z) != 0.0) ? -1 : 0);
        // 3
        if ((R1i.y == 0))
            discard_fragment();
    }
    if (activeMaskStackC[1] == true) {
        activeMaskStack[1] = activeMaskStack[0];
        activeMaskStackC[2] = activeMaskStackC[1];
        // 0
        predResult = (R1i.y != 0);
        activeMaskStack[1] = predResult;
        activeMaskStackC[2] = predResult == true && activeMaskStackC[1] == true;
    } else {
        activeMaskStack[1] = false;
        activeMaskStackC[2] = false;
    }
    if (activeMaskStackC[2] == true) {
        // 0
        PV0iw =
            as_type<int>(as_type<float>(supportBuffer.remapped[2].y) + -(1.0));
        // 1
        R123i.z = as_type<int>(
            mul_nonIEEE(as_type<float>(PV0iw), as_type<float>(R0i.z)) + 1.0);
        // 2
        tempResultf = max(0.0, as_type<float>(R123i.z));
        tempResultf = log2(tempResultf);
        PS0i = as_type<int>(tempResultf);
        // 3
        R127i.x = as_type<int>(as_type<float>(PS0i) + -(1.0));
        // 4
        R2i.z = R127i.x;
        R1i.w = R127i.x;
        PS0i = as_type<int>(exp2(as_type<float>(R127i.x)));
        // 5
        PV1iz = as_type<int>(as_type<float>(PS0i) +
                             as_type<float>(supportBuffer.remapped[3].w));
        R3i.w = R127i.x;
        R4i.w = R127i.x;
        // 6
        backupReg0i = R127i.x;
        R127i.x = as_type<int>(
            mul_nonIEEE(as_type<float>(PV1iz),
                        as_type<float>(supportBuffer.remapped[4].y)));
        PV0iy = as_type<int>(mul_nonIEEE(
            as_type<float>(PV1iz),
            as_type<float>(supportBuffer.remapped[4].x) / resXScale));
        R127i.z = as_type<int>(mul_nonIEEE(
            as_type<float>(PV1iz),
            as_type<float>(supportBuffer.remapped[4].w) / resYScale));
        R127i.w = as_type<int>(
            mul_nonIEEE(as_type<float>(PV1iz),
                        as_type<float>(supportBuffer.remapped[4].z)));
        R2i.w = as_type<int>(as_type<float>(backupReg0i) + 1.0);
        R2i.w = clampFI32(R2i.w);
        // 7
        R1i.x = as_type<int>(as_type<float>(R0i.x) + as_type<float>(PV0iy));
        R1i.y = as_type<int>(as_type<float>(R0i.y) + as_type<float>(R127i.x));
        R3i.z = as_type<int>(as_type<float>(R0i.x) + as_type<float>(R127i.w));
        R2i.x = as_type<int>(as_type<float>(R0i.x) + -(as_type<float>(PV0iy)));
        // 8
        R3i.x = as_type<int>(as_type<float>(R0i.y) + as_type<float>(R127i.z));
        R2i.y =
            as_type<int>(as_type<float>(R0i.y) + -(as_type<float>(R127i.x)));
        R4i.z =
            as_type<int>(as_type<float>(R0i.x) + -(as_type<float>(R127i.w)));
        R4i.y =
            as_type<int>(as_type<float>(R0i.y) + -(as_type<float>(R127i.z)));
    }
    if (activeMaskStackC[2] == true) {
        R1i.xyz = as_type<int3>(
            tex1.sample(samplr1,
                        float2(as_type<float>(R1i.x), as_type<float>(R1i.y)),
                        level(as_type<float>(R1i.w)))
                .xyz);
        R2i.xyz = as_type<int3>(
            tex1.sample(samplr1,
                        float2(as_type<float>(R2i.x), as_type<float>(R2i.y)),
                        level(as_type<float>(R2i.z)))
                .xyz);
        R3i.xyz = as_type<int3>(
            tex1.sample(samplr1,
                        float2(as_type<float>(R3i.z), as_type<float>(R3i.x)),
                        level(as_type<float>(R3i.w)))
                .xyz);
        R4i.xyz = as_type<int3>(
            tex1.sample(samplr1,
                        float2(as_type<float>(R4i.z), as_type<float>(R4i.y)),
                        level(as_type<float>(R4i.w)))
                .xyz);
    }
    if (activeMaskStackC[2] == true) {
        // 0
        PV0iy = as_type<int>(as_type<float>(R1i.z) + as_type<float>(R2i.z));
        PV0iy = as_type<int>(as_type<float>(PV0iy) / 2.0);
        PV0iz = as_type<int>(as_type<float>(R1i.y) + as_type<float>(R2i.y));
        PV0iz = as_type<int>(as_type<float>(PV0iz) / 2.0);
        PV0iw = as_type<int>(as_type<float>(R1i.x) + as_type<float>(R2i.x));
        PV0iw = as_type<int>(as_type<float>(PV0iw) / 2.0);
        // 1
        R123i.x =
            as_type<int>(as_type<float>(R3i.x) * 0.5 + as_type<float>(PV0iw));
        R123i.z =
            as_type<int>(as_type<float>(R3i.z) * 0.5 + as_type<float>(PV0iy));
        R123i.w =
            as_type<int>(as_type<float>(R3i.y) * 0.5 + as_type<float>(PV0iz));
        // 2
        R2i.x = as_type<int>(
            (as_type<float>(R4i.x) * 0.5 + as_type<float>(R123i.x)) / 2.0);
        R2i.y = as_type<int>(
            (as_type<float>(R4i.y) * 0.5 + as_type<float>(R123i.w)) / 2.0);
        R2i.z = as_type<int>(
            (as_type<float>(R4i.z) * 0.5 + as_type<float>(R123i.z)) / 2.0);
    }
    activeMaskStackC[1] =
        activeMaskStack[0] == true && activeMaskStackC[0] == true;
    // export
    out.passPixelColor0 =
        as_type<float4>(float4(as_type<float>(R2i.x), as_type<float>(R2i.y),
                               as_type<float>(R2i.z), as_type<float>(R2i.w)));
    return out;
}
