]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/framebuffer.cpp
Add debug name capability to more classes
[libs/gl.git] / source / core / framebuffer.cpp
index 795465cbbad37f28c9e2f208e22a080e055310f4..23b39f2d691c8de06aeaf378be10c25fe39db601 100644 (file)
@@ -5,6 +5,7 @@
 #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"
@@ -117,30 +118,24 @@ void Framebuffer::update_attachment(unsigned mask) const
                                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)
@@ -219,48 +214,58 @@ unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
        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);
+       attachments[i].set(tex, level, layer);
        update_attachment(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);
+       attachments[i].set(rbuf);
        update_attachment(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)
@@ -389,6 +394,16 @@ void Framebuffer::unbind()
        system().bind();
 }
 
+void Framebuffer::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       if(KHR_debug)
+               glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
+#else
+       (void)name;
+#endif
+}
+
 Framebuffer &Framebuffer::system()
 {
        static Framebuffer sys_framebuf(0);
@@ -411,7 +426,7 @@ void Framebuffer::Attachment::set(Renderbuffer &r)
        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;