private IntPtr SelectFBConfig(GraphicsMode mode) { Debug.Print("Selecting FB config for {0}", mode); var visualAttributes = new List <int>(); if (mode.ColorFormat.BitsPerPixel > 0) { if (!mode.ColorFormat.IsIndexed) { visualAttributes.Add((int)GLXAttribute.RENDER_TYPE); visualAttributes.Add((int)GLXRenderTypeMask.RGBA_BIT); } visualAttributes.Add((int)GLXAttribute.RED_SIZE); visualAttributes.Add(mode.ColorFormat.Red); visualAttributes.Add((int)GLXAttribute.GREEN_SIZE); visualAttributes.Add(mode.ColorFormat.Green); visualAttributes.Add((int)GLXAttribute.BLUE_SIZE); visualAttributes.Add(mode.ColorFormat.Blue); visualAttributes.Add((int)GLXAttribute.ALPHA_SIZE); visualAttributes.Add(mode.ColorFormat.Alpha); } if (mode.Depth > 0) { visualAttributes.Add((int)GLXAttribute.DEPTH_SIZE); visualAttributes.Add(mode.Depth); } if (mode.Buffers > 1) { visualAttributes.Add((int)GLXAttribute.DOUBLEBUFFER); visualAttributes.Add(1); } if (mode.Stereo) { visualAttributes.Add((int)GLXAttribute.STENCIL_SIZE); visualAttributes.Add(mode.Stereo ? 1 : 0); } if (mode.AccumulatorFormat.BitsPerPixel > 0) { visualAttributes.Add((int)GLXAttribute.ACCUM_ALPHA_SIZE); visualAttributes.Add(mode.AccumulatorFormat.Alpha); visualAttributes.Add((int)GLXAttribute.ACCUM_BLUE_SIZE); visualAttributes.Add(mode.AccumulatorFormat.Blue); visualAttributes.Add((int)GLXAttribute.ACCUM_GREEN_SIZE); visualAttributes.Add(mode.AccumulatorFormat.Green); visualAttributes.Add((int)GLXAttribute.ACCUM_RED_SIZE); visualAttributes.Add(mode.AccumulatorFormat.Red); } if (mode.Samples > 0) { visualAttributes.Add((int)GLXAttribute.SAMPLE_BUFFERS); visualAttributes.Add(1); visualAttributes.Add((int)GLXAttribute.SAMPLES); visualAttributes.Add(mode.Samples); } if (mode.Stereo) { visualAttributes.Add((int)GLXAttribute.STEREO); visualAttributes.Add(1); } visualAttributes.Add(0); // Select a visual that matches the parameters set by the user. var display = API.DefaultDisplay; var result = IntPtr.Zero; using (new XLock(display)) { try { var screen = Functions.XDefaultScreen(display); var root = Functions.XRootWindow(display, screen); Debug.Print("Display: {0}, Screen: {1}, RootWindow: {2}", display, screen, root); unsafe { Debug.Print("Getting FB config."); int fbcount; // Note that ChooseFBConfig returns an array of GLXFBConfig opaque structures (i.e. mapped to IntPtrs). var fbconfigs = Glx.ChooseFBConfig(display, screen, visualAttributes.ToArray(), out fbcount); if (fbcount > 0 && fbconfigs != null) { // We want to use the first GLXFBConfig from the fbconfigs array (the first one is the best match). Debug.Print("Selected FB config: {0}", *fbconfigs); result = *fbconfigs; Functions.XFree((IntPtr)fbconfigs); } else { Debug.Print("No matching FB config found."); } } } catch (EntryPointNotFoundException) { Debug.Print("Function glXChooseFBConfig not supported."); } } return(result); }
private unsafe IntPtr SelectVisualUsingFBConfig(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) { List <int> list = new List <int>(); IntPtr num1 = IntPtr.Zero; if (color.BitsPerPixel > 0) { if (!color.IsIndexed) { list.Add(4); list.Add(1); } list.Add(8); list.Add(color.Red); list.Add(9); list.Add(color.Green); list.Add(10); list.Add(color.Blue); list.Add(11); list.Add(color.Alpha); } if (depth > 0) { list.Add(12); list.Add(depth); } if (buffers > 1) { list.Add(5); list.Add(1); } if (stencil > 1) { list.Add(13); list.Add(stencil); } if (accum.BitsPerPixel > 0) { list.Add(17); list.Add(accum.Alpha); list.Add(16); list.Add(accum.Blue); list.Add(15); list.Add(accum.Green); list.Add(14); list.Add(accum.Red); } if (samples > 0) { list.Add(100000); list.Add(1); list.Add(100001); list.Add(samples); } if (stereo) { list.Add(6); list.Add(1); } list.Add(0); IntPtr defaultDisplay = API.DefaultDisplay; using (new XLock(defaultDisplay)) { try { int num2 = Functions.XDefaultScreen(defaultDisplay); Functions.XRootWindow(defaultDisplay, num2); int fbount; IntPtr *numPtr = Glx.ChooseFBConfig(defaultDisplay, num2, list.ToArray(), out fbount); if (fbount > 0) { if ((IntPtr)numPtr != IntPtr.Zero) { num1 = Glx.GetVisualFromFBConfig(defaultDisplay, *numPtr); Functions.XFree((IntPtr)((void *)numPtr)); } } } catch (EntryPointNotFoundException ex) { return(IntPtr.Zero); } } return(num1); }
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."); } } }
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."); } }
// See http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.opengl/doc/openglrf/glXChooseFBConfig.htm // for the attribute declarations. Note that the attributes are different than those used in Glx.ChooseVisual. IntPtr SelectVisualUsingFBConfig(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) { List <int> visualAttributes = new List <int>(); IntPtr visual = IntPtr.Zero; Debug.Print("Bits per pixel: {0}", color.BitsPerPixel); if (color.BitsPerPixel > 0) { if (!color.IsIndexed) { visualAttributes.Add((int)GLXAttribute.RGBA); visualAttributes.Add(1); } visualAttributes.Add((int)GLXAttribute.RED_SIZE); visualAttributes.Add(color.Red); visualAttributes.Add((int)GLXAttribute.GREEN_SIZE); visualAttributes.Add(color.Green); visualAttributes.Add((int)GLXAttribute.BLUE_SIZE); visualAttributes.Add(color.Blue); visualAttributes.Add((int)GLXAttribute.ALPHA_SIZE); visualAttributes.Add(color.Alpha); } Debug.Print("Depth: {0}", depth); if (depth > 0) { visualAttributes.Add((int)GLXAttribute.DEPTH_SIZE); visualAttributes.Add(depth); } if (buffers > 1) { visualAttributes.Add((int)GLXAttribute.DOUBLEBUFFER); visualAttributes.Add(1); } if (stencil > 1) { visualAttributes.Add((int)GLXAttribute.STENCIL_SIZE); visualAttributes.Add(stencil); } if (accum.BitsPerPixel > 0) { visualAttributes.Add((int)GLXAttribute.ACCUM_ALPHA_SIZE); visualAttributes.Add(accum.Alpha); visualAttributes.Add((int)GLXAttribute.ACCUM_BLUE_SIZE); visualAttributes.Add(accum.Blue); visualAttributes.Add((int)GLXAttribute.ACCUM_GREEN_SIZE); visualAttributes.Add(accum.Green); visualAttributes.Add((int)GLXAttribute.ACCUM_RED_SIZE); visualAttributes.Add(accum.Red); } if (samples > 0) { visualAttributes.Add((int)GLXAttribute.SAMPLE_BUFFERS); visualAttributes.Add(1); visualAttributes.Add((int)GLXAttribute.SAMPLES); visualAttributes.Add(samples); } if (stereo) { visualAttributes.Add((int)GLXAttribute.STEREO); visualAttributes.Add(1); } visualAttributes.Add(0); // Select a visual that matches the parameters set by the user. IntPtr display = API.DefaultDisplay; using (new XLock(display)) { try { int screen = Functions.XDefaultScreen(display); IntPtr root = Functions.XRootWindow(display, screen); Debug.Print("Display: {0}, Screen: {1}, RootWindow: {2}", display, screen, root); unsafe { Debug.Print("Getting FB config."); int fbcount; // Note that ChooseFBConfig returns an array of GLXFBConfig opaque structures (i.e. mapped to IntPtrs). IntPtr *fbconfigs = Glx.ChooseFBConfig(display, screen, visualAttributes.ToArray(), out fbcount); if (fbcount > 0 && fbconfigs != null) { // We want to use the first GLXFBConfig from the fbconfigs array (the first one is the best match). visual = Glx.GetVisualFromFBConfig(display, *fbconfigs); Functions.XFree((IntPtr)fbconfigs); } } } catch (EntryPointNotFoundException) { Debug.Print("Function glXChooseFBConfig not supported."); return(IntPtr.Zero); } } return(visual); }
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); }