
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 hd0=SAMPLE(+1, +1);
  float hd1=SAMPLE(-1, +1);
  float hd2=SAMPLE(-1, -1);
  float hd3=SAMPLE(+1, -1);

  //float3 n=normalize(float3(hx0-hx1, 2, hz0-hz1));
  float3 n=normalize(float3((hx0-hx1)*2+hd1+hd2-hd0-hd3, 8, (hz0-hz1)*2+hd2+hd3-hd0-hd1));

  float2 n2=n.xz*0.5+0.5;

  float2 i;
  float2 f=modf(n2*256, i);

  return float4(i*(1.0/256), f);

  #undef SAMPLE
}
