#include "common.sh"


#define THRESHOLD 0.05


struct VsOutput
{
  float4 pos: POSITION;
  float4 tc : TEXCOORD0;
  float4 tc2: TEXCOORD1;
};

float4 textm : register(c16);
float4 texsz : register(c17);

VsOutput vsh(float2 pos: POSITION)
{
  VsOutput O;
  O.pos=float4(pos, 1, 1);

  float2 t=pos.xy*textm.xy+textm.zw;
  O.tc =t.xyxy+float4(0, 0, texsz.x, texsz.y);
  O.tc2=t.xyxy-float4(0, 0, texsz.x, texsz.y);
  return O;
}


sampler sceneTex : register(s0);


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

float brightDiff(float3 c1, float3 c2) { return abs(brightness(c1-c2)); }


float diff(float3 c2, float3 c1) { return brightDiff(c1, c2)>THRESHOLD; }


float4 psh(VsOutput I) : COLOR
{
  float4 cc=tex2D(sceneTex, I.tc .xy);
  float4 rc=tex2D(sceneTex, I.tc .zy);
  float4 dc=tex2D(sceneTex, I.tc .xw);
  float4 lc=tex2D(sceneTex, I.tc2.zy);
  float4 uc=tex2D(sceneTex, I.tc2.xw);

  /*
  unpack_color(cc.rgb);
  unpack_color(rc.rgb);
  unpack_color(dc.rgb);
  unpack_color(lc.rgb);
  unpack_color(uc.rgb);
  //*/

  return float4(
    diff(cc.rgb, rc.rgb),
    diff(cc.rgb, dc.rgb),
    diff(cc.rgb, lc.rgb),
    diff(cc.rgb, uc.rgb)
  );
}
