]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/framebuffer.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / framebuffer.cpp
index 4dbb30929e05dc38ff822c981b51e251613f61bb..4f30dbb158b346a71d726fcd9f431da171193b9a 100644 (file)
@@ -1,15 +1,7 @@
-#include <msp/gl/extensions/arb_draw_buffers.h>
-#include <msp/gl/extensions/arb_direct_state_access.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 "texture2dmultisample.h"
 #include "texture3d.h"
 #include "windowview.h"
 
@@ -18,324 +10,229 @@ 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(const std::string &reason):
+       runtime_error(reason)
+{ }
+
 
-framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status):
-       runtime_error(lexical_cast<string>(status))
+Framebuffer::Framebuffer(bool s):
+       FramebufferBackend(s)
 { }
 
+Framebuffer::Framebuffer():
+       FramebufferBackend(false)
+{ }
 
-Framebuffer::Framebuffer(unsigned i):
-       id(i),
-       status(FRAMEBUFFER_COMPLETE),
-       dirty(0)
+Framebuffer::Framebuffer(FrameAttachment fa):
+       Framebuffer()
 {
-       if(id)
-               throw invalid_argument("System framebuffer must have id 0");
-
-       int view[4];
-       glGetIntegerv(GL_VIEWPORT, view);
-       width = view[2];
-       height = view[3];
+       set_format(fa);
 }
 
-Framebuffer::Framebuffer():
-       width(0),
-       height(0),
-       status(FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT),
-       dirty(0)
+Framebuffer::Framebuffer(const FrameFormat &f):
+       Framebuffer()
 {
-       static Require _req(EXT_framebuffer_object);
-
-       if(ARB_direct_state_access)
-               glCreateFramebuffers(1, &id);
-       else
-               glGenFramebuffers(1, &id);
+       set_format(f);
 }
 
-Framebuffer::~Framebuffer()
+void Framebuffer::set_format(const FrameFormat &fmt)
 {
-       if(id)
-               glDeleteFramebuffers(1, &id);
+       if(!format.empty())
+               throw invalid_operation("Framebuffer::set_format");
+       if(fmt.empty() || !is_format_supported(fmt))
+               throw invalid_argument("Framebuffer::set_format");
+
+       format = fmt;
+       attachments.resize(format.size());
+       format_changed(format);
 }
 
 void Framebuffer::update() const
 {
-       vector<GLenum> color_bufs;
-       color_bufs.reserve(attachments.size());
-       for(unsigned i=0; i<attachments.size(); ++i)
-       {
-               const Attachment &attch = attachments[i];
-               if(dirty&(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)
-                       {
-                               if(ARB_direct_state_access)
-                               {
-                                       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.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 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)
-                                       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);
-       }
-
-       if(ARB_direct_state_access)
-               status = static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
-       else
-               status = static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
-
+       FramebufferBackend::update(dirty);
        dirty = 0;
 }
 
 void Framebuffer::check_size()
 {
-       for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
-               if(i->type)
+       bool first = true;
+       for(Attachment &a: attachments)
+               if(a.tex)
                {
-                       if(i->type==GL_RENDERBUFFER)
+                       unsigned w = 0;
+                       unsigned h = 0;
+                       unsigned l = 1;
+                       if(const Texture2D *tex2d = dynamic_cast<const Texture2D *>(a.tex))
                        {
-                               width = i->rbuf->get_width();
-                               height = i->rbuf->get_height();
+                               w = max(tex2d->get_width()>>a.level, 1U);
+                               h = max(tex2d->get_height()>>a.level, 1U);
                        }
-                       else if(i->type==GL_TEXTURE_2D)
+                       else if(const Texture2DMultisample *tex2d_ms = dynamic_cast<const Texture2DMultisample *>(a.tex))
                        {
-                               Texture2D *tex = static_cast<Texture2D *>(i->tex);
-                               width = max(tex->get_width()>>i->level, 1U);
-                               height = max(tex->get_height()>>i->level, 1U);
+                               w = tex2d_ms->get_width();
+                               h = tex2d_ms->get_height();
                        }
-                       else if(i->type==GL_TEXTURE_3D || i->type==GL_TEXTURE_2D_ARRAY)
+                       else if(const Texture3D *tex3d = dynamic_cast<const Texture3D *>(a.tex))
                        {
-                               Texture3D *tex = static_cast<Texture3D *>(i->tex);
-                               width = max(tex->get_width()>>i->level, 1U);
-                               height = max(tex->get_height()>>i->level, 1U);
+                               w = max(tex3d->get_width()>>a.level, 1U);
+                               h = max(tex3d->get_height()>>a.level, 1U);
+                               l = (a.layer<0 ? tex3d->get_depth() : 1);
                        }
-                       else if(i->type==GL_TEXTURE_CUBE_MAP)
+                       else if(const TextureCube *tex_cube = dynamic_cast<const TextureCube *>(a.tex))
                        {
-                               width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
-                               height = width;
+                               w = max(tex_cube->get_size()>>a.level, 1U);
+                               h = w;
+                               l = (a.layer<0 ? 6 : 1);
                        }
-                       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;
+                       if(first)
+                       {
+                               width = w;
+                               height = h;
+                               layers = l;
+                               first = false;
+                       }
+                       else
+                       {
+                               width = min(width, w);
+                               height = min(height, h);
+                               layers = min(layers, l);
+                       }
+               }
 }
 
-void Framebuffer::set_texture_attachment(FramebufferAttachment attch, Texture &tex, unsigned level, int layer)
+void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, Texture *res, unsigned level, int layer, unsigned samples)
 {
-       if(!id)
+       if(format.empty() || attachments.empty())
                throw invalid_operation("Framebuffer::attach");
 
-       unsigned i = get_attachment_index(attch);
-       attachments[i].set(tex, level, layer);
-       dirty |= 1<<i;
-       check_size();
+       if((format.get_samples()>1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples))
+               throw incompatible_data("Framebuffer::attach");
+
+       unsigned i = 0;
+       for(FrameAttachment a: format)
+       {
+               if(a==attch)
+               {
+                       attachments[i].set(tex, res, level, layer);
+                       dirty |= 1<<i;
+                       check_size();
+                       return;
+               }
+               ++i;
+       }
+
+       throw incompatible_data("Framebuffer::attach");
 }
 
-void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
+void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
 {
-       if(!id)
-               throw invalid_operation("Framebuffer::attach");
-
-       unsigned i = get_attachment_index(attch);
-       attachments[i].set(rbuf);
-       dirty |= 1<<i;
-       check_size();
+       set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, level, 0, 0);
 }
 
-void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
+void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex, Texture2D *res)
 {
-       tex.allocate(level);
-       set_texture_attachment(attch, tex, level, 0);
+       set_attachment(make_typed_attachment(attch, tex.get_format()), tex, res, 0, 0, tex.get_samples());
 }
 
-void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
+void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
 {
-       tex.allocate(level);
-       set_texture_attachment(attch, tex, level, layer);
+       set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, level, layer, 0);
 }
 
-void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
+void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
 {
-       tex.allocate(level);
-       set_texture_attachment(attch, tex, level, TextureCube::get_face_index(face));
+       set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, level, face, 0);
 }
 
-void Framebuffer::attach_layered(FramebufferAttachment attch, Texture3D &tex, unsigned level)
+void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
 {
-       static Require _req(ARB_geometry_shader4);
-       tex.allocate(level);
-       set_texture_attachment(attch, tex, level, -1);
+       require_layered();
+       set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, level, -1, 0);
 }
 
-void Framebuffer::attach_layered(FramebufferAttachment attch, TextureCube &tex, unsigned level)
+void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
 {
-       static Require _req(ARB_geometry_shader4);
-       tex.allocate(level);
-       set_texture_attachment(attch, tex, level, -1);
+       require_layered();
+       set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, level, -1, 0);
 }
 
-void Framebuffer::detach(FramebufferAttachment attch)
+void Framebuffer::detach(FrameAttachment attch)
 {
-       if(!id)
+       if(attachments.empty())
                throw invalid_operation("Framebuffer::detach");
 
-       unsigned i = get_attachment_index(attch);
-       attachments[i].clear();
-       dirty |= 1<<i;
-       check_size();
+       int i = format.index(attch);
+       if(i>=0)
+       {
+               attachments[i].clear();
+               dirty |= 1<<i;
+               check_size();
+       }
 }
 
-void Framebuffer::resize(const WindowView &view)
+const Texture *Framebuffer::get_attachment(FrameAttachment attch) const
 {
-       if(id)
-               throw invalid_operation("Framebuffer::resize");
+       if(attachments.empty())
+               return 0;
 
-       width = view.get_width();
-       height = view.get_height();
+       int i = format.index(attch);
+       return (i>=0 ? attachments[i].tex : 0);
 }
 
-void Framebuffer::require_complete() const
+const Texture *Framebuffer::get_attachment(unsigned i) const
 {
-       if(status!=FRAMEBUFFER_COMPLETE)
-               throw framebuffer_incomplete(status);
+       return (i<attachments.size() ? attachments[i].tex : 0);
 }
 
-void Framebuffer::set_debug_name(const string &name)
+const Texture *Framebuffer::get_resolve_attachment(FrameAttachment attch) const
 {
-#ifdef DEBUG
-       if(KHR_debug)
-               glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
-#else
-       (void)name;
-#endif
+       if(attachments.empty())
+               return 0;
+
+       int i = format.index(attch);
+       return (i>=0 ? attachments[i].resolve : 0);
 }
 
-Framebuffer &Framebuffer::system()
+const Texture *Framebuffer::get_resolve_attachment(unsigned i) const
 {
-       static Framebuffer sys_framebuf(0);
-       return sys_framebuf;
+       return (i<attachments.size() ? attachments[i].resolve : 0);
 }
 
+bool Framebuffer::has_resolve_attachments() const
+{
+       for(const Attachment &a: attachments)
+               if(a.resolve)
+                       return true;
+       return false;
+}
 
-Framebuffer::Attachment::Attachment(FramebufferAttachment a):
-       attachment(a),
-       type(0),
-       level(0),
-       layer(0)
-{ }
-
-void Framebuffer::Attachment::set(Renderbuffer &r)
+void Framebuffer::require_complete() const
 {
-       type = GL_RENDERBUFFER;
-       rbuf = &r;
-       level = 0;
-       layer = 0;
+       bool layered = (!attachments.empty() && attachments.front().layer<0);
+       for(const Attachment &a: attachments)
+       {
+               if(!a.tex)
+                       throw framebuffer_incomplete("missing attachment");
+               if(layered!=(a.layer<0))
+                       throw framebuffer_incomplete("inconsistent layering");
+       }
+
+       FramebufferBackend::require_complete();
 }
 
-void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
+
+void Framebuffer::Attachment::set(Texture &t, Texture *r, unsigned l, int z)
 {
-       type = t.get_target();
        tex = &t;
+       resolve = r;
        level = l;
        layer = z;
 }
 
 void Framebuffer::Attachment::clear()
 {
-       type = 0;
+       tex = 0;
 }
 
 } // namespace GL