Using loads of particle effects without fragmenting your memory
If you're making a game that uses a lot of short-lived particle effects, you run the risk of fragmenting your available memory. Shooters and action games often run into this problem, and developers need to be careful not to let these small visual effects hinder their game's performance.
As Radical Entertainment's senior rendering coder Keith O'Conor puts it, "Fragmentation happens when many small pieces of memory are allocated and freed in essentially random order, leading to a 'Swiss cheese' effect that limits the amount of contiguous free memory."
Radical's Prototype 2
was especially susceptible to this problem, as the open world action game often needed to have numerous explosions, sparks, and blood effects going off at any one time. Thus, O'Conor and the rest of the team had to come up with some strategies to make sure the game could support those effects and regulate its precious memory.
"Whenever possible, we use static segmented memory pools (allocated at start-up) to avoid both fragmentation and the cost of dynamic allocations," O'Conor said in the latest Gamasutra feature
. "The segments are sized to match the structures most commonly used during particle system allocations. Only once these pools are full is it necessary to perform dynamic allocations, which can happen during particularly heavy combat moments or other situations where many particle effects are being played at once.
"Our effects system makes multiple memory allocations when a single particle system is being created. If any of these fail (because of fragmentation, or because the heap is just full), it means the effect cannot be created. Instead of half-creating the effect and trying to free any allocations already made (possibly fragmenting the heap further), we perform a single large allocation out of the effects heap.
"If this succeeds, we go ahead and use that memory for all the allocations. If it fails, we don't even attempt to initialize the effect, and it simply doesn't get played. This is obviously undesirable from the player's point of view, since an exploding car looks really strange when no explosion effect is played, so this is a last resort. Instead, we try to ensure that the heap never gets full or excessively fragmented in the first place."
To avoid that fragmentation, the team partitioned its various effects into "stores" based on their various types. The team had stores for explosions, ambient effects, bullet effects, and more. By dividing effects into categories, the team could more easily control how many of each effect type could go off at once.
With these and other rendering techniques, the team was able to streamline Prototype 2
's memory allocation and ensure that the game could pull off its explosive, action-heavy visuals. To learn more about how Radical refined its effects technology for Prototype 2
, be sure to check out O'Conor's full feature, which is now live on Gamasutra