Search code examples
directxdirectx-11hlslcompute-shader

HLSL ComputeShader Sample from a texture behave strange


Hy!

I have a compute shader:

[numthreads(128, 1, 1)]
void csAdvect(uint2 dtid : SV_DispatchThreadID)
{
  uint4 dtl = uint4(dtid, 0, 0);
  float2 inverseSize = float2(1.0f / gridSize.x, 1.0f / gridSize.y);
  float2 coord = float2(dtid.x*inverseSize.x, dtid.y*inverseSize.y);

  float statict = statictemp.Load(dtl).x;
  if (statict < 0)statict = 0;
  float t = temperature.SampleLevel(zeroBoundarySampler, coord, 0).x;
  outputTemperature[dtid] = statict + t;
}

It's read from a static texture cell, sample an another and write it to an output texture. Bare sample. The textures and the gridSize are 512x512, I call this with Dispatch(4, 512, 1) and in every render step I swap the temperature and outputTemperature textures.

I thought it will do something like a circular grow from the static points, but instead its growing only right-down. The blackish/redish thing in the top left corner is the static 'heat', the less red thing is the temperature from the shader.

enter image description here

Any idea what am I doing wrong?


Solution

  • So all about the coordinate system transformation...

    If you have an 512x512 texture, the texture can sampled as an array from 0,0 to 511,511 so if you want this to scale to 0,0 to 1,1 you need to division with 511 not 512! Silly mistake but hard to find...