# OpenGL true occlusion culling

## Recommended Posts

Hi, I need to know wether an object is visible or not. Since we have some issues with openGL and hardware occlusion queries and the support of prev. video cards, I decided to propose another way, and see if this is doable. Maybe I am missing something, or maybe my methods would suck, please give me some feedback! First we render the whole scene to the backbuffer and depthbuffer, without the objects to test. Second we use the depthbuffer from the main scene, use a render target texture, and render all the objects to identify with a color code that can reproduce a number once read back. Third we scale down the rendertexture to a small size. This will remove all "little visible" objects but maintain most very visible once, say we scale with a factor of 4 Forth we read back all the pixels from the scaled down texture and identify the visible objects. How does this sound?

##### Share on other sites
That requires you to synch up the CPU and GPU, so you'll create a large stall...

Some new console games are actually using a software renderer to rasterize a low-resolution depth buffer, and then do all the occlusion tests on the CPU to avoid the stall ;)

##### Share on other sites
Do you have any idea if this is more productive than causing that stall? It seems logic to reuse the original depthbuffer, but i get your point about transfering the data...

##### Share on other sites
Is your scene static? Perhaps you can precompute occlusion information into a scene partitioning structure like a BSP or octree then you don't have to do this in real time.

##### Share on other sites
not fully perse, my intentions are to calculate visibility within dynamic scenes as fast as possible, but most games at this moment have most of their content static

##### Share on other sites
Hardware occlusion culling isn't used by lots of games, and it definitely shouldn't be the only method of occlusion-culling used.

There's an article on gamasutra (can't find it at the moment) and probably some here on Gamedev too, which describe "occluders" or "occlusion volumes".

Basically, you define a convex polygonal shape for large objects in your scene. You then find the edges of this shape and use them to project an occlusion frustum out into the scene. You can then test the bounding-boxes of small objects on the CPU to see if they lie inside any of these frustums -- if they do, they're hidden.

You should probably also look into sector/portal systems. Portals can be culled using the same method as above (test the portal's BB against occluders, if hidden, don't traverse into the sector linked by the portal).

##### Share on other sites
just to be sure, before i go into depth of research, this does result in knowing if an objects visiblity is not disturbed by other objects right?

##### Share on other sites
before you start with occlusion culling, you shall identify what you want to achive.

occlusion culling is not to make something faster, it's rather to load-balance work. you will have more costs on one side and less on another, the goal is to save time in average by utilizing underutilized resources.

e.g.
-you have damn expensive pixelshaders, in this case your goal #1 is to cull pixels. This could be accomplished by a z-prepass.
-you are vertex limited. in this case your idea is not that bad, you could use the ID-rendertarget in a 2nd pass and identify the visible IDs. resolve them into an ID-buffer (1d texture either set to 0 or 1) and the first instruction in the vertexshader would check the ID and either reject the wohle vertex (dynamic branching) or execute the whole shader (like comple FFT for water or 8weight skinning for some 50k character).
-you are drawcall bound, in this case u need an earlier stage to cull, maybe a software rasterizer or even some artist set anti-portals (which are damn simple to implement) would help already.

sometimes some middleware could help :)

##### Share on other sites
Quote:
 You should probably also look into sector/portal systems. Portals can be culled using the same method as above (test the portal's BB against occluders, if hidden, don't traverse into the sector linked by the portal).

I would advise against using portals for the following reasons:

1. the are limited to indoor environments

2. you cannot classify where you are in the world without keeping track of every single sector you are in and testing whether you intersects a portal or not every time you move. (you also need to pair yourself with an initial sector) (this goes for all objects, not just the player). You *could* use a BSP tree to classify where you are (like in doom3) but anything involving a bsp is hellish work.

3. portal culling is done in world space, image-space methods should be used as they address the actual problem of occlusion on a raster grid rather than be specific to triangular meshes. e.g. In an image space method - dynamic particles could also be used as occluders.

##### Share on other sites
Quote:
 2. you cannot classify where you are in the world without keeping track of every single sector you are in and testing whether you intersects a portal or not every time you move. (you also need to pair yourself with an initial sector) (this goes for all objects, not just the player). You *could* use a BSP tree to classify where you are (like in doom3) but anything involving a bsp is hellish work.
that depends on the rooms. if you have room geometry as well, it's fairly easy to check where you're in, and you can assume that "indoor" scene do have those "rooms". in cryengine it seems to be a simple n-gone.

Quote:
 3. portal culling is done in world space, image-space methods should be used as they address the actual problem of occlusion on a raster grid rather than be specific to triangular meshes. e.g. In an image space method - dynamic particles could also be used as occluders.

imagespace doesn't strictly imply that a raster grid is used. doing portalculling in imagespace reduces the work to simple 2d-vector-math (rect-rect clipping).

I think a big advantage and at the same time disadvantage is that you need humans to set it up. so they need to be skilled in setting them in the most performant way. portals to cull empty rooms are useless, but also missing portals can be a big performance hit.

but I think the OP was anyway asking for something for dynamic scene, that makes PVS and portals kinda obsolete.

##### Share on other sites
You can still make use of scene partitioning even if not the entire scene is static. If you know, given a part of the map, what other parts are visible, you can check if the dynamic object is outside the visible parts, and if so, cull it.

##### Share on other sites
Just to let you guys know:

I need to know if a view specific objects are visible for the viewer or not, hence the idea of checking with occluders. This doesn't need to be 100% pixel perfect, I need to know if an object is visible enough, so there is room for error (thus I can use a primitive object like a box in replacement of the actual objects in the scene)

I thought about using a software rasterizer (idea1, extension) in case of no hardware occlusion support.

Also i thought about using hardware occlusion when possible, either hardware occlusion queries or the method I described in my fipo.

It is not important that the check is done every frame from the game, but it does need to be checked periodically (let's say at least twice each second) so in case of software rasterizing / occlusion check this could be threaded in a second thread, away from the main thread.

Please keep spoofing me with idea's, critism and suggestions, since I am very curious on how to solve this.

Oh and p.s. as in any case, it needs to be as fast as possible :-P

##### Share on other sites
How is your scene structured? I.e., Is there a good amount of occlusion from just culling dynamic objects behind static objects, or do you really need dynamic objects to cull dynamic objects?

If there are good static occluders (indoor scenes, outdoor scenes with occluding obstacles?), then I suggest looking for a visibility preprocessing algorithm or tool, which will allow you to make very quick decisions using simple queries at run-time.

If you really need to test for occlusion behind dynamic objects, then as another poster said, you should really consider where your bottleneck is, and see if either a z-pass or front-to-back sorting will get you what you need (removing pixel shaded overdraw). If you are geometry limited, then you should probably consider hardware occlusion culling (be careful with this, if you query to frequently, you will sync gpu to cpu and perf will suffer, if you batch too much, then by the definition of your scene type you may not get enough occlusion).

A SW rasterizer is good and all, but it is really difficult to build one with the flexibility of the API geometry pipe (VS and all). Perhaps if you were only using a some simple transforms (single matrices, pass through, etc.)

Something that you haven't really mentioned is if you intend to use this for rendering or for some other purpose (e.g., AI). Is it the former or latter? If the latter, then definitely use occlusion queries, and read back the results after a few frames.

As for the original idea. Just don't do it. :-) Reading back the frame data is a terrible idea (a huge stall, at least in DX9).

##### Share on other sites
You could render all occluders in the scene without texture mapping, lighting etc. (This could be done in one draw call).

Then get the z buffer and build mip-maps, each mip-map is one fourth the size of the previous. When building a mip map you combine four z values into one by using the maximum z value of the four. At the highest level you would have just one pixel (z value) which is the furthest z value in the scene for this view point.

You could then take your objects 3d bounding box, project all vertices to the screen and build a 2d bounding box around those vertices. You then would use the closest z value of the bounding box and rasterize a simple rectangle to the highest level mip-map. If at any point the rectangles z value is < the z-value in the z buffer you move to a lower level mip until you reach the lowest level, if however no z-values in the buffer are < the rectangles z value you know that the box is not visible and can be culled away.

This is an idea I am going to implement soon, its just a slightly modified HOM's algorithm. I don't know how expensive it would be to lock the z buffer and build mip-maps each frame.

##### Share on other sites
Quote:
 Original post by crowley9How is your scene structured? I.e., Is there a good amount of occlusion from just culling dynamic objects behind static objects, or do you really need dynamic objects to cull dynamic objects?If there are good static occluders (indoor scenes, outdoor scenes with occluding obstacles?), then I suggest looking for a visibility preprocessing algorithm or tool, which will allow you to make very quick decisions using simple queries at run-time.If you really need to test for occlusion behind dynamic objects, then as another poster said, you should really consider where your bottleneck is, and see if either a z-pass or front-to-back sorting will get you what you need (removing pixel shaded overdraw). If you are geometry limited, then you should probably consider hardware occlusion culling (be careful with this, if you query to frequently, you will sync gpu to cpu and perf will suffer, if you batch too much, then by the definition of your scene type you may not get enough occlusion).A SW rasterizer is good and all, but it is really difficult to build one with the flexibility of the API geometry pipe (VS and all). Perhaps if you were only using a some simple transforms (single matrices, pass through, etc.)Something that you haven't really mentioned is if you intend to use this for rendering or for some other purpose (e.g., AI). Is it the former or latter? If the latter, then definitely use occlusion queries, and read back the results after a few frames.As for the original idea. Just don't do it. :-) Reading back the frame data is a terrible idea (a huge stall, at least in DX9).

Hi,

to remove some confusion, we trying to make different setups for different engines plugin-wise, to check if certain objects were seen by the user, for testing purpose in research. The scene's are dynamic and not persé static, but it would be for lower profile games that do not take the extreme out of your pc just yet ;-)

I read some articles but i forgot to bookmark them since it was a while ago that a lot of engines now days create CPU occlusion maps to optimize scenes, which would be a good idea the other way around, to test if something was visible rather then if something is invisible.

To react on your idea about software rasterizers: nothing fancy is needed, it is just pure for testing occlusion, on a non 100% accurate basis but accurate enough for us to tell that the person beeing tested saw the object good enough. Imagine us hanging a plane with a bord saying "did u see this?" in a room. The whole idea of the plugin probably has some different output later on, that's why i want to try a software solution rather then standing behind it. So nothing fancy, just transformation of the view/projection and draw of an id or something.

I do know about all other techniques to pre process a lot (frustum culling, octtree's etc) and I also even thought about the idea that we can have a maximum radius where visibilty comes in to action. Since we have about 5 objects per scene/level to test, we can make a routine that first checks if those objects are in range, and if so, if they are visible.

And to note: I have no intention to think I can make a plugin that works on any engine that is out there, but I do want to try and make a few fallback theories to support things that are there (hardware occlusion queries, my idea was an idea but as we can see it was crushed and shot down ;) ) and based on that try to make plugins for different engines.

Thanks so much for all your help guys, you are really giving me some idea's and thoughts. I hope I answer any unclear questions/idea's with this post, and I hope you have some energy left ;-)

##### Share on other sites
Quote:
 Original post by staticVoid2You could render all occluders in the scene without texture mapping, lighting etc. (This could be done in one draw call).Then get the z buffer and build mip-maps, each mip-map is one fourth the size of the previous. When building a mip map you combine four z values into one by using the maximum z value of the four. At the highest level you would have just one pixel (z value) which is the furthest z value in the scene for this view point.You could then take your objects 3d bounding box, project all vertices to the screen and build a 2d bounding box around those vertices. You then would use the closest z value of the bounding box and rasterize a simple rectangle to the highest level mip-map. If at any point the rectangles z value is < the z-value in the z buffer you move to a lower level mip until you reach the lowest level, if however no z-values in the buffer are < the rectangles z value you know that the box is not visible and can be culled away.This is an idea I am going to implement soon, its just a slightly modified HOM's algorithm. I don't know how expensive it would be to lock the z buffer and build mip-maps each frame.

To me, this sounds very expensive and a lot of work. How would this improve against rendering 3D bounding volumes to an occluder buffer and do a read-back on that? Wether this occluder buffer is through hardware occlusion queries of an offscreen plane or a smart software rasterizer thing, I think making mipmaps and locking the depth buffer would stall a lot, but I am not sure about this?

##### Share on other sites
Quote:
 To me, this sounds very expensive and a lot of work. How would this improve against rendering 3D bounding volumes to an occluder buffer and do a read-back on that?

it is essentially the same thing although with using mip-maps you avoid expensive fill rates.

you also render a simple rectangle around the bounding box rather than up to a possible 6 triangles.

##### Share on other sites
Quote:
 Then get the z buffer and build mip-maps, each mip-map is one fourth the size of the previous.

from VMem to system mem? that's probably more expensive than anything you can gain from it. if you want to rely on the hardware, you need to work asynchronously and for that occlusion queries are the way to go.

Quote:
 You could then take your objects 3d bounding box, project all vertices to the screen and build a 2d bounding box around those vertices. You then would use the closest z value of the bounding box and rasterize a simple rectangle to the highest level mip-map.
this approauch would be very view dependend. in lot of cases object would be market visible behind walls, just because their rect in screenspace, using nearest-z stick through walls. it would work great with occluders that are coplanar with the nearplane, but you just need to turn the came slightly and you might get an dramatic increase in drawcalls.

maybe that'll be good as an first-pass approach.

Quote:
 This is an idea I am going to implement soon, its just a slightly modified HOM's algorithm. I don't know how expensive it would be to lock the z buffer and build mip-maps each frame.
building mipmaps on gpu would probably be fairly simple, but getting the buffer might be damn expensive.
you could maybe experiment with staging buffers from d3d10/11 and map them like 5frames later to be sure u dont stall anything. but then again, occlusion queries would deliver the same result with the same latency.

@FeverGames
so you're making a culling middleware like dPVS by umbra? what exactly do you mean by "we have about 5 objects per scene/level to test"? just 5objects? is it worth to implement culling for 5objects?
but yeah, distance based "culling" is quite common, it's usually combined with LOD systems, so the lowest LOD is just an empty mesh.

##### Share on other sites
Quote:
Original post by Krypt0n
Quote:
 Then get the z buffer and build mip-maps, each mip-map is one fourth the size of the previous.

from VMem to system mem? that's probably more expensive than anything you can gain from it. if you want to rely on the hardware, you need to work asynchronously and for that occlusion queries are the way to go.

Quote:
 You could then take your objects 3d bounding box, project all vertices to the screen and build a 2d bounding box around those vertices. You then would use the closest z value of the bounding box and rasterize a simple rectangle to the highest level mip-map.
this approauch would be very view dependend. in lot of cases object would be market visible behind walls, just because their rect in screenspace, using nearest-z stick through walls. it would work great with occluders that are coplanar with the nearplane, but you just need to turn the came slightly and you might get an dramatic increase in drawcalls.

maybe that'll be good as an first-pass approach.

Quote:
 This is an idea I am going to implement soon, its just a slightly modified HOM's algorithm. I don't know how expensive it would be to lock the z buffer and build mip-maps each frame.
building mipmaps on gpu would probably be fairly simple, but getting the buffer might be damn expensive.
you could maybe experiment with staging buffers from d3d10/11 and map them like 5frames later to be sure u dont stall anything. but then again, occlusion queries would deliver the same result with the same latency.

@FeverGames
so you're making a culling middleware like dPVS by umbra? what exactly do you mean by "we have about 5 objects per scene/level to test"? just 5objects? is it worth to implement culling for 5objects?
but yeah, distance based "culling" is quite common, it's usually combined with LOD systems, so the lowest LOD is just an empty mesh.

Well yeah, "just 5". These objects are important to be marked as seen once they have been seen, so yeah, a fair small share of the CPU or GPU can be given away for this. To come to a fair trade of cost against speed, I already said it doesn't need to check every game frame, it can be done in a fixed once in a while time step.

Why would a threaded CPU occlusion rasterizer be not done? With Bounding boxes?

##### Share on other sites
Quote:
 from VMem to system mem? that's probably more expensive than anything you can gain from it. if you want to rely on the hardware, you need to work asynchronously and for that occlusion queries are the way to go.

you could implement a basic software rasterizer to render the occlusion geometry to a low-resolution z buffer and then build the mip-maps but then it becomes of a problem of determining the set of occluders that are visible as the software rasterizer will likely be to slow to render them all.

##### Share on other sites
Quote:
Original post by staticVoid2
Quote:
 from VMem to system mem? that's probably more expensive than anything you can gain from it. if you want to rely on the hardware, you need to work asynchronously and for that occlusion queries are the way to go.

you could implement a basic software rasterizer to render the occlusion geometry to a low-resolution z buffer and then build the mip-maps but then it becomes of a problem of determining the set of occluders that are visible as the software rasterizer will likely be to slow to render them all.

I still don't really get what the use of mip maps are in the ideas..

##### Share on other sites
The objects that I want to know visibility from are always a plane. How about calculating a few rows and columns of points on that plane in world space, and test with rays from the centre of the camera to these points against the view frustum visible objects their bounding volumes?

##### Share on other sites
Quote:
 Well yeah, "just 5". These objects are important to be marked as seen once they have been seen, so yeah, a fair small share of the CPU or GPU can be given away for this. To come to a fair trade of cost against speed, I already said it doesn't need to check every game frame, it can be done in a fixed once in a while time step.

so you're cost would be kinda 99% for generating an occlusion buffer, 1% for testing some bounding box against it. that's not effective.

Quote:
 Why would a threaded CPU occlusion rasterizer be not done? With Bounding boxes?
why that effort if the result is the same as a hardware occlusion query would be?

in a case where you've few variable costs and most is fixcost, you goal needs to be to reduce the fix costs, so get rid of generating an occlusion buffer.
an alternative method to accomplish that can be raycasts. That's a common way to check for visibility e.g. for AI where you have N objects (typically <10) that need to know if there is any visibility inbetween. that would usually mean you have to rasterize N occlusion buffers. it's cheaper to make raycasts in that case.
especially with just 5 objects to test, you'll probably be faster.

Quote:
 I still don't really get what the use of mip maps are in the ideas..

Mipmaps are used to have a conservative representation of far more data. it's useful in cases where u assume that most objects will be culled.
Imagin the simplest case, the mip level with just 1zexel. you just need to compare the nearest-z of an object with one depth value an if everything works out, you reject the whole object immediatelly.
it's of course the opposide with fine occlusion that is not that dense e.g. open enviropments where you always have some skybox-depth value that is max.
for reference: http://www.gamasutra.com/view/feature/3394/occlusion_culling_algorithms.php?page=2

Quote:
 The objects that I want to know visibility from are always a plane.
you test always 5 planes? i'm really curious what you're doing. with 5 planes you might be able to have the possibility for some fast analytical methode, like projecting the scene triangles on the planes and tesselate it (some kind of 2d bsp). that could be specially fast if you have a low amount of triangles that cause a lot of overdraw.

Quote:
 How about calculating a few rows and columns of points on that plane in world space, and test with rays from the centre of the camera to these points against the view frustum visible objects their bounding volumes?

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627653
• Total Posts
2978433
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33