/// <summary> /// Unbinds the renderbuffer. /// <para>The renderbuffer will remain internally attached to the framebuffer.</para> /// </summary> internal void Unbind() { if (info == null) { return; } // Return the renderbuffer to the cache renderBufferCache[Format].Push(info); info = null; }
/// <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; }