#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 <msp/gl/extensions/khr_debug.h>
#include "error.h"
#include "framebuffer.h"
#include "misc.h"
#include "renderbuffer.h"
#include "texture2d.h"
#include "texture3d.h"
+#include "windowview.h"
using namespace std;
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);
{
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;
- }
-
- std::vector<GLenum> color_bufs;
+ 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)
{
else
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
}
- else if(attch.type==GL_TEXTURE_2D)
+ else if(attch.type)
{
- static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
if(ARB_direct_state_access)
- glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level);
- else
+ {
+ if(attch.type==GL_TEXTURE_2D || attch.layer<0)
+ glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level);
+ else
+ glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
+ }
+ else if(attch.type==GL_TEXTURE_2D)
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.layer<0)
+ glFramebufferTexture(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level);
else if(attch.type==GL_TEXTURE_2D_ARRAY)
glFramebufferTextureLayer(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level, attch.layer);
- else
+ else if(attch.type==GL_TEXTURE_3D)
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
+ else if(attch.type==GL_TEXTURE_CUBE_MAP)
glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level);
}
else if(ARB_direct_state_access)
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)
{
width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
height = width;
}
- if(full_viewport)
- reset_viewport();
break;
}
}
return attachments.size()-1;
}
-void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
+void Framebuffer::set_texture_attachment(FramebufferAttachment attch, Texture &tex, unsigned level, int layer)
{
if(!id)
throw invalid_operation("Framebuffer::attach");
unsigned i = get_attachment_index(attch);
- attachments[i].set(rbuf);
- update_attachment(1<<i);
+ attachments[i].set(tex, level, layer);
+ dirty |= 1<<i;
check_size();
}
-void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
+void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
{
if(!id)
throw invalid_operation("Framebuffer::attach");
unsigned i = get_attachment_index(attch);
- attachments[i].set(tex, level, 0);
- update_attachment(1<<i);
+ attachments[i].set(rbuf);
+ dirty |= 1<<i;
check_size();
}
-void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
+void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
{
- if(!id)
- throw invalid_operation("Framebuffer::attach");
+ tex.allocate(level);
+ set_texture_attachment(attch, tex, level, 0);
+}
- unsigned i = get_attachment_index(attch);
- attachments[i].set(tex, level, layer);
- update_attachment(1<<i);
- check_size();
+void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
+{
+ tex.allocate(level);
+ set_texture_attachment(attch, tex, level, layer);
}
void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
{
- if(!id)
- throw invalid_operation("Framebuffer::attach");
+ tex.allocate(level);
+ set_texture_attachment(attch, tex, level, TextureCube::get_face_index(face));
+}
- unsigned i = get_attachment_index(attch);
- attachments[i].set(tex, level, TextureCube::get_face_index(face));
- update_attachment(1<<i);
- check_size();
+void Framebuffer::attach_layered(FramebufferAttachment attch, Texture3D &tex, unsigned level)
+{
+ static Require _req(ARB_geometry_shader4);
+ tex.allocate(level);
+ set_texture_attachment(attch, tex, level, -1);
+}
+
+void Framebuffer::attach_layered(FramebufferAttachment attch, TextureCube &tex, unsigned level)
+{
+ static Require _req(ARB_geometry_shader4);
+ tex.allocate(level);
+ set_texture_attachment(attch, tex, level, -1);
}
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()
+void Framebuffer::set_debug_name(const string &name)
{
- system().bind();
+#ifdef DEBUG
+ if(KHR_debug)
+ glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
+#else
+ (void)name;
+#endif
}
Framebuffer &Framebuffer::system()
layer = 0;
}
-void Framebuffer::Attachment::set(Texture &t, unsigned l, unsigned z)
+void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
{
type = t.get_target();
tex = &t;
type = 0;
}
-
-Framebuffer::Viewport::Viewport():
- left(0),
- bottom(0),
- width(0),
- height(0)
-{ }
-
} // namespace GL
} // namespace Msp