mesa: Move api_exec_es*.c into mesa/main
[mesa.git] / src / mesa / main / es_generator.py
1 #*************************************************************************
2 # Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
3 # All Rights Reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 # TUNGSTEN GRAPHICS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20 # OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 #*************************************************************************
23
24
25 import sys, os
26 import APIspecutil as apiutil
27
28 # These dictionary entries are used for automatic conversion.
29 # The string will be used as a format string with the conversion
30 # variable.
31 Converters = {
32 'GLfloat': {
33 'GLdouble': "(GLdouble) (%s)",
34 'GLfixed' : "(GLint) (%s * 65536)",
35 },
36 'GLfixed': {
37 'GLfloat': "(GLfloat) (%s / 65536.0f)",
38 'GLdouble': "(GLdouble) (%s / 65536.0)",
39 },
40 'GLdouble': {
41 'GLfloat': "(GLfloat) (%s)",
42 'GLfixed': "(GLfixed) (%s * 65536)",
43 },
44 'GLclampf': {
45 'GLclampd': "(GLclampd) (%s)",
46 'GLclampx': "(GLclampx) (%s * 65536)",
47 },
48 'GLclampx': {
49 'GLclampf': "(GLclampf) (%s / 65536.0f)",
50 'GLclampd': "(GLclampd) (%s / 65536.0)",
51 },
52 'GLubyte': {
53 'GLfloat': "(GLfloat) (%s / 255.0f)",
54 },
55 }
56
57 def GetBaseType(type):
58 typeTokens = type.split(' ')
59 baseType = None
60 typeModifiers = []
61 for t in typeTokens:
62 if t in ['const', '*']:
63 typeModifiers.append(t)
64 else:
65 baseType = t
66 return (baseType, typeModifiers)
67
68 def ConvertValue(value, fromType, toType):
69 """Returns a string that represents the given parameter string,
70 type-converted if necessary."""
71
72 if not Converters.has_key(fromType):
73 print >> sys.stderr, "No base converter for type '%s' found. Ignoring." % fromType
74 return value
75
76 if not Converters[fromType].has_key(toType):
77 print >> sys.stderr, "No converter found for type '%s' to type '%s'. Ignoring." % (fromType, toType)
78 return value
79
80 # This part is simple. Return the proper conversion.
81 conversionString = Converters[fromType][toType]
82 return conversionString % value
83
84 FormatStrings = {
85 'GLenum' : '0x%x',
86 'GLfloat' : '%f',
87 'GLint' : '%d',
88 'GLbitfield' : '0x%x',
89 }
90 def GetFormatString(type):
91 if FormatStrings.has_key(type):
92 return FormatStrings[type]
93 else:
94 return None
95
96
97 ######################################################################
98 # Version-specific values to be used in the main script
99 # header: which header file to include
100 # api: what text specifies an API-level function
101 VersionSpecificValues = {
102 'GLES1.1' : {
103 'description' : 'GLES1.1 functions',
104 'header' : 'GLES/gl.h',
105 'extheader' : 'GLES/glext.h',
106 'shortname' : 'es1'
107 },
108 'GLES2.0': {
109 'description' : 'GLES2.0 functions',
110 'header' : 'GLES2/gl2.h',
111 'extheader' : 'GLES2/gl2ext.h',
112 'shortname' : 'es2'
113 }
114 }
115
116
117 ######################################################################
118 # Main code for the script begins here.
119
120 # Get the name of the program (without the directory part) for use in
121 # error messages.
122 program = os.path.basename(sys.argv[0])
123
124 # Set default values
125 verbose = 0
126 functionList = "APIspec.xml"
127 version = "GLES1.1"
128
129 # Allow for command-line switches
130 import getopt, time
131 options = "hvV:S:"
132 try:
133 optlist, args = getopt.getopt(sys.argv[1:], options)
134 except getopt.GetoptError, message:
135 sys.stderr.write("%s: %s. Use -h for help.\n" % (program, message))
136 sys.exit(1)
137
138 for option, optarg in optlist:
139 if option == "-h":
140 sys.stderr.write("Usage: %s [-%s]\n" % (program, options))
141 sys.stderr.write("Parse an API specification file and generate wrapper functions for a given GLES version\n")
142 sys.stderr.write("-h gives help\n")
143 sys.stderr.write("-v is verbose\n")
144 sys.stderr.write("-V specifies GLES version to generate [%s]:\n" % version)
145 for key in VersionSpecificValues.keys():
146 sys.stderr.write(" %s - %s\n" % (key, VersionSpecificValues[key]['description']))
147 sys.stderr.write("-S specifies API specification file to use [%s]\n" % functionList)
148 sys.exit(1)
149 elif option == "-v":
150 verbose += 1
151 elif option == "-V":
152 version = optarg
153 elif option == "-S":
154 functionList = optarg
155
156 # Beyond switches, we support no further command-line arguments
157 if len(args) > 0:
158 sys.stderr.write("%s: only switch arguments are supported - use -h for help\n" % program)
159 sys.exit(1)
160
161 # If we don't have a valid version, abort.
162 if not VersionSpecificValues.has_key(version):
163 sys.stderr.write("%s: version '%s' is not valid - use -h for help\n" % (program, version))
164 sys.exit(1)
165
166 # Grab the version-specific items we need to use
167 versionHeader = VersionSpecificValues[version]['header']
168 versionExtHeader = VersionSpecificValues[version]['extheader']
169 shortname = VersionSpecificValues[version]['shortname']
170
171 # If we get to here, we're good to go. The "version" parameter
172 # directs GetDispatchedFunctions to only allow functions from
173 # that "category" (version in our parlance). This allows
174 # functions with different declarations in different categories
175 # to exist (glTexImage2D, for example, is different between
176 # GLES1 and GLES2).
177 keys = apiutil.GetAllFunctions(functionList, version)
178
179 allSpecials = apiutil.AllSpecials()
180
181 print """/* DO NOT EDIT *************************************************
182 * THIS FILE AUTOMATICALLY GENERATED BY THE %s SCRIPT
183 * API specification file: %s
184 * GLES version: %s
185 * date: %s
186 */
187 """ % (program, functionList, version, time.strftime("%Y-%m-%d %H:%M:%S"))
188
189 # The headers we choose are version-specific.
190 print """
191 #include "%s"
192 #include "%s"
193 """ % (versionHeader, versionExtHeader)
194
195 # Everyone needs these types.
196 print """
197 /* These types are needed for the Mesa veneer, but are not defined in
198 * the standard GLES headers.
199 */
200 typedef double GLdouble;
201 typedef double GLclampd;
202
203 /* This type is normally in glext.h, but needed here */
204 typedef char GLchar;
205
206 /* Mesa error handling requires these */
207 extern void *_mesa_get_current_context(void);
208 extern void _mesa_error(void *ctx, GLenum error, const char *fmtString, ... );
209
210 #include "main/compiler.h"
211 #include "main/api_exec.h"
212 #include "main/remap.h"
213
214 #ifdef IN_DRI_DRIVER
215 #define _GLAPI_USE_REMAP_TABLE
216 #endif
217
218 #include "es/glapi/glapi-%s/glapi/glapitable.h"
219 #include "es/glapi/glapi-%s/glapi/glapioffsets.h"
220 #include "es/glapi/glapi-%s/glapi/glapidispatch.h"
221
222 #if FEATURE_remap_table
223
224 #define need_MESA_remap_table
225
226 #include "es/glapi/glapi-%s/main/remap_helper.h"
227
228 void
229 _mesa_init_remap_table_%s(void)
230 {
231 _mesa_do_init_remap_table(_mesa_function_pool,
232 driDispatchRemapTable_size,
233 MESA_remap_table_functions);
234 }
235
236 void
237 _mesa_map_static_functions_%s(void)
238 {
239 }
240
241 #endif
242
243 typedef void (*_glapi_proc)(void); /* generic function pointer */
244 """ % (shortname, shortname, shortname, shortname, shortname, shortname);
245
246 # Finally we get to the all-important functions
247 print """/*************************************************************
248 * Generated functions begin here
249 */
250 """
251 for funcName in keys:
252 if verbose > 0: sys.stderr.write("%s: processing function %s\n" % (program, funcName))
253
254 # start figuring out what this function will look like.
255 returnType = apiutil.ReturnType(funcName)
256 props = apiutil.Properties(funcName)
257 params = apiutil.Parameters(funcName)
258 declarationString = apiutil.MakeDeclarationString(params)
259
260 # In case of error, a function may have to return. Make
261 # sure we have valid return values in this case.
262 if returnType == "void":
263 errorReturn = "return"
264 elif returnType == "GLboolean":
265 errorReturn = "return GL_FALSE"
266 else:
267 errorReturn = "return (%s) 0" % returnType
268
269 # These are the output of this large calculation block.
270 # passthroughDeclarationString: a typed set of parameters that
271 # will be used to create the "extern" reference for the
272 # underlying Mesa or support function. Note that as generated
273 # these have an extra ", " at the beginning, which will be
274 # removed before use.
275 #
276 # passthroughDeclarationString: an untyped list of parameters
277 # that will be used to call the underlying Mesa or support
278 # function (including references to converted parameters).
279 # This will also be generated with an extra ", " at the
280 # beginning, which will be removed before use.
281 #
282 # variables: C code to create any local variables determined to
283 # be necessary.
284 # conversionCodeOutgoing: C code to convert application parameters
285 # to a necessary type before calling the underlying support code.
286 # May be empty if no conversion is required.
287 # conversionCodeIncoming: C code to do the converse: convert
288 # values returned by underlying Mesa code to the types needed
289 # by the application.
290 # Note that *either* the conversionCodeIncoming will be used (for
291 # generated query functions), *or* the conversionCodeOutgoing will
292 # be used (for generated non-query functions), never both.
293 passthroughFuncName = ""
294 passthroughDeclarationString = ""
295 passthroughCallString = ""
296 variables = []
297 conversionCodeOutgoing = []
298 conversionCodeIncoming = []
299 switchCode = []
300
301 # Calculate the name of the underlying support function to call.
302 # By default, the passthrough function is named _mesa_<funcName>.
303 # We're allowed to override the prefix and/or the function name
304 # for each function record, though. The "ConversionFunction"
305 # utility is poorly named, BTW...
306 if funcName in allSpecials:
307 # perform checks and pass through
308 funcPrefix = "_check_"
309 aliasprefix = "_es_"
310 else:
311 funcPrefix = "_es_"
312 aliasprefix = apiutil.AliasPrefix(funcName)
313 alias = apiutil.ConversionFunction(funcName)
314 if not alias:
315 # There may still be a Mesa alias for the function
316 if apiutil.Alias(funcName):
317 passthroughFuncName = "%s%s" % (aliasprefix, apiutil.Alias(funcName))
318 else:
319 passthroughFuncName = "%s%s" % (aliasprefix, funcName)
320 else: # a specific alias is provided
321 passthroughFuncName = "%s%s" % (aliasprefix, alias)
322
323 # Look at every parameter: each one may have only specific
324 # allowed values, or dependent parameters to check, or
325 # variant-sized vector arrays to calculate
326 for (paramName, paramType, paramMaxVecSize, paramConvertToType, paramValidValues, paramValueConversion) in params:
327 # We'll need this below if we're doing conversions
328 (paramBaseType, paramTypeModifiers) = GetBaseType(paramType)
329
330 # Conversion management.
331 # We'll handle three cases, easiest to hardest: a parameter
332 # that doesn't require conversion, a scalar parameter that
333 # requires conversion, and a vector parameter that requires
334 # conversion.
335 if paramConvertToType == None:
336 # Unconverted parameters are easy, whether they're vector
337 # or scalar - just add them to the call list. No conversions
338 # or anything to worry about.
339 passthroughDeclarationString += ", %s %s" % (paramType, paramName)
340 passthroughCallString += ", %s" % paramName
341
342 elif paramMaxVecSize == 0: # a scalar parameter that needs conversion
343 # A scalar to hold a converted parameter
344 variables.append(" %s converted_%s;" % (paramConvertToType, paramName))
345
346 # Outgoing conversion depends on whether we have to conditionally
347 # perform value conversion.
348 if paramValueConversion == "none":
349 conversionCodeOutgoing.append(" converted_%s = (%s) %s;" % (paramName, paramConvertToType, paramName))
350 elif paramValueConversion == "some":
351 # We'll need a conditional variable to keep track of
352 # whether we're converting values or not.
353 if (" int convert_%s_value = 1;" % paramName) not in variables:
354 variables.append(" int convert_%s_value = 1;" % paramName)
355
356 # Write code based on that conditional.
357 conversionCodeOutgoing.append(" if (convert_%s_value) {" % paramName)
358 conversionCodeOutgoing.append(" converted_%s = %s;" % (paramName, ConvertValue(paramName, paramBaseType, paramConvertToType)))
359 conversionCodeOutgoing.append(" } else {")
360 conversionCodeOutgoing.append(" converted_%s = (%s) %s;" % (paramName, paramConvertToType, paramName))
361 conversionCodeOutgoing.append(" }")
362 else: # paramValueConversion == "all"
363 conversionCodeOutgoing.append(" converted_%s = %s;" % (paramName, ConvertValue(paramName, paramBaseType, paramConvertToType)))
364
365 # Note that there can be no incoming conversion for a
366 # scalar parameter; changing the scalar will only change
367 # the local value, and won't ultimately change anything
368 # that passes back to the application.
369
370 # Call strings. The unusual " ".join() call will join the
371 # array of parameter modifiers with spaces as separators.
372 passthroughDeclarationString += ", %s %s %s" % (paramConvertToType, " ".join(paramTypeModifiers), paramName)
373 passthroughCallString += ", converted_%s" % paramName
374
375 else: # a vector parameter that needs conversion
376 # We'll need an index variable for conversions
377 if " register unsigned int i;" not in variables:
378 variables.append(" register unsigned int i;")
379
380 # This variable will hold the (possibly variant) size of
381 # this array needing conversion. By default, we'll set
382 # it to the maximal size (which is correct for functions
383 # with a constant-sized vector parameter); for true
384 # variant arrays, we'll modify it with other code.
385 variables.append(" unsigned int n_%s = %d;" % (paramName, paramMaxVecSize))
386
387 # This array will hold the actual converted values.
388 variables.append(" %s converted_%s[%d];" % (paramConvertToType, paramName, paramMaxVecSize))
389
390 # Again, we choose the conversion code based on whether we
391 # have to always convert values, never convert values, or
392 # conditionally convert values.
393 if paramValueConversion == "none":
394 conversionCodeOutgoing.append(" for (i = 0; i < n_%s; i++) {" % paramName)
395 conversionCodeOutgoing.append(" converted_%s[i] = (%s) %s[i];" % (paramName, paramConvertToType, paramName))
396 conversionCodeOutgoing.append(" }")
397 elif paramValueConversion == "some":
398 # We'll need a conditional variable to keep track of
399 # whether we're converting values or not.
400 if (" int convert_%s_value = 1;" % paramName) not in variables:
401 variables.append(" int convert_%s_value = 1;" % paramName)
402 # Write code based on that conditional.
403 conversionCodeOutgoing.append(" if (convert_%s_value) {" % paramName)
404 conversionCodeOutgoing.append(" for (i = 0; i < n_%s; i++) {" % paramName)
405 conversionCodeOutgoing.append(" converted_%s[i] = %s;" % (paramName, ConvertValue("%s[i]" % paramName, paramBaseType, paramConvertToType)))
406 conversionCodeOutgoing.append(" }")
407 conversionCodeOutgoing.append(" } else {")
408 conversionCodeOutgoing.append(" for (i = 0; i < n_%s; i++) {" % paramName)
409 conversionCodeOutgoing.append(" converted_%s[i] = (%s) %s[i];" % (paramName, paramConvertToType, paramName))
410 conversionCodeOutgoing.append(" }")
411 conversionCodeOutgoing.append(" }")
412 else: # paramValueConversion == "all"
413 conversionCodeOutgoing.append(" for (i = 0; i < n_%s; i++) {" % paramName)
414 conversionCodeOutgoing.append(" converted_%s[i] = %s;" % (paramName, ConvertValue("%s[i]" % paramName, paramBaseType, paramConvertToType)))
415
416 conversionCodeOutgoing.append(" }")
417
418 # If instead we need an incoming conversion (i.e. results
419 # from Mesa have to be converted before handing back
420 # to the application), this is it. Fortunately, we don't
421 # have to worry about conditional value conversion - the
422 # functions that do (e.g. glGetFixedv()) are handled
423 # specially, outside this code generation.
424 #
425 # Whether we use incoming conversion or outgoing conversion
426 # is determined later - we only ever use one or the other.
427
428 if paramValueConversion == "none":
429 conversionCodeIncoming.append(" for (i = 0; i < n_%s; i++) {" % paramName)
430 conversionCodeIncoming.append(" %s[i] = (%s) converted_%s[i];" % (paramName, paramConvertToType, paramName))
431 conversionCodeIncoming.append(" }")
432 elif paramValueConversion == "some":
433 # We'll need a conditional variable to keep track of
434 # whether we're converting values or not.
435 if (" int convert_%s_value = 1;" % paramName) not in variables:
436 variables.append(" int convert_%s_value = 1;" % paramName)
437
438 # Write code based on that conditional.
439 conversionCodeIncoming.append(" if (convert_%s_value) {" % paramName)
440 conversionCodeIncoming.append(" for (i = 0; i < n_%s; i++) {" % paramName)
441 conversionCodeIncoming.append(" %s[i] = %s;" % (paramName, ConvertValue("converted_%s[i]" % paramName, paramConvertToType, paramBaseType)))
442 conversionCodeIncoming.append(" }")
443 conversionCodeIncoming.append(" } else {")
444 conversionCodeIncoming.append(" for (i = 0; i < n_%s; i++) {" % paramName)
445 conversionCodeIncoming.append(" %s[i] = (%s) converted_%s[i];" % (paramName, paramBaseType, paramName))
446 conversionCodeIncoming.append(" }")
447 conversionCodeIncoming.append(" }")
448 else: # paramValueConversion == "all"
449 conversionCodeIncoming.append(" for (i = 0; i < n_%s; i++) {" % paramName)
450 conversionCodeIncoming.append(" %s[i] = %s;" % (paramName, ConvertValue("converted_%s[i]" % paramName, paramConvertToType, paramBaseType)))
451 conversionCodeIncoming.append(" }")
452
453 # Call strings. The unusual " ".join() call will join the
454 # array of parameter modifiers with spaces as separators.
455 passthroughDeclarationString += ", %s %s %s" % (paramConvertToType, " ".join(paramTypeModifiers), paramName)
456 passthroughCallString += ", converted_%s" % paramName
457
458 # endif conversion management
459
460 # Parameter checking. If the parameter has a specific list of
461 # valid values, we have to make sure that the passed-in values
462 # match these, or we make an error.
463 if len(paramValidValues) > 0:
464 # We're about to make a big switch statement with an
465 # error at the end. By default, the error is GL_INVALID_ENUM,
466 # unless we find a "case" statement in the middle with a
467 # non-GLenum value.
468 errorDefaultCase = "GL_INVALID_ENUM"
469
470 # This parameter has specific valid values. Make a big
471 # switch statement to handle it. Note that the original
472 # parameters are always what is checked, not the
473 # converted parameters.
474 switchCode.append(" switch(%s) {" % paramName)
475
476 for valueIndex in range(len(paramValidValues)):
477 (paramValue, dependentVecSize, dependentParamName, dependentValidValues, errorCode, valueConvert) = paramValidValues[valueIndex]
478
479 # We're going to need information on the dependent param
480 # as well.
481 if dependentParamName:
482 depParamIndex = apiutil.FindParamIndex(params, dependentParamName)
483 if depParamIndex == None:
484 sys.stderr.write("%s: can't find dependent param '%s' for function '%s'\n" % (program, dependentParamName, funcName))
485
486 (depParamName, depParamType, depParamMaxVecSize, depParamConvertToType, depParamValidValues, depParamValueConversion) = params[depParamIndex]
487 else:
488 (depParamName, depParamType, depParamMaxVecSize, depParamConvertToType, depParamValidValues, depParamValueConversion) = (None, None, None, None, [], None)
489
490 # This is a sneaky trick. It's valid syntax for a parameter
491 # that is *not* going to be converted to be declared
492 # with a dependent vector size; but in this case, the
493 # dependent vector size is unused and unnecessary.
494 # So check for this and ignore the dependent vector size
495 # if the parameter is not going to be converted.
496 if depParamConvertToType:
497 usedDependentVecSize = dependentVecSize
498 else:
499 usedDependentVecSize = None
500
501 # We'll peek ahead at the next parameter, to see whether
502 # we can combine cases
503 if valueIndex + 1 < len(paramValidValues) :
504 (nextParamValue, nextDependentVecSize, nextDependentParamName, nextDependentValidValues, nextErrorCode, nextValueConvert) = paramValidValues[valueIndex + 1]
505 if depParamConvertToType:
506 usedNextDependentVecSize = nextDependentVecSize
507 else:
508 usedNextDependentVecSize = None
509
510 # Create a case for this value. As a mnemonic,
511 # if we have a dependent vector size that we're ignoring,
512 # add it as a comment.
513 if usedDependentVecSize == None and dependentVecSize != None:
514 switchCode.append(" case %s: /* size %s */" % (paramValue, dependentVecSize))
515 else:
516 switchCode.append(" case %s:" % paramValue)
517
518 # If this is not a GLenum case, then switch our error
519 # if no value is matched to be GL_INVALID_VALUE instead
520 # of GL_INVALID_ENUM. (Yes, this does get confused
521 # if there are both values and GLenums in the same
522 # switch statement, which shouldn't happen.)
523 if paramValue[0:3] != "GL_":
524 errorDefaultCase = "GL_INVALID_VALUE"
525
526 # If all the remaining parameters are identical to the
527 # next set, then we're done - we'll just create the
528 # official code on the next pass through, and the two
529 # cases will share the code.
530 if valueIndex + 1 < len(paramValidValues) and usedDependentVecSize == usedNextDependentVecSize and dependentParamName == nextDependentParamName and dependentValidValues == nextDependentValidValues and errorCode == nextErrorCode and valueConvert == nextValueConvert:
531 continue
532
533 # Otherwise, we'll have to generate code for this case.
534 # Start off with a check: if there is a dependent parameter,
535 # and a list of valid values for that parameter, we need
536 # to generate an error if something other than one
537 # of those values is passed.
538 if len(dependentValidValues) > 0:
539 conditional=""
540
541 # If the parameter being checked is actually an array,
542 # check only its first element.
543 if depParamMaxVecSize == 0:
544 valueToCheck = dependentParamName
545 else:
546 valueToCheck = "%s[0]" % dependentParamName
547
548 for v in dependentValidValues:
549 conditional += " && %s != %s" % (valueToCheck, v)
550 switchCode.append(" if (%s) {" % conditional[4:])
551 if errorCode == None:
552 errorCode = "GL_INVALID_ENUM"
553 switchCode.append(' _mesa_error(_mesa_get_current_context(), %s, "gl%s(%s=0x%s)", %s);' % (errorCode, funcName, paramName, "%x", paramName))
554 switchCode.append(" %s;" % errorReturn)
555 switchCode.append(" }")
556 # endif there are dependent valid values
557
558 # The dependent parameter may require conditional
559 # value conversion. If it does, and we don't want
560 # to convert values, we'll have to generate code for that
561 if depParamValueConversion == "some" and valueConvert == "noconvert":
562 switchCode.append(" convert_%s_value = 0;" % dependentParamName)
563
564 # If there's a dependent vector size for this parameter
565 # that we're actually going to use (i.e. we need conversion),
566 # mark it.
567 if usedDependentVecSize:
568 switchCode.append(" n_%s = %s;" % (dependentParamName, dependentVecSize))
569
570 # In all cases, break out of the switch if any valid
571 # value is found.
572 switchCode.append(" break;")
573
574
575 # Need a default case to catch all the other, invalid
576 # parameter values. These will all generate errors.
577 switchCode.append(" default:")
578 if errorCode == None:
579 errorCode = "GL_INVALID_ENUM"
580 formatString = GetFormatString(paramType)
581 if formatString == None:
582 switchCode.append(' _mesa_error(_mesa_get_current_context(), %s, "gl%s(%s)");' % (errorCode, funcName, paramName))
583 else:
584 switchCode.append(' _mesa_error(_mesa_get_current_context(), %s, "gl%s(%s=%s)", %s);' % (errorCode, funcName, paramName, formatString, paramName))
585 switchCode.append(" %s;" % errorReturn)
586
587 # End of our switch code.
588 switchCode.append(" }")
589
590 # endfor every recognized parameter value
591
592 # endfor every param
593
594 # Here, the passthroughDeclarationString and passthroughCallString
595 # are complete; remove the extra ", " at the front of each.
596 passthroughDeclarationString = passthroughDeclarationString[2:]
597 passthroughCallString = passthroughCallString[2:]
598
599 # The Mesa functions are scattered across all the Mesa
600 # header files. The easiest way to manage declarations
601 # is to create them ourselves.
602 if funcName in allSpecials:
603 print "/* this function is special and is defined elsewhere */"
604 print "extern %s GLAPIENTRY %s(%s);" % (returnType, passthroughFuncName, passthroughDeclarationString)
605
606 # A function may be a core function (i.e. it exists in
607 # the core specification), a core addition (extension
608 # functions added officially to the core), a required
609 # extension (usually an extension for an earlier version
610 # that has been officially adopted), or an optional extension.
611 #
612 # Core functions have a simple category (e.g. "GLES1.1");
613 # we generate only a simple callback for them.
614 #
615 # Core additions have two category listings, one simple
616 # and one compound (e.g. ["GLES1.1", "GLES1.1:OES_fixed_point"]).
617 # We generate the core function, and also an extension function.
618 #
619 # Required extensions and implemented optional extensions
620 # have a single compound category "GLES1.1:OES_point_size_array".
621 # For these we generate just the extension function.
622 for categorySpec in apiutil.Categories(funcName):
623 compoundCategory = categorySpec.split(":")
624
625 # This category isn't for us, if the base category doesn't match
626 # our version
627 if compoundCategory[0] != version:
628 continue
629
630 # Otherwise, determine if we're writing code for a core
631 # function (no suffix) or an extension function.
632 if len(compoundCategory) == 1:
633 # This is a core function
634 extensionName = None
635 extensionSuffix = ""
636 else:
637 # This is an extension function. We'll need to append
638 # the extension suffix.
639 extensionName = compoundCategory[1]
640 extensionSuffix = extensionName.split("_")[0]
641 fullFuncName = funcPrefix + funcName + extensionSuffix
642
643 # Now the generated function. The text used to mark an API-level
644 # function, oddly, is version-specific.
645 if extensionName:
646 print "/* Extension %s */" % extensionName
647
648 if (not variables and
649 not switchCode and
650 not conversionCodeOutgoing and
651 not conversionCodeIncoming):
652 # pass through directly
653 print "#define %s %s" % (fullFuncName, passthroughFuncName)
654 print
655 continue
656
657 print "static %s %s(%s)" % (returnType, fullFuncName, declarationString)
658 print "{"
659
660 # Start printing our code pieces. Start with any local
661 # variables we need. This unusual syntax joins the
662 # lines in the variables[] array with the "\n" separator.
663 if len(variables) > 0:
664 print "\n".join(variables) + "\n"
665
666 # If there's any sort of parameter checking or variable
667 # array sizing, the switch code will contain it.
668 if len(switchCode) > 0:
669 print "\n".join(switchCode) + "\n"
670
671 # In the case of an outgoing conversion (i.e. parameters must
672 # be converted before calling the underlying Mesa function),
673 # use the appropriate code.
674 if "get" not in props and len(conversionCodeOutgoing) > 0:
675 print "\n".join(conversionCodeOutgoing) + "\n"
676
677 # Call the Mesa function. Note that there are very few functions
678 # that return a value (i.e. returnType is not "void"), and that
679 # none of them require incoming translation; so we're safe
680 # to generate code that directly returns in those cases,
681 # even though it's not completely independent.
682
683 if returnType == "void":
684 print " %s(%s);" % (passthroughFuncName, passthroughCallString)
685 else:
686 print " return %s(%s);" % (passthroughFuncName, passthroughCallString)
687
688 # If the function is one that returns values (i.e. "get" in props),
689 # it might return values of a different type than we need, that
690 # require conversion before passing back to the application.
691 if "get" in props and len(conversionCodeIncoming) > 0:
692 print "\n".join(conversionCodeIncoming)
693
694 # All done.
695 print "}"
696 print
697 # end for each category provided for a function
698
699 # end for each function
700
701 print """
702 struct _glapi_table *
703 _mesa_create_exec_table_%s(void)
704 {
705 struct _glapi_table *exec;
706 exec = _mesa_alloc_dispatch_table(sizeof *exec);
707 if (exec == NULL)
708 return NULL;
709
710 """ % shortname
711
712 for func in keys:
713 prefix = "_es_" if func not in allSpecials else "_check_"
714 for spec in apiutil.Categories(func):
715 ext = spec.split(":")
716 # version does not match
717 if ext.pop(0) != version:
718 continue
719 entry = func
720 if ext:
721 suffix = ext[0].split("_")[0]
722 entry += suffix
723 print " SET_%s(exec, %s%s);" % (entry, prefix, entry)
724 print ""
725 print " return exec;"
726 print "}"