//++++++++++++++++++++++++++++++++++++
// iCEnhancer 3.0 post-effects
// COPYRIGHT - HAYSSAM KEILANY // BORIS VORONTSOV // JEROEN VAN SCHIJNDEL
// YOU ARE NOT ALLOWED TO COPY/REDISTRIBUTE/MODIFY THIS CODE WITHOUT PERMISSION
// ASK PERMISSIONS AT ICELAGLACE@YAHOO.FR
//++++++++++++++++++++++++++++++++++++

#include "icenhancer_config.txt"

// enb values
float4 Timer;
float4 tempF1;
float4 tempF2;
float4 ScreenSize;

// textures
texture2D texColor;
texture2D texNoise;

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

sampler2D SamplerNoise = sampler_state
{
Texture   = <texNoise>;
MinFilter = POINT;
MagFilter = POINT;
MipFilter = NONE;
AddressU  = Wrap;
AddressV  = Wrap;
SRGBTexture=FALSE;
MaxMipLevel=0;
MipMapLodBias=0;
};

// vs
struct VS_OUTPUT_POST {
float4 vpos  : POSITION;
float2 txcoord : TEXCOORD0;
};
struct VS_OUTPUT_DOF {
float4 vpos  : POSITION;
float2 txcoord : TEXCOORD0;
float focaldepth: TEXCOORD1;
};

struct VS_INPUT_POST {
float3 pos  : POSITION;
float2 txcoord : TEXCOORD0;
};

// public release version
float zw(float f){return 1.f-1.f/f;}
float pp(){float f=0,S=.05;for(int r=0;r<6;r++){float t=(float)r;t*=3.1415*2/6;float2 e=float2(cos(t),sin(t));e.y*=ScreenSize.z;e*=S;float z=zw(tex2Dlod(SamplerColor,float4(e+float2(.5,.4),0,0)).w);f+=z;}return f/6;}
float fdp(float2 s){float x=tex2D(SamplerNoise,float2(.1,cos(Timer.x*11000))*Timer.x*11000*8.+s).x;x*=x;return x;}
float rien(float s,float c,float T){return step(T,sin(Timer.x*11000+s*cos(Timer.x*11000*c)));}
float ramp(float f,float p,float t){float o=step(p,f)-step(t,f),s=(f-p)/(t-p)*o;return(1.-s)*o;}
float booty(float2 r){float n=fdp(r*float2(.5,1.)+float2(1.,3.));return ramp(fmod(r.y*4.+Timer.x*11000/2.+sin(Timer.x*11000+sin(Timer.x*11000*.63)),1.),.5,.6)*n;}
float3 tamere(float2 n){float2 r=n;float o=1./(1.+20.*(r.y-fmod(Timer.x*11000/4.,1.))*(r.y-fmod(Timer.x*11000/4.,1.)));r.x=r.x+sin(r.y*10.+Timer.x*11000)/50.*rien(4.,4.,.3)*(1.+cos(Timer.x*11000*80.))*o;float x=.4*rien(2.,3.,.9)*(sin(Timer.x*11000)*sin(Timer.x*11000*20.)+(.5+.1*sin(Timer.x*11000*200.)*cos(Timer.x*11000)));r.y=fmod(r.y+x,1.);float3 y=tex2D(SamplerColor,r).xyz;return y;}
float2 truc(float2 f){return f-=float2(.5,.5),f=f*1.2*(1./1.2+2.*f.x*f.x*f.y*f.y),f+=float2(.5,.5),f;}
VS_OUTPUT_DOF VS_DOF(VS_INPUT_POST s){VS_OUTPUT_DOF d;float4 y=float4(s.pos.x,s.pos.y,s.pos.z,1.);d.vpos=y;d.txcoord.xy=s.txcoord.xy;d.focaldepth=pp();return d;}
VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST s){VS_OUTPUT_POST d;float4 y=float4(s.pos.x,s.pos.y,s.pos.z,1.);d.vpos=y;d.txcoord.xy=s.txcoord.xy;return d;}
float4 DOF_Pass1(VS_OUTPUT_DOF p,float2 w:VPOS):COLOR{float2 t=p.txcoord.xy;
#if (DEPTHOFFIELD==1)
int f=6;float l=.5;int z=3;if(tempF1.x<.99)z=0;float S=.7,r=2,s=0,D=1.,y=4;float4 i=tex2D(SamplerColor,t);i.xyz*=lerp(S,r,pow(0,s));float3 I=pow(i.xyz,y);float m=1,c=1,x=zw(i.w),o=p.focaldepth;
#if (MANUAL_DOF_FOCUS==1)
float P=pow(tempF1.z,20.),V=pow(tempF1.y,20.);o=(x-P)/(V+P);
#endif
float h=abs(x-o);h=sqrt(h/2.);if(x<o)h/=4;h=min(h,1.);float a=1.f/ScreenSize.x*(3*z*2)*h;int M=max(1,ceil((float)z*h));a*=(float)z/(float)(M+1);for(int e=0;e<M;e++){for(int n=0;n<f;n++){float O=(float)n;O*=3.1415*2/(float)f;O+=.37;float2 F=float2(cos(O),sin(O)),C=float2(cos(O+3.1415*2/(float)f),sin(O+3.1415*2/(float)f));for(float d=0;d<1.f;d+=1.f/((float)e+1)){float U=(float)e/(float)z;float2 T=lerp(F,C,d);T.y*=ScreenSize.z;float G=1;float4 L=tex2D(SamplerColor,t+T*a*(e+1)/z);L.xyz*=lerp(S,r,pow(U,s));L.xyz=pow(L.xyz,y);float E=zw(L.w);if(E<x*1.3&&E<o*.5)G=0;I+=L.xyz*G;m+=G;c+=1;}}}I/=m;I=pow(I,1.f/y);I*=l;return float4(I,h*(m/c));
#else
float4 G=tex2D(SamplerColor,p.txcoord.xy);return G;
#endif
}
float4 DOF_Pass2(VS_OUTPUT_POST e,float2 D:VPOS):COLOR{
#if (DEPTHOFFIELD==1)
float2 f=0.;float3 r=0.;int z=6;f.xy=e.txcoord.xy*2-1;float3 x=float3(-1,0,1)*16;float4 t=tex2D(SamplerColor,f*.5+.5);float S=t.w*1.5;r+=t.xyz;x/=(float)ScreenSize.x;x*=S;x++;for(int i=0;i<z;i++){float y=(float)i;y*=3.1415*2/(float)z;y+=.37;float2 d=float2(cos(y),sin(y));d.y*=ScreenSize.z;if(tempF1.x>.99)r.x+=tex2D(SamplerColor,x.x*f*.5+.5+d/ScreenSize.x*2*S).x,r.y+=tex2D(SamplerColor,x.y*f*.5+.5+d/ScreenSize.x*2*S).y,r.z+=tex2D(SamplerColor,x.z*f*.5+.5+d/ScreenSize.x*2*S).z;if(tempF1.x<.99)r+=tex2D(SamplerColor,f*.5+.5+d/ScreenSize.x*0*S).xyz;}r/=7;return float4(r,1);
#else
float4 y=tex2D(SamplerColor,e.txcoord.xy);return y;
#endif
}
float4 Effects(VS_OUTPUT_POST f,float2 p:VPOS):COLOR{float l=fmod(Timer.x*1000+cos(Timer.x*9.98),1);float3 t=tex2D(SamplerColor,f.txcoord.xy).xyz;
#if (NOISE==1)
float2 d=f.txcoord.xy*14.3+.42,r=f.txcoord.xy*14.+.25;d.xy+=l*10;r.xy+=l*10;float4 S=(tex2D(SamplerNoise,d)*2-1)*(tex2D(SamplerNoise,r)*2-1);float y=2;float3 E=float3(.299,.587,.114);float s=lerp(0.,dot(t,E),1.),e=smoothstep(.2,0.,s);e+=s;float x=lerp(S,0.,pow(e,4.));float3 T=pow(pow(t,1/y)+S*.0333,y);T=t+x*.02;return float4(T,1);
#else
return float4(t,1);
#endif
}
float4 TVStuff(VS_OUTPUT_POST r,float2 f:VPOS):COLOR{float2 s=r.txcoord.xy;float3 T=tex2D(SamplerColor,s).xyz;
#if (TVSTUFF==1)
float2 x=s;x=truc(x);float3 S=tamere(x);float y=3.+.3*sin(Timer.x*11000+5.*cos(Timer.x*11000*5.)),t=(1.-y*(x.y-.5)*(x.y-.5))*(1.-y*(x.x-.5)*(x.x-.5));S+=booty(x)/4.;S+=fdp(x*2.)/6.;S*=t;S*=(12.+fmod(x.y*30.+Timer.x,1.))/13.;return float4(S,1);
#else
return float4(T,1);
#endif
}
float4 LensDistortion(VS_OUTPUT_POST t,float2 i:VPOS):COLOR{float d;float2 f=t.txcoord.xy;
#if (LENSDISTORTION==1)
float2 S=float2(.5,.5),r=f-S;float z=sqrt(dot(r,r)),a=-tempF2.z*3.14159/(2.*sqrt(dot(S,S)))*(1./ScreenSize.x-.5);if(a>0.)d=sqrt(dot(S,S));else{if(a<1.)d=S.x;else d=S.y;}float2 y;if(a>0.)y=S+normalize(r)*tan(z*a)*d/tan(d*a);else if(a<0.)y=S+normalize(r)*atan(z*-a*10.)*d/atan(-a*d*10.);else y=f;float4 o=tex2D(SamplerColor,y);
#else
float4 o=tex2D(SamplerColor,f);
#endif
return o;}
float4 Final(VS_OUTPUT_POST f,float2 z:VPOS):COLOR{float2 t=f.txcoord.xy;float3 r=tex2D(SamplerColor,t).xyz;
#if (CINEMA==1)
r.xyz=t.y>.1&&t.y<.9?r.xyz:0.;
#endif
return float4(r,1);}
	
technique PostProcess
{
   pass P0
   {
VertexShader = compile vs_3_0 VS_DOF();
PixelShader  = compile ps_3_0 DOF_Pass1();

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

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

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

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

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

technique PostProcess4
{
   pass P0
   {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 TVStuff();

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 LensDistortion();

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

technique PostProcess6
{
   pass P0
   {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 Final();

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

