Beispiel #1
0
        /// <summary>
        /// Create a device context on the specified window.
        /// </summary>
        /// <param name="display">
        /// A <see cref="IntPtr"/> that specifies the display handle associated to <paramref name="windowHandle"/>. Some platforms
        /// ignore this parameter (i.e. Windows or EGL implementation).
        /// </param>
        /// <param name='windowHandle'>
        /// A <see cref="IntPtr"/> that specifies the window handle used to create the device context.
        /// </param>
        /// <exception cref='ArgumentException'>
        /// Is thrown when <paramref name="windowHandle"/> is <see cref="IntPtr.Zero"/>.
        /// </exception>
        /// <exception cref='NotSupportedException'>
        /// Exception thrown if the current platform is not supported.
        /// </exception>
        public static DeviceContext Create(IntPtr display, IntPtr windowHandle)
        {
            DeviceContext deviceContext = null;

            if (IsEglRequired == false)
            {
                switch (Platform.CurrentPlatformId)
                {
                case Platform.Id.WindowsNT:
                    deviceContext = new DeviceContextWGL(windowHandle);
                    break;

                case Platform.Id.Linux:
                    deviceContext = new DeviceContextGLX(display, windowHandle);
                    break;

                case Platform.Id.MacOS:
                    if (Glx.IsRequired)
                    {
                        deviceContext = new DeviceContextGLX(display, windowHandle);
                    }
                    else
                    {
                        throw new NotSupportedException("platform MacOS not supported without Glx.IsRequired=true");
                    }
                    break;

                default:
                    throw new NotSupportedException(String.Format("platform {0} not supported", Environment.OSVersion));
                }
            }
            else
            {
                deviceContext = new DeviceContextEGL(windowHandle);
            }

            deviceContext._Api = _DefaultApi;

            return(deviceContext);
        }
Beispiel #2
0
        /// <summary>
        /// Create a device context on the specified P-Buffer.
        /// </summary>
        /// <param name="nativeBuffer">
        /// A <see cref="INativePBuffer"/> created with <see cref="CreatePBuffer(DevicePixelFormat, uint, uint)"/> which
        /// the created context shall be able to render on.
        /// </param>
        /// <exception cref='NotSupportedException'>
        /// Exception thrown if the current platform is not supported.
        /// </exception>
        public static DeviceContext Create(INativePBuffer nativeBuffer)
        {
            DeviceContext deviceContext = null;

            if (IsEglRequired == false)
            {
                switch (Platform.CurrentPlatformId)
                {
                case Platform.Id.WindowsNT:
                    deviceContext = new DeviceContextWGL(nativeBuffer);
                    break;

                default:
                    throw new NotSupportedException("platform not supported");
                }
            }
            else
            {
                deviceContext = new DeviceContextEGL(nativeBuffer);
            }

            return(deviceContext);
        }
Beispiel #3
0
        /// <summary>
        /// Create a device context without a specific window.
        /// </summary>
        /// <exception cref='NotSupportedException'>
        /// Exception thrown if the current platform is not supported.
        /// </exception>
        public static DeviceContext Create()
        {
            Debug.Assert(Gl._NativeWindow != null);

            DeviceContext deviceContext = null;

            if (IsEglRequired == false)
            {
                switch (Platform.CurrentPlatformId)
                {
                case Platform.Id.WindowsNT:
                    deviceContext = new DeviceContextWGL();
                    break;

                case Platform.Id.Linux:
                    deviceContext = new DeviceContextGLX();
                    break;

                case Platform.Id.MacOS:
                    if (Glx.IsRequired)
                    {
                        deviceContext = new DeviceContextGLX();
                    }
                    else
                    {
                        throw new NotSupportedException("platform MacOS not supported without Glx.IsRequired=true");
                    }
                    break;

                default:
                    throw new NotSupportedException(String.Format("platform {0} not supported", Platform.CurrentPlatformId));
                }
            }
            else
            {
                if (deviceContext == null)
                {
                    INativePBuffer nativeBuffer = Gl._NativeWindow as INativePBuffer;
                    if (nativeBuffer != null)
                    {
                        deviceContext = new DeviceContextEGL(nativeBuffer);
                    }
                }

                if (deviceContext == null)
                {
                    INativeWindow nativeWindow = Gl._NativeWindow as INativeWindow;
                    if (nativeWindow != null)
                    {
                        deviceContext = new DeviceContextEGL(nativeWindow.Handle);
                    }
                }

                if (deviceContext == null)
                {
                    Debug.Fail("unsupported EGL surface");
                    throw new NotSupportedException("EGL surface not supported");
                }
            }

            return(deviceContext);
        }
Beispiel #4
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);
            }
        }
Beispiel #5
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
#if !NETCORE
            string assemblyPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Egl)).Location);
#else
            string assemblyPath = Directory.GetCurrentDirectory();
#endif
            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
                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();
            } finally {
                Terminate(eglDisplay);
            }
        }