public TemporaryContext(INativeWindow native) { Debug.WriteLine("[WGL] Creating temporary context to load extensions"); if (native == null) { throw new ArgumentNullException(); } // Create temporary context and load WGL entry points // First, set a compatible pixel format to the device context // of the temp window WinWindowInfo window = native.WindowInfo as WinWindowInfo; WinGraphicsMode selector = new WinGraphicsMode(window.DeviceContext); WinGLContext.SetGraphicsModePFD(selector, GraphicsMode.Default, window); bool success = false; // Then, construct a temporary context and load all wgl extensions Context = new ContextHandle(Wgl.CreateContext(window.DeviceContext)); if (Context != ContextHandle.Zero) { success = TryMakeCurrent(window, Context.Handle); } else { Debug.Print("[WGL] CreateContext failed with error: {0}", Marshal.GetLastWin32Error()); } if (!success) { Debug.WriteLine("[WGL] Failed to create temporary context"); } }
public TemporaryContext(INativeWindow native) { Debug.WriteLine("[WGL] Creating temporary context to load extensions"); if (native == null) { throw new ArgumentNullException(); } // Create temporary context and load WGL entry points // First, set a compatible pixel format to the device context // of the temp window WinWindowInfo window = native.WindowInfo as WinWindowInfo; WinGraphicsMode selector = new WinGraphicsMode(window.DeviceContext); WinGLContext.SetGraphicsModePFD(selector, GraphicsMode.Default, window); bool success = false; // Then, construct a temporary context and load all wgl extensions Context = new ContextHandle(Wgl.CreateContext(window.DeviceContext)); if (Context != ContextHandle.Zero) { // Make the context current. // Note: on some video cards and on some virtual machines, wglMakeCurrent // may fail with an errorcode of 6 (INVALID_HANDLE). The suggested workaround // is to call wglMakeCurrent in a loop until it succeeds. // See https://www.opengl.org/discussion_boards/showthread.php/171058-nVidia-wglMakeCurrent()-multiple-threads // Sigh... for (int retry = 0; retry < 5 && !success; retry++) { success = Wgl.MakeCurrent(window.DeviceContext, Context.Handle); if (!success) { Debug.Print("wglMakeCurrent failed with error: {0}. Retrying", Marshal.GetLastWin32Error()); System.Threading.Thread.Sleep(10); } } } else { Debug.Print("[WGL] CreateContext failed with error: {0}", Marshal.GetLastWin32Error()); } if (!success) { Debug.WriteLine("[WGL] Failed to create temporary context"); } }