1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/gl/GLParseRegistryXML.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,216 @@ 1.4 +#!/usr/bin/env python 1.5 +# coding=utf8 1.6 + 1.7 + 1.8 +################################################################################ 1.9 +# TUTORIAL 1.10 +# This script will generate GLConsts.h 1.11 +# 1.12 +# Step 1: 1.13 +# Download the last gl.xml, egl.xml, glx.xml and wgl.xml from 1.14 +# http://www.opengl.org/registry/#specfiles into a directory of your choice 1.15 +# 1.16 +# Step 2: 1.17 +# Execute this script ./GLParseRegistryXML.py [your dir containing XML files] 1.18 +# 1.19 +# Step 3: 1.20 +# Do not add the downloaded XML in the patch 1.21 +# 1.22 +# Step 4: 1.23 +# Enjoy =) 1.24 +# 1.25 +################################################################################ 1.26 + 1.27 +# includes 1.28 +import os 1.29 +import sys 1.30 +import xml.etree.ElementTree 1.31 + 1.32 + 1.33 +################################################################################ 1.34 +# export management 1.35 + 1.36 +class GLConstHeader: 1.37 + def __init__(self, f): 1.38 + self.f = f 1.39 + 1.40 + 1.41 + def write(self, arg): 1.42 + if isinstance(arg, list): 1.43 + self.f.write('\n'.join(arg) + '\n') 1.44 + elif isinstance(arg, (int, long)): 1.45 + self.f.write('\n' * arg) 1.46 + else: 1.47 + self.f.write(str(arg) + '\n') 1.48 + 1.49 + 1.50 + def formatFileBegin(self): 1.51 + self.write([ 1.52 + '/* This Source Code Form is subject to the terms of the Mozilla Public', 1.53 + ' * License, v. 2.0. If a copy of the MPL was not distributed with this', 1.54 + ' * file, You can obtain one at http://mozilla.org/MPL/2.0/. */', 1.55 + '', 1.56 + '#ifndef GLCONSTS_H_', 1.57 + '#define GLCONSTS_H_', 1.58 + '', 1.59 + '/**', 1.60 + ' * GENERATED FILE, DO NOT MODIFY DIRECTLY.', 1.61 + ' * This is a file generated directly from the official OpenGL registry', 1.62 + ' * xml available http://www.opengl.org/registry/#specfiles.', 1.63 + ' *', 1.64 + ' * To generate this file, see tutorial in \'GLParseRegistryXML.py\'.', 1.65 + ' */', 1.66 + '' 1.67 + ]) 1.68 + 1.69 + 1.70 + def formatLibBegin(self, lib): 1.71 + # lib would be 'GL', 'EGL', 'GLX' or 'WGL' 1.72 + self.write('// ' + lib) 1.73 + 1.74 + 1.75 + def formatLibConstant(self, lib, name, value): 1.76 + # lib would be 'GL', 'EGL', 'GLX' or 'WGL' 1.77 + # name is the name of the const (example: MAX_TEXTURE_SIZE) 1.78 + # value is the value of the const (example: 0xABCD) 1.79 + 1.80 + define = '#define LOCAL_' + lib + '_' + name 1.81 + whitespace = 60 - len(define) 1.82 + 1.83 + if whitespace < 0: 1.84 + whitespace = whitespace % 8 1.85 + 1.86 + self.write(define + ' ' * whitespace + ' ' + value) 1.87 + 1.88 + 1.89 + def formatLibEnd(self, lib): 1.90 + # lib would be 'GL', 'EGL', 'GLX' or 'WGL' 1.91 + self.write(2) 1.92 + 1.93 + 1.94 + def formatFileEnd(self): 1.95 + self.write([ 1.96 + '', 1.97 + '#endif // GLCONSTS_H_' 1.98 + ]) 1.99 + 1.100 + 1.101 +################################################################################ 1.102 +# underground code 1.103 + 1.104 +def getScriptDir(): 1.105 + return os.path.dirname(__file__) + '/' 1.106 + 1.107 + 1.108 +def getXMLDir(): 1.109 + if len(sys.argv) == 1: 1.110 + return './' 1.111 + 1.112 + dirPath = sys.argv[1] 1.113 + if dirPath[-1] != '/': 1.114 + dirPath += '/' 1.115 + 1.116 + return dirPath 1.117 + 1.118 + 1.119 +class GLConst: 1.120 + def __init__(self, lib, name, value, type): 1.121 + self.lib = lib 1.122 + self.name = name 1.123 + self.value = value 1.124 + self.type = type 1.125 + 1.126 + 1.127 +class GLDatabase: 1.128 + 1.129 + LIBS = ['GL', 'EGL', 'GLX', 'WGL'] 1.130 + 1.131 + def __init__(self): 1.132 + self.consts = {} 1.133 + self.libs = set(GLDatabase.LIBS) 1.134 + self.vendors = set(['EXT', 'ATI']) 1.135 + # there is no vendor="EXT" and vendor="ATI" in gl.xml, 1.136 + # so we manualy declare them 1.137 + 1.138 + 1.139 + def loadXML(self, path): 1.140 + xmlPath = getXMLDir() + path 1.141 + 1.142 + if not os.path.isfile(xmlPath): 1.143 + print 'missing file "' + xmlPath + '"' 1.144 + return False 1.145 + 1.146 + tree = xml.etree.ElementTree.parse(xmlPath) 1.147 + root = tree.getroot() 1.148 + 1.149 + for enums in root.iter('enums'): 1.150 + vendor = enums.get('vendor') 1.151 + if not vendor: 1.152 + # there some standart enums that do have the vendor attribute, 1.153 + # so we fake them as ARB's enums 1.154 + vendor = 'ARB' 1.155 + 1.156 + if vendor not in self.vendors: 1.157 + # we map this new vendor in the vendors set. 1.158 + self.vendors.add(vendor) 1.159 + 1.160 + namespaceType = enums.get('type') 1.161 + 1.162 + for enum in enums: 1.163 + if enum.tag != 'enum': 1.164 + # this is not an enum => we skip it 1.165 + continue 1.166 + 1.167 + lib = enum.get('name').split('_')[0] 1.168 + 1.169 + if lib not in self.libs: 1.170 + # unknown library => we skip it 1.171 + continue 1.172 + 1.173 + name = enum.get('name')[len(lib) + 1:] 1.174 + value = enum.get('value') 1.175 + type = enum.get('type') 1.176 + 1.177 + if not type: 1.178 + # if no type specified, we get the namespace's default type 1.179 + type = namespaceType 1.180 + 1.181 + self.consts[lib + '_' + name] = GLConst(lib, name, value, type) 1.182 + 1.183 + return True 1.184 + 1.185 + 1.186 + def exportConsts(self, path): 1.187 + with open(getScriptDir() + path,'w') as f: 1.188 + 1.189 + headerFile = GLConstHeader(f) 1.190 + headerFile.formatFileBegin() 1.191 + 1.192 + constNames = self.consts.keys() 1.193 + constNames.sort() 1.194 + 1.195 + for lib in GLDatabase.LIBS: 1.196 + headerFile.formatLibBegin(lib) 1.197 + 1.198 + for constName in constNames: 1.199 + const = self.consts[constName] 1.200 + 1.201 + if const.lib != lib: 1.202 + continue 1.203 + 1.204 + headerFile.formatLibConstant(lib, const.name, const.value) 1.205 + 1.206 + headerFile.formatLibEnd(lib) 1.207 + 1.208 + headerFile.formatFileEnd() 1.209 + 1.210 + 1.211 +glDatabase = GLDatabase() 1.212 + 1.213 +success = glDatabase.loadXML('gl.xml') 1.214 +success = success and glDatabase.loadXML('egl.xml') 1.215 +success = success and glDatabase.loadXML('glx.xml') 1.216 +success = success and glDatabase.loadXML('wgl.xml') 1.217 + 1.218 +if success: 1.219 + glDatabase.exportConsts('GLConsts.h')