BlueMonkMN's picture

Error using GetError

I have the following code to check for errors after most operations I execute with OpenTK:

   private static void CheckError()
      ErrorCode ec = GL.GetError();
      if (ec != 0)
         throw new System.Exception(ec.ToString());

On some systems (after disabling the OpenGL version check which also causes problems -- wrong version of OpenGL is reported I suspect) the following error message occurs:

Is there some more correct way I should be checking for errors?
I'm having a difficult time determining if the error is from calling GetError and storing the result in an Enum, or if InvalidEnum is actually the error that occurred during DrawFrame, which is now being reported. At first I suspected the earlier, and now I suspect the later. Is this the error that would be reported if, for example, I tried to use

GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

on a system that does not support alpha blending or something?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

InvalidEnum typically means that you are passing an invalid parameter to some OpenGL method. You could be using a parameter or a combination of parameters that is not supported on the target hardware/drivers. Do those systems have something in common (e.g. Intel IGPs)?

Moreover, what kind of problems does the version check cause? Note that version checks have changed between GL2 and GL3 (the first only supports GetString, while the latter supports both GetString and GetInteger for version checks).

Finally, try running your application against a debug version of OpenTK (0.9.8+). Debug versions add an implicit GetError() check after each OpenGL call, allowing you to pinpoint the *exact* method that causes the error. (Obviously this carries a large performance penalty, so use a release version of OpenTK for actual releases).

zahirtezcan's picture
the Fiddler wrote:

... try running your application against a debug version of OpenTK (0.9.8+). Debug versions add an implicit GetError() check after each OpenGL call, allowing you to pinpoint the *exact* method that causes the error. (Obviously this carries a large performance penalty, so use a release version of OpenTK for actual releases).

Is there an easy way to reference both debug and release versions of OpenTK for debug and release versions (respectively) of my software project. Since, there will be - supposedly - tens of projects in my solution and most probably they will all be referencing to OpenTK binaries. I'd rather reference once and just switch solution settings from debug to release, than update all references to OpenTK as release version.

I assumed that it would be possible if you are using GAC (Global Assembly Cache, i am using windows) but it is not possible to add OpenTK to GAC because it has not been signed (it does not have a strong name it says.)

Do you have any suggestions? Such that is it really possible through GAC? Or is there any easier way?

the Fiddler's picture

Yes, it is possible and no it doesn't involve the GAC (in reality, I don't think the GAC even supports this scenario).

Step by step instructions:

  1. Place the two binaries into a folder structure similar to this:
  2. Add a reference to the release version, save the solution and exit visual studio.
  3. Open the .csproj file and find the Reference element that refers to OpenTK.
  4. Change the path attribute from "../Dependencies/Release/OpenTK.dll" to "../Dependencies/$(Configuration)/OpenTK.dll"
  5. (Optional) Log a feature request to Microsoft Connect that you'd like to see support for this in the UI.

That's all: your project will now use the correct reference for each configuration.

The only caveat is that MonoDevelop does not support the $(Configuration) variable yet, so this trick won't work. I've logged a bug report, but I have the feeling this won't be fixed in time for MD 2.2.

BlueMonkMN's picture

I don't have much information about the problem since it's coming to me quite indirectly (someone is telling me about someone else who is having this problem). This is what I know:
The following code is what I use to verify the OpenGL version and features, and it reports that the system is inadequate (not sure if it's because of version or features, but I suspect version because that's the only one that's fatal):

         if (!GL.SupportsExtension("VERSION_1_2"))
            string errString = "OpenGL version 1.2 is required";
               errString += "; your version is: " + GL.GetString(StringName.Version);
            throw new ApplicationException(errString);
         if (!GL.SupportsExtension("GL_ARB_texture_rectangle"))
            System.Windows.Forms.MessageBox.Show(this, "GL_ARB_texture_rectangle may be required for proper operation. The current video driver does not support this feature. Try updating your video drivers.", "Requirement Check Warning", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);

When they disable this code, the program reports the other error instead. They say that other OpenGL programs work fine. I suggested they try the OpenTK demo programs, but I don't think I've heard back about that.

I'll try to get a more direct line between this thread and the source of the problem. :)

- Ben / "BlueMonkMN" / Scrolling Game Development Kit -

the Fiddler's picture

It could be that their drivers do not support OpenGL 1.2 (unlikely) or ARB_texture_rectangle (more likely). Older Intel drivers didn't support the texture rectangle extension and most people don't update their drivers.

Without knowing the renderer and version string, it's difficult to tell what the problem is, but it is possible to get an InvalidEnum error if you use an extension that is not supported (i.e. the extension accepts a new enum that is not normally valid - unless the extension is supported).

OTKLaw's picture

Hey Fiddler:
I've been working with Ben on this little problem for a while now. Trouble is this computer that is giving the errors is a computer that I only get to visit once in a while when I babysit!
Her video card is indeed an Intel card, and I believe her OpenGL version was 1.1. I did go straight to the manufacture's website and download the very latest drivers,
All I can say at this point is that other OpenGL based games are running great and seem to be using the same features that Ben's SGDK app is using. So, I wonder if there is some way when I visit the computer this Friday (June 24) I can run some kind of OpenGL feature query / diagnostic. I'd like to post back with all card and OpenGL related information that I can get - if you can recommend me such a tool?


the Fiddler's picture

The fact that some OpenGL applications run on this card does not mean that *all* OpenGL applications will be supported. There is a good chance this application is hitting a driver bug or using an unsupported feature (the fact that the version check fails strongly hints at the latter).

First of all, try running this specific application using a debug version of OpenTK. This way, you'll find out which OpenGL function is causing the InvalidEnum error, exactly.

Other than that, the output of GL.GetString(StringName.[Vendor|Renderer|Version]) would be most helpful. You can get that information using the "OpenGL Extensions" sample that ships with OpenTK (Examples.exe -> OpenTK). GLView is another very nice program that tests OpenGL support.

BlueMonkMN's picture

I started getting a similar error on my own laptop now (completely different project, but still based on SGDK2).
When I'm running in debug mode (with debug OpenTK), I can see the game come up and operate to a limited extent (there's a menu screen that works OK), and then when the game tries to do something more sophisticated (not sure what) I get an error. I'm looking at the CheckErrors function in GraphicsContext.cs

error_list contains 2 items. At position 0 is InvalidEnum, and at position 1 is NoError.
The next step up the call stack is ErrorHelper.Dispose.
Next step up is OpenTK..Graphics.GL.TexImage2D (at the end of the using block of course). The parameters to Delegates.glTexImage2D are:
target = TextureRectangleArb, level = 0, InternalFormat = Rgba8, width = 1280, height = 2560, border = 0, PixelFormat = Bgra, type = UnsignedByte.

I do have a check "if (!GL.SupportsExtension("GL_ARB_texture_rectangle"))" which passed.

The debug output is:
Detected configuration: Windows / .Net
DisplayDevice 0 (primary) supports 51 resolutions.
Creating default GraphicsMode (32, 16, 0, 0, 0, 2, False).
Device context: 1761676036
Selecting pixel format... Device context: 1761676036
Selecting pixel format... Creating GraphicsContext.
GraphicsMode: Index: 2, Color: 32 (8888), Depth: 24, Stencil: False, Samples: 0, Accum: 0 (0000), Buffers: 2, Stereo: False
IWindowInfo: Windows.WindowInfo: Handle 656830, Parent (null)
Loaded opengl32.dll: 1703739392
OpenGL will be bound to handle: 656830
Setting pixel format... 2
Creating temporary context for wgl extensions.
Load extensions for OpenTK.Platform.Windows.Wgl... 59 extensions loaded in 50 ms.
Falling back to GL2... success! (id: 131072)

Any suggestion on where to go from here?

- Ben / "BlueMonkMN" / Scrolling Game Development Kit -

the Fiddler's picture

Make sure that your video card supports ARB_texture_rectangle:


Edit: seems you edited your post while I was typing.

Check the max texture size of your drivers (GL.GetInteger()).

Edit 2: InvalidEnum... This probably means that PixelInternalFormat.Rgba8 is not good. Try pure Rgba.

BlueMonkMN's picture

All textures are created with the same parameters (at least with regard to pixel format), so I doubt that this would make a difference because some textures are working. But just to make sure, I used Rgba. I got the same result as before, and as I expected.

I tried the GetInteger call like this:

      int param;
      GL.GetInteger(GetPName.MaxTextureSize, out param);

and got 2048. Does that mean the height is too large?

Is there any better error handling (without specifically knowing everything that could go wrong) that could report this kind of error better if that's the case?

- Ben / "BlueMonkMN" / Scrolling Game Development Kit -