
#if defined(CONST_COLOR) && defined(VERTEX_COLOR)
// "const color" handled in vertex shader
#undef CONST_COLOR
#endif

// ------- UNIFORMS (SHADER CONSTS) -------

#if defined(TEXTURE0)
Texture2D		chu_tAmbient;
SamplerState	SampleType0;
#endif

#if defined(GEONORMALMAP) || defined(GEONORMALMAP09)
Texture2D		chu_tGeoNormalmap;
SamplerState	SampleTypeNM;
vec3			chu_vGeoNormalmapLight;
#endif

#if defined(CONST_COLOR)
vec4			chu_vColor;
#endif

#ifdef ALPHA_TEST
float			chu_fAlphaTestValue;
#endif

// ------- INTERPOLATORS (SHADER INPUTS) -------

struct PIn
{
    vec4	position: SV_POSITION;
#if defined(VERTEX_COLOR)
	vec4	color: COLOR;
#endif
#if defined(TEXTURE0)
	vec2	uv: TEXCOORD;
#endif
#if defined(GEONORMALMAP) || defined(GEONORMALMAP09)
	vec2	geouv: TEXCOORD1;
#endif
};

// ------- FUNCTIONS ------- 

// suffix:
// F - incoming fragment
// T - texture fragment
// V - produced value

vec4 main(PIn input): SV_TARGET
{
	vec4 vColorF;
	vec4 vColorT;
	vec4 vColorV;
	
// incoming fragment
#define INCOMING_FRAGMENT
#if defined(CONST_COLOR) && defined(VERTEX_COLOR)
	#error blbost
#elif defined(CONST_COLOR)
	vColorF = chu_vColor;
#elif defined(VERTEX_COLOR)
	vColorF = input.color;
#else
	#undef INCOMING_FRAGMENT
#endif

// texture fragment
#define TEXTURE_FRAGMENT
#if defined(TEXTURE0)
	vColorT = chu_tAmbient.Sample(SampleType0, input.uv);
#else
	#undef TEXTURE_FRAGMENT
#endif

// modulate
#if defined(TEXTURE_FRAGMENT)
	#if defined(TEXTURE0_ALPHA)
		#if defined(INCOMING_FRAGMENT)
			vColorV.rgb = vColorF.rgb;
			vColorV.a = vColorF.a*vColorT.r;
		#else
			#error incoming fragment required
		#endif
	#else
		#if defined(INCOMING_FRAGMENT)
			vColorV = vColorF*vColorT;
		#else
			vColorV = vColorT;
		#endif
	#endif
#elif defined(INCOMING_FRAGMENT)
	vColorV = vColorF;
#else
	#error neither incoming nor texture fragment defined
#endif

#if defined(ALPHA_TEST)
	if (vColorV.a <= chu_fAlphaTestValue)
	{
		discard;
	}
#endif

#if defined(GEONORMALMAP) || defined(GEONORMALMAP09)
	vec3 vNormal = chu_tGeoNormalmap.Sample(SampleTypeNM, input.geouv).xyz;
	float d = dot((vNormal - 0.5)*2.0, chu_vGeoNormalmapLight);
	vColorV.rgb *= (1.0 - 0.5*d);
#endif

	return vColorV;
}
