
struct VsOutput
{
  float4 pos: POSITION;
  float2 tc : TEXCOORD0;
};

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

VsOutput vsh(float2 pos : POSITION)
{
  VsOutput O;
  float2 p=pos*ofs.xy+ofs.zw;
  O.pos=float4(p, 1, 1);
  O.tc=p*texsz.xy+texsz.zw;
  return O;
}


sampler hmap       : register(s0);

float4 pixelOfs    : register(c0);


float4 psh(VsOutput I) : COLOR
{
  #define SAMPLE(DX, DZ) tex2D(hmap, I.tc+pixelOfs.xy*float2(DX, DZ)).r

  float hx1=SAMPLE(+1,  0);
  float hx0=SAMPLE(-1,  0);
  float hz1=SAMPLE( 0, +1);
  float hz0=SAMPLE( 0, -1);

  float hc =SAMPLE( 0,  0);

  float3 n=normalize(float3(hx0-hx1, 2, hz0-hz1));

  float4 dh=float4(hx0, hx1, hz0, hz1)-hc;
  float ao=dot(1-dh*rsqrt(dh*dh+1), 0.25);

  return float4(n*0.5+0.5, ao);

  #undef SAMPLE
}
