Skip to content

Instantly share code, notes, and snippets.

@thefiddler
Created January 17, 2014 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefiddler/8469469 to your computer and use it in GitHub Desktop.
Save thefiddler/8469469 to your computer and use it in GitHub Desktop.
Index: Source/OpenTK/Graphics/GraphicsMode.cs
===================================================================
--- Source/OpenTK/Graphics/GraphicsMode.cs (revision 3072)
+++ Source/OpenTK/Graphics/GraphicsMode.cs (working copy)
@@ -18,7 +18,7 @@
{
ColorFormat color_format, accumulator_format;
int depth, stencil, buffers, samples;
- bool stereo;
+ bool stereo, srgb;
IntPtr? index = null; // The id of the pixel format or visual.
static GraphicsMode defaultMode;
@@ -42,14 +42,14 @@
#region internal GraphicsMode(GraphicsMode mode)
internal GraphicsMode(GraphicsMode mode)
- : this(mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo) { }
+ : this(mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo, mode.Srgb) { }
#endregion
- #region internal GraphicsMode(IntPtr? index, ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo)
+ #region internal GraphicsMode(IntPtr? index, ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo, bool srgb)
internal GraphicsMode(IntPtr? index, ColorFormat color, int depth, int stencil, int samples, ColorFormat accum,
- int buffers, bool stereo)
+ int buffers, bool stereo, bool srgb)
{
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.");
@@ -64,6 +64,7 @@
this.AccumulatorFormat = accum;
this.Buffers = buffers;
this.Stereo = stereo;
+ this.Srgb = srgb;
}
#endregion
@@ -160,15 +161,31 @@
/// <param name="stencil">The number of bits in the stencil buffer.</param>
/// <param name="samples">The number of samples for FSAA.</param>
/// <param name="accum">The ColorFormat of the accumilliary buffer.</param>
+ /// <param name="buffers">The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering).</param>
/// <param name="stereo">Set to true for a GraphicsMode with stereographic capabilities.</param>
- /// <param name="buffers">The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering).</param>
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) { }
+ : this(color, depth, stencil, samples, accum, buffers, stereo, Default.Srgb) { }
#endregion
+
+ #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo, bool srgb)
+ /// <summary>Constructs a new GraphicsMode with the specified parameters.</summary>
+ /// <param name="color">The ColorFormat of the color buffer.</param>
+ /// <param name="depth">The number of bits in the depth buffer.</param>
+ /// <param name="stencil">The number of bits in the stencil buffer.</param>
+ /// <param name="samples">The number of samples for FSAA.</param>
+ /// <param name="accum">The ColorFormat of the accumilliary buffer.</param>
+ /// <param name="buffers">The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering).</param>
+ /// <param name="stereo">Set to true for a GraphicsMode with stereographic capabilities.</param>
+ /// <param name="srgb">Set to true for a GraphicsMode with standard color encoding capabilities.</param>
+ public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo, bool srgb)
+ : this(null, color, depth, stencil, samples, accum, buffers, stereo, srgb) { }
+
#endregion
+ #endregion
+
#region --- Public Methods ---
#region public IntPtr Index
@@ -275,6 +292,24 @@
#endregion
+ #region public int Buffers
+
+ /// <summary>
+ /// Gets a System.Int32 containing the number of buffers associated with this
+ /// DisplayMode.
+ /// </summary>
+ public int Buffers
+ {
+ get
+ {
+ LazySelectGraphicsMode();
+ return buffers;
+ }
+ private set { buffers = value; }
+ }
+
+ #endregion
+
#region public bool Stereo
/// <summary>
@@ -291,21 +326,20 @@
}
#endregion
-
- #region public int Buffers
-
+
+ #region public bool Srgb
+
/// <summary>
- /// Gets a System.Int32 containing the number of buffers associated with this
- /// DisplayMode.
+ /// Gets a System.Boolean indicating whether this DisplayMode has standard color encoding.
/// </summary>
- public int Buffers
+ public bool Srgb
{
get
{
LazySelectGraphicsMode();
- return buffers;
+ return srgb;
}
- private set { buffers = value; }
+ private set { srgb = value; }
}
#endregion
@@ -321,9 +355,9 @@
{
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);
+ Debug.Print("Creating default GraphicsMode ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}).",
+ DisplayDevice.Default.BitsPerPixel, 16, 0, 0, 0, 2, false, false);
+ defaultMode = new GraphicsMode(DisplayDevice.Default.BitsPerPixel, 16, 0, 0, 0, 2, false, false);
}
return defaultMode;
}
@@ -343,7 +377,7 @@
{
if (index == null)
{
- GraphicsMode mode = implementation.SelectGraphicsMode(color_format, depth, stencil, samples, accumulator_format, buffers, stereo);
+ GraphicsMode mode = implementation.SelectGraphicsMode(color_format, depth, stencil, samples, accumulator_format, buffers, stereo, srgb);
Index = mode.Index;
ColorFormat = mode.ColorFormat;
@@ -353,6 +387,7 @@
AccumulatorFormat = mode.AccumulatorFormat;
Buffers = mode.Buffers;
Stereo = mode.Stereo;
+ Srgb = mode.Srgb;
}
}
@@ -364,8 +399,8 @@
/// <returns>! System.String describing the current GraphicsFormat.</returns>
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);
+ return String.Format("Index: {0}, Color: {1}, Depth: {2}, Stencil: {3}, Samples: {4}, Accum: {5}, Buffers: {6}, Stereo: {7}, Srgb: {8}",
+ Index, ColorFormat, Depth, Stencil, Samples, AccumulatorFormat, Buffers, Stereo, Srgb);
}
/// <summary>
Index: Source/OpenTK/Graphics/IGraphicsMode.cs
===================================================================
--- Source/OpenTK/Graphics/IGraphicsMode.cs (revision 3072)
+++ Source/OpenTK/Graphics/IGraphicsMode.cs (working copy)
@@ -17,6 +17,6 @@
// Creates a temporary OpenGL context (if necessary) and finds the mode which closest matches
// the specified parameters.
GraphicsMode SelectGraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers,
- bool stereo);
+ bool stereo, bool srgb);
}
}
Index: Source/OpenTK/OpenTK.csproj
===================================================================
--- Source/OpenTK/OpenTK.csproj (revision 3072)
+++ Source/OpenTK/OpenTK.csproj (working copy)
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<ProjectType>Local</ProjectType>
<ProductVersion>8.0.50727</ProductVersion>
@@ -14,7 +14,7 @@
<DefaultHTMLPageLayout>Grid</DefaultHTMLPageLayout>
<DefaultTargetSchema>IE50</DefaultTargetSchema>
<DelaySign>false</DelaySign>
- <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<OutputType>Library</OutputType>
<AppDesignerFolder>
</AppDesignerFolder>
@@ -124,9 +124,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
- <Compile Include="..\GlobalAssemblyInfo.cs">
- <Link>Properties\GlobalAssemblyInfo.cs</Link>
- </Compile>
<Compile Include="DisplayDevice.cs">
<SubType>Code</SubType>
</Compile>
@@ -769,6 +766,9 @@
</EmbeddedResource>
<Compile Include="Platform\MacOS\HIDInput.cs" />
<Compile Include="IntPtrEqualityComparer.cs" />
+ <Compile Include="Platform\MacOS\CarbonBindings\Cgl.cs" />
+ <Compile Include="Platform\MacOS\BaseContext.cs" />
+ <Compile Include="Platform\MacOS\CglContext.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
Index: Source/OpenTK/Platform/Dummy/DummyGLContext.cs
===================================================================
--- Source/OpenTK/Platform/Dummy/DummyGLContext.cs (revision 3072)
+++ Source/OpenTK/Platform/Dummy/DummyGLContext.cs (working copy)
@@ -32,7 +32,7 @@
public DummyGLContext()
: this(new ContextHandle(new IntPtr(++handle_count)))
{
- Mode = new GraphicsMode(new IntPtr(2), 32, 16, 0, 0, 0, 2, false);
+ Mode = new GraphicsMode(new IntPtr(2), 32, 16, 0, 0, 0, 2, false, false);
}
public DummyGLContext(ContextHandle handle)
Index: Source/OpenTK/Platform/Egl/EglContext.cs
===================================================================
--- Source/OpenTK/Platform/Egl/EglContext.cs (revision 3072)
+++ Source/OpenTK/Platform/Egl/EglContext.cs (working copy)
@@ -66,7 +66,7 @@
// and it may not support the desired renderer.
Mode = new EglGraphicsMode().SelectGraphicsMode(mode.ColorFormat,
mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat,
- mode.Buffers, mode.Stereo,
+ mode.Buffers, mode.Stereo, mode.Srgb,
major > 1 ? RenderableFlags.ES2 : RenderableFlags.ES);
if (!Mode.Index.HasValue)
throw new GraphicsModeException("Invalid or unsupported GraphicsMode.");
Index: Source/OpenTK/Platform/Egl/EglGraphicsMode.cs
===================================================================
--- Source/OpenTK/Platform/Egl/EglGraphicsMode.cs (revision 3072)
+++ Source/OpenTK/Platform/Egl/EglGraphicsMode.cs (working copy)
@@ -37,11 +37,11 @@
#region IGraphicsMode Members
public GraphicsMode SelectGraphicsMode(ColorFormat color, int depth, int stencil,
- int samples, ColorFormat accum, int buffers, bool stereo)
+ int samples, ColorFormat accum, int buffers, bool stereo, bool srgb)
{
// According to the EGL specs, the ES flag should select ES 1.0 or higher, which
// makes sense as a default. EglContext.cs checks
- return SelectGraphicsMode(color, depth, stencil, samples, accum, buffers, stereo,
+ return SelectGraphicsMode(color, depth, stencil, samples, accum, buffers, stereo, srgb,
RenderableFlags.ES);
}
@@ -50,7 +50,7 @@
#region Public Members
public GraphicsMode SelectGraphicsMode(ColorFormat color, int depth, int stencil,
- int samples, ColorFormat accum, int buffers, bool stereo,
+ int samples, ColorFormat accum, int buffers, bool stereo, bool srgb,
RenderableFlags renderable_flags)
{
IntPtr[] configs = new IntPtr[1];
@@ -99,7 +99,7 @@
Egl.GetConfigAttrib(display, active_config, Egl.SAMPLES, out sample_buffers);
Egl.GetConfigAttrib(display, active_config, Egl.SAMPLES, out samples);
- return new GraphicsMode(active_config, new ColorFormat(r, g, b, a), d, s, sample_buffers > 0 ? samples : 0, 0, 2, false);
+ return new GraphicsMode(active_config, new ColorFormat(r, g, b, a), d, s, sample_buffers > 0 ? samples : 0, 0, 2, false, false);
}
#endregion
Index: Source/OpenTK/Platform/MacOS/AglContext.cs
===================================================================
--- Source/OpenTK/Platform/MacOS/AglContext.cs (revision 3072)
+++ Source/OpenTK/Platform/MacOS/AglContext.cs (working copy)
@@ -31,65 +31,34 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
+using OpenTK.Graphics;
namespace OpenTK.Platform.MacOS
{
- using Carbon;
- using Graphics;
-
using AGLRendererInfo = IntPtr;
using AGLPixelFormat = IntPtr;
using AGLContext = IntPtr;
using AGLPbuffer = IntPtr;
- class AglContext : DesktopGraphicsContext
+ class AglContext : BaseContext
{
- bool mVSync = false;
- // Todo: keep track of which display adapter was specified when the context was created.
- // IntPtr displayID;
+ public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool isCoreProfile)
+ : base(mode,window,shareContext,isCoreProfile)
+ {}
- GraphicsMode graphics_mode;
- CarbonWindowInfo carbonWindow;
- IntPtr shareContextRef;
- DisplayDevice device;
- bool mIsFullscreen = false;
-
- public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext)
- {
- Debug.Print("Context Type: {0}", shareContext);
- Debug.Print("Window info: {0}", window);
-
- this.graphics_mode = mode;
- this.carbonWindow = (CarbonWindowInfo)window;
-
- if (shareContext is AglContext)
- shareContextRef = ((AglContext)shareContext).Handle.Handle;
- if (shareContext is GraphicsContext)
- {
- ContextHandle shareHandle = shareContext != null ? (shareContext as IGraphicsContextInternal).Context : (ContextHandle)IntPtr.Zero;
-
- shareContextRef = shareHandle.Handle;
- }
-
- if (shareContextRef == IntPtr.Zero)
- {
- Debug.Print("No context sharing will take place.");
- }
-
- CreateContext(mode, carbonWindow, shareContextRef, true);
- }
-
public AglContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext)
+ : base(handle,window,shareContext)
+ {}
+
+ void MyAGLReportError(string function)
{
- if (handle == ContextHandle.Zero)
- throw new ArgumentException("handle");
- if (window == null)
- throw new ArgumentNullException("window");
+ Agl.AglError err = Agl.GetError();
- Handle = handle;
- carbonWindow = (CarbonWindowInfo)window;
+ if (err != Agl.AglError.NoError)
+ throw new MacOSException((OSStatus)err, string.Format("AGL Error from function {0}: {1} {2}", function, err, Agl.ErrorString(err)));
}
+ #region BaseContext implementation
private void AddPixelAttrib(List<int> aglAttributes, Agl.PixelFormatAttribute pixelFormatAttribute)
{
@@ -104,9 +73,10 @@
aglAttributes.Add((int)pixelFormatAttribute);
aglAttributes.Add(value);
}
- void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, IntPtr shareContextRef, bool fullscreen)
+ protected override void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, IntPtr shareContextRef, bool isCoreProfile)
{
List<int> aglAttributes = new List<int>();
+ bool fullscreen = true;
Debug.Print("AGL pixel format attributes:");
Debug.Indent();
@@ -168,17 +138,7 @@
throw new MacOSException(status, "DMGetGDeviceByDisplayID failed.");
myAGLPixelFormat = Agl.aglChoosePixelFormat(ref gdevice, 1, aglAttributes.ToArray());
-
- Agl.AglError err = Agl.GetError();
-
- if (err == Agl.AglError.BadPixelFormat)
- {
- Debug.Print("Failed to create full screen pixel format.");
- Debug.Print("Trying again to create a non-fullscreen pixel format.");
-
- CreateContext(mode, carbonWindow, shareContextRef, false);
- return;
- }
+ mIsFullscreen = true;
}
else
@@ -185,10 +145,9 @@
{
myAGLPixelFormat = Agl.aglChoosePixelFormat(IntPtr.Zero, 0, aglAttributes.ToArray());
- MyAGLReportError("aglChoosePixelFormat");
}
-
+ MyAGLReportError("aglChoosePixelFormat");
Debug.Print("Creating AGL context. Sharing with {0}", shareContextRef);
// create the context and share it with the share reference.
@@ -198,6 +157,17 @@
// Free the pixel format from memory.
Agl.aglDestroyPixelFormat(myAGLPixelFormat);
MyAGLReportError("aglDestroyPixelFormat");
+
+ IntPtr cgl; Cgl.Error error;
+ bool result = Agl.aglGetCGLContext( Handle.Handle, ref cgl );
+ error = Cgl.CGLSetCurrentContext(cgl);
+ int num = 1;
+ IntPtr pixFormat;
+ List<int> cgAt = new List<int>();
+ CglContext.AddPixelAttrib(cgAt, Cgl.PixelFormatInt.OpenGLProfile, (int)Cgl.OpenGLProfileVersion.Core3_2);
+ CglContext.AddPixelAttrib(cgAt, Cgl.PixelFormatBool.None);
+ error = Cgl.CGLChoosePixelFormat( cgAt.ToArray(), ref pixFormat, ref num );
+ String str = Cgl.ErrorString(error);
Debug.Print("IsControl: {0}", carbonWindow.IsControl);
@@ -210,69 +180,8 @@
Debug.Print("context: {0}", Handle.Handle);
}
- private IntPtr GetQuartzDevice(CarbonWindowInfo carbonWindow)
+ protected override void SetDrawable(CarbonWindowInfo carbonWindow)
{
- IntPtr windowRef = carbonWindow.WindowRef;
-
- if (CarbonGLNative.WindowRefMap.ContainsKey(windowRef) == false)
- return IntPtr.Zero;
-
- WeakReference nativeRef = CarbonGLNative.WindowRefMap[windowRef];
- if (nativeRef.IsAlive == false)
- return IntPtr.Zero;
-
- CarbonGLNative window = nativeRef.Target as CarbonGLNative;
-
- if (window == null)
- return IntPtr.Zero;
-
- return QuartzDisplayDeviceDriver.HandleTo(window.TargetDisplayDevice);
-
- }
-
- void SetBufferRect(CarbonWindowInfo carbonWindow)
- {
- if (carbonWindow.IsControl == false)
- return;
-
- // Todo: See if there is a way around using WinForms.
- throw new NotImplementedException();
-#if false
- System.Windows.Forms.Control ctrl = Control.FromHandle(carbonWindow.WindowRef);
-
- if (ctrl.TopLevelControl == null)
- return;
-
- Rect rect = API.GetControlBounds(carbonWindow.WindowRef);
- System.Windows.Forms.Form frm = (System.Windows.Forms.Form)ctrl.TopLevelControl;
-
- System.Drawing.Point loc = frm.PointToClient(ctrl.PointToScreen(System.Drawing.Point.Empty));
-
- rect.X = (short)loc.X;
- rect.Y = (short)loc.Y;
-
- Debug.Print("Setting buffer_rect for control.");
- Debug.Print("MacOS Coordinate Rect: {0}", rect);
-
- rect.Y = (short)(ctrl.TopLevelControl.ClientSize.Height - rect.Y - rect.Height);
- Debug.Print(" AGL Coordinate Rect: {0}", rect);
-
- int[] glrect = new int[4];
-
- glrect[0] = rect.X;
- glrect[1] = rect.Y;
- glrect[2] = rect.Width;
- glrect[3] = rect.Height;
-
- Agl.aglSetInteger(Handle.Handle, Agl.ParameterNames.AGL_BUFFER_RECT, glrect);
- MyAGLReportError("aglSetInteger");
-
- Agl.aglEnable(Handle.Handle, Agl.ParameterNames.AGL_BUFFER_RECT);
- MyAGLReportError("aglEnable");
-#endif
- }
- void SetDrawable(CarbonWindowInfo carbonWindow)
- {
IntPtr windowPort = GetWindowPortForWindowInfo(carbonWindow);
//Debug.Print("Setting drawable for context {0} to window port: {1}", Handle.Handle, windowPort);
@@ -282,121 +191,17 @@
}
- private static IntPtr GetWindowPortForWindowInfo(CarbonWindowInfo carbonWindow)
+ protected override void UpdateContext(IntPtr context)
{
- IntPtr windowPort;
- if (carbonWindow.IsControl)
- {
- IntPtr controlOwner = API.GetControlOwner(carbonWindow.WindowRef);
-
- windowPort = API.GetWindowPort(controlOwner);
- }
-
- else
- windowPort = API.GetWindowPort(carbonWindow.WindowRef);
-
- return windowPort;
+ Agl.aglUpdateContext(context);
}
- public override void Update(IWindowInfo window)
- {
- CarbonWindowInfo carbonWindow = (CarbonWindowInfo)window;
-
- if (carbonWindow.GoFullScreenHack)
- {
- carbonWindow.GoFullScreenHack = false;
- CarbonGLNative wind = GetCarbonWindow(carbonWindow);
-
- if (wind != null)
- wind.SetFullscreen(this);
- else
- Debug.Print("Could not find window!");
-
- return;
- }
-
- else if (carbonWindow.GoWindowedHack)
- {
- carbonWindow.GoWindowedHack = false;
- CarbonGLNative wind = GetCarbonWindow(carbonWindow);
-
- if (wind != null)
- wind.UnsetFullscreen(this);
- else
- Debug.Print("Could not find window!");
-
- }
-
- if (mIsFullscreen)
- return;
-
- SetDrawable(carbonWindow);
- SetBufferRect(carbonWindow);
-
- Agl.aglUpdateContext(Handle.Handle);
- }
-
- private CarbonGLNative GetCarbonWindow(CarbonWindowInfo carbonWindow)
- {
- WeakReference r = CarbonGLNative.WindowRefMap[carbonWindow.WindowRef];
-
- if (r.IsAlive)
- {
- return (CarbonGLNative)r.Target;
- }
-
- else
- return null;
- }
-
- void MyAGLReportError(string function)
- {
- Agl.AglError err = Agl.GetError();
-
- if (err != Agl.AglError.NoError)
- throw new MacOSException((OSStatus)err, string.Format("AGL Error from function {0}: {1} {2}", function, err, Agl.ErrorString(err)));
- }
-
- bool firstFullScreen = false;
-
- internal void SetFullScreen(CarbonWindowInfo info, out int width, out int height)
- {
- CarbonGLNative wind = GetCarbonWindow(info);
-
- Debug.Print("Switching to full screen {0}x{1} on context {2}", wind.TargetDisplayDevice.Width, wind.TargetDisplayDevice.Height, Handle.Handle);
-
- CG.DisplayCapture(GetQuartzDevice(info));
- Agl.aglSetFullScreen(Handle.Handle, wind.TargetDisplayDevice.Width, wind.TargetDisplayDevice.Height, 0, 0);
- MakeCurrent(info);
-
- width = wind.TargetDisplayDevice.Width;
- height = wind.TargetDisplayDevice.Height;
-
- // This is a weird hack to workaround a bug where the first time a context
- // is made fullscreen, we just end up with a blank screen. So we undo it as fullscreen
- // and redo it as fullscreen.
- if (firstFullScreen == false)
- {
- firstFullScreen = true;
- UnsetFullScreen(info);
- SetFullScreen(info, out width, out height);
- }
-
- mIsFullscreen = true;
- }
- internal void UnsetFullScreen(CarbonWindowInfo windowInfo)
- {
- Debug.Print("Unsetting AGL fullscreen.");
- Agl.aglSetDrawable(Handle.Handle, IntPtr.Zero);
- Agl.aglUpdateContext(Handle.Handle);
-
- CG.DisplayRelease(GetQuartzDevice(windowInfo));
- Debug.Print("Resetting drawable.");
- SetDrawable(windowInfo);
-
- mIsFullscreen = false;
- }
-
-
+
+ protected override void SetFullSceenImp(IntPtr context, int width, int height)
+ {
+ Agl.aglSetFullScreen(Handle.Handle, width, height, 0, 0);
+ }
+
+ #endregion
#region IGraphicsContext Members
bool firstSwap = false;
@@ -490,31 +295,5 @@
}
#endregion
-
- #region IGraphicsContextInternal Members
-
- 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);
-
- public override IntPtr GetAddress(string function)
- {
- string fname = "_" + function;
- if (!NSIsSymbolNameDefined(fname))
- return IntPtr.Zero;
-
- IntPtr symbol = NSLookupAndBindSymbol(fname);
- if (symbol != IntPtr.Zero)
- symbol = NSAddressOfSymbol(symbol);
-
- return symbol;
- }
-
- #endregion
}
}
Index: Source/OpenTK/Platform/MacOS/BaseContext.cs
===================================================================
--- Source/OpenTK/Platform/MacOS/BaseContext.cs (revision 0)
+++ Source/OpenTK/Platform/MacOS/BaseContext.cs (working copy)
@@ -0,0 +1,295 @@
+#region License
+//
+// The Open Toolkit Library License
+//
+// Copyright (c) 2006 - 2010 the Open Toolkit library.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights to
+// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+// the Software, and to permit persons to whom the Software is furnished to do
+// so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+using OpenTK.Graphics;
+
+namespace OpenTK.Platform.MacOS
+{
+ abstract class BaseContext : DesktopGraphicsContext
+ {
+ protected bool mVSync = false;
+ protected bool mIsFullscreen = false;
+ protected CarbonWindowInfo carbonWindow;
+ private GraphicsMode graphicsMode;
+ private IntPtr shareContextRef;
+
+ public BaseContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool isCoreProfile)
+ {
+ Debug.Print("Context Type: {0}", shareContext);
+ Debug.Print("Window info: {0}", window);
+
+ this.graphicsMode = mode;
+ this.carbonWindow = (CarbonWindowInfo)window;
+
+ if (shareContext is BaseContext)
+ shareContextRef = ((BaseContext)shareContext).Handle.Handle;
+ if (shareContext is GraphicsContext)
+ {
+ ContextHandle shareHandle = shareContext != null ?
+ (shareContext as IGraphicsContextInternal).Context : (ContextHandle)IntPtr.Zero;
+
+ shareContextRef = shareHandle.Handle;
+ }
+
+ if (shareContextRef == IntPtr.Zero)
+ {
+ Debug.Print("No context sharing will take place.");
+ }
+
+ CreateContext(mode, carbonWindow, shareContextRef, isCoreProfile);
+ }
+
+ public BaseContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext)
+ {
+ if (handle == ContextHandle.Zero)
+ throw new ArgumentException("handle");
+ if (window == null)
+ throw new ArgumentNullException("window");
+
+ Handle = handle;
+ carbonWindow = (CarbonWindowInfo)window;
+ }
+
+ protected abstract void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, IntPtr shareContextRef, bool isCoreProfile);
+
+ protected virtual void SetDrawable(CarbonWindowInfo carbonWindow)
+ {}
+ protected virtual void UpdateContext(IntPtr context)
+ {}
+
+ #region Window Management
+
+ protected void SetBufferRect(CarbonWindowInfo carbonWindow)
+ {
+ if (carbonWindow.IsControl == false)
+ return;
+
+ // Todo: See if there is a way around using WinForms.
+ //throw new NotImplementedException();
+
+ #if false
+ System.Windows.Forms.Control ctrl = Control.FromHandle(carbonWindow.WindowRef);
+
+ if (ctrl.TopLevelControl == null)
+ return;
+
+ Rect rect = API.GetControlBounds(carbonWindow.WindowRef);
+ System.Windows.Forms.Form frm = (System.Windows.Forms.Form)ctrl.TopLevelControl;
+
+ System.Drawing.Point loc = frm.PointToClient(ctrl.PointToScreen(System.Drawing.Point.Empty));
+
+ rect.X = (short)loc.X;
+ rect.Y = (short)loc.Y;
+
+ Debug.Print("Setting buffer_rect for control.");
+ Debug.Print("MacOS Coordinate Rect: {0}", rect);
+
+ rect.Y = (short)(ctrl.TopLevelControl.ClientSize.Height - rect.Y - rect.Height);
+ Debug.Print(" AGL Coordinate Rect: {0}", rect);
+
+ int[] glrect = new int[4];
+
+ glrect[0] = rect.X;
+ glrect[1] = rect.Y;
+ glrect[2] = rect.Width;
+ glrect[3] = rect.Height;
+
+ Agl.aglSetInteger(Handle.Handle, Agl.ParameterNames.AGL_BUFFER_RECT, glrect);
+ MyAGLReportError("aglSetInteger");
+
+ Agl.aglEnable(Handle.Handle, Agl.ParameterNames.AGL_BUFFER_RECT);
+ MyAGLReportError("aglEnable");
+ #endif
+ }
+
+ protected static IntPtr GetWindowPortForWindowInfo(CarbonWindowInfo carbonWindow)
+ {
+ IntPtr windowPort;
+ if (carbonWindow.IsControl)
+ {
+ IntPtr controlOwner = Carbon.API.GetControlOwner(carbonWindow.WindowRef);
+
+ windowPort = Carbon.API.GetWindowPort(controlOwner);
+ }
+
+ else
+ windowPort = Carbon.API.GetWindowPort(carbonWindow.WindowRef);
+
+ return windowPort;
+ }
+
+ public override void Update(IWindowInfo window)
+ {
+ CarbonWindowInfo carbonWindow = (CarbonWindowInfo)window;
+
+ if (carbonWindow.GoFullScreenHack)
+ {
+ carbonWindow.GoFullScreenHack = false;
+ CarbonGLNative wind = GetCarbonWindow(carbonWindow);
+
+ if (wind != null)
+ wind.SetFullscreen(this);
+ else
+ Debug.Print("Could not find window!");
+
+ return;
+ }
+
+ else if (carbonWindow.GoWindowedHack)
+ {
+ carbonWindow.GoWindowedHack = false;
+ CarbonGLNative wind = GetCarbonWindow(carbonWindow);
+
+ if (wind != null)
+ wind.UnsetFullscreen(this);
+ else
+ Debug.Print("Could not find window!");
+
+ }
+
+ if (mIsFullscreen)
+ return;
+
+ SetDrawable(carbonWindow);
+ SetBufferRect(carbonWindow);
+ UpdateContext(Handle.Handle);
+ }
+
+ private CarbonGLNative GetCarbonWindow(CarbonWindowInfo carbonWindow)
+ {
+ WeakReference r = CarbonGLNative.WindowRefMap[carbonWindow.WindowRef];
+
+ if (r.IsAlive)
+ {
+ return (CarbonGLNative)r.Target;
+ }
+
+ else
+ return null;
+ }
+
+ #endregion
+
+ #region Full Screen
+
+ protected virtual void SetFullSceenImp(IntPtr context, int width, int height)
+ {}
+
+ protected IntPtr GetQuartzDevice(CarbonWindowInfo carbonWindow)
+ {
+ IntPtr windowRef = carbonWindow.WindowRef;
+
+ if (CarbonGLNative.WindowRefMap.ContainsKey(windowRef) == false)
+ return IntPtr.Zero;
+
+ WeakReference nativeRef = CarbonGLNative.WindowRefMap[windowRef];
+ if (nativeRef.IsAlive == false)
+ return IntPtr.Zero;
+
+ CarbonGLNative window = nativeRef.Target as CarbonGLNative;
+
+ if (window == null)
+ return IntPtr.Zero;
+
+ return QuartzDisplayDeviceDriver.HandleTo(window.TargetDisplayDevice);
+
+ }
+
+ bool firstFullScreen = false;
+
+ internal void SetFullScreen(CarbonWindowInfo info, out int width, out int height)
+ {
+ CarbonGLNative wind = GetCarbonWindow(info);
+
+ Debug.Print("Switching to full screen {0}x{1} on context {2}", wind.TargetDisplayDevice.Width, wind.TargetDisplayDevice.Height, Handle.Handle);
+
+ Carbon.CG.DisplayCapture(GetQuartzDevice(info));
+ SetFullSceenImp(Handle.Handle, wind.TargetDisplayDevice.Width, wind.TargetDisplayDevice.Height);
+ MakeCurrent(info);
+
+ width = wind.TargetDisplayDevice.Width;
+ height = wind.TargetDisplayDevice.Height;
+
+ // This is a weird hack to workaround a bug where the first time a context
+ // is made fullscreen, we just end up with a blank screen. So we undo it as fullscreen
+ // and redo it as fullscreen.
+ if (firstFullScreen == false)
+ {
+ firstFullScreen = true;
+ UnsetFullScreen(info);
+ SetFullScreen(info, out width, out height);
+ }
+
+ mIsFullscreen = true;
+ }
+
+ internal void UnsetFullScreen(CarbonWindowInfo windowInfo)
+ {
+ Debug.Print("Unsetting AGL fullscreen.");
+ SetDrawable(windowInfo);
+ UpdateContext(Handle.Handle);
+
+ Carbon.CG.DisplayRelease(GetQuartzDevice(windowInfo));
+ Debug.Print("Resetting drawable.");
+ SetDrawable(windowInfo);
+
+ mIsFullscreen = false;
+ }
+
+ #endregion
+
+ #region IGraphicsContextInternal Members
+
+ 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);
+
+ public override IntPtr GetAddress(string function)
+ {
+ string fname = "_" + function;
+ if (!NSIsSymbolNameDefined(fname))
+ return IntPtr.Zero;
+
+ IntPtr symbol = NSLookupAndBindSymbol(fname);
+ if (symbol != IntPtr.Zero)
+ symbol = NSAddressOfSymbol(symbol);
+
+ return symbol;
+ }
+
+ #endregion
+ }
+}
\ No newline at end of file
Property changes on: Source/OpenTK/Platform/MacOS/BaseContext.cs
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: Source/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs
===================================================================
--- Source/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs (revision 3072)
+++ Source/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs (working copy)
@@ -340,8 +340,8 @@
return (_aglDestroyContext(context) != 0) ? true : false;
}
- [DllImport(agl)] static extern byte aglCopyContext(AGLContext src, AGLContext dst, uint mask);
- [DllImport(agl)] internal static extern byte aglUpdateContext(AGLContext ctx);
+ [DllImport(agl)] static extern bool aglCopyContext(AGLContext src, AGLContext dst, uint mask);
+ [DllImport(agl)] internal static extern bool aglUpdateContext(AGLContext ctx);
/*
** Current state functions
@@ -451,6 +451,12 @@
return Marshal.PtrToStringAnsi(_aglErrorString(code));
}
+ /*
+ ** CGL interaction
+ */
+ [DllImport(agl)]
+ internal static extern bool aglGetCGLContext(AGLContext ctx, ref IntPtr cgl);
+
/*
** Soft reset function
*/
@@ -480,7 +486,15 @@
*/
[DllImport(agl)] static extern byte aglGetCGLContext(AGLContext ctx, void **cgl_ctx) ;
[DllImport(agl)] static extern byte aglGetCGLPixelFormat(AGLPixelFormat pix, void **cgl_pix);
-
+
+ /*
+ ** New (OS 10.5+) Windowing Functions
+ */
+ [DllImport(agl)]
+ internal static extern bool aglSetWindowRef(AGLContext ctx, IntPtr windowRef);
+ [DllImport(agl)]
+ internal static extern IntPtr aglGetWindowRef(AGLContext ctx);
+
#pragma warning restore 0169
}
}
Index: Source/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs
===================================================================
--- Source/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs (revision 3072)
+++ Source/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs (working copy)
@@ -914,6 +914,13 @@
internal extern static void RestoreApplicationDockTileImage();
#endregion
+
+ #region Window Management
+
+ [DllImport(carbon)]
+ internal static extern int HIWindowGetCGWindowID(IntPtr window);
+
+ #endregion
[DllImport(carbon)]
static extern IntPtr GetControlBounds(IntPtr control, out Rect bounds);
Index: Source/OpenTK/Platform/MacOS/CarbonBindings/Cgl.cs
===================================================================
--- Source/OpenTK/Platform/MacOS/CarbonBindings/Cgl.cs (revision 0)
+++ Source/OpenTK/Platform/MacOS/CarbonBindings/Cgl.cs (working copy)
@@ -0,0 +1,106 @@
+using System;
+using System.Runtime.InteropServices;
+
+namespace OpenTK.Platform.MacOS
+{
+ using CGLPixelFormat = IntPtr;
+ using CGLContext = IntPtr;
+
+ static class Cgl
+ {
+ internal enum PixelFormatBool
+ {
+ None = 0,
+ AllRenderers = 1,
+ Doublebuffer = 5,
+ Stereo = 6,
+ AuxBuffers = 7,
+ MinimumPolicy = 51,
+ MaximumPolicy = 52,
+ Offscreen = 53,
+ AuxDepthStencil = 57,
+ ColorFloat = 58,
+ Multisample = 59,
+ Supersample = 60,
+ SampleALpha = 61,
+ SingleRenderer = 71,
+ NoRecovery = 72,
+ Accelerated = 73,
+ ClosestPolicy = 74,
+ BackingStore = 76,
+ Window = 80,
+ Compliant = 83,
+ PBuffer = 90,
+ RemotePBuffer = 91,
+ }
+
+ internal enum PixelFormatInt
+ {
+ ColorSize = 8,
+ AlphaSize = 11,
+ DepthSize = 12,
+ StencilSize = 13,
+ AccumSize = 14,
+ SampleBuffers = 55,
+ Samples = 56,
+ RendererID = 70,
+ DisplayMask = 84,
+ OpenGLProfile = 99,
+ VScreenCount = 128,
+ }
+
+ internal enum OpenGLProfileVersion
+ {
+ Legacy = 0x100,
+ Core3_2 = 0x3200,
+ }
+
+ internal enum ParameterNames
+ {
+ SwapInterval = 222,
+ }
+
+ internal enum Error
+ {
+ None = 0x000,
+ }
+
+
+ const string cgl = "/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon";
+
+ [DllImport(cgl)] internal static extern Error CGLGetError();
+ [DllImport(cgl)] private static extern IntPtr CGLErrorString(Error code);
+ internal static string ErrorString(Error code)
+ {
+ return Marshal.PtrToStringAnsi(CGLErrorString(code));
+ }
+
+ [DllImport(cgl)]
+ internal static extern Error CGLChoosePixelFormat(int []attribs, ref CGLPixelFormat format, ref int numPixelFormats);
+ [DllImport(cgl)]
+ internal static extern Error CGLCreateContext(CGLPixelFormat format, CGLContext share, ref CGLContext context);
+ [DllImport(cgl)]
+ internal static extern Error CGLDestroyPixelFormat(CGLPixelFormat format);
+ [DllImport(cgl)]
+ internal static extern CGLContext CGLGetCurrentContext();
+ [DllImport(cgl)]
+ internal static extern Error CGLSetCurrentContext(CGLContext context);
+ [DllImport(cgl)]
+ internal static extern Error CGLDestroyContext(CGLContext context);
+ [DllImport(cgl)]
+ internal static extern Error CGLSetParameter(CGLContext context, int parameter, ref int value);
+ [DllImport(cgl)]
+ internal static extern Error CGLFlushDrawable(CGLContext context);
+
+ [DllImport(cgl)]
+ internal static extern int CGSMainConnectionID();
+ [DllImport(cgl)]
+ internal static extern Error CGLSetSurface(CGLContext context, int conId, int winId, int surfId);
+ [DllImport(cgl)]
+ internal static extern Error CGLUpdateContext(CGLContext context);
+ [DllImport(cgl)]
+ internal static extern Error CGSGetSurfaceCount(int conId, int winId, ref int count);
+ [DllImport(cgl)]
+ internal static extern Error CGSGetSurfaceList(int conId, int winId, int count, ref int ids, ref int filled);
+ }
+}
Property changes on: Source/OpenTK/Platform/MacOS/CarbonBindings/Cgl.cs
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: Source/OpenTK/Platform/MacOS/CarbonGLNative.cs
===================================================================
--- Source/OpenTK/Platform/MacOS/CarbonGLNative.cs (revision 3072)
+++ Source/OpenTK/Platform/MacOS/CarbonGLNative.cs (working copy)
@@ -264,7 +264,7 @@
API.HideWindow(window.WindowRef);
}
- internal void SetFullscreen(AglContext context)
+ internal void SetFullscreen(BaseContext context)
{
windowedBounds = bounds;
@@ -283,7 +283,7 @@
windowState = WindowState.Fullscreen;
}
- internal void UnsetFullscreen(AglContext context)
+ internal void UnsetFullscreen(BaseContext context)
{
context.UnsetFullScreen(window);
Index: Source/OpenTK/Platform/MacOS/CglContext.cs
===================================================================
--- Source/OpenTK/Platform/MacOS/CglContext.cs (revision 0)
+++ Source/OpenTK/Platform/MacOS/CglContext.cs (working copy)
@@ -0,0 +1,233 @@
+#region License
+//
+// The Open Toolkit Library License
+//
+// Copyright (c) 2006 - 2010 the Open Toolkit library.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights to
+// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+// the Software, and to permit persons to whom the Software is furnished to do
+// so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+using OpenTK.Graphics;
+
+namespace OpenTK.Platform.MacOS
+{
+ using CGLPixelFormat = IntPtr;
+ using CGLContext = IntPtr;
+
+ class CglContext : BaseContext
+ {
+ Cgl.Error err;
+
+ public CglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool isCoreProfile)
+ : base(mode,window,shareContext,isCoreProfile)
+ {}
+
+ public CglContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext)
+ : base(handle,window,shareContext)
+ {}
+
+ void CheckError(string function)
+ {
+ if (err == Cgl.Error.None)
+ return;
+ string message = string.Format("CGL Error from function {0}: {1} {2}", function, err, Cgl.ErrorString(err));
+ throw new MacOSException( (OSStatus)err, message );
+ }
+
+ #region BaseContext Implementation
+
+ public static void AddPixelAttrib(List<int> attribs, Cgl.PixelFormatBool pfAttrib)
+ {
+ Debug.Print(pfAttrib.ToString());
+
+ attribs.Add((int)pfAttrib);
+ }
+ public static void AddPixelAttrib(List<int> attribs, Cgl.PixelFormatInt pfAttrib, int value)
+ {
+ Debug.Print("{0} : {1}", pfAttrib, value);
+
+ attribs.Add((int)pfAttrib);
+ attribs.Add(value);
+ }
+
+ protected override void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, IntPtr shareContextRef, bool isCoreProfile)
+ {
+ List<int> attribs = new List<int>();
+
+ Debug.Print("CGL pixel format attributes:");
+ Debug.Indent();
+
+ // Choose OpenGL profile
+ int profile = (int)(isCoreProfile ? Cgl.OpenGLProfileVersion.Core3_2 : Cgl.OpenGLProfileVersion.Legacy);
+ AddPixelAttrib(attribs, Cgl.PixelFormatInt.OpenGLProfile, profile);
+
+ // Set bit per pixel values
+ AddPixelAttrib(attribs, Cgl.PixelFormatInt.ColorSize, mode.ColorFormat.BitsPerPixel);
+ AddPixelAttrib(attribs, Cgl.PixelFormatInt.AlphaSize, mode.ColorFormat.Alpha);
+ AddPixelAttrib(attribs, Cgl.PixelFormatInt.AccumSize, mode.AccumulatorFormat.BitsPerPixel);
+ AddPixelAttrib(attribs, Cgl.PixelFormatInt.DepthSize, mode.Depth);
+ AddPixelAttrib(attribs, Cgl.PixelFormatInt.StencilSize, mode.Stencil);
+
+ if (mode.Stereo)
+ AddPixelAttrib(attribs, Cgl.PixelFormatBool.Stereo);
+ if (mode.Buffers>1)
+ AddPixelAttrib(attribs, Cgl.PixelFormatBool.Doublebuffer);
+ if (mode.Samples>0)
+ {
+ AddPixelAttrib(attribs, Cgl.PixelFormatBool.Multisample);
+ AddPixelAttrib(attribs, Cgl.PixelFormatInt.Samples, mode.Samples);
+ }
+
+ AddPixelAttrib(attribs, Cgl.PixelFormatBool.None);
+
+ Debug.Unindent();
+
+ Debug.Write("Attribute array: ");
+ for (int i = 0; i < attribs.Count; i++)
+ Debug.Write(attribs[i].ToString() + " ");
+ Debug.WriteLine("");
+
+ CGLPixelFormat pixelFormat = IntPtr.Zero;
+ int numFormats = 1;
+ err = Cgl.CGLChoosePixelFormat( attribs.ToArray(), ref pixelFormat, ref numFormats);
+ CheckError("CGLChoosePixelFormat");
+
+ Debug.Print("Creating CGL context. Sharing with {0}", shareContextRef);
+
+ // create the context and share it with the share reference.
+ CGLContext context = IntPtr.Zero;
+ err = Cgl.CGLCreateContext(pixelFormat, shareContextRef, ref context);
+ CheckError("CGLCreateContext");
+ Handle = new ContextHandle(context);
+
+ // Free the pixel format from memory.
+ err = Cgl.CGLDestroyPixelFormat(pixelFormat);
+ CheckError("CGLDestroyPixelFormat");
+
+ Debug.Print("IsControl: {0}", carbonWindow.IsControl);
+ MakeCurrent(carbonWindow);
+
+ SetDrawable(carbonWindow);
+ SetBufferRect(carbonWindow);
+ Update(carbonWindow);
+
+ Debug.Print("context: {0}", Handle.Handle);
+ }
+
+ protected override void SetDrawable(CarbonWindowInfo carbonWindow)
+ {
+ return;
+ //Undocumented hack: http://vchannel.sourceforge.net/hacks.html
+ int conId = Cgl.CGSMainConnectionID();
+ int winId = Carbon.API.HIWindowGetCGWindowID( carbonWindow.WindowRef );
+ int surface = -1, filled=0, total=-1;
+ //err = Cgl.CGSGetSurfaceCount( conId, winId, ref total);
+ //CheckError("CGSGetSurfaceCount");
+ //err = Cgl.CGSGetSurfaceList(conId,winId,1,ref surface,ref filled);
+ //CheckError("CGSGetSurfaceList");
+ err = Cgl.CGLSetSurface( Context.Handle, conId, winId, 0 );
+ CheckError("CGLSetSurface");
+ }
+
+ protected override void UpdateContext(IntPtr context)
+ {
+ //err = Cgl.CGLUpdateContext(context);
+ //CheckError("CGLUpdateContext");
+ }
+
+ #endregion
+ #region IGraphicsContext Members
+
+ public override void SwapBuffers()
+ {
+ err = Cgl.CGLFlushDrawable( Handle.Handle );
+ CheckError("CGLFlushDrawable");
+ }
+
+ public override void MakeCurrent(IWindowInfo window)
+ {
+ err = Cgl.CGLSetCurrentContext( Handle.Handle );
+ CheckError("CGLSetCurrentContext");
+ }
+
+ public override bool IsCurrent
+ {
+ get { return (Handle.Handle == Cgl.CGLGetCurrentContext()); }
+ }
+
+ public override bool VSync
+ {
+ get { return mVSync; }
+ set
+ {
+ int intVal = value ? 1 : 0;
+ Cgl.CGLSetParameter(Handle.Handle, (int)Cgl.ParameterNames.SwapInterval, ref intVal);
+ mVSync = value;
+ }
+ }
+
+ #endregion
+
+ #region IDisposable Members
+
+ ~CglContext()
+ {
+ Dispose(false);
+ }
+
+ public override void Dispose()
+ {
+ Dispose(true);
+ }
+
+ void Dispose(bool disposing)
+ {
+ if (IsDisposed || Handle.Handle == IntPtr.Zero)
+ return;
+
+ Debug.Print("Disposing of CGL context.");
+ Cgl.CGLSetCurrentContext( IntPtr.Zero );
+
+ Debug.Print("Destroying context");
+ err = Cgl.CGLDestroyContext(Handle.Handle);
+ if (disposing)
+ CheckError("cglDestroyContext");
+ else if (err == Cgl.Error.None)
+ {
+ Debug.Print("Context destruction completed successfully.");
+ Handle = ContextHandle.Zero;
+ return;
+ }
+
+ // failed to destroy context.
+ Debug.WriteLine("Failed to destroy context.");
+ Debug.WriteLine( Cgl.ErrorString(err) );
+
+ IsDisposed = true;
+ }
+
+ #endregion
+ }
+}
\ No newline at end of file
Property changes on: Source/OpenTK/Platform/MacOS/CglContext.cs
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: Source/OpenTK/Platform/MacOS/MacOSFactory.cs
===================================================================
--- Source/OpenTK/Platform/MacOS/MacOSFactory.cs (revision 3072)
+++ Source/OpenTK/Platform/MacOS/MacOSFactory.cs (working copy)
@@ -56,12 +56,15 @@
public virtual IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
{
- return new AglContext(mode, window, shareContext);
+ bool isCore = (flags & GraphicsContextFlags.ForwardCompatible) == GraphicsContextFlags.ForwardCompatible;
+ return new AglContext(mode, window, shareContext, isCore);
+ return new CglContext(mode, window, shareContext, isCore);
}
public virtual IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
{
return new AglContext(handle, window, shareContext);
+ return new CglContext(handle, window, shareContext);
}
public virtual GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
@@ -68,7 +71,8 @@
{
return (GraphicsContext.GetCurrentContextDelegate)delegate
{
- return new ContextHandle(Agl.aglGetCurrentContext());
+ return new ContextHandle(Agl.aglGetCurrentContext());
+ return new ContextHandle(Cgl.CGLGetCurrentContext());
};
}
Index: Source/OpenTK/Platform/MacOS/MacOSGraphicsMode.cs
===================================================================
--- Source/OpenTK/Platform/MacOS/MacOSGraphicsMode.cs (revision 3072)
+++ Source/OpenTK/Platform/MacOS/MacOSGraphicsMode.cs (working copy)
@@ -40,9 +40,9 @@
#region IGraphicsMode Members
public GraphicsMode SelectGraphicsMode(ColorFormat color, int depth, int stencil,
- int samples, ColorFormat accum, int buffers, bool stereo)
+ int samples, ColorFormat accum, int buffers, bool stereo, bool srgb)
{
- IntPtr pixelformat = SelectPixelFormat(color, depth, stencil, samples, accum, buffers, stereo);
+ IntPtr pixelformat = SelectPixelFormat(color, depth, stencil, samples, accum, buffers, stereo, srgb);
return GetGraphicsModeFromPixelFormat(pixelformat);
}
@@ -69,12 +69,12 @@
Agl.aglDescribePixelFormat(pixelformat, Agl.PixelFormatAttribute.AGL_DOUBLEBUFFER, out buffers);
Agl.aglDescribePixelFormat(pixelformat, Agl.PixelFormatAttribute.AGL_STEREO, out stereo);
- return new GraphicsMode(pixelformat, new ColorFormat(r, g, b, a),
- depth, stencil, samples, new ColorFormat(ar, ag, ab, aa), buffers + 1, stereo != 0);
+ return new GraphicsMode(pixelformat, new ColorFormat(r, g, b, a), //todo: SRGB support
+ depth, stencil, samples, new ColorFormat(ar, ag, ab, aa), buffers + 1, stereo != 0, false);
}
IntPtr SelectPixelFormat(ColorFormat color, int depth, int stencil, int samples,
- ColorFormat accum, int buffers, bool stereo)
+ ColorFormat accum, int buffers, bool stereo, bool srgb)
{
List<int> attribs = new List<int>();
Index: Source/OpenTK/Platform/Windows/API.cs
===================================================================
--- Source/OpenTK/Platform/Windows/API.cs (revision 3072)
+++ Source/OpenTK/Platform/Windows/API.cs (working copy)
@@ -1,4 +1,4 @@
-#region --- License ---
+#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* Contributions from Erik Ylvisaker
* See license.txt for license info
@@ -3060,7 +3060,10 @@
// PixelFormatDescriptor flags for use in ChoosePixelFormat only
DEPTH_DONTCARE = unchecked((int)0x20000000),
DOUBLEBUFFER_DONTCARE = unchecked((int)0x40000000),
- STEREO_DONTCARE = unchecked((int)0x80000000)
+ STEREO_DONTCARE = unchecked((int)0x80000000),
+
+ // Ignored flags
+ SRGB_CAPABLE = 0,
}
#endregion
Index: Source/OpenTK/Platform/Windows/Bindings/WglEnums.cs
===================================================================
--- Source/OpenTK/Platform/Windows/Bindings/WglEnums.cs (revision 3072)
+++ Source/OpenTK/Platform/Windows/Bindings/WglEnums.cs (working copy)
@@ -129,8 +129,9 @@
DrawToBitmapArb = ((int)0x2002),
BlueShiftArb = ((int)0x201a),
SwapCopyArb = ((int)0x2029),
+ FramebufferSrgbCapableArb = ((int)0x20a9),
}
-
+
public enum WGL_EXT_pbuffer
{
DrawToPbufferExt = ((int)0x202d),
@@ -298,6 +299,11 @@
TypeRgbaFloatAti = ((int)0x21a0),
}
+ public enum WGL_ARB_framebuffer_sRGB
+ {
+ FramebufferSrgbCapableArb = ((int)0x20a9),
+ }
+
public enum WGL_font_type
{
FontLines = ((int)0),
@@ -476,6 +482,7 @@
Aux6Arb = ((int)0x208d),
ShareAccumExt = ((int)0x200e),
StereoArb = ((int)0x2012),
+ FramebufferSrgbCapableArb = ((int)0x20a9),
TextureRgbArb = ((int)0x2075),
AccelerationArb = ((int)0x2003),
TextureCubeMapPositiveXArb = ((int)0x207d),
Index: Source/OpenTK/Platform/Windows/WinGraphicsMode.cs
===================================================================
--- Source/OpenTK/Platform/Windows/WinGraphicsMode.cs (revision 3072)
+++ Source/OpenTK/Platform/Windows/WinGraphicsMode.cs (working copy)
@@ -68,7 +68,7 @@
#region IGraphicsMode Members
public GraphicsMode SelectGraphicsMode(ColorFormat color, int depth, int stencil, int samples,
- ColorFormat accum, int buffers, bool stereo)
+ ColorFormat accum, int buffers, bool stereo, bool srgb)
{
GraphicsMode mode = null;
do
@@ -75,10 +75,10 @@
{
mode = modes.Find(delegate(GraphicsMode current)
{
- return ModeSelector(current, color, depth, stencil, samples, accum, buffers, stereo);
+ return ModeSelector(current, color, depth, stencil, samples, accum, buffers, stereo, srgb);
});
} while (mode == null && RelaxParameters(
- ref color, ref depth, ref stencil, ref samples, ref accum, ref buffers, ref stereo));
+ ref color, ref depth, ref stencil, ref samples, ref accum, ref buffers, ref stereo, ref srgb));
if (mode == null)
mode = modes[0];
@@ -87,8 +87,9 @@
}
bool RelaxParameters(ref ColorFormat color, ref int depth, ref int stencil, ref int samples,
- ref ColorFormat accum, ref int buffers, ref bool stereo)
+ ref ColorFormat accum, ref int buffers, ref bool stereo, ref bool srgb)
{
+ if (!srgb) { srgb = true; return true; }
if (stereo) { stereo = false; return true; }
if (buffers != 2) { buffers = 2; return true; }
if (accum != 0) { accum = 0; return true; }
@@ -165,7 +166,8 @@
0,
new ColorFormat(pfd.AccumBits),
(pfd.Flags & PixelFormatDescriptorFlags.DOUBLEBUFFER) != 0 ? 2 : 1,
- (pfd.Flags & PixelFormatDescriptorFlags.STEREO) != 0);
+ (pfd.Flags & PixelFormatDescriptorFlags.STEREO) != 0,
+ (pfd.Flags & PixelFormatDescriptorFlags.SRGB_CAPABLE) != 0 );
yield return fmt;
}
@@ -179,7 +181,7 @@
IEnumerable<GraphicsMode> GetModesARB(INativeWindow native)
{
using (IGraphicsContext context = new GraphicsContext(
- new GraphicsMode(new IntPtr(2), new ColorFormat(), 0, 0, 0, new ColorFormat(), 2, false),
+ new GraphicsMode(new IntPtr(2), new ColorFormat(), 0, 0, 0, new ColorFormat(), 2, false, false),
(WinWindowInfo)native.WindowInfo, 1, 0, GraphicsContextFlags.Default))
{
WinWindowInfo window = (WinWindowInfo)native.WindowInfo;
@@ -217,6 +219,7 @@
(int)WGL_ARB_pixel_format.DoubleBufferArb,
(int)WGL_ARB_pixel_format.StereoArb,
+ (int)WGL_ARB_pixel_format.FramebufferSrgbCapableArb,
0
};
@@ -256,7 +259,8 @@
values[8] != 0 ? values[9] : 0,
new ColorFormat(values[10], values[11], values[12], values[13]),
values[15] == 1 ? 2 : 1,
- values[16] == 1 ? true : false);
+ values[16] == 1 ? true : false, //stereo
+ values[17] == 1 ? true : false); //srgb
yield return mode;
}
@@ -270,7 +274,7 @@
#region ModeSelector
bool ModeSelector(GraphicsMode current, ColorFormat color, int depth, int stencil, int samples,
- ColorFormat accum, int buffers, bool stereo)
+ ColorFormat accum, int buffers, bool stereo, bool srgb)
{
bool result =
(color != ColorFormat.Empty ? current.ColorFormat >= color : true) &&
@@ -279,7 +283,8 @@
(samples != 0 ? current.Samples >= samples : true) &&
(accum != ColorFormat.Empty ? current.AccumulatorFormat >= accum : true) &&
(buffers != 0 ? current.Buffers >= buffers : true) &&
- current.Stereo == stereo;
+ current.Stereo == stereo &&
+ current.Srgb == srgb;
return result;
}
Index: Source/OpenTK/Platform/X11/Bindings/Glx.cs
===================================================================
--- Source/OpenTK/Platform/X11/Bindings/Glx.cs (revision 3072)
+++ Source/OpenTK/Platform/X11/Bindings/Glx.cs (working copy)
@@ -134,6 +134,7 @@
LEVEL = 3,
CONFIG_CAVEAT = 0x20,
RENDER_TYPE_SGIX = 0x8011,
+ FRAMEBUFFER_SRGB_CAPABLE = 0x20B2,
}
enum GLXHyperpipeAttrib : int
Index: Source/OpenTK/Platform/X11/X11GraphicsMode.cs
===================================================================
--- Source/OpenTK/Platform/X11/X11GraphicsMode.cs (revision 3072)
+++ Source/OpenTK/Platform/X11/X11GraphicsMode.cs (working copy)
@@ -33,7 +33,7 @@
#region IGraphicsMode Members
public GraphicsMode SelectGraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum,
- int buffers, bool stereo)
+ int buffers, bool stereo, bool srgb)
{
GraphicsMode gfx;
// The actual GraphicsMode that will be selected.
@@ -42,10 +42,10 @@
// 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.
- visual = SelectVisualUsingFBConfig(color, depth, stencil, samples, accum, buffers, stereo);
+ visual = SelectVisualUsingFBConfig(color, depth, stencil, samples, accum, buffers, stereo, srgb);
if (visual == IntPtr.Zero)
- visual = SelectVisualUsingChooseVisual(color, depth, stencil, samples, accum, buffers, stereo);
+ visual = SelectVisualUsingChooseVisual(color, depth, stencil, samples, accum, buffers, stereo, srgb);
if (visual == IntPtr.Zero)
throw new GraphicsModeException("Requested GraphicsMode not available.");
@@ -69,12 +69,14 @@
Glx.GetConfig(display, ref info, GLXAttribute.DOUBLEBUFFER, out buffers);
++buffers;
// the above lines returns 0 - false and 1 - true.
- int st;
+ int st,sc;
Glx.GetConfig(display, ref info, GLXAttribute.STEREO, out st);
+ Glx.GetConfig(display, ref info, GLXAttribute.FRAMEBUFFER_SRGB_CAPABLE, out sc);
stereo = st != 0;
+ srgb = sc != 0;
gfx = new GraphicsMode(info.VisualID, new ColorFormat(r, g, b, a), depth, stencil, samples,
- new ColorFormat(ar, ag, ab, aa), buffers, stereo);
+ new ColorFormat(ar, ag, ab, aa), buffers, stereo, srgb);
using (new XLock(display))
{
@@ -91,7 +93,7 @@
// See http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.opengl/doc/openglrf/glXChooseFBConfig.htm
// for the attribute declarations. Note that the attributes are different than those used in Glx.ChooseVisual.
IntPtr SelectVisualUsingFBConfig(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum,
- int buffers, bool stereo)
+ int buffers, bool stereo, bool srgb)
{
List<int> visualAttributes = new List<int>();
IntPtr visual = IntPtr.Zero;
@@ -160,6 +162,12 @@
visualAttributes.Add((int)GLXAttribute.STEREO);
visualAttributes.Add(1);
}
+
+ if (srgb)
+ {
+ visualAttributes.Add((int)GLXAttribute.FRAMEBUFFER_SRGB_CAPABLE);
+ visualAttributes.Add(1);
+ }
visualAttributes.Add(0);
@@ -199,7 +207,7 @@
// See http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.opengl/doc/openglrf/glXChooseVisual.htm
IntPtr SelectVisualUsingChooseVisual(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum,
- int buffers, bool stereo)
+ int buffers, bool stereo, bool srgb)
{
List<int> visualAttributes = new List<int>();
@@ -258,6 +266,9 @@
if (stereo)
visualAttributes.Add((int)GLXAttribute.STEREO);
+
+ if (srgb)
+ visualAttributes.Add((int)GLXAttribute.FRAMEBUFFER_SRGB_CAPABLE);
visualAttributes.Add(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment