OpenGL 4.2 use same texture as image and as sampler

2.5k Views Asked by At

Is it legal (and well defined) to use the same texture as an image2D as well as a sampler2D?

currently I use imageLoad() and imageStore() within the GLSL shader to write and load from a image2D. However, I would like to read (not write) also from some mipmap levels of the texture, but this is not supported by imageLoad (I think I would have to bind each mipMap level as a separate image2D but the ammount of available image units is quite limited). So my question is, whether it's ok to use the same texture which i use as a image2D for imageStore() also as a sampler2D to use with textureLod()?

1

There are 1 best solutions below

3
On

Legal? Yes. Well-defined? It depends on what you're doing with them.

You say that you want to write to images and then sample mipmaps from textures. Are you sampling from the same mipmap that you wrote from? If so... that's going to be a problem. Texture accesses are not required to be coherant, so there's no guarantee that this will work. Not without an explicit CPU glMemoryBarrier call between the time it is written and the time it is sampled. And since it requires CPU intervention, it can't be done within the same shader.

If you provide protection to prevent sampling from mipmaps that you aren't writing to (and by "you", I mean "every shader invocation you're using"), then everything should be fine. Using textureLod or setting the BASE_LEVEL of the texture or something similar should be fine.