1

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()?

jozxyqk
  • 16,424
  • 12
  • 91
  • 180
matthias_buehlmann
  • 4,641
  • 6
  • 34
  • 76

1 Answers1

1

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • currently I don't use mipmaps at all. I just try to use the same texture which i use as image2D also as sampler2D (before i used imageLoad and imageSave, and now i want to repalce imageLoad by texelFetch). but if I use texelFetch on the sampler (with the same coordinates which i used for imageLoad on the image before) then i just get black. – matthias_buehlmann Mar 23 '12 at 10:26
  • in one pass, i never read and write from the same texel. i always only either read or only write a particular pixel – matthias_buehlmann Mar 23 '12 at 12:13
  • so u suggest I should use imageLoad and image2D to access MipMap Level 0 and use texelFetch and sampler2D to access mipMap levels > 0? – matthias_buehlmann Mar 23 '12 at 12:15