2

I implement HDR in my graphics engine (deferred rendering) and I need to calculate an average luminance of the image (RGBA16F). I generate mipmaps and then I read the smallest mipmap (1x1).

glBindTexture(GL_TEXTURE_2D, hdrTex);
glGenerateMipmap(GL_TEXTURE_2D);
glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_FLOAT, data);
glBindTexture(GL_TEXTURE_2D, 0);

glm::vec3 color = glm::vec3(data[0], data[1], data[2]);
float avgLum = glm::dot(color, glm::vec3(0.2125, 0.7154, 0.0721));

I noticed that sometimes (when I move a camera) data[0], data[1] and data[2] contain 1.#QNAN. Why ? How to fix that ?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Irbis
  • 11,537
  • 6
  • 39
  • 68

1 Answers1

-2

Some OpenGL drivers require you call glFinish() just after the glGetTexImage() to make sure the result is available to the next instruction.

Otherwise you have a race condition where you read uninitialized values from data[].

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
  • 1
    No, you do not. OpenGL guarantees that it will be finished with the operations on any pointer you pass when it returns to you. So `glGetTexImage` must have finished reading the texture into your pointer when it returns to you. If that means the implementation must implicitly execute a `glFinish`, so be it. Now, you may be correct that this resolves the problem. But that would only be a workaround for a *driver bug*. – Nicol Bolas Dec 29 '15 at 00:49
  • In theory, there is no difference between theory and practice. But, in practice, there is. I'm on the practical side of things. – Sergey K. Dec 29 '15 at 06:20
  • 1
    In practice, you shouldn't tell people things that give them the wrong impression. Specifically, that this is the *expected behavior* of the function, rather than a specific driver's bug. Otherwise, they'll start sticking `glFinish` all over the place under the mistaken assumption that they need to. In practice, misinformation is misinformation. – Nicol Bolas Dec 29 '15 at 13:19
  • True. But the real world issue will not be solved this way (and IHVs are way to slow pushing any bugfixes from indie developers into their drivers). – Sergey K. Dec 29 '15 at 21:07