2009-02-22 10:43:35 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* 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>
|
|
|
|
|
internal sealed class X11GLContext : IGraphicsContext, IGraphicsContextInternal
|
|
|
|
|
{
|
|
|
|
|
ContextHandle context;
|
|
|
|
|
X11WindowInfo currentWindow;
|
|
|
|
|
bool vsync_supported;
|
|
|
|
|
int vsync_interval;
|
2009-03-07 10:36:51 +00:00
|
|
|
|
bool glx_loaded;
|
2009-03-23 11:41:11 +00:00
|
|
|
|
GraphicsMode graphics_mode;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
|
|
bool disposed;
|
|
|
|
|
|
|
|
|
|
#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-03-07 10:36:51 +00:00
|
|
|
|
currentWindow = (X11WindowInfo)window;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
currentWindow.VisualInfo = SelectVisual(mode, currentWindow);
|
|
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
|
ContextHandle shareHandle = shared != null ?
|
|
|
|
|
(shared as IGraphicsContextInternal).Context : (ContextHandle)IntPtr.Zero;
|
2009-02-22 10:43:35 +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-02-22 10:43:35 +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;
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
Glx.LoadAll();
|
|
|
|
|
Glx.MakeCurrent(currentWindow.Display, IntPtr.Zero, IntPtr.Zero);
|
2009-03-20 23:16:52 +00:00
|
|
|
|
//Glx.DestroyContext(currentWindow.Display, ctx);
|
2009-03-07 10:36:51 +00:00
|
|
|
|
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.
|
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-03-07 10:36:51 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2009-03-20 23:16:52 +00:00
|
|
|
|
attributes.Add(0);
|
2009-03-07 10:36:51 +00:00
|
|
|
|
|
|
|
|
|
context = new ContextHandle(Glx.Arb.CreateContextAttribs(currentWindow.Display, *fbconfigs,
|
|
|
|
|
shareHandle.Handle, direct, attributes.ToArray()));
|
|
|
|
|
|
|
|
|
|
if (context == ContextHandle.Zero)
|
2009-03-20 23:16:52 +00:00
|
|
|
|
{
|
|
|
|
|
Debug.Write(String.Format("failed. Trying direct: {0}... ", !direct));
|
2009-03-07 10:36:51 +00:00
|
|
|
|
context = new ContextHandle(Glx.Arb.CreateContextAttribs(currentWindow.Display, *fbconfigs,
|
|
|
|
|
shareHandle.Handle, !direct, attributes.ToArray()));
|
2009-03-20 23:16:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context == ContextHandle.Zero)
|
|
|
|
|
Debug.WriteLine("failed.");
|
|
|
|
|
else
|
|
|
|
|
Debug.WriteLine("success!");
|
2009-03-07 10:36:51 +00:00
|
|
|
|
|
|
|
|
|
Functions.XFree((IntPtr)fbconfigs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context == ContextHandle.Zero)
|
|
|
|
|
{
|
2009-03-20 23:16:52 +00:00
|
|
|
|
Debug.Write("Using legacy context creation... ");
|
2009-03-07 10:36:51 +00:00
|
|
|
|
|
|
|
|
|
XVisualInfo info = currentWindow.VisualInfo; // Cannot pass a Property by reference.
|
|
|
|
|
context = new ContextHandle(Glx.CreateContext(currentWindow.Display, ref info, shareHandle.Handle, direct));
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
|
if (context == ContextHandle.Zero)
|
2009-03-20 23:16:52 +00:00
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine(String.Format("failed. Trying direct: {0}... ", !direct));
|
2009-03-07 10:36:51 +00:00
|
|
|
|
context = new ContextHandle(Glx.CreateContext(currentWindow.Display, ref info, IntPtr.Zero, !direct));
|
2009-03-20 23:16:52 +00:00
|
|
|
|
}
|
2009-03-07 10:36:51 +00:00
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
|
if (context != ContextHandle.Zero)
|
2009-03-20 23:16:52 +00:00
|
|
|
|
Debug.Print("Context created (id: {0}).", context);
|
2009-03-07 10:36:51 +00:00
|
|
|
|
else
|
|
|
|
|
throw new GraphicsContextException("Failed to create OpenGL context. Glx.CreateContext call returned 0.");
|
2009-03-20 23:16:52 +00:00
|
|
|
|
|
|
|
|
|
if (!Glx.IsDirect(currentWindow.Display, context.Handle))
|
|
|
|
|
Debug.Print("Warning: Context is not direct.");
|
2009-03-23 11:41:11 +00:00
|
|
|
|
|
|
|
|
|
graphics_mode = mode;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Private Methods ---
|
|
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
{
|
|
|
|
|
IntPtr vs = Functions.XGetVisualInfo(currentWindow.Display, XVisualInfoMask.ID | XVisualInfoMask.Screen, ref info, out items);
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
string extensions = Glx.QueryExtensionsString(window.Display, window.Screen);
|
|
|
|
|
return !String.IsNullOrEmpty(extensions) && extensions.Contains(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IGraphicsContext Members ---
|
|
|
|
|
|
|
|
|
|
#region public void SwapBuffers()
|
|
|
|
|
|
|
|
|
|
public void SwapBuffers()
|
|
|
|
|
{
|
|
|
|
|
//if (window == null) throw new ArgumentNullException("window", "Must point to a valid window.");
|
|
|
|
|
//X11WindowInfo w = (X11WindowInfo)window;
|
|
|
|
|
if (currentWindow.Display == IntPtr.Zero || currentWindow.WindowHandle == IntPtr.Zero)
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
String.Format("Window is invalid. Display ({0}), Handle ({1}).", currentWindow.Display, currentWindow.WindowHandle));
|
|
|
|
|
Glx.SwapBuffers(currentWindow.Display, currentWindow.WindowHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public void MakeCurrent(IWindowInfo window)
|
|
|
|
|
|
|
|
|
|
public void MakeCurrent(IWindowInfo window)
|
|
|
|
|
{
|
2009-06-02 15:49:39 +00:00
|
|
|
|
if (window == null)
|
|
|
|
|
{
|
|
|
|
|
Glx.MakeCurrent(currentWindow.Display, IntPtr.Zero, IntPtr.Zero);
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
else
|
2009-06-02 15:49:39 +00:00
|
|
|
|
{
|
|
|
|
|
X11WindowInfo w = (X11WindowInfo)window;
|
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
|
|
Debug.Write(String.Format("Making context {0} current on thread {1} (Display: {2}, Screen: {3}, Window: {4})... ",
|
|
|
|
|
context, System.Threading.Thread.CurrentThread.ManagedThreadId, w.Display, w.Screen, w.WindowHandle));
|
|
|
|
|
|
|
|
|
|
if (w.Display == IntPtr.Zero || w.WindowHandle == IntPtr.Zero || context == ContextHandle.Zero)
|
|
|
|
|
throw new InvalidOperationException("Invalid display, window or context.");
|
|
|
|
|
|
|
|
|
|
result = Glx.MakeCurrent(w.Display, w.WindowHandle, context);
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
throw new GraphicsContextException("Failed to make context current.");
|
|
|
|
|
else
|
|
|
|
|
Debug.WriteLine("done!");
|
|
|
|
|
}
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public bool IsCurrent
|
|
|
|
|
|
|
|
|
|
public bool IsCurrent
|
|
|
|
|
{
|
|
|
|
|
get { return Glx.GetCurrentContext() == this.context.Handle; }
|
|
|
|
|
//set
|
|
|
|
|
//{
|
|
|
|
|
// if (value)
|
|
|
|
|
// Glx.MakeCurrent(window.Display, window.Handle, context);
|
|
|
|
|
// else
|
|
|
|
|
// Glx.MakeCurrent(window.Handle, IntPtr.Zero, IntPtr.Zero);
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public bool VSync
|
|
|
|
|
|
|
|
|
|
public bool VSync
|
|
|
|
|
{
|
|
|
|
|
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-06-04 11:21:33 +00:00
|
|
|
|
[Obsolete]
|
2009-02-22 10:43:35 +00:00
|
|
|
|
public event DestroyEvent<IGraphicsContext> Destroy;
|
|
|
|
|
|
|
|
|
|
#region public IntPtr GetAddress(string function)
|
|
|
|
|
|
|
|
|
|
public IntPtr GetAddress(string function)
|
|
|
|
|
{
|
|
|
|
|
return Glx.GetProcAddress(function);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public void Update
|
|
|
|
|
public void Update(IWindowInfo window)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2009-03-23 11:41:11 +00:00
|
|
|
|
#region public DisplayMode Mode
|
|
|
|
|
|
|
|
|
|
GraphicsMode IGraphicsContext.GraphicsMode
|
|
|
|
|
{
|
|
|
|
|
get { return graphics_mode; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2009-06-04 11:21:33 +00:00
|
|
|
|
[Obsolete]
|
2009-02-22 10:43:35 +00:00
|
|
|
|
public void RegisterForDisposal(IDisposable resource)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException("Use OpenTK.GraphicsContext instead.");
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-04 11:21:33 +00:00
|
|
|
|
[Obsolete]
|
2009-02-22 10:43:35 +00:00
|
|
|
|
public void DisposeResources()
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException("Use OpenTK.GraphicsContext instead.");
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-29 21:41:30 +00:00
|
|
|
|
public bool ErrorChecking
|
|
|
|
|
{
|
|
|
|
|
get { throw new NotImplementedException(); }
|
|
|
|
|
set { throw new NotImplementedException(); }
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IGLContextInternal Members ---
|
|
|
|
|
|
2009-03-07 10:36:51 +00:00
|
|
|
|
#region Implementation
|
|
|
|
|
|
|
|
|
|
IGraphicsContext IGraphicsContextInternal.Implementation
|
|
|
|
|
{
|
|
|
|
|
get { return this; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
#region void LoadAll()
|
|
|
|
|
|
|
|
|
|
void IGraphicsContextInternal.LoadAll()
|
|
|
|
|
{
|
2009-08-14 13:13:28 +00:00
|
|
|
|
OpenTK.Graphics.OpenGL.GL.LoadAll();
|
2009-02-22 10:43:35 +00:00
|
|
|
|
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-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ContextHandle IGLContextInternal.Context
|
|
|
|
|
|
|
|
|
|
ContextHandle IGraphicsContextInternal.Context
|
|
|
|
|
{
|
|
|
|
|
get { return context; }
|
|
|
|
|
/*private set { context = value; }*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IWindowInfo IGLContextInternal.Info
|
|
|
|
|
|
|
|
|
|
//IWindowInfo IGraphicsContextInternal.Info { get { return window; } }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Methods ---
|
|
|
|
|
|
|
|
|
|
void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
if (Destroy != null)
|
|
|
|
|
Destroy(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IDisposable Members ---
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Dispose(bool manuallyCalled)
|
|
|
|
|
{
|
|
|
|
|
if (!disposed)
|
|
|
|
|
{
|
2009-06-02 15:49:39 +00:00
|
|
|
|
if (manuallyCalled)
|
2009-02-22 10:43:35 +00:00
|
|
|
|
{
|
2009-06-02 15:49:39 +00:00
|
|
|
|
if (GraphicsContext.CurrentContext != null &&
|
|
|
|
|
((IGraphicsContextInternal)GraphicsContext.CurrentContext).Context == context)
|
|
|
|
|
GraphicsContext.CurrentContext.MakeCurrent(null);
|
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
|
Glx.DestroyContext(currentWindow.Display, context);
|
|
|
|
|
}
|
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-06-02 15:49:39 +00:00
|
|
|
|
disposed = true;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~X11GLContext()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|