blob: f3dc3d2e95a64bce65b8b704457e1a83aa21365c [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
José Fonseca4a826ed2010-11-30 16:58:22 +000027"""Generated an header, glproc.hpp, which does pretty much what GLEW does, but
28covers all the functions we support.
29"""
30
31
José Fonsecac6b49ce2010-11-28 16:43:04 +000032import stdapi
33from glapi import glapi
34from glxapi import glxapi
35from wglapi import wglapi
36
37
José Fonseca68ec4122011-02-20 11:25:25 +000038# See http://www.opengl.org/registry/ABI/
José Fonsecac6b49ce2010-11-28 16:43:04 +000039public_symbols = set([
40 "glAccum",
41 "glAlphaFunc",
42 "glAreTexturesResident",
43 "glArrayElement",
44 "glBegin",
45 "glBindTexture",
46 "glBitmap",
47 "glBlendFunc",
48 "glCallList",
49 "glCallLists",
50 "glClear",
51 "glClearAccum",
52 "glClearColor",
53 "glClearDepth",
54 "glClearIndex",
55 "glClearStencil",
56 "glClipPlane",
57 "glColor3b",
58 "glColor3bv",
59 "glColor3d",
60 "glColor3dv",
61 "glColor3f",
62 "glColor3fv",
63 "glColor3i",
64 "glColor3iv",
65 "glColor3s",
66 "glColor3sv",
67 "glColor3ub",
68 "glColor3ubv",
69 "glColor3ui",
70 "glColor3uiv",
71 "glColor3us",
72 "glColor3usv",
73 "glColor4b",
74 "glColor4bv",
75 "glColor4d",
76 "glColor4dv",
77 "glColor4f",
78 "glColor4fv",
79 "glColor4i",
80 "glColor4iv",
81 "glColor4s",
82 "glColor4sv",
83 "glColor4ub",
84 "glColor4ubv",
85 "glColor4ui",
86 "glColor4uiv",
87 "glColor4us",
88 "glColor4usv",
89 "glColorMask",
90 "glColorMaterial",
91 "glColorPointer",
92 "glCopyPixels",
93 "glCopyTexImage1D",
94 "glCopyTexImage2D",
95 "glCopyTexSubImage1D",
96 "glCopyTexSubImage2D",
97 "glCullFace",
98# "glDebugEntry",
99 "glDeleteLists",
100 "glDeleteTextures",
101 "glDepthFunc",
102 "glDepthMask",
103 "glDepthRange",
104 "glDisable",
105 "glDisableClientState",
106 "glDrawArrays",
107 "glDrawBuffer",
108 "glDrawElements",
109 "glDrawPixels",
110 "glEdgeFlag",
111 "glEdgeFlagPointer",
112 "glEdgeFlagv",
113 "glEnable",
114 "glEnableClientState",
115 "glEnd",
116 "glEndList",
117 "glEvalCoord1d",
118 "glEvalCoord1dv",
119 "glEvalCoord1f",
120 "glEvalCoord1fv",
121 "glEvalCoord2d",
122 "glEvalCoord2dv",
123 "glEvalCoord2f",
124 "glEvalCoord2fv",
125 "glEvalMesh1",
126 "glEvalMesh2",
127 "glEvalPoint1",
128 "glEvalPoint2",
129 "glFeedbackBuffer",
130 "glFinish",
131 "glFlush",
132 "glFogf",
133 "glFogfv",
134 "glFogi",
135 "glFogiv",
136 "glFrontFace",
137 "glFrustum",
138 "glGenLists",
139 "glGenTextures",
140 "glGetBooleanv",
141 "glGetClipPlane",
142 "glGetDoublev",
143 "glGetError",
144 "glGetFloatv",
145 "glGetIntegerv",
146 "glGetLightfv",
147 "glGetLightiv",
148 "glGetMapdv",
149 "glGetMapfv",
150 "glGetMapiv",
151 "glGetMaterialfv",
152 "glGetMaterialiv",
153 "glGetPixelMapfv",
154 "glGetPixelMapuiv",
155 "glGetPixelMapusv",
156 "glGetPointerv",
157 "glGetPolygonStipple",
158 "glGetString",
159 "glGetTexEnvfv",
160 "glGetTexEnviv",
161 "glGetTexGendv",
162 "glGetTexGenfv",
163 "glGetTexGeniv",
164 "glGetTexImage",
165 "glGetTexLevelParameterfv",
166 "glGetTexLevelParameteriv",
167 "glGetTexParameterfv",
168 "glGetTexParameteriv",
169 "glHint",
170 "glIndexMask",
171 "glIndexPointer",
172 "glIndexd",
173 "glIndexdv",
174 "glIndexf",
175 "glIndexfv",
176 "glIndexi",
177 "glIndexiv",
178 "glIndexs",
179 "glIndexsv",
180 "glIndexub",
181 "glIndexubv",
182 "glInitNames",
183 "glInterleavedArrays",
184 "glIsEnabled",
185 "glIsList",
186 "glIsTexture",
187 "glLightModelf",
188 "glLightModelfv",
189 "glLightModeli",
190 "glLightModeliv",
191 "glLightf",
192 "glLightfv",
193 "glLighti",
194 "glLightiv",
195 "glLineStipple",
196 "glLineWidth",
197 "glListBase",
198 "glLoadIdentity",
199 "glLoadMatrixd",
200 "glLoadMatrixf",
201 "glLoadName",
202 "glLogicOp",
203 "glMap1d",
204 "glMap1f",
205 "glMap2d",
206 "glMap2f",
207 "glMapGrid1d",
208 "glMapGrid1f",
209 "glMapGrid2d",
210 "glMapGrid2f",
211 "glMaterialf",
212 "glMaterialfv",
213 "glMateriali",
214 "glMaterialiv",
215 "glMatrixMode",
216 "glMultMatrixd",
217 "glMultMatrixf",
218 "glNewList",
219 "glNormal3b",
220 "glNormal3bv",
221 "glNormal3d",
222 "glNormal3dv",
223 "glNormal3f",
224 "glNormal3fv",
225 "glNormal3i",
226 "glNormal3iv",
227 "glNormal3s",
228 "glNormal3sv",
229 "glNormalPointer",
230 "glOrtho",
231 "glPassThrough",
232 "glPixelMapfv",
233 "glPixelMapuiv",
234 "glPixelMapusv",
235 "glPixelStoref",
236 "glPixelStorei",
237 "glPixelTransferf",
238 "glPixelTransferi",
239 "glPixelZoom",
240 "glPointSize",
241 "glPolygonMode",
242 "glPolygonOffset",
243 "glPolygonStipple",
244 "glPopAttrib",
245 "glPopClientAttrib",
246 "glPopMatrix",
247 "glPopName",
248 "glPrioritizeTextures",
249 "glPushAttrib",
250 "glPushClientAttrib",
251 "glPushMatrix",
252 "glPushName",
253 "glRasterPos2d",
254 "glRasterPos2dv",
255 "glRasterPos2f",
256 "glRasterPos2fv",
257 "glRasterPos2i",
258 "glRasterPos2iv",
259 "glRasterPos2s",
260 "glRasterPos2sv",
261 "glRasterPos3d",
262 "glRasterPos3dv",
263 "glRasterPos3f",
264 "glRasterPos3fv",
265 "glRasterPos3i",
266 "glRasterPos3iv",
267 "glRasterPos3s",
268 "glRasterPos3sv",
269 "glRasterPos4d",
270 "glRasterPos4dv",
271 "glRasterPos4f",
272 "glRasterPos4fv",
273 "glRasterPos4i",
274 "glRasterPos4iv",
275 "glRasterPos4s",
276 "glRasterPos4sv",
277 "glReadBuffer",
278 "glReadPixels",
279 "glRectd",
280 "glRectdv",
281 "glRectf",
282 "glRectfv",
283 "glRecti",
284 "glRectiv",
285 "glRects",
286 "glRectsv",
287 "glRenderMode",
288 "glRotated",
289 "glRotatef",
290 "glScaled",
291 "glScalef",
292 "glScissor",
293 "glSelectBuffer",
294 "glShadeModel",
295 "glStencilFunc",
296 "glStencilMask",
297 "glStencilOp",
298 "glTexCoord1d",
299 "glTexCoord1dv",
300 "glTexCoord1f",
301 "glTexCoord1fv",
302 "glTexCoord1i",
303 "glTexCoord1iv",
304 "glTexCoord1s",
305 "glTexCoord1sv",
306 "glTexCoord2d",
307 "glTexCoord2dv",
308 "glTexCoord2f",
309 "glTexCoord2fv",
310 "glTexCoord2i",
311 "glTexCoord2iv",
312 "glTexCoord2s",
313 "glTexCoord2sv",
314 "glTexCoord3d",
315 "glTexCoord3dv",
316 "glTexCoord3f",
317 "glTexCoord3fv",
318 "glTexCoord3i",
319 "glTexCoord3iv",
320 "glTexCoord3s",
321 "glTexCoord3sv",
322 "glTexCoord4d",
323 "glTexCoord4dv",
324 "glTexCoord4f",
325 "glTexCoord4fv",
326 "glTexCoord4i",
327 "glTexCoord4iv",
328 "glTexCoord4s",
329 "glTexCoord4sv",
330 "glTexCoordPointer",
331 "glTexEnvf",
332 "glTexEnvfv",
333 "glTexEnvi",
334 "glTexEnviv",
335 "glTexGend",
336 "glTexGendv",
337 "glTexGenf",
338 "glTexGenfv",
339 "glTexGeni",
340 "glTexGeniv",
341 "glTexImage1D",
342 "glTexImage2D",
343 "glTexParameterf",
344 "glTexParameterfv",
345 "glTexParameteri",
346 "glTexParameteriv",
347 "glTexSubImage1D",
348 "glTexSubImage2D",
349 "glTranslated",
350 "glTranslatef",
351 "glVertex2d",
352 "glVertex2dv",
353 "glVertex2f",
354 "glVertex2fv",
355 "glVertex2i",
356 "glVertex2iv",
357 "glVertex2s",
358 "glVertex2sv",
359 "glVertex3d",
360 "glVertex3dv",
361 "glVertex3f",
362 "glVertex3fv",
363 "glVertex3i",
364 "glVertex3iv",
365 "glVertex3s",
366 "glVertex3sv",
367 "glVertex4d",
368 "glVertex4dv",
369 "glVertex4f",
370 "glVertex4fv",
371 "glVertex4i",
372 "glVertex4iv",
373 "glVertex4s",
374 "glVertex4sv",
375 "glVertexPointer",
376 "glViewport",
377 "wglChoosePixelFormat",
378 "wglCopyContext",
379 "wglCreateContext",
380 "wglCreateLayerContext",
381 "wglDeleteContext",
382 "wglDescribeLayerPlane",
383 "wglDescribePixelFormat",
384 "wglGetCurrentContext",
385 "wglGetCurrentDC",
386 "wglGetDefaultProcAddress",
387 "wglGetLayerPaletteEntries",
388 "wglGetPixelFormat",
389 "wglGetProcAddress",
390 "wglMakeCurrent",
391 "wglRealizeLayerPalette",
392 "wglSetLayerPaletteEntries",
393 "wglSetPixelFormat",
394 "wglShareLists",
395 "wglSwapBuffers",
396 "wglSwapLayerBuffers",
397 "wglSwapMultipleBuffers",
398 "wglUseFontBitmapsA",
399 "wglUseFontBitmapsW",
400 "wglUseFontOutlinesA",
401 "wglUseFontOutlinesW",
José Fonseca68ec4122011-02-20 11:25:25 +0000402
403 "glXGetProcAddressARB",
404 "glXGetProcAddress",
José Fonsecac6b49ce2010-11-28 16:43:04 +0000405])
406
407
408class Dispatcher:
409
José Fonseca68ec4122011-02-20 11:25:25 +0000410 def header(self):
411 pass
412 #print 'typedef void (*__PROC)(void);'
413 #print
414 #print 'static __PROC __getPublicProcAddress(const char *name);'
415 #print 'static __PROC __getPrivateProcAddress(const char *name);'
416 #print
417
418 def dispatch_api(self, api):
419 for function in api.functions:
420 dispatcher.dispatch_function(function)
421
422 print '#ifdef RETRACE'
423 for function in api.functions:
424 if not self.is_public_function(function):
425 print '#define %s __%s' % (function.name, function.name)
426 print '#endif /* RETRACE */'
427 print
428
José Fonsecac6b49ce2010-11-28 16:43:04 +0000429 def function_pointer_type(self, function):
430 return '__PFN' + function.name.upper()
431
432 def function_pointer_value(self, function):
433 return '__' + function.name + '_ptr'
434
435 def dispatch_function(self, function):
José Fonseca68ec4122011-02-20 11:25:25 +0000436 if self.is_public_function(function):
437 print '#ifndef RETRACE'
438 print
José Fonsecac6b49ce2010-11-28 16:43:04 +0000439 ptype = self.function_pointer_type(function)
440 pvalue = self.function_pointer_value(function)
441 print 'typedef ' + function.prototype('* %s' % ptype) + ';'
442 print 'static %s %s = NULL;' % (ptype, pvalue)
443 print
444 print 'static inline ' + function.prototype('__' + function.name) + ' {'
445 if function.type is stdapi.Void:
446 ret = ''
447 else:
448 ret = 'return '
449 self.get_true_pointer(function)
450 pvalue = self.function_pointer_value(function)
451 print ' %s%s(%s);' % (ret, pvalue, ', '.join([str(arg.name) for arg in function.args]))
452 print '}'
453 print
José Fonseca68ec4122011-02-20 11:25:25 +0000454 if self.is_public_function(function):
455 print '#endif /* !RETRACE */'
456 print
457
458 def is_public_function(self, function):
459 return True
José Fonsecac6b49ce2010-11-28 16:43:04 +0000460
461 def get_true_pointer(self, function):
462 ptype = self.function_pointer_type(function)
463 pvalue = self.function_pointer_value(function)
José Fonseca68ec4122011-02-20 11:25:25 +0000464 if self.is_public_function(function):
465 get_proc_address = '__getPublicProcAddress'
José Fonsecac6b49ce2010-11-28 16:43:04 +0000466 else:
José Fonseca68ec4122011-02-20 11:25:25 +0000467 get_proc_address = '__getPrivateProcAddress'
José Fonsecac6b49ce2010-11-28 16:43:04 +0000468 print ' if (!%s) {' % (pvalue,)
469 print ' %s = (%s)%s("%s");' % (pvalue, ptype, get_proc_address, function.name)
470 print ' if (!%s) {' % (pvalue,)
471 self.fail_function(function)
472 print ' }'
473 print ' }'
474
475 def fail_function(self, function):
José Fonseca68ec4122011-02-20 11:25:25 +0000476 print ' OS::DebugMessage("error: unavailable function \\"%s\\"\\n");' % function.name
José Fonsecac6b49ce2010-11-28 16:43:04 +0000477 if function.fail is not None:
478 if function.type is stdapi.Void:
479 assert function.fail == ''
480 print ' return;'
481 else:
482 assert function.fail != ''
483 print ' return %s;' % function.fail
484 else:
José Fonseca68ec4122011-02-20 11:25:25 +0000485 print ' __abort();'
486
487
488class GlDispatcher(Dispatcher):
489
490 def header(self):
491 Dispatcher.header(self)
492 print '#ifdef RETRACE'
493 print '# ifdef WIN32'
494 print '# define __getPrivateProcAddress(name) wglGetProcAddress(name)'
495 print '# else'
496 print '# define __getPrivateProcAddress(name) glXGetProcAddressARB((const GLubyte *)(name))'
497 print '# endif'
498 print '# define __abort() OS::Abort()'
499 print '#else /* !RETRACE */'
500 print '# ifdef WIN32'
501 print '# define __getPrivateProcAddress(name) __wglGetProcAddress(name)'
502 print ' static inline PROC __stdcall __wglGetProcAddress(const char * lpszProc);'
503 print '# else'
504 print '# define __getPublicProcAddress(name) dlsym(RTLD_NEXT, name)'
505 print '# define __getPrivateProcAddress(name) __glXGetProcAddressARB((const GLubyte *)(name))'
506 print ' static inline __GLXextFuncPtr __glXGetProcAddressARB(const GLubyte * procName);'
507 print '# endif'
508 print '# define __abort() Trace::Abort()'
509 print '#endif /* !RETRACE */'
510 print
511
512 def is_public_function(self, function):
513 return function.name in public_symbols
José Fonsecac6b49ce2010-11-28 16:43:04 +0000514
515
516if __name__ == '__main__':
517 print
518 print '#include "glimports.hpp"'
519 print '#include "os.hpp"'
520 print
José Fonsecac6b49ce2010-11-28 16:43:04 +0000521 print
José Fonseca68ec4122011-02-20 11:25:25 +0000522 dispatcher = GlDispatcher()
523 dispatcher.header()
524 print '#ifdef WIN32'
525 print
526 dispatcher.dispatch_api(wglapi)
527 print '#else /* !WIN32 */'
528 print
529 dispatcher.dispatch_api(glxapi)
530 print '#endif /* !WIN32 */'
531 print
532 dispatcher.dispatch_api(glapi)
José Fonsecac6b49ce2010-11-28 16:43:04 +0000533 print
534 if 0:
535 print '''
536
537#ifdef WIN32
538
539static HINSTANCE g_hDll = NULL;
540
541static __PROC
542__getProcAddress(const char *name)
543{
544 if (!g_hDll) {
545 char szDll[MAX_PATH] = {0};
546
547 if (!GetSystemDirectoryA(szDll, MAX_PATH)) {
548 return NULL;
549 }
550
551 strcat(szDll, "\\\\opengl32.dll");
552
553 g_hDll = LoadLibraryA(szDll);
554 if (!g_hDll) {
555 return NULL;
556 }
557 }
558
559 return GetProcAddress(g_hDll, name);
560}
561
José Fonsecac6b49ce2010-11-28 16:43:04 +0000562#else
563
564static void g_module = NULL;
565
566static __PROC
567__getProcAddress(const char *name)
568{
569 if (!g_module) {
570 g_module = dlopen("libGL.so", RTLD_LAZY);
571 if (!g_module) {
572 return NULL;
573 }
574 }
575
576 return (__PROC)dlsym(g_module, name);
577}
578
579static inline __PROC
580__glGetProcAddress(const char *name) {
José Fonseca68ec4122011-02-20 11:25:25 +0000581 return __glXGetProcAddressARB(name);
José Fonsecac6b49ce2010-11-28 16:43:04 +0000582}
583
584#endif
585
586 '''