Search code examples
performanceopenglglsl

Does conversion from vec2 to ivec2 result in a dependent texture read?


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?


Solution

  • "Dependent texture reads" have not been a meaningful concept in graphics programming for over 15 years. It existed to describe hardware that had extreme limitations on texture accesses, but that hardware basically doesn't exist anymore. And none of that hardware could have handled texelFetch; they couldn't even handle real integers in shader math. So it's unclear how hypothetical hardware where this was a problem would behave with such shaders.