Export (0) Print
Expand All

How To: Enable Antialiasing (Multisampling)

This example demonstrates how to use the GraphicsDeviceManager to enable antialiasing for your game. We use the CheckDeviceMultiSampleType method to determine what kind of antialiasing your game machine supports.

Antialiasing is the process of smoothing the edges of triangles by averaging neighboring pixels in a render target. Because this process samples multiple pixels, it is also known as multisampling.

Bb975403.note(en-US,XNAGameStudio.20).gifNote
Enabling antialiasing on the Xbox 360 will have a small performance impact. Enabling antialiasing on PCs may have a large impact on your game performance.

The Complete Sample

The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.

To enable Antialiasing in your game

  1. Set PreferMultiSampling to true in your Game class constructor. This will default to MultiSampleType.TwoSamples on Xbox 360 (2x antialiasing). On a PC, the GraphicsDeviceManager will query for MultiSampleType.NonMaskable and select that if possible, and otherwise it will choose the best available antialiasing.
    graphics.PreferMultiSampling = true;
    

To customize Antialiasing in your game

  1. In your Game class constructor, add an event handler to the PreparingDeviceSettings event on your GraphicsDeviceManager. If PreferMultiSampling is true, a MultiSampleType will be chosen automatically before the event is raised. Whether or not a MultiSampleType is chosen when the event is raised, you can use the event handler to query and choose your own MultiSampleType.

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferMultiSampling = true;
        graphics.PreparingDeviceSettings +=
          new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
    }
    
  2. Create a method to use as an event handler for the PreparingDeviceSettings event. You will be using the PreparingDeviceSettingsEventArgs instance to access the PresentationParameters and set your antialiasing preferences.

    void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
    {
    
  3. If your game is running on Xbox 360, you can directly set the MultiSampleQuality and MultiSampleType properties and return from your event handler. The Xbox supports MultiSampleType.TwoSamples and MultiSampleType.FourSamples, each with a MultiSampleQuality of 1. MultiSampleType.FourSamples is 4x antialiasing; MultiSampleType.TwoSamples is 2x antialiasing.

                // Xbox 360 and most PCs support FourSamples/1 (4x) and TwoSamples/1 (2x)
                // antialiasing.
    #if XBOX
                e.GraphicsDeviceInformation.PresentationParameters.MultiSampleQuality = 1;
                e.GraphicsDeviceInformation.PresentationParameters.MultiSampleType =
                    MultiSampleType.FourSamples;
                return;
    #endif
    
  4. If your game is running on a PC, use CheckDeviceMultiSampleType to query for antialiasing support. Many PCs support 4x antialiasing, and most will support 2x antialiasing. Once you have determined that antialiasing is supported, set the MultiSampleType and MultiSampleQuality properties appropriately and return from the event handler.

        int quality = 0;
        GraphicsAdapter adapter = e.GraphicsDeviceInformation.Adapter;
        SurfaceFormat format = adapter.CurrentDisplayMode.Format;
        // Check for 4xAA
        if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format,
            false, MultiSampleType.FourSamples, out quality))
        {
            // even if a greater quality is returned, we only want quality 1
            e.GraphicsDeviceInformation.PresentationParameters.MultiSampleQuality = 1;
            e.GraphicsDeviceInformation.PresentationParameters.MultiSampleType =
                MultiSampleType.FourSamples;
        }
        // Check for 2xAA
        else if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format,
            false, MultiSampleType.TwoSamples, out quality))
        {
            // even if a greater quality is returned, we only want quality 1
            e.GraphicsDeviceInformation.PresentationParameters.MultiSampleQuality = 1;
            e.GraphicsDeviceInformation.PresentationParameters.MultiSampleType =
                MultiSampleType.TwoSamples;
        }
        return;
    }
    

Community Additions

ADD
Show:
© 2015 Microsoft