Use Multisampling in MonoGame.Forms

Hey everyone,
in my Windows Forms application, I use a InvalidationControl (from MonoGame.Forms.Controls) to display geometric objects. Now I want to use multisampling.

Without it, the drawed lines (stretched 1 x 1 pixel textures) look really unusable when zoomed out (these are 4 lines):
image

Some things I know:

  • I can make multisampling work in another application when I inherit from Game (from Microsoft.Xna.Framework), create a GraphicsDeviceManager and set PreferMultiSampling to true. But in my InvalidationControl, I have no access such a GraphicsDeviceManager because it takes a Game as construction parameter.

      public class Game1 : Game {
          public Game1() {
              new GraphicsDeviceManager(this) { PreferMultiSampling = true };
    
  • The following lines in my InvalidationControl (called in Initialize) did not enable Multisampling:

    GraphicsProfile = GraphicsProfile.HiDef;
    GraphicsDevice.PresentationParameters.MultiSampleCount = 4;
    SetMultiSampleCount(8);

You need to enable Multisampling on the SwapChainRendertTarget control.
But I think that was broken on 3.8, if it doesn’t work try the latest develop build.

Thanks for the answer. What do you mean exactly by enable Multisampling? SwapChainRenderTarget.MultiSampleCount is read only. Do I need to use the SwapChainRenderTarget.GraphicsDevice for my SpriteBatch like this? (currently not working out but I’m gonna try the develop build)

        SwapChainRenderTarget.GraphicsDevice.PresentationParameters.MultiSampleCount = 8;
        SwapChainRenderTarget.GraphicsDevice.RasterizerState = new RasterizerState()
        {
            MultiSampleAntiAlias = true,
        };
        spriteBatch = new SpriteBatch(SwapChainRenderTarget.GraphicsDevice);

I suppose the SwapChainRendertTarget is wrapped in the InvalidationControl.
I am not sure if [MonoGame.Forms] exposes multisampling setting. You would need to look in the code/documentation or ask @SandboxBlizz .

2 Likes

A quick answer: You just need to call SetMultiSampleCount(8); in the Initialize() of your custom MonoGame.Forms control.

Don’t worry about the number. It gets automagically clamped to what the users graphics card can handle.

This is the root call in the GraphicsDeviceControl:

And this how the msaa parameter get clamped:

(If you are curious about how it works under the hood).

Use it like this:

Editor.BeginAntialising();

//Your code

Editor.EndAntialising();

In your Draw();

Note: RenderTargets are also managed automagically under the hood. You can use the integrated RenderTargetManager to create your own.

I hope this helps for the first. Currently very busy.

If you need further help just reply here. But it could take a while till I can answer.

1 Like

Thank you so, so much! :slight_smile:
It’s working perfectly now.
image

And one thing I found out: Editor.EndAntialising() must be called after the spriteBatch.End() method in order to work.
Very glad to find such fast and great help in this board!

1 Like