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

using namespace metal;

struct Uniforms
{
    float2 inv_source_size;
    uint downsample_factor;
    uint _pad0;
};

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

fragment main0_out main0(constant Uniforms& _18 [[buffer(0)]], texture2d<float> source_tex [[texture(0)]], sampler source_texSmplr [[sampler(0)]], float4 gl_FragCoord [[position]])
{
    main0_out out = {};
    float2 src_block_center = gl_FragCoord.xy * float(_18.downsample_factor);
    if (_18.downsample_factor == 1u)
    {
        float2 uv = src_block_center * _18.inv_source_size;
        out.out_color = source_tex.sample(source_texSmplr, uv);
    }
    else
    {
        if (_18.downsample_factor == 2u)
        {
            float2 uv_1 = src_block_center * _18.inv_source_size;
            out.out_color = source_tex.sample(source_texSmplr, uv_1);
        }
        else
        {
            float off = float(_18.downsample_factor) * 0.25;
            float2 uv_tl = (src_block_center + float2(-off, -off)) * _18.inv_source_size;
            float2 uv_tr = (src_block_center + float2(off, -off)) * _18.inv_source_size;
            float2 uv_bl = (src_block_center + float2(-off, off)) * _18.inv_source_size;
            float2 uv_br = (src_block_center + float2(off)) * _18.inv_source_size;
            float4 c = ((source_tex.sample(source_texSmplr, uv_tl) + source_tex.sample(source_texSmplr, uv_tr)) + source_tex.sample(source_texSmplr, uv_bl)) + source_tex.sample(source_texSmplr, uv_br);
            out.out_color = c * 0.25;
        }
    }
    return out;
}

