Skip to content

Lag on left mouse button click on macOS #8011

@wazawoo

Description

@wazawoo

I am seeing a noticeable lag on left click using a very minimal example, on both M1 and Intel Macs. Full discussion here.

I did find settings needed to remove this (posted below), but it really doesn't seem like intended behavior.

If I apply both of these settings, it goes away:

GraphicsDeviceManager.SynchronizeWithVerticalRetrace = false
Game.IsFixedTimeStep = false

PS: Here are the results of all four combos of these settings:

  • fixed timestep OFF, vsync OFF: Works great, no noticeable lag
  • fixed timestep OFF, vsync ON: Small lag noticeable
  • fixed timestep ON, vsync OFF: Significant lag
  • fixed timestep ON, vsync ON: Significant lag

Full minimal example Game1.cs:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MouseButtonLagTest;

public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
    private SpriteBatch _spriteBatch;

    private Texture2D whiteTexture;
    private MouseState currentMouseState;

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);

        // leave all commented to see default behavior (lag)
        // uncomment sections to see each case

        // fixed timestep OFF, vsync OFF: Works great, no noticeable lag:
        //IsFixedTimeStep = false;
        //_graphics.SynchronizeWithVerticalRetrace = false;

        // fixed timestep OFF, vsync ON: Small lag noticeable
        //IsFixedTimeStep = false;
        //_graphics.SynchronizeWithVerticalRetrace = true;

        // fixed timestep ON, vsync OFF: Significant lag
        //IsFixedTimeStep = true;
        //_graphics.SynchronizeWithVerticalRetrace = false;

        // fixed timestep ON, vsync ON: Significant lag
        //IsFixedTimeStep = true;
        //_graphics.SynchronizeWithVerticalRetrace = true;

        _graphics.ApplyChanges();
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        _spriteBatch = new SpriteBatch(GraphicsDevice);

        whiteTexture = new Texture2D(GraphicsDevice, 1, 1);
        whiteTexture.SetData(new[] { Color.White });
    }

    protected override void Update(GameTime gameTime)
    {
        currentMouseState = Mouse.GetState();
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        _spriteBatch.Begin();

        _spriteBatch.Draw(
            texture: whiteTexture,
            position: currentMouseState.Position.ToVector2(),
            sourceRectangle: null,
            color: currentMouseState.LeftButton == ButtonState.Pressed ? Color.Red : Color.White,
            rotation: 0f,
            origin: Vector2.Zero,
            scale: 10 * Vector2.One,
            effects: SpriteEffects.None,
            layerDepth: 1f
        );

        _spriteBatch.End();

        base.Draw(gameTime);
    }
}

What version of MonoGame does the bug occur on:

  • MonoGame 3.8.1

What operating system are you using:

  • macOS (Intel), macOS (M1)

What MonoGame platform are you using:

  • DesktopGL

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions