TERN_GLYPHS 6u
#define GSK_GPU_PATTERN_LINEAR_GRADIENT 7u
#define GSK_GPU_PATTERN_REPEATING_LINEAR_GRADIENT 8u
#define GSK_GPU_PATTERN_RADIAL_GRADIENT 9u
#define GSK_GPU_PATTERN_REPEATING_RADIAL_GRADIENT 10u
#define GSK_GPU_PATTERN_CONIC_GRADIENT 11u
#define GSK_GPU_PATTERN_CLIP 12u
#define GSK_GPU_PATTERN_ROUNDED_CLIP 13u
#define GSK_GPU_PATTERN_REPEAT_PUSH 14u
#define GSK_GPU_PATTERN_POSITION_POP 15u
#define GSK_GPU_PATTERN_PUSH_COLOR 16u
#define GSK_GPU_PATTERN_POP_CROSS_FADE 17u
#define GSK_GPU_PATTERN_POP_MASK_ALPHA 18u
#define GSK_GPU_PATTERN_POP_MASK_INVERTED_ALPHA 19u
#define GSK_GPU_PATTERN_POP_MASK_LUMINANCE 20u
#define GSK_GPU_PATTERN_POP_MASK_INVERTED_LUMINANCE 21u
#define GSK_GPU_PATTERN_AFFINE 22u
#define GSK_GPU_PATTERN_BLEND_DEFAULT 23u
#define GSK_GPU_PATTERN_BLEND_MULTIPLY 24u
#define GSK_GPU_PATTERN_BLEND_SCREEN 25u
#define GSK_GPU_PATTERN_BLEND_OVERLAY 26u
#define GSK_GPU_PATTERN_BLEND_DARKEN 27u
#define GSK_GPU_PATTERN_BLEND_LIGHTEN 28u
#define GSK_GPU_PATTERN_BLEND_COLOR_DODGE 29u
#define GSK_GPU_PATTERN_BLEND_COLOR_BURN 30u
#define GSK_GPU_PATTERN_BLEND_HARD_LIGHT 31u
#define GSK_GPU_PATTERN_BLEND_SOFT_LIGHT 32u
#define GSK_GPU_PATTERN_BLEND_DIFFERENCE 33u
#define GSK_GPU_PATTERN_BLEND_EXCLUSION 34u
#define GSK_GPU_PATTERN_BLEND_COLOR 35u
#define GSK_GPU_PATTERN_BLEND_HUE 36u
#define GSK_GPU_PATTERN_BLEND_SATURATION 37u
#define GSK_GPU_PATTERN_BLEND_LUMINOSITY 38u

#define GSK_MASK_MODE_ALPHA 0u
#define GSK_MASK_MODE_INVERTED_ALPHA 1u
#define GSK_MASK_MODE_LUMINANCE 2u
#define GSK_MASK_MODE_INVERTED_LUMINANCE 3u

#define TOP 0u
#define RIGHT 1u
#define BOTTOM 2u
#define LEFT 3u

#define TOP_LEFT 0u
#define TOP_RIGHT 1u
#define BOTTOM_RIGHT 2u
#define BOTTOM_LEFT 3u

#define SLICE_TOP_LEFT 0u
#define SLICE_TOP 1u
#define SLICE_TOP_RIGHT 2u
#define SLICE_RIGHT 3u
#define SLICE_BOTTOM_RIGHT 4u
#define SLICE_BOTTOM 5u
#define SLICE_BOTTOM_LEFT 6u
#define SLICE_LEFT 7u

#endif

#ifdef VULKAN
#ifdef HAVE_VULKAN_1_2
#extension GL_EXT_nonuniform_qualifier : enable
#endif


layout(push_constant) uniform PushConstants {
    mat4 mvp;
    mat3x4 clip;
    vec2 scale;
} push;

layout(constant_id=0) const uint GSK_SHADER_CLIP = GSK_GPU_SHADER_CLIP_NONE;
layout(constant_id=1) const uint GSK_N_IMMUTABLE_SAMPLERS = 32;
layout(constant_id=2) const uint GSK_N_SAMPLERS = 32;
layout(constant_id=3) const uint GSK_N_BUFFERS = 32;
layout(constant_id=4) const uint GSK_VARIATION = 0;

#define GSK_GLOBAL_MVP push.mvp
#define GSK_GLOBAL_CLIP push.clip
#define GSK_GLOBAL_CLIP_RECT push.clip[0]
#define GSK_GLOBAL_SCALE push.scale

#define GSK_VERTEX_INDEX gl_VertexIndex

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


#ifdef GSK_FRAGMENT_SHADER
#define PASS(_loc) layout(location = _loc) in
#define PASS_FLAT(_loc) layout(location = _loc) flat in

layout(set = 0, binding = 0) uniform sampler2D immutable_textures[GSK_N_IMMUTABLE_SAMPLERS];
layout(set = 0, binding = 1) uniform sampler2D textures[GSK_N_SAMPLERS];
layout(set = 1, binding = 0) readonly buffer FloatBuffers {
  float floats[];
} buffers[GSK_N_BUFFERS];

layout(location = 0) out vec4 out_color;

vec4
gsk_texture (uint id,
             vec2 pos)
{
  if ((id & 1) != 0)
    {
      id >>= 1;
      if (id < 16)
        {
          if (id < 8)
            {
              if (id < 4)
                {
                  if (id < 2)
                    {
                      if (id < 1)
                        return texture (immutable_textures[0], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 1)
                        return texture (immutable_textures[1], pos);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 2)
                    {
                      if (id < 3)
                        return texture (immutable_textures[2], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 3)
                        return texture (immutable_textures[3], pos);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 4)
                {
                  if (id < 6)
                    {
                      if (id < 5)
                        return texture (immutable_textures[4], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 5)
                        return texture (immutable_textures[5], pos);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 6)
                    {
                      if (id < 7)
                        return texture (immutable_textures[6], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 7)
                        return texture (immutable_textures[7], pos);
                    }
                }
            }
          else if (GSK_N_IMMUTABLE_SAMPLERS > 8)
            {
              if (id < 12)
                {
                  if (id < 10)
                    {
                      if (id < 9)
                        return texture (immutable_textures[8], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 9)
                        return texture (immutable_textures[9], pos);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 10)
                    {
                      if (id < 11)
                        return texture (immutable_textures[10], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 11)
                        return texture (immutable_textures[11], pos);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 12)
                {
                  if (id < 14)
                    {
                      if (id < 13)
                        return texture (immutable_textures[12], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 13)
                        return texture (immutable_textures[13], pos);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 14)
                    {
                      if (id < 15)
                        return texture (immutable_textures[14], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 15)
                        return texture (immutable_textures[15], pos);
                    }
                }
            }
        }
      else if (GSK_N_IMMUTABLE_SAMPLERS > 16)
        {
          if (id < 24)
            {
              if (id < 20)
                {
                  if (id < 18)
                    {
                      if (id < 17)
                        return texture (immutable_textures[16], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 17)
                        return texture (immutable_textures[17], pos);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 18)
                    {
                      if (id < 19)
                        return texture (immutable_textures[18], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 19)
                        return texture (immutable_textures[19], pos);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 20)
                {
                  if (id < 22)
                    {
                      if (id < 21)
                        return texture (immutable_textures[20], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 21)
                        return texture (immutable_textures[21], pos);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 22)
                    {
                      if (id < 23)
                        return texture (immutable_textures[22], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 23)
                        return texture (immutable_textures[23], pos);
                    }
                }
            }
          else if (GSK_N_IMMUTABLE_SAMPLERS > 24)
            {
              if (id < 28)
                {
                  if (id < 26)
                    {
                      if (id < 25)
                        return texture (immutable_textures[24], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 25)
                        return texture (immutable_textures[25], pos);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 26)
                    {
                      if (id < 27)
                        return texture (immutable_textures[26], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 27)
                        return texture (immutable_textures[27], pos);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 28)
                {
                  if (id < 30)
                    {
                      if (id < 29)
                        return texture (immutable_textures[28], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 29)
                        return texture (immutable_textures[29], pos);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 30)
                    {
                      if (id < 31)
                        return texture (immutable_textures[30], pos);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 31)
                        return texture (immutable_textures[31], pos);
                    }
                }
            }
        }
    }
  else
    {
      id >>= 1;
#ifdef HAVE_VULKAN_1_2
      return texture (textures[nonuniformEXT (id)], pos);
#else
      if (id < 16)
        {
          if (id < 8)
            {
              if (id < 4)
                {
                  if (id < 2)
                    {
                      if (id < 1)
                        return texture (textures[0], pos);
                      else if (GSK_N_SAMPLERS > 1)
                        return texture (textures[1], pos);
                    }
                  else if (GSK_N_SAMPLERS > 2)
                    {
                      if (id < 3)
                        return texture (textures[2], pos);
                      else if (GSK_N_SAMPLERS > 3)
                        return texture (textures[3], pos);
                    }
                }
              else if (GSK_N_SAMPLERS > 4)
                {
                  if (id < 6)
                    {
                      if (id < 5)
                        return texture (textures[4], pos);
                      else if (GSK_N_SAMPLERS > 5)
                        return texture (textures[5], pos);
                    }
                  else if (GSK_N_SAMPLERS > 6)
                    {
                      if (id < 7)
                        return texture (textures[6], pos);
                      else if (GSK_N_SAMPLERS > 7)
                        return texture (textures[7], pos);
                    }
                }
            }
          else if (GSK_N_SAMPLERS > 8)
            {
              if (id < 12)
                {
                  if (id < 10)
                    {
                      if (id < 9)
                        return texture (textures[8], pos);
                      else if (GSK_N_SAMPLERS > 9)
                        return texture (textures[9], pos);
                    }
                  else if (GSK_N_SAMPLERS > 10)
                    {
                      if (id < 11)
                        return texture (textures[10], pos);
                      else if (GSK_N_SAMPLERS > 11)
                        return texture (textures[11], pos);
                    }
                }
              else if (GSK_N_SAMPLERS > 12)
                {
                  if (id < 14)
                    {
                      if (id < 13)
                        return texture (textures[12], pos);
                      else if (GSK_N_SAMPLERS > 13)
                        return texture (textures[13], pos);
                    }
                  else if (GSK_N_SAMPLERS > 14)
                    {
                      if (id < 15)
                        return texture (textures[14], pos);
                      else if (GSK_N_SAMPLERS > 15)
                        return texture (textures[15], pos);
                    }
                }
            }
        }
      else if (GSK_N_SAMPLERS > 16)
        {
          if (id < 24)
            {
              if (id < 20)
                {
                  if (id < 18)
                    {
                      if (id < 17)
                        return texture (textures[16], pos);
                      else if (GSK_N_SAMPLERS > 17)
                        return texture (textures[17], pos);
                    }
                  else if (GSK_N_SAMPLERS > 18)
                    {
                      if (id < 19)
                        return texture (textures[18], pos);
                      else if (GSK_N_SAMPLERS > 19)
                        return texture (textures[19], pos);
                    }
                }
              else if (GSK_N_SAMPLERS > 20)
                {
                  if (id < 22)
                    {
                      if (id < 21)
                        return texture (textures[20], pos);
                      else if (GSK_N_SAMPLERS > 21)
                        return texture (textures[21], pos);
                    }
                  else if (GSK_N_SAMPLERS > 22)
                    {
                      if (id < 23)
                        return texture (textures[22], pos);
                      else if (GSK_N_SAMPLERS > 23)
                        return texture (textures[23], pos);
                    }
                }
            }
          else if (GSK_N_SAMPLERS > 24)
            {
              if (id < 28)
                {
                  if (id < 26)
                    {
                      if (id < 25)
                        return texture (textures[24], pos);
                      else if (GSK_N_SAMPLERS > 25)
                        return texture (textures[25], pos);
                    }
                  else if (GSK_N_SAMPLERS > 26)
                    {
                      if (id < 27)
                        return texture (textures[26], pos);
                      else if (GSK_N_SAMPLERS > 27)
                        return texture (textures[27], pos);
                    }
                }
              else if (GSK_N_SAMPLERS > 28)
                {
                  if (id < 30)
                    {
                      if (id < 29)
                        return texture (textures[28], pos);
                      else if (GSK_N_SAMPLERS > 29)
                        return texture (textures[29], pos);
                    }
                  else if (GSK_N_SAMPLERS > 30)
                    {
                      if (id < 31)
                        return texture (textures[30], pos);
                      else if (GSK_N_SAMPLERS > 31)
                        return texture (textures[31], pos);
                    }
                }
            }
        }
#endif
    }
  return vec4 (1.0, 0.0, 0.8, 1.0);
}

ivec2
gsk_texture_size (uint id,
                  int  lod)
{
  if ((id & 1) != 0)
    {
      id >>= 1;
      if (id < 16)
        {
          if (id < 8)
            {
              if (id < 4)
                {
                  if (id < 2)
                    {
                      if (id < 1)
                        return textureSize (immutable_textures[0], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 1)
                        return textureSize (immutable_textures[1], lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 2)
                    {
                      if (id < 3)
                        return textureSize (immutable_textures[2], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 3)
                        return textureSize (immutable_textures[3], lod);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 4)
                {
                  if (id < 6)
                    {
                      if (id < 5)
                        return textureSize (immutable_textures[4], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 5)
                        return textureSize (immutable_textures[5], lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 6)
                    {
                      if (id < 7)
                        return textureSize (immutable_textures[6], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 7)
                        return textureSize (immutable_textures[7], lod);
                    }
                }
            }
          else if (GSK_N_IMMUTABLE_SAMPLERS > 8)
            {
              if (id < 12)
                {
                  if (id < 10)
                    {
                      if (id < 9)
                        return textureSize (immutable_textures[8], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 9)
                        return textureSize (immutable_textures[9], lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 10)
                    {
                      if (id < 11)
                        return textureSize (immutable_textures[10], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 11)
                        return textureSize (immutable_textures[11], lod);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 12)
                {
                  if (id < 14)
                    {
                      if (id < 13)
                        return textureSize (immutable_textures[12], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 13)
                        return textureSize (immutable_textures[13], lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 14)
                    {
                      if (id < 15)
                        return textureSize (immutable_textures[14], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 15)
                        return textureSize (immutable_textures[15], lod);
                    }
                }
            }
        }
      else if (GSK_N_IMMUTABLE_SAMPLERS > 16)
        {
          if (id < 24)
            {
              if (id < 20)
                {
                  if (id < 18)
                    {
                      if (id < 17)
                        return textureSize (immutable_textures[16], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 17)
                        return textureSize (immutable_textures[17], lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 18)
                    {
                      if (id < 19)
                        return textureSize (immutable_textures[18], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 19)
                        return textureSize (immutable_textures[19], lod);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 20)
                {
                  if (id < 22)
                    {
                      if (id < 21)
                        return textureSize (immutable_textures[20], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 21)
                        return textureSize (immutable_textures[21], lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 22)
                    {
                      if (id < 23)
                        return textureSize (immutable_textures[22], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 23)
                        return textureSize (immutable_textures[23], lod);
                    }
                }
            }
          else if (GSK_N_IMMUTABLE_SAMPLERS > 24)
            {
              if (id < 28)
                {
                  if (id < 26)
                    {
                      if (id < 25)
                        return textureSize (immutable_textures[24], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 25)
                        return textureSize (immutable_textures[25], lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 26)
                    {
                      if (id < 27)
                        return textureSize (immutable_textures[26], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 27)
                        return textureSize (immutable_textures[27], lod);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 28)
                {
                  if (id < 30)
                    {
                      if (id < 29)
                        return textureSize (immutable_textures[28], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 29)
                        return textureSize (immutable_textures[29], lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 30)
                    {
                      if (id < 31)
                        return textureSize (immutable_textures[30], lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 31)
                        return textureSize (immutable_textures[31], lod);
                    }
                }
            }
        }
    }
  else
    {
      id >>= 1;
#ifdef HAVE_VULKAN_1_2
      return textureSize (textures[nonuniformEXT (id)], lod);
#else
      if (id < 16)
        {
          if (id < 8)
            {
              if (id < 4)
                {
                  if (id < 2)
                    {
                      if (id < 1)
                        return textureSize (textures[0], lod);
                      else if (GSK_N_SAMPLERS > 1)
                        return textureSize (textures[1], lod);
                    }
                  else if (GSK_N_SAMPLERS > 2)
                    {
                      if (id < 3)
                        return textureSize (textures[2], lod);
                      else if (GSK_N_SAMPLERS > 3)
                        return textureSize (textures[3], lod);
                    }
                }
              else if (GSK_N_SAMPLERS > 4)
                {
                  if (id < 6)
                    {
                      if (id < 5)
                        return textureSize (textures[4], lod);
                      else if (GSK_N_SAMPLERS > 5)
                        return textureSize (textures[5], lod);
                    }
                  else if (GSK_N_SAMPLERS > 6)
                    {
                      if (id < 7)
                        return textureSize (textures[6], lod);
                      else if (GSK_N_SAMPLERS > 7)
                        return textureSize (textures[7], lod);
                    }
                }
            }
          else if (GSK_N_SAMPLERS > 8)
            {
              if (id < 12)
                {
                  if (id < 10)
                    {
                      if (id < 9)
                        return textureSize (textures[8], lod);
                      else if (GSK_N_SAMPLERS > 9)
                        return textureSize (textures[9], lod);
                    }
                  else if (GSK_N_SAMPLERS > 10)
                    {
                      if (id < 11)
                        return textureSize (textures[10], lod);
                      else if (GSK_N_SAMPLERS > 11)
                        return textureSize (textures[11], lod);
                    }
                }
              else if (GSK_N_SAMPLERS > 12)
                {
                  if (id < 14)
                    {
                      if (id < 13)
                        return textureSize (textures[12], lod);
                      else if (GSK_N_SAMPLERS > 13)
                        return textureSize (textures[13], lod);
                    }
                  else if (GSK_N_SAMPLERS > 14)
                    {
                      if (id < 15)
                        return textureSize (textures[14], lod);
                      else if (GSK_N_SAMPLERS > 15)
                        return textureSize (textures[15], lod);
                    }
                }
            }
        }
      else if (GSK_N_SAMPLERS > 16)
        {
          if (id < 24)
            {
              if (id < 20)
                {
                  if (id < 18)
                    {
                      if (id < 17)
                        return textureSize (textures[16], lod);
                      else if (GSK_N_SAMPLERS > 17)
                        return textureSize (textures[17], lod);
                    }
                  else if (GSK_N_SAMPLERS > 18)
                    {
                      if (id < 19)
                        return textureSize (textures[18], lod);
                      else if (GSK_N_SAMPLERS > 19)
                        return textureSize (textures[19], lod);
                    }
                }
              else if (GSK_N_SAMPLERS > 20)
                {
                  if (id < 22)
                    {
                      if (id < 21)
                        return textureSize (textures[20], lod);
                      else if (GSK_N_SAMPLERS > 21)
                        return textureSize (textures[21], lod);
                    }
                  else if (GSK_N_SAMPLERS > 22)
                    {
                      if (id < 23)
                        return textureSize (textures[22], lod);
                      else if (GSK_N_SAMPLERS > 23)
                        return textureSize (textures[23], lod);
                    }
                }
            }
          else if (GSK_N_SAMPLERS > 24)
            {
              if (id < 28)
                {
                  if (id < 26)
                    {
                      if (id < 25)
                        return textureSize (textures[24], lod);
                      else if (GSK_N_SAMPLERS > 25)
                        return textureSize (textures[25], lod);
                    }
                  else if (GSK_N_SAMPLERS > 26)
                    {
                      if (id < 27)
                        return textureSize (textures[26], lod);
                      else if (GSK_N_SAMPLERS > 27)
                        return textureSize (textures[27], lod);
                    }
                }
              else if (GSK_N_SAMPLERS > 28)
                {
                  if (id < 30)
                    {
                      if (id < 29)
                        return textureSize (textures[28], lod);
                      else if (GSK_N_SAMPLERS > 29)
                        return textureSize (textures[29], lod);
                    }
                  else if (GSK_N_SAMPLERS > 30)
                    {
                      if (id < 31)
                        return textureSize (textures[30], lod);
                      else if (GSK_N_SAMPLERS > 31)
                        return textureSize (textures[31], lod);
                    }
                }
            }
        }
#endif
    }
  return ivec2 (1, 1);
}

vec4
gsk_texel_fetch (uint  id,
                 ivec2 pos,
                 int   lod)
{
  if ((id & 1) != 0)
    {
      id >>= 1;
      if (id < 16)
        {
          if (id < 8)
            {
              if (id < 4)
                {
                  if (id < 2)
                    {
                      if (id < 1)
                        return texelFetch (immutable_textures[0], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 1)
                        return texelFetch (immutable_textures[1], pos, lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 2)
                    {
                      if (id < 3)
                        return texelFetch (immutable_textures[2], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 3)
                        return texelFetch (immutable_textures[3], pos, lod);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 4)
                {
                  if (id < 6)
                    {
                      if (id < 5)
                        return texelFetch (immutable_textures[4], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 5)
                        return texelFetch (immutable_textures[5], pos, lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 6)
                    {
                      if (id < 7)
                        return texelFetch (immutable_textures[6], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 7)
                        return texelFetch (immutable_textures[7], pos, lod);
                    }
                }
            }
          else if (GSK_N_IMMUTABLE_SAMPLERS > 8)
            {
              if (id < 12)
                {
                  if (id < 10)
                    {
                      if (id < 9)
                        return texelFetch (immutable_textures[8], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 9)
                        return texelFetch (immutable_textures[9], pos, lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 10)
                    {
                      if (id < 11)
                        return texelFetch (immutable_textures[10], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 11)
                        return texelFetch (immutable_textures[11], pos, lod);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 12)
                {
                  if (id < 14)
                    {
                      if (id < 13)
                        return texelFetch (immutable_textures[12], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 13)
                        return texelFetch (immutable_textures[13], pos, lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 14)
                    {
                      if (id < 15)
                        return texelFetch (immutable_textures[14], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 15)
                        return texelFetch (immutable_textures[15], pos, lod);
                    }
                }
            }
        }
      else if (GSK_N_IMMUTABLE_SAMPLERS > 16)
        {
          if (id < 24)
            {
              if (id < 20)
                {
                  if (id < 18)
                    {
                      if (id < 17)
                        return texelFetch (immutable_textures[16], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 17)
                        return texelFetch (immutable_textures[17], pos, lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 18)
                    {
                      if (id < 19)
                        return texelFetch (immutable_textures[18], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 19)
                        return texelFetch (immutable_textures[19], pos, lod);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 20)
                {
                  if (id < 22)
                    {
                      if (id < 21)
                        return texelFetch (immutable_textures[20], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 21)
                        return texelFetch (immutable_textures[21], pos, lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 22)
                    {
                      if (id < 23)
                        return texelFetch (immutable_textures[22], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 23)
                        return texelFetch (immutable_textures[23], pos, lod);
                    }
                }
            }
          else if (GSK_N_IMMUTABLE_SAMPLERS > 24)
            {
              if (id < 28)
                {
                  if (id < 26)
                    {
                      if (id < 25)
                        return texelFetch (immutable_textures[24], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 25)
                        return texelFetch (immutable_textures[25], pos, lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 26)
                    {
                      if (id < 27)
                        return texelFetch (immutable_textures[26], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 27)
                        return texelFetch (immutable_textures[27], pos, lod);
                    }
                }
              else if (GSK_N_IMMUTABLE_SAMPLERS > 28)
                {
                  if (id < 30)
                    {
                      if (id < 29)
                        return texelFetch (immutable_textures[28], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 29)
                        return texelFetch (immutable_textures[29], pos, lod);
                    }
                  else if (GSK_N_IMMUTABLE_SAMPLERS > 30)
                    {
                      if (id < 31)
                        return texelFetch (immutable_textures[30], pos, lod);
                      else if (GSK_N_IMMUTABLE_SAMPLERS > 31)
                        return texelFetch (immutable_textures[31], pos, lod);
                    }
                }
            }
        }
    }
  else
    {
      id >>= 1;
#ifdef HAVE_VULKAN_1_2
      return texelFetch (textures[nonuniformEXT (id)], pos, lod);
#else
      if (id < 16)
        {
          if (id < 8)
            {
              if (id < 4)
                {
                  if (id < 2)
                    {
                      if (id < 1)
                        return texelFetch (textures[0], pos, lod);
                      else if (GSK_N_SAMPLERS > 1)
                        return texelFetch (textures[1], pos, lod);
                    }
                  else if (GSK_N_SAMPLERS > 2)
                    {
                      if (id < 3)
                        return texelFetch (textures[2], pos, lod);
                      else if (GSK_N_SAMPLERS > 3)
                        return texelFetch (textures[3], pos, lod);
                    }
                }
              else if (GSK_N_SAMPLERS > 4)
                {
                  if (id < 6)
                    {
                      if (id < 5)
                        return texelFetch (textures[4], pos, lod);
                      else if (GSK_N_SAMPLERS > 5)
                        return texelFetch (textures[5], pos, lod);
                    }
                  else if (GSK_N_SAMPLERS > 6)
                    {
                      if (id < 7)
                        return texelFetch (textures[6], pos, lod);
                      else if (GSK_N_SAMPLERS > 7)
                        return texelFetch (textures[7], pos, lod);
                    }
                }
            }
          else if (GSK_N_SAMPLERS > 8)
            {
              if (id < 12)
                {
                  if (id < 10)
                    {
                      if (id < 9)
                        return texelFetch (textures[8], pos, lod);
                      else if (GSK_N_SAMPLERS > 9)
                        return texelFetch (textures[9], pos, lod);
                    }
                  else if (GSK_N_SAMPLERS > 10)
                    {
                      if (id < 11)
                        return texelFetch (textures[10], pos, lod);
                      else if (GSK_N_SAMPLERS > 11)
                        return texelFetch (textures[11], pos, lod);
                    }
                }
              else if (GSK_N_SAMPLERS > 12)
                {
                  if (id < 14)
                    {
                      if (id < 13)
                        return texelFetch (textures[12], pos, lod);
                      else if (GSK_N_SAMPLERS > 13)
                        return texelFetch (textures[13], pos, lod);
                    }
                  else if (GSK_N_SAMPLERS > 14)
                    {
                      if (id < 15)
                        return texelFetch (textures[14], pos, lod);
                      else if (GSK_N_SAMPLERS > 15)
                        return texelFetch (textures[15], pos, lod);
                    }
                }
            }
        }
      else if (GSK_N_SAMPLERS > 16)
        {
          if (id < 24)
            {
              if (id < 20)
                {
                  if (id < 18)
                    {
                      if (id < 17)
                        return texelFetch (textures[16], pos, lod);
                      else if (GSK_N_SAMPLERS > 17)
                        return texelFetch (textures[17], pos, lod);
                    }
                  else if (GSK_N_SAMPLERS > 18)
                    {
                      if (id < 19)
                        return texelFetch (textures[18], pos, lod);
                      else if (GSK_N_SAMPLERS > 19)
                        return texelFetch (textures[19], pos, lod);
                    }
                }
              else if (GSK_N_SAMPLERS > 20)
                {
                  if (id < 22)
                    {
                      if (id < 21)
                        return texelFetch (textures[20], pos, lod);
                      else if (GSK_N_SAMPLERS > 21)
                        return texelFetch (textures[21], pos, lod);
                    }
                  else if (GSK_N_SAMPLERS > 22)
                    {
                      if (id < 23)
                        return texelFetch (textures[22], pos, lod);
                      else if (GSK_N_SAMPLERS > 23)
                        return texelFetch (textures[23], pos, lod);
                    }
                }
            }
          else if (GSK_N_SAMPLERS > 24)
            {
              if (id < 28)
                {
                  if (id < 26)
                    {
                      if (id < 25)
                        return texelFetch (textures[24], pos, lod);
                      else if (GSK_N_SAMPLERS > 25)
                        return texelFetch (textures[25], pos, lod);
                    }
                  else if (GSK_N_SAMPLERS > 26)
                    {
                      if (id < 27)
                        return texelFetch (textures[26], pos, lod);
                      else if (GSK_N_SAMPLERS > 27)
                        return texelFetch (textures[27], pos, lod);
                    }
                }
              else if (GSK_N_SAMPLERS > 28)
                {
                  if (id < 30)
                    {
                      if (id < 29)
                        return texelFetch (textures[28], pos, lod);
                      else if (GSK_N_SAMPLERS > 29)
                        return texelFetch (textures[29], pos, lod);
                    }
                  else if (GSK_N_SAMPLERS > 30)
                    {
                      if (id < 31)
                        return texelFetch (textures[30], pos, lod);
                      else if (GSK_N_SAMPLERS > 31)
                        return texelFetch (textures[31], pos, lod);
                    }
                }
            }
        }
#endif
    }
  return vec4 (1.0, 0.0, 0.8, 1.0);
}

#ifdef HAVE_VULKAN_1_2
#define gsk_get_buffer(id) buffers[nonuniformEXT (id)]
#define gsk_get_float(id) gsk_get_buffer(id >> 22).floats[id & 0x3FFFFF]
#else
float
gsk_get_float (uint id)
{
  uint buffer_id = id >> 22;
  uint float_id = id & 0x3FFFFF;
  if (buffer_id < 16)
    {
      if (buffer_id < 8)
        {
          if (buffer_id < 4)
            {
              if (buffer_id < 2)
                {
                  if (buffer_id < 1)
                    return buffers[0].floats[float_id];
                  else if (GSK_N_BUFFERS > 1)
                    return buffers[1].floats[float_id];
                }
              else if (GSK_N_BUFFERS > 2)
                {
                  if (buffer_id < 3)
                    return buffers[2].floats[float_id];
                  else if (GSK_N_BUFFERS > 3)
                    return buffers[3].floats[float_id];
                }
            }
          else if (GSK_N_BUFFERS > 4)
            {
              if (buffer_id < 6)
                {
                  if (buffer_id < 5)
                    return buffers[4].floats[float_id];
                  else if (GSK_N_BUFFERS > 5)
                    return buffers[5].floats[float_id];
                }
              else if (GSK_N_BUFFERS > 6)
                {
                  if (buffer_id < 7)
                    return buffers[6].floats[float_id];
                  else if (GSK_N_BUFFERS > 7)
                    return buffers[7].floats[float_id];
                }
            }
        }
      else if (GSK_N_BUFFERS > 8)
        {
          if (buffer_id < 12)
            {
              if (buffer_id < 10)
                {
                  if (buffer_id < 9)
                    return buffers[8].floats[float_id];
                  else if (GSK_N_BUFFERS > 9)
                    return buffers[9].floats[float_id];
                }
              else if (GSK_N_BUFFERS > 10)
                {
                  if (buffer_id < 11)
                    return buffers[10].floats[float_id];
                  else if (GSK_N_BUFFERS > 11)
                    return buffers[11].floats[float_id];
                }
            }
          else if (GSK_N_BUFFERS > 12)
            {
              if (buffer_id < 14)
                {
                  if (buffer_id < 13)
                    return buffers[12].floats[float_id];
                  else if (GSK_N_BUFFERS > 13)
                    return buffers[13].floats[float_id];
                }
              else if (GSK_N_BUFFERS > 14)
                {
                  if (buffer_id < 15)
                    return buffers[14].floats[float_id];
                  else if (GSK_N_BUFFERS > 15)
                    return buffers[15].floats[float_id];
                }
            }
        }
    }
  else if (GSK_N_BUFFERS > 16)
    {
      if (buffer_id < 24)
        {
          if (buffer_id < 20)
            {
              if (buffer_id < 18)
                {
                  if (buffer_id < 17)
                    return buffers[16].floats[float_id];
                  else if (GSK_N_BUFFERS > 17)
                    return buffers[17].floats[float_id];
                }
              else if (GSK_N_BUFFERS > 18)
                {
                  if (buffer_id < 19)
                    return buffers[18].floats[float_id];
                  else if (GSK_N_BUFFERS > 19)
                    return buffers[19].floats[float_id];
                }
            }
          else if (GSK_N_BUFFERS > 20)
            {
              if (buffer_id < 22)
                {
                  if (buffer_id < 21)
                    return buffers[20].floats[float_id];
                  else if (GSK_N_BUFFERS > 21)
                    return buffers[21].floats[float_id];
                }
              else if (GSK_N_BUFFERS > 22)
                {
                  if (buffer_id < 23)
                    return buffers[22].floats[float_id];
                  else if (GSK_N_BUFFERS > 23)
                    return buffers[23].floats[float_id];
                }
            }
        }
      else if (GSK_N_BUFFERS > 24)
        {
          if (buffer_id < 28)
            {
              if (buffer_id < 26)
                {
                  if (buffer_id < 25)
                    return buffers[24].floats[float_id];
                  else if (GSK_N_BUFFERS > 25)
                    return buffers[25].floats[float_id];
                }
              else if (GSK_N_BUFFERS > 26)
                {
                  if (buffer_id < 27)
                    return buffers[26].floats[float_id];
                  else if (GSK_N_BUFFERS > 27)
                    return buffers[27].floats[float_id];
                }
            }
          else if (GSK_N_BUFFERS > 28)
            {
              if (buffer_id < 30)
                {
                  if (buffer_id < 29)
                    return buffers[28].floats[float_id];
                  else if (GSK_N_BUFFERS > 29)
                    return buffers[29].floats[float_id];
                }
              else if (GSK_N_BUFFERS > 30)
                {
                  if (buffer_id < 31)
                    return buffers[30].floats[float_id];
                  else if (GSK_N_BUFFERS > 31)
                    return buffers[31].floats[float_id];
                }
            }
        }
    }
  return 0.0;
}
#endif
#define gsk_get_int(id) (floatBitsToInt(gsk_get_float(id)))
#define gsk_get_uint(id) (floatBitsToUint(gsk_get_float(id)))

void
gsk_set_output_color (vec4 color)
{
  out_color = color;
}

#endif
#else
precision highp float;

#if __VERSION__ < 420 || (defined(GSK_GLES) && __VERSION__ < 310)
layout(std140)
#else
layout(std140, binding = 0)
#endif
uniform PushConstants
{
    mat4 mvp;
    mat3x4 clip;
    vec2 scale;
} push;

#define GSK_GLOBAL_MVP push.mvp
#define GSK_GLOBAL_CLIP push.clip
#define GSK_GLOBAL_CLIP_RECT push.clip[0]
#define GSK_GLOBAL_SCALE push.scale

#if __VERSION__ < 420 || (defined(GSK_GLES) && __VERSION__ < 310)
layout(std140)
#else
layout(std140, binding = 1)
#endif
uniform Floats
{
  vec4 really_just_floats[1024];
} floats[11];

#if N_EXTERNAL_TEXTURES > 0
uniform samplerExternalOES 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


PASS(0) vec2 _pos;
PASS_FLAT(1) Rect _start_rect;
PASS_FLAT(2) Rect _end_rect;
PASS(3) vec2 _start_coord;
PASS(4) vec2 _end_coord;
PASS_FLAT(5) uint _start_id;
PASS_FLAT(6) uint _end_id;
PASS_FLAT(7) float _start_opacity;
PASS_FLAT(8) float _end_opacity;


#ifdef GSK_VERTEX_SHADER

IN(0) vec4 in_rect;
IN(1) vec4 in_start_rect;
IN(2) uint in_start_id;
IN(3) vec4 in_end_rect;
IN(4) uint in_end_id;
IN(5) vec2 in_opacity_progress;

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

  _pos = pos;

  Rect start_rect = rect_from_gsk (in_start_rect);
  _start_rect = start_rect;
  _start_coord = rect_get_coord (start_rect, pos);
  _start_id = in_start_id;
  _start_opacity = in_opacity_progress[0] * (1.0 - in_opacity_progress[1]);

  Rect end_rect = rect_from_gsk (in_end_rect);
  _end_rect = end_rect;
  _end_coord = rect_get_coord (end_rect, pos);
  _end_id = in_end_id;
  _end_opacity = in_opacity_progress[0] * in_opacity_progress[1];
}

#endif



#ifdef GSK_FRAGMENT_SHADER

void
run (out vec4 color,
     out vec2 position)
{
  color = gsk_texture (_start_id, _start_coord) *
          rect_coverage (_start_rect, _pos) *
          _start_opacity +
          gsk_texture (_end_id, _end_coord) *
          rect_coverage (_end_rect, _pos) *
          _end_opacity;
  position = _pos;
}

#endif
