From 80276c70c45d15c6d972a826db6bea8a7eecee90 Mon Sep 17 00:00:00 2001 From: Glatzemann Date: Mon, 5 Dec 2011 15:55:17 +0000 Subject: [PATCH] some work on OpenGL3 RenderSystem for X11 support (Linux) --- .../GraphicsDeviceWindowsGL3.cs | 92 +++++++++++++++++-- 1 file changed, 85 insertions(+), 7 deletions(-) diff --git a/RenderSystems/ANX.Framework.Windows.GL3/GraphicsDeviceWindowsGL3.cs b/RenderSystems/ANX.Framework.Windows.GL3/GraphicsDeviceWindowsGL3.cs index d32a6645..bc502a8a 100644 --- a/RenderSystems/ANX.Framework.Windows.GL3/GraphicsDeviceWindowsGL3.cs +++ b/RenderSystems/ANX.Framework.Windows.GL3/GraphicsDeviceWindowsGL3.cs @@ -85,6 +85,41 @@ namespace ANX.Framework.Windows.GL3 public int Bottom; // y position of lower-right corner } + [DllImport("libX11")] + static extern IntPtr XCreateColormap(IntPtr display, IntPtr window, IntPtr visual, int alloc); + + [DllImport("libX11", EntryPoint = "XGetVisualInfo")] + static extern IntPtr XGetVisualInfoInternal(IntPtr display, IntPtr vinfo_mask, ref XVisualInfo template, out int nitems); + + static IntPtr XGetVisualInfo(IntPtr display, int vinfo_mask, ref XVisualInfo template, out int nitems) + { + return XGetVisualInfoInternal(display, (IntPtr)vinfo_mask, ref template, out nitems); + } + + [DllImport("libX11")] + extern static int XPending(IntPtr diplay); + + [StructLayout(LayoutKind.Sequential)] + struct XVisualInfo + { + public IntPtr Visual; + public IntPtr VisualID; + public int Screen; + public int Depth; + public int Class; + public long RedMask; + public long GreenMask; + public long blueMask; + public int ColormapSize; + public int BitsPerRgb; + + public override string ToString() + { + return String.Format("id ({0}), screen ({1}), depth ({2}), class ({3})", + VisualID, Screen, Depth, Class); + } + } + #endregion #region Private @@ -179,14 +214,45 @@ namespace ANX.Framework.Windows.GL3 break; } - nativeWindowInfo = Utilities.CreateWindowsWindowInfo(presentationParameters.DeviceWindowHandle); + GraphicsMode graphicsMode = new GraphicsMode(DatatypesMapping.SurfaceToColorFormat(presentationParameters.BackBufferFormat), + depth, + stencil, + presentationParameters.MultiSampleCount // AntiAlias Samples: 2/4/8/16/32 + ); - GraphicsMode graphicsMode = new GraphicsMode( - DatatypesMapping.SurfaceToColorFormat( - presentationParameters.BackBufferFormat), - depth, stencil, - // AntiAlias Samples: 2/4/8/16/32 - presentationParameters.MultiSampleCount); + if (OpenTK.Configuration.RunningOnWindows) + { + nativeWindowInfo = Utilities.CreateWindowsWindowInfo(presentationParameters.DeviceWindowHandle); + } + else if (OpenTK.Configuration.RunningOnX11) + { + // Use reflection to retrieve the necessary values from Mono's Windows.Forms implementation. + Type xplatui = Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms"); + if (xplatui == null) throw new PlatformNotSupportedException( + "System.Windows.Forms.XplatUIX11 missing. Unsupported platform or Mono runtime version, aborting."); + + // get the required handles from the X11 API. + IntPtr display = (IntPtr)GetStaticFieldValue(xplatui, "DisplayHandle"); + IntPtr rootWindow = (IntPtr)GetStaticFieldValue(xplatui, "RootWindow"); + int screen = (int)GetStaticFieldValue(xplatui, "ScreenNo"); + + // get the XVisualInfo for this GraphicsMode + XVisualInfo info = new XVisualInfo(); + info.VisualID = graphicsMode.Index.Value; + int dummy; + IntPtr infoPtr = XGetVisualInfo(display, 1 /* VisualInfoMask.ID */, ref info, out dummy); + info = (XVisualInfo)Marshal.PtrToStructure(infoPtr, typeof(XVisualInfo)); + + // set the X11 colormap. + SetStaticFieldValue(xplatui, "CustomVisual", info.Visual); + SetStaticFieldValue(xplatui, "CustomColormap", XCreateColormap(display, rootWindow, info.Visual, 0)); + + nativeWindowInfo = Utilities.CreateX11WindowInfo(display, screen, presentationParameters.DeviceWindowHandle, rootWindow, infoPtr); + } + else + { + throw new NotImplementedException(); + } ResizeRenderWindow(presentationParameters); @@ -420,6 +486,18 @@ namespace ANX.Framework.Windows.GL3 } } + static object GetStaticFieldValue(Type type, string fieldName) + { + return type.GetField(fieldName, + System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).GetValue(null); + } + + static void SetStaticFieldValue(Type type, string fieldName, object value) + { + type.GetField(fieldName, + System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).SetValue(null, value); + } + public void SetRenderTargets(params RenderTargetBinding[] renderTargets) { throw new NotImplementedException();