#version 330

float bayer4x4(vec2 fragCoord) {
    ivec2 p = ivec2(mod(floor(fragCoord), 4.0));

    int index = p.x + p.y * 4;

    float threshold = 0.0;

    if (index == 0) threshold = 0.0;
    else if (index == 1) threshold = 8.0;
    else if (index == 2) threshold = 2.0;
    else if (index == 3) threshold = 10.0;

    else if (index == 4) threshold = 12.0;
    else if (index == 5) threshold = 4.0;
    else if (index == 6) threshold = 14.0;
    else if (index == 7) threshold = 6.0;

    else if (index == 8) threshold = 3.0;
    else if (index == 9) threshold = 11.0;
    else if (index == 10) threshold = 1.0;
    else if (index == 11) threshold = 9.0;

    else if (index == 12) threshold = 15.0;
    else if (index == 13) threshold = 7.0;
    else if (index == 14) threshold = 13.0;
    else if (index == 15) threshold = 5.0;

    return (threshold + 0.5) / 16.0;
}

void apply_alpha_dither(float alpha) {
    if (alpha < bayer4x4(gl_FragCoord.xy)) {
        discard;
    }
}