xternalOES external_textures[N_EXTERNAL_TEXTURES];
#endif
#if N_TEXTURES > 0
uniform sampler2D textures[N_TEXTURES];
#endif

#define GSK_VERTEX_INDEX gl_VertexID


#ifdef GSK_VERTEX_SHADER
#define IN(_loc) in
#define PASS(_loc) out
#define PASS_FLAT(_loc) flat out
#endif


#ifdef GSK_FRAGMENT_SHADER
#define PASS(_loc) in
#define PASS_FLAT(_loc) flat in

float
gsk_get_float (int id)
{
  int float_id = id & 0x3FFFFF;
  int array_id = (id >> 22) & 0xFF;
  switch (array_id)
    {
      case 0:
        return floats[0].really_just_floats[float_id >> 2][float_id & 3];
      case 1:
        return floats[1].really_just_floats[float_id >> 2][float_id & 3];
      case 2:
        return floats[2].really_just_floats[float_id >> 2][float_id & 3];
      case 3:
        return floats[3].really_just_floats[float_id >> 2][float_id & 3];
      case 4:
        return floats[4].really_just_floats[float_id >> 2][float_id & 3];
      case 5:
        return floats[5].really_just_floats[float_id >> 2][float_id & 3];
      case 6:
        return floats[6].really_just_floats[float_id >> 2][float_id & 3];
      case 7:
        return floats[7].really_just_floats[float_id >> 2][float_id & 3];
      case 8:
        return floats[8].really_just_floats[float_id >> 2][float_id & 3];
      case 9:
        return floats[9].really_just_floats[float_id >> 2][float_id & 3];
      case 10:
        return floats[10].really_just_floats[float_id >> 2][float_id & 3];
      default:
        return 0.0;
    }
}

float
gsk_get_float (uint id)
{
  return gsk_get_float (int (id));
}

#define gsk_get_int(id) (floatBitsToInt(gsk_get_float(id)))
#define gsk_get_uint(id) (floatBitsToUint(gsk_get_float(id)))

#if __VERSION__ < 400 || defined(GSK_GLES)

vec4
gsk_texture (uint id,
             vec2 pos)
{
  if ((id & 1u) != 0u)
    {
      switch (id >> 1u)
        {
        case 0u:
#if N_EXTERNAL_TEXTURES > 0
          return texture (external_textures[0], pos);
#endif
#if N_EXTERNAL_TEXTURES > 1
        case 1u:
          return texture (external_textures[1], pos);
#endif
        case 2u:
#if N_EXTERNAL_TEXTURES > 2
          return texture (external_textures[2], pos);
#endif
        case 3u:
#if N_EXTERNAL_TEXTURES > 3
          return texture (external_textures[3], pos);
#endif
        case 4u:
#if N_EXTERNAL_TEXTURES > 4
          return texture (external_textures[4], pos);
#endif
        case 5u:
#if N_EXTERNAL_TEXTURES > 5
          return texture (external_textures[5], pos);
#endif
        default:
          break;
        }
    }
  else
    {
      switch (id >> 1u)
        {
        case 0u:
#if N_TEXTURES > 0
          return texture (textures[0], pos);
#endif
        case 1u:
#if N_TEXTURES > 1
          return texture (textures[1], pos);
#endif
        case 2u:
#if N_TEXTURES > 2
          return texture (textures[2], pos);
#endif
        case 3u:
#if N_TEXTURES > 3
          return texture (textures[3], pos);
#endif
        case 4u:
#if N_TEXTURES > 4
          return texture (textures[4], pos);
#endif
        case 5u:
#if N_TEXTURES > 5
          return texture (textures[5], pos);
#endif
        case 6u:
#if N_TEXTURES > 6
          return texture (textures[6], pos);
#endif
        case 7u:
#if N_TEXTURES > 7
          return texture (textures[7], pos);
#endif
        case 8u:
#if N_TEXTURES > 8
          return texture (textures[8], pos);
#endif
        case 9u:
#if N_TEXTURES > 9
          return texture (textures[9], pos);
#endif
        case 10u:
#if N_TEXTURES > 10
          return texture (textures[10], pos);
#endif
        case 11u:
#if N_TEXTURES > 11
          return texture (textures[11], pos);
#endif
        case 12u:
#if N_TEXTURES > 12
          return texture (textures[12], pos);
#endif
        case 13u:
#if N_TEXTURES > 13
          return texture (textures[13], pos);
#endif
        case 14u:
#if N_TEXTURES > 14
          return texture (textures[14], pos);
#endif
        case 15u:
#if N_TEXTURES > 15
          return texture (textures[15], pos);
#endif
        default:
          break;
        }
    }
  return vec4 (1.0, 0.0, 0.8, 1.0);
}

ivec2
gsk_texture_size (uint id,
                  int  lod)
{
  if ((id & 1u) != 0u)
    {
      switch (id >> 1u)
        {
        case 0u:
#if N_EXTERNAL_TEXTURES > 0
          return textureSize (external_textures[0], lod);
#endif
#if N_EXTERNAL_TEXTURES > 1
        case 1u:
          return textureSize (external_textures[1], lod);
#endif
        case 2u:
#if N_EXTERNAL_TEXTURES > 2
          return textureSize (external_textures[2], lod);
#endif
        case 3u:
#if N_EXTERNAL_TEXTURES > 3
          return textureSize (external_textures[3], lod);
#endif
        case 4u:
#if N_EXTERNAL_TEXTURES > 4
          return textureSize (external_textures[4], lod);
#endif
        case 5u:
#if N_EXTERNAL_TEXTURES > 5
          return textureSize (external_textures[5], lod);
#endif
        default:
          break;
        }
    }
  else
    {
      switch (id >> 1u)
        {
        case 0u:
#if N_TEXTURES > 0
          return textureSize (textures[0], lod);
#endif
        case 1u:
#if N_TEXTURES > 1
          return textureSize (textures[1], lod);
#endif
        case 2u:
#if N_TEXTURES > 2
          return textureSize (textures[2], lod);
#endif
        case 3u:
#if N_TEXTURES > 3
          return textureSize (textures[3], lod);
#endif
        case 4u:
#if N_TEXTURES > 4
          return textureSize (textures[4], lod);
#endif
        case 5u:
#if N_TEXTURES > 5
          return textureSize (textures[5], lod);
#endif
        case 6u:
#if N_TEXTURES > 6
          return textureSize (textures[6], lod);
#endif
        case 7u:
#if N_TEXTURES > 7
          return textureSize (textures[7], lod);
#endif
        case 8u:
#if N_TEXTURES > 8
          return textureSize (textures[8], lod);
#endif
        case 9u:
#if N_TEXTURES > 9
          return textureSize (textures[9], lod);
#endif
        case 10u:
#if N_TEXTURES > 10
          return textureSize (textures[10], lod);
#endif
        case 11u:
#if N_TEXTURES > 11
          return textureSize (textures[11], lod);
#endif
        case 12u:
#if N_TEXTURES > 12
          return textureSize (textures[12], lod);
#endif
        case 13u:
#if N_TEXTURES > 13
          return textureSize (textures[13], lod);
#endif
        case 14u:
#if N_TEXTURES > 14
          return textureSize (textures[14], lod);
#endif
        case 15u:
#if N_TEXTURES > 15
          return textureSize (textures[15], lod);
#endif
        default:
          break;
        }
    }
  return ivec2 (1, 1);
}

vec4
gsk_texel_fetch (uint  id,
                 ivec2 pos,
                 int   lod)
{
  if ((id & 1u) != 0u)
    {
      switch (id >> 1u)
        {
        case 0u:
#if N_EXTERNAL_TEXTURES > 0
          return texelFetch (external_textures[0], pos, lod);
#endif
#if N_EXTERNAL_TEXTURES > 1
        case 1u:
          return texelFetch (external_textures[1], pos, lod);
#endif
        case 2u:
#if N_EXTERNAL_TEXTURES > 2
          return texelFetch (external_textures[2], pos, lod);
#endif
        case 3u:
#if N_EXTERNAL_TEXTURES > 3
          return texelFetch (external_textures[3], pos, lod);
#endif
        case 4u:
#if N_EXTERNAL_TEXTURES > 4
          return texelFetch (external_textures[4], pos, lod);
#endif
        case 5u:
#if N_EXTERNAL_TEXTURES > 5
          return texelFetch (external_textures[5], pos, lod);
#endif
        default:
          break;
        }
    }
  else
    {
      switch (id >> 1u)
        {
        case 0u:
#if N_TEXTURES > 0
          return texelFetch (textures[0], pos, lod);
#endif
        case 1u:
#if N_TEXTURES > 1
          return texelFetch (textures[1], pos, lod);
#endif
        case 2u:
#if N_TEXTURES > 2
          return texelFetch (textures[2], pos, lod);
#endif
        case 3u:
#if N_TEXTURES > 3
          return texelFetch (textures[3], pos, lod);
#endif
        case 4u:
#if N_TEXTURES > 4
          return texelFetch (textures[4], pos, lod);
#endif
        case 5u:
#if N_TEXTURES > 5
          return texelFetch (textures[5], pos, lod);
#endif
        case 6u:
#if N_TEXTURES > 6
          return texelFetch (textures[6], pos, lod);
#endif
        case 7u:
#if N_TEXTURES > 7
          return texelFetch (textures[7], pos, lod);
#endif
        case 8u:
#if N_TEXTURES > 8
          return texelFetch (textures[8], pos, lod);
#endif
        case 9u:
#if N_TEXTURES > 9
          return texelFetch (textures[9], pos, lod);
#endif
        case 10u:
#if N_TEXTURES > 10
          return texelFetch (textures[10], pos, lod);
#endif
        case 11u:
#if N_TEXTURES > 11
          return texelFetch (textures[11], pos, lod);
#endif
        case 12u:
#if N_TEXTURES > 12
          return texelFetch (textures[12], pos, lod);
#endif
        case 13u:
#if N_TEXTURES > 13
          return texelFetch (textures[13], pos, lod);
#endif
        case 14u:
#if N_TEXTURES > 14
          return texelFetch (textures[14], pos, lod);
#endif
        case 15u:
#if N_TEXTURES > 15
          return texelFetch (textures[15], pos, lod);
#endif
        default:
          break;
        }
    }
  return vec4 (1.0, 0.0, 0.8, 1.0);
}

#else /* !GSK_GLES */

#define gsk_texture(id, pos) texture (textures[id >> 1], pos)
#define gsk_texture_size(id, lod) textureSize (textures[id >> 1], lod)
#define gsk_texel_fetch(id, pos, lod) texelFetch (textures[id >> 1], pos, lod)

#endif

layout(location = 0) out vec4 out_color;
void
gsk_set_output_color (vec4 color)
{
  out_color = color;
}

#endif
#endif

#ifndef _COLOR_
#define _COLOR_

vec4
color_premultiply (vec4 color)
{
  return vec4 (color.rgb, 1.0) * color.a;
}

vec4
color_unpremultiply (vec4 color)
{
  return color.a > 0.0 ? color / vec4 (color.aaa, 1.0) : color;
}

float
luminance (vec3 color)
{
  return dot (vec3 (0.2126, 0.7152, 0.0722), color);
}

#endif /* _COLOR_ */
#ifndef _RECT_
#define _RECT_

struct Rect
{
  /* x,y and y,w make up the 2 points of this rect,
     note that this is not containing width or height */
  vec4 bounds;
};

Rect
rect_new_size (vec4 coords)
{
  return Rect (coords + vec4 (0.0, 0.0, coords.xy));
}

Rect
rect_from_gsk (vec4 coords)
{
  Rect result = rect_new_size (coords);
  result.bounds *= GSK_GLOBAL_SCALE.xyxy;
  return result;
}

float
rect_distance (Rect r, vec2 p)
{
  vec4 distance = (r.bounds - p.xyxy) * vec4(1.0, 1.0, -1.0, -1.0);
  vec2 max2 = max (distance.xy, distance.zw);
  return length (max (max2, 0.0)) + min (max(max2.x, max2.y), 0.0);
}

vec2
rect_size (Rect r)
{
  return r.bounds.zw - r.bounds.xy;
}

Rect
rect_round_larger (Rect r)
{
  return Rect (vec4 (floor(r.bounds.xy), ceil (r.bounds.zw)));
}

Rect
rect_round_larger_smaller (Rect r)
{
  return Rect (mix (floor(r.bounds), ceil (r.bounds), bvec4(0, 1, 1, 0)));
}

Rect
rect_round_smaller_larger (Rect r)
{
  return Rect (mix (floor(r.bounds), ceil (r.bounds), bvec4(1, 0, 0, 1)));
}

Rect
rect_intersect (Rect a, Rect b)
{
  vec4 result = vec4(max(a.bounds.xy, b.bounds.xy), min(a.bounds.zw, b.bounds.zw));
  if (any (greaterThanEqual (result.xy, result.zw)))
    return Rect (vec4(0.0));
  return Rect(result);
}

Rect
rect_union (Rect a, Rect b)
{
  return Rect (vec4 (min (a.bounds.xy, b.bounds.xy), max (a.bounds.zw, b.bounds.zw)));
}

vec2
rect_get_coord (Rect r, vec2 pt)
{
  return (pt - r.bounds.xy) / rect_size (r);
}

#ifdef GSK_FRAGMENT_SHADER

float
rect_coverage (Rect r,
               vec2 p,
               vec2 dFdp)
{
  Rect prect = Rect(vec4(p - 0.5 * dFdp, p + 0.5 * dFdp));
  Rect coverect = rect_intersect (r, prect);
  vec2 coverage = rect_size(coverect) / dFdp;
  return coverage.x * coverage.y;
}

float
rect_coverage (Rect r, vec2 p)
{
  return rect_coverage (r, p, abs (fwidth (p)));
}


#endif

#endif
#ifndef _ROUNDED_RECT_
#define _ROUNDED_RECT_

#ifndef _ELLIPSE_
#define _ELLIPSE_

struct Ellipse
{
  vec2 center;
  vec2 radius;
};

float
ellipse_distance (Ellipse r, vec2 p)
{
  vec2 e = r.radius;
  p = p - r.center;

  if (e.x == e.y)
    return length (p) - e.x;

  /* from https://www.shadertoy.com/view/tt3yz7 */
  vec2 pAbs = abs(p);
  vec2 ei = 1.0 / e;
  vec2 e2 = e*e;
  vec2 ve = ei * vec2(e2.x - e2.y, e2.y - e2.x);
  
  vec2 t = vec2(0.70710678118654752, 0.70710678118654752);
  for (int i = 0; i < 3; i++) {
      vec2 v = ve*t*t*t;
      vec2 u = normalize(pAbs - v) * length(t * e - v);
      vec2 w = ei * (v + u);
      t = normalize(clamp(w, 0.0, 1.0));
  }
  
  vec2 nearestAbs = t * e;
  float dist = length(pAbs - nearestAbs);
  return dot(pAbs, pAbs) < dot(nearestAbs, nearestAbs) ? -dist : dist;
}

#endif

struct RoundedRect
{
  vec4 bounds;
  vec4 corner_widths;
  vec4 corner_heights;
};

RoundedRect
rounded_rect_from_rect (Rect r)
{
  return RoundedRect (r.bounds, vec4 (0.0), vec4 (0.0));
}

RoundedRect
rounded_rect_from_gsk (mat3x4 gsk_rounded_rect)
{
  return RoundedRect ((gsk_rounded_rect[0].xyxy + vec4 (0.0, 0.0, gsk_rounded_rect[0].zw)) * GSK_GLOBAL_SCALE.xyxy,
                      gsk_rounded_rect[1] * GSK_GLOBAL_SCALE.xxxx,
                      gsk_rounded_rect[2] * GSK_GLOBAL_SCALE.yyyy);
}

float
rounded_rect_distance (RoundedRect r, vec2 p)
{
  Rect bounds = Rect(vec4(r.bounds));

  float bounds_distance = rect_distance (bounds, p);
                      
  Ellipse tl = Ellipse (r.bounds.xy + vec2( r.corner_widths.x,  r.corner_heights.x),
                        vec2(r.corner_widths.x, r.corner_heights.x));
  Ellipse tr = Ellipse (r.bounds.zy + vec2(-r.corner_widths.y,  r.corner_heights.y),
                        vec2(r.corner_widths.y, r.corner_heights.y));
  Ellipse br = Ellipse (r.bounds.zw + vec2(-r.corner_widths.z, -r.corner_heights.z),
                        vec2(r.corner_widths.z, r.corner_heights.z));
  Ellipse bl = Ellipse (r.bounds.xw + vec2( r.corner_widths.w, -r.corner_heights.w),
                        vec2(r.corner_widths.w, r.corner_heights.w));

 vec4 distances = vec4(ellipse_distance (tl, p),
                       ellipse_distance (tr, p),
                       ellipse_distance (br, p),
                       ellipse_distance (bl, p));

  bvec4 is_out = bvec4(p.x < tl.center.x && p.y < tl.center.y,
                       p.x > tr.center.x && p.y < tr.center.y,
                       p.x > br.center.x && p.y > br.center.y,
                       p.x < bl.center.x && p.y > bl.center.y);
  distances = mix (vec4(bounds_distance), distances, is_out);

  vec2 max2 = max (distances.xy, distances.zw);
  return max (max2.x, max2.y);
}

RoundedRect
rounded_rect_shrink (RoundedRect r, vec4 amount)
{
  vec4 new_bounds = r.bounds + vec4(1.0,1.0,-1.0,-1.0) * amount.wxyz;
  vec4 new_widths = max (r.corner_widths - sign (r.corner_widths) * amount.wyyw, 0.0);
  vec4 new_heights = max (r.corner_heights - sign (r.corner_heights) * amount.xxzz, 0.0);
  new_widths = min (new_widths, new_bounds.z - new_bounds.x);
  new_heights = min (new_heights, new_bounds.w - new_bounds.y);
              
  return RoundedRect (new_bounds, new_widths, new_heights);
}

void
rounded_rect_scale (inout RoundedRect r,
                    vec2              scale)
{
  r.bounds *= scale.xyxy;
  r.corner_widths *= scale.xxxx;
  r.corner_heights *= scale.yyyy;
}

void
rounded_rect_offset (inout RoundedRect r,
                     vec2              offset)
{
  r.bounds += offset.xyxy;
}

bool
rounded_rect_is_slicable (RoundedRect r)
{
  vec2 size = rect_size (Rect (r.bounds));
  return (r.corner_widths[TOP_LEFT] + r.corner_widths[BOTTOM_RIGHT] <= size.x ||
          r.corner_heights[TOP_LEFT] + r.corner_heights[BOTTOM_RIGHT] <= size.y)
      && (r.corner_widths[BOTTOM_LEFT] + r.corner_widths[TOP_RIGHT] <= size.x ||
          r.corner_heights[BOTTOM_LEFT] + r.corner_heights[TOP_RIGHT] <= size.y);
}

Rect
rounded_rect_intersection_fallback_slice (RoundedRect outside,
                                          RoundedRect inside,
                                          uint        slice)
{
  switch (slice)
    {
    default:
    case SLICE_TOP:
    case SLICE_RIGHT:
    case SLICE_BOTTOM:
    case SLICE_LEFT:
      return Rect (vec4 (0.0));

    case SLICE_TOP_LEFT:
      return Rect (vec4 (outside.bounds.xy, 0.5 * (outside.bounds.xy + outside.bounds.zw)));

    case SLICE_TOP_RIGHT:
      return Rect (vec4 (0.5 * (outside.bounds.x + outside.bounds.z), outside.bounds.y,
                         outside.bounds.z, 0.5 * (outside.bounds.y + outside.bounds.w)));

    case SLICE_BOTTOM_RIGHT:
      return Rect (vec4 (0.5 * (outside.bounds.xy + outside.bounds.zw), outside.bounds.zw));

    case SLICE_BOTTOM_LEFT:
      return Rect (vec4 (outside.bounds.x, 0.5 * (outside.bounds.y + outside.bounds.w),
                         0.5 * (outside.bounds.x + outside.bounds.z), outside.bounds.w));
    }
}

Rect
rounded_rect_intersection_slice (RoundedRect outside,
                                 RoundedRect inside,
                                 uint        slice)
{
  float left, right, top, bottom;

  if (!rounded_rect_is_slicable (outside) ||
      !rounded_rect_is_slicable (inside))
    return rounded_rect_intersection_fallback_slice (outside, inside, slice);

  switch (slice)
    {
    case SLICE_TOP_LEFT:
      return Rect (vec4 (outside.bounds.x,
                         outside.bounds.y,
                         max (outside.bounds.x + outside.corner_widths[TOP_LEFT],
                              inside.bounds.x + inside.corner_widths[TOP_LEFT]),
                         max (outside.bounds.y + outside.corner_heights[TOP_LEFT],
                              inside.bounds.y + inside.corner_heights[TOP_LEFT])));

    case SLICE_TOP:
      left = max (outside.bounds.x + outside.corner_widths[TOP_LEFT],
                  inside.bounds.x + inside.corner_widths[TOP_LEFT]);
      right = min (outside.bounds.z - outside.corner_widths[TOP_RIGHT],
                   inside.bounds.z - inside.corner_widths[TOP_RIGHT]);
      return Rect (vec4 (left,
                         outside.bounds.y,
                         max (left, right),
                         max (outside.bounds.y, inside.bounds.y)));

    case SLICE_TOP_RIGHT:
      left = max (min (outside.bounds.z - outside.corner_widths[TOP_RIGHT],
                       inside.bounds.z - inside.corner_widths[TOP_RIGHT]),
                  max (outside.bounds.x + outside.corner_widths[TOP_LEFT],
                       inside.bounds.x + inside.corner_widths[TOP_LEFT]));
      return Rect (vec4 (left,
                         outside.bounds.y,
                         outside.bounds.z,
                         max (outside.bounds.y + outside.corner_heights[TOP_RIGHT],
                              inside.bounds.y + inside.corner_heights[TOP_RIGHT])));

    case SLICE_RIGHT:
      top = max (outside.bounds.y + outside.corner_heights[TOP_RIGHT],
                 inside.bounds.y + inside.corner_heights[TOP_RIGHT]);
      bottom = min (outside.bounds.w - outside.corner_heights[BOTTOM_RIGHT],
                    inside.bounds.w - inside.corner_heights[BOTTOM_RIGHT]);
      return Rect (vec4 (min (outside.bounds.z, inside.bounds.z),
                         top,
                         outside.bounds.z,
                         max (bottom, top)));

    case SLICE_BOTTOM_RIGHT:
      left = max (min (outside.bounds.z - outside.corner_widths[BOTTOM_RIGHT],
                       inside.bounds.z - inside.corner_widths[BOTTOM_RIGHT]),
                  max (outside.bounds.x + outside.corner_widths[BOTTOM_LEFT],
                       inside.bounds.x + inside.corner_widths[BOTTOM_LEFT]));
      bottom = max (min (outside.bounds.w - outside.corner_heights[BOTTOM_RIGHT],
                         inside.bounds.w - inside.corner_heights[BOTTOM_RIGHT]),
                    max (outside.bounds.y + outside.corner_heights[TOP_RIGHT],
                         inside.bounds.y + inside.corner_heights[TOP_RIGHT]));
      return Rect (vec4 (left,
                         bottom,
                         outside.bounds.z,
                         outside.bounds.w));

    case SLICE_BOTTOM:
      left = max (outside.bounds.x + outside.corner_widths[BOTTOM_LEFT],
                  inside.bounds.x + inside.corner_widths[BOTTOM_LEFT]);
      right = min (outside.bounds.z - outside.corner_widths[BOTTOM_RIGHT],
                   inside.bounds.z - inside.corner_widths[BOTTOM_RIGHT]);
      return Rect (vec4 (left,
                         min (outside.bounds.w, inside.bounds.w),
                         max (left, right),
                         outside.bounds.w));

    case SLICE_BOTTOM_LEFT:
      bottom = max (min (outside.bounds.w - outside.corner_heights[BOTTOM_LEFT],
                         inside.bounds.w - inside.corner_heights[BOTTOM_LEFT]),
                    max (outside.bounds.y + outside.corner_heights[TOP_LEFT],
                         inside.bounds.y + inside.corner_heights[TOP_LEFT]));
      return Rect (vec4 (outside.bounds.x,
                         bottom,
                         max (outside.bounds.x + outside.corner_widths[BOTTOM_LEFT],
                              inside.bounds.x + inside.corner_widths[BOTTOM_LEFT]),
                         outside.bounds.w));

    case SLICE_LEFT:
      top = max (outside.bounds.y + outside.corner_heights[TOP_LEFT],
                 inside.bounds.y + inside.corner_heights[TOP_LEFT]);
      bottom = min (outside.bounds.w - outside.corner_heights[BOTTOM_LEFT],
                    inside.bounds.w - inside.corner_heights[BOTTOM_LEFT]);
      return Rect (vec4 (outside.bounds.x,
                         top,
                         max (outside.bounds.x, inside.bounds.x),
                         max (bottom, top)));

    default:
      return Rect (vec4 (0.0));
    }
}

#ifdef GSK_FRAGMENT_SHADER

float
rounded_rect_coverage (RoundedRect r, vec2 p)
{
  vec2 fw = abs (fwidth (p));
  float distance_scale = max (fw.x, fw.y);
  float distance = rounded_rect_distance (r, p) / distance_scale;
  float coverage = 0.5 - distance;

  return clamp (coverage, 0.0, 1.0);
}

#endif

#endif

#define PI 3.1415926535897932384626433832795
#define SQRT1_2 1.4142135623730951

Rect
rect_clip (Rect r)
{
  if (GSK_SHADER_CLIP == GSK_GPU_SHADER_CLIP_NONE)
    return r;
  else
    return rect_intersect (r, rect_from_gsk (GSK_GLOBAL_CLIP_RECT));
}

#ifdef GSK_VERTEX_SHADER

const vec2 offsets[6] = vec2[6](vec2(0.0, 0.0),
                                vec2(1.0, 0.0),
                                vec2(0.0, 1.0),
                                vec2(0.0, 1.0),
                                vec2(1.0, 0.0),
                                vec2(1.0, 1.0));

void
gsk_set_position (vec2 pos)
{
  gl_Position = GSK_GLOBAL_MVP * vec4 (pos, 0.0, 1.0);
}

vec2
rect_get_position (Rect rect)
{
  Rect r = rect_round_larger (rect_clip (rect));

  vec2 pos = mix (r.bounds.xy, r.bounds.zw, offsets[GSK_VERTEX_INDEX]);

  return pos;
}

vec2
border_get_position (RoundedRect outside,
                     RoundedRect inside)
{
  uint slice_index = uint (GSK_VERTEX_INDEX) / 6u;
  uint vert_index = uint (GSK_VERTEX_INDEX) % 6u;

  Rect rect = rounded_rect_intersection_slice (outside, inside, slice_index);

  switch (slice_index)
    {
    case SLICE_TOP_LEFT:
      rect = rect_round_larger (rect);
      rect.bounds = rect.bounds.xwzy;
      break;
    case SLICE_TOP:
      rect = rect_round_smaller_larger (rect);
      break;
    case SLICE_TOP_RIGHT:
      rect = rect_round_larger (rect);
      break;
    case SLICE_RIGHT:
      rect = rect_round_larger_smaller (rect);
      break;
    case SLICE_BOTTOM_RIGHT:
      rect = rect_round_larger (rect);
      rect.bounds = rect.bounds.zyxw;
      break;
    case SLICE_BOTTOM:
      rect = rect_round_smaller_larger (rect);
      break;
    case SLICE_BOTTOM_LEFT:
      rect = rect_round_larger (rect);
      rect.bounds = rect.bounds.zwxy;
      break;
    case SLICE_LEFT:
      rect = rect_round_larger_smaller (rect);
      break;
    }

  vec2 pos = mix (rect.bounds.xy, rect.bounds.zw, offsets[vert_index]);

  return pos;
}

vec2
scale_tex_coord (vec2 in_pos,
                 Rect in_rect,
                 vec4 tex_rect)
{
  return tex_rect.xy + (in_pos - in_rect.bounds.xy) / rect_size (in_rect) * tex_rect.zw;
}

void            run                             (out vec2 pos);

void
main (void)
{
  vec2 pos;

  run (pos);

  gsk_set_position (pos);
}

#endif /* GSK_VERTEX_SHADER */


#ifdef GSK_FRAGMENT_SHADER

vec4
gsk_texture_straight_alpha (uint tex_id,
                            vec2 pos)
{
  vec2 size = vec2 (gsk_texture_size (tex_id, 0));
  pos *= size;
  size -= vec2 (1.0);
  /* GL_CLAMP_TO_EDGE */
  pos = clamp (pos - 0.5, vec2 (0.0), size);
  ivec2 ipos = ivec2 (pos);
  pos = fract (pos);
  vec4 tl = gsk_texel_fetch (tex_id, ipos, 0);
  tl.rgb *= tl.a;
  vec4 tr = gsk_texel_fetch (tex_id, ipos + ivec2(1, 0), 0);
  tr.rgb *= tr.a;
  vec4 bl = gsk_texel_fetch (tex_id, ipos + ivec2(0, 1), 0);
  bl.rgb *= bl.a;
  vec4 br = gsk_texel_fetch (tex_id, ipos + ivec2(1, 1), 0);
  br.rgb *= br.a;
  return mix (mix (tl, tr, pos.x), mix (bl, br, pos.x), pos.y);
}

void            run                             (out vec4 color,
                                                 out vec2 pos);

void
main_clip_none (void)
{
  vec4 color;
  vec2 pos;

  run (color, pos);

  gsk_set_output_color (color);
}

void
main_clip_rect (void)
{
  vec4 color;
  vec2 pos;

  run (color, pos);

  Rect clip = rect_from_gsk (GSK_GLOBAL_CLIP_RECT);

  float coverage = rect_coverage (clip, pos);
  color *= coverage;

  gsk_set_output_color (color);
}

void
main_clip_rounded (void)
{
  vec4 color;
  vec2 pos;

  run (color, pos);

  RoundedRect clip = rounded_rect_from_gsk (GSK_GLOBAL_CLIP);

  float coverage = rounded_rect_coverage (clip, pos);
  color *= coverage;

  gsk_set_output_color (color);
}

void
main (void)
{
  if (GSK_SHADER_CLIP == GSK_GPU_SHADER_CLIP_NONE)
    main_clip_none ();
  else if (GSK_SHADER_CLIP == GSK_GPU_SHADER_CLIP_RECT)
    main_clip_rect ();
  else if (GSK_SHADER_CLIP == GSK_GPU_SHADER_CLIP_ROUNDED)
    main_clip_rounded ();
}

#endif /* GSK_FRAGMENT_SHADER */

#endif

#ifndef _BLENDMODE_
#define _BLENDMODE_

vec4
composite (vec4 Cs, vec4 Cb, vec3 B)
{
  float ao = Cs.a + Cb.a * (1.0 - Cs.a);
  vec3 Co = (Cs.a*(1.0 - Cb.a)*Cs.rgb + Cs.a*Cb.a*B + (1.0 - Cs.a)*Cb.a*Cb.rgb) / ao;
  return vec4(Co, ao);
}

vec4
normal (vec4 Cs, vec4 Cb)
{
  return composite (Cs, Cb, Cs.rgb);
}

vec4
multiply (vec4 Cs, vec4 Cb)
{
  return composite (Cs, Cb, Cs.rgb * Cb.rgb);
}

vec4
difference (vec4 Cs, vec4 Cb)
{
  return composite (Cs, Cb, abs(Cs.rgb - Cb.rgb));
}

vec4
screen (vec4 Cs, vec4 Cb)
{
  return composite (Cs, Cb, Cs.rgb + Cb.rgb - Cs.rgb * Cb.rgb);
}

float
hard_light (float source, float backdrop)
{
  if (source <= 0.5)
    return 2.0 * backdrop * source;
  else
    return 2.0 * (backdrop + source - backdrop * source) - 1.0;
}

vec4
hard_light (vec4 Cs, vec4 Cb)
{
  vec3 B = vec3 (hard_light (Cs.r, Cb.r),
                 hard_light (Cs.g, Cb.g),
                 hard_light (Cs.b, Cb.b));
  return composite (Cs, Cb, B);
}

float
soft_light (float source, float backdrop)
{
  float db;

  if (backdrop <= 0.25)
    db = ((16.0 * backdrop - 12.0) * backdrop + 4.0) * backdrop;
  else
    db = sqrt (backdrop);

  if (source <= 0.5)
    return backdrop - (1.0 - 2.0 * source) * backdrop * (1.0 - backdrop);
  else
    return backdrop + (2.0 * source - 1.0) * (db - backdrop);
}

vec4
soft_light (vec4 Cs, vec4 Cb)
{
  vec3 B = vec3 (soft_light (Cs.r, Cb.r),
                 soft_light (Cs.g, Cb.g),
                 soft_light (Cs.b, Cb.b));
  return composite (Cs, Cb, B);
}

vec4
overlay (vec4 Cs, vec4 Cb)
{
  vec3 B = vec3 (hard_light (Cb.r, Cs.r),
                 hard_light (Cb.g, Cs.g),
                 hard_light (Cb.b, Cs.b));
  return composite (Cs, Cb, B);
}

vec4
darken (vec4 Cs, vec4 Cb)
{
  vec3 B = min (Cs.rgb, Cb.rgb);
  return composite (Cs, Cb, B);
}

vec4
lighten (vec4 Cs, vec4 Cb)
{
  vec3 B = max (Cs.rgb, Cb.rgb);
  return composite (Cs, Cb, B);
}

float
color_dodge (float source, float backdrop)
{
  return (source == 1.0) ? source : min (backdrop / (1.0 - source), 1.0);
}

vec4
color_dodge (vec4 Cs, vec4 Cb)
{
  vec3 B = vec3 (color_dodge (Cs.r, Cb.r),
                 color_dodge (Cs.g, Cb.g),
                 color_dodge (Cs.b, Cb.b));
  return composite (Cs, Cb, B);
}


float
color_burn (float source, float backdrop)
{
  return (source == 0.0) ? source : max ((1.0 - ((1.0 - backdrop) / source)), 0.0);
}

vec4
color_burn (vec4 Cs, vec4 Cb)
{
  vec3 B = vec3 (color_burn (Cs.r, Cb.r),
                 color_burn (Cs.g, Cb.g),
                 color_burn (Cs.b, Cb.b));
  return composite (Cs, Cb, B);
}

vec4
exclusion (vec4 Cs, vec4 Cb)
{
  vec3 B = Cb.rgb + Cs.rgb - 2.0 * Cb.rgb * Cs.rgb;
  return composite (Cs, Cb, B);
}

float
lum (vec3 c)
{
  return 0.3 * c.r + 0.59 * c.g + 0.11 * c.b;
}

vec3
clip_color (vec3 c)
{
  float l = lum (c);
  float n = min (c.r, min (c.g, c.b));
  float x = max (c.r, max (c.g, c.b));
  if (n < 0.0) c = l + (((c - l) * l) / (l - n));
  if (x > 1.0) c = l + (((c - l) * (1.0 - l)) / (x - l));
  return c;
}

vec3
set_lum (vec3 c, float l)
{
  float d = l - lum (c);
  return clip_color (vec3 (c.r + d, c.g + d, c.b + d));
}

float
sat (vec3 c)
{
  return max (c.r, max (c.g, c.b)) - min (c.r, min (c.g, c.b));
}

vec3
set_sat (vec3 c, float s)
{
  float cmin = min (c.r, min (c.g, c.b));
  float cmax = max (c.r, max (c.g, c.b));
  vec3 res;

  if (cmax == cmin)
    res = vec3 (0, 0, 0);
  else
    {
      if (c.r == cmax)
        {
          if (c.g == cmin)
            {
              res.b = ((c.b - cmin) * s) / (cmax - cmin);
              res.g = 0.0;
            }
          else
            {
              res.g = ((c.g - cmin) * s) / (cmax - cmin);
              res.b = 0.0;
            }
          res.r = s;
        }
      else if (c.g == cmax)
        {
          if (c.r == cmin)
            {
              res.b = ((c.b - cmin) * s) / (cmax - cmin);
              res.r = 0.0;
            }
          else
            {
              res.r = ((c.r - cmin) * s) / (cmax - cmin);
              res.b = 0.0;
            }
          res.g = s;
        }
      else
        {
          if (c.r == cmin)
            {
              res.g = ((c.g - cmin) * s) / (cmax - cmin);
              res.r = 0.0;
            }
          else
            {
              res.r = ((c.r - cmin) * s) / (cmax - cmin);
              res.g = 0.0;
            }
          res.b = s;
        }
    }
  return res;
}

vec4
color (vec4 Cs, vec4 Cb)
{
  vec3 B = set_lum (Cs.rgb, lum (Cb.rgb));
  return composite (Cs, Cb, B);
}

vec4
hue (vec4 Cs, vec4 Cb)
{
  vec3 B = set_lum (set_sat (Cs.rgb, sat (Cb.rgb)), lum (Cb.rgb));
  return composite (Cs, Cb, B);
}

vec4
saturation (vec4 Cs, vec4 Cb)
{
  vec3 B = set_lum (set_sat (Cb.rgb, sat (Cs.rgb)), lum (Cb.rgb));
  return composite (Cs, Cb, B);
}

vec4
luminosity (vec4 Cs, vec4 Cb)
{
  vec3 B = set_lum (Cb.rgb, lum (Cs.rgb));
  return composite (Cs, Cb, B);
}

vec4
blend_mode (vec4 bottom_color,
            vec4 top_color,
            uint u_mode)
{
  vec4 result;

  if (u_mode == 0u)
    result = normal(top_color, bottom_color);
  else if (u_mode == 1u)
    result = multiply(top_color, bottom_color);
  else if (u_mode == 2u)
    result = screen(top_color, bottom_color);
  else if (u_mode == 3u)
    result = overlay(top_color, bottom_color);
  else if (u_mode == 4u)
    result = darken(top_color, bottom_color);
  else if (u_mode == 5u)
    result = lighten(top_color, bottom_color);
  else if (u_mode == 6u)
    result = color_dodge(top_color, bottom_color);
  else if (u_mode == 7u)
    result = color_burn(top_color, bottom_color);
  else if (u_mode == 8u)
    result = hard_light(top_color, bottom_color);
  else if (u_mode == 9u)
    result = soft_light(top_color, bottom_color);
  else if (u_mode == 10u)
    result = difference(top_color, bottom_color);
  else if (u_mode == 11u)
    result = exclusion(top_color, bottom_color);
  else if (u_mode == 12u)
    result = color(top_color, bottom_color);
  else if (u_mode == 13u)
    result = hue(top_color, bottom_color);
  else if (u_mode == 14u)
    result = saturation(top_color, bottom_color);
  else if (u_mode == 15u)
    result = luminosity(top_color, bottom_color);
  else
    result = vec4 (10.0, 0.0, 0.8, 1.0);

  return result;
}

#endif

PASS(0) vec2 _pos;
PASS_FLAT(1) Rect _bottom_rect;
PASS_FLAT(2) Rect _top_rect;
PASS(3) vec2 _bottom_coord;
PASS(4) vec2 _top_coord;
PASS_FLAT(5) uint _bottom_id;
PASS_FLAT(6) uint _top_id;
PASS_FLAT(7) float _opacity;


#ifdef GSK_VERTEX_SHADER

IN(0) vec4 in_rect;
IN(1) vec4 in_bottom_rect;
IN(2) uint in_bottom_id;
IN(3) vec4 in_top_rect;
IN(4) uint in_top_id;
IN(5) float in_opacity;

void
run (out vec2 pos)
{
  Rect r = rect_from_gsk (in_rect);
  
  pos = rect_get_position (r);

  _pos = pos;
  _opacity = in_opacity;

  Rect bottom_rect = rect_from_gsk (in_bottom_rect);
  _bottom_rect = bottom_rect;
  _bottom_coord = rect_get_coord (bottom_rect, pos);
  _bottom_id = in_bottom_id;

  Rect top_rect = rect_from_gsk (in_top_rect);
  _top_rect = top_rect;
  _top_coord = rect_get_coord (top_rect, pos);
  _top_id = in_top_id;
}

#endif



#ifdef GSK_FRAGMENT_SHADER

void
run (out vec4 color,
     out vec2 position)
{
  color = _opacity * blend_mode (gsk_texture (_bottom_id, _bottom_coord)
                                 * rect_coverage (_bottom_rect, _pos),
                                 gsk_texture (_top_id, _top_coord)
                                 * rect_coverage (_top_rect, _pos),
                                 GSK_VARIATION);
  position = _pos;
}

#endif
