DirectX <> OpenGL difference: line smearing

I noticed some unwanted behaviour which is making our app look quite nasty.

When drawing horizontal/vertical lines from vertices, it should always be 1 pixel wide. This is the case on Windows-OpenGL, but not in DirectX: depending on the coordinates, it will be 1 pixel or 2 pixels wide.

Easy to reproduce: launch a new project for DirectX, and another one for OpenGL, use the following code as Draw method and you’ll notice a difference in execution! (y is a float variable)

Would anyone have a suggestion whether/how I can get sharp lines in DirectX? I tried nearly all possible renderstate configs, but it seems to be more related to the DX implementation used by monogame.

protected override void Draw(GameTime gameTime)

        y += 0.0001f;

        List<VertexPositionColor> vertList = new List<VertexPositionColor>();
        vertList.Add(new VertexPositionColor(new Vector3(0, y, 0), Color.White));
        vertList.Add(new VertexPositionColor(new Vector3(1, y, 0), Color.White));

        BasicEffect effect = new BasicEffect(GraphicsDevice);
        effect.World = effect.Projection = effect.View = Matrix.Identity;
        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertList.ToArray(), 0, vertList.Count / 2);


(other than that, and as always, kudos for this amazing software package!)

Are you using DX9? If so half-pixel issue?

DxDiag reports version 11. Just tried installing the latest DX runtimes; no change.

I created empty MonoGame 3.4. Windows Project, copy-pasted your code and line is one pixel. Maybe some driver settings?

Thanks for testing!

Strange. We’ve seen this on plenty of PCs, and also numerous of our Win users have reported this as a bug. Last week I had to freshly install this PC again with a Win7, added MonoGame 3.4 and just created these 2 empty project. I hoped my problem would be gone with a completely fresh install, but it didn’t, that’s why I post this here. Would it be a valid assumption to say what I have would be the default behaviour, until I change some setting somewhere and then it’s corrected? The questions is: which setting :slight_smile:
Might be a driver setting, although I assume OpenGL and DirectX use the same hardware driver?

Do you have multisample antialiasing enabled or disabled?

When multisampling is disabled, try to set a RasterizerState where RasterizerState.MultiSampleAntiAlias = false.
(The built-in rasterizer states have MultiSampleAntiAlias = true, which makes the lines look bad.)

1 Like

wow you actually entirely solved my problem which I’ve been suffering from since 10 months or so… you should go write some books on xna/monogame :wink:
Thanks a lot!!!

For anyone struggling with the same issue, this is what you need:

        RasterizerState r = new RasterizerState();
        r.MultiSampleAntiAlias = false;
        GraphicsDevice.RasterizerState = r;

Each time you call SpriteBatch.Begin though, your rasterizestate is overwritten by a default one where the MultiSampleAntiAlias property is true. Since AA isn’t supported yet by monogame, I solved this quick’n’dirty by changing line106 of monogame’s RasterizerState.cs to the following:

MultiSampleAntiAlias = false;

Cleaner would have been to store the current state at spritebatch.begin and restore it at spritebatch.end. Not sure if this is always desired though.