#include "common.h"
#include "lmodel.h"

// Check Screen Space Shaders modules & addons
#include "check_screenspace.h"

//	TODO: DX10: Move to Load
#ifdef MSAA_OPTIMIZATION
float4 main ( float4 tc:TEXCOORD0, float4 pos2d : SV_Position, uint iSample : SV_SAMPLEINDEX ) : SV_Target
#else
float4 main ( float4 tc:TEXCOORD0, float4 pos2d : SV_Position ) : SV_Target
#endif
{
	const float bias_mul = 0.999f;

 	// Sample the fat framebuffer:
	float2	tcProj	= tc.xy / tc.w;
	gbuffer_data gbd = gbuffer_load_data( GLD_P(tcProj, pos2d, ISAMPLE) );

	float4 _P = float4( gbd.P,gbd.mtl );
	float4 _N = float4( gbd.N,gbd.hemi );
	float4 _C = float4( gbd.C, gbd.gloss );
	
	float m = xmaterial	;
#ifndef USE_R2_STATIC_SUN
	m = _P.w;
#endif
	
	// FLORA FIXES & IMPROVEMENTS - SSS Update 14.2
	// Fix Flora ilumination ( Align normal to light )
	if(abs(m - MAT_FLORA) <= 0.05)
	{
		_N.rgb = -normalize(_P - Ldynamic_pos.xyz);
		_C.w *= 0.3f;
	}

	float rsqr;
	float4 light = plight_local( m, _P, _N, _C, Ldynamic_pos, Ldynamic_pos.w, rsqr );

#ifdef SSFX_ENHANCED_SHADERS
	return float4(float4( SRGBToLinear(Ldynamic_color.rgb),1) * light);
#else
	return float4(Ldynamic_color * light);
#endif
}