Depth-sorting particles..

Discuss programming topics for the various GPL'd game engine sources.
Post Reply
Downsider
Posts: 621
Joined: Tue Sep 16, 2008 1:35 am

Depth-sorting particles..

Post by Downsider »

I don't want all my particles being additively blended. And when I have those being additively blended with those not being it looks like complete ass because of lack of depth sorting.

I'm assuming there's probably a huge performance impact when it comes to sorting by depth; is there?
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Post by Spike »

depends on your sorting algorithm

if you disable depth writing for it, you can just draw them all with aproximate depth without really caring about actual depth.
this is what fte does - just sorts them into buckets based on distance then goes through the buckets drawing each group as they come.
mh
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Post by mh »

DirectQ does rough depth-sorting of particles by initially grouping them into batches based on the function that spawned them, then depth-sorting those batches. The particle depth-sort goes through the same sorting routine as anything else with alpha, so you get a more-or-less accurate blend not only between particles and other particles, but also between particles and everything else. You'll obviously need to re-engineer your particle system a little for this, of course.
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
Sajt
Posts: 1215
Joined: Sat Oct 16, 2004 3:39 am

Post by Sajt »

mh wrote:DirectQ does rough depth-sorting of particles by initially grouping them into batches based on the function that spawned them, then depth-sorting those batches. The particle depth-sort goes through the same sorting routine as anything else with alpha, so you get a more-or-less accurate blend not only between particles and other particles, but also between particles and everything else. You'll obviously need to re-engineer your particle system a little for this, of course.
What about smoke trails?
F. A. Špork, an enlightened nobleman and a great patron of art, had a stately Baroque spa complex built on the banks of the River Labe.
mh
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Post by mh »

Sajt wrote:
mh wrote:DirectQ does rough depth-sorting of particles by initially grouping them into batches based on the function that spawned them, then depth-sorting those batches. The particle depth-sort goes through the same sorting routine as anything else with alpha, so you get a more-or-less accurate blend not only between particles and other particles, but also between particles and everything else. You'll obviously need to re-engineer your particle system a little for this, of course.
What about smoke trails?
Yeah, it does those too. They all come from R_RocketTrail but it groups by type within that. A trail in R_RocketTrail is actually a quite short thing, so reasonably decent sorting is obtainable.

Typically there might be just over 100 groups to be sorted in a typical trail/explosion combination, but that's not too bad. There's no need to sqrt the distances for doing the comparison op, and if you justuse qsort rather than trying anything fancy it all works out fine.
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
Downsider
Posts: 621
Joined: Tue Sep 16, 2008 1:35 am

Post by Downsider »

From what I'm getting here, I'll have to do the following:

When creating particles, put them in groups.

Sort groups by distance.

In those groups, sort each particle in the group by distance as well.

Go down the sorted list of groups..

Render each particle in the group based upon the previously sorted particles.

Works? =D
mh
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Post by mh »

Nah, I don't bother with the sorting each particle in the group by distance part. It's rough but it works. The setup goes something like this:

Code: Select all

void Particle_SpawnFunc (vec3_t org, int count, etc)
{
  particle_type_t *pt = R_NewParticleType ();
  VectoryCopy (org, pt->origin);

  for (i = 0; i < count; i++)
  {
    particle_t *p = R_NewParticle (pt);
  }
}
Then depth-sort on pt->origin.

Have a look at my code, it should be clearer there.
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Post by Spike »

if the particles are close enough and have compatible blend modes, they don't really need to be sorted if depth writing is off.
its only really when the blend mode is destructive that they need sorting.
If the only particles on screen are additive rocket-explosion particles, then sorting isn't needed.

Tbh though, whatever lets you batch particles best will give better speedup than would be lost from sorting them.
Downsider
Posts: 621
Joined: Tue Sep 16, 2008 1:35 am

Post by Downsider »

The problem resides in my own particle effects, where it's several smokepuffs being emitted in a radial pattern around an additively blended yellowish explosion particles in the center.

The smokepuffs that end up behind it show as if they were in front of it.

I think I'd have to sort by depth for every particle in that situation, wouldn't you agree?
Post Reply