]> git.tdb.fi Git - gldbg.git/blobdiff - source/glwrap.c
Add a performance profiler
[gldbg.git] / source / glwrap.c
index 0a22c3c1ce4b6677c0bb66cdb10d559294fbe160..bf05214b8a09d7ee844d5567626b395b2066f1db 100644 (file)
@@ -13,6 +13,7 @@ Distributed under the GPL
 #include <X11/Xlib.h>
 #include <GL/gl.h>
 #include <GL/glx.h>
+#include "arraysize.h"
 #include "functions.h"
 
 static inline void *glsym(const char *sym)
@@ -20,10 +21,13 @@ static inline void *glsym(const char *sym)
        static void *libgl = NULL;
        if(!libgl)
        {
-               libgl = dlopen("libGL.so", RTLD_NOW);
+               const char *libgl_name = getenv("GLWRAP_LIBGL");
+               if(!libgl_name)
+                       libgl_name = "libGL.so";
+               libgl = dlopen(libgl_name, RTLD_NOW);
                if(!libgl)
                {
-                       fprintf(stderr, "Could not open libGL: %s\n", dlerror());
+                       fprintf(stderr, "Could not open %s: %s\n", libgl_name, dlerror());
                        abort();
                }
        }
@@ -75,12 +79,7 @@ static inline void write_long(long v)
        write_bytes((char *)&v, sizeof(long));
 }
 
-static inline void write_ulong(unsigned long v)
-{
-       write_bytes((char *)&v, sizeof(unsigned long));
-}
-
-static inline void write_longlong(long long v)
+static inline void write_long_long(long long v)
 {
        write_bytes((char *)&v, sizeof(long long));
 }
@@ -116,11 +115,20 @@ static inline void write_data(const void *data, unsigned size)
                write_int(0);
 }
 
-static inline void write_string(const unsigned char *s)
+static inline void write_string(const char *s)
 {
        write_data(s, strlen(s)+1);
 }
 
+static inline void write_string_array(const char **sa, unsigned size)
+{
+       unsigned i;
+       size /= sizeof(const char *);
+       write_int(size);
+       for(i=0; i<size; ++i)
+               write_string(sa[i]);
+}
+
 static inline void begin_packet(int func)
 {
        if(!buffer)
@@ -152,93 +160,6 @@ static inline void send_packet()
        writev(fd, iovecs, cur_vec-iovecs);
 }
 
-static inline int typesize(GLenum type)
-{
-       switch(type)
-       {
-       case GL_BYTE: return sizeof(GLbyte);
-       case GL_SHORT: return sizeof(GLshort);
-       case GL_INT: return sizeof(GLint);
-       case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
-       case GL_UNSIGNED_SHORT: return sizeof(GLushort);
-       case GL_UNSIGNED_INT: return sizeof(GLuint);
-       case GL_FLOAT: return sizeof(GLfloat);
-       case GL_DOUBLE: return sizeof(GLdouble);
-       // Short and byte packed types are broken
-       default: return 1;
-       }
-}
-
-static inline int formatsize(GLenum format)
-{
-       switch(format)
-       {
-       case GL_COLOR_INDEX: return 1;
-       case GL_STENCIL_INDEX: return 1;
-       case GL_DEPTH_COMPONENT: return 1;
-       case GL_RED: return 1;
-       case GL_GREEN: return 1;
-       case GL_BLUE: return 1;
-       case GL_ALPHA: return 1;
-       case GL_RGB: return 3;
-       case GL_RGBA: return 4;
-       case GL_BGR: return 3;
-       case GL_BGRA: return 4;
-       case GL_LUMINANCE: return 1;
-       case GL_LUMINANCE_ALPHA: return 2;
-       default: return 1;
-       }
-}
-
-static inline int paramsize(GLenum pname)
-{
-       switch(pname)
-       {
-       // Lighting and material
-       case GL_AMBIENT: return 4;
-       case GL_DIFFUSE: return 4;
-       case GL_AMBIENT_AND_DIFFUSE: return 4;
-       case GL_SPECULAR: return 4;
-       case GL_EMISSION: return 4;
-       case GL_SHININESS: return 1;
-       case GL_COLOR_INDEXES: return 3;
-       case GL_POSITION: return 4;
-       case GL_SPOT_DIRECTION: return 3;
-       case GL_SPOT_EXPONENT: return 1;
-       case GL_SPOT_CUTOFF: return 1;
-       case GL_CONSTANT_ATTENUATION: return 1;
-       case GL_LINEAR_ATTENUATION: return 1;
-       case GL_QUADRATIC_ATTENUATION: return 1;
-       case GL_LIGHT_MODEL_AMBIENT: return 4;
-       case GL_LIGHT_MODEL_LOCAL_VIEWER: return 1;
-       case GL_LIGHT_MODEL_TWO_SIDE: return 1;
-       case GL_LIGHT_MODEL_COLOR_CONTROL: return 1;
-
-       // Texture
-       case GL_TEXTURE_WRAP_S: return 1;
-       case GL_TEXTURE_WRAP_T: return 1;
-       case GL_TEXTURE_WRAP_R: return 1;
-       case GL_TEXTURE_MIN_FILTER: return 1;
-       case GL_TEXTURE_MAG_FILTER: return 1;
-       case GL_TEXTURE_BORDER_COLOR: return 4;
-       case GL_TEXTURE_MIN_LOD: return 1;
-       case GL_TEXTURE_MAX_LOD: return 1;
-       case GL_TEXTURE_BASE_LEVEL: return 1;
-       case GL_TEXTURE_MAX_LEVEL: return 1;
-       case GL_TEXTURE_LOD_BIAS: return 1;
-       case GL_DEPTH_TEXTURE_MODE: return 1;
-       case GL_TEXTURE_COMPARE_MODE: return 1;
-       case GL_TEXTURE_COMPARE_FUNC: return 1;
-       case GL_GENERATE_MIPMAP: return 1;
-       default: return 1;
-       }
-}
-
-static inline int mapsize(GLenum target)
-{
-       return 1;
-}
-
 GLenum cur_error = GL_NO_ERROR;
 
 static void check_error()
@@ -262,13 +183,29 @@ GLenum APIENTRY glGetError()
 {
        GLenum ret = cur_error;
        cur_error = GL_NO_ERROR;
+       begin_packet(FUNC_GLGETERROR);
+       write_int(ret);
+       send_packet();
        return ret;
 }
 
 void (*glXGetProcAddress(const GLubyte *procname))(void)
 {
-       void *handle = dlopen(NULL, RTLD_LAZY);
-       return dlsym(handle, (const char *)procname);
+       void *handle = 0;
+       void (*ret)() = 0;
+
+       if(glsym((const char *)procname))
+       {
+               handle = dlopen(NULL, RTLD_LAZY);
+               ret = dlsym(handle, (const char *)procname);
+       }
+
+       begin_packet(FUNC_GLXGETPROCADDRESS);
+       write_pointer(ret);
+       write_string((const char *)procname);
+       send_packet();
+
+       return ret;
 }
 
 void (*glXGetProcAddressARB(const GLubyte *))(void) __attribute__((alias("glXGetProcAddress")));