问题
I understand that Render Targets in XNA are volatile, but how volatile are they? I can't find any documentation that tells when exactly their contents are discarded. Is it just when you start drawing to them, or could it be at any time?
I would like to simply draw to a render target once and then use it as a Texture2D indefinitely. Is this possible? Would I need to enable RenderTargetUsage.PreserveContents
for this to work properly? I have read that PreserveContents is very slow on Xbox and phone hardware.
回答1:
Jonathan has given a great explanation of RenderTargetUsage
, but I'd like to clarify some things with regard to the original question, involving the use of a RenderTarget2D
as a Texture2D
:
Put simply, RenderTargetUsage
controls the behaviour of the surface when it is set on the device as a render target. If it is not set to preserve the contents, they will be cleared. On Xbox 360 this happens as a quirk of the hardware (for performance). On Windows this is emulated to match Xbox 360 by simply clearing the surface when you set it.
But, if you are using it as a texture, this is irrelevant. It's only an issue when you want to render to the one surface multiple times (setting it to the device each time) without losing the previous contents.
If you only set it as the render target once, at the start of your game, it won't matter what the preserve setting is because it never gets set as a render target again.
What you do care about is the IsContentLost
property (MSDN) or the ContentLost
event (MSDN). These will be set/raised if the contents that you rendered to the render target were lost (due to device changes, eg: going full-screen).
If you're re-rendering the render target each frame, then you don't need to check for content-lost. But if you expect to create your render targets at the start of your game and continue using them throughout, you need to ensure they haven't been lost after the frame on which they were created.
(Note that you don't need to worry about content-lost happening part-way through your Draw
call. I'm reasonably certain that it can only ever happen between frames.)
回答2:
Firstly, in regard to RenderTargetUsage.PreserveContents
your reading is correct. There are some hardware limitations (on the XBox at the very least) that prevent it from preserving the contents of a back buffer natively, this means it needs to be copied into the system RAM before a possible discard occurs and copied back afterwards.
The contents of buffers are discarded when it is set as the active render target. This means that they are discarded after it is flipped (if it is part of your primary flip chain, at the end of every Draw loop) or when you set it as one of the active render targets.
If you are in doubt install the DirectX developer runtime while developing your application (which is part of any DirectX SDK package) - this has an option to forcefully clear back buffers (because on the PC platform back buffers are not guaranteed to be discarded, even if PreserveContents isn't set).
来源:https://stackoverflow.com/questions/6897889/when-does-xna-discard-render-target-contents