# Is it possible to use the GPU on a mobile device to accelerate a particle physics engine?

I am interested in answers for any mobile device but I am mainly considering the iPhone and devices that run Java.

I am developing a game that relies heavily on a particle physics engine for core gameplay (not just animation effects) and have been developing/testing on a PC up to now with the intention of porting it to iPhone and/or Android. The problem is that the particle physics engine (which I implemented myself) is still way too slow. I can barely reach the target frame rate on a netbook and I am guessing that most phones will be worse (maybe not the iPhone but that will be expensive to port so I want to be confident that it will be worthwhile.)

I've optimized the software implementation as far as I can and now I am wondering if it is possible to accelerate it in hardware. The GPU looks like the obvious thing to try first.

Has anyone researched this or do you know of an existing library that implements this?

• You say you've optimized the software solution as much as possible, but are you making use of the Neon vector processing unit on the Arm7 devices? Look for the WWDC2010 session on the Accelerate framework.
– U62
Aug 10 '10 at 14:55

It is certainly possible to run a particle simulation on the GPU using vertex shaders, but you may be very limited in the kinds of behaviors that you can simulate, depending on the capabilities of the GPU.

In order to run the entire simulation on the GPU you cannot make use of the results of previous frames. Which means that the current state for any particle must be computable from a combination of its initial state and any global variables (such as the time). The requirements of a physics engine would usually rule out this approach.

One way you can get around this is to split the computation between the CPU and GPU. You could have the CPU update the particle positions from the physics simulation and use the GPU to calculate the vertex positions (do the billboarding math), color, etc.

More advanced GPUs will allow a vertex shader to write into a vertex buffer, effectively closing the loop and allowing the results of the previous frame to become the input for the next frame. This opens up far more possibilities but is probably out of reach for most mobile GPUs (I don't remember what version of OGL you would require).

In short, yes you can run particle simulations on a GPU, but the constraints imposed by the GPU capabilities might rule it out for your purposes.

If you mean drawing, you can certainly use OpenGL ES on both Android and iPhone, and OpenGL on PC (and if you also opt for DirectX you might get a bit better performance in Windows netbooks). It is just as functional for 2D acceleration as it is for 3D.

But I presume you mean computation. While I cannot give any experience in using GPU for computation, here are a few things you might not have thought of when optimizing your application:

• I assume you used a profiler, not just optimized by hand
• Are you using fast math functions for sin/cos/tan/sqrt? For example, using 1 / fast inverse square root instead of the slow sqrt function.
• Whenever possible you should use ints/shorts in place of floats, and really avoid doubles. From what I've learned, mobile processors typically have much slower floating point engines (FPUs). I don't know if that's true so much today.

Sorry if you already did the above; just trying to think of anything that might help.

• Yes I do mean for computation, and yes I have profiled it and I am using a fast implementation of atan2 (the only one of those that the profiler highlighted.) I will experiment with fixed point arithmetic. Aug 10 '10 at 14:06

Any device supporting Open GL ES 2.0 should support programmable shaders. Iphone 3GS and above supports Open GL ES 2.0.

Android phones only have Open GL ES 1.1 support as far as I know.

With Open GL ES 1.1 you could use Vertex Buffer Objects with point sprites as a substitute to the proper vertex and fragment shaders you can make in Open GL ES 2.

The PowerVR chip has some low level shading language for Open GL ES 1.1 if I recall correctly.

Check out the examples bundled with the PowerVR SDK. The 1.1 SDK has some particle effects sample. You should also read up on the differences in the API between 1.1 and 2.0.

EDIT: Gah, I realized I didn't answer the question at all. Ok, so here are some other tips not mentioned here: * Align the data you are doing computations on in such a way that you avoid cache misses. This probably depends a bit on your target architecture, and you should read about it for each platform you're targeting. There are some general guidelines though on how to make stuff quicker.

That is all I have time to write right now...

• No I mean for computation, not for visible particle effects Aug 10 '10 at 14:04
• Many Android Phones have OpenGL ES 2.x support. You can use it with NDK or Android SDK. I have created a thread to list opengl 2.x extensions by devices: stackoverflow.com/questions/3881197/… Oct 24 '10 at 15:49

For particle computations with OpenGL ES 2.x (iPhone 3GS, 4 or many Android Phones), you can use the vertex shader but the equation must be very simple and depend on few variables.

The main idea is to send parameters with uniform or attributes:

• time elapsed: uniform
• initial position of particle: attribute
• initial velocity of particle: attribute
• color: attribute

You can use a static VBO to send these data one times. If you need advanced physics, this is probably not the solution and you must use the CPU. With Neon instructions, you can have a great speedup. Warning the Tegra2 (available on few android tablets) doesn't support this instructions set.