Beispiel #1
0
            public TemporaryContext(INativeWindow 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   = native.WindowInfo as WinWindowInfo;
                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)
                {
                    success = TryMakeCurrent(window, Context.Handle);
                }
                else
                {
                    Debug.Print("[WGL] CreateContext failed with error: {0}", Marshal.GetLastWin32Error());
                }

                if (!success)
                {
                    Debug.WriteLine("[WGL] Failed to create temporary context");
                }
            }
Beispiel #2
0
        // 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);
        }
Beispiel #3
0
        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       = TryMakeCurrent(wnd, 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()));
                }
            }
        }
Beispiel #4
0
        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;
        }
Beispiel #5
0
            public TemporaryContext(INativeWindow 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   = native.WindowInfo as WinWindowInfo;
                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");
                }
            }
Beispiel #6
0
        /// <summary>
        /// Tries to make the context current over several attempts.
        /// </summary>
        /// <remarks>
        /// On some video cards and on some virtual machines, wglMakeCurrent
        /// may fail with an error code 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
        /// In a majority of cases this will succeed on the first try.
        /// </remarks>
        /// <returns>Whether the context was successfully made current.</returns>
        private static bool TryMakeCurrent(WinWindowInfo window, IntPtr contextHandle)
        {
            bool success = false;

            for (int retry = 0; retry < 10 && !success; retry++)
            {
                success = Wgl.MakeCurrent(window.DeviceContext, contextHandle);
                if (!success)
                {
                    Debug.Print("wglMakeCurrent failed with error: {0}. Retrying", Marshal.GetLastWin32Error());
                    System.Threading.Thread.Sleep(10);
                }
            }

            return(success);
        }
Beispiel #7
0
        private 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);
        }
Beispiel #8
0
        /// <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));
        }
Beispiel #9
0
        private 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.Handle,
                                                                         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);
        }
Beispiel #10
0
        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());
            }
        }
Beispiel #11
0
 /// <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;
 }