How expensive is an occlusion test?

I have the objects in my scene roughly sorted according to a painter’s algorithm and am rendering them front to back. To speed things up, I want to first render their bounding box with writes to the color and depth buffers disabled and use a Query object to count the number of fragments that make it onto the screen. If 0 fragments are drawn, I do not draw the bounded object. While this lets me throw away a lot of geometry, every Query will require the pipeline to be flushed.

Would I save more time than I loose with this technique? Is there a way to speed this up?

Theres some interesting papers on using the occulsion query on nvidia’s site, well worth a read. IIRC most focus around doing “something” while wating for the query to return, rather than the basic approach of sending the query and stalling until it returns. Tricks like sending the query early during the frame and using it later, or even using the result from one frame in the next (at the expense of some accuracy, naturally).

The next major version of GL is rumoured to have some form of conditional rendering, so you can tie the result of a query to a render chunk, but that’ll be a while comming.

Whether you get any sort of speed up probably depends on your scene - but unless you’ve got hugely detailed meshes which are often hidden from view I suspect it’ll be more trouble than its worth. A simple z-only pass to save fillrate might be a better idea (of course, you could always do both).

Thanks, I’ll take a look at the docs.

What do you mean by a z-only pass to save fill rate?

A z-only pass means you draw some/most/all of your geometry to the zbuffer, but without writing anything into the framebuffer. Because you’re not using textures or shaders it can be very quick. Then when you do your actual drawing the full zbuffer is already present and each pixel only needs to be shaded once. Good if you’ve got lots of overdraw or complex pixel shaders.

Of course the rough front to back sorting should have a similar effect, so you may want to compare the performance difference between them.

The functionality you say is rumored for the next major release of OpenGL (OpenGL 3.0, I presume?) sounds just like what I’m looking for. Is there any estimate about when it might be released? Where could I track it’s progress?

Also, would you have any idea if queries could be nested? (Ie, the conditional instructions themself contain conditional instructions)

I wouldn’t hold your breath, it should be in for the “Mt. Evans” standard ( http://www.opengl.org/pipeline/vol002/ ) which is aimed at October 2007. But there’s no gurantee it’ll make it in. You’ll also need at least a few months delay before it starts appearing in drivers, and it’ll probably only run on really high end hardware.