Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / glapi / gl_table.py
1 #!/usr/bin/python2
2
3 # (C) Copyright IBM Corporation 2004
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 gl_XML
29 import license
30 import sys, getopt
31
32 class PrintGlTable(gl_XML.gl_print_base):
33 def __init__(self):
34 gl_XML.gl_print_base.__init__(self)
35
36 self.header_tag = '_GLAPI_TABLE_H_'
37 self.name = "gl_table.py (from Mesa)"
38 self.license = license.bsd_license_template % ( \
39 """Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
40 (C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
41 return
42
43
44 def printBody(self, api):
45 for f in api.functionIterateByOffset():
46 arg_string = f.get_parameter_string()
47 print ' %s (GLAPIENTRYP %s)(%s); /* %d */' % (f.return_type, f.name, arg_string, f.offset)
48
49
50 def printRealHeader(self):
51 print '#ifndef GLAPIENTRYP'
52 print '# ifndef GLAPIENTRY'
53 print '# define GLAPIENTRY'
54 print '# endif'
55 print ''
56 print '# define GLAPIENTRYP GLAPIENTRY *'
57 print '#endif'
58 print ''
59 print ''
60 print 'struct _glapi_table'
61 print '{'
62 return
63
64
65 def printRealFooter(self):
66 print '};'
67 return
68
69
70 class PrintRemapTable(gl_XML.gl_print_base):
71 def __init__(self):
72 gl_XML.gl_print_base.__init__(self)
73
74 self.header_tag = '_GLAPI_DISPATCH_H_'
75 self.name = "gl_table.py (from Mesa)"
76 self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM")
77 return
78
79
80 def printRealHeader(self):
81 print """
82 /* this file should not be included directly in mesa */
83
84 /**
85 * \\file glapidispatch.h
86 * Macros for handling GL dispatch tables.
87 *
88 * For each known GL function, there are 3 macros in this file. The first
89 * macro is named CALL_FuncName and is used to call that GL function using
90 * the specified dispatch table. The other 2 macros, called GET_FuncName
91 * can SET_FuncName, are used to get and set the dispatch pointer for the
92 * named function in the specified dispatch table.
93 */
94 """
95
96 return
97
98 def printBody(self, api):
99 print '#define CALL_by_offset(disp, cast, offset, parameters) \\'
100 print ' (*(cast (GET_by_offset(disp, offset)))) parameters'
101 print '#define GET_by_offset(disp, offset) \\'
102 print ' (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL'
103 print '#define SET_by_offset(disp, offset, fn) \\'
104 print ' do { \\'
105 print ' if ( (offset) < 0 ) { \\'
106 print ' /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\'
107 print ' /* __func__, __LINE__, disp, offset, # fn); */ \\'
108 print ' /* abort(); */ \\'
109 print ' } \\'
110 print ' else { \\'
111 print ' ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\'
112 print ' } \\'
113 print ' } while(0)'
114 print ''
115
116 abi = [ "1.0", "1.1", "1.2", "GL_ARB_multitexture" ]
117
118 functions = []
119 abi_functions = []
120 count = 0
121 for f in api.functionIterateByOffset():
122 [category, num] = api.get_category_for_name( f.name )
123 if category not in abi:
124 functions.append( [f, count] )
125 count += 1
126 else:
127 abi_functions.append( f )
128
129
130 for f in abi_functions:
131 print '#define CALL_%s(disp, parameters) (*((disp)->%s)) parameters' % (f.name, f.name)
132 print '#define GET_%s(disp) ((disp)->%s)' % (f.name, f.name)
133 print '#define SET_%s(disp, fn) ((disp)->%s = fn)' % (f.name, f.name)
134
135
136 print ''
137 print '#if !defined(_GLAPI_USE_REMAP_TABLE)'
138 print ''
139
140 for [f, index] in functions:
141 print '#define CALL_%s(disp, parameters) (*((disp)->%s)) parameters' % (f.name, f.name)
142 print '#define GET_%s(disp) ((disp)->%s)' % (f.name, f.name)
143 print '#define SET_%s(disp, fn) ((disp)->%s = fn)' % (f.name, f.name)
144
145 print ''
146 print '#else'
147 print ''
148 print '#define driDispatchRemapTable_size %u' % (count)
149 print 'extern int driDispatchRemapTable[ driDispatchRemapTable_size ];'
150 print ''
151
152 for [f, index] in functions:
153 print '#define %s_remap_index %u' % (f.name, index)
154
155 print ''
156
157 for [f, index] in functions:
158 arg_string = gl_XML.create_parameter_string( f.parameters, 0 )
159 cast = '%s (GLAPIENTRYP)(%s)' % (f.return_type, arg_string)
160
161 print '#define CALL_%s(disp, parameters) CALL_by_offset(disp, (%s), driDispatchRemapTable[%s_remap_index], parameters)' % (f.name, cast, f.name)
162 print '#define GET_%s(disp) GET_by_offset(disp, driDispatchRemapTable[%s_remap_index])' % (f.name, f.name)
163 print '#define SET_%s(disp, fn) SET_by_offset(disp, driDispatchRemapTable[%s_remap_index], fn)' % (f.name, f.name)
164
165
166 print ''
167 print '#endif /* !defined(_GLAPI_USE_REMAP_TABLE) */'
168 return
169
170
171 def show_usage():
172 print "Usage: %s [-f input_file_name] [-m mode]" % sys.argv[0]
173 print " -m mode Mode can be 'table' or 'remap_table'."
174 sys.exit(1)
175
176 if __name__ == '__main__':
177 file_name = "gl_API.xml"
178
179 try:
180 (args, trail) = getopt.getopt(sys.argv[1:], "f:m:")
181 except Exception,e:
182 show_usage()
183
184 mode = "table"
185 for (arg,val) in args:
186 if arg == "-f":
187 file_name = val
188 elif arg == "-m":
189 mode = val
190
191 if mode == "table":
192 printer = PrintGlTable()
193 elif mode == "remap_table":
194 printer = PrintRemapTable()
195 else:
196 show_usage()
197
198 api = gl_XML.parse_GL_API( file_name )
199
200 printer.Print( api )