]> git.tdb.fi Git - gldbg.git/blobdiff - source/glwrap.c
Initial revision
[gldbg.git] / source / glwrap.c
diff --git a/source/glwrap.c b/source/glwrap.c
new file mode 100644 (file)
index 0000000..a33021a
--- /dev/null
@@ -0,0 +1,231 @@
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2009  Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <dlfcn.h>
+#include <sys/uio.h>
+#include <GL/gl.h>
+#include "functions.h"
+
+static inline void *glsym(const char *sym)
+{
+       static void *libgl=NULL;
+       if(!libgl)
+       {
+               libgl=dlopen("libGL.so", RTLD_NOW);
+               if(!libgl)
+               {
+                       fprintf(stderr, "Could not open libGL: %s\n", dlerror());
+                       abort();
+               }
+       }
+
+       return dlsym(libgl, sym);
+}
+
+char *buffer=0;
+char *write_pos;
+struct iovec *iovecs=0;
+struct iovec *cur_vec;
+
+static inline void next_vec()
+{
+       if(write_pos!=cur_vec->iov_base)
+       {
+               cur_vec->iov_len=write_pos-(char *)cur_vec->iov_base;
+               ++cur_vec;
+               cur_vec->iov_base=write_pos;
+       }
+}
+
+static inline void write_bytes(const char *ptr, unsigned size)
+{
+       unsigned i;
+       for(i=0; i<size; ++i)
+               *write_pos++=*ptr++;
+}
+
+static inline void write_char(char v)
+{
+       *write_pos++=v;
+}
+
+static inline void write_short(short v)
+{
+       write_bytes((char *)&v, sizeof(short));
+}
+
+static inline void write_int(int v)
+{
+       write_bytes((char *)&v, sizeof(int));
+}
+
+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)
+{
+       write_bytes((char *)&v, sizeof(long long));
+}
+
+static inline void write_float(float v)
+{
+       write_bytes((char *)&v, sizeof(float));
+}
+
+static inline void write_double(float v)
+{
+       write_bytes((char *)&v, sizeof(double));
+}
+
+static inline void write_pointer(const void *p)
+{
+       write_bytes((char *)&p, sizeof(void *));
+}
+
+static inline void write_data(const void *data, unsigned size)
+{
+       write_int(size);
+       next_vec();
+       cur_vec->iov_base=(void *)data;
+       cur_vec->iov_len=size;
+       ++cur_vec;
+       cur_vec->iov_base=write_pos;
+}
+
+static inline void write_string(const unsigned char *s)
+{
+       write_data(s, strlen(s));
+       /*int len=strlen(s);
+       write_int(len);
+       write_bytes(s, len);*/
+}
+
+static inline void begin_packet(int func)
+{
+       if(!buffer)
+               buffer=(char *)malloc(1024);
+       if(!iovecs)
+               iovecs=(struct iovec *)malloc(16*sizeof(struct iovec));
+       write_pos=buffer;
+       cur_vec=iovecs;
+       cur_vec->iov_base=write_pos;
+       write_short(func);
+}
+
+static inline void send_packet()
+{
+       static int fd=-1;
+       if(fd<0)
+       {
+               const char *var=getenv("GLWRAP_FD");
+               if(var)
+                       fd=strtol(var, NULL, 0);
+               else
+                       fd=2;
+       }
+       next_vec();
+       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;
+}
+
+#include "glwrap.funcs"