Particle systems and performance

Hi,

I have been taking a look at Monogame and have noticed it doesn’t have any built-in particle system. It’s ok, a low level framework doesn’t have the need to bring one to the table.

Anyway, I have found some implementations around and I’m a bit worried about performance. Particle systems are little monsters that iterate through a bunch of items to bring nice effects to life. When iterating through such a big amount of objects, contiguous arrays is the way to go. Mainly, as you know because we want no cache misses if possible (at least just as little as we could).

C# can create this contiguos kind of data structures (if I recall it correctly). An Array and even a List<>(that is based on Array) are a contiguos chunk of cache freendly memory. The problem is that it is a contiguos array of refernces not objects. So, based on my experience this is not what I would want to build a system like this.

Is there any possibility to build this using C++, minimizing the unwanted Managed<->Unmanaged traffic? Becasue if I have a lighting fast C++ particle system but can´t talk with the Rendering system natively it will be a pain :D.

As an example, unity3d has a built-in particle system but people have implemented their own particle systems and added to them the asset stores. My tests shown that they are less performant than the built-in ones (considerably).

P.D.: I don’t want to be “that” guy. Just, I need to be sure about monogame prior to step out of unity.

It depends… if your array is an array of a struct type then it will be contiguous in memory. This is generally the safest bet when working on a particle system if you want performance.

Still in C# memory allocations are generally done sequentially when possible… meaning most of the time if you allocate two objects they will be one after the other in memory. So if early during game startup you allocate an array of reference types then allocate each reference object and assign it back to the array… things should be contiguous in memory. But of course… working with value types is always better in that regard.

Sure C# and C++ can easily communicate… if you stick to primitive value types and simple C-like APIs things can be pretty fast.

1 Like

After reading some posts on stack overflow and some detailed .net documentation it seems your were totally right.

Structs is the way to go here and it seems you can allocate an array of them just at once. Pretty neat.

Cheers.

1 Like

Consider GPU particles… of course you can´t use them everywhere but as far as performance goes nothing will beat them, only shame is we dont have access to Geometry shader in monogame.

Something to suggest here: MonoGame Feature Wishlist