X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcore%2Fframebuffer.cpp;h=93a726868c040d6464382ead82c148d58ed20c08;hp=795465cbbad37f28c9e2f208e22a080e055310f4;hb=cd5f37b066352119cf92d53d0001af7ff99be437;hpb=3a6eb030fb4eca4c2a317f270704fddf31613130 diff --git a/source/core/framebuffer.cpp b/source/core/framebuffer.cpp index 795465cb..93a72686 100644 --- a/source/core/framebuffer.cpp +++ b/source/core/framebuffer.cpp @@ -1,16 +1,17 @@ #include #include -#include #include #include #include #include +#include #include "error.h" #include "framebuffer.h" #include "misc.h" -#include "renderbuffer.h" #include "texture2d.h" +#include "texture2dmultisample.h" #include "texture3d.h" +#include "windowview.h" using namespace std; @@ -64,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); @@ -91,62 +95,42 @@ 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 color_bufs; color_bufs.reserve(attachments.size()); for(unsigned i=0; iget_target(); 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(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(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) + { + if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE || 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(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE) + glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, type, attch.tex->get_id(), attch.level); + else if(attch.layer<0) + glFramebufferTexture(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level); + else if(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(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(type==GL_TEXTURE_3D) + glFramebufferTexture3D(GL_FRAMEBUFFER, attch.attachment, type, attch.tex->get_id(), attch.level, attch.layer); + else if(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) - glNamedFramebufferRenderbuffer(id, attch.attachment, 0, 0); + glNamedFramebufferTexture(id, attch.attachment, 0, 0); else - glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, GL_TEXTURE_2D, 0, 0); } if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3) @@ -174,38 +158,44 @@ void Framebuffer::update_attachment(unsigned mask) const if(MSP_buffer_control) glReadBuffer(first_buffer); } + + if(ARB_direct_state_access) + status = static_cast(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER)); + else + status = static_cast(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::iterator i=attachments.begin(); i!=attachments.end(); ++i) - if(i->type) + if(i->tex) { - if(i->type==GL_RENDERBUFFER) - { - width = i->rbuf->get_width(); - height = i->rbuf->get_height(); - } - else if(i->type==GL_TEXTURE_2D) + GLenum type = i->tex->get_target(); + if(type==GL_TEXTURE_2D) { Texture2D *tex = static_cast(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) + else if(type==GL_TEXTURE_2D_MULTISAMPLE) + { + Texture2DMultisample *tex = static_cast(i->tex); + width = tex->get_width(); + height = tex->get_height(); + } + else if(type==GL_TEXTURE_3D || type==GL_TEXTURE_2D_ARRAY) { Texture3D *tex = static_cast(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) + else if(type==GL_TEXTURE_CUBE_MAP) { width = max(static_cast(i->tex)->get_size()>>i->level, 1U); height = width; } - if(full_viewport) - reset_viewport(); break; } } @@ -219,48 +209,52 @@ unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch) return attachments.size()-1; } -void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf) +void Framebuffer::set_attachment(FrameAttachment 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<(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER)); - else - { - BindRestore _bind(this); - return static_cast(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) +void Framebuffer::set_debug_name(const string &name) { - 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(); +#ifdef DEBUG + if(KHR_debug) + glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str()); +#else + (void)name; +#endif } Framebuffer &Framebuffer::system() @@ -398,22 +302,13 @@ Framebuffer &Framebuffer::system() Framebuffer::Attachment::Attachment(FramebufferAttachment a): attachment(a), - type(0), + tex(0), level(0), layer(0) { } -void Framebuffer::Attachment::set(Renderbuffer &r) -{ - type = GL_RENDERBUFFER; - rbuf = &r; - level = 0; - 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; level = l; layer = z; @@ -421,16 +316,8 @@ void Framebuffer::Attachment::set(Texture &t, unsigned l, unsigned z) void Framebuffer::Attachment::clear() { - type = 0; + tex = 0; } - -Framebuffer::Viewport::Viewport(): - left(0), - bottom(0), - width(0), - height(0) -{ } - } // namespace GL } // namespace Msp