Example #1
0
        private void CreateFramebuffer()
        {
            AssertNotDisposed();
            AssertValidContext();

            _graphicsContext.MakeCurrent(null);

            int previousRenderbuffer = 0;

            _glapi.GetInteger(All.RenderbufferBinding, ref previousRenderbuffer);

            _glapi.GenRenderbuffers(1, ref _renderbuffer);
            _glapi.BindRenderbuffer(All.Renderbuffer, _renderbuffer);

            var ctx = ((IGraphicsContextInternal)_graphicsContext).Implementation as iPhoneOSGraphicsContext;

            // TODO: EAGLContext.RenderBufferStorage returns false
            //       on all but the first call.  Nevertheless, it
            //       works.  Still, it would be nice to know why it
            //       claims to have failed.
            ctx.EAGLContext.RenderBufferStorage((uint)All.Renderbuffer, Layer);

            _glapi.GenFramebuffers(1, ref _framebuffer);
            _glapi.BindFramebuffer(All.Framebuffer, _framebuffer);
            _glapi.FramebufferRenderbuffer(
                All.Framebuffer, All.ColorAttachment0, All.Renderbuffer, _renderbuffer);

            // HACK:  GraphicsDevice itself should be calling
            //        glViewport, so we shouldn't need to do it
            //        here and then force the state into
            //        GraphicsDevice.  However, that change is a
            //        ways off, yet.
            int unscaledViewportWidth  = (int)Math.Round(Layer.Bounds.Size.Width);
            int unscaledViewportHeight = (int)Math.Round(Layer.Bounds.Size.Height);

            _glapi.Viewport(0, 0, unscaledViewportWidth, unscaledViewportHeight);
            _glapi.Scissor(0, 0, unscaledViewportWidth, unscaledViewportHeight);

            var gds = (IGraphicsDeviceService)_platform.Game.Services.GetService(
                typeof(IGraphicsDeviceService));

            if (gds != null && gds.GraphicsDevice != null)
            {
                gds.GraphicsDevice.Viewport = new Viewport(
                    0, 0,
                    (int)(Layer.Bounds.Width * Layer.ContentsScale),
                    (int)(Layer.Bounds.Height * Layer.ContentsScale));
            }

            var status = _glapi.CheckFramebufferStatus(All.Framebuffer);

            if (status != All.FramebufferComplete)
            {
                throw new InvalidOperationException(
                          "Framebuffer was not created correctly: " + status);
            }

            // FIXME: These static methods on GraphicsDevice need
            //        to go away someday.
            GraphicsDevice.FrameBufferScreen = _framebuffer;
        }