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

#include "icenhancer_config.txt"

// enb values
float4	ScreenSize;
float4	TempParameters; 
float4	LenzParameters;
float4	BloomParameters;

// textures
texture2D texBloom1;
texture2D texBloom2;
texture2D texBloom3;
texture2D texBloom4;
texture2D texBloom5;

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

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

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

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

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

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

// public release version
VS_OUTPUT_POST VS_Bloom(VS_INPUT_POST s){VS_OUTPUT_POST T;T.vpos=float4(s.pos.x,s.pos.y,s.pos.z,1.);T.txcoord0.xy=s.txcoord0.xy+TempParameters.xy;return T;}
static const float2 pp[4]={float2(.25,.25),float2(.25,-.25),float2(-.25,.25),float2(-.25,-.25)};
static const float2 pq[8]={float2(1.,1.),float2(1.,-1.),float2(-1.,1.),float2(-1.,-1.),float2(0.,1.),float2(0.,-1.),float2(1.,0.),float2(-1.,0.)};
float4 pa(VS_OUTPUT_POST x):COLOR{float4 f,y=0.;float2 e=TempParameters.z;e.y*=ScreenSize.z;for(int r=0;r<4;r++){f.xy=pp[r];f.xy=f.xy*e.xy+x.txcoord0.xy;float4 S=tex2D(SamplerBloom1,f.xy)-2;S=max(S,0.);y.xyz+=S.xyz;}y.xyz*=.25;return y;}
float4 pb(VS_OUTPUT_POST s):COLOR{float4 f,y=tex2D(SamplerBloom1,s.txcoord0);float2 D=TempParameters.z;D.y*=ScreenSize.z;float t=TempParameters.w-.25;D.xy*=t*4;for(int r=0;r<8;r++){f.xy=pq[r];f.xy=f.xy*D.xy+s.txcoord0.xy;float4 T=tex2D(SamplerBloom1,f.xy);y+=T;}y*=.111111;return y;}
float4 pc(VS_OUTPUT_POST y):COLOR{float4 f,z=tex2D(SamplerBloom1,y.txcoord0);float2 s=TempParameters.z;s.y*=ScreenSize.z;float t=TempParameters.w-.25;s.xy*=t*4;float4 D=0.;sincos(.3927,D.x,D.y);for(int r=0;r<8;r++){f.xy=pq[r];f.xy=reflect(f.xy,D.xy);f.xy=f.xy*s.xy+y.txcoord0.xy;float4 T=tex2D(SamplerBloom1,f.xy);z+=T;}z*=.111111;return z;}
float4 pf(sampler2D D,float2 f,float2 x,float3 y){float4 z=0;z.x=tex2D(D,f.xy+x.xy*y.x).x;z.y=tex2D(D,f.xy+x.xy*y.y).y;z.z=tex2D(D,f.xy+x.xy*y.z).z;z.w=1.;return z;}
float4 pd(VS_OUTPUT_POST f):COLOR{float4 y;y=tex2D(SamplerBloom1,f.txcoord0);y+=tex2D(SamplerBloom2,f.txcoord0);y+=tex2D(SamplerBloom3,f.txcoord0);y+=tex2D(SamplerBloom4,f.txcoord0);y+=tex2D(SamplerBloom5,f.txcoord0);y*=.142857;float3 t=0;float2 m=0.;const float3 p[4]={float3(.5,.01,-2.),float3(.01,20.,-1.),float3(100.,20.,10.),float3(-10.,0.,1.)},l[4]={float3(.01,.01,.3),float3(.3,.3,.3),float3(.3,.3,.3),float3(.05,.05,.5)};float z=.4;float2 D=.5-f.txcoord0,x=normalize(-D);float3 o=float3(-D.x*z,0.,D.x*z);for(int r=0;r<4;r++){float2 S=f.txcoord0.xy-.5;m.xy=p[r].x*S;m.xy*=pow(4.*length(float2(S.x*ScreenSize.z,S.y)),p[r].y);m.xy*=p[r].z;m.xy=.5+m.xy;float3 e=pf(SamplerBloom3,m.xy,x,o);e=e*l[r];S=m.xy-.5;S*=1.75;e*=saturate(1.-dot(S,S));float n=max(e.x,max(e.y,e.z)),c=n/(1.+n);c=pow(c,10);e.xyz*=c;t+=e;}t.xyz*=.5;y.xyz+=t.xyz;return y;}


technique BloomPrePass
{
    pass p0
    {
	VertexShader = compile vs_3_0 VS_Bloom();
	PixelShader  = compile ps_3_0 pa();

	ColorWriteEnable=ALPHA|RED|GREEN|BLUE;
	CullMode=NONE;
	AlphaBlendEnable=FALSE;
	AlphaTestEnable=FALSE;
	SEPARATEALPHABLENDENABLE=FALSE;
	FogEnable=FALSE;
	SRGBWRITEENABLE=FALSE;
	}
}

technique BloomTexture1
{
    pass p0
    {
	VertexShader = compile vs_3_0 VS_Bloom();
	PixelShader  = compile ps_3_0 pb();

	ColorWriteEnable=ALPHA|RED|GREEN|BLUE;
	CullMode=NONE;
	AlphaBlendEnable=FALSE;
	AlphaTestEnable=FALSE;
	SEPARATEALPHABLENDENABLE=FALSE;
	FogEnable=FALSE;
	SRGBWRITEENABLE=FALSE;
	}
}


technique BloomTexture2
{
    pass p0
    {
	VertexShader = compile vs_3_0 VS_Bloom();
	PixelShader  = compile ps_3_0 pc();

	ColorWriteEnable=ALPHA|RED|GREEN|BLUE;
	CullMode=NONE;
	AlphaBlendEnable=FALSE;
	AlphaTestEnable=FALSE;
	SEPARATEALPHABLENDENABLE=FALSE;
	FogEnable=FALSE;
	SRGBWRITEENABLE=FALSE;
	}
}


technique BloomPostPass
{
    pass p0
    {
	VertexShader = compile vs_3_0 VS_Bloom();
	PixelShader  = compile ps_3_0 pd();

	ColorWriteEnable=ALPHA|RED|GREEN|BLUE;
	CullMode=NONE;
	AlphaBlendEnable=FALSE;
	AlphaTestEnable=FALSE;
	SEPARATEALPHABLENDENABLE=FALSE;
	FogEnable=FALSE;
	SRGBWRITEENABLE=FALSE;
	}
}

