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; } } } }
void EnsureTextureAttachment() { _context.GlInterface.BindFramebuffer(GL_FRAMEBUFFER, _fb); if (_bitmap == null || _attachment == null || _bitmap.PixelSize != GetPixelSize()) { _attachment?.Dispose(); _attachment = null; _bitmap?.Dispose(); _bitmap = null; _bitmap = new OpenGlBitmap(GetPixelSize(), new Vector(96, 96)); _attachment = _bitmap.CreateFramebufferAttachment(_context); } }
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); } } }