Render Targets

Describes how render targets can be an important part of your XNA Framework game when you render simple or complex scenes. For a description of a render target, see What Is a Render Target?.

The following sections detail different aspects of render targets, useful when developing XNA Framework games.

  • Render Targets and Persistence
  • Resolving a Render Target
  • Multiple Render Targets

Render Targets and Persistence

Before XNA Game Studio 3.0, the persistence of render targets depended on the target platform. You can specify the persistence behavior now by using RenderTargetUsage in the constructor. For example, the following line of code constructs a render target object that is never cleared when set.

Note

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 list related persistence details, based on the target platform and the default value RenderTargetUsage.DiscardContents used by the constructor.

Creating the Render Target

Platform Details
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 you specify persistence (for example, RenderTargetUsage.PreserveContents) and the render target does not have multisampling, you will create only one surface. This surface is a render target texture that is both the surface and the output texture for resolve actions.

Setting the Render Target

Platform Details
Xbox 360 By default, the resolve texture data is deleted when you set the render target. If you enable persistence, the resolve texture data moves back into the render target (emulating a persisted render target) before you set the new render target.
Windows The current resolve texture data is saved when you set the render target.

Resolving the Render Target

Platform Details
Xbox 360 Resolution occurs when you set a new render target. The render target clears to a known, standard color rather than Color.TransparentBlack.
Windows Resolution occurs when you set a new render target. 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.

In addition to RenderTargetUsage.PreserveContents and RenderTargetUsage.DiscardContents, there is a third option that is platform-specific: RenderTargetUsage.PlatformContents.

This option creates a render target that has persistence for Windows, but not for Xbox 360. However, for games targeting Windows, if you create the render target ithout multisampling, you only create a single texture. This texture is both the render target surface as well as the resolved texture.

Resolving a Render Target

Before XNA Game Studio 3.0, you called ResolveRenderTarget to indicate that you were finished drawing to a render target. This method is now considered obsolete. It causes a compilation error if you use it in your code. The actual resolve operation occurs automatically when you set a new render target (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 you can use the texture 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. You can use this feature 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, you won't find any available post-pixel shader blending operations 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.

See Also

Conceptual

Graphics