I’m writing a frag shader which must sample a given texture in a region around the given pixel. I’m using texelFetch
instead of texture
because I want the exact value of the neighboring pixels without any filtering. Originally I wrote code along these lines:
in vec2 pixel;
uniform sampler2D tex;
void main() {
float leftPixel = texelFetch(tex, ivec2(pixel.x - 1, pixel.y), 0).r;
...
}
However, after reading about dependent texture reads, I changed my code to something more like this:
in vec2 pixel;
in vec2 leftPixelLoc;
uniform sampler2D tex;
void main() {
float leftPixel = texelFetch(tex, ivec2(leftPixelLoc), 0).r;
...
}
In the above code, the vertex shader calculates leftPixelLoc
for me. My hope was that this would prevent a dependent texture read. However, I’m realizing now that simply the call to ivec2
may cause a dependent texture read regardless. Is this the case? If so, how can I avoid a dependent texture read while accessing the exact value of the pixels around my target pixel?
Thank you!