]> git.tdb.fi Git - gldbg.git/blob - source/glwrap.c
Initial revision
[gldbg.git] / source / glwrap.c
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <dlfcn.h>
12 #include <sys/uio.h>
13 #include <GL/gl.h>
14 #include "functions.h"
15
16 static inline void *glsym(const char *sym)
17 {
18         static void *libgl=NULL;
19         if(!libgl)
20         {
21                 libgl=dlopen("libGL.so", RTLD_NOW);
22                 if(!libgl)
23                 {
24                         fprintf(stderr, "Could not open libGL: %s\n", dlerror());
25                         abort();
26                 }
27         }
28
29         return dlsym(libgl, sym);
30 }
31
32 char *buffer=0;
33 char *write_pos;
34 struct iovec *iovecs=0;
35 struct iovec *cur_vec;
36
37 static inline void next_vec()
38 {
39         if(write_pos!=cur_vec->iov_base)
40         {
41                 cur_vec->iov_len=write_pos-(char *)cur_vec->iov_base;
42                 ++cur_vec;
43                 cur_vec->iov_base=write_pos;
44         }
45 }
46
47 static inline void write_bytes(const char *ptr, unsigned size)
48 {
49         unsigned i;
50         for(i=0; i<size; ++i)
51                 *write_pos++=*ptr++;
52 }
53
54 static inline void write_char(char v)
55 {
56         *write_pos++=v;
57 }
58
59 static inline void write_short(short v)
60 {
61         write_bytes((char *)&v, sizeof(short));
62 }
63
64 static inline void write_int(int v)
65 {
66         write_bytes((char *)&v, sizeof(int));
67 }
68
69 static inline void write_long(long v)
70 {
71         write_bytes((char *)&v, sizeof(long));
72 }
73
74 static inline void write_ulong(unsigned long v)
75 {
76         write_bytes((char *)&v, sizeof(unsigned long));
77 }
78
79 static inline void write_longlong(long long v)
80 {
81         write_bytes((char *)&v, sizeof(long long));
82 }
83
84 static inline void write_float(float v)
85 {
86         write_bytes((char *)&v, sizeof(float));
87 }
88
89 static inline void write_double(float v)
90 {
91         write_bytes((char *)&v, sizeof(double));
92 }
93
94 static inline void write_pointer(const void *p)
95 {
96         write_bytes((char *)&p, sizeof(void *));
97 }
98
99 static inline void write_data(const void *data, unsigned size)
100 {
101         write_int(size);
102         next_vec();
103         cur_vec->iov_base=(void *)data;
104         cur_vec->iov_len=size;
105         ++cur_vec;
106         cur_vec->iov_base=write_pos;
107 }
108
109 static inline void write_string(const unsigned char *s)
110 {
111         write_data(s, strlen(s));
112         /*int len=strlen(s);
113         write_int(len);
114         write_bytes(s, len);*/
115 }
116
117 static inline void begin_packet(int func)
118 {
119         if(!buffer)
120                 buffer=(char *)malloc(1024);
121         if(!iovecs)
122                 iovecs=(struct iovec *)malloc(16*sizeof(struct iovec));
123         write_pos=buffer;
124         cur_vec=iovecs;
125         cur_vec->iov_base=write_pos;
126         write_short(func);
127 }
128
129 static inline void send_packet()
130 {
131         static int fd=-1;
132         if(fd<0)
133         {
134                 const char *var=getenv("GLWRAP_FD");
135                 if(var)
136                         fd=strtol(var, NULL, 0);
137                 else
138                         fd=2;
139         }
140         next_vec();
141         writev(fd, iovecs, cur_vec-iovecs);
142 }
143
144 static inline int typesize(GLenum type)
145 {
146         switch(type)
147         {
148         case GL_BYTE: return sizeof(GLbyte);
149         case GL_SHORT: return sizeof(GLshort);
150         case GL_INT: return sizeof(GLint);
151         case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
152         case GL_UNSIGNED_SHORT: return sizeof(GLushort);
153         case GL_UNSIGNED_INT: return sizeof(GLuint);
154         case GL_FLOAT: return sizeof(GLfloat);
155         case GL_DOUBLE: return sizeof(GLdouble);
156         // Short and byte packed types are broken
157         default: return 1;
158         }
159 }
160
161 static inline int formatsize(GLenum format)
162 {
163         switch(format)
164         {
165         case GL_COLOR_INDEX: return 1;
166         case GL_STENCIL_INDEX: return 1;
167         case GL_DEPTH_COMPONENT: return 1;
168         case GL_RED: return 1;
169         case GL_GREEN: return 1;
170         case GL_BLUE: return 1;
171         case GL_ALPHA: return 1;
172         case GL_RGB: return 3;
173         case GL_RGBA: return 4;
174         case GL_BGR: return 3;
175         case GL_BGRA: return 4;
176         case GL_LUMINANCE: return 1;
177         case GL_LUMINANCE_ALPHA: return 2;
178         default: return 1;
179         }
180 }
181
182 static inline int paramsize(GLenum pname)
183 {
184         switch(pname)
185         {
186         // Lighting and material
187         case GL_AMBIENT: return 4;
188         case GL_DIFFUSE: return 4;
189         case GL_AMBIENT_AND_DIFFUSE: return 4;
190         case GL_SPECULAR: return 4;
191         case GL_EMISSION: return 4;
192         case GL_SHININESS: return 1;
193         case GL_COLOR_INDEXES: return 3;
194         case GL_POSITION: return 4;
195         case GL_SPOT_DIRECTION: return 3;
196         case GL_SPOT_EXPONENT: return 1;
197         case GL_SPOT_CUTOFF: return 1;
198         case GL_CONSTANT_ATTENUATION: return 1;
199         case GL_LINEAR_ATTENUATION: return 1;
200         case GL_QUADRATIC_ATTENUATION: return 1;
201         case GL_LIGHT_MODEL_AMBIENT: return 4;
202         case GL_LIGHT_MODEL_LOCAL_VIEWER: return 1;
203         case GL_LIGHT_MODEL_TWO_SIDE: return 1;
204         case GL_LIGHT_MODEL_COLOR_CONTROL: return 1;
205
206         // Texture
207         case GL_TEXTURE_WRAP_S: return 1;
208         case GL_TEXTURE_WRAP_T: return 1;
209         case GL_TEXTURE_WRAP_R: return 1;
210         case GL_TEXTURE_MIN_FILTER: return 1;
211         case GL_TEXTURE_MAG_FILTER: return 1;
212         case GL_TEXTURE_BORDER_COLOR: return 4;
213         case GL_TEXTURE_MIN_LOD: return 1;
214         case GL_TEXTURE_MAX_LOD: return 1;
215         case GL_TEXTURE_BASE_LEVEL: return 1;
216         case GL_TEXTURE_MAX_LEVEL: return 1;
217         case GL_TEXTURE_LOD_BIAS: return 1;
218         case GL_DEPTH_TEXTURE_MODE: return 1;
219         case GL_TEXTURE_COMPARE_MODE: return 1;
220         case GL_TEXTURE_COMPARE_FUNC: return 1;
221         case GL_GENERATE_MIPMAP: return 1;
222         default: return 1;
223         }
224 }
225
226 static inline int mapsize(GLenum target)
227 {
228         return 1;
229 }
230
231 #include "glwrap.funcs"