]> git.tdb.fi Git - libs/gl.git/blobdiff - source/framebuffer.cpp
Explicitly define the number of mipmap levels in textures
[libs/gl.git] / source / framebuffer.cpp
index 1c2ae519bade1ef1059df7ff1a43dbdd33a23440..cf478ce29b686d842ce2074078952633b2e47b02 100644 (file)
@@ -1,12 +1,16 @@
 #include <msp/gl/extensions/arb_draw_buffers.h>
+#include <msp/gl/extensions/arb_direct_state_access.h>
 #include <msp/gl/extensions/ext_framebuffer_blit.h>
 #include <msp/gl/extensions/ext_framebuffer_object.h>
-#include <msp/gl/extensions/msp_draw_buffer.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 "error.h"
 #include "framebuffer.h"
 #include "misc.h"
 #include "renderbuffer.h"
 #include "texture2d.h"
+#include "texture3d.h"
 
 using namespace std;
 
@@ -35,6 +39,15 @@ void operator<<(LexicalConverter &conv, FramebufferStatus status)
        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;
@@ -68,7 +81,10 @@ Framebuffer::Framebuffer():
 {
        static Require _req(EXT_framebuffer_object);
 
-       glGenFramebuffers(1, &id);
+       if(ARB_direct_state_access)
+               glCreateFramebuffers(1, &id);
+       else
+               glGenFramebuffers(1, &id);
 }
 
 Framebuffer::~Framebuffer()
@@ -81,49 +97,83 @@ Framebuffer::~Framebuffer()
 
 void Framebuffer::update_attachment(unsigned mask) const
 {
-       if(current()==this)
+       if(!ARB_direct_state_access && current()!=this)
+       {
+               dirty |= mask;
+               return;
+       }
+
+       std::vector<GLenum> color_bufs;
+       color_bufs.reserve(attachments.size());
+       for(unsigned i=0; i<attachments.size(); ++i)
        {
-               std::vector<GLenum> color_bufs;
-               color_bufs.reserve(attachments.size());
-               for(unsigned i=0; i<attachments.size(); ++i)
+               const Attachment &attch = attachments[i];
+               if(mask&(1<<i))
                {
-                       const Attachment &attch = attachments[i];
-                       if(mask&(1<<i))
+                       if(attch.type==GL_RENDERBUFFER)
                        {
-                               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==GL_TEXTURE_2D)
-                               {
-                                       static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
+                       }
+                       else if(attch.type==GL_TEXTURE_2D)
+                       {
+                               static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
+                               if(ARB_direct_state_access)
+                                       glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level);
+                               else
                                        glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
-                               }
-                               else if(attch.type==GL_TEXTURE_CUBE_MAP)
-                               {
-                                       static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
-                                       glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, 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
-                                       glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
+                                       glFramebufferTexture3D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level, attch.layer);
                        }
-
-                       if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
-                               color_bufs.push_back(attch.attachment);
+                       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
+                                       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(color_bufs.size()>1)
-                       static Require _req(ARB_draw_buffers);
+               if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
+                       color_bufs.push_back(attch.attachment);
+       }
 
-               GLenum first_buffer = (color_bufs.empty() ? GL_NONE : color_bufs.front());
+       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_draw_buffer)
+               else if(MSP_buffer_control)
                        glDrawBuffer(first_buffer);
 
-               if(MSP_draw_buffer)
+               if(MSP_buffer_control)
                        glReadBuffer(first_buffer);
        }
-       else
-               dirty |= mask;
 }
 
 void Framebuffer::check_size()
@@ -140,12 +190,18 @@ void Framebuffer::check_size()
                        else if(i->type==GL_TEXTURE_2D)
                        {
                                Texture2D *tex = static_cast<Texture2D *>(i->tex);
-                               width = tex->get_width();
-                               height = tex->get_height();
+                               width = max(tex->get_width()>>i->level, 1U);
+                               height = max(tex->get_height()>>i->level, 1U);
+                       }
+                       else if(i->type==GL_TEXTURE_3D || i->type==GL_TEXTURE_2D_ARRAY)
+                       {
+                               Texture3D *tex = static_cast<Texture3D *>(i->tex);
+                               width = max(tex->get_width()>>i->level, 1U);
+                               height = max(tex->get_height()>>i->level, 1U);
                        }
                        else if(i->type==GL_TEXTURE_CUBE_MAP)
                        {
-                               width = static_cast<TextureCube *>(i->tex)->get_size();
+                               width = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
                                height = width;
                        }
                        if(full_viewport)
@@ -180,7 +236,18 @@ void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned l
                throw invalid_operation("Framebuffer::attach");
 
        unsigned i = get_attachment_index(attch);
-       attachments[i].set(tex, 0, level);
+       attachments[i].set(tex, level, 0);
+       update_attachment(1<<i);
+       check_size();
+}
+
+void Framebuffer::attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
+{
+       if(!id)
+               throw invalid_operation("Framebuffer::attach");
+
+       unsigned i = get_attachment_index(attch);
+       attachments[i].set(tex, level, layer);
        update_attachment(1<<i);
        check_size();
 }
@@ -191,7 +258,7 @@ void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureC
                throw invalid_operation("Framebuffer::attach");
 
        unsigned i = get_attachment_index(attch);
-       attachments[i].set(tex, face, level);
+       attachments[i].set(tex, level, TextureCube::get_face_index(face));
        update_attachment(1<<i);
        check_size();
 }
@@ -209,8 +276,13 @@ void Framebuffer::detach(FramebufferAttachment attch)
 
 FramebufferStatus Framebuffer::check_status() const
 {
-       BindRestore _bind(this);
-       return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
+       if(ARB_direct_state_access)
+               return static_cast<FramebufferStatus>(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER));
+       else
+       {
+               BindRestore _bind(this);
+               return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
+       }
 }
 
 void Framebuffer::require_complete() const
@@ -246,6 +318,12 @@ void Framebuffer::blit_from(const Framebuffer &other, int sx0, int sy0, int sx1,
 {
        static Require _req(EXT_framebuffer_blit);
 
+       if(ARB_direct_state_access)
+       {
+               glBlitNamedFramebuffer(other.id, id, sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
+               return;
+       }
+
        const Framebuffer *old = current();
        if(set_current(this))
        {
@@ -313,7 +391,8 @@ Framebuffer &Framebuffer::system()
 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
        attachment(a),
        type(0),
-       level(0)
+       level(0),
+       layer(0)
 { }
 
 void Framebuffer::Attachment::set(Renderbuffer &r)
@@ -321,14 +400,15 @@ void Framebuffer::Attachment::set(Renderbuffer &r)
        type = GL_RENDERBUFFER;
        rbuf = &r;
        level = 0;
+       layer = 0;
 }
 
-void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l)
+void Framebuffer::Attachment::set(Texture &t, unsigned l, unsigned z)
 {
        type = t.get_target();
        tex = &t;
-       cube_face = f;
        level = l;
+       layer = z;
 }
 
 void Framebuffer::Attachment::clear()