Additive Blending with Images and Optimal Sprite Drawing Speeds

For future reference, and for my particle engine, I need to know how to take multiple images and make their colors additively blend for something like fire particles and such. Take a look at StumpyStrusts particle engine:: http://www.java-gaming.org/topics/particle-editor-tool/28535/view.html :: to get a feeling of what I am aiming for. Also, what is a good method for drawing many many sprites at once? I can get up to 50,000 and then I drop to 20 fps without re sizing the image with g.drawImage(pixel, x, y, WIDTH, HEIGHT);. However, when I do resize it, my fps is absolutley horrendous with only 5000 sprites. Any suggestions? Thanks, -cMp

I’d strongly recommend that you move on to OpenGL, either through LWJGL or LibGDX. Although it’s possible to get good performance with Java2D, it’s inconsistent and has other problems. With OpenGL, you’ll get access to your GPU in a much more efficient and powerful way. GPUs have hardware for texture filtering which means that resizing does not cost anything except for the increase in the number of pixels your GPU has to fill. Blending is also extremely fast.

Let’s say your particles are 100x100 pixels. That’s 10 000 pixels for a single particle. 50 000 of them is 500 000 000 pixels per frame. At 60 FPS, that’s 30 000 000 000 pixels per second, or 30Gpixel/sec. On most hardware you’re going to get far below 60 FPS, but guess what? A GTX 680 actually has a (theoretical) fillrate of 32.2GPixel/sec, so it’s not impossible. When it comes to texture filtering speed, a GTX 680 has an astounding 128.8GTexels/sec, so we’re perfectly fine there.

Of course, reducing the number of particles, the size of each particle or the FPS target will reduce the number of pixels per second your GPU has to fill drastically. This was just an example. ^^

The reason why GPUs are so much faster at rendering is because they use lots of stream processing cores. Note that the following numbers are extremely simplified, there’s a lot more going on that needs to be taken into consideration to accurately compare performance like this.

  • A quad-core CPU at 3GHz can process 3 000 000 000 * 4 instructions per second = 12 000 000 000 instructions per second.
  • A GTX 680 has 2 688 cores running at around 1GHz —> 2 688 * 1 000 000 000 = 2 688 000 000 000 instructions per second, which is over 200x as fast.

Great advice I was already thinking of this too… I would prefer to learn LibGDX, where do you think I should start? I have searched around the internet alot over the past week but havent found a whole lot. Thanks again for the information, I never knew any of that stuff!

You cannot do additive blending with java2d and remain accelerated. I use opengl for my particle engine which is how I can do additive blending. Particles are basically sprites. So if you want lots of them you need a fast sprite batcher. Libgdx has a good one. You should be able to get 50k no problem which actually is more then you should ever need. Both me and davedes posted a tutorial on how to make one if you want to get into opengl.

The biggest bottle neck you will most likely hit in a 2D game is what theagentd talked about which is texture fill. Too many pixels put simply. The more particles you have the smaller they have to be in order to not hit that bottle neck. That is why creating good particle effects can be hard. You don’t want too many particles but you also don’t want too large of images being used.

Also, my particle engine works for libgdx and they have a particle engine which is probably better but I don’t think it can do animated particles. So unless you really want to, don’t remake something that has already been done for you.

On a java2d note, I kinda figured out a hack to keep images accelerated whilst doing additive blending but it only works for non opaque pixels which makes it useless.