diff --git a/Source/OpenTK/Graphics/GraphicsMode.cs b/Source/OpenTK/Graphics/GraphicsMode.cs index dddb5a66..0ad2f5d4 100644 --- a/Source/OpenTK/Graphics/GraphicsMode.cs +++ b/Source/OpenTK/Graphics/GraphicsMode.cs @@ -1,293 +1,293 @@ -#region --- License --- -/* 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.Diagnostics; - -namespace OpenTK.Graphics -{ - /// Defines the format for graphics operations. - public class GraphicsMode - { - ColorFormat color_format, accumulator_format; - int depth, stencil, buffers, samples; - bool stereo; - IntPtr? index; // The id of the pixel format or visual. - - static GraphicsMode defaultMode; - static IGraphicsMode implementation; - static object mode_selection_lock = new object(); - - #region --- Constructors --- - - #region static GraphicsMode() - - static GraphicsMode() - { - implementation = Platform.Factory.Default.CreateGraphicsMode(); - } - - #endregion - - #region internal GraphicsMode(GraphicsMode mode) - - internal GraphicsMode(GraphicsMode mode) - : this(mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo) { } - - #endregion - - #region internal GraphicsMode((IntPtr index, ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) - - internal GraphicsMode(IntPtr index, ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, - int buffers, bool stereo) - { - if (depth < 0) throw new ArgumentOutOfRangeException("depth", "Must be greater than, or equal to zero."); - if (stencil < 0) throw new ArgumentOutOfRangeException("stencil", "Must be greater than, or equal to zero."); - if (buffers <= 0) throw new ArgumentOutOfRangeException("buffers", "Must be greater than zero."); - if (samples < 0) throw new ArgumentOutOfRangeException("samples", "Must be greater than, or equal to zero."); - - this.Index = index; - this.ColorFormat = color; - this.Depth = depth; - this.Stencil = stencil; - this.Samples = samples; - this.AccumulatorFormat = accum; - this.Buffers = buffers; - this.Stereo = stereo; - } - - #endregion - - #region public GraphicsMode() - - /// Constructs a new GraphicsMode with sensible default parameters. - public GraphicsMode() - : this(Default) - { } - - #endregion - - #region public GraphicsMode(ColorFormat color) - - /// Constructs a new GraphicsMode with the specified parameters. - /// The ColorFormat of the color buffer. - public GraphicsMode(ColorFormat color) - : this(color, Default.Depth, Default.Stencil, Default.Samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) - { } - - #endregion - - #region public GraphicsMode(ColorFormat color, int depth) - - /// Constructs a new GraphicsMode with the specified parameters. - /// The ColorFormat of the color buffer. - /// The number of bits in the depth buffer. - public GraphicsMode(ColorFormat color, int depth) - : this(color, depth, Default.Stencil, Default.Samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) - { } - - #endregion - - #region public GraphicsMode(ColorFormat color, int depth, int stencil) - - /// Constructs a new GraphicsMode with the specified parameters. - /// The ColorFormat of the color buffer. - /// The number of bits in the depth buffer. - /// The number of bits in the stencil buffer. - public GraphicsMode(ColorFormat color, int depth, int stencil) - : this(color, depth, stencil, Default.Samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) - { } - - #endregion - - #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples) - - /// Constructs a new GraphicsMode with the specified parameters. - /// The ColorFormat of the color buffer. - /// The number of bits in the depth buffer. - /// The number of bits in the stencil buffer. - /// The number of samples for FSAA. - public GraphicsMode(ColorFormat color, int depth, int stencil, int samples) - : this(color, depth, stencil, samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) - { } - - #endregion - - #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum) - - /// Constructs a new GraphicsMode with the specified parameters. - /// The ColorFormat of the color buffer. - /// The number of bits in the depth buffer. - /// The number of bits in the stencil buffer. - /// The number of samples for FSAA. - /// The ColorFormat of the accumilliary buffer. - public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum) - : this(color, depth, stencil, samples, accum, Default.Buffers, Default.Stereo) - { } - - #endregion - - #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers) - - /// Constructs a new GraphicsMode with the specified parameters. - /// The ColorFormat of the color buffer. - /// The number of bits in the depth buffer. - /// The number of bits in the stencil buffer. - /// The number of samples for FSAA. - /// The ColorFormat of the accumilliary buffer. - /// The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering). - public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers) - : this(color, depth, stencil, samples, accum, buffers, Default.Stereo) - { } - - #endregion - - #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) - - /// Constructs a new GraphicsMode with the specified parameters. - /// The ColorFormat of the color buffer. - /// The number of bits in the depth buffer. - /// The number of bits in the stencil buffer. - /// The number of samples for FSAA. - /// The ColorFormat of the accumilliary buffer. - /// Set to true for a GraphicsMode with stereographic capabilities. - /// The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering). - public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) - : this(IntPtr.Zero, color, depth, stencil, samples, accum, buffers, stereo) { } - - #endregion - - #endregion - - #region --- Public Methods --- - - #region public int ColorFormat - - /// - /// Gets an OpenTK.Graphics.ColorFormat that describes the color format for this GraphicsFormat. - /// - public ColorFormat ColorFormat - { - get { return color_format; } - private set { color_format = value; } - } - - #endregion - - #region public int AccumulatorFormat - - /// - /// Gets an OpenTK.Graphics.ColorFormat that describes the accumulator format for this GraphicsFormat. - /// - public ColorFormat AccumulatorFormat - { - get { return accumulator_format; } - private set { accumulator_format = value; } - } - - #endregion - - #region public int Depth - - /// - /// Gets a System.Int32 that contains the bits per pixel for the depth buffer - /// for this GraphicsFormat. - /// - public int Depth - { - get { return depth; } - private set { depth = value; } - } - - #endregion - - #region public int Stencil - - /// - /// Gets a System.Int32 that contains the bits per pixel for the stencil buffer - /// of this GraphicsFormat. - /// - public int Stencil - { - get { return stencil; } - private set { stencil = value; } - } - - #endregion - - #region public int Samples - - /// - /// Gets a System.Int32 that contains the number of FSAA samples per pixel for this GraphicsFormat. - /// - public int Samples - { - get { return samples; } - private set { samples = value; } - } - - #endregion - - #region public bool Stereo - - /// - /// Gets a System.Boolean indicating whether this DisplayMode is stereoscopic. - /// - public bool Stereo - { - get { return this.stereo; } - private set { this.stereo = value; } - } - - #endregion - - #region public int Buffers - - /// - /// Gets a System.Int32 containing the number of buffers associated with this - /// DisplayMode. - /// - public int Buffers - { - get { return this.buffers; } - private set { this.buffers = value; } - } - - #endregion - - #region public static GraphicsFormat Default - - /// Returns an OpenTK.GraphicsFormat compatible with the underlying platform. - public static GraphicsMode Default - { - get - { - if (defaultMode == null) - { - Debug.Print("Creating default GraphicsMode ({0}, {1}, {2}, {3}, {4}, {5}, {6}).", DisplayDevice.Default.BitsPerPixel, - 16, 0, 0, 0, 2, false); - defaultMode = new GraphicsMode(DisplayDevice.Default.BitsPerPixel, 16, 0, 0, 0, 2, false); - } - return defaultMode; - } - } - - #endregion - - #endregion - - #region --- Internal Methods --- - - #region internal IntPtr Index - - internal IntPtr Index +#region --- License --- +/* 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.Diagnostics; + +namespace OpenTK.Graphics +{ + /// Defines the format for graphics operations. + public class GraphicsMode + { + ColorFormat color_format, accumulator_format; + int depth, stencil, buffers, samples; + bool stereo; + IntPtr? index = null; // The id of the pixel format or visual. + + static GraphicsMode defaultMode; + static IGraphicsMode implementation; + static object mode_selection_lock = new object(); + + #region --- Constructors --- + + #region static GraphicsMode() + + static GraphicsMode() + { + implementation = Platform.Factory.Default.CreateGraphicsMode(); + } + + #endregion + + #region internal GraphicsMode(GraphicsMode mode) + + internal GraphicsMode(GraphicsMode mode) + : this(mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo) { } + + #endregion + + #region internal GraphicsMode(IntPtr? index, ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) + + internal GraphicsMode(IntPtr? index, ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, + int buffers, bool stereo) + { + if (depth < 0) throw new ArgumentOutOfRangeException("depth", "Must be greater than, or equal to zero."); + if (stencil < 0) throw new ArgumentOutOfRangeException("stencil", "Must be greater than, or equal to zero."); + if (buffers <= 0) throw new ArgumentOutOfRangeException("buffers", "Must be greater than zero."); + if (samples < 0) throw new ArgumentOutOfRangeException("samples", "Must be greater than, or equal to zero."); + + this.Index = index; + this.ColorFormat = color; + this.Depth = depth; + this.Stencil = stencil; + this.Samples = samples; + this.AccumulatorFormat = accum; + this.Buffers = buffers; + this.Stereo = stereo; + } + + #endregion + + #region public GraphicsMode() + + /// Constructs a new GraphicsMode with sensible default parameters. + public GraphicsMode() + : this(Default) + { } + + #endregion + + #region public GraphicsMode(ColorFormat color) + + /// Constructs a new GraphicsMode with the specified parameters. + /// The ColorFormat of the color buffer. + public GraphicsMode(ColorFormat color) + : this(color, Default.Depth, Default.Stencil, Default.Samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) + { } + + #endregion + + #region public GraphicsMode(ColorFormat color, int depth) + + /// Constructs a new GraphicsMode with the specified parameters. + /// The ColorFormat of the color buffer. + /// The number of bits in the depth buffer. + public GraphicsMode(ColorFormat color, int depth) + : this(color, depth, Default.Stencil, Default.Samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) + { } + + #endregion + + #region public GraphicsMode(ColorFormat color, int depth, int stencil) + + /// Constructs a new GraphicsMode with the specified parameters. + /// The ColorFormat of the color buffer. + /// The number of bits in the depth buffer. + /// The number of bits in the stencil buffer. + public GraphicsMode(ColorFormat color, int depth, int stencil) + : this(color, depth, stencil, Default.Samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) + { } + + #endregion + + #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples) + + /// Constructs a new GraphicsMode with the specified parameters. + /// The ColorFormat of the color buffer. + /// The number of bits in the depth buffer. + /// The number of bits in the stencil buffer. + /// The number of samples for FSAA. + public GraphicsMode(ColorFormat color, int depth, int stencil, int samples) + : this(color, depth, stencil, samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) + { } + + #endregion + + #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum) + + /// Constructs a new GraphicsMode with the specified parameters. + /// The ColorFormat of the color buffer. + /// The number of bits in the depth buffer. + /// The number of bits in the stencil buffer. + /// The number of samples for FSAA. + /// The ColorFormat of the accumilliary buffer. + public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum) + : this(color, depth, stencil, samples, accum, Default.Buffers, Default.Stereo) + { } + + #endregion + + #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers) + + /// Constructs a new GraphicsMode with the specified parameters. + /// The ColorFormat of the color buffer. + /// The number of bits in the depth buffer. + /// The number of bits in the stencil buffer. + /// The number of samples for FSAA. + /// The ColorFormat of the accumilliary buffer. + /// The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering). + public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers) + : this(color, depth, stencil, samples, accum, buffers, Default.Stereo) + { } + + #endregion + + #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) + + /// Constructs a new GraphicsMode with the specified parameters. + /// The ColorFormat of the color buffer. + /// The number of bits in the depth buffer. + /// The number of bits in the stencil buffer. + /// The number of samples for FSAA. + /// The ColorFormat of the accumilliary buffer. + /// Set to true for a GraphicsMode with stereographic capabilities. + /// The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering). + public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) + : this(null, color, depth, stencil, samples, accum, buffers, stereo) { } + + #endregion + + #endregion + + #region --- Public Methods --- + + #region public int ColorFormat + + /// + /// Gets an OpenTK.Graphics.ColorFormat that describes the color format for this GraphicsFormat. + /// + public ColorFormat ColorFormat + { + get { return color_format; } + private set { color_format = value; } + } + + #endregion + + #region public int AccumulatorFormat + + /// + /// Gets an OpenTK.Graphics.ColorFormat that describes the accumulator format for this GraphicsFormat. + /// + public ColorFormat AccumulatorFormat + { + get { return accumulator_format; } + private set { accumulator_format = value; } + } + + #endregion + + #region public int Depth + + /// + /// Gets a System.Int32 that contains the bits per pixel for the depth buffer + /// for this GraphicsFormat. + /// + public int Depth + { + get { return depth; } + private set { depth = value; } + } + + #endregion + + #region public int Stencil + + /// + /// Gets a System.Int32 that contains the bits per pixel for the stencil buffer + /// of this GraphicsFormat. + /// + public int Stencil + { + get { return stencil; } + private set { stencil = value; } + } + + #endregion + + #region public int Samples + + /// + /// Gets a System.Int32 that contains the number of FSAA samples per pixel for this GraphicsFormat. + /// + public int Samples + { + get { return samples; } + private set { samples = value; } + } + + #endregion + + #region public bool Stereo + + /// + /// Gets a System.Boolean indicating whether this DisplayMode is stereoscopic. + /// + public bool Stereo + { + get { return this.stereo; } + private set { this.stereo = value; } + } + + #endregion + + #region public int Buffers + + /// + /// Gets a System.Int32 containing the number of buffers associated with this + /// DisplayMode. + /// + public int Buffers + { + get { return this.buffers; } + private set { this.buffers = value; } + } + + #endregion + + #region public static GraphicsFormat Default + + /// Returns an OpenTK.GraphicsFormat compatible with the underlying platform. + public static GraphicsMode Default + { + get + { + if (defaultMode == null) + { + Debug.Print("Creating default GraphicsMode ({0}, {1}, {2}, {3}, {4}, {5}, {6}).", DisplayDevice.Default.BitsPerPixel, + 16, 0, 0, 0, 2, false); + defaultMode = new GraphicsMode(DisplayDevice.Default.BitsPerPixel, 16, 0, 0, 0, 2, false); + } + return defaultMode; + } + } + + #endregion + + #endregion + + #region --- Internal Methods --- + + #region internal IntPtr Index + + internal IntPtr? Index { get { @@ -309,25 +309,25 @@ namespace OpenTK.Graphics Stereo = mode.Stereo; } - return index.Value; - } - set { index = value; } - } - - #endregion - - #endregion - - #region --- Overrides --- - - /// Returns a System.String describing the current GraphicsFormat. - /// ! System.String describing the current GraphicsFormat. - public override string ToString() - { - return String.Format("Index: {0}, Color: {1}, Depth: {2}, Stencil: {3}, Samples: {4}, Accum: {5}, Buffers: {6}, Stereo: {7}", - Index, ColorFormat, Depth, Stencil, Samples, AccumulatorFormat, Buffers, Stereo); - } - - #endregion - } -} + return index; + } + set { index = value; } + } + + #endregion + + #endregion + + #region --- Overrides --- + + /// Returns a System.String describing the current GraphicsFormat. + /// ! System.String describing the current GraphicsFormat. + public override string ToString() + { + return String.Format("Index: {0}, Color: {1}, Depth: {2}, Stencil: {3}, Samples: {4}, Accum: {5}, Buffers: {6}, Stereo: {7}", + Index, ColorFormat, Depth, Stencil, Samples, AccumulatorFormat, Buffers, Stereo); + } + + #endregion + } +} diff --git a/Source/OpenTK/Platform/Egl/EglContext.cs b/Source/OpenTK/Platform/Egl/EglContext.cs index af9891aa..d6faaa7d 100644 --- a/Source/OpenTK/Platform/Egl/EglContext.cs +++ b/Source/OpenTK/Platform/Egl/EglContext.cs @@ -29,14 +29,15 @@ using System; using OpenTK.Graphics; using System.Diagnostics; +using OpenTK.Platform.Windows; namespace OpenTK.Platform.Egl { - class EglContext : IGraphicsContext + class EglContext : IGraphicsContext, IGraphicsContextInternal { #region Fields - EglWindowInfo window; + EglWindowInfo WindowInfo; EGLContext context; GraphicsMode mode; bool vsync = true; // Default vsync value is defined as 1 (true) in EGL. @@ -56,14 +57,23 @@ namespace OpenTK.Platform.Egl EglContext shared = (EglContext)sharedContext; - Egl.Initialize(window.Display, out major, out minor); + int dummy_major, dummy_minor; + if (!Egl.Initialize(window.Display, out dummy_major, out dummy_minor)) + throw new GraphicsContextException(String.Format("Failed to initialize EGL, error {0}.", Egl.GetError())); - EGLConfig config = new EGLConfig(mode.Index); + WindowInfo = window; + + mode = new EglGraphicsMode().SelectGraphicsMode(mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo); + if (!mode.Index.HasValue) + throw new GraphicsModeException("Invalid or unsupported GraphicsMode."); + EGLConfig config = new EGLConfig(mode.Index.Value); if (window.Surface.Handle == EGLSurface.None.Handle) window.CreateWindowSurface(config); - context = Egl.CreateContext(window.Display, config, shared.context, null); + int[] attrib_list = new int[] { Egl.CONTEXT_CLIENT_VERSION, major, Egl.NONE }; + context = Egl.CreateContext(window.Display, config, shared != null ? shared.context : EGLContext.None, attrib_list); + MakeCurrent(window); } @@ -73,14 +83,18 @@ namespace OpenTK.Platform.Egl public void SwapBuffers() { - Egl.SwapBuffers(window.Display, window.Surface); + Egl.SwapBuffers(WindowInfo.Display, WindowInfo.Surface); } public void MakeCurrent(IWindowInfo window) { - EglWindowInfo egl = (EglWindowInfo)window; - Egl.MakeCurrent(egl.Display, egl.Surface, egl.Surface, context); - this.window = egl; + // Ignore 'window', unless it actually is an EGL window. In other words, + // trying to make the EglContext current on a non-EGL window will do, + // nothing (the EglContext will remain current on the previous EGL window + // or the window it was constructed on (which may not be EGL)). + if (window is EglWindowInfo) + WindowInfo = (EglWindowInfo)window; + Egl.MakeCurrent(WindowInfo.Display, WindowInfo.Surface, WindowInfo.Surface, context); } public bool IsCurrent @@ -100,10 +114,10 @@ namespace OpenTK.Platform.Egl } set { - if (Egl.SwapInterval(window.Display, value ? 1 : 0)) + if (Egl.SwapInterval(WindowInfo.Display, value ? 1 : 0)) vsync = value; else - Debug.Print("[Warning] Egl.SwapInterval({0}, {1}) failed.", window.Display, value); + Debug.Print("[Warning] Egl.SwapInterval({0}, {1}) failed.", WindowInfo.Display, value); } } @@ -147,8 +161,8 @@ namespace OpenTK.Platform.Egl { if (manual) { - Egl.MakeCurrent(window.Display, window.Surface, window.Surface, EGLContext.None); - Egl.DestroyContext(window.Display, context); + Egl.MakeCurrent(WindowInfo.Display, WindowInfo.Surface, WindowInfo.Surface, EGLContext.None); + Egl.DestroyContext(WindowInfo.Display, context); } else { @@ -164,5 +178,42 @@ namespace OpenTK.Platform.Egl } #endregion + + #region IGraphicsContextInternal Members + + public IGraphicsContext Implementation + { + get { return this; } + } + + public void LoadAll() + { + // Todo: enable those + //OpenTK.Graphics.ES10.ES.LoadAll(); + OpenTK.Graphics.ES11.GL.LoadAll(); + //OpenTK.Graphics.ES20.ES.LoadAll(); + } + + public ContextHandle Context + { + get { return new ContextHandle(context.Handle.Value); } + } + + public void RegisterForDisposal(IDisposable resource) + { + throw new NotImplementedException(); + } + + public void DisposeResources() + { + throw new NotImplementedException(); + } + + public IntPtr GetAddress(string function) + { + return Egl.GetProcAddress(function); + } + + #endregion } } diff --git a/Source/OpenTK/Platform/MacOS/AglContext.cs b/Source/OpenTK/Platform/MacOS/AglContext.cs index 96b9e0d7..9e62568f 100644 --- a/Source/OpenTK/Platform/MacOS/AglContext.cs +++ b/Source/OpenTK/Platform/MacOS/AglContext.cs @@ -429,9 +429,26 @@ namespace OpenTK.Platform.MacOS throw new Exception("The method or operation is not implemented."); } + private const string Library = "libdl.dylib"; + + [DllImport(Library, EntryPoint = "NSIsSymbolNameDefined")] + private static extern bool NSIsSymbolNameDefined(string s); + [DllImport(Library, EntryPoint = "NSLookupAndBindSymbol")] + private static extern IntPtr NSLookupAndBindSymbol(string s); + [DllImport(Library, EntryPoint = "NSAddressOfSymbol")] + private static extern IntPtr NSAddressOfSymbol(IntPtr symbol); + IntPtr IGraphicsContextInternal.GetAddress(string function) { - throw new Exception("The method or operation is not implemented."); + string fname = "_" + function; + if (!NSIsSymbolNameDefined(fname)) + return IntPtr.Zero; + + IntPtr symbol = NSLookupAndBindSymbol(fname); + if (symbol != IntPtr.Zero) + symbol = NSAddressOfSymbol(symbol); + + return symbol; } #endregion diff --git a/Source/OpenTK/Platform/Windows/WinGLContext.cs b/Source/OpenTK/Platform/Windows/WinGLContext.cs index 3b47d0bb..6ea55c78 100644 --- a/Source/OpenTK/Platform/Windows/WinGLContext.cs +++ b/Source/OpenTK/Platform/Windows/WinGLContext.cs @@ -314,15 +314,16 @@ namespace OpenTK.Platform.Windows void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window) { - if (mode.Index == IntPtr.Zero) throw new ArgumentException( - "mode", "The Index (pixel format) of the GraphicsMode is not set."); + if (!mode.Index.HasValue) + throw new GraphicsModeException("Invalid or unsupported GraphicsMode."); + if (window == null) throw new ArgumentNullException("window", "Must point to a valid window."); PixelFormatDescriptor pfd = new PixelFormatDescriptor(); - Functions.DescribePixelFormat(window.DeviceContext, (int)mode.Index, + Functions.DescribePixelFormat(window.DeviceContext, (int)mode.Index.Value, API.PixelFormatDescriptorSize, ref pfd); Debug.WriteLine(mode.Index.ToString()); - if (!Functions.SetPixelFormat(window.DeviceContext, (int)mode.Index, ref pfd)) + if (!Functions.SetPixelFormat(window.DeviceContext, (int)mode.Index.Value, ref pfd)) throw new GraphicsContextException(String.Format( "Requested GraphicsMode not available. SetPixelFormat error: {0}", Marshal.GetLastWin32Error())); } diff --git a/Source/OpenTK/Platform/X11/X11GLControl.cs b/Source/OpenTK/Platform/X11/X11GLControl.cs index 7f23af22..49d2275c 100644 --- a/Source/OpenTK/Platform/X11/X11GLControl.cs +++ b/Source/OpenTK/Platform/X11/X11GLControl.cs @@ -24,13 +24,16 @@ namespace OpenTK.Platform.X11 internal X11GLControl(GraphicsMode mode, Control control) { + if (!mode.Index.HasValue) + throw new GraphicsModeException("Invalid GraphicsMode."); + this.mode = mode; this.control = control; X11WindowInfo window = (X11WindowInfo)this.WindowInfo; - XVisualInfo info = new XVisualInfo(); - info.VisualID = mode.Index; + + info.VisualID = mode.Index.Value; int dummy; window.VisualInfo = (XVisualInfo)Marshal.PtrToStructure( Functions.XGetVisualInfo(window.Display, XVisualInfoMask.ID, ref info, out dummy), typeof(XVisualInfo)); diff --git a/Source/OpenTK/Platform/X11/X11GLNative.cs b/Source/OpenTK/Platform/X11/X11GLNative.cs index 7585077b..c69f6243 100644 --- a/Source/OpenTK/Platform/X11/X11GLNative.cs +++ b/Source/OpenTK/Platform/X11/X11GLNative.cs @@ -124,7 +124,10 @@ namespace OpenTK.Platform.X11 lock (API.Lock) { - info.VisualID = mode.Index; + if (!mode.Index.HasValue) + throw new GraphicsModeException("Invalid or unsupported GraphicsMode."); + + info.VisualID = mode.Index.Value; int dummy; window.VisualInfo = (XVisualInfo)Marshal.PtrToStructure( Functions.XGetVisualInfo(window.Display, XVisualInfoMask.ID, ref info, out dummy), typeof(XVisualInfo));