1

I've just started getting into the topic of occlusion queries in OpenGL, but I'm a bit confused about how they actually work.

In most examples I've found, the depth and color masks are deactivated before drawing with the occlusion query (Because we don't need to actually 'draw' anything), in essence somewhat like this:

glDepthMask(GL_FALSE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

glBeginQuery(GL_ANY_SAMPLES_PASSED,query1);
    // Draw Object 1
glEndQuery(GL_ANY_SAMPLES_PASSED);

glBeginQuery(GL_ANY_SAMPLES_PASSED,query2);
    // Draw Object 2
glEndQuery(GL_ANY_SAMPLES_PASSED);
// etc

glDepthMask(GL_TRUE);
glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);

(It's assumed that objects are drawn front to back, so object 1 is in front of object 2. The above code is just pseudo code for the sake of this question. The result of the queries would be retrieved at a later time.)

Now, to know if object 2 is actually occluded by object 1, it would need to keep the fragment information from query 1 somehow (I'm assuming in some sort of depth buffer). But we've disabled drawing to the depth and color buffers, which means nothing is drawn, which means it shouldn't store anything anywhere?

Is there a special 'query' buffer? If so, is there a way to access it? Is it in any way connected to the currently bound texture or frame buffer? Do I need to clear it? Am I misunderstanding how occlusion queries actually work?

Silverlan
  • 2,783
  • 3
  • 31
  • 66
  • 1
    it's usually another render pass after everything has been rendered already. – ratchet freak Dec 09 '14 at 17:43
  • Right, that makes sense. Still, where is the information actually stored, and is it possible to access it? (Mostly for debugging purposes) – Silverlan Dec 09 '14 at 17:45
  • 1
    On the GPU/driver and you can get the result with glGetQueryObject – ratchet freak Dec 09 '14 at 17:51
  • So answer to this question would be that you don't clear Depth Buffer before you start Occlusion Queries, is that correct? In other words, you test your queries against the final Depth Buffer from the previous frame? – Bartosz Boczula Jul 12 '18 at 17:23

1 Answers1

1

Now, to know if object 2 is actually occluded by object 1, it would need to keep the fragment information from query 1 somehow

Why would it? Occlusion queries store a counter of the number of samples that pass the depth test, which is a single integer.

Since you've disabled writing to the color and depth buffer, the only thing drawing the objects will do is increment the occlusion query counter*. Object 2 can't possibly occlude object 1 because drawing object 1 doesn't change the depth buffer.

* Unless you have a stencil buffer or are doing something like image load/store in shaders

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • I wouldn't really describe occlusion queries as boolean. They actually count the number of samples that pass the depth test. `GL_ANY_SAMPLES_PASSED` is just a sort of simplification of this, which returns true if the number of samples passed > 0. In effect, it doesn't have to keep counting after a single sample passes. – Andon M. Coleman Dec 09 '14 at 18:08