///////////////////////////////////////////////////////////////////
// ENBSeries effect file
// made by : PetkaGTA, Marty McFly
// Feel free to mod it like you want & distribute on your own settings
// As long as you mention everyone's names
// 
//Copyright : Boris Vorontsov/PetkaGTA/Marty McFly
//
//////////////////////////////////////////////////////////////////

//+++++++++++++++++++++++++++++
// Internal parameters, can be modified
//+++++++++++++++++++++++++++++

float ScreenScaleY;
float ScreenSize;
float2 screenRes = {1024,768};

//Tonemapping
float _ExposureAdjustment = 3.3;
float4 _HdrParams = {0.4,0.4,0.4,4};// 1st-3d - middle gray, 4th - white*white; 


float PI = 3.14159265;

float2 texel = {0.0009765625,0.00130208333333333333333333333333};

//float focalDepth = 10.5;
//float focalLength = 90.5;
//float fstop = 30.5;

float focalDepth = 50.5;
float focalLength = 25.5;
float fstop = 20.5;


bool vignetting = true;
float vignout = 25.31; //vignetting outer border
float vignin = 5.30; //vignetting inner border
float MartyMcFly = 25.30; //f-stops till vignette fades




bool manualdof = false; //manual dof calculation
float ndofstart = 1.0; //near dof blur start
float ndofdist = 2.0; //near dof blur falloff distance
float fdofstart = 1.0; //far dof blur start
float fdofdist = 3.0; //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;
float namount = 0.0000;
float DOFdownsample = 4;
float maxblur = 2.5;

static const int samples = 26; 
static const int rings = 1; 

float threshold = 0.8;//threshold for gain
float gain = 10.0;//brightens blurred areas to make bokeh effect visible

float bias = 0.0;
float fringe = 0.0;

float znear = 2.0; //camera clipping start
float zfar = 2000.0; //camera clipping end


////////////////////////////////////////////////////////////////////////////
////// END OF TWEAKING VALUES //////////////////////////////////////////////                                                                                                             //Copyright : Boris Vorontsov/PetkaGTA/Marty McFly
////////////////////////////////////////////////////////////////////////////


texture2D texColor;
texture2D texDepth;
texture2D texNoise;

//++++++++++++++++++++++++++
// Sampler Inputs
//++++++++++++++++++++++++++

sampler2D InputSampler = sampler_state
{
    Texture = (texColor);
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    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 Inputs
//++++++++++++++++++++++++++

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;//copyright by Marty McFly
	OUT.txcoord.xy=IN.txcoord.xy;

	return OUT;
}



//++++++++++++++++++++++++++
// DOF
//++++++++++++++++++++++++++

bool pentagon = false; //use pentagon as bokeh shape?  
float feather = 0.0; //pentagon shape feather

																									#include "SweetFX\Shaders\ProcessPentagonShape.h"


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

																									#include "SweetFX\Shaders\ProcessColorDOF.h"

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


																									#include "SweetFX\Shaders\ProcessBokeh.h"

//++++++++++++++++++++++++++
// Compiler
//++++++++++++++++++++++++++

technique PostProcess
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessDoFBokeh();
		CullMode=NONE;
	}
	
}