// CSM Shadowmapping without bloom

#include "../common/constants.cg"
#include "../common/hdr.cg"
#include "bloom.cg"
#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 sampler2D sceneMap    : TEXUNIT0,
  uniform sampler2D velocityMap : TEXUNIT1,
  /// Z in TEXUNIT5
  //uniform sampler2D bloomMap1   : TEXUNIT2,
  //uniform sampler2D bloomMap2   : TEXUNIT3,
  //uniform sampler2D bloomMap3   : TEXUNIT4,
#ifdef CSM_MRT
  uniform sampler2D depthMap    : TEXUNIT5,
  uniform sampler2D shadowMap   : TEXUNIT6,
#endif
  uniform float alpha,
  uniform float exposure
)
{
  FragOut fout;

  float4 ambientColor,
         sdColor,                 // Specular+diffuse
         color;

#ifdef CSM_MRT
  // Original color
  ambientColor=tex2D(sceneMap,vin.tc0);
  sdColor=tex2D(shadowMap,vin.tc0);

  float shadowAmount=BlurTexture(shadowMap, vin.tc0,smBlurAmount,depthMap);
  color=ambientColor+shadowAmount*sdColor;
#else
  // Single render-target (RT)
  color=tex2D(sceneMap,vin.tc0);
#endif

#ifdef OBS
  // Bloom  
  float4 bloomColor=BloomCombineMaps3(bloomMap1,bloomMap2,bloomMap3,vin.tc0);
  color=BloomMix(color,bloomColor);
#endif

  // HDR -> LDR tonemap
  fout.col=ToneMapHDR(color,exposure);
  fout.col.a=alpha;

  return fout;
} 
