Exemple #1
0
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (Object.ReferenceEquals(value, null))
            {
                return(base.ConvertFrom(context, culture, value));
            }

            Type valueType = value.GetType();

            if (valueType == typeof(string))
            {
                try {
                    string valueString = (string)value;

                    if (valueString == String.Empty)
                    {
                        return(null);
                    }

                    return(KhronosVersion.Parse(valueString));
                } catch (Exception exception) {
                    throw new NotSupportedException("unable to parse the value", exception);
                }
            }

            // Base implementation
            return(base.ConvertFrom(context, culture, value));
        }
Exemple #2
0
            /// <summary>
            /// Query the extensions supported by current platform.
            /// </summary>
            /// <remarks>
            /// An OpenGL context must be current on the calling thread.
            /// </remarks>
            public void Query()
            {
                LogComment("Query GL extensions.");

                string glVersionString = GetString(StringName.Version);

                if (glVersionString == null)
                {
                    throw new InvalidOperationException("unable to determine OpenGL version");
                }

                KhronosVersion glVersion         = KhronosVersion.Parse(glVersionString);
                bool           indexedExtensions = (glVersion.Major >= 3) && (Delegates.pglGetStringi != null);

                if (indexedExtensions)
                {
                    int extensionCount;

                    Get(GetPName.NumExtensions, out extensionCount);

                    List <string> extensions = new List <string>();
                    for (uint i = 0; i < (uint)extensionCount; i++)
                    {
                        extensions.Add(GetString(StringName.Extensions, i));
                    }

                    Query(glVersion, extensions.ToArray());
                }
                else
                {
                    Query(glVersion, GetString(StringName.Extensions));
                }
            }
Exemple #3
0
        /// <summary>
        /// Query the version of the current OpenGL context.
        /// </summary>
        /// <returns>
        /// It returns the <see cref="KhronosVersion"/> specifying the actual version of the context current on this thread.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// Exception thrown if no GL context is current on the calling thread.
        /// </exception>
        public static KhronosVersion QueryContextVersion()
        {
            // Parse version string (effective for detecting Desktop and ES contextes)
            KhronosVersion glversion = KhronosVersion.Parse(GetString(StringName.Version));

            // Context profile
            if (glversion.Api == KhronosVersion.ApiGl && glversion >= Version_320)
            {
                string glProfile;
                int    ctxProfile;

                Get(CONTEXT_PROFILE_MASK, out ctxProfile);

                if ((ctxProfile & CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0)
                {
                    glProfile = KhronosVersion.ProfileCompatibility;
                }
                else if ((ctxProfile & CONTEXT_CORE_PROFILE_BIT) != 0)
                {
                    glProfile = KhronosVersion.ProfileCore;
                }
                else
                {
                    glProfile = KhronosVersion.ProfileCompatibility;
                }

                return(new KhronosVersion(glversion, glProfile));
            }
            else
            {
                return(new KhronosVersion(glversion, KhronosVersion.ProfileCompatibility));
            }
        }
Exemple #4
0
        /// <summary>
        /// Query the version of the current OpenGL context.
        /// </summary>
        /// <returns>
        /// It returns the <see cref="KhronosVersion"/> specifying teh actual version of <paramref name="ctx"/>.
        /// </returns>
        internal static KhronosVersion QueryContextVersion()
        {
            IntPtr ctx = GetCurrentContext();

            if (ctx == null)
            {
                throw new InvalidOperationException("no current context");
            }

            // Load minimal Gl functions for querying information
            IGetProcAddress getProcAddress = GetProcAddress.GetProcAddressGL;

            if (Egl.IsRequired == false)
            {
                Gl.BindAPIFunction(Gl.Version_100, "glGetError", getProcAddress);
                Gl.BindAPIFunction(Gl.Version_100, "glGetString", getProcAddress);
            }
            else
            {
                Gl.BindAPIFunction(Gl.Version_320_ES, "glGetError", getProcAddress);
                Gl.BindAPIFunction(Gl.Version_320_ES, "glGetString", getProcAddress);
            }

            // Parse version string (effective for detecting Desktop and ES contextes)
            KhronosVersion glversion = KhronosVersion.Parse(Gl.GetString(StringName.Version));

            // ATM do not support fancy context creation flags

            return(glversion);
        }
Exemple #5
0
        /// <summary>
        /// Query the version of the current OpenGL context.
        /// </summary>
        /// <returns>
        /// It returns the <see cref="KhronosVersion"/> specifying teh actual version of <paramref name="ctx"/>.
        /// </returns>
        private static KhronosVersion QueryContextVersion()
        {
            IntPtr ctx = DeviceContext.GetCurrentContext();

            if (ctx == null)
            {
                throw new InvalidOperationException("no current context");
            }

            // Load minimal Gl functions for querying information
            IGetProcAddress getProcAddress = GetProcAddress.GetProcAddressGL;

            if (Egl.IsRequired == false)
            {
                Gl.BindAPIFunction(Gl.Version_100, null, "glGetError", getProcAddress);
                Gl.BindAPIFunction(Gl.Version_100, null, "glGetString", getProcAddress);
                Gl.BindAPIFunction(Gl.Version_100, null, "glGetIntegerv", getProcAddress);
            }
            else
            {
                Gl.BindAPIFunction(Gl.Version_320_ES, null, "glGetError", getProcAddress);
                Gl.BindAPIFunction(Gl.Version_320_ES, null, "glGetString", getProcAddress);
                Gl.BindAPIFunction(Gl.Version_320_ES, null, "glGetIntegerv", getProcAddress);
            }

            // Parse version string (effective for detecting Desktop and ES contextes)
            KhronosVersion glversion = KhronosVersion.Parse(Gl.GetString(StringName.Version));

            // Context profile
            if (glversion.Api == KhronosVersion.ApiGl && glversion >= Gl.Version_320)
            {
                string glProfile  = null;
                int    ctxProfile = 0;

                Gl.Get(Gl.CONTEXT_PROFILE_MASK, out ctxProfile);

                if ((ctxProfile & Gl.CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0)
                {
                    glProfile = KhronosVersion.ProfileCompatibility;
                }
                else if ((ctxProfile & Gl.CONTEXT_CORE_PROFILE_BIT) != 0)
                {
                    glProfile = KhronosVersion.ProfileCore;
                }
                else
                {
                    glProfile = KhronosVersion.ProfileCompatibility;
                }

                return(new KhronosVersion(glversion, glProfile));
            }
            else
            {
                return(new KhronosVersion(glversion, KhronosVersion.ProfileCompatibility));
            }
        }
        /// <summary>
        /// Query the OpenGL version without binding the API.
        /// </summary>
        /// <param name="procLoadFunction">The function OpenGL functions will be loaded through (in this case glGetString only).</param>
        /// <returns>
        /// It returns the <see cref="KhronosVersion" /> specifying the actual version of the context current on this
        /// thread.
        /// </returns>
        public static KhronosVersion QueryVersionExternal(Func <string, IntPtr> procLoadFunction)
        {
            IntPtr func = procLoadFunction("glGetString");

            if (func == IntPtr.Zero)
            {
                return(null);
            }

            var    getString = Marshal.GetDelegateForFunctionPointer <Delegates.glGetString>(func);
            IntPtr ptr       = getString((int)StringName.Version);
            string str       = NativeHelpers.StringFromPtr(ptr);

            return(KhronosVersion.Parse(str));
        }
Exemple #7
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);
        }
Exemple #8
0
        /// <summary>
        /// Query the OpenGL version. Requires some information lookup functions to be bound.
        /// </summary>
        /// <returns>The version of the current OpenGL context.</returns>
        private static KhronosVersion QueryContextVersionInternal()
        {
            // Parse version string (effective for detecting Desktop and ES contextes)
            string         str       = GetString(StringName.Version);
            KhronosVersion glVersion = KhronosVersion.Parse(str);

            // Vendor/Render information
            CurrentVendor    = GetString(StringName.Vendor);
            CurrentRenderer  = GetString(StringName.Renderer);
            SoftwareRenderer = CurrentRenderer.Contains("llvmpipe");

            // Context profile
            if (glVersion.Api == KhronosVersion.API_GL && glVersion >= Version320)
            {
                string glProfile;

                Get(CONTEXT_PROFILE_MASK, out int ctxProfile);

                if ((ctxProfile & CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0)
                {
                    glProfile = KhronosVersion.PROFILE_COMPATIBILITY;
                }
                else if ((ctxProfile & CONTEXT_CORE_PROFILE_BIT) != 0)
                {
                    glProfile = KhronosVersion.PROFILE_CORE;
                }
                else
                {
                    glProfile = KhronosVersion.PROFILE_COMPATIBILITY;
                }

                return(new KhronosVersion(glVersion, glProfile));
            }
            else
            {
                string profile = KhronosVersion.PROFILE_COMPATIBILITY;
                if (CurrentRenderer.Contains("WebGL"))
                {
                    profile = KhronosVersion.PROFILE_WEBGL;
                }
                return(new KhronosVersion(glVersion, profile));
            }
        }
Exemple #9
0
        /// <summary>
        /// Query the version of the current OpenGL context.
        /// </summary>
        /// <returns>
        /// It returns the <see cref="KhronosVersion"/> specifying the actual version of the context current on this thread.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// Exception thrown if no GL context is current on the calling thread.
        /// </exception>
        public static KhronosVersion QueryContextVersion()
        {
            IntPtr ctx = DeviceContext.GetCurrentContext();

            if (ctx == IntPtr.Zero)
            {
                throw new InvalidOperationException("no current context");
            }

            // Parse version string (effective for detecting Desktop and ES contextes)
            KhronosVersion glversion = KhronosVersion.Parse(Gl.GetString(StringName.Version));

            // Context profile
            if (glversion.Api == KhronosVersion.ApiGl && glversion >= Gl.Version_320)
            {
                string glProfile  = null;
                int    ctxProfile = 0;

                Gl.Get(Gl.CONTEXT_PROFILE_MASK, out ctxProfile);

                if ((ctxProfile & Gl.CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0)
                {
                    glProfile = KhronosVersion.ProfileCompatibility;
                }
                else if ((ctxProfile & Gl.CONTEXT_CORE_PROFILE_BIT) != 0)
                {
                    glProfile = KhronosVersion.ProfileCore;
                }
                else
                {
                    glProfile = KhronosVersion.ProfileCompatibility;
                }

                return(new KhronosVersion(glversion, glProfile));
            }
            else
            {
                return(new KhronosVersion(glversion, KhronosVersion.ProfileCompatibility));
            }
        }
Exemple #10
0
        /// <summary>
        /// Query the version of the current OpenGL context.
        /// </summary>
        /// <returns>
        /// It returns the <see cref="KhronosVersion" /> specifying the actual version of the context current on this thread.
        /// </returns>
        public static KhronosVersion QueryContextVersion()
        {
            BindAPIFunction <Gl>("glGetError", Version100, null);
            BindAPIFunction <Gl>("glGetString", Version100, null);
            BindAPIFunction <Gl>("glGetIntegerv", Version100, null);

            // Parse version string (effective for detecting Desktop and ES contextes)
            string         str       = GetString(StringName.Version);
            KhronosVersion glVersion = KhronosVersion.Parse(str);

            // Context profile
            if (glVersion.Api == KhronosVersion.API_GL && glVersion >= Version320)
            {
                string glProfile;

                Get(CONTEXT_PROFILE_MASK, out int ctxProfile);

                if ((ctxProfile & CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0)
                {
                    glProfile = KhronosVersion.PROFILE_COMPATIBILITY;
                }
                else if ((ctxProfile & CONTEXT_CORE_PROFILE_BIT) != 0)
                {
                    glProfile = KhronosVersion.PROFILE_CORE;
                }
                else
                {
                    glProfile = KhronosVersion.PROFILE_COMPATIBILITY;
                }

                return(new KhronosVersion(glVersion, glProfile));
            }
            else
            {
                return(new KhronosVersion(glVersion, KhronosVersion.PROFILE_COMPATIBILITY));
            }
        }
Exemple #11
0
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (Object.ReferenceEquals(value, null))
            {
                return(base.ConvertFrom(context, culture, value));
            }

            Type valueType = value.GetType();

            if (valueType == typeof(string))
            {
                string valueString = (string)value;

                if (valueString == String.Empty)
                {
                    return(null);
                }

                return(KhronosVersion.Parse(valueString));
            }

            // Base implementation
            return(base.ConvertFrom(context, culture, value));
        }
Exemple #12
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());
            }
        }
Exemple #13
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;

            // Before linking procedures, append ANGLE directory in path
            string assemblyPath = GetAssemblyLocation();
            string anglePath    = null;

            switch (Platform.CurrentPlatformId)
            {
            case Platform.Id.WindowsNT:
                if (assemblyPath != null)
                {
#if DEBUG
                    if (IntPtr.Size == 8)
                    {
                        anglePath = Path.Combine(assemblyPath, @"ANGLE\winrt10d\x64");
                    }
                    else
                    {
                        anglePath = Path.Combine(assemblyPath, @"ANGLE\winrt10d\x86");
                    }
#else
                    if (IntPtr.Size == 8)
                    {
                        anglePath = Path.Combine(assemblyPath, @"ANGLE\winrt10\x64");
                    }
                    else
                    {
                        anglePath = Path.Combine(assemblyPath, @"ANGLE\winrt10\x86");
                    }
#endif
                }
                break;

            case Platform.Id.Linux:
                // Note: on RPi libEGL.so depends on libGLESv2.so, so it's required to pre-load the shared library
                // Note: maybe a configurable and generic method for pre-loading assemblies may be introduced
                GetProcAddressLinux.GetLibraryHandle("libGLESv2.so", false);
                break;
            }

            // Include ANGLE path, if any
#if NETSTANDARD1_1
            if (anglePath != String.Empty)
            {
                OpenGL.GetProcAddressOS.AddLibraryDirectory(Path.Combine(assemblyPath, anglePath));
            }
#else
            if (anglePath != null && Directory.Exists(anglePath))
            {
                OpenGL.GetProcAddressOS.AddLibraryDirectory(Path.Combine(assemblyPath, anglePath));
            }
#endif

            // Load procedures
            BindAPI();

            if (IsAvailable == false)
            {
                return;
            }

#if DEBUG
            string envEglInit = Environment.GetEnvironmentVariable("EGL_INIT");

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

            // Platform initialization
            EglEventArgs args = new EglEventArgs();

            RaiseEglInitializing(args);

            // Get EGL information
            IntPtr eglDisplay = Egl.GetDisplay(args.Display);

            try {
                if (Initialize(eglDisplay, null, null) == false)
                {
                    throw new InvalidOperationException("unable to initialize EGL");
                }

                // Query EGL version
                string eglVersionString = QueryString(eglDisplay, VERSION);
                _CurrentVersion = KhronosVersion.Parse(eglVersionString, KhronosVersion.ApiEgl);
                // Query EGL vendor
                _Vendor = QueryString(eglDisplay, VENDOR);
                // Client APIs
                List <string> clientApis = new List <string>();

                if (_CurrentVersion >= Version_120)
                {
                    string   clientApisString = QueryString(eglDisplay, CLIENT_APIS);
                    string[] clientApiTokens  = System.Text.RegularExpressions.Regex.Split(clientApisString, " ");

                    foreach (string api in DeviceContextEGL.ConvertApiNames(clientApiTokens))
                    {
                        clientApis.Add(api);
                    }
                }

                _AvailableApis = clientApis.ToArray();

                // Null device context for querying extensions
                using (DeviceContextEGL deviceContext = new DeviceContextEGL(args.Display, IntPtr.Zero)) {
                    _CurrentExtensions = new Extensions();
                    _CurrentExtensions.Query(deviceContext);
                }
            } finally {
                Terminate(eglDisplay);
            }
        }
Exemple #14
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;

            // Before linking procedures, append ANGLE directory in path
            string assemblyPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Egl)).Location);
            string anglePath    = null;

            switch (Platform.CurrentPlatformId)
            {
            case Platform.Id.WindowsNT:
#if DEBUG
                if (IntPtr.Size == 8)
                {
                    anglePath = Path.Combine(assemblyPath, @"ANGLE\winrt10d\x64");
                }
                else
                {
                    anglePath = Path.Combine(assemblyPath, @"ANGLE\winrt10d\x86");
                }
#else
                if (IntPtr.Size == 8)
                {
                    anglePath = Path.Combine(assemblyPath, @"ANGLE\winrt10\x64");
                }
                else
                {
                    anglePath = Path.Combine(assemblyPath, @"ANGLE\winrt10\x86");
                }
#endif
                break;

            case Platform.Id.Linux:
                // Note: on RPi libEGL.so depends on libGLESv2.so, so it's required to pre-load the shared library
                GetProcAddressX11.GetLibraryHandle("libGLESv2.so", false);
                break;
            }

            // Include ANGLE path, if any
            if (anglePath != null && Directory.Exists(anglePath))
            {
                OpenGL.GetProcAddress.GetProcAddressOS.AddLibraryDirectory(Path.Combine(assemblyPath, anglePath));
            }

            // Load procedures
            string platformLibrary = GetPlatformLibrary();
            try {
                LogComment("Querying EGL from {0}", platformLibrary);
                BindAPI <Egl>(platformLibrary, OpenGL.GetProcAddress.GetProcAddressOS);
                LogComment("EGL availability: {0}", IsAvailable);
            } catch (Exception exception) {
                /* Fail-safe (it may fail due Egl access) */
                LogComment("EGL not available:\n{0}", exception.ToString());
            }

            if (IsAvailable == false)
            {
                return;
            }

#if DEBUG
            string envEglInit = Environment.GetEnvironmentVariable("EGL_INIT");

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

            // Platform initialization
            EglEventArgs args = new EglEventArgs();

            RaiseEglInitializing(args);

            // Get EGL information
            IntPtr eglDisplay = Egl.GetDisplay(args.Display);

            try {
                if (Initialize(eglDisplay, null, null) == false)
                {
                    throw new InvalidOperationException("unable to initialize EGL");
                }

                // Query EGL version
                string eglVersionString = QueryString(eglDisplay, VERSION);
                _CurrentVersion = KhronosVersion.Parse(eglVersionString, KhronosVersion.ApiEgl);
                // Query EGL vendor
                _Vendor = QueryString(eglDisplay, VENDOR);
                // Client APIs
                if (_CurrentVersion >= Version_120)
                {
                    string clientApisString = QueryString(eglDisplay, CLIENT_APIS);
                    _AvailableApis = System.Text.RegularExpressions.Regex.Split(clientApisString, " ");
                }
            } finally {
                Terminate(eglDisplay);
            }
        }
Exemple #15
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());
            }
        }
Exemple #16
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");
            }
        }