]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/framebuffer.cpp
Move all OpenGL-specific code to a separate directory
[libs/gl.git] / source / core / framebuffer.cpp
index 53234cc74696478a630acc11b7fcdcfbfe4e11c9..167b403aec18e142548c9f02f775e453e71e6cb0 100644 (file)
@@ -1,14 +1,3 @@
-#include <msp/gl/extensions/arb_draw_buffers.h>
-#include <msp/gl/extensions/arb_direct_state_access.h>
-#include <msp/gl/extensions/arb_geometry_shader4.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 "texture2d.h"
@@ -26,50 +15,32 @@ framebuffer_incomplete::framebuffer_incomplete(const std::string &reason):
 { }
 
 
-Framebuffer::Framebuffer(unsigned i):
-       id(i),
-       status(GL_FRAMEBUFFER_COMPLETE),
+Framebuffer::Framebuffer(bool s):
+       FramebufferBackend(s),
        dirty(0)
 {
-       if(id)
-               throw invalid_argument("System framebuffer must have id 0");
-
-       if(EXT_framebuffer_object)
+       if(s)
        {
-               int value;
-               glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_BACK, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
-               if(value==GL_NONE)
-                       glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_FRONT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
-               if(value!=GL_NONE)
-                       format = (format,COLOR_ATTACHMENT);
-
-               glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
-               if(value!=GL_NONE)
-                       format = (format,DEPTH_ATTACHMENT);
-
-               glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_STENCIL, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value);
-               if(value!=GL_NONE)
-                       format = (format,STENCIL_ATTACHMENT);
+               format = get_system_format();
+               get_system_size(width, height);
        }
-
-       int view[4];
-       glGetIntegerv(GL_VIEWPORT, view);
-       width = view[2];
-       height = view[3];
 }
 
-Framebuffer::Framebuffer()
+Framebuffer::Framebuffer():
+       FramebufferBackend(false)
 {
        init();
 }
 
-Framebuffer::Framebuffer(FrameAttachment fa)
+Framebuffer::Framebuffer(FrameAttachment fa):
+       FramebufferBackend(false)
 {
        init();
        set_format(fa);
 }
 
-Framebuffer::Framebuffer(const FrameFormat &f)
+Framebuffer::Framebuffer(const FrameFormat &f):
+       FramebufferBackend(false)
 {
        init();
        set_format(f);
@@ -77,119 +48,25 @@ Framebuffer::Framebuffer(const FrameFormat &f)
 
 void Framebuffer::init()
 {
-       static Require _req(EXT_framebuffer_object);
-
        width = 0;
        height = 0;
-       status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
        dirty = 0;
-
-       if(ARB_direct_state_access)
-               glCreateFramebuffers(1, &id);
-       else
-               glGenFramebuffers(1, &id);
-}
-
-Framebuffer::~Framebuffer()
-{
-       if(id)
-               glDeleteFramebuffers(1, &id);
 }
 
 void Framebuffer::set_format(const FrameFormat &fmt)
 {
        if(!format.empty() || !id)
                throw invalid_operation("Framebuffer::set_format");
-       if(fmt.empty())
+       if(fmt.empty() || !is_format_supported(fmt))
                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());
 }
 
 void Framebuffer::update() const
 {
-       vector<GLenum> color_bufs;
-       color_bufs.reserve(format.size());
-       unsigned i = 0;
-       for(FrameAttachment a: format)
-       {
-               GLenum gl_attach_point = get_gl_attachment(a);
-               if(dirty&(1<<i))
-               {
-                       const Attachment &attch = attachments[i];
-                       if(attch.tex)
-                       {
-                               if(ARB_direct_state_access)
-                               {
-                                       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->id, attch.level, attch.layer);
-                               }
-                               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->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);
-                       else
-                               glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0);
-               }
-
-               if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
-                       color_bufs.push_back(gl_attach_point);
-
-               ++i;
-       }
-
-       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_buffer_control)
-                       glDrawBuffer(first_buffer);
-
-               if(MSP_buffer_control)
-                       glReadBuffer(first_buffer);
-       }
-
-       if(ARB_direct_state_access)
-               status = glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER);
-       else
-               status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
-
+       FramebufferBackend::update(dirty);
        dirty = 0;
 }
 
@@ -201,27 +78,24 @@ void Framebuffer::check_size()
                {
                        unsigned w = 0;
                        unsigned h = 0;
-                       if(a.tex->target==GL_TEXTURE_2D)
+                       if(const Texture2D *tex2d = dynamic_cast<const Texture2D *>(a.tex))
                        {
-                               Texture2D *tex = static_cast<Texture2D *>(a.tex);
-                               w = max(tex->get_width()>>a.level, 1U);
-                               h = max(tex->get_height()>>a.level, 1U);
+                               w = max(tex2d->get_width()>>a.level, 1U);
+                               h = max(tex2d->get_height()>>a.level, 1U);
                        }
-                       else if(a.tex->target==GL_TEXTURE_2D_MULTISAMPLE)
+                       else if(const Texture2DMultisample *tex2d_ms = dynamic_cast<const Texture2DMultisample *>(a.tex))
                        {
-                               Texture2DMultisample *tex = static_cast<Texture2DMultisample *>(a.tex);
-                               w = tex->get_width();
-                               h = tex->get_height();
+                               w = tex2d_ms->get_width();
+                               h = tex2d_ms->get_height();
                        }
-                       else if(a.tex->target==GL_TEXTURE_3D || a.tex->target==GL_TEXTURE_2D_ARRAY)
+                       else if(const Texture3D *tex3d = dynamic_cast<const Texture3D *>(a.tex))
                        {
-                               Texture3D *tex = static_cast<Texture3D *>(a.tex);
-                               w = max(tex->get_width()>>a.level, 1U);
-                               h = max(tex->get_height()>>a.level, 1U);
+                               w = max(tex3d->get_width()>>a.level, 1U);
+                               h = max(tex3d->get_height()>>a.level, 1U);
                        }
-                       else if(a.tex->target==GL_TEXTURE_CUBE_MAP)
+                       else if(const TextureCube *tex_cube = dynamic_cast<const TextureCube *>(a.tex))
                        {
-                               w = max(static_cast<TextureCube *>(a.tex)->get_size()>>a.level, 1U);
+                               w = max(tex_cube->get_size()>>a.level, 1U);
                                h = w;
                        }
 
@@ -285,13 +159,13 @@ void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFac
 
 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
 {
-       static Require _req(ARB_geometry_shader4);
+       require_layered();
        set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
 }
 
 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
 {
-       static Require _req(ARB_geometry_shader4);
+       require_layered();
        set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
 }
 
@@ -332,27 +206,12 @@ void Framebuffer::require_complete() const
                        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)
-{
-#ifdef DEBUG
-       if(KHR_debug)
-               glObjectLabel(GL_FRAMEBUFFER, id, name.size(), name.c_str());
-#else
-       (void)name;
-#endif
+       FramebufferBackend::require_complete();
 }
 
 Framebuffer &Framebuffer::system()
 {
-       static Framebuffer sys_framebuf(0);
+       static Framebuffer sys_framebuf(true);
        return sys_framebuf;
 }