//++++++++++++++++++++++++++++++++++++
// iCEnhancer 3.0 Main fx file
// COPYRIGHT - HAYSSAM KEILANY // BORIS VORONTSOV
// ASK PERMISSIONS AT ICELAGLACE@YAHOO.FR IF YOU WANNA USE
// THIS FILE ON YOUR OWN SETTINGS
//++++++++++++++++++++++++++++++++++++

// for the configuration tool
#include "icenhancer_config.txt"

// tweakable values
	float4 Timer;
	float4 SunDirection;
	float4 ScreenSize;

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

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

texture2D texs0 : register(s0);
texture2D texs1 : register(s1);
texture2D texs2 : register(s2);
texture2D texs3 : register(s3);
texture2D texs4 : register(s4);
texture2D texs5 : register(s5);
texture2D texs6 : register(s6);
texture2D texs7 : register(s7);
texture2D texs13 : register(s13);
texture2D texs15 : register(s15);

sampler2D s2=sampler_state { Texture=<texs2>; };
sampler2D s4=sampler_state { Texture=<texs4>; };
sampler2D s5=sampler_state { Texture=<texs5>; };
sampler2D s6=sampler_state { Texture=<texs6>; };
sampler2D s7=sampler_state { Texture=<texs7>; };
sampler2D s15=sampler_state { Texture=<texs15>; };


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

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

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


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

// beginning the custom post-effect here...
// edit : public version release

float4 PS_C215BE6E(VS_OUTPUT_POST f):COLOR{float4 y=0.,z=float4(0,.2125,.7154,.0721),w=float4(.25,1,256,0),x=float4(0,2,4,8),n=float4(58.164,47.13,0,0),S=float4(-.5,-1.5,1.5,.5),s=float4(2,-1,.125,0),R=float4(256,2,4,8),Y=float4(1.,.1,.8,1.),E=float4(0.,.1,.8,.333),T=float4(.06,.1,.8,.1),C=float4(.01,1.4,0.,.32),t=float4(.333333,1.,0.,0.),o=float4(.27,.67,.06,0.),a=float4(0.,0.,0.,1e-05),l=float4(.55,.9,.85,.9),c=float4(0.,0.,0.,.07),i,p,m,d,e,G,r,O,D,A,L,U,I=0.;I.xy=f.txcoord0.xy;p=tex2D(s2,I.xy);i=tex2D(s1,I.xy);i.y=-_c77.x+_c77.y;i.y=1./i.y;i.z=i.y*_c77.y;i.z=i.z*-_c77.x;i.x=_c77.y*-i.y+i.x;i.x=1./i.x;i.y=i.z*i.x;i.w=abs(_c79.w);float B=i.z*i.x-_c78.w;B+=_c78.y*-.5;d=p;i.x=1.;e.x=I.y*s.x+s.y;e.y=I.x*s.x+s.y;e.z=I.y*s.x+s.y;i.z=e.y*_c77.z;i.z=i.y*i.z;p.w=-e.x*_c77.w;p.w=i.y*p.w;G.xyz=p.w*_c73.xyz;G.xyz+=i.z*_c72.xyz;G.xyz+=-i.y*_c74.xyz;G.xyz+=_c75.xyz;i.y=-G.z*_c77.z;i.y=1./i.y;r.x=G.x*i.y;i.y=G.z*_c77.w;i.y=1./i.y;r.y=G.y*i.y;i.yz=-e.yz+r.xy;i.y=i.y*_c80.x;i.z=i.z*_c80.x;int N;if(MOTION_BLUR!=1)N=0;if(MOTION_BLUR==1)N=25;if(MOTION_BLUR==2)N=50;if(MOTION_BLUR==3)N=100;int M=int(pow(_c80.x,.5)*N);e.xy=i.yz/(float(M)+1);G=tex2D(s6,I.xy);p.w=G.x-_c85.x;e.zw=d.xy*x.w;e.zw=I.xy*n.xy+e.zw;G=tex2D(s5,e.zw);e.zw=e.xy*(G.x-(float(M)+1.)*.5)+I.xy;G.xyz=d.xyz;m.w=w.y;d.w=w.y;for(int u=0;u<M;u++){float4 P;P.xy=e.xy*d.w+e.zw;P.zw=0;O=tex2Dlod(s6,P);G.w=O.x-_c85.x;if(G.w<-.3)G.w=w.y;else G.w=w.w;r=tex2Dlod(s2,P);G.xyz=r.xyz*G.w+G.xyz;m.w=m.w+G.w;d.w=d.w+w.y;}m.w=1./m.w;i.y=i.y*_c44.x;i.z=i.z*_c44.y;i.y=dot(i.yz,i.yz);i.y=pow(i.y,S.w);i.y=i.y*S.w;i.x=i.x+w.y;i.x=1./i.x;i.x=saturate(i.y*i.x*.15);e.xyz=G.xyz*m.w-d.xyz;i.xyz=i.x*e.xyz+d.xyz;if(-p.w<0.)i.xyz=d.xyz;if(-i.w<0.)i.xyz=p.xyz;p=tex2D(s4,z.x);p.xyz=m*_c66.x-p.x;p.x=1./i.w;p.x=p.x*_c81.x;p.xyz=m*_c66.x-p.x;p.xyz=m*_c81.z;p.xyz=p*w.x;p.xyz=i.w*i;p.x=dot(p.xyz,z.yzw);p.yzw=i.w*_c84.xxyz;p.x=saturate(p.x);i.w=_c82.z-D.y;m.x=pow(p.x,i.w);y.xyz=i.xyz*m.x;y.w=w.y;
#if (GODRAYS==1)
float4 P=0.;float K=.001,W=.9,Q=.5,H=.25;int F=100;if(GODRAY_QUALITY==0)F=32;if(GODRAY_QUALITY==1)F=64;if(GODRAY_QUALITY==2)F=100;float2 b=I.xy*2.-1.,V=SunDirection.xy/SunDirection.w;V.y+=.75;V.y=-V.y;float2 h=b-V,g=I.xy;h*=1./F*Q;float Z=1.,X=2.;if(SunDirection.w>0.){for(int J=0;J<F;J++){g-=h;float4 v=tex2D(s2,g)-X,q=tex2D(s1,g.xy);q.y=-_c77.x+_c77.y;q.y=1./q.y;q.z=q.y*_c77.y;q.z=q.z*-_c77.x;q.x=_c77.y*-q.y+q.x;q.x=1./q.x;q.y=q.z*q.x;q.w=abs(_c79.w);float k=q.z*q.x-_c78.w;k+=_c78.y*-.5;v=min(v,1.);v=max(v,0.);if(k<2000.)v.xyz=0.;v*=Z*H;P+=v;Z*=W;}}K*=SunDirection.w*.1;K=max(K,0.);P*=K;
#endif
float4 q=i,v=tex2D(s3,I.xy);G=tex2D(s4,z.x);G.w=G.x;G.w=.03/G.w;q.xyz=q*G.w;v.xyz*=G.w;float J=pow(1.-G.x,2.);J=min(J,1.);float3 k=tex2D(s13,I.xy).xyz,j=tex2D(s2,I.xy+k.x*.1).xyz;if(LENS_DIRT==1)v.xyz+=v*(k.y*J)*2.;q.xyz+=v.xyz*.5*BLOOM_POWER;q.w=B;q.w*=1/(q.w+.5);
#if (GODRAYS==1)
P.xyz*=G.w;q.xyz+=P.xyz;
#endif
#if (KODACHROME==1)
q.xyz=min(.99,q.xyz);q.x=tex2D(s13,float2(q.x,1.)).x;q.y=tex2D(s13,float2(q.y,1.)).y;q.z=tex2D(s13,float2(q.z,1.)).z;
#else
q.xyz=pow(q.xyz,1/2.2);q.xyz=min(1.,q.xyz);
#endif
#if (POST_EFFECTS==1)
float ab=dot(q.xyz,float3(.299,.587,.114));q.xyz=lerp(ab,q.xyz,SATURATION);q.xyz=lerp(q.xyz,.5*(1.+sin((q.xyz-.5)*3.14159)),CONTRAST);float3 ac;ac.x=CROSS_R;ac.y=CROSS_G;ac.z=CROSS_B;float ad=dot(q.xyz,.333);ac=lerp(0.,ac,saturate(ad*2.));ac=lerp(ac,1.,saturate(ad-.5)*2.);q.xyz=lerp(q.xyz,ac,saturate(ad*CROSS_POWER));if(BLACKNWHITE==1)q.xyz=dot(q.xyz,.333);
#endif
float ae;float2 af;af.x=I.x-.5;af.y=.5-I.y;ae=length(af.xy*float2(3.14,2)*.333)*1.41421;ae=pow(ae,4);q.xyz-=ae*G.x*.4;y=q;return y;}

technique Shader_C215BE6E
{
    pass p0 
    {
		PixelShader =compile ps_3_0 PS_C215BE6E();
	}
}

