I have a compute shader that samples a depth sampler2D, however when using texture() the result is weird with some jagged edges. If I use texelFetch() then it works fine, so I would guess its an issue with getting the correct texture coordinates? FYI the sampler uses nearest filtering and has no mips.
This is how the result looks when using texelFetch
:
texelFetch
expects integer coordinates of the texel; you provide these, they are exact, suffering from no rounding errors, thus you see no artifacts.
texture*
family of functions expect floating-point coordinates, and then converts them back to texel coordinates by floor(uv*size)
formula (this is assuming it's a GL_TEXTURE_2D
with GL_NEAREST
filters, which seems to be your case). This calculation is sensitive to floating point rounding errors when uv
sits right between the texels, which is exactly what happens here.
To fix that, offset uv coordinates by half a pixel:
vec2 TexCoord = (vec2(iuv) + 0.5)/vec2(size);