MakeCurrent() public static method

public static MakeCurrent ( IntPtr display, IntPtr drawable, ContextHandle context ) : bool
display System.IntPtr
drawable System.IntPtr
context ContextHandle
return bool
Example #1
0
 private void Dispose(bool manuallyCalled)
 {
     if (!IsDisposed)
     {
         if (manuallyCalled)
         {
             IntPtr display = Display;
             if (IsCurrent)
             {
                 using (new XLock(display))
                 {
                     Glx.MakeCurrent(display, IntPtr.Zero, IntPtr.Zero);
                 }
             }
             using (new XLock(display))
             {
                 Glx.DestroyContext(display, Handle);
             }
         }
     }
     else
     {
         Debug.Print("[Warning] {0} leaked.", this.GetType().Name);
     }
     IsDisposed = true;
 }
Example #2
0
        public override void MakeCurrent(IWindowInfo window)
        {
            if (window == null)
            {
                Glx.MakeCurrent(currentWindow.Display, IntPtr.Zero, IntPtr.Zero);
            }
            else
            {
                X11WindowInfo w = (X11WindowInfo)window;
                bool          result;

                Debug.Write(String.Format("Making context {0} current on thread {1} (Display: {2}, Screen: {3}, Window: {4})... ",
                                          Handle, System.Threading.Thread.CurrentThread.ManagedThreadId, w.Display, w.Screen, w.WindowHandle));

                if (w.Display == IntPtr.Zero || w.WindowHandle == IntPtr.Zero || Handle == ContextHandle.Zero)
                {
                    throw new InvalidOperationException("Invalid display, window or context.");
                }

                result = Glx.MakeCurrent(w.Display, w.WindowHandle, Handle);

                if (!result)
                {
                    throw new GraphicsContextException("Failed to make context current.");
                }
                else
                {
                    Debug.WriteLine("done!");
                }
            }
        }
Example #3
0
        public override void MakeCurrent(IWindowInfo window)
        {
            if (window == currentWindow && IsCurrent)
            {
                return;
            }

            if (window != null && ((X11WindowInfo)window).Display != Display)
            {
                throw new InvalidOperationException("MakeCurrent() may only be called on windows originating from the same display that spawned this GL context.");
            }

            if (window == null)
            {
                Debug.Write(String.Format("Releasing context {0} from thread {1} (Display: {2})... ",
                                          Handle, System.Threading.Thread.CurrentThread.ManagedThreadId, Display));

                bool result;
                result = Glx.MakeCurrent(Display, IntPtr.Zero, IntPtr.Zero);
                if (result)
                {
                    currentWindow = null;
                }

                Debug.Print("{0}", result ? "done!" : "failed.");
            }
            else
            {
                X11WindowInfo w = (X11WindowInfo)window;
                bool          result;

                Debug.Write(String.Format("Making context {0} current on thread {1} (Display: {2}, Screen: {3}, Window: {4})... ",
                                          Handle, System.Threading.Thread.CurrentThread.ManagedThreadId, Display, w.Screen, w.Handle));

                if (Display == IntPtr.Zero || w.Handle == IntPtr.Zero || Handle == ContextHandle.Zero)
                {
                    throw new InvalidOperationException("Invalid display, window or context.");
                }

                result = Glx.MakeCurrent(Display, w.Handle, Handle);
                if (result)
                {
                    currentWindow = w;
                }

                if (!result)
                {
                    throw new GraphicsContextException("Failed to make context current.");
                }
                else
                {
                    Debug.WriteLine("done!");
                }
            }

            currentWindow = (X11WindowInfo)window;
        }
Example #4
0
 private void Dispose(bool manuallyCalled)
 {
     if (!this.IsDisposed && manuallyCalled)
     {
         IntPtr display = this.Display;
         if (this.IsCurrent)
         {
             using (new XLock(display))
                 Glx.MakeCurrent(display, IntPtr.Zero, IntPtr.Zero);
         }
         using (new XLock(display))
             Glx.DestroyContext(display, this.Handle);
     }
     this.IsDisposed = true;
 }
Example #5
0
 public override void MakeCurrent(IWindowInfo window)
 {
     if (window == this.currentWindow && this.IsCurrent)
     {
         return;
     }
     if (window != null && ((X11WindowInfo)window).Display != this.Display)
     {
         throw new InvalidOperationException("MakeCurrent() may only be called on windows originating from the same display that spawned this GL context.");
     }
     if (window == null)
     {
         using (new XLock(this.Display))
         {
             if (Glx.MakeCurrent(this.Display, IntPtr.Zero, IntPtr.Zero))
             {
                 this.currentWindow = (X11WindowInfo)null;
             }
         }
     }
     else
     {
         X11WindowInfo x11WindowInfo = (X11WindowInfo)window;
         if (this.Display == IntPtr.Zero || x11WindowInfo.WindowHandle == IntPtr.Zero || this.Handle == ContextHandle.Zero)
         {
             throw new InvalidOperationException("Invalid display, window or context.");
         }
         bool flag;
         using (new XLock(this.Display))
         {
             flag = Glx.MakeCurrent(this.Display, x11WindowInfo.WindowHandle, this.Handle);
             if (flag)
             {
                 this.currentWindow = x11WindowInfo;
             }
         }
         if (!flag)
         {
             throw new GraphicsContextException("Failed to make context current.");
         }
     }
     this.currentWindow = (X11WindowInfo)window;
 }
Example #6
0
        public X11GLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shared, bool direct,
                            int major, int minor, GraphicsContextFlags flags)
            : base(DesktopBackend.OpenGL)
        {
            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.");
                }
            }
        }
Example #7
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.");
            }
        }
Example #8
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);
        }
Example #9
0
 public static bool MakeCurrent(IntPtr display, IntPtr drawable, ContextHandle context)
 {
     return(Glx.MakeCurrent(display, drawable, context.Handle));
 }