Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to understand the exact meaning of RenderTargetUsage.DiscardContents. The documentation says:

Determines how render target data is used once a new render target is set.

DiscardContents - Always clears the render target data.

  1. Does calling GraphicsDevice.SetRenderTarget(null) to return to the back buffer count as setting a new render target (therefore clearing discarding any previous one)?
  2. Are the contents discarded either way at the end of the frame (i.e. when the graphic device presents), or do they persist into future frames even in this mode?
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Does calling GraphicsDevice.SetRenderTarget(null) to return to the back buffer count as setting a new render target (therefore clearing discarding any previous one)?

Yes.

Are the contents discarded either way at the end of the frame (i.e. when the graphic device presents), or do they persist into future frames even in this mode?

In DiscardContents mode all of the render targets and the backbuffer share the same chunk of memory. So contents are not discarded they're overwritten triggering the ContentLost event. So the current rendertarget will retain its contents until its cleared/overwritten which I'm pretty sure graphicDevice.present() doesn't do.

Source: Shawn Hargreaves Blog

share|improve this answer
    
Thanks for the clarification. At first I thought it was inconsistent with the behavior I had experienced but I now realize why. When resolving the rendertarget (which since XNA 2.0 happens automatically when you switch rendertargets) its contents are copied into a separate area of texture memory. So, even though the contents of the rendertarget were discarded, using the rendertarget object as a texture is still valid. I haven't tested, but it would make sense to believe this texture persists until the next time I bind to that rendertarget too. –  David Gouveia Dec 30 '11 at 23:09
    
Pretty sure (also haven't tested) it would persist until you switched away from the associated renderTarget and then it would then be overwritten. Also as far as I can tell the renderTargets have little to no overhead to a texture2d when used as such. I see a lot of people reusing rendetargets and trying to clone the contents into texture2Ds. However, I've never had an issue with using a large number of render targets to set things up as needed outside of the main loop and using thought the life of the application as texture2Ds. –  ClassicThunder Dec 30 '11 at 23:38
    
Yeah, I could swear I've read somewhere that there was some reason regarding the lifetime (or rather a lack of guarantee of its lifetime) of a RenderTarget2D texture, but I can't seem to find it now. And about your first sentence in the comment, were you referring to the render target contents or the texture? Because if you were talking about the texture, it contradicts the last part of your comment about them persisting through the entire game. –  David Gouveia Dec 31 '11 at 0:34
    
"or rather a lack of guarantee of its lifetime" I hope not. My GUI renders all of the widgets using renderTargets during the layout stage and if renderTargets have a limited lifetime my users are randomly going to have everything crash. However, no one has ever complained of such nor have I encountered such a problem. Also I meant the texture in the first sentence. I was saying not be shy of creating new renderTargets and keep them around as textures, since I can see no impact on performance. It work as long as you don't reuse the associated renderTarget. –  ClassicThunder Dec 31 '11 at 0:59
    
My confusion was with this part: "until you switched away from the associated renderTarget and then it would then be overwritten". It's not overwritten when you switch away from the render target; it's overwritten the next time you bind it to the graphics device. –  David Gouveia Dec 31 '11 at 1:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.