// Anamorphic flare, based on Petka's bloom and CC
//////////////////////////////////-----------------------------------------------------------------]

//ANAMORPHICFLARE

//#define FLAREMODE_1					//Flaremode 1 = new flare by me; Flaremode 2 = colormod-type flare
//#define FLAREMODE_2

//the flares work differently so there are different values for the Threshold and power needed.

static const float thresh1 = 0.95; 			//Flaremode 1 Threshold
static const float thresh2 = 47.0;			//Flaremode 2 Threshold

float fFlareIntensity1 = 20.0;				//Flaremode 1 Intensity
float fFlareIntensity2 = 0.02;				//Flaremode 2 Intensity
	  		
float4 paramsS = {1.0,1.0,1.0,1.0}; 			//Colorcorrection for dark areas
float4 paramsM = {1.0,1.0,1.0,1.0}; 			//Colorcorrection for medium areas
float4 paramsH = {1.0,1.0,1.0,1.0}; 			//Colorcorrection for bright areas

const float fBlurSize = 1.0/512.0;			//How big the flare appears, don't go too high or it will look ugly
const float fBlurDownsampling = 128;			//Something IDK, remained from Petka work, only change in powers of 2 like 16, 32, 64 etc.


// Flare tintcolor, if you want it colored

bool ANAMORPHICTINTCOLOR=true;
float ANAMORPHICREDAMOUNT=1.0; 	//power of red godray color
float ANAMORPHICGREENAMOUNT=1.0; 	//power of green godray color
float ANAMORPHICBLUEAMOUNT=1.0; 	//power of blue godray color
	
#include "mta-helper.fx"
//--------------------------------------------------------------------------------------
// Textures
//--------------------------------------------------------------------------------------
texture sTex0 :TEX0;

//--------------------------------------------------------------------------------------
// Sampler Inputs
//--------------------------------------------------------------------------------------

sampler2D InputSampler = sampler_state
{
    Texture = (sTex0);
    MinFilter = POINT;
    MagFilter = POINT;
    MipFilter = POINT;
    AddressU   = Clamp;
	AddressV   = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
	
};

struct VSInput
{
    float3 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};

struct PSInput
{
    float4 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};
 
PSInput VertexShaderFunction(VSInput VS)
{
    PSInput PS = (PSInput)0;
    PS.Position = mul(float4(VS.Position, 1), gWorldViewProjection);
    PS.TexCoord = VS.TexCoord;
    return PS;
}

float Luminance( float3 c )
{
	return dot( c, float3(0.22, 0.707, 0.071) );
}

float4 PixelShaderFunction(PSInput PS) : COLOR0
{

float4 threshhold1 = {thresh1, 1/(1-thresh1), 5.0, 5.0}; 
float4 threshhold2 = {thresh2, 1/(1-thresh2), 5.0, 5.0};
	float4 col = tex2D(InputSampler, PS.TexCoord);
	//float4 color = tex2Dlod(InputSampler, float4(PS.TexCoord,0,8));
	
    float4 sum = float4(0,0,0,0);
 
    // blur this in y (vertical)
    // also I downsample it to get interesting result 

#ifdef FLAREMODE_1
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y - 4.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.05;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y - 3.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.09;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y - 2.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.12;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y - fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.15;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y, PS.TexCoord.y,0,fBlurDownsampling)) * 0.16;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y + fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.15;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y + 2.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.12;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y + 3.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.09;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y + 4.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.05;
#endif
#ifdef FLAREMODE_2
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 100.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.01;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 99.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.01;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 98.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.02;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 97.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.03;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 96.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.04;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 95.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.05;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 94.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.06;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 93.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.07;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 92.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.08;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 91.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.09;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 90.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.10;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 89.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.11;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 88.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.12;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 87.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.13;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 86.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.14;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 85.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.15;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 84.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.16;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 83.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.17;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 82.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.18;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 81.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.19;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 80.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.20;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 79.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.21;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 78.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.22;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 77.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.23;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 76.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.24;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 75.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.25;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 74.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.26;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 73.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.27;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 72.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.28;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 71.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.29;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 70.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.30;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 69.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.31;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 68.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.32;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 67.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.33;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 66.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.34;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 65.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.35;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 64.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.36;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 63.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.37;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 62.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.38;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 61.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.39;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 60.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.40;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 59.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.41;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 58.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.42;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 57.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.43;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 56.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.44;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 55.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.45;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 54.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.46;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 53.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.47;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 52.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.48;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 51.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.49;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 50.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.50;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 49.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.51;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 48.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.52;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 47.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.53;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 46.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.54;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 45.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.55;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 44.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.56;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 43.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.57;
//------------------------------------------------------------------------------------------------------------------------------
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 42.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.58;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 41.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.59;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 40.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.60;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 39.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.61;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 38.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.62;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 37.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.63;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 36.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.64;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 35.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.65;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 34.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.66;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 33.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.67;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 32.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.68;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 31.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.69;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 30.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.70;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 29.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.71;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 28.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.72;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 27.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.73;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 26.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.74;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 25.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.75;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 24.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.76;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 23.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.77;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 22.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.78;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 21.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.79;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 20.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.80;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 19.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.81;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 18.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.82;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 17.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.83;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 16.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.84;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 15.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.85;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 14.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.86;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 13.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.87;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 12.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.88;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 11.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.89;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 10.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.90;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 9.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.91;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 8.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.92;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 7.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.93;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 6.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.94;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 5.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.95;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 4.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.96;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 3.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.97;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - 2.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.98;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x - fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.99;


sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x, PS.TexCoord.y,0,fBlurDownsampling)) * 1.0;


sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 100.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.01;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 99.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.01;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 98.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.02;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 97.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.03;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 96.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.04;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 95.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.05;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 94.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.06;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 93.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.07;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 92.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.08;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 91.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.09;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 90.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.10;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 89.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.11;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 88.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.12;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 87.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.13;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 86.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.14;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 85.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.15;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 84.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.16;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 83.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.17;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 82.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.18;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 81.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.19;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 80.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.20;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 79.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.21;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 78.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.22;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 77.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.23;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 76.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.24;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 75.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.25;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 74.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.26;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 73.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.27;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 72.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.28;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 71.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.29;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 70.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.30;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 69.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.31;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 68.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.32;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 67.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.33;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 66.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.34;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 65.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.35;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 64.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.36;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 63.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.37;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 62.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.38;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 61.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.39;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 60.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.40;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 59.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.41;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 58.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.42;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 57.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.43;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 56.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.44;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 55.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.45;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 54.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.46;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 53.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.47;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 52.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.48;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 51.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.49;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 50.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.50;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 49.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.51;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 48.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.52;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 47.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.53;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 46.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.54;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 45.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.55;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 44.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.56;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 43.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.57;
//+++++-------------------------------------------------------------------------------------------------------------------------
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 42.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.58;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 41.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.59;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 40.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.60;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 39.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.61;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 38.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.62;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 37.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.63;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 36.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.64;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 35.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.65;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 34.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.66;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 33.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.67;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 32.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.68;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 31.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.69;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 30.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.70;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 29.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.71;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 28.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.72;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 27.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.73;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 26.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.74;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 25.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.75;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 24.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.76;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 23.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.77;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 22.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.78;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 21.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.79;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 20.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.80;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 19.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.81;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 18.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.82;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 17.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.83;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 16.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.84;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 15.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.85;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 14.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.86;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 13.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.87;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 12.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.88;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 11.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.89;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 10.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.90;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 9.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.91;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 8.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.92;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 7.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.93;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 6.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.94;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 5.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.95;

sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 4.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.96;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 3.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.97;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + 2.0*fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.98;
sum += tex2Dlod(InputSampler, float4(PS.TexCoord.x + fBlurSize, PS.TexCoord.y,0,fBlurDownsampling)) * 0.99;

//vertical for smoothing
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y - 10.0*fBlurSize, PS.TexCoord.x,0,fBlurDownsampling)) * 0.1;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y - 6.6*fBlurSize, PS.TexCoord.x,0,fBlurDownsampling)) * 0.25;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y - 3.3*fBlurSize, PS.TexCoord.x,0,fBlurDownsampling)) * 0.50;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y - fBlurSize, PS.TexCoord.x,0,fBlurDownsampling)) * 0.75;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y, PS.TexCoord.x,0,fBlurDownsampling)) * 1.0;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y + fBlurSize, PS.TexCoord.x,0,fBlurDownsampling)) * 0.75;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y + 3.3*fBlurSize, PS.TexCoord.x,0,fBlurDownsampling)) * 0.50;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y + 6.6*fBlurSize, PS.TexCoord.x,0,fBlurDownsampling)) * 0.25;
    sum += tex2Dlod(InputSampler, float4(PS.TexCoord.y + 10.0*fBlurSize, PS.TexCoord.x,0,fBlurDownsampling)) * 0.1;

#endif


sum = dot(sum, float3(0.3, 0.59, 0.11));

if (ANAMORPHICTINTCOLOR==true) {
sum.r = sum.r * ANAMORPHICREDAMOUNT;
sum.g = sum.g * ANAMORPHICGREENAMOUNT;
sum.b = sum.b * ANAMORPHICBLUEAMOUNT;
}


	// do some cool stuff to get bloom...
#ifdef FLAREMODE_1
	float4 color = max(float4(0.0,0.0,0.0,0.0), sum-threshhold1.x);
	float4 toBlend = saturate (color * fFlareIntensity1);
#else
        float4 color = max(float4(0.0,0.0,0.0,0.0), sum-threshhold2.x);
	float4 toBlend = saturate (color * fFlareIntensity2);
#endif
		
	
	
	float4 smpl = 1-(1-col)*(1-toBlend);
	
	float factor = max(smpl.x, max(smpl.y, smpl.z));
	float factorM = (smpl.x + smpl.y + smpl.z)/3;
	float4 shadows = paramsS;
	float4 midtones = paramsM;
	float4 highlights = paramsH;
	
	float4 Color;
	
	if(factor < 0.1)	//Shadows
	{
		if(factor > 0.01)
		{
			factor = (factor + 0.09)*10;
			
			shadows.w = shadows.x; //.w is value backup
			shadows.x = ((1 - shadows.w) / 2) * factor;
			shadows.x += shadows.w;	//Adding backup
			
			shadows.w = shadows.y; //.w is value backup
			shadows.y = ((1 - shadows.w) / 2) * factor;
			shadows.y += shadows.w;	//Adding backup
			
			shadows.w = shadows.z; //.w is value backup
			shadows.z = ((1 - shadows.w) / 2) * factor;
			shadows.z += shadows.w;	//Adding backup
		}
		Color = float4(smpl.x * shadows.x, smpl.y * shadows.y, smpl.z * shadows.z, smpl.w);	
	}
	else if(factorM >= 0.1 && factorM <= 0.5)	//Middle-tones
	{
		if(factorM > 0.3)
		{
			factorM = (factorM - 0.31)*10;
			
			midtones.w = midtones.x; //.w is value backup
			midtones.x = ((1 - midtones.w) / 2) * factorM;
			midtones.x += midtones.w;	//Adding backup
			
			midtones.w = midtones.y; //.w is value backup
			midtones.y = ((1 - midtones.w) / 2) * factorM;
			midtones.y += midtones.w;	//Adding backup
			
			midtones.w = midtones.z; //.w is value backup
			midtones.z = ((1 - midtones.w) / 2) * factorM;
			midtones.z += midtones.w;	//Adding backup
		}
		else if(factorM <= 0.3)
		{
			factorM = (factorM - 0.1)*10;
			
			midtones.w = midtones.x;
			midtones.x = (1 - midtones.w) - (factorM * ((1 - midtones.w) / 2));
			midtones.x += midtones.w;
			
			midtones.w = midtones.y;
			midtones.y = (1 - midtones.w) - (factorM * ((1 - midtones.w) / 2));
			midtones.y += midtones.w;
			
			midtones.w = midtones.z;
			midtones.z = (1 - midtones.w) - (factorM * ((1 - midtones.w) / 2));
			midtones.z += midtones.w;
		}
		
		Color = float4(smpl.x * midtones.x, smpl.y * midtones.y, smpl.z * midtones.z, smpl.w);
	}		
	else if(factorM > 0.5)	//Hightlights
	{
		if(factorM <= 0.8)
		{
			factorM = (factorM - 0.51)*10;
			
			highlights.w = highlights.x;
			highlights.x = (1 - highlights.w) - (factorM * ((1 - highlights.w) / 2));
			highlights.x += highlights.w;
			
			highlights.w = highlights.y;
			highlights.y = (1 - highlights.w) - (factorM * ((1 - highlights.w) / 2));
			highlights.y += highlights.w;
			
			highlights.w = highlights.z;
			highlights.z = (1 - highlights.w) - (factorM * ((1 - highlights.w) / 2));
			highlights.z += highlights.w;
		}
	
		Color = float4(smpl.x * highlights.x, smpl.y * highlights.y, smpl.z * highlights.z, smpl.w);
	}
	else Color = smpl;
	
	float lum = Luminance(Color.xyz);
return Color;
	
}


technique CCShadow
{	
	pass P0
	{
	  VertexShader = compile vs_3_0 VertexShaderFunction();
	  PixelShader  = compile ps_3_0 PixelShaderFunction();
	}
}

