Hi all,
In my opinion and limited experience, it seems that Java programmers make weird and wonderful algorithms and ideas to increase the performance out of OpenGL, while C/C++ programmers dont need to do that just yet; but when simple java benchmarks become as fast as C/C++ applications, Java’s scenegraphs and such will have all these cool new ways to do stuff and hence are going to be faster than the equivilant C/C++ applications.
So feel free to post here any cool ideas regarding how you got some performance out of OpenGL that you think is uber cool. My contribution is state sorting:
From what ive read (very little), it seems that state changes flag the server to do a complete validation of the server states and the client states so that they both conform, and OpenGL seems to take this time to also do some memory juggling and that can take a while. So, a good way to avoid this is minimise the state changes that do occur. The obvious solution is to have some sort of bucket, clear the bucket everyframe, use some sort of cool sorting algorithm to sort your objects, then loop through the bucket and render. All is well and the kittens are drinking milk…
However, all this sorting every frame can be a little computationaly expensive especially when you have alot of objects, and from what ive seen, the fastest sort is the Radix sort which has a performance of O(n). My Contribution has an init sorting time of O (n log m) whereby n is the number of objects and m is the number of states, but during run time (i.e. frame to frame), there is no overhead. This does come with a limitation, i’ll discuss that later.
As an example, lets say you have four states: Texture State, Lighting State, Pixel/Vertex State, and Depth state. They vary in their performance cost, but lets say for simplicity sake, that the above list is ordered from highest to lowest expense (nothing is better than actual costs, so do your own benchmarks and see which is more expensive)
Now during runtime you init a sort of tree, each node reflects a state change (i.e. an enable and a disable of that state) and the nodes contain a list of objects they hold (Look At the picture). You create your material with its fancy state changes and such, then you recurse the tree until you find that no more nodes below the current one support the material (a simple “isStateAvailable(Material)” method will do in each node that returns a boolean). In the diagram, if your object has Texture state, and lighting, the object will be stored in lighting node (on the left); As you can see, the tree is ordered from top-bottom and left-right. Now all of this is during init time, once you have found the tree, store the node of the render tree into the material.
When rendering, do “material.getStateNode().add(material);” for every object, then recurse the entire tree and draw the children at every node. No sorting during run time.
Now the only limitation of this is texture ID’s, glBindTexture can also flag a validation and the expense is the equivilant of state switch, the only way left to do is do a proper sort on the children of the Texture State node. Now the diagram is just a little sub tree, next to the texture you would also have Lighting with its Depth and Shader children as nodes describe a glEnable function.
Whats your clever cool idea/algorithm ?
DP