aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/hw/xwin/glx/gen_gl_wrappers.py
blob: a24c5b580c7dd37a41dd568f719c090f0392c1d3 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/python3
#
# Copyright (c) 2013 The Khronos Group Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and/or associated documentation files (the
# "Materials"), to deal in the Materials without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Materials, and to
# permit persons to whom the Materials are furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Materials.
#
# THE MATERIALS ARE 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 NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 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
# MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.

import sys, time, pdb, string, cProfile
from reg import *

# debug - start header generation in debugger
# dump - dump registry after loading
# profile - enable Python profiling
# protect - whether to use #ifndef protections
# registry <filename> - use specified XML registry instead of gl.xml
# timeit - time length of registry loading & header generation
# validate - validate return & parameter group tags against <group>
debug   = False
dump    = False
profile = False
protect = True
timeit  = False
validate= False
# Default input / log files
errFilename = None
diagFilename = 'diag.txt'
regFilename = 'gl.xml'
outFilename = 'gen_gl_wrappers.c'
dispatchheader=None
prefix="gl"
preresolve=False
staticwrappers=False
nodebugcallcounting=False

# list of WGL extension functions we use
used_wgl_ext_fns = {key: 1 for key in [
    "wglSwapIntervalEXT",
    "wglGetExtensionsStringARB",
    "wglDestroyPbufferARB",
    "wglGetPbufferDCARB",
    "wglReleasePbufferDCARB",
    "wglCreatePbufferARB",
    "wglMakeContextCurrentARB",
    "wglChoosePixelFormatARB",
    "wglGetPixelFormatAttribivARB",
    "wglGetPixelFormatAttribivARB"
]}

if __name__ == '__main__':
    i = 1
    while (i < len(sys.argv)):
        arg = sys.argv[i]
        i = i + 1
        if (arg == '-debug'):
            print('Enabling debug (-debug)', file=sys.stderr)
            debug = True
        elif (arg == '-dump'):
            print('Enabling dump (-dump)', file=sys.stderr)
            dump = True
        elif (arg == '-noprotect'):
            print('Disabling inclusion protection in output headers', file=sys.stderr)
            protect = False
        elif (arg == '-profile'):
            print('Enabling profiling (-profile)', file=sys.stderr)
            profile = True
        elif (arg == '-registry'):
            regFilename = sys.argv[i]
            i = i+1
            print('Using registry ', regFilename, file=sys.stderr)
        elif (arg == '-time'):
            print('Enabling timing (-time)', file=sys.stderr)
            timeit = True
        elif (arg == '-validate'):
            print('Enabling group validation (-validate)', file=sys.stderr)
            validate = True
        elif (arg == '-outfile'):
            outFilename = sys.argv[i]
            i = i+1
        elif (arg == '-preresolve'):
            preresolve=True
        elif (arg == '-staticwrappers'):
            staticwrappers=True
        elif (arg == '-dispatchheader'):
            dispatchheader = sys.argv[i]
            i = i+1
        elif (arg == '-prefix'):
            prefix = sys.argv[i]
            i = i+1
        elif (arg == '-nodbgcount'):
            nodebugcallcounting = True
        elif (arg[0:1] == '-'):
            print('Unrecognized argument:', arg, file=sys.stderr)
            exit(1)
print('Generating ', outFilename, file=sys.stderr)

# Simple timer functions
startTime = None
def startTimer():
    global startTime
    startTime = time.clock()
def endTimer(msg):
    global startTime
    endTime = time.clock()
    if (timeit):
        print(msg, endTime - startTime)
        startTime = None

# Load & parse registry
reg = Registry()

startTimer()
tree = etree.parse(regFilename)
endTimer('Time to make ElementTree =')

startTimer()
reg.loadElementTree(tree)
endTimer('Time to parse ElementTree =')

if (validate):
    reg.validateGroups()

if (dump):
    print('***************************************')
    print('Performing Registry dump to regdump.txt')
    print('***************************************')
    reg.dumpReg(filehandle = open('regdump.txt','w'))

# Turn a list of strings into a regexp string matching exactly those strings
def makeREstring(list):
    return '^(' + '|'.join(list) + ')$'

# These are "mandatory" OpenGL ES 1 extensions, to
# be included in the core GLES/gl.h header.
es1CoreList = [
    'GL_OES_read_format',
    'GL_OES_compressed_paletted_texture',
    'GL_OES_point_size_array',
    'GL_OES_point_sprite'
]

# Descriptive names for various regexp patterns used to select
# versions and extensions

allVersions     = allExtensions = '.*'
noVersions      = noExtensions = None
gl12andLaterPat = '1\.[2-9]|[234]\.[0-9]'
gles2onlyPat    = '2\.[0-9]'
gles2and3Pat    = '[23]\.[0-9]'
es1CorePat      = makeREstring(es1CoreList)
# Extensions in old glcorearb.h but not yet tagged accordingly in gl.xml
glCoreARBPat    = None
glx13andLaterPat = '1\.[3-9]'

# Defaults for generating re-inclusion protection wrappers (or not)
protectFile = protect
protectFeature = protect
protectProto = protect

genOpts = CGeneratorOptions(
        apiname           = prefix,
        profile           = 'compatibility',
        versions          = allVersions,
        emitversions      = allVersions,
        defaultExtensions = prefix,                   # Default extensions for GL
#        addExtensions     = None,
#        removeExtensions  = None,
#        prefixText        = prefixStrings + glExtPlatformStrings + glextVersionStrings,
#        genFuncPointers   = True,
#        protectFile       = protectFile,
#        protectFeature    = protectFeature,
#        protectProto      = protectProto,
#        apicall           = 'GLAPI ',
#        apientry          = 'APIENTRY ',
#        apientryp         = 'APIENTRYP '),
        )

# create error/warning & diagnostic files
if (errFilename):
    errWarn = open(errFilename,'w')
else:
    errWarn = sys.stderr
diag = open(diagFilename, 'w')

#
# look for all the SET_ macros in dispatch.h, this is the set of functions
# we need to generate
#

dispatch = {}

if dispatchheader :
    fh = open(dispatchheader)
    dispatchh = fh.readlines()

    dispatch_regex = re.compile(r'(?:#define|static\s+INLINE\s+void)\s+SET_([^\()]+)\(')

    for line in dispatchh :
        line = line.strip()
        m1 = dispatch_regex.search(line)

        if m1 :
            dispatch[prefix+m1.group(1)] = 1

    del dispatch['glby_offset']

def ParseCmdRettype(cmd):
    proto=noneStr(cmd.elem.find('proto'))
    rettype=noneStr(proto.text)
    if rettype.lower()!="void ":
        plist = ([t for t in proto.itertext()])
        rettype = ''.join(plist[:-1])
    rettype=rettype.strip()
    return rettype

def ParseCmdParams(cmd):
    params = cmd.elem.findall('param')
    plist=[]
    for param in params:
        # construct the formal parameter definition from ptype and name
        # elements, also using any text found around these in the
        # param element, in the order it appears in the document
        paramtype = ''
        # also extract the formal parameter name from the name element
        paramname = ''
        for t in param.iter():
            if t.tag == 'ptype' or t.tag == 'param':
                paramtype = paramtype + noneStr(t.text)
            if t.tag == 'name':
                paramname = t.text + '_'
                paramtype = paramtype + ' ' + paramname
            if t.tail is not None:
                paramtype = paramtype + t.tail.strip()
        plist.append((paramtype, paramname))
    return plist

class PreResolveOutputGenerator(OutputGenerator):
    def __init__(self,
                 errFile = sys.stderr,
                 warnFile = sys.stderr,
                 diagFile = sys.stdout):
        OutputGenerator.__init__(self, errFile, warnFile, diagFile)
        self.wrappers={}
    def beginFile(self, genOpts):
        pass
    def endFile(self):
        self.outFile.write('\nvoid ' + prefix + 'ResolveExtensionProcs(void)\n{\n')
        for funcname in self.wrappers.keys():
            self.outFile.write( '  PRERESOLVE(PFN' + funcname.upper() + 'PROC, "' + funcname + '");\n')
        self.outFile.write('}\n\n')
    def beginFeature(self, interface, emit):
        OutputGenerator.beginFeature(self, interface, emit)
        self.OldVersion = self.featureName.startswith('GL_VERSION_1_0') or self.featureName.startswith('GL_VERSION_1_1')
    def endFeature(self):
        OutputGenerator.endFeature(self)
    def genType(self, typeinfo, name):
        OutputGenerator.genType(self, typeinfo, name)
    def genEnum(self, enuminfo, name):
        OutputGenerator.genEnum(self, enuminfo, name)
    def genCmd(self, cmd, name):
        OutputGenerator.genCmd(self, cmd, name)
        if prefix == 'wgl' and not name in used_wgl_ext_fns:
            return

        self.outFile.write('RESOLVE_DECL(PFN' + name.upper() + 'PROC);\n')
        self.wrappers[name]=1

class MyOutputGenerator(OutputGenerator):
    def __init__(self,
                 errFile = sys.stderr,
                 warnFile = sys.stderr,
                 diagFile = sys.stdout):
        OutputGenerator.__init__(self, errFile, warnFile, diagFile)
        self.wrappers={}
    def beginFile(self, genOpts):
        pass
    def endFile(self):
        pass
    def beginFeature(self, interface, emit):
        OutputGenerator.beginFeature(self, interface, emit)
        self.OldVersion = self.featureName.startswith('GL_VERSION_1_0') or self.featureName.startswith('GL_VERSION_1_1')
    def endFeature(self):
        OutputGenerator.endFeature(self)
    def genType(self, typeinfo, name):
        OutputGenerator.genType(self, typeinfo, name)
    def genEnum(self, enuminfo, name):
        OutputGenerator.genEnum(self, enuminfo, name)
    def genCmd(self, cmd, name):
        OutputGenerator.genCmd(self, cmd, name)
        # Avoid generating wrappers which aren't referenced by the dispatch table
        if dispatchheader and not name in dispatch :
            self.outFile.write('/* No wrapper for ' + name + ', not in dispatch table */\n')
            return

        if prefix == 'wgl' and not name in used_wgl_ext_fns:
            return

        self.wrappers[name]=1
        rettype=ParseCmdRettype(cmd)

        if staticwrappers: self.outFile.write("static ")
        self.outFile.write("%s __stdcall %sWrapper("%(rettype, name))
        plist=ParseCmdParams(cmd)
        Comma=""
        if len(plist):
            for ptype, pname in plist:
                self.outFile.write("%s%s"%(Comma, ptype))
                Comma=", "
        else:
            self.outFile.write("void")
        if self.OldVersion:
            if nodebugcallcounting:
                self.outFile.write(")\n{\n")
            else:
                self.outFile.write( """)
{
#ifdef _DEBUG
  if (glxWinDebugSettings.enable%scallTrace) ErrorF("%s\\n");
  glWinDirectProcCalls++;
#endif
"""%(prefix.upper(), name))
            if rettype.lower()=="void ":
                self.outFile.write("  %s( "%(name))
            else:
                self.outFile.write("  return %s( "%(name))
            Comma=""
            for ptype, pname in plist:
                self.outFile.write("%s%s"%(Comma, pname))
                Comma=", "
        else:
            if rettype.lower()=="void ":
                self.outFile.write(""")
{
  RESOLVE(PFN%sPROC, "%s");"""%(name.upper(), name))
                if not nodebugcallcounting: self.outFile.write("""
#ifdef _DEBUG
  if (glxWinDebugSettings.enable%scallTrace) ErrorF("%s\\n");
#endif"""%(prefix.upper(), name))
                self.outFile.write("""
  RESOLVED_PROC(PFN%sPROC)( """%(name.upper()))
            else:
                self.outFile.write(""")
{
  RESOLVE_RET(PFN%sPROC, "%s", FALSE);"""%(name.upper(), name))
                if not nodebugcallcounting: self.outFile.write("""
#ifdef _DEBUG
  if (glxWinDebugSettings.enable%scallTrace) ErrorF("%s\\n");
#endif"""%(prefix.upper(), name))
                self.outFile.write("""
  return RESOLVED_PROC(PFN%sPROC)( """%(name.upper()))
            Comma=""
            for ptype, pname in plist:
                self.outFile.write("%s%s"%(Comma, pname))
                Comma=", "
        self.outFile.write(" );\n}\n\n")

def genHeaders():
    startTimer()
    outFile = open(outFilename,"w")
    if preresolve:
        gen = PreResolveOutputGenerator(errFile=errWarn,
                                        warnFile=errWarn,
                                        diagFile=diag)
        gen.outFile=outFile
        reg.setGenerator(gen)
        reg.apiGen(genOpts)
    gen = MyOutputGenerator(errFile=errWarn,
                            warnFile=errWarn,
                            diagFile=diag)
    gen.outFile=outFile
    reg.setGenerator(gen)
    reg.apiGen(genOpts)

    # generate function to setup the dispatch table, which sets each
    # dispatch table entry to point to it's wrapper function
    # (assuming we were able to make one)

    if dispatchheader :
        outFile.write( 'void glWinSetupDispatchTable(void)\n')
        outFile.write( '{\n')
        outFile.write( '  struct _glapi_table *disp = _glapi_get_dispatch();\n')

        for d in sorted(dispatch.keys()) :
                if d in gen.wrappers :
                        outFile.write('  SET_'+ d[len(prefix):] + '(disp, (void *)' + d + 'Wrapper);\n')
                else :
                        outFile.write('#pragma message("No wrapper for ' + d + ' !")\n')

        outFile.write('}\n')



    outFile.close()

if (debug):
    pdb.run('genHeaders()')
elif (profile):
    import cProfile, pstats
    cProfile.run('genHeaders()', 'profile.txt')
    p = pstats.Stats('profile.txt')
    p.strip_dirs().sort_stats('time').print_stats(50)
else:
    genHeaders()