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
28import license
29import gl_XML, glX_XML
30import sys, getopt
31
32class PrintGlProcs(gl_XML.gl_print_base):
33	def __init__(self, long_strings, es=False):
34		gl_XML.gl_print_base.__init__(self)
35
36		self.es = es
37		self.long_strings = long_strings
38		self.name = "gl_procs.py (from Mesa)"
39		self.license = license.bsd_license_template % ( \
40"""Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
41(C) Copyright IBM Corporation 2004, 2006""", "BRIAN PAUL, IBM")
42
43
44	def printRealHeader(self):
45		print """
46/* This file is only included by glapi.c and is used for
47 * the GetProcAddress() function
48 */
49
50typedef struct {
51    GLint Name_offset;
52#if defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING)
53    _glapi_proc Address;
54#endif
55    GLuint Offset;
56} glprocs_table_t;
57
58#if   !defined(NEED_FUNCTION_POINTER) && !defined(GLX_INDIRECT_RENDERING)
59#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , o }
60#elif  defined(NEED_FUNCTION_POINTER) && !defined(GLX_INDIRECT_RENDERING)
61#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f1 , o }
62#elif  defined(NEED_FUNCTION_POINTER) &&  defined(GLX_INDIRECT_RENDERING)
63#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f2 , o }
64#elif !defined(NEED_FUNCTION_POINTER) &&  defined(GLX_INDIRECT_RENDERING)
65#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f3 , o }
66#endif
67
68"""
69		return
70
71	def printRealFooter(self):
72		print ''
73		print '#undef NAME_FUNC_OFFSET'
74		return
75
76	def printFunctionString(self, name):
77		if self.long_strings:
78			print '    "gl%s\\0"' % (name)
79		else:
80			print "    'g','l',",
81			for c in name:
82				print "'%s'," % (c),
83
84			print "'\\0',"
85
86
87	def printBody(self, api):
88		print ''
89		if self.long_strings:
90			print 'static const char gl_string_table[] ='
91		else:
92			print 'static const char gl_string_table[] = {'
93
94		base_offset = 0
95		table = []
96		for func in api.functionIterateByOffset():
97			name = func.dispatch_name()
98			self.printFunctionString(func.name)
99			table.append((base_offset, "gl" + name, "gl" + name, "NULL", func.offset))
100
101			# The length of the function's name, plus 2 for "gl",
102			# plus 1 for the NUL.
103
104			base_offset += len(func.name) + 3
105
106
107		for func in api.functionIterateByOffset():
108			for n in func.entry_points:
109				if n != func.name:
110					name = func.dispatch_name()
111					self.printFunctionString( n )
112
113					if func.has_different_protocol(n):
114						alt_name = "gl" + func.static_glx_name(n)
115						table.append((base_offset, "gl" + name, alt_name, alt_name, func.offset))
116					else:
117						table.append((base_offset, "gl" + name, "gl" + name, "NULL", func.offset))
118
119					base_offset += len(n) + 3
120
121
122		if self.long_strings:
123			print '    ;'
124		else:
125			print '};'
126
127		print ''
128		print ''
129		print "#ifdef USE_MGL_NAMESPACE"
130		for func in api.functionIterateByOffset():
131			for n in func.entry_points:
132				if (not func.is_static_entry_point(func.name)) or (func.has_different_protocol(n) and not func.is_static_entry_point(n)):
133					print '#define gl_dispatch_stub_%u mgl_dispatch_stub_%u' % (func.offset, func.offset)
134					break
135		print "#endif /* USE_MGL_NAMESPACE */"
136		print ''
137		print ''
138		print '#if defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING)'
139		for func in api.functionIterateByOffset():
140			for n in func.entry_points:
141				if (not func.is_static_entry_point(func.name)) or (func.has_different_protocol(n) and not func.is_static_entry_point(n)):
142					print '%s GLAPIENTRY gl_dispatch_stub_%u(%s);' % (func.return_type, func.offset, func.get_parameter_string())
143					break
144
145		if self.es:
146			categories = {}
147			for func in api.functionIterateByOffset():
148				for n in func.entry_points:
149					cat, num = api.get_category_for_name(n)
150					if (cat.startswith("es") or cat.startswith("GL_OES")):
151						if not categories.has_key(cat):
152							categories[cat] = []
153						proto = 'GLAPI %s GLAPIENTRY %s(%s);' \
154								% (func.return_type, "gl" + n, func.get_parameter_string(n))
155						categories[cat].append(proto)
156			if categories:
157				print ''
158				print '/* OpenGL ES specific prototypes */'
159				print ''
160				keys = categories.keys()
161				keys.sort()
162				for key in keys:
163					print '/* category %s */' % key
164					print "\n".join(categories[key])
165				print ''
166
167		print '#endif /* defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING) */'
168
169		print ''
170		print 'static const glprocs_table_t static_functions[] = {'
171
172		for info in table:
173			print '    NAME_FUNC_OFFSET(%5u, %s, %s, %s, %d),' % info
174
175		print '    NAME_FUNC_OFFSET(-1, NULL, NULL, NULL, 0)'
176		print '};'
177		return
178
179
180def show_usage():
181	print "Usage: %s [-f input_file_name] [-m mode] [-c]" % sys.argv[0]
182	print "-c          Enable compatibility with OpenGL ES."
183	print "-m mode     mode can be one of:"
184	print "    long  - Create code for compilers that can handle very"
185	print "            long string constants. (default)"
186	print "    short - Create code for compilers that can only handle"
187	print "            ANSI C89 string constants."
188	sys.exit(1)
189
190if __name__ == '__main__':
191	file_name = "gl_API.xml"
192
193	try:
194		(args, trail) = getopt.getopt(sys.argv[1:], "f:m:c")
195	except Exception,e:
196		show_usage()
197
198	long_string = 1
199	es = False
200	for (arg,val) in args:
201		if arg == "-f":
202			file_name = val
203		elif arg == "-m":
204			if val == "short":
205				long_string = 0
206			elif val == "long":
207				long_string = 1
208			else:
209				show_usage()
210		elif arg == "-c":
211		    es = True
212
213	api = gl_XML.parse_GL_API(file_name, glX_XML.glx_item_factory())
214	printer = PrintGlProcs(long_string, es)
215	printer.Print(api)
216