public void Dispose()
 {
     if (_disposed)
     {
         return;
     }
     _disposed = true;
     _gl.DeleteFramebuffers(1, _framebuffer);
     _gl.DeleteRenderbuffers(1, _depthBuffer);
     _gl.DeleteRenderbuffers(1, _renderbuffer);
     if (_context != EAGLContext.CurrentContext)
     {
         throw new InvalidOperationException("Associated EAGLContext is not current");
     }
 }
        void EnsureDepthBufferAttachment(GlInterface gl)
        {
            var size = GetPixelSize();

            if (size == _depthBufferSize && _depthBuffer != 0)
            {
                return;
            }

            gl.GetIntegerv(GL_RENDERBUFFER_BINDING, out var oldRenderBuffer);
            if (_depthBuffer != 0)
            {
                gl.DeleteRenderbuffers(1, new[] { _depthBuffer });
            }

            var oneArr = new int[1];

            gl.GenRenderbuffers(1, oneArr);
            _depthBuffer = oneArr[0];
            gl.BindRenderbuffer(GL_RENDERBUFFER, _depthBuffer);
            gl.RenderbufferStorage(GL_RENDERBUFFER,
                                   GlVersion.Type == GlProfileType.OpenGLES ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT,
                                   size.Width, size.Height);
            gl.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthBuffer);
            gl.BindRenderbuffer(GL_RENDERBUFFER, oldRenderBuffer);
        }
        public static LayerFbo TryCreate(EAGLContext context, GlInterface gl, CAEAGLLayer layer)
        {
            if (context != EAGLContext.CurrentContext)
            {
                return(null);
            }

            var fb = new int[2];
            var rb = new int[2];
            var db = new int[2];

            gl.GenRenderbuffers(1, rb);
            gl.BindRenderbuffer(GlConsts.GL_RENDERBUFFER, rb[0]);
            context.RenderBufferStorage(GlConsts.GL_RENDERBUFFER, layer);

            gl.GenFramebuffers(1, fb);
            gl.BindFramebuffer(GlConsts.GL_FRAMEBUFFER, fb[0]);
            gl.FramebufferRenderbuffer(GlConsts.GL_FRAMEBUFFER, GlConsts.GL_COLOR_ATTACHMENT0, GlConsts.GL_RENDERBUFFER, rb[0]);

            int[] w = new int[1];
            int[] h = new int[1];
            gl.GetRenderbufferParameteriv(GlConsts.GL_RENDERBUFFER, GlConsts.GL_RENDERBUFFER_WIDTH, w);
            gl.GetRenderbufferParameteriv(GlConsts.GL_RENDERBUFFER, GlConsts.GL_RENDERBUFFER_HEIGHT, h);

            gl.GenRenderbuffers(1, db);

            //GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, depthBuffer);
            //GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferInternalFormat.DepthComponent16, w, h);
            gl.FramebufferRenderbuffer(GlConsts.GL_FRAMEBUFFER, GlConsts.GL_DEPTH_ATTACHMENT, GlConsts.GL_RENDERBUFFER, db[0]);

            var frameBufferError = gl.CheckFramebufferStatus(GlConsts.GL_FRAMEBUFFER);

            if (frameBufferError != GlConsts.GL_FRAMEBUFFER_COMPLETE)
            {
                gl.DeleteFramebuffers(1, fb);
                gl.DeleteRenderbuffers(1, db);
                gl.DeleteRenderbuffers(1, rb);
                return(null);
            }

            return(new LayerFbo(context, gl, layer, fb, rb, db)
            {
                Width = w[0],
                Height = h[0]
            });
        }