#version 110
#extension GL_EXT_gpu_shader4 : enable

uniform sampler2D tex;
uniform bool indexed;
uniform isampler2D indexTex;
uniform sampler2D palette;

void main()
{
	if (indexed)
	{
		ivec4 index = texture2D(indexTex, gl_TexCoord[0].st);

		if(index.a == 0)
		{
			gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
		}
		else
		{
			vec4 texel = texelFetch2D(palette, ivec2(index.a, 0), 0);
			gl_FragColor = vec4(texel.r, texel.g, texel.b, 1.0);
		}
	}
	else
	{
		gl_FragColor = texture2D(tex, gl_TexCoord[0].st);
	}
}