Example #1
0
 /// <summary>
 /// Makes the current.
 /// </summary>
 /// <param name="xid">The xid.</param>
 /// <exception cref="OpenGlException">glXMakeContextCurrent failed</exception>
 public void MakeCurrent(IntPtr xid)
 {
     if (!Glx.MakeContextCurrent(_x11.Display, xid, xid, Handle))
     {
         throw new OpenGlException("glXMakeContextCurrent failed ");
     }
 }
        /// <summary>
        /// Creates the context.
        /// </summary>
        /// <param name="defaultXid">The default xid.</param>
        /// <param name="share">The share.</param>
        /// <returns>GlxContext.</returns>
        /// <exception cref="OpenGlException">Unable to create direct GLX context</exception>
        public GlxContext CreateContext(IntPtr defaultXid, IGlContext share)
        {
            var    sharelist = ((GlxContext)share)?.Handle ?? IntPtr.Zero;
            IntPtr handle    = default;

            foreach (var ver in new[]
            {
                new Version(4, 0), new Version(3, 2),
                new Version(3, 0), new Version(2, 0)
            })
            {
                var attrs = new[]
                {
                    GLX_CONTEXT_MAJOR_VERSION_ARB, ver.Major,
                    GLX_CONTEXT_MINOR_VERSION_ARB, ver.Minor,
                    GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
                    0
                };
                try
                {
                    handle = Glx.CreateContextAttribsARB(_x11.Display, _fbconfig, sharelist, true, attrs);
                    if (handle != IntPtr.Zero)
                    {
                        break;
                    }
                }
                catch
                {
                    break;
                }
            }

            if (handle == IntPtr.Zero)
            {
                throw new OpenGlException("Unable to create direct GLX context");
            }
            return(new GlxContext(new GlxInterface(), handle, this, _x11, defaultXid));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="GlxDisplay" /> class.
        /// </summary>
        /// <param name="x11">The X11.</param>
        /// <exception cref="OpenGlException">Unable to choose FBConfig
        /// or
        /// Unable to get visual info from FBConfig
        /// or
        /// GL version string is null, aborting
        /// or
        /// GL renderer string is null, aborting
        /// or
        /// Renderer '{GlInterface.Renderer}' is blacklisted by '{item}'</exception>
        public GlxDisplay(X11Info x11)
        {
            _x11 = x11;

            var baseAttribs = new[]
            {
                GLX_X_RENDERABLE, 1,
                GLX_RENDER_TYPE, GLX_RGBA_BIT,
                GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT | GLX_PBUFFER_BIT,
                GLX_DOUBLEBUFFER, 1,
                GLX_RED_SIZE, 8,
                GLX_GREEN_SIZE, 8,
                GLX_BLUE_SIZE, 8,
                GLX_ALPHA_SIZE, 8,
                GLX_DEPTH_SIZE, 1,
                GLX_STENCIL_SIZE, 8,
            };

            foreach (var attribs in new[]
            {
                //baseAttribs.Concat(multiattribs),
                baseAttribs,
            })
            {
                var ptr = Glx.ChooseFBConfig(_x11.Display, x11.DefaultScreen,
                                             attribs, out var count);
                for (var c = 0; c < count; c++)
                {
                    var visual = Glx.GetVisualFromFBConfig(_x11.Display, ptr[c]);
                    // We prefer 32 bit visuals
                    if (_fbconfig == IntPtr.Zero || visual->depth == 32)
                    {
                        _fbconfig  = ptr[c];
                        VisualInfo = visual;
                        if (visual->depth == 32)
                        {
                            break;
                        }
                    }
                }

                if (_fbconfig != IntPtr.Zero)
                {
                    break;
                }
            }

            if (_fbconfig == IntPtr.Zero)
            {
                throw new OpenGlException("Unable to choose FBConfig");
            }

            if (VisualInfo == null)
            {
                throw new OpenGlException("Unable to get visual info from FBConfig");
            }
            if (Glx.GetFBConfigAttrib(_x11.Display, _fbconfig, GLX_SAMPLES, out var samples) == 0)
            {
                SampleCount = samples;
            }
            if (Glx.GetFBConfigAttrib(_x11.Display, _fbconfig, GLX_STENCIL_SIZE, out var stencil) == 0)
            {
                StencilSize = stencil;
            }

            var pbuffers = Enumerable.Range(0, 2).Select(_ => Glx.CreatePbuffer(_x11.Display, _fbconfig, new[]
            {
                GLX_PBUFFER_WIDTH, 1, GLX_PBUFFER_HEIGHT, 1, 0
            })).ToList();

            XLib.XFlush(_x11.Display);

            ImmediateContext = CreateContext(pbuffers[0], null);
            DeferredContext  = CreateContext(pbuffers[1], ImmediateContext);
            ImmediateContext.MakeCurrent();
            var err = Glx.GetError();

            GlInterface = new GlInterface(GlxInterface.SafeGetProcAddress);
            if (GlInterface.Version == null)
            {
                throw new OpenGlException("GL version string is null, aborting");
            }
            if (GlInterface.Renderer == null)
            {
                throw new OpenGlException("GL renderer string is null, aborting");
            }

            if (Environment.GetEnvironmentVariable("AVALONIA_GLX_IGNORE_RENDERER_BLACKLIST") != "1")
            {
                var blacklist = AvaloniaLocator.Current.GetService <X11PlatformOptions>()
                                ?.GlxRendererBlacklist;
                if (blacklist != null)
                {
                    foreach (var item in blacklist)
                    {
                        if (GlInterface.Renderer.Contains(item))
                        {
                            throw new OpenGlException($"Renderer '{GlInterface.Renderer}' is blacklisted by '{item}'");
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Swaps the buffers.
 /// </summary>
 /// <param name="xid">The xid.</param>
 public void SwapBuffers(IntPtr xid) => Glx.SwapBuffers(_x11.Display, xid);
 /// <summary>
 /// Clears the context.
 /// </summary>
 public void ClearContext() => Glx.MakeContextCurrent(_x11.Display,
                                                      IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
Example #6
0
 public override void Dispose()
 {
     Glx.glXMakeCurrent(display, visual, IntPtr.Zero);
     Glx.glXDestroyContext(display, handle);
 }
Example #7
0
 public override void Swap()
 {
     Glx.glXSwapBuffers(display, visual);
 }
Example #8
0
 public override void MakeCurrent()
 {
     Glx.glXMakeCurrent(display, visual, handle);
 }
Example #9
0
        public override void CreateContext(IntPtr wnd_handle, IntPtr drawable, OpenGLContext share)
        {
            // Force loading of OpenGL library
            // This is later used by the OpenGL class to implement extensions.
            //linker = GetLinker();

            display = API.OpenDisplay(IntPtr.Zero);
            if (display == IntPtr.Zero)
            {
                throw new InvalidOperationException("Cannot connect to X server");
            }

            wnd = wnd_handle;
            int[] attr =
            {
                Glx.GLX_X_RENDERABLE,                   1,
                Glx.GLX_DRAWABLE_TYPE, Glx.GLX_WINDOW_BIT,
                Glx.GLX_RENDER_TYPE,   Glx.GLX_RGBA_BIT,
                Glx.GLX_X_VISUAL_TYPE, Glx.GLX_TRUE_COLOR,
                Glx.GLX_RED_SIZE,                       8,
                Glx.GLX_GREEN_SIZE,                     8,
                Glx.GLX_BLUE_SIZE,                      8,
                Glx.GLX_ALPHA_SIZE,                     0,
                Glx.GLX_DEPTH_SIZE,                    16,
                //Glx.GLX_STENCIL_SIZE    , 8,
                Glx.GLX_DOUBLEBUFFER,                   1,
                0, 0
            };

            API.MapWindow(display, wnd);
            int def_screen = API.DefaultScreen(display);

            Console.WriteLine("Display: 0x" + display.ToString("x"));
            Console.WriteLine("Deault screen: 0x" + def_screen.ToString("x"));
            int[] elems = new int[] { 0 };

            //IntPtr fb = Glx.glXChooseFBConfig(display, def_screen, attr, elems);

            //if (fb == IntPtr.Zero)
            //throw new InvalidOperationException("Could not retrieve framebuffer configuration");

            Console.WriteLine(elems[0]);

            //var info_ptr = Glx.glXGetVisualFromFBConfig(display, Marshal.ReadIntPtr(fb));
            attr = new int[] { GLX_RGBA, GLX_DEPTH_SIZE, 32, GLX_DOUBLEBUFFER, 0 };
            //IntPtr info_ptr = Glx.glXChooseVisual(display, def_screen, attr);
            var info_ptr = API.DefaultVisual(display, def_screen);

            if (info_ptr == IntPtr.Zero)
            {
                throw new NotSupportedException("OpenGL is not supported.");
            }

            VisualInfo info = new VisualInfo();

            Marshal.PtrToStructure(info_ptr, info);
            visual = info_ptr;



            handle = Glx.glXCreateContext(display, visual, IntPtr.Zero, true);

            if (handle == IntPtr.Zero)
            {
                throw new InvalidOperationException("Unable to create OpenGL context");
            }

            Glx.glXMakeCurrent(display, visual, handle);

            int[] parameters = new int[]
            {
                GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
                GLX_CONTEXT_MINOR_VERSION_ARB, 2,
                0
            };

            glXCreateContextAttribsARB = GetProc <XCreateContextAttribsARBProc>("glXCreateContextAttribsARB");

            if (glXCreateContextAttribsARB == null)
            {
                throw new NotSupportedException("OpenGL 3.0 not supported.");
            }

            IntPtr new_handle = glXCreateContextAttribsARB(display, info_ptr, share.Handle, true, parameters);

            if (new_handle == IntPtr.Zero)
            {
                throw new NotSupportedException("OpenGL 3.0 not supported.");
            }

            Glx.glXMakeCurrent(display, visual, IntPtr.Zero);
            Glx.glXDestroyContext(display, handle);
            Glx.glXMakeCurrent(display, visual, new_handle);
            handle = new_handle;

            Console.WriteLine("glX Initialization succeded.");
        }