Jump to content

  • Log In with Google      Sign In   
  • Create Account


spriteBatch.Draw slow?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Dobbydoo   Members   -  Reputation: 174

Like
0Likes
Like

Posted 02 March 2014 - 08:24 AM

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?



Sponsor:

#2 xnafan   Members   -  Reputation: 159

Like
0Likes
Like

Posted 02 March 2014 - 03:26 PM

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.


www.xnafan.net - tutorials, tools and code samples for making games :)


#3 Dobbydoo   Members   -  Reputation: 174

Like
0Likes
Like

Posted 02 March 2014 - 04:06 PM

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.



#4 phil_t   Crossbones+   -  Reputation: 3202

Like
2Likes
Like

Posted 02 March 2014 - 08:06 PM

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).



#5 Dobbydoo   Members   -  Reputation: 174

Like
0Likes
Like

Posted 03 March 2014 - 09:28 AM

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 :)



#6 NEXUSKill   Members   -  Reputation: 446

Like
0Likes
Like

Posted 05 March 2014 - 09:24 AM

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, 05 March 2014 - 09:47 AM.

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272



#7 Dobbydoo   Members   -  Reputation: 174

Like
0Likes
Like

Posted 05 March 2014 - 11:34 AM

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);

            // TODO: Add your drawing code here
            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, 05 March 2014 - 11:35 AM.


#8 NEXUSKill   Members   -  Reputation: 446

Like
0Likes
Like

Posted 05 March 2014 - 12:08 PM

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.


Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272



#9 Dobbydoo   Members   -  Reputation: 174

Like
0Likes
Like

Posted 05 March 2014 - 12:19 PM

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 :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS