summaryrefslogtreecommitdiff
path: root/helpers/glsize.py
blob: 0eb92557fc17831e25fd92048661bf245df50803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python

# Copyright 2010 VMware, Inc.
# Copyright 2004, 2005 IBM Corporation
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# on the rights to use, copy, modify, merge, publish, distribute, sub
# license, and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# Authors:
#    Jose Fonseca <jfonseca@vmware.com>
#    Ian Romanick <idr@us.ibm.com>

import os.path
import sys

glapi_path =  os.path.join(os.environ['MESA'], 'src/mapi/glapi/gen')
sys.path.insert(0, glapi_path)

import gl_XML, glX_XML
import license
import getopt, copy, string


class glx_enum_function:
    def __init__(self, func_name, enum_dict):
        self.name = func_name
        self.mode = 1
        self.sig = None

        # "enums" is a set of lists.  The element in the set is the
        # value of the enum.  The list is the list of names for that
        # value.  For example, [0x8126] = {"POINT_SIZE_MIN",
        # "POINT_SIZE_MIN_ARB", "POINT_SIZE_MIN_EXT",
        # "POINT_SIZE_MIN_SGIS"}.

        self.enums = {}

        # "count" is indexed by count values.  Each element of count
        # is a list of index to "enums" that have that number of
        # associated data elements.  For example, [4] = 
        # {GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_EMISSION,
        # GL_AMBIENT_AND_DIFFUSE} (the enum names are used here,
        # but the actual hexadecimal values would be in the array).

        self.count = {}


        # Fill self.count and self.enums using the dictionary of enums
        # that was passed in.  The generic Get functions (e.g.,
        # GetBooleanv and friends) are handled specially here.  In
        # the data the generic Get functions are refered to as "Get".

        if func_name in ["GetIntegerv", "GetBooleanv", "GetFloatv", "GetDoublev"]:
            match_name = "Get"
        else:
            match_name = func_name

        mode_set = 0
        for enum_name in enum_dict:
            e = enum_dict[ enum_name ]

            if e.functions.has_key( match_name ):
                [count, mode] = e.functions[ match_name ]

                if mode_set and mode != self.mode:
                    raise RuntimeError("Not all enums for %s have the same mode." % (func_name))

                self.mode = mode

                if self.enums.has_key( e.value ):
                    if e.name not in self.enums[ e.value ]:
                        self.enums[ e.value ].append( e )
                else:
                    if not self.count.has_key( count ):
                        self.count[ count ] = []

                    self.enums[ e.value ] = [ e ]
                    self.count[ count ].append( e.value )


        return


    def signature( self ):
        if self.sig == None:
            self.sig = ""
            for i in self.count:
                if i == None:
                    raise RuntimeError("i is None.  WTF?")

                self.count[i].sort()
                for e in self.count[i]:
                    self.sig += "%04x,%d," % (e, i)

        return self.sig


    def PrintUsingSwitch(self, name):
        """Emit the body of the __gl*_size function using a 
        switch-statement."""

        print '    switch (pname) {'

        for c in self.count:
            for e in self.count[c]:
                first = 1

                # There may be multiple enums with the same
                # value.  This happens has extensions are
                # promoted from vendor-specific or EXT to
                # ARB and to the core.  Emit the first one as
                # a case label, and emit the others as
                # commented-out case labels.

                list = {}
                for enum_obj in self.enums[e]:
                    list[ enum_obj.priority() ] = enum_obj.name

                keys = list.keys()
                keys.sort()
                for k in keys:
                    j = list[k]
                    if first:
                        print '    case GL_%s:' % (j)
                        first = 0
                    else:
                        print '/*  case GL_%s:*/' % (j)
                    
            if c == -1:
                print '        return __gl%s_variable_size(pname);' % (name)
            else:
                print '        return %u;' % (c)
                    
        print '    default:' 
        print '        assert(0);' 
        print '        return 1;'
        print '    }'


    def Print(self, name):
        print 'static inline size_t'
        print '__gl%s_size(GLenum pname)' % (name)
        print '{'

        self.PrintUsingSwitch(name)

        print '}'
        print ''


class glx_server_enum_function(glx_enum_function):
    def __init__(self, func, enum_dict):
        glx_enum_function.__init__(self, func.name, enum_dict)
        
        self.function = func
        return


    def signature( self ):
        if self.sig == None:
            sig = glx_enum_function.signature(self)

            p = self.function.variable_length_parameter()
            if p:
                sig += "%u" % (p.size())

            self.sig = sig

        return self.sig;


    def Print(self, name, printer):
        f = self.function
        printer.common_func_print_just_header( f )

        fixup = []
        
        foo = {}
        for param_name in f.count_parameter_list:
            o = f.offset_of( param_name )
            foo[o] = param_name

        for param_name in f.counter_list:
            o = f.offset_of( param_name )
            foo[o] = param_name

        keys = foo.keys()
        keys.sort()
        for o in keys:
            p = f.parameters_by_name[ foo[o] ]

            printer.common_emit_one_arg(p, "pc", 0)
            fixup.append( p.name )


        print '    GLsizei compsize;'
        print ''

        printer.common_emit_fixups(fixup)

        print ''
        print '    compsize = __gl%s_size(%s);' % (f.name, string.join(f.count_parameter_list, ","))
        p = f.variable_length_parameter()
        print '    return __GLX_PAD(%s);' % (p.size_string())

        print '}'
        print ''


class PrintGlxSizeStubs_c(gl_XML.gl_print_base):

    def __init__(self):
        gl_XML.gl_print_base.__init__(self)

        self.name = "glX_proto_size.py (from Mesa)"
        self.license = license.bsd_license_template % ( "Copyright 2010 VMware, Inc.\nCopyright 2004 IBM Corporation", "AUTHORS")

    def printRealHeader(self):
        print
        print
        print '#include <cassert>'
        print
        print '#include "glimports.hpp"'
        print
        print

    def printBody(self, api):
        enum_sigs = {}

        for func in api.functionIterateByOffset():
            ef = glx_enum_function( func.name, api.enums_by_name )
            if len(ef.enums) == 0:
                continue

            if True:
                sig = ef.signature()
                if enum_sigs.has_key( sig ):
                    alias_name, real_name = func.name, enum_sigs[ sig ]
                    print '#define __gl%s_size __gl%s_size' % (alias_name, real_name)
                    print
                else:
                    enum_sigs[ sig ] = func.name
                    ef.Print( func.name )


def show_usage():
    print "Usage: %s [-f input_file_name] [--only-get | --only-set] [--get-alias-set]" % sys.argv[0]
    sys.exit(1)


if __name__ == '__main__':
    file_name = os.path.join(glapi_path, "gl_API.xml")

    try:
        (args, trail) = getopt.getopt(sys.argv[1:], "f:h:", ["header-tag"])
    except Exception,e:
        show_usage()

    header_tag = None

    for (arg,val) in args:
        if arg == "-f":
            file_name = val
        elif (arg == '-h') or (arg == "--header-tag"):
            header_tag = val

    printer = PrintGlxSizeStubs_c()

    api = gl_XML.parse_GL_API( file_name, glX_XML.glx_item_factory() )


    printer.Print( api )