//
// Filter Gaussian to lower level
// Input : LDR bloom
// Output: blurred next level
//
// RvG, 21-4-09
//

#include "common_fs.cg"

struct VertOut
{
	float4 pos : POSITION;
	float4 col : COLOR0;
	float2 tc0 : TEXCOORD0;
};

struct FragOut
{
	float4 col : COLOR;
};

FragOut main(
  VertOut vin,
  uniform float     texelSize,
  uniform sampler2D bloomTex : TEXUNIT0
)
// Faster 3x3 linear Gaussian blur
{
  FragOut fout;
  float4 color; //SuppressLDR(tex2D(sceneTex,vin.tc0));

  //const float texelWidth=1.2f/512.0f;
  //const float texelWidth=1.2f/256.0f;
  const float texelWidth=1.0*texelSize;
  
  const float total=2+5+2+ 5+6+5 + 2+5+2;
  //const float total=1+5+1+ 5+6+5 + 1+5+1;
  
  half4 gaussFilter1[3];
  half4 gaussFilter2[3];
  half4 gaussFilter3[3];
  
  // For linear interpolated source textures, we can reduce 5x5 to 3x3
  gaussFilter1[0]=half4(-texelWidth,-texelWidth,0,2.0/total);
  gaussFilter1[1]=half4(          0,-texelWidth,0,5.0/total);
  gaussFilter1[2]=half4( texelWidth,-texelWidth,0,2.0/total);
  
  gaussFilter2[0]=half4(-texelWidth,0,0,5.0/total);
  gaussFilter2[1]=half4(              0,0,0,6.0/total);
  gaussFilter2[2]=half4( texelWidth,0,0,5.0/total);
  
  gaussFilter3[0]=half4(-texelWidth,texelWidth,0,2.0/total);
  gaussFilter3[1]=half4(          0,texelWidth,0,5.0/total);
  gaussFilter3[2]=half4( texelWidth,texelWidth,0,2.0/total);
  
  
  int ctest=0;
  if(texelSize==(1.0/256.0))
  {
  
  float tx=1/256;
  float ty=1/256;
  float2 offset[8]={float2(tx,ty),
					float2(0,ty),
					float2(tx,0),
					float2(-tx,-ty),
					float2(0,-ty),
					float2(-tx,0),
					float2(-tx,ty),
					float2(tx,-ty)
					};
					
  for (int i=0;i<8;i++)
  {
	half4 coloraro=tex2D(bloomTex,vin.tc0);
	if (coloraro.r==0&&coloraro.g==0&&coloraro.b==0)
	{
		ctest++;
	}
  }
  }
  
  if(texelSize>(1.0/256.0))
  {
	ctest=1;
  }
  
  if (ctest<8)
  {
  int i;
  color=0;
  for(i=0;i<3;i++)
  {
    // Row 1
    color+=tex2D(bloomTex,vin.tc0+gaussFilter1[i].xy)*gaussFilter1[i].w;
    // Row 2
    color+=tex2D(bloomTex,vin.tc0+gaussFilter2[i].xy)*gaussFilter2[i].w;
    // Row 3
    color+=tex2D(bloomTex,vin.tc0+gaussFilter3[i].xy)*gaussFilter3[i].w;
  }
  } 

  //color=tex2D(bloomTex,vin.tc0);
  fout.col=color;

  return fout;
} 

/*
FragOut main2(
  VertOut vin,
  uniform float     texelSize,
	uniform sampler2D bloomTex : TEXUNIT0
)
{
  FragOut fout;
  float4 color; //SuppressLDR(tex2D(sceneTex,vin.tc0));

  const float texelWidth=1.0f/512.0f;
  
  float4 gaussFilter1[5];
  float4 gaussFilter2[5];
  float4 gaussFilter3[5];
  float4 gaussFilter4[5];
  float4 gaussFilter5[5];
  
  gaussFilter1[0]=float4(-2.0*texelWidth,-2.0*texelWidth,0,1.0/273.0);
  gaussFilter1[1]=float4(-1.0*texelWidth,-2.0*texelWidth,0,4.0/273.0);
  gaussFilter1[2]=float4(-0.0*texelWidth,-2.0*texelWidth,0,7.0/273.0);
  gaussFilter1[3]=float4( 1.0*texelWidth,-2.0*texelWidth,0,4.0/273.0);
  gaussFilter1[4]=float4( 2.0*texelWidth,-2.0*texelWidth,0,1.0/273.0);
  
  gaussFilter2[0]=float4(-2.0*texelWidth,-1.0*texelWidth,0,4.0/273.0);
  gaussFilter2[1]=float4(-1.0*texelWidth,-1.0*texelWidth,0,16.0/273.0);
  gaussFilter2[2]=float4(              0,-1.0*texelWidth,0,26.0/273.0);
  gaussFilter2[3]=float4( 1.0*texelWidth,-1.0*texelWidth,0,16.0/273.0);
  gaussFilter2[4]=float4( 2.0*texelWidth,-1.0*texelWidth,0,4.0/273.0);
  
  gaussFilter3[0]=float4(-2.0*texelWidth,0,0,7.0/273.0);
  gaussFilter3[1]=float4(-1.0*texelWidth,0,0,26.0/273.0);
  gaussFilter3[2]=float4(              0,0,0,41.0/273.0);
  gaussFilter3[3]=float4( 1.0*texelWidth,0,0,26.0/273.0);
  gaussFilter3[4]=float4( 2.0*texelWidth,0,0,7.0/273.0);
  
  gaussFilter4[0]=float4(-2.0*texelWidth,1.0*texelWidth,0,4.0/273.0);
  gaussFilter4[1]=float4(-1.0*texelWidth,1.0*texelWidth,0,16.0/273.0);
  gaussFilter4[2]=float4(              0,1.0*texelWidth,0,26.0/273.0);
  gaussFilter4[3]=float4( 1.0*texelWidth,1.0*texelWidth,0,16.0/273.0);
  gaussFilter4[4]=float4( 2.0*texelWidth,1.0*texelWidth,0,4.0/273.0);
  
  gaussFilter5[0]=float4(-2.0*texelWidth,2.0*texelWidth,0,1.0/273.0);
  gaussFilter5[1]=float4(-1.0*texelWidth,2.0*texelWidth,0,4.0/273.0);
  gaussFilter5[2]=float4(-0.0*texelWidth,2.0*texelWidth,0,7.0/273.0);
  gaussFilter5[3]=float4( 1.0*texelWidth,2.0*texelWidth,0,4.0/273.0);
  gaussFilter5[4]=float4( 2.0*texelWidth,2.0*texelWidth,0,1.0/273.0);
  
  int i;
  color=0;
  for(i=0;i<5;i++)
  {
    // Row 1
    color+=tex2D(bloomTex,vin.tc0+gaussFilter1[i].xy)*gaussFilter1[i].w;
    // Row 2
    color+=tex2D(bloomTex,vin.tc0+gaussFilter2[i].xy)*gaussFilter2[i].w;
    // Row 3
    color+=tex2D(bloomTex,vin.tc0+gaussFilter3[i].xy)*gaussFilter3[i].w;
    // Row 4
    color+=tex2D(bloomTex,vin.tc0+gaussFilter4[i].xy)*gaussFilter4[i].w;
    //color+=tex2D(bloomTex,vin.tc0+float2(gaussFilter2[i].x,-gaussFilter2[i].y))*gaussFilter2[i].w;
    // Row 5
    color+=tex2D(bloomTex,vin.tc0+gaussFilter5[i].xy)*gaussFilter5[i].w;
    //color+=tex2D(bloomTex,vin.tc0+float2(gaussFilter1[i].x,-gaussFilter1[i].y))*gaussFilter1[i].w;
  }

  //color=tex2D(bloomTex,vin.tc0);
  fout.col=color;

  return fout;
}
*/

/*
FragOut main(
  VertOut vin,
  uniform float     texelSize,
	uniform sampler2D bloomTex : TEXUNIT0
)
{
  FragOut fout;
  float4 color=0; //SuppressLDR(tex2D(sceneTex,vin.tc0));

  const float texelWidth=1.0f/512.0f;
  
  float4 gaussFilter1[5];
  float4 gaussFilter2[5];
  float4 gaussFilter3[5];
  
  gaussFilter1[0]=float4(-2.0*texelWidth,-2.0*texelWidth,0,1.0/273.0);
  gaussFilter1[1]=float4(-1.0*texelWidth,-2.0*texelWidth,0,4.0/273.0);
  gaussFilter1[2]=float4(-0.0*texelWidth,-2.0*texelWidth,0,7.0/273.0);
  gaussFilter1[3]=float4( 1.0*texelWidth,-2.0*texelWidth,0,4.0/273.0);
  gaussFilter1[4]=float4( 2.0*texelWidth,-2.0*texelWidth,0,1.0/273.0);
  
  gaussFilter2[0]=float4(-2.0*texelWidth,-1.0*texelWidth,0,4.0/273.0);
  gaussFilter2[1]=float4(-1.0*texelWidth,-1.0*texelWidth,0,16.0/273.0);
  gaussFilter2[2]=float4(              0,-1.0*texelWidth,0,26.0/273.0);
  gaussFilter2[3]=float4( 1.0*texelWidth,-1.0*texelWidth,0,16.0/273.0);
  gaussFilter2[4]=float4( 2.0*texelWidth,-1.0*texelWidth,0,4.0/273.0);
  
  gaussFilter3[0]=float4(-2.0*texelWidth,0,0,7.0/273.0);
  gaussFilter3[1]=float4(-1.0*texelWidth,0,0,26.0/273.0);
  gaussFilter3[2]=float4(              0,0,0,41.0/273.0);
  gaussFilter3[3]=float4( 1.0*texelWidth,0,0,26.0/273.0);
  gaussFilter3[4]=float4( 2.0*texelWidth,0,0,7.0/273.0);
  
  int i;
  for(i=0;i<5;i++)
  {
    // Row 1
    color+=tex2D(bloomTex,vin.tc0+gaussFilter1[i].xy)*gaussFilter1[i].w;
    // Row 2
    color+=tex2D(bloomTex,vin.tc0+gaussFilter2[i].xy)*gaussFilter2[i].w;
    // Row 3
    color+=tex2D(bloomTex,vin.tc0+gaussFilter3[i].xy)*gaussFilter3[i].w;
    // Row 4
    color+=tex2D(bloomTex,vin.tc0+float2(gaussFilter2[i].x,-gaussFilter2[i].y))*gaussFilter2[i].w;
    // Row 5
    color+=tex2D(bloomTex,vin.tc0+float2(gaussFilter1[i].x,-gaussFilter1[i].y))*gaussFilter1[i].w;
  }

  //color=tex2D(bloomTex,vin.tc0);
  fout.col=color;

  return fout;
} 
*/

/*
FragOut main(
  VertOut vin,
  uniform float     texelSize,
	uniform sampler2D bloomTex : TEXUNIT0
)
{
  FragOut fout;
  float4 color=0; //SuppressLDR(tex2D(sceneTex,vin.tc0));

  const float texelWidth=1.0f/512.0f;
  
  float4 gaussFilter[15];
  
  gaussFilter[0]=float4(-2.0*texelWidth,-2.0*texelWidth,0,1.0);
  gaussFilter[1]=float4(-1.0*texelWidth,-2.0*texelWidth,0,4.0);
  gaussFilter[2]=float4(-0.0*texelWidth,-2.0*texelWidth,0,7.0);
  gaussFilter[3]=float4( 1.0*texelWidth,-2.0*texelWidth,0,4.0);
  gaussFilter[4]=float4( 2.0*texelWidth,-2.0*texelWidth,0,1.0);
  
  gaussFilter[5]=float4(-2.0*texelWidth,-1.0*texelWidth,0,4.0);
  gaussFilter[6]=float4(-1.0*texelWidth,-1.0*texelWidth,0,16.0);
  gaussFilter[7]=float4(              0,-1.0*texelWidth,0,26.0);
  gaussFilter[8]=float4( 1.0*texelWidth,-1.0*texelWidth,0,16.0);
  gaussFilter[9]=float4( 2.0*texelWidth,-1.0*texelWidth,0,4.0);
  
  gaussFilter[10]=float4(-2.0*texelWidth,0,0,7.0);
  gaussFilter[11]=float4(-1.0*texelWidth,0,0,26.0);
  gaussFilter[12]=float4(              0,0,0,41.0);
  gaussFilter[13]=float4( 1.0*texelWidth,0,0,26.0);
  gaussFilter[14]=float4( 2.0*texelWidth,0,0,7.0);
  
  int i,iPlus5;
  for(i=0;i<5;i++)
  {
    iPlus5=i+5;
    // Row 1
    color+=tex2D(bloomTex,vin.tc0+gaussFilter[i].xy)*gaussFilter[i].w;
    // Row 5
    color+=tex2D(bloomTex,vin.tc0+float2(gaussFilter[i].x,-gaussFilter[i].y))*gaussFilter[i].w;
    // Row 2
    color+=tex2D(bloomTex,vin.tc0+gaussFilter[iPlus5].xy)*gaussFilter[iPlus5].w;
    // Row 4
    color+=tex2D(bloomTex,vin.tc0+float2(gaussFilter[iPlus5].x,-gaussFilter[iPlus5].y))*gaussFilter[iPlus5].w;
    // Row 3
    color+=tex2D(bloomTex,vin.tc0+gaussFilter[i+10].xy)*gaussFilter[i+10].w;
  }

  //color=tex2D(bloomTex,vin.tc0);
  fout.col=color*0.0036630036630036630036630036630037; // /273.0f;

  return fout;
} 
*/

/*
FragOut main(
  VertOut vin,
  uniform float     texelSize,
	uniform sampler2D bloomTex : TEXUNIT0
)
{
  FragOut fout;
  float4 color=0; //SuppressLDR(tex2D(sceneTex,vin.tc0));

  const float texelWidth=1.0f/512.0f;
  
  float4 gaussFilter[15];
  
  gaussFilter[0]=float4(-2.0*texelWidth,-2.0*texelWidth,0,1.0/273.0);
  gaussFilter[1]=float4(-1.0*texelWidth,-2.0*texelWidth,0,4.0/273.0);
  gaussFilter[2]=float4(-0.0*texelWidth,-2.0*texelWidth,0,7.0/273.0);
  gaussFilter[3]=float4( 1.0*texelWidth,-2.0*texelWidth,0,4.0/273.0);
  gaussFilter[4]=float4( 2.0*texelWidth,-2.0*texelWidth,0,1.0/273.0);
  
  gaussFilter[5]=float4(-2.0*texelWidth,-1.0*texelWidth,0,4.0/273.0);
  gaussFilter[6]=float4(-1.0*texelWidth,-1.0*texelWidth,0,16.0/273.0);
  gaussFilter[7]=float4(              0,-1.0*texelWidth,0,26.0/273.0);
  gaussFilter[8]=float4( 1.0*texelWidth,-1.0*texelWidth,0,16.0/273.0);
  gaussFilter[9]=float4( 2.0*texelWidth,-1.0*texelWidth,0,4.0/273.0);
  
  gaussFilter[10]=float4(-2.0*texelWidth,0,0,7.0/273.0);
  gaussFilter[11]=float4(-1.0*texelWidth,0,0,26.0/273.0);
  gaussFilter[12]=float4(              0,0,0,41.0/273.0);
  gaussFilter[13]=float4( 1.0*texelWidth,0,0,26.0/273.0);
  gaussFilter[14]=float4( 2.0*texelWidth,0,0,7.0/273.0);
  
  int i,iPlus5;
  for(i=0;i<5;i++)
  {
    iPlus5=i+5;
    // Row 1
    color+=tex2D(bloomTex,vin.tc0+gaussFilter[i].xy)*gaussFilter[i].w;
    // Row 2
    color+=tex2D(bloomTex,vin.tc0+gaussFilter[iPlus5].xy)*gaussFilter[iPlus5].w;
    // Row 3
    color+=tex2D(bloomTex,vin.tc0+gaussFilter[i+10].xy)*gaussFilter[i+10].w;
    // Row 4
    color+=tex2D(bloomTex,vin.tc0+float2(gaussFilter[iPlus5].x,-gaussFilter[i+5].y))*gaussFilter[iPlus5].w;
    // Row 5
    color+=tex2D(bloomTex,vin.tc0+float2(gaussFilter[i].x,-gaussFilter[i].y))*gaussFilter[i].w;
  }

  //color=tex2D(bloomTex,vin.tc0);
  fout.col=color;

  return fout;
} 
*/

/*
FragOut main(
  VertOut vin,
  uniform float     texelSize,
	uniform sampler2D bloomTex : TEXUNIT0
)
{
  FragOut fout;
  float4 color=0; //SuppressLDR(tex2D(sceneTex,vin.tc0));

  const float gauss0 = 1.0/32.0;
  const float gauss1 =10.0/32.0;
  const float gauss2 =20.0/32.0;
  const float gauss3 =26.0/32.0;
  const float gauss4 =20.0/32.0;
  const float gauss5 =10.0/32.0;
  const float gauss6 = 1.0/32.0;    

  float4 gaussFilter[7];
  gaussFilter[0]=float4( -3.0*texelSize , 0.0, 0.0, gauss0);
  gaussFilter[1]=float4( -2.0*texelSize , 0.0, 0.0, gauss1);
  gaussFilter[2]=float4( -1.0*texelSize , 0.0, 0.0, gauss2);
  gaussFilter[3]=float4(  0.0*texelSize , 0.0, 0.0, gauss3);
  gaussFilter[4]=float4( +1.0*texelSize , 0.0, 0.0, gauss4);
  gaussFilter[5]=float4( +2.0*texelSize , 0.0, 0.0, gauss5);
  gaussFilter[6]=float4( +3.0*texelSize , 0.0, 0.0, gauss6);

  int i;
  for(i=0;i<7;i++)
  {
    color+=tex2D(bloomTex,vin.tc0+gaussFilter[i].xy)*gaussFilter[i].w;
    color+=tex2D(bloomTex,vin.tc0+gaussFilter[i].yx)*gaussFilter[i].w;
  }
  //color*=0.5;
  color*=0.18;

  //color=float4(1,0,0,1);
  fout.col=color;

  return fout;
} 
*/
