Provides access to GLX functions.
Example #1
0
        private bool SupportsExtension(X11WindowInfo window, string e)
        {
            if (window == null)
            {
                throw new ArgumentNullException("window");
            }
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            if (window.Display != this.Display)
            {
                throw new InvalidOperationException();
            }
            string str = (string)null;

            using (new XLock(this.Display))
                str = Glx.QueryExtensionsString(this.Display, window.Screen);
            if (!string.IsNullOrEmpty(str))
            {
                return(str.Contains(e));
            }
            else
            {
                return(false);
            }
        }
Example #2
0
 public override void SwapBuffers()
 {
     if (this.Display == IntPtr.Zero || this.currentWindow.WindowHandle == IntPtr.Zero)
     {
         throw new InvalidOperationException(string.Format("Window is invalid. Display ({0}), Handle ({1}).", (object)this.Display, (object)this.currentWindow.WindowHandle));
     }
     using (new XLock(this.Display))
         Glx.SwapBuffers(this.Display, this.currentWindow.WindowHandle);
 }
Example #3
0
        /// <summary>
        /// Creates a System.Delegate that can be used to call a dynamically exported OpenGL function.
        /// </summary>
        /// <param name="name">The name of the OpenGL function (eg. "glNewList")</param>
        /// <param name="signature">The signature of the OpenGL function.</param>
        /// <returns>
        /// A System.Delegate that can be used to call this OpenGL function or null
        /// if the function is not available in the current OpenGL context.
        /// </returns>
        private static Delegate GetExtensionDelegate(string name, Type signature)
        {
            IntPtr address = Glx.GetProcAddress(name);

            if (address == IntPtr.Zero ||
                address == new IntPtr(1) ||     // Workaround for buggy nvidia drivers which return
                address == new IntPtr(2))       // 1 or 2 instead of IntPtr.Zero for some extensions.
                return null;
            else
                return Marshal.GetDelegateForFunctionPointer(address, signature);
        }
Example #4
0
        private unsafe static ContextHandle CreateContextAttribs(
            IntPtr display, int screen, IntPtr fbconfig,
            bool direct, int major, int minor,
            GraphicsContextFlags flags, ContextHandle shareContext)
        {
            Debug.Write("Using GLX_ARB_create_context... ");
            IntPtr context = IntPtr.Zero;

            {
                // We need the FB config for the current GraphicsMode.
                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)GetARBContextFlags(flags));
                    attributes.Add((int)ArbCreateContext.ProfileMask);
                    attributes.Add((int)GetARBProfileFlags(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))
                {
                    fixed(int *attrib = attributes.ToArray())
                    {
                        context = Glx.pglXCreateContextAttribsARB(display, fbconfig, shareContext.Handle, direct, attrib);
                        if (context == IntPtr.Zero)
                        {
                            Debug.Write(String.Format("failed. Trying direct: {0}... ", !direct));
                            context = Glx.pglXCreateContextAttribsARB(display, fbconfig, shareContext.Handle, !direct, attrib);
                        }
                    }
                }

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

            return(new ContextHandle(context));
        }
Example #5
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 #6
0
        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(new ColorFormat(r, g, b, a), depth, stencil, buffers));
        }
Example #7
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 #8
0
        protected override void Dispose(bool manuallyCalled)
        {
            if (!IsDisposed)
            {
                if (manuallyCalled)
                {
                    IntPtr display = Display;

                    if (IsCurrent)
                    {
                        Glx.glXMakeCurrent(display, IntPtr.Zero, IntPtr.Zero);
                    }
                    Glx.glXDestroyContext(display, ContextHandle);
                }
            }
            else
            {
                Debug.Print("[Warning] {0} leaked.", this.GetType().Name);
            }
            IsDisposed = true;
        }
        public X11GLContext(GraphicsMode mode, X11Window window)
        {
            Debug.Print("Creating X11GLContext context: ");
            cur = window;
            XVisualInfo info = cur.VisualInfo;

            // Cannot pass a Property by reference.
            IntPtr display = API.DefaultDisplay;

            ContextHandle = Glx.glXCreateContext(display, ref info, IntPtr.Zero, true);

            if (ContextHandle == IntPtr.Zero)
            {
                Debug.Print("failed. Trying indirect... ");
                ContextHandle = Glx.glXCreateContext(display, ref info, IntPtr.Zero, false);
            }

            if (ContextHandle != IntPtr.Zero)
            {
                Debug.Print("Context created (id: {0}).", ContextHandle);
            }
            else
            {
                throw new GraphicsContextException("Context creation failed. Error: NULL returned");
            }

            if (!Glx.glXIsDirect(display, ContextHandle))
            {
                Debug.Print("Warning: Context is not direct.");
            }
            if (!Glx.glXMakeCurrent(API.DefaultDisplay, window.WinHandle, ContextHandle))
            {
                throw new GraphicsContextException("Failed to make context current.");
            }

            Glx.LoadEntryPoints();
            vsync_supported = Glx.glXSwapIntervalSGI != null;
        }