]> git.cworth.org Git - apitrace/commitdiff
Create contexts with DEBUG_BIT when not benchmarking.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Sun, 9 Oct 2011 15:16:18 +0000 (16:16 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Sun, 9 Oct 2011 15:16:18 +0000 (16:16 +0100)
CMakeLists.txt
glretrace_main.cpp
glws.cpp [new file with mode: 0644]
glws.hpp
glws_glx.cpp

index 8987c7dd01d9c68b7e78d6611511505d5524132a..c81e44d8f0131e15766a27bd2821fee5d648a437 100755 (executable)
@@ -197,10 +197,10 @@ add_custom_command (
 
 if (WIN32)
     set (os os_win32.cpp)
-    set (glws glws_wgl.cpp)
+    set (glws_os glws_wgl.cpp)
 else (WIN32)
     set (os os_posix.cpp)
-    set (glws glws_glx.cpp)
+    set (glws_os glws_glx.cpp)
 endif (WIN32)
 
 add_library (common STATIC
@@ -413,7 +413,8 @@ add_executable (glretrace
     glstate.cpp
     glstate_params.cpp
     retrace.cpp
-    ${glws}
+    glws.cpp
+    ${glws_os}
     ${CMAKE_CURRENT_BINARY_DIR}/glproc.hpp
 )
 
index dc022762fc35d439eb358cbff18eed2b77cdc2cc..4e4b8aee8da4cbf942dc4ac8ca1dd3ddb6805d2d 100644 (file)
@@ -266,6 +266,7 @@ int main(int argc, char **argv)
         } else if (!strcmp(arg, "-b")) {
             benchmark = true;
             retrace::verbosity = -1;
+            glws::debug = false;
         } else if (!strcmp(arg, "-c")) {
             compare_prefix = argv[++i];
             if (snapshot_frequency == FREQUENCY_NEVER) {
diff --git a/glws.cpp b/glws.cpp
new file mode 100644 (file)
index 0000000..c5c41eb
--- /dev/null
+++ b/glws.cpp
@@ -0,0 +1,62 @@
+/**************************************************************************
+ *
+ * Copyright 2011 Jose Fonseca
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#include "glws.hpp"
+
+
+namespace glws {
+
+
+bool debug = true;
+
+
+bool
+checkExtension(const char *extName, const char *extString)
+{
+   const char *p = extString;
+   const char *q = extName;
+   char c;
+   do {
+       c = *p++;
+       if (c == '\0' || c == ' ') {
+           if (q && *q == '\0') {
+               return true;
+           } else {
+               q = extName;
+           }
+       } else {
+           if (q && *q == c) {
+               ++q;
+           } else {
+               q = 0;
+           }
+       }
+   } while (c);
+   return false;
+}
+
+
+} /* namespace glws */
index 12e53f3e8f93e5fd5f5c0f5de39eaa579ad48782..47fdc7f40729f24e4a7905a2a914b3877ffbe8e3 100644 (file)
--- a/glws.hpp
+++ b/glws.hpp
 #define _GLWS_HPP_
 
 
+#include <vector>
+
+
 namespace glws {
 
 
+extern bool debug;
+
+
+bool
+checkExtension(const char *extName, const char *extString);
+
+
+template< class T >
+class Attributes {
+protected:
+    std::vector<T> attribs;
+
+public:
+    void add(T param) {
+        attribs.push_back(param);
+    }
+
+    void add(T pname, T pvalue) {
+        add(pname);
+        add(pvalue);
+    }
+
+    void end(void) {
+        add(0);
+    }
+
+    operator T * (void) {
+        return &attribs[0];
+    }
+
+    operator const T * (void) const {
+        return &attribs[0];
+    }
+};
+
+
 class Visual
 {
 public:
@@ -105,7 +144,7 @@ Drawable *
 createDrawable(const Visual *visual, int width = 32, int height = 32);
 
 Context *
-createContext(const Visual *visual, Context *shareContext = NULL);
+createContext(const Visual *visual, Context *shareContext = 0);
 
 bool
 makeCurrent(Drawable *drawable, Context *context);
index 1c73564d0ccb7ccab6ce586ab9e722535b2e5998..23b4082d0a577414522a67f6c62d0d1ee224ec60 100644 (file)
  *
  **************************************************************************/
 
+#include <assert.h>
 #include <stdlib.h>
+
 #include <iostream>
 
 #include "glws.hpp"
-
-#include <X11/Xlib.h>
-#include <GL/gl.h>
-#include <GL/glx.h>
+#include "glproc.hpp"
 
 
 namespace glws {
@@ -39,18 +38,25 @@ namespace glws {
 static Display *display = NULL;
 static int screen = 0;
 
+static unsigned glxVersion = 0;
+static const char *extensions = 0;
+static bool has_GLX_ARB_create_context = false;
+
 
 class GlxVisual : public Visual
 {
 public:
+    GLXFBConfig fbconfig;
     XVisualInfo *visinfo;
 
-    GlxVisual(XVisualInfo *vi) :
-        visinfo(vi)
+    GlxVisual() :
+        fbconfig(0),
+        visinfo(0)
     {}
 
     ~GlxVisual() {
         XFree(visinfo);
+        XFree(fbconfig);
     }
 };
 
@@ -205,14 +211,20 @@ public:
 
 void
 init(void) {
+    display = XOpenDisplay(NULL);
     if (!display) {
-        display = XOpenDisplay(NULL);
-        if (!display) {
-            std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
-            exit(1);
-        }
-        screen = DefaultScreen(display);
+        std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
+        exit(1);
     }
+
+    screen = DefaultScreen(display);
+
+    int major = 0, minor = 0;
+    glXQueryVersion(display, &major, &minor);
+    glxVersion = (major << 8) | minor;
+
+    extensions = glXQueryExtensionsString(display, screen);
+    has_GLX_ARB_create_context = checkExtension("GLX_ARB_create_context", extensions);
 }
 
 void
@@ -225,34 +237,47 @@ cleanup(void) {
 
 Visual *
 createVisual(bool doubleBuffer) {
-    int single_attribs[] = {
-        GLX_RGBA,
-        GLX_RED_SIZE, 1,
-        GLX_GREEN_SIZE, 1,
-        GLX_BLUE_SIZE, 1,
-        GLX_ALPHA_SIZE, 1,
-        GLX_DEPTH_SIZE, 1,
-        GLX_STENCIL_SIZE, 1,
-        None
-    };
-
-    int double_attribs[] = {
-        GLX_RGBA,
-        GLX_RED_SIZE, 1,
-        GLX_GREEN_SIZE, 1,
-        GLX_BLUE_SIZE, 1,
-        GLX_ALPHA_SIZE, 1,
-        GLX_DOUBLEBUFFER,
-        GLX_DEPTH_SIZE, 1,
-        GLX_STENCIL_SIZE, 1,
-        None
-    };
-
-    XVisualInfo *visinfo;
+    GlxVisual *visual = new GlxVisual;
+
+    if (glxVersion >= 0x0103) {
+        Attributes<int> attribs;
+        attribs.add(GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT);
+        attribs.add(GLX_RENDER_TYPE, GLX_RGBA_BIT);
+        attribs.add(GLX_RED_SIZE, 1);
+        attribs.add(GLX_GREEN_SIZE, 1);
+        attribs.add(GLX_BLUE_SIZE, 1);
+        attribs.add(GLX_ALPHA_SIZE, 1);
+        attribs.add(GLX_DOUBLEBUFFER, doubleBuffer ? GL_TRUE : GL_FALSE);
+        attribs.add(GLX_DEPTH_SIZE, 1);
+        attribs.add(GLX_STENCIL_SIZE, 1);
+        attribs.end();
+
+        int num_configs = 0;
+        GLXFBConfig * fbconfigs;
+        fbconfigs = glXChooseFBConfig(display, screen, attribs, &num_configs);
+        assert(num_configs && fbconfigs);
+        visual->fbconfig = fbconfigs[0];
+        assert(visual->fbconfig);
+        visual->visinfo = glXGetVisualFromFBConfig(display, visual->fbconfig);
+        assert(visual->visinfo);
+    } else {
+        Attributes<int> attribs;
+        attribs.add(GLX_RGBA);
+        attribs.add(GLX_RED_SIZE, 1);
+        attribs.add(GLX_GREEN_SIZE, 1);
+        attribs.add(GLX_BLUE_SIZE, 1);
+        attribs.add(GLX_ALPHA_SIZE, 1);
+        if (doubleBuffer) {
+            attribs.add(GLX_DOUBLEBUFFER);
+        }
+        attribs.add(GLX_DEPTH_SIZE, 1);
+        attribs.add(GLX_STENCIL_SIZE, 1);
+        attribs.end();
 
-    visinfo = glXChooseVisual(display, screen, doubleBuffer ? double_attribs : single_attribs);
+        visual->visinfo = glXChooseVisual(display, screen, attribs);
+    }
 
-    return new GlxVisual(visinfo);
+    return visual;
 }
 
 Drawable *
@@ -262,9 +287,9 @@ createDrawable(const Visual *visual, int width, int height)
 }
 
 Context *
-createContext(const Visual *visual, Context *shareContext)
+createContext(const Visual *_visual, Context *shareContext)
 {
-    XVisualInfo *visinfo = dynamic_cast<const GlxVisual *>(visual)->visinfo;
+    const GlxVisual *visual = dynamic_cast<const GlxVisual *>(_visual);
     GLXContext share_context = NULL;
     GLXContext context;
 
@@ -272,8 +297,22 @@ createContext(const Visual *visual, Context *shareContext)
         share_context = dynamic_cast<GlxContext*>(shareContext)->context;
     }
 
-    context = glXCreateContext(display, visinfo,
-                               share_context, True);
+
+    if (glxVersion >= 0x0104 && has_GLX_ARB_create_context) {
+        Attributes<int> attribs;
+        attribs.add(GLX_RENDER_TYPE, GLX_RGBA_TYPE);
+        if (debug) {
+            attribs.add(GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB);
+        }
+        attribs.end();
+
+        context = glXCreateContextAttribsARB(display, visual->fbconfig, share_context, True, attribs);
+    } else if (glxVersion >= 0x103) {
+        context = glXCreateNewContext(display, visual->fbconfig, GLX_RGBA_TYPE, share_context, True);
+    } else {
+        context = glXCreateContext(display, visual->visinfo, share_context, True);
+    }
+
     return new GlxContext(visual, context);
 }