5 ################################################################################
7 # This script will generate GLConsts.h
10 # Download the last gl.xml, egl.xml, glx.xml and wgl.xml from
11 # http://www.opengl.org/registry/#specfiles into a directory of your choice
14 # Execute this script ./GLParseRegistryXML.py [your dir containing XML files]
17 # Do not add the downloaded XML in the patch
22 ################################################################################
25 from __future__
import print_function
28 import xml
.etree
.ElementTree
31 ################################################################################
35 def __init__(self
, f
):
39 if isinstance(arg
, list):
40 self
.f
.write('\n'.join(arg
) + '\n')
41 elif isinstance(arg
, (int, long)):
42 self
.f
.write('\n' * arg
)
44 self
.f
.write(str(arg
) + '\n')
46 def formatFileBegin(self
):
48 '/* This Source Code Form is subject to the terms of the Mozilla Public',
49 ' * License, v. 2.0. If a copy of the MPL was not distributed with this',
50 ' * file, You can obtain one at http://mozilla.org/MPL/2.0/. */',
52 '#ifndef GLCONSTS_H_',
53 '#define GLCONSTS_H_',
56 ' * GENERATED FILE, DO NOT MODIFY DIRECTLY.',
57 ' * This is a file generated directly from the official OpenGL registry',
58 ' * xml available http://www.opengl.org/registry/#specfiles.',
60 ' * To generate this file, see tutorial in \'GLParseRegistryXML.py\'.',
65 def formatLibBegin(self
, lib
):
66 # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
67 self
.write('// ' + lib
)
69 def formatLibConstant(self
, lib
, name
, value
):
70 # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
71 # name is the name of the const (example: MAX_TEXTURE_SIZE)
72 # value is the value of the const (example: 0xABCD)
74 define
= '#define LOCAL_' + lib
+ '_' + name
75 whitespace
= 60 - len(define
)
78 whitespace
= whitespace
% 8
80 self
.write(define
+ ' ' * whitespace
+ ' ' + value
)
82 def formatLibEnd(self
, lib
):
83 # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
86 def formatFileEnd(self
):
89 '#endif // GLCONSTS_H_'
93 ################################################################################
97 return os
.path
.dirname(__file__
) + '/'
101 if len(sys
.argv
) == 1:
104 dirPath
= sys
.argv
[1]
105 if dirPath
[-1] != '/':
112 def __init__(self
, lib
, name
, value
, type):
121 LIBS
= ['GL', 'EGL', 'GLX', 'WGL']
125 self
.libs
= set(GLDatabase
.LIBS
)
126 self
.vendors
= set(['EXT', 'ATI'])
127 # there is no vendor="EXT" and vendor="ATI" in gl.xml,
128 # so we manualy declare them
130 def loadXML(self
, path
):
131 xmlPath
= getXMLDir() + path
133 if not os
.path
.isfile(xmlPath
):
134 print('missing file "' + xmlPath
+ '"')
137 tree
= xml
.etree
.ElementTree
.parse(xmlPath
)
138 root
= tree
.getroot()
140 for enums
in root
.iter('enums'):
141 vendor
= enums
.get('vendor')
143 # there some standart enums that do have the vendor attribute,
144 # so we fake them as ARB's enums
147 if vendor
not in self
.vendors
:
148 # we map this new vendor in the vendors set.
149 self
.vendors
.add(vendor
)
151 namespaceType
= enums
.get('type')
154 if enum
.tag
!= 'enum':
155 # this is not an enum => we skip it
158 lib
= enum
.get('name').split('_')[0]
160 if lib
not in self
.libs
:
161 # unknown library => we skip it
164 name
= enum
.get('name')[len(lib
) + 1:]
165 value
= enum
.get('value')
166 type = enum
.get('type')
169 # if no type specified, we get the namespace's default type
172 self
.consts
[lib
+ '_' + name
] = GLConst(lib
, name
, value
, type)
176 def exportConsts(self
, path
):
177 with
open(getScriptDir() + path
, 'w') as f
:
179 headerFile
= GLConstHeader(f
)
180 headerFile
.formatFileBegin()
182 constNames
= self
.consts
.keys()
185 for lib
in GLDatabase
.LIBS
:
186 headerFile
.formatLibBegin(lib
)
188 for constName
in constNames
:
189 const
= self
.consts
[constName
]
194 headerFile
.formatLibConstant(lib
, const
.name
, const
.value
)
196 headerFile
.formatLibEnd(lib
)
198 headerFile
.formatFileEnd()
201 glDatabase
= GLDatabase()
203 success
= glDatabase
.loadXML('gl.xml')
204 success
= success
and glDatabase
.loadXML('egl.xml')
205 success
= success
and glDatabase
.loadXML('glx.xml')
206 success
= success
and glDatabase
.loadXML('wgl.xml')
209 glDatabase
.exportConsts('GLConsts.h')