-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include "extension.h"
-#include "ext_framebuffer_object.h"
+#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>
+#include <msp/gl/extensions/msp_buffer_control.h>
+#include "error.h"
#include "framebuffer.h"
#include "misc.h"
#include "renderbuffer.h"
#include "texture2d.h"
+#include "texture3d.h"
using namespace std;
namespace Msp {
namespace GL {
+void operator<<(LexicalConverter &conv, FramebufferStatus status)
+{
+ switch(status)
+ {
+ case FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
+ conv.result("incomplete attachment");
+ break;
+ case FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
+ conv.result("missing attachment");
+ break;
+ case FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
+ conv.result("mismatched attachment dimensions");
+ break;
+ case FRAMEBUFFER_INCOMPLETE_FORMATS:
+ conv.result("mismatched attachment formats");
+ break;
+ case FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
+ conv.result("missing draw buffer attachment");
+ break;
+ case FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
+ conv.result("missing read buffer attachment");
+ break;
+ case FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
+ conv.result("mismatched attachment sample counts");
+ break;
+ case FRAMEBUFFER_INCOMPLETE_LAYER_COUNT:
+ conv.result("mismatched attachment layer counts");
+ break;
+ case FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
+ conv.result("mismatched attachment layering");
+ break;
+ case FRAMEBUFFER_UNSUPPORTED:
+ conv.result("unsupported");
+ break;
+ default:
+ conv.result(lexical_cast<string, unsigned>(status, "%#x"));
+ break;
+ }
+}
+
+framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status):
+ runtime_error(lexical_cast<string>(status))
+{ }
+
+
+Framebuffer::Framebuffer(unsigned i):
+ id(i),
+ dirty(0)
+{
+ if(id)
+ throw invalid_argument("System framebuffer must have id 0");
+
+ glGetIntegerv(GL_VIEWPORT, &view.left);
+ width = view.width;
+ height = view.height;
+}
+
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();
+ if(ARB_direct_state_access)
+ glCreateFramebuffers(1, &id);
+ else
+ 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
{
- const Framebuffer *old = current();
- if(set_current(this))
+ if(!ARB_direct_state_access && current()!=this)
{
- if(!old)
- get(GL_VIEWPORT, sys_viewport);
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
- if(width && height)
- viewport(0, 0, width, height);
+ dirty |= mask;
+ return;
+ }
+
+ std::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(attch.type==GL_RENDERBUFFER)
+ {
+ if(ARB_direct_state_access)
+ glNamedFramebufferRenderbuffer(id, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
+ else
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
+ }
+ else if(attch.type==GL_TEXTURE_2D)
+ {
+ static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
+ if(ARB_direct_state_access)
+ glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level);
+ else
+ glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
+ }
+ else if(attch.type==GL_TEXTURE_3D || attch.type==GL_TEXTURE_2D_ARRAY)
+ {
+ static_cast<Texture3D *>(attch.tex)->allocate(attch.level);
+ if(ARB_direct_state_access)
+ glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
+ else if(attch.type==GL_TEXTURE_2D_ARRAY)
+ glFramebufferTextureLayer(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
+ else
+ glFramebufferTexture3D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level, attch.layer);
+ }
+ else if(attch.type==GL_TEXTURE_CUBE_MAP)
+ {
+ static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
+ if(ARB_direct_state_access)
+ glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
+ else
+ glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
+ }
+ else if(ARB_direct_state_access)
+ glNamedFramebufferRenderbuffer(id, attch.attachment, 0, 0);
+ else
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
+ }
+
+ if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
+ color_bufs.push_back(attch.attachment);
+ }
+
+ if(color_bufs.size()>1)
+ static Require _req(ARB_draw_buffers);
+
+ GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
+ if(ARB_direct_state_access)
+ {
+ /* ARB_direct_state_access ties the availability of these functions to
+ framebuffers themselves, so no further checks are needed. */
+ glNamedFramebufferDrawBuffers(id, color_bufs.size(), &color_bufs[0]);
+ glNamedFramebufferReadBuffer(id, first_buffer);
+ }
+ else
+ {
+ if(ARB_draw_buffers)
+ glDrawBuffers(color_bufs.size(), &color_bufs[0]);
+ else if(MSP_buffer_control)
+ glDrawBuffer(first_buffer);
+
+ if(MSP_buffer_control)
+ glReadBuffer(first_buffer);
}
}
+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)
+ {
+ 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<Texture2D *>(i->tex);
+ width = max(tex->get_width()>>i->level, 1U);
+ height = max(tex->get_height()>>i->level, 1U);
+ }
+ else if(i->type==GL_TEXTURE_3D || i->type==GL_TEXTURE_2D_ARRAY)
+ {
+ Texture3D *tex = static_cast<Texture3D *>(i->tex);
+ width = max(tex->get_width()>>i->level, 1U);
+ height = max(tex->get_height()>>i->level, 1U);
+ }
+ else if(i->type==GL_TEXTURE_CUBE_MAP)
+ {
+ width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
+ height = width;
+ }
+ if(full_viewport)
+ reset_viewport();
+ break;
+ }
+}
+
+unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
+{
+ for(unsigned i=0; i<attachments.size(); ++i)
+ if(attachments[i].attachment==attch)
+ return i;
+ attachments.push_back(Attachment(attch));
+ return attachments.size()-1;
+}
+
void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
{
- bind();
- glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch, GL_RENDERBUFFER_EXT, rbuf.get_id());
- get_or_create_attachment(attch) = rbuf;
+ if(!id)
+ throw invalid_operation("Framebuffer::attach");
+
+ 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)
+void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
{
- bind();
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch, tex.get_target(), tex.get_id(), level);
- get_or_create_attachment(attch) = tex;
+ if(!id)
+ throw invalid_operation("Framebuffer::attach");
+
+ unsigned i = get_attachment_index(attch);
+ attachments[i].set(tex, level, 0);
+ update_attachment(1<<i);
check_size();
}
-void Framebuffer::detach(FramebufferAttachment attch)
+void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
{
- bind();
- for(vector<Attachment>::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::attach");
+
+ unsigned i = get_attachment_index(attch);
+ attachments[i].set(tex, level, layer);
+ update_attachment(1<<i);
+ check_size();
}
-FramebufferStatus Framebuffer::check_status() const
+void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
{
- bind();
- return static_cast<FramebufferStatus>(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
+ if(!id)
+ throw invalid_operation("Framebuffer::attach");
+
+ unsigned i = get_attachment_index(attch);
+ attachments[i].set(tex, level, TextureCube::get_face_index(face));
+ update_attachment(1<<i);
+ check_size();
}
-void Framebuffer::unbind()
+void Framebuffer::detach(FramebufferAttachment attch)
+{
+ if(!id)
+ throw invalid_operation("Framebuffer::detach");
+
+ unsigned i = get_attachment_index(attch);
+ attachments[i].clear();
+ update_attachment(1<<i);
+ check_size();
+}
+
+FramebufferStatus Framebuffer::check_status() const
{
- if(set_current(0))
+ if(ARB_direct_state_access)
+ return static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
+ else
{
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- viewport(sys_viewport[0], sys_viewport[1], sys_viewport[2], sys_viewport[3]);
+ BindRestore _bind(this);
+ return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
}
}
-Framebuffer::Attachment &Framebuffer::get_or_create_attachment(FramebufferAttachment attch)
+void Framebuffer::require_complete() const
{
- for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
- if(i->attachment==attch)
- return *i;
- attachments.push_back(Attachment(attch));
- return attachments.back();
+ FramebufferStatus status = check_status();
+ if(status!=FRAMEBUFFER_COMPLETE)
+ throw framebuffer_incomplete(status);
}
-void Framebuffer::check_size()
+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)
{
- if(!attachments.empty())
+ 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))
{
- const Attachment &attch = attachments.front();
- if(attch.type==GL_RENDERBUFFER_EXT)
+ glBindFramebuffer(GL_DRAW_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)
+ }
+ 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)
{
- Texture2D *tex = static_cast<Texture2D *>(attch.tex);
- width = tex->get_width();
- height = tex->get_height();
+ update_attachment(dirty);
+ dirty = 0;
}
- if(current()==this)
- viewport(0, 0, width, height);
+
+ if(width && height)
+ glViewport(view.left, view.bottom, view.width, view.height);
}
}
-int Framebuffer::sys_viewport[4] = { 0, 0, 1, 1 };
+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;
+}
Framebuffer::Attachment::Attachment(FramebufferAttachment a):
attachment(a),
- type(0)
+ type(0),
+ level(0),
+ layer(0)
{ }
-Framebuffer::Attachment &Framebuffer::Attachment::operator=(Renderbuffer &r)
+void Framebuffer::Attachment::set(Renderbuffer &r)
{
- type = GL_RENDERBUFFER_EXT;
+ type = GL_RENDERBUFFER;
rbuf = &r;
- return *this;
+ level = 0;
+ layer = 0;
}
-Framebuffer::Attachment &Framebuffer::Attachment::operator=(Texture &t)
+void Framebuffer::Attachment::set(Texture &t, unsigned l, unsigned z)
{
type = t.get_target();
tex = &t;
- return *this;
+ level = l;
+ layer = z;
}
-
-void viewport(int x, int y, unsigned w, unsigned h)
+void Framebuffer::Attachment::clear()
{
- glViewport(x, y, w, h);
+ type = 0;
}
-void clear(BufferBits bits)
-{
- glClear(bits);
-}
-void draw_buffer(RWBuffer buf)
-{
- glDrawBuffer(buf);
-}
-
-void read_buffer(RWBuffer buf)
-{
- glReadBuffer(buf);
-}
+Framebuffer::Viewport::Viewport():
+ left(0),
+ bottom(0),
+ width(0),
+ height(0)
+{ }
} // namespace GL
} // namespace Msp