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;
|
|
|
|
|
|
|
|
|
|
using OpenTK.OpenGL;
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
/// This class supports OpenTK, and is not intended for use by OpenTK programs.
|
2007-08-05 13:42:31 +00:00
|
|
|
|
/// </summary>
|
2007-09-09 11:52:09 +00:00
|
|
|
|
internal sealed class X11GLContext : OpenTK.Platform.IGLContext
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
private IntPtr context;
|
2007-08-21 10:48:32 +00:00
|
|
|
|
private DisplayMode mode;
|
2007-09-09 11:52:09 +00:00
|
|
|
|
private WindowInfo windowInfo;
|
|
|
|
|
private IntPtr visual;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
private bool disposed;
|
|
|
|
|
|
2007-09-09 11:52:09 +00:00
|
|
|
|
#region --- Constructors ---
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-09-09 11:52:09 +00:00
|
|
|
|
internal X11GLContext() : this(new DisplayMode(), new WindowInfo())
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-09 11:52:09 +00:00
|
|
|
|
internal X11GLContext(DisplayMode mode) : this(mode, new WindowInfo())
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-09-09 11:52:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
public X11GLContext(DisplayMode mode, IWindowInfo info)
|
2007-09-09 11:52:09 +00:00
|
|
|
|
{
|
|
|
|
|
if (info == null)
|
|
|
|
|
throw new ArgumentException("IWindowInfo cannot be null.");
|
|
|
|
|
|
|
|
|
|
this.windowInfo = info as WindowInfo;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
this.mode = mode;
|
2007-09-09 11:52:09 +00:00
|
|
|
|
this.ChooseContext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
//#region private XVisualInfo VisualInfo
|
2007-09-09 11:52:09 +00:00
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
//private XVisualInfo VisualInfo
|
|
|
|
|
//{
|
|
|
|
|
// get { return windowInfo.VisualInfo; }
|
|
|
|
|
//}
|
2007-09-09 11:52:09 +00:00
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
//#endregion
|
2007-09-09 11:52:09 +00:00
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
//#region private IntPtr Handle
|
2007-09-09 11:52:09 +00:00
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
//private IntPtr Handle
|
|
|
|
|
//{
|
|
|
|
|
// get { return windowInfo.Handle; }
|
|
|
|
|
// set { windowInfo.Handle = value; }
|
|
|
|
|
//}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
//#endregion
|
2007-08-20 12:25:48 +00:00
|
|
|
|
|
2007-09-09 11:52:09 +00:00
|
|
|
|
#region private void PrepareContext()
|
2007-08-20 12:25:48 +00:00
|
|
|
|
|
2007-09-09 11:52:09 +00:00
|
|
|
|
private void ChooseContext()
|
2007-08-20 12:25:48 +00:00
|
|
|
|
{
|
|
|
|
|
List<int> visualAttributes = new List<int>();
|
2007-08-21 10:48:32 +00:00
|
|
|
|
|
|
|
|
|
if (mode == null)
|
|
|
|
|
{
|
|
|
|
|
// Define the bare essentials - needed for compatibility with Mono's System.Windows.Forms
|
|
|
|
|
Debug.Print("Preparing visual for System.Windows.Forms (compatibility mode)");
|
2007-09-02 22:52:00 +00:00
|
|
|
|
|
2007-08-21 10:48:32 +00:00
|
|
|
|
visualAttributes.Add((int)Glx.Enums.GLXAttribute.RGBA);
|
|
|
|
|
visualAttributes.Add((int)Glx.Enums.GLXAttribute.DEPTH_SIZE);
|
|
|
|
|
visualAttributes.Add((int)1);
|
|
|
|
|
visualAttributes.Add((int)Glx.Enums.GLXAttribute.DOUBLEBUFFER);
|
|
|
|
|
visualAttributes.Add((int)0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("Preparing visual for DisplayMode: {0}", mode.ToString());
|
|
|
|
|
|
|
|
|
|
visualAttributes.Add((int)Glx.Enums.GLXAttribute.RGBA);
|
|
|
|
|
visualAttributes.Add((int)Glx.Enums.GLXAttribute.RED_SIZE);
|
|
|
|
|
visualAttributes.Add((int)mode.Color.Red);
|
|
|
|
|
visualAttributes.Add((int)Glx.Enums.GLXAttribute.GREEN_SIZE);
|
|
|
|
|
visualAttributes.Add((int)mode.Color.Green);
|
|
|
|
|
visualAttributes.Add((int)Glx.Enums.GLXAttribute.BLUE_SIZE);
|
|
|
|
|
visualAttributes.Add((int)mode.Color.Blue);
|
|
|
|
|
visualAttributes.Add((int)Glx.Enums.GLXAttribute.ALPHA_SIZE);
|
|
|
|
|
visualAttributes.Add((int)mode.Color.Alpha);
|
|
|
|
|
visualAttributes.Add((int)Glx.Enums.GLXAttribute.DEPTH_SIZE);
|
|
|
|
|
visualAttributes.Add((int)mode.DepthBits);
|
|
|
|
|
visualAttributes.Add((int)Glx.Enums.GLXAttribute.DOUBLEBUFFER);
|
|
|
|
|
visualAttributes.Add((int)0);
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
visual = Glx.ChooseVisual(windowInfo.Display, windowInfo.Screen, visualAttributes.ToArray());
|
|
|
|
|
if (visual == IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException(String.Format("Requested DisplayMode not available ({0}).", mode.ToString()));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2007-09-02 00:40:43 +00:00
|
|
|
|
windowInfo.VisualInfo = (XVisualInfo)Marshal.PtrToStructure(visual, typeof(XVisualInfo));
|
2007-08-20 12:25:48 +00:00
|
|
|
|
Debug.Print("Prepared visual: {0}", windowInfo.VisualInfo.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
#region --- IGLContext Members ---
|
|
|
|
|
|
|
|
|
|
#region public IntPtr Context
|
2007-08-20 12:25:48 +00:00
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
public IntPtr Context
|
2007-08-20 12:25:48 +00:00
|
|
|
|
{
|
2007-09-09 15:10:21 +00:00
|
|
|
|
get { return context; }
|
|
|
|
|
private set { context = value; }
|
|
|
|
|
}
|
2007-08-20 12:25:48 +00:00
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
#endregion
|
2007-08-20 12:25:48 +00:00
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
#region public DisplayMode Mode
|
2007-08-20 12:25:48 +00:00
|
|
|
|
|
2007-09-09 15:10:21 +00:00
|
|
|
|
public DisplayMode Mode
|
|
|
|
|
{
|
|
|
|
|
get { return mode; }
|
|
|
|
|
private set
|
2007-08-20 12:25:48 +00:00
|
|
|
|
{
|
2007-09-09 15:10:21 +00:00
|
|
|
|
if (context == IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
mode = value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("Cannot change DisplayMode of an existing context.");
|
|
|
|
|
}
|
2007-08-20 12:25:48 +00:00
|
|
|
|
}
|
2007-09-09 15:10:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public IWindowInfo Info
|
|
|
|
|
|
|
|
|
|
public IWindowInfo Info { get { return windowInfo; } }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public void CreateContext()
|
|
|
|
|
{
|
|
|
|
|
this.CreateContext(true, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreateContext(bool direct)
|
|
|
|
|
{
|
|
|
|
|
this.CreateContext(direct, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region public void CreateContext(bool direct, IGLContext shareContext)
|
|
|
|
|
|
|
|
|
|
public void CreateContext(bool direct, IGLContext shareContext)
|
|
|
|
|
{
|
|
|
|
|
try
|
2007-08-20 12:25:48 +00:00
|
|
|
|
{
|
2007-09-09 15:10:21 +00:00
|
|
|
|
Debug.WriteLine("Creating opengl context.");
|
|
|
|
|
Debug.Indent();
|
|
|
|
|
|
|
|
|
|
IntPtr shareHandle = shareContext != null ? (shareContext as X11GLContext).Context: IntPtr.Zero;
|
|
|
|
|
|
|
|
|
|
Debug.Write(direct ? "Context is direct, " : "Context is indirect, ");
|
|
|
|
|
Debug.WriteLine(shareHandle == IntPtr.Zero ? "not shared." :
|
|
|
|
|
String.Format("shared with ({0}).", shareHandle));
|
|
|
|
|
|
|
|
|
|
// Try to call Glx.CreateContext with the specified parameters.
|
|
|
|
|
context = Glx.CreateContext(windowInfo.Display, visual, shareHandle, direct);
|
|
|
|
|
|
|
|
|
|
// Context creation succeeded, return.
|
|
|
|
|
if (context != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("New opengl context created. (id: {0})", context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Context creation failed. Retry with a non-shared context with the
|
|
|
|
|
// direct/indirect rendering mode flipped.
|
|
|
|
|
Debug.Print("Cotnext creation failed, retrying with a non-shared, {0} context.",
|
|
|
|
|
!direct ? "direct" : "indirect");
|
|
|
|
|
context = Glx.CreateContext(windowInfo.Display, visual, IntPtr.Zero, !direct);
|
|
|
|
|
if (context != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("New opengl context created. (id: {0})", context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
throw new ApplicationException("Glx.CreateContext call failed (returned 0).");
|
|
|
|
|
}
|
2007-09-09 15:10:21 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Debug.Unindent();
|
|
|
|
|
}
|
2007-08-20 12:25:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
#region public void SwapBuffers()
|
|
|
|
|
|
|
|
|
|
public void SwapBuffers()
|
|
|
|
|
{
|
2007-08-05 13:42:31 +00:00
|
|
|
|
Glx.SwapBuffers(windowInfo.Display, windowInfo.Handle);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public void MakeCurrent()
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
bool result;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
public void MakeCurrent()
|
|
|
|
|
{
|
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})... ",
|
|
|
|
|
context, System.Threading.Thread.CurrentThread.ManagedThreadId, windowInfo.Display, windowInfo.Screen, windowInfo.Handle));
|
|
|
|
|
|
|
|
|
|
if (windowInfo.Display != IntPtr.Zero && windowInfo.Handle != IntPtr.Zero && context != IntPtr.Zero)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
result = Glx.MakeCurrent(windowInfo.Display, windowInfo.Handle, context);
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("failed.");
|
|
|
|
|
// probably need to recreate context here.
|
|
|
|
|
//throw new ApplicationException(String.Format("Failed to make context {0} current on thread {1}.",
|
|
|
|
|
// context, System.Threading.Thread.CurrentThread.ManagedThreadId));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("done!");
|
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public bool IsCurrent()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region public IntPtr GetAddress(string function)
|
|
|
|
|
|
|
|
|
|
public IntPtr GetAddress(string function)
|
|
|
|
|
{
|
|
|
|
|
return Glx.GetProcAddress(function);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public IEnumerable<DisplayMode> GetDisplayModes()
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("The method or operation is not implemented.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- IDisposable Members ---
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Dispose(bool manuallyCalled)
|
|
|
|
|
{
|
|
|
|
|
if (!disposed)
|
|
|
|
|
{
|
|
|
|
|
// Clean unmanaged resources:
|
2007-08-20 12:25:48 +00:00
|
|
|
|
Glx.MakeCurrent(windowInfo.Display, IntPtr.Zero, IntPtr.Zero);
|
|
|
|
|
Glx.DestroyContext(windowInfo.Display, context);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
API.Free(visual);
|
|
|
|
|
|
|
|
|
|
if (manuallyCalled)
|
|
|
|
|
{
|
|
|
|
|
// Safe to clean managed resources, too
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
disposed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~X11GLContext()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|