f

#if defined(ROUNDED) || defined(BORDERED)
uniform vec2 size;
#endif

#if defined(ROUNDED) || defined(BORDERED) || defined(REPEAT)
smooth in vec2 local_position;
#endif

#ifdef ROUNDED
// top-left, top-right, bottom-left, bottom-right
uniform vec4 radii;
#endif

#ifdef BORDERED
// top, right, bottom, left
uniform vec4 borders;

uniform vec4 border_color_left;
uniform vec4 border_color_top;
uniform vec4 border_color_right;
uniform vec4 border_color_bottom;
#endif

#if defined(TEXTURED) && (defined(ROUNDED) || defined(BORDERED))
#error Not supported
#endif

out vec4 frag_color;

float point_to_line_distance_signed(vec2 a, vec2 b, vec2 p)
{
    return (p.x - a.x) * (b.y - a.y) - (p.y - a.y) * (b.x - a.x);
}

float point_in_rect(vec2 min, vec2 max, vec2 p)
{
    vec2 t = step(min, p) - step(max, p);
    return t.x * t.y;
}

float rounded_corner_bounds_test(
    vec2 p0,
    vec2 p1,
    float clip_outside,
    vec2 bounds_min,
    vec2 bounds_max,
    vec2 point)
{
    float on_outside = step(point_to_line_distance_signed(p0, p1, point), 0);
    float in_bounds = point_in_rect(bounds_min, bounds_max, point);

    return max(on_outside, (1 - clip_outside)) * in_bounds;
}

float elipse_test(vec2 point, vec2 origin, vec2 radii)
{
    vec2 rel = point - origin;

    return step((rel.x * rel.x) / (radii.x * radii.x) + (rel.y * rel.y) / (radii.y * radii.y), 1);
}

#if defined(ROUNDED) || defined(BORDERED)
// Render rounded or bordered rects
vec4 render_rect_decoration(vec4 color, vec2 position)
{
    // All border/rounding calculations are done in the local view space of the rect
    vec2 top_left = vec2(0, size.y);
    vec2 top_right = size;
    vec2 bottom_left = vec2(0, 0);
    vec2 bottom_right = vec2(size.x, 0);

#ifdef BORDERED
    // The border is mostly calculated from the "inner" space, ie. the rect minus the border
    vec2 inner_size = size - vec2(borders[1] + borders[3], borders[0] + borders[2]);
    vec2 inner_offset = vec2(borders[3], borders[2]);
    vec2 inner_position = position - inner_offset;
    vec2 inner_center = inner_offset + inner_size / 2;

    vec2 inner_top_left = inner_offset + vec2(0, inner_size.y);
    vec2 inner_top_right = inner_offset + inner_size;
    vec2 inner_bottom_left = inner_offset;
    vec2 inner_bottom_right = inner_offset + vec2(inner_size.x, 0);

    // Determine where in relation to the inner center we are
    float on_top = step(inner_center.y, position.y);
    float on_right = step(inner_center.x, position.x);
    float on_bottom = 1 - on_top;
    float on_left = 1 - on_right;

    // We also require which quadrant we're on
    float on_top_left = on_top * on_left;
    float on_top_right = on_top * on_right;
    float on_bottom_left = on_bottom * on_left;
    float on_bottom_right = on_bottom * on_right;

    // Linearize the top_color and bottom_color of this "quadrant"
    vec4 border_top_color = on_top * border_color_top
                          + on_bottom_left * border_color_left
                          + on_bottom_right * border_color_right;
    vec4 border_bottom_color = on_bottom * border_color_bottom
                             + on_top_left * border_color_left
                             + on_top_right * border_color_right;

    // Use 2 clip points to form a line separating the top and bottom color
    vec2 border_clip0 = on_top_left * top_left
                      + on_bottom_left * bottom_left
                      + on_top_right * inner_top_right
                      + on_bottom_right * inner_bottom_right;

    vec2 border_clip1 = on_top_left * inner_top_left
                      + on_bottom_left * inner_bottom_left
                      + on_top_right * top_right
                      + on_bottom_right * bottom_right;

    // Determine the actual border color to draw using the clip points
    float is_bottom_border_color = step(0, point_to_line_distance_signed(border_clip0, border_clip1, position));
    vec4 border_color = mix(border_top_color, border_bottom_color, is_bottom_border_color);
#endif

#if defined(ROUNDED)
    // Calculate bounding points of rounded corners
    // This is used to determine which corner (if any) we're on
    vec2 top_left_lower_clip = top_left - vec2(0, radii[0]);
    vec2 top_left_upper_clip = top_left + vec2(radii[0], 0);
    vec2 top_left_bounds_min = top_left_lower_clip;
    vec2 top_left_bounds_max = top_left_upper_clip;

    vec2 top_right_upper_clip = top_right - vec2(radii[1], 0);
    vec2 top_right_lower_clip = top_right - vec2(0, radii[1]);
    vec2 top_right_bounds_min = top_right - vec2(radii[1]);
    vec2 top_right_bounds_max = top_right;

    vec2 bottom_left_lower_clip = bottom_left + vec2(radii[2], 0);
    vec2 bottom_left_upper_clip = bottom_left + vec2(0, radii[2]);
    vec2 bottom_left_bounds_min = bottom_left;
    vec2 bottom_left_bounds_max = bottom_left + vec2(radii[2]);

    vec2 bottom_right_upper_clip = bottom_right + vec2(0, radii[3]);
    vec2 bottom_right_lower_clip = bottom_right - vec2(radii[3], 0);
    vec2 bottom_right_bounds_min = bottom_right_lower_clip;
    vec2 bottom_right_bounds_max = bottom_right_upper_clip;

#ifdef BORDERED
    // Bounds don't change for bordered rounded rects, but the clip points do
    top_left_lower_clip.x += borders[3];
    top_left_upper_clip.y -= borders[0];

    top_right_upper_clip.y -= borders[0];
    top_right_lower_clip.x -= borders[1];

    bottom_left_upper_clip.x += borders[3];
    bottom_left_lower_clip.y += borders[2];

    bottom_right_upper_clip.x -= borders[1];
    bottom_right_lower_clip.y += borders[2];

    // Don't clip the outside if the radius exceeds the border
    float top_left_clip_outside = max(step(borders[3], radii[0]), step(borders[0], radii[0]));
    float top_right_clip_outside = max(step(borders[0], radii[1]), step(borders[1], radii[1]));
    float bottom_left_clip_outside = max(step(borders[3], radii[2]), step(borders[2], radii[2]));
    float bottom_right_clip_outside = max(step(borders[1], radii[3]), step(borders[2], radii[3]));
#else
    float top_left_clip_outside = 1;
    float top_right_clip_outside = 1;
    float bottom_left_clip_outside = 1;
    float bottom_right_clip_outside = 1;
#endif

    // Determine which corner we're in, if any.
    float in_top_left_corner = rounded_corner_bounds_test(
        top_left_lower_clip,
        top_left_upper_clip,
        top_left_clip_outside,
        top_left_bounds_min,
        top_left_bounds_max,
        position);

    float in_top_right_corner = rounded_corner_bounds_test(
        top_right_upper_clip,
        top_right_lower_clip,
        top_right_clip_outside,
        top_right_bounds_min,
        top_right_bounds_max,
        position);

    float in_bottom_left_corner = rounded_corner_bounds_test(
        bottom_left_lower_clip,
        bottom_left_upper_clip,
        bottom_left_clip_outside,
        bottom_left_bounds_min,
        bottom_left_bounds_max,
        position);

    float in_bottom_right_corner = rounded_corner_bounds_test(
        bottom_right_upper_clip,
        bottom_right_lower_clip,
        bottom_right_clip_outside,
        bottom_right_bounds_min,
        bottom_right_bounds_max,
        position);

    // Whether we even are on a corner at all
    float in_corner = clamp(in_top_left_corner + in_top_right_corner + in_bottom_left_corner + in_bottom_right_corner, 0, 1);

    // Linearize the radius
    float radius = in_top_left_corner * radii[0]
                 + in_top_right_corner * radii[1]
                 + in_bottom_left_corner * radii[2]
                 + in_bottom_right_corner * radii[3];

    // The center of rotation for the corner
    vec2 corner_center = in_top_left_corner * (top_left + vec2(radii[0], -radii[0]))
                       + in_top_right_corner * (top_right - vec2(radii[1]))
                       + in_bottom_left_corner * (bottom_left + vec2(radii[2]))
                       + in_bottom_right_corner * (bottom_right + vec2(-radii[3], radii[3]));
#endif

#if defined(ROUNDED) && defined(BORDERED) // Both rounded and bordered
    // Linearlize the border size for the rounded portion.
    // This is for the elipse test of the rect contents
    vec2 corner_border_size = on_top_left * vec2(borders[3], borders[0])
                            + on_top_right * vec2(borders[1], borders[0])
                            + on_bottom_left * vec2(borders[3], borders[2])
                            + on_bottom_right * vec2(borders[1], borders[2]);

    float distance_from_corner_center = length(corner_center - position);

    // Determine whether we're inside the rect or inside the border.
    vec2 inner_radii = max(vec2(radius) - corner_border_size, vec2(0, 0));
    float rounded_is_inner = elipse_test(position, corner_center, inner_radii);
    float rounded_is_border = (1 - rounded_is_inner) * step(distance_from_corner_center, radius);
    vec4 rounded_color = rounded_is_inner * color + rounded_is_border * border_color;

    // Determine the color for the bordered rect ignoring rounding
    float distance_from_inner = length(max(max(-inner_position, inner_position - inner_size), vec2(0)));
    float on_border_unrounded = 1 - step(distance_from_inner, 0);
    vec4 unrounded_color = mix(color, border_color, on_border_unrounded);

    return mix(unrounded_color, rounded_color, in_corner);

#elif defined(ROUNDED) // Only rounded
    // Simplified check for whether the point is in the rounded rect
    float distance_from_corner_center = length(corner_center - position);
    float is_outside = step(radius, distance_from_corner_center) * in_corner;
    return color * (1 - is_outside);

#elif defined(BORDERED) // Only bordered
    // Simplified check for whether the point is on the border or in the rect
    float distance_from_inner = length(max(max(-inner_position, inner_position - inner_size), vec2(0)));
    float on_border = 1 - step(distance_from_inner, 0);
    return mix(color, border_color, on_border);
#endif
}
#endif

// Keep this always on for now
#define SUPERSAMPLE

void main()
{
    vec4 color = tint;

#ifdef TEXTURED
#ifdef REPEAT
    // Use the local position to determine the repeated texture coord, anchoring to the top
    vec2 flipped_position = vec2(local_position.x, size.y - local_position.y);
    vec2 texture_position = texture_source_position + mod(flipped_position, texture_source_size);
    vec2 uv = texture_position / texture_size;
    uv.y = 1 - uv.y;
#endif

    color *= texture(tex, uv);
#endif

#if defined(ROUNDED) || defined(BORDERED)
#ifdef SUPERSAMPLE
    // 8x supersample rounded/bordered
    // This is set up for 16x supersample in a grid, with half the samples commented out, giving us 8x
    vec4 accumulator = vec4(0);

    // Top left
    // accumulator += render_rect_decoration(color, local_position + vec2(-0.125, 0.125));
    accumulator += render_rect_decoration(color, local_position + vec2(-0.375, 0.125));
    accumulator += render_rect_decoration(color, local_position + vec2(-0.125, 0.375));
    // accumulator += render_rect_decoration(color, local_position + vec2(-0.375, 0.375));

    // Top right
    // accumulator += render_rect_decoration(color, local_position + vec2(0.125, 0.125));
    // accumulator += render_rect_decoration(color, local_position + vec2(0.375, 0.125));
    accumulator += render_rect_decoration(color, local_position + vec2(0.125, 0.375));
    accumulator += render_rect_decoration(color, local_position + vec2(0.375, 0.375));

    // Bottom left
    accumulator += render_rect_decoration(color, local_position + vec2(-0.125, -0.125));
    // accumulator += render_rect_decoration(color, local_position + vec2(-0.375, -0.125));
    // accumulator += render_rect_decoration(color, local_position + vec2(-0.125, -0.375));
    accumulator += render_rect_decoration(color, local_position + vec2(-0.375, -0.375));

    // Bottom right
    // accumulator += render_rect_decoration(color, local_position + vec2(0.125, -0.125));
    accumulator += render_rect_decoration(color, local_position + vec2(0.375, -0.125));
    // accumulator += render_rect_decoration(color, local_position + vec2(0.125, -0.375));
    accumulator += render_rect_decoration(color, local_position + vec2(0.375, -0.375));

    color = accumulator / 8;
#else
    color = render_rect_decoration(color, local_position);
#endif
#endif

    frag_color = color;
}
