struct Light
{
    float3 position;
    float3 color;
    float invRadius;
};
// кол-во активных источников и массив из 3-ех источников
int numberOfLights;
Light lights[40];
float3 ambient = {0.93,0.91,1.003};

float BloomAmountMix = 0.1;
float BloomAdditive = 0.1;
float AdaptationValue = 0.40;
float toneMapExposure = 0.2;
float DirtyLensEffect = 1.22;
#define	BLOOM_DISTORTION
float BloomDAmount = 0.95;
float BP_factor = 0.65;
#define GlobalSaturation
float Saturation = 1.28;
float AddContrast = 0.3;
float Vignetting = 0.3;
//#define NormalMB
#define AlternativeMB

struct VS_OUTPUT_POST
{
	float4 vpos  : POSITION;
	float2 txcoord0 : TEXCOORD0;
};
struct VS_INPUT_POST
{
	float3 pos  : POSITION;
	float2 txcoord0 : TEXCOORD0;
};


float4	_c44 : register(c44);
float4	_c66 : register(c66);
float4	_c72 : register(c72);
float4	_c73 : register(c73);
float4	_c74 : register(c74);
float4	_c75 : register(c75);
float4	_c76 : register(c76);
float4	_c77 : register(c77);
float4	_c78 : register(c78);
float4	_c79 : register(c79);
float4	_c80 : register(c80);
float4	_c81 : register(c81);
float4	_c82 : register(c82);
float4	_c83 : register(c83);
float4	_c84 : register(c84);
float4	_c85 : register(c85);


texture2D texs0 : register(s0);
texture2D texs1 : register(s1);
texture2D texs2 : register(s2);
texture2D texs3 : register(s3);
texture2D texs4 : register(s4);
texture2D texs5 : register(s5);
texture2D texs6 : register(s6);
texture2D texs7 : register(s7);
texture2D texs13 : register(s13);//palette
texture2D texs15 : register(s15);
sampler2D s2=sampler_state { Texture=<texs2>; };
sampler2D s4=sampler_state { Texture=<texs4>; };
#ifdef NormalMB
sampler2D s5=sampler_state { Texture=<texs5>; };
#endif
sampler2D s6=sampler_state { Texture=<texs6>; };
sampler2D s7=sampler_state { Texture=<texs7>; };
sampler2D s15=sampler_state { Texture=<texs15>; };

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

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

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


#ifdef AlternativeMB
sampler2D s5 = sampler_state //fixed
{
Texture   = <texs5>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = NONE;
AddressU  = Wrap;
AddressV  = Wrap;
SRGBTexture=FALSE;
MaxMipLevel=0;
MipMapLodBias=0;
};
#endif

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

float4 addFog( float3 position, float4 colour )
{
float3 eyeDirection = _c44.xyz - position;
float distanceSquared = eyeDirection.x*eyeDirection.x + eyeDirection.y*eyeDirection.y + eyeDirection.z*eyeDirection.z;
//float maxDistanceSquared = 10000000000.0f;
//float fogAmount = min(distanceSquared/maxDistanceSquared,1.0f);
float dist = sqrt(distanceSquared);
float maxDistance = 25000.0f;
float fogAmount = min(dist/maxDistance,1.0f);
//PaulP was colour.rgb = lerp((colour.rgb*1.1),float3(0.9f,0.9f,0.9f),fogAmount);
colour.rgb = lerp((colour.rgb*0.88),float3(0.72f,0.72f,0.72f),fogAmount);
return colour;
}

float4 addIngame( float3 position, float4 colour )
{
float3 tintcolour = float3(1.0, 1.0, 1.0);
float brightness = -0.005;
float contrast = 1.5;
float desaturate = 0.2;
float tint = 0.10;

float luminance = (colour.r+colour.g+colour.b)*0.3333f;
float3 bwcolour = float3(luminance,luminance,luminance);
float3 colourOut = colour.rgb*contrast + brightness;
colourOut = lerp(colourOut,bwcolour,desaturate);
colourOut = lerp(colourOut,tintcolour,tint);

return float4(colourOut,colour.a);
}

/////////////////////////

float3 CalculateLight(Light light, float3 normal,
                    float3 pixelPosition)
{
    // направление
    float3 direction = light.position - pixelPosition;
    float atten = length(direction);

    direction /= atten;

       // скалярное произведение нормали и направления
    float amount = max(dot(normal, direction), 0);

    atten *= light.invRadius;

    // делаем так, чтобы modifer был всегда больше нуля или равен ему, дабы при далеких источниках область не становилась темной
    float modifer = max((1 - atten), 0);

       // возращаем результирующий цвет пикселя
    return light.color * modifer * amount;
}

float4 PS_C215BE6E(VS_OUTPUT_POST IN) : COLOR
{
	float4 _oC0=0.0; //output
	float4 _c0=float4(0, 0.212500006, 0.715399981, 0.0720999986);
	float4 _c1=float4(0.25, 1, 256, 0);
	float4 _c2=float4(0, 2, 4, 8);
	float4 _c3=float4(58.1640015, 47.1300011, 0, 0);
	float4 _c4=float4(-0.5, -1.5, 1.5, 0.5);
	float4 _c5=float4(2, -1, 0.125, 0);
	float4 _c6=float4(256, 2, 4, 8);
	float4 _c7=float4(1.0, 0.1, 0.8, 1.0);
	float4 _c8=float4(0.0, 0.1, 0.8, 0.333);
	float4 _c9=float4(0.06, 0.1, 0.8, 0.1);
	float4 _c10=float4(0.01, 1.4, 0.0, 0.32);
	float4 _c11=float4(0.333333, 1.0, 0.0, 0.0);
	float4 _c12=float4(0.27, 0.67, 0.06, 0.0);
	float4 _c13=float4(0.0, 0.0, 0.0, 0.00001);
	float4 _c150=float4(0.55, 0.9, 0.85, 0.9);
	float4 _c195=float4(0.0, 0.0, 0.0, 0.07);//0.7
	float4 r0;
	float4 r1;
	float4 r2;
	float4 r3;
	float4 r4;
	float4 r5;
	float4 r6;
	float4 r7;
	float4 r8;
	float4 r9;
	float4 r10;
	float4 r11;
float4 _v0=0.0;
_v0.xy=IN.txcoord0.xy;


r1=tex2D(s2, _v0.xy); //HDRSampler
r2=tex2D(s3, _v0.xy); //BloomSampler

float2  center;
float   depth;
float   focus;
     
    r0=tex2D(s1, _v0.xy); 
    r0.y=-_c77.x + _c77.y; 
    r0.y=1.0/r0.y;
    r0.z=r0.y * _c77.y; 
	
    r0.z=r0.z * -_c77.x; 
    r0.x=_c77.y * -r0.y + r0.x;
    r0.x=1.0/r0.x;
     r0.y=r0.z * r0.x;
 
    r0.w=abs(_c79.w);
    r3=_c4; 

    depth=r0.z * r0.x - _c78.w; 
    depth+=_c78.y * -r3.w; 
    depth+=148;
	
    center.x=_v0.x-0.5;
center.y=0.5-_v0.y;
	float4	vDofParams = float4(-90, 1, 1000, 1);
   	float2	FocusPoint = float2(0.5, 0.5);
	
	r4=tex2D(s1, FocusPoint); 
	r4.y=-_c77.x + _c77.y; 
	r4.y=1/r4.y;
	r4.z=r4.y * _c77.y; 
	r4.z=r4.z * -_c77.x; 
	r4.x=_c77.y * -r4.y + r4.x;
	r4.x=1/r4.x;
	r4.y=r4.z * r4.x;	

	float cdepth;

	cdepth=r4.z * r4.x - _c78.w; 
	cdepth+=_c78.y * -r3.w; 
	cdepth+=148;

	r4.w = tex2D(s0, FocusPoint).w;
//[loop]
	if(r4.w > 0)
	{
		vDofParams.z = cdepth * 10.5;
		vDofParams.y = cdepth;
	}
	
	depth = max(depth, 0); 

	float	dist;

	if(depth < vDofParams.y)
	    dist = (depth - vDofParams.y)/(vDofParams.y - vDofParams.x);
	else
	{
	    dist = (depth - vDofParams.y)/(vDofParams.z - vDofParams.y);
	    dist = clamp(dist, 0, vDofParams.w);
	}

    	dist = dist * 0.5 + 0.5;

[unroll]

	static float3 poisson[60] =
		{
			   float3( 0.2165, 0.1250, 1.0000 ),
			   float3( 0.0000, 0.2500, 1.0000 ),
			   float3( -0.2165, 0.1250, 1.0000 ),
			   float3( -0.2165, -0.1250, 1.0000 ),
			   float3( -0.0000, -0.2500, 1.0000 ),
			   float3( 0.2165, -0.1250, 1.0000 ),
			   float3( 0.4330, 0.2500, 1.0000 ),
			   float3( 0.0000, 0.5000, 1.0000 ),
			   float3( -0.4330, 0.2500, 1.0000 ),
			   float3( -0.4330, -0.2500, 1.0000 ),
			   float3( -0.0000, -0.5000, 1.0000 ),
			   float3( 0.4330, -0.2500, 1.0000 ),
			   float3( 0.6495, 0.3750, 1.0000 ),
			   float3( 0.0000, 0.7500, 1.0000 ),
			   float3( -0.6495, 0.3750, 1.0000 ),
			   float3( -0.6495, -0.3750, 1.0000 ),
			   float3( -0.0000, -0.7500, 1.0000 ),
			   float3( 0.6495, -0.3750, 1.0000 ),
			   float3( 0.8660, 0.5000, 1.0000 ),
			   float3( 0.0000, 1.0000, 1.0000 ),
			   float3( -0.8660, 0.5000, 1.0000 ),
			   float3( -0.8660, -0.5000, 1.0000 ),
			   float3( -0.0000, -1.0000, 1.0000 ),
			   float3( 0.8660, -0.5000, 1.0000 ),
			   float3( 0.2163, 0.3754, 0.8670 ),
			   float3( -0.2170, 0.3750, 0.8670 ),
			   float3( -0.4333, -0.0004, 0.8670 ),
			   float3( -0.2163, -0.3754, 0.8670 ),
			   float3( 0.2170, -0.3750, 0.8670 ),
			   float3( 0.4333, 0.0004, 0.8670 ),
			   float3( 0.4328, 0.5004, 0.8847 ),
			   float3( -0.2170, 0.6250, 0.8847 ),
			   float3( -0.6498, 0.1246, 0.8847 ),
			   float3( -0.4328, -0.5004, 0.8847 ),
			   float3( 0.2170, -0.6250, 0.8847 ),
			   float3( 0.6498, -0.1246, 0.8847 ),
			   float3( 0.6493, 0.6254, 0.9065 ),
			   float3( -0.2170, 0.8750, 0.9065 ),
			   float3( -0.8663, 0.2496, 0.9065 ),
			   float3( -0.6493, -0.6254, 0.9065 ),
			   float3( 0.2170, -0.8750, 0.9065 ),
			   float3( 0.8663, -0.2496, 0.9065 ),
			   float3( 0.2160, 0.6259, 0.8851 ),
			   float3( -0.4340, 0.5000, 0.8851 ),
			   float3( -0.6500, -0.1259, 0.8851 ),
			   float3( -0.2160, -0.6259, 0.8851 ),
			   float3( 0.4340, -0.5000, 0.8851 ),
			   float3( 0.6500, 0.1259, 0.8851 ),
			   float3( 0.4325, 0.7509, 0.8670 ),
			   float3( -0.4340, 0.7500, 0.8670 ),
			   float3( -0.8665, -0.0009, 0.8670 ),
			   float3( -0.4325, -0.7509, 0.8670 ),
			   float3( 0.4340, -0.7500, 0.8670 ),
			   float3( 0.8665, 0.0009, 0.8670 ),
			   float3( 0.2158, 0.8763, 0.9070 ),
			   float3( -0.6510, 0.6250, 0.9070 ),
			   float3( -0.8668, -0.2513, 0.9070 ),
			   float3( -0.2158, -0.8763, 0.9070 ),
			   float3( 0.6510, -0.6250, 0.9070 ),
			   float3( 0.8668, 0.2513, 0.9070 )
		};


	float2	pixelSizeHigh;
	float2	pixelSizeLow;


	pixelSizeHigh.x = 2.0 / _c44.x;
	pixelSizeHigh.y = 2.0 / _c44.y;

	pixelSizeHigh.xy *= 1;

	pixelSizeLow.xy = pixelSizeHigh.xy;

	float2	vMaxCoC = float2(5.0, 10.0);

	float	radiusScale = 0.65;
	
	float	discRadius;
	float	discRadiusLow;
	float	centerDepth;
	float4	tdepth;

	float4  tapHigh;
	float4  tapLow;

	centerDepth = dist;
	
	discRadius = abs(centerDepth * vMaxCoC.y - vMaxCoC.x);
	discRadiusLow = discRadius * radiusScale;
	
	r1 = 0.0;

	for(int t = 0; t < 30; t++){
	
	    float2  coordLow = _v0.xy + (pixelSizeLow.xy * poisson[t].xy * discRadiusLow);
	    float2  coordHigh = _v0.xy + (pixelSizeHigh.xy * poisson[t].xy * discRadius);

	    tapLow = tex2D(s2, coordLow.xy) * poisson[t].z;
	    tapHigh = tex2D(s2, coordHigh.xy) * poisson[t].z;

	    tdepth = tex2D(s1, coordHigh.xy); 
	    tdepth.y = -_c77.x + _c77.y;
	    tdepth.y = 1.0 / tdepth.y;
	    tdepth.z = tdepth.y * _c77.y; 
	    tdepth.z = tdepth.z * -_c77.x; 
	    tdepth.x = _c77.y * -tdepth.y + tdepth.x;
	    tdepth.x = 1.0 / tdepth.x;

	    tdepth.w = tdepth.z * tdepth.x - _c78.w; 
	    tdepth.w += _c78.y * -r3.w; 
	    tdepth.w += 148;

	    tdepth.w = max(tdepth.w, 0); 
//[loop]
	    if(tdepth.w < vDofParams.y)
	        tapHigh.w = (tdepth.w - vDofParams.y)/(vDofParams.y - vDofParams.x);
	    else
	    {
	        tapHigh.w = (tdepth.w - vDofParams.y)/(vDofParams.z - vDofParams.y);
	        tapHigh.w = clamp(tapHigh.w, 0, vDofParams.w);
	    }

    	    tapHigh.w = tapHigh.w * 0.5 + 0.5;

	    tapLow.w = tapHigh.w;

	    float  tapBlur = abs(tapHigh.w * 2.0 - 1.0);
	    float4  tap = lerp(tapHigh, tapLow, tapBlur);

	    if(tap.w >= centerDepth)	tap.w = 1;
	    else	tap.w = abs(tap.w * 2.0 - 1.0);

	    r1.xyz += tap.xyz * tap.w;
	    r1.w += tap.w;
	}
	
	r1.xyz /= r1.w;
//[unroll]
r1.w=dot(r1.xyz, r1.xyz); 
r1.w=1.0/r1.w;
 
    r3 = lerp(_c0.x, r1, saturate(r1.w * 10000));
 
        r4 = r3;
        r5 = r3;
        r6 = r3;
        r7 = r3;
 
        r0.x=r0.z * r0.x - _c78.w; 
        r0.x=_c78.y * -r3.w + r0.x; 
        r1.w=max(r0.x, _c0.x);
        r0.x=1.0/_c78.z; 
        r0.x=r1.w * r0.x;
        r1.w=lerp(_c79.y, _c79.z, r0.x);
        r0.x=min(_c79.z, r1.w); 
 
        r0.z=dot(r7.xyz, _c0.yzw); 
        r8.x=dot(r3.xyz, _c0.yzw); 
        r8.y=dot(r4.xyz, _c0.yzw); 
        r8.z=dot(r5.xyz, _c0.yzw); 
        r8.w=dot(r6.xyz, _c0.yzw); 
        r1.w=dot(r8, _c1.xxxx); 
 
        r8=r8 - r1.w;
        r2.w=dot(r8, r8);
        r0.z=r0.z - r1.w;
        r0.z=r0.z * r0.z - r2.w;
        r0.x=r0.x*r0.x;
        if (r0.z<0.0)
        {
                r0.x=r0.x;
        } else r0.x=_c1.y; 
 
        r8=tex2D(s0, _v0.xy);


	r8.yz=_c1.yz; 
	r9=r8.w * -r8.z + _c2;
//[loop] 
	if (r9.w<0.0) 
	{
		r9=_c1.w; 
	} else r9=_c1.y; 

	r10=r8.w * abs(-_c6.x + _c6.yzwx); 
	if (r10.x<0.0)
	{
		r10.x=-_c1.w; 
	} else r10.x=-_c1.y;

	if (r10.y<0.0)
	{
		r10.y=-_c1.w; 
	} else r10.y=-_c1.y; 

	if (r10.z<0.0)
	{
		r10.z=-_c1.w; 
	} else r10.z=-_c1.y; 

	if (r10.w<0.0)
	{
		r10.w=-_c1.w; 
	} else r10.w=-_c1.y; 
//[unroll]
	r9=r9 + r10;
	r9=r9 * _c4.x; 

	r0.z=dot(r9, _c1.yyyy); 
	r0.z=r0.z + _c4.w; 
	r0.z=1.0/r0.z;
	r8.xzw=r3.xyz * r9.x;

	r7.xyz=r7 * _c4.w + r8.xzw; 
	r7.xyz=r4 * r9.y + r7;
	r7.xyz=r5 * r9.z + r7;
	
	r7.xyz=r6 * r9.w + r7;
	r7.xyz=r0.z * r7;
	r0.z=-r0.x + _c1.y; 
	r1.w=r0.x * _c1.x; 


	r3.xyz=r3 * r1.w;
	r3.xyz=r7 * r0.z + r3;
	r3.xyz=r4 * r1.w + r3;
	r3.xyz=r5 * r1.w + r3;
	r3.xyz=r6 * r1.w + r3;
	r4.x=_v0.y * _c5.x + _c5.y;
	r4.y=_v0.x * _c5.x + _c5.y;
	r4.z=_v0.y * _c5.x + _c5.y;

	r0.z=r4.y * _c77.z; 
	r0.z=r0.y * r0.z;
	r1.w=-r4.x * _c77.w; 
	r1.w=r0.y * r1.w;
	
	r5.xyz=r1.w * _c73; 
	r5.xyz=r0.z * _c72 + r5; 
	r5.xyz=-r0.y * _c74 + r5; 
	r5.xyz=r5 + _c75; 
	r0.y=-r5.z * _c77.z; 
	r0.y=1.0/r0.y;
	
	r6.x=r5.x * r0.y;
	
	r0.y=r5.z * _c77.w; 
	r0.y=1.0/r0.y;
	r6.y=r5.y * r0.y;

	r0.yz=-r4.yz + r6.xy;
	r0.y=r0.y * _c80.x;
	r0.z=r0.z * _c80.x;

	r4.x=r0.y * _c5.z;
	r4.y=r0.z * _c5.z;
	r5=tex2D(s6, _v0.xy);
	r1.w=r5.x + -_c85.x; 
	r4.zw=r3.xyxy * _c2.w; 
	r4.z=_v0.x * _c3.x + r4.z; 
	r4.w=_v0.y * _c3.y + r4.w;
	
	r5=tex2D(s5, r4.zw); 
	r2.w=r5.x + _c4.x; 

	r4.z=r4.x * r2.w + _v0.x; 
	r4.w=r4.y * r2.w + _v0.y; 
	r5.xyz=r3.xyz;
	r2.w=_c1.y; 
	r3.w=_c1.y; 

   for (int iii=0; iii<10; iii++)
    {
    r6.x=r4.x * r3.w + r4.z;
    r6.y=r4.y * r3.w + r4.w;
    r7=tex2D(s6, r6.xy);
    r5.w=r7.x - _c85.x;
//[loop]
    if (r5.w<-0.3)
    {
 	       r5.w=_c1.y; 
    } else r5.w=_c1.w; 
    r6=tex2D(s2, r6.xy);
    r5.xyz=r6.xyz * r5.w + r5.xyz;
		
    r2.w=r2.w + r5.w;
    r3.w=r3.w + _c1.y; 
    }
r2.w=1.0/r2.w;
r0.y=r0.y * _c44.x; 
r0.z=r0.z * _c44.y; 
r0.y=dot(r0.yz, r0.yz)+_c0.x;

r0.y=pow(r0.y, _c4.w); 
r0.y=r0.y * _c4.w; 
r0.x=r0.x + _c1.y;

r0.x=1.0/r0.x;
r0.x=saturate(r0.y * r0.x);

r4.xyz=r5 * r2.w - r3;	   
r0.xyz=r0.x * r4 + r3;
if (-r1.w<0.0) r0.xyz=r3.xyz;
if (-r0.w<0.0) r0.xyz=r1.xyz;
//[unroll]

r11=r0;

//**************************
#define RAYS
#ifdef RAYS
	float2 sunpos = 0.5;
	float2 _v1 = 0;
	float maxi = 0;
	int count = 0;
	float4 ratio = 0.1;
	float deltax = 0.04 / ratio;
	int samplex = 25 * ratio;

	for(int i = 1; i <= 25; i++)
	{
		for(int j = 1; j <= samplex; j++)
		{	

			r2 = tex2D(s2, _v1.xy);
			r2.x = saturate(cross(r5.xyz, 0.003) - 10.0);
//[loop]
			if((r5.x >= maxi) && r5.x) 
			{
				maxi = r5.x;
				sunpos.xy += _v1.xy;
				count ++;
			}
			_v1.x += deltax ;
		}
		_v1.x = 0.32;
		_v1.y += 0.015;
	}

	if(maxi) sunpos.xy /= count--;

	float4 color;
	//r0=tex2D(s2, _v0.xy);//hdr color
	r11=r0 + r11; 
	//color.xyz=0.0;

	int NUM_SAMPLES = 64;
	float Decay = 0.96;
	float Exposure = 1.0;
	float Weight = 0.20;
	float Density = 1.2;


	float2	ScreenLightPos;

	ScreenLightPos.xy = sunpos.xy;


	float2	texCoord = _v0;
	float2	deltaTexCoord = (texCoord.xy - ScreenLightPos.xy);
		
	deltaTexCoord *= 1.0 / NUM_SAMPLES * Density;

	
	float	illuminationDecay = 1.0;

	if(maxi)
	{

		for(int x = 0; x < NUM_SAMPLES; x++)
		{
	   	 	texCoord -= deltaTexCoord;
	    		float4 sample = tex2D(s0, texCoord.xy);
	    		if(sample.w) sample.xyz = 0.0;
	    	else
	    	{
			sample = tex2D(s2, texCoord.xy);
			sample.w = saturate(dot(sample.xyz, 0.333) - 9);
			if(sample.w == 0) sample.xyz = 0.0;
			sample.r *= 0.1;
			sample.g *= 0.095;
			sample.b *= 0.085;
	    	}
	    	    
	    	sample *= illuminationDecay * Weight;
	    	r11 += sample;
	    	illuminationDecay *= Decay;
		}
	}

//[unroll]	
	r11 *= Exposure;
#endif

	r1=tex2D(s4, _c0.x); //AdapLumSampler , 0.0
	r0.w=1.0/r1.x;
	r0.w=r0.w * _c81.y; //ToneMapParams.y
	r1.x=1.0/r0.w;
	r1.x=r1.x * _c81.x; //ToneMapParams.x
	r1.xyz=r2 * _c66.x - r1.x; //Exposure.x
	r2.xyz=max(r1, _c0.x); //0.0
	r1.xyz=r2 * _c81.z; //ToneMapParams.z
	r1.xyz=r1 * _c1.x; //0.25
	r0.xyz=r0 * _c66.x + r1; //Exposure.x
	r1.xyz=r0.w * r0;
	r1.x=dot(r1.xyz, _c0.yzw); //0.2125, 0.7154, 0.0721
	r0.xyz=r0 * r0.w - r1.x;
	r0.xyz=_c82.x * r0 + r1.x; //deSatContrastGamma.x
	r0.w=r1.x * _c84.w; //ColorShift.w
	r1.yzw=r0.w * _c84.xxyz; //ColorShift


	//darken as DarkeningAmount, but keeps colors
	r2.x=saturate(r0.w);
	r0.xyz=r0.xyz - _c84.xyz * r0.w; //ColorShift
	r0.xyz=r2.x * r0.xyz + r1.yzww;

	//coloring (green, brown, ...)
	r0.xyz=2.0 * r0.xyz * _c83.xyz; //ColorCorrect

	//keeps colors, changes luma
	r1.x=saturate(r1.x);
	r0.w=_c82.z - r8.y; //deSatContrastGamma.z
	r2.x=pow(r1.x, r0.w);

	_oC0.xyz=r0.xyz * r2.x; //orig
	_oC0.w=_c1.y; //1.0

r5=tex2D(s4, _c0.x);//adaptation luminance
r0=tex2D(s2, _v0.xy);


r0=r11;



r5.w=r5.x;
r5.w=AdaptationValue/r5.w;
r0.xyz=r0 * r5.w;
r3.xy=_c10.xwxw;
r3.xy=r3.xy * r5.w;
float4 dirtPaletteTex=tex2D(s13, _v0.xy);
float4 uvsrc=0.0;
float3 lensDirt=dirtPaletteTex.xyz; 
r7=tex2D(s3, _v0.xy);
#ifdef BLOOM_DISTORTION
r7.r=tex2D(s3, _v0.xy + BloomDAmount * -0.01).x;
r7.g=tex2D(s3, _v0.xy + BloomDAmount * 0).y;
r7.b=tex2D(s3, _v0.xy + BloomDAmount * 0.01).z;
r7 = r5.w * r7;
#else
float4 r7 = tex2D(s3, _v0.xy);
#endif	
r7.xyz+=r7.xyz*lensDirt.xyz*DirtyLensEffect;
//0.15 is bloom
r0=r7 * BloomAdditive + r0; //0.05 //TEMP DISABLED
r10=dot(r0.xyz, _c12.xyz);
r9.x=r10.x * 1.50;//c199.x;
r10.x=1.0/r10.x;
r10=r10 * r9.x;
r0=r0 * r10.x;
r0=r0 * _c7.y; 

r1=r0 * _c7.z;
r1=r1 + _c7.w;
r1.xyz=1.0/r1.xyz;
r0=r0 * r1;


//float lumasat=dot(r0.xyz,_c12.xyz);
float gris=dot(r0.xyz, 0.333);
float3 SaturatedColor=pow(r0.xyz, Saturation);
float Niveaudegris=dot(SaturatedColor.xyz, 0.333);
float3 Resultat=SaturatedColor.xyz*gris/(Niveaudegris+0.0001);
float4 Mix=lerp(1, 0.75, saturate(pow(depth/1000.0, 1)));
r0.xyz = pow(Resultat,Mix);

r0.xyz = lerp(r0.xyz, 0.5 * (1 + sin((r0.xyz - 0.5)*3.1415926)), AddContrast);
r0=BloomAmountMix*r7 * r3.x + r0;


//#define VIGNETTE
#ifdef VIGNETTE
float rovigpwr = 0.75; //for a circular vignette
float2 sqvigpwr = float2( 0.5, 0.5 ); // for the top and bottom of the screen
float vsatstrength = 1; // color distortion
float vignettepow = 1; // increases the contrast and sharpness
float vstrengthatnight = 10.25;
 float2 inTex = _v0; 
 float vhnd = 0.5;
 float4 voriginal = r0;
 float4 vcolor = voriginal;
 vcolor.xyz=1;
 inTex -= 0.5; // center
 inTex.y += 0.01; // offset from the center
 float vignette = 1.0 - dot( inTex, inTex );
 vcolor *= pow( vignette, vignettepow );
 float4 rvigtex = vcolor;
 rvigtex.xyz = pow( vcolor, 1 );
 rvigtex.xyz = lerp(float3(0.5, 0.5, 0.5), rvigtex.xyz, 2.0); // contrast
 rvigtex.xyz = lerp(float3(1,1,1),rvigtex.xyz,rovigpwr); // strength of the circular vinetty
 float4 vigtex = vcolor;
 vcolor.xyz = float3(1,1,1);
 float3 topv = min((inTex.y+0.5)*2,1.5) * 2; // top
 float3 botv = min(((0-inTex.y)+0.5)*2,1.5) * 2; // botton
 topv= lerp(float3(1,1,1), topv, sqvigpwr.x);
 botv= lerp(float3(1,1,1), botv, sqvigpwr.y);
 vigtex.xyz = (topv)*(botv);
 vigtex.xyz*=rvigtex.xyz; 
vigtex.xyz = lerp(vigtex.xyz,float3(1,1,1),(1-vstrengthatnight)*(1-vhnd)); //for a dark screen
 vigtex.xyz = min(vigtex.xyz,1);
 vigtex.xyz = max(vigtex.xyz,0);
 float3 vtintensity = dot(r0.xyz, float3(0.2125, 0.7154, 0.0721));
 r0.xyz = lerp(vtintensity, r0.xyz, ((((1-(vigtex.xyz*2))+2)-1)*vsatstrength)+1 );
 r0.xyz *= (vigtex.xyz);
#endif


focus = length(center.xy)*1.41421;
focus = pow(focus, 4);
//#define FOG
#ifdef FOG
r0 = addFog(_v0.xyz,r0);
r0 = addIngame(_v0.xyz,r0);
#endif
r0.xyz -= focus * Vignetting;

///////////////////////
    float3 normal0 = normalize(r8.xyz/*tex2D(s0, _v0.xy)*/ * 2.0f - 1.0f); // получаем значения X,Y,Z из нормальной-карты по координатам texCoords и заодно приводим к виду: -1, 1.
    // преобразуем координаты пикселя 
    float3 pixelPosition = float3(_c76.x * _v0.x,_c76.y * _v0.y,0);//c44.x,c44.y
    // задаем буффер-пикселя 
    float3 finalColor = 0;
    for (int i=0;i<numberOfLights;i++)
    {
        // подсчитываем все источники света и записываем их в буффер
        finalColor += CalculateLight(lights[i], normal0, pixelPosition);
    }
    // возращаем результат шейдера, эмбиент * результирующий цвет пикселя от света, делаем multiply с картой цвета и отдаем с альфой карты цвета
    r0 = float4((ambient.xyz + finalColor) * r0.rgb, r0.a);

_oC0.xyz=r0.xyz;


//_oC0=0.077*tex2D(s15, _v0.xy);//s2



//_oC0.xyz=1.0;






	//_oC0.w=1.0;
	return _oC0;
}


//mine temporary post process
technique Shader_C215BE6E
{
    pass p0 
    {
		PixelShader  = compile ps_3_0 PS_C215BE6E();
	}
}