Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

RenderTarget Class

Represents a resource that will be written to at the end of a render pass. This is the base class for RenderTarget2D and RenderTargetCube.

Namespace: Microsoft.Xna.Framework.Graphics
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

public abstract class RenderTarget : IDisposable

After a render pass the render target contains the color information of a rendered image.

Render targets represent a linear area of display memory and usually reside in the display memory of the display card. Because of this, RenderTarget objects must be recreated when the device is reset.

To use a RenderTarget, you must:

  1. Create the RenderTarget.
    shadowRenderTarget = GfxComponent.CreateRenderTarget(GraphicsDevice,
        1, SurfaceFormat.Single);
    
    public static RenderTarget2D CreateRenderTarget(GraphicsDevice device, int numberLevels, 
        SurfaceFormat surface)
    {
    
        if (!GraphicsAdapter.DefaultAdapter.CheckDeviceFormat(DeviceType.Hardware,
            GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Format, TextureUsage.None,
            QueryUsages.None, ResourceType.RenderTarget, surface))
        {
            // Fall back to current display format
            surface = device.DisplayMode.Format;
        }
        return new RenderTarget2D(device,
            device.PresentationParameters.BackBufferWidth,
            device.PresentationParameters.BackBufferHeight,
            numberLevels, surface,
            device.PresentationParameters.MultiSampleType,
            device.PresentationParameters.MultiSampleQuality);
    
    }
    
  2. Set the RenderTarget.
    GraphicsDevice.SetRenderTarget(0, shadowRenderTarget);
    
  3. Draw into the RenderTarget.
    // Render the shadow map
    GraphicsDevice.Clear(Color.Black);
    DrawScene(MyEffect.shadowMap);
    
  4. Reset the original RenderTarget. null as the default render target - i.e. the back buffer.
    // Set render target back to the back buffer
    GraphicsDevice.SetRenderTarget(0, null);
    
  5. Call GetTexture to retrieve the contents of the RenderTarget.
    // Return the shadow map as a texture
    return shadowRenderTarget.GetTexture();
    

Xbox 360, Windows XP SP2, Windows Vista

Community Additions

Show:
© 2015 Microsoft