#pragma parameter WATERPAINT_EDGE_COLOR_AMOUNT "Waterpaint Edge Color Amount"           2.0 0.0 10.0 0.1
#pragma parameter WATERPAINT_DETAIL            "Waterpaint Detail"                      1.0 0.0 10.0 0.1

#ifdef PARAMETER_UNIFORM
uniform float WATERPAINT_EDGE_COLOR_AMOUNT;
uniform float WATERPAINT_DETAIL;
#else
#define WATERPAINT_EDGE_COLOR_AMOUNT 2.0
#define WATERPAINT_DETAIL 1.0
#endif

/* Default Vertex shader */
void main_vertex
(
   float4 position   : POSITION,
   float4 color   : COLOR,
   float2 texCoord : TEXCOORD0,
    uniform float4x4 modelViewProj,
   out float4 oPosition : POSITION,
   out float4 oColor    : COLOR,
   out float2 otexCoord : TEXCOORD
)
{
   oPosition = mul(modelViewProj, position);
   oColor = color;
   otexCoord = texCoord;
}


struct output
{
  float4 color    : COLOR;
};

struct input
{
   float2 video_size;
   float2 texture_size;
   float2 output_size;
   float frame_count;
   float frame_direction;
   float frame_rotation;
};


static const float BlurWeights[13] =
{
    0.002216,
    0.008764,
    0.026995,
    0.064759,
    0.120985,
    0.176033,
    0.199471,
    0.176033,
    0.120985,
    0.064759,
    0.026995,
    0.008764,
    0.002216,
};

float edge_detect_sobel(sampler tex, float2 tc, float2 off)
{
    // sobel
    float4 g00 = tex2D(tex,    tc + float2(-off.x,    -off.y));   // Top Left
    float4 g01 = tex2D(tex,    tc + float2(  0,    -off.y));   // Up
    float4 g02 = tex2D(tex,    tc + float2( off.x,    -off.y));   // Top Right
    float4 g10 = tex2D(tex,    tc + float2(-off.x,      0));   // Left
    float4 g12 = tex2D(tex,    tc + float2( off.x,      0));   // Right
    float4 g20 = tex2D(tex,    tc + float2(-off.x,     off.y));   // Bottom Left
    float4 g21 = tex2D(tex,    tc + float2(  0,     off.y));   // Bottom
    float4 g22 = tex2D(tex,   tc + float2( off.x,     off.y));   // Bottom Right

    //compute luminance from sampled regions
    float3 rgb2lum = float3(0.3, 0.59, 0.11);
    g00 = dot(g00.rgb, rgb2lum);
    g01 = dot(g01.rgb, rgb2lum);
    g02 = dot(g02.rgb, rgb2lum);
    g10 = dot(g10.rgb, rgb2lum);
    g12 = dot(g12.rgb, rgb2lum);
    g20 = dot(g20.rgb, rgb2lum);
    g21 = dot(g21.rgb, rgb2lum);
    g22 = dot(g22.rgb, rgb2lum);
 
    //compute sobel mask
    float4 Gx = -(g00 + 2 * g10 + g20) + (g02 + 2 * g12 + g22);
    float4 Gy = -(g00 + 2 * g01 + g02) + (g20 + 2 * g21 + g22);
    float4 color = abs(Gx) +abs(Gy);
   return color;
}

output main_fragment( in float2 Tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0,uniform input IN )
{
   float4 outcolor = {1.0f, 0.0f, 0.0f, 1.0f};
   float2 texelSize = 1.0 / IN.texture_size;
   float2 offset = { texelSize.x, texelSize.y };

   float gradient = edge_detect_sobel(s0, Tex, offset);
   gradient = saturate(gradient*0.2);

   float4 sum = 0;

    for (int i = 0; i < 13; i++) {
        sum += tex2D(s0, Tex.xy + (i-6) * offset.xy) * BlurWeights[i];
    }
    float4 pixelColor = tex2D(s0, Tex);
    outcolor = ( gradient * WATERPAINT_DETAIL * (1-WATERPAINT_EDGE_COLOR_AMOUNT) + gradient * WATERPAINT_DETAIL * pixelColor * WATERPAINT_EDGE_COLOR_AMOUNT + sum);


   output OUT;
   OUT.color.rgb= outcolor;
   return OUT;
}
