This is an exciting time to work with real-time graphics, as the capabilities of GPUs keep increasing in raw performance and functionality, and also because the “GL family” of programming API’s (OpenGL, GLSL, OpenGL ES, JOGL) has been evolving rapidly during the past years in order to give access to new hardware features and devices. One area I have been interested in for some time is General Processing on GPUs (GPGPU). GPUs can allow for major speed-ups in computational problems that are suitable for data parallelization. Originally, it was possible to carry out GPGPU calculations (such as the simulation of a particle system) with the graphics API and writing the computation “kernels” using Cg or GLSL shaders. The major disadvantage of this approach was the need to cast a general computation algorithm in graphics terminology, i.e.: an array of particle positions becoming a texture, the output of a calculation being stored in a color variable, and so on. Despite these complications, many initial GPGPU projects were carried out in this way. I also implemented some simple particle systems for non-photorealistic rendering using OpenGL and GLSL shaders. Today, there are several API’s specifically designed to program the GPUs as general parallel processors, and among the most mature and widely used ones I would mention CUDA and OpenCL. I recently chose to learn OpenCL as it is a hardware-agnostic API aimed to support GPUs from different vendors as well as CPUs and other compute devices. Also, there are already several Java-bindings for OpenCL which are in an advanced stage of development (Jogamp’s JOCL, JavaCL, and JOCL.org). This opened-up the possibility of combining OpenCL and OpenGL in Processing in order to simulate and render large particle systems with full GPU-acceleration. Continue reading for the details (and also for some video renderings made possible by Syphon).
Taking the source code of the traer.physics library by Jeffrey Traer Bernstein as the starting point, I wrote a first version of a new Processing library called CLPhysics that implements all the computationally-expensive steps in a physics simulation (basically, force evaluation and time integration) as OpenCL kernels. CLPhysics currently includes the modified Euler and Runge-Kutta integrators, as well as gravitational, spring, and drag forces. Although the implementation is very crude at this point and the OpenCL kernels are very far from being optimal, the library contains two working examples and it is fully available with its source code at this Google Code repository.
The first example is a N-body simulation where 5,000 particles move under the effect of the mutual gravitational attraction, calculated between all the pairs at every simulation step. The particles are drawn as simple point sprites using the OPENGL2 rendering engine, and the resulting video is captured in real-time with the Java interface of the Syphon framework for OSX (so my acknowledgments to Tom Butterworth and Anton Marini here). This simulation run was done on a NVidia Geforce 320M, which is a fairly basic GPU for general computing. The framerate was nonetheless quite acceptable at ~35 fps.
The second example is a simplified re-implementation of the RandomArboretum sketch from traer.physics. Here, 2,500 particles are interconnected randomly with springs and kept apart by an all-pairs repulsive force. This ends-up in a force-directed network layout that reveals the central nodes of the graph quite clearly:
These results are just a sample of the ongoing work I’m currently doing related to the integration of OpenGL (and now OpenCL) into Processing. In fact, none of the examples above will work directly out of the box, as they require compiling Processing manually from the latest source code, and installing highly incomplete versions of the Syphon and CLPhysics libraries. However, they provide a glimpse of what is possible in real-time graphics and simulation by combining these new software and hardware technologies. The coming months should be quite interesting as more polished versions of all these libraries become available.