2009-11-02 20:50:16 +00:00
|
|
|
#region --- License ---
|
2009-02-22 10:43:35 +00:00
|
|
|
/* Copyright (c) 2007 Stefanos Apostolopoulos
|
|
|
|
* See license.txt for license info
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.X11
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Provides methods to create and control an opengl context on the X11 platform.
|
|
|
|
/// This class supports OpenTK, and is not intended for use by OpenTK programs.
|
|
|
|
/// </summary>
|
2009-08-17 10:23:16 +00:00
|
|
|
internal sealed class X11GLContext : DesktopGraphicsContext
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-11-02 20:50:16 +00:00
|
|
|
#region Fields
|
|
|
|
|
|
|
|
// We assume that we cannot move a GL context to a different display connection.
|
|
|
|
// For this reason, we'll "lock" onto the display of the window used in the context
|
|
|
|
// constructor and we'll throw an exception if the user ever tries to make the context
|
|
|
|
// current on window originating from a different display.
|
|
|
|
IntPtr display;
|
2009-02-22 10:43:35 +00:00
|
|
|
X11WindowInfo currentWindow;
|
|
|
|
bool vsync_supported;
|
|
|
|
int vsync_interval;
|
2009-03-07 10:36:51 +00:00
|
|
|
bool glx_loaded;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-11-02 20:50:16 +00:00
|
|
|
#endregion
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
#region --- Constructors ---
|
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
public X11GLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shared, bool direct,
|
|
|
|
int major, int minor, GraphicsContextFlags flags)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-03-07 10:36:51 +00:00
|
|
|
if (mode == null)
|
|
|
|
throw new ArgumentNullException("mode");
|
|
|
|
if (window == null)
|
|
|
|
throw new ArgumentNullException("window");
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-10-19 18:11:32 +00:00
|
|
|
Mode = mode;
|
|
|
|
|
2009-11-02 20:50:16 +00:00
|
|
|
// Do not move this lower, as almost everything requires the Display
|
|
|
|
// property to be correctly set.
|
|
|
|
Display = ((X11WindowInfo)window).Display;
|
2009-11-02 22:37:13 +00:00
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
currentWindow = (X11WindowInfo)window;
|
2009-02-22 10:43:35 +00:00
|
|
|
currentWindow.VisualInfo = SelectVisual(mode, currentWindow);
|
2009-11-02 22:37:13 +00:00
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
ContextHandle shareHandle = shared != null ?
|
|
|
|
(shared as IGraphicsContextInternal).Context : (ContextHandle)IntPtr.Zero;
|
2009-11-02 22:37:13 +00:00
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
Debug.Write("Creating X11GLContext context: ");
|
|
|
|
Debug.Write(direct ? "direct, " : "indirect, ");
|
|
|
|
Debug.WriteLine(shareHandle.Handle == IntPtr.Zero ? "not shared... " :
|
|
|
|
String.Format("shared with ({0})... ", shareHandle));
|
2009-11-02 22:37:13 +00:00
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
if (!glx_loaded)
|
|
|
|
{
|
2009-03-20 23:16:52 +00:00
|
|
|
Debug.WriteLine("Creating temporary context to load GLX extensions.");
|
2009-03-07 10:36:51 +00:00
|
|
|
|
|
|
|
// Create a temporary context to obtain the necessary function pointers.
|
|
|
|
XVisualInfo visual = currentWindow.VisualInfo;
|
2009-11-02 20:50:16 +00:00
|
|
|
IntPtr ctx = Glx.CreateContext(Display, ref visual, IntPtr.Zero, true);
|
2009-03-07 10:36:51 +00:00
|
|
|
if (ctx == IntPtr.Zero)
|
2009-11-02 20:50:16 +00:00
|
|
|
ctx = Glx.CreateContext(Display, ref visual, IntPtr.Zero, false);
|
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
if (ctx != IntPtr.Zero)
|
|
|
|
{
|
2009-10-07 10:44:45 +00:00
|
|
|
new Glx().LoadAll();
|
2009-11-02 20:50:16 +00:00
|
|
|
Glx.MakeCurrent(Display, IntPtr.Zero, IntPtr.Zero);
|
|
|
|
//Glx.DestroyContext(Display, ctx);
|
2009-03-07 10:36:51 +00:00
|
|
|
glx_loaded = true;
|
|
|
|
}
|
|
|
|
}
|
2009-11-02 20:50:16 +00:00
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
// 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.
|
2009-03-20 23:16:52 +00:00
|
|
|
// 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)
|
2009-03-07 10:36:51 +00:00
|
|
|
{
|
2009-03-20 23:16:52 +00:00
|
|
|
Debug.Write("Using GLX_ARB_create_context... ");
|
2009-11-02 20:50:16 +00:00
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
// We need the FB config for the current GraphicsMode.
|
|
|
|
int count;
|
2009-11-02 20:50:16 +00:00
|
|
|
IntPtr* fbconfigs = Glx.ChooseFBConfig(Display, currentWindow.Screen,
|
|
|
|
new int[] {
|
|
|
|
(int)GLXAttribute.VISUAL_ID,
|
|
|
|
(int)mode.Index,
|
|
|
|
0
|
|
|
|
}, out count);
|
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
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);
|
|
|
|
}
|
2009-03-20 23:16:52 +00:00
|
|
|
attributes.Add(0);
|
2009-11-02 20:50:16 +00:00
|
|
|
|
|
|
|
Handle = new ContextHandle(Glx.Arb.CreateContextAttribs(Display, *fbconfigs,
|
2009-03-07 10:36:51 +00:00
|
|
|
shareHandle.Handle, direct, attributes.ToArray()));
|
2009-11-02 20:50:16 +00:00
|
|
|
|
2009-08-25 09:03:19 +00:00
|
|
|
if (Handle == ContextHandle.Zero)
|
2009-03-20 23:16:52 +00:00
|
|
|
{
|
|
|
|
Debug.Write(String.Format("failed. Trying direct: {0}... ", !direct));
|
2009-11-02 20:50:16 +00:00
|
|
|
Handle = new ContextHandle(Glx.Arb.CreateContextAttribs(Display, *fbconfigs,
|
2009-03-07 10:36:51 +00:00
|
|
|
shareHandle.Handle, !direct, attributes.ToArray()));
|
2009-03-20 23:16:52 +00:00
|
|
|
}
|
2009-11-02 20:50:16 +00:00
|
|
|
|
2009-08-25 09:03:19 +00:00
|
|
|
if (Handle == ContextHandle.Zero)
|
2009-03-20 23:16:52 +00:00
|
|
|
Debug.WriteLine("failed.");
|
|
|
|
else
|
|
|
|
Debug.WriteLine("success!");
|
2009-11-02 20:50:16 +00:00
|
|
|
|
2009-11-02 22:37:13 +00:00
|
|
|
using (new XLock(Display))
|
|
|
|
{
|
|
|
|
Functions.XFree((IntPtr)fbconfigs);
|
|
|
|
}
|
2009-03-07 10:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-02 20:50:16 +00:00
|
|
|
|
2009-08-25 09:03:19 +00:00
|
|
|
if (Handle == ContextHandle.Zero)
|
2009-03-07 10:36:51 +00:00
|
|
|
{
|
2009-03-20 23:16:52 +00:00
|
|
|
Debug.Write("Using legacy context creation... ");
|
2009-11-02 20:50:16 +00:00
|
|
|
|
|
|
|
XVisualInfo info = currentWindow.VisualInfo;
|
|
|
|
// Cannot pass a Property by reference.
|
|
|
|
Handle = new ContextHandle(Glx.CreateContext(Display, ref info, shareHandle.Handle, direct));
|
|
|
|
|
2009-08-25 09:03:19 +00:00
|
|
|
if (Handle == ContextHandle.Zero)
|
2009-03-20 23:16:52 +00:00
|
|
|
{
|
|
|
|
Debug.WriteLine(String.Format("failed. Trying direct: {0}... ", !direct));
|
2009-11-02 20:50:16 +00:00
|
|
|
Handle = new ContextHandle(Glx.CreateContext(Display, ref info, IntPtr.Zero, !direct));
|
2009-03-20 23:16:52 +00:00
|
|
|
}
|
2009-03-07 10:36:51 +00:00
|
|
|
}
|
2009-11-02 20:50:16 +00:00
|
|
|
|
2009-08-25 09:03:19 +00:00
|
|
|
if (Handle != ContextHandle.Zero)
|
|
|
|
Debug.Print("Context created (id: {0}).", Handle);
|
2009-03-07 10:36:51 +00:00
|
|
|
else
|
|
|
|
throw new GraphicsContextException("Failed to create OpenGL context. Glx.CreateContext call returned 0.");
|
2009-11-02 20:50:16 +00:00
|
|
|
|
|
|
|
if (!Glx.IsDirect(Display, Handle.Handle))
|
2009-03-20 23:16:52 +00:00
|
|
|
Debug.Print("Warning: Context is not direct.");
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region --- Private Methods ---
|
|
|
|
|
2009-11-02 20:50:16 +00:00
|
|
|
IntPtr Display
|
|
|
|
{
|
|
|
|
get { return display; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == IntPtr.Zero)
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
if (display != IntPtr.Zero)
|
|
|
|
throw new InvalidOperationException("The display connection may not be changed after being set.");
|
|
|
|
display = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
#region XVisualInfo SelectVisual(GraphicsMode mode, X11WindowInfo currentWindow)
|
|
|
|
|
|
|
|
XVisualInfo SelectVisual(GraphicsMode mode, X11WindowInfo currentWindow)
|
|
|
|
{
|
|
|
|
XVisualInfo info = new XVisualInfo();
|
2009-08-11 21:11:39 +00:00
|
|
|
info.VisualID = (IntPtr)mode.Index;
|
|
|
|
info.Screen = currentWindow.Screen;
|
2009-02-22 10:43:35 +00:00
|
|
|
int items;
|
|
|
|
|
|
|
|
lock (API.Lock)
|
|
|
|
{
|
2009-11-02 20:50:16 +00:00
|
|
|
IntPtr vs = Functions.XGetVisualInfo(Display, XVisualInfoMask.ID | XVisualInfoMask.Screen, ref info, out items);
|
2009-02-22 10:43:35 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
bool SupportsExtension(X11WindowInfo window, string e)
|
|
|
|
{
|
2009-11-02 20:50:16 +00:00
|
|
|
if (window == null)
|
|
|
|
throw new ArgumentNullException("window");
|
|
|
|
if (e == null)
|
|
|
|
throw new ArgumentNullException("e");
|
|
|
|
if (window.Display != Display)
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
|
|
|
string extensions = Glx.QueryExtensionsString(Display, window.Screen);
|
2009-02-22 10:43:35 +00:00
|
|
|
return !String.IsNullOrEmpty(extensions) && extensions.Contains(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region --- IGraphicsContext Members ---
|
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
#region SwapBuffers()
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override void SwapBuffers()
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-11-02 20:50:16 +00:00
|
|
|
if (Display == IntPtr.Zero || currentWindow.WindowHandle == IntPtr.Zero)
|
2009-02-22 10:43:35 +00:00
|
|
|
throw new InvalidOperationException(
|
2009-11-02 20:50:16 +00:00
|
|
|
String.Format("Window is invalid. Display ({0}), Handle ({1}).", Display, currentWindow.WindowHandle));
|
|
|
|
Glx.SwapBuffers(Display, currentWindow.WindowHandle);
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
#region MakeCurrent
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override void MakeCurrent(IWindowInfo window)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-11-02 20:50:16 +00:00
|
|
|
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.");
|
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
if (window == null)
|
|
|
|
{
|
2009-11-02 20:50:16 +00:00
|
|
|
Glx.MakeCurrent(Display, IntPtr.Zero, IntPtr.Zero);
|
2009-06-02 15:49:39 +00:00
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
else
|
2009-06-02 15:49:39 +00:00
|
|
|
{
|
|
|
|
X11WindowInfo w = (X11WindowInfo)window;
|
|
|
|
bool result;
|
2009-11-02 20:50:16 +00:00
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
Debug.Write(String.Format("Making context {0} current on thread {1} (Display: {2}, Screen: {3}, Window: {4})... ",
|
2009-11-02 20:50:16 +00:00
|
|
|
Handle, System.Threading.Thread.CurrentThread.ManagedThreadId, Display, w.Screen, w.WindowHandle));
|
|
|
|
|
|
|
|
if (Display == IntPtr.Zero || w.WindowHandle == IntPtr.Zero || Handle == ContextHandle.Zero)
|
2009-06-02 15:49:39 +00:00
|
|
|
throw new InvalidOperationException("Invalid display, window or context.");
|
2009-11-02 20:50:16 +00:00
|
|
|
|
|
|
|
result = Glx.MakeCurrent(Display, w.WindowHandle, Handle);
|
|
|
|
|
2009-06-02 15:49:39 +00:00
|
|
|
if (!result)
|
|
|
|
throw new GraphicsContextException("Failed to make context current.");
|
|
|
|
else
|
|
|
|
Debug.WriteLine("done!");
|
|
|
|
}
|
2009-11-02 20:50:16 +00:00
|
|
|
|
|
|
|
currentWindow = (X11WindowInfo)window;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
#region IsCurrent
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override bool IsCurrent
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-11-02 20:50:16 +00:00
|
|
|
get { return Glx.GetCurrentContext() == Handle.Handle; }
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
#region VSync
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override bool VSync
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2009-03-20 23:16:52 +00:00
|
|
|
return vsync_supported && vsync_interval != 0;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (vsync_supported)
|
|
|
|
{
|
2009-03-20 23:16:52 +00:00
|
|
|
ErrorCode error_code = Glx.Sgi.SwapInterval(value ? 1 : 0);
|
|
|
|
if (error_code != X11.ErrorCode.NO_ERROR)
|
|
|
|
Debug.Print("VSync = {0} failed, error code: {1}.", value, error_code);
|
2009-02-22 10:43:35 +00:00
|
|
|
vsync_interval = value ? 1 : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
#region GetAddress
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override IntPtr GetAddress(string function)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
return Glx.GetProcAddress(function);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region --- IGLContextInternal Members ---
|
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
#region LoadAll
|
2009-02-22 10:43:35 +00:00
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override void LoadAll()
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-10-07 10:44:45 +00:00
|
|
|
new Glx().LoadAll();
|
2009-03-20 23:16:52 +00:00
|
|
|
vsync_supported = this.GetAddress("glXSwapIntervalSGI") != IntPtr.Zero;
|
|
|
|
Debug.Print("Context supports vsync: {0}.", vsync_supported);
|
2009-10-21 10:16:32 +00:00
|
|
|
|
|
|
|
base.LoadAll();
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IWindowInfo IGLContextInternal.Info
|
|
|
|
|
|
|
|
//IWindowInfo IGraphicsContextInternal.Info { get { return window; } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region --- IDisposable Members ---
|
|
|
|
|
2009-08-17 10:23:16 +00:00
|
|
|
public override void Dispose()
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
|
|
|
this.Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Dispose(bool manuallyCalled)
|
|
|
|
{
|
2009-09-03 12:23:11 +00:00
|
|
|
if (!IsDisposed)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-06-02 15:49:39 +00:00
|
|
|
if (manuallyCalled)
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-11-02 20:50:16 +00:00
|
|
|
IntPtr display = Display;
|
|
|
|
if (IsCurrent)
|
|
|
|
MakeCurrent(null);
|
|
|
|
|
|
|
|
Glx.DestroyContext(display, Handle);
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
2009-06-02 15:49:39 +00:00
|
|
|
else
|
2009-02-22 10:43:35 +00:00
|
|
|
{
|
2009-06-02 15:49:39 +00:00
|
|
|
Debug.Print("[Warning] {0} leaked.", this.GetType().Name);
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
2009-09-03 12:23:11 +00:00
|
|
|
IsDisposed = true;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~X11GLContext()
|
|
|
|
{
|
|
|
|
this.Dispose(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|