#include "common.sh"


struct VsOutput
{
  float4 pos: POSITION;
  float4 tcp: TEXCOORD0;
  float4 tcn: 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.tcp=t.xyxy+float4(0, 0, texsz.x, texsz.y);
  O.tcn=t.xyxy-float4(0, 0, texsz.x, texsz.y);
  return O;
}


sampler sceneTex : register(s0);
sampler  edgeTex : register(s1);
sampler   rowTex : register(s2);
//sampler   colTex : register(s3);

float4 pixelofs  : register(c0);


float area(float d, float s) { return max(d, 0.25)*s; }
//float area(float d, float s) { return 0.5; }


float4 psh(VsOutput I) : COLOR
{
  //float4 src     =tex2D(sceneTex, I.tcp.xy);
  float4 edge    =tex2D( edgeTex, I.tcp.xy);
  //float  edgeUp  =tex2D( edgeTex, I.tcn.xw).g;
  //float  edgeLeft=tex2D( edgeTex, I.tcn.zy).r;

//  if (!any(edge)) return tex2D(sceneTex, I.tcp.xy);
  //if (!any(float4(edge, edgeUp, edgeLeft))) return src;

  //return dot(src, float4(0.33, 0.33, 0.33, 0)); //==
  //return float4(edge, 0, 0); //==

  float4 fac=0;

  float4 rowcol=tex2D(rowTex, I.tcp.xy)*255;

  // with down
  if (edge.g>0)
  {
    float2 len=rowcol.xy;
    float d=len.y-len.x;
    float s=0.5/(len.x+len.y+1);

    if (d>=0 && tex2D(edgeTex, I.tcp.xy+float2((len.x  )*pixelofs.x, 0)).r>0) fac.y+=area( d, s);
    if (d<=0 && tex2D(edgeTex, I.tcp.xy-float2((len.y+1)*pixelofs.x, 0)).r>0) fac.y+=area(-d, s);
  }

  // with right
  if (edge.r>0)
  {
    float2 len=rowcol.zw;
    float d=len.y-len.x;
    float s=0.5/(len.x+len.y+1);

    if (d>=0 && tex2D(edgeTex, I.tcp.xy+float2(0, (len.x  )*pixelofs.y)).g>0) fac.x+=area( d, s);
    if (d<=0 && tex2D(edgeTex, I.tcp.xy-float2(0, (len.y+1)*pixelofs.y)).g>0) fac.x+=area(-d, s);
  }

  // with up
  if (edge.a>0)
  {
    float2 len=tex2D(rowTex, I.tcn.xw).xy*255;
    float d=len.y-len.x;
    float s=0.5/(len.x+len.y+1);

    if (d>=0 && tex2D(edgeTex, I.tcp.xy+float2((len.x  )*pixelofs.x, 0)).r>0) fac.w+=area( d, s);
    if (d<=0 && tex2D(edgeTex, I.tcp.xy-float2((len.y+1)*pixelofs.x, 0)).r>0) fac.w+=area(-d, s);
  }

  // with left
  if (edge.b>0)
  {
    float2 len=tex2D(rowTex, I.tcn.zy).zw*255;
    float d=len.y-len.x;
    float s=0.5/(len.x+len.y+1);

    if (d>=0 && tex2D(edgeTex, I.tcp.xy+float2(0, (len.x  )*pixelofs.y)).g>0) fac.z+=area( d, s);
    if (d<=0 && tex2D(edgeTex, I.tcp.xy-float2(0, (len.y+1)*pixelofs.y)).g>0) fac.z+=area(-d, s);
  }

  //fac *= (fac == max(max(fac.x, fac.y), max(fac.z, fac.w))); // keep max factors only

  //return float4(edge, fac.y, 0)*2; //==
  //return float4(fac.yw, 0, 0)*2; //==
  //return sqrt(float4(fac.yw, 0, 0)*2); //==
  //return sqrt(float4(fac.xz, 0, 0)*2); //==

  // blend
  //if (!any(fac)) return src;

  //unpack_color(src.rgb);

  /*
  src=lerp(src, tex2D(sceneTex, I.tcp.zy), fac.x);
  src=lerp(src, tex2D(sceneTex, I.tcp.xw), fac.y);
  src=lerp(src, tex2D(sceneTex, I.tcn.zy), fac.z);
  src=lerp(src, tex2D(sceneTex, I.tcn.xw), fac.w);
  //*/

  /*
  if (fac.x>0) src=lerp(src, tex2D(sceneTex, I.tcp.zy), fac.x);
  if (fac.y>0) src=lerp(src, tex2D(sceneTex, I.tcp.xw), fac.y);
  if (fac.z>0) src=lerp(src, tex2D(sceneTex, I.tcn.zy), fac.z);
  if (fac.w>0) src=lerp(src, tex2D(sceneTex, I.tcn.xw), fac.w);
  //*/

  fac=(fac>0 ? fac : -fac.zwxy);

  return (
    tex2D(sceneTex, I.tcp.xy+fac.xy*pixelofs.xy) +
    tex2D(sceneTex, I.tcp.xy-fac.zw*pixelofs.xy)
  )*0.5;

  //return src;
  //return pack_color(src.rgb, src.a);
}
