]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/framebuffer.cpp
Check supported attachment formats in Framebuffer::set_format
[libs/gl.git] / source / core / framebuffer.cpp
index a60b437eff6fbe9e4c9a1eec8e2b6012b35395af..1dd2f61f9bab10139772b8619075370df41b2849 100644 (file)
@@ -1,10 +1,13 @@
 #include <msp/gl/extensions/arb_draw_buffers.h>
 #include <msp/gl/extensions/arb_direct_state_access.h>
+#include <msp/gl/extensions/arb_internalformat_query.h>
+#include <msp/gl/extensions/arb_internalformat_query2.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 <msp/strings/format.h>
 #include "error.h"
 #include "framebuffer.h"
 #include "misc.h"
@@ -18,54 +21,14 @@ 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_incomplete::framebuffer_incomplete(const std::string &reason):
+       runtime_error(reason)
 { }
 
 
 Framebuffer::Framebuffer(unsigned i):
        id(i),
-       status(FRAMEBUFFER_COMPLETE),
+       status(GL_FRAMEBUFFER_COMPLETE),
        dirty(0)
 {
        if(id)
@@ -100,7 +63,7 @@ void Framebuffer::init()
 
        width = 0;
        height = 0;
-       status = FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
+       status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
        dirty = 0;
 
        if(ARB_direct_state_access)
@@ -122,6 +85,19 @@ void Framebuffer::set_format(const FrameFormat &fmt)
        if(fmt.empty())
                throw invalid_argument("Framebuffer::set_format");
 
+       if(ARB_internalformat_query && ARB_internalformat_query2)
+       {
+               unsigned target = (fmt.get_samples()>1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D);
+               for(FrameAttachment a: fmt)
+               {
+                       unsigned pf = get_gl_pixelformat(get_attachment_pixelformat(a));
+                       int supported = 0;
+                       glGetInternalformativ(target, pf, GL_FRAMEBUFFER_RENDERABLE, 1, &supported);
+                       if(supported!=GL_FULL_SUPPORT)
+                               throw invalid_argument("Framebuffer::set_format");
+               }
+       }
+
        format = fmt;
        attachments.resize(format.size());
 }
@@ -139,24 +115,23 @@ void Framebuffer::update() const
                        const Attachment &attch = attachments[i];
                        if(attch.tex)
                        {
-                               GLenum type = attch.tex->get_target();
                                if(ARB_direct_state_access)
                                {
-                                       if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0)
-                                               glNamedFramebufferTexture(id, gl_attach_point, attch.tex->get_id(), attch.level);
+                                       if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0)
+                                               glNamedFramebufferTexture(id, gl_attach_point, attch.tex->id, attch.level);
                                        else
-                                               glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->get_id(), attch.level, attch.layer);
+                                               glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->id, attch.level, attch.layer);
                                }
-                               else if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE)
-                                       glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, type, attch.tex->get_id(), attch.level);
+                               else if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
+                                       glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level);
                                else if(attch.layer<0)
-                                       glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->get_id(), attch.level);
-                               else if(type==GL_TEXTURE_2D_ARRAY)
-                                       glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->get_id(), attch.level, attch.layer);
-                               else if(type==GL_TEXTURE_3D)
-                                       glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, type, attch.tex->get_id(), attch.level, attch.layer);
-                               else if(type==GL_TEXTURE_CUBE_MAP)
-                                       glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast<TextureCubeFace>(attch.layer)), attch.tex->get_id(), attch.level);
+                                       glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level);
+                               else if(attch.tex->target==GL_TEXTURE_2D_ARRAY)
+                                       glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level, attch.layer);
+                               else if(attch.tex->target==GL_TEXTURE_3D)
+                                       glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level, attch.layer);
+                               else if(attch.tex->target==GL_TEXTURE_CUBE_MAP)
+                                       glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast<TextureCubeFace>(attch.layer)), attch.tex->id, attch.level);
                        }
                        else if(ARB_direct_state_access)
                                glNamedFramebufferTexture(id, gl_attach_point, 0, 0);
@@ -193,9 +168,9 @@ void Framebuffer::update() const
        }
 
        if(ARB_direct_state_access)
-               status = static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
+               status = glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER);
        else
-               status = static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
+               status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
 
        dirty = 0;
 }
@@ -206,28 +181,27 @@ void Framebuffer::check_size()
        for(Attachment &a: attachments)
                if(a.tex)
                {
-                       GLenum type = a.tex->get_target();
                        unsigned w = 0;
                        unsigned h = 0;
-                       if(type==GL_TEXTURE_2D)
+                       if(a.tex->target==GL_TEXTURE_2D)
                        {
                                Texture2D *tex = static_cast<Texture2D *>(a.tex);
                                w = max(tex->get_width()>>a.level, 1U);
                                h = max(tex->get_height()>>a.level, 1U);
                        }
-                       else if(type==GL_TEXTURE_2D_MULTISAMPLE)
+                       else if(a.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
                        {
                                Texture2DMultisample *tex = static_cast<Texture2DMultisample *>(a.tex);
                                w = tex->get_width();
                                h = tex->get_height();
                        }
-                       else if(type==GL_TEXTURE_3D || type==GL_TEXTURE_2D_ARRAY)
+                       else if(a.tex->target==GL_TEXTURE_3D || a.tex->target==GL_TEXTURE_2D_ARRAY)
                        {
                                Texture3D *tex = static_cast<Texture3D *>(a.tex);
                                w = max(tex->get_width()>>a.level, 1U);
                                h = max(tex->get_height()>>a.level, 1U);
                        }
-                       else if(type==GL_TEXTURE_CUBE_MAP)
+                       else if(a.tex->target==GL_TEXTURE_CUBE_MAP)
                        {
                                w = max(static_cast<TextureCube *>(a.tex)->get_size()>>a.level, 1U);
                                h = w;
@@ -328,8 +302,24 @@ void Framebuffer::resize(const WindowView &view)
 
 void Framebuffer::require_complete() const
 {
-       if(status!=FRAMEBUFFER_COMPLETE)
-               throw framebuffer_incomplete(status);
+       if(!id)
+               return;
+
+       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");
+       }
+
+       if(status==GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
+               throw framebuffer_incomplete("incomplete or unsupported attachment");
+       if(status==GL_FRAMEBUFFER_UNSUPPORTED)
+               throw framebuffer_incomplete("unsupported configuration");
+       if(status!=GL_FRAMEBUFFER_COMPLETE)
+               throw framebuffer_incomplete(Msp::format("incomplete (%#x)", status));
 }
 
 void Framebuffer::set_debug_name(const string &name)