The following sections detail different aspects of render targets, useful when developing XNA Framework games.
Render Targets and Persistence
Prior to XNA Game Studio 2.0, the persistence of render targets depended on the target platform. Now, the persistence behavior can be specified, using RenderTargetUsage, in the constructor. For example, the following line of code constructs a render target object that is never cleared when set.
|Persistence behavior for the related depth-stencil buffer matches that of the render target.|
discardTarget= new RenderTarget2D(GraphicsDevice, 200, 200, 1, SurfaceFormat.Color, RenderTargetUsage.PreserveContents);
The following tables lists related persistence details, based on the target platform and the default value RenderTargetUsage.DiscardContents, used by the constructor.
Creating the Render Target
|Xbox 360||Two surfaces are created: the render target and the resolve texture.|
|Windows||The default behavior creates two surfaces: the render target surface and the texture used as the output for resolve actions. If persistence is specified (for example, RenderTargetUsage.PreserveContents) and the render target does not have multisampling, only one surface is created. This surface is a render target texture that is both the surface and the output texture for resolve actions.|
Setting the Render Target
|Xbox 360||By default, the resolve texture data is deleted when the render target is set. If persistence is enabled, the resolve texture data is moved back into the render target (emulating a persisted render target) before the new render target is set.|
|Windows||The current resolve texture data is saved when the render target is set.|
Resolving the Render Target
|Xbox 360||Resolution occurs when a new render target is set. The render target clears to a known, standard color rather than Color.TransparentBlack.|
|Windows||Resolution occurs when a new render target is set. However, by default, the render target (always a separate surface) is cleared to a known, standard color rather than Color.TransparentBlack. If persistence is enabled, no action is taken.|
This option creates a render target that has persistence for Windows but not for Xbox 360. However, for games targeting Windows, if the render target is created without multisampling only a single texture is created. This texture is both the render target surface as well as the resolved texture.
Resolving a Render Target
Prior to XNA Game Studio 2.0, you called ResolveRenderTarget to indicate that you were finished drawing to a render target. This method is now considered obsolete and causes a compilation error if used in your code. The actual resolve operation occurs automatically when a new render target is set (for example, a call to SetRenderTarget). Note that calling GetTexture on the current render target throws an exception. This behavior ensures that a new render target has been set (and thus resolved) before the texture can be used for anything else.
Multiple Render Targets
Multiple Render Targets (MRT) refers to the ability to render to multiple surfaces with a single draw call. For games targeting Windows, this option is available on some graphics cards. For games targeting Xbox 360, this option is natively supported. This feature can be used to perform some very nice effects such as deferred shading, or effects such as depth of field or refraction.
The following restrictions apply to MRTs on both platforms.
- All render targets of an MRT must have the same bit depth. However, they can use different formats unless the SupportsMultipleRenderTargetsIndependentBitDepths property is false.
- All surfaces of an MRT should have the same width and height.
- Antialiasing is not supported for MRTs.
- Some MRT implementations do not apply the output write mask. Those that do (indicated by a value of true for the SupportsColorWrite property) have independent color write masks. The number of independent color write masks available is equal to the maximum number of elements the device is capable of supporting.
Some MRT implementations do not perform post-pixel shader operations, such as dithering, alpha testing, fogging, and blending or masking (except for depth-buffer and depth-stencil testing). Those that do (indicated by a value of true for the SupportsMultipleRenderTargetsPostPixelShaderBlending property).
For those devices that support post-pixel shader operations, use CheckDeviceFormat (passing PostPixelShaderBlending for the queryUsages parameter value) to verify support for the specific surface format. If the result is false, no post-pixel shader blending operations are available for that specific surface format. If true, the device is expected to apply the same state to all simultaneous render targets as follows:
- Alpha blend: The color value in oCi is blended with the ith render target.
- Alpha test: Comparison uses oC0. If the comparison fails, the pixel test is terminated for all render targets.
- Fog: Render target 0 is fogged. Other render targets are undefined. Implementations can choose to fog all by using the same state.
- Dithering: Undefined.