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