
struct VsInput
{
  float2 pos       : POSITION;
  float2 tc        : TEXCOORD0;
  float4 color     : COLOR0;
  float4 scolor    : COLOR1;
  float4 ranges    : TEXCOORD1;
};

struct VsOutput
{
  float4 pos       : POSITION;
  float2 tc        : TEXCOORD0;
  float4 color     : COLOR0;
  float4 scolor    : COLOR1;
  float4 ranges    : TEXCOORD1;
};


float4 tm          : register(c16);


VsOutput vsh(VsInput I)
{
  VsOutput O;
  O.pos=float4(I.pos.xy*tm.xy+tm.zw, 1, 1);
  O.tc=I.tc;
  O.color=I.color;
  O.scolor=I.scolor;
  O.ranges.xy=(I.ranges.xy*2-1)/64+I.tc;
  O.ranges.zw=I.ranges.zw;
  return O;
}


sampler tex : register(s0);


float4 psh(VsOutput I) : COLOR
{
  float  d=tex2D(tex, I.tc).r;
  float sd=tex2D(tex, I.ranges.xy).r;

  float sa=smoothstep(I.ranges.z, 1-I.ranges.z, sd);
  float  a=smoothstep(I.ranges.w, 1-I.ranges.w,  d);

  sa=sqrt(sa);
  a=sqrt(a);

  float4 sc=I.scolor*sa;
  float4  c=I. color* a;

  float4 r=sc*(1-c.a)+c;

  clip(dot(r, 1)-1.0/255);

  return r;
}
