Jump to content

  • Log In with Google      Sign In   
  • Create Account

Rendering many trees


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
27 replies to this topic

#1 jmakitalo   Members   -  Reputation: 551

Like
0Likes
Like

Posted 27 May 2012 - 09:39 AM

I have set up a system for rendering specifically trees, which are also generated by my engine. They are divided into basically two groups: trunk/branches and leaves. Both have their own index buffer and texture and they share the same vertex, normal etc. buffers. I decided to try instancing to speed up the OpenGL implementation, but I'm not sure if it will be much use, at least on its own. I have not yet implemented a level of detail, but want first to see how fast rendering can be with only quadtree culling and instancing. The tree instances are also sorted by textures and vertex buffers binds.

I have found that for some reason, rendering the trunk and branches is fast, but rendering the leaves is really slow. The leaves consist basically of triangle pairs, rendered with alpha testing (no blending) and with no face culling. In my test scene, I have 3000 trees, which are frustum culled to less than 1/4. The trunk/branches consist of 2166 indices and the leaves consist of 582 indices. When rendering only the trunk/branches, FPS can be around 60, whereas rendering the leaves only yields around 20 FPS. See the images below:

http://www.perilouspenguin.com/glfps/wp-content/uploads/2012/05/full.jpg
http://www.perilouspenguin.com/glfps/wp-content/uploads/2012/05/leaves.jpg
http://www.perilouspenguin.com/glfps/wp-content/uploads/2012/05/trunks.jpg

First I was thinking that it might be a fillrate problem, as the leaves clearly occupy more screen space. But turning the camera away from the leaves does not seem to make any practical difference. Could this have to do with how indexing or vertex data placement works out for the branches and the leaves? For the branches, a single vertex is referenced around 5 times, whereas for the leaves, only 1 or 2 times. Still, there are a lot less vertices in total for the leaves in the test scene.

I understand that LOD could make all the difference in the end, but I have also tried to reduce the number of leaves, and it does not seem to make a big difference. Even a small amount of leaves per tree seems to kill the frame rate pretty good.

Sponsor:

#2 (ok)   Members   -  Reputation: 104

Like
0Likes
Like

Posted 27 May 2012 - 09:53 AM

Are you using mip-maps for your leaves texture ?

If not, you may speed up things by doing so, and also potentially reduce your aliasing.

Edited by (ok), 27 May 2012 - 09:57 AM.


#3 jmakitalo   Members   -  Reputation: 551

Like
0Likes
Like

Posted 27 May 2012 - 10:05 AM

Are you using mip-maps for your leaves texture ?

If not, you may speed up things by doing so, and also potentially reduce your aliasing.


Yes, I'm using mip-maps.

#4 (ok)   Members   -  Reputation: 104

Like
0Likes
Like

Posted 27 May 2012 - 10:16 AM

Are you using frustum culling ?
If yes, I don't understand why your fps still stalls even if the camera is not facing the leaves.

What is the size of your leave texture ?
Did you try to load a really low one (eg. 1*1) to see if the problem comes from here ?

#5 (ok)   Members   -  Reputation: 104

Like
0Likes
Like

Posted 27 May 2012 - 10:25 AM

Ooops sorry, you are doing quadtree culling yet so there might be unnecessary traversal.
My best guess is to debug your quadtree scheme and make sure no unnecessary trees are processed before or after.

#6 jmakitalo   Members   -  Reputation: 551

Like
0Likes
Like

Posted 27 May 2012 - 10:57 AM

My best guess is to debug your quadtree scheme and make sure no unnecessary trees are processed before or after.


Actually the quadtree frustum culling is so simple, that it only considers a 2D view frustum, which is independent of the camera pitch. I know that this does not work correctly for all cases, but it is fast and works in most cases. The point being now that it does not change the amount of trees processed, if the camera faces the ground. I have also tried low-resolution texture for the leaves with no improvement.

#7 (ok)   Members   -  Reputation: 104

Like
0Likes
Like

Posted 27 May 2012 - 11:10 AM

Ok so it is not the drawing...

I don't know how you have implemented your quadtree but maybe you could test if some trees located outside the desired area get processed ?

For example, you could try to setup a breakpoint if a candidate tree is outside the desired area just to check. You could also double check if your whole loop or part of it does not get processed several time instead of only once. Check if one of your visible trees does not get considered more than once.

It happens to me all the time, fast(your

trunk/branches

) does unfortunately not always mean neat.

Good luck.

#8 belfegor   Crossbones+   -  Reputation: 2611

Like
0Likes
Like

Posted 27 May 2012 - 11:16 AM

Does it help to sort leaves front to back?

#9 clb   Members   -  Reputation: 1781

Like
0Likes
Like

Posted 27 May 2012 - 11:29 AM

The general steps go roughly like this:

First thing to answer is if the slowdown caused by being CPU-bound or GPU-bound? (or, in very rare cases, bad sequential structure causing both CPU and GPU -bound?)
Use tools like AMD CodeAnalyst, Intel vTune, nVidia Parallel NSight, nVidia PerfHUD, Intel GPA, custom intrusive profiling blocks (Game Programming Gems 1), to profile the CPU and GPU workloads.


If you are CPU bound:
- Identify where the additional CPU time is spent. If it is directly in your code, optimize it.

- If the time is spent inside the graphics driver dll, identify how to reduce draw call count by batching. Group vertex buffers together and atlas textures to achieve this.

If you are GPU bound:
- Look at the GPU instruction call trace to identify if you can optimize redundant state sets.
- Identify whether you are vertex shader, pixel shader, texture load, or framebuffer raster op bound. Optimize the appropriate stages or reduce the workload.

I suspect you are somehow CPU bound, since you say that adding rendering workload that is outside the view frustum still causes slowdown.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#10 jmakitalo   Members   -  Reputation: 551

Like
0Likes
Like

Posted 27 May 2012 - 12:32 PM

As disabling the leaves does not affect quadtree and increases performance a lot, I dont believe that the quadtree is the problem. I could still double check.

I have not tried to sort the leaves by distance, but I did try sorting the tree objects, after the frustum culling. Did not help either, though I'm not sure how efficient my sorting procedure was (used the stl algorithm to sort the vector of indices to visible trees).

I have never used any actual performance measurement tools, though maybe they would prove useful. Probably takes some time to learn how to use them. Are there any versions for Linux?

#11 (ok)   Members   -  Reputation: 104

Like
0Likes
Like

Posted 27 May 2012 - 02:03 PM

Clb is right, you must measure as much as you can, and NO, you don't really need training for that.
You will be glad to simply check out methods with the highest profiles(the ones on top of the list) and possibly concentrate on/optimize them.

You can also more directly evaluate the time spent between statements with very simple functions.

Just google for c++ profiler/profiling tools.

Bench-marking your code is crucial to evaluate yet if you have enough budget for a specific job before writing it.
It also helps a lot to get extra clues about what's possibly wrong.

#12 jmakitalo   Members   -  Reputation: 551

Like
0Likes
Like

Posted 27 May 2012 - 02:29 PM

I have used gnu profiler before (I use g++), but usually the data seems useless, as it tends to list functions that just dont seem to be related to the problem at hand. I do realize that it takes some skill to be able to dig out the meaningful data.

I'm not sure if this is related, but I have also found that if I have, say, just one mesh that represents e.g. a hedge, consisting of maybe twenty crossing planes with alpha tested semi-transparent texture with opaque and transparent parts alternating at small scale, then a closeup view will make the rendering to crawl. Why would this happen? I mean, I understand that a large alpha blended polygon would have a large impact, but why alpha tested?

#13 (ok)   Members   -  Reputation: 104

Like
0Likes
Like

Posted 27 May 2012 - 03:03 PM

but usually the data seems useless

Sometimes it is hard to figure out the cause of your stalls from the profiler only but if a module function comes on top at your app's run-time, at least you know your app is intensively using it. You must also use timing functions directly in your code to locate bottlenecks.

For your second question, it often depend on your transparency approach. If you use raw blending, you need a clever primitive ordering to get the most of it but sometimes screen door is more appropriate (no back to front order needed).

Check those :
http://www.gamedev.net/topic/599103-issues-with-blending-transparency/

http://www.opengl.org/archives/resources/faq/technical/transparency.htm

#14 jmakitalo   Members   -  Reputation: 551

Like
0Likes
Like

Posted 28 May 2012 - 11:03 AM

I think I made some progress here. I reduced the rendering code temporarily to a simple form, where I was able to see that textures and vertex buffers are only bound once per whole tree system and nothing is clearly done in excess. Still no improvement. Then I realized that I was still drawing the trees to shadowmaps. This explains why looking at the ground did not affect framerate. I then disabled this feature and then the framerate increased when looking away from the trees. I placed three times more trees, and facing the ground still yielded a good framerate. So I guess that one could think that it is fillrate related. I then tried again sorting the trees, checking that sorting is actually happening, but this did not improve the situation. It is strange that even if I went really close to a tree trunk so that it occluded the whole screen area, there was no improvement. I would have guessed that sorting from front to back would have shown improvement in this case at least.

#15 jmakitalo   Members   -  Reputation: 551

Like
0Likes
Like

Posted 28 May 2012 - 01:29 PM

Do you suppose it would help significantly, if the leaves were rendered to an off screen target, which would be of low res? There was an article about this sort of thing in gpu gems 3 for particle effects. The problem would be downsampling and obtaining the zbuffer. Does arma2 do something like this? I remember that the trees looked a bit blurry.

#16 hupsilardee   Members   -  Reputation: 486

Like
0Likes
Like

Posted 28 May 2012 - 01:51 PM

You could try a depth-only z prepass without texture reads or color writes. Or you could try occlusion culling which might be very effective for culling away large numbers of distant trees

#17 jmakitalo   Members   -  Reputation: 551

Like
0Likes
Like

Posted 28 May 2012 - 02:54 PM

I did a quick tryout with z-pretest. I need to enable alpha writing for the leaves for this. I got some strange flickering artifacts, so meybe I need to use some kind of depth offset. Anyway, there was no improvement. Rendering the leaves without any shaders nor texturing did have a huge impact, though. So the shaders must be somewhat demanding, but z pretest wont seem to save it, or then I'm just doing it wrong.

#18 Tordin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 29 May 2012 - 04:27 AM

Try to use alpha to coverage, it´s realy fast and could get you a extra fps.
"There will be major features. none to be thought of yet"

#19 hupsilardee   Members   -  Reputation: 486

Like
0Likes
Like

Posted 29 May 2012 - 04:37 AM

Only other thing I can think of is impostors, which can look very good if done right - Oblivion (SpeedTree) faded the tree model out for a flat billboard as close as 50m from the camera, and the treescapes always looked great (imo)

#20 jmakitalo   Members   -  Reputation: 551

Like
0Likes
Like

Posted 29 May 2012 - 08:20 AM

Try to use alpha to coverage, it´s realy fast and could get you a extra fps.


What do you mean by using alpha? I use alpha testing, which I believe is cheap, but it still yields low framerates.

I also just realized that there is the "discard" function in fragment shaders. I think that this improved the situation slightly, but it's not doing wonders.

Only other thing I can think of is impostors, which can look very good if done right - Oblivion (SpeedTree) faded the tree model out for a flat billboard as close as 50m from the camera, and the treescapes always looked great (imo)


Perhaps this is one way, but I bet it is a fair amount of work to make it fast and still look good. Probably need some fancy texture atlasing and what not.
Occlusion query might help to reduce the overdraw, but I'm suspicous as the meshes are quite low-poly and there are many of them.

Ok so, it is established that the low fps is obtained when the trees are in the view frustum and fill most of the screen. Can one deduce from this, that
the program is limited by fillrate or can there be some more complicated caching between vertex and pixel processing, so that the number of polygons
could still matter?

How do games generally cope with the problem, that if you have e.g. a hedge or a bush consisting of a few hundred overlapping alpha tested polygons and the player goes very
close to this bush? This really seems to crush the framerate even without alpha blending. Is the only way to improve the framerate just to do a precise depth sort from front to back?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS