X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fframebuffer.cpp;h=43b0f94b82e4c882327ef686491453a1bc7cf263;hb=6afbace895a7bbcf216ab8e48280ea0303ab5892;hp=229c7f0a094c01ce64bfc43563a62882b6606164;hpb=98c810b6d2256aa65986bbde12c38917678121bb;p=libs%2Fgl.git diff --git a/source/framebuffer.cpp b/source/framebuffer.cpp index 229c7f0a..43b0f94b 100644 --- a/source/framebuffer.cpp +++ b/source/framebuffer.cpp @@ -1,11 +1,5 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include "extension.h" +#include "error.h" +#include "ext_framebuffer_blit.h" #include "ext_framebuffer_object.h" #include "framebuffer.h" #include "misc.h" @@ -17,162 +11,256 @@ using namespace std; namespace Msp { namespace GL { +Framebuffer::Framebuffer(unsigned i): + id(i), + dirty(0) +{ + if(id) + throw invalid_argument("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) + height(0), + dirty(0) { - static RequireExtension _ext("GL_EXT_framebuffer_object"); + static Require _req(EXT_framebuffer_object); - glGenFramebuffersEXT(1, &id); - bind(); + glGenFramebuffers(1, &id); } Framebuffer::~Framebuffer() { - glDeleteFramebuffersEXT(1, &id); + if(id) + glDeleteFramebuffers(1, &id); + if(current()==this) + unbind(); } -void Framebuffer::bind() const +void Framebuffer::update_attachment(unsigned mask) const { - if(!cur_fbo) - get(GL_VIEWPORT, sys_viewport); - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id); - cur_fbo=this; - if(width && height) - viewport(0, 0, width, height); + if(current()==this) + { + GLenum color_buf = GL_NONE; + for(unsigned i=0; iget_id()); + else if(attch.type==GL_TEXTURE_2D) + { + static_cast(attch.tex)->allocate(attch.level); + glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level); + } + else if(attch.type==GL_TEXTURE_CUBE_MAP) + { + static_cast(attch.tex)->allocate(attch.level); + glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level); + } + else + glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0); + } + + if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3) + color_buf = attch.attachment; + } + + glDrawBuffer(color_buf); + } + else + dirty |= mask; +} + +void Framebuffer::check_size() +{ + for(vector::iterator i=attachments.begin(); i!=attachments.end(); ++i) + if(i->type) + { + if(i->type==GL_RENDERBUFFER) + { + width = i->rbuf->get_width(); + height = i->rbuf->get_height(); + } + else if(i->type==GL_TEXTURE_2D) + { + Texture2D *tex = static_cast(i->tex); + width = tex->get_width(); + height = tex->get_height(); + } + else if(i->type==GL_TEXTURE_CUBE_MAP) + { + width = static_cast(i->tex)->get_size(); + height = width; + } + if(current()==this) + glViewport(0, 0, width, height); + break; + } +} + +unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch) +{ + for(unsigned i=0; i::iterator i=attachments.begin(); i!=attachments.end(); ++i) - if(i->attachment==attch) - { - if(i->type==GL_RENDERBUFFER_EXT) - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch, GL_RENDERBUFFER_EXT, 0); - else if(i->type==GL_TEXTURE_2D) - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch, GL_TEXTURE_2D, 0, 0); - attachments.erase(i); - check_size(); - return; - } + if(!id) + throw invalid_operation("Framebuffer::detach"); + + unsigned i = get_attachment_index(attch); + attachments[i].clear(); + update_attachment(1<(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); + Bind _bind(this, true); + return static_cast(glCheckFramebufferStatus(GL_FRAMEBUFFER)); } -const Framebuffer *Framebuffer::current() +void Framebuffer::clear(BufferBits bits) { - return cur_fbo; + Bind _bind(this, true); + glClear(bits); } -void Framebuffer::unbind() +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) { - if(cur_fbo) + static Require _req(EXT_framebuffer_blit); + + const Framebuffer *old = current(); + if(set_current(this)) { - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); - cur_fbo=0; - viewport(sys_viewport[0], sys_viewport[1], sys_viewport[2], sys_viewport[3]); + 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::maybe_bind() const +void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits) { - if(cur_fbo!=this) - bind(); + blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false); } -Framebuffer::Attachment &Framebuffer::get_or_create_attachment(FramebufferAttachment attch) +void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter) { - for(vector::iterator i=attachments.begin(); i!=attachments.end(); ++i) - if(i->attachment==attch) - return *i; - attachments.push_back(Attachment(attch)); - return attachments.back(); + blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter); } -void Framebuffer::check_size() +void Framebuffer::bind() const { - if(!attachments.empty()) + if(set_current(this)) { - const Attachment &attch=attachments.front(); - if(attch.type==GL_RENDERBUFFER_EXT) + glBindFramebuffer(GL_FRAMEBUFFER, id); + if(dirty) { - width=attch.rbuf->get_width(); - height=attch.rbuf->get_height(); + update_attachment(dirty); + dirty = 0; } - else if(attch.type==GL_TEXTURE_2D) - { - Texture2D *tex=static_cast(attch.tex); - width=tex->get_width(); - height=tex->get_height(); - } - if(cur_fbo==this) - viewport(0, 0, width, height); + if(width && height) + glViewport(0, 0, width, height); } } -const Framebuffer *Framebuffer::cur_fbo=0; -int Framebuffer::sys_viewport[4]={ 0, 0, 1, 1 }; - - -Framebuffer::Attachment::Attachment(FramebufferAttachment a): - attachment(a), - type(0) -{ } +const Framebuffer *Framebuffer::current() +{ + if(!cur_obj) + cur_obj = &system(); + return cur_obj; +} -Framebuffer::Attachment &Framebuffer::Attachment::operator=(Renderbuffer &r) +void Framebuffer::unbind() { - type=GL_RENDERBUFFER_EXT; - rbuf=&r; - return *this; + system().bind(); } -Framebuffer::Attachment &Framebuffer::Attachment::operator=(Texture &t) +Framebuffer &Framebuffer::system() { - type=t.get_target(); - tex=&t; - return *this; + static Framebuffer sys_framebuf(0); + return sys_framebuf; } -void viewport(int x, int y, unsigned w, unsigned h) -{ - glViewport(x, y, w, h); -} +Framebuffer::Attachment::Attachment(FramebufferAttachment a): + attachment(a), + type(0), + level(0) +{ } -void clear(BufferBits bits) +void Framebuffer::Attachment::set(Renderbuffer &r) { - glClear(bits); + type = GL_RENDERBUFFER; + rbuf = &r; + level = 0; } -void draw_buffer(RWBuffer buf) +void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l) { - glDrawBuffer(buf); + type = t.get_target(); + tex = &t; + cube_face = f; + level = l; } -void read_buffer(RWBuffer buf) +void Framebuffer::Attachment::clear() { - glReadBuffer(buf); + type = 0; } } // namespace GL