//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//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!
*/

//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 = LINEAR;//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
{		
	float4 color = tex2D(SamplerColor, In.txcoord);

	color= color * ((1.2+(color.a*0.2)));
	if((round(color.g*900.0)/900.0)>200||(round(color.r*900.0)/900.0)>200||(round(color.b*900.0)/900.0)>200)
	{	
	float cr=255;
	float cg=255;
	float cb=255;
	color.r = 0.9;
	color.g = 0.7;
	color.b = 0.1;
	}
	else
	{
	color.r = (round(color.r*900.0)/900.0);
	color.g = (round(color.g*900.0)/900.0);
	color.b = (round(color.b*900.0)/900.0);
	}
	
	const float threshold = 0.01;

	const int NUM = 9;
	const float2 c[NUM] =
	{
		float2(-0.0078125, 0.0078125), 
		float2(-0.0078125, 0.0078125),
		float2( 0.0078125, 0.0078125),
		float2(-0.0078125, 0.00 ),
		float2( 0.0,       0.0),
		float2( 0.0078125, 0.007 ),
		float2(-0.0078125,-0.0078125),
		float2( 0.0078125,-0.0078125),
		float2( 0.0078125,-0.0078125),
	};	

	int i;
	float3 col[NUM];
	for (i=0; i < NUM; i++)
	{
		col[i] = tex2D(SamplerColor, In.txcoord.xy + 0.004512*c[i]);
	}
	
	float3 rgb2lum = float3(0.30, 0.59, 0.11);
	float lum[NUM];
	for (i = 0; i < NUM; i++)
	{
		lum[i] = dot(col[i].xyz, rgb2lum);
	}
	float x = lum[2]+2*lum[8]+2*lum[5]-lum[0]-2*lum[3]-2*lum[6];
	float y = lum[6]+2*lum[7]+2*lum[8]-lum[0]-2*lum[1]-2*lum[2];
	float edge =(x*x + y*y < threshold)? 1.1:0.75;
	
	color.rgb *= edge;
	return color;
}



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

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

