Export (0) Print
Expand All

How To: Enable Antialiasing (Multisampling)

Demonstrates how to use the GraphicsDeviceManager to enable antialiasing for your game. The CheckDeviceMultiSampleType method determines what kind of antialiasing your game computer 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.30).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 topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Using Antialiasing

To enable antialiasing in your game

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, set directly the MultiSampleQuality and MultiSampleType properties, and then return from your event handler.

    The Xbox supports MultiSampleType.TwoSamples and MultiSampleType.FourSamples, each with a MultiSampleQuality of 0 or 1. MultiSampleType.FourSamples is 4x antialiasing; MultiSampleType.TwoSamples is 2x antialiasing.

                // Xbox 360 and most PCs support FourSamples/0 (4x) and TwoSamples/0 (2x)
                // antialiasing.
    #if XBOX
                e.GraphicsDeviceInformation.PresentationParameters.MultiSampleQuality = 0;
                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 then 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 0
            e.GraphicsDeviceInformation.PresentationParameters.MultiSampleQuality = 0;
            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 0
            e.GraphicsDeviceInformation.PresentationParameters.MultiSampleQuality = 0;
            e.GraphicsDeviceInformation.PresentationParameters.MultiSampleType =
                MultiSampleType.TwoSamples;
        }
        return;
    }
    

Community Additions

ADD
Show:
© 2014 Microsoft