Question

There are two types of overloads of DrawUserIndexedPrimitive: Those accepting 16-bit indices and those accepting 32-bit indices.

When using 16-bit indices, everything works fine. When using 32-bit indices, I get an InvalidOperationException with the "helpful" error text An unexpected error has occurred (no inner exceptions) when DrawUserIndexedPrimitive is called. When I enable "Unmanaged Code Debugging" in the project properties, I get the lines

First-chance exception at 0x75a5b9bc in DrawUserPrimitives.exe: Microsoft C++ exception: long at memory location 0x0032e6b4..
First-chance exception at 0x75a5b9bc in DrawUserPrimitives.exe: Microsoft C++ exception: long at memory location 0x0032e728..

in the debug window right before the exception is thrown.

Why does this happen? Since both overloads are available, I would imagine that both would work (or at least throw a meaningful exception if only one of them is supported). Here's a complete minimal sample program that can be used to reproduce this issue. I'm using XNA 3.0.

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

public class GameTest : Microsoft.Xna.Framework.Game
{
    static void Main(string[] args)
    {
        using (var game = new GameTest())
        {
            game.Run();
        }
    }

    GraphicsDeviceManager manager;

    public GameTest() { manager = new GraphicsDeviceManager(this); }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.SteelBlue);
        GraphicsDevice.VertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionColor.VertexElements);

        var basicEffect = new BasicEffect(GraphicsDevice, null);
        basicEffect.VertexColorEnabled = true;
        basicEffect.View = Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up);
        basicEffect.Projection = Matrix.CreateOrthographicOffCenter(-1, 2, 2, -1, 1.0f, 1000.0f);

        // two simple points
        var pointList = new VertexPositionColor[] {
            new VertexPositionColor(new Vector3(0,0,0), Color.White),
            new VertexPositionColor(new Vector3(0,1,0), Color.White)
        };

        // one simple line between those two points
        var lineListIndices = new short[] { 0, 1 };      // works fine
        // var lineListIndices = new int[] { 0, 1 };     // causes exception below

        basicEffect.Begin();
        foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
        {
            pass.Begin();
            GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
                PrimitiveType.LineList, pointList, 0, 2, 
                lineListIndices, 0, 1);  // exception occurs here
            pass.End();
        }
        basicEffect.End();

        base.Draw(gameTime);
    }
}
Was it helpful?

Solution

The cryptic error message seems to be a bug in XNA 3.0 (thanks Andrew for the hint). Running the code (slightly modified) in XNA 4.0 yields a more useful error message (NotSupportedException):

XNA Framework Reach profile does not support 32 bit indices. Use IndexElementSize.SixteenBits or a type that has a size of two bytes.

That make sense, since my graphics card does not seem to support the HiDef profile, which would support 32 bit indices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top