DrawUserIndexedPrimitives:为什么索引必须是short而不是int?

时间:2011-08-17 09:50:08

标签: c# xna xna-3.0

DrawUserIndexedPrimitive有两种类型的重载:Those accepting 16-bit indicesthose accepting 32-bit indices

使用16位索引时,一切正常。使用32位索引时,在调用InvalidOperationException时,我会收到An unexpected error has occurred带有“有用”错误文本DrawUserIndexedPrimitive(无内部异常)。当我在项目属性中启用“非托管代码调试”时,我得到了行

  

DrawUserPrimitives.exe中0x75a5b9bc的第一次机会异常:Microsoft C ++异常:长在内存位置0x0032e6b4 ..
  DrawUserPrimitives.exe中0x75a5b9bc的第一次机会异常:Microsoft C ++异常:长在内存位置0x0032e728 ..

在抛出异常之前的调试窗口中。

为什么会这样?由于两个重载都可用,我认为两者都可以工作(或者如果只支持其中一个,则至少抛出一个有意义的异常)。这是一个完整的最小示例程序,可用于重现此问题。我正在使用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);
    }
}

1 个答案:

答案 0 :(得分:2)

隐藏的错误消息似乎是XNA 3.0中的一个错误(感谢Andrew提示)。在XNA 4.0中运行代码(稍加修改)会产生更有用的错误消息(NotSupportedException):

  

XNA Framework Reach配置文件不支持32位索引。使用IndexElementSize.SixteenBits或大小为两个字节的类型。

这是有道理的,因为我的显卡似乎不支持HiDef配置文件,它支持32位索引。