3 # $Id: glapitemp.py,v 1.7 2004/10/02 22:47:48 brianp Exp $
5 # Mesa 3-D graphics library
8 # Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
10 # Permission is hereby granted, free of charge, to any person obtaining a
11 # copy of this software and associated documentation files (the "Software"),
12 # to deal in the Software without restriction, including without limitation
13 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 # and/or sell copies of the Software, and to permit persons to whom the
15 # Software is furnished to do so, subject to the following conditions:
17 # The above copyright notice and this permission notice shall be included
18 # in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 # BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
24 # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 # Generate the glapitemp.h file.
31 # gloffsets.py >glapitemp.h
34 # The apispec file must be in the current directory.
43 /* DO NOT EDIT! This file is generated by the glapitemp.py script. */
46 * This file is a template which generates the OpenGL API entry point
47 * functions. It should be included by a .c file which first defines
48 * the following macros:
49 * KEYWORD1 - usually nothing, but might be __declspec(dllexport) on Win32
50 * KEYWORD2 - usually nothing, but might be __stdcall on Win32
51 * NAME(n) - builds the final function name (usually add "gl" prefix)
52 * DISPATCH(func, args, msg) - code to do dispatch of named function.
53 * msg is a printf-style debug message.
54 * RETURN_DISPATCH(func, args, msg) - code to do dispatch with a return value
56 * Here's an example which generates the usual OpenGL functions:
59 * #define NAME(func) gl##func
60 * #define DISPATCH(func, args, msg) \\
61 * struct _glapi_table *dispatch = CurrentDispatch; \\
62 * (*dispatch->func) args
63 * #define RETURN DISPATCH(func, args, msg) \\
64 * struct _glapi_table *dispatch = CurrentDispatch; \\
65 * return (*dispatch->func) args
80 #error DISPATCH must be defined
83 #ifndef RETURN_DISPATCH
84 #error RETURN_DISPATCH must be defined
98 #undef RETURN_DISPATCH
99 #undef DISPATCH_TABLE_NAME
100 #undef UNUSED_TABLE_NAME
106 def MakeParamList(nameList
):
110 for name
in nameList
:
111 result
= result
+ name
113 result
= result
+ ', '
119 def Contains(haystack
, needle
):
120 if string
.find(haystack
, needle
) >= 0:
127 def MakePrintfString(funcName
, argTypeList
, argNameList
):
128 result
= '(F, "gl%s(' % (funcName
)
134 for argType
in argTypeList
:
137 if argType
== 'GLenum':
138 result
= result
+ '0x%x'
139 elif argType
in ['GLfloat', 'GLdouble', 'GLclampf', 'GLclampd']:
140 result
= result
+ '%f'
141 elif argType
in ['GLbyte', 'GLubyte', 'GLshort', 'GLushort', 'GLint', 'GLuint', 'GLboolean', 'GLsizei']:
142 result
= result
+ '%d'
144 result
= result
+ '%p'
146 if argType
[0:13] == 'const GLfloat' or argType
[0:14] == 'const GLdouble':
147 if Contains(funcName
, '2fv') or Contains(funcName
, '2dv'):
148 result
= result
+ ' /* %g, %g */'
150 elif Contains(funcName
, '3fv') or Contains(funcName
, '3dv'):
151 result
= result
+ ' /* %g, %g, %g */'
153 elif Contains(funcName
, '4fv') or Contains(funcName
, '4dv'):
154 result
= result
+ ' /* %g, %g, %g, %g */'
158 result
= result
+ ', '
162 result
= result
+ ');\\n"'
167 result
= result
+ ', '
168 for pname
in argNameList
:
170 result
= result
+ '(const void *) '
171 result
= result
+ pname
173 result
= result
+ ', ' + pname
+ '[0], ' + pname
+ '[1]'
175 result
= result
+ ', ' + pname
+ '[0], ' + pname
+ '[1], ' + pname
+ '[2]'
177 result
= result
+ ', ' + pname
+ '[0], ' + pname
+ '[1], ' + pname
+ '[2], ' + pname
+ '[3]'
179 result
= result
+ ', '
181 result
= result
+ ')'
190 def FindOffset(funcName
):
191 for (name
, alias
, offset
) in records
:
199 def EmitFunction(name
, returnType
, argTypeList
, argNameList
, alias
, offset
):
200 argList
= apiparser
.MakeArgList(argTypeList
, argNameList
)
201 parms
= MakeParamList(argNameList
)
202 printString
= MakePrintfString(name
, argTypeList
, argNameList
)
208 offset
= FindOffset(dispatchName
)
210 print 'KEYWORD1 %s KEYWORD2 NAME(%s)(%s)' % (returnType
, name
, argList
)
212 if returnType
== 'void':
213 print ' DISPATCH(%s, (%s), %s);' % (dispatchName
, parms
, printString
)
215 print ' RETURN_DISPATCH(%s, (%s), %s);' % (dispatchName
, parms
, printString
)
218 records
.append((name
, dispatchName
, offset
))
219 if not emittedFuncs
.has_key(offset
):
220 emittedFuncs
[offset
] = name
222 aliasedFuncs
.append(name
)
224 print '/* No dispatch for %s() */' % (name
)
228 def PrintInitDispatch():
230 #endif /* defined( NAME ) */
233 * This is how a dispatch table can be initialized with all the functions
234 * we generated above.
236 #ifdef DISPATCH_TABLE_NAME
239 #error TABLE_ENTRY must be defined
242 void *DISPATCH_TABLE_NAME[] = {"""
243 keys
= emittedFuncs
.keys()
246 print ' TABLE_ENTRY(%s),' % (emittedFuncs
[k
])
248 print ' /* A whole bunch of no-op functions. These might be called'
249 print ' * when someone tries to call a dynamically-registered'
250 print ' * extension function without a current rendering context.'
252 for i
in range(1, 100):
253 print ' TABLE_ENTRY(Unused),'
256 print '#endif /* DISPATCH_TABLE_NAME */'
262 def PrintAliasedTable():
265 * This is just used to silence compiler warnings.
266 * We list the functions which aren't otherwise used.
268 #ifdef UNUSED_TABLE_NAME
269 void *UNUSED_TABLE_NAME[] = {"""
270 for alias
in aliasedFuncs
:
271 print ' TABLE_ENTRY(%s),' % (alias
)
274 print '#endif /*UNUSED_TABLE_NAME*/'
281 apiparser
.ProcessSpecFile("APIspec", EmitFunction
)