public WinGLNative(int x, int y, int width, int height, string title, DisplayDevice device) { WindowProcedureDelegate = WindowProcedure; UngroupFromTaskbar(); window = new WinWindowInfo(CreateWindow(x, y, width, height, title, device)); exists = true; }
// Note: there is no relevant ARB function. internal static void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window) { Debug.Write("Setting pixel format... "); 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.Value, API.PixelFormatDescriptorSize, ref pfd); Debug.WriteLine(mode.Index.ToString()); 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())); } }
public WinGLContext(GraphicsMode format, WinWindowInfo window) { if (window == null) { throw new ArgumentNullException("window", "Must point to a valid window."); } if (window.handle == IntPtr.Zero) { throw new ArgumentException("window", "Must be a valid window."); } Debug.Print("OpenGL will be bound to handle: {0}", window.handle); SelectGraphicsModePFD(format, (WinWindowInfo)window); Debug.Print("Setting pixel format... "); SetGraphicsModePFD(format, (WinWindowInfo)window); ContextHandle = Wgl.wglCreateContext(window.DeviceContext); if (ContextHandle == IntPtr.Zero) { ContextHandle = Wgl.wglCreateContext(window.DeviceContext); } if (ContextHandle == IntPtr.Zero) { throw new GraphicsContextException( String.Format("Context creation failed. Wgl.CreateContext() error: {0}.", Marshal.GetLastWin32Error())); } Debug.Print("success! (id: {0})", ContextHandle); }
public WinGLNative(int x, int y, int width, int height, string title, GameWindowFlags options, DisplayDevice device) { // This is the main window procedure callback. We need the callback in order to create the window, so // don't move it below the CreateWindow calls. WindowProcedureDelegate = WindowProcedure; //// This timer callback is called periodically when the window enters a sizing / moving modal loop. //ModalLoopCallback = delegate(IntPtr handle, WindowMessage msg, UIntPtr eventId, int time) //{ // // Todo: find a way to notify the frontend that it should process queued up UpdateFrame/RenderFrame events. // if (Move != null) // Move(this, EventArgs.Empty); //}; // To avoid issues with Ati drivers on Windows 6+ with compositing enabled, the context will not be // bound to the top-level window, but rather to a child window docked in the parent. window = new WinWindowInfo( CreateWindow(x, y, width, height, title, options, device, IntPtr.Zero), null); child_window = new WinWindowInfo( CreateWindow(0, 0, ClientSize.Width, ClientSize.Height, title, options, device, window.WindowHandle), window); exists = true; keyboard.Description = "Standard Windows keyboard"; keyboard.NumberOfFunctionKeys = 12; keyboard.NumberOfKeys = 101; keyboard.NumberOfLeds = 3; mouse.Description = "Standard Windows mouse"; mouse.NumberOfButtons = 3; mouse.NumberOfWheels = 1; keyboards.Add(keyboard); mice.Add(mouse); }
private IEnumerable <GraphicsMode> GetModesPFD(INativeWindow native) { WinWindowInfo window = native.WindowInfo as WinWindowInfo; IntPtr deviceContext = window.DeviceContext; PixelFormatDescriptor pfd = new PixelFormatDescriptor(); pfd.Size = API.PixelFormatDescriptorSize; pfd.Version = API.PixelFormatDescriptorVersion; pfd.Flags = PixelFormatDescriptorFlags.DRAW_TO_WINDOW | PixelFormatDescriptorFlags.SUPPORT_OPENGL; if (Environment.OSVersion.Version.Major >= 6) { pfd.Flags |= PixelFormatDescriptorFlags.SUPPORT_COMPOSITION; } // ISSUE: reference to a compiler-generated field this.\u003C\u003E7__wrapc = new bool[2] { false, true }; bool[] flagArray; for (int index = 0; index < flagArray.Length; ++index) { bool generic_allowed = flagArray[index]; int pixel = 0; while (WinGraphicsMode.DescribePixelFormat(deviceContext, ++pixel, (int)API.PixelFormatDescriptorSize, ref pfd) != 0) { if (generic_allowed || (pfd.Flags & PixelFormatDescriptorFlags.GENERIC_FORMAT) == (PixelFormatDescriptorFlags)0) { GraphicsMode fmt = new GraphicsMode(new IntPtr?((IntPtr)pixel), new ColorFormat((int)pfd.RedBits, (int)pfd.GreenBits, (int)pfd.BlueBits, (int)pfd.AlphaBits), (int)pfd.DepthBits, (int)pfd.StencilBits, 0, new ColorFormat((int)pfd.AccumBits), (pfd.Flags & PixelFormatDescriptorFlags.DOUBLEBUFFER) != (PixelFormatDescriptorFlags)0 ? 2 : 1, (pfd.Flags & PixelFormatDescriptorFlags.STEREO) != (PixelFormatDescriptorFlags)0); yield return(fmt); } } } }
public override void MakeCurrent(IWindowInfo window) { lock (LoadLock) { bool success; WinWindowInfo wnd = window as WinWindowInfo; if (wnd != null) { if (wnd.Handle == IntPtr.Zero) { throw new ArgumentException("window", "Must point to a valid window."); } success = Wgl.MakeCurrent(wnd.DeviceContext, Handle.Handle); DeviceContext = wnd.DeviceContext; } else { success = Wgl.MakeCurrent(IntPtr.Zero, IntPtr.Zero); DeviceContext = IntPtr.Zero; } if (!success) { throw new GraphicsContextException(String.Format( "Failed to make context {0} current. Error: {1}", this, Marshal.GetLastWin32Error())); } } }
// Note: there is no relevant ARB function. internal static GraphicsMode SetGraphicsModePFD(WinGraphicsMode mode_selector, GraphicsMode mode, WinWindowInfo window) { Debug.Write("Setting pixel format... "); if (window == null) { throw new ArgumentNullException("window", "Must point to a valid window."); } if (!mode.Index.HasValue) { mode = mode_selector.SelectGraphicsMode( mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo); } PixelFormatDescriptor pfd = new PixelFormatDescriptor(); 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.Value, ref pfd)) { throw new GraphicsContextException(String.Format( "Requested GraphicsMode not available. SetPixelFormat error: {0}", Marshal.GetLastWin32Error())); } return(mode); }
internal WinRawInput(WinWindowInfo parent) { Debug.WriteLine("Initalizing windows raw input driver."); Debug.Indent(); AssignHandle(parent.WindowHandle); Debug.Print("Input window attached to parent {0}", parent); keyboardDriver = new WinRawKeyboard(this.Handle); mouseDriver = new WinRawMouse(this.Handle); Debug.Unindent(); //AllocateBuffer(); }
public WinInputBase() { WndProc = WndProcHandler; Parent = new WinWindowInfo(NativeWindow.OsuWindowHandle, null); CreateDrivers(); // Subclass the window to retrieve the events we are interested in. OldWndProc = Functions.SetWindowLong(Parent.Handle, WndProc); Debug.Print("Input window attached to {0}", Parent); }
internal WinRawInput(WinWindowInfo parent) { Debug.WriteLine("Initalizing windows raw input driver."); Debug.Indent(); AssignHandle(parent.WindowHandle); Debug.Print("Input window attached to parent {0}", parent); keyboardDriver = new WinRawKeyboard(this.Handle); mouseDriver = new WinRawMouse(this.Handle); Debug.Unindent(); //AllocateBuffer(); }
public WinGLNative(int x, int y, int width, int height, string title, GameWindowFlags options, DisplayDevice device) { WindowProcedureDelegate = WindowProcedure; // To avoid issues with Ati drivers on Windows 6+ with compositing enabled, the context will not be // bound to the top-level window, but rather to a child window docked in the parent. window = new WinWindowInfo( CreateWindow(x, y, width, height, title, options, device, IntPtr.Zero), null); child_window = new WinWindowInfo( CreateWindow(0, 0, ClientSize.Width, ClientSize.Height, title, options, device, window.WindowHandle), window); exists = true; }
public WinGLContext(ContextHandle handle, WinWindowInfo window, IGraphicsContext sharedContext, int major, int minor, GraphicsContextFlags flags) { if (handle == ContextHandle.Zero) { throw new ArgumentException("handle"); } if (window == null) { throw new ArgumentNullException("window"); } this.Handle = handle; }
public WinGLNative(int x, int y, int width, int height, string title, GameWindowFlags options, DisplayDevice device) { WindowProcedureDelegate = WindowProcedure; // To avoid issues with Ati drivers on Windows 6+ with compositing enabled, the context will not be // bound to the top-level window, but rather to a child window docked in the parent. window = new WinWindowInfo( CreateWindow(x, y, width, height, title, options, device, IntPtr.Zero), null); child_window = new WinWindowInfo( CreateWindow(0, 0, ClientSize.Width, ClientSize.Height, title, options, device, window.WindowHandle), window); exists = true; }
private static unsafe IntPtr RegisterForDeviceNotifications(WinWindowInfo parent) { BroadcastDeviceInterface type = new BroadcastDeviceInterface(); type.Size = BlittableValueType.StrideOf <BroadcastDeviceInterface>(type); type.DeviceType = DeviceBroadcastType.INTERFACE; type.ClassGuid = WinRawInput.DeviceInterfaceHid; IntPtr num1 = Functions.RegisterDeviceNotification(parent.WindowHandle, new IntPtr((void *)&type), DeviceNotification.WINDOW_HANDLE); int num2 = num1 == IntPtr.Zero ? 1 : 0; return(num1); }
public TemporaryContext(IntPtr native) { Debug.WriteLine("[WGL] Creating temporary context to load extensions"); if (native == null) { throw new ArgumentNullException(); } // Create temporary context and load WGL entry points // First, set a compatible pixel format to the device context // of the temp window WinWindowInfo window = new WinWindowInfo { Handle = native }; WinGraphicsMode selector = new WinGraphicsMode(window.DeviceContext); WinGLContext.SetGraphicsModePFD(selector, GraphicsMode.Default, window); bool success = false; // Then, construct a temporary context and load all wgl extensions Context = new ContextHandle(Wgl.CreateContext(window.DeviceContext)); if (Context != ContextHandle.Zero) { // Make the context current. // Note: on some video cards and on some virtual machines, wglMakeCurrent // may fail with an errorcode of 6 (INVALID_HANDLE). The suggested workaround // is to call wglMakeCurrent in a loop until it succeeds. // See https://www.opengl.org/discussion_boards/showthread.php/171058-nVidia-wglMakeCurrent()-multiple-threads // Sigh... for (int retry = 0; retry < 5 && !success; retry++) { success = Wgl.MakeCurrent(window.DeviceContext, Context.Handle); if (!success) { Debug.Print("wglMakeCurrent failed with error: {0}. Retrying", Marshal.GetLastWin32Error()); System.Threading.Thread.Sleep(10); } } } else { Debug.Print("[WGL] CreateContext failed with error: {0}", Marshal.GetLastWin32Error()); } if (!success) { Debug.WriteLine("[WGL] Failed to create temporary context"); } }
public WinGLContext(ContextHandle handle, WinWindowInfo window, IGraphicsContext sharedContext, int major, int minor, GraphicsContextFlags flags, DesktopBackend backEnd) : base(backEnd) { if (handle == ContextHandle.Zero) { throw new ArgumentException("handle"); } if (window == null) { throw new ArgumentNullException("window"); } Handle = handle; }
IEnumerable <GraphicsMode> GetModesPFD(INativeWindow native) { WinWindowInfo window = native.WindowInfo as WinWindowInfo; IntPtr deviceContext = ((WinWindowInfo)window).DeviceContext; Debug.WriteLine(String.Format("Device context: {0}", deviceContext)); Debug.WriteLine("Retrieving PFD pixel formats... "); PixelFormatDescriptor pfd = new PixelFormatDescriptor(); pfd.Size = API.PixelFormatDescriptorSize; pfd.Version = API.PixelFormatDescriptorVersion; pfd.Flags = PixelFormatDescriptorFlags.SUPPORT_OPENGL | PixelFormatDescriptorFlags.DRAW_TO_WINDOW; // Make sure we don't turn off Aero on Vista and newer. if (Environment.OSVersion.Version.Major >= 6) { pfd.Flags |= PixelFormatDescriptorFlags.SUPPORT_COMPOSITION; } foreach (bool generic_allowed in new bool[] { false, true }) { // Iterate through all accelerated formats first. Afterwards, iterate through non-accelerated formats. // This should fix issue #2224, which causes OpenTK to fail on VMs without hardware acceleration. int pixel = 0; while (DescribePixelFormat(deviceContext, ++pixel, API.PixelFormatDescriptorSize, ref pfd) != 0) { // Ignore non-accelerated formats. if (!generic_allowed && (pfd.Flags & PixelFormatDescriptorFlags.GENERIC_FORMAT) != 0) { continue; } GraphicsMode fmt = new GraphicsMode((IntPtr)pixel, new ColorFormat(pfd.RedBits, pfd.GreenBits, pfd.BlueBits, pfd.AlphaBits), pfd.DepthBits, pfd.StencilBits, 0, new ColorFormat(pfd.AccumBits), (pfd.Flags & PixelFormatDescriptorFlags.DOUBLEBUFFER) != 0 ? 2 : 1, (pfd.Flags & PixelFormatDescriptorFlags.STEREO) != 0); yield return(fmt); } } }
public override bool Equals(object obj) { if (obj == null || this.GetType() != obj.GetType()) { return(false); } WinWindowInfo winWindowInfo = (WinWindowInfo)obj; if (winWindowInfo == null) { return(false); } else { return(this.handle.Equals((object)winWindowInfo.handle)); } }
public WMInput(WinWindowInfo parent) { Debug.WriteLine("Initalizing WMInput driver."); Debug.Indent(); AssignHandle(parent.WindowHandle); Debug.Print("Input window attached to parent {0}", parent); Debug.Unindent(); keyboard.Description = "Standard Windows keyboard"; keyboard.NumberOfFunctionKeys = 12; keyboard.NumberOfKeys = 101; keyboard.NumberOfLeds = 3; mouse.Description = "Standard Windows mouse"; mouse.NumberOfButtons = 3; mouse.NumberOfWheels = 1; keyboards.Add(keyboard); mice.Add(mouse); }
static IntPtr RegisterForDeviceNotifications(WinWindowInfo parent) { IntPtr dev_notify_handle; BroadcastDeviceInterface bdi = new BroadcastDeviceInterface(); bdi.Size = BlittableValueType.StrideOf(bdi); bdi.DeviceType = DeviceBroadcastType.INTERFACE; bdi.ClassGuid = DeviceInterfaceHid; unsafe { dev_notify_handle = Functions.RegisterDeviceNotification(parent.WindowHandle, new IntPtr((void*)&bdi), DeviceNotification.WINDOW_HANDLE); } if (dev_notify_handle == IntPtr.Zero) Debug.Print("[Warning] Failed to register for device notifications. Error: {0}", Marshal.GetLastWin32Error()); return dev_notify_handle; }
public WMInput(WinWindowInfo parent) { Debug.WriteLine("Initalizing WMInput driver."); Debug.Indent(); AssignHandle(parent.WindowHandle); Debug.Print("Input window attached to parent {0}", parent); Debug.Unindent(); keyboard.Description = "Standard Windows keyboard"; keyboard.NumberOfFunctionKeys = 12; keyboard.NumberOfKeys = 101; keyboard.NumberOfLeds = 3; mouse.Description = "Standard Windows mouse"; mouse.NumberOfButtons = 3; mouse.NumberOfWheels = 1; keyboards.Add(keyboard); mice.Add(mouse); }
INativeWindow ConstructMessageWindow() { Debug.WriteLine("Initializing input driver."); Debug.Indent(); // Create a new message-only window to retrieve WM_INPUT messages. INativeWindow native = new NativeWindow(); native.ProcessEvents(); WinWindowInfo parent = native.WindowInfo as WinWindowInfo; Functions.SetParent(parent.Handle, Constants.MESSAGE_ONLY); native.ProcessEvents(); Debug.Unindent(); return(native); }
private void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window) { 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 formatDescriptor = new PixelFormatDescriptor(); Functions.DescribePixelFormat(window.DeviceContext, (int)mode.Index.Value, (int)API.PixelFormatDescriptorSize, ref formatDescriptor); if (!Functions.SetPixelFormat(window.DeviceContext, (int)mode.Index.Value, ref formatDescriptor)) { throw new GraphicsContextException(string.Format("Requested GraphicsMode not available. SetPixelFormat error: {0}", (object)Marshal.GetLastWin32Error())); } }
public WinGLNative(int x, int y, int width, int height, string title, GameWindowFlags options, DisplayDevice device) { lock (WinGLNative.SyncRoot) { this.WindowProcedureDelegate = new WindowProcedure(this.WindowProcedure); this.window = new WinWindowInfo(this.CreateWindow(x, y, width, height, title, options, device, IntPtr.Zero), (WinWindowInfo)null); this.child_window = new WinWindowInfo(this.CreateWindow(0, 0, this.ClientSize.Width, this.ClientSize.Height, title, options, device, this.window.WindowHandle), this.window); this.exists = true; this.keyboard.Description = "Standard Windows keyboard"; this.keyboard.NumberOfFunctionKeys = 12; this.keyboard.NumberOfKeys = 101; this.keyboard.NumberOfLeds = 3; this.mouse.Description = "Standard Windows mouse"; this.mouse.NumberOfButtons = 3; this.mouse.NumberOfWheels = 1; this.keyboards.Add(this.keyboard); this.mice.Add(this.mouse); } }
/// <summary>Checks if <c>this</c> and <c>obj</c> reference the same win32 window.</summary> /// <param name="obj">The object to check against.</param> /// <returns>True if <c>this</c> and <c>obj</c> reference the same win32 window; false otherwise.</returns> public override bool Equals(object obj) { if (obj == null) { return(false); } if (this.GetType() != obj.GetType()) { return(false); } WinWindowInfo info = (WinWindowInfo)obj; if (info == null) { return(false); } // TODO: Assumes windows will always have unique handles. return(handle.Equals(info.handle)); }
static IntPtr RegisterForDeviceNotifications(WinWindowInfo parent) { IntPtr dev_notify_handle; BroadcastDeviceInterface bdi = new BroadcastDeviceInterface(); bdi.Size = BlittableValueType.StrideOf(bdi); bdi.DeviceType = DeviceBroadcastType.INTERFACE; bdi.ClassGuid = DeviceInterfaceHid; unsafe { dev_notify_handle = Functions.RegisterDeviceNotification(parent.WindowHandle, new IntPtr((void *)&bdi), DeviceNotification.WINDOW_HANDLE); } if (dev_notify_handle == IntPtr.Zero) { Debug.Print("[Warning] Failed to register for device notifications. Error: {0}", Marshal.GetLastWin32Error()); } return(dev_notify_handle); }
void SelectGraphicsModePFD(GraphicsMode format, WinWindowInfo window) { IntPtr deviceContext = window.DeviceContext; Debug.Print("Device context: {0}", deviceContext); ColorFormat color = format.ColorFormat; Debug.Print("Selecting pixel format PFD... "); PixelFormatDescriptor pfd = new PixelFormatDescriptor(); pfd.Size = PixelFormatDescriptor.DefaultSize; pfd.Version = PixelFormatDescriptor.DefaultVersion; pfd.Flags = PixelFormatDescriptorFlags.SUPPORT_OPENGL | PixelFormatDescriptorFlags.DRAW_TO_WINDOW; pfd.ColorBits = (byte)(color.Red + color.Green + color.Blue); pfd.PixelType = color.IsIndexed ? PixelType.INDEXED : PixelType.RGBA; pfd.RedBits = (byte)color.Red; pfd.GreenBits = (byte)color.Green; pfd.BlueBits = (byte)color.Blue; pfd.AlphaBits = (byte)color.Alpha; pfd.DepthBits = (byte)format.Depth; pfd.StencilBits = (byte)format.Stencil; if (format.Depth <= 0) { pfd.Flags |= PixelFormatDescriptorFlags.DEPTH_DONTCARE; } if (format.Buffers > 1) { pfd.Flags |= PixelFormatDescriptorFlags.DOUBLEBUFFER; } modeIndex = API.ChoosePixelFormat(deviceContext, ref pfd); if (modeIndex == 0) { throw new GraphicsModeException("The requested GraphicsMode is not available."); } }
void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window) { // Find out what we really got as a format: IntPtr deviceContext = window.DeviceContext; PixelFormatDescriptor pfd = new PixelFormatDescriptor(); pfd.Size = PixelFormatDescriptor.DefaultSize; pfd.Version = PixelFormatDescriptor.DefaultVersion; API.DescribePixelFormat(deviceContext, modeIndex, pfd.Size, ref pfd); Mode = new GraphicsMode( new ColorFormat(pfd.RedBits, pfd.GreenBits, pfd.BlueBits, pfd.AlphaBits), pfd.DepthBits, pfd.StencilBits, (pfd.Flags & PixelFormatDescriptorFlags.DOUBLEBUFFER) != 0 ? 2 : 1); Debug.Print("WGL mode index: " + modeIndex.ToString()); if (!API.SetPixelFormat(window.DeviceContext, modeIndex, ref pfd)) { throw new GraphicsContextException(String.Format( "Requested GraphicsMode not available. SetPixelFormat error: {0}", Marshal.GetLastWin32Error())); } }
public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext, int major, int minor, GraphicsContextFlags flags) { if (window == null) throw new ArgumentNullException("window", "Must point to a valid window."); if (window.WindowHandle == IntPtr.Zero) throw new ArgumentException("window", "Must be a valid window."); Mode = format; Debug.Print("OpenGL will be bound to handle: {0}", window.WindowHandle); Debug.Write("Setting pixel format... "); this.SetGraphicsModePFD(format, (WinWindowInfo)window); if (!wgl_loaded) { // We need to create a temp context in order to load wgl extensions (e.g. for multisampling or GL3). // We cannot rely on OpenTK.Platform.Wgl until we create the context and call Wgl.LoadAll(). Debug.Print("Creating temporary context for wgl extensions."); ContextHandle temp_context = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); Wgl.Imports.MakeCurrent(window.DeviceContext, temp_context.Handle); Wgl.LoadAll(); Wgl.MakeCurrent(IntPtr.Zero, IntPtr.Zero); Wgl.DeleteContext(temp_context.Handle); wgl_loaded = true; } if (Wgl.Delegates.wglCreateContextAttribsARB != null) { try { Debug.Write("Using WGL_ARB_create_context... "); List<int> attributes = new List<int>(); attributes.Add((int)ArbCreateContext.MajorVersion); attributes.Add(major); attributes.Add((int)ArbCreateContext.MinorVersion); attributes.Add(minor); if (flags != 0) { attributes.Add((int)ArbCreateContext.Flags); attributes.Add((int)flags); } attributes.Add(0); Handle = new ContextHandle( Wgl.Arb.CreateContextAttribs( window.DeviceContext, sharedContext != null ? (sharedContext as IGraphicsContextInternal).Context.Handle : IntPtr.Zero, attributes.ToArray())); if (Handle == ContextHandle.Zero) Debug.Print("failed. (Error: {0})", Marshal.GetLastWin32Error()); else Debug.Print("success!"); } catch (EntryPointNotFoundException e) { Debug.Print(e.ToString()); } catch (NullReferenceException e) { Debug.Print(e.ToString()); } } if (Handle == ContextHandle.Zero) { // Failed to create GL3-level context, fall back to GL2. Debug.Write("Falling back to GL2... "); Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); if (Handle == ContextHandle.Zero) Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); if (Handle == ContextHandle.Zero) throw new GraphicsContextException( String.Format("Context creation failed. Wgl.CreateContext() error: {0}.", Marshal.GetLastWin32Error())); } Debug.WriteLine(String.Format("success! (id: {0})", Handle)); if (sharedContext != null) { Debug.Print("Sharing state with context {0}", sharedContext.ToString()); Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, Handle.Handle); } }
public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext, int major, int minor, GraphicsContextFlags flags) { lock (WinGLContext.SyncRoot) { if (window == null) throw new ArgumentNullException("window", "Must point to a valid window."); if (window.WindowHandle == IntPtr.Zero) throw new ArgumentException("window", "Must be a valid window."); this.Mode = format; this.SetGraphicsModePFD(format, window); lock (WinGLContext.LoadLock) { if (!WinGLContext.wgl_loaded) { ContextHandle local_0 = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); Wgl.Imports.MakeCurrent(window.DeviceContext, local_0.Handle); Wgl.LoadAll(); Wgl.Imports.MakeCurrent(IntPtr.Zero, IntPtr.Zero); Wgl.Imports.DeleteContext(local_0.Handle); WinGLContext.wgl_loaded = true; } if (Wgl.Delegates.wglCreateContextAttribsARB != null) { try { List<int> local_1 = new List<int>(); local_1.Add(8337); local_1.Add(major); local_1.Add(8338); local_1.Add(minor); if (flags != GraphicsContextFlags.Default) { local_1.Add(8340); local_1.Add((int) flags); } local_1.Add(0); local_1.Add(0); this.Handle = new ContextHandle(Wgl.Arb.CreateContextAttribs(window.DeviceContext, sharedContext != null ? (sharedContext as IGraphicsContextInternal).Context.Handle : IntPtr.Zero, local_1.ToArray())); int temp_84 = this.Handle == ContextHandle.Zero ? 1 : 0; } catch (EntryPointNotFoundException exception_0) { } catch (NullReferenceException exception_1) { } } } if (this.Handle == ContextHandle.Zero) { this.Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); if (this.Handle == ContextHandle.Zero) this.Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); if (this.Handle == ContextHandle.Zero) throw new GraphicsContextException(string.Format("Context creation failed. Wgl.CreateContext() error: {0}.", (object) Marshal.GetLastWin32Error())); } if (sharedContext == null) return; Marshal.GetLastWin32Error(); Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, this.Handle.Handle); } }
public WinGLNative(int x, int y, int width, int height, string title, GameWindowFlags options, DisplayDevice device) { lock (SyncRoot) { // This is the main window procedure callback. We need the callback in order to create the window, so // don't move it below the CreateWindow calls. WindowProcedureDelegate = WindowProcedure; //// This timer callback is called periodically when the window enters a sizing / moving modal loop. //ModalLoopCallback = delegate(IntPtr handle, WindowMessage msg, UIntPtr eventId, int time) //{ // // Todo: find a way to notify the frontend that it should process queued up UpdateFrame/RenderFrame events. // if (Move != null) // Move(this, EventArgs.Empty); //}; // To avoid issues with Ati drivers on Windows 6+ with compositing enabled, the context will not be // bound to the top-level window, but rather to a child window docked in the parent. window = new WinWindowInfo( CreateWindow(x, y, width, height, title, options, device, IntPtr.Zero), null); child_window = new WinWindowInfo( CreateWindow(0, 0, ClientSize.Width, ClientSize.Height, title, options, device, window.WindowHandle), window); exists = true; keyboard.Description = "Standard Windows keyboard"; keyboard.NumberOfFunctionKeys = 12; keyboard.NumberOfKeys = 101; keyboard.NumberOfLeds = 3; mouse.Description = "Standard Windows mouse"; mouse.NumberOfButtons = 3; mouse.NumberOfWheels = 1; keyboards.Add(keyboard); mice.Add(mouse); } }
static WinGLContext() { lock (LoadLock) { // Dynamically load opengl32.dll in order to use the extension loading capabilities of Wgl. if (opengl32Handle == IntPtr.Zero) { opengl32Handle = Functions.LoadLibrary(opengl32Name); if (opengl32Handle == IntPtr.Zero) { throw new ApplicationException(String.Format("LoadLibrary(\"{0}\") call failed with code {1}", opengl32Name, Marshal.GetLastWin32Error())); } Debug.WriteLine(String.Format("Loaded opengl32.dll: {0}", opengl32Handle)); } // We need to create a temp context in order to load // wgl extensions (e.g. for multisampling or GL3). // We cannot rely on OpenTK.Platform.Wgl until we // create the context and call Wgl.LoadAll(). Debug.Print("Creating temporary context for wgl extensions."); using (INativeWindow native = new NativeWindow()) { // Create temporary context and load WGL entry points // First, set a compatible pixel format to the device context // of the temp window WinWindowInfo window = native.WindowInfo as WinWindowInfo; WinGraphicsMode selector = new WinGraphicsMode(window.DeviceContext); SetGraphicsModePFD(selector, GraphicsMode.Default, window); // Then, construct a temporary context and load all wgl extensions ContextHandle temp_context = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); if (temp_context != ContextHandle.Zero) { // Make the context current. // Note: on some video cards and on some virtual machines, wglMakeCurrent // may fail with an errorcode of 6 (INVALID_HANDLE). The suggested workaround // is to call wglMakeCurrent in a loop until it succeeds. // See https://www.opengl.org/discussion_boards/showthread.php/171058-nVidia-wglMakeCurrent()-multiple-threads // Sigh... for (int retry = 0; retry < 5; retry++) { bool success = Wgl.Imports.MakeCurrent(window.DeviceContext, temp_context.Handle); if (!success) { Debug.Print("wglMakeCurrent failed with error: {0}. Retrying", Marshal.GetLastWin32Error()); System.Threading.Thread.Sleep(10); } else { // wglMakeCurrent succeeded, we are done here! break; } } // Load wgl extensions and destroy temporary context Wgl.LoadAll(); Wgl.Imports.MakeCurrent(IntPtr.Zero, IntPtr.Zero); Wgl.Imports.DeleteContext(temp_context.Handle); } else { Debug.Print("wglCreateContext failed with error: {0}", Marshal.GetLastWin32Error()); } } } }
private static unsafe IntPtr RegisterForDeviceNotifications(WinWindowInfo parent) { BroadcastDeviceInterface type = new BroadcastDeviceInterface(); type.Size = BlittableValueType.StrideOf<BroadcastDeviceInterface>(type); type.DeviceType = DeviceBroadcastType.INTERFACE; type.ClassGuid = WinRawInput.DeviceInterfaceHid; IntPtr num1 = Functions.RegisterDeviceNotification(parent.WindowHandle, new IntPtr((void*) &type), DeviceNotification.WINDOW_HANDLE); int num2 = num1 == IntPtr.Zero ? 1 : 0; return num1; }
// Note: there is no relevant ARB function. void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window) { Debug.Write("Setting pixel format... "); 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.Value, API.PixelFormatDescriptorSize, ref pfd); Debug.WriteLine(mode.Index.ToString()); 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())); }
private void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window) { 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 formatDescriptor = new PixelFormatDescriptor(); Functions.DescribePixelFormat(window.DeviceContext, (int) mode.Index.Value, (int) API.PixelFormatDescriptorSize, ref formatDescriptor); if (!Functions.SetPixelFormat(window.DeviceContext, (int) mode.Index.Value, ref formatDescriptor)) throw new GraphicsContextException(string.Format("Requested GraphicsMode not available. SetPixelFormat error: {0}", (object) Marshal.GetLastWin32Error())); }
GraphicsMode SelectGraphicsModePFD(ColorDepth color, int depth, int stencil, int samples, ColorDepth accum, int buffers, bool stereo) { using (Control native_window = new Control()) using (WinWindowInfo window = new WinWindowInfo(native_window.Handle, null)) { IntPtr deviceContext = ((WinWindowInfo)window).DeviceContext; Debug.WriteLine(String.Format("Device context: {0}", deviceContext)); Debug.Write("Selecting pixel format... "); PixelFormatDescriptor pixelFormat = new PixelFormatDescriptor(); pixelFormat.Size = API.PixelFormatDescriptorSize; pixelFormat.Version = API.PixelFormatDescriptorVersion; pixelFormat.Flags = PixelFormatDescriptorFlags.SUPPORT_OPENGL | PixelFormatDescriptorFlags.DRAW_TO_WINDOW; pixelFormat.ColorBits = (byte)(color.Red + color.Green + color.Blue); pixelFormat.PixelType = color.IsIndexed ? PixelType.INDEXED : PixelType.RGBA; pixelFormat.RedBits = (byte)color.Red; pixelFormat.GreenBits = (byte)color.Green; pixelFormat.BlueBits = (byte)color.Blue; pixelFormat.AlphaBits = (byte)color.Alpha; if (accum.BitsPerPixel > 0) { pixelFormat.AccumBits = (byte)(accum.Red + accum.Green + accum.Blue); pixelFormat.AccumRedBits = (byte)accum.Red; pixelFormat.AccumGreenBits = (byte)accum.Green; pixelFormat.AccumBlueBits = (byte)accum.Blue; pixelFormat.AccumAlphaBits = (byte)accum.Alpha; } pixelFormat.DepthBits = (byte)depth; pixelFormat.StencilBits = (byte)stencil; if (depth <= 0) { pixelFormat.Flags |= PixelFormatDescriptorFlags.DEPTH_DONTCARE; } if (stereo) { pixelFormat.Flags |= PixelFormatDescriptorFlags.STEREO; } if (buffers > 1) { pixelFormat.Flags |= PixelFormatDescriptorFlags.DOUBLEBUFFER; } int pixel = Functions.ChoosePixelFormat(deviceContext, ref pixelFormat); if (pixel == 0) { throw new GraphicsModeException("The requested GraphicsMode is not available."); } // Find out what we really got as a format: PixelFormatDescriptor pfd = new PixelFormatDescriptor(); pixelFormat.Size = API.PixelFormatDescriptorSize; pixelFormat.Version = API.PixelFormatDescriptorVersion; Functions.DescribePixelFormat(deviceContext, pixel, API.PixelFormatDescriptorSize, ref pfd); GraphicsMode fmt = new GraphicsMode((IntPtr)pixel, new ColorDepth(pfd.RedBits, pfd.GreenBits, pfd.BlueBits, pfd.AlphaBits), pfd.DepthBits, pfd.StencilBits, 0, new ColorDepth(pfd.AccumBits), (pfd.Flags & PixelFormatDescriptorFlags.DOUBLEBUFFER) != 0 ? 2 : 1, (pfd.Flags & PixelFormatDescriptorFlags.STEREO) != 0); return(fmt); } }
GraphicsMode SelectGraphicsModeARB(ColorDepth color, int depth, int stencil, int samples, ColorDepth accum, int buffers, bool stereo) { using (INativeWindow native_window = new NativeWindow()) using (IGraphicsContext context = new GraphicsContext(new GraphicsMode(new ColorFormat(), 0, 0, 0, new ColorFormat(), 2, false), native_window.WindowInfo, 1, 0, GraphicsContextFlags.Default)) { WinWindowInfo window = (WinWindowInfo)native_window.WindowInfo; Debug.Write("Selecting pixel format (ARB)... "); if (Wgl.Delegates.wglChoosePixelFormatARB == null || Wgl.Delegates.wglGetPixelFormatAttribivARB == null) { Debug.WriteLine("failed"); return(null); } int[] attribs = new int[] { (int)WGL_ARB_pixel_format.AccelerationArb, (int)WGL_ARB_pixel_format.AlphaBitsArb, (int)WGL_ARB_pixel_format.RedBitsArb, (int)WGL_ARB_pixel_format.GreenBitsArb, (int)WGL_ARB_pixel_format.BlueBitsArb, (int)WGL_ARB_pixel_format.ColorBitsArb, (int)WGL_ARB_pixel_format.DepthBitsArb, (int)WGL_ARB_pixel_format.StencilBitsArb, (int)WGL_ARB_multisample.SampleBuffersArb, (int)WGL_ARB_multisample.SamplesArb, (int)WGL_ARB_pixel_format.AccumAlphaBitsArb, (int)WGL_ARB_pixel_format.AccumRedBitsArb, (int)WGL_ARB_pixel_format.AccumGreenBitsArb, (int)WGL_ARB_pixel_format.AccumBlueBitsArb, (int)WGL_ARB_pixel_format.AccumBitsArb, (int)WGL_ARB_pixel_format.DoubleBufferArb, (int)WGL_ARB_pixel_format.StereoArb, 0 }; int[] values = new int[attribs.Length]; int[] attribs_values = new int[] { (int)WGL_ARB_pixel_format.AccelerationArb, (int)WGL_ARB_pixel_format.FullAccelerationArb, (int)WGL_ARB_pixel_format.RedBitsArb, color.Red, (int)WGL_ARB_pixel_format.GreenBitsArb, color.Green, (int)WGL_ARB_pixel_format.BlueBitsArb, color.Blue, (int)WGL_ARB_pixel_format.AlphaBitsArb, color.Alpha, (int)WGL_ARB_pixel_format.ColorBitsArb, color.BitsPerPixel, (int)WGL_ARB_pixel_format.DepthBitsArb, depth, (int)WGL_ARB_pixel_format.StencilBitsArb, stencil, (int)WGL_ARB_multisample.SampleBuffersArb, samples > 0 ? 1 : 0, (int)WGL_ARB_multisample.SamplesArb, samples, (int)WGL_ARB_pixel_format.AccumRedBitsArb, accum.Red, (int)WGL_ARB_pixel_format.AccumGreenBitsArb, accum.Green, (int)WGL_ARB_pixel_format.AccumBlueBitsArb, accum.Blue, (int)WGL_ARB_pixel_format.AccumAlphaBitsArb, accum.Alpha, (int)WGL_ARB_pixel_format.AccumBitsArb, accum.BitsPerPixel, (int)WGL_ARB_pixel_format.DoubleBufferArb, 1, (int)WGL_ARB_pixel_format.StereoArb, stereo ? 1 : 0, 0, 0 }; int[] pixel = new int[1], num_formats = new int[1]; Wgl.Arb.ChoosePixelFormat(window.DeviceContext, attribs_values, null, 1, pixel, num_formats); if (num_formats[0] == 0 || pixel[0] == 0) { // Try again without an accumulator. Many modern cards cannot accelerate multisampled formats with accumulator buffers. attribs_values[10 * 2 + 1] = attribs_values[11 * 2 + 1] = attribs_values[12 * 2 + 1] = attribs_values[13 * 2 + 1] = attribs_values[14 * 2 + 1] = 0; Wgl.Arb.ChoosePixelFormat(window.DeviceContext, attribs_values, null, 1, pixel, num_formats); } if (num_formats[0] == 0 || pixel[0] == 0) { Debug.WriteLine("failed"); return(null); } // Find out what we really got as a format: Wgl.Arb.GetPixelFormatAttrib(window.DeviceContext, pixel[0], 0, attribs.Length, attribs, values); GraphicsMode mode = new GraphicsMode(new IntPtr(pixel[0]), new ColorDepth(values[1], values[2], values[3], values[4]), values[6], values[7], values[8] != 0 ? values[9] : 0, new ColorDepth(values[10], values[11], values[12], values[13]), values[15] == 1 ? 2 : 1, values[16] == 1 ? true : false); Debug.WriteLine("success!"); return(mode); } }
public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext, int major, int minor, GraphicsContextFlags flags) { // There are many ways this code can break when accessed by multiple threads. The biggest offender is // the sharedContext stuff, which will only become valid *after* this constructor returns. // The easiest solution is to serialize all context construction - hence the big lock, below. lock (SyncRoot) { if (window == null) throw new ArgumentNullException("window", "Must point to a valid window."); if (window.Handle == IntPtr.Zero) throw new ArgumentException("window", "Must be a valid window."); Debug.Print("OpenGL will be bound to window:{0} on thread:{1}", window.Handle, System.Threading.Thread.CurrentThread.ManagedThreadId); lock (LoadLock) { ModeSelector = new WinGraphicsMode(window.DeviceContext); Mode = SetGraphicsModePFD(ModeSelector, format, (WinWindowInfo)window); if (Wgl.Delegates.wglCreateContextAttribsARB != null) { try { Debug.Write("Using WGL_ARB_create_context... "); List<int> attributes = new List<int>(); attributes.Add((int)ArbCreateContext.MajorVersion); attributes.Add(major); attributes.Add((int)ArbCreateContext.MinorVersion); attributes.Add(minor); if (flags != 0) { attributes.Add((int)ArbCreateContext.ContextFlags); attributes.Add((int)GetARBContextFlags(flags)); attributes.Add((int)ArbCreateContext.ProfileMask); attributes.Add((int)GetARBContextProfile(flags)); } // According to the docs, " <attribList> specifies a list of attributes for the context. // The list consists of a sequence of <name,value> pairs terminated by the // value 0. [...]" // Is this a single 0, or a <0, 0> pair? (Defensive coding: add two zeroes just in case). attributes.Add(0); attributes.Add(0); Handle = new ContextHandle( Wgl.Arb.CreateContextAttribs( window.DeviceContext, sharedContext != null ? (sharedContext as IGraphicsContextInternal).Context.Handle : IntPtr.Zero, attributes.ToArray())); if (Handle == ContextHandle.Zero) Debug.Print("failed. (Error: {0})", Marshal.GetLastWin32Error()); } catch (EntryPointNotFoundException e) { Debug.Print(e.ToString()); } catch (NullReferenceException e) { Debug.Print(e.ToString()); } } } if (Handle == ContextHandle.Zero) { // Failed to create GL3-level context, fall back to GL2. Debug.Write("Falling back to GL2... "); Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); if (Handle == ContextHandle.Zero) Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); if (Handle == ContextHandle.Zero) throw new GraphicsContextException( String.Format("Context creation failed. Wgl.CreateContext() error: {0}.", Marshal.GetLastWin32Error())); } Debug.WriteLine(String.Format("success! (id: {0})", Handle)); // Todo: is this comment still true? // On intel drivers, wgl entry points appear to change // when creating multiple contexts. As a workaround, // we reload Wgl entry points every time we create a // new context - this solves the issue without any apparent // side-effects (i.e. the old contexts can still be handled // using the new entry points.) // Sigh... Wgl.LoadAll(); if (sharedContext != null) { Marshal.GetLastWin32Error(); Debug.Write(String.Format("Sharing state with context {0}: ", sharedContext)); bool result = Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, Handle.Handle); Debug.WriteLine(result ? "success!" : "failed with win32 error " + Marshal.GetLastWin32Error()); } } }
public WinGLNative(int x, int y, int width, int height, string title, GameWindowFlags options, DisplayDevice device) { lock (WinGLNative.SyncRoot) { this.WindowProcedureDelegate = new WindowProcedure(this.WindowProcedure); this.window = new WinWindowInfo(this.CreateWindow(x, y, width, height, title, options, device, IntPtr.Zero), (WinWindowInfo) null); this.child_window = new WinWindowInfo(this.CreateWindow(0, 0, this.ClientSize.Width, this.ClientSize.Height, title, options, device, this.window.WindowHandle), this.window); this.exists = true; this.keyboard.Description = "Standard Windows keyboard"; this.keyboard.NumberOfFunctionKeys = 12; this.keyboard.NumberOfKeys = 101; this.keyboard.NumberOfLeds = 3; this.mouse.Description = "Standard Windows mouse"; this.mouse.NumberOfButtons = 3; this.mouse.NumberOfWheels = 1; this.keyboards.Add(this.keyboard); this.mice.Add(this.mouse); } }
public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext, int major, int minor, GraphicsContextFlags flags) { // There are many ways this code can break when accessed by multiple threads. The biggest offender is // the sharedContext stuff, which will only become valid *after* this constructor returns. // The easiest solution is to serialize all context construction - hence the big lock, below. lock (LoadLock) { if (window == null) { throw new ArgumentNullException("window", "Must point to a valid window."); } if (window.Handle == IntPtr.Zero) { throw new ArgumentException("window", "Must be a valid window."); } IntPtr current_context = Wgl.GetCurrentContext(); INativeWindow temp_window = null; TemporaryContext temp_context = null; try { if (current_context == IntPtr.Zero) { // Create temporary context to load WGL extensions temp_window = new NativeWindow(); temp_context = new TemporaryContext(temp_window); current_context = Wgl.GetCurrentContext(); if (current_context != IntPtr.Zero && current_context == temp_context.Context.Handle) { new Wgl().LoadEntryPoints(); } } Debug.Print("OpenGL will be bound to window:{0} on thread:{1}", window.Handle, System.Threading.Thread.CurrentThread.ManagedThreadId); ModeSelector = new WinGraphicsMode(window.DeviceContext); Mode = SetGraphicsModePFD(ModeSelector, format, (WinWindowInfo)window); if (Wgl.SupportsFunction("wglCreateContextAttribsARB")) { try { Debug.Write("Using WGL_ARB_create_context... "); List <int> attributes = new List <int>(); attributes.Add((int)ArbCreateContext.MajorVersion); attributes.Add(major); attributes.Add((int)ArbCreateContext.MinorVersion); attributes.Add(minor); if (flags != 0) { attributes.Add((int)ArbCreateContext.ContextFlags); attributes.Add((int)GetARBContextFlags(flags)); attributes.Add((int)ArbCreateContext.ProfileMask); attributes.Add((int)GetARBContextProfile(flags)); } // According to the docs, " <attribList> specifies a list of attributes for the context. // The list consists of a sequence of <name,value> pairs terminated by the // value 0. [...]" // Is this a single 0, or a <0, 0> pair? (Defensive coding: add two zeroes just in case). attributes.Add(0); attributes.Add(0); Handle = new ContextHandle( Wgl.Arb.CreateContextAttribs( window.DeviceContext, sharedContext != null ? (sharedContext as IGraphicsContextInternal).Context.Handle : IntPtr.Zero, attributes.ToArray())); if (Handle == ContextHandle.Zero) { Debug.Print("failed. (Error: {0})", Marshal.GetLastWin32Error()); } } catch (Exception e) { Debug.Print(e.ToString()); } } if (Handle == ContextHandle.Zero) { // Failed to create GL3-level context, fall back to GL2. Debug.Write("Falling back to GL2... "); Handle = new ContextHandle(Wgl.CreateContext(window.DeviceContext)); if (Handle == ContextHandle.Zero) { Handle = new ContextHandle(Wgl.CreateContext(window.DeviceContext)); } if (Handle == ContextHandle.Zero) { throw new GraphicsContextException( String.Format("Context creation failed. Wgl.CreateContext() error: {0}.", Marshal.GetLastWin32Error())); } } Debug.WriteLine(String.Format("success! (id: {0})", Handle)); } finally { if (temp_context != null) { temp_context.Dispose(); temp_context = null; } if (temp_window != null) { temp_window.Dispose(); temp_window = null; } } } // Todo: is this comment still true? // On intel drivers, wgl entry points appear to change // when creating multiple contexts. As a workaround, // we reload Wgl entry points every time we create a // new context - this solves the issue without any apparent // side-effects (i.e. the old contexts can still be handled // using the new entry points.) // Sigh... MakeCurrent(window); new Wgl().LoadEntryPoints(); if (sharedContext != null) { Marshal.GetLastWin32Error(); Debug.Write(String.Format("Sharing state with context {0}: ", sharedContext)); bool result = Wgl.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, Handle.Handle); Debug.WriteLine(result ? "success!" : "failed with win32 error " + Marshal.GetLastWin32Error()); } }
/// <summary> /// Constructs a new instance with the specified window handle and paren.t /// </summary> /// <param name="handle">The window handle for this instance.</param> /// <param name="parent">The parent window of this instance (may be null).</param> public WinWindowInfo(IntPtr handle, WinWindowInfo parent) { this.handle = handle; this.parent = parent; }
public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext, int major, int minor, GraphicsContextFlags flags) { if (window == null) { throw new ArgumentNullException("window", "Must point to a valid window."); } if (window.WindowHandle == IntPtr.Zero) { throw new ArgumentException("window", "Must be a valid window."); } Mode = format; Debug.Print("OpenGL will be bound to handle: {0}", window.WindowHandle); Debug.Write("Setting pixel format... "); this.SetGraphicsModePFD(format, (WinWindowInfo)window); if (!wgl_loaded) { // We need to create a temp context in order to load wgl extensions (e.g. for multisampling or GL3). // We cannot rely on OpenTK.Platform.Wgl until we create the context and call Wgl.LoadAll(). Debug.Print("Creating temporary context for wgl extensions."); ContextHandle temp_context = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); Wgl.Imports.MakeCurrent(window.DeviceContext, temp_context.Handle); Wgl.LoadAll(); Wgl.MakeCurrent(IntPtr.Zero, IntPtr.Zero); Wgl.DeleteContext(temp_context.Handle); wgl_loaded = true; } if (Wgl.Delegates.wglCreateContextAttribsARB != null) { try { Debug.Write("Using WGL_ARB_create_context... "); List <int> attributes = new List <int>(); attributes.Add((int)ArbCreateContext.MajorVersion); attributes.Add(major); attributes.Add((int)ArbCreateContext.MinorVersion); attributes.Add(minor); if (flags != 0) { attributes.Add((int)ArbCreateContext.Flags); attributes.Add((int)flags); } attributes.Add(0); Handle = new ContextHandle( Wgl.Arb.CreateContextAttribs( window.DeviceContext, sharedContext != null ? (sharedContext as IGraphicsContextInternal).Context.Handle : IntPtr.Zero, attributes.ToArray())); if (Handle == ContextHandle.Zero) { Debug.Print("failed. (Error: {0})", Marshal.GetLastWin32Error()); } else { Debug.Print("success!"); } } catch (EntryPointNotFoundException e) { Debug.Print(e.ToString()); } catch (NullReferenceException e) { Debug.Print(e.ToString()); } } if (Handle == ContextHandle.Zero) { // Failed to create GL3-level context, fall back to GL2. Debug.Write("Falling back to GL2... "); Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); if (Handle == ContextHandle.Zero) { Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); } if (Handle == ContextHandle.Zero) { throw new GraphicsContextException( String.Format("Context creation failed. Wgl.CreateContext() error: {0}.", Marshal.GetLastWin32Error())); } } Debug.WriteLine(String.Format("success! (id: {0})", Handle)); if (sharedContext != null) { Debug.Print("Sharing state with context {0}", sharedContext.ToString()); Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, Handle.Handle); } }
public WinGLNative(int x, int y, int width, int height, string title, GameWindowFlags options, DisplayDevice device) { lock (SyncRoot) { // This is the main window procedure callback. We need the callback in order to create the window, so // don't move it below the CreateWindow calls. WindowProcedureDelegate = WindowProcedure; //// This timer callback is called periodically when the window enters a sizing / moving modal loop. //ModalLoopCallback = delegate(IntPtr handle, WindowMessage msg, UIntPtr eventId, int time) //{ // // Todo: find a way to notify the frontend that it should process queued up UpdateFrame/RenderFrame events. // if (Move != null) // Move(this, EventArgs.Empty); //}; int scale_width = width; int scale_height = height; int scale_x = x; int scale_y = y; if (Toolkit.Options.EnableHighResolution) { // CreateWindow takes values in pixels. // According to the high-dpi guidelines, // we need to scale these values by the // current DPI. // Search MSDN for "How to Ensure That // Your Application Displays Properly on // High-DPI Displays" scale_width = ScaleX(width); scale_height = ScaleY(height); scale_x = ScaleX(x); scale_y = ScaleY(y); } // To avoid issues with Ati drivers on Windows 6+ with compositing enabled, the context will not be // bound to the top-level window, but rather to a child window docked in the parent. window = new WinWindowInfo( CreateWindow( scale_x, scale_y, scale_width, scale_height, title, options, device, IntPtr.Zero), null); child_window = new WinWindowInfo( CreateWindow( 0, 0, ClientSize.Width, ClientSize.Height, title, options, device, window.Handle), window); exists = true; keyboard.Description = "Standard Windows keyboard"; keyboard.NumberOfFunctionKeys = 12; keyboard.NumberOfKeys = 101; keyboard.NumberOfLeds = 3; mouse.Description = "Standard Windows mouse"; mouse.NumberOfButtons = 3; mouse.NumberOfWheels = 1; keyboards.Add(keyboard); mice.Add(mouse); } }
// Note: there is no relevant ARB function. internal static GraphicsMode SetGraphicsModePFD(WinGraphicsMode mode_selector, GraphicsMode mode, WinWindowInfo window) { Debug.Write("Setting pixel format... "); if (window == null) throw new ArgumentNullException("window", "Must point to a valid window."); if (!mode.Index.HasValue) { mode = mode_selector.SelectGraphicsMode( mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo); } PixelFormatDescriptor pfd = new PixelFormatDescriptor(); 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.Value, ref pfd)) { throw new GraphicsContextException(String.Format( "Requested GraphicsMode not available. SetPixelFormat error: {0}", Marshal.GetLastWin32Error())); } return mode; }
public WinGLContext(ContextHandle handle, WinWindowInfo window, IGraphicsContext sharedContext, int major, int minor, GraphicsContextFlags flags) { if (handle == ContextHandle.Zero) throw new ArgumentException("handle"); if (window == null) throw new ArgumentNullException("window"); Handle = handle; }
GraphicsMode SelectGraphicsModePFD(ColorDepth color, int depth, int stencil, int samples, ColorDepth accum, int buffers, bool stereo) { using (Control native_window = new Control()) using (WinWindowInfo window = new WinWindowInfo(native_window.Handle, null)) { IntPtr deviceContext = ((WinWindowInfo)window).DeviceContext; Debug.WriteLine(String.Format("Device context: {0}", deviceContext)); Debug.Write("Selecting pixel format... "); PixelFormatDescriptor pixelFormat = new PixelFormatDescriptor(); pixelFormat.Size = API.PixelFormatDescriptorSize; pixelFormat.Version = API.PixelFormatDescriptorVersion; pixelFormat.Flags = PixelFormatDescriptorFlags.SUPPORT_OPENGL | PixelFormatDescriptorFlags.DRAW_TO_WINDOW; pixelFormat.ColorBits = (byte)(color.Red + color.Green + color.Blue); pixelFormat.PixelType = color.IsIndexed ? PixelType.INDEXED : PixelType.RGBA; pixelFormat.RedBits = (byte)color.Red; pixelFormat.GreenBits = (byte)color.Green; pixelFormat.BlueBits = (byte)color.Blue; pixelFormat.AlphaBits = (byte)color.Alpha; if (accum.BitsPerPixel > 0) { pixelFormat.AccumBits = (byte)(accum.Red + accum.Green + accum.Blue); pixelFormat.AccumRedBits = (byte)accum.Red; pixelFormat.AccumGreenBits = (byte)accum.Green; pixelFormat.AccumBlueBits = (byte)accum.Blue; pixelFormat.AccumAlphaBits = (byte)accum.Alpha; } pixelFormat.DepthBits = (byte)depth; pixelFormat.StencilBits = (byte)stencil; if (depth <= 0) pixelFormat.Flags |= PixelFormatDescriptorFlags.DEPTH_DONTCARE; if (stereo) pixelFormat.Flags |= PixelFormatDescriptorFlags.STEREO; if (buffers > 1) pixelFormat.Flags |= PixelFormatDescriptorFlags.DOUBLEBUFFER; int pixel = Functions.ChoosePixelFormat(deviceContext, ref pixelFormat); if (pixel == 0) throw new GraphicsModeException("The requested GraphicsMode is not available."); // Find out what we really got as a format: PixelFormatDescriptor pfd = new PixelFormatDescriptor(); pixelFormat.Size = API.PixelFormatDescriptorSize; pixelFormat.Version = API.PixelFormatDescriptorVersion; Functions.DescribePixelFormat(deviceContext, pixel, API.PixelFormatDescriptorSize, ref pfd); GraphicsMode fmt = new GraphicsMode((IntPtr)pixel, new ColorDepth(pfd.RedBits, pfd.GreenBits, pfd.BlueBits, pfd.AlphaBits), pfd.DepthBits, pfd.StencilBits, 0, new ColorDepth(pfd.AccumBits), (pfd.Flags & PixelFormatDescriptorFlags.DOUBLEBUFFER) != 0 ? 2 : 1, (pfd.Flags & PixelFormatDescriptorFlags.STEREO) != 0); return fmt; } }
public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext, int major, int minor, GraphicsContextFlags flags) { // There are many ways this code can break when accessed by multiple threads. The biggest offender is // the sharedContext stuff, which will only become valid *after* this constructor returns. // The easiest solution is to serialize all context construction - hence the big lock, below. lock (SyncRoot) { if (window == null) throw new ArgumentNullException("window", "Must point to a valid window."); if (window.WindowHandle == IntPtr.Zero) throw new ArgumentException("window", "Must be a valid window."); Mode = format; Debug.Print("OpenGL will be bound to window:{0} on thread:{1}", window.WindowHandle, System.Threading.Thread.CurrentThread.ManagedThreadId); this.SetGraphicsModePFD(format, (WinWindowInfo)window); lock (LoadLock) { if (!wgl_loaded) { // We need to create a temp context in order to load wgl extensions (e.g. for multisampling or GL3). // We cannot rely on OpenTK.Platform.Wgl until we create the context and call Wgl.LoadAll(). Debug.Print("Creating temporary context for wgl extensions."); ContextHandle temp_context = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); Wgl.Imports.MakeCurrent(window.DeviceContext, temp_context.Handle); Wgl.LoadAll(); Wgl.Imports.MakeCurrent(IntPtr.Zero, IntPtr.Zero); Wgl.Imports.DeleteContext(temp_context.Handle); wgl_loaded = true; } if (Wgl.Delegates.wglCreateContextAttribsARB != null) { try { Debug.Write("Using WGL_ARB_create_context... "); List<int> attributes = new List<int>(); attributes.Add((int)ArbCreateContext.MajorVersion); attributes.Add(major); attributes.Add((int)ArbCreateContext.MinorVersion); attributes.Add(minor); if (flags != 0) { attributes.Add((int)ArbCreateContext.Flags); #warning "This is not entirely correct: Embedded is not a valid flag! We need to add a GetARBContextFlags(GraphicsContextFlags) method." attributes.Add((int)flags); } // According to the docs, " <attribList> specifies a list of attributes for the context. // The list consists of a sequence of <name,value> pairs terminated by the // value 0. [...]" // Is this a single 0, or a <0, 0> pair? (Defensive coding: add two zeroes just in case). attributes.Add(0); attributes.Add(0); Handle = new ContextHandle( Wgl.Arb.CreateContextAttribs( window.DeviceContext, sharedContext != null ? (sharedContext as IGraphicsContextInternal).Context.Handle : IntPtr.Zero, attributes.ToArray())); if (Handle == ContextHandle.Zero) Debug.Print("failed. (Error: {0})", Marshal.GetLastWin32Error()); } catch (EntryPointNotFoundException e) { Debug.Print(e.ToString()); } catch (NullReferenceException e) { Debug.Print(e.ToString()); } } } if (Handle == ContextHandle.Zero) { // Failed to create GL3-level context, fall back to GL2. Debug.Write("Falling back to GL2... "); Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); if (Handle == ContextHandle.Zero) Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext)); if (Handle == ContextHandle.Zero) throw new GraphicsContextException( String.Format("Context creation failed. Wgl.CreateContext() error: {0}.", Marshal.GetLastWin32Error())); } Debug.WriteLine(String.Format("success! (id: {0})", Handle)); if (sharedContext != null) { Marshal.GetLastWin32Error(); Debug.Write(String.Format("Sharing state with context {0}: ", sharedContext)); bool result = Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, Handle.Handle); Debug.WriteLine(result ? "success!" : "failed with win32 error " + Marshal.GetLastWin32Error()); } } }
public WinDXContext(GraphicsMode format, WinWindowInfo window) { Debug.Print( "doing nothing" ); }