anathema's picture

Getting GraphicsModeException from WinGraphicsMode constructor

Project:The Open Toolkit library
Version:1.1-2014-01-02
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed
Description

I get this :-)

System.TypeInitializationException: The type initializer for 'Digitalis.LDTools.Controls.OpenGL.ModelView' threw an exception. ---> System.TypeInitializationException: The type initializer for 'OpenTK.Graphics.GraphicsMode' threw an exception. ---> OpenTK.Graphics.GraphicsModeException: No GraphicsMode available. This should never happen, please report a bug at http://www.opentk.com
at OpenTK.Platform.Windows.WinGraphicsMode..ctor()
at OpenTK.Platform.Windows.WinFactory.CreateGraphicsMode()
at OpenTK.Graphics.GraphicsMode..cctor()
--- End of inner exception stack trace ---
at OpenTK.Graphics.GraphicsMode.get_Default()
at Digitalis.LDTools.Controls.OpenGL.ModelView..cctor() in D:\Development\Digitalis.LDTools\ModelView.cs:line 210

I am running the code on WindowsXP on a VirtualBox VM, which might have something to do with it; however, this has worked in the past and the only thing I have changed is the build of OpenTK that I'm compiling against. At present I'm using the trunk, rev 3056, built locally.

The same code works normally on various Windows7 machines and on a real XP machine.


Comments

Comment viewing options

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

#11

I'm interested

c2woody's picture

#12

Some ideas put together, what I've made up from different docs how the pixel format selection works.

Don't see any way to attach files here, so this'll be a code snippet (put into a .diff file and use your favorite patch):

Index: Source/OpenTK/Platform/Windows/WinGraphicsMode.cs
===================================================================
--- Source/OpenTK/Platform/Windows/WinGraphicsMode.cs	(revision 3064)
+++ Source/OpenTK/Platform/Windows/WinGraphicsMode.cs	(working copy)
@@ -52,10 +52,26 @@
             {
                 using (INativeWindow native = new NativeWindow())
                 {
-                    modes.AddRange(GetModesARB(native));
+                    modes.AddRange(GetModesARB(native, WGL_ARB_pixel_format.FullAccelerationArb));
                     if (modes.Count == 0)
-                        modes.AddRange(GetModesPFD(native));
+                        modes.AddRange(GetModesPFD(native, true, false));   // accelerated non-generic only
                     if (modes.Count == 0)
+                    {
+                        Debug.WriteLine("No hardware-accelerated graphics modes found, testing generic formats.");
+
+                        modes.AddRange(GetModesARB(native, WGL_ARB_pixel_format.GenericAccelerationArb));
+                        if (modes.Count == 0)
+                            modes.AddRange(GetModesPFD(native, true, true));   // accelerated only
+                        if (modes.Count == 0)
+                        {
+                            Debug.WriteLine("No accelerated generic graphics modes found, testing non-accelerated generic formats.");
+
+                            modes.AddRange(GetModesARB(native, WGL_ARB_pixel_format.NoAccelerationArb));
+                            if (modes.Count == 0)
+                                modes.AddRange(GetModesPFD(native, false, true));   // any
+                        }
+                    }
+                    if (modes.Count == 0)
                         throw new GraphicsModeException(
                             "No GraphicsMode available. This should never happen, please report a bug at http://www.opentk.com");
                 }
@@ -127,7 +143,7 @@
 
         #region GetModesPFD
 
-        IEnumerable<GraphicsMode> GetModesPFD(INativeWindow native)
+        IEnumerable<GraphicsMode> GetModesPFD(INativeWindow native, bool accelerated_only, bool generic_allowed)
         {
             WinWindowInfo window = native.WindowInfo as WinWindowInfo;
             IntPtr deviceContext = ((WinWindowInfo)window).DeviceContext;
@@ -141,12 +157,18 @@
                 PixelFormatDescriptorFlags.SUPPORT_OPENGL |
                 PixelFormatDescriptorFlags.DRAW_TO_WINDOW;
 
+            if (accelerated_only && generic_allowed)
+                pfd.Flags |= PixelFormatDescriptorFlags.GENERIC_ACCELERATED;
+
             int pixel = 0;
             while (DescribePixelFormat(deviceContext, ++pixel, API.PixelFormatDescriptorSize, ref pfd) != 0)
             {
-                // Ignore non-accelerated formats.
-                if ((pfd.Flags & PixelFormatDescriptorFlags.GENERIC_FORMAT) != 0)
-                    continue;
+                if (!generic_allowed)
+                {
+                    // Ignore non-hardwareaccelerated formats.
+                    if ((pfd.Flags & PixelFormatDescriptorFlags.GENERIC_FORMAT) != 0)
+                        continue;
+                }
 
                 GraphicsMode fmt = new GraphicsMode((IntPtr)pixel,
                     new ColorFormat(pfd.RedBits, pfd.GreenBits, pfd.BlueBits, pfd.AlphaBits),
@@ -165,8 +187,12 @@
 
         #region GetModesARB
 
-        IEnumerable<GraphicsMode> GetModesARB(INativeWindow native)
+        IEnumerable<GraphicsMode> GetModesARB(INativeWindow native, WGL_ARB_pixel_format acceleration_type)
         {
+            if ((acceleration_type != WGL_ARB_pixel_format.FullAccelerationArb) &&
+                (acceleration_type != WGL_ARB_pixel_format.GenericAccelerationArb) &&
+                (acceleration_type != WGL_ARB_pixel_format.NoAccelerationArb))
+                throw new ArgumentException();
             using (IGraphicsContext context = new GraphicsContext(
                 new GraphicsMode(new IntPtr(2), new ColorFormat(), 0, 0, 0, new ColorFormat(), 2, false),
                 (WinWindowInfo)native.WindowInfo, 1, 0, GraphicsContextFlags.Default))
@@ -214,7 +240,7 @@
                 int[] attribs_values = new int[]
                 {
                     (int)WGL_ARB_pixel_format.AccelerationArb,
-                    (int)WGL_ARB_pixel_format.FullAccelerationArb,
+                    (int)acceleration_type,
                     (int)WGL_ARB_pixel_format.SupportOpenglArb, 1,
                     (int)WGL_ARB_pixel_format.DrawToWindowArb, 1,
                     0, 0
[/quote]
the Fiddler's picture

#13

Status:in progress» fixed

Fix committed to SVN trunk r3066.

c2woody's picture

#14

Still like my patch better since it has ARB fallbacks and takes MCD precedence over software-only generic implementations. Hope the driver does some usable ordering.

the Fiddler's picture

#15

Version:1.x-dev» 1.1-2014-01-02
Status:fixed» closed

Closing bugs fixed in OpenTK 1.1.

If this is still an issue please file a new bug report at https://github.com/opentk/opentk/issues