summaryrefslogtreecommitdiff
path: root/gfx/gl/GLParseRegistryXML.py
blob: 60daca6e565252d54230a4c22ceba19787c96fa6 (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
#!/usr/bin/env python
# coding=utf8


################################################################################
# TUTORIAL
# This script will generate GLConsts.h
#
# Step 1:
#   Download the last gl.xml, egl.xml, glx.xml and wgl.xml from
#   http://www.opengl.org/registry/#specfiles into a directory of your choice
#
# Step 2:
#   Execute this script ./GLParseRegistryXML.py [your dir containing XML files]
#
# Step 3:
#   Do not add the downloaded XML in the patch
#
# Step 4:
#   Enjoy =)
#
################################################################################

# includes
import os
import sys
import xml.etree.ElementTree


################################################################################
# export management

class GLConstHeader:
    def __init__(self, f):
        self.f = f


    def write(self, arg):
        if isinstance(arg, list):
            self.f.write('\n'.join(arg) + '\n')
        elif isinstance(arg, (int, long)):
            self.f.write('\n' * arg)
        else:
            self.f.write(str(arg) + '\n')


    def formatFileBegin(self):
        self.write([
            '/* This Source Code Form is subject to the terms of the Mozilla Public',
            ' * License, v. 2.0. If a copy of the MPL was not distributed with this',
            ' * file, You can obtain one at http://mozilla.org/MPL/2.0/. */',
            '',
            '#ifndef GLCONSTS_H_',
            '#define GLCONSTS_H_',
            '',
            '/**',
            ' * GENERATED FILE, DO NOT MODIFY DIRECTLY.',
            ' * This is a file generated directly from the official OpenGL registry',
            ' * xml available http://www.opengl.org/registry/#specfiles.',
            ' *',
            ' * To generate this file, see tutorial in \'GLParseRegistryXML.py\'.',
            ' */',
            ''
        ])


    def formatLibBegin(self, lib):
        # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
        self.write('// ' + lib)


    def formatLibConstant(self, lib, name, value):
        # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
        # name is the name of the const (example: MAX_TEXTURE_SIZE)
        # value is the value of the const (example: 0xABCD)

        define = '#define LOCAL_' + lib + '_' + name
        whitespace = 60 - len(define)
        
        if whitespace < 0:
            whitespace = whitespace % 8
        
        self.write(define + ' ' * whitespace + ' ' + value)


    def formatLibEnd(self, lib):
        # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
        self.write(2)


    def formatFileEnd(self):
        self.write([
                    '',
                    '#endif // GLCONSTS_H_'
                   ])


################################################################################
# underground code

def getScriptDir():
    return os.path.dirname(__file__) + '/'


def getXMLDir():
    if len(sys.argv) == 1:
        return './'

    dirPath = sys.argv[1]
    if dirPath[-1] != '/':
        dirPath += '/'

    return dirPath


class GLConst:
    def __init__(self, lib, name, value, type):
        self.lib = lib
        self.name = name
        self.value = value
        self.type = type


class GLDatabase:

    LIBS = ['GL', 'EGL', 'GLX', 'WGL']

    def __init__(self):
        self.consts = {}
        self.libs = set(GLDatabase.LIBS)
        self.vendors = set(['EXT', 'ATI'])
        # there is no vendor="EXT" and vendor="ATI" in gl.xml,
        # so we manualy declare them


    def loadXML(self, path):
        xmlPath = getXMLDir() + path

        if not os.path.isfile(xmlPath):
            print 'missing file "' + xmlPath + '"'
            return False

        tree = xml.etree.ElementTree.parse(xmlPath)
        root = tree.getroot()

        for enums in root.iter('enums'):
            vendor = enums.get('vendor')
            if not vendor:
                # there some standart enums that do have the vendor attribute,
                # so we fake them as ARB's enums
                vendor = 'ARB'

            if vendor not in self.vendors:
                # we map this new vendor in the vendors set.
                self.vendors.add(vendor)

            namespaceType = enums.get('type')

            for enum in enums:
                if enum.tag != 'enum':
                    # this is not an enum => we skip it
                    continue

                lib = enum.get('name').split('_')[0]

                if lib not in self.libs:
                    # unknown library => we skip it
                    continue

                name = enum.get('name')[len(lib) + 1:]
                value = enum.get('value')
                type = enum.get('type')

                if not type:
                    # if no type specified, we get the namespace's default type
                    type = namespaceType

                self.consts[lib + '_' + name] = GLConst(lib, name, value, type)

        return True


    def exportConsts(self, path):
        with open(getScriptDir() + path,'w') as f:

            headerFile = GLConstHeader(f)
            headerFile.formatFileBegin()

            constNames = self.consts.keys()
            constNames.sort()

            for lib in GLDatabase.LIBS:
                headerFile.formatLibBegin(lib)

                for constName in constNames:
                    const = self.consts[constName]

                    if const.lib != lib:
                        continue

                    headerFile.formatLibConstant(lib, const.name, const.value)

                headerFile.formatLibEnd(lib)

            headerFile.formatFileEnd()


glDatabase = GLDatabase()

success = glDatabase.loadXML('gl.xml')
success = success and glDatabase.loadXML('egl.xml')
success = success and glDatabase.loadXML('glx.xml')
success = success and glDatabase.loadXML('wgl.xml')

if success:
    glDatabase.exportConsts('GLConsts.h')