//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//http://enbdev.com
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
THIS IS HLSL FILE FORMAT FOR EXECUTING ADDITIONAL
POST PROCESSING 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 BlurSamplingRange=0.6;//0.6
float ShiftSamplingRange=0.75;

float SharpSamplingRange=1.0;//1.0; //sharpening or blurring range
float SharpeningAmount=5.0;//3.0;
float ScanLineAmount=0.0;
float ScanLineRepeat=1.0; //0.5, 0.3333, 0.25, 0.125, so on
float NoiseAmount=0.6;





#define E_SHADER_3_0


//keyboard controled variables
float tempF1 : register(c208);
float tempF2 : register(c209);
float tempF3 : register(c210);
float tempF4 : register(c211);
float tempF5 : register(c212);
float tempF6 : register(c213);
float tempF7 : register(c214);
float tempF8 : register(c215);
float tempF9 : register(c216);
float tempF0 : register(c217);



//global variables, already set before executing this code
float ScreenSize : register(c218); //width of the display resolution (1024 f.e.)
float ScreenScaleY : register(c219); //screen proportions (1.333 for 1024/768)





//textures
texture2D texColor;
texture2D texNoise;

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

sampler2D SamplerNoise = sampler_state
{
Texture   = <texNoise>;
MinFilter = POINT;
MagFilter = POINT;
MipFilter = NONE;//NONE;
AddressU  = Wrap;
AddressV  = Wrap;
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_ProcessDos(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float4 res;
	float4 coord=0.0;

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

	coord.w=0.0;

	float2	xs;
	xs.x=ScreenSize;
	xs.y=ScreenSize/ScreenScaleY;
	//v1
	//xs*=0.25;
	//v2
	xs=float2(320.0, 240.0);
	float	EColorsCount=16.0001;

	coord.xy=floor(IN.txcoord.xy * xs)/xs;

	origcolor=tex2Dlod(SamplerColor, coord);

	origcolor+=0.0001;
//	origcolor=lerp(origcolor, normalize(origcolor), 0.5);

	float	graymax=max(origcolor.x, max(origcolor.y, origcolor.z));
	float3	ncolor=origcolor.xyz/graymax;
	graymax=floor(graymax * EColorsCount)/EColorsCount;
	origcolor.xyz*=graymax;
//	origcolor=floor(origcolor * EColorsCount)/EColorsCount;

	res.xyz=origcolor.xyz;

	res.w=1.0;
	return res;
}




//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//first pass
technique PostProcess
{
	pass P0
	{

		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessDos();

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



