2009-11-02 23:37:13 +01:00
|
|
|
#region --- License ---
|
2009-02-22 11:43:35 +01:00
|
|
|
/* Licensed under the MIT/X11 license.
|
|
|
|
* Copyright (c) 2006-2008 the OpenTK Team.
|
|
|
|
* This notice may not be removed from any source distribution.
|
|
|
|
* See license.txt for licensing detailed licensing details.
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.X11
|
|
|
|
{
|
2014-07-19 20:39:17 +02:00
|
|
|
class X11GraphicsMode
|
2009-09-25 10:28:51 +02:00
|
|
|
{
|
|
|
|
// Todo: Add custom visual selection algorithm, instead of ChooseFBConfig/ChooseVisual.
|
2009-07-19 18:09:46 +02:00
|
|
|
// It seems the Choose* methods do not take multisampling into account (at least on some
|
|
|
|
// drivers).
|
|
|
|
|
2009-03-07 08:16:46 +01:00
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
public X11GraphicsMode()
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-03-07 08:16:46 +01:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IGraphicsMode Members
|
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
public GraphicsMode SelectGraphicsMode(GraphicsMode desired_mode, out IntPtr visual, out IntPtr fbconfig)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-11-02 23:37:13 +01:00
|
|
|
GraphicsMode gfx;
|
2014-07-19 20:39:17 +02:00
|
|
|
GraphicsMode mode = new GraphicsMode(desired_mode);
|
|
|
|
visual = IntPtr.Zero;
|
|
|
|
fbconfig = IntPtr.Zero;
|
2009-03-20 20:38:52 +01:00
|
|
|
IntPtr display = API.DefaultDisplay;
|
2013-12-27 13:01:21 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Try to select a visual using Glx.ChooseFBConfig and Glx.GetVisualFromFBConfig.
|
|
|
|
// This is only supported on GLX 1.3 - if it fails, fall back to Glx.ChooseVisual.
|
2014-07-19 20:39:17 +02:00
|
|
|
fbconfig = SelectFBConfig(mode);
|
|
|
|
if (fbconfig != IntPtr.Zero)
|
|
|
|
visual = Glx.GetVisualFromFBConfig(display, fbconfig);
|
2013-12-27 13:01:21 +01:00
|
|
|
|
|
|
|
if (visual == IntPtr.Zero)
|
2014-07-19 20:39:17 +02:00
|
|
|
visual = SelectVisual(mode);
|
2013-12-27 13:01:21 +01:00
|
|
|
|
|
|
|
if (visual == IntPtr.Zero)
|
|
|
|
{
|
|
|
|
// Relax parameters and retry
|
2014-07-19 20:39:17 +02:00
|
|
|
if (!Utilities.RelaxGraphicsMode(ref mode))
|
2014-01-22 10:49:27 +01:00
|
|
|
throw new GraphicsModeException("Requested GraphicsMode not available.");
|
2013-12-27 13:01:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (visual == IntPtr.Zero);
|
|
|
|
|
2009-03-20 20:38:52 +01:00
|
|
|
XVisualInfo info = (XVisualInfo)Marshal.PtrToStructure(visual, typeof(XVisualInfo));
|
2014-07-19 20:39:17 +02:00
|
|
|
gfx = CreateGraphicsMode(display, ref info);
|
|
|
|
return gfx;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Private Members
|
2013-12-27 13:01:21 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
static GraphicsMode CreateGraphicsMode(IntPtr display, ref XVisualInfo info)
|
|
|
|
{
|
2009-03-20 20:38:52 +01:00
|
|
|
// See what we *really* got:
|
|
|
|
int r, g, b, a;
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.ALPHA_SIZE, out a);
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.RED_SIZE, out r);
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.GREEN_SIZE, out g);
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.BLUE_SIZE, out b);
|
|
|
|
int ar, ag, ab, aa;
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.ACCUM_ALPHA_SIZE, out aa);
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.ACCUM_RED_SIZE, out ar);
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.ACCUM_GREEN_SIZE, out ag);
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.ACCUM_BLUE_SIZE, out ab);
|
2014-07-19 20:39:17 +02:00
|
|
|
int depth, stencil, samples, buffers;
|
2009-03-20 20:38:52 +01:00
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.DEPTH_SIZE, out depth);
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.STENCIL_SIZE, out stencil);
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.SAMPLES, out samples);
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.DOUBLEBUFFER, out buffers);
|
|
|
|
int st;
|
|
|
|
Glx.GetConfig(display, ref info, GLXAttribute.STEREO, out st);
|
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
// Note: Glx.GetConfig return buffers = 0 (false) or 1 (true).
|
|
|
|
// OpenTK expects buffers = 1 (single-) or 2 (double-buffering),
|
|
|
|
// so increase the GLX value by one.
|
|
|
|
return new GraphicsMode(info.VisualID, new ColorFormat(r, g, b, a), depth, stencil, samples,
|
|
|
|
new ColorFormat(ar, ag, ab, aa), buffers + 1, st != 0);
|
|
|
|
}
|
2009-03-20 20:38:52 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
IntPtr SelectFBConfig(GraphicsMode mode)
|
2009-03-20 20:38:52 +01:00
|
|
|
{
|
2014-07-19 20:39:17 +02:00
|
|
|
Debug.Print("Selecting FB config for {0}", mode);
|
|
|
|
|
2008-03-24 14:12:02 +01:00
|
|
|
List<int> visualAttributes = new List<int>();
|
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.ColorFormat.BitsPerPixel > 0)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2014-07-19 20:39:17 +02:00
|
|
|
if (!mode.ColorFormat.IsIndexed)
|
2009-03-20 20:38:52 +01:00
|
|
|
{
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add((int)GLXAttribute.RENDER_TYPE);
|
|
|
|
visualAttributes.Add((int)GLXRenderTypeMask.RGBA_BIT);
|
2009-03-20 20:38:52 +01:00
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.RED_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.ColorFormat.Red);
|
2009-02-22 11:43:35 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.GREEN_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.ColorFormat.Green);
|
2009-02-22 11:43:35 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.BLUE_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.ColorFormat.Blue);
|
2009-02-22 11:43:35 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.ALPHA_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.ColorFormat.Alpha);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
2008-03-24 14:12:02 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.Depth > 0)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
|
|
|
visualAttributes.Add((int)GLXAttribute.DEPTH_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.Depth);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.Buffers > 1)
|
2009-03-20 20:38:52 +01:00
|
|
|
{
|
2009-02-22 11:43:35 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.DOUBLEBUFFER);
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add(1);
|
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.Stereo)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
|
|
|
visualAttributes.Add((int)GLXAttribute.STENCIL_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.Stereo ? 1 : 0);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.AccumulatorFormat.BitsPerPixel > 0)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
|
|
|
visualAttributes.Add((int)GLXAttribute.ACCUM_ALPHA_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.AccumulatorFormat.Alpha);
|
2009-02-22 11:43:35 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.ACCUM_BLUE_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.AccumulatorFormat.Blue);
|
2009-02-22 11:43:35 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.ACCUM_GREEN_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.AccumulatorFormat.Green);
|
2009-02-22 11:43:35 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.ACCUM_RED_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.AccumulatorFormat.Red);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
2014-07-19 20:39:17 +02:00
|
|
|
|
|
|
|
if (mode.Samples > 0)
|
2009-07-19 18:09:46 +02:00
|
|
|
{
|
|
|
|
visualAttributes.Add((int)GLXAttribute.SAMPLE_BUFFERS);
|
|
|
|
visualAttributes.Add(1);
|
|
|
|
visualAttributes.Add((int)GLXAttribute.SAMPLES);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.Samples);
|
2009-07-19 18:09:46 +02:00
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.Stereo)
|
2009-03-20 20:38:52 +01:00
|
|
|
{
|
2009-02-22 11:43:35 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.STEREO);
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add(1);
|
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add(0);
|
2008-03-24 14:12:02 +01:00
|
|
|
|
|
|
|
// Select a visual that matches the parameters set by the user.
|
2009-03-20 20:38:52 +01:00
|
|
|
IntPtr display = API.DefaultDisplay;
|
2014-07-19 20:39:17 +02:00
|
|
|
IntPtr result = IntPtr.Zero;
|
2010-03-11 23:53:11 +01:00
|
|
|
using (new XLock(display))
|
2008-03-26 20:43:57 +01:00
|
|
|
{
|
2010-03-11 23:53:11 +01:00
|
|
|
try
|
2009-03-07 08:16:46 +01:00
|
|
|
{
|
2010-03-11 23:53:11 +01:00
|
|
|
int screen = Functions.XDefaultScreen(display);
|
|
|
|
IntPtr root = Functions.XRootWindow(display, screen);
|
|
|
|
Debug.Print("Display: {0}, Screen: {1}, RootWindow: {2}", display, screen, root);
|
2014-07-19 20:39:17 +02:00
|
|
|
|
2010-03-11 23:53:11 +01:00
|
|
|
unsafe
|
2009-03-07 08:16:46 +01:00
|
|
|
{
|
2010-03-11 23:53:11 +01:00
|
|
|
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).
|
2014-07-19 20:39:17 +02:00
|
|
|
Debug.Print("Selected FB config: {0}", *fbconfigs);
|
|
|
|
result = *fbconfigs;
|
2010-03-11 23:53:11 +01:00
|
|
|
Functions.XFree((IntPtr)fbconfigs);
|
|
|
|
}
|
2014-07-19 20:39:17 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Print("No matching FB config found.");
|
|
|
|
}
|
2009-03-07 08:16:46 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-11 23:53:11 +01:00
|
|
|
catch (EntryPointNotFoundException)
|
|
|
|
{
|
|
|
|
Debug.Print("Function glXChooseFBConfig not supported.");
|
|
|
|
}
|
2009-09-25 10:28:51 +02:00
|
|
|
}
|
2009-03-07 08:16:46 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
return result;
|
2009-03-20 20:38:52 +01:00
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
IntPtr SelectVisual(GraphicsMode mode)
|
2009-03-20 20:38:52 +01:00
|
|
|
{
|
2014-07-19 20:39:17 +02:00
|
|
|
Debug.Print("Selecting FB config for {0}", mode);
|
2009-03-20 20:38:52 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
List<int> visualAttributes = new List<int>();
|
2009-03-20 20:38:52 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.ColorFormat.BitsPerPixel > 0)
|
2008-03-26 20:43:57 +01:00
|
|
|
{
|
2014-07-19 20:39:17 +02:00
|
|
|
if (!mode.ColorFormat.IsIndexed)
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.RGBA);
|
|
|
|
visualAttributes.Add((int)GLXAttribute.RED_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.ColorFormat.Red);
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.GREEN_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.ColorFormat.Green);
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.BLUE_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.ColorFormat.Blue);
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.ALPHA_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.ColorFormat.Alpha);
|
2008-03-24 14:12:02 +01:00
|
|
|
}
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.Depth > 0)
|
2009-03-20 20:38:52 +01:00
|
|
|
{
|
|
|
|
visualAttributes.Add((int)GLXAttribute.DEPTH_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.Depth);
|
2009-03-20 20:38:52 +01:00
|
|
|
}
|
2008-03-24 14:12:02 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.Buffers > 1)
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.DOUBLEBUFFER);
|
2008-03-24 14:12:02 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.Stencil > 1)
|
2008-03-26 20:43:57 +01:00
|
|
|
{
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.STENCIL_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.Stencil);
|
2008-03-26 20:43:57 +01:00
|
|
|
}
|
2009-03-20 20:38:52 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.AccumulatorFormat.BitsPerPixel > 0)
|
2008-03-26 20:43:57 +01:00
|
|
|
{
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.ACCUM_ALPHA_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.AccumulatorFormat.Alpha);
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.ACCUM_BLUE_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.AccumulatorFormat.Blue);
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.ACCUM_GREEN_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.AccumulatorFormat.Green);
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.ACCUM_RED_SIZE);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.AccumulatorFormat.Red);
|
2008-03-26 20:43:57 +01:00
|
|
|
}
|
2014-07-19 20:39:17 +02:00
|
|
|
|
|
|
|
if (mode.Samples > 0)
|
2009-07-19 18:09:46 +02:00
|
|
|
{
|
|
|
|
visualAttributes.Add((int)GLXAttribute.SAMPLE_BUFFERS);
|
|
|
|
visualAttributes.Add(1);
|
|
|
|
visualAttributes.Add((int)GLXAttribute.SAMPLES);
|
2014-07-19 20:39:17 +02:00
|
|
|
visualAttributes.Add(mode.Samples);
|
2009-07-19 18:09:46 +02:00
|
|
|
}
|
2009-03-20 20:38:52 +01:00
|
|
|
|
2014-07-19 20:39:17 +02:00
|
|
|
if (mode.Stereo)
|
2009-03-20 20:38:52 +01:00
|
|
|
visualAttributes.Add((int)GLXAttribute.STEREO);
|
|
|
|
|
|
|
|
visualAttributes.Add(0);
|
|
|
|
|
|
|
|
Debug.Print("Falling back to glXChooseVisual.");
|
|
|
|
IntPtr display = API.DefaultDisplay;
|
2010-03-11 23:53:11 +01:00
|
|
|
using (new XLock(display))
|
2009-09-25 10:28:51 +02:00
|
|
|
{
|
|
|
|
return Glx.ChooseVisual(display, Functions.XDefaultScreen(display), visualAttributes.ToArray());
|
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
2009-03-07 08:16:46 +01:00
|
|
|
|
|
|
|
#endregion
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
}
|