/*
   Author: Themaister
   License: Public domain
*/
struct tex_coord
{
   float2 c11;
   float2 pixel_no;
};

struct input
{
   float2 video_size;
   float2 texture_size;
   float2 output_size;
};

void main_vertex
(
   float4 position : POSITION,
   out float4 oPosition : POSITION,
   uniform float4x4 modelViewProj,

   float4 color : COLOR,
   out float4 oColor : COLOR,

   float2 tex : TEXCOORD0,

   uniform input IN,
   out tex_coord coords
)
{
   oPosition = mul(modelViewProj, position);
   oColor = color;

   coords = tex_coord(
      tex,
      tex * IN.texture_size
   );
}

float dist(float2 coord, float2 source)
{
   float2 delta = coord - source;
   return sqrt(dot(delta, delta));
}

float color_bloom(float3 color)
{
   const float3 gray_coeff = float3(0.30, 0.59, 0.11);
   float bright = dot(color, gray_coeff);
   return lerp(1.0 + shine, 1.0 - shine, bright);
}

float rolloff(float len)
{
   return exp(-6.0 * len);
}

const float high = 0.35;
const float low = 0.2;

float3 lookup(float2 pixel_no, float3 color)
{
   float delta = dist(frac(pixel_no), float2(0.5));

   if (delta > low && delta < high)
      return color;
   else if (delta >= high)
      return color * rolloff(delta - high);
   else if (delta <= low)
      return color * rolloff(low - delta);
   else
      return float3(0.0);
}

#define TEX(coord) tex2D(s0, co.coord).rgb

float4 main_fragment(in tex_coord co, uniform sampler2D s0 : TEXUNIT0) : COLOR
{
   float3 mid_color = lookup(co.pixel_no, TEX(c11));
   return float4(mid_color, 1.0);
}

