Setting render target, even if just for a moment, blackens everything

If I set a render target, even if I don’t really use it and do something like this:

GraphicsDevice.SetRenderTarget(target);
GraphicsDevice.SetRenderTarget(null);

Will turn the screen black, except for few things drawn in the end for some reason. Its like its clearing the back buffer or something. What am I doing wrong?

Thanks!

intended behaviour.
When changing rendertargets they get cleared. If you change to (null) it gets cleared

But how do I change that behavior? If every time I try to set a render target I lose everything I had in the back buffer it makes it nearly useless… Sure I can still use it for post effects etc, but everything like drawing a minimap, doing post-process on only some of the drawings etc, seems to be impossible.

Here is an answer i googled for you.

Seems like you can set it to PreserveContents (just like a rendertarget) with GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage

That said

Why don’t you just change the order? Draw all your rendertargets first and then draw to the backbuffer? This is how it’s supposed to be done.

1 Like

Thanks.
BTW apparently this has to be set inside the Game constructor or else it doesn’t work…

I have a tree of entities with hierarchy, sorting etc. It would be a nightmare.

Just a heads up that preserving contents can hurt performance a little bit.

2 Likes

PreserveContents is definitely not the recommended way of using render targets or the back buffer. The most efficient way of using them is to draw to all of your render targets first, and then lastly use those render targets when drawing to the back buffer.

1 Like