2007-07-23 00:15:18 +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;
|
|
|
|
|
|
2008-02-02 00:58:26 +00:00
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
2008-01-31 14:22:37 +00:00
|
|
|
|
using OpenTK.Graphics;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.X11
|
|
|
|
|
{
|
2007-08-05 13:42:31 +00:00
|
|
|
|
/// <summary>
|
2007-09-09 11:52:09 +00:00
|
|
|
|
/// Provides methods to create and control an opengl context on the X11 platform.
|
2007-12-09 18:15:51 +00:00
|
|
|
|
/// This class supports OpenTK, and is not intended for use by OpenTK programs.
|
2007-08-05 13:42:31 +00:00
|
|
|
|
/// </summary>
|
2008-03-01 13:15:31 +00:00
|
|
|
|
internal sealed class X11GLContext : IGraphicsContext, IGraphicsContextInternal
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2008-03-01 13:15:31 +00:00
|
|
|
|
IntPtr context;
|
2008-03-03 12:44:56 +00:00
|
|
|
|
//X11WindowInfo window;
|
|
|
|
|
X11WindowInfo currentWindow;
|
|
|
|
|
//IntPtr visual;
|
2008-03-01 13:15:31 +00:00
|
|
|
|
bool vsync_supported;
|
|
|
|
|
int vsync_interval;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2008-03-01 13:15:31 +00:00
|
|
|
|
bool disposed;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-09-09 11:52:09 +00:00
|
|
|
|
#region --- Constructors ---
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2008-03-01 13:15:31 +00:00
|
|
|
|
static X11GLContext()
|
|
|
|
|
{
|
|
|
|
|
// Set the GetCurrentContext implementation.
|
|
|
|
|
if (GraphicsContext.GetCurrentContext == null)
|
|
|
|
|
GraphicsContext.GetCurrentContext = X11GLContext.GetCurrentContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal X11GLContext(GraphicsMode mode, IWindowInfo info, IGraphicsContext shared, bool directRendering)
|
|
|
|
|
{
|
|
|
|
|
//if (mode == null) mode = GraphicsMode.Default;
|
|
|
|
|
if (info == null) throw new ArgumentNullException("info", "Should point to a valid window.");
|
|
|
|
|
|
2008-03-03 12:44:56 +00:00
|
|
|
|
currentWindow = (X11WindowInfo)info;
|
|
|
|
|
currentWindow.VisualInfo = SelectVisual(mode, currentWindow);
|
2008-01-31 13:15:17 +00:00
|
|
|
|
|
2008-03-03 12:44:56 +00:00
|
|
|
|
Debug.Print("Chose visual: {0}", currentWindow.VisualInfo);
|
|
|
|
|
|
|
|
|
|
CreateContext(shared, directRendering, currentWindow);
|
2008-03-01 13:15:31 +00:00
|
|
|
|
}
|
2007-09-09 11:52:09 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-03-01 13:15:31 +00:00
|
|
|
|
#region --- Private Methods ---
|
2007-08-20 12:25:48 +00:00
|
|
|
|
|
2008-03-03 12:44:56 +00:00
|
|
|
|
#region XVisualInfo SelectVisual(GraphicsMode mode, X11WindowInfo currentWindow)
|
2008-01-11 20:23:41 +00:00
|
|
|
|
|
2008-03-03 12:44:56 +00:00
|
|
|
|
XVisualInfo SelectVisual(GraphicsMode mode, X11WindowInfo currentWindow)
|
2007-08-20 12:25:48 +00:00
|
|
|
|
{
|
2008-03-03 12:44:56 +00:00
|
|
|
|
XVisualInfo info = new XVisualInfo();
|
|
|
|
|
info.visualid = (IntPtr)mode.Index;
|
|
|
|
|
info.screen = currentWindow.Screen;
|
2008-04-04 21:08:09 +00:00
|
|
|
|
int items;
|
|
|
|
|
|
|
|
|
|
lock (API.Lock)
|
2008-03-26 19:43:57 +00:00
|
|
|
|
{
|
|
|
|
|
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));
|
2008-04-04 21:08:09 +00:00
|
|
|
|
Functions.XFree(vs);
|
|
|
|
|
}
|
2008-03-03 12:44:56 +00:00
|
|
|
|
|
|
|
|
|
return info;
|
2007-09-09 15:10:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-01-31 13:15:17 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2008-03-01 13:15:31 +00:00
|
|
|
|
#region void CreateContext(IGraphicsContext shareContext, bool direct)
|
2007-09-09 15:10:21 +00:00
|
|
|
|
|
2008-03-03 12:44:56 +00:00
|
|
|
|
void CreateContext(IGraphicsContext shareContext, bool direct, X11WindowInfo window)
|
2007-09-09 15:10:21 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
2007-08-20 12:25:48 +00:00
|
|
|
|
{
|
2008-03-03 12:44:56 +00:00
|
|
|
|
ContextHandle shareHandle = shareContext != null ? (shareContext as IGraphicsContextInternal).Context :
|
|
|
|
|
(ContextHandle)IntPtr.Zero;
|
2007-09-09 15:10:21 +00:00
|
|
|
|
|
2008-03-01 13:15:31 +00:00
|
|
|
|
Debug.Write("Creating OpenGL context: ");
|
|
|
|
|
Debug.Write(direct ? "direct, " : "indirect, ");
|
|
|
|
|
Debug.Write(shareHandle.Handle == IntPtr.Zero ? "not shared... " :
|
|
|
|
|
String.Format("shared with ({0})... ", shareHandle));
|
2008-04-04 21:08:09 +00:00
|
|
|
|
|
|
|
|
|
lock (API.Lock)
|
|
|
|
|
{
|
2008-03-26 19:43:57 +00:00
|
|
|
|
XVisualInfo info = window.VisualInfo; // Cannot pass a Property by reference.
|
|
|
|
|
context = Glx.CreateContext(window.Display, ref info, shareHandle.Handle, direct);
|
|
|
|
|
|
|
|
|
|
// Context creation succeeded, return.
|
|
|
|
|
if (context != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("done! (id: {0})", context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Context creation failed. Retry with a non-shared context with the direct/indirect bit flipped.
|
|
|
|
|
Debug.Print("failed.");
|
|
|
|
|
Debug.Write(String.Format("Creating OpenGL context: {0}, not shared... ", !direct ? "direct" : "indirect"));
|
|
|
|
|
context = Glx.CreateContext(window.Display, ref info, IntPtr.Zero, !direct);
|
|
|
|
|
if (context != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("done! (id: {0})", context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-04-04 21:08:09 +00:00
|
|
|
|
}
|
2008-01-23 00:18:52 +00:00
|
|
|
|
|
2008-03-01 13:15:31 +00:00
|
|
|
|
Debug.Print("failed.");
|
|
|
|
|
throw new GraphicsModeException("Failed to create OpenGL context. Glx.CreateContext call returned 0.");
|
2007-08-20 12:25:48 +00:00
|
|
|
|
}
|
2007-09-09 15:10:21 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Debug.Unindent();
|
|
|
|
|
}
|
2007-08-20 12:25:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-03-03 12:44:56 +00:00
|
|
|
|
bool SupportsExtension(X11WindowInfo window, string e)
|
2008-03-01 13:15:31 +00:00
|
|
|
|
{
|
|
|
|
|
string extensions = Glx.QueryExtensionsString(window.Display, window.Screen);
|
|
|
|
|
return !String.IsNullOrEmpty(extensions) && extensions.Contains(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IGraphicsContext Members ---
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
#region public void SwapBuffers()
|
|
|
|
|
|
|
|
|
|
public void SwapBuffers()
|
|
|
|
|
{
|
2008-03-03 12:44:56 +00:00
|
|
|
|
//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);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-03-03 12:44:56 +00:00
|
|
|
|
#region public void MakeCurrent(IWindowInfo window)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2008-03-03 12:44:56 +00:00
|
|
|
|
public void MakeCurrent(IWindowInfo window)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2008-03-03 12:44:56 +00:00
|
|
|
|
X11WindowInfo w = (X11WindowInfo)window;
|
|
|
|
|
bool result;
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
Debug.Write(String.Format("Making context {0} current on thread {1} (Display: {2}, Screen: {3}, Window: {4})... ",
|
2008-03-03 12:44:56 +00:00
|
|
|
|
context, System.Threading.Thread.CurrentThread.ManagedThreadId, w.Display, w.Screen, w.WindowHandle));
|
2007-08-20 12:25:48 +00:00
|
|
|
|
|
2008-03-03 12:44:56 +00:00
|
|
|
|
if (w.Display == IntPtr.Zero || w.WindowHandle == IntPtr.Zero || context == IntPtr.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!");
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-15 12:47:51 +00:00
|
|
|
|
#region public bool IsCurrent
|
|
|
|
|
|
2007-12-09 18:15:51 +00:00
|
|
|
|
public bool IsCurrent
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2008-01-15 12:47:51 +00:00
|
|
|
|
get { return Glx.GetCurrentContext() == this.context; }
|
2008-03-03 12:44:56 +00:00
|
|
|
|
//set
|
|
|
|
|
//{
|
|
|
|
|
// if (value)
|
|
|
|
|
// Glx.MakeCurrent(window.Display, window.Handle, context);
|
|
|
|
|
// else
|
|
|
|
|
// Glx.MakeCurrent(window.Handle, IntPtr.Zero, IntPtr.Zero);
|
|
|
|
|
//}
|
2007-12-09 18:15:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-01-15 12:47:51 +00:00
|
|
|
|
#endregion
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2008-01-23 00:18:52 +00:00
|
|
|
|
#region public bool VSync
|
|
|
|
|
|
|
|
|
|
public bool VSync
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return vsync_supported && vsync_interval > 0;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (vsync_supported)
|
2008-04-04 21:08:09 +00:00
|
|
|
|
{
|
2008-01-23 00:18:52 +00:00
|
|
|
|
int error_code = Glx.Sgi.SwapInterval(value ? 1 : 0);
|
|
|
|
|
if (error_code != 0)
|
2008-04-04 21:08:09 +00:00
|
|
|
|
throw new GraphicsException(String.Format("Could not set vsync, error code: {0}", error_code));
|
2008-01-23 00:18:52 +00:00
|
|
|
|
vsync_interval = value ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-31 13:17:42 +00:00
|
|
|
|
public event DestroyEvent<IGraphicsContext> Destroy;
|
2007-12-09 18:15:51 +00:00
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
#region public IntPtr GetAddress(string function)
|
|
|
|
|
|
|
|
|
|
public IntPtr GetAddress(string function)
|
|
|
|
|
{
|
|
|
|
|
return Glx.GetProcAddress(function);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-12-09 18:15:51 +00:00
|
|
|
|
public void RegisterForDisposal(IDisposable resource)
|
|
|
|
|
{
|
2008-01-31 14:39:54 +00:00
|
|
|
|
throw new NotSupportedException("Use OpenTK.GraphicsContext instead.");
|
2007-12-09 18:15:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DisposeResources()
|
|
|
|
|
{
|
2008-01-31 14:39:54 +00:00
|
|
|
|
throw new NotSupportedException("Use OpenTK.GraphicsContext instead.");
|
2007-12-09 18:15:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-15 12:47:51 +00:00
|
|
|
|
#region --- IGLContextInternal Members ---
|
|
|
|
|
|
2008-03-01 13:15:31 +00:00
|
|
|
|
#region void LoadAll()
|
|
|
|
|
|
|
|
|
|
void IGraphicsContextInternal.LoadAll()
|
|
|
|
|
{
|
|
|
|
|
GL.LoadAll();
|
|
|
|
|
Glu.LoadAll();
|
|
|
|
|
Glx.LoadAll();
|
2008-03-03 12:44:56 +00:00
|
|
|
|
vsync_supported = this.SupportsExtension(currentWindow, "SGI_swap_control") &&
|
|
|
|
|
this.GetAddress("glXSwapControlSGI") != IntPtr.Zero;
|
2008-03-01 13:15:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-02-28 13:57:07 +00:00
|
|
|
|
#region public DisplayMode Mode
|
|
|
|
|
|
2008-03-01 13:15:31 +00:00
|
|
|
|
GraphicsMode IGraphicsContextInternal.GraphicsMode
|
2008-02-28 13:57:07 +00:00
|
|
|
|
{
|
2008-03-01 13:15:31 +00:00
|
|
|
|
get { return null; }
|
2008-02-28 13:57:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-15 12:47:51 +00:00
|
|
|
|
#region IntPtr IGLContextInternal.Context
|
|
|
|
|
|
2008-03-01 13:15:31 +00:00
|
|
|
|
ContextHandle IGraphicsContextInternal.Context
|
2008-01-15 12:47:51 +00:00
|
|
|
|
{
|
|
|
|
|
get { return context; }
|
|
|
|
|
/*private set { context = value; }*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IWindowInfo IGLContextInternal.Info
|
|
|
|
|
|
2008-03-03 12:44:56 +00:00
|
|
|
|
//IWindowInfo IGraphicsContextInternal.Info { get { return window; } }
|
2008-01-15 12:47:51 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-31 13:15:17 +00:00
|
|
|
|
#region --- Methods ---
|
2008-01-20 19:29:42 +00:00
|
|
|
|
|
|
|
|
|
void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
if (Destroy != null)
|
|
|
|
|
Destroy(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-31 13:15:17 +00:00
|
|
|
|
#region static ContextHandle GetCurrentContext()
|
|
|
|
|
|
|
|
|
|
static ContextHandle GetCurrentContext()
|
2008-04-04 21:08:09 +00:00
|
|
|
|
{
|
2008-01-31 13:15:17 +00:00
|
|
|
|
return (ContextHandle)Glx.GetCurrentContext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-20 19:29:42 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
#region --- IDisposable Members ---
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Dispose(bool manuallyCalled)
|
|
|
|
|
{
|
|
|
|
|
if (!disposed)
|
2008-04-04 21:08:09 +00:00
|
|
|
|
{
|
2007-07-23 00:15:18 +00:00
|
|
|
|
// Clean unmanaged resources:
|
2008-04-04 21:08:09 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2008-03-26 19:43:57 +00:00
|
|
|
|
Functions.XLockDisplay(currentWindow.Display);
|
|
|
|
|
Glx.MakeCurrent(currentWindow.Display, IntPtr.Zero, IntPtr.Zero);
|
2008-04-04 21:08:09 +00:00
|
|
|
|
Glx.DestroyContext(currentWindow.Display, context);
|
|
|
|
|
//Functions.XFree(visual);
|
2008-03-26 19:43:57 +00:00
|
|
|
|
}
|
2008-04-04 21:08:09 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Functions.XUnlockDisplay(currentWindow.Display);
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
if (manuallyCalled)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
disposed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~X11GLContext()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|