#include "config.efh"

#define HDR_IS_A_FAKE
#define HDR_FAKE_DEFLATE  0.25
#define USE_XY_BLOOM
#define BLOOM_BLUR_MUL    2.10
#define DEFAULT_SCENE_LUM 0.50
#define HDR_USE_STARS 

#define DYNAMIC_LUM_CLAMPMIN 0.40f
#define DYNAMIC_LUM_CLAMPMAX 0.50f
#define ADOPT_MULTIPLIER     0.5f   // 0.5 -> Full adoption will take 1.0 / 0.5 => 2 seconds

//#define DEBUG_STARS
//#define DEBUG_BLOOM

// #define USE_TONE_MAPPING_HDR 
// #define USE_TONE_MAPPING_LDR 
#define HDR_USE_ADAPTIVE_LUMINANCE

const half3 LUM_VEC  = half3(0.25, 0.75, 0.10);
const half3 LUM_VECX = half3(0.50, 0.85, 0.40);
const half3 BS_VEC   = half3(1.05, 0.97, 1.27); 


//
// Annotations processing test
//
// Texture 2D
//TEX_STD_2D_ANNOTATION  (IN_SomeCustom2DTexture,   IN_SomeCustomSampler2D,   "T_5_STONE3.DDS")

// Texture CUBE
//TEX_STD_CUBE_ANNOTATION(IN_SomeCustomCUBETexture, IN_SomeCustomSamplerCUBE, "cube_sky_haze.dds")

// Current time in ms
float IN_CurrentTimeINMS <string CurrentTimeInMS = "Eval";>;

// Matrixes
matrix IN_CurrProjectionMatrix				<string CurrProjectionMatrix			 = "Eval";>;
matrix IN_CurrCameraMatrix					<string CurrCameraMatrix			     = "Eval";>;
matrix IN_CurrWorldViewProjectionMatrix		<string CurrWorldViewProjectionMatrix	 = "Eval";>;
matrix IN_CurrProjectionMatrixInv			<string CurrProjectionMatrixInv			 = "Eval";>;
matrix IN_CurrCameraMatrixInv				<string CurrCameraMatrixInv				 = "Eval";>;
matrix IN_CurrWorldViewProjectionMatrixInv	<string CurrWorldViewProjectionMatrixInv = "Eval";>;
matrix IN_PrevProjectionMatrix				<string PrevProjectionMatrix			 = "Eval";>;
matrix IN_PrevCameraMatrix					<string PrevCameraMatrix			     = "Eval";>;
matrix IN_PrevWorldViewProjectionMatrix		<string PrevWorldViewProjectionMatrix    = "Eval";>;
matrix IN_PrevProjectionMatrixInv			<string PrevProjectionMatrixInv			 = "Eval";>;
matrix IN_PrevCameraMatrixInv				<string PrevCameraMatrixInv				 = "Eval";>;
matrix IN_PrevWorldViewProjectionMatrixInv	<string PrevWorldViewProjectionMatrixInv = "Eval";>;

// Other annotations may follow ...


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Inputs
//
struct pixelOutput
{
  half4 color : COLOR;
};

struct appdata_HDRBASE
{
 float4 Position : POSITION;
 float4 Color	 : COLOR0;
 float2 UV		 : TEXCOORD0;
};

#ifdef _XBOX
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct appdata_simple_vs
{
 float4 Position : POSITION;
 float4 Color	 : COLOR0;
 float2 UV		 : TEXCOORD0;
};

appdata_HDRBASE XBOXSimpleVS(appdata_simple_vs IN)
{
 appdata_HDRBASE OUT;
 
 OUT.Position = float4(IN.Position.xyz, 1.0);
 OUT.Color    = IN.Color;
 OUT.UV       = IN.UV;
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Variables
//
TEX_STD_FOG(IN_HDRBaseTextureA,  IN_HDRBASEA_Sampler)   // Sampler with linear sampling
TEX_STD_FOG(IN_HDRBaseTextureB,  IN_HDRBASEB_Sampler)   // Sampler with linear sampling
TEX_STD_FPN(IN_HDRBaseTextureAP, IN_HDRBASEAP_Sampler)  // Sampler with point sampling
TEX_STD_FPN(IN_HDRBaseTextureBP, IN_HDRBASEBP_Sampler)  // Sampler with point sampling
TEX_STD_FPN(IN_HDRDepthMap,      IN_HDRDepthMap_Sampler)// Depthmap sampler

TEX_STD_TR (IN_DissortTexture,    IN_DissortSampler)    // Dissort sampler
TEX_STD_FPN(IN_HDRLumTexture,    IN_LuminanceSampler)   // Luminance sampler
TEX_STD_FOG(IN_HDRStarsTexture,  IN_StarsSampler)

TEX_STD_FOG(IN_CDFTexture, IN_CDFSampler)               // CDF Histogram sampler

float4 IN_HDRBlurCoefs;
bool   IN_HDRBlurUse;

float4 IN_HDRBaseTextureA_TEX;                          
float4 IN_HDRBaseTextureB_TEX;	

TEX_STD_FOG(IN_StarsPassTex0,  IN_StarsPassSampler0)
TEX_STD_FOG(IN_StarsPassTex1,  IN_StarsPassSampler1)
TEX_STD_FOG(IN_StarsPassTex2,  IN_StarsPassSampler2)
TEX_STD_FOG(IN_StarsPassTex3,  IN_StarsPassSampler3)
TEX_STD_FOG(IN_StarsPassTex4,  IN_StarsPassSampler4)
TEX_STD_FOG(IN_StarsPassTex5,  IN_StarsPassSampler5)
TEX_STD_FOG(IN_StarsPassTex6,  IN_StarsPassSampler6)
TEX_STD_FOG(IN_StarsPassTex7,  IN_StarsPassSampler7)

#ifdef FX_LITE
#define IN_HDRDepthMap_Sampler IN_HDRDepthMap
#define IN_CDFSampler		   IN_CDFTexture
#define IN_HDRBASEA_Sampler    IN_HDRBaseTextureA
#define IN_HDRBASEB_Sampler    IN_HDRBaseTextureB
#define IN_HDRBASEAP_Sampler   IN_HDRBaseTextureAP
#define IN_HDRBASEBP_Sampler   IN_HDRBaseTextureBP
#define IN_DissortSampler      IN_DissortTexture
#define IN_LuminanceSampler    IN_HDRLumTexture
#define IN_StarsSampler        IN_HDRStarsTexture

#define IN_StarsPassSampler0   IN_StarsPassTex0
#define IN_StarsPassSampler1   IN_StarsPassTex1
#define IN_StarsPassSampler2   IN_StarsPassTex2
#define IN_StarsPassSampler3   IN_StarsPassTex3
#define IN_StarsPassSampler4   IN_StarsPassTex4
#define IN_StarsPassSampler5   IN_StarsPassTex5
#define IN_StarsPassSampler6   IN_StarsPassTex6
#define IN_StarsPassSampler7   IN_StarsPassTex7
#endif

float   IN_ExposureLevel;
float   IN_GammaCtrl;
float   IN_HDRDefog;
float   IN_HDRCutoff;
float   IN_GlowMull;
float   IN_DOF_Focus;
float   IN_DOF_Tolerance;
float   IN_DOF_ToleranceInv;
float   IN_HDRSaturationHDR;
float   IN_HDRSaturationLDR;
float   IN_HDRBlueShifting;
float   IN_DreamlandFactor;
float   IN_SceneLuminanceFactor;
float   IN_HDRStarsMull;
float   IN_HDRStarsTres;
bool    IN_UseHistogramEqualization;
float4  IN_ScreenspaceSunPos;		  // (x, y) -> sun position in screenspace (0 - 1), w -> sun occlusion (0 - sun is occluded or cliped, 1 - sun is fully visible)

float4  IN_BloomTable[16];

//
// Sampling depth map stuff
//
float SAMPLE_DEPTH_MAP(in float2 UV)
{
#ifdef USE_DEPTH_RAWZ
 //
 // NVIDIA packed RAWZ format
 //
 return dot(tex2D(IN_HDRDepthMap_Sampler, UV).arg, float3(0.996093809371817670572857294849, 0.0038909914428586627756752238080039, 1.5199185323666651467481343000015e-5));
#else
#ifdef USE_DEPTH_INTZ
 //
 // NVIDIA 8xxx series INTZ format
 //
 return tex2D(IN_HDRDepthMap_Sampler, UV).r;
#else
 //
 // XBOX EDRAM depth format
 //
 return tex2D(IN_HDRDepthMap_Sampler, UV).r;
#endif
#endif
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//																												//
//												Useful functions												//
//																												//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
half GetAdaptiveLuminanceFctr()
{
 half ADPLum = tex2D(IN_LuminanceSampler, float2(0.5, 0.5)).r;
 ADPLum      = clamp(ADPLum, DYNAMIC_LUM_CLAMPMIN, DYNAMIC_LUM_CLAMPMAX);
 
 return ADPLum;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
half3 changeSaturation(half3 color, half change) 
{
 const half SAT_Pr = 0.241;
 const half SAT_Pg = 0.691;
 const half SAT_Pb = 0.068;

 half P = ((color.x*color.x*SAT_Pr) +
		   (color.y*color.y*SAT_Pg) +
		   (color.z*color.z*SAT_Pb) );
 P += half(0.005);
 P  = sqrt(P);
  
 half3 res;
  
 res.x = P + ((color.x-P)*change);
 res.y = P + ((color.y-P)*change);
 res.z = P + ((color.z-P)*change);
 
 return res;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
half3 BlueShift(half3 color, half BlueShift)
{ 
 half3 RODColor = dot(color, LUM_VEC) * BS_VEC;  
 return lerp(color, RODColor, saturate(BlueShift));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
half3 SuppressLDR(half4 c)
{
#ifndef HDR_USE_ADAPTIVE_LUMINANCE
 
 if (dot(LUM_VEC, c.rgb) > IN_HDRCutoff)
 {
#ifndef HDR_IS_A_FAKE 
     return c.rgb;
#else
	 return c.rgb * HDR_FAKE_DEFLATE;
#endif     
 }
 else
 {
     return 0;
 }
#else
 float AdaptedLum = lerp(GetAdaptiveLuminanceFctr(),
						 DEFAULT_SCENE_LUM, 
						 IN_SceneLuminanceFactor);

 //
 // Determine what the pixel's value will be after tone-mapping occurs
 //
 c.rgb *= IN_HDRDefog / (AdaptedLum + 0.001f);

 //	
 // Subtract out dark pixels
 //
 c.rgb -= IN_HDRCutoff;
 c      = max(c, 0.0f);

 //	
 // Map the resulting value into the 0 to 1 range. Higher values for
 // BRIGHT_PASS_OFFSET will isolate lights from illuminated scene 
 // objects.
 //
#ifdef USE_TONE_MAPPING_LDR 
 c.rgb /= (1.0f + c);
#endif
 
#ifndef HDR_IS_A_FAKE 
  return c.rgb;
#else
  return c.rgb * HDR_FAKE_DEFLATE;
#endif  
#endif 
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
half4 BlinFilter(sampler2D Sampler, float2 UV, float4 BlurOffset)
{
 float4 SampleA = tex2D(Sampler, UV.xy);
 float4 SampleB = tex2D(Sampler, UV.xy + float2(BlurOffset.x, 0));
 float4 SampleC = tex2D(Sampler, UV.xy + float2(0, BlurOffset.y));
 float4 SampleD = tex2D(Sampler, UV.xy + float2(BlurOffset.x, BlurOffset.y));
 
 float2 Lerps   = frac(UV.xy * BlurOffset.zw);
 
 return lerp(lerp(SampleA, SampleB, Lerps.x),
             lerp(SampleC, SampleD, Lerps.x),
             Lerps.y);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
half4 BlurDream(sampler2D tex, float2 UV, float2 Size)
{
#ifdef SHADERS_PROFILE_2B
 half4 tmp = tex2D(tex, UV.xy + float2(-Size.x,       0)) * 0.25;
       tmp+= tex2D(tex, UV.xy + float2(	     0, -Size.y)) * 0.25;
       tmp+= tex2D(tex, UV.xy + float2(      0,  Size.y)) * 0.25;
       tmp+= tex2D(tex, UV.xy + float2( Size.x,       0)) * 0.25;
 return tmp;              
#else
 return tex2D(tex, UV.xy);
#endif
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
half4 SampleDreamlandTextureNrm(sampler2D tex, float2 UV, float4 TexOffsets, half4 Normal)
{
 half4 Dream  = BlurDream(tex, UV, TexOffsets.xy * 1.25);
 
 float lum    = saturate(dot(Dream.rgb, float3(0.2, 0.7, 0.3)));
	   lum    = pow(lum, 2.0);
 Dream.rgb    = 1.0 - saturate(Dream.rgb);
 Dream.rgb    = pow(Dream.rgb, 16.0);
 Dream.rgb   *= 64.0;
 Dream.rgb    = 1.0 - saturate(Dream.rgb / 64.0);
 Dream.rgb    = pow(Dream.rgb, 6.0);
 Dream.rgb   *= half3(saturate(lum * 4.0), saturate(lum * 4.0), saturate(lum * 8.0));
 
 return lerp(Normal, saturate(Dream), IN_DreamlandFactor);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
half4 SampleDreamlandTexture(sampler2D tex, float2 UV, float4 TexOffsets)
{
 half4 Normal = saturate(tex2D(tex, UV + TexOffsets.xy));
 return SampleDreamlandTextureNrm(tex, UV, TexOffsets, Normal);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_PRT(appdata_HDRBASE IN)
{
 pixelOutput OUT;

 half4 depth     = tex2D(IN_DissortSampler,    IN.UV.xy);
#ifndef _XBOX 
 half4 original  = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + IN_HDRBaseTextureA_TEX.xy);
#else
 half4 original  = tex2D(IN_HDRBASEA_Sampler,  IN.UV.xy + IN_HDRBaseTextureA_TEX.xy);
#endif 
 
 //
 // depth.g --> Xdirection distorsion
 // depth.b --> Ydirection distorsion
 // depth.a --> 1 - distorsion force
 //
 float2 diss;
		diss.x = ((depth.g * 2.0) - 1.0) * 0.1;
		diss.y = ((depth.b * 2.0) - 1.0) * 0.1;
 half dintense = depth.a;	

 half4 swizled = BlinFilter(IN_HDRBASEAP_Sampler, IN.UV.xy + diss, IN_HDRBaseTextureA_TEX);
 
 half4 sdepth  = tex2D(IN_DissortSampler, IN.UV.xy + diss);
 if ((sdepth.r + (1.0 / 255.0)) < depth.r)
    dintense = 1.0;
 
 OUT.color.rgb = lerp(swizled, original, dintense);
 OUT.color.a   = half(1.0);

 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderPRTDIS
{
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		
		PixelShader		 = compile ps_2_0 psHDRBase_PRT();
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_PRT_DRM(appdata_HDRBASE IN)
{
 pixelOutput OUT;
 
 half4 depth     = tex2D(IN_DissortSampler, IN.UV.xy);
#ifndef _XBOX  
 half4 original  = SampleDreamlandTexture(IN_HDRBASEAP_Sampler, IN.UV.xy, IN_HDRBaseTextureA_TEX);
#else
 half4 original  = SampleDreamlandTexture(IN_HDRBASEA_Sampler,  IN.UV.xy, IN_HDRBaseTextureA_TEX);
#endif
 
 //
 // depth.g --> Xdirection distorsion
 // depth.b --> Ydirection distorsion
 // depth.a --> 1 - distorsion force
 //
 float2 diss;
		diss.x = ((depth.g * 2.0) - 1.0) * 0.1;
		diss.y = ((depth.b * 2.0) - 1.0) * 0.1;
 half dintense = depth.a;	
 
 half4 swizled = SampleDreamlandTexture(IN_HDRBASEA_Sampler, IN.UV.xy + diss, IN_HDRBaseTextureA_TEX);
 
 half4 sdepth = tex2D(IN_DissortSampler, IN.UV.xy + diss);
 if ((sdepth.r+(1.0 / 255.0)) < depth.r)
      dintense = 1.0;
 
 OUT.color.rgb = lerp(swizled, original, dintense);
 OUT.color.a   = half(1.0);

 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderPRTDIS_DRM
{
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		
		PixelShader		 = compile ps_2_0 psHDRBase_PRT_DRM();
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDR_LDR(appdata_HDRBASE IN)
{
 pixelOutput OUT;
 
 half4 tmp     = tex2D(IN_HDRBASEA_Sampler, IN.UV);
#ifndef HDR_USE_ADAPTIVE_LUMINANCE 
 tmp.rgb       = changeSaturation(tmp.xyz, IN_HDRSaturationHDR);
#endif 
 OUT.color.rgb = SuppressLDR(tmp);

 //
 // Calculate luminance component
 //
 OUT.color.a   = saturate(dot(LUM_VEC, tmp.rgb) * 0.5);
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderHDR_LDR
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

		PixelShader		 = compile ps_2_0 psHDR_LDR();
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef  SHADERS_PROFILE_2B
#define ALTERNATIVE_BLOOM_FILTER
#endif

#ifdef ALTERNATIVE_BLOOM_FILTER
#define BLOOM_FILTER_MIN_ITERATION -14
#define BLOOM_FILTER_MAX_ITERATION  14

float BLOOMFilterKelnelWeight(float i)
{
 float rho = saturate(abs(i) / (float)BLOOM_FILTER_MAX_ITERATION);
 return (1.0 - (rho*rho)) * 0.05;
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDR_X(appdata_HDRBASE IN)
{
 pixelOutput OUT;

#ifdef USE_XY_BLOOM
 float3 vSample = 0.0f;
 float3 vColor  = 0.0f;
 float2 vSamplePosition;
 
#ifdef ALTERNATIVE_BLOOM_FILTER 
 for (int i = BLOOM_FILTER_MIN_ITERATION; i <= BLOOM_FILTER_MAX_ITERATION; i++)
 {
	 vSamplePosition = IN.UV + float2(IN_HDRBaseTextureA_TEX.x * (float)i, 0);
	 vColor          = tex2D(IN_HDRBASEAP_Sampler, vSamplePosition).rgb;
	 float TWh       = BLOOMFilterKelnelWeight(i);
	 vSample        += (vColor.xyz * BLOOM_BLUR_MUL) * TWh;
 }
#else
 for (int i = 0; i < 15; i++)
 {
      vSamplePosition = IN.UV + float2(IN_BloomTable[i].w, 0);
      vColor          = tex2D(IN_HDRBASEAP_Sampler, vSamplePosition).rgb;
      vSample        += IN_BloomTable[i].xyz * (vColor.xyz * BLOOM_BLUR_MUL);
 }
#endif
 
 OUT.color.rgb = vSample.rgb;
 OUT.color.a   = 1.0;
#else
 OUT.color.rgb = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy).rgb;
 OUT.color.a   = 1.0;
#endif 
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderHDRX
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		
		PixelShader		 = compile ps_2_0 psHDR_X();
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDR_Y(appdata_HDRBASE IN)
{
 pixelOutput OUT;

#ifdef USE_XY_BLOOM
 float3 vSample = 0.0f;
 float3 vColor  = 0.0f;
 float2 vSamplePosition;
 
#ifdef ALTERNATIVE_BLOOM_FILTER 
 for (int i = BLOOM_FILTER_MIN_ITERATION; i <= BLOOM_FILTER_MAX_ITERATION; i++)
 {
	 vSamplePosition = IN.UV + float2(0, IN_HDRBaseTextureA_TEX.y * (float)i);
	 vColor          = tex2D(IN_HDRBASEAP_Sampler, vSamplePosition).rgb;
	 float TWh       = BLOOMFilterKelnelWeight(i);
	 vSample        += (vColor.xyz * BLOOM_BLUR_MUL) * TWh;
 }
#else   
 for (int i = 0; i < 15; i++)
 {
      vSamplePosition = IN.UV + float2(0, IN_BloomTable[i].w);
      vColor          = tex2D(IN_HDRBASEAP_Sampler, vSamplePosition).rgb;
      vSample        += IN_BloomTable[i].xyz * (vColor.xyz * BLOOM_BLUR_MUL);
 }
#endif
 
 OUT.color.rgb = vSample.rgb;
 OUT.color.a   = 1.0;
#else
 OUT.color.rgb = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy).rgb;
 OUT.color.a   = 1.0;
#endif 

 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderHDRY
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

		PixelShader		 = compile ps_2_0 psHDR_Y();
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
half3 GetNormalHDRValue(half4 original, half4 blur, float2 OriginalUV)
{
#ifdef DEBUG_BLOOM
#ifdef HDR_IS_A_FAKE
	   blur     *= (1.0 / HDR_FAKE_DEFLATE);
#endif
	   return blur.rgb;
#else
#ifdef DEBUG_STARS
 return tex2D(IN_StarsSampler, OriginalUV.xy).rgb;
#else
#ifdef HDR_IS_A_FAKE
	   blur     *= (1.0 / HDR_FAKE_DEFLATE);
#endif

#ifndef HDR_USE_ADAPTIVE_LUMINANCE

	   blur     *= half(IN_GlowMull);
 half3 color	 = original.rgb + blur.rgb;
	   color     = max(0, color.rgb - half(IN_HDRDefog));
 color          *= half(IN_ExposureLevel);

 color           = changeSaturation(color, IN_HDRSaturationLDR);
 color			 = BlueShift(color, IN_HDRBlueShifting);
 
#else

 float AdaptedLum = lerp(GetAdaptiveLuminanceFctr(),
						 DEFAULT_SCENE_LUM, 
						 IN_SceneLuminanceFactor);
 
 //
 // For very low light conditions, the rods will dominate the perception
 // of light, and therefore color will be desaturated and shifted
 // towards blue.
 //
#ifdef SHADERS_PROFILE_30  
 if (!IN_UseHistogramEqualization)
 {
#endif 
	 float BlueShiftCoefficient = saturate(1.0f - (AdaptedLum + 1.5) / 4.1) * IN_HDRBlueShifting;
	 original.rgb = BlueShift(original.rgb, BlueShiftCoefficient);
#ifdef SHADERS_PROFILE_30 	 
 }
#endif 

 //
 // Map the high range of color values into a range appropriate for
 // display, taking into account the user's adaptation level, and selected
 // values for for middle gray and white cutoff.
 //
 original.rgb *= IN_ExposureLevel / (AdaptedLum + 0.001f);
 
 //
 // Tonemap L = Y / (1 + Y);
 //
#ifdef USE_TONE_MAPPING_HDR 
 original.rgb /= (1.0f+original.rgb);
#endif

#ifdef SHADERS_PROFILE_30
 if (IN_UseHistogramEqualization)
 {
	 //
	 // Histogram equalize original HDR image
	 //
	 half3 original_heq;
	 original_heq.r = tex2D(IN_CDFSampler, saturate(float2(original.r, 0.5))).r;
	 original_heq.g = tex2D(IN_CDFSampler, saturate(float2(original.g, 0.5))).g;
	 original_heq.b = tex2D(IN_CDFSampler, saturate(float2(original.b, 0.5))).b;
	 original.rgb   = original_heq;
 }
#endif 
 
 blur       *= half(IN_GlowMull);
 blur.rgb    = changeSaturation(blur.xyz, IN_HDRSaturationHDR);
 half3 color = original.rgb + blur.rgb;
 
 //
 // Apply stars
 //
#ifdef HDR_USE_STARS 
 color += tex2D(IN_StarsSampler, OriginalUV.xy).rgb * IN_HDRStarsMull;
#endif 

 // if (OriginalUV.x > 0.95 && OriginalUV.y < 0.05)
 //     color = AdaptedLum;
 
#ifdef SHADERS_PROFILE_30 
 /*
 //
 // Debug show histogram
 //
 if (OriginalUV.x > 0.5 && OriginalUV.y < 0.02)
	 color = float3(tex2D(IN_CDFSampler, float2((OriginalUV.x-0.5)*2.0, 0.5)).r, 0, 0);
	 
 if (OriginalUV.x > 0.5 && OriginalUV.y > 0.02 && OriginalUV.y < 0.04)
	 color = float3(0, tex2D(IN_CDFSampler, float2((OriginalUV.x-0.5)*2.0, 0.5)).g, 0);

 if (OriginalUV.x > 0.5 && OriginalUV.y > 0.04 && OriginalUV.y < 0.06)
	 color = float3(0, 0, tex2D(IN_CDFSampler, float2((OriginalUV.x-0.5)*2.0, 0.5)).b);
 */
#endif
 
#endif 
 
 return color;
#endif 
#endif

 /*
 //
 // Pure equalization witchout additional effects
 //
 
 half3 color = original.rgb;
 
 //
 // Histogram equalization
 //
 half3 hcolor;
 hcolor.r = tex2D(IN_CDFSampler, saturate(float2(color.r, 0.5))).r;
 hcolor.g = tex2D(IN_CDFSampler, saturate(float2(color.g, 0.5))).g;
 hcolor.b = tex2D(IN_CDFSampler, saturate(float2(color.b, 0.5))).b;
 
 color    = hcolor;

 //
 // Debug show histogram
 //
 if (OriginalUV.x > 0.5 && OriginalUV.y < 0.018)
	 color = tex2D(IN_CDFSampler, float2((OriginalUV.x-0.5)*2.0, 0.5)).r;
	 
 if (OriginalUV.x > 0.5 && OriginalUV.y > 0.02 && OriginalUV.y < 0.038)
	 color = tex2D(IN_CDFSampler, float2((OriginalUV.x-0.5)*2.0, 0.5)).g;

 if (OriginalUV.x > 0.5 && OriginalUV.y > 0.04 && OriginalUV.y < 0.058)
	 color = tex2D(IN_CDFSampler, float2((OriginalUV.x-0.5)*2.0, 0.5)).b;
	 
 return color;
 */
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SampleDirectionalBlurHDR(in float2 UV, inout half4 originalA, inout half4 blurA, uniform int LoopCnt)
{
#ifdef SHADERS_PROFILE_2B

 float2 ScaledUV = UV.xy - IN_HDRBlurCoefs.zw;
 float  Distance = length(ScaledUV.xy);
	    Distance = saturate(Distance * 2.0);
		Distance = pow(Distance, IN_HDRBlurCoefs.y);
		
 half4 original  = originalA;
 half4 blur      = blurA;
 
 [unroll]
 for (int i = 1; i < LoopCnt; i++)
 {
	float smpDistance = 1.0 / (float)i;

	float2 FinalUV  = ScaledUV * (1.0 + (IN_HDRBlurCoefs.x * smpDistance));
		   FinalUV += IN_HDRBlurCoefs.zw;
	
	half4 originalB = tex2D(IN_HDRBASEAP_Sampler, FinalUV.xy + IN_HDRBaseTextureA_TEX.xy);
	half4 blurB	    = tex2D(IN_HDRBASEB_Sampler,  FinalUV.xy + IN_HDRBaseTextureB_TEX.xy);
	
	original += lerp(originalA, originalB, Distance);
	blur     += lerp(blurA,     blurB,     Distance);
 }
 
 originalA = original / (float)LoopCnt;
 blurA     = blur     / (float)LoopCnt;
 
#endif 
 return;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase(appdata_HDRBASE IN, uniform bool UseHDRBlur)
{
 //
 // Plain HDR witchout any other effects
 //
 pixelOutput OUT;

#ifndef _XBOX
 half4 original = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + IN_HDRBaseTextureA_TEX.xy);
#else
 half4 original = tex2D(IN_HDRBASEA_Sampler,  IN.UV.xy + IN_HDRBaseTextureA_TEX.xy);
#endif 
 half4 blur  	= tex2D(IN_HDRBASEB_Sampler,  IN.UV.xy + IN_HDRBaseTextureB_TEX.xy);
 
 if (UseHDRBlur)
 {
	 SampleDirectionalBlurHDR(IN.UV, original, blur, 8);
 }
 
 OUT.color.rgb  = GetNormalHDRValue(original, blur, IN.UV.xy);
 OUT.color.a    = half(1.0);
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderHDRBase
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
		PixelShader		 = compile ps_3_0_wa psHDRBase(IN_HDRBlurUse);
#else
		VertexShader	 = compile vs_3_0 XBOXSimpleVS();
		ViewportEnable   = False;
		PixelShader		 = compile ps_3_0 psHDRBase(IN_HDRBlurUse);
#endif		
		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBaseDIS(appdata_HDRBASE IN, uniform bool UseHDRBlur)
{
 pixelOutput OUT;

 half4 depth    = tex2D(IN_DissortSampler,    IN.UV.xy);
#ifndef _XBOX
 half4 original = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + IN_HDRBaseTextureA_TEX.xy);
#else
 half4 original = tex2D(IN_HDRBASEA_Sampler,  IN.UV.xy + IN_HDRBaseTextureA_TEX.xy);
#endif 
 half4 blur	    = tex2D(IN_HDRBASEB_Sampler,  IN.UV.xy + IN_HDRBaseTextureB_TEX.xy);
 
 if (UseHDRBlur)
 {
	 SampleDirectionalBlurHDR(IN.UV, original, blur, 8);
 }
 
 //
 // depth.g --> Xdirection distorsion
 // depth.b --> Ydirection distorsion
 // depth.a --> 1 - distorsion force
 //
 float2 diss;
		diss.x = ((depth.g * 2.0) - 1.0) * 0.1;
		diss.y = ((depth.b * 2.0) - 1.0) * 0.1;
 half dintense = depth.a;	

#ifndef _XBOX
 half4 swizled = BlinFilter(IN_HDRBASEAP_Sampler, IN.UV.xy + diss, IN_HDRBaseTextureA_TEX);
#else
 half4 swizled = BlinFilter(IN_HDRBASEA_Sampler, IN.UV.xy + diss, IN_HDRBaseTextureA_TEX);
#endif 
 
 half4 sdepth  = tex2D(IN_DissortSampler, IN.UV.xy + diss);
 if ((sdepth.r + (1.0 / 255.0)) < depth.r)
    dintense = 1.0;
 
 original       = lerp(swizled, original, dintense);
 
 OUT.color.rgb  = float3(1,0,0);//GetNormalHDRValue(original, blur, IN.UV.xy);
 OUT.color.a    = half(1.0);
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderHDRBaseDIS
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
		PixelShader		 = compile ps_3_0_wa psHDRBaseDIS(IN_HDRBlurUse);
#else
		VertexShader	 = compile vs_3_0 XBOXSimpleVS();
		ViewportEnable   = False;
		PixelShader		 = compile ps_3_0 psHDRBaseDIS(IN_HDRBlurUse);
#endif		
		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_DRM(appdata_HDRBASE IN, uniform bool UseHDRBlur)
{
 pixelOutput OUT;
 
#ifndef _XBOX 
 half4 original = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + IN_HDRBaseTextureA_TEX.xy);
#else
 half4 original = tex2D(IN_HDRBASEA_Sampler,  IN.UV.xy + IN_HDRBaseTextureA_TEX.xy);
#endif 
 half4 blur	    = tex2D(IN_HDRBASEB_Sampler,  IN.UV.xy + IN_HDRBaseTextureB_TEX.xy);
 
 if (UseHDRBlur)
 {
	 SampleDirectionalBlurHDR(IN.UV, original, blur, 8);
 }
 
 half3 hdred    = GetNormalHDRValue(original, blur, IN.UV.xy);
 OUT.color      = SampleDreamlandTextureNrm(IN_HDRBASEAP_Sampler, IN.UV.xy, IN_HDRBaseTextureA_TEX, half4(hdred, 1.0));
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderHDRBase_DRM
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
		PixelShader		 = compile ps_3_0_wa psHDRBase_DRM(IN_HDRBlurUse);
#else
		VertexShader	 = compile vs_3_0 XBOXSimpleVS();
		ViewportEnable   = False;
		PixelShader		 = compile ps_3_0 psHDRBase_DRM(IN_HDRBlurUse);
#endif		
		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBaseDIS_DRM(appdata_HDRBASE IN, uniform bool UseHDRBlur)
{
 pixelOutput OUT;
 
#ifndef _XBOX 
 half4 original = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + IN_HDRBaseTextureA_TEX.xy);
#else
 half4 original = tex2D(IN_HDRBASEA_Sampler,  IN.UV.xy + IN_HDRBaseTextureA_TEX.xy);
#endif 
 half4 blur	    = tex2D(IN_HDRBASEB_Sampler,  IN.UV.xy + IN_HDRBaseTextureB_TEX.xy);
 
 if (UseHDRBlur)
 {
	 SampleDirectionalBlurHDR(IN.UV, original, blur, 8);
 }
 
#ifdef SHADERS_PROFILE_2B 
 half4 depth    = tex2D(IN_DissortSampler,    IN.UV.xy);
 
 //
 // depth.g --> Xdirection distorsion
 // depth.b --> Ydirection distorsion
 // depth.a --> 1 - distorsion force
 //
 float2 diss;
		diss.x = ((depth.g * 2.0) - 1.0) * 0.1;
		diss.y = ((depth.b * 2.0) - 1.0) * 0.1;
 half dintense = depth.a;	

 half4 swizled = BlinFilter(IN_HDRBASEAP_Sampler, IN.UV.xy + diss, IN_HDRBaseTextureA_TEX);
 
 half4 sdepth  = tex2D(IN_DissortSampler, IN.UV.xy + diss);
 if ((sdepth.r + (1.0 / 255.0)) < depth.r)
    dintense = 1.0;
 
 original       = lerp(swizled, original, dintense);
#endif
 
 half3 hdred    = GetNormalHDRValue(original, blur, IN.UV.xy);
 OUT.color      = SampleDreamlandTextureNrm(IN_HDRBASEAP_Sampler, IN.UV.xy, IN_HDRBaseTextureA_TEX, half4(hdred, 1.0));
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderHDRBaseDIS_DRM
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
		PixelShader		 = compile ps_3_0_wa psHDRBaseDIS_DRM(IN_HDRBlurUse);
#else
		VertexShader	 = compile vs_3_0 XBOXSimpleVS();
		ViewportEnable   = False;
		PixelShader		 = compile ps_3_0 psHDRBaseDIS_DRM(IN_HDRBlurUse);
#endif		
		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
half4 BlurMe(float2 UV, float2 Size)
{
 half4 tmp = tex2D(IN_HDRBASEA_Sampler, UV.xy + float2(-Size.x, -Size.y)) * 0.0769;
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2(	     0, -Size.y)) * 0.0769;
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2( Size.x, -Size.y)) * 0.0769;
       
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2(-Size.x,       0)) * 0.0769;
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2(	     0,       0)) * 0.0769;
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2( Size.x,	      0)) * 0.0769;
       
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2(-Size.x,  Size.y)) * 0.0769;
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2(      0,  Size.y)) * 0.0769;
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2( Size.x,  Size.y)) * 0.0769;
       
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2(        0,  Size.y*2)) * 0.0769;
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2(        0, -Size.y*2)) * 0.0769;
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2( Size.x*2,         0)) * 0.0769;
       tmp+= tex2D(IN_HDRBASEA_Sampler, UV.xy + float2(-Size.x*2,         0)) * 0.0769;
       
 return tmp;       
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_DOF(appdata_HDRBASE IN, uniform bool UseHDRBlur)
{
 pixelOutput OUT;

 half4 depth = tex2D(IN_DissortSampler, IN.UV.xy);
 depth.r = SAMPLE_DEPTH_MAP(IN.UV.xy);
 
 //
 // depth.g --> Xdirection distorsion
 // depth.b --> Ydirection distorsion
 // depth.a --> 1 - distorsion force
 //
 float2 diss;
		diss.x = ((depth.g * 2.0) - 1.0) * 0.1;
		diss.y = ((depth.b * 2.0) - 1.0) * 0.1;
 half dintense = depth.a;	

#ifndef _XBOX 
 half4 original = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + IN_HDRBaseTextureA_TEX.xy); 
 half4 swizled  = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + diss + IN_HDRBaseTextureA_TEX.xy);
#else
 half4 original = tex2D(IN_HDRBASEA_Sampler,  IN.UV.xy + IN_HDRBaseTextureA_TEX.xy); 
 half4 swizled  = tex2D(IN_HDRBASEA_Sampler, IN.UV.xy + diss + IN_HDRBaseTextureA_TEX.xy);
#endif 

 half4 sdepth = tex2D(IN_DissortSampler, IN.UV.xy + diss);
 if ((sdepth.r+(1.0 / 255.0)) < depth.r)
     dintense = 1.0;
 
 original = lerp(swizled, original, dintense);

#ifdef SHADERS_PROFILE_2B  
 //
 // Do DOF
 // 
 //half4 blured = BlurMe(IN.UV.xy, IN_HDRBaseTextureA_TEX.xy);
 //half LFactor = min(1.0, max(0.0, abs(depth.r - IN_DOF_Focus) - IN_DOF_Tolerance) * IN_DOF_ToleranceInv);
 //original.rgb = lerp(original.rgb, blured.rgb, LFactor);
#endif 

 //
 // Apply HDR
 //
 half4 blur	     = tex2D(IN_HDRBASEB_Sampler, IN.UV.xy + IN_HDRBaseTextureB_TEX.xy);
 
 if (UseHDRBlur)
 {
	//SampleDirectionalBlurHDR(IN.UV, original, blur, 8);
 }
 
 half3 color = original.rgb;
 float2 TC = IN.UV.xy;
 
 // Screen Space Velocity Motion Blur
  const float blurScale = 0.0;
  float zOverW = depth.r;
  //float4 H = float4(IN.UV.x * 2.0 - 1.0, IN.UV.y * 2.0 - 1.0, zOverW, 1.0);
  float4 H = float4(IN.UV.x * 2 - 1, (1 - IN.UV.y) * 2 - 1, zOverW , 1);
  float4 D = mul(H, IN_CurrWorldViewProjectionMatrixInv);
  float4 worldPos = D/D.w;
  
  float4 currentPos = H;
  float4 prevPos = mul(worldPos, IN_PrevWorldViewProjectionMatrix);
  prevPos /= prevPos.w;
  float2 velocity = (currentPos - prevPos)*blurScale;
  
  //float bound = 0.0004*blurScale;
  
  //if((velocity.x < bound) && (velocity.x > -bound)) velocity.x = 0.0;
  //if((velocity.y < bound) && (velocity.y > -bound)) velocity.y = 0.0;
  
  TC = IN.UV.xy;
  
  //color = tex2D(IN_HDRBASEAP_Sampler, TC);
  
  int MBLUR_SAMPLES = 8;
  
  for(int i=0; i< MBLUR_SAMPLES; i++)
  {
    TC += velocity;
	half3 Sample = tex2D(IN_HDRBASEAP_Sampler, TC);
	color += Sample;
  }
  
  color.rgb /= MBLUR_SAMPLES;
  
 
  /*// Sobel Depth
  const float offset = 1.0 / 512.0;
  half3 c = tex2D(IN_DissortSampler, IN.UV.xy);
  half3 edge = tex2D(IN_DissortSampler, IN.UV.xy + float2(-offset, -offset)) +
  tex2D(IN_DissortSampler, IN.UV.xy + float2(-offset, 0.0)) +
  tex2D(IN_DissortSampler, IN.UV.xy + float2(-offset, offset)) +
  tex2D(IN_DissortSampler, IN.UV.xy + float2( 0.0, offset)) +
  tex2D(IN_DissortSampler, IN.UV.xy + float2( offset, offset)) +
  tex2D(IN_DissortSampler, IN.UV.xy + float2( offset, 0.0)) +
  tex2D(IN_DissortSampler, IN.UV.xy + float2( offset, -offset)) +
  tex2D(IN_DissortSampler, IN.UV.xy + float2( 0.0, -offset));

  edge.rgb = 8.0 * (c + -0.125 * edge);
  */
  
  
  //FEAA gradiental difference
  const float offset = 1.0 / 1024.0;
  const half barrier = 0.0005;
  const half kernel = 0.0007;//0.003;
  const half weights = 0.1;
  
  float2 tc0 = IN.UV.xy;
  float2 tc1 = IN.UV.xy + float2(-offset, offset);
  float2 tc2 = IN.UV.xy + float2(offset, -offset);
  float2 tc3 = IN.UV.xy + float2(offset, offset);
  float2 tc4 = IN.UV.xy + float2( -offset, -offset);
  float2 tc5 = IN.UV.xy + float2( -offset, 0.0);
  float2 tc6 = IN.UV.xy + float2( offset, 0.0);
  float2 tc7 = IN.UV.xy + float2( 0.0, offset);
  float2 tc8 = IN.UV.xy + float2( 0.0, -offset);  
  
  float c = SAMPLE_DEPTH_MAP(tc0);
  half4 edge;
  edge.r = SAMPLE_DEPTH_MAP(tc1) + SAMPLE_DEPTH_MAP(tc2);
  edge.g = SAMPLE_DEPTH_MAP(tc3) + SAMPLE_DEPTH_MAP(tc4);
  edge.b = SAMPLE_DEPTH_MAP(tc5) + SAMPLE_DEPTH_MAP(tc6);
  edge.a = SAMPLE_DEPTH_MAP(tc7) + SAMPLE_DEPTH_MAP(tc8);
  
  edge = abs(2 * c.r - edge)-barrier;
  edge = step(edge,0.0);
  
  half e = saturate(dot(edge,weights));
  
  half w = (1 - edge)*kernel;
  
 
  float2 AAOffset = tc0 * (1.0-w);
  half4 AASample = tex2D(IN_HDRBASEAP_Sampler, AAOffset + tc1 * w) +
  tex2D(IN_HDRBASEAP_Sampler, AAOffset + tc2 * w) +
  tex2D(IN_HDRBASEAP_Sampler, AAOffset + tc3 * w) +
  tex2D(IN_HDRBASEAP_Sampler, AAOffset + tc4 * w)+
  tex2D(IN_HDRBASEAP_Sampler, AAOffset);
  
  AASample /= 5.h;
  
  color.rgb = AASample.rgb;
  
  if(w > 0.0001) color.rgb = BlurMe(tc0,w);

  
  /*
  //const float offset = 1.0 / 512.0;
  half3 c = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy);
  half3 edge = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-offset, -offset)) +
  tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-offset, 0.0)) +
  tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-offset, offset)) +
  tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( 0.0, offset)) +
  tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( offset, offset)) +
  tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( offset, 0.0)) +
  tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( offset, -offset)) +
  tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( 0.0, -offset));

  edge.rgb = 8.0 * (c + -0.125 * edge) * edged;
  */
  
  /*
  half4 blured = BlurMe(IN.UV.xy, 0.002);

  original.rgb = lerp(original.rgb, blured.rgb, edge.r);
  original.rgb = w;
  */
  
/*
  half dBlurScale = 0.00007;
  half heatScale = 0.00008;
  TC = IN.UV.xy;
  
 const float Length = 1000; //500
 const float amplitude  = 800;//200
 const float speed      = 0.5;//0.5

 const float frequency  = 2.0 * 3.14159 / Length;
 const float phase      = speed * frequency;
  
  float waveCos = frequency * amplitude * cos(frequency * Length - phase * IN_CurrentTimeINMS);
  
  TC.y += sin(TC.x*200+IN_CurrentTimeINMS/100)*smoothstep(0.924,0.9245,depth.r) * heatScale ;
  
  //TC.x += sin(TC.y * 50 + IN_CurrentTimeINMS/1000) * smoothstep(0.8,1.0,(1-depth.r)) * 0.008;// * waveCos;
  //TC.y += waveCos * smoothstep(0.8,1.0,(1-depth.r)) * 0.004;
  
  //TC.x += waveCos * smoothstep(0.9,1.0,(1-depth.r)) * heatScale * sin(TC.x*50+IN_CurrentTimeINMS/1000)*smoothstep(0.9,1.0,(1-depth.r)) * heatScale ;
  
  //if(depth.r > 0.925)
  {
    color.rgb = lerp(BlurMe(TC,dBlurScale*smoothstep(0.924,0.94,depth.r)), color.rgb , 0);//100*length(velocity)
	//color.rgb = float3(1,0,0);
  }
*/
  
  /*
  //GodRays

 float Density = 0.2;
 float Weight = 0.08;
 float Decay = 0.9;
 int Samples = 16;
 float efWeight = Weight;
 
  TC = IN.UV.xy;
  float2 deltaTC = TC - IN_ScreenspaceSunPos.xy;
    
  //half3 color = tex2D(IN_HDRBASEAP_Sampler, TC);
  
  half illDecay = 1.0;
  
  int NUM_SAMPLES = Samples;
  deltaTC *= 1.0/NUM_SAMPLES * Density;
  
  if(length(deltaTC) > 0.01) deltaTC = normalize(deltaTC) * 0.01;
  
  //efWeight *= (1.0-color.b);
  
  /*
  //half3 EdgeCheck = tex2D(IN_DissortSampler, TC + deltaTC);
  
  //efWeight *= (1.0-color.b);
  
  
  if(depth.r < 1){
  
  for(int i=0; i< NUM_SAMPLES; i++)
  {
    TC -= deltaTC;
	//if(tex2D(IN_DissortSampler, TC.xy).r > 0.0)
	half3 Sample = tex2D(IN_HDRBASEAP_Sampler, TC);
	Sample *= illDecay * efWeight*(1.0-Sample.b);
	color += Sample;
	illDecay *= Decay;
	//color = half3(1.0,0.0,0.0);
  }
  }*/
  
/*
  [branch]if(depth.r < 0.97){
  
  [loop]for(int i=0; i< NUM_SAMPLES; i++)
  {
    TC -= deltaTC;
	//if(tex2D(IN_DissortSampler, TC.xy).r > 0.0)
	half3 Sample = tex2Dlod(IN_HDRBASEAP_Sampler, float4(TC,0,0));
	Sample *= illDecay * efWeight*(1.0-color.b);
	color += Sample;
	illDecay *= Decay;
  }
  }
  else
  {
  [loop]for(int i=0; i< 8; i++)
  {
    TC -= deltaTC;
	//if(tex2D(IN_DissortSampler, TC.xy).r > 0.0)
	half3 Sample = tex2Dlod(IN_HDRBASEAP_Sampler, float4(TC,0,0));
	Sample *= illDecay * efWeight*(1.0-color.b);
	color += Sample;
	illDecay *= Decay;
	//color = half3(1.0,0.0,0.0);
  }
  }
  */
  
  original.rgb = color.rgb;
  
  //half3 AO = depth.r - BlurMe(IN.UV.xy, IN_HDRBaseTextureA_TEX.xy);
   
 //original.rgb = Godray(IN.UV.xy,IN_ScreenspaceSunPos,0.05,0.08,0.95,16);
 OUT.color.rgb   = GetNormalHDRValue(original, blur, IN.UV.xy);
 
 //original.rgb = GetNormalHDRValue(original, blur, IN.UV.xy);
 //OUT.color.rgb   = Godray(IN.UV.xy,IN_ScreenspaceSunPos,0.1,0.15,0.9,16);
if (IN.UV.x > 0.95 && IN.UV.y < 0.05)
      OUT.color.rgb = half3(IN_ScreenspaceSunPos.w,IN_ScreenspaceSunPos.w,IN_ScreenspaceSunPos.w);
 
 //float scale = 1.0;
 
 //float AA = saturate(scale*depth.r+0.0) - saturate(scale*BlurDepth(IN.UV.xy, 0.003).r);
 
 //OUT.color.rgb = saturate(scale*depth.r+0.0) - saturate(scale*BlurDepth(IN.UV.xy, 0.005).r);
 
 //OUT.color.rgb = lerp(original,half3(1.0,0.0,0.0),2.0*AA);//lurMe(IN.UV.xy, IN_HDRBaseTextureA_TEX.xy)
 
 OUT.color.a     = half(1.0);
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderHDRBase_DOF
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
		PixelShader		 = compile ps_3_0_wa psHDRBase_DOF(IN_HDRBlurUse);
#else
		VertexShader	 = compile vs_3_0 XBOXSimpleVS();
		ViewportEnable   = False;
		PixelShader		 = compile ps_3_0 psHDRBase_DOF(IN_HDRBlurUse);
#endif		
		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_DOF_DRM(appdata_HDRBASE IN, uniform bool UseHDRBlur)
{
 //
 // DOF, Dissort, Dreamland - all in one poor shader
 //
 pixelOutput OUT;

 half4 depth = tex2D(IN_DissortSampler, IN.UV.xy);
 
 //
 // depth.g --> Xdirection distorsion
 // depth.b --> Ydirection distorsion
 // depth.a --> 1 - distorsion force
 //
 float2 diss;
		diss.x = ((depth.g * 2.0) - 1.0) * 0.1;
		diss.y = ((depth.b * 2.0) - 1.0) * 0.1;
 half dintense = depth.a;	

#ifndef _XBOX
 half4 original = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + IN_HDRBaseTextureA_TEX.xy); 
 half4 swizled  = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + diss + IN_HDRBaseTextureA_TEX.xy);
#else
 half4 original = tex2D(IN_HDRBASEA_Sampler, IN.UV.xy + IN_HDRBaseTextureA_TEX.xy); 
 half4 swizled  = tex2D(IN_HDRBASEA_Sampler, IN.UV.xy + diss + IN_HDRBaseTextureA_TEX.xy);
#endif 

 half4 sdepth = tex2D(IN_DissortSampler, IN.UV.xy + diss);
 if ((sdepth.r+(1.0 / 255.0)) < depth.r)
     dintense = 1.0;
 
 original = lerp(swizled, original, dintense);

#ifdef SHADERS_PROFILE_2B  
 //
 // Do DOF
 // 
 half4 blured = BlurMe(IN.UV.xy, IN_HDRBaseTextureA_TEX.xy);
 half LFactor = min(1.0, max(0.0, abs(depth.r - IN_DOF_Focus) - IN_DOF_Tolerance) * IN_DOF_ToleranceInv);
 original.rgb = lerp(original.rgb, blured.rgb, LFactor);
 
#endif 

 //
 // Apply HDR
 //
 half4 blur	 = tex2D(IN_HDRBASEB_Sampler, IN.UV.xy + IN_HDRBaseTextureB_TEX.xy);
 
 if (UseHDRBlur)
 {
#ifndef SHADERS_PROFILE_30 
	 SampleDirectionalBlurHDR(IN.UV, original, blur, 4);
#else
	 SampleDirectionalBlurHDR(IN.UV, original, blur, 8);
#endif	 
 }

 half3 hdred = GetNormalHDRValue(original, blur, IN.UV.xy);
 OUT.color   = SampleDreamlandTextureNrm(IN_HDRBASEAP_Sampler, IN.UV.xy, IN_HDRBaseTextureA_TEX, half4(hdred, 1.0));
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique RenderHDRBase_DOF_DRM
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
		PixelShader		 = compile ps_3_0_wa psHDRBase_DOF_DRM(IN_HDRBlurUse);
#else
		VertexShader	 = compile vs_3_0 XBOXSimpleVS();
		ViewportEnable   = False;
		PixelShader		 = compile ps_3_0 psHDRBase_DOF_DRM(IN_HDRBlurUse);
#endif		
		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
		
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_LUMInit(appdata_HDRBASE IN)
{
 pixelOutput OUT;
 
 float2 BF   = IN_HDRBaseTextureA_TEX.xy;
 
 float3 smp  = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-BF.x, -BF.y)).rgb;
	    smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(    0, -BF.y)).rgb;
	    smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( BF.x, -BF.y)).rgb;

	    smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-BF.x,     0)).rgb;
	    smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(    0,     0)).rgb;
	    smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( BF.x,     0)).rgb;

	    smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-BF.x,  BF.y)).rgb;
	    smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(    0,  BF.y)).rgb;
	    smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( BF.x,  BF.y)).rgb;
 
        smp /= 9.0;
        smp  = clamp(smp, 0.0, 1024.0);
 
 OUT.color.r = dot(smp, LUM_VECX);
 OUT.color.g = smp.r;
 OUT.color.b = smp.g;
 OUT.color.a = smp.b;

 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRCalculateLuminanceInitial
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
		PixelShader		 = compile ps_2_b psHDRBase_CALC_LUMInit();
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_LUMInit();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_LUMIt(appdata_HDRBASE IN)
{
 pixelOutput OUT;
 
 float2 BF  = IN_HDRBaseTextureA_TEX.xy;
 
 float smp  = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-BF.x, -BF.y)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(    0, -BF.y)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( BF.x, -BF.y)).r;

	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-BF.x,     0)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(    0,     0)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( BF.x,     0)).r;

	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-BF.x,  BF.y)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(    0,  BF.y)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( BF.x,  BF.y)).r;
 
       smp /= 9.0;
 
 OUT.color.rgba = smp;
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRCalculateLuminanceIterative
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
		PixelShader		 = compile ps_2_b psHDRBase_CALC_LUMIt();
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_LUMIt();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_LUMFin(appdata_HDRBASE IN)
{
 pixelOutput OUT;
 
 float2 BF  = IN_HDRBaseTextureA_TEX.xy;
 
 float smp  = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-BF.x, -BF.y)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(    0, -BF.y)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( BF.x, -BF.y)).r;

	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-BF.x,     0)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(    0,     0)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( BF.x,     0)).r;

	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(-BF.x,  BF.y)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2(    0,  BF.y)).r;
	   smp += tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy + float2( BF.x,  BF.y)).r;
 
       smp /= 9.0;
       
       smp  = log(smp+0.0001);
 
 OUT.color.rgba = exp(smp);
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRCalculateLuminanceFinal
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
		PixelShader		 = compile ps_2_b psHDRBase_CALC_LUMFin();
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_LUMFin();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_LUMAdopt(appdata_HDRBASE IN)
{
 pixelOutput OUT;
 
 float AdaptedLum = tex2D(IN_HDRBASEAP_Sampler, float2(0.5, 0.5)).r;
 float CurrentLum = tex2D(IN_HDRBASEBP_Sampler, float2(0.5, 0.5)).r;
 
 // IN_SceneLuminanceFactor -> Delta time in seconds from last call
 // Multiplier 0.5			-> Two seconds for full adoption
 
 float Factor     = saturate(IN_SceneLuminanceFactor * ADOPT_MULTIPLIER);
 
 OUT.color.rgba   = AdaptedLum + ((CurrentLum - AdaptedLum) * Factor);
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRCalculateLuminanceAdopt
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
		PixelShader		 = compile ps_2_b psHDRBase_CALC_LUMAdopt();
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_LUMAdopt();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//																												//
//												HDR STARS														//
//																												//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_StarsPrepare(appdata_HDRBASE IN)
{
 pixelOutput OUT;
 
 half3 tv = tex2D(IN_StarsPassSampler1, IN.UV.xy).rgb;

#ifdef HDR_IS_A_FAKE
	   tv*= (1.0 / HDR_FAKE_DEFLATE);
#endif

 tv *= IN_HDRSaturationLDR;
 
 if (dot(tv, LUM_VEC) < IN_HDRStarsTres)
     tv = 0;
     
 OUT.color.rgb = tv;
 OUT.color.a   = 1.0;
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRStars_Prepare
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
		PixelShader		 = compile ps_2_b psHDRBase_CALC_StarsPrepare();
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_StarsPrepare();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef SHADERS_PROFILE_2B
#define HDR_STARS_LEN       6
#else
#define HDR_STARS_LEN       10
#endif

#define HDR_STARS_MAIN_W    0.40
#define HDR_STARS_CENTER_W  0.12
#define HDR_STARS_SIDE_W    0.06

#define HDR_STARS_DISTANCE  1.50

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_StarsBlurA(appdata_HDRBASE IN)
{
 pixelOutput OUT;

 const float2 BF = IN_HDRBaseTextureA_TEX.xy;
 float3 smp      = tex2D(IN_StarsPassSampler0, IN.UV.xy).rgb * HDR_STARS_MAIN_W;
 
 for (int i = 1; i < HDR_STARS_LEN; i++)
 {
	  float2 cdst = BF.xy * ((float)i * HDR_STARS_DISTANCE);
	  
	  float2 UV1 = IN.UV.xy + float2(-cdst.x,       0);
	  float2 UV2 = IN.UV.xy + float2(-cdst.x, -cdst.y);
	  float2 UV3 = IN.UV.xy + float2(      0, -cdst.y);
	  
	  smp += tex2D(IN_StarsPassSampler0, UV1).rgb * HDR_STARS_SIDE_W;
	  smp += tex2D(IN_StarsPassSampler0, UV2).rgb * HDR_STARS_CENTER_W;
	  smp += tex2D(IN_StarsPassSampler0, UV3).rgb * HDR_STARS_SIDE_W;
 }

 OUT.color.rgb = smp;
 OUT.color.a   = 1.0;
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRStars_StarsBlurA
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
#ifdef SHADERS_PROFILE_30
		PixelShader		 = compile ps_3_0 psHDRBase_CALC_StarsBlurA();
#else
		PixelShader		 = compile ps_2_b psHDRBase_CALC_StarsBlurA();
#endif		
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_StarsBlurA();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_StarsBlurB(appdata_HDRBASE IN)
{
 pixelOutput OUT;

 const float2 BF = IN_HDRBaseTextureA_TEX.xy;
 float3 smp      = tex2D(IN_StarsPassSampler0, IN.UV.xy).rgb * HDR_STARS_MAIN_W;
 
 for (int i = 1; i < HDR_STARS_LEN; i++)
 {
	  float2 cdst = BF.xy * ((float)i * HDR_STARS_DISTANCE);
	  
	  float2 UV1 = IN.UV.xy + float2( cdst.x,       0);
	  float2 UV2 = IN.UV.xy + float2( cdst.x, -cdst.y);
	  float2 UV3 = IN.UV.xy + float2(      0, -cdst.y);
	  
	  smp += tex2D(IN_StarsPassSampler0, UV1).rgb * HDR_STARS_SIDE_W;
	  smp += tex2D(IN_StarsPassSampler0, UV2).rgb * HDR_STARS_CENTER_W;
	  smp += tex2D(IN_StarsPassSampler0, UV3).rgb * HDR_STARS_SIDE_W;
 }

 OUT.color.rgb = smp;
 OUT.color.a   = 1.0;
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRStars_StarsBlurB
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
#ifdef SHADERS_PROFILE_30
		PixelShader		 = compile ps_3_0 psHDRBase_CALC_StarsBlurB();
#else
		PixelShader		 = compile ps_2_b psHDRBase_CALC_StarsBlurB();
#endif		
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_StarsBlurB();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_StarsBlurC(appdata_HDRBASE IN)
{
 pixelOutput OUT;

 const float2 BF = IN_HDRBaseTextureA_TEX.xy;
 float3 smp      = tex2D(IN_StarsPassSampler0, IN.UV.xy).rgb * HDR_STARS_MAIN_W;
 
 for (int i = 1; i < HDR_STARS_LEN; i++)
 {
	  float2 cdst = BF.xy * ((float)i * HDR_STARS_DISTANCE);
	  
	  float2 UV1 = IN.UV.xy + float2(-cdst.x,       0);
	  float2 UV2 = IN.UV.xy + float2(-cdst.x,  cdst.y);
	  float2 UV3 = IN.UV.xy + float2(      0,  cdst.y);
	  
	  smp += tex2D(IN_StarsPassSampler0, UV1).rgb * HDR_STARS_SIDE_W;
	  smp += tex2D(IN_StarsPassSampler0, UV2).rgb * HDR_STARS_CENTER_W;
	  smp += tex2D(IN_StarsPassSampler0, UV3).rgb * HDR_STARS_SIDE_W;
 }

 OUT.color.rgb = smp;
 OUT.color.a   = 1.0;
 
 return OUT;

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRStars_StarsBlurC
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
#ifdef SHADERS_PROFILE_30
		PixelShader		 = compile ps_3_0 psHDRBase_CALC_StarsBlurC();
#else
		PixelShader		 = compile ps_2_b psHDRBase_CALC_StarsBlurC();
#endif		
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_StarsBlurC();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_StarsBlurD(appdata_HDRBASE IN)
{
 pixelOutput OUT;

 const float2 BF = IN_HDRBaseTextureA_TEX.xy;
 float3 smp      = tex2D(IN_StarsPassSampler0, IN.UV.xy).rgb * HDR_STARS_MAIN_W;
 
 for (int i = 1; i < HDR_STARS_LEN; i++)
 {
	  float2 cdst = BF.xy * ((float)i * HDR_STARS_DISTANCE);
	  
	  float2 UV1 = IN.UV.xy + float2( cdst.x,       0);
	  float2 UV2 = IN.UV.xy + float2( cdst.x,  cdst.y);
	  float2 UV3 = IN.UV.xy + float2(      0,  cdst.y);
	  
	  smp += tex2D(IN_StarsPassSampler0, UV1).rgb * HDR_STARS_SIDE_W;
	  smp += tex2D(IN_StarsPassSampler0, UV2).rgb * HDR_STARS_CENTER_W;
	  smp += tex2D(IN_StarsPassSampler0, UV3).rgb * HDR_STARS_SIDE_W;
 }

 OUT.color.rgb = smp;
 OUT.color.a   = 1.0;
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRStars_StarsBlurD
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
#ifdef SHADERS_PROFILE_30
		PixelShader		 = compile ps_3_0 psHDRBase_CALC_StarsBlurD();
#else
		PixelShader		 = compile ps_2_b psHDRBase_CALC_StarsBlurD();
#endif		
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_StarsBlurD();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_StarsCombine(appdata_HDRBASE IN)
{
 pixelOutput OUT;
 
 float3 smp = 0;
 
		smp += tex2D(IN_StarsPassSampler0, IN.UV.xy).rgb;
		smp += tex2D(IN_StarsPassSampler1, IN.UV.xy).rgb;
		smp += tex2D(IN_StarsPassSampler2, IN.UV.xy).rgb;
		smp += tex2D(IN_StarsPassSampler3, IN.UV.xy).rgb;
 
 OUT.color.rgb = smp;
 OUT.color.a   = 1.0;
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRStars_StarsBlurCmb
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
		PixelShader		 = compile ps_2_b psHDRBase_CALC_StarsCombine();
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_StarsCombine();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

float  IN_HistogramMin;
float  IN_HistogramMax;
float4 IN_HistogramMask;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pixelOutput psHDRBase_CALC_Histo(appdata_HDRBASE IN)
{
 pixelOutput OUT;
 
 float3 smp = tex2D(IN_HDRBASEAP_Sampler, IN.UV.xy).gba;
 float  ctb = saturate(dot(smp.xyz, IN_HistogramMask.xyz));
 
 if (ctb < IN_HistogramMin)
     clip(-1);
 
 if (ctb > IN_HistogramMax)    
	 clip(-1);
     
 OUT.color = 1;
 
 return OUT;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
technique HDRCalculateHistogram
{	
	pass p0
	{
#ifndef _XBOX	
		VertexShader	 = Null;
#else
		VertexShader	 = compile vs_2_0 XBOXSimpleVS();
		ViewportEnable   = False;
#endif		

#ifdef SHADERS_PROFILE_2B
		PixelShader		 = compile ps_2_b psHDRBase_CALC_Histo();
#else		
		PixelShader		 = compile ps_2_0 psHDRBase_CALC_Histo();
#endif		
		AlphaBlendEnable = False;
		SrcBlend		 = SrcAlpha;
        DestBlend		 = InvSrcAlpha;
		ZEnable          = False;
		AlphaTestEnable	 = False;
		CullMode         = None;
#ifndef _XBOX		
		FogEnable		 = False;
#endif		
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
