13464ebd5Sriastradh
23464ebd5Sriastradh# (C) Copyright IBM Corporation 2004, 2005
33464ebd5Sriastradh# All Rights Reserved.
43464ebd5Sriastradh#
53464ebd5Sriastradh# Permission is hereby granted, free of charge, to any person obtaining a
63464ebd5Sriastradh# copy of this software and associated documentation files (the "Software"),
73464ebd5Sriastradh# to deal in the Software without restriction, including without limitation
83464ebd5Sriastradh# on the rights to use, copy, modify, merge, publish, distribute, sub
93464ebd5Sriastradh# license, and/or sell copies of the Software, and to permit persons to whom
103464ebd5Sriastradh# the Software is furnished to do so, subject to the following conditions:
113464ebd5Sriastradh#
123464ebd5Sriastradh# The above copyright notice and this permission notice (including the next
133464ebd5Sriastradh# paragraph) shall be included in all copies or substantial portions of the
143464ebd5Sriastradh# Software.
153464ebd5Sriastradh#
163464ebd5Sriastradh# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
173464ebd5Sriastradh# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
183464ebd5Sriastradh# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
193464ebd5Sriastradh# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
203464ebd5Sriastradh# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
213464ebd5Sriastradh# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
223464ebd5Sriastradh# IN THE SOFTWARE.
233464ebd5Sriastradh#
243464ebd5Sriastradh# Authors:
253464ebd5Sriastradh#    Ian Romanick <idr@us.ibm.com>
263464ebd5Sriastradh
2701e04c3fSmrgimport argparse
2801e04c3fSmrg
293464ebd5Sriastradhimport license
3001e04c3fSmrgimport gl_XML
3101e04c3fSmrgimport glX_XML
3201e04c3fSmrg
333464ebd5Sriastradh
343464ebd5Sriastradhclass PrintGlProcs(gl_XML.gl_print_base):
35af69d88dSmrg    def __init__(self, es=False):
36af69d88dSmrg        gl_XML.gl_print_base.__init__(self)
373464ebd5Sriastradh
38af69d88dSmrg        self.es = es
39af69d88dSmrg        self.name = "gl_procs.py (from Mesa)"
40af69d88dSmrg        self.license = license.bsd_license_template % ( \
413464ebd5Sriastradh"""Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
423464ebd5Sriastradh(C) Copyright IBM Corporation 2004, 2006""", "BRIAN PAUL, IBM")
433464ebd5Sriastradh
44af69d88dSmrg    def printRealHeader(self):
4501e04c3fSmrg        print("""
463464ebd5Sriastradh/* This file is only included by glapi.c and is used for
473464ebd5Sriastradh * the GetProcAddress() function
483464ebd5Sriastradh */
493464ebd5Sriastradh
503464ebd5Sriastradhtypedef struct {
513464ebd5Sriastradh    GLint Name_offset;
523464ebd5Sriastradh#if defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING)
533464ebd5Sriastradh    _glapi_proc Address;
543464ebd5Sriastradh#endif
553464ebd5Sriastradh    GLuint Offset;
563464ebd5Sriastradh} glprocs_table_t;
573464ebd5Sriastradh
583464ebd5Sriastradh#if   !defined(NEED_FUNCTION_POINTER) && !defined(GLX_INDIRECT_RENDERING)
593464ebd5Sriastradh#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , o }
603464ebd5Sriastradh#elif  defined(NEED_FUNCTION_POINTER) && !defined(GLX_INDIRECT_RENDERING)
613464ebd5Sriastradh#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f1 , o }
623464ebd5Sriastradh#elif  defined(NEED_FUNCTION_POINTER) &&  defined(GLX_INDIRECT_RENDERING)
633464ebd5Sriastradh#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f2 , o }
643464ebd5Sriastradh#elif !defined(NEED_FUNCTION_POINTER) &&  defined(GLX_INDIRECT_RENDERING)
653464ebd5Sriastradh#  define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f3 , o }
663464ebd5Sriastradh#endif
673464ebd5Sriastradh
6801e04c3fSmrg""")
69af69d88dSmrg        return
70af69d88dSmrg
71af69d88dSmrg    def printRealFooter(self):
7201e04c3fSmrg        print('')
7301e04c3fSmrg        print('#undef NAME_FUNC_OFFSET')
74af69d88dSmrg        return
75af69d88dSmrg
76af69d88dSmrg    def printFunctionString(self, name):
7701e04c3fSmrg        print('    "gl%s\\0"' % (name))
78af69d88dSmrg
79af69d88dSmrg    def printBody(self, api):
8001e04c3fSmrg        print('')
8101e04c3fSmrg        print('static const char gl_string_table[] =')
82af69d88dSmrg
83af69d88dSmrg        base_offset = 0
84af69d88dSmrg        table = []
85af69d88dSmrg        for func in api.functionIterateByOffset():
86af69d88dSmrg            name = func.dispatch_name()
87af69d88dSmrg            self.printFunctionString(func.name)
88af69d88dSmrg            table.append((base_offset, "gl" + name, "gl" + name, "NULL", func.offset))
89af69d88dSmrg
90af69d88dSmrg            # The length of the function's name, plus 2 for "gl",
91af69d88dSmrg            # plus 1 for the NUL.
92af69d88dSmrg
93af69d88dSmrg            base_offset += len(func.name) + 3
94af69d88dSmrg
95af69d88dSmrg
96af69d88dSmrg        for func in api.functionIterateByOffset():
97af69d88dSmrg            for n in func.entry_points:
98af69d88dSmrg                if n != func.name:
99af69d88dSmrg                    name = func.dispatch_name()
100af69d88dSmrg                    self.printFunctionString( n )
101af69d88dSmrg
102af69d88dSmrg                    if func.has_different_protocol(n):
103af69d88dSmrg                        alt_name = "gl" + func.static_glx_name(n)
104af69d88dSmrg                        table.append((base_offset, "gl" + name, alt_name, alt_name, func.offset))
105af69d88dSmrg                    else:
106af69d88dSmrg                        table.append((base_offset, "gl" + name, "gl" + name, "NULL", func.offset))
107af69d88dSmrg
108af69d88dSmrg                    base_offset += len(n) + 3
109af69d88dSmrg
110af69d88dSmrg
11101e04c3fSmrg        print('    ;')
11201e04c3fSmrg        print('')
11301e04c3fSmrg        print('')
11401e04c3fSmrg        print('#if defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING)')
115af69d88dSmrg        for func in api.functionIterateByOffset():
116af69d88dSmrg            for n in func.entry_points:
117af69d88dSmrg                if (not func.is_static_entry_point(func.name)) or (func.has_different_protocol(n) and not func.is_static_entry_point(n)):
11801e04c3fSmrg                    print('%s GLAPIENTRY gl_dispatch_stub_%u(%s);' % (func.return_type, func.offset, func.get_parameter_string()))
119af69d88dSmrg                    break
120af69d88dSmrg
121af69d88dSmrg        if self.es:
122af69d88dSmrg            categories = {}
123af69d88dSmrg            for func in api.functionIterateByOffset():
124af69d88dSmrg                for n in func.entry_points:
125af69d88dSmrg                    cat, num = api.get_category_for_name(n)
126af69d88dSmrg                    if (cat.startswith("es") or cat.startswith("GL_OES")):
12701e04c3fSmrg                        if cat not in categories:
128af69d88dSmrg                            categories[cat] = []
129af69d88dSmrg                        proto = 'GLAPI %s GLAPIENTRY %s(%s);' \
130af69d88dSmrg                                        % (func.return_type, "gl" + n, func.get_parameter_string(n))
131af69d88dSmrg                        categories[cat].append(proto)
132af69d88dSmrg            if categories:
13301e04c3fSmrg                print('')
13401e04c3fSmrg                print('/* OpenGL ES specific prototypes */')
13501e04c3fSmrg                print('')
13601e04c3fSmrg                keys = sorted(categories.keys())
137af69d88dSmrg                for key in keys:
13801e04c3fSmrg                    print('/* category %s */' % key)
13901e04c3fSmrg                    print("\n".join(categories[key]))
14001e04c3fSmrg                print('')
141af69d88dSmrg
14201e04c3fSmrg        print('#endif /* defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING) */')
143af69d88dSmrg
14401e04c3fSmrg        print('')
14501e04c3fSmrg        print('static const glprocs_table_t static_functions[] = {')
146af69d88dSmrg
147af69d88dSmrg        for info in table:
14801e04c3fSmrg            print('    NAME_FUNC_OFFSET(%5u, %s, %s, %s, %d),' % info)
149af69d88dSmrg
15001e04c3fSmrg        print('    NAME_FUNC_OFFSET(-1, NULL, NULL, NULL, 0)')
15101e04c3fSmrg        print('};')
152af69d88dSmrg        return
1533464ebd5Sriastradh
1543464ebd5Sriastradh
15501e04c3fSmrgdef _parser():
15601e04c3fSmrg    """Parse arguments and return a namepsace."""
15701e04c3fSmrg
15801e04c3fSmrg    parser = argparse.ArgumentParser()
15901e04c3fSmrg    parser.add_argument('-f', '--filename',
16001e04c3fSmrg                        default='gl_API.xml',
16101e04c3fSmrg                        metavar="input_file_name",
16201e04c3fSmrg                        dest='file_name',
16301e04c3fSmrg                        help="Path to an XML description of OpenGL API.")
16401e04c3fSmrg    parser.add_argument('-c', '--es-version',
16501e04c3fSmrg                        dest='es',
16601e04c3fSmrg                        action="store_true",
16701e04c3fSmrg                        help="filter functions for es")
16801e04c3fSmrg    return parser.parse_args()
16901e04c3fSmrg
17001e04c3fSmrg
17101e04c3fSmrgdef main():
17201e04c3fSmrg    """Main function."""
17301e04c3fSmrg    args = _parser()
17401e04c3fSmrg    api = gl_XML.parse_GL_API(args.file_name, glX_XML.glx_item_factory())
17501e04c3fSmrg    PrintGlProcs(args.es).Print(api)
17601e04c3fSmrg
1773464ebd5Sriastradh
1783464ebd5Sriastradhif __name__ == '__main__':
17901e04c3fSmrg    main()
180