开发者

How often to call SpriteBatch.Begin()/.End()?

开发者 https://www.devze.com 2023-04-05 16:00 出处:网络
I\'m searching a performanc开发者_运维知识库e problem in the Drawing part of my xna code for windows phone 7 that occures sometimes after some seconds. Is there a best practice how/when to call the Sp

I'm searching a performanc开发者_运维知识库e problem in the Drawing part of my xna code for windows phone 7 that occures sometimes after some seconds. Is there a best practice how/when to call the SpriteBatch.Begin to draw something? Should it be called for each couple of sprites (in each class when it draws the player, the background, background objects,...) or would a calling in the beginning -> draw everything in all subclasses - be better for the performance?


You should not use them more than necessary, because Begin() means preparing the device for sprite rendering and End() means to restore to its previous state. This may vary through some flags in Begin() telling you want no state changes, and may complicate your code because you would have to manually set some states.

Device state changes are often slow and you should prevent doing them if not required, they also may interfere with anything you're doing, so if you want to render something not in SpriteBatch you should call End(), but if you are rendering sprites you should call it once for all sprites.

To prevent calling End() for modifying states that don't show on the previous sprite, you should call Flush() instead of End(). This renders the queued sprites with the current transformations and device states, but leaves the states intact. What the End() function does is to call Flush() and reset particular states set by Begin().


The best thing performance-wise is to call begin/end only once per Draw(), this causes all of the sprites in a batch to get processed by the graphics card at the same time. You should only use multiple sprite batches if you need to. Examples of when you might need to are:

  • You want to apply an effect only to certain sprites, but not others
  • You want to take a screenshot
  • You want to do something more complex with effects

Can anyone think of an other reason for multiple sprite batches (begin->end)?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号