Terry A. King

Just had to test the gfx power with some simple sprites. Printing 48x48 sprites, ideal size for tile map based games it cranked out over 10,000 sprites at just over 60fps Obviously size, scaling, rotation all has a significant impact on speed but nice to know !

I noticed that when the framerate was fixed it didn't matter how many sprites I displayed it always ran at 60fps. How does it do that Does the gfx card abort what its doing when the next frame is ready



Re: XNA Game Studio Express XBOX >10k sprites per frame !

Shawn Hargreaves - MSFT

You should find that scaling and rotation don't effect performance at all on Xbox. We're using some crazy GPU tricks to do all that computation in the vertex shader, and the Xbox GPU isn't exactly stressed out by drawing 2D sprites, so effectively you get all that math "for free".

When you fix the framerate it will always call your update method 60 times a second, and then call draw as many times as it can fit in between updates. As long as your total update+draw time is less than 1/60 of a second, this gives a lovely smooth framerate. If the total goes over 1/60, it will start having to skip some draws, so the display won't be so smooth, but the update will still happen at the same rate so your gameplay doesn't slow down.





Re: XNA Game Studio Express XBOX >10k sprites per frame !

Bill Reiss

Terry,

How many sprites were you able to draw on the PC with the same program I tried something like this once with XNA on Windows and it seemed to bog down at around 5000 in my tests.

Bill






Re: XNA Game Studio Express XBOX >10k sprites per frame !

Terry A. King

Bill Reiss wrote:
How many sprites were you able to draw on the PC with the same program

Hi Bill,

I'd love to say the performance was lower on the PC, however, I've got a 7950GX2 which ran the same code at around 200fps (60fps on 360) ! It was quite a tight loop to get >10k sprites :

batch.Begin();
for (int i = 0; i < 10000; i++)
{
batch.Draw(tex,
new Rectangle(0,0, 48, 48),
new Rectangle(0, 0, 24, 24),
Color.Red,
0f,
new Vector2(0, 0),
SpriteEffects.None,
0f);
}
batch.End();

The overhead of creating instances of Rectangles and Vector2's wasted a few frames. However, change the parameters to randoms and it drops dramatically to around 2500. A couple of thousand sprites is probably more practical in most games, there goes my particle extravaganza Seems the speed limit is on the cpu rather than gpu, no doubt someone has some tricks up their sleeve ...

Terry





Re: XNA Game Studio Express XBOX >10k sprites per frame !

Shawn Hargreaves - MSFT

You may be able to micro-optimize that by inlining the Rectangle and Vector2 construction. Instead of new Rectangle(0, 0, 48, 48), use:

Rectangle r = new Rectangle();
r.Width = 48;
r.Height = 48;

The CLR handles default constructors for value types in a special way with a highly optimized set-everything-to-zero, rather than actually bothering to call a constructor method, so this rewriting, although making less readable code, can give better performance especially in tight inner loops.

Going beyond that, the way to get really silly numbers of particles with good performance is using point sprites and a custom vertex shader. You can create an array sized the maximum number of particles you ever want to draw, and a vertex structure containing an initial position, time, and direction. When you want to create a new particle, just add it into the next free position in this array, and cycle through the array in a loop retiring old particles after a fixed amount of time (so the block of active particles are always consecutive). Then in the vertex shader, you can upload the current time as a shader constant, and use shader code to calculate how old the particle must be, and from that work out where it should be positioned (original position + direction * age, perhaps plus a gravitational effect if you want) and to make it fade out over time. Then draw all the particles in a single go with one call to DrawUserPrimitives. Nice thing about this approach is you only have to touch the array once when you create each particle, as all the per-frame animation happens on the GPU. That can give really silly number of particles at good framerates, and it even works in quite low end PC cards, only needs shader 1.1 to do the vertex shader animation.





Re: XNA Game Studio Express XBOX >10k sprites per frame !

Micky D

Terry, you should probably setup all your shapes before the rendering loop. Move your loop into an Init() or equivalent and setup random positions there.

That way your Draw method simply loops and draws the rectangles rather than creating them and positioning them using rand. Rand comes with a cpu cost too.

An Update() method can move them around.

I don't think you are really drawing sprites anyway - aren't they just scan-filled rectangles






Re: XNA Game Studio Express XBOX >10k sprites per frame !

Terry A. King

Micky D wrote:
Terry, you should probably setup all your shapes before the rendering loop. Move your loop into an Init() or equivalent and setup random positions there.

That way your Draw method simply loops and draws the rectangles rather than creating them and positioning them using rand. Rand comes with a cpu cost too.

Cheers Micky , I wouldn't ever do this in a game, it was merely to make the test more realistic in terms of expected performance. Like you say, all the sprites would normally be set up in the Update() and Draw() would have the minimal amount needed in it.

Micky D wrote:
I don't think you are really drawing sprites anyway - aren't they just scan-filled rectangles

No, they're all alphablended sprites