PDA

View Full Version : Any obvious improvements to my particle system?



Stuart McDonald
01-20-2011, 02:29 AM
Hi again,

I've written a basic particle system. I don't need a GPU based one, but I'd like to check that there aren't some obvious improvements to what I've implemented.

Each frame I fill an array with a quad for each particle i.e. 4 vertices which each have; Pos (3 floats), Tex Coords (2 floats) and Color (4 floats). I then call glBufferSubData to put this into an existing GL_WRITE_ONLY VBO which I have created to be big enough to hold the max number of particles

I then render the VBO of particles with glDrawArrays GL_QUADS.

Each particle and be a different size and colour and graphics come from a single texture sprite sheet of particles.

Any improvements I could make?

Thanks,

Stuart.

carsten neumann
01-20-2011, 04:45 PM
You can save per particle data (less data to upload) by using a geometry shader to generate the 4 particle vertices from a single position and color. Make position a vec4 and you can use the w coordinate as an index into a texture atlas (if you need that).

I don't know how much of a performance improvement this can yield, but since you avoid a bit of CPU computation (no need to figure out the 4 corners of the quad) and PCIe bandwith it may help.

Using two (or more) VBOs to avoid stalling the pipeline (ideally you want the previous render and the next upload to be parallel) may help too.

ZbuffeR
01-21-2011, 01:31 AM
I don't know how much of a performance improvement this can yield, but since you avoid a bit of CPU computation (no need to figure out the 4 corners of the quad) and PCIe bandwith it may help.
If you have no geom shader/or too slow, you can still use a vertex shader : send 4 vertices with same position, but each with a different screen-space direction to inflate it within the vertex shader.

ugluk
01-21-2011, 10:02 AM
Zbuffer and neumann, isn't it possible that the shader switch will take up more time than the particles he wants to render would take without the switch?

This is one of the dilemmas I am trying to resolve right now. When do you adapt an existing shader to render something and when you write a new shader to render something. Maybe you write a shader for everything at first and then coalesce them together at a later time as needed.

ZbuffeR
01-21-2011, 10:43 AM
Well to be honest, the original poster did not reveal how many particles are drawn.
Of course for such low-level tuning, benchmarks must be done to verify if any tangible gains exists. A shader based approach can free CPU work, so in the end "it depends".

Stuart McDonald
01-21-2011, 02:22 PM
Thanks for the replys. Want to keep running on older cards, so Geom Shader is out, but I'll try the vertex shader approach and do some benchmarking. I've only just got it to the "it seems to run" and haven't had time to do any real testing.