#ifndef FLOOR_DITHER_INCLUDED
#define FLOOR_DITHER_INCLUDED

void FloorDither_float(
    float2 screen_uv,
    out float threshold)
{
    float2 pixel = floor(screen_uv * _ScreenParams.xy);

    float x = fmod(pixel.x, 4.0);
    float y = fmod(pixel.y, 4.0);

    float index = x + y * 4.0;

    float bayer = 0.0;

    if      (index < 0.5)  bayer = 0.0;
    else if (index < 1.5)  bayer = 8.0;
    else if (index < 2.5)  bayer = 2.0;
    else if (index < 3.5)  bayer = 10.0;
    else if (index < 4.5)  bayer = 12.0;
    else if (index < 5.5)  bayer = 4.0;
    else if (index < 6.5)  bayer = 14.0;
    else if (index < 7.5)  bayer = 6.0;
    else if (index < 8.5)  bayer = 3.0;
    else if (index < 9.5)  bayer = 11.0;
    else if (index < 10.5) bayer = 1.0;
    else if (index < 11.5) bayer = 9.0;
    else if (index < 12.5) bayer = 15.0;
    else if (index < 13.5) bayer = 7.0;
    else if (index < 14.5) bayer = 13.0;
    else                   bayer = 5.0;

    threshold = (bayer + 0.5) / 16.0;
}

void FloorDither_half(
    half2 screen_uv,
    out half threshold)
{
    float t;
    FloorDither_float(screen_uv, t);
    threshold = (half)t;
}

#endif