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 argparse
29
30import license
31import gl_XML
32import glX_XML
33
34
35class PrintGlProcs(gl_XML.gl_print_base):
36    def __init__(self, es=False):
37        gl_XML.gl_print_base.__init__(self)
38
39        self.es = es
40        self.name = "gl_procs.py (from Mesa)"
41        self.license = license.bsd_license_template % ( \
42"""Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
43(C) Copyright IBM Corporation 2004, 2006""", "BRIAN PAUL, IBM")
44
45    def printRealHeader(self):
46        print """
47/* This file is only included by glapi.c and is used for
48 * the GetProcAddress() function
49 */
50
51typedef struct {
52    GLint Name_offset;
53#if defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING)
54    _glapi_proc Address;
55#endif
56    GLuint Offset;
57} glprocs_table_t;
58
59#if   !defined(NEED_FUNCTION_POINTER) && !defined(GLX_INDIRECT_RENDERING)
60#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , o }
61#elif  defined(NEED_FUNCTION_POINTER) && !defined(GLX_INDIRECT_RENDERING)
62#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f1 , o }
63#elif  defined(NEED_FUNCTION_POINTER) &&  defined(GLX_INDIRECT_RENDERING)
64#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f2 , o }
65#elif !defined(NEED_FUNCTION_POINTER) &&  defined(GLX_INDIRECT_RENDERING)
66#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f3 , o }
67#endif
68
69"""
70        return
71
72    def printRealFooter(self):
73        print ''
74        print '#undef NAME_FUNC_OFFSET'
75        return
76
77    def printFunctionString(self, name):
78        print '    "gl%s\\0"' % (name)
79
80    def printBody(self, api):
81        print ''
82        print 'static const char gl_string_table[] ='
83
84        base_offset = 0
85        table = []
86        for func in api.functionIterateByOffset():
87            name = func.dispatch_name()
88            self.printFunctionString(func.name)
89            table.append((base_offset, "gl" + name, "gl" + name, "NULL", func.offset))
90
91            # The length of the function's name, plus 2 for "gl",
92            # plus 1 for the NUL.
93
94            base_offset += len(func.name) + 3
95
96
97        for func in api.functionIterateByOffset():
98            for n in func.entry_points:
99                if n != func.name:
100                    name = func.dispatch_name()
101                    self.printFunctionString( n )
102
103                    if func.has_different_protocol(n):
104                        alt_name = "gl" + func.static_glx_name(n)
105                        table.append((base_offset, "gl" + name, alt_name, alt_name, func.offset))
106                    else:
107                        table.append((base_offset, "gl" + name, "gl" + name, "NULL", func.offset))
108
109                    base_offset += len(n) + 3
110
111
112        print '    ;'
113        print ''
114        print ''
115        print "#ifdef USE_MGL_NAMESPACE"
116        for func in api.functionIterateByOffset():
117            for n in func.entry_points:
118                if (not func.is_static_entry_point(func.name)) or (func.has_different_protocol(n) and not func.is_static_entry_point(n)):
119                    print '#define gl_dispatch_stub_%u mgl_dispatch_stub_%u' % (func.offset, func.offset)
120                    break
121        print "#endif /* USE_MGL_NAMESPACE */"
122        print ''
123        print ''
124        print '#if defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING)'
125        for func in api.functionIterateByOffset():
126            for n in func.entry_points:
127                if (not func.is_static_entry_point(func.name)) or (func.has_different_protocol(n) and not func.is_static_entry_point(n)):
128                    print '%s GLAPIENTRY gl_dispatch_stub_%u(%s);' % (func.return_type, func.offset, func.get_parameter_string())
129                    break
130
131        if self.es:
132            categories = {}
133            for func in api.functionIterateByOffset():
134                for n in func.entry_points:
135                    cat, num = api.get_category_for_name(n)
136                    if (cat.startswith("es") or cat.startswith("GL_OES")):
137                        if not categories.has_key(cat):
138                            categories[cat] = []
139                        proto = 'GLAPI %s GLAPIENTRY %s(%s);' \
140                                        % (func.return_type, "gl" + n, func.get_parameter_string(n))
141                        categories[cat].append(proto)
142            if categories:
143                print ''
144                print '/* OpenGL ES specific prototypes */'
145                print ''
146                keys = categories.keys()
147                keys.sort()
148                for key in keys:
149                    print '/* category %s */' % key
150                    print "\n".join(categories[key])
151                print ''
152
153        print '#endif /* defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING) */'
154
155        print ''
156        print 'static const glprocs_table_t static_functions[] = {'
157
158        for info in table:
159            print '    NAME_FUNC_OFFSET(%5u, %s, %s, %s, %d),' % info
160
161        print '    NAME_FUNC_OFFSET(-1, NULL, NULL, NULL, 0)'
162        print '};'
163        return
164
165
166def _parser():
167    """Parse arguments and return a namepsace."""
168
169    parser = argparse.ArgumentParser()
170    parser.add_argument('-f', '--filename',
171                        default='gl_API.xml',
172                        metavar="input_file_name",
173                        dest='file_name',
174                        help="Path to an XML description of OpenGL API.")
175    parser.add_argument('-c', '--es-version',
176                        dest='es',
177                        action="store_true",
178                        help="filter functions for es")
179    return parser.parse_args()
180
181
182def main():
183    """Main function."""
184    args = _parser()
185    api = gl_XML.parse_GL_API(args.file_name, glX_XML.glx_item_factory())
186    PrintGlProcs(args.es).Print(api)
187
188
189if __name__ == '__main__':
190    main()
191