Can SpriteBatch's be nested?

In my limited time using Monogame I’ve always ended a Spritebatch before beginning a new one, even if the spritebatch was a different one. I was wondering if it’s possible to have a spritebatch begin and start doing all the draws then part way through create a new RenderTarget2D and have another sprite batch draw to that so that the new Texture can then be used in the main spritebatch? Is that possible or do I need to End the first one, create the new texture then start up the original spritebatch again? Thanks for any help.

Hi procd,

SpriteBatches can be nested without problems (I’m using it for ingame elements and UI), but I’m not sure what you’re trying is possible.

As far as I know, on most platforms original XNA (WP7 and XNA, not sure on PC) the RenderTarget was cleared when selecting it. I think Shawn explained that there was a hardware reason for that, it was not a design decision.

So, on original XNA you couldn’t selected RT1 , draw a little bit, select RT2, draw, and then select back RT1 because this last selection would delete all you drawed first.

However, no drawing is performed until you call SpriteBatch.End , so depending on the combination of code could be working if no “End” is called first. Anyways, it’s probably cleaner selecting RT2, do what you need, and afterwards select RT1 and draw everything else, including RT2.

I don’t have a lot of experience on this kind of things, but I think the explanation is correct (anybody, feel free to correct me if I’m wrong)

Thanks for the reply. What I would be doing is have a spritebatch that is writing to the backbuffer and this spritebatch would have to draw many sprite textures, possibly thousands. I mention that because I remember reading that deferred mode may do a draw even though End hasn’t been called due to caching limits. All of the textures that the spritebatch draws will already be ready to go however some will need to be tweaked by a pixelshader and I want to be able to create a new rendertarget and draw a new texture that is altered by a pixelshader I will then cache that texture which can then be drawn to the backbuffer by the main spritebatch… Hope that makes sense?

Hi!

I’m not sure if SpriteBatch can send data to the GPU if it has filled its maximum number of polygons, however what I’m almost sure is that mixing render targets often lead to problems.

I think it’d be better if you did all your job to render the auxiliar render targets ( Render first the draws which need to be tweated into the new rendertarget with a pixel shader) and after that, draw everything into the “main” render target.

Or you could look at the implementation of MonoGame sprite batchs. It seems to me that the spriteBatch grows as needed as does not render anything until End is called (unless in Immediate mode) but I just had a quick look and can’t assure that for sure.

Thank you again. My gut reaction was that it would be problematic and I’ve avoided it so far but I was trying to avoid having to iterate through potentially thousands of sprites to check for a texture update then do it all again to draw them