void DoCleanup()
        {
            if (_context != null)
            {
                using (_context.MakeCurrent())
                {
                    var gl = _context.GlInterface;
                    gl.BindTexture(GL_TEXTURE_2D, 0);
                    gl.BindFramebuffer(GL_FRAMEBUFFER, 0);
                    gl.DeleteFramebuffers(1, new[] { _fb });
                    _fb = 0;
                    gl.DeleteRenderbuffers(1, new[] { _depthBuffer });
                    _depthBuffer = 0;
                    _attachment?.Dispose();
                    _attachment = null;
                    _bitmap?.Dispose();
                    _bitmap = null;

                    try
                    {
                        if (_initialized)
                        {
                            _initialized = false;
                            OnOpenGlDeinit(_context.GlInterface, _fb);
                        }
                    }
                    finally
                    {
                        _context.Dispose();
                        _context = null;
                    }
                }
            }
        }
Esempio n. 2
0
        void DoCleanup(bool callUserDeinit)
        {
            if (_context != null)
            {
                using (_context.MakeCurrent())
                {
                    var gl = _context.GlInterface;
                    gl.BindTexture(GL_TEXTURE_2D, 0);
                    gl.BindFramebuffer(GL_FRAMEBUFFER, 0);
                    gl.DeleteFramebuffers(1, new[] { _fb });
                    using (_bitmap.Lock())
                        _bitmap.SetTexture(0, 0, new PixelSize(1, 1), 1);
                    gl.DeleteTextures(1, new[] { _texture });
                    gl.DeleteRenderbuffers(1, new[] { _renderBuffer });
                    _bitmap.Dispose();

                    try
                    {
                        if (callUserDeinit)
                        {
                            OnOpenGlDeinit(_context.GlInterface, _fb);
                        }
                    }
                    finally
                    {
                        _context.Dispose();
                        _context = null;
                    }
                }
            }
        }
        private bool EnsureInitializedCore()
        {
            if (_context != null)
            {
                return(true);
            }

            if (_glFailed)
            {
                return(false);
            }

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

            if (feature == null)
            {
                return(false);
            }
            if (!feature.CanShareContexts)
            {
                Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
                                                                  "Unable to initialize OpenGL: current platform does not support multithreaded context sharing");
                return(false);
            }
            try
            {
                _context = feature.CreateSharedContext();
            }
            catch (Exception e)
            {
                Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
                                                                  "Unable to initialize OpenGL: unable to create additional OpenGL context: {exception}", e);
                return(false);
            }

            GlVersion = _context.Version;
            try
            {
                _bitmap = new OpenGlBitmap(GetPixelSize(), new Vector(96, 96));
                if (!_bitmap.SupportsContext(_context))
                {
                    Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
                                                                      "Unable to initialize OpenGL: unable to create OpenGlBitmap: OpenGL context is not compatible");
                    return(false);
                }
            }
            catch (Exception e)
            {
                _context.Dispose();
                _context = null;
                Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
                                                                  "Unable to initialize OpenGL: unable to create OpenGlBitmap: {exception}", e);
                return(false);
            }

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

                    EnsureDepthBufferAttachment(gl);
                    EnsureTextureAttachment();

                    return(CheckFramebufferStatus(gl));
                }
                catch (Exception e)
                {
                    Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
                                                                      "Unable to initialize OpenGL FBO: {exception}", e);
                    return(false);
                }
            }
        }
Esempio n. 4
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);
        }