Ejemplo n.º 1
0
        /// <summary>
        /// Constructs a new GraphicsContext with the specified GraphicsMode, version and flags,  and attaches it to the specified window.
        /// </summary>
        /// <param name="mode">The OpenTK.Graphics.GraphicsMode of the GraphicsContext.</param>
        /// <param name="window">The OpenTK.Platform.IWindowInfo to attach the GraphicsContext to.</param>
        /// <param name="major">The major version of the new GraphicsContext.</param>
        /// <param name="minor">The minor version of the new GraphicsContext.</param>
        /// <param name="flags">The GraphicsContextFlags for the GraphicsContext.</param>
        /// <remarks>
        /// Different hardware supports different flags, major and minor versions. Invalid parameters will be silently ignored.
        /// </remarks>
        public GraphicsContext(GraphicsMode mode, IWindowInfo window, int major, int minor, GraphicsContextFlags flags)
        {
            bool designMode = false;

            if (mode == null && window == null)
            {
                designMode = true;
            }
            else if (mode == null)
            {
                throw new ArgumentNullException("mode", "Must be a valid GraphicsMode.");
            }
            else if (window == null)
            {
                throw new ArgumentNullException("window", "Must point to a valid window.");
            }

            // Silently ignore invalid major and minor versions.
            if (major <= 0)
            {
                major = 1;
            }
            if (minor < 0)
            {
                minor = 0;
            }

            Debug.Print("Creating GraphicsContext.");
            try
            {
                Debug.Indent();
                Debug.Print("GraphicsMode: {0}", mode);
                Debug.Print("IWindowInfo: {0}", window);
                Debug.Print("GraphicsContextFlags: {0}", flags);
                Debug.Print("Requested version: {0}.{1}", major, minor);

                IGraphicsContext shareContext = null;
                if (GraphicsContext.ShareContexts)
                {
                    lock (context_lock)
                    {
                        // A small hack to create a shared context with the first available context.
                        foreach (WeakReference r in GraphicsContext.available_contexts.Values)
                        {
                            shareContext = (IGraphicsContext)r.Target;
                            break;
                        }
                    }
                }

                // Todo: Add a DummyFactory implementing IPlatformFactory.
                if (designMode)
#if !IPHONE
                { implementation = new Platform.Dummy.DummyGLContext(); }
#else
                { implementation = Factory.Embedded.CreateGLContext(mode, window, shareContext, direct_rendering, major, minor, flags); }
#endif
                else
                {
                    switch ((flags & GraphicsContextFlags.Embedded) == GraphicsContextFlags.Embedded)
                    {
                    case false: implementation = Factory.Default.CreateGLContext(mode, window, shareContext, direct_rendering, major, minor, flags); break;

                    case true: implementation = Factory.Embedded.CreateGLContext(mode, window, shareContext, direct_rendering, major, minor, flags); break;
                    }
                }

                lock (context_lock)
                {
                    available_contexts.Add((this as IGraphicsContextInternal).Context, new WeakReference(this));
                }
            }
Ejemplo n.º 2
0
 /// <summary>
 /// Constructs a new GraphicsContext from a pre-existing context created outside of OpenTK.
 /// </summary>
 /// <param name="handle">The handle of the existing context. This must be a valid, unique handle that is not known to OpenTK.</param>
 /// <param name="window">This parameter is reserved.</param>
 /// <param name="shareContext">This parameter is reserved.</param>
 /// <param name="major">This parameter is reserved.</param>
 /// <param name="minor">This parameter is reserved.</param>
 /// <param name="flags">This parameter is reserved..</param>
 public GraphicsContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, int major, int minor, GraphicsContextFlags flags)
     : this(handle, Platform.Utilities.CreateGetAddress(), Factory.Default.CreateGetCurrentGraphicsContext())
 {
 }
        /// <summary>
        /// Constructs a new GraphicsContext with the specified GraphicsMode, version and flags,  and attaches it to the specified window.
        /// </summary>
        /// <param name="mode">The OpenTK.Graphics.GraphicsMode of the GraphicsContext.</param>
        /// <param name="window">The OpenTK.Platform.IWindowInfo to attach the GraphicsContext to.</param>
        /// <param name="major">The major version of the new GraphicsContext.</param>
        /// <param name="minor">The minor version of the new GraphicsContext.</param>
        /// <param name="flags">The GraphicsContextFlags for the GraphicsContext.</param>
        /// <remarks>
        /// Different hardware supports different flags, major and minor versions. Invalid parameters will be silently ignored.
        /// </remarks>
        public GraphicsContext(GraphicsMode mode, IWindowInfo window, int major, int minor, GraphicsContextFlags flags)
        {
            lock (SyncRoot)
            {
                bool designMode = false;
                if (mode == null && window == null)
                {
                    designMode = true;
                }
                else if (mode == null)
                {
                    throw new ArgumentNullException("mode", "Must be a valid GraphicsMode.");
                }
                else if (window == null)
                {
                    throw new ArgumentNullException("window", "Must point to a valid window.");
                }

                // Silently ignore invalid major and minor versions.
                if (major <= 0)
                {
                    major = 1;
                }
                if (minor < 0)
                {
                    minor = 0;
                }

                Debug.Print("Creating GraphicsContext.");
                try
                {
                    Debug.Indent();
                    Debug.Print("GraphicsMode: {0}", mode);
                    Debug.Print("IWindowInfo: {0}", window);
                    Debug.Print("GraphicsContextFlags: {0}", flags);
                    Debug.Print("Requested version: {0}.{1}", major, minor);

                    IGraphicsContext shareContext = shareContext = FindSharedContext();

                    // Todo: Add a DummyFactory implementing IPlatformFactory.
                    if (designMode)
                    {
                        implementation = new Platform.Dummy.DummyGLContext();
                    }
                    else
                    {
                        IPlatformFactory factory = null;
                        switch ((flags & GraphicsContextFlags.Embedded) == GraphicsContextFlags.Embedded)
                        {
                        case false: factory = Factory.Default; break;

                        case true: factory = Factory.Embedded; break;
                        }

                        // Note: this approach does not allow us to mix native and EGL contexts in the same process.
                        // This should not be a problem, as this use-case is not interesting for regular applications.
                        // Note 2: some platforms may not support a direct way of getting the current context
                        // (this happens e.g. with DummyGLContext). In that case, we use a slow fallback which
                        // iterates through all known contexts and checks if any is current (check GetCurrentContext
                        // declaration).
                        if (GetCurrentContext == null)
                        {
                            GetCurrentContext = factory.CreateGetCurrentGraphicsContext();
                        }

                        implementation = factory.CreateGLContext(mode, window, shareContext, direct_rendering, major, minor, flags);
                    }

                    AddContext(this);
                }
                finally
                {
                    Debug.Unindent();
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Constructs a new GraphicsContext with the specified GraphicsMode, version and flags,  and attaches it to the specified window.
        /// </summary>
        /// <param name="mode">The OpenTK.Graphics.GraphicsMode of the GraphicsContext.</param>
        /// <param name="window">The OpenTK.Platform.IWindowInfo to attach the GraphicsContext to.</param>
        /// <param name="shareContext">The GraphicsContext to share resources with, or null for explicit non-sharing.</param>
        /// <param name="major">The major version of the new GraphicsContext.</param>
        /// <param name="minor">The minor version of the new GraphicsContext.</param>
        /// <param name="flags">The GraphicsContextFlags for the GraphicsContext.</param>
        /// <remarks>
        /// Different hardware supports different flags, major and minor versions. Invalid parameters will be silently ignored.
        /// </remarks>
        public GraphicsContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, int major, int minor, GraphicsContextFlags flags)
        {
            lock (SyncRoot)
            {
                bool designMode = false;
                if (mode == null && window == null)
                {
                    designMode = true;
                }
                else if (mode == null)
                {
                    throw new ArgumentNullException("mode", "Must be a valid GraphicsMode.");
                }
                else if (window == null)
                {
                    throw new ArgumentNullException("window", "Must point to a valid window.");
                }

                // Silently ignore invalid major and minor versions.
                if (major <= 0)
                {
                    major = 1;
                }
                if (minor < 0)
                {
                    minor = 0;
                }

                // Angle needs an embedded context
                var use_angle_flag = GraphicsContextFlags.Angle
                                     | GraphicsContextFlags.AngleD3D9
                                     | GraphicsContextFlags.AngleD3D11
                                     | GraphicsContextFlags.AngleOpenGL;
                var use_angle = false;
                if ((flags & use_angle_flag) != 0)
                {
                    flags    |= GraphicsContextFlags.Embedded;
                    use_angle = true;
                }

                Debug.Print("Creating GraphicsContext.");
                try
                {
                    Debug.Indent();
                    Debug.Print("GraphicsMode: {0}", mode);
                    Debug.Print("IWindowInfo: {0}", window);
                    Debug.Print("GraphicsContextFlags: {0}", flags);
                    Debug.Print("Requested version: {0}.{1}", major, minor);

                    // Todo: Add a DummyFactory implementing IPlatformFactory.
                    if (designMode)
                    {
                        implementation = new Platform.Dummy.DummyGLContext();
                    }
                    else
                    {
                        IPlatformFactory factory = null;
                        switch ((flags & GraphicsContextFlags.Embedded) == GraphicsContextFlags.Embedded)
                        {
                        case false:
                            factory = Factory.Default;
                            break;

                        case true:
                            factory = use_angle ? Factory.Angle : Factory.Embedded;
                            break;
                        }

                        GetCurrentContext = factory.CreateGetCurrentGraphicsContext();

                        implementation = factory.CreateGLContext(mode, window, shareContext, direct_rendering, major, minor, flags);
                        handle_cached  = ((IGraphicsContextInternal)implementation).Context;
                        factory.RegisterResource(this);
                    }

                    AddContext(this);
                }
                finally
                {
                    Debug.Unindent();
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Constructs a new GraphicsContext with the specified GraphicsMode, version and flags, and attaches it to the specified window. A dummy context will be created if both
        /// the handle and the window are null.
        /// </summary>
        /// <param name="mode">The OpenTK.Graphics.GraphicsMode of the GraphicsContext.</param>
        /// <param name="window">The OpenTK.Platform.IWindowInfo to attach the GraphicsContext to.</param>
        /// <param name="shareContext">The GraphicsContext to share resources with, or null for explicit non-sharing.</param>
        /// <param name="major">The major version of the new GraphicsContext.</param>
        /// <param name="minor">The minor version of the new GraphicsContext.</param>
        /// <param name="flags">The GraphicsContextFlags for the GraphicsContext.</param>
        /// <remarks>
        /// Different hardware supports different flags, major and minor versions. Invalid parameters will be silently ignored.
        /// </remarks>
        public GraphicsContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, int major, int minor, GraphicsContextFlags flags)
        {
            bool   success = false;
            string errMsg  = null;

            lock (SyncRoot)
            {
                bool designMode = false;
                if (mode == null && window == null)
                {
                    designMode = true;
                }
                else if (mode == null)
                {
                    throw new ArgumentNullException("mode", "Must be a valid GraphicsMode.");
                }
                else if (window == null)
                {
                    throw new ArgumentNullException("window", "Must point to a valid window.");
                }

                // Silently ignore invalid major and minor versions.
                if (major <= 0)
                {
                    major = 1;
                }
                if (minor < 0)
                {
                    minor = 0;
                }

                // Angle needs an embedded context
                const GraphicsContextFlags useAngleFlag = GraphicsContextFlags.Angle
                                                          | GraphicsContextFlags.AngleD3D9
                                                          | GraphicsContextFlags.AngleD3D11
                                                          | GraphicsContextFlags.AngleOpenGL;

                flags = useAngleFlag;
                var useAngle = false;
                if ((flags & useAngleFlag) != 0)
                {
                    flags   |= GraphicsContextFlags.Embedded;
                    useAngle = true;
                }

                Debug.WriteLine("Creating GraphicsContext.");



                try
                {
                    Debug.WriteLine("");
                    Debug.WriteLine($"GraphicsMode: {mode}");
                    Debug.WriteLine($"IWindowInfo: {window}");
                    Debug.WriteLine($"GraphicsContextFlags: {flags}");
                    Debug.WriteLine($"Requested version: {major}.{minor}");

                    // Todo: Add a DummyFactory implementing IPlatformFactory.
                    if (designMode)
                    {
                        _implementation = new Platform.Dummy.DummyGLContext();
                    }
                    else
                    {
                        IPlatformFactory factory = null;
                        switch ((flags & GraphicsContextFlags.Embedded) == GraphicsContextFlags.Embedded)
                        {
                        case false:
                            factory = Factory.Default;
                            break;

                        case true:
                            factory = useAngle ? Factory.Angle : Factory.Embedded;
                            break;
                        }

                        // Note: this approach does not allow us to mix native and EGL contexts in the same process.
                        // This should not be a problem, as this use-case is not interesting for regular applications.
                        // Note 2: some platforms may not support a direct way of getting the current context
                        // (this happens e.g. with DummyGLContext). In that case, we use a slow fallback which
                        // iterates through all known contexts and checks if any is current (check GetCurrentContext
                        // declaration).
                        if (GetCurrentContext == null)
                        {
                            GetCurrentContext = factory.CreateGetCurrentGraphicsContext();
                        }

                        _implementation = factory.CreateGLContext(mode, window, shareContext, DirectRendering, major, minor, flags);
                        _handle_cached  = ((IGraphicsContextInternal)_implementation).Context;
                        factory.RegisterResource(this);
                    }

                    AddContext(this);
                    success = true;
                }
                catch (Exception ex)
                {
                    //some exception err here
                    //we write a log file
                }
                finally
                {
                }
            }

            if (!success)
            {
                ContextCreationError?.Invoke(null, new GraphicContextCreationErrorArgs(errMsg));
            }
        }
Ejemplo n.º 6
0
 private void CreateContext(bool direct, IGraphicsContext source)
 {
     lock (GraphicsContext.SyncRoot)
         GraphicsContext.available_contexts.Add(this.Context, new WeakReference((object)this));
 }
Ejemplo n.º 7
0
 private GraphicsContext(ContextHandle handle)
 {
     this.implementation = (IGraphicsContext) new DummyGLContext(handle);
     lock (GraphicsContext.SyncRoot)
         GraphicsContext.available_contexts.Add((this.implementation as IGraphicsContextInternal).Context, new WeakReference((object)this));
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Constructs a new GraphicsContext with the specified GraphicsMode, version and flags,  and attaches it to the specified window.
        /// </summary>
        /// <param name="mode">The OpenTK.Graphics.GraphicsMode of the GraphicsContext.</param>
        /// <param name="window">The OpenTK.Platform.IWindowInfo to attach the GraphicsContext to.</param>
        /// <param name="major">The major version of the new GraphicsContext.</param>
        /// <param name="minor">The minor version of the new GraphicsContext.</param>
        /// <param name="flags">The GraphicsContextFlags for the GraphicsContext.</param>
        /// <remarks>
        /// Different hardware supports different flags, major and minor versions. Invalid parameters will be silently ignored.
        /// </remarks>
        public GraphicsContext(GraphicsMode mode, IWindowInfo window, int major, int minor, GraphicsContextFlags flags)
        {
            lock (SyncRoot)
            {
                bool designMode = false;
                if (mode == null && window == null)
                {
                    designMode = true;
                }
                else if (mode == null)
                {
                    throw new ArgumentNullException("mode", "Must be a valid GraphicsMode.");
                }
                else if (window == null)
                {
                    throw new ArgumentNullException("window", "Must point to a valid window.");
                }

                // Silently ignore invalid major and minor versions.
                if (major <= 0)
                {
                    major = 1;
                }
                if (minor < 0)
                {
                    minor = 0;
                }

                Debug.Print("Creating GraphicsContext.");
                try
                {
                    Debug.Indent();
                    Debug.Print("GraphicsMode: {0}", mode);
                    Debug.Print("IWindowInfo: {0}", window);
                    Debug.Print("GraphicsContextFlags: {0}", flags);
                    Debug.Print("Requested version: {0}.{1}", major, minor);

                    IGraphicsContext shareContext = shareContext = FindSharedContext();

                    // Todo: Add a DummyFactory implementing IPlatformFactory.
                    if (designMode)
                    {
                        implementation = new Platform.Dummy.DummyGLContext();
                    }
                    else
                    {
                        IPlatformFactory factory = null;
                        switch ((flags & GraphicsContextFlags.Embedded) == GraphicsContextFlags.Embedded)
                        {
                        case false: factory = Factory.Default; break;

                        case true: factory = Factory.Embedded; break;
                        }

                        implementation = factory.CreateGLContext(mode, window, shareContext, direct_rendering, major, minor, flags);
                        // Note: this approach does not allow us to mix native and EGL contexts in the same process.
                        // This should not be a problem, as this use-case is not interesting for regular applications.
                        if (GetCurrentContext == null)
                        {
                            GetCurrentContext = factory.CreateGetCurrentGraphicsContext();
                        }
                    }

                    available_contexts.Add((this as IGraphicsContextInternal).Context, new WeakReference(this));
                }
                finally
                {
                    Debug.Unindent();
                }
            }
        }