// __multiversion__
// This signals the loading code to prepend either #version 100 or #version 300 es as apropriate.

#include "shaders/fragmentVersionCentroidUV.h"
#include "shaders/uniformMacro.h"

varying vec4 ColorOut;
varying vec4 AdditionalOut;

uniform sampler2D TEXTURE_0;
uniform int SHADER_TYPE;

uniform vec4 COEFFICIENTS[3];
uniform vec4 PIXEL_OFFSETS[6];

void main() {
	// snes-todo: Put this in separate shader when Hummingbird sets ST_StandardTextureWithColorMatrix on filters (other than drop-shadow)
	// we get the correct shadertype, but not the correct shader mapping.
	if (SHADER_TYPE == 7) {
		vec4 baseColor = texture2D(TEXTURE_0, AdditionalOut.xy);
		float nonZeroAlpha = max(baseColor.a, 0.00001);
		baseColor = vec4(baseColor.rgb / nonZeroAlpha, nonZeroAlpha);
		// TODO: Rename the members of the constant buffer so they are not weird for non-blurs
		vec4 finalColor;
		finalColor.r = dot(baseColor, vec4(COEFFICIENTS[0].x, COEFFICIENTS[0].y, COEFFICIENTS[0].z, COEFFICIENTS[0].w));
		finalColor.g = dot(baseColor, vec4(COEFFICIENTS[1].x, COEFFICIENTS[1].y, COEFFICIENTS[1].z, COEFFICIENTS[1].w));
		finalColor.b = dot(baseColor, vec4(COEFFICIENTS[2].x, COEFFICIENTS[2].y, COEFFICIENTS[2].z, COEFFICIENTS[2].w));
		finalColor.a = dot(baseColor, vec4(PIXEL_OFFSETS[0].x, PIXEL_OFFSETS[0].y, PIXEL_OFFSETS[0].z, PIXEL_OFFSETS[0].w));
		finalColor += vec4(PIXEL_OFFSETS[1].x, PIXEL_OFFSETS[1].y, PIXEL_OFFSETS[1].z, PIXEL_OFFSETS[1].w);
		finalColor.rgb *= finalColor.a;
		finalColor.rgba *= ColorOut.a;
		gl_FragColor = finalColor;
	} else {
		float alpha = ColorOut.a * clamp(AdditionalOut.z, 0.0, 1.0);
		gl_FragColor = texture2D(TEXTURE_0, AdditionalOut.xy) * alpha;
	}
}
