Hardware Occlusion Queries, why use them next frame?

Started by
5 comments, last by solenoidz 11 years, 9 months ago
So working with Hardware occlusion queries, I see many tutorials and game developers saying to use them a frame later due to stalling and all that.

What I don't get is how occlusion queries are different from, for example, multiple render passes. In deferred shading, you render to a G buffer, and afterwards you get the buffer back and use it instead in the lighting shaders pass. Wouldn't that cause some kind of stall just like getting back occlusion query results?

I'm working on kind of a hybrid algorithm using a unifrom grid.

I have a first pass to determine which uniform grid cells are completely occluded. My view frustum traverses the grid front to back and I render simplified artist defined geometry for large occluders to the depth buffer with color write off. This geometry would have to be fully containable by the real geometry. At the same time, I draw the grid cell itself with depth and color write off and issue an occlusion query for it. The cells are queried against the large artist defined occluders to see if any cells in the scene can be completely filtered out.

After this I retreive the hardware query results for the cells, then send all objects to the renderer in the cells I determined to be visible. The depth buffer is still populated by the simplified large occluders I used in the last pass.

So in the second pass of the frame I now do the standard, draw simple bounding box for each object with color and depth write off and issue a query for it, then render it next frame in the second pass for real if it passed by using the occlusion query result next frame.

So I'm wondering if my first pass is a bad idea. I read that Battlefield 3 and Cryengine 3 do this kind of thing with software occlusion queries instead to avoid the CPU stall and use the results in the same frame instead of next frame. Am I effectively halfing my frame rate by doing this with hardware queries?
Advertisement
The difference with your example of deferred rendering is that with a deferred render, or any technique that has multiple render passes, is that you never actually read back that render target on the CPU. You're only using the data as a texture that's being fed into the next render pass. So there's never a stall from having to copy the data back to the CPU.

With occlusion queries though you need to read them back on the CPU side. That's why developers wait a frame with them.
At least OpenGL has conditional render which in theory would allow the occlusion queries also to stay on the GPU. I found this to work reasonably well on new hardware (gtx560ti) but very badly on my gtx260m. On the new hardware using the conditional render in the same pass as the queries gave me a speed up of factor 1.5-5 depending on the amount of occlusion. But it would actually reduce performance on the older GPU.

I have an example of this here: https://github.com/p...onal_render.cpp

Edit: DX seems to have this functionality as well I just wasn't sure at the point of writing the post since I'm not really experienced with DX

What I don't get is how occlusion queries are different from, for example, multiple render passes. In deferred shading, you render to a G buffer, and afterwards you get the buffer back and use it instead in the lighting shaders pass. Wouldn't that cause some kind of stall just like getting back occlusion query results?

When you call the graphics API, your command is most of the time enqueued in a command queue, the GPU will process this command queue independently of the API call. We talk about a stall, when you enqueue a command which requires an immediate feedback, in this case the CPU has to wait until the GPU has processed all commands up to the according API call, which is more or less some kind of busy waiting (aka flushing the command queue, the CPU is not actually busy waiting, but your processing thread is paused at least).

Therefore it is very important to almost never call such a feedback API function to optimise the usage of CPU and GPU. A simple OGL error checking call can already flush the command queue, requesting the result of an occlusion query will flush the command queue up to the occlusion query command at least, therefore the safest way to check it, is once the framebuffer is displayed (most likely in the next frame when the framebuffer has been swapped).


So I'm wondering if my first pass is a bad idea. I read that Battlefield 3 and Cryengine 3 do this kind of thing with software occlusion queries instead to avoid the CPU stall and use the results in the same frame instead of next frame. Am I effectively halfing my frame rate by doing this with hardware queries?

Occ queries, much like geometry shaders, sounds awesome first, but fall short of your expectation once you understand their limitations.
As a side note, in GPU Pro 2, there is a good article describing the use of hierarchical z-buffer queries using a render target that looks very interesting.
We think in generalities, but we live in details.
- Alfred North Whitehead
OK thanks guys. That really cleared it up. I can see how rendering to textures and all that avoids the stalls I thought were the same thing as using occlusion queries. The CPU side just has the texture or buffer name and doesn't need the data, while the GPU actually uses the data in them.
People are talking about software occlusion culling. I think Frostbite guys had an article where they describe it. Basically, they maintain a low resolution software depth buffer - pretty much an array of floats. They generate this buffer by rendering big occlusion low res geometry that happens to be in front of the camera. For example big planes, "hidden inside" the walls, or low res version of the building itself. They are using software rasterizer to draw those triangles into the software depth buffer. So far, everything is done on the CPU side. When the buffer is filled with depth values from the potential occluders, they simply check other objects bounding volumes against that buffer and decide to draw or not to draw.
At least, that's the way I get it.

This topic is closed to new replies.

Advertisement