DrawUserIndexedPrimitives has 1px offset

Hey guys,

I have some stange issue with my xna port. In XNA I used a litte lib to draw lines into my editor. In Monogame this lines have a strange offset of one pixel. Here a some screenshots:

Monogame: https://www.dropbox.com/s/ixv9qviiwu85niy/monogamewrong.png?dl=0
XNA: https://www.dropbox.com/s/ixv9qviiwu85niy/monogamewrong.png?dl=0

Platform is windows 10 with DX and current monogame nuget build

The code is very short:
this.effect = new BasicEffect(this.GraphicsDevice); this.effect.VertexColorEnabled = true; this.effect.View = Matrix.Identity;// Matrix.CreateLookAt(new Vector3(0f, 0f, 1f), Vector3.Zero, Vector3.Up); this.effect.Projection = Matrix.CreateOrthographicOffCenter (0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);

protected override void Draw(GameTime gameTime)
{
    this.GraphicsDevice.Clear(Color.CornflowerBlue);
                
    short[] indexData = { 0, 1 };
    VertexPositionColor[] vertexData =
    {
    new VertexPositionColor(new Vector3(10, 10, 0), Color.Red),
    new VertexPositionColor(new Vector3(100, 10, 0), Color.Red)
    };

    // TODO: Add your drawing code here
    this.effect.CurrentTechnique.Passes[0].Apply();
    this.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineList, vertexData, 0, 2, indexData, 0, 1);

    base.Draw(gameTime);
}

I’m not sure but could this be related to the “half pixel offset”?

I think DX9 (so XNA) required a half pixel offset correction for some operations, but not anymore in DX11 (MG)

I can’t find the document which describes the problem, but here is an example where the half pixel offset was applied:

https://blogs.msdn.microsoft.com/shawnhar/2010/04/05/spritebatch-and-custom-shaders-in-xna-game-studio-4-0/

You should apply the correction to the XNA version, but not to the MonoGame in order to fix it. If it doesn’t fix it, then it’s probably not this problem :slight_smile:

edit:

I found an excellent link with information about the half-pixel problem.

http://drilian.com/2008/11/25/understanding-half-pixel-and-half-texel-offsets/

hi,
this seems to work. But I have to change it a little bit. The offset has to be positive.
Matrix halfPixelOffset = Matrix.CreateTranslation(0.5f, 0.5f, 0);