<p>The specification <a href="https://github.com/KhronosGroup/OpenGL-API/issues/19" rel="nofollow noreferrer">has been clarified on this point</a>, as has the Wiki article. So let's explain that.</p>

<p>The new wording in the spec says:</p>

<blockquote>
  <p>the samples-passed count is incremented for each fragment still being processed after the depth test</p>
</blockquote>

<p>This is better wording, as it makes clear that the order in which things are processed matters. OpenGL <a href="https://www.khronos.org/opengl/wiki/Per-Sample_Processing" rel="nofollow noreferrer">defines the depth test as happening after the stencil test, scissor test, etc</a>. So this wording clearly states that the depth test, and everything before it, matter.</p>

<p>So it does respect the stencil test. And fragment shader <code>discard</code>, since the FS is specified to come before all of these tests.</p>

<p>Well, unless you <a href="https://www.khronos.org/opengl/wiki/Early_Fragment_Test#Explicit_specification" rel="nofollow noreferrer">force these tests to happen before the fragment shader</a>. If you do that, then <code>discard</code>ing the fragment in the FS will not prevent that fragment from being counted by the occlusion query. So if you enforce early tests, you lose the right to use <code>discard</code> to affect the occlusion query count.</p>

CC BY-SA 4.0
2017-10-02T15:34:51.003Z
2019-04-03T17:21:41.760Z
2019-04-03T17:21:41.760Z
Nicol Bolas
<p>In a project where I want to select visible objects using using a screen-aligned polygon and various conditions for what "selected" means with regards to that polygon, I'm basically drawing the polygon into the stencil buffer and then drawing the individual objects with <strong>stencil testing and using occlusion queries</strong> for measuring how much the object and selection polygon overlap in screen space.</p>

<p>This works pretty well. But imagine my surprise when stumbling over the statement that, <a href="https://www.khronos.org/opengl/wiki/Query_Object#Occlusion_queries" rel="nofollow noreferrer">according to the OpenGL Wiki</a>, occlusion queries supposedly don't actually care about the stencil test:</p>

<blockquote>
  <p>They detect whether the scoped drawing commands pass the depth test and if so, how many samples in those triangles passed the Depth Test. Note that the sample count and pass/fail are <strong>based only on the depth test. The Stencil Test, or fragment shader discard is irrelevant</strong> with queries, even though they technically happen after the fragment shader...</p>
</blockquote>

<p>This clearly contradicts my practical experience and made me a little worried/confused. I took a look at the specification (both the newest 4.6 core as well as good old 2.0, which we're actually targetting in our project for compatibility) and it also says that occlusion queries count the number of <strong>samples passing the depth test</strong>. It does however not make such a definite statement as the wiki pertaining to the stencil test. And since the <strong>depth test technically comes <em>after</em> the stencil test</strong>, I'm not entirely sure if that maybe already includes the stencil test anyway and the wording of the specification is simply using the last stage for convenience.</p>

<p>So, my question is, <em>do</em> occlusion queries respect the stencil test (or any other earlier per-fragment tests, I'm actually using scissor testing with occlusion queries in another unrelated algorithm)? I see a few possiblities here:</p>

<ul>
<li>They <em>do definitely</em> respect all tests and the wording in the spec is just using the last test for convenience reasons. This would mean that <strong>fragments failing earlier tests and thus not even <em>reaching</em> the depth test would technically count as <em>failing</em> that</strong>. I'm not sure this comes too clearly out of the specification's wording, though, but maybe I'm missing something and people better-versed in the intricacies of specification laywering can shed more light on this. This would also mean the above Wiki article is slightly misleading (or I just misread it).</li>
<li>They <em>definitely don't</em> respect the stencil test and really only care about the depth test. This would mean that a <strong>fragment failing the stencil test would have to still go through depth testing</strong>. It would also mean that what I experience on my specific hardware (it's an nVidia GeForce GTX 1xxx) is clearly a bug.</li>
<li>Or this might simply not be a well-specified part of the API and it leaves room for interpretation. This might be rather odd, though, as this isn't a particularly rarely used feature I'd suppose.</li>
</ul>

<p>(In practice, I could definitely restrucure my algorithm to use depth testing instead, but it is a problem that makes me curious about the theoretic implications and underpinnings nevertheless.)</p>

<opengl><stencil-test>
Do occlusion queries respect the stencil test at all?
Christian Rau
# Do occlusion queries respect the stencil test at all?

(Asked by: username_0 on 2017-10-02)

In a project where I want to select visible objects using using a screen-aligned polygon and various conditions for what "selected" means with regards to that polygon, I'm basically drawing the polygon into the stencil buffer and then drawing the individual objects with **stencil testing and using occlusion queries** for measuring how much the object and selection polygon overlap in screen space.

This works pretty well. But imagine my surprise when stumbling over the statement that, [according to the OpenGL Wiki](https://www.khronos.org/opengl/wiki/Query_Object#Occlusion_queries), occlusion queries supposedly don't actually care about the stencil test:

> They detect whether the scoped drawing commands pass the depth test and if so, how many samples in those triangles passed the Depth Test. Note that the sample count and pass/fail are **based only on the depth test. The Stencil Test, or fragment shader discard is irrelevant** with queries, even though they technically happen after the fragment shader...

This clearly contradicts my practical experience and made me a little worried/confused. I took a look at the specification (both the newest 4.6 core as well as good old 2.0, which we're actually targetting in our project for compatibility) and it also says that occlusion queries count the number of **samples passing the depth test**. It does however not make such a definite statement as the wiki pertaining to the stencil test. And since the **depth test technically comes *after* the stencil test**, I'm not entirely sure if that maybe already includes the stencil test anyway and the wording of the specification is simply using the last stage for convenience.

So, my question is, *do* occlusion queries respect the stencil test (or any other earlier per-fragment tests, I'm actually using scissor testing with occlusion queries in another unrelated algorithm)? I see a few possiblities here:

* They *do definitely* respect all tests and the wording in the spec is just using the last test for convenience reasons. This would mean that **fragments failing earlier tests and thus not even *reaching* the depth test would technically count as *failing* that**. I'm not sure this comes too clearly out of the specification's wording, though, but maybe I'm missing something and people better-versed in the intricacies of specification laywering can shed more light on this. This would also mean the above Wiki article is slightly misleading (or I just misread it).
* They *definitely don't* respect the stencil test and really only care about the depth test. This would mean that a **fragment failing the stencil test would have to still go through depth testing**. It would also mean that what I experience on my specific hardware (it's an nVidia GeForce GTX 1xxx) is clearly a bug.
* Or this might simply not be a well-specified part of the API and it leaves room for interpretation. This might be rather odd, though, as this isn't a particularly rarely used feature I'd suppose.

(In practice, I could definitely restrucure my algorithm to use depth testing instead, but it is a problem that makes me curious about the theoretic implications and underpinnings nevertheless.)

**Tags:** opengl, stencil-test


---

## 1 answers

### Answer by username_1 on 2017-10-02. Score: 4 | Accepted answer

The specification [has been clarified on this point](https://github.com/KhronosGroup/OpenGL-API/issues/19), as has the Wiki article. So let's explain that.

The new wording in the spec says:

> the samples-passed count is incremented for each fragment still being processed after the depth test

This is better wording, as it makes clear that the order in which things are processed matters. OpenGL [defines the depth test as happening after the stencil test, scissor test, etc](https://www.khronos.org/opengl/wiki/Per-Sample_Processing). So this wording clearly states that the depth test, and everything before it, matter.

So it does respect the stencil test. And fragment shader `discard`, since the FS is specified to come before all of these tests.

Well, unless you [force these tests to happen before the fragment shader](https://www.khronos.org/opengl/wiki/Early_Fragment_Test#Explicit_specification). If you do that, then `discard`ing the fragment in the FS will not prevent that fragment from being counted by the occlusion query. So if you enforce early tests, you lose the right to use `discard` to affect the occlusion query count.


---

