/*********************************************************************NVMH3****

******************************************************************************/

float4x4 worldViewProj : WorldViewProjection;
float4x4 Proj : Projection;

float3 campos : CAMERAPOS;

float  ImageWidth;
float  ImageHeight;
float  MaxSampleRadius;
float  Smooth;


// this table is garbage -> rebuild on CPU and program
float3 gaussian[9] = {
float3( -2,  -2, 0.001418 ),
float3( -1,  -2, 0.009035 ),
float3( 0,  -2, 0.016750 ),
float3( 1,  -2, 0.009035 ),
float3( 2,  -2, 0.001418 ),
float3( -2,  -1, 0.001418 ),
float3( 1,  -2, 0.009035 ),
float3( 2,  -2, 0.001418 ),
float3( -2,  -1, 0.009035 ) 
};


texture diffuseTexture : DiffuseTexture;
sampler DiffuseTextureSampler = sampler_state 
{
    texture = <diffuseTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = POINT;
    MINFILTER = POINT;
    MAGFILTER = POINT;
};

texture depthTexture : Custom0;
sampler DepthTextureSampler = sampler_state 
{
    texture = <depthTexture>;
};


texture halfSampleTexture : Custom1;
sampler HalfTextureSampler = sampler_state 
{
    texture = <halfSampleTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

struct vertexInput {
    float4 position				: POSITION;
    float2 texCoordDiffuse		: TEXCOORD0;
};

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
};


vertexOutput VS_Screen(vertexInput IN) 
{
    vertexOutput OUT;
    
    OUT.hPosition = mul( IN.position, Proj );
    
    OUT.texCoordDiffuse = IN.texCoordDiffuse;
    return OUT;
}


float4 PS_TexturedSmooth( vertexOutput IN): COLOR
{
	float3 sampled = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse ).xyz;  		  
	float pixelSizeW = 1.0f / ImageWidth;
	float pixelSizeH = 1.0f / ImageHeight;
	
	float3 blurred = float3( 0.0f, 0.0f, 0.0f);	
	
	float NUM_SAMPLES = 9;
	float2 coord;
	float3 thisGaussian;		
		
	pixelSizeW *= MaxSampleRadius;
	pixelSizeH *= MaxSampleRadius;
	for(int s=0; s<NUM_SAMPLES; s++)
	{
		thisGaussian = gaussian[s];
		coord.x = IN.texCoordDiffuse.x  + (pixelSizeW * thisGaussian.x); //warning, assumes w==h
		coord.y = IN.texCoordDiffuse.y  + (pixelSizeH * thisGaussian.y); //warning, assumes w==h
		blurred += tex2D(DiffuseTextureSampler, coord) * thisGaussian.z;
	}
		
	float3 outColor = lerp( sampled, blurred, Smooth );
	
	return float4( outColor, 1 );
}


//-----------------------------------
technique basic
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_Screen();
		PixelShader  = compile ps_2_0 PS_TexturedSmooth();
		cullmode = none;
    }
}
