]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor texture attachment management in Framebuffer
authorMikko Rasa <tdb@tdb.fi>
Wed, 21 Apr 2021 10:09:03 +0000 (13:09 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 21 Apr 2021 10:09:03 +0000 (13:09 +0300)
Duplicated code from the attach functions has been collapsed into a
helper function.

Attaching a texture does not need binding it, so there's little point
in deferring the allocation to update_attachment.  By calling the type-
specific allocate function in attach, update_attachment can be simpler.

source/core/framebuffer.cpp
source/core/framebuffer.h

index 795465cbbad37f28c9e2f208e22a080e055310f4..c90d6a70670b4560155d6a84f86d613a9565b193 100644 (file)
@@ -117,30 +117,22 @@ 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)
+                                               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.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 +211,44 @@ 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");
-
-       unsigned i = get_attachment_index(attch);
-       attachments[i].set(tex, level, TextureCube::get_face_index(face));
-       update_attachment(1<<i);
-       check_size();
+       tex.allocate(level);
+       set_texture_attachment(attch, tex, level, TextureCube::get_face_index(face));
 }
 
 void Framebuffer::detach(FramebufferAttachment attch)
index 3054bd646b3e4d4709c4ee7ec8a96a5b20bdfa7a..3086685e1ce29234e8f1e8629bb7ffd103f7050f 100644 (file)
@@ -119,6 +119,7 @@ private:
        void update_attachment(unsigned) const;
        void check_size();
        unsigned get_attachment_index(FramebufferAttachment);
+       void set_texture_attachment(FramebufferAttachment, Texture &, unsigned, int);
 public:
        void attach(FramebufferAttachment attch, Renderbuffer &rbuf);
        void attach(FramebufferAttachment attch, Texture2D &tex, unsigned level = 0);