Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / mapi / glapi / gen / gl_table.py
1 #!/usr/bin/python2
2
3 # (C) Copyright IBM Corporation 2004
4 # All Rights Reserved.
5 # Copyright (c) 2014 Intel Corporation
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the "Software"),
9 # to deal in the Software without restriction, including without limitation
10 # on the rights to use, copy, modify, merge, publish, distribute, sub
11 # license, and/or sell copies of the Software, and to permit persons to whom
12 # the Software is furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice (including the next
15 # paragraph) shall be included in all copies or substantial portions of the
16 # Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 # IN THE SOFTWARE.
25 #
26 # Authors:
27 # Ian Romanick <idr@us.ibm.com>
28
29 import argparse
30
31 import gl_XML
32 import license
33
34
35 class PrintGlTable(gl_XML.gl_print_base):
36 def __init__(self):
37 gl_XML.gl_print_base.__init__(self)
38
39 self.header_tag = '_GLAPI_TABLE_H_'
40 self.name = "gl_table.py (from Mesa)"
41 self.license = license.bsd_license_template % ( \
42 """Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
43 (C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
44 self.ifdef_emitted = False
45 return
46
47 def printBody(self, api):
48 for f in api.functionIterateByOffset():
49 if not f.is_abi() and not self.ifdef_emitted:
50 print '#if !defined HAVE_SHARED_GLAPI'
51 self.ifdef_emitted = True
52 arg_string = f.get_parameter_string()
53 print ' %s (GLAPIENTRYP %s)(%s); /* %d */' % (
54 f.return_type, f.name, arg_string, f.offset)
55
56 print '#endif /* !defined HAVE_SHARED_GLAPI */'
57
58 def printRealHeader(self):
59 print '#ifndef GLAPIENTRYP'
60 print '# ifndef GLAPIENTRY'
61 print '# define GLAPIENTRY'
62 print '# endif'
63 print ''
64 print '# define GLAPIENTRYP GLAPIENTRY *'
65 print '#endif'
66 print ''
67 print ''
68 print 'struct _glapi_table'
69 print '{'
70 return
71
72 def printRealFooter(self):
73 print '};'
74 return
75
76
77 class PrintRemapTable(gl_XML.gl_print_base):
78 def __init__(self):
79 gl_XML.gl_print_base.__init__(self)
80
81 self.header_tag = '_DISPATCH_H_'
82 self.name = "gl_table.py (from Mesa)"
83 self.license = license.bsd_license_template % (
84 "(C) Copyright IBM Corporation 2005", "IBM")
85 return
86
87
88 def printRealHeader(self):
89 print """
90 /**
91 * \\file main/dispatch.h
92 * Macros for handling GL dispatch tables.
93 *
94 * For each known GL function, there are 3 macros in this file. The first
95 * macro is named CALL_FuncName and is used to call that GL function using
96 * the specified dispatch table. The other 2 macros, called GET_FuncName
97 * can SET_FuncName, are used to get and set the dispatch pointer for the
98 * named function in the specified dispatch table.
99 */
100 """
101 return
102
103
104 def printBody(self, api):
105 print '#define CALL_by_offset(disp, cast, offset, parameters) \\'
106 print ' (*(cast (GET_by_offset(disp, offset)))) parameters'
107 print '#define GET_by_offset(disp, offset) \\'
108 print ' (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL'
109 print '#define SET_by_offset(disp, offset, fn) \\'
110 print ' do { \\'
111 print ' if ( (offset) < 0 ) { \\'
112 print ' /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\'
113 print ' /* __func__, __LINE__, disp, offset, # fn); */ \\'
114 print ' /* abort(); */ \\'
115 print ' } \\'
116 print ' else { \\'
117 print ' ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\'
118 print ' } \\'
119 print ' } while(0)'
120 print ''
121
122 functions = []
123 abi_functions = []
124 count = 0
125 for f in api.functionIterateByOffset():
126 if not f.is_abi():
127 functions.append([f, count])
128 count += 1
129 else:
130 abi_functions.append([f, -1])
131
132 print '/* total number of offsets below */'
133 print '#define _gloffset_COUNT %d' % (len(abi_functions + functions))
134 print ''
135
136 for f, index in abi_functions:
137 print '#define _gloffset_%s %d' % (f.name, f.offset)
138
139 remap_table = "driDispatchRemapTable"
140
141 print '#define %s_size %u' % (remap_table, count)
142 print 'extern int %s[ %s_size ];' % (remap_table, remap_table)
143 print ''
144
145 for f, index in functions:
146 print '#define %s_remap_index %u' % (f.name, index)
147
148 print ''
149
150 for f, index in functions:
151 print '#define _gloffset_%s %s[%s_remap_index]' % (f.name, remap_table, f.name)
152
153 print ''
154
155 for f, index in abi_functions + functions:
156 arg_string = gl_XML.create_parameter_string(f.parameters, 0)
157
158 print 'typedef %s (GLAPIENTRYP _glptr_%s)(%s);' % (f.return_type, f.name, arg_string)
159 print '#define CALL_%s(disp, parameters) \\' % (f.name)
160 print ' (* GET_%s(disp)) parameters' % (f.name)
161 print 'static inline _glptr_%s GET_%s(struct _glapi_table *disp) {' % (f.name, f.name)
162 print ' return (_glptr_%s) (GET_by_offset(disp, _gloffset_%s));' % (f.name, f.name)
163 print '}'
164 print
165 print 'static inline void SET_%s(struct _glapi_table *disp, %s (GLAPIENTRYP fn)(%s)) {' % (f.name, f.return_type, arg_string)
166 print ' SET_by_offset(disp, _gloffset_%s, fn);' % (f.name)
167 print '}'
168 print
169
170 return
171
172
173 def _parser():
174 """Parse arguments and return a namespace."""
175 parser = argparse.ArgumentParser()
176 parser.add_argument('-f', '--filename',
177 default='gl_API.xml',
178 metavar="input_file_name",
179 dest='file_name',
180 help="Path to an XML description of OpenGL API.")
181 parser.add_argument('-m', '--mode',
182 choices=['table', 'remap_table'],
183 default='table',
184 metavar="mode",
185 help="Generate either a table or a remap_table")
186 return parser.parse_args()
187
188
189 def main():
190 """Main function."""
191 args = _parser()
192
193 api = gl_XML.parse_GL_API(args.file_name)
194
195 if args.mode == "table":
196 printer = PrintGlTable()
197 elif args.mode == "remap_table":
198 printer = PrintRemapTable()
199
200 printer.Print(api)
201
202
203 if __name__ == '__main__':
204 main()