public static GlContextInfo Create(GlVersion version, Func <string, IntPtr> getProcAddress)
            {
                var basicInfoInterface = new GlBasicInfoInterface(getProcAddress);
                var exts = basicInfoInterface.GetExtensions();

                return(new GlContextInfo(version, new HashSet <string>(exts)));
            }
 public static GlInterface FromNativeUtf8GetProcAddress(GlVersion version, Func <IntPtr, IntPtr> getProcAddress)
 {
     return(new GlInterface(version, s =>
     {
         var ptr = Marshal.StringToHGlobalAnsi(s);
         var rv = getProcAddress(ptr);
         Marshal.FreeHGlobal(ptr);
         return rv;
     }));
 }
Exemple #3
0
 public EglContext(EglDisplay display, EglInterface egl, IntPtr ctx, EglSurface offscreenSurface,
                   GlVersion version, int sampleCount, int stencilSize)
 {
     _disp            = display;
     _egl             = egl;
     Context          = ctx;
     OffscreenSurface = offscreenSurface;
     Version          = version;
     SampleCount      = sampleCount;
     StencilSize      = stencilSize;
     using (MakeCurrent())
         GlInterface = GlInterface.FromNativeUtf8GetProcAddress(version, b => _egl.GetProcAddress(b));
 }
 public static GlInterface FromNativeUtf8GetProcAddress(GlVersion version, Func <Utf8Buffer, IntPtr> getProcAddress) =>
 new GlInterface(version, getProcAddress);
 public GlInterface(GlVersion version, Func <Utf8Buffer, IntPtr> n) : this(version, ConvertNative(n))
 {
 }
 public GlInterface(GlVersion version, Func <string, IntPtr> getProcAddress) : this(
         GlContextInfo.Create(version, getProcAddress), getProcAddress)
 {
 }
 public GlContextInfo(GlVersion version, HashSet <string> extensions)
 {
     Version    = version;
     Extensions = extensions;
 }
Exemple #8
0
        public EglDisplay(EglInterface egl, int platformType, IntPtr platformDisplay, int[] attrs)
        {
            _egl = egl;

            if (platformType == -1 && platformDisplay == IntPtr.Zero)
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    if (_egl.GetPlatformDisplayEXT == null)
                    {
                        throw new OpenGlException("eglGetPlatformDisplayEXT is not supported by libegl.dll");
                    }

                    var allowedApis = AvaloniaLocator.Current.GetService <AngleOptions>()?.AllowedPlatformApis
                                      ?? new List <AngleOptions.PlatformApi> {
                        AngleOptions.PlatformApi.DirectX9
                    };

                    foreach (var platformApi in allowedApis)
                    {
                        int dapi;
                        if (platformApi == AngleOptions.PlatformApi.DirectX9)
                        {
                            dapi = EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE;
                        }
                        else if (platformApi == AngleOptions.PlatformApi.DirectX11)
                        {
                            dapi = EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE;
                        }
                        else
                        {
                            continue;
                        }

                        _display = _egl.GetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, IntPtr.Zero,
                                                              new[] { EGL_PLATFORM_ANGLE_TYPE_ANGLE, dapi, EGL_NONE });
                        if (_display != IntPtr.Zero)
                        {
                            _angleApi = platformApi;
                            break;
                        }
                    }

                    if (_display == IntPtr.Zero)
                    {
                        throw new OpenGlException("Unable to create ANGLE display");
                    }
                }

                if (_display == IntPtr.Zero)
                {
                    _display = _egl.GetDisplay(IntPtr.Zero);
                }
            }
            else
            {
                if (_egl.GetPlatformDisplayEXT == null)
                {
                    throw new OpenGlException("eglGetPlatformDisplayEXT is not supported by libegl");
                }
                _display = _egl.GetPlatformDisplayEXT(platformType, platformDisplay, attrs);
            }

            if (_display == IntPtr.Zero)
            {
                throw OpenGlException.GetFormattedException("eglGetDisplay", _egl);
            }

            if (!_egl.Initialize(_display, out var major, out var minor))
            {
                throw OpenGlException.GetFormattedException("eglInitialize", _egl);
            }

            foreach (var cfg in new[]
            {
                new
                {
                    Attributes = new[]
                    {
                        EGL_CONTEXT_CLIENT_VERSION, 2,
                        EGL_NONE
                    },
                    Api = EGL_OPENGL_ES_API,
                    RenderableTypeBit = EGL_OPENGL_ES2_BIT,
                    Version = new GlVersion(GlProfileType.OpenGLES, 2, 0)
                }
            })
            {
                if (!_egl.BindApi(cfg.Api))
                {
                    continue;
                }
                foreach (var surfaceType in new[] { EGL_PBUFFER_BIT | EGL_WINDOW_BIT, EGL_WINDOW_BIT })
                {
                    foreach (var stencilSize in new[] { 8, 1, 0 })
                    {
                        foreach (var depthSize in new [] { 8, 1, 0 })
                        {
                            var attribs = new[]
                            {
                                EGL_SURFACE_TYPE, surfaceType,
                                EGL_RENDERABLE_TYPE, cfg.RenderableTypeBit,
                                EGL_RED_SIZE, 8,
                                EGL_GREEN_SIZE, 8,
                                EGL_BLUE_SIZE, 8,
                                EGL_ALPHA_SIZE, 8,
                                EGL_STENCIL_SIZE, stencilSize,
                                EGL_DEPTH_SIZE, depthSize,
                                EGL_NONE
                            };
                            if (!_egl.ChooseConfig(_display, attribs, out _config, 1, out int numConfigs))
                            {
                                continue;
                            }
                            if (numConfigs == 0)
                            {
                                continue;
                            }
                            _contextAttributes = cfg.Attributes;
                            _surfaceType       = surfaceType;
                            _version           = cfg.Version;
                            _egl.GetConfigAttrib(_display, _config, EGL_SAMPLES, out _sampleCount);
                            _egl.GetConfigAttrib(_display, _config, EGL_STENCIL_SIZE, out _stencilSize);
                            goto Found;
                        }
                    }
                }
            }
Found:
            if (_contextAttributes == null)
            {
                throw new OpenGlException("No suitable EGL config was found");
            }
        }
Exemple #9
0
        bool EnsureInitialized()
        {
            if (_context != null)
            {
                return(true);
            }

            if (_glFailed)
            {
                return(false);
            }

            var feature = AvaloniaLocator.Current.GetService <IWindowingPlatformGlFeature>();

            if (feature == null)
            {
                return(false);
            }
            try
            {
                _context = feature.CreateContext();
            }
            catch (Exception e)
            {
                Logger.TryGet(LogEventLevel.Error)?.Log("OpenGL", "OpenGlControlBase",
                                                        "Unable to initialize OpenGL: unable to create additional OpenGL context: {exception}", e);
                _glFailed = true;
                return(false);
            }

            GlVersion = _context.Version;
            try
            {
                _bitmap = new OpenGlTextureBitmap();
            }
            catch (Exception e)
            {
                _context.Dispose();
                _context = null;
                Logger.TryGet(LogEventLevel.Error)?.Log("OpenGL", "OpenGlControlBase",
                                                        "Unable to initialize OpenGL: unable to create OpenGlTextureBitmap: {exception}", e);
                _glFailed = true;
                return(false);
            }

            using (_context.MakeCurrent())
            {
                try
                {
                    _oldSize = GetPixelSize();
                    var gl     = _context.GlInterface;
                    var oneArr = new int[1];
                    gl.GenFramebuffers(1, oneArr);
                    _fb = oneArr[0];
                    gl.BindFramebuffer(GL_FRAMEBUFFER, _fb);

                    gl.GenTextures(1, oneArr);
                    _texture = oneArr[0];

                    ResizeTexture(gl);

                    gl.FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture, 0);

                    var status = gl.CheckFramebufferStatus(GL_FRAMEBUFFER);
                    if (status != GL_FRAMEBUFFER_COMPLETE)
                    {
                        int code;
                        while ((code = gl.GetError()) != 0)
                        {
                            Logger.TryGet(LogEventLevel.Error)?.Log("OpenGL", "OpenGlControlBase",
                                                                    "Unable to initialize OpenGL FBO: {code}", code);
                        }

                        _glFailed = true;
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    Logger.TryGet(LogEventLevel.Error)?.Log("OpenGL", "OpenGlControlBase",
                                                            "Unable to initialize OpenGL FBO: {exception}", e);
                    _glFailed = true;
                }

                if (!_glFailed)
                {
                    OnOpenGlInit(_context.GlInterface, _fb);
                }
            }

            if (_glFailed)
            {
                DoCleanup(false);
            }

            return(true);
        }
        public EglDisplay(EglInterface egl, IntPtr display)
        {
            _egl     = egl;
            _display = display;
            if (_display == IntPtr.Zero)
            {
                throw new ArgumentException();
            }


            if (!_egl.Initialize(_display, out var major, out var minor))
            {
                throw OpenGlException.GetFormattedException("eglInitialize", _egl);
            }

            var glProfiles = AvaloniaLocator.Current.GetService <AngleOptions>()?.GlProfiles
                             ?? new[]
            {
                new GlVersion(GlProfileType.OpenGLES, 3, 0),
                new GlVersion(GlProfileType.OpenGLES, 2, 0)
            };

            var cfgs = glProfiles.Select(x =>
            {
                var typeBit = EGL_OPENGL_ES3_BIT;

                switch (x.Major)
                {
                case 2:
                    typeBit = EGL_OPENGL_ES2_BIT;
                    break;

                case 1:
                    typeBit = EGL_OPENGL_ES_BIT;
                    break;
                }

                return(new
                {
                    Attributes = new[]
                    {
                        EGL_CONTEXT_MAJOR_VERSION, x.Major,
                        EGL_CONTEXT_MINOR_VERSION, x.Minor,
                        EGL_NONE
                    },
                    Api = EGL_OPENGL_ES_API,
                    RenderableTypeBit = typeBit,
                    Version = x
                });
            });

            foreach (var cfg in cfgs)
            {
                if (!_egl.BindApi(cfg.Api))
                {
                    continue;
                }
                foreach (var surfaceType in new[] { EGL_PBUFFER_BIT | EGL_WINDOW_BIT, EGL_WINDOW_BIT })
                {
                    foreach (var stencilSize in new[] { 8, 1, 0 })
                    {
                        foreach (var depthSize in new [] { 8, 1, 0 })
                        {
                            var attribs = new[]
                            {
                                EGL_SURFACE_TYPE, surfaceType,
                                EGL_RENDERABLE_TYPE, cfg.RenderableTypeBit,
                                EGL_RED_SIZE, 8,
                                EGL_GREEN_SIZE, 8,
                                EGL_BLUE_SIZE, 8,
                                EGL_ALPHA_SIZE, 8,
                                EGL_STENCIL_SIZE, stencilSize,
                                EGL_DEPTH_SIZE, depthSize,
                                EGL_NONE
                            };
                            if (!_egl.ChooseConfig(_display, attribs, out _config, 1, out int numConfigs))
                            {
                                continue;
                            }
                            if (numConfigs == 0)
                            {
                                continue;
                            }
                            _contextAttributes = cfg.Attributes;
                            _surfaceType       = surfaceType;
                            _version           = cfg.Version;
                            _egl.GetConfigAttrib(_display, _config, EGL_SAMPLES, out _sampleCount);
                            _egl.GetConfigAttrib(_display, _config, EGL_STENCIL_SIZE, out _stencilSize);
                            goto Found;
                        }
                    }
                }
            }
Found:
            if (_contextAttributes == null)
            {
                throw new OpenGlException("No suitable EGL config was found");
            }
        }
Exemple #11
0
        public EglDisplay(EglInterface egl, IntPtr display)
        {
            _egl     = egl;
            _display = display;
            if (_display == IntPtr.Zero)
            {
                throw new ArgumentException();
            }


            if (!_egl.Initialize(_display, out var major, out var minor))
            {
                throw OpenGlException.GetFormattedException("eglInitialize", _egl);
            }

            foreach (var cfg in new[]
            {
                new
                {
                    Attributes = new[]
                    {
                        EGL_CONTEXT_CLIENT_VERSION, 2,
                        EGL_NONE
                    },
                    Api = EGL_OPENGL_ES_API,
                    RenderableTypeBit = EGL_OPENGL_ES2_BIT,
                    Version = new GlVersion(GlProfileType.OpenGLES, 2, 0)
                }
            })
            {
                if (!_egl.BindApi(cfg.Api))
                {
                    continue;
                }
                foreach (var surfaceType in new[] { EGL_PBUFFER_BIT | EGL_WINDOW_BIT, EGL_WINDOW_BIT })
                {
                    foreach (var stencilSize in new[] { 8, 1, 0 })
                    {
                        foreach (var depthSize in new [] { 8, 1, 0 })
                        {
                            var attribs = new[]
                            {
                                EGL_SURFACE_TYPE, surfaceType,
                                EGL_RENDERABLE_TYPE, cfg.RenderableTypeBit,
                                EGL_RED_SIZE, 8,
                                EGL_GREEN_SIZE, 8,
                                EGL_BLUE_SIZE, 8,
                                EGL_ALPHA_SIZE, 8,
                                EGL_STENCIL_SIZE, stencilSize,
                                EGL_DEPTH_SIZE, depthSize,
                                EGL_NONE
                            };
                            if (!_egl.ChooseConfig(_display, attribs, out _config, 1, out int numConfigs))
                            {
                                continue;
                            }
                            if (numConfigs == 0)
                            {
                                continue;
                            }
                            _contextAttributes = cfg.Attributes;
                            _surfaceType       = surfaceType;
                            _version           = cfg.Version;
                            _egl.GetConfigAttrib(_display, _config, EGL_SAMPLES, out _sampleCount);
                            _egl.GetConfigAttrib(_display, _config, EGL_STENCIL_SIZE, out _stencilSize);
                            goto Found;
                        }
                    }
                }
            }
Found:
            if (_contextAttributes == null)
            {
                throw new OpenGlException("No suitable EGL config was found");
            }
        }