/// <summary>
        /// Binds the renderbuffer to the specfied framebuffer.
        /// </summary>
        /// <param name="frameBuffer">The framebuffer this renderbuffer should be bound to.</param>
        internal void Bind(int frameBuffer)
        {
            if (info.ID != -1)
            {
                return;
            }

            if (!renderBufferCache.ContainsKey(Format))
            {
                renderBufferCache[Format] = new ConcurrentStack <RenderBufferInfo>();
            }

            // Make sure we have renderbuffers available
            if (renderBufferCache[Format].Count == 0)
            {
                int newBuffer = GL.GenRenderbuffer();
                GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, newBuffer);
                GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, Format, Game.Window.Width, Game.Window.Height);

                renderBufferCache[Format].Push(new RenderBufferInfo()
                {
                    ID = newBuffer, LastFramebuffer = -1
                });
            }

            // Get a renderbuffer from the cache
            renderBufferCache[Format].TryPop(out info);

            // For performance reasons, we only need to re-bind the renderbuffer to
            // the framebuffer if it is not already attached to it
            if (info.LastFramebuffer != frameBuffer)
            {
                // Make sure the framebuffer we want to attach to is bound
                int lastFrameBuffer = GLWrapper.BindFrameBuffer(frameBuffer);

                switch (Format)
                {
                case RenderbufferInternalFormat.DepthComponent16:
                    GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.DepthAttachment, RenderbufferTarget.Renderbuffer, info.ID);
                    break;

                case RenderbufferInternalFormat.Rgb565:
                case RenderbufferInternalFormat.Rgb5A1:
                case RenderbufferInternalFormat.Rgba4:
                    GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.ColorAttachment0, RenderbufferTarget.Renderbuffer, info.ID);
                    break;

                case RenderbufferInternalFormat.StencilIndex8:
                    GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.DepthAttachment, RenderbufferTarget.Renderbuffer, info.ID);
                    break;
                }

                GLWrapper.BindFrameBuffer(lastFrameBuffer);
            }

            info.LastFramebuffer = frameBuffer;
        }
示例#2
0
        /// <summary>
        /// Unbinds the framebuffer.
        /// </summary>
        public void Unbind()
        {
            if (!IsBound)
            {
                return;
            }

            GLWrapper.BindFrameBuffer(lastFramebuffer);
            attachedRenderBuffers.ForEach(r => r.Unbind());

            lastFramebuffer = -1;
        }
示例#3
0
        /// <summary>
        /// Unbinds the framebuffer.
        /// </summary>
        public void Unbind()
        {
            if (!IsBound)
            {
                return;
            }

            GLWrapper.BindFrameBuffer(lastFramebuffer);
            foreach (var r in attachedRenderBuffers)
            {
                r.Unbind();
            }

            lastFramebuffer = -1;
        }
示例#4
0
        /// <summary>
        /// Binds the framebuffer.
        /// <para>Does not clear the buffer or reset the viewport/ortho.</para>
        /// </summary>
        internal void Bind()
        {
            if (frameBuffer == -1)
            {
                return;
            }

            if (lastFramebuffer == frameBuffer)
            {
                return;
            }

            // Bind framebuffer and all its renderbuffers
            lastFramebuffer = GLWrapper.BindFrameBuffer(frameBuffer);
            attachedRenderBuffers.ForEach(r => r.Bind(frameBuffer));
        }
示例#5
0
        private void initializeLevel(int level, int width, int height)
        {
            GL.TexImage2D(TextureTarget2d.Texture2D, level, TextureComponentCount.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);

            if (clearFBO < 0)
            {
                clearFBO = GL.GenFramebuffer();
            }

            int lastFramebuffer = GLWrapper.BindFrameBuffer(clearFBO);

            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferSlot.ColorAttachment0, TextureTarget.Texture2D, TextureId, 0);

            GL.ClearColor(new Color4(255, 255, 255, 0));
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.ClearColor(new Color4(0, 0, 0, 0));

            GLWrapper.BindFrameBuffer(lastFramebuffer);
        }
示例#6
0
        /// <summary>
        /// Binds the framebuffer.
        /// <para>Does not clear the buffer or reset the viewport/ortho.</para>
        /// </summary>
        public void Bind()
        {
            if (frameBuffer == -1)
            {
                return;
            }

            if (lastFramebuffer == frameBuffer)
            {
                return;
            }

            // Bind framebuffer and all its renderbuffers
            lastFramebuffer = GLWrapper.BindFrameBuffer(frameBuffer);
            foreach (var r in attachedRenderBuffers)
            {
                r.Size = Size;
                r.Bind(frameBuffer);
            }
        }
示例#7
0
        /// <summary>
        /// Binds the framebuffer.
        /// <para>Does not clear the buffer or reset the viewport/ortho.</para>
        /// </summary>
        public void Bind()
        {
            if (frameBuffer == -1)
            {
                return;
            }

            if (lastFramebuffer == frameBuffer)
            {
                return;
            }

            // Bind framebuffer and all its renderbuffers
            lastFramebuffer = GLWrapper.BindFrameBuffer(frameBuffer);
            attachedRenderBuffers.ForEach(r =>
            {
                r.Size = Size;
                r.Bind(frameBuffer);
            });
        }
示例#8
0
        private void getImageGL(Action <Image <Rgba32> > callback)
        {
            host.DrawThread.Scheduler.Add(() =>
            {
                var image = new Image <Rgba32>((int)DrawWidth, (int)DrawHeight);

                var sharedData = (BufferedDrawNodeSharedData)typeof(BufferedContainer <Drawable>).GetField("sharedData", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this);

                var fbo = new FrameBuffer();
                fbo.Bind();
                GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget2d.Texture2D, sharedData.MainBuffer.Texture.TextureId, 0);

                bool success = image.TryGetSinglePixelSpan(out var span);
                Debug.Assert(success);
                GL.ReadPixels(0, 0, (int)DrawWidth, (int)DrawHeight, PixelFormat.Rgba, PixelType.UnsignedByte, ref MemoryMarshal.GetReference(span));
                fbo.Dispose();
                GLWrapper.BindFrameBuffer(GLWrapper.DefaultFrameBuffer);

                callback(image);
            });
        }
示例#9
0
        /// <summary>
        /// Binds the renderbuffer to the specfied framebuffer.
        /// </summary>
        /// <param name="frameBuffer">The framebuffer this renderbuffer should be bound to.</param>
        internal void Bind(int frameBuffer)
        {
            // Check if we're already bound
            if (info != null)
            {
                return;
            }

            if (!renderBufferCache.ContainsKey(Format))
            {
                renderBufferCache[Format] = new Stack <RenderBufferInfo>();
            }

            // Make sure we have renderbuffers available
            if (renderBufferCache[Format].Count == 0)
            {
                renderBufferCache[Format].Push(new RenderBufferInfo()
                {
                    RenderBufferID = GL.GenRenderbuffer(), FrameBufferID = -1
                });
            }

            // Get a renderbuffer from the cache
            info = renderBufferCache[Format].Pop();

            // Check if we need to update the size
            if (info.Size != Size)
            {
                GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, info.RenderBufferID);
                GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, Format, (int)Math.Ceiling(Size.X), (int)Math.Ceiling(Size.Y));

                info.Size = Size;
            }

            // For performance reasons, we only need to re-bind the renderbuffer to
            // the framebuffer if it is not already attached to it
            if (info.FrameBufferID != frameBuffer)
            {
                // Make sure the framebuffer we want to attach to is bound
                int lastFrameBuffer = GLWrapper.BindFrameBuffer(frameBuffer);

                switch (Format)
                {
                case RenderbufferInternalFormat.DepthComponent16:
                    GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.DepthAttachment, RenderbufferTarget.Renderbuffer, info.RenderBufferID);
                    break;

                case RenderbufferInternalFormat.Rgb565:
                case RenderbufferInternalFormat.Rgb5A1:
                case RenderbufferInternalFormat.Rgba4:
                    GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.ColorAttachment0, RenderbufferTarget.Renderbuffer, info.RenderBufferID);
                    break;

                case RenderbufferInternalFormat.StencilIndex8:
                    GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.DepthAttachment, RenderbufferTarget.Renderbuffer, info.RenderBufferID);
                    break;
                }

                GLWrapper.BindFrameBuffer(lastFrameBuffer);
            }

            info.FrameBufferID = frameBuffer;
        }