static GraphicsMode CreateGraphicsMode(IntPtr display, ref XVisualInfo info)
        {
            // See what we *really* got:
            int r, g, b, a;
            Glx.GetConfig(display, ref info, GLXAttribute.ALPHA_SIZE, out a);
            Glx.GetConfig(display, ref info, GLXAttribute.RED_SIZE, out r);
            Glx.GetConfig(display, ref info, GLXAttribute.GREEN_SIZE, out g);
            Glx.GetConfig(display, ref info, GLXAttribute.BLUE_SIZE, out b);
            int ar, ag, ab, aa;
            Glx.GetConfig(display, ref info, GLXAttribute.ACCUM_ALPHA_SIZE, out aa);
            Glx.GetConfig(display, ref info, GLXAttribute.ACCUM_RED_SIZE, out ar);
            Glx.GetConfig(display, ref info, GLXAttribute.ACCUM_GREEN_SIZE, out ag);
            Glx.GetConfig(display, ref info, GLXAttribute.ACCUM_BLUE_SIZE, out ab);
            int depth, stencil, samples, buffers;
            Glx.GetConfig(display, ref info, GLXAttribute.DEPTH_SIZE, out depth);
            Glx.GetConfig(display, ref info, GLXAttribute.STENCIL_SIZE, out stencil);
            Glx.GetConfig(display, ref info, GLXAttribute.SAMPLES, out samples);
            Glx.GetConfig(display, ref info, GLXAttribute.DOUBLEBUFFER, out buffers);
            int st;
            Glx.GetConfig(display, ref info, GLXAttribute.STEREO, out st);

            // Note: Glx.GetConfig return buffers = 0 (false) or 1 (true).
            // OpenTK expects buffers = 1 (single-) or 2 (double-buffering),
            // so increase the GLX value by one.
            return new GraphicsMode(info.VisualID, new ColorFormat(r, g, b, a), depth, stencil, samples,
                new ColorFormat(ar, ag, ab, aa), buffers + 1, st != 0);
        }
Esempio n. 2
0
 public X11WindowInfo(IntPtr handle, X11WindowInfo parent)
 {
     this.handle = handle;
       this.parent = parent;
       if (parent == null)
     return;
       this.rootWindow = parent.rootWindow;
       this.display = parent.display;
       this.screen = parent.screen;
       this.visualInfo = parent.visualInfo;
 }
Esempio n. 3
0
        XVisualInfo SelectVisual(GraphicsMode mode, X11WindowInfo currentWindow)
        {
            XVisualInfo info = new XVisualInfo();
            info.VisualID = (IntPtr)mode.Index;
            info.Screen = currentWindow.Screen;
            int items;
            
            lock (API.Lock)
            {
                IntPtr vs = Functions.XGetVisualInfo(Display, XVisualInfoMask.ID | XVisualInfoMask.Screen, ref info, out items);
                if (items == 0)
                    throw new GraphicsModeException(String.Format("Invalid GraphicsMode specified ({0}).", mode));

                info = (XVisualInfo)Marshal.PtrToStructure(vs, typeof(XVisualInfo));
                Functions.XFree(vs);
            }

            return info;
        }
Esempio n. 4
0
 private XVisualInfo SelectVisual(GraphicsMode mode, X11WindowInfo currentWindow)
 {
     XVisualInfo template = new XVisualInfo();
       template.VisualID = mode.Index.Value;
       template.Screen = currentWindow.Screen;
       lock (API.Lock)
       {
     int local_1;
     IntPtr local_2 = Functions.XGetVisualInfo(this.Display, XVisualInfoMask.ID | XVisualInfoMask.Screen, ref template, out local_1);
     if (local_1 == 0)
       throw new GraphicsModeException(string.Format("Invalid GraphicsMode specified ({0}).", (object) mode));
     template = (XVisualInfo) Marshal.PtrToStructure(local_2, typeof (XVisualInfo));
     Functions.XFree(local_2);
       }
       return template;
 }
Esempio n. 5
0
        public X11GLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shared, bool direct,
                            int major, int minor, GraphicsContextFlags flags)
        {
            if (mode == null)
            {
                throw new ArgumentNullException("mode");
            }
            if (window == null)
            {
                throw new ArgumentNullException("window");
            }

            Mode = mode;

            // Do not move this lower, as almost everything requires the Display
            // property to be correctly set.
            Display = ((X11WindowInfo)window).Display;

            currentWindow            = (X11WindowInfo)window;
            currentWindow.VisualInfo = SelectVisual(mode, currentWindow);

            ContextHandle shareHandle = shared != null ?
                                        (shared as IGraphicsContextInternal).Context : (ContextHandle)IntPtr.Zero;

            Debug.Write("Creating X11GLContext context: ");
            Debug.Write(direct ? "direct, " : "indirect, ");
            Debug.WriteLine(shareHandle.Handle == IntPtr.Zero ? "not shared... " :
                            String.Format("shared with ({0})... ", shareHandle));

            if (!glx_loaded)
            {
                Debug.WriteLine("Creating temporary context to load GLX extensions.");

                // Create a temporary context to obtain the necessary function pointers.
                XVisualInfo visual = currentWindow.VisualInfo;
                IntPtr      ctx    = IntPtr.Zero;

                using (new XLock(Display))
                {
                    ctx = Glx.CreateContext(Display, ref visual, IntPtr.Zero, true);
                    if (ctx == IntPtr.Zero)
                    {
                        ctx = Glx.CreateContext(Display, ref visual, IntPtr.Zero, false);
                    }
                }

                if (ctx != IntPtr.Zero)
                {
                    new Glx().LoadEntryPoints();
                    using (new XLock(Display))
                    {
                        Glx.MakeCurrent(Display, IntPtr.Zero, IntPtr.Zero);
                        //Glx.DestroyContext(Display, ctx);
                    }
                    glx_loaded = true;
                }
            }

            // Try using the new context creation method. If it fails, fall back to the old one.
            // For each of these methods, we try two times to create a context:
            // one with the "direct" flag intact, the other with the flag inversed.
            // HACK: It seems that Catalyst 9.1 - 9.4 on Linux have problems with contexts created through
            // GLX_ARB_create_context, including hideous input lag, no vsync and other. Use legacy context
            // creation if the user doesn't request a 3.0+ context.
            if ((major * 10 + minor >= 30) && Glx.Delegates.glXCreateContextAttribsARB != null)
            {
                Debug.Write("Using GLX_ARB_create_context... ");

                unsafe
                {
                    // We need the FB config for the current GraphicsMode.
                    int     count;
                    IntPtr *fbconfigs = Glx.ChooseFBConfig(Display, currentWindow.Screen,
                                                           new int[] {
                        (int)GLXAttribute.VISUAL_ID,
                        (int)mode.Index,
                        0
                    }, out count);

                    if (count > 0)
                    {
                        List <int> attributes = new List <int>();
                        attributes.Add((int)ArbCreateContext.MajorVersion);
                        attributes.Add(major);
                        attributes.Add((int)ArbCreateContext.MinorVersion);
                        attributes.Add(minor);
                        if (flags != 0)
                        {
#warning "This is not entirely correct: Embedded is not a valid flag! We need to add a GetARBContextFlags(GraphicsContextFlags) method."
                            attributes.Add((int)ArbCreateContext.Flags);
                            attributes.Add((int)flags);
                        }
                        // According to the docs, " <attribList> specifies a list of attributes for the context.
                        // The list consists of a sequence of <name,value> pairs terminated by the
                        // value 0. [...]"
                        // Is this a single 0, or a <0, 0> pair? (Defensive coding: add two zeroes just in case).
                        attributes.Add(0);
                        attributes.Add(0);

                        using (new XLock(Display))
                        {
                            Handle = new ContextHandle(Glx.Arb.CreateContextAttribs(Display, *fbconfigs,
                                                                                    shareHandle.Handle, direct, attributes.ToArray()));

                            if (Handle == ContextHandle.Zero)
                            {
                                Debug.Write(String.Format("failed. Trying direct: {0}... ", !direct));
                                Handle = new ContextHandle(Glx.Arb.CreateContextAttribs(Display, *fbconfigs,
                                                                                        shareHandle.Handle, !direct, attributes.ToArray()));
                            }
                        }

                        if (Handle == ContextHandle.Zero)
                        {
                            Debug.WriteLine("failed.");
                        }
                        else
                        {
                            Debug.WriteLine("success!");
                        }

                        using (new XLock(Display))
                        {
                            Functions.XFree((IntPtr)fbconfigs);
                        }
                    }
                }
            }

            if (Handle == ContextHandle.Zero)
            {
                Debug.Write("Using legacy context creation... ");

                XVisualInfo info = currentWindow.VisualInfo;
                using (new XLock(Display))
                {
                    // Cannot pass a Property by reference.
                    Handle = new ContextHandle(Glx.CreateContext(Display, ref info, shareHandle.Handle, direct));

                    if (Handle == ContextHandle.Zero)
                    {
                        Debug.WriteLine(String.Format("failed. Trying direct: {0}... ", !direct));
                        Handle = new ContextHandle(Glx.CreateContext(Display, ref info, IntPtr.Zero, !direct));
                    }
                }
            }

            if (Handle != ContextHandle.Zero)
            {
                Debug.Print("Context created (id: {0}).", Handle);
            }
            else
            {
                throw new GraphicsContextException("Failed to create OpenGL context. Glx.CreateContext call returned 0.");
            }

            using (new XLock(Display))
            {
                if (!Glx.IsDirect(Display, Handle.Handle))
                {
                    Debug.Print("Warning: Context is not direct.");
                }
            }
        }
Esempio n. 6
0
 public static IntPtr XGetVisualInfo(IntPtr display, XVisualInfoMask vinfo_mask, ref XVisualInfo template, out int nitems)
 {
     return(XGetVisualInfoInternal(display, (IntPtr)(int)vinfo_mask, ref template, out nitems));
 }
Esempio n. 7
0
        public static IntPtr CreateContext(IntPtr dpy, XVisualInfo vis, IntPtr shareList, bool direct)
        {
            GCHandle h0 = GCHandle.Alloc(vis, GCHandleType.Pinned);

            try
            {
                return CreateContext(dpy, h0.AddrOfPinnedObject(), shareList, direct);
            }
            finally
            {
                h0.Free();
            }
        }
Esempio n. 8
0
 static extern IntPtr XGetVisualInfoInternal(IntPtr display, IntPtr vinfo_mask, ref XVisualInfo template, out int nitems);
Esempio n. 9
0
 public static extern int GetConfig(IntPtr dpy, ref XVisualInfo vis, GLXAttribute attrib, out int value);
Esempio n. 10
0
 public static extern IntPtr CreateContext(IntPtr dpy, ref XVisualInfo vis, IntPtr shareList, bool direct);
Esempio n. 11
0
        public X11Window(int x, int y, int width, int height, string title, GraphicsMode mode, DisplayDevice device)
        {
            Debug.Print("Creating X11GLNative window.");
            // Open a display connection to the X server, and obtain the screen and root window.
            Debug.Print("Display: {0}, Screen {1}, Root window: {2}",
                        API.DefaultDisplay, API.DefaultScreen, API.RootWindow);

            RegisterAtoms();
            VisualInfo = X11GLContext.SelectGraphicsMode(mode);
            // Create a window on this display using the visual above
            Debug.Print("Opening render window... ");

            XSetWindowAttributes attributes = new XSetWindowAttributes();

            attributes.colormap = API.XCreateColormap(API.DefaultDisplay, API.RootWindow, VisualInfo.Visual, 0 /*AllocNone*/);

            eventMask = EventMask.StructureNotifyMask /*| EventMask.SubstructureNotifyMask*/ | EventMask.ExposureMask |
                        EventMask.KeyReleaseMask | EventMask.KeyPressMask | EventMask.KeymapStateMask |
                        EventMask.PointerMotionMask | EventMask.FocusChangeMask |
                        EventMask.ButtonPressMask | EventMask.ButtonReleaseMask |
                        EventMask.EnterWindowMask | EventMask.LeaveWindowMask |
                        EventMask.PropertyChangeMask;
            attributes.event_mask = (IntPtr)eventMask;

            uint mask =
                (uint)SetWindowValuemask.ColorMap | (uint)SetWindowValuemask.EventMask |
                (uint)SetWindowValuemask.BackPixel | (uint)SetWindowValuemask.BorderPixel;

            WinHandle = API.XCreateWindow(API.DefaultDisplay, API.RootWindow,
                                          x, y, width, height, 0, VisualInfo.Depth /*(int)CreateWindowArgs.CopyFromParent*/,
                                          (int)CreateWindowArgs.InputOutput, VisualInfo.Visual, (IntPtr)mask, ref attributes);

            if (WinHandle == IntPtr.Zero)
            {
                throw new ApplicationException("XCreateWindow call failed (returned 0).");
            }

            if (title != null)
            {
                API.XStoreName(API.DefaultDisplay, WinHandle, title);
            }

            XSizeHints hints = new XSizeHints();

            hints.base_width  = width;
            hints.base_height = height;
            hints.flags       = (IntPtr)(XSizeHintsFlags.PSize | XSizeHintsFlags.PPosition);
            API.XSetWMNormalHints(API.DefaultDisplay, WinHandle, ref hints);
            // Register for window destroy notification
            API.XSetWMProtocols(API.DefaultDisplay, WinHandle, new IntPtr[] { wm_destroy }, 1);

            // Set the initial window size to ensure X, Y, Width, Height and the rest
            // return the correct values inside the constructor and the Load event.
            XEvent e = new XEvent();

            e.ConfigureEvent.x      = x;
            e.ConfigureEvent.y      = y;
            e.ConfigureEvent.width  = width;
            e.ConfigureEvent.height = height;
            RefreshWindowBounds(ref e);

            Debug.Print("X11GLNative window created successfully (id: {0}).", WinHandle);
            // Request that auto-repeat is only set on devices that support it physically.
            // This typically means that it's turned off for keyboards (which is what we want).
            // We prefer this method over XAutoRepeatOff/On, because the latter needs to
            // be reset before the program exits.
            bool supported;

            API.XkbSetDetectableAutoRepeat(API.DefaultDisplay, true, out supported);
            Exists = true;
        }
Esempio n. 12
0
        public unsafe X11GLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shared, bool direct, int major, int minor, GraphicsContextFlags flags)
        {
            if (mode == null)
            {
                throw new ArgumentNullException("mode");
            }
            if (window == null)
            {
                throw new ArgumentNullException("window");
            }
            this.Mode                     = mode;
            this.Display                  = ((X11WindowInfo)window).Display;
            this.currentWindow            = (X11WindowInfo)window;
            this.currentWindow.VisualInfo = this.SelectVisual(mode, this.currentWindow);
            ContextHandle contextHandle = shared != null ? (shared as IGraphicsContextInternal).Context : (ContextHandle)IntPtr.Zero;

            if (!this.glx_loaded)
            {
                XVisualInfo visualInfo = this.currentWindow.VisualInfo;
                IntPtr      num        = IntPtr.Zero;
                using (new XLock(this.Display))
                {
                    num = Glx.CreateContext(this.Display, ref visualInfo, IntPtr.Zero, true);
                    if (num == IntPtr.Zero)
                    {
                        num = Glx.CreateContext(this.Display, ref visualInfo, IntPtr.Zero, false);
                    }
                }
                if (num != IntPtr.Zero)
                {
                    new Glx().LoadEntryPoints();
                    using (new XLock(this.Display))
                        Glx.MakeCurrent(this.Display, IntPtr.Zero, IntPtr.Zero);
                    this.glx_loaded = true;
                }
            }
            if (major * 10 + minor >= 30 && Glx.Delegates.glXCreateContextAttribsARB != null)
            {
                int     fbount;
                IntPtr *numPtr = Glx.ChooseFBConfig(this.Display, this.currentWindow.Screen, new int[3]
                {
                    32779,
                    (int)mode.Index.Value,
                    0
                }, out fbount);
                if (fbount > 0)
                {
                    List <int> list = new List <int>();
                    list.Add(8337);
                    list.Add(major);
                    list.Add(8338);
                    list.Add(minor);
                    if (flags != GraphicsContextFlags.Default)
                    {
                        list.Add(8340);
                        list.Add((int)flags);
                    }
                    list.Add(0);
                    list.Add(0);
                    using (new XLock(this.Display))
                    {
                        this.Handle = new ContextHandle(Glx.Arb.CreateContextAttribs(this.Display, *numPtr, contextHandle.Handle, direct, list.ToArray()));
                        if (this.Handle == ContextHandle.Zero)
                        {
                            this.Handle = new ContextHandle(Glx.Arb.CreateContextAttribs(this.Display, *numPtr, contextHandle.Handle, !direct, list.ToArray()));
                        }
                    }
                    int num = this.Handle == ContextHandle.Zero ? 1 : 0;
                    using (new XLock(this.Display))
                        Functions.XFree((IntPtr)((void *)numPtr));
                }
            }
            if (this.Handle == ContextHandle.Zero)
            {
                XVisualInfo visualInfo = this.currentWindow.VisualInfo;
                using (new XLock(this.Display))
                {
                    this.Handle = new ContextHandle(Glx.CreateContext(this.Display, ref visualInfo, contextHandle.Handle, direct));
                    if (this.Handle == ContextHandle.Zero)
                    {
                        this.Handle = new ContextHandle(Glx.CreateContext(this.Display, ref visualInfo, IntPtr.Zero, !direct));
                    }
                }
            }
            if (!(this.Handle != ContextHandle.Zero))
            {
                throw new GraphicsContextException("Failed to create OpenGL context. Glx.CreateContext call returned 0.");
            }
            using (new XLock(this.Display))
                Glx.IsDirect(this.Display, this.Handle.Handle);
        }
        internal static GraphicsMode SelectGraphicsMode( GraphicsMode template, out XVisualInfo info )
        {
            int[] attribs = GetVisualAttribs( template.ColorFormat, template.Depth, template.Stencil, template.Buffers );
            IntPtr visual = SelectVisual( attribs );
            if( visual == IntPtr.Zero )
                throw new GraphicsModeException( "Requested GraphicsMode not available." );

            info = (XVisualInfo)Marshal.PtrToStructure( visual, typeof( XVisualInfo ) );
            API.XFree( visual );
            return GetGraphicsMode( info );
        }
 internal static GraphicsMode GetGraphicsMode( XVisualInfo info )
 {
     // See what we *really* got:
     int r, g, b, a, depth, stencil, buffers;
     IntPtr display = API.DefaultDisplay;
     Glx.glXGetConfig( display, ref info, GLXAttribute.ALPHA_SIZE, out a );
     Glx.glXGetConfig( display, ref info, GLXAttribute.RED_SIZE, out r );
     Glx.glXGetConfig( display, ref info, GLXAttribute.GREEN_SIZE, out g );
     Glx.glXGetConfig( display, ref info, GLXAttribute.BLUE_SIZE, out b );
     Glx.glXGetConfig( display, ref info, GLXAttribute.DEPTH_SIZE, out depth );
     Glx.glXGetConfig( display, ref info, GLXAttribute.STENCIL_SIZE, out stencil );
     Glx.glXGetConfig( display, ref info, GLXAttribute.DOUBLEBUFFER, out buffers );
     ++buffers;
     // the above lines returns 0 - false and 1 - true.
     return new GraphicsMode( info.VisualID, new ColorFormat(r, g, b, a), depth, stencil, buffers );
 }
Esempio n. 15
0
 public static extern IntPtr CreateContext(IntPtr dpy, ref XVisualInfo vis, IntPtr shareList, bool direct);
Esempio n. 16
0
        public X11GLNative(int x, int y, int width, int height, string title,
                           GraphicsMode mode, GameWindowFlags options, DisplayDevice device)
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width", "Must be higher than zero.");
            }
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", "Must be higher than zero.");
            }

            Debug.Print("Creating X11GLNative window.");
            // Open a display connection to the X server, and obtain the screen and root window.
            window.Display    = API.DefaultDisplay;
            window.Screen     = API.XDefaultScreen(window.Display);             //API.DefaultScreen;
            window.RootWindow = API.XRootWindow(window.Display, window.Screen); // API.RootWindow;

            Debug.Print("Display: {0}, Screen {1}, Root window: {2}", window.Display, window.Screen, window.RootWindow);
            RegisterAtoms(window);
            XVisualInfo info = new XVisualInfo();

            mode = X11GLContext.SelectGraphicsMode(mode, out info);
            window.VisualInfo = info;
            // Create a window on this display using the visual above
            Debug.Print("Opening render window... ");

            XSetWindowAttributes attributes = new XSetWindowAttributes();

            attributes.background_pixel = IntPtr.Zero;
            attributes.border_pixel     = IntPtr.Zero;
            attributes.colormap         = API.XCreateColormap(window.Display, window.RootWindow, window.VisualInfo.Visual, 0 /*AllocNone*/);
            window.EventMask            = EventMask.StructureNotifyMask /*| EventMask.SubstructureNotifyMask*/ | EventMask.ExposureMask |
                                          EventMask.KeyReleaseMask | EventMask.KeyPressMask | EventMask.KeymapStateMask |
                                          EventMask.PointerMotionMask | EventMask.FocusChangeMask |
                                          EventMask.ButtonPressMask | EventMask.ButtonReleaseMask |
                                          EventMask.EnterWindowMask | EventMask.LeaveWindowMask |
                                          EventMask.PropertyChangeMask;
            attributes.event_mask = (IntPtr)window.EventMask;

            uint mask = (uint)SetWindowValuemask.ColorMap | (uint)SetWindowValuemask.EventMask |
                        (uint)SetWindowValuemask.BackPixel | (uint)SetWindowValuemask.BorderPixel;

            window.WindowHandle = API.XCreateWindow(window.Display, window.RootWindow,
                                                    x, y, width, height, 0, window.VisualInfo.Depth /*(int)CreateWindowArgs.CopyFromParent*/,
                                                    (int)CreateWindowArgs.InputOutput, window.VisualInfo.Visual, (IntPtr)mask, ref attributes);

            if (window.WindowHandle == IntPtr.Zero)
            {
                throw new ApplicationException("XCreateWindow call failed (returned 0).");
            }

            if (title != null)
            {
                API.XStoreName(window.Display, window.WindowHandle, title);
            }

            // Set the window hints
            SetWindowMinMax(_min_width, _min_height, -1, -1);

            XSizeHints hints = new XSizeHints();

            hints.base_width  = width;
            hints.base_height = height;
            hints.flags       = (IntPtr)(XSizeHintsFlags.PSize | XSizeHintsFlags.PPosition);
            API.XSetWMNormalHints(window.Display, window.WindowHandle, ref hints);
            // Register for window destroy notification
            API.XSetWMProtocols(window.Display, window.WindowHandle, new IntPtr[] { wm_destroy }, 1);

            // Set the initial window size to ensure X, Y, Width, Height and the rest
            // return the correct values inside the constructor and the Load event.
            XEvent e = new XEvent();

            e.ConfigureEvent.x      = x;
            e.ConfigureEvent.y      = y;
            e.ConfigureEvent.width  = width;
            e.ConfigureEvent.height = height;
            RefreshWindowBounds(ref e);

            Debug.Print("X11GLNative window created successfully (id: {0}).", Handle);
            SetupInput();
            exists = true;
        }
Esempio n. 17
0
 public static extern int GetConfig(IntPtr dpy, ref XVisualInfo vis, GLXAttribute attrib, out int value);
Esempio n. 18
0
 private static extern IntPtr XGetVisualInfoInternal(IntPtr display, IntPtr vinfo_mask, ref XVisualInfo template, out int nitems);
Esempio n. 19
0
 public static IntPtr XGetVisualInfo(IntPtr display, XVisualInfoMask vinfo_mask, ref XVisualInfo template, out int nitems)
 {
     return XGetVisualInfoInternal(display, (IntPtr)(int)vinfo_mask, ref template, out nitems);
 }
Esempio n. 20
0
        public X11GLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shared, bool direct,
                            int major, int minor, GraphicsContextFlags flags)
        {
            if (mode == null)
            {
                throw new ArgumentNullException("mode");
            }
            if (window == null)
            {
                throw new ArgumentNullException("window");
            }

            Mode = mode;

            currentWindow            = (X11WindowInfo)window;
            currentWindow.VisualInfo = SelectVisual(mode, currentWindow);

            ContextHandle shareHandle = shared != null ?
                                        (shared as IGraphicsContextInternal).Context : (ContextHandle)IntPtr.Zero;

            Debug.Write("Creating X11GLContext context: ");
            Debug.Write(direct ? "direct, " : "indirect, ");
            Debug.WriteLine(shareHandle.Handle == IntPtr.Zero ? "not shared... " :
                            String.Format("shared with ({0})... ", shareHandle));

            if (!glx_loaded)
            {
                Debug.WriteLine("Creating temporary context to load GLX extensions.");

                // Create a temporary context to obtain the necessary function pointers.
                XVisualInfo visual = currentWindow.VisualInfo;
                IntPtr      ctx    = Glx.CreateContext(currentWindow.Display, ref visual, IntPtr.Zero, true);
                if (ctx == IntPtr.Zero)
                {
                    ctx = Glx.CreateContext(currentWindow.Display, ref visual, IntPtr.Zero, false);
                }

                if (ctx != IntPtr.Zero)
                {
                    new Glx().LoadAll();
                    Glx.MakeCurrent(currentWindow.Display, IntPtr.Zero, IntPtr.Zero);
                    //Glx.DestroyContext(currentWindow.Display, ctx);
                    glx_loaded = true;
                }
            }

            // Try using the new context creation method. If it fails, fall back to the old one.
            // For each of these methods, we try two times to create a context:
            // one with the "direct" flag intact, the other with the flag inversed.
            // HACK: It seems that Catalyst 9.1 - 9.4 on Linux have problems with contexts created through
            // GLX_ARB_create_context, including hideous input lag, no vsync and other. Use legacy context
            // creation if the user doesn't request a 3.0+ context.
            if ((major * 10 + minor >= 30) && Glx.Delegates.glXCreateContextAttribsARB != null)
            {
                Debug.Write("Using GLX_ARB_create_context... ");

                unsafe
                {
                    // We need the FB config for the current GraphicsMode.
                    int     count;
                    IntPtr *fbconfigs = Glx.ChooseFBConfig(currentWindow.Display, currentWindow.Screen,
                                                           new int[] { (int)GLXAttribute.VISUAL_ID, (int)mode.Index, 0 }, out count);

                    if (count > 0)
                    {
                        List <int> attributes = new List <int>();
                        attributes.Add((int)ArbCreateContext.MajorVersion);
                        attributes.Add(major);
                        attributes.Add((int)ArbCreateContext.MinorVersion);
                        attributes.Add(minor);
                        if (flags != 0)
                        {
                            attributes.Add((int)ArbCreateContext.Flags);
                            attributes.Add((int)flags);
                        }
                        attributes.Add(0);

                        Handle = new ContextHandle(Glx.Arb.CreateContextAttribs(currentWindow.Display, *fbconfigs,
                                                                                shareHandle.Handle, direct, attributes.ToArray()));

                        if (Handle == ContextHandle.Zero)
                        {
                            Debug.Write(String.Format("failed. Trying direct: {0}... ", !direct));
                            Handle = new ContextHandle(Glx.Arb.CreateContextAttribs(currentWindow.Display, *fbconfigs,
                                                                                    shareHandle.Handle, !direct, attributes.ToArray()));
                        }

                        if (Handle == ContextHandle.Zero)
                        {
                            Debug.WriteLine("failed.");
                        }
                        else
                        {
                            Debug.WriteLine("success!");
                        }

                        Functions.XFree((IntPtr)fbconfigs);
                    }
                }
            }

            if (Handle == ContextHandle.Zero)
            {
                Debug.Write("Using legacy context creation... ");

                XVisualInfo info = currentWindow.VisualInfo;   // Cannot pass a Property by reference.
                Handle = new ContextHandle(Glx.CreateContext(currentWindow.Display, ref info, shareHandle.Handle, direct));

                if (Handle == ContextHandle.Zero)
                {
                    Debug.WriteLine(String.Format("failed. Trying direct: {0}... ", !direct));
                    Handle = new ContextHandle(Glx.CreateContext(currentWindow.Display, ref info, IntPtr.Zero, !direct));
                }
            }

            if (Handle != ContextHandle.Zero)
            {
                Debug.Print("Context created (id: {0}).", Handle);
            }
            else
            {
                throw new GraphicsContextException("Failed to create OpenGL context. Glx.CreateContext call returned 0.");
            }

            if (!Glx.IsDirect(currentWindow.Display, Handle.Handle))
            {
                Debug.Print("Warning: Context is not direct.");
            }
        }