From: Mikko Rasa Date: Wed, 21 Apr 2021 10:09:03 +0000 (+0300) Subject: Refactor texture attachment management in Framebuffer X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ccd636b2fa8b9f270fbe600629dd109e78d00992;p=libs%2Fgl.git Refactor texture attachment management in Framebuffer 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. --- diff --git a/source/core/framebuffer.cpp b/source/core/framebuffer.cpp index 795465cb..c90d6a70 100644 --- a/source/core/framebuffer.cpp +++ b/source/core/framebuffer.cpp @@ -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(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(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(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<