OpenGL - Occlusion query depth buffer?

1.3k views Asked by At

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?

1

There are 1 answers

1
Colonel Thirty Two On

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