Texture sampling will move when sampled same position.

I’m facing a sampling problem. I’m trying to use Compute Shader via SampleLevel method.

I’ve written shader code like below.

[numthreads(8,8,1)]
void Test(uint2 id : SV_DispatchThreadID)
{
    float w, h;
    _SourceTexture.GetDimensions(w, h);

    float2 uv = float2(id.x/w, id.y/h);

    float4 t = _SourceTexture.SampleLevel(_LinearClamp, uv, 0);
    _ResultTexture[id] = t;
}

And it will be dispatched like below.

int kernel = _shader.FindKernel("Test");
_shader.SetTexture(kernel, "_SourceTexture", _previewBuffer.Current);
_shader.SetTexture(kernel, "_ResultTexture", _previewBuffer.Other);
_shader.Dispatch(kernel, _previewBuffer.Width / 8, _previewBuffer.Height / 8, 1);
_previewBuffer.Swap();
_preview.texture = _previewBuffer.Current;

The texture will swap each frame.

So I’m wondering why the shader code sampled same position but the texture will move toward up-right.

I also attached a movie for the problem.

Does anyone have any idea?

alt text

I’ve solved it myself. The problem is the sampling to left bottom of pixel. I modified a code like below.

float2 uv = float2(id.x/w, id.y/h) + float2(0.5/w, 0.5/h);

It will access to the center of pixel. Then it will fix.