#include "extension.h"
#include "ext_framebuffer_object.h"
#include "framebuffer.h"
+#include "misc.h"
#include "renderbuffer.h"
#include "texture2d.h"
+using namespace std;
+
namespace Msp {
namespace GL {
-Framebuffer::Framebuffer()
+Framebuffer::Framebuffer(unsigned i):
+ id(i),
+ dirty(0)
+{
+ if(id)
+ throw InvalidParameterValue("System framebuffer must have id 0");
+
+ int viewport[4];
+ glGetIntegerv(GL_VIEWPORT, viewport);
+ width = viewport[2];
+ height = viewport[3];
+}
+
+Framebuffer::Framebuffer():
+ width(0),
+ height(0),
+ dirty(0)
{
static RequireExtension _ext("GL_EXT_framebuffer_object");
glGenFramebuffersEXT(1, &id);
- bind();
}
Framebuffer::~Framebuffer()
{
- glDeleteFramebuffersEXT(1, &id);
+ if(id)
+ glDeleteFramebuffersEXT(1, &id);
+ if(current()==this)
+ unbind();
}
-void Framebuffer::bind() const
+void Framebuffer::update_attachment(unsigned mask) const
{
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
- cur_fbo=this;
+ if(current()==this)
+ {
+ bool has_color = false;
+ bool has_depth = false;
+ for(unsigned i=0; i<attachments.size(); ++i)
+ {
+ const Attachment &attch = attachments[i];
+ if(mask&(1<<i))
+ {
+ if(attch.type==GL_RENDERBUFFER_EXT)
+ glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, GL_RENDERBUFFER_EXT, attch.rbuf->get_id());
+ else if(attch.type==GL_TEXTURE_2D)
+ glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
+ else
+ glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, 0, 0);
+ }
+
+ if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
+ has_color = true;
+ if(attch.attachment==DEPTH_ATTACHMENT)
+ has_depth = true;
+ }
+
+ glDrawBuffer(has_color ? GL_FRONT : GL_NONE);
+ glDepthMask(has_depth);
+ }
+ else
+ dirty |= mask;
+}
+
+void Framebuffer::check_size()
+{
+ for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
+ if(i->type)
+ {
+ if(i->type==GL_RENDERBUFFER_EXT)
+ {
+ width = i->rbuf->get_width();
+ height = i->rbuf->get_height();
+ }
+ else if(i->type==GL_TEXTURE_2D)
+ {
+ Texture2D *tex = static_cast<Texture2D *>(i->tex);
+ width = tex->get_width();
+ height = tex->get_height();
+ }
+ if(current()==this)
+ glViewport(0, 0, width, height);
+ break;
+ }
}
void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
{
- maybe_bind();
- glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch, GL_RENDERBUFFER_EXT, rbuf.get_id());
+ if(!id)
+ throw InvalidState("Can't attach to system framebuffer");
+
+ unsigned i = get_attachment_index(attch);
+ attachments[i].set(rbuf);
+ update_attachment(1<<i);
+ check_size();
}
void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, int level)
{
- maybe_bind();
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch, tex.get_target(), tex.get_id(), level);
+ if(!id)
+ throw InvalidState("Can't attach to system framebuffer");
+
+ unsigned i = get_attachment_index(attch);
+ attachments[i].set(tex, level);
+ update_attachment(1<<i);
+ check_size();
+}
+
+void Framebuffer::detach(FramebufferAttachment attch)
+{
+ if(!id)
+ throw InvalidState("Can't detach from system framebuffer");
+
+ unsigned i = get_attachment_index(attch);
+ attachments[i].clear();
+ update_attachment(1<<i);
+ check_size();
}
FramebufferStatus Framebuffer::check_status() const
{
- maybe_bind();
+ Bind _bind(this, true);
return static_cast<FramebufferStatus>(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
}
-const Framebuffer *Framebuffer::current()
+void Framebuffer::clear(BufferBits bits)
{
- return cur_fbo;
+ Bind _bind(this, true);
+ glClear(bits);
}
-void Framebuffer::unbind()
+void Framebuffer::bind() const
{
- if(cur_fbo)
+ if(set_current(this))
{
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- cur_fbo=0;
+ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
+ if(dirty)
+ {
+ update_attachment(dirty);
+ dirty = 0;
+ }
+ if(width && height)
+ glViewport(0, 0, width, height);
}
}
-void Framebuffer::maybe_bind() const
+const Framebuffer *Framebuffer::current()
+{
+ if(!cur_obj)
+ cur_obj = &system();
+ return cur_obj;
+}
+
+void Framebuffer::unbind()
+{
+ system().bind();
+}
+
+Framebuffer &Framebuffer::system()
+{
+ static Framebuffer sys_framebuf(0);
+ return sys_framebuf;
+}
+
+unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
{
- if(cur_fbo!=this)
- bind();
+ for(unsigned i=0; i<attachments.size(); ++i)
+ if(attachments[i].attachment==attch)
+ return i;
+ attachments.push_back(Attachment(attch));
+ return attachments.size()-1;
}
-const Framebuffer *Framebuffer::cur_fbo=0;
+
+Framebuffer::Attachment::Attachment(FramebufferAttachment a):
+ attachment(a),
+ type(0),
+ level(0)
+{ }
+
+void Framebuffer::Attachment::set(Renderbuffer &r)
+{
+ type = GL_RENDERBUFFER_EXT;
+ rbuf = &r;
+ level = 0;
+}
+
+void Framebuffer::Attachment::set(Texture &t, int l)
+{
+ type = t.get_target();
+ tex = &t;
+ level = l;
+}
+
+void Framebuffer::Attachment::clear()
+{
+ type = 0;
+}
} // namespace GL
} // namespace Msp