Jump to content
  • Advertisement
Sign in to follow this  
Vile V

SpriteBatch BlendMode

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone,

I've almost finished with my game engine, all that remains are a few last bugs and problems I've yet to find a good solution for. One of the most important ones for my game involves the blend modes for the SpriteBatch class. My game engine uses a drawmanager to serve as a central point for all the drawing going on. I add every sprite that wishes to draw itself to that manager and a single call to the Manager's Draw calls .Begin() on the internal SpriteBatch and then .end(). But to use BlendModes you have to specify this when calling Begin(). The reason I'm using a DrawManager is that I only call Begin() once, everything gets drawn and then I call End(). It's clean, it's simple and it should be more performant than letting each sprite do this individually.

So far I haven't found a way to change the blendstate of the effect used internally by SpriteBatch because some sprite may want to be drawn with a certain blend state while others may not want to be. I want to avoid calling Begin and End multiple times for every time a sprite wants to use a different blend state. Is there some efficient, dynamic way to have my DrawManager use Blendstates when necessary without constantly beginning and ending the SpriteBatch class?

As I understand it, the advantage of a SpriteBatch is that it draws in batches (as the name implies), or am I mistaken in believing that the performance cost of constantly calling Begin() and End() would be noticeable?

I hope my question is clear, I'm not very good at explaining the problems I come across at times.

Thank you in advance for any and all answers.

Share this post


Link to post
Share on other sites
Advertisement
You are correct that making less calls to Begin() and End() are better, because each End() call submits the batch to the hardware. However, you're probably overthinking it a bit too much. You can send hundreds of batches to the hardware without really worrying about performance.

You should consider batching your sprites depending on their needs, instead of forcing everything into a single batch. Certainly batch them together as much as possible, but don't be afraid to start new batches when they need different things.

Share this post


Link to post
Share on other sites

You are correct that making less calls to Begin() and End() are better, because each End() call submits the batch to the hardware. However, you're probably overthinking it a bit too much. You can send hundreds of batches to the hardware without really worrying about performance.

You should consider batching your sprites depending on their needs, instead of forcing everything into a single batch. Certainly batch them together as much as possible, but don't be afraid to start new batches when they need different things.



Hmm, alright. I was kind of suspecting that I was overly worried about performance impacts. Thank you for the fast reply. I'll think on an efficient way to assess and organise sprites according to their needs. I gave each sprite a certain priority which determines when they should be drawn. I can further group them according to any blend states they might need and then draw them like that.

Once again thank you, you've given me food for thought :)

Share this post


Link to post
Share on other sites
My first idea would be to group the sprites according to their desired blend mode in the spritebatch manager class. So you'd have a dictionary with a key for each different blend mode, then when a sprite is added you simply put it into the right array in the dictionary based on its blend mode.

When drawing it would be a simple matter of iterating through the keys of the dictionary, if their arrays aren't empty then call the Begin method with the key then iterate through its array.

Share this post


Link to post
Share on other sites

My first idea would be to group the sprites according to their desired blend mode in the spritebatch manager class. So you'd have a dictionary with a key for each different blend mode, then when a sprite is added you simply put it into the right array in the dictionary based on its blend mode.

When drawing it would be a simple matter of iterating through the keys of the dictionary, if their arrays aren't empty then call the Begin method with the key then iterate through its array.


Since my game is completely 2D I have a lot of sprites that are drawn one atop the other and that order is quite important. If I'm not mistaken, using your method would give them the correct BlendStates but would result in some sprites being drawn over others, even though they should've been drawn beneath it. My drawManager has a PriorityQueue that handles this.

I fixed it by changing the BlendState used every time a different one is necessary. Most of the time it won't be so it's shorter than managing a few dictionaries.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!