#pragma parameter SIGMA_R "Bilateral Blur" 0.4 0.0 1.0 0.1
#ifdef PARAMETER_UNIFORM
uniform float SIGMA_R;
#else
#define SIGMA_R 0.4
#endif
// END PARAMETERS //

/*
   Hyllian's Fast Bilateral Shader
   
   Copyright (C) 2011/2016 Hyllian - sergiogdb@gmail.com

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is 
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.

*/

/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
   - FX11 compilers
*/

#include "../../compat_includes.inc"
uniform COMPAT_Texture2D(decal) : TEXUNIT0;
uniform float4x4 modelViewProj;

struct out_vertex
{
	float4 position : COMPAT_POS;
	float2 texCoord : TEXCOORD0;
#ifndef HLSL_4
	float4 Color    : COLOR;
#endif
};

out_vertex main_vertex(COMPAT_IN_VERTEX)
{
	out_vertex OUT;
#ifdef HLSL_4
	float4 position = VIN.position;
	float2 texCoord = VIN.texCoord;
#else
	OUT.Color = color;
#endif
	OUT.position = mul(modelViewProj, position);
	OUT.texCoord = texCoord;
	
	return OUT;
}

#define GET(M,K) (COMPAT_SamplePoint(decal,tc+M*dx+K*dy).xyz)

#define BIL(M,K)  {col=GET(M,K);ds=M*M+K*K;weight=exp(-ds/sd2)*exp(-(col-center)*(col-center)/si2);color+=(weight*col);wsum+=weight;}


float4 fast_bilateral(float2 texture_size, float2 texCoord, COMPAT_Texture2D(decal))
{
      float ds, sd2, si2;
      float sigma_d = 3.0;
      float sigma_r = SIGMA_R*0.04;

      float3 color = float3(0.0, 0.0, 0.0);
      float3 wsum = float3(0.0, 0.0, 0.0);
      float3 weight;

      float2 dx = float2(1.0, 0.0)/texture_size;
      float2 dy = float2(0.0, 1.0)/texture_size;

      sd2 = 2.0 * sigma_d * sigma_d;
      si2 = 2.0 * sigma_r * sigma_r;

      float2 tc = texCoord;

      float3 col;
      float3 center = GET(0,0);

      BIL(-2,-2)
      BIL(-1,-2)
      BIL( 0,-2)
      BIL( 1,-2)
      BIL( 2,-2)
      BIL(-2,-1)
      BIL(-1,-1)
      BIL( 0,-1)
      BIL( 1,-1)
      BIL( 2,-1)
      BIL(-2, 0)
      BIL(-1, 0)
      BIL( 0, 0)
      BIL( 1, 0)
      BIL( 2, 0)
      BIL(-2, 1)
      BIL(-1, 1)
      BIL( 0, 1)
      BIL( 1, 1)
      BIL( 2, 1)
      BIL(-2, 2)
      BIL(-1, 2)
      BIL( 0, 2)
      BIL( 1, 2)
      BIL( 2, 2)

      // Weight normalization
      color /= wsum;

      return float4(color, 1);
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return fast_bilateral(COMPAT_texture_size, VOUT.texCoord, decal);
}
COMPAT_END
