GL.GetError() returned InvalidValue

Hello,

I’m trying to get a Windows GL version of my game running. I’ve had some success with the DirectX version, but when the GL version boots:

A first chance exception of type ‘Microsoft.Xna.Framework.Graphics.MonoGameGLException’ occurred in MonoGame.Framework.dll
An unhandled exception of type ‘Microsoft.Xna.Framework.Graphics.MonoGameGLException’ occurred in MonoGame.Framework.dll
Additional information: GL.GetError() returned InvalidValue

with the callstack:

MonoGame.Framework.dll!Microsoft.Xna.Framework.Graphics.VertexBuffer.SetBufferData(int bufferSize, int elementSizeInBytes, int offsetInBytes, ParticleVertex data, int startIndex, int elementCount, int vertexStride, Microsoft.Xna.Framework.Graphics.SetDataOptions options) Line 167 C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Graphics.VertexBuffer.PlatformSetDataInternal(int offsetInBytes, ParticleVertex data, int startIndex, int elementCount, int vertexStride, Microsoft.Xna.Framework.Graphics.SetDataOptions options, int bufferSize, int elementSizeInBytes) Line 136 C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Graphics.VertexBuffer.SetDataInternal(int offsetInBytes, ParticleVertex data, int startIndex, int elementCount, int vertexStride, Microsoft.Xna.Framework.Graphics.SetDataOptions options) Line 108 C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Graphics.DynamicVertexBuffer.SetData(int offsetInBytes, ParticleVertex data, int startIndex, int elementCount, int vertexStride, Microsoft.Xna.Framework.Graphics.SetDataOptions options) Line 72 C#
Miasma2Mono.exe!ParticleSystem.AddNewParticlesToVertexBuffer() Line 475 C#
Miasma2Mono.exe!ParticleSystem.Draw() Line 400 C#
Miasma2Mono.exe!GraphicsManager.DrawComponents(LightingSetup lightCfg) Line 1217 C#
Miasma2Mono.exe!SceneRendering.DrawView(RenderView view, bool isMainLayer) Line 82 C#
Miasma2Mono.exe!Miasma.Miasma2Game.Draw(Microsoft.Xna.Framework.GameTime gameTime) Line 566 C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.DoDraw(Microsoft.Xna.Framework.GameTime gameTime) Line 673 C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Tick() Line 508 C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.OpenTKGamePlatform.RunLoop() Line 155 C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Run(Microsoft.Xna.Framework.GameRunBehavior runBehavior) Line 403 C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Run() Line 376 C#
Miasma2Mono.exe!Miasma.Program.Run<Miasma.Miasma2Game>() Line 27 C#
Miasma2Mono.exe!Miasma.Program.Main(string args) Line 16 C#
[External Code]

Any idea what is going on there?

I had to change quite a lot of shader effect parameter setting, I think because the shader compiler optimised them out. (I now check if they exist before setting them.)

Also, the game currently boots up and only shows a red screen. Possibly related?

Any and all input welcome!

Thanks,
David

What parameters are you passing in your call to DynamicVertexBuffer.SetData? And how big is the DynamicVertexBuffer?

Hurrah! Your prodding made me properly go and check the vertex buffer creation and declaration code. Turns out I had an error in my offsets, and two of the elements overlapped.

Funnily enough this ran ok in DirectX! (Even with the warning level cranked up.) Once I’d fixed the offsets (and resultant vertex size) it ran.

Well I still have a weird red background (which makes it look bizarrely like a VirtualBoy game) but at least the characters are drawing. Thank you. :slight_smile:

Great! Do you mind pasting the previously-incorrect vertex buffer creation / declaration code here? so we can tighten up the unit tests in that area - perhaps there are some cases where we should be throwing exceptions for invalid parameters, but currently aren’t.

Sure… It’s a bit embarrassing but here’s the incorrect vertex declaration:

// Describe the layout of this vertex structure.
public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration
(
    new VertexElement(4, VertexElementFormat.Vector2,
                         VertexElementUsage.Position, 0),

    new VertexElement(4, VertexElementFormat.Vector3,
                         VertexElementUsage.Normal, 1),

    new VertexElement(16, VertexElementFormat.Vector3,
                          VertexElementUsage.Normal, 0),

    new VertexElement(28, VertexElementFormat.Color,
                          VertexElementUsage.Color, 0),

    new VertexElement(32, VertexElementFormat.Single,
                          VertexElementUsage.TextureCoordinate, 0)
);

// Describe the size of this vertex structure.
public const int SizeInBytes = 36;

It had gone wrong because I’d modified the ‘standard’ (from XNA samples) particle system Position element from a Short2 (of 4 Bytes total) to a Vector2 (8 Bytes total), and had neither updated the offsets due to the change in size, nor fixed a lazy copy-paste error resulting in the first element having the same offset as the second.

There was a valid reason I had to update the type from a Short2 - I think because it either didn’t exist in D3D11 or was incompatible with the MonoGame shader compiler.

Fyi here’s the fixed & updated vertex description:

// Describe the layout of this vertex structure.
public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration
(
    new VertexElement(0, VertexElementFormat.Vector2,
                         VertexElementUsage.Position, 0),

    new VertexElement(8, VertexElementFormat.Vector3,
                         VertexElementUsage.Normal, 1),

    new VertexElement(20, VertexElementFormat.Vector3,
                          VertexElementUsage.Normal, 0),

    new VertexElement(32, VertexElementFormat.Color,
                          VertexElementUsage.Color, 0),

    new VertexElement(36, VertexElementFormat.Single,
                          VertexElementUsage.TextureCoordinate, 0)
);


// Describe the size of this vertex structure.
public const int SizeInBytes = 40;

Yeah, I always get those vertex declarations wrong :slight_smile: I don’t think XNA / MG support the implicit “just add it after the previous element” (D3D11_APPEND_ALIGNED_ELEMENT) that you can do in D3D10+. It would be a nice future addition to the MG API.

If you don’t mind one more request: what parameters were you passing in to DynamicVertexBuffer? I’m still not clear how you got that OpenGL exception that you were getting - I think we should be able to prevent it with better parameter validation.

Hi,

I think it’s straight from the XNA particle system example:

    // Create a dynamic vertex buffer.
    vertexBuffer = new DynamicVertexBuffer(GraphicsManager.GetDevice(), ParticleVertex.VertexDeclaration, settings.MaxParticles * 4, BufferUsage.WriteOnly);

settings.MaxParticles in this case was 100.