user1152717
user1152717

Reputation: 59

SpriteBatch.Begin and SpriteBatch.End

This is a performance question about SpriteBatches in XNA. When you want to start drawing in XNA, you have to use spritebatch.Begin(). This is how you specify a bunch of things, like the shaders used. This creates a problem because different sprites could use different shaders. I saw some tutorials where the begin and end was used in the draw call of every single object. I would like to do this, because I want every sprite to be able to use its own shader. How can I do this without causing performance problems?

Upvotes: 2

Views: 1887

Answers (1)

Andrew Russell
Andrew Russell

Reputation: 27245

If you're going to let each sprite use its own shader, presumably some of them will share the same shader. Make sure that those sprites go into the same batch, in order to get the maximum performance possible.

(This might involve some fairly ugly or fairly complicated code to sort your objects by shader, and detect when to change batches. You will just have to live with that.)

The idea behind SpriteBatch is that it literally batches sprites together before sending them to the GPU. Changing texture within a begin/end block, or starting a new begin/end block will require a new batch.

You get a few hundred batches per frame.

For a deeper discussion of this, see this answer and perhaps this answer over on the game development site.

Upvotes: 4

Related Questions