#include "postfx_common.sh"

POSTFX_VSH_1_TEX


sampler sceneTex   : register(s0);

float4 pixelOfs    : register(c0);


float brightness(float3 c) { return dot(c, float3(0.299, 0.587, 0.114)); }

float4 psh(VsOutput I) : COLOR
{
  float4 img=tex2D(sceneTex, I.tc);

  static const int NUM_SAMPLES=18;
  static const float2 ofs[NUM_SAMPLES]=
  {
    {-1,  0},
    {+1,  0},
    { 0, -1},
    { 0, +1},

    {-1, -1},
    {+1, -1},
    {-1, +1},
    {+1, +1},

    {-2,  0},
    {+2,  0},
    { 0, -2},
    { 0, +2},

    /*
    {-2, -1},
    {-2, +1},
    {+2, -1},
    {+2, +1},

    {-3,  0},
    {+3,  0},
    //*/

    //*
    {-1, -2},
    {+1, -2},
    {-1, +2},
    {+1, +2},

    { 0, -3},
    { 0, +3},
    //*/
  };

  img.a=brightness(img.rgb);

  float4 minc, maxc;
  minc=maxc=img;

  [unroll]
  for (int i=0; i<NUM_SAMPLES; ++i)
  {
    float4 smp=tex2D(sceneTex, I.tc+ofs[i]*pixelOfs.xy);

    //smp.a=brightness(smp.rgb);

    //maxc=(smp.a>maxc.a?smp:maxc);
    //minc=(smp.a<minc.a?smp:minc);

    maxc=min(maxc, smp);
  }

  //float4 res=minc;
  float4 res=maxc;

  return res;
}
