# spriteBatch.Draw slow?

This topic is 1780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to make a particle system, but I'm having problems when drawing. At around 30 000 particles the program slows down. So I tested by making a new empty program with only a for loop drawing a single pixel texture. I found out that when drawing around 60 000 times it slows down drastically from 60 to 30 FPS. Yet theres videos of particle systems that can handle up to a million particles (not my goal, but at least 100 000 would be nice). How is that possible? Do I have to use pixel shaders or something? And if so, isn't spriteBatch already a shader, and should I just write my own spriteBatch instead then?

##### Share on other sites

Do you only have a single SpriteBatch.Begin before and a single SpriteBatch.End after your drawing?

Having many separate spritebatches with the begin/end will slow your drawing down a lot.

##### Share on other sites

Yes, but I'm using additive blending. I didn't get any difference in FPS from other blend modes, or just plain spriteBatch.Begin(), though, so that doesn't seem to be the problem.

##### Share on other sites

First, ensure each sprite is using the same texture, and that you are using SpriteSortMode.Deferred (this is the default). That will max out your speed. But even then, 30,000 sprites is going to be taxing for the system. SpriteBatch is not designed for really large numbers (i.e. tens of thousands) of sprites.

Spritebatch is slow if you're drawing lots of items, because it needs to send that data to the GPU every frame. Each sprite is 96 bytes, and so 30,000 particles is nearly 3MB of data you're sending each frame.

One fix is to use GPU-based particles. There is a sample for this in the XNA education catalog on Microsoft's XNA website (it's a 3d implementation though, I'm not sure if they also have a 2d version). The sprites are drawn from a vertex buffer which is already on the GPU. As sprites are added or removed, only parts of the vertex buffer are modified accordingly. The end result is that there is minimal CPU->GPU communication. That is where you'll get your big speed up.

A drawback of GPU-based particles is that their position is calculated based off their initial position/velocity/timestamp and the current timestemp. So that makes it much more difficult to have particles that interact with your environment (e.g. if you have logic that makes your particles bounce off objects in your game, for instance).

##### Share on other sites

Thanks for the answer, I'll look into that then. If I want collision detection etc, I'll just have a seperate list for collidable particles and do them normally on the cpu, I guess :)

##### Share on other sites

Could you post your test update and draw methods? I've seen severe errors in implementation born out of bad examples, first clue would be if you are executing the End method for every particle, that would be VERY bad, there are other common errors though.

EDIT: Also no, SpriteBatch is not a shader, it does HAVE a shader, which you can have access to through the XNA resources, you'll find it under sprite batch default shader or something like that, what srpite batch is, is a batchnig and sorting abstaction algorithm, so you don't have to deal with those things yourself, which works wonderfully until you WANT to deal with those things yourself.

It takes away the tedious task of figuring out what is in front of what and if used correctly it optimizes instructions to the graphics card.

Edited by NEXUSKill

##### Share on other sites

I'm pretty certain that I'm drawing everything correctly. I've found out that I can draw aound 100 000 sprites on my test project before it slows down, though. There must have been some slowdown on my computer when I tested.

Here is the draw code on my test project:

            GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();
for (int i = 0; i < 100000; i++)
{
spriteBatch.Draw(pixel, rect, Color.ForestGreen);
}
spriteBatch.End();

base.Draw(gameTime);


Rect and pixel are created in the initialization, and are never changed after that.

Edited by Dobbydoo

##### Share on other sites

Then I'd go with phil_t's explaination, spritebatch is not meant for point sprite particle systems, there are samples in xna's resources webpage that implement point sprites, particle sistems both in 2d and 3d, look it up.

##### Share on other sites

Yes, that is my plan. I haven't had time to start yet, but I think I'm gonna use this opportunity to properly learn hlsl shaders. I've been meddling a bit with them, but not enough to properly understand them, so it'll be fun :)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 9
• ### Forum Statistics

• Total Topics
634085
• Total Posts
3015407
×