blob: c2e5ffcddbdf69e5d22db99229506262cf8f4b13 [file] [log] [blame]
José Fonsecac6b49ce2010-11-28 16:43:04 +00001##########################################################################
2#
3# Copyright 2010 VMware, Inc.
4# All Rights Reserved.
5#
6# Permission is hereby granted, free of charge, to any person obtaining a copy
7# of this software and associated documentation files (the "Software"), to deal
8# in the Software without restriction, including without limitation the rights
9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12#
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22# THE SOFTWARE.
23#
24##########################################################################/
25
26
27import stdapi
28from glapi import glapi
29from glxapi import glxapi
30from wglapi import wglapi
31
32
33public_symbols = set([
34 "glAccum",
35 "glAlphaFunc",
36 "glAreTexturesResident",
37 "glArrayElement",
38 "glBegin",
39 "glBindTexture",
40 "glBitmap",
41 "glBlendFunc",
42 "glCallList",
43 "glCallLists",
44 "glClear",
45 "glClearAccum",
46 "glClearColor",
47 "glClearDepth",
48 "glClearIndex",
49 "glClearStencil",
50 "glClipPlane",
51 "glColor3b",
52 "glColor3bv",
53 "glColor3d",
54 "glColor3dv",
55 "glColor3f",
56 "glColor3fv",
57 "glColor3i",
58 "glColor3iv",
59 "glColor3s",
60 "glColor3sv",
61 "glColor3ub",
62 "glColor3ubv",
63 "glColor3ui",
64 "glColor3uiv",
65 "glColor3us",
66 "glColor3usv",
67 "glColor4b",
68 "glColor4bv",
69 "glColor4d",
70 "glColor4dv",
71 "glColor4f",
72 "glColor4fv",
73 "glColor4i",
74 "glColor4iv",
75 "glColor4s",
76 "glColor4sv",
77 "glColor4ub",
78 "glColor4ubv",
79 "glColor4ui",
80 "glColor4uiv",
81 "glColor4us",
82 "glColor4usv",
83 "glColorMask",
84 "glColorMaterial",
85 "glColorPointer",
86 "glCopyPixels",
87 "glCopyTexImage1D",
88 "glCopyTexImage2D",
89 "glCopyTexSubImage1D",
90 "glCopyTexSubImage2D",
91 "glCullFace",
92# "glDebugEntry",
93 "glDeleteLists",
94 "glDeleteTextures",
95 "glDepthFunc",
96 "glDepthMask",
97 "glDepthRange",
98 "glDisable",
99 "glDisableClientState",
100 "glDrawArrays",
101 "glDrawBuffer",
102 "glDrawElements",
103 "glDrawPixels",
104 "glEdgeFlag",
105 "glEdgeFlagPointer",
106 "glEdgeFlagv",
107 "glEnable",
108 "glEnableClientState",
109 "glEnd",
110 "glEndList",
111 "glEvalCoord1d",
112 "glEvalCoord1dv",
113 "glEvalCoord1f",
114 "glEvalCoord1fv",
115 "glEvalCoord2d",
116 "glEvalCoord2dv",
117 "glEvalCoord2f",
118 "glEvalCoord2fv",
119 "glEvalMesh1",
120 "glEvalMesh2",
121 "glEvalPoint1",
122 "glEvalPoint2",
123 "glFeedbackBuffer",
124 "glFinish",
125 "glFlush",
126 "glFogf",
127 "glFogfv",
128 "glFogi",
129 "glFogiv",
130 "glFrontFace",
131 "glFrustum",
132 "glGenLists",
133 "glGenTextures",
134 "glGetBooleanv",
135 "glGetClipPlane",
136 "glGetDoublev",
137 "glGetError",
138 "glGetFloatv",
139 "glGetIntegerv",
140 "glGetLightfv",
141 "glGetLightiv",
142 "glGetMapdv",
143 "glGetMapfv",
144 "glGetMapiv",
145 "glGetMaterialfv",
146 "glGetMaterialiv",
147 "glGetPixelMapfv",
148 "glGetPixelMapuiv",
149 "glGetPixelMapusv",
150 "glGetPointerv",
151 "glGetPolygonStipple",
152 "glGetString",
153 "glGetTexEnvfv",
154 "glGetTexEnviv",
155 "glGetTexGendv",
156 "glGetTexGenfv",
157 "glGetTexGeniv",
158 "glGetTexImage",
159 "glGetTexLevelParameterfv",
160 "glGetTexLevelParameteriv",
161 "glGetTexParameterfv",
162 "glGetTexParameteriv",
163 "glHint",
164 "glIndexMask",
165 "glIndexPointer",
166 "glIndexd",
167 "glIndexdv",
168 "glIndexf",
169 "glIndexfv",
170 "glIndexi",
171 "glIndexiv",
172 "glIndexs",
173 "glIndexsv",
174 "glIndexub",
175 "glIndexubv",
176 "glInitNames",
177 "glInterleavedArrays",
178 "glIsEnabled",
179 "glIsList",
180 "glIsTexture",
181 "glLightModelf",
182 "glLightModelfv",
183 "glLightModeli",
184 "glLightModeliv",
185 "glLightf",
186 "glLightfv",
187 "glLighti",
188 "glLightiv",
189 "glLineStipple",
190 "glLineWidth",
191 "glListBase",
192 "glLoadIdentity",
193 "glLoadMatrixd",
194 "glLoadMatrixf",
195 "glLoadName",
196 "glLogicOp",
197 "glMap1d",
198 "glMap1f",
199 "glMap2d",
200 "glMap2f",
201 "glMapGrid1d",
202 "glMapGrid1f",
203 "glMapGrid2d",
204 "glMapGrid2f",
205 "glMaterialf",
206 "glMaterialfv",
207 "glMateriali",
208 "glMaterialiv",
209 "glMatrixMode",
210 "glMultMatrixd",
211 "glMultMatrixf",
212 "glNewList",
213 "glNormal3b",
214 "glNormal3bv",
215 "glNormal3d",
216 "glNormal3dv",
217 "glNormal3f",
218 "glNormal3fv",
219 "glNormal3i",
220 "glNormal3iv",
221 "glNormal3s",
222 "glNormal3sv",
223 "glNormalPointer",
224 "glOrtho",
225 "glPassThrough",
226 "glPixelMapfv",
227 "glPixelMapuiv",
228 "glPixelMapusv",
229 "glPixelStoref",
230 "glPixelStorei",
231 "glPixelTransferf",
232 "glPixelTransferi",
233 "glPixelZoom",
234 "glPointSize",
235 "glPolygonMode",
236 "glPolygonOffset",
237 "glPolygonStipple",
238 "glPopAttrib",
239 "glPopClientAttrib",
240 "glPopMatrix",
241 "glPopName",
242 "glPrioritizeTextures",
243 "glPushAttrib",
244 "glPushClientAttrib",
245 "glPushMatrix",
246 "glPushName",
247 "glRasterPos2d",
248 "glRasterPos2dv",
249 "glRasterPos2f",
250 "glRasterPos2fv",
251 "glRasterPos2i",
252 "glRasterPos2iv",
253 "glRasterPos2s",
254 "glRasterPos2sv",
255 "glRasterPos3d",
256 "glRasterPos3dv",
257 "glRasterPos3f",
258 "glRasterPos3fv",
259 "glRasterPos3i",
260 "glRasterPos3iv",
261 "glRasterPos3s",
262 "glRasterPos3sv",
263 "glRasterPos4d",
264 "glRasterPos4dv",
265 "glRasterPos4f",
266 "glRasterPos4fv",
267 "glRasterPos4i",
268 "glRasterPos4iv",
269 "glRasterPos4s",
270 "glRasterPos4sv",
271 "glReadBuffer",
272 "glReadPixels",
273 "glRectd",
274 "glRectdv",
275 "glRectf",
276 "glRectfv",
277 "glRecti",
278 "glRectiv",
279 "glRects",
280 "glRectsv",
281 "glRenderMode",
282 "glRotated",
283 "glRotatef",
284 "glScaled",
285 "glScalef",
286 "glScissor",
287 "glSelectBuffer",
288 "glShadeModel",
289 "glStencilFunc",
290 "glStencilMask",
291 "glStencilOp",
292 "glTexCoord1d",
293 "glTexCoord1dv",
294 "glTexCoord1f",
295 "glTexCoord1fv",
296 "glTexCoord1i",
297 "glTexCoord1iv",
298 "glTexCoord1s",
299 "glTexCoord1sv",
300 "glTexCoord2d",
301 "glTexCoord2dv",
302 "glTexCoord2f",
303 "glTexCoord2fv",
304 "glTexCoord2i",
305 "glTexCoord2iv",
306 "glTexCoord2s",
307 "glTexCoord2sv",
308 "glTexCoord3d",
309 "glTexCoord3dv",
310 "glTexCoord3f",
311 "glTexCoord3fv",
312 "glTexCoord3i",
313 "glTexCoord3iv",
314 "glTexCoord3s",
315 "glTexCoord3sv",
316 "glTexCoord4d",
317 "glTexCoord4dv",
318 "glTexCoord4f",
319 "glTexCoord4fv",
320 "glTexCoord4i",
321 "glTexCoord4iv",
322 "glTexCoord4s",
323 "glTexCoord4sv",
324 "glTexCoordPointer",
325 "glTexEnvf",
326 "glTexEnvfv",
327 "glTexEnvi",
328 "glTexEnviv",
329 "glTexGend",
330 "glTexGendv",
331 "glTexGenf",
332 "glTexGenfv",
333 "glTexGeni",
334 "glTexGeniv",
335 "glTexImage1D",
336 "glTexImage2D",
337 "glTexParameterf",
338 "glTexParameterfv",
339 "glTexParameteri",
340 "glTexParameteriv",
341 "glTexSubImage1D",
342 "glTexSubImage2D",
343 "glTranslated",
344 "glTranslatef",
345 "glVertex2d",
346 "glVertex2dv",
347 "glVertex2f",
348 "glVertex2fv",
349 "glVertex2i",
350 "glVertex2iv",
351 "glVertex2s",
352 "glVertex2sv",
353 "glVertex3d",
354 "glVertex3dv",
355 "glVertex3f",
356 "glVertex3fv",
357 "glVertex3i",
358 "glVertex3iv",
359 "glVertex3s",
360 "glVertex3sv",
361 "glVertex4d",
362 "glVertex4dv",
363 "glVertex4f",
364 "glVertex4fv",
365 "glVertex4i",
366 "glVertex4iv",
367 "glVertex4s",
368 "glVertex4sv",
369 "glVertexPointer",
370 "glViewport",
371 "wglChoosePixelFormat",
372 "wglCopyContext",
373 "wglCreateContext",
374 "wglCreateLayerContext",
375 "wglDeleteContext",
376 "wglDescribeLayerPlane",
377 "wglDescribePixelFormat",
378 "wglGetCurrentContext",
379 "wglGetCurrentDC",
380 "wglGetDefaultProcAddress",
381 "wglGetLayerPaletteEntries",
382 "wglGetPixelFormat",
383 "wglGetProcAddress",
384 "wglMakeCurrent",
385 "wglRealizeLayerPalette",
386 "wglSetLayerPaletteEntries",
387 "wglSetPixelFormat",
388 "wglShareLists",
389 "wglSwapBuffers",
390 "wglSwapLayerBuffers",
391 "wglSwapMultipleBuffers",
392 "wglUseFontBitmapsA",
393 "wglUseFontBitmapsW",
394 "wglUseFontOutlinesA",
395 "wglUseFontOutlinesW",
396])
397
398
399class Dispatcher:
400
401 def function_pointer_type(self, function):
402 return '__PFN' + function.name.upper()
403
404 def function_pointer_value(self, function):
405 return '__' + function.name + '_ptr'
406
407 def dispatch_function(self, function):
408 if function.name in public_symbols:
409 return
410
411 ptype = self.function_pointer_type(function)
412 pvalue = self.function_pointer_value(function)
413 print 'typedef ' + function.prototype('* %s' % ptype) + ';'
414 print 'static %s %s = NULL;' % (ptype, pvalue)
415 print
416 print 'static inline ' + function.prototype('__' + function.name) + ' {'
417 if function.type is stdapi.Void:
418 ret = ''
419 else:
420 ret = 'return '
421 self.get_true_pointer(function)
422 pvalue = self.function_pointer_value(function)
423 print ' %s%s(%s);' % (ret, pvalue, ', '.join([str(arg.name) for arg in function.args]))
424 print '}'
425 print
426 print '#define %s __%s' % (function.name, function.name)
427
428 def get_true_pointer(self, function):
429 ptype = self.function_pointer_type(function)
430 pvalue = self.function_pointer_value(function)
431 if function.name in public_symbols:
432 get_proc_address = '__getProcAddress'
433 else:
434 get_proc_address = '__glGetProcAddress'
435 print ' if (!%s) {' % (pvalue,)
436 print ' %s = (%s)%s("%s");' % (pvalue, ptype, get_proc_address, function.name)
437 print ' if (!%s) {' % (pvalue,)
438 self.fail_function(function)
439 print ' }'
440 print ' }'
441
442 def fail_function(self, function):
443 print ' std::cerr << "error: unavailable function \\"%s\\"\\n";' % function.name
444 if function.fail is not None:
445 if function.type is stdapi.Void:
446 assert function.fail == ''
447 print ' return;'
448 else:
449 assert function.fail != ''
450 print ' return %s;' % function.fail
451 else:
452 print ' abort();'
453
454
455if __name__ == '__main__':
456 print
457 print '#include "glimports.hpp"'
458 print '#include "os.hpp"'
459 print
460 if 0:
461 print 'typedef void (*__PROC)(void);'
462 print
463 print 'static __PROC __getProcAddress(const char *name);'
464 print
465 print 'static __PROC __glGetProcAddress(const char *name);'
466 else:
467 print '#ifdef WIN32'
468 print '#define __glGetProcAddress wglGetProcAddress'
469 print '#else'
470 print '#define __glGetProcAddress(name) glXGetProcAddress((const GLubyte *)(name))'
471 print '#endif'
472 print
473 dispatcher = Dispatcher()
474 for function in glapi.functions:
475 dispatcher.dispatch_function(function)
476 print
477 if 0:
478 print '''
479
480#ifdef WIN32
481
482static HINSTANCE g_hDll = NULL;
483
484static __PROC
485__getProcAddress(const char *name)
486{
487 if (!g_hDll) {
488 char szDll[MAX_PATH] = {0};
489
490 if (!GetSystemDirectoryA(szDll, MAX_PATH)) {
491 return NULL;
492 }
493
494 strcat(szDll, "\\\\opengl32.dll");
495
496 g_hDll = LoadLibraryA(szDll);
497 if (!g_hDll) {
498 return NULL;
499 }
500 }
501
502 return GetProcAddress(g_hDll, name);
503}
504
505static inline __PROC
506__glGetProcAddress(const char *name) {
507 return __wglGetProcAddress(name);
508}
509
510#else
511
512static void g_module = NULL;
513
514static __PROC
515__getProcAddress(const char *name)
516{
517 if (!g_module) {
518 g_module = dlopen("libGL.so", RTLD_LAZY);
519 if (!g_module) {
520 return NULL;
521 }
522 }
523
524 return (__PROC)dlsym(g_module, name);
525}
526
527static inline __PROC
528__glGetProcAddress(const char *name) {
529 return __glXGetProcAddress(name);
530}
531
532#endif
533
534 '''