layout (location = 0) in ivec2 inPos;
layout (location = 1) in float depth;
layout (location = 2) in uint inColor;
layout (location = 3) in uint inClut;
layout (location = 4) in uint inTexpage;
layout (location = 5) in uvec2 inUV;

out vec4 vertexColor;
out vec2 texCoords;
flat out ivec2 clutBase;
flat out ivec2 texpageBase;
flat out uint rawTexpage;
flat out int texMode;

uniform vec2 u_vertexOffsets;

void main()
{
   float x = float(inPos.x);
   float y = float(inPos.y);
   float xx = (x + u_vertexOffsets.x) / 512.0;
   float yy = (y + u_vertexOffsets.y) / 256.0;
   xx -= 1.0;
   yy -= 1.0;

   gl_Position = vec4(xx, yy, depth, 1.0);

   float red = float(inColor & 0xffu);
   float green = float((inColor >> 8u) & 0xffu);
   float blue = float((inColor >> 16u) & 0xffu);
   vec3 color = vec3(red, green, blue);
   vertexColor = vec4(color / 255.0, 1.0);

   if ((int(inTexpage) & 0x8000) != 0) { // Untextured primitive
       texMode = 4;
   } else {
       texMode = (int(inTexpage) >> 7) & 3;
       texCoords = vec2(inUV);
       texpageBase = ivec2((int(inTexpage) & 0xf) * 64, ((int(inTexpage) >> 4) & 0x1) * 256);
       rawTexpage = inTexpage;
       clutBase = ivec2((int(inClut) & 0x3f) * 16, int(inClut) >> 6);
   }
}
