//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//		M A S T E R  E F F E C T  1.2.1		     //
//	   effect.txt shader file for GTA San Andreas        //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
// 		   made by : Marty McFly                     //
// 		Original Codes & Shaders by: 		     //		
//      Matso, Ceejay.dk, SSontech, icelaglace, AAA	     //
//      Reinhard, HeliosDoubleSix, PetkaGtA, DKT70           //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
float4 ScreenSize=float4(1280,(1/1280),(1280/720),(720/1280));
/***************************************************************
|//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//|
|\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\|
|   Choose Effects to enable by removing "//" before #define   |
|//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//|
|\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\|
***************************************************************/


#define LENZ				// Skyrim ENB Lensflare
//#define SKYRIMTONEMAPV8		// Collection of Skyrim post process shaders
//#define FINALADJUSTER			// Saturation, Contrast, Brightness which come after all other effects 
//#define AHDR				// something ripped from Portal2x2, doesn't work like in GTA IV
//#define TVLEVELS			// Adjusts the brightness/darkness of the scene with a straight line instead of a curve.
#define BORDER			// 1 pixel broad border around the screen to fix white outlining by SMAA/FXAA injectors
#define BORDER_DOF			// use this option instead of border if using the dof in this effect.txt
//#define COLORWASHOUT			// Color washout from GTA IV
//#define MATSO_FLARE			// Matso's anamorphic flare with some adjustments
#define NOISE				// Adds Film Grain
#define USE_DITHER			// Applies dithering to simulate more colors than your monitor can display. This lessens banding artifacts (mostly caused by Vignette)
#define VIBRANCEPASS			// Intelligently saturates (or desaturates if you use negative values) the pixels depending on their original saturation.
//#define CHROMATICABBERATION		// Lens chromatic abberation and distortion
#define HDR				// No actual HDR - fog colors, exposure, gamma and blueshift adjustments
//#define COLORMODBLOOM			// BETA: colormod/enb-like bloom
//#define LIGHTSCATTERING		// Godrays
//#define FILMICPASS			// OBGE filmic image improvement
//#define REINHARD_ICE21		// reinhard tonemapping with iCEnhancer 2.1 code improvement
#define DOF				// Dynamic DOF, focus on foreground ->background blurred, focus on background -> foreground blurred
#define GAMMACORRECTION		// Gamma adjustment
//#define FILMICCURVE			// filmic brightness curving
//#define HD6_VIGNETTE			// Advanced vignette
#define BORISVIGNETTE			// regular vignette shader - colorable
//#define CROSSPROCESS			// Cross Processing/Technicolor, imitates 50/60's image look
#define CROSSICE		        // adds Crysis-like color shader (you only read crysis and enable it, huh? :D )
//#define HSVULTIMATE			// HSV based color grading, real advanced, really difficult to adjust

 


   /*-----------------------------------------------------------.
  /                        Lenz Parameters                      /
  '-----------------------------------------------------------*/

float	ELenzIntensity=0.0;				// Lensflare intensity
float	ELenzPower=1.0;					// Lensflare power
float   LenzThresh = 0.98;				// Brightness threshold for lensflare


   /*-----------------------------------------------------------.
  /                    Skyrim tonemap settings                  /
  '-----------------------------------------------------------*/

#define POSTPROCESS				 	1		//1,2,3,4,5,6

//PP 1 values
	float	EAdaptationMinV1 = 0.05;
	float	EAdaptationMaxV1 = 0.125;
	float	EContrastV1 = 1.0;
	float	EColorSaturationV1 = 1.0;
	float	EToneMappingCurveV1 = 6.0;
//PP 2 Values	
	float	EAdaptationMinV2=0.36;
	float	EAdaptationMaxV2=0.29;
	float	EToneMappingCurveV2=8.0;
	float	EIntensityContrastV2=2.5;
	float	EColorSaturationV2=3.2;
	float	EToneMappingOversaturationV2=180.0;
//PP 3 Values
	float	EAdaptationMinV3 = 0.001; // Higher Gets Darker
	float	EAdaptationMaxV3 = 0.025;
	float	EToneMappingCurveV3 = 30.0; // Higher gets darker
	float	EToneMappingOversaturationV3 = 111160.0;
//PP 4 Values
	float	EAdaptationMinV4 = 0.2;
	float	EAdaptationMaxV4 = 0.125;
	float	EBrightnessCurveV4 = 0.7;
	float	EBrightnessMultiplierV4 = 0.45;
	float	EBrightnessToneMappingCurveV4 = 0.3;
//PP 5 Values
	float	EAdaptationMinV5 = 0.08; // 0.28 // lower gets brighter
	float	EAdaptationMaxV5 = 0.20; // 0.30 // 0.65 // 0.35 // 0.29
	float	EToneMappingCurveV5 = 8; // 130
	float	EIntensityContrastV5 = 3.475; // 3.375 // 4.75 // 3.975
	float	EColorSaturationV5 = 4; // 1.65;
	float 	HCompensateSatV5 = 2; // Compensate for darkening caused by increasing EColorSaturationV2	
	float	EToneMappingOversaturationV5 = 180.0;
//PP 6 values
	float	EBrightnessV6Day = 2.9;							//higher than normal values needed for shadow and brightspot to work correctly
	float 	EIntensityContrastV6Day = 2.2;					//very powerful, be careful of high values
	float	EColorSaturationV6Day = 2.0; 					//very powerful, be careful of high values
	float 	HCompensateSatV6Day = 2.9; 						//higher than normal values needed for shadow and brightspot to work correctly
	float	EAdaptationMinV6Day = 0.64;						//works differently than usual, be careful with too high/too low values
	float	EAdaptationMaxV6Day = 0.24;						//works differently than usual, be careful with too high/too low values
	float	EAdaptationCompensateV6Day = 1.0;				//darkening that happens after adaptation, higher is darker
	float	EBrightnessMinV6Day = 0.0;						//clamps rgb brightness, range 0-1
	float	EBrightnessMaxV6Day = 1.0;						//clamps rgb brightness, range 0-1
	float	EToneMappingCurveV6Day = 8;
	float	EToneMappingOversaturationV6Day = 2500.0;		
	float   EHSVDesatCurveDay = 1.444444;					//intelligent desaturation, idea credit goes to saltr. higher is less saturated
	float3 	EMoodColorDay = float3(0.0, 0.0, 0.0);			
	float   EMoodAmountDay = 0.0;							//higher is stronger, range 0-1
	float   EMoodCurveDay = 4.0;							//lower is stronger, be careful of going under 1.0
	float3 	EShadowColorDay = float3(0.0, 0.0, 0.55);		//unlike other two color controls, colors shadows/dark spots only. max is 2.55
	float   EShadowThresholdDay = 1.0;						//lower is stronger, be careful of low values under 0.4
	float   EShadowCurveDay = 16.0;							//lower is stronger, be careful of low values under 4
	float3 	EBrightSpotColorDay = float3(0.0, 0.25, 2.55);
	float   EBrightSpotThresholdDay = 1.0;					//lower is stronger, be careful of low values under 0.4
	float   EBrightSpotCurveDay = 24.0;						//lower is stronger, be careful of low values under 4


   /*-----------------------------------------------------------.
  /                    Final Adjuster settings                  /
  '-----------------------------------------------------------*/

float3 SaturationDay 	= float3(1.0, 1.0, 1.0);		//Adjusts saturation, higher is more saturated
float BrightnessDay 	= 0.8;		//Adjusts brightness, higher is brighter
float ContrastDay 		= 0.2;


   /*-----------------------------------------------------------.
  /                        AHDR settings                        /
  '-----------------------------------------------------------*/

float HDRthreshold = 0.0;


   /*-----------------------------------------------------------.
  /                        TVLevels  settings                   /
  '-----------------------------------------------------------*/

#define DARK_LEVEL 16.0				//[0 to 255] Darkness strength. Higher values mean darker shadows, 0 means no change. Default is 16.0
#define BRIGHT_LEVEL 0.0			//[0 to 255] Brightness strength. Higher values mean brighter lights, 0 means no change. Default is 20.0

   /*-----------------------------------------------------------.
  /                      Border settings                        /
  '-----------------------------------------------------------*/

float screenwidth = 1680;
float screenheight = 1050;

   /*-----------------------------------------------------------.
  /                      Colorwashout settings                  /
  '-----------------------------------------------------------*/

float	ColorSaturation=0.25;				// Saturation control
float	ColorWashoutThreshold=0.85;			// Threshold above which colors get washed out
float	ColorWashoutPow=1.0;				// Effect Power
float	ColorWashoutAmount=1.0;				// Effect Amount
float   ColorDullnessAmount=1.0;			// Let the colors look linear like SA and GTA IV without ENB
float   ExpAdjustment=1.1;				// High Color Dullnessamount darkens screen, compensate with this here


   /*-----------------------------------------------------------.
  /                  Anamorphic flare settings                  /
  '-----------------------------------------------------------*/

#define fFlareLuminance 0.99						// bright pass luminance value 
#define fFlareBlur 1000.0						// manages the size of the flare
#define fFlareIntensity 1.00					// effect intensity
#define fFlareTint	float3(0.1, 0.15, 1.0)		// effect tint
//#define fFlareVertical
#define fFlareHorizontal
const float FlareRadius = 1.0/512.0;


   /*-----------------------------------------------------------.
  /                  	  Noise settings                        /
  '-----------------------------------------------------------*/

float4	Timer;								//leave as it is, needed for grain frequency
#define SEED			Timer.w					//same
#define fGrainFreq 500.0						// image grain frequency, for some reason high values cause shadow flickering
#define fGrainScale 0.0001						// grain effect scale

   /*-----------------------------------------------------------.
  /                  	  Dither settings                       /
  '-----------------------------------------------------------*/

float screen_size = 1680; 				// Horizontal screen resolution

   /*-----------------------------------------------------------.
  /                  	  Vibrance settings                     /
  '-----------------------------------------------------------*/

float Vibrance = 0.925; 					//[-1.0 to 1.0] 

   /*-----------------------------------------------------------.
  /                Chromatic Abberation settings                /
  '-----------------------------------------------------------*/

float ChromaticAmount = 0.009; 				// Amount of chromatic aberration on the edges
float LensSize = 0.55;   				// 0.5 = Original image size; 1.0 = Zoomed
float LensDistortion = 0.05;   				// Can be negative to stretch the image
float LensDistortionCubic = 0.05;			// similiar to above, better results if same like LensDistortion

   /*-----------------------------------------------------------.
  /                        FXAA settings                        /
  '-----------------------------------------------------------*/

#define FXAA_LINEAR 0
#define FXAA_QUALITY__EDGE_THRESHOLD (1.0/32.0)
#define FXAA_QUALITY__EDGE_THRESHOLD_MIN (1.0/16.0)
#define FXAA_QUALITY__SUBPIX_CAP (7.0/8.0)
#define FXAA_QUALITY__SUBPIX_TRIM (1.0/12.0)
#define FXAA_QUALITY__SUBPIX_TRIM_SCALE  (1.0/(1.0 - FXAA_QUALITY__SUBPIX_TRIM))
#define FXAA_SEARCH_STEPS     16
#define FXAA_SEARCH_THRESHOLD (1.0/4.0)

   /*-----------------------------------------------------------.
  /                   HDR + Sharpen settings                    /
  '-----------------------------------------------------------*/

float Defog=0.000; 					// Strength of FogColor, higher = more.
float4 FogColor={0.0, 0.0, 0.0, 0.0}; 			// Lens-style color filters for Blue, Red, Yellow, White.
float Exposure=0.90; 					// Contrast settings, higher = brighter, but also more white.
float Gamma=0.450; 					// Gamma settings for darker or lighter shadows and dark areas, higher = darker.
float BlueShift=0.4; 					// Shifts entire color spectrum towards blue, good for images too yellow, but this is global.
float sharps = 0.0; 					//sharpen strength; always offset value x 2
float offsetv = 0.0;					//sharpen offset 
float sxres = 1920; 					// Horizontal Resolution setting x 2
float syres = 1080; 					// Vertical Resolution setting x 2
float aspect = 1.77777777778; 				// horizontal/vertical resolution



   /*-----------------------------------------------------------.
  /                Colormod-like bloom settings                 /
  '-----------------------------------------------------------*/


float BLOOMPOW = 3.0;					// Bloompower
const float BloomDownsample = 8;			// Bloom Downsampling, better leave as it is if you dunno what it does
const float Bloomradius = 1.0/32.0;			// Radius, how wide bloom bleeds its color and brightness into other areas
float BLOOMTHRESH = 2.6;				// Threshold above which pixels get sampled for bloom
float BloomSat = 0.05;


   /*-----------------------------------------------------------.
  /              Light Scattering settings ("Godrays")          /
  '-----------------------------------------------------------*/


//#define TINTCOLOR   				//Shifts godray color
	#define COLORING				//Colorizes godrays, original color gets lost
	#define REDAMOUNT  		1.2 		//power of red godray color
	#define GREENAMOUNT 		0.8 		//power of green godray color
	#define BLUEAMOUNT 		2.0 		//power of blue godray color


float2 ScreenLightPos = {0.5,0.5};			//where the Godrays appear, 0.5, 0.5 means screen center
float Density = 1.50;					//power of the rays
static int NUM_SAMPLES = 128;				//how many samples
float Weight = 0.5;
float Decay = 1.0;
float SStresh = 0.938;					//pixel threshhold, above this level pixels get sampled, let it stay like this!
float SunExposure = 2.0;				//Sun gets brighter with this value


   /*-----------------------------------------------------------.
  /                    Filmicpass settings                      /
  '-----------------------------------------------------------*/


	float Strenght 		= 0.25;			//  from 0.0 to 1.0
	float BaseGamma 	= 1.6;			//  from 0.1.0 to 2.5
	float Fade		= 0.2;			//  from 0.0 to 0.5
	float Contrast		= 1.0;			//  from 0.6 to 1.0
	float Saturation	= -0.15;			//  from 0.25 to -0.25
	float Bleach		= 0.005;		//  from 0.0 to 1.0
	float RedCurve		= 6.0;			//  from 1.0 to 10.0
	float GreenCurve	= 6.0;			//  from 1.0 to 10.0
	float BlueCurve		= 6.0;			//  from 1.0 to 10.0
	float BaseCurve		= 1.5;			//  from 1.0 to 10.0
	float EffectGammaR	= 1.0;			//  from 0.1 to 2.5
	float EffectGammaG	= 1.0;			//  from 0.1 to 2.5
	float EffectGammaB	= 1.0;			//  from 0.1 to 2.5
	float EffectGamma	= 0.75;			//  from 0.1 to 2.5
	float Linearization	= 1.3;			//  from 1.0 to 2.5


   /*-----------------------------------------------------------.
  /                    Reinhard tonemap settings                /
  '-----------------------------------------------------------*/

#define REINHARD_SAT	 1.7 			//Saturation only for Reinhard tonemap
	#define TONEMAPEXP 	 3.4 			//Exposure only for Reinhard tonemap
	#define LUM_FACTOR 	float4(0.299, 0.587, 0.114, 0.0) //DON NOT CHANGE IT


   /*-----------------------------------------------------------.
  /                    PetkaGtA DOF settings                    /
  '-----------------------------------------------------------*/

//constants, don't change

//float ScreenScaleY;
//float ScreenSize;
float PI = 3.14159265;
float2 texel = {0.0009765625,0.00130208333333333333333333333333};

//your screen resolution

float2 screenRes = {1280,720};

//DOF params

//DOF Precision 1, 2 and 3. 1 means standard DOF smoothness, 2 provides double precision and 3 provides triple precision. 
//the more precision, the smoother the blurred areas get.
//IMPORTANT: PREC_2 and _3 are performance intensive!
#ifdef DOF



#define DOF_PREC_1
//#define DOF_PREC_2
//#define DOF_PREC_3


#endif
float focalDepth = 20.5;
float focalLength = 10.5;
float fstop = 20.5;
bool manualdof = true; 				//manual dof calculation
float ndofstart = 0.002; 					//near dof blur start
float ndofdist = 0.04; 					//near dof blur falloff distance
float fdofstart = 0.0; 					//far dof blur start
float fdofdist = 0.1; 					//far dof blur falloff distance
bool autofocus = true;
float2 focus = float2(0.5,0.5);
float CoC = 0.300;					//Circle of confusion http://en.wikipedia.org/wiki/Circle_of_confusion
bool noise = false; 					//DOF noise, adds grain
float namount = 0.0000; 				//grain amount
float DOFdownsample = 3.5;
float maxblur = 3.0; 					//Power of DOF blur
static const int samples = 26; 				//more samples =better quality but also less fps
static const int rings = 2; 				//ring count, DO NOT GO BEYOND 2 OR GAME CRASH!!!
float threshold = 1.90;					//threshold for gain
float gain =89.0;					//brightens blurred areas to make bokeh effect visible
float bias = 0.0;
float fringe = 0.25;
float znear = 1.9; 					//camera clipping start (blur start)
float zfar = 2122.0; 					//camera clipping end (distance beyond which blur doesn't get bigger anymore)
bool pentagon = false; 					//use pentagon as bokeh shape? Bugged!
float feather = 1.0; 					//pentagon shape feather

//vignetting, darkens screen borders
bool vignetting = false;
float vignout = 25.31; 					//vignetting outer border
float vignin = 5.30; 					//vignetting inner border
float MartyMcFly = 25.30; 				//f-stops till vignette fades


   /*-----------------------------------------------------------.
  /                       Gamma settings                        /
  '-----------------------------------------------------------*/

#define GammaAmount       4.0 			//Gamma intensity

   /*-----------------------------------------------------------.
  /                       Filmiccurve settings                  /
  '-----------------------------------------------------------*/

float	FilmicExposure = 7;

//configuring the following stuff is very difficult, let it stay like this
//explanation is senseless because only the most advanced coders really understand 
//what each value does (not even me), maybe only the author. Set one to 10 and look what happens
//is the best way to get its effect

		float	ShoulderStrength = 0.35;
		float	LinearStrength = 0.6;
		float	LinearAngle = 0.05;
		float	ToeStrength = 0.22;
		float	ToeNumerator = 0.04;
		float	ToeDenominator = 0.205;
		float	FilmWeight = 13.5; // 20

float3 Uncharted2Tonemap(float3 x)
{
	return ((x*(ShoulderStrength*x+LinearAngle*LinearStrength)+ToeStrength*ToeNumerator)/(x*(ShoulderStrength*x+LinearStrength)+ToeStrength*ToeDenominator))-ToeNumerator/ToeDenominator;
}


   /*-----------------------------------------------------------.
  /                     HD6 Vignette settings                   /
  '-----------------------------------------------------------*/

//#define LEFTANDRIGHT 				//only enable one of the three options here!
#define TOPANDBOTTOM 				//only enable one of the three options here!
//#define CORNERDARKEN 				//only enable one of the three options here!

float   SquareTop = 0.58;              		// TOP of Screen.
float   SquareBottom = 0.58;   			// BOTTOM of Screen.
float   CircularPower = 0.5;    		// CIRCULAR Vignette Amount.
float   ColorDistortion = -0.666;  		// COLOR Distortion Strength.
float   ContrastSharpen = 11.666;       	// Contrast & Sharpness Increase.
float   VignetteBorder = 5.5;            	// High value means black bar, low means smooth gradient


   /*-----------------------------------------------------------.
  /                   Standard Vignette settings                /
  '-----------------------------------------------------------*/

float	EVignetteAmount=2.5;				//Darkening/coloring power
float	EVignetteCurve=1.5;				//Vignette Curve (helpful huh? :D)
float	EVignetteRadius=0.94; 				//Radius beyond which the screen starts to get darkened/colored
	//#define VIGNCOLORING				//optional colorable vignette, ever wanted to make a red vignette or such? here you go

	#define VIGNREDAMOUNT		0.0		//self-explaining I think
	#define VIGNGREENAMOUNT		5.0		//self-explaining I think
	#define VIGNBLUEAMOUNT		0.0		//self-explaining I think

   /*-----------------------------------------------------------.
  /                    Crossprocess settings                    /
  '-----------------------------------------------------------*/


float CrossContrast =1.0;		//Image contrast
float CrossSaturation2 = 1.0;		//Image Saturation
float CrossBrightness = 0.0;		//Image Brightness
float CrossAmount = 2.0;		//amount of color changing
float2 crossMatrix [3] =
 {
	float2 (1.03, 0.04),
	float2 (1.09, 0.01),
	float2 (0.78, 0.13),
 };

   /*-----------------------------------------------------------.
  /                    color mood/crossice settings             /
  '-----------------------------------------------------------*/

float fRatio = 2.0;				//power of the color distortion
float moodR = 1.0;				//preconfigured by ice
float moodG = 1.1;				//preconfigured by ice
float moodB = 0.5;				//preconfigured by ice

   /*-----------------------------------------------------------.
  /                     Color Grading settings                  /
  '-----------------------------------------------------------*/


#define ENABLE_HSV_CONVERSION  			// Enables HSV Conversion. Required for color controls below.
	#define ENABLE_SATURATION_ADJUSTMENT		// Enables color saturation control for the entire scene.
	#define ENABLE_INTENSITY_ADJUSTMENT  		// Enables brightness control for the entire scene.
	#define ENABLE_HUE_ADJUSTMENT    		// Enables hue control for the entire scene.
	#define ENABLE_COLOR_EQUALIZER    		// Enables the Color Equalizer system for advanced tweaking.

	//#define ENABLE_POW_CURVE    			// Enables power curve controls.

	// Color Saturation Variables
	float	EColorSaturationMod=0.0;  		// Adds to the overall color saturation. Range: -1 to 1. Default: 0.0
	float	EColorSaturationMult=1.0;  		// Multiplies the color saturation. Range: 0.0 (greyscale) to ???. Default: 1.0
	float	EColorSaturationPow=1.0;  		// Curves the color saturation. Range: 0.0 to ???. Default: 1.0

	// Color Intensity Variables
	float   EColorIntensityMod=0.0;  		// Adds to the overall brightness. Range: -1 to 1. Default: 0.0
	float   EColorIntensityMult=1.0;  		// Multiplies the color intensity. Range: 0.0 (black) to ???. Default: 1.0
	float   EColorIntensityPow=1.0;  			// Curves the color intensity. Range: 0.0 to ???. Default: 1.0

	// Color Hue Variables
	float   EColorHueMod=0.0;    			// Adds to the overall color hue (color shifting). Range: -1 to 1. Default: 0.0
	float   EColorHueMult=1.0;    			// Multiplies the color hue (color shifting). Range: 0.0 to ???. Default: 1.0
	float	EColorHuePow=1.0;    			// Curves the color hue. Range: 0.0 to ???. Default: 1.0.

	// Color Equalizer Variables
	// Adjust modifier to specific saturations. Range: -??? to ???. Default: 0.0
	float	SaturationModRed = 0.0;
	float	SaturationModOrange = 0.0;
	float	SaturationModYellow = 0.0;
	float	SaturationModGreen = 0.0;
	float	SaturationModCyan = 0.0;
	float	SaturationModBlue = 0.0;
	float	SaturationModMagenta = 0.0;

	// Adjust multiplier to specific saturations. Range: 0.0 to ???. Default: 0.0
	float	SaturationMultRed = 0.0;
	float	SaturationMultOrange = 0.0;
	float	SaturationMultYellow = 0.0;
	float	SaturationMultGreen = 0.0;
	float	SaturationMultCyan = 0.0;
	float	SaturationMultBlue = 0.0;
	float	SaturationMultMagenta = 0.0;

	// Adjust curve of specific saturations. Range: -1.0 to ???. Default: 0.0
	float	SaturationPowRed = 0.0;
	float	SaturationPowOrange = 0.0;
	float	SaturationPowYellow = 0.0;
	float	SaturationPowGreen = 0.0;
	float	SaturationPowCyan = 0.3;
	float	SaturationPowBlue = 0.0;
	float	SaturationPowMagenta = 0.0;


//////////////////////////////////-----------------------------------------------------------------]
//////////////////////////////////////////////
// DO NOT MODIFY ANYTHING BELOW THIS LINE,  //
// UNLESS YOU KNOW HLSL SHADER PROGRAMMING. //
//////////////////////////////////////////////
//////////////////////////////////-----------------------------------------------------------------]

//#define COLORCORRMAIN


//#ifdef HSVULTIMATE
// List of floating point values for hues, used in equalizer.
float	HueRed = 0.0;
float	HueOrange = 0.08333333;
float	HueYellow = 0.16666667;
float	HueGreen = 0.33333333;
float	HueCyan = 0.5;
float	HueBlue = 0.66666667;
float	HueMagenta = 0.83333333;
float	HueRed2 = 1.0;

float	HueSkin = 0.07;
float	HueSky = 0.58;


float ColorEqualizerMult(in float H)
{
float SMult = 1.0;
SMult += SaturationMultRed * ( 1.0 - min( 1.0, abs( ( HueRed - H ) / ( HueRed - HueOrange ) ) ) );
SMult += SaturationMultOrange * ( 1.0 - min( 1.0, abs( ( HueOrange - H ) / ( HueOrange - HueYellow ) ) ) );
SMult += SaturationMultYellow * ( 1.0 - min( 1.0, abs( ( HueYellow - H ) / ( HueYellow - HueGreen ) ) ) );
SMult += SaturationMultGreen * ( 1.0 - min( 1.0, abs( ( HueGreen - H ) / ( HueGreen - HueYellow ) ) ) );
SMult += SaturationMultCyan * ( 1.0 - min( 1.0, abs( ( HueCyan - H ) / ( HueCyan - HueGreen ) ) ) );
SMult += SaturationMultBlue * ( 1.0 - min( 1.0, abs( ( HueBlue - H ) / ( HueBlue - HueCyan ) ) ) );
SMult += SaturationMultMagenta * ( 1.0 - min( 1.0, abs( ( HueMagenta - H ) / ( HueMagenta - HueBlue ) ) ) );
SMult += SaturationMultRed * ( 1.0 - min( 1.0, abs( ( HueRed2 - H ) / ( HueRed2 - HueMagenta ) ) ) );
return SMult;
}

float ColorEqualizerMod(in float H)	
{
float SMod = 0.0;
SMod += SaturationModRed * ( 1.0 - min( 1.0, abs( ( HueRed - H ) / ( HueRed - HueOrange ) ) ) );
SMod += SaturationModOrange * ( 1.0 - min( 1.0, abs( ( HueOrange - H ) / ( HueOrange - HueYellow ) ) ) );
SMod += SaturationModYellow * ( 1.0 - min( 1.0, abs( ( HueYellow - H ) / ( HueYellow - HueGreen ) ) ) );
SMod += SaturationModGreen * ( 1.0 - min( 1.0, abs( ( HueGreen - H ) / ( HueGreen - HueYellow ) ) ) );
SMod += SaturationModCyan * ( 1.0 - min( 1.0, abs( ( HueCyan - H ) / ( HueCyan - HueGreen ) ) ) );
SMod += SaturationModBlue * ( 1.0 - min( 1.0, abs( ( HueBlue - H ) / ( HueBlue - HueCyan ) ) ) );
SMod += SaturationModMagenta * ( 1.0 - min( 1.0, abs( ( HueMagenta - H ) / ( HueMagenta - HueBlue ) ) ) );
SMod += SaturationModRed * ( 1.0 - min( 1.0, abs( ( HueRed2 - H ) / ( HueRed2 - HueMagenta ) ) ) );
return SMod;
}

float ColorEqualizerPow(in float H)	
{
float SPow = 1.0;
SPow += SaturationPowRed * ( 1.0 - min( 1.0, abs( ( HueRed - H ) / ( HueRed - HueOrange ) ) ) );
SPow += SaturationPowOrange * ( 1.0 - min( 1.0, abs( ( HueOrange - H ) / ( HueOrange - HueYellow ) ) ) );
SPow += SaturationPowYellow * ( 1.0 - min( 1.0, abs( ( HueYellow - H ) / ( HueYellow - HueGreen ) ) ) );
SPow += SaturationPowGreen * ( 1.0 - min( 1.0, abs( ( HueGreen - H ) / ( HueGreen - HueYellow ) ) ) );
SPow += SaturationPowCyan * ( 1.0 - min( 1.0, abs( ( HueCyan - H ) / ( HueCyan - HueGreen ) ) ) );
SPow += SaturationPowBlue * ( 1.0 - min( 1.0, abs( ( HueBlue - H ) / ( HueBlue - HueCyan ) ) ) );
SPow += SaturationPowMagenta * ( 1.0 - min( 1.0, abs( ( HueMagenta - H ) / ( HueMagenta - HueBlue ) ) ) );
SPow += SaturationPowRed * ( 1.0 - min( 1.0, abs( ( HueRed2 - H ) / ( HueRed2 - HueMagenta ) ) ) );
return SPow;
}

float3 HUEtoRGB(in float H)
{
   float R = abs(H * 6.0 - 3.0) - 1.0;
   float G = 2.0 - abs(H * 6.0 - 2.0);
   float B = 2.0 - abs(H * 6.0 - 4.0);
   return saturate(float3(R,G,B));
}

float RGBCVtoHUE(in float3 RGB, in float C, in float V)
{
     float3 Delta = (V - RGB) / C;
     Delta.rgb -= Delta.brg;
     Delta.rgb += float3(2.0,4.0,6.0);
     Delta.brg = step(V, RGB) * Delta.brg;
     float H;
     H = max(Delta.r, max(Delta.g, Delta.b));
     return frac(H / 6.0);
}

float3 HSVtoRGB(in float3 HSV)
{
   float3 RGB = HUEtoRGB(HSV.x);
   return ((RGB - 1) * HSV.y + 1) * HSV.z;
}
 
float3 RGBtoHSV(in float3 RGB)
{
   float3 HSV = 0.0;
   HSV.z = max(RGB.r, max(RGB.g, RGB.b));
   float M = min(RGB.r, min(RGB.g, RGB.b));
   float C = HSV.z - M;
   if (C != 0.0)
   {
     HSV.x = RGBCVtoHUE(RGB, C, HSV.z);
     HSV.y = C / HSV.z;
   }
   return HSV;
}
//#endif


float GetLuminance(float3 sample)
	 {
		 return dot(sample.xyz, 0.333);
	 }
#ifdef FILMICPASS	 
float4 FilmPass(float4 B)
{
	float4 G = B;
	float4 H = 0.01;
 
	B = pow(abs(B), Linearization);
	B = lerp(H, B, Contrast);
 
	float A = GetLuminance(B.rgb);
	float4 D = A;
 
	B = pow(abs(B), 1.0 / BaseGamma);
 
	float a = RedCurve;
	float b = GreenCurve;
	float c = BlueCurve;
	float d = BaseCurve;
 
	float y = 1.0 / (1.0 + exp(a / 2.0));
	float z = 1.0 / (1.0 + exp(b / 2.0));
	float w = 1.0 / (1.0 + exp(c / 2.0));
	float v = 1.0 / (1.0 + exp(d / 2.0));
 
	float4 C = B;
 
	D.r = (1.0 / (1.0 + exp(-a * (D.r - 0.5))) - y) / (1.0 - 2.0 * y);
	D.g = (1.0 / (1.0 + exp(-b * (D.g - 0.5))) - z) / (1.0 - 2.0 * z);
	D.b = (1.0 / (1.0 + exp(-c * (D.b - 0.5))) - w) / (1.0 - 2.0 * w);
 
	D = pow(abs(D), 1.0 / EffectGamma);
 
	float4 Di = 1.0 - D;
 
	D = lerp(D, Di, Bleach);
 
	D.r = pow(abs(D.r), 1.0 / EffectGammaR);
	D.g = pow(abs(D.g), 1.0 / EffectGammaG);
	D.b = pow(abs(D.b), 1.0 / EffectGammaB);
 
	if (D.r < 0.5)
		C.r = (2.0 * D.r - 1.0) * (B.r - B.r * B.r) + B.r;
	else
		C.r = (2.0 * D.r - 1.0) * (sqrt(B.r) - B.r) + B.r;
 
	if (D.g < 0.5)
		C.g = (2.0 * D.g - 1.0) * (B.g - B.g * B.g) + B.g;
	else
		C.g = (2.0 * D.g - 1.0) * (sqrt(B.g) - B.g) + B.g;
 	//if (AgainstAllAutority) then fckoff else fckoff
	if (D.b < 0.5)
		C.b = (2.0 * D.b - 1.0) * (B.b - B.b * B.b) + B.b;
	else
		C.b = (2.0 * D.b - 1.0) * (sqrt(B.b) - B.b) + B.b;
 
	float4 F = lerp(B, C, Strenght);
 
	F = (1.0 / (1.0 + exp(-d * (F - 0.5))) - v) / (1.0 - 2.0 * v);
 
	float r2R = 1.0 - Saturation;
	float g2R = 0.0 + Saturation;
	float b2R = 0.0 + Saturation;
 
	float r2G = 0.0 + Saturation;
	float g2G = (1.0 - Fade) - Saturation;
	float b2G = (0.0 + Fade) + Saturation;
 
	float r2B = 0.0 + Saturation;
	float g2B = (0.0 + Fade) + Saturation;
	float b2B = (1.0 - Fade) - Saturation;
 
	float4 iF = F;
 
	F.r = (iF.r * r2R + iF.g * g2R + iF.b * b2R);
	F.g = (iF.r * r2G + iF.g * g2G + iF.b * b2G);
	F.b = (iF.r * r2B + iF.g * g2B + iF.b * b2B);
 
	float N = GetLuminance(F.rgb);
	float4 Cn = F;
 
	if (N < 0.5)
		Cn = (2.0 * N - 1.0) * (F - F * F) + F;
	else
		Cn = (2.0 * N - 1.0) * (sqrt(F) - F) + F;
 
	Cn = pow(abs(Cn), 1.0 / Linearization);
 
	float4 Fn = lerp(B, Cn, Strenght);
	return Fn;
}
#endif
//--------------------------------------------------------------------------------------
// Textures
//--------------------------------------------------------------------------------------
texture2D texColor;
texture2D texDepth;
texture2D texNoise;
//--------------------------------------------------------------------------------------
// Sampler Inputs
//--------------------------------------------------------------------------------------


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

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

sampler2D SamplerNoise = sampler_state
{
	Texture   = <texNoise>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	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;
};


//--------------------------------------------------------------------------------------
// Vertex Shader Input
//--------------------------------------------------------------------------------------

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;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------


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



#ifdef DOF
float penta(float2 coords) //pentagonal shape
{
	float scale = float(rings) - 1.5;
	float4  HS0 = float4( -20.0, -15.0, -15.0, -20.0);
	float4  HS1 = float4( -20.0, -15.0, -15.0, -20.0);
	float4  HS2 = float4( -20.0, -15.0, -15.0, -20.0);
	float4  HS3 = float4( -20.0, -15.0, -15.0, -20.0);
	float4  HS4 = float4( -20.0, -15.0, -15.0, -20.0);
	float4  HS5 = float4( -20.0, -15.0, -15.0, -20.0);
	
	float4  one = float4(8.0, 8.0, 8.0, 8.0);
	
	float4 P = float4(coords,float2(scale, scale)); 
	
	float4 dist = float4(4.0, 4.0, 4.0, 4.0);
	float inorout = 0.0;
	
	dist.x = dot( P, HS0 );
	dist.y = dot( P, HS1 );
	dist.z = dot( P, HS2 );
	dist.w = dot( P, HS3 );
	
	dist = smoothstep( -feather, feather, dist );
	
	inorout += dot( dist, one );
	
	dist.x = dot( P, HS4 );
	dist.y = HS5.w - abs( P.z );
	
	dist = smoothstep( -feather, feather, dist );
	inorout += dist.x;
	
	return saturate( inorout );
}


float linearize(float depth)
{
	return -zfar * znear / (depth * (zfar - znear) - zfar);
}

float2 rand(float2 coord) //generating noise/pattern texture for dithering
{
	float noiseX = ((frac(1.0-coord.x*(screenRes.x/2.0))*0.25)+(frac(coord.y*(screenRes.y/2.0))*0.75))*2.0-1.0;
	float noiseY = ((frac(1.0-coord.x*(screenRes.x/2.0))*0.75)+(frac(coord.y*(screenRes.y/2.0))*0.25))*2.0-1.0;
	
	return float2(noiseX,noiseY);
}

float4 colorDof(float2 coords,float blur) //processing the sample
{
	float4 colDF = float4(0,0,0,1);
	
	colDF.x = tex2D(InputSampler,coords + float2(0.0,1.0)*texel*fringe*blur).x;
	colDF.y = tex2D(InputSampler,coords + float2(-0.866,-0.5)*texel*fringe*blur).y;
	colDF.z = tex2D(InputSampler,coords + float2(0.866,-0.5)*texel*fringe*blur).z; //by Marty McFly
	
	float3 lumcoeff = float3(0.299,0.587,0.114);
	float lum = dot(colDF.xyz,lumcoeff);
	float thresh = max((lum-threshold)*gain, 0.0);
	float3 nullcol = float3(0,0,0);
	colDF.xyz +=lerp(nullcol,colDF.xyz,thresh*blur);
	return colDF;
}

float vignette(float2 coord)
{
	float dist = distance(coord, float2(0.5,0.5));
	dist = smoothstep(vignout+(fstop/MartyMcFly), vignin+(fstop/MartyMcFly), dist);
	return saturate(dist);
}
#endif

#ifdef NOISE

float Random(float2 co)
{
    return frac(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453);
}

float Grain(float3 tex)
{
	float PI = 3.1415926535897932384626433832795;
	float rofl = Random(tex.xy);
	float grain = sin(PI * tex.z * rofl * fGrainFreq) * fGrainScale * rofl;
	return grain;
}
#endif


float4 main(VS_OUTPUT_POST i, float2 vPos : VPOS) : COLOR
{
    float4 color = tex2D(InputSampler, i.txcoord);

#ifdef HSVULTIMATE
#ifdef ENABLE_HSV_CONVERSION
float3 hsvcolor = RGBtoHSV( color.xyz );

#ifdef ENABLE_POW_CURVE
#ifdef ENABLE_HUE_ADJUSTMENT
hsvcolor.x = EColorHueMod + ( EColorHueMult * pow( hsvcolor.x, EColorHuePow ) );
#endif
#ifdef ENABLE_SATURATION_ADJUSTMENT
hsvcolor.y = EColorSaturationMod + ( EColorSaturationMult * pow( hsvcolor.y, EColorSaturationPow ) );
#endif
#ifdef ENABLE_INTENSITY_ADJUSTMENT
hsvcolor.z = EColorIntensityMod + ( EColorIntensityMult * pow( hsvcolor.z, EColorIntensityPow ) );
#endif
#ifdef ENABLE_COLOR_EQUALIZER
hsvcolor.y = ColorEqualizerMod( hsvcolor.x ) + ( ColorEqualizerMult( hsvcolor.x ) * pow( hsvcolor.y, ColorEqualizerPow( hsvcolor.x ) ) );
#endif
#endif

#ifndef ENABLE_POW_CURVE
#ifdef ENABLE_HUE_ADJUSTMENT
hsvcolor.x = EColorHueMod + ( EColorHueMult * hsvcolor.x );
#endif
#ifdef ENABLE_SATURATION_ADJUSTMENT
hsvcolor.y = EColorSaturationMod + ( EColorSaturationMult * hsvcolor.y );
#endif
#ifdef ENABLE_INTENSITY_ADJUSTMENT
hsvcolor.z = EColorIntensityMod + ( EColorIntensityMult * hsvcolor.z );
#endif
#ifdef ENABLE_COLOR_EQUALIZER
hsvcolor.y = ColorEqualizerMod( hsvcolor.x ) + ( ColorEqualizerMult( hsvcolor.x ) * hsvcolor.y );
#endif
#endif

hsvcolor.y = max( hsvcolor.y, 0.0 );
hsvcolor.z = max( hsvcolor.z, 0.0 );
color.xyz = HSVtoRGB( hsvcolor );
#endif
#endif


#ifdef GAMMACORRECTION
color = pow(color, 1/GammaAmount);
#endif

#ifdef COLORCORRMAIN
color.r = (color.r)*(0.05 + 1.0f) + (color.g + color.b)*(-0.04); 
color.g = (color.g)*(0.06 + 1.0f) + (color.r + color.b)*(-0.04);
color.b = (color.b)*(0.055 + 1.0f) + (color.r + color.g)*(-0.04);

color.rgb = (color.rgb * (1.0 + color.rgb / 2))/(color.rgb + 4);
color = color*(1.0f + 0.2) - 0.12;
#endif


#ifdef COLORCORRMAIN
color = saturate(color);
color = 1 - (0.97 - 0.065)*(1.04 - color);
color = saturate(color);
color = color + (0.82f / 2.0 - 0.5) * (1.0 - color);
color = saturate(color);
color.rgb = pow(0.85f * color, 1.0 / 0.95f); 

float dbr = -color.r + 1.4f;
float dbg = -color.g + 1.4f;
float dbb = -color.b + 1.4f;

float pr = lerp(dbr, 0.55f, 0.66f);
float pg = lerp(dbg, 0.55f, 0.66f);
float pb = lerp(dbb, 0.55f, 0.66f);

color.r = pow((color.r * 0.94f - 0.0035f), pr);
color.g = pow((color.g * 0.94f - 0.002f), pg);
color.b = pow((color.b * 0.91f + 0.001f), pb);

color = color * 4.0;
#endif

#ifdef FILMICCURVE
float3 curr = Uncharted2Tonemap(color);
float3 whiteScale = Uncharted2Tonemap(FilmWeight);
color.xyz = curr*whiteScale;
color = pow(color, 0.55);
color = color*FilmicExposure;
#endif

#ifdef REINHARD_ICE21
float pixelLumi = dot(color.xyz, LUM_FACTOR.xyz);
float toneMapCalc = pixelLumi / (pixelLumi + 1);
color =  toneMapCalc * pow(color / pixelLumi, REINHARD_SAT);
color = color * TONEMAPEXP;
#endif

#ifdef CROSSPROCESS

		float4 image1 = color;
		float4 image2 = color;
		float gray = dot(float3(0.5,0.5,0.5), image1);

		image1 = lerp (gray, image1,CrossSaturation2);

		image1 = lerp (0.35, image1,CrossContrast);

		image1 +=CrossBrightness;
		image1 *=1.2;

		image2.r = image1.r * crossMatrix[0].x + crossMatrix[0].y;
		image2.g = image1.g * crossMatrix[1].x + crossMatrix[1].y;
		image2.b = image1.b * crossMatrix[2].x + crossMatrix[2].y;

		color = lerp(image1, image2, CrossAmount);

#endif

#ifdef CROSSICE
float Weight;
float4 colInput = color;
float4 colMood = 1.0f;
colMood.r = moodR;
colMood.g = moodG;
colMood.b = moodB;
float fLum = ( colInput.r + colInput.g + colInput.b ) / 3;
colMood = lerp(0, colMood, saturate(fLum * 2.0));
colMood = lerp(colMood, 1, saturate(fLum - 0.5) * 2.0);
float4 colOutput = lerp(colInput, colMood, saturate(fLum * fRatio));
color.rgb=max(0, colOutput);
#endif

#ifdef HDR
color.xyz = max(0, color.xyz - Defog * FogColor.xyz);
color.xyz *= pow(2.0f, Exposure);
color.xyz = pow(color.xyz, Gamma);
float3 trollface = color.xyz * float3(1.05f, 0.97f, 1.27f);
color.xyz = lerp(color.xyz, trollface, BlueShift);
#endif

#ifdef FILMICPASS
color = FilmPass(color);
#endif

#ifdef VIBRANCEPASS
float3 lumCoeff = float3(0.2126, 0.7152, 0.0722);
float vibranceluma = dot(lumCoeff, color.xyz);

float max_color = max(color.x, max(color.y,color.z)); //Find the strongest color
float min_color = min(color.x, max(color.y,color.z)); //Find the weakest color
float color_saturation = max_color - min_color;

color.xyz = lerp(vibranceluma, color.xyz, (1.0 + (Vibrance * (1.0 - (sign(Vibrance) * color_saturation)))));
#endif

#ifdef COLORWASHOUT
float	colorGray=dot(color.xyz, 0.333);
float	colorGray2=(colorGray*(6.2*colorGray+0.5))/(colorGray*(6.2*colorGray+1.7)+0.06);
float	colorGray3=pow(colorGray, 0.36);
float	colorWashout=saturate(colorGray2 - ColorWashoutThreshold)/(1.00 - ColorWashoutThreshold);
colorWashout=saturate(pow(colorWashout, ColorWashoutPow) * ColorWashoutAmount);

float3	middleColor=(color.xyz+0.0001)/(colorGray+0.000001);
middleColor.xyz=lerp(pow(middleColor.xyz, ColorSaturation), middleColor.xyz, ColorSaturation);
middleColor.xyz=lerp(middleColor.xyz, 1.00, saturate(colorWashout*ColorWashoutAmount));
float tonemapMix=(0.5 - ColorDullnessAmount)*0.60; //temp
	colorGray=lerp(colorGray2, colorGray3, tonemapMix);
	color.xyz=middleColor.xyz*colorGray;
color.xyz *= ExpAdjustment;
#endif


#ifdef AHDR

float4 clr1 =  1.2*color;
float4 clr2 =  3*tex2D(InputSampler, i.txcoord.xy);
float avrg1 = (clr1.r + clr1.g + clr1.b)/3;
float avrg2 = (clr2.r + clr2.g + clr2.b)/3;
float diff1 = abs(avrg1 - HDRthreshold);
diff1 *= (1/(max(1-HDRthreshold, HDRthreshold)));
float diff2 = abs(avrg2 - HDRthreshold);
diff2 *= (1/(max(1-HDRthreshold, HDRthreshold)));
color = (clr1*(1-diff1+diff2) + clr2*(1-diff2+diff1))/2;
#endif



#ifdef BORISVIGNETTE
	float2	uv=(i.txcoord-0.5)*EVignetteRadius;
	float	vignetteold=saturate(dot(uv.xy, uv.xy));
	vignetteold=pow(vignetteold, EVignetteCurve);
	#ifdef VIGNCOLORING
	float3	EVignetteColor=float3(VIGNREDAMOUNT, VIGNGREENAMOUNT, VIGNBLUEAMOUNT);
	#else
	float3	EVignetteColor=float3(0.0, 0.0, 0.0);
	#endif
	color.xyz=lerp(color.xyz, EVignetteColor, vignetteold*EVignetteAmount);
#endif

#ifdef HD6_VIGNETTE

	float rovigpwr = CircularPower; //for a circular vignette
	float2 sqvigpwr = float2( SquareTop, SquareBottom ); // for the top and bottom of the screen
	float vsatstrength = ColorDistortion; // color distortion
	float vignettepow = ContrastSharpen; // increases the contrast and sharpness
	float vstrengthatnight = VignetteBorder;
 
 	float2 inTex = i.txcoord;
 	float vhnd = 0.5;
 	float4 voriginal = color;
 	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.25); // contrast
 	rvigtex.xyz = lerp(float3(1,1,1),rvigtex.xyz,rovigpwr); // strength of the circular vinetty
 
//darken the top and bottom
 	float4 vigtex = vcolor;
 	vcolor.xyz = float3(1,1,1);

#ifdef LEFTANDRIGHT
 	float3 topv = min((inTex.x+0.5)*2,1.5) * 2; // top
 	float3 botv = min(((0-inTex.x)+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);
#endif
#ifdef TOPANDBOTTOM
        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);
#endif
#ifdef CORNERDARKEN
	float3 rightv = min((inTex.x+0.5)*2,1.5) * 2;
 	float3 leftv = min(((0-inTex.x)+0.5)*2,1.5) * 2; 
        float3 topv = min((inTex.y+0.5)*2,1.5) * 2; 
 	float3 botv = min(((0-inTex.y)+0.5)*2,1.5) * 2; 
 	rightv= lerp(float3(1,1,1), rightv, sqvigpwr.y);
 	leftv= lerp(float3(1,1,1), leftv, sqvigpwr.x);
        topv= lerp(float3(1,1,1), topv, sqvigpwr.x);
 	botv= lerp(float3(1,1,1), botv, sqvigpwr.y);
 	vigtex.xyz = (topv)*(botv)*(rightv)*(leftv);
#endif
 	
 	
 	//vigtex.xyz = lerp(float3(1,1,1),vigtex.xyz,sqvigpwr); // strength of the top and bottom
 
 // mix the two types of vignettes
 	vigtex.xyz*=rvigtex.xyz;
	vigtex.xyz = lerp(vigtex.xyz,float3(1,1,1),(vhnd-vstrengthatnight*vhnd)); //for a dark screen
 	vigtex.xyz = min(vigtex.xyz,1);
 	vigtex.xyz = max(vigtex.xyz,0);
 	float3 vtintensity = dot(voriginal.xyz, float3(0.2125, 0.7154, 0.0721));
 	color.xyz = lerp(vtintensity, voriginal.xyz, ((((1-(vigtex.xyz*2))+2)-1)*vsatstrength)+1 );
  	color.xyz *= (vigtex.xyz);
#endif

#define const_1 (DARK_LEVEL/255.0)
#define const_2 (255.0/(255.0-BRIGHT_LEVEL))


#ifdef TVLEVELS
color.xyz = (color.xyz  - const_1) * const_2;
#endif


 
#ifdef USE_DITHER2
float dither_size = 2.0;  //move to settings?
float dither_bit  = 8.0;  //move to settings?
float grid_position = frac(dot(i.txcoord,(screen_size / dither_size)) + (0.5 / dither_size)); 
float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0));
color.xyz += lerp(2.0 * dither_shift,(-2.0 * dither_shift),grid_position); 
#endif

#ifdef USE_DITHER
float dither_size = 2.0;  //move to settings?
float dither_bit  = 8.0;  //move to settings?
float grid_position = frac(dot(i.txcoord,(screen_size / dither_size)) + (0.5 / dither_size)); 
float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0));
float3 dither_shift_RGB = float3(dither_shift, dither_shift, dither_shift);
color.xyz += lerp(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); 
#endif

#define BUFFER_RCP_WIDTH (1.0 / screenwidth)
#define BUFFER_RCP_HEIGHT (1.0 / screenheight)

#define px BUFFER_RCP_WIDTH
#define py BUFFER_RCP_HEIGHT
#define pixel float2(px,py)

#ifdef BORDER
float2 distancefromcenter = abs(i.txcoord.xy - 0.5);
bool2 screen_border = step(0.5 - pixel,distancefromcenter);
color.xyz = (!dot(screen_border, 1.0)) ? color.xyz : 0.0;
#endif


#ifdef FINALADJUSTER
	float3 	FinalSaturation 	= SaturationDay;
	float 	FinalBrightness 	= BrightnessDay;
	float 	FinalContrast 		= clamp(ContrastDay, -1.0, 1.0);				
	//convert to linear colorspace
	color.r 		= 1.0/(1.0+color.r);
	color.g 		= 1.0/(1.0+color.g);
	color.b 		= 1.0/(1.0+color.b);
	//reverse gamma correction
	color.rgb 		= pow(color.rgb, 2.2);
	//saturate
	color.rgb 		= lerp(dot(color.rgb,(float3(0.299, 0.587, 0.114))), color.rgb, FinalSaturation);
	//brighten
	color.rgb		/= FinalBrightness;
	//adjust contrast
	float factorZ 	= (1.0156862745098039215686274509804 * (FinalContrast + 1.0)) / (1.0156862745098039215686274509804 - FinalContrast);
	float newRed   	= saturate(factorZ * (color.r   - 0.5078431372549019607843137254902) + 0.5078431372549019607843137254902);
	float newGreen 	= saturate(factorZ * (color.g - 0.5078431372549019607843137254902) + 0.5078431372549019607843137254902);
	float newBlue  	= saturate(factorZ * (color.b  - 0.5078431372549019607843137254902) + 0.5078431372549019607843137254902);
	color.rgb 		= float3(newRed, newGreen, newBlue);
	//restore gamma correction
	color.rgb 		= pow(color.rgb, 0.45454545454545454545454545454545);
	//restore dynamic range
	color.r 		= (1.0/color.r)-1.0;
	color.g 		= (1.0/color.g)-1.0;
	color.b 		= (1.0/color.b)-1.0;
#endif


color.w = 1.0;

return color;
}


#ifdef LIGHTSCATTERING
float4 PS_ProcessSunShafts(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR 
{	
	float2 texCoord = IN.txcoord;
	// Calculate vector from pixel to light source in screen space.  
    half2 deltaTexCoord = (texCoord - ScreenLightPos.xy);  
    // Divide by number of samples and scale by control factor.  
    deltaTexCoord *= 1.0f / NUM_SAMPLES * Density;  
    // Store initial sample.  
    half3 color = max(float4(0,0,0,0),tex2D(InputSampler, texCoord)-SStresh);  
	// Set up illumination decay factor.  
	half illuminationDecay = 1.0f;  
	// Evaluate summation from Equation 3 NUM_SAMPLES iterations.  
	for (int i = 0; i < NUM_SAMPLES; i++)  
	{  
		// Step sample location along ray.  
		texCoord -= deltaTexCoord;  
		// Retrieve sample at new location.  
	    half3 sample = max(float4(0,0,0,0),tex2D(InputSampler, texCoord)-SStresh);  
		// Apply sample attenuation scale/decay factors.  

#ifdef TINTCOLOR
                sample.r *= REDAMOUNT;
		sample.g *= GREENAMOUNT;
		sample.b *= BLUEAMOUNT;
#endif

#ifdef COLORING
sample = dot(sample, float3(0.3, 0.59, 0.11));
                sample.r *= REDAMOUNT;
		sample.g *= GREENAMOUNT;
		sample.b *= BLUEAMOUNT;
#endif

		sample *= illuminationDecay * Weight;  
		// Accumulate combined color.  
		color += sample;  
		// Update exponential decay factor.  
		illuminationDecay *= Decay;  
	}
	// Output final color with a further scale control factor.  
	return float4( saturate(color*SunExposure) , color.x);
}
#endif

/* I need space so...
float4 CCShadow (VS_OUTPUT_POST i, float2 vPos : VPOS) : COLOR
{

float4 threshhold1 = {thresh1, 1/(1-thresh1), 5.0, 5.0}; 
float4 threshhold2 = {thresh2, 1/(1-thresh2), 5.0, 5.0};
	float4 col = tex2D(InputSampler, i.txcoord);
	//float4 color = tex2Dlod(InputSampler, float4(i.txcoord,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(i.txcoord.y - 4.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.05;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y - 3.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.09;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y - 2.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.12;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y - fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.15;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y, i.txcoord.y,0,fBlurDownsampling)) * 0.16;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.15;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + 2.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.12;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + 3.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.09;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + 4.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.05;
#endif
#ifdef FLAREMODE_2
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 100.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.01;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 99.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.01;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#endif



#ifdef FORCECOLOR
sum = dot(sum, float3(0.3, 0.59, 0.11));
#endif

#ifdef ANAMORPHICTINTCOLOR
sum.r = sum.r * ANAMORPHICREDAMOUNT;
sum.g = sum.g * ANAMORPHICGREENAMOUNT;
sum.b = sum.b * ANAMORPHICBLUEAMOUNT;
#endif	


	// 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;
	
}
*/
float4 CBloom (VS_OUTPUT_POST i, float2 vPos : VPOS) : COLOR
{

float4 Acol = tex2D(InputSampler, i.txcoord);
	
	
    float4 sum = float4(0,0,0,0);
 #ifdef COLORMODBLOOM
sum += tex2Dlod(InputSampler, float4(i.txcoord.x, i.txcoord.y,0,BloomDownsample)) * 0.19741;

    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.94201624*Bloomradius, i.txcoord.y- 0.39906216*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.94558609*Bloomradius, i.txcoord.y- 0.76890725*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.09418410*Bloomradius, i.txcoord.y- 0.92938870*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.34495938*Bloomradius, i.txcoord.y+ 0.29387760*Bloomradius,0,BloomDownsample)) * 0.19741; 
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.91588581*Bloomradius, i.txcoord.y+ 0.45771432*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81544232*Bloomradius, i.txcoord.y- 0.87912464*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.38277543*Bloomradius, i.txcoord.y+ 0.27676845*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.97484398*Bloomradius, i.txcoord.y+ 0.75648379*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.44323325*Bloomradius, i.txcoord.y- 0.97511554*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.53742981*Bloomradius, i.txcoord.y- 0.47373420*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.26496911*Bloomradius, i.txcoord.y- 0.41893023*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.79197514*Bloomradius, i.txcoord.y+ 0.19090188*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.24188840*Bloomradius, i.txcoord.y+ 0.99706507*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81409955*Bloomradius, i.txcoord.y+ 0.91437590*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.19984126*Bloomradius, i.txcoord.y+ 0.78641367*Bloomradius,0,BloomDownsample)) * 0.19741;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.14383161*Bloomradius, i.txcoord.y- 0.14100790*Bloomradius,0,BloomDownsample)) * 0.19741;


    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.94201624*Bloomradius*1.5, i.txcoord.y- 0.39906216*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.94558609*Bloomradius*1.5, i.txcoord.y- 0.76890725*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.09418410*Bloomradius*1.5, i.txcoord.y- 0.92938870*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.34495938*Bloomradius*1.5, i.txcoord.y+ 0.29387760*Bloomradius*1.5,0,BloomDownsample)) * 0.17467; 
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.91588581*Bloomradius*1.5, i.txcoord.y+ 0.45771432*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81544232*Bloomradius*1.5, i.txcoord.y- 0.87912464*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.38277543*Bloomradius*1.5, i.txcoord.y+ 0.27676845*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.97484398*Bloomradius*1.5, i.txcoord.y+ 0.75648379*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.44323325*Bloomradius*1.5, i.txcoord.y- 0.97511554*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.53742981*Bloomradius*1.5, i.txcoord.y- 0.47373420*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.26496911*Bloomradius*1.5, i.txcoord.y- 0.41893023*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.79197514*Bloomradius*1.5, i.txcoord.y+ 0.19090188*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.24188840*Bloomradius*1.5, i.txcoord.y+ 0.99706507*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81409955*Bloomradius*1.5, i.txcoord.y+ 0.91437590*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.19984126*Bloomradius*1.5, i.txcoord.y+ 0.78641367*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.14383161*Bloomradius*1.5, i.txcoord.y- 0.14100790*Bloomradius*1.5,0,BloomDownsample)) * 0.17467;


    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.94201624*Bloomradius*2.0, i.txcoord.y- 0.39906216*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.94558609*Bloomradius*2.0, i.txcoord.y- 0.76890725*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.09418410*Bloomradius*2.0, i.txcoord.y- 0.92938870*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.34495938*Bloomradius*2.0, i.txcoord.y+ 0.29387760*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.91588581*Bloomradius*2.0, i.txcoord.y+ 0.45771432*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81544232*Bloomradius*2.0, i.txcoord.y- 0.87912464*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.38277543*Bloomradius*2.0, i.txcoord.y+ 0.27676845*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.97484398*Bloomradius*2.0, i.txcoord.y+ 0.75648379*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.44323325*Bloomradius*2.0, i.txcoord.y- 0.97511554*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.53742981*Bloomradius*2.0, i.txcoord.y- 0.47373420*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.26496911*Bloomradius*2.0, i.txcoord.y- 0.41893023*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.79197514*Bloomradius*2.0, i.txcoord.y+ 0.19090188*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.24188840*Bloomradius*2.0, i.txcoord.y+ 0.99706507*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81409955*Bloomradius*2.0, i.txcoord.y+ 0.91437590*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.19984126*Bloomradius*2.0, i.txcoord.y+ 0.78641367*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.14383161*Bloomradius*2.0, i.txcoord.y- 0.14100790*Bloomradius*2.0,0,BloomDownsample)) * 0.12098;



    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.94201624*Bloomradius*2.5, i.txcoord.y- 0.39906216*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.94558609*Bloomradius*2.5, i.txcoord.y- 0.76890725*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.09418410*Bloomradius*2.5, i.txcoord.y- 0.92938870*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.34495938*Bloomradius*2.5, i.txcoord.y+ 0.29387760*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.91588581*Bloomradius*2.5, i.txcoord.y+ 0.45771432*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81544232*Bloomradius*2.5, i.txcoord.y- 0.87912464*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.38277543*Bloomradius*2.5, i.txcoord.y+ 0.27676845*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.97484398*Bloomradius*2.5, i.txcoord.y+ 0.75648379*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.44323325*Bloomradius*2.5, i.txcoord.y- 0.97511554*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.53742981*Bloomradius*2.5, i.txcoord.y- 0.47373420*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.26496911*Bloomradius*2.5, i.txcoord.y- 0.41893023*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.79197514*Bloomradius*2.5, i.txcoord.y+ 0.19090188*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.24188840*Bloomradius*2.5, i.txcoord.y+ 0.99706507*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81409955*Bloomradius*2.5, i.txcoord.y+ 0.91437590*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.19984126*Bloomradius*2.5, i.txcoord.y+ 0.78641367*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.14383161*Bloomradius*2.5, i.txcoord.y- 0.14100790*Bloomradius*2.5,0,BloomDownsample)) * 0.06559;


    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.94201624*Bloomradius*3.0, i.txcoord.y- 0.39906216*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.94558609*Bloomradius*3.0, i.txcoord.y- 0.76890725*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.09418410*Bloomradius*3.0, i.txcoord.y- 0.92938870*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.34495938*Bloomradius*3.0, i.txcoord.y+ 0.29387760*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.91588581*Bloomradius*3.0, i.txcoord.y+ 0.45771432*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81544232*Bloomradius*3.0, i.txcoord.y- 0.87912464*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.38277543*Bloomradius*3.0, i.txcoord.y+ 0.27676845*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.97484398*Bloomradius*3.0, i.txcoord.y+ 0.75648379*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.44323325*Bloomradius*3.0, i.txcoord.y- 0.97511554*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.53742981*Bloomradius*3.0, i.txcoord.y- 0.47373420*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.26496911*Bloomradius*3.0, i.txcoord.y- 0.41893023*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.79197514*Bloomradius*3.0, i.txcoord.y+ 0.19090188*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.24188840*Bloomradius*3.0, i.txcoord.y+ 0.99706507*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81409955*Bloomradius*3.0, i.txcoord.y+ 0.91437590*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.19984126*Bloomradius*3.0, i.txcoord.y+ 0.78641367*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.14383161*Bloomradius*3.0, i.txcoord.y- 0.14100790*Bloomradius*3.0,0,BloomDownsample)) * 0.02783;


    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.94201624*Bloomradius*3.5, i.txcoord.y- 0.39906216*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.94558609*Bloomradius*3.5, i.txcoord.y- 0.76890725*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.09418410*Bloomradius*3.5, i.txcoord.y- 0.92938870*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.34495938*Bloomradius*3.5, i.txcoord.y+ 0.29387760*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.91588581*Bloomradius*3.5, i.txcoord.y+ 0.45771432*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81544232*Bloomradius*3.5, i.txcoord.y- 0.87912464*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.38277543*Bloomradius*3.5, i.txcoord.y+ 0.27676845*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.97484398*Bloomradius*3.5, i.txcoord.y+ 0.75648379*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.44323325*Bloomradius*3.5, i.txcoord.y- 0.97511554*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.53742981*Bloomradius*3.5, i.txcoord.y- 0.47373420*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.26496911*Bloomradius*3.5, i.txcoord.y- 0.41893023*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.79197514*Bloomradius*3.5, i.txcoord.y+ 0.19090188*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.24188840*Bloomradius*3.5, i.txcoord.y+ 0.99706507*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 0.81409955*Bloomradius*3.5, i.txcoord.y+ 0.91437590*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.19984126*Bloomradius*3.5, i.txcoord.y+ 0.78641367*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 0.14383161*Bloomradius*3.5, i.txcoord.y- 0.14100790*Bloomradius*3.5,0,BloomDownsample)) * 0.01222;

#endif



sum *= BLOOMPOW;	
	

sum.xyz=lerp(pow(sum.xyz, BloomSat), sum.xyz, BloomSat);


sum=sum-float4(BLOOMTHRESH,BLOOMTHRESH,BLOOMTHRESH,0);
sum.a=1.0;

if(sum.x < 0)	
	{
sum.x=0;
}
if(sum.y < 0)	
	{
sum.y=0;
}
if(sum.z < 0)	
	{
sum.z=0;
}


float4 finalcolor = sum+Acol;
return finalcolor;

}




#ifdef DOF
float4 PS_ProcessDoFBokeh(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR 
{
	float depth = linearize(tex2D(SamplerDepth,IN.txcoord.xy).x);
	
	float fDepth = focalDepth;
	
	if (autofocus)
	{
		fDepth = linearize(tex2D(SamplerDepth,focus).x);
	}
	
	float blur = 0.0;
	if (manualdof)
	{    
		float a = depth-fDepth; //focal plane
		float b = (a-fdofstart)/fdofdist; //far DoF
		float c = (-a-ndofstart)/ndofdist; //near Dof
		blur = (a>0.0)?b:c;
	}
	else
	{
		float f = focalLength; //focal length in mm
		float d = fDepth*1000.0; //focal plane in mm
		float o = depth*1000.0; //depth in mm
		
		float a = (o*f)/(o-f); 
		float b = (d*f)/(d-f); 
		float c = (d-f)/(d*fstop*CoC); 
		
		blur = abs(a-b)*c;
	}
	blur = saturate(blur);
	float2 noise = rand(IN.txcoord.xy)*namount*blur;
	
	float w = (1.0/screenRes.x)*blur*maxblur+noise.x;
	float h = (1.0/screenRes.y)*blur*maxblur+noise.y;
	
	float4 col = float4(0,0,0,1);
	
	if(blur < 0.05) //some optimization thingy
	{
		col = tex2D(InputSampler, IN.txcoord.xy);
	}
	else
	{
	col = tex2D(InputSampler, IN.txcoord.xy);
	float s = 1.0;
	int ringsamples;
	for (int i = 1; i <= rings; i += 1)
	{
		ringsamples = i * samples;
		for (int j = 0 ; j < ringsamples ; j += 1)
		{
			float step = PI*2.0 / ringsamples;
			float pw = cos(j*step)*i;
			float ph = sin(j*step)*i;
			float p = 1.0;
			if (pentagon)
			{ 
				p = penta(float2(pw,ph));
			}
			col.xyz += colorDof(IN.txcoord.xy + float2(pw*w,ph*h),blur).xyz;  
			s += 1.0*lerp(1.0,i/rings,bias)*p;
		}
	}
	col = col/s; //divide by sample count
	}
	
	if (vignetting)
	{
		col *= vignette(IN.txcoord.xy);
	}
#ifdef BORDER_DOF
	float2 distancefromcenter = abs(IN.txcoord.xy - 0.5);
	bool2 screen_border = step(0.5 - pixel,distancefromcenter);

col.xyz = (!dot(screen_border, 1.0)) ? col.xyz : 0.0;
#endif
	return col;
}
#endif



#ifdef CHROMATICABBERATION
float4 PS_PostProcessChroma(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{

float4 coord=0.0;

coord.xy=IN.txcoord.xy;
coord.w=0.0;

float3 eta = float3(1.0+ChromaticAmount*0.9,1.0+ChromaticAmount*0.6,1.0+ChromaticAmount*0.3);
float2 center;
center.x = coord.x-0.5;
center.y = coord.y-0.5;
float LensZoom = 1.0/LensSize;

float r2 = (IN.txcoord.x-0.5) * (IN.txcoord.x-0.5) + (IN.txcoord.y-0.5) * (IN.txcoord.y-0.5);     
float f = 0;

if( LensDistortionCubic == 0.0){
	f = 1 + r2 * LensDistortion;
}else{
                f = 1 + r2 * (LensDistortion + LensDistortionCubic * sqrt(r2));
};

float x = f*LensZoom*(coord.x-0.5)+0.5;
float y = f*LensZoom*(coord.y-0.5)+0.5;
float2 rCoords = (f*eta.r)*LensZoom*(center.xy*0.5)+0.5;
float2 gCoords = (f*eta.g)*LensZoom*(center.xy*0.5)+0.5;
float2 bCoords = (f*eta.b)*LensZoom*(center.xy*0.5)+0.5;

//float3 inputDistord = tex2D(InputSampler,float2(x,y));
float4 inputDistord = float4(tex2D(InputSampler,rCoords).r , tex2D(InputSampler,gCoords).g ,tex2D(InputSampler,bCoords).b, tex2D(InputSampler,float2(x,y)).a);
return float4(inputDistord.r,inputDistord.g,inputDistord.b,1);
}
#endif



float ScreenSizeFXAA = 1920;; //width of the display resolution (1920 f.e.)
float ScreenScaleYFXAA = 1.333; //screen proportions (1.333 for 1920/1080)

float4 FxaaPixelShader(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{  

#define FxaaTexTop(t, p) tex2Dlod(t, float4(p, 0.0, 0.0))
#define FxaaTexOff(t, p, o, r) tex2Dlod(t, float4(p + (o * r), 0, 0))

float2 pos = IN.txcoord.xy;

float2 rcpFrame = float2(1/ScreenSizeFXAA, ScreenScaleYFXAA/ScreenSizeFXAA);
float4 rcpFrameOpt = float4(2/ScreenSizeFXAA, 2*ScreenScaleYFXAA/ScreenSizeFXAA, 0.5/ScreenSizeFXAA, 0.5*ScreenScaleYFXAA/ScreenSizeFXAA);

float lumaN = dot(FxaaTexOff(InputSampler, pos.xy, float2(0, -1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaW = dot(FxaaTexOff(InputSampler, pos.xy, float2(-1, 0), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));


float4 rgbyM;
rgbyM.xyz = FxaaTexTop(InputSampler, pos.xy).xyz;
rgbyM.w = dot(rgbyM.xyz, float3(0.299, 0.587, 0.114));
float lumaE = dot(FxaaTexOff(InputSampler, pos.xy, float2( 1, 0), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaS = dot(FxaaTexOff(InputSampler, pos.xy, float2( 0, 1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaM = rgbyM.w;


float rangeMin = min(lumaM, min(min(lumaN, lumaW), min(lumaS, lumaE)));
float rangeMax = max(lumaM, max(max(lumaN, lumaW), max(lumaS, lumaE)));
float range = rangeMax - rangeMin;

if(range < max(FXAA_QUALITY__EDGE_THRESHOLD_MIN, rangeMax * FXAA_QUALITY__EDGE_THRESHOLD)) return rgbyM;


float lumaNW = dot(FxaaTexOff(InputSampler, pos.xy, float2(-1,-1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaNE = dot(FxaaTexOff(InputSampler, pos.xy, float2( 1,-1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaSW = dot(FxaaTexOff(InputSampler, pos.xy, float2(-1, 1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaSE = dot(FxaaTexOff(InputSampler, pos.xy, float2( 1, 1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));



float lumaL = (lumaN + lumaW + lumaE + lumaS) * 0.25;
float rangeL = abs(lumaL - lumaM);
float blendL = saturate((rangeL / range) - FXAA_QUALITY__SUBPIX_TRIM) * FXAA_QUALITY__SUBPIX_TRIM_SCALE;
blendL = min(FXAA_QUALITY__SUBPIX_CAP, blendL);

float edgeVert = abs(lumaNW + (-2.0 * lumaN) + lumaNE) + 2.0 * abs(lumaW  + (-2.0 * lumaM) + lumaE ) + abs(lumaSW + (-2.0 * lumaS) + lumaSE);
   float edgeHorz = abs(lumaNW + (-2.0 * lumaW) + lumaSW) + 2.0 * abs(lumaN  + (-2.0 * lumaM) + lumaS ) + abs(lumaNE + (-2.0 * lumaE) + lumaSE);
   bool horzSpan = edgeHorz >= edgeVert;

float lengthSign = horzSpan ? -rcpFrame.y : -rcpFrame.x;
if(!horzSpan) lumaN = lumaW;
if(!horzSpan) lumaS = lumaE;
float gradientN = abs(lumaN - lumaM);
float gradientS = abs(lumaS - lumaM);
lumaN = (lumaN + lumaM) * 0.5;
lumaS = (lumaS + lumaM) * 0.5;

bool pairN = gradientN >= gradientS;
if(!pairN) lumaN = lumaS;
if(!pairN) gradientN = gradientS;
if(!pairN) lengthSign *= -1.0;
float2 posN;
posN.x = pos.x + (horzSpan ? 0.0 : lengthSign * 0.5);
posN.y = pos.y + (horzSpan ? lengthSign * 0.5 : 0.0);


gradientN *= FXAA_SEARCH_THRESHOLD;

float2 posP = posN;
float2 offNP = horzSpan ?
float2(rcpFrame.x, 0.0) :
float2(0.0f, rcpFrame.y);
float lumaEndN;
float lumaEndP;
bool doneN = false;
bool doneP = false;
posN += offNP * (-1.5);
posP += offNP * ( 1.5);
for(int i = 0; i < FXAA_SEARCH_STEPS; i++)
{
lumaEndN = dot(FxaaTexTop(InputSampler, posN.xy).xyz, float3(0.299, 0.587, 0.114));
lumaEndP = dot(FxaaTexTop(InputSampler, posP.xy).xyz, float3(0.299, 0.587, 0.114));
bool doneN2 = abs(lumaEndN - lumaN) >= gradientN;
bool doneP2 = abs(lumaEndP - lumaN) >= gradientN;
if(doneN2 && !doneN) posN += offNP;
if(doneP2 && !doneP) posP -= offNP;
if(doneN2 && doneP2) break;
doneN = doneN2;
doneP = doneP2;
if(!doneN) posN -= offNP * 2.0;
if(!doneP) posP += offNP * 2.0;
}

float dstN = horzSpan ? pos.x - posN.x : pos.y - posN.y;
float dstP = horzSpan ? posP.x - pos.x : posP.y - pos.y;

bool directionN = dstN < dstP;
lumaEndN = directionN ? lumaEndN : lumaEndP;

if(((lumaM - lumaN) < 0.0) == ((lumaEndN - lumaN) < 0.0))
lengthSign = 0.0;

float spanLength = (dstP + dstN);
dstN = directionN ? dstN : dstP;
float subPixelOffset = 0.5 + (dstN * (-1.0/spanLength));
subPixelOffset += blendL * (1.0/8.0);
subPixelOffset *= lengthSign;
float3 rgbF = FxaaTexTop(InputSampler, float2(pos.x + (horzSpan ? 0.0 : subPixelOffset), pos.y + (horzSpan ? subPixelOffset : 0.0))).xyz;

  #if (FXAA_LINEAR == 1)
lumaL *= lumaL;
  #endif
  float lumaF = dot(rgbF, float3(0.299, 0.587, 0.114)) + (1.0/(65536.0*256.0));
  float lumaB = lerp(lumaF, lumaL, blendL);
  float scale = min(4.0, lumaB/lumaF);
  rgbF *= scale;

  return float4(rgbF, lumaM);
}

#ifdef NOISE
float4 PS_ProcessPass_ImageGrain(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float4 res;
	float2 coord = IN.txcoord.xy;
	res.xyz = tex2D(InputSampler, coord.xy).xyz;
	res.xyz += tex2D(SamplerNoise, coord.xy * 1024).xyz * Grain(float3(coord.xy, SEED));
	res.a = 1.0;
	return res;
}
#endif
#ifdef MATSO_FLARE
float3 BrightPass(float2 tex)
{
    float3 couleur = tex2D(InputSampler, tex).rgb;
    float3 bC = max(couleur - float3(fFlareLuminance, fFlareLuminance, fFlareLuminance), 0.0);
    float bright = dot(bC, 1.0);
    bright = smoothstep(0.0f, 0.5, bright);
    return lerp(0.0, couleur, bright);
}


float3 AnamorphicSample(int axis, float2 tex, float blur)
{
	tex = 2.0 * tex - 1.0;
	if (!axis) tex.x /= -blur;
	else tex.y /= -blur;
	tex = 0.5 * tex + 0.5;
	return BrightPass(tex);
}


float4 PS_ProcessPass_Anamorphic(VS_OUTPUT_POST IN, float2 vPos : VPOS, uniform int axis) : COLOR
{
	float4 res;
	float2 coord = IN.txcoord.xy;
	

#ifdef fFlareHorizontal
	#define fFlareAxis	0
#endif
#ifdef fFlareVertical
	#define fFlareAxis	90
#endif
        
	float3 anamFlare = AnamorphicSample(axis, coord.xy, fFlareBlur) * fFlareTint;
#ifdef fFlareHorizontal
	anamFlare += AnamorphicSample(axis, coord.xy+float2(0, 4)*FlareRadius, fFlareBlur) * fFlareTint* 0.30;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(0, 3)*FlareRadius, fFlareBlur) * fFlareTint* 0.54;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(0, 2)*FlareRadius, fFlareBlur) * fFlareTint* 0.72;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(0, 1)*FlareRadius, fFlareBlur) * fFlareTint* 0.90;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(0, -1)*FlareRadius, fFlareBlur) * fFlareTint* 0.90;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(0, -2)*FlareRadius, fFlareBlur) * fFlareTint* 0.72;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(0, -3)*FlareRadius, fFlareBlur) * fFlareTint* 0.54;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(0, -4)*FlareRadius, fFlareBlur) * fFlareTint* 0.30;
#endif
#ifdef fFlareVertical
        anamFlare += AnamorphicSample(axis, coord.xy+float2(4, 0)*FlareRadius, fFlareBlur) * fFlareTint* 0.30;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(3, 0)*FlareRadius, fFlareBlur) * fFlareTint* 0.54;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(2, 0)*FlareRadius, fFlareBlur) * fFlareTint* 0.72;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(1, 0)*FlareRadius, fFlareBlur) * fFlareTint* 0.90;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(1, 0)*FlareRadius, fFlareBlur) * fFlareTint* 0.90;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(2, 0)*FlareRadius, fFlareBlur) * fFlareTint* 0.72;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(3, 0)*FlareRadius, fFlareBlur) * fFlareTint* 0.54;
	anamFlare += AnamorphicSample(axis, coord.xy+float2(4, 0)*FlareRadius, fFlareBlur) * fFlareTint* 0.30;
#endif

	res.rgb = anamFlare * fFlareIntensity;
	res.a = 1.0;
	return res;
}
#endif




#ifdef SKYRIMTONEMAPV8
float4 Skyrimtonemap(VS_OUTPUT_POST i, float2 vPos : VPOS) : COLOR
{
    float4 Skyrimcolor = tex2D(InputSampler, i.txcoord);
	 

float4	Adaptation=float4( 0.0, 0.0, 0.0, 1 );
	//float	grayadaptation=max(max(Adaptation.x, Adaptation.y), Adaptation.z);
	float	grayadaptation = Luminance(Skyrimcolor);




#if (POSTPROCESS==1)

	grayadaptation = max(grayadaptation, 0.0);
	grayadaptation = min(grayadaptation, 50.0);
	Skyrimcolor.xyz = Skyrimcolor.xyz / (grayadaptation * EAdaptationMaxV1 + EAdaptationMinV1);
		
	float cgray = dot(Skyrimcolor.xyz, float3(0.27, 0.67, 0.06));
	cgray = pow(cgray, EContrastV1);
	float3 poweredcolor = pow(Skyrimcolor.xyz, EColorSaturationV1);
	float newgray = dot(poweredcolor.xyz, float3(0.27, 0.67, 0.06));
	Skyrimcolor.xyz = poweredcolor.xyz * cgray / (newgray + 0.0001);

	float3	luma = Skyrimcolor.xyz;
	float	lumamax = 300.0;
	Skyrimcolor.xyz = (Skyrimcolor.xyz * (1.0 + Skyrimcolor.xyz / lumamax)) / (Skyrimcolor.xyz + EToneMappingCurveV1);	
#endif


#if (POSTPROCESS==2)

	grayadaptation = max(grayadaptation, 0.0);
	grayadaptation = min(grayadaptation, 50.0);
	Skyrimcolor.xyz = Skyrimcolor.xyz / (grayadaptation * EAdaptationMaxV2 + EAdaptationMinV2);
	float3 xncol = normalize(Skyrimcolor.xyz);
	float3 scl = Skyrimcolor.xyz / xncol.xyz;
	scl = pow(scl, EIntensityContrastV2);
	xncol.xyz = pow(xncol.xyz, EColorSaturationV2);
	Skyrimcolor.xyz = scl*xncol.xyz;
	float	lumamax = EToneMappingOversaturationV2;
	Skyrimcolor.xyz = (Skyrimcolor.xyz * (1.0 + Skyrimcolor.xyz / lumamax)) / (Skyrimcolor.xyz + EToneMappingCurveV2);
Skyrimcolor.xyz*=4;

#endif


#if (POSTPROCESS==3)
	Skyrimcolor.xyz *= 35;
	float	lumamax = EToneMappingOversaturationV3;
	Skyrimcolor.xyz = (Skyrimcolor.xyz * (1.0 + Skyrimcolor.xyz / lumamax)) / (Skyrimcolor.xyz + EToneMappingCurveV3);
#endif


#if (POSTPROCESS == 4)
	grayadaptation = max(grayadaptation, 0.0);
	grayadaptation = min(grayadaptation, 50.0);
	Skyrimcolor.xyz = Skyrimcolor.xyz / (grayadaptation * EAdaptationMaxV4 + EAdaptationMinV4);

	float Y = dot(Skyrimcolor.xyz, float3(0.299, 0.587, 0.114)); //0.299 * R + 0.587 * G + 0.114 * B;
	float U = dot(Skyrimcolor.xyz, float3(-0.14713, -0.28886, 0.436)); //-0.14713 * R - 0.28886 * G + 0.436 * B;
	float V = dot(Skyrimcolor.xyz, float3(0.615, -0.51499, -0.10001)); //0.615 * R - 0.51499 * G - 0.10001 * B;
		
	Y = pow(Y, EBrightnessCurveV4);
	Y = Y * EBrightnessMultiplierV4;
	Skyrimcolor.xyz = V * float3(1.13983, -0.58060, 0.0) + U * float3(0.0, -0.39465, 2.03211) + Y;
	Skyrimcolor.xyz = max(Skyrimcolor.xyz, 0.0);
	Skyrimcolor.xyz = Skyrimcolor.xyz / (Skyrimcolor.xyz + EBrightnessToneMappingCurveV4);
#endif


#if (POSTPROCESS == 5)
	float hnd = 1;
	float2 hndtweak = float2( 3.1 , 1.5 );
	grayadaptation = max(grayadaptation, 0.0);
	grayadaptation = min(grayadaptation, 50.0);
	Skyrimcolor.xyz *= lerp( hndtweak.x, hndtweak.y, hnd );
	float3 xncol = normalize(Skyrimcolor.xyz);
	float3 scl = Skyrimcolor.xyz/xncol.xyz;
	scl = pow(scl, EIntensityContrastV5);
	xncol.xyz = pow(xncol.xyz, EColorSaturationV5);
	Skyrimcolor.xyz = scl*xncol.xyz;
	Skyrimcolor.xyz *= HCompensateSatV5; // compensate for darkening caused my EcolorSat above
	Skyrimcolor.xyz = Skyrimcolor.xyz / (Skyrimcolor.xyz + EToneMappingCurveV5);
	Skyrimcolor.xyz *= 4;
#endif

#if (POSTPROCESS==6)
//Postprocessing V6 by Kermles
		//hd6/ppv2///////////////////////////////////////////
		float 	EIntensityContrastV6 = EIntensityContrastV6Day;
		float 	EColorSaturationV6 = EColorSaturationV6Day;
		float 	HCompensateSatV6 = HCompensateSatV6Day;
		float 	EToneMappingCurveV6 = EToneMappingCurveV6Day;
		float 	EBrightnessV6 = EBrightnessV6Day;
		float 	EToneMappingOversaturationV6 = EToneMappingOversaturationV6Day;
		float 	EAdaptationMaxV6 = EAdaptationMaxV6Day;
		float 	EAdaptationMinV6 = EAdaptationMinV6Day;
		float	lumamax = EToneMappingOversaturationV6;
		//kermles////////////////////////////////////////////
		float 	EAdaptationCompensateV6 = EAdaptationCompensateV6Day;
		float 	EBrightnessMaxV6 = EBrightnessMaxV6Day;
		float 	EBrightnessMinV6 = EBrightnessMinV6Day;
		float3 	moodColor = EMoodColorDay;
		float 	moodAmount = EMoodAmountDay;
		float 	moodCurve = EMoodCurveDay;
		float3 	shadowColor = EShadowColorDay;
		float 	shadowThreshold = EShadowThresholdDay;
		float 	shadowCurve = EShadowCurveDay;
		float3	brightSpotColor = EBrightSpotColorDay;
		float	brightSpotThreshold = EBrightSpotThresholdDay;
		float 	brightSpotCurve = EBrightSpotCurveDay / 10;
		float 	hsvDesatCurve = EHSVDesatCurveDay;
		float 	PPAmount = 0.5;				//controls interpolation between vanilla colors and PP6 colors	
		float4 	ncolor;					//temporary variable for color adjustments
		float 	avgbr;					//temporary variable for color adjustments
		
	//begin pp code/////////////////////////////////////////////////
		//store vanilla colors//////////////////////////////////////////
		float4 oldcolor = Skyrimcolor;
		//convert to hsv////////////////////////////////////////////////
		float3 hsvncolor = RGBtoHSV( Skyrimcolor.xyz );
		hsvncolor.y = max( hsvncolor.y, 0.0 );
		hsvncolor.z = max( hsvncolor.z, 0.0 );
		//desaturate based on original saturation then resaturate///////
		hsvncolor.y = pow( hsvncolor.y, hsvDesatCurve );
		hsvncolor.y = saturate(hsvncolor.y * EColorSaturationV6);
		//convert back to rgb///////////////////////////////////////////
		hsvncolor.y = max( hsvncolor.y, 0.0 );
		Skyrimcolor.xyz = HSVtoRGB( hsvncolor );
		//brightness clamping///////////////////////////////////////////
		Skyrimcolor.xyz=clamp(Skyrimcolor.xyz, EBrightnessMinV6, EBrightnessMaxV6); 
		//ppv2 modified by kermles//////////////////////////////////////
		
		grayadaptation = clamp(grayadaptation, 0, 50);
		Skyrimcolor.xyz *= EBrightnessV6;
		float3 xncol = normalize(Skyrimcolor.xyz);
		float3 scl = Skyrimcolor.xyz/xncol.xyz;
		scl = pow(scl, EIntensityContrastV6);
		xncol.xyz = pow(xncol.xyz, EColorSaturationV6);
		Skyrimcolor.xyz = scl*xncol.xyz;
		Skyrimcolor.xyz *= HCompensateSatV6;
		Skyrimcolor.xyz = (Skyrimcolor.xyz * (1.0 + Skyrimcolor.xyz/lumamax))/(Skyrimcolor.xyz + EToneMappingCurveV6);
		//mood coloring/////////////////////////////////////////////////
		ncolor = Skyrimcolor;
		avgbr = (ncolor.x + ncolor.y + ncolor.z)/3;
		moodColor.xyz = lerp( moodColor/10, moodColor, saturate( avgbr * 2 ) );
		moodColor.xyz = lerp( moodColor, 1, saturate( avgbr - 0.5 ) * 2 );
		moodColor.xyz = lerp( ncolor, moodColor, saturate(avgbr / moodCurve));
		ncolor.xyz = lerp( ncolor, moodColor, saturate( avgbr * moodAmount ) );
		Skyrimcolor.xyz = max(0, ncolor);
		//shadows///////////////////////////////////////////////////////
		ncolor = Skyrimcolor;
		avgbr = (ncolor.x + ncolor.y + ncolor.z)/3;
		shadowColor = lerp(0.1*(2.55-shadowColor), 2.55-shadowColor, saturate(avgbr*2));
		shadowColor = lerp(shadowColor, 1, saturate(avgbr-0.5)*2);
		ncolor.xyz = max(ncolor, pow(ncolor, ((1.0+ncolor) * (shadowColor))*shadowThreshold)/shadowCurve);
		Skyrimcolor.xyz = saturate(ncolor);
		//brightspots///////////////////////////////////////////////////
		brightSpotColor = lerp(brightSpotColor/10, brightSpotColor, saturate(avgbr*2));
		brightSpotColor = lerp(brightSpotColor, 1, saturate(avgbr-0.5)*2);
		ncolor = 1-Skyrimcolor;
		ncolor.xyz = max(ncolor, pow(ncolor, ((1.0 + ncolor) * (brightSpotColor))*brightSpotThreshold)/brightSpotCurve);
		Skyrimcolor.xyz = saturate(1-ncolor);
		//convert to hsv////////////////////////////////////////////////
		hsvncolor = RGBtoHSV( Skyrimcolor.xyz );
		hsvncolor.y = max( hsvncolor.y, 0.0 );
		hsvncolor.z = max( hsvncolor.z, 0.0 );
		//adaptation/contrast///////////////////////////////////////////
		hsvncolor.z = pow(hsvncolor.z, (grayadaptation*EAdaptationMaxV6+EAdaptationMinV6));
		//convert back to rgb///////////////////////////////////////////
		hsvncolor.y = max( hsvncolor.y, 0.0 );
		hsvncolor.z = max( hsvncolor.z, 0.0 );
		Skyrimcolor.xyz = HSVtoRGB( hsvncolor );
		Skyrimcolor.xyz /= grayadaptation*EAdaptationMaxV6+EAdaptationMinV6;
		Skyrimcolor.xyz /= EAdaptationCompensateV6;
		//rerun ppv2////////////////////////////////////////////////////
		Skyrimcolor.xyz *= EBrightnessV6;
		xncol = normalize(Skyrimcolor.xyz);
		scl = Skyrimcolor.xyz/xncol.xyz;
		scl = pow(scl, EIntensityContrastV6);
		xncol.xyz = pow(xncol.xyz, EColorSaturationV6);
		Skyrimcolor.xyz = scl*xncol.xyz;
		Skyrimcolor.xyz *= HCompensateSatV6;
		Skyrimcolor.xyz = (Skyrimcolor.xyz * (1.0 + Skyrimcolor.xyz/lumamax))/(Skyrimcolor.xyz + EToneMappingCurveV6);
		//lerp between vanilla and pp6 colors///////////////////////////
		Skyrimcolor = lerp(oldcolor, Skyrimcolor, PPAmount);

#endif


Skyrimcolor.w=1;

    return Skyrimcolor; 
}
#endif



#ifdef LENZ
float3 sampleblurred(float2 uvsrc)
{
	
	float4	res;
	res =tex2D(InputSampler, uvsrc);
	float ScaleXY= (1/1024);

	res+=tex2D(InputSampler, uvsrc+float2(-0.94201624*ScaleXY, -0.39906216*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2( 0.94558609*ScaleXY, -0.76890725*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2(-0.09418410*ScaleXY, -0.92938870*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2( 0.34495938*ScaleXY,  0.29387760*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2(-0.91588581*ScaleXY,  0.45771432*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2(-0.81544232*ScaleXY, -0.87912464*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2(-0.38277543*ScaleXY,  0.27676845*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2( 0.97484398*ScaleXY,  0.75648379*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2( 0.44323325*ScaleXY, -0.97511554*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2( 0.53742981*ScaleXY, -0.47373420*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2(-0.26496911*ScaleXY, -0.41893023*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2( 0.79197514*ScaleXY,  0.19090188*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2(-0.24188840*ScaleXY,  0.99706507*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2(-0.81409955*ScaleXY,  0.91437590*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2( 0.19984126*ScaleXY,  0.78641367*ScaleXY));
	res+=tex2D(InputSampler, uvsrc+float2( 0.14383161*ScaleXY, -0.14100790*ScaleXY));
res /= 17;



res.xyz = dot(res.xyz, float3(0.3, 0.59, 0.11));


res.xyz=res.xyz-float3(LenzThresh,LenzThresh,LenzThresh);

if(res.x < 0)	
	{
res.x=0;
}
if(res.y < 0)	
	{
res.y=0;
}
if(res.z < 0)	
	{
res.z=0;
}
res.w= 1.0;



res *= 4;
	return res;
}


float4 PS_Process(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float4	res;
	float3	origcolor=tex2D(InputSampler, IN.txcoord.xy);

	float3	lenz=0;
	float2	lenzuv=0.0;
	
	const float3 offset[15]=
	{
		float3(0.3, 0.01, 4),
		float3(0.7, 0.25, 25),
		float3(0.3, 0.25, 15),
// Full
		float3(1, 1.0, 5),
// Arriere
		float3(-0.15, 20, 1),
		float3(-0.3, 20, 1),
// Avant
		float3(0.5, 0.1, 1),
		float3(0.01, 10, 1),
		float3(20, 0.25, 1),
		float3(1000, 5, 100),
//Plein ecran passe inverse
		float3(0.5, -0.5, 2),
//Rond bleu du debut
		float3(2, 2, -5),
//2eme partie de la passe FS
		float3(-5, 0.2, 0.2),
//Derniere passe
		float3(0.15, 0.5, 20),
		float3(0.4, 1, 10)
	};

const float3 factors[15]=
	{
		float3(0.5, 0.5, 0),
		float3(0, 0.5, 0),
		float3(0, 0, 0.5),
// Full
		float3(0.2, 0.25, 0),
// Arriere
		float3(0.15, 0, 0.0),
		float3(0, 0.0, 0.15),
// Avant
		float3(0.2, 0.2, 0.05),
		float3(0.25, 0.25, 0.25),
		float3(1, 1, 1),
		float3(0, 0.25, 1),
//Plein ecran
		float3(0, 0,0.25),
//Rond bleu au debut
		float3(0, 0, 1),
//Derniere passe avec passe full combine
		float3(2, 2, 2),
		float3(1, 1, 0.25),
		float3(0, 0, 0)

	};


	for (int i=0; i<15; i++)
	{
		float2 distfact=(IN.txcoord.xy-0.5);
		lenzuv.xy=offset[i].x*distfact;
		lenzuv.xy*=pow(2.0*length(float2(distfact.x*ScreenSize.z,distfact.y)), offset[i].y);
		lenzuv.xy*=offset[i].z*0.5;
		lenzuv.xy=0.5-lenzuv.xy;
		float3 templenz=sampleblurred(lenzuv.xy);
		templenz=templenz*factors[i];
		distfact=(lenzuv.xy-0.5);
		distfact*=2.0;
		templenz*=saturate(1.0-dot(distfact,distfact));
		float maxlenz=max(templenz.x, max(templenz.y, templenz.z));
		float tempnor=(maxlenz/(1.0+maxlenz));
		tempnor=pow(tempnor, ELenzPower);
		templenz.xyz*=tempnor;
		lenz+=templenz;
	}


	lenz.xyz*=1.25*ELenzIntensity;

	res.xyz=origcolor + lenz;


	res.w=1.0;
	return res;
}
#endif

//--------------------------------------------------------------------------------------
// Compiler
//--------------------------------------------------------------------------------------
technique PostProcess
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 main();
	}
#ifdef LENZ
pass P1
	{

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

		DitherEnable=FALSE;
		ZEnable=FALSE;
		CullMode=NONE;
		ALPHATESTENABLE=FALSE;
		SEPARATEALPHABLENDENABLE=FALSE;
		AlphaBlendEnable=FALSE;
		StencilEnable=FALSE;
		FogEnable=FALSE;
		SRGBWRITEENABLE=FALSE;
	}
#endif
#ifdef LIGHTSCATTERING
	pass P2
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessSunShafts();
		AlphaBlendEnable=True;
		SrcBlend = One;
		DestBlend = One;
	}
#endif
#ifdef MATSO_FLARE
        pass P3
        {
                AlphaBlendEnable = true;
                SrcBlend = One;
                DestBlend = One;
                               
                PixelShader = compile ps_3_0 PS_ProcessPass_Anamorphic(fFlareAxis);
         }
#endif
}

technique PostProcess2
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#ifdef CHROMATICABBERATION
pass P1
{

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

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

technique PostProcess3
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
}
#ifndef DOF
technique PostProcess4
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

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

technique PostProcess5
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#ifdef NOISE
pass P1
{

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

DitherEnable=FALSE;
ZEnable=FALSE;
CullMode=NONE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
StencilEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#endif
}
technique PostProcess6
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 CBloom();
	}
}
#ifdef SKYRIMTONEMAPV8
technique PostProcess7
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 Skyrimtonemap();
ZEnable=FALSE;
 CullMode=NONE;
 ALPHATESTENABLE=FALSE;
 SEPARATEALPHABLENDENABLE=FALSE;
 AlphaBlendEnable=FALSE;
 FogEnable=FALSE;
 SRGBWRITEENABLE=FALSE;	

}
}
#endif


#endif
#ifdef DOF_PREC_1
technique PostProcess4
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

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

technique PostProcess5
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#ifdef NOISE
pass P1
{

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

DitherEnable=FALSE;
ZEnable=FALSE;
CullMode=NONE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
StencilEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#endif
}
technique PostProcess6
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 CBloom();
	}
}

technique PostProcess7
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessDoFBokeh();
		CullMode=NONE;
	}
	
}
#ifdef SKYRIMTONEMAPV8
technique PostProcess8
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 Skyrimtonemap();
ZEnable=FALSE;
 CullMode=NONE;
 ALPHATESTENABLE=FALSE;
 SEPARATEALPHABLENDENABLE=FALSE;
 AlphaBlendEnable=FALSE;
 FogEnable=FALSE;
 SRGBWRITEENABLE=FALSE;	

}
}
#endif
#endif
#ifdef DOF_PREC_2
technique PostProcess4
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

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

technique PostProcess5
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#ifdef NOISE
pass P1
{

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

DitherEnable=FALSE;
ZEnable=FALSE;
CullMode=NONE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
StencilEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#endif
}
technique PostProcess6
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 CBloom();
	}
	pass P1
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessDoFBokeh();
		CullMode=NONE;
	}
}

technique PostProcess7
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessDoFBokeh();
		CullMode=NONE;
	}
	
}
#ifdef SKYRIMTONEMAPV8
technique PostProcess8
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 Skyrimtonemap();
ZEnable=FALSE;
 CullMode=NONE;
 ALPHATESTENABLE=FALSE;
 SEPARATEALPHABLENDENABLE=FALSE;
 AlphaBlendEnable=FALSE;
 FogEnable=FALSE;
 SRGBWRITEENABLE=FALSE;	

}
}
#endif
#endif
#ifdef DOF_PREC_3
technique PostProcess4
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#ifdef NOISE
pass P1
{

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

DitherEnable=FALSE;
ZEnable=FALSE;
CullMode=NONE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
StencilEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#endif
}
technique PostProcess5
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 CBloom();
	}
	pass P1
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessDoFBokeh();
		CullMode=NONE;
	}
}

technique PostProcess6
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessDoFBokeh();
		CullMode=NONE;
	}
	
}
technique PostProcess7
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessDoFBokeh();
		CullMode=NONE;
	}
	
}
#ifdef SKYRIMTONEMAPV8
technique PostProcess8
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 Skyrimtonemap();
ZEnable=FALSE;
 CullMode=NONE;
 ALPHATESTENABLE=FALSE;
 SEPARATEALPHABLENDENABLE=FALSE;
 AlphaBlendEnable=FALSE;
 FogEnable=FALSE;
 SRGBWRITEENABLE=FALSE;	

}
}
#endif
#endif