d5ffb9286ef69cbff6a144c07d364d1497f9bb6f
[mesa.git] / src / mapi / glapi / gen / gl_procs.py
1 #!/usr/bin/env python
2
3 # (C) Copyright IBM Corporation 2004, 2005
4 # All Rights Reserved.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # on the rights to use, copy, modify, merge, publish, distribute, sub
10 # license, and/or sell copies of the Software, and to permit persons to whom
11 # the Software is furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
15 # Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 # IN THE SOFTWARE.
24 #
25 # Authors:
26 # Ian Romanick <idr@us.ibm.com>
27
28 import sys
29 import getopt
30
31 import license
32 import gl_XML
33 import glX_XML
34
35
36 class PrintGlProcs(gl_XML.gl_print_base):
37 def __init__(self, es=False):
38 gl_XML.gl_print_base.__init__(self)
39
40 self.es = es
41 self.name = "gl_procs.py (from Mesa)"
42 self.license = license.bsd_license_template % ( \
43 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
44 (C) Copyright IBM Corporation 2004, 2006""", "BRIAN PAUL, IBM")
45
46 def printRealHeader(self):
47 print """
48 /* This file is only included by glapi.c and is used for
49 * the GetProcAddress() function
50 */
51
52 typedef struct {
53 GLint Name_offset;
54 #if defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING)
55 _glapi_proc Address;
56 #endif
57 GLuint Offset;
58 } glprocs_table_t;
59
60 #if !defined(NEED_FUNCTION_POINTER) && !defined(GLX_INDIRECT_RENDERING)
61 # define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , o }
62 #elif defined(NEED_FUNCTION_POINTER) && !defined(GLX_INDIRECT_RENDERING)
63 # define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f1 , o }
64 #elif defined(NEED_FUNCTION_POINTER) && defined(GLX_INDIRECT_RENDERING)
65 # define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f2 , o }
66 #elif !defined(NEED_FUNCTION_POINTER) && defined(GLX_INDIRECT_RENDERING)
67 # define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f3 , o }
68 #endif
69
70 """
71 return
72
73 def printRealFooter(self):
74 print ''
75 print '#undef NAME_FUNC_OFFSET'
76 return
77
78 def printFunctionString(self, name):
79 print ' "gl%s\\0"' % (name)
80
81 def printBody(self, api):
82 print ''
83 print 'static const char gl_string_table[] ='
84
85 base_offset = 0
86 table = []
87 for func in api.functionIterateByOffset():
88 name = func.dispatch_name()
89 self.printFunctionString(func.name)
90 table.append((base_offset, "gl" + name, "gl" + name, "NULL", func.offset))
91
92 # The length of the function's name, plus 2 for "gl",
93 # plus 1 for the NUL.
94
95 base_offset += len(func.name) + 3
96
97
98 for func in api.functionIterateByOffset():
99 for n in func.entry_points:
100 if n != func.name:
101 name = func.dispatch_name()
102 self.printFunctionString( n )
103
104 if func.has_different_protocol(n):
105 alt_name = "gl" + func.static_glx_name(n)
106 table.append((base_offset, "gl" + name, alt_name, alt_name, func.offset))
107 else:
108 table.append((base_offset, "gl" + name, "gl" + name, "NULL", func.offset))
109
110 base_offset += len(n) + 3
111
112
113 print ' ;'
114 print ''
115 print ''
116 print "#ifdef USE_MGL_NAMESPACE"
117 for func in api.functionIterateByOffset():
118 for n in func.entry_points:
119 if (not func.is_static_entry_point(func.name)) or (func.has_different_protocol(n) and not func.is_static_entry_point(n)):
120 print '#define gl_dispatch_stub_%u mgl_dispatch_stub_%u' % (func.offset, func.offset)
121 break
122 print "#endif /* USE_MGL_NAMESPACE */"
123 print ''
124 print ''
125 print '#if defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING)'
126 for func in api.functionIterateByOffset():
127 for n in func.entry_points:
128 if (not func.is_static_entry_point(func.name)) or (func.has_different_protocol(n) and not func.is_static_entry_point(n)):
129 print '%s GLAPIENTRY gl_dispatch_stub_%u(%s);' % (func.return_type, func.offset, func.get_parameter_string())
130 break
131
132 if self.es:
133 categories = {}
134 for func in api.functionIterateByOffset():
135 for n in func.entry_points:
136 cat, num = api.get_category_for_name(n)
137 if (cat.startswith("es") or cat.startswith("GL_OES")):
138 if not categories.has_key(cat):
139 categories[cat] = []
140 proto = 'GLAPI %s GLAPIENTRY %s(%s);' \
141 % (func.return_type, "gl" + n, func.get_parameter_string(n))
142 categories[cat].append(proto)
143 if categories:
144 print ''
145 print '/* OpenGL ES specific prototypes */'
146 print ''
147 keys = categories.keys()
148 keys.sort()
149 for key in keys:
150 print '/* category %s */' % key
151 print "\n".join(categories[key])
152 print ''
153
154 print '#endif /* defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING) */'
155
156 print ''
157 print 'static const glprocs_table_t static_functions[] = {'
158
159 for info in table:
160 print ' NAME_FUNC_OFFSET(%5u, %s, %s, %s, %d),' % info
161
162 print ' NAME_FUNC_OFFSET(-1, NULL, NULL, NULL, 0)'
163 print '};'
164 return
165
166
167 def show_usage():
168 print "Usage: %s [-f input_file_name] [-c]" % sys.argv[0]
169 print "-c Enable compatibility with OpenGL ES."
170 sys.exit(1)
171
172
173 def main():
174 """Main function."""
175 file_name = "gl_API.xml"
176
177 try:
178 args, _ = getopt.getopt(sys.argv[1:], "f:c")
179 except Exception:
180 show_usage()
181
182 es = False
183 for arg, val in args:
184 if arg == "-f":
185 file_name = val
186 elif arg == "-c":
187 es = True
188
189 api = gl_XML.parse_GL_API(file_name, glX_XML.glx_item_factory())
190 printer = PrintGlProcs(es)
191 printer.Print(api)
192
193
194 if __name__ == '__main__':
195 main()