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

//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)
float4	ScreenBrightness;//rgba(0..1) color of the screen with time dependent inertia
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;
texture2D texBloom;

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

sampler2D SamplerBloom = sampler_state
{
	Texture   = <texBloom>;
	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
{

	float2 offset[4]=
	{
		float2(-1.0,-1.0),
		float2(-1.0, 1.0),
		float2( 1.0, 1.0),
		float2( 1.0,-1.0)

	};
	float4 res=0.0;
	float4 coord=0.0;
	coord.xy=In.txcoord.xy;
	float4 origcolor=tex2D(SamplerColor, coord.xy);
	float origgray=max(origcolor.r, max(origcolor.g, origcolor.b));
	res+=origcolor;
	float range=0.7*tempF9/ScreenSize;
	for (int i=0; i<4; i++)
	{

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




//res=origcolor;

coord.xy=In.txcoord.xy;
coord.w=2.5*tempF1;
float4 envcol1=tex2Dbias(SamplerColor, coord);
coord.w=3.5*tempF2;
float4 envcol2=tex2Dbias(SamplerColor, coord);
float4 envcol3=tex2D(SamplerBloom, coord.xy);
float4 bloom=envcol3;


float4 envcol=(envcol1+envcol2+envcol3)*0.666;


//envcol=tempF5*pow(envcol,tempF4);

//envcol=envcol*tempF7/(1.0+envcol*tempF8);

//envcol=dot(envcol.xyz,0.333);
//envcol=max(envcol.x, max(envcol.y, envcol.z));
float4 tempcol;//=max(envcol.x, max(envcol.y, envcol.z));
//envcol=lerp(tempcol, envcol, tempF3);

//tempcol.xyz=saturate(res.xyz-envcol.xyz);
//float diff=dot(tempcol.xyz, 0.333);

//float srcgray=dot(res.xyz,0.333);
//envcol.xyz=res.xyz-(tempF7*envcol.xyz)*(1.0-saturate(srcgray));

envcol=tempF5*pow(envcol,tempF4);
res=lerp(res, res*envcol, tempF6);
//res=res*tempF7/(1.0+res*tempF8);
//res+=tempF7*pow(bloom,4.0*tempF8);


//desaturate
	float middlegray=(res.r+res.g+res.b)*0.333;
	float3 diffcolor=res.rgb-middlegray;
	res.rgb-=diffcolor*0.4*tempF0;

if (tempF3>1.1) res=origcolor;

	res.a=1.0;
	return res;
}



/*

float4 PS_PostProcess(VS_OUTPUT_POST In) : COLOR
{

	float2 offset[4]=
	{
		float2(-1.0,-1.0),
		float2(-1.0, 1.0),
		float2( 1.0, 1.0),
		float2( 1.0,-1.0)
	};
	float4 res=0.0;
	float4 coord=0.0;
	coord.xy=In.txcoord.xy;
	float4 origcolor=tex2Dlod(SamplerColor, coord);
	float origgray=max(origcolor.r, max(origcolor.g, origcolor.b));
	res+=origcolor;
	float range=tempF5*0.001;// /ScreenSize.x
	for (int i=0; i<4; i++)
	{
		coord.xy=In.txcoord+offset[i]*range;
		float4 color;
		float gray;
		color=tex2Dlod(SamplerColor, coord);
		float4 colordiff=abs(origcolor-color);
		gray=max(colordiff.r, max(colordiff.g, colordiff.b));
		float lerpfact=saturate(tempF6*abs(gray));//saturate
		res+=lerp(origcolor, color, lerpfact);
	}
	res=res/5.0;

	res.a=1.0;
	return res;
}
*/



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

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

