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