//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//ENBSeries: boris-vorontsov@yandex.ru, boris-vorontsov.narod.ru
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
THIS IS HLSL (HIGH LEVEL SHADER LANGUAGE) FILE FOR EXECUTING ADDITIONAL
HARDWARE EFFECTS. MAKE THE COPY BEFORE CHANGING IT!
*/

//enable blurring, useless, disabled at all
//#define EBLURRING

//enable sharpening
#define ESHARPENING

//if defined, color sharpen, otherwise sharp by gray
#define ESHARPENINGCOLOR

//enable noise in dark areas
#define ENOISE

float SamplingRange=1.0; //sharpening or blurring range
float SharpeningAmount=10.0;
float ScanLineAmount=0.0;
float ScanLineRepeat=1.0; //0.5, 0.3333, 0.25, 0.125, so on
float NoiseAmount=0.0;

#define E_SHADER_3_0

//keyboard controled variables
float	tempF1;
float	tempF2;
float	tempF3;
float	tempF4;
float	tempF5;
float	tempF6;
float	tempF7;
float	tempF8;
float	tempF9;
float	tempF0;


//global variables, already set before executing this code
float	ScreenSize;	//width of the display resolution (1024 f.e.)
float	ScreenScaleY;	//screen proportions (1.333 for 1024/768)
float	ScreenBrightnessAdaptation;//(-10..10) for bloom it controls how much to dark in the night or when scene is dark (user defined constant factor)
//float	bloomPower;//(0..10) actually used for bloom, but may be useful here (user defined constant factor)
//float	useBloom;//(0 or 1) if bloom enabled by user



//textures
texture2D texColor;

sampler2D SamplerColor = sampler_state
{
	Texture   = <texColor>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;//NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
};

struct VS_OUTPUT_POST {
	float4 vpos  : POSITION;
	float2 txcoord : TEXCOORD0;
};

struct VS_INPUT_POST {
	float3 pos  : POSITION;
	float2 txcoord : TEXCOORD0;
};




//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST IN)
{
	VS_OUTPUT_POST OUT;

	float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);

	OUT.vpos=pos;
	OUT.txcoord.xy=IN.txcoord.xy;

	return OUT;
}




float4 PS_PostProcess(VS_OUTPUT_POST In) : COLOR
{
/*
float	tempF1=5.0;
float	tempF2=4.0;
float	tempF3=1.0;
float	tempF4=1.0;
float	tempF5=1.3;
float	tempF6=4.35;
float	tempF7=1.12;
float	tempF8=1.2;
float	tempF9=3.17;
float	tempF0=1.4;
*/
//START TO MAKE YOUR CHANGES FROM HERE
	float4 res;
	float4 uvsrc=0;
	uvsrc.xy=In.txcoord;


	float2 offset[8]=
	{
		float2(-1.0,-1.0),
		float2(-1.0, 1.0),
		float2( 1.0, 1.0),
		float2( 1.0,-1.0),
		float2( 0.0, 1.41),
		float2( 0.0,-1.41),
		float2(-1.41, 0.0),
		float2( 1.41, 0.0)
	};
	res=0.2;
	float4 coord=1.0;
	coord.xy=In.txcoord.xy;
	float4 origcolor=tex2D(SamplerColor, coord.xy)*0.9;
	float origgray=max(origcolor.r, max(origcolor.g, origcolor.b));
	res+=origcolor;
	float range=0.92*tempF1/ScreenSize;
	for (int i=0; i<8; i++)
	{

		coord.xy=In.txcoord+offset[i]*range;
		float4 color;
		float gray;
		color=tex2D(SamplerColor, coord.xy);
		float4 colordiff=abs(origcolor-color)*2;
		gray=dot(colordiff.rgb,0.111);
		float lerpfact=saturate(8.0*abs(gray)*color.a)*0.99;//saturate
		res+=lerp(origcolor, color, lerpfact);
	}
	res=res*0.122222;



	float4 color=res;	//tex2Dbias(SamplerColor, uvsrc);

	float	tf7=tempF7*1.07;
	float	tf8=tempF8*0.58;

	float3 correctedcolor=saturate((1.0-color*tempF9*0.24)*tempF0*0.16);

	res.rgb=saturate(color.rgb-correctedcolor)*0.96;
	res.rgb=pow(res.rgb,tf8)*tf7;

	res.a=1.0;
	return  res;
}







//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
technique PostProcess
{
    pass P0
    {
	VertexShader = compile vs_2_0 VS_PostProcess();
	PixelShader  = compile ps_2_a PS_PostProcess();

	FogEnable=FALSE;
	ALPHATESTENABLE=FALSE;
	SEPARATEALPHABLENDENABLE=FALSE;
	AlphaBlendEnable=FALSE;
	FogEnable=FALSE;
	SRGBWRITEENABLE=FALSE;
	}
}

