//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ENBSeries effect file
// http://enbdev.com
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//THIS IS HLSL FILE FORMAT FOR EXECUTING ADDITIONAL
//POST PROCESSING EFFECTS. MAKE THE COPY BEFORE CHANGING IT!

//WARNING! This is old standart file, compatible with ENBSeries pre 0.101

//#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;


float	PassNumber;


//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





float4x4 worldviewproj;




//textures
texture2D texColor;

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


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 res;
	float4 coord=0.0;

	coord.xy=IN.txcoord.xy;
	float4 origcolor;

	coord.w=0.5;

#ifdef E_SHADER_3_0
	origcolor=tex2Dlod(SamplerColor, coord);
#else
	origcolor=tex2Dbias(SamplerColor, coord);
#endif

	float4 color=0.0;
	float2 offset[8]=
	{
		float2(1.0, 1.0),
		float2(-1.0, -1.0),
		float2(-1.0, 1.0),
		float2(1.0, -1.0),

		float2(1.41, 0.0),
		float2(-1.41, 0.0),
		float2(0.0, 1.41),
		float2(0.0, -1.41)
	};
	int i=0;

	color=tex2D(SamplerColor, IN.txcoord.xy);
	float4 tcol=0.0;
	float invscreensize=1.0/ScreenSize;
	for (i=0; i<8; i++)
	{
		coord.xy=IN.txcoord.xy+offset[i].xy*tempF2*invscreensize;// *0.0015;
#ifdef E_SHADER_3_0
		tcol+=tex2Dlod(SamplerColor, coord);
#else
		tcol+=tex2Dbias(SamplerColor, coord);
#endif
	}
//	tcol*=0.125;

tcol+=color;
tcol*=0.111;

	float gray=dot(color.xyz, 0.333);
	float tempgray=dot(tcol.xyz, 0.333);
	float cfact=abs(gray-tempgray)*5.0*tempF1;
	cfact=saturate(cfact);

//	res.xyz*=lerp(color.xyz, tcol.xyz, cfact);
//	res.xyz=(1.0-cfact)*tcol.xyz;//pow(tcol.xyz, tempF5);
//res.xyz=frac(gray*tempF6);
float	lines=(1.0-cfact);


res=lines;


	return res;
}




//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
technique PostProcess
{
    pass P0
    {
#ifdef E_SHADER_3_0
	VertexShader = compile vs_3_0 VS_PostProcess();
	PixelShader  = compile ps_3_0 PS_PostProcess();
#else
	VertexShader = compile vs_2_0 VS_PostProcess();
	PixelShader  = compile ps_2_0 PS_PostProcess();
#endif

	ZEnable=FALSE;
	CullMode=NONE;
	ALPHATESTENABLE=FALSE;
	SEPARATEALPHABLENDENABLE=FALSE;
	AlphaBlendEnable=FALSE;
	FogEnable=FALSE;
	SRGBWRITEENABLE=FALSE;
	}
}



