]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/framebuffer.cpp
Convert framebuffers and related functionality to new state management
[libs/gl.git] / source / core / framebuffer.cpp
index 23b39f2d691c8de06aeaf378be10c25fe39db601..4dbb30929e05dc38ff822c981b51e251613f61bb 100644 (file)
@@ -1,6 +1,5 @@
 #include <msp/gl/extensions/arb_draw_buffers.h>
 #include <msp/gl/extensions/arb_direct_state_access.h>
-#include <msp/gl/extensions/ext_framebuffer_blit.h>
 #include <msp/gl/extensions/ext_framebuffer_object.h>
 #include <msp/gl/extensions/ext_texture_array.h>
 #include <msp/gl/extensions/ext_texture3d.h>
@@ -12,6 +11,7 @@
 #include "renderbuffer.h"
 #include "texture2d.h"
 #include "texture3d.h"
+#include "windowview.h"
 
 using namespace std;
 
@@ -65,19 +65,22 @@ framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status):
 
 Framebuffer::Framebuffer(unsigned i):
        id(i),
+       status(FRAMEBUFFER_COMPLETE),
        dirty(0)
 {
        if(id)
                throw invalid_argument("System framebuffer must have id 0");
 
-       glGetIntegerv(GL_VIEWPORT, &view.left);
-       width = view.width;
-       height = view.height;
+       int view[4];
+       glGetIntegerv(GL_VIEWPORT, view);
+       width = view[2];
+       height = view[3];
 }
 
 Framebuffer::Framebuffer():
        width(0),
        height(0),
+       status(FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT),
        dirty(0)
 {
        static Require _req(EXT_framebuffer_object);
@@ -92,24 +95,16 @@ Framebuffer::~Framebuffer()
 {
        if(id)
                glDeleteFramebuffers(1, &id);
-       if(current()==this)
-               unbind();
 }
 
-void Framebuffer::update_attachment(unsigned mask) const
+void Framebuffer::update() const
 {
-       if(!ARB_direct_state_access && current()!=this)
-       {
-               dirty |= mask;
-               return;
-       }
-
        vector<GLenum> color_bufs;
        color_bufs.reserve(attachments.size());
        for(unsigned i=0; i<attachments.size(); ++i)
        {
                const Attachment &attch = attachments[i];
-               if(mask&(1<<i))
+               if(dirty&(1<<i))
                {
                        if(attch.type==GL_RENDERBUFFER)
                        {
@@ -169,11 +164,17 @@ void Framebuffer::update_attachment(unsigned mask) const
                if(MSP_buffer_control)
                        glReadBuffer(first_buffer);
        }
+
+       if(ARB_direct_state_access)
+               status = static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
+       else
+               status = static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
+
+       dirty = 0;
 }
 
 void Framebuffer::check_size()
 {
-       bool full_viewport = (view.left==0 && view.bottom==0 && view.width==width && view.height==height);
        for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
                if(i->type)
                {
@@ -199,8 +200,6 @@ void Framebuffer::check_size()
                                width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
                                height = width;
                        }
-                       if(full_viewport)
-                               reset_viewport();
                        break;
                }
 }
@@ -221,7 +220,7 @@ void Framebuffer::set_texture_attachment(FramebufferAttachment attch, Texture &t
 
        unsigned i = get_attachment_index(attch);
        attachments[i].set(tex, level, layer);
-       update_attachment(1<<i);
+       dirty |= 1<<i;
        check_size();
 }
 
@@ -232,7 +231,7 @@ void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
 
        unsigned i = get_attachment_index(attch);
        attachments[i].set(rbuf);
-       update_attachment(1<<i);
+       dirty |= 1<<i;
        check_size();
 }
 
@@ -275,125 +274,25 @@ void Framebuffer::detach(FramebufferAttachment attch)
 
        unsigned i = get_attachment_index(attch);
        attachments[i].clear();
-       update_attachment(1<<i);
+       dirty |= 1<<i;
        check_size();
 }
 
-FramebufferStatus Framebuffer::check_status() const
+void Framebuffer::resize(const WindowView &view)
 {
-       if(ARB_direct_state_access)
-               return static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
-       else
-       {
-               BindRestore _bind(this);
-               return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
-       }
+       if(id)
+               throw invalid_operation("Framebuffer::resize");
+
+       width = view.get_width();
+       height = view.get_height();
 }
 
 void Framebuffer::require_complete() const
 {
-       FramebufferStatus status = check_status();
        if(status!=FRAMEBUFFER_COMPLETE)
                throw framebuffer_incomplete(status);
 }
 
-void Framebuffer::viewport(int l, int b, unsigned w, unsigned h)
-{
-       view.left = l;
-       view.bottom = b;
-       view.width = w;
-       view.height = h;
-
-       if(current()==this)
-               glViewport(view.left, view.bottom, view.width, view.height);
-}
-
-void Framebuffer::reset_viewport()
-{
-       viewport(0, 0, width, height);
-}
-
-void Framebuffer::clear()
-{
-       clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT|STENCIL_BUFFER_BIT);
-}
-
-void Framebuffer::clear(BufferBits bits)
-{
-       BindRestore _bind(this);
-       glClear(bits);
-}
-
-void Framebuffer::blit_from(const Framebuffer &other, int sx0, int sy0, int sx1, int sy1, int dx0, int dy0, int dx1, int dy1, BufferBits bits, bool filter)
-{
-       static Require _req(EXT_framebuffer_blit);
-
-       if(ARB_direct_state_access)
-       {
-               glBlitNamedFramebuffer(other.id, id, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
-               return;
-       }
-
-       const Framebuffer *old = current();
-       if(set_current(this))
-       {
-               glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
-               if(dirty)
-               {
-                       update_attachment(dirty);
-                       dirty = 0;
-               }
-       }
-       if(old!=&other)
-               glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
-
-       glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
-
-       set_current(old);
-       glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
-}
-
-void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
-{
-       blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
-}
-
-void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
-{
-       blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
-}
-
-void Framebuffer::bind() const
-{
-       if(id && attachments.empty())
-               throw invalid_operation("Framebuffer::bind");
-
-       if(set_current(this))
-       {
-               glBindFramebuffer(GL_FRAMEBUFFER, id);
-               if(dirty)
-               {
-                       update_attachment(dirty);
-                       dirty = 0;
-               }
-
-               if(width && height)
-                       glViewport(view.left, view.bottom, view.width, view.height);
-       }
-}
-
-const Framebuffer *Framebuffer::current()
-{
-       if(!cur_obj)
-               cur_obj = &system();
-       return cur_obj;
-}
-
-void Framebuffer::unbind()
-{
-       system().bind();
-}
-
 void Framebuffer::set_debug_name(const string &name)
 {
 #ifdef DEBUG
@@ -439,13 +338,5 @@ void Framebuffer::Attachment::clear()
        type = 0;
 }
 
-
-Framebuffer::Viewport::Viewport():
-       left(0),
-       bottom(0),
-       width(0),
-       height(0)
-{ }
-
 } // namespace GL
 } // namespace Msp