示例#1
0
        /// <summary>
        /// Parse a GlslVersion from a string.
        /// </summary>
        /// <param name="input">
        /// A <see cref="String" /> that specifies the API version.
        /// </param>
        /// <param name="api">
        /// </param>
        /// <returns>
        /// It returns a <see cref="KhronosVersion" /> based on the pattern recognized in <paramref name="input" />.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="input" /> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Exception thrown if no pattern is recognized in <paramref name="input" />.
        /// </exception>
        public new static GlslVersion Parse(string input, string api)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            GlslVersion glslVersion = Parse(input);

            switch (api)
            {
            case null:
                break;

            case API_GL:
            case API_GLSL:
                glslVersion = new GlslVersion(glslVersion.Major, glslVersion.Minor, API_GLSL);
                break;

            case API_GLES2:
            case API_GLSC2:
            case API_ESSL:
                glslVersion = new GlslVersion(glslVersion.Major, glslVersion.Minor, API_ESSL);
                break;

            default:
                throw new NotSupportedException($"api \'{api}\' not supported");
            }

            return(glslVersion);
        }
示例#2
0
        /// <summary>
        /// Parse a GlslVersion from a string.
        /// </summary>
        /// <param name="input">
        /// A <see cref="String"/> that specifies the API version.
        /// </param>
        /// <param name="api">
        ///
        /// </param>
        /// <returns>
        /// It returns a <see cref="KhronosVersion"/> based on the pattern recognized in <paramref name="input"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="input"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Exception thrown if no pattern is recognized in <paramref name="input"/>.
        /// </exception>
        public new static GlslVersion Parse(string input, string api)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            GlslVersion glslVersion = Parse(input);

            switch (api)
            {
            case null:
                break;

            case ApiGl:
            case ApiGlsl:
                glslVersion = new GlslVersion(glslVersion.Major, glslVersion.Minor, glslVersion.Revision, ApiGlsl);
                break;

            case ApiGles2:
            case ApiEssl:
                glslVersion = new GlslVersion(glslVersion.Major, glslVersion.Minor, glslVersion.Revision, ApiEssl);
                break;

            default:
                throw new NotSupportedException("api '" + api + "' not supported");
            }

            return(glslVersion);
        }
示例#3
0
文件: Gl.cs 项目: Cryru/Emotion
        /// <summary>
        /// Bind OpenGL delegates.
        /// </summary>
        /// <param name="context">The Emotion graphics context.</param>
        public static void BindAPI(GraphicsContext context)
        {
            // Bind minimal API to query version, and do it.
            BindAPIFunction("glGetError", Version100, null, context);
            BindAPIFunction("glGetString", Version100, null, context);
            BindAPIFunction("glGetIntegerv", Version100, null, context);
            BindAPIFunction("glGetFloatv", Version100, null, context);

            CurrentVersion = QueryContextVersionInternal();

            // Obtain current OpenGL Shading Language version
            string glslVersion = null;

            switch (CurrentVersion.Api)
            {
            case KhronosVersion.API_GL:
                if (CurrentVersion >= Version200 || CurrentExtensions.ShadingLanguage100_ARB)
                {
                    glslVersion = GetString(StringName.ShadingLanguageVersion);
                }
                break;

            case KhronosVersion.API_GLES2:
                glslVersion = GetString(StringName.ShadingLanguageVersion);
                break;
            }

            if (glslVersion != null)
            {
                CurrentShadingVersion = GlslVersion.Parse(glslVersion, CurrentVersion.Api);
            }

            // Query OpenGL extensions (current OpenGL implementation, CurrentCaps)
            if (CurrentVersion.Major >= 3)
            {
                BindAPIFunction("glGetStringi", Version300, null, context);                            // Try to get extensions indexed.
            }
            CurrentExtensions = new Extensions();
            CurrentExtensions.Query();

            // Query OpenGL limits
            CurrentLimits = Limits.Query(CurrentVersion, CurrentExtensions);

            // Bind all functions.
            foreach (FieldInfo fi in _functionContext.Delegates)
            {
                BindAPIFunction(fi, CurrentVersion, CurrentExtensions, context);
            }
        }
示例#4
0
        /// <summary>
        /// Bind the OpenGL delegates for the API corresponding to the current OpenGL context.
        /// </summary>
        /// <param name="procLoadFunction">The function OpenGL functions will be loaded through. Should be provided by your context creator.</param>
        public static void BindAPI(Func <string, IntPtr> procLoadFunction)
        {
            // Set the function loading function.
            KhronosApi.ProcLoadFunction = procLoadFunction;

            // Get version.
            BindAPI(QueryContextVersionCore(), CurrentExtensions);

            // Query OpenGL informations
            string glVersion = GetString(StringName.Version);

            _CurrentVersion = KhronosVersion.Parse(glVersion);

            // Query OpenGL extensions (current OpenGL implementation, CurrentCaps)
            _CurrentExtensions = new Extensions();
            _CurrentExtensions.Query();

            // Query OpenGL limits
            _CurrentLimits = Limits.Query(CurrentVersion, _CurrentExtensions);

            // Obtain current OpenGL Shading Language version
            string glslVersion = null;

            switch (_CurrentVersion.Api)
            {
            case KhronosVersion.ApiGl:
                if (_CurrentVersion >= Version_200 || _CurrentExtensions.ShadingLanguage100_ARB)
                {
                    glslVersion = GetString(StringName.ShadingLanguageVersion);
                }
                break;

            case KhronosVersion.ApiGles2:
                glslVersion = GetString(StringName.ShadingLanguageVersion);
                break;
            }

            if (glslVersion != null)
            {
                _CurrentShadingVersion = GlslVersion.Parse(glslVersion, _CurrentVersion.Api);
            }

            // Vendor/Render information
            _Vendor   = GetString(StringName.Vendor);
            _Renderer = GetString(StringName.Renderer);
        }
示例#5
0
        /// <summary>
        /// Bind the OpenGL delegates for the API corresponding to the current OpenGL context.
        /// </summary>
        /// <param name="procLoadFunction">
        /// The function OpenGL functions will be loaded through. Should be provided by your context creator.
        /// </param>
        public static void BindAPI(Func <string, IntPtr> procLoadFunction)
        {
            // Set the function loading function.
            ProcLoadFunction = procLoadFunction;

            // Get version.
            CurrentVersion = QueryContextVersion();
            BindAPI <Gl>(CurrentVersion, CurrentExtensions);

            // Query OpenGL extensions (current OpenGL implementation, CurrentCaps)
            CurrentExtensions = new Extensions();
            CurrentExtensions.Query();

            // Query OpenGL limits
            CurrentLimits = Limits.Query(CurrentVersion, CurrentExtensions);

            // Obtain current OpenGL Shading Language version
            string glslVersion = null;

            switch (CurrentVersion.Api)
            {
            case KhronosVersion.API_GL:
                if (CurrentVersion >= Version200 || CurrentExtensions.ShadingLanguage100_ARB)
                {
                    glslVersion = GetString(StringName.ShadingLanguageVersion);
                }
                break;

            case KhronosVersion.API_GLES2:
                glslVersion = GetString(StringName.ShadingLanguageVersion);
                break;
            }

            if (glslVersion != null)
            {
                CurrentShadingVersion = GlslVersion.Parse(glslVersion, CurrentVersion.Api);
            }

            // Vendor/Render information
            CurrentVendor   = GetString(StringName.Vendor);
            CurrentRenderer = GetString(StringName.Renderer);
        }
示例#6
0
        /// <summary>
        /// Initialize OpenGL namespace static environment. This method shall be called before any other classes methods.
        /// </summary>
        public static void Initialize()
        {
            if (_Initialized == true)
            {
                return;                 // Already initialized
            }
            _Initialized = true;
#if !NETSTANDARD1_1
            // Optional initialization
            string envGlInit = Environment.GetEnvironmentVariable("OPENGL_NET_INIT");
            if (envGlInit != null && envGlInit == "NO")
            {
                return;
            }
#endif
            // Environment options
            LogComment("OpenGL.Net is initializing");

            // Loader function			OS API			GL API
            // ------------------------------------------------------
            // Supported platform: Windows
            // wglGetProcAddress		WGL				GL
            // wglGetProcAddress		WGL				GLES2+ (with WGL_create_context_es(2)?_profile_EXT)
            // eglGetProcAddress		EGL(Angle)		GLES2+
            // ------------------------------------------------------
            // Supported platform: Linux
            // glXGetProcAddress		GLX				GL
            // glXGetProcAddress		GLX				GLES2+ (with GLX_create_context_es(2)?_profile_EXT)
            // eglGetProcAddress		EGL				GLES2+
            // ------------------------------------------------------
            // Supported platform: Android
            // eglGetProcAddress		EGL				GL
            // eglGetProcAddress		EGL				GLES2+

            try {
#if !MONODROID
                // Determine whether use EGL as device context backend
                if (Egl.IsAvailable)
                {
                    switch (Platform.CurrentPlatformId)
                    {
                    case Platform.Id.Linux:
                        if (Glx.IsAvailable == false)
                        {
                            Egl.IsRequired = true;
                        }
                        break;
                    }
                }
#endif

                // Create native window for getting preliminary information on desktop systems
                // This instance will be used for creating contexts without explictly specify a window
                _NativeWindow = DeviceContext.CreateHiddenWindow();

                // Create device context
                using (DeviceContext windowDevice = DeviceContext.Create()) {
                    // Create basic OpenGL context
                    IntPtr renderContext = windowDevice.CreateSimpleContext();
                    if (renderContext == IntPtr.Zero)
                    {
                        throw new NotImplementedException("unable to create a simple context");
                    }

                    // Make contect current
                    if (windowDevice.MakeCurrent(renderContext) == false)
                    {
                        throw new InvalidOperationException("unable to make current", windowDevice.GetPlatformException());
                    }

#if !MONODROID
                    // Reload platform function pointers, if required
                    if (Egl.IsRequired == false)
                    {
                        switch (Platform.CurrentPlatformId)
                        {
                        case Platform.Id.WindowsNT:
                            Wgl.BindAPI();
                            break;
                        }
                    }
#endif

                    // Query OpenGL informations
                    string glVersion = GetString(StringName.Version);
                    _CurrentVersion = KhronosVersion.Parse(glVersion);

                    // Query OpenGL extensions (current OpenGL implementation, CurrentCaps)
                    _CurrentExtensions = new Extensions();
                    _CurrentExtensions.Query();
                    // Query platform extensions
                    windowDevice.QueryPlatformExtensions();
                    // Query OpenGL limits
                    _CurrentLimits = Limits.Query(Gl.CurrentVersion, _CurrentExtensions);

                    // Obtain current OpenGL Shading Language version
                    string glslVersion = null;

                    switch (_CurrentVersion.Api)
                    {
                    case KhronosVersion.ApiGl:
                        if (_CurrentVersion >= Version_200 || _CurrentExtensions.ShadingLanguage100_ARB)
                        {
                            glslVersion = GetString(StringName.ShadingLanguageVersion);
                        }
                        break;

                    case KhronosVersion.ApiGles2:
                        glslVersion = GetString(StringName.ShadingLanguageVersion);
                        break;
                    }

                    if (glslVersion != null)
                    {
                        _CurrentShadingVersion = GlslVersion.Parse(glslVersion, _CurrentVersion.Api);
                    }

                    // Vendor/Render information
                    _Vendor   = GetString(StringName.Vendor);
                    _Renderer = GetString(StringName.Renderer);

                    if (EnvDebug || EnvExperimental)
                    {
                        Debug.Assert(CurrentVersion != null && CurrentExtensions != null);
                        CheckExtensionCommands <Gl>(CurrentVersion, CurrentExtensions, EnvExperimental);
                    }

                    // Before deletion, make uncurrent
                    windowDevice.MakeCurrent(IntPtr.Zero);
                    // Detroy context
                    if (windowDevice.DeleteContext(renderContext) == false)
                    {
                        throw new InvalidOperationException("unable to delete OpenGL context");
                    }
                }

                LogComment("OpenGL.Net has been initialized");
            } catch (Exception excepton) {
                _InitializationException = excepton;
                LogComment("Unable to initialize OpenGL.Net: {0}", _InitializationException.ToString());
            }
        }
示例#7
0
        /// <summary>
        /// Initialize OpenGL namespace static environment. This method shall be called before any other classes methods.
        /// </summary>
        public static void Initialize()
        {
            if (_Initialized == true)
            {
                return;                 // Already initialized
            }
            _Initialized = true;

#if DEBUG
            string envGlInit = Environment.GetEnvironmentVariable("GL_INIT");

            if (envGlInit != null && envGlInit == "NO")
            {
                return;
            }
#endif

            LogComment("OpenGL.Net is initializing");

            // Loader function			OS API			GL API
            // ------------------------------------------------------
            // Supported platform: Windows
            // wglGetProcAddress		WGL				GL
            // wglGetProcAddress		WGL				GLES2+ (with WGL_create_context_es(2)?_profile_EXT)
            // eglGetProcAddress		EGL(Angle)		GLES2+
            // ------------------------------------------------------
            // Supported platform: Linux
            // glXGetProcAddress		GLX				GL
            // glXGetProcAddress		GLX				GLES2+ (with GLX_create_context_es(2)?_profile_EXT)
            // ------------------------------------------------------
            // Supported platform: Android
            // eglGetProcAddress		EGL				GL
            // eglGetProcAddress		EGL				GLES2+

            try {
                // Create native window for getting preliminary information on desktop systems
                // This instance will be used for creating contexts without explictly specify a window
                _NativeWindow = DeviceContext.CreateHiddenWindow();

                // Create device context
                using (DeviceContext windowDevice = DeviceContext.Create()) {
                    // Create basic OpenGL context
                    IntPtr renderContext = windowDevice.CreateSimpleContext();
                    if (renderContext == IntPtr.Zero)
                    {
                        throw new NotImplementedException("unable to create a simple context");
                    }

                    // Make contect current
                    if (windowDevice.MakeCurrent(renderContext) == false)
                    {
                        throw new InvalidOperationException("unable to make current", windowDevice.GetPlatformException());
                    }

                    // Query OpenGL informations
                    string glVersion = GetString(StringName.Version);
                    _CurrentVersion = KhronosVersion.Parse(glVersion);

                    // Obtain current OpenGL Shading Language version
                    switch (_CurrentVersion.Api)
                    {
                    case KhronosVersion.ApiGl:
                    case KhronosVersion.ApiGles2:
                        string glslVersion = GetString(StringName.ShadingLanguageVersion);
                        _CurrentShadingVersion = GlslVersion.Parse(glslVersion);
                        break;
                    }

                    // Vendor/Render information
                    _Vendor   = GetString(StringName.Vendor);
                    _Renderer = GetString(StringName.Renderer);

                    // Query OpenGL extensions (current OpenGL implementation, CurrentCaps)
                    _CurrentExtensions = new Extensions();
                    _CurrentExtensions.Query();

                    // Query OpenGL limits
                    _CurrentLimits = Limits.Query(_CurrentExtensions);

                    // Query platform extensions
                    windowDevice.QueryPlatformExtensions();

                    // Before deletion, make uncurrent
                    windowDevice.MakeCurrent(IntPtr.Zero);
                    // Detroy context
                    if (windowDevice.DeleteContext(renderContext) == false)
                    {
                        throw new InvalidOperationException("unable to delete OpenGL context");
                    }
                }

                LogComment("OpenGL.Net has been initialized");
            } catch (Exception excepton) {
                LogComment("Unable to initialize OpenGL.Net: {0}", excepton.ToString());
            }
        }
示例#8
0
        /// <summary>
        /// Static constructor.
        /// </summary>
        static Gl()
        {
            // Cache imports & delegates
            _Delegates = GetDelegateList(typeof(Gl));
            _ImportMap = GetImportMap(typeof(Gl));
            // Load procedures (OpenGL desktop by default) @todo Really necessary?
            LoadProcDelegates(_ImportMap, _Delegates);

            // Create common hidden window
            _HiddenWindow = new System.Windows.Forms.Form();
            // Create device context
            _HiddenWindowDevice = DeviceContextFactory.Create(_HiddenWindow);
            _HiddenWindowDevice.IncRef();

            // Create basic OpenGL context
            IntPtr renderContext;

            if (_HiddenWindowDevice is WindowsDeviceContext)
            {
                renderContext = CreateWinSimpleContext(_HiddenWindowDevice);
            }
            else if (_HiddenWindowDevice is XServerDeviceContext)
            {
                renderContext = CreateX11SimpleContext(_HiddenWindowDevice);
            }
            else if (_HiddenWindowDevice is NativeDeviceContext)
            {
                renderContext = CreateEglSimpleContext(_HiddenWindowDevice);
            }
            else
            {
                throw new NotImplementedException(String.Format("{0} is not a supported device context", _HiddenWindowDevice.GetType()));
            }

            // Query OpenGL informations
            if (_HiddenWindowDevice.MakeCurrent(renderContext) == false)
            {
                throw new InvalidOperationException("unable to make current");
            }

            // Obtain current OpenGL implementation
            string glVersion = Gl.GetString(StringName.Version);

            _CurrentVersion = KhronosVersion.Parse(glVersion);

            // Obtain current OpenGL Shading Language version
            string glslVersion = Gl.GetString(StringName.ShadingLanguageVersion);

            _CurrentShadingVersion = GlslVersion.Parse(glslVersion);

            // Query OpenGL extensions (current OpenGL implementation, CurrentCaps)
            _CurrentExtensions = new Extensions();
            _CurrentExtensions.Query();
            // Query OpenGL limits
            _CurrentLimits = Limits.Query(_CurrentExtensions);

            if (_HiddenWindowDevice is WindowsDeviceContext)
            {
                Wgl._CurrentExtensions = new Wgl.Extensions();
                Wgl._CurrentExtensions.Query((WindowsDeviceContext)_HiddenWindowDevice);
            }
            else if (_HiddenWindowDevice is XServerDeviceContext)
            {
                Glx._CurrentExtensions = new Glx.Extensions();
                Glx._CurrentExtensions.Query((XServerDeviceContext)_HiddenWindowDevice);
            }
            else if (_HiddenWindowDevice is NativeDeviceContext)
            {
                Egl._CurrentExtensions = new Egl.Extensions();
                Egl._CurrentExtensions.Query((NativeDeviceContext)_HiddenWindowDevice);
            }

            // Before deletion, make uncurrent
            _HiddenWindowDevice.MakeCurrent(IntPtr.Zero);
            // Detroy context
            if (_HiddenWindowDevice.DeleteContext(renderContext) == false)
            {
                throw new InvalidOperationException("unable to delete OpenGL context");
            }
        }