/**
 *	K ENB Evolution DOF 
 *	Dynamic Extensive Code v13.0 a/ Adaptive
 *	Immersive DoF / Cinematic DoF toggle
 *	Polygonal Bokeh Methods toggle w/Rotation a/Dynamics
 *	Dynamic Pentagonal
 *
 *	Increasing DOF QUALITY will increase and smooth blur
 *	The better the DOF Q, the harder the performance hit
 *
 *	Set with 85.0 FOV In Game
 *	Initial Request from Midhras
 *	Code Credits : --JawZ--, gp65cj04, Matso, Arsil, Michael Tichenor, Kyo
 *	Code Extensive Modifications, Implementations and Settings by Kyo
 *	Copyright (c) 2007-2013 Boris Vorontsov
 */

/*==============================================================================================================
	Usefull Constants
==============================================================================================================*/

#define PI			3.1415926535897932384626433832795
#define PI_OVER_180		0.01745329251994329576923690768489
#define SEED			Timer.w		// Set ' 1 ' instead of ' Timer.w ' for no time-dependancy


/*==============================================================================================================
	KYO : IMPORTANT DEFINE
	Below is a toggle, depending on you're using pre-1.4x binaries a/o Old Detector version in enbseries.ini
	If using post 0.14x binaries and Old Detector, the following define SHALL REMAIN ENABLED
	If using previous binaries or New Detector, you have to comment it ''//')
==============================================================================================================*/

#define ENABLE_FLIP_DAY_NIGHT_FACTOR


/*==============================================================================================================
	Downsampling Toggle - Set for Personal Convenience
==============================================================================================================*/

#define USE_4K_DOWNSAMPLING			0


/*==============================================================================================================
	DISPLAY Resolution - Enter your exact resolution scales below
==============================================================================================================*/

#if (USE_4K_DOWNSAMPLING == 1)

#define fWidth					3840.0
#define fHeight					2160.0

#else

#define fWidth					1920.0
#define fHeight					1080.0

#endif


/*==============================================================================================================
	DOF MODES
==============================================================================================================*/

#define USE_DYNAMIC_DOF				1	// 0 for STATIC
#define USE_NOT_BLURRING_SKY			0
#define USE_TILT_SHIFT				0
#define USE_IMMERSIVE_DOF			0	// 0 for CINEMATIC


/*==============================================================================================================
	POLYGONAL BOKEH MODES
	Can be 1 or 2
	1	> No Bokeh Shape Rotation
	2	> Bokeh Shape Rotation
	2 allows for rotation of Bokeh Shapes via defined fPolygonRotation below
==============================================================================================================*/

#define USE_POLYGONAL_BOKEH			2


/*==============================================================================================================
	POLYGONAL BOKEH METHOD 2 Parameter
	Rotates clockwise, PolygonRotation 0.0 means no rotation, first vertex being up
	Rotation counts in degrees, 0.0 to 360.0
	0 or 360 > No a/o full rotation
	180	> Rotates 1st Vertex Down
	270	> Rotates 1st Vertex Left
	90	> Rotates 1st Vertex Right
==============================================================================================================*/

#define fPolygonRotation_Day			270.0		
#define fPolygonRotation_Night			90.0		
#define fPolygonRotation_Interior		180.0		


/*==============================================================================================================
	DYNAMIC SHAPE ROTATION enable / disable
	Speed Factor
	If enabled, Bokeh Shape will rotate in-game real time, according to speed factor defined below
	1.0	>	Low Motion
	5.0	> 	Medium Motion
	7.0	>	Fast Motion
	9.0 and above	>	Very Fast Motion a/o Blinking
	Decimal values work as well	
==============================================================================================================*/

#define	USE_DYNAMIC_ROTATION			1

#define	fRotationSpeed_Day			1.0
#define	fRotationSpeed_Night			5.777
#define	fRotationSpeed_Interior			7.777


/*==============================================================================================================
	Additional FX applied *with* DoF 
	Grain, Chromatic Aberration
	Enable with 1 - Disable with 0
==============================================================================================================*/

#define	USE_GRAIN				0	// Global Toggle
#define	USE_GRAIN_DISPLACEMENT			1

#define Use_Grain_Formula			1	// Can be 1 or 2
#define Use_Grain_Day				0
#define Use_Grain_Night				0
#define Use_Grain_Interior			0


#define	USE_CHROMA				1	// Global Toggle
#define	Use_Chroma_Day				1
#define	Use_Chroma_Night			1
#define	Use_Chroma_Interior			1

#define	Use_Chroma_Formula_Day			1	// From 1 (Regular Displacement of RGBs) to 10 (Custom RGBs Displacements)	// 7
#define	Use_Chroma_Formula_Night		7
#define	Use_Chroma_Formula_Interior		10	


/*==============================================================================================================
	Additional FX applied *before* DoF 
	Pixel Colour Sharpening, Anamorphic Lens Flare
	Enable with 1 - Disable with 0
==============================================================================================================*/

#define USE_PRE_FX				1	// Enables-Disables all Pre-FX

#define USE_SHARPENING				1	// Global Toggle			
#define	Use_Sharpening_Day			1
#define	Use_Sharpening_Night			1
#define	Use_Sharpening_Interior			1

#define USE_ANAMFLARE				0	// Global Toggle	
#define	Use_AnamFlare_Day			0
#define	Use_AnamFlare_Night			0
#define	Use_AnamFlare_Interior			0


/*==============================================================================================================
	Additional FX applied *after* DoF 
	Cinematic Grain
	Enable with 1 - Disable with 0
==============================================================================================================*/

#define USE_POST_FX				0	// Enables-Disables all Post-FX

#define USE_CINEMATIC_GRAIN			0	// Global Toggle
#define	Use_Cinematic_Grain_Day			0
#define	Use_Cinematic_Grain_Night		0
#define	Use_Cinematic_Grain_Interior		0


/*==============================================================================================================
	DYNAMIC VARS
	Enable with 1 - Disable with 0
	*CHOOSE ONLY 1* Dynamic Method out of ' PUSH ' a/o ' PULL '
==============================================================================================================*/

#define	USE_DYNAMIC_CHROMA_PUSH			0	// Increase Chromatic Aberration dynamically, according to factors below
#define	USE_DYNAMIC_CHROMA_PULL			0	// Decrease Chromatic Aberration dynamically, according to factors below

#define	USE_DYNAMIC_GRAIN_PUSH			0	// Increase Grain dynamically, according to factors below
#define	USE_DYNAMIC_GRAIN_PULL			1	// Decrease Grain dynamically, according to factors below

#define	USE_DYNAMIC_GRAIN_CURVE_PUSH		0	// Increase Grain Curve dynamically, according to factors below
#define	USE_DYNAMIC_GRAIN_CURVE_PULL		0	// Decrease Grain Curve dynamically, according to factors below

#define	USE_DYNAMIC_GRAIN_DIFF_PUSH		0	// Increase Grain Diffusion dynamically, according to factors below
#define	USE_DYNAMIC_GRAIN_DIFF_PULL		1	// Decrease Grain Diffusion dynamically, according to factors below

#define	USE_DYNAMIC_BOKEH_BRIGHT_PUSH		0	// Increase Bokeh Brightness dynamically, according to factors below
#define	USE_DYNAMIC_BOKEH_BRIGHT_PULL		1	// Decrease Bokeh Brightness dynamically, according to factors below

#define	USE_DYNAMIC_BOKEH_THRESH_PUSH		1	// Decrease Bokeh Threshold dynamically according to factors below, hence " Bokehrize " sooner
#define	USE_DYNAMIC_BOKEH_THRESH_PULL		0	// Increase Bokeh Threshold dynamically according to factors below, hence " Bokehrize " later

#define	USE_DYNAMIC_RADIUS_SCALE_PUSH		1	// Increase DoF Blur Range dynamically, according to factors below	
#define	USE_DYNAMIC_RADIUS_SCALE_PULL		0	// Decrease DoF Blur Range dynamically, according to factors below	
		

/*==============================================================================================================
	DOF QUALITY LEVEL - 1ST PERSON IMMERSIVE
==============================================================================================================*/

#if (USE_IMMERSIVE_DOF == 1)

#define	DEPTH_OF_FIELD_QUALITY 			2	// 1, ScA 5	// From 1 to 7 		// 3, 5, 7	

#else

/*==============================================================================================================
	DOF QUALITY LEVEL - 3RD PERSON
==============================================================================================================*/

#define	DEPTH_OF_FIELD_QUALITY 			4	// 3, ScA 7	// From 1 to 7 		// 2, 5, 7

#endif


/*==============================================================================================================
	ADAPTIVE QUALITY TOGGLE
==============================================================================================================*/

#define	USE_ENB_ADAPTIVE_QUALITY		0	// Works dynamically if enabled in enblocal.ini, otherwise enables alternative code


/*==============================================================================================================
	Adaptive Quality Functions - TESV Wrappers ONLY
==============================================================================================================*/

#if (USE_ENB_ADAPTIVE_QUALITY == 1)
int MaxDepthOfFieldQuality <
   string UIName="MaxDepthOfFieldQuality";
   string UIWidget="Spinner";
   int UIMin=1;
   int UIMax=7;
> = DEPTH_OF_FIELD_QUALITY;
#endif


/*==============================================================================================================
	DOF Sampling Vectors
	Alternative method to modify blur
	* Choose only ONE * - Default is ORIGINAL_OFFSETS
==============================================================================================================*/

#define USE_TIGHT_OFFSETS			0			
#define USE_CENTERED_OFFSETS			0				
#define USE_REGULAR_OFFSETS			0		
#define USE_ORIGINAL_OFFSETS			1			
#define USE_WIDE_OFFSETS			0		
#define USE_NATURAL_OFFSETS			0


/*==============================================================================================================
	Tilt-Shift Angle
==============================================================================================================*/

#define	fTiltShiftAngle_Day			30.0
#define	fTiltShiftAngle_Night			30.0
#define	fTiltShiftAngle_Interior		30.0


/*==============================================================================================================
	Polygonal Bokeh Shape
	Works if USE_ENB_ADAPTIVE_QUALITY is disabled	
	Separated vars for Polygonal Bokeh methods
	fPolygon_Numb		> 	No Shape Rotation
	fPolygon_Number		> 	Shape Rotation enabled
	The lesser the polygons, the more visible the rotation effect will be
==============================================================================================================*/

#define fPolygon_Numb 			8

#define fPolygon_Number			5	// 5,	8,	6


/*==============================================================================================================
	Polygonal Bokeh Shape - DNI separated
	Works if USE_ENB_ADAPTIVE_QUALITY is enabled
==============================================================================================================*/

#define fPolygons_Day				5
#define fPolygons_Night				7
#define fPolygons_Interior			6


/*==============================================================================================================
	DYNAMIC DOF - IMMERSIVE
==============================================================================================================*/

#if (USE_IMMERSIVE_DOF == 1)

#define fFocusPoint_Day				float2(0.5, 0.5) 
#define fFocusSampleRange_Day			1.0
#define fNearBlurCurve_Day			0.175
#define fFarBlurCurve_Day			1.5					// 1.675, 1.55	
#define fDepthClip_Day				150000.0 

#define fFocusPoint_Night			float2(0.5, 0.5)
#define fFocusSampleRange_Night			1.0
#define fNearBlurCurve_Night			0.6 
#define fFarBlurCurve_Night			2.25			
#define fDepthClip_Night			150000.0 

#define fFocusPoint_Interior			float2(0.5, 0.5)
#define fFocusSampleRange_Interior		1.0
#define fNearBlurCurve_Interior			0.5
#define fFarBlurCurve_Interior			0.8					// 0.825, 0.725, 0.5
#define fDepthClip_Interior			150000.0 

#else

/*==============================================================================================================
	DYNAMIC DOF - CINEMATIC
==============================================================================================================*/

#define fFocusPoint_Day				float2(0.5, 0.5) 
#define fFocusSampleRange_Day			1.0
#define fNearBlurCurve_Day			14.55
#define fFarBlurCurve_Day			1.675					// 1.25, 1.55	
#define fDepthClip_Day				150000.0 

#define fFocusPoint_Night			float2(0.5, 0.5)
#define fFocusSampleRange_Night			1.0
#define fNearBlurCurve_Night			14.45 
#define fFarBlurCurve_Night			1.625			
#define fDepthClip_Night			150000.0 

#define fFocusPoint_Interior			float2(0.5, 0.5)
#define fFocusSampleRange_Interior		1.0
#define fNearBlurCurve_Interior			10.00
#define fFarBlurCurve_Interior			0.85					// 0.825, 0.725, 0.5
#define fDepthClip_Interior			150000.0  

#endif			


/*==============================================================================================================
	STATIC DOF 
==============================================================================================================*/

#define	fFocalPlaneDepth_Day			1.00
#define	fFarBlurDepth_Day			150.00	

#define	fFocalPlaneDepth_Night			1.00
#define	fFarBlurDepth_Night			150.00

#define	fFocalPlaneDepth_Interior		1.00
#define	fFarBlurDepth_Interior			150.00
			
			
/*==============================================================================================================
	BOKEH Effect Parameters - IMMERSIVE
==============================================================================================================*/

#if (USE_IMMERSIVE_DOF == 1)

#define	fBokehBias_Day				-0.00125
#define	fBokehBiasCurve_Day			0.75
#define	fBokehBrightnessThreshold_Day		0.95 
#define	fBokehBrightnessMultiplier_Day		1.00
#define	fRadiusScaleMultiplier_Day		1.10

#define	fBokehBias_Night			0.00
#define	fBokehBiasCurve_Night			0.25
#define	fBokehBrightnessThreshold_Night		0.95
#define	fBokehBrightnessMultiplier_Night	1.00
#define	fRadiusScaleMultiplier_Night		2.50

#define	fBokehBias_Interior			0.00
#define	fBokehBiasCurve_Interior		0.75
#define	fBokehBrightnessThreshold_Interior	0.95
#define	fBokehBrightnessMultiplier_Interior	2.05
#define	fRadiusScaleMultiplier_Interior		3.23

#else

/*==============================================================================================================
	BOKEH Effect Parameters - CINEMATIC
==============================================================================================================*/

#define	fBokehBias_Day				-0.00125
#define	fBokehBiasCurve_Day			0.75
#define	fBokehBrightnessThreshold_Day		0.975 
#define	fBokehBrightnessMultiplier_Day		1.00
#define	fRadiusScaleMultiplier_Day		2.23

#define	fBokehBias_Night			0.00
#define	fBokehBiasCurve_Night			0.75
#define	fBokehBrightnessThreshold_Night		1.00
#define	fBokehBrightnessMultiplier_Night	1.00
#define	fRadiusScaleMultiplier_Night		2.23

#define	fBokehBias_Interior			0.00
#define	fBokehBiasCurve_Interior		0.75
#define	fBokehBrightnessThreshold_Interior	0.975
#define	fBokehBrightnessMultiplier_Interior	1.00
#define	fRadiusScaleMultiplier_Interior		2.23

#endif


/**
 *	If DYNAMIC VARS enabled, otherwise ignored
 */

#define fIntensityFactor_BokehBright_Day		0.0		
#define fTimeFactor_BokehBright_Day			300.0
#define fFrequency_BokehBright_Day			0.45	
#define fAmplitude_BokehBright_Day			0.45

#define fIntensityFactor_BokehBright_Night		5.0
#define fTimeFactor_BokehBright_Night			300.0
#define fFrequency_BokehBright_Night			0.475
#define fAmplitude_BokehBright_Night			0.475

#define fIntensityFactor_BokehBright_Interior		7.0
#define fTimeFactor_BokehBright_Interior		350.0
#define fFrequency_BokehBright_Interior			0.45
#define fAmplitude_BokehBright_Interior			0.45

#define fIntensityFactor_BokehThreshold_Day		0.0		
#define fTimeFactor_BokehThreshold_Day			100.0
#define fFrequency_BokehThreshold_Day			0.45	
#define fAmplitude_BokehThreshold_Day			0.45

#define fIntensityFactor_BokehThreshold_Night		5.0
#define fTimeFactor_BokehThreshold_Night		150.0
#define fFrequency_BokehThreshold_Night			0.5
#define fAmplitude_BokehThreshold_Night			0.5

#define fIntensityFactor_BokehThreshold_Interior	8.0
#define fTimeFactor_BokehThreshold_Interior		350.0
#define fFrequency_BokehThreshold_Interior		0.5
#define fAmplitude_BokehThreshold_Interior		0.5

#define fIntensityFactor_RadiusScale_Day		0.0				
#define fTimeFactor_RadiusScale_Day			50.0
#define fFrequency_RadiusScale_Day			0.35		
#define fAmplitude_RadiusScale_Day			0.35		

#define fIntensityFactor_RadiusScale_Night		4.0
#define fTimeFactor_RadiusScale_Night			100.0
#define fFrequency_RadiusScale_Night			0.425
#define fAmplitude_RadiusScale_Night			0.425

#define fIntensityFactor_RadiusScale_Interior		8.0
#define fTimeFactor_RadiusScale_Interior		250.0
#define fFrequency_RadiusScale_Interior			0.5
#define fAmplitude_RadiusScale_Interior			0.5


/*==============================================================================================================
	GRAIN 
==============================================================================================================*/

#define	fNoiseAmount_Day			0.075
#define	fNoiseCurve_Day				1.177
#define	fNoiseDiffusion_Day			0.5		// [KYO : From 0.0 to 1.8]

#define	fNoiseAmount_Night			0.0775
#define	fNoiseCurve_Night			0.999
#define	fNoiseDiffusion_Night			0.5		

#define	fNoiseAmount_Interior			0.0725
#define	fNoiseCurve_Interior			0.999
#define	fNoiseDiffusion_Interior		0.5		


/**
 *	If DYNAMIC VARS enabled, otherwise ignored
 */

#define fIntensityFactor_Noise_Day		8.0
#define fTimeFactor_Noise_Day			300.0
#define fFrequency_Noise_Day			0.45	
#define fAmplitude_Noise_Day			0.45

#define fIntensityFactor_Noise_Night		8.0
#define fTimeFactor_Noise_Night			300.0
#define fFrequency_Noise_Night			0.5
#define fAmplitude_Noise_Night			0.5

#define fIntensityFactor_Noise_Interior		8.0
#define fTimeFactor_Noise_Interior		350.0
#define fFrequency_Noise_Interior		0.5
#define fAmplitude_Noise_Interior		0.5

#define fIntensityFactor_Curve_Day		8.0
#define fTimeFactor_Curve_Day			300.0
#define fFrequency_Curve_Day			0.45	
#define fAmplitude_Curve_Day			0.45

#define fIntensityFactor_Curve_Night		8.0
#define fTimeFactor_Curve_Night			300.0
#define fFrequency_Curve_Night			0.5
#define fAmplitude_Curve_Night			0.5

#define fIntensityFactor_Curve_Interior		8.0
#define fTimeFactor_Curve_Interior		350.0
#define fFrequency_Curve_Interior		0.5
#define fAmplitude_Curve_Interior		0.5

#define fIntensityFactor_Diffusion_Day		8.0
#define fTimeFactor_Diffusion_Day		100.0
#define fFrequency_Diffusion_Day		0.45	
#define fAmplitude_Diffusion_Day		0.45

#define fIntensityFactor_Diffusion_Night	8.0
#define fTimeFactor_Diffusion_Night		100.0
#define fFrequency_Diffusion_Night		0.5
#define fAmplitude_Diffusion_Night		0.5

#define fIntensityFactor_Diffusion_Interior	8.0
#define fTimeFactor_Diffusion_Interior		250.0
#define fFrequency_Diffusion_Interior		0.5
#define fAmplitude_Diffusion_Interior		0.5

	
/*==============================================================================================================
	CHROMATIC ABERRATION 
==============================================================================================================*/

#define	fChromaticAberration_Day		0.225 	

#define	fChromaticAberration_Night		0.25275

#define	fChromaticAberration_Interior		0.2225 


/**
 *	If DYNAMIC VARS enabled, otherwise ignored
 */

#define fIntensityFactor_Chroma_Day		0.5		
#define fTimeFactor_Chroma_Day			100.0
#define fFrequency_Chroma_Day			0.325	
#define fAmplitude_Chroma_Day			0.325

#define fIntensityFactor_Chroma_Night		4.0
#define fTimeFactor_Chroma_Night		150.0
#define fFrequency_Chroma_Night			0.335
#define fAmplitude_Chroma_Night			0.335

#define fIntensityFactor_Chroma_Interior	1.0		
#define fTimeFactor_Chroma_Interior		100.0		
#define fFrequency_Chroma_Interior		0.315		
#define fAmplitude_Chroma_Interior		0.315		


/*==============================================================================================================
	SHARPENING 
==============================================================================================================*/

#define fSharpScale_Day				0.0625		// 0.0325, 0.0315, 0.0515, 0.0645, 0.0465			

#define fSharpScale_Night			0.0635		// 0.0325, 0.0315, 0.0515, 0.06225, 0.0475	

#define fSharpScale_Interior			0.0685		// 0.0335, 0.345, 0.0325, 0.0525, 0.06275, 0.0485	


/*==============================================================================================================
	ANAMORPHIC LENS FLARE 
==============================================================================================================*/

#define fFlareLuminance_Day			 1.6				// Threshold  
#define fFlareBlur_Day				 20.0				// Size 
#define fFlareIntensity_Day			 0.032				// Intensity
#define fFlareTint_Day			  	 float3(0.0, 0.0, 1.0)		// Tint R-G-B
//#define fFlareTint_Day			 float3(0.012, 0.313, 0.588)	// Medium Electric Blue
#define fFlareAxis_Day			         0				// Blur axis [KYO : 0 is horizontal, 1 is vertical]

#define fFlareLuminance_Night			 1.6				
#define fFlareBlur_Night			 200.0				// 16.0				
#define fFlareIntensity_Night			 0.052	
//#define fFlareTint_Night			 float3(0.012, 0.313, 0.588)			
#define fFlareTint_Night			 float3(0.0, 0.0, 1.0)		
#define fFlareAxis_Night			 1				

#define fFlareLuminance_Interior		 1.6				
#define fFlareBlur_Interior			 200.0				// 16.0				
#define fFlareIntensity_Interior		 0.058
//#define fFlareTint_Interior			 float3(0.012, 0.313, 0.588)					
#define fFlareTint_Interior			 float3(0.0, 0.0, 1.0)		
#define fFlareAxis_Interior			 1				


/*==============================================================================================================
	CINEMATIC GRAIN settings
==============================================================================================================*/

#define fGrainFreq_Day  			 3333.0				// Frequency
#define fGrainScale_Day 			 0.02185			// Intensity

#define fGrainFreq_Night  			 2500.0			
#define fGrainScale_Night 			 0.02185			

#define fGrainFreq_Interior  			 2777.0			
#define fGrainScale_Interior			 0.02385	
		

/*==============================================================================================================
	END of Defines
==============================================================================================================*/
/*==============================================================================================================
	Retro-Compatibility Toggle Code
==============================================================================================================*/

#ifndef ENABLE_FLIP_DAY_NIGHT_FACTOR
		1 - ENightDayFactor = ENightDayFactor;
#endif


/*==============================================================================================================
	Bypasses - Fixed Values * Do not intervene here *
==============================================================================================================*/

#if (USE_CHROMA == 0)
#define	fChromaticAberration_Day		0.0
#define	fChromaticAberration_Night		0.0
#define	fChromaticAberration_Interior		0.0
#endif
#if (Use_Chroma_Day == 0)
#define	fChromaticAberration_Day		0.0
#endif
#if (Use_Chroma_Night == 0)
#define	fChromaticAberration_Night		0.0
#endif
#if (Use_Chroma_Interior == 0)
#define	fChromaticAberration_Interior		0.0
#endif

#if (Use_AnamFlare_Day == 0)
#define fFlareLuminance_Day			 1000.0
#define fFlareBlur_Day				 0.0
#define fFlareIntensity_Day			 0.0
#define fFlareTint_Day			  	 float3(0.0, 0.0, 0.0)		
#define fFlareAxis_Day			         0
#endif	
#if (Use_AnamFlare_Night == 0)
#define fFlareLuminance_Night			 1000.0
#define fFlareBlur_Night			 0.0
#define fFlareIntensity_Night			 0.0
#define fFlareTint_Night			 float3(0.0, 0.0, 0.0)		
#define fFlareAxis_Night			 0
#endif			
#if (Use_AnamFlare_Interior == 0)
#define fFlareLuminance_Interior		 1000.0
#define fFlareBlur_Interior			 0.0
#define fFlareIntensity_Interior		 0.0
#define fFlareTint_Interior			 float3(0.0, 0.0, 0.0)		
#define fFlareAxis_Interior			 0
#endif	

#if (Use_Sharpening_Day == 0)
#define fSharpScale_Day			0.0
#endif
#if (Use_Sharpening_Night == 0)
#define fSharpScale_Night		0.0
#endif
#if (Use_Sharpening_Interior == 0)
#define fSharpScale_Interior		0.0
#endif

#if (USE_GRAIN == 0)
#define	fNoiseAmount_Day		0.0
#define	fNoiseCurve_Day			0.0	
#define	fNoiseDiffusion_Day		0.0
#define	fNoiseAmount_Night		0.0
#define	fNoiseCurve_Night		0.0	
#define	fNoiseDiffusion_Night		0.0
#define	fNoiseAmount_Interior		0.0
#define	fNoiseCurve_Interior		0.0	
#define	fNoiseDiffusion_Interior	0.0
#endif
#if (Use_Grain_Day == 0)
#define	fNoiseAmount_Day		0.0
#define	fNoiseCurve_Day			0.0	
#define	fNoiseDiffusion_Day		0.0
#endif
#if (Use_Grain_Night == 0)
#define	fNoiseAmount_Night		0.0
#define	fNoiseCurve_Night		0.0	
#define	fNoiseDiffusion_Night		0.0
#endif
#if (Use_Grain_Interior == 0)
#define	fNoiseAmount_Interior		0.0
#define	fNoiseCurve_Interior		0.0	
#define	fNoiseDiffusion_Interior	0.0
#endif

#if (Use_Cinematic_Grain_Day == 0)
#define	fGrainFreq_Day			0.0
#define	fGrainScale_Day			0.0	
#endif
#if (Use_Cinematic_Grain_Night == 0)
#define	fGrainFreq_Night		0.0
#define	fGrainScale_Night		0.0	
#endif
#if (Use_Cinematic_Grain_Interior == 0)
#define	fGrainFreq_Interior		0.0
#define	fGrainScale_Interior		0.0	
#endif

/*==============================================================================================================
================================================================================================================
	Samplers, Code, Routines, Shaders 
================================================================================================================
==============================================================================================================*/

float4 tempF1; //0,1,2,3
float4 tempF2; //5,6,7,8
float4 tempF3; //9,0
float4 ScreenSize;
float4 Timer;
float ENightDayFactor;
float EInteriorFactor;
float FadeFactor;
//changes in range 0..1, 0 means full quality, 1 lowest quality (0.33, 0.66 are limits for quality levels)
float	EAdaptiveQualityFactor;
//fov in degrees
float	FieldOfView;

texture2D texColor;
texture2D texDepth;
texture2D texNoise;
texture2D texPalette;
texture2D texFocus; //computed focusing depth
texture2D texCurr; //4*4 texture for focusing
texture2D texPrev; //4*4 texture for focusing

sampler2D SamplerColor = sampler_state
{
	Texture   = <texColor>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;		
	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 = POINT;
	MagFilter = POINT;
	MipFilter = NONE;//NONE;
	AddressU  = Wrap;
	AddressV  = Wrap;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

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

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

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

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

struct VS_OUTPUT_POST
{
	float4 vpos  : POSITION;
	float2 txcoord : TEXCOORD0;
};

struct VS_INPUT_POST
{
	float3 pos  : POSITION;
	float2 txcoord : TEXCOORD0;
};
		
/*==============================================================================================================
	Start focusing
==============================================================================================================*/

VS_OUTPUT_POST VS_Focus(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;
}

float linearlizeDepth(float nonlinearDepth)
{
	float2 dofProj=float2(0.0509804, 3098.0392);
	float2 dofDist=float2(0.0, 0.0509804);
		
	float4 depth=nonlinearDepth;
	
	
	depth.y=-dofProj.x + dofProj.y;
	depth.y=1.0/depth.y;
	depth.z=depth.y * dofProj.y; 
	depth.z=depth.z * -dofProj.x; 
	depth.x=dofProj.y * -depth.y + depth.x;
	depth.x=1.0/depth.x;

	depth.y=depth.z * depth.x;

	depth.x=depth.z * depth.x - dofDist.y; 
	depth.x+=dofDist.x * -0.5;

	depth.x=max(depth.x, 0.0);
		
	return depth.x;
}

float4 PS_ReadFocus(VS_OUTPUT_POST IN) : COLOR
{

/*==============================================================================================================
	Offsets 
==============================================================================================================*/

float2 fFocusPoint =lerp( lerp( fFocusPoint_Day, fFocusPoint_Night, 1 - ENightDayFactor ), fFocusPoint_Interior, EInteriorFactor);
	float2 uvsrc=fFocusPoint;

	float2 pixelSize=ScreenSize.y;
	pixelSize.y*=ScreenSize.z;

#if (USE_TIGHT_OFFSETS == 1)
	const float2 offset[4] = 
	{
		float2(0.262, 0.641),
		float2(0.262, -0.641),
		float2(0.641, 0.262),
		float2(-0.641, 0.262)
	};
#endif

#if (USE_CENTERED_OFFSETS == 1)
	const float2 offset[4] = 
	{
		float2(0.5, 1.0),
		float2(0.5, -1.0),
		float2(1.0, 0.5),
		float2(-1.0, 0.5)
	};
#endif

#if (USE_REGULAR_OFFSETS == 1)
	const float2 offset[4] = 
	{
		float2(0.3, 0.9),
		float2(0.3, -0.9),
		float2(0.9, 0.3),
		float2(-0.9, 0.3)
	};
#endif

#if (USE_ORIGINAL_OFFSETS == 1)
	const float2 offset[4]=
	{
		float2(0.0, 1.0),
		float2(0.0, -1.0),
		float2(1.0, 0.0),
		float2(-1.0, 0.0)
	};
#endif

#if (USE_WIDE_OFFSETS == 1)	
	const float2 offset[4]=
	{
		float2(0.524, 1.282),
		float2(0.524, -1.282),
		float2(1.282, 0.524),
		float2(-1.282, 0.524)
	};	
#endif

#if (USE_NATURAL_OFFSETS == 1)
	const float2 offset[16] = { 
	float2(0.2007, 0.9796),
	float2(-0.2007, 0.9796), 
	float2(0.2007, 0.9796),
	float2(-0.2007, 0.9796), 
		
	float2(0.8240, 0.5665),
	float2(0.5665, 0.8240),
	float2(0.8240, 0.5665),
	float2(0.5665, 0.8240),

	float2(0.9796, 0.2007),
	float2(0.9796, -0.2007),
	float2(0.9796, 0.2007),
	float2(0.9796, -0.2007),
		
	float2(-0.8240, 0.5665),
	float2(-0.5665, 0.8240),
	float2(-0.8240, 0.5665),
	float2(-0.5665, 0.8240)
};			
#endif	

	float res=linearlizeDepth(tex2D(SamplerDepth, uvsrc.xy).x);
//	for (int i=0; i<4; i++)
#if (USE_NATURAL_OFFSETS == 1)
	for (int i=0; i<16; i++)
#else
	for (int i=0; i<4; i++)
#endif
	{
		uvsrc.xy=uvsrc.xy;

	float fFocusSampleRange =lerp( lerp( fFocusSampleRange_Day, fFocusSampleRange_Night, 1 - ENightDayFactor ), fFocusSampleRange_Interior, EInteriorFactor);
		uvsrc.xy+=offset[i] * pixelSize.xy * fFocusSampleRange;

	float fDepthClip =lerp( lerp( fDepthClip_Day, fDepthClip_Night, 1 - ENightDayFactor ), fDepthClip_Interior, EInteriorFactor);

		#if (USE_NOT_BLURRING_SKY == 1)
			res+=linearlizeDepth(tex2D(SamplerDepth, uvsrc).x);
		#else
			res+=min(linearlizeDepth(tex2D(SamplerDepth, uvsrc).x), fDepthClip);
		#endif
	}
	res*=0.2;	//.21	//.22
	return res;
}

float4 PS_WriteFocus(VS_OUTPUT_POST IN) : COLOR
{
float2 fFocusPoint =lerp( lerp( fFocusPoint_Day, fFocusPoint_Night, 1 - ENightDayFactor ), fFocusPoint_Interior, EInteriorFactor);
	float2 uvsrc=fFocusPoint;

	float res=0.0;
	float curr=tex2D(SamplerCurr, uvsrc.xy).x;
	float prev=tex2D(SamplerPrev, uvsrc.xy).x;

	
	res=lerp(prev, curr, saturate(FadeFactor));	//time elapsed factor

	return res;
}

technique ReadFocus
{
	pass P0
	{
		VertexShader = compile vs_3_0 VS_Focus();
		PixelShader  = compile ps_3_0 PS_ReadFocus();

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

technique WriteFocus
{
	pass P0
	{
		VertexShader = compile vs_3_0 VS_Focus();
		PixelShader  = compile ps_3_0 PS_WriteFocus();

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

/**
 * Bright pass - rescales sampled pixel to emboss bright enough value. Samples texture directly.
 */
float3 BrightPass(float2 tex)
{
float fFlareLuminance =lerp( lerp( fFlareLuminance_Day, fFlareLuminance_Night, 1 - ENightDayFactor ), fFlareLuminance_Interior, EInteriorFactor);

    float3 c = tex2D(SamplerColor, tex).rgb;
    float3 bC = max(c - float3(fFlareLuminance, fFlareLuminance, fFlareLuminance), 0.0);
    float bright = dot(bC, 1.0);
    bright = smoothstep(0.0f, 0.5, bright);
    return lerp(0.0, c, bright);
}

/**
 * Anamorphic sampling function - scales pixel coordinate
 * to stretch the image along one of the axels.
 * (http://en.wikipedia.org/wiki/Anamorphosis)
 */
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);
}

/**
 * Converts pixel color to gray-scale.
 */
float GrayScale(float3 sample)
{
	return dot(sample, float3(0.3, 0.59, 0.11));
}

/**
 * Pseudo-random number generator - returns a number generated according to the provided vector.
 */
float Random(float2 co)
{
    return frac(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453);
}

/**
 * Pseudo-random number generator - returns a vector generated according to the provided one.
 */
float2 Random2(float2 coord)
{
	float noiseX = ((frac(1.0-coord.x*(1920.0/2.0))*0.25)+(frac(coord.y*(1080.0/2.0))*0.75))*2.0-1.0;
	float noiseY = ((frac(1.0-coord.x*(1920.0/2.0))*0.75)+(frac(coord.y*(1080.0/2.0))*0.25))*2.0-1.0;
	
	noiseX = clamp(frac(sin(dot(coord ,float2(12.9898,78.233))) * 43758.5453),0.0,1.0)*2.0-1.0;
	noiseY = clamp(frac(sin(dot(coord ,float2(12.9898,78.233)*2.0)) * 43758.5453),0.0,1.0)*2.0-1.0;
	
	return float2(noiseX, noiseY);
}

/**
 * Movie grain function - returns a random, time scaled value for the given pixel coordinate.
 */
float Grain(float3 tex)
{
float fGrainFreq =lerp( lerp( fGrainFreq_Day, fGrainFreq_Night, 1 - ENightDayFactor ), fGrainFreq_Interior, EInteriorFactor);
float fGrainScale =lerp( lerp( fGrainScale_Day, fGrainScale_Night, 1 - ENightDayFactor ), fGrainScale_Interior, EInteriorFactor);

	float r = Random(tex.xy);
	float grain = sin(PI * tex.z * r * fGrainFreq) * fGrainScale * r;
	return grain;
}

VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST IN)
{
	VS_OUTPUT_POST OUT;

	float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);

	OUT.vpos=pos;
	OUT.txcoord.xy=IN.txcoord.xy;

	return OUT;
}

float4 PS_ProcessPass1(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR0
{

/**
// Set your Display resolution	
// 1,777777777777778 Ratio			
//float2 fvTexelSize = float2(1.0 / 1920.0, 1.0 / 1080.0);	
//float2 fvTexelSize = float2(1.0 / 2560.0, 1.0 / 1440.0);
//float2 fvTexelSize = float2(1.0 / 3449.0, 1.0 / 1940.0);
//float2 fvTexelSize = float2(1.0 / 3840.0, 1.0 / 2160.0);
//float2 fvTexelSize = float2(1.0 / 2880.0, 1.0 / 1620.0);
//float2 fvTexelSize = float2(1.0 / 3200.0, 1.0 / 1800.0);
//float2 fvTexelSize = float2(1.0 / 3552.0, 1.0 / 1998.0);

// 1,6 Ratio			
//float2 fvTexelSize = float2(1.0 / 1680.0, 1.0 / 1050.0);	
//float2 fvTexelSize = float2(1.0 / 1920.0, 1.0 / 1200.0);
//float2 fvTexelSize = float2(1.0 / 3840.0, 1.0 / 2400.0);

// 2,35 : 1 Ratio
//float2 fvTexelSize = float2(1.0 / 2538.0, 1.0 / 1080.0);
//float2 fvTexelSize = float2(1.0 / 2869.0, 1.0 / 1221.0);
//float2 fvTexelSize = float2(1.0 / 3200.0, 1.0 / 1362.0);
//float2 fvTexelSize = float2(1.0 / 3760.0, 1.0 / 1600.0);
*/

	float2 coord = IN.txcoord.xy;
	float4 Color = 9.0 * tex2D(SamplerColor, coord.xy);

float fSharpScale =lerp( lerp( fSharpScale_Day, fSharpScale_Night, 1 - ENightDayFactor ), fSharpScale_Interior, EInteriorFactor);	
float2 fvTexelSize = float2(1.0 / fWidth, 1.0 / fHeight);

	Color -= tex2D(SamplerColor, coord.xy + float2(-fvTexelSize.x, fvTexelSize.y) * fSharpScale);
	Color -= tex2D(SamplerColor, coord.xy + float2(0.0, fvTexelSize.y) * fSharpScale);
	Color -= tex2D(SamplerColor, coord.xy + float2(fvTexelSize.x, fvTexelSize.y) * fSharpScale);
	Color -= tex2D(SamplerColor, coord.xy + float2(fvTexelSize.x, 0.0) * fSharpScale);
	Color -= tex2D(SamplerColor, coord.xy + float2(fvTexelSize.x, -fvTexelSize.y) * fSharpScale);
	Color -= tex2D(SamplerColor, coord.xy + float2(0.0, -fvTexelSize.y) * fSharpScale);
	Color -= tex2D(SamplerColor, coord.xy + float2(-fvTexelSize.x, -fvTexelSize.y) * fSharpScale);
	Color -= tex2D(SamplerColor, coord.xy + float2(-fvTexelSize.x, 0.0) * fSharpScale);
	
	Color.a = 1.0;
	return Color;
}

float4 PS_ProcessPass_Anamorphic(VS_OUTPUT_POST IN, float2 vPos : VPOS, uniform int axis) : COLOR0
{
float fFlareBlur =lerp( lerp( fFlareBlur_Day, fFlareBlur_Night, 1 - ENightDayFactor ), fFlareBlur_Interior, EInteriorFactor);
float3 fFlareTint =lerp( lerp( fFlareTint_Day, fFlareTint_Night, 1 - ENightDayFactor ), fFlareTint_Interior, EInteriorFactor);
float fFlareIntensity =lerp( lerp( fFlareIntensity_Day, fFlareIntensity_Night, 1 - ENightDayFactor ), fFlareIntensity_Interior, EInteriorFactor);
//[KYO : axis is separated below in pass p1]

	float4 res;
	float2 coord = IN.txcoord.xy;
	float3 anamFlare = AnamorphicSample(axis, coord.xy, fFlareBlur) * fFlareTint;
	
	res.rgb = anamFlare * fFlareIntensity;
	res.a = 1.0;

#if !(USE_SHARPENING == 1)
	res.rgb += tex2D(SamplerColor, coord.xy).rgb;
#endif
	
	return res;
}

float4 PS_ProcessPass_ImageGrain(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR0
{
	float4 res;
	float2 coord = IN.txcoord.xy;
	res.rgb = tex2D(SamplerColor, coord.xy).rgb;
	res.rgb += tex2D(SamplerNoise, coord.xy * 1024).rgb * Grain(float3(coord.xy, SEED));
	res.a = 1.0;
	return res;
}

float4 PS_ProcessPass2(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float4 res;
	float2 coord=IN.txcoord.xy;

	float4 origcolor=tex2D(SamplerColor, coord.xy);
	float scenedepth=tex2D(SamplerDepth, IN.txcoord.xy).x;
	float scenefocus=tex2D(SamplerFocus, 0.5).x;
	res.xyz=origcolor.xyz;

	float depth=linearlizeDepth(scenedepth);

float fFarBlurCurve =lerp( lerp( fFarBlurCurve_Day, fFarBlurCurve_Night, 1 - ENightDayFactor ), fFarBlurCurve_Interior, EInteriorFactor);
float fFocalPlaneDepth =lerp( lerp( fFocalPlaneDepth_Day, fFocalPlaneDepth_Night, 1 - ENightDayFactor ), fFocalPlaneDepth_Interior, EInteriorFactor);
float fFarBlurDepth =lerp( lerp( fFarBlurDepth_Day, fFarBlurDepth_Night, 1 - ENightDayFactor ), fFarBlurDepth_Interior, EInteriorFactor);

	#if (USE_DYNAMIC_DOF == 1)
		float focalPlaneDepth=scenefocus;
		float farBlurDepth=scenefocus*pow(4.0, fFarBlurCurve);
	#else
		float focalPlaneDepth=fFocalPlaneDepth;
		float farBlurDepth=fFarBlurDepth;
	#endif
	
#if (USE_TILT_SHIFT == 1)
float fTiltShiftAngle =lerp( lerp( fTiltShiftAngle_Day, fTiltShiftAngle_Night, 1 - ENightDayFactor ), fTiltShiftAngle_Interior, EInteriorFactor);
		float shiftAngle=(frac(fTiltShiftAngle / 90.0) == 0) ? 0.0 : fTiltShiftAngle;
		float depthShift=1.0 + (0.5 - coord.x)*tan(-shiftAngle * 0.017453292);
		focalPlaneDepth*=depthShift;
		farBlurDepth*=depthShift;
	#endif
	
	
	if(depth < focalPlaneDepth)
		res.w=(depth - focalPlaneDepth)/focalPlaneDepth;
	else
	{
		res.w=(depth - focalPlaneDepth)/(farBlurDepth - focalPlaneDepth);
		res.w=saturate(res.w);
	}

	res.w=res.w * 0.5 + 0.5;
	
	#if (USE_NOT_BLURRING_SKY == 1)
		#define	DEPTH_OF_FIELD_QUALITY 2	
		//#define DEPTH_OF_FIELD_QUALITY 0
		res.w=(depth > 1000.0) ? 0.5 : res.w;
	#endif
	
	return res;
}

#if (USE_ENB_ADAPTIVE_QUALITY == 1)
float4 PS_ProcessPass3(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float4 res;
	
	float2 coord=IN.txcoord.xy;

	float4 origcolor=tex2D(SamplerColor, coord.xy);
	
	float centerDepth=origcolor.w;

	float2 pixelSize=ScreenSize.y;
	pixelSize.y*=ScreenSize.z;
	float blurAmount=abs(centerDepth * 2.0 - 1.0);
	float discRadius=blurAmount * MaxDepthOfFieldQuality;

float fPolygons =lerp( lerp( fPolygons_Day, fPolygons_Night, 1 - ENightDayFactor ), fPolygons_Interior, EInteriorFactor);

float fRadiusScaleMultiplier =lerp( lerp( fRadiusScaleMultiplier_Day, fRadiusScaleMultiplier_Night, 1 - ENightDayFactor ), fRadiusScaleMultiplier_Interior, EInteriorFactor);	
float fNearBlurCurve =lerp( lerp( fNearBlurCurve_Day, fNearBlurCurve_Night, 1 - ENightDayFactor ), fNearBlurCurve_Interior, EInteriorFactor);
float fBokehBias =lerp( lerp( fBokehBias_Day, fBokehBias_Night, 1 - ENightDayFactor ), fBokehBias_Interior, EInteriorFactor);
float fBokehBrightnessThreshold =lerp( lerp( fBokehBrightnessThreshold_Day, fBokehBrightnessThreshold_Night, 1 - ENightDayFactor ), fBokehBrightnessThreshold_Interior, EInteriorFactor);
float fBokehBrightnessMultiplier =lerp( lerp( fBokehBrightnessMultiplier_Day, fBokehBrightnessMultiplier_Night, 1 - ENightDayFactor ), fBokehBrightnessMultiplier_Interior, EInteriorFactor);
float fBokehBiasCurve =lerp( lerp( fBokehBiasCurve_Day, fBokehBiasCurve_Night, 1 - ENightDayFactor ), fBokehBiasCurve_Interior, EInteriorFactor);

float fIntensityFactor_BokehBright =lerp( lerp( fIntensityFactor_BokehBright_Day, fIntensityFactor_BokehBright_Night, 1 - ENightDayFactor ), fIntensityFactor_BokehBright_Interior, EInteriorFactor);
float fFrequency_BokehBright =lerp( lerp( fFrequency_BokehBright_Day, fFrequency_BokehBright_Night, 1 - ENightDayFactor ), fFrequency_BokehBright_Interior, EInteriorFactor);
float fAmplitude_BokehBright =lerp( lerp( fAmplitude_BokehBright_Day, fAmplitude_BokehBright_Night, 1 - ENightDayFactor ), fAmplitude_BokehBright_Interior, EInteriorFactor);
float fTimeFactor_BokehBright =lerp( lerp( fTimeFactor_BokehBright_Day, fTimeFactor_BokehBright_Night, 1 - ENightDayFactor ), fTimeFactor_BokehBright_Interior, EInteriorFactor);
float TimeFactorBokehBright =(0.5 * sin(2 * PI * fFrequency_BokehBright * Timer.x * (fIntensityFactor_BokehBright * fTimeFactor_BokehBright)) + 0.75) * fAmplitude_BokehBright;	//+ 0.5 Non-Limiter

float fIntensityFactor_BokehThreshold =lerp( lerp( fIntensityFactor_BokehThreshold_Day, fIntensityFactor_BokehThreshold_Night, 1 - ENightDayFactor ), fIntensityFactor_BokehThreshold_Interior, EInteriorFactor);
float fFrequency_BokehThreshold =lerp( lerp( fFrequency_BokehThreshold_Day, fFrequency_BokehThreshold_Night, 1 - ENightDayFactor ), fFrequency_BokehThreshold_Interior, EInteriorFactor);
float fAmplitude_BokehThreshold =lerp( lerp( fAmplitude_BokehThreshold_Day, fAmplitude_BokehThreshold_Night, 1 - ENightDayFactor ), fAmplitude_BokehThreshold_Interior, EInteriorFactor);
float fTimeFactor_BokehThreshold =lerp( lerp( fTimeFactor_BokehThreshold_Day, fTimeFactor_BokehThreshold_Night, 1 - ENightDayFactor ), fTimeFactor_BokehThreshold_Interior, EInteriorFactor);
float TimeFactorBokehThreshold =(0.5 * sin(2 * PI * fFrequency_BokehThreshold * Timer.x * (fIntensityFactor_BokehThreshold * fTimeFactor_BokehThreshold)) + 0.75) * fAmplitude_BokehThreshold;	//+ 0.5 Non-Limiter

float fIntensityFactor_RadiusScale =lerp( lerp( fIntensityFactor_RadiusScale_Day, fIntensityFactor_RadiusScale_Night, 1 - ENightDayFactor ), fIntensityFactor_RadiusScale_Interior, EInteriorFactor);
float fFrequency_RadiusScale =lerp( lerp( fFrequency_RadiusScale_Day, fFrequency_RadiusScale_Night, 1 - ENightDayFactor ), fFrequency_RadiusScale_Interior, EInteriorFactor);
float fAmplitude_RadiusScale =lerp( lerp( fAmplitude_RadiusScale_Day, fAmplitude_RadiusScale_Night, 1 - ENightDayFactor ), fAmplitude_RadiusScale_Interior, EInteriorFactor);
float fTimeFactor_RadiusScale =lerp( lerp( fTimeFactor_RadiusScale_Day, fTimeFactor_RadiusScale_Night, 1 - ENightDayFactor ), fTimeFactor_RadiusScale_Interior, EInteriorFactor);
float TimeFactorRadiusScale =(0.5 * sin(2 * PI * fFrequency_RadiusScale * Timer.x * (fIntensityFactor_RadiusScale * fTimeFactor_RadiusScale)) + 0.75) * fAmplitude_RadiusScale;	//+ 0.5 Non-Limiter

#if (USE_DYNAMIC_BOKEH_BRIGHT_PUSH == 1)
	fBokehBrightnessMultiplier=fBokehBrightnessMultiplier + TimeFactorBokehBright;
#endif
#if (USE_DYNAMIC_BOKEH_BRIGHT_PULL == 1)
	fBokehBrightnessMultiplier=fBokehBrightnessMultiplier - TimeFactorBokehBright;
#endif	
#if (USE_DYNAMIC_BOKEH_THRESH_PUSH == 1)
	fBokehBrightnessThreshold=fBokehBrightnessThreshold - TimeFactorBokehThreshold;
#endif
#if (USE_DYNAMIC_BOKEH_THRESH_PULL == 1)
	fBokehBrightnessThreshold=fBokehBrightnessThreshold + TimeFactorBokehThreshold;
#endif
#if (USE_DYNAMIC_RADIUS_SCALE_PUSH == 1)
	fRadiusScaleMultiplier=fRadiusScaleMultiplier + TimeFactorRadiusScale;
#endif
#if (USE_DYNAMIC_RADIUS_SCALE_PULL == 1)
	fRadiusScaleMultiplier=fRadiusScaleMultiplier - TimeFactorRadiusScale;
#endif
	
	discRadius*=fRadiusScaleMultiplier;
	
	#if (USE_DYNAMIC_DOF == 1)
	discRadius*=(centerDepth < 0.5) ? (1.0 / max(fNearBlurCurve, 1.0)) : 1.0;
	#endif
		
	res.xyz=origcolor.xyz;
	

	res.w=dot(res.xyz, 0.3333);
	res.w=max((res.w - fBokehBrightnessThreshold) * fBokehBrightnessMultiplier, 0.0);
	res.xyz*=1.0 + res.w*blurAmount;
	
	res.w=1.0;
	

	int dofQuality = MaxDepthOfFieldQuality;

	if (discRadius <= 0.2)
		dofQuality = 1;
	else if (discRadius <= 1.0)
		dofQuality = min(dofQuality, 2);
	else if (discRadius <= 2.3)
		dofQuality = min(dofQuality, 3);

	int	dofTaps = dofQuality * (dofQuality + 1) / 2.0;

	// adaptive quality
	if (dofQuality > 1 && EAdaptiveQualityFactor > 0.01)
	{
		float changeFactor = EAdaptiveQualityFactor * 4.0;
		float requestedDof = dofQuality - changeFactor;
		requestedDof = max(requestedDof, 1);
		
		dofTaps = requestedDof * (requestedDof + 1) / 2.0;
	}

	int loopNumber = dofQuality;

	float basedAngle = 360.0 / fPolygons;
	int inPartAngle = 0;

	float dofQualityRecip = 1.0f / float(dofQuality);
	float2 discRadiusInPixels = (pixelSize.xy * discRadius);

	int minLoopNumber = 0;

	[loop] 
	for(int i = 0; i < dofTaps; i++)
	{
		int numberOfSamplesInLoop = loopNumber * fPolygons;
		float inPartAngleStep = 360.0f / float(numberOfSamplesInLoop);
		float radiusCoeff = float(loopNumber) * dofQualityRecip;
		float powerCoeff = 1.0 + fBokehBias * pow(radiusCoeff, fBokehBiasCurve);

		float remainAngle = inPartAngleStep * ((inPartAngle * 17) % loopNumber);

		// sampling for each sixth

		// 0 -> 1
		float2 sampleOffset = lerp(float2(0.5f, 0.866025403f), float2(-0.5f, 0.866025403f), remainAngle / basedAngle) * radiusCoeff;

		float2 coordLow=coord.xy + (sampleOffset.xy * discRadiusInPixels);
		float4 tap=tex2Dlod(SamplerColor, float4(coordLow.x, coordLow.y, 0.0, 0.0));
		
		float weight = (tap.w >= centerDepth) ? 1.0 : abs(tap.w * 2.0 - 1.0);

		float luma = dot(tap.xyz, 0.3333);
		float brightMultiplier = max((luma - fBokehBrightnessThreshold) * fBokehBrightnessMultiplier, 0.0);
		tap.xyz *= 1.0 + brightMultiplier * abs(tap.w * 2.0 - 1.0);
		tap.xyz *= powerCoeff;
		
		res.xyz += tap.xyz * weight;
		res.w += weight;

		// 1 -> 2
		sampleOffset = lerp(float2(-0.5f, 0.866025403f), float2(-1.0f, 0.0f), remainAngle / basedAngle) * radiusCoeff;

		coordLow=coord.xy + (sampleOffset.xy * discRadiusInPixels);
		tap=tex2Dlod(SamplerColor, float4(coordLow.x, coordLow.y, 0.0, 0.0));
		
		weight = (tap.w >= centerDepth) ? 1.0 : abs(tap.w * 2.0 - 1.0);

		luma = dot(tap.xyz, 0.3333);
		brightMultiplier = max((luma - fBokehBrightnessThreshold) * fBokehBrightnessMultiplier, 0.0);
		tap.xyz *= 1.0 + brightMultiplier * abs(tap.w * 2.0 - 1.0);
		tap.xyz *= powerCoeff;
		
		res.xyz += tap.xyz * weight;
		res.w += weight;

		// 2 -> 3
		sampleOffset = lerp(float2(-1.0f, 0.0f), float2(-0.5f, -0.866025403f), remainAngle / basedAngle) * radiusCoeff;

		coordLow=coord.xy + (sampleOffset.xy * discRadiusInPixels);
		tap=tex2Dlod(SamplerColor, float4(coordLow.x, coordLow.y, 0.0, 0.0));
		
		weight = (tap.w >= centerDepth) ? 1.0 : abs(tap.w * 2.0 - 1.0);

		luma = dot(tap.xyz, 0.3333);
		brightMultiplier = max((luma - fBokehBrightnessThreshold) * fBokehBrightnessMultiplier, 0.0);
		tap.xyz *= 1.0 + brightMultiplier * abs(tap.w * 2.0 - 1.0);
		tap.xyz *= powerCoeff;
		
		res.xyz += tap.xyz * weight;
		res.w += weight;

		// 3 -> 4
		sampleOffset = lerp(float2(-0.5f, -0.866025403f), float2(0.5f, -0.866025403f), remainAngle / basedAngle) * radiusCoeff;

		coordLow=coord.xy + (sampleOffset.xy * discRadiusInPixels);
		tap=tex2Dlod(SamplerColor, float4(coordLow.x, coordLow.y, 0.0, 0.0));
		
		weight = (tap.w >= centerDepth) ? 1.0 : abs(tap.w * 2.0 - 1.0);

		luma = dot(tap.xyz, 0.3333);
		brightMultiplier = max((luma - fBokehBrightnessThreshold) * fBokehBrightnessMultiplier, 0.0);
		tap.xyz *= 1.0 + brightMultiplier * abs(tap.w * 2.0 - 1.0);
		tap.xyz *= powerCoeff;
		
		res.xyz += tap.xyz * weight;
		res.w += weight;

		// 4 -> 5
		sampleOffset = lerp(float2(0.5f, -0.866025403f), float2(1.0f, 0.0f), remainAngle / basedAngle) * radiusCoeff;

		coordLow=coord.xy + (sampleOffset.xy * discRadiusInPixels);
		tap=tex2Dlod(SamplerColor, float4(coordLow.x, coordLow.y, 0.0, 0.0));
		
		weight = (tap.w >= centerDepth) ? 1.0 : abs(tap.w * 2.0 - 1.0);

		luma = dot(tap.xyz, 0.3333);
		brightMultiplier = max((luma - fBokehBrightnessThreshold) * fBokehBrightnessMultiplier, 0.0);
		tap.xyz *= 1.0 + brightMultiplier * abs(tap.w * 2.0 - 1.0);
		tap.xyz *= powerCoeff;
		
		res.xyz += tap.xyz * weight;
		res.w += weight;

		// 5 -> 0
		sampleOffset = lerp(float2(1.0f, 0.0f), float2(0.5f, 0.866025403f), remainAngle / basedAngle) * radiusCoeff;

		coordLow=coord.xy + (sampleOffset.xy * discRadiusInPixels);
		tap=tex2Dlod(SamplerColor, float4(coordLow.x, coordLow.y, 0.0, 0.0));
		
		weight = (tap.w >= centerDepth) ? 1.0 : abs(tap.w * 2.0 - 1.0);

		luma = dot(tap.xyz, 0.3333);
		brightMultiplier = max((luma - fBokehBrightnessThreshold) * fBokehBrightnessMultiplier, 0.0);
		tap.xyz *= 1.0 + brightMultiplier * abs(tap.w * 2.0 - 1.0);
		tap.xyz *= powerCoeff;
		
		res.xyz += tap.xyz * weight;
		res.w += weight;


		--loopNumber;
		if (loopNumber == minLoopNumber)
		{
			loopNumber = dofQuality;
			inPartAngle += 1;
			++minLoopNumber;
		}
	}

	res.xyz /= res.w;
	res.w=centerDepth;

	return res;
}
#endif

#if (USE_ENB_ADAPTIVE_QUALITY == 0)
float4 PS_ProcessPass3(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float4 res;
	
	float2 coord=IN.txcoord.xy;

	float4 origcolor=tex2D(SamplerColor, coord.xy);
	
	float centerDepth=origcolor.w;

	
	float2 pixelSize=ScreenSize.y;
	pixelSize.y*=ScreenSize.z;
	
	float blurAmount=abs(centerDepth * 2.0 - 1.0);
	float discRadius=blurAmount * float(DEPTH_OF_FIELD_QUALITY);

float fRadiusScaleMultiplier =lerp( lerp( fRadiusScaleMultiplier_Day, fRadiusScaleMultiplier_Night, 1 - ENightDayFactor ), fRadiusScaleMultiplier_Interior, EInteriorFactor);	
float fNearBlurCurve =lerp( lerp( fNearBlurCurve_Day, fNearBlurCurve_Night, 1 - ENightDayFactor ), fNearBlurCurve_Interior, EInteriorFactor);
float fBokehBias =lerp( lerp( fBokehBias_Day, fBokehBias_Night, 1 - ENightDayFactor ), fBokehBias_Interior, EInteriorFactor);
float fBokehBrightnessThreshold =lerp( lerp( fBokehBrightnessThreshold_Day, fBokehBrightnessThreshold_Night, 1 - ENightDayFactor ), fBokehBrightnessThreshold_Interior, EInteriorFactor);
float fBokehBrightnessMultiplier =lerp( lerp( fBokehBrightnessMultiplier_Day, fBokehBrightnessMultiplier_Night, 1 - ENightDayFactor ), fBokehBrightnessMultiplier_Interior, EInteriorFactor);
float fBokehBiasCurve =lerp( lerp( fBokehBiasCurve_Day, fBokehBiasCurve_Night, 1 - ENightDayFactor ), fBokehBiasCurve_Interior, EInteriorFactor);

float fIntensityFactor_BokehBright =lerp( lerp( fIntensityFactor_BokehBright_Day, fIntensityFactor_BokehBright_Night, 1 - ENightDayFactor ), fIntensityFactor_BokehBright_Interior, EInteriorFactor);
float fFrequency_BokehBright =lerp( lerp( fFrequency_BokehBright_Day, fFrequency_BokehBright_Night, 1 - ENightDayFactor ), fFrequency_BokehBright_Interior, EInteriorFactor);
float fAmplitude_BokehBright =lerp( lerp( fAmplitude_BokehBright_Day, fAmplitude_BokehBright_Night, 1 - ENightDayFactor ), fAmplitude_BokehBright_Interior, EInteriorFactor);
float fTimeFactor_BokehBright =lerp( lerp( fTimeFactor_BokehBright_Day, fTimeFactor_BokehBright_Night, 1 - ENightDayFactor ), fTimeFactor_BokehBright_Interior, EInteriorFactor);
float TimeFactorBokehBright =(0.5 * sin(2 * PI * fFrequency_BokehBright * Timer.x * (fIntensityFactor_BokehBright * fTimeFactor_BokehBright)) + 0.75) * fAmplitude_BokehBright;	//+ 0.5 Non-Limiter

float fIntensityFactor_BokehThreshold =lerp( lerp( fIntensityFactor_BokehThreshold_Day, fIntensityFactor_BokehThreshold_Night, 1 - ENightDayFactor ), fIntensityFactor_BokehThreshold_Interior, EInteriorFactor);
float fFrequency_BokehThreshold =lerp( lerp( fFrequency_BokehThreshold_Day, fFrequency_BokehThreshold_Night, 1 - ENightDayFactor ), fFrequency_BokehThreshold_Interior, EInteriorFactor);
float fAmplitude_BokehThreshold =lerp( lerp( fAmplitude_BokehThreshold_Day, fAmplitude_BokehThreshold_Night, 1 - ENightDayFactor ), fAmplitude_BokehThreshold_Interior, EInteriorFactor);
float fTimeFactor_BokehThreshold =lerp( lerp( fTimeFactor_BokehThreshold_Day, fTimeFactor_BokehThreshold_Night, 1 - ENightDayFactor ), fTimeFactor_BokehThreshold_Interior, EInteriorFactor);
float TimeFactorBokehThreshold =(0.5 * sin(2 * PI * fFrequency_BokehThreshold * Timer.x * (fIntensityFactor_BokehThreshold * fTimeFactor_BokehThreshold)) + 0.75) * fAmplitude_BokehThreshold;	//+ 0.5 Non-Limiter

float fIntensityFactor_RadiusScale =lerp( lerp( fIntensityFactor_RadiusScale_Day, fIntensityFactor_RadiusScale_Night, 1 - ENightDayFactor ), fIntensityFactor_RadiusScale_Interior, EInteriorFactor);
float fFrequency_RadiusScale =lerp( lerp( fFrequency_RadiusScale_Day, fFrequency_RadiusScale_Night, 1 - ENightDayFactor ), fFrequency_RadiusScale_Interior, EInteriorFactor);
float fAmplitude_RadiusScale =lerp( lerp( fAmplitude_RadiusScale_Day, fAmplitude_RadiusScale_Night, 1 - ENightDayFactor ), fAmplitude_RadiusScale_Interior, EInteriorFactor);
float fTimeFactor_RadiusScale =lerp( lerp( fTimeFactor_RadiusScale_Day, fTimeFactor_RadiusScale_Night, 1 - ENightDayFactor ), fTimeFactor_RadiusScale_Interior, EInteriorFactor);
float TimeFactorRadiusScale =(0.5 * sin(2 * PI * fFrequency_RadiusScale * Timer.x * (fIntensityFactor_RadiusScale * fTimeFactor_RadiusScale)) + 0.75) * fAmplitude_RadiusScale;	//+ 0.5 Non-Limiter

#if (USE_DYNAMIC_BOKEH_BRIGHT_PUSH == 1)
	fBokehBrightnessMultiplier=fBokehBrightnessMultiplier + TimeFactorBokehBright;
#endif
#if (USE_DYNAMIC_BOKEH_BRIGHT_PULL == 1)
	fBokehBrightnessMultiplier=fBokehBrightnessMultiplier - TimeFactorBokehBright;
#endif	
#if (USE_DYNAMIC_BOKEH_THRESH_PUSH == 1)
	fBokehBrightnessThreshold=fBokehBrightnessThreshold - TimeFactorBokehThreshold;
#endif
#if (USE_DYNAMIC_BOKEH_THRESH_PULL == 1)
	fBokehBrightnessThreshold=fBokehBrightnessThreshold + TimeFactorBokehThreshold;
#endif
#if (USE_DYNAMIC_RADIUS_SCALE_PUSH == 1)
	fRadiusScaleMultiplier=fRadiusScaleMultiplier + TimeFactorRadiusScale;
#endif
#if (USE_DYNAMIC_RADIUS_SCALE_PULL == 1)
	fRadiusScaleMultiplier=fRadiusScaleMultiplier - TimeFactorRadiusScale;
#endif

	discRadius*=fRadiusScaleMultiplier;
	
	#if (USE_DYNAMIC_DOF == 1)
	discRadius*=(centerDepth < 0.5) ? (1.0 / max(fNearBlurCurve, 1.0)) : 1.0;
	#endif
		
	res.xyz=origcolor.xyz;
	res.w=dot(res.xyz, 0.3333);
	res.w=max((res.w - fBokehBrightnessThreshold) * fBokehBrightnessMultiplier, 0.0);
	res.xyz*=1.0 + res.w*blurAmount;
	
	res.w=1.0;

float fPolygonRotation = lerp( lerp( fPolygonRotation_Day, fPolygonRotation_Night, 1 - ENightDayFactor ), fPolygonRotation_Interior, EInteriorFactor);	
float fRotationSpeed = lerp( lerp( fRotationSpeed_Day, fRotationSpeed_Night, 1 - ENightDayFactor ), fRotationSpeed_Interior, EInteriorFactor);	
float fRotationFactor = abs(frac(Timer.x * ( 100.0 * fRotationSpeed))* 2.0 - 1.0);
	
	int sampleCycle=0;
	int sampleCycleCounter=0;
	int sampleCounterInCycle=0;

	#if (USE_POLYGONAL_BOKEH == 2)
		float basedAngle=360.0 / fPolygon_Number;
	#if (USE_DYNAMIC_ROTATION == 1)
		float polygonRotation = 90.0 + (fPolygonRotation * fRotationFactor);
	#else
		float polygonRotation = 90.0 + fPolygonRotation;
	#endif	
		float2 currentVertex;
		float2 nextVertex;
	
		int	dofTaps=DEPTH_OF_FIELD_QUALITY * (DEPTH_OF_FIELD_QUALITY + 1) * fPolygon_Number / 2.0;
	#else

	#if (USE_POLYGONAL_BOKEH == 1)
		float basedAngle=360.0 / fPolygon_Numb;
		float2 currentVertex;
		float2 nextVertex;
	
		int	dofTaps=DEPTH_OF_FIELD_QUALITY * (DEPTH_OF_FIELD_QUALITY + 1) * fPolygon_Numb / 2.0;
	#else
		int	dofTaps=DEPTH_OF_FIELD_QUALITY * (DEPTH_OF_FIELD_QUALITY + 1) * 4;
	#endif
       #endif		
	
	for(int i=0; i < dofTaps; i++)
	{
		if(sampleCounterInCycle % sampleCycle == 0) 
		{
			sampleCounterInCycle=0;
			sampleCycleCounter++;
		
			#if (USE_POLYGONAL_BOKEH == 1)
				sampleCycle+=fPolygon_Numb;
				currentVertex.xy=float2(1.0 , 0.0);
				sincos(basedAngle* 0.017453292, nextVertex.y, nextVertex.x);	

			#else

			#if (USE_POLYGONAL_BOKEH == 2)

				sampleCycle+=fPolygon_Number;
				currentVertex.xy=float2(1.0 , 0.0);
				sincos((polygonRotation) * PI_OVER_180, currentVertex.y, currentVertex.x); //rotate bokeh
				sincos((basedAngle + polygonRotation) * PI_OVER_180, nextVertex.y, nextVertex.x);	

			#else	
				sampleCycle+=8;
			#endif
		       #endif
		}
		sampleCounterInCycle++;
		
		#if (USE_POLYGONAL_BOKEH == 2)
			float sampleAngle=basedAngle / float(sampleCycleCounter) * sampleCounterInCycle;
			float remainAngle=frac(sampleAngle / basedAngle) * basedAngle;
		
			if(remainAngle == 0)
			{
				currentVertex=nextVertex;
				sincos((sampleAngle +  basedAngle + polygonRotation) * PI_OVER_180, nextVertex.y, nextVertex.x);
			}

			float2 sampleOffset=lerp(currentVertex.xy, nextVertex.xy, remainAngle / basedAngle);
		#else
	
		#if (USE_POLYGONAL_BOKEH == 1)
			float sampleAngle=basedAngle / float(sampleCycleCounter) * sampleCounterInCycle;
			float remainAngle=frac(sampleAngle / basedAngle) * basedAngle;
		
			if(remainAngle == 0)
			{
				currentVertex=nextVertex;
				sincos((sampleAngle +  basedAngle) * 0.017453292, nextVertex.y, nextVertex.x);
			}

			float2 sampleOffset=lerp(currentVertex.xy, nextVertex.xy, remainAngle / basedAngle);
		#else
			float sampleAngle=0.78539816 / float(sampleCycleCounter) * sampleCounterInCycle;
			float2 sampleOffset;
			sincos(sampleAngle, sampleOffset.y, sampleOffset.x);
		#endif
	       #endif
		
		sampleOffset*=sampleCycleCounter / float(DEPTH_OF_FIELD_QUALITY);
		float2  coordLow=coord.xy + (pixelSize.xy * sampleOffset.xy * discRadius);
		float4 tap=tex2D(SamplerColor, coordLow.xy);
		
		float weight=(tap.w >= centerDepth) ? 1.0 : abs(tap.w * 2.0 - 1.0);
		
		float luma=dot(tap.xyz, 0.3333);
		float brightMultiplier=max((luma - fBokehBrightnessThreshold) * fBokehBrightnessMultiplier, 0.0);
		tap.xyz*=1.0 + brightMultiplier*abs(tap.w*2.0 - 1.0);
		
		tap.xyz*=1.0 + fBokehBias * pow(float(sampleCycleCounter)/float(DEPTH_OF_FIELD_QUALITY), fBokehBiasCurve);
		
	    res.xyz+=tap.xyz * weight;
	    res.w+=weight;
	}

	res.xyz /= res.w;
		
	res.w=centerDepth;


	return res;
}
#endif

float4 PS_ProcessPass4(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
float fRadiusScaleMultiplier =lerp( lerp( fRadiusScaleMultiplier_Day, fRadiusScaleMultiplier_Night, 1 - ENightDayFactor ), fRadiusScaleMultiplier_Interior, EInteriorFactor);	

float fIntensityFactor_RadiusScale =lerp( lerp( fIntensityFactor_RadiusScale_Day, fIntensityFactor_RadiusScale_Night, 1 - ENightDayFactor ), fIntensityFactor_RadiusScale_Interior, EInteriorFactor);
float fFrequency_RadiusScale =lerp( lerp( fFrequency_RadiusScale_Day, fFrequency_RadiusScale_Night, 1 - ENightDayFactor ), fFrequency_RadiusScale_Interior, EInteriorFactor);
float fAmplitude_RadiusScale =lerp( lerp( fAmplitude_RadiusScale_Day, fAmplitude_RadiusScale_Night, 1 - ENightDayFactor ), fAmplitude_RadiusScale_Interior, EInteriorFactor);
float fTimeFactor_RadiusScale =lerp( lerp( fTimeFactor_RadiusScale_Day, fTimeFactor_RadiusScale_Night, 1 - ENightDayFactor ), fTimeFactor_RadiusScale_Interior, EInteriorFactor);
float TimeFactorRadiusScale =(0.5 * sin(2 * PI * fFrequency_RadiusScale * Timer.x * (fIntensityFactor_RadiusScale * fTimeFactor_RadiusScale)) + 0.75) * fAmplitude_RadiusScale;	//+ 0.5 Non-Limiter

#if (USE_DYNAMIC_RADIUS_SCALE_PUSH == 1)
	fRadiusScaleMultiplier=fRadiusScaleMultiplier + TimeFactorRadiusScale;
#endif
#if (USE_DYNAMIC_RADIUS_SCALE_PULL == 1)
	fRadiusScaleMultiplier=fRadiusScaleMultiplier - TimeFactorRadiusScale;
#endif
	float2 coord=IN.txcoord.xy;	
	float2 pixelSize=ScreenSize.y;
	pixelSize.y*=ScreenSize.z;
	
	float4 origcolor=tex2D(SamplerColor, coord.xy);
	float depth=origcolor.w;
	float blurAmount=abs(depth * 2.0 - 1.0);
	float discRadius=blurAmount * float(DEPTH_OF_FIELD_QUALITY) * fRadiusScaleMultiplier;

float fNearBlurCurve =lerp( lerp( fNearBlurCurve_Day, fNearBlurCurve_Night, 1 - ENightDayFactor ), fNearBlurCurve_Interior, EInteriorFactor);	

	#if (USE_DYNAMIC_DOF == 1)
		discRadius*=(depth < 0.5) ? (1.0 / max(fNearBlurCurve, 1.0)) : 1.0;
	#endif

	float4 res=origcolor;

#if (Use_Chroma_Formula_Day == 1)
	float3 distortion_Day=float3(-1.0, 0.0, 1.0);
#elif (Use_Chroma_Formula_Day == 2)
	float3 distortion_Day=float3(-1.333, 0.0, 1.333);
#elif (Use_Chroma_Formula_Day == 3)
	float3 distortion_Day=float3(-0.777, 0.0, 0.777);
#elif (Use_Chroma_Formula_Day == 4)
	float3 distortion_Day=float3(-0.5, 0.0, 0.5);
#elif (Use_Chroma_Formula_Day == 5)
	float3 distortion_Day=float3(-1.0, 0.0, 0.333);
#elif (Use_Chroma_Formula_Day == 6)
	float3 distortion_Day=float3(-0.333, 0.0, 1.0);
#elif (Use_Chroma_Formula_Day == 7)
	float3 distortion_Day=float3(-0.333, 0.777, 0.333);
#elif (Use_Chroma_Formula_Day == 8)
	float3 distortion_Day=float3(0.9995, 1.000, 1.0005);
#elif (Use_Chroma_Formula_Day == 9)
	float3 distortion_Day=float3(-0.7995, 0.222, 1.0005);
#elif (Use_Chroma_Formula_Day == 10)
	float3 distortion_Day=float3(-0.282, 0.122, 0.222);
#endif

#if (Use_Chroma_Formula_Night == 1)
	float3 distortion_Night=float3(-1.0, 0.0, 1.0);
#elif (Use_Chroma_Formula_Night == 2)
	float3 distortion_Night=float3(-1.333, 0.0, 1.333);
#elif (Use_Chroma_Formula_Night == 3)
	float3 distortion_Night=float3(-0.777, 0.0, 0.777);
#elif (Use_Chroma_Formula_Night == 4)
	float3 distortion_Night=float3(-0.5, 0.0, 0.5);
#elif (Use_Chroma_Formula_Night == 5)
	float3 distortion_Night=float3(-1.0, 0.0, 0.333);
#elif (Use_Chroma_Formula_Night == 6)
	float3 distortion_Night=float3(-0.333, 0.0, 1.0);
#elif (Use_Chroma_Formula_Night == 7)
	float3 distortion_Night=float3(-0.333, 0.777, 0.333);
#elif (Use_Chroma_Formula_Night == 8)
	float3 distortion_Night=float3(0.9995, 1.000, 1.0005);
#elif (Use_Chroma_Formula_Night == 9)
	float3 distortion_Night=float3(-0.7995, 0.222, 1.0005);
#elif (Use_Chroma_Formula_Night == 10)
	float3 distortion_Night=float3(-0.282, 0.122, 0.222);
#endif

#if (Use_Chroma_Formula_Interior == 1)
	float3 distortion_Interior=float3(-1.0, 0.0, 1.0);
#elif (Use_Chroma_Formula_Interior == 2)
	float3 distortion_Interior=float3(-1.333, 0.0, 1.333);
#elif (Use_Chroma_Formula_Interior == 3)
	float3 distortion_Interior=float3(-0.777, 0.0, 0.777);
#elif (Use_Chroma_Formula_Interior == 4)
	float3 distortion_Interior=float3(-0.5, 0.0, 0.5);
#elif (Use_Chroma_Formula_Interior == 5)
	float3 distortion_Interior=float3(-1.0, 0.0, 0.333);
#elif (Use_Chroma_Formula_Interior == 6)
	float3 distortion_Interior=float3(-0.333, 0.0, 1.0);
#elif (Use_Chroma_Formula_Interior == 7)
	float3 distortion_Interior=float3(-0.333, 0.777, 0.333);
#elif (Use_Chroma_Formula_Interior == 8)
	float3 distortion_Interior=float3(0.9995, 1.000, 1.0005);
#elif (Use_Chroma_Formula_Interior == 9)
	float3 distortion_Interior=float3(-0.7995, 0.222, 1.0005);
#elif (Use_Chroma_Formula_Interior == 10)
	float3 distortion_Interior=float3(-0.222, 0.122, 0.222);
#endif

/**
 *	KYO : add your own RGBs Displacements above
 */

float3 distortion =lerp( lerp( distortion_Day, distortion_Night, 1 - ENightDayFactor ), distortion_Interior, EInteriorFactor);
float fChromaticAberration =lerp( lerp( fChromaticAberration_Day, fChromaticAberration_Night, 1 - ENightDayFactor ), fChromaticAberration_Interior, EInteriorFactor);

//	+ abs(frac(Timer.x * 20000.0)* 2.0 - 1.0)	//KYO : Memo Formula

float fFrequency_Chroma =lerp( lerp( fFrequency_Chroma_Day, fFrequency_Chroma_Night, 1 - ENightDayFactor ), fFrequency_Chroma_Interior, EInteriorFactor);
float fAmplitude_Chroma =lerp( lerp( fAmplitude_Chroma_Day, fAmplitude_Chroma_Night, 1 - ENightDayFactor ), fAmplitude_Chroma_Interior, EInteriorFactor);
float fTimeFactor_Chroma =lerp( lerp( fTimeFactor_Chroma_Day, fTimeFactor_Chroma_Night, 1 - ENightDayFactor ), fTimeFactor_Chroma_Interior, EInteriorFactor);
float fIntensityFactor_Chroma =lerp( lerp( fIntensityFactor_Chroma_Day, fIntensityFactor_Chroma_Night, 1 - ENightDayFactor ), fIntensityFactor_Chroma_Interior, EInteriorFactor);
float TimeFactorChroma =(0.5 * sin(2 * PI * fFrequency_Chroma * Timer.x * (fIntensityFactor_Chroma * fTimeFactor_Chroma)) + 0.75) * fAmplitude_Chroma;	//+ 0.5 Non-Limiter

#if (USE_DYNAMIC_CHROMA_PUSH == 1)
	distortion*=(fChromaticAberration + TimeFactorChroma)*discRadius;
#endif

#if (USE_DYNAMIC_CHROMA_PULL == 1)
	distortion*=(fChromaticAberration - TimeFactorChroma)*discRadius;
#endif
	distortion*=fChromaticAberration*discRadius;

	origcolor=tex2D(SamplerColor, coord.xy + pixelSize.xy*distortion.x);
	origcolor.w=smoothstep(0.0, depth, origcolor.w);
	res.x=lerp(res.x, origcolor.x, origcolor.w);
	
	origcolor=tex2D(SamplerColor, coord.xy + pixelSize.xy*distortion.z);
	origcolor.w=smoothstep(0.0, depth, origcolor.w);
	res.z=lerp(res.z, origcolor.z, origcolor.w);

	return res;
}

float4 PS_ProcessPass5(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float2 coord=IN.txcoord.xy;
	
	float2 pixelSize=ScreenSize.y;
	pixelSize.y*=ScreenSize.z;
	
	float4 origcolor=tex2D(SamplerColor, coord.xy);
	float depth=origcolor.w;
	float blurAmount=abs(depth*2.0 - 1.0);

float fNearBlurCurve =lerp( lerp( fNearBlurCurve_Day, fNearBlurCurve_Night, 1 - ENightDayFactor ), fNearBlurCurve_Interior, EInteriorFactor);	

	#if (DEPTH_OF_FIELD_QUALITY > 0)
		#if (USE_DYNAMIC_DOF == 1)
			blurAmount*=(depth < 0.5) ? (1.0 / max(fNearBlurCurve, 1.0)) : 1.0;
		#endif
		blurAmount=smoothstep(0.15, 1.0, blurAmount);
	#endif
	
	float weight[5] = {0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 
		0.0162162162};
	
	float4 res=origcolor * weight[0];
	
	for(int i=1; i < 5; i++)
	{
		res+=tex2D(SamplerColor, coord.xy + float2(i*pixelSize.x*blurAmount, 0)) * weight[i];
		res+=tex2D(SamplerColor, coord.xy - float2(i*pixelSize.x*blurAmount, 0)) * weight[i];
	}
	
	res.w=depth;
	
	return res;
}

float4 PS_ProcessPass6(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float2 coord=IN.txcoord.xy;
	
	float2 pixelSize=ScreenSize.y;
	pixelSize.y*=ScreenSize.z;
		
	float4 origcolor=tex2D(SamplerColor, coord.xy);
	float depth=origcolor.w;
	float blurAmount=abs(depth*2.0 - 1.0);

float fNearBlurCurve =lerp( lerp( fNearBlurCurve_Day, fNearBlurCurve_Night, 1 - ENightDayFactor ), fNearBlurCurve_Interior, EInteriorFactor);
	
	#if (DEPTH_OF_FIELD_QUALITY > 0)
		#if (USE_DYNAMIC_DOF == 1)
		blurAmount*=(depth < 0.5) ? (1.0 / max(fNearBlurCurve, 1.0)) : 1.0;
		#endif
		blurAmount=smoothstep(0.15, 1.0, blurAmount);
	#endif
	
	float weight[5] = {0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 
		0.0162162162};
	float4 res=origcolor * weight[0];

	for(int i=1; i < 5; i++)
	{
		res+=tex2D(SamplerColor, coord.xy + float2(0, i*pixelSize.y*blurAmount)) * weight[i];
		res+=tex2D(SamplerColor, coord.xy - float2(0, i*pixelSize.y*blurAmount)) * weight[i];
	}

float fFrequency_Noise =lerp( lerp( fFrequency_Noise_Day, fFrequency_Noise_Night, 1 - ENightDayFactor ), fFrequency_Noise_Interior, EInteriorFactor);
float fAmplitude_Noise =lerp( lerp( fAmplitude_Noise_Day, fAmplitude_Noise_Night, 1 - ENightDayFactor ), fAmplitude_Noise_Interior, EInteriorFactor);
float fTimeFactor_Noise =lerp( lerp( fTimeFactor_Noise_Day, fTimeFactor_Noise_Night, 1 - ENightDayFactor ), fTimeFactor_Noise_Interior, EInteriorFactor);
float fIntensityFactor_Noise =lerp( lerp( fIntensityFactor_Noise_Day, fIntensityFactor_Noise_Night, 1 - ENightDayFactor ), fIntensityFactor_Noise_Interior, EInteriorFactor);
float TimeFactorNoise =(0.5 * sin(2 * PI * fFrequency_Noise * Timer.x * (fIntensityFactor_Noise * fTimeFactor_Noise)) + 0.75) * fAmplitude_Noise;	//+ 0.5 Non-Limiter

float fFrequency_Curve =lerp( lerp( fFrequency_Curve_Day, fFrequency_Curve_Night, 1 - ENightDayFactor ), fFrequency_Curve_Interior, EInteriorFactor);
float fAmplitude_Curve =lerp( lerp( fAmplitude_Curve_Day, fAmplitude_Curve_Night, 1 - ENightDayFactor ), fAmplitude_Curve_Interior, EInteriorFactor);
float fTimeFactor_Curve =lerp( lerp( fTimeFactor_Curve_Day, fTimeFactor_Curve_Night, 1 - ENightDayFactor ), fTimeFactor_Curve_Interior, EInteriorFactor);
float fIntensityFactor_Curve =lerp( lerp( fIntensityFactor_Curve_Day, fIntensityFactor_Curve_Night, 1 - ENightDayFactor ), fIntensityFactor_Curve_Interior, EInteriorFactor);
float TimeFactorCurve =(0.5 * sin(2 * PI * fFrequency_Curve * Timer.x * (fIntensityFactor_Curve * fTimeFactor_Curve)) + 0.75) * fAmplitude_Curve;	//+ 0.5 Non-Limiter

float fFrequency_Diffusion =lerp( lerp( fFrequency_Diffusion_Day, fFrequency_Diffusion_Night, 1 - ENightDayFactor ), fFrequency_Diffusion_Interior, EInteriorFactor);
float fAmplitude_Diffusion =lerp( lerp( fAmplitude_Diffusion_Day, fAmplitude_Diffusion_Night, 1 - ENightDayFactor ), fAmplitude_Diffusion_Interior, EInteriorFactor);
float fTimeFactor_Diffusion =lerp( lerp( fTimeFactor_Diffusion_Day, fTimeFactor_Diffusion_Night, 1 - ENightDayFactor ), fTimeFactor_Diffusion_Interior, EInteriorFactor);
float fIntensityFactor_Diffusion =lerp( lerp( fIntensityFactor_Diffusion_Day, fIntensityFactor_Diffusion_Night, 1 - ENightDayFactor ), fIntensityFactor_Diffusion_Interior, EInteriorFactor);
float TimeFactorDiffusion =(0.5 * sin(2 * PI * fFrequency_Diffusion * Timer.x * (fIntensityFactor_Diffusion * fTimeFactor_Diffusion)) + 0.75) * fAmplitude_Diffusion;	//+ 0.5 Non-Limiter

	#if (Use_Grain_Formula == 1)
	float origgray=max(res.x, res.y);	//dot(res.xyz, 0.333);
	origgray=max(origgray, res.z);
	#else
	#if (Use_Grain_Formula == 2)
	float origgray=dot(res.xyz, 0.3333);
	origgray/=origgray + 1.0;
 	 #endif
	#endif

#if (USE_GRAIN_DISPLACEMENT == 1)
	coord.xy=IN.txcoord.xy*16.0 + frac(Timer.x * 400000.0 + Timer.x);
#endif
	coord.xy=IN.txcoord.xy*16.0 + origgray;
	float4 cnoi=tex2D(SamplerNoise, coord);

float fNoiseAmount =lerp( lerp( fNoiseAmount_Day, fNoiseAmount_Night, 1 - ENightDayFactor ), fNoiseAmount_Interior, EInteriorFactor);
float fNoiseCurve =lerp( lerp( fNoiseCurve_Day, fNoiseCurve_Night, 1 - ENightDayFactor ), fNoiseCurve_Interior, EInteriorFactor);
float noiseAmount=fNoiseAmount*pow(blurAmount, fNoiseCurve);
float fNoiseDiffusion =lerp( lerp( fNoiseDiffusion_Day, fNoiseDiffusion_Night, 1 - ENightDayFactor ), fNoiseDiffusion_Interior, EInteriorFactor);

#if (USE_DYNAMIC_GRAIN_PUSH == 1)
	fNoiseAmount=fNoiseAmount + TimeFactorNoise;
#endif
#if (USE_DYNAMIC_GRAIN_PULL == 1)
	fNoiseAmount=fNoiseAmount - TimeFactorNoise;
#endif

#if (USE_DYNAMIC_GRAIN_CURVE_PUSH == 1)
	fNoiseCurve=fNoiseCurve + TimeFactorCurve;
#endif
#if (USE_DYNAMIC_GRAIN_CURVE_PULL == 1)
	fNoiseCurve=fNoiseCurve - TimeFactorCurve;
#endif

#if (USE_DYNAMIC_GRAIN_DIFF_PUSH == 1)
	fNoiseDiffusion=fNoiseDiffusion + TimeFactorDiffusion;
#endif
#if (USE_DYNAMIC_GRAIN_DIFF_PULL == 1)
	fNoiseDiffusion=fNoiseDiffusion - TimeFactorDiffusion;
#endif

	res=lerp(res, (cnoi.x+0.5)*res, noiseAmount*saturate(1.0-origgray*fNoiseDiffusion));	
	
	res.w=depth;
		
	return res;
}

/*==============================================================================================================
	Passes of Techniques
==============================================================================================================*/

#if (USE_PRE_FX == 1)
technique PostProcess
{
	pass P0
	{

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

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

#if (USE_ANAMFLARE == 1)
	pass P1
	{
	#if (USE_SHARPENING == 1)
		AlphaBlendEnable = true;
		SrcBlend = One;
		DestBlend = One;
			
		PixelShader = compile ps_3_0 PS_ProcessPass_Anamorphic(lerp( lerp( fFlareAxis_Day, fFlareAxis_Night, 1 - ENightDayFactor ), fFlareAxis_Interior, EInteriorFactor));
	#else
		
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessPass_Anamorphic(lerp( lerp( fFlareAxis_Day, fFlareAxis_Night, 1 - ENightDayFactor ), fFlareAxis_Interior, EInteriorFactor));
		
		DitherEnable = FALSE;
		ZEnable = FALSE;
		CullMode = NONE;
		ALPHATESTENABLE = FALSE;
		SEPARATEALPHABLENDENABLE = FALSE;
		AlphaBlendEnable = FALSE;
		StencilEnable = FALSE;
		FogEnable = FALSE;
		SRGBWRITEENABLE = FALSE;
	#endif
	}
#endif
}
#endif
#if (USE_PRE_FX == 1)
technique PostProcess2
#else
technique PostProcess
#endif
{
	pass P0
	{

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

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

#if (USE_PRE_FX == 1)
technique PostProcess3
#else
technique PostProcess2
#endif
{
	pass P0
	{

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

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

#if (USE_PRE_FX == 1)
technique PostProcess4
#else
technique PostProcess3
#endif
{
	pass P0
	{

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

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

#if (USE_PRE_FX == 1)
technique PostProcess5
#else
technique PostProcess4
#endif
{
	pass P0
	{

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

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

#if (USE_PRE_FX == 1)
technique PostProcess6
#else
technique PostProcess5
#endif
{
	pass P0
	{

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

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

#if (USE_POST_FX == 1)
#if (USE_PRE_FX == 1)
technique PostProcess7
#else
technique PostProcess6
#endif
  {
#if (USE_CINEMATIC_GRAIN == 1)
	pass P0
	{
		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	
  }
#endif