OpenSceneGraph, Python and Performance under Linux

Since I made the decision to go with OpenSceneGraph (see scenegraphs) I’ve spent quite a bit of time tinkering with the python openscenegraph bindings. This involved learning how to use boost python (I used cargo cult techniques) and brushing up on C++, (which I understand better now, but still don’t like much). I was able to get the major parts of the wrappers working, with render to texture via fbo’s and basic shaders working, and have a basic understanding of how to do callbacks. I’ve also ported some of the older demo files to use the new methods of OSG 1.0.

In particular I’ve done some tinkering with osgprerender.py, which exposes a number of render to texture techniques, and uses a callback to do an animated wave effect. Since this is similar to some of the things that I want to do I thought that this would be a good test case to do some performance evaluations. The downside of working with python is its numerical performance. I decided to do some tests to see how well pyosg could perform. For examples where little python numerical computation was being done, the python versions of the demos came very close in performance to the C++ versions. In osgprerender.py the difference in performance came almost soley from the update callback to calculate the mesh. I looked around for what simple types of optimisations could be performed.

| def apply(self, drawtype, begin): if drawtype == osg.Drawable.VERTICES: TwoPI = 2.0 * osg.PI phase = -self._time/self._period for itr in begin: dv = itr - self._origin local = osg.Vec3(dv*self._xAxis,dv*self._yAxis,dv*self._zAxis) local[2] = local._x*self._amplitude * math.sin(TwoPI*(phase+local._x*self._xphase)) # Don't assign to itr, since this will create a new object itr.set(self._origin + self._xAxis*local._x+ self._yAxis*local._y+ self._zAxis*local._z) | osgprerender.py 1024x768 | | | | architecture | x86_64 | i386 | | C++ | 720-800 | 720-800 | | Python | 350 | 280 | | Python + psyco | | 300+ | | Python + pyrex | 375 | 300- |

My test machine uses an AMD 64 3500+ CPU with a GeForce 6800GT graphics card running Ubuntu Linux, and the NVIDIA 8178 X windows driver. I’d had a suspiscion that the 64bit version of Linux (x86_64) might actually be slower that the 32bit (i386) version. It was gratifying to see that OpenGL performance was identical and that Python actually seemed to be faster. I tried using pysco a kind of JIT compiler for i386 Python, but the performance improvement was meagre. Taking out the callback class and turning it into a pyrex (an extension to python that uses C data types) module with some very minor optimisations made some improvement.
One of the problems with the osgprerender callback I was trying to optimise is that it is quite heavily embeded with the functions that the OSG C++ library provides. One solution would be to write the callback class in C++ and wrap it with boost or swig or to expose the structures with pyrex. This all seems like quite a lot of work for using something that is supposed to make your life easier.

For callbacks I write myself this will probably not be such a problem as the OSG API does provide a way to just upload information from and to a vertex array, in which case Pyrex should be able to do the job quite easily.

Along the way of this journey I also looked at Python to C++ converters (in particular shedskin) which looks like something may be available that works well enough in the next 12-18 months. I also looked at systems to generate wrapping code automatically and it looks like pyplusplus is worthwhile keeping an eye on.