Jesse Hall | 4a73962 | 2014-05-13 21:21:46 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2014 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | from __future__ import print_function |
| 18 | from operator import itemgetter |
| 19 | import collections |
| 20 | import os.path |
| 21 | import re |
| 22 | import sys |
| 23 | |
| 24 | |
| 25 | # Avoid endlessly adding to the path if this module is imported multiple |
| 26 | # times, e.g. in an interactive session |
| 27 | regpath = os.path.join(sys.path[0], "registry") |
| 28 | if sys.path[1] != regpath: |
| 29 | sys.path.insert(1, regpath) |
| 30 | import reg |
| 31 | |
| 32 | |
Jesse Hall | 16f0392 | 2014-05-19 15:12:22 -0700 | [diff] [blame] | 33 | AEP_EXTENSIONS = [ |
| 34 | 'GL_KHR_blend_equation_advanced', |
| 35 | 'GL_KHR_debug', |
| 36 | 'GL_KHR_texture_compression_astc_ldr', |
| 37 | 'GL_OES_sample_shading', |
| 38 | 'GL_OES_sample_variables', |
| 39 | 'GL_OES_shader_image_atomic', |
| 40 | 'GL_OES_shader_multisample_interpolation', |
| 41 | 'GL_OES_texture_stencil8', |
| 42 | 'GL_OES_texture_storage_multisample_2d_array', |
| 43 | 'GL_EXT_copy_image', |
| 44 | 'GL_EXT_draw_buffers_indexed', |
| 45 | 'GL_EXT_geometry_shader', |
| 46 | 'GL_EXT_gpu_shader5', |
| 47 | 'GL_EXT_primitive_bounding_box', |
| 48 | 'GL_EXT_shader_io_blocks', |
| 49 | 'GL_EXT_tessellation_shader', |
| 50 | 'GL_EXT_texture_border_clamp', |
| 51 | 'GL_EXT_texture_buffer', |
| 52 | 'GL_EXT_texture_cube_map_array', |
| 53 | 'GL_EXT_texture_sRGB_decode'] |
| 54 | |
| 55 | |
Jesse Hall | 4a73962 | 2014-05-13 21:21:46 -0700 | [diff] [blame] | 56 | def nonestr(s): |
| 57 | return s if s else "" |
| 58 | |
| 59 | |
| 60 | def parseTypedName(elem): |
| 61 | type = [nonestr(elem.text)] |
| 62 | name = None |
| 63 | for subelem in elem: |
| 64 | text = nonestr(subelem.text) |
| 65 | tail = nonestr(subelem.tail) |
| 66 | if subelem.tag == 'name': |
| 67 | name = text |
| 68 | break |
| 69 | else: |
| 70 | type.extend([text, tail]) |
| 71 | return (''.join(type).strip(), name) |
| 72 | |
| 73 | |
| 74 | # Format a list of (type, name) tuples as a C-style parameter list |
| 75 | def fmtParams(params): |
| 76 | if not params: |
| 77 | return 'void' |
| 78 | return ', '.join(['%s %s' % (p[0], p[1]) for p in params]) |
| 79 | |
| 80 | # Format a list of (type, name) tuples as a C-style argument list |
| 81 | def fmtArgs(params): |
| 82 | return ', '.join(p[1] for p in params) |
| 83 | |
| 84 | # Format a list of (type, name) tuples as comma-separated '"type", name' |
| 85 | def fmtTypeNameList(params): |
| 86 | return ', '.join(['"%s", %s' % (p[0], p[1]) for p in params]) |
| 87 | |
| 88 | |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame^] | 89 | def overrideSymbolName(sym, apiname): |
| 90 | # The wrapper intercepts various glGet and glGetString functions and |
| 91 | # (sometimes) calls the generated thunk which dispatches to the |
| 92 | # driver's implementation |
| 93 | wrapped_get_syms = { |
| 94 | 'gles1' : [ |
| 95 | 'glGetString' |
| 96 | ], |
| 97 | 'gles2' : [ |
| 98 | 'glGetString', |
| 99 | 'glGetStringi', |
| 100 | 'glGetBooleanv', |
| 101 | 'glGetFloatv', |
| 102 | 'glGetIntegerv', |
| 103 | 'glGetInteger64v', |
| 104 | ], |
| 105 | } |
| 106 | if sym in wrapped_get_syms.get(apiname): |
| 107 | return '__' + sym |
Jesse Hall | 4a73962 | 2014-05-13 21:21:46 -0700 | [diff] [blame] | 108 | else: |
| 109 | return sym |
| 110 | |
| 111 | |
| 112 | # Generate API trampoline templates: |
| 113 | # <rtype> API_ENTRY(<name>)(<params>) { |
| 114 | # CALL_GL_API(<name>, <args>); |
| 115 | # // or |
| 116 | # CALL_GL_API_RETURN(<name>, <args>); |
| 117 | # } |
| 118 | class TrampolineGen(reg.OutputGenerator): |
| 119 | def __init__(self): |
| 120 | reg.OutputGenerator.__init__(self, sys.stderr, sys.stderr, None) |
| 121 | |
| 122 | def genCmd(self, cmd, name): |
| 123 | reg.OutputGenerator.genCmd(self, cmd, name) |
| 124 | |
| 125 | rtype, fname = parseTypedName(cmd.elem.find('proto')) |
| 126 | params = [parseTypedName(p) for p in cmd.elem.findall('param')] |
| 127 | |
| 128 | call = 'CALL_GL_API' if rtype == 'void' else 'CALL_GL_API_RETURN' |
| 129 | print('%s API_ENTRY(%s)(%s) {\n' |
| 130 | ' %s(%s%s%s);\n' |
| 131 | '}' |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame^] | 132 | % (rtype, overrideSymbolName(fname, self.genOpts.apiname), |
| 133 | fmtParams(params), call, fname, |
Jesse Hall | 4a73962 | 2014-05-13 21:21:46 -0700 | [diff] [blame] | 134 | ', ' if len(params) > 0 else '', |
| 135 | fmtArgs(params)), |
| 136 | file=self.outFile) |
| 137 | |
| 138 | |
| 139 | |
| 140 | # Collect all API prototypes across all families, remove duplicates, |
| 141 | # emit to entries.in and trace.in files. |
| 142 | class ApiGenerator(reg.OutputGenerator): |
| 143 | def __init__(self): |
| 144 | reg.OutputGenerator.__init__(self, sys.stderr, sys.stderr, None) |
| 145 | self.cmds = [] |
| 146 | self.enums = collections.OrderedDict() |
| 147 | |
| 148 | def genCmd(self, cmd, name): |
| 149 | reg.OutputGenerator.genCmd(self, cmd, name) |
| 150 | rtype, fname = parseTypedName(cmd.elem.find('proto')) |
| 151 | params = [parseTypedName(p) for p in cmd.elem.findall('param')] |
| 152 | self.cmds.append({'rtype': rtype, 'name': fname, 'params': params}) |
| 153 | |
| 154 | def genEnum(self, enuminfo, name): |
| 155 | reg.OutputGenerator.genEnum(self, enuminfo, name) |
| 156 | value = enuminfo.elem.get('value') |
| 157 | |
| 158 | # Skip bitmask enums. Pattern matches: |
| 159 | # - GL_DEPTH_BUFFER_BIT |
| 160 | # - GL_MAP_INVALIDATE_BUFFER_BIT_EXT |
| 161 | # - GL_COLOR_BUFFER_BIT1_QCOM |
| 162 | # but not |
| 163 | # - GL_DEPTH_BITS |
| 164 | # - GL_QUERY_COUNTER_BITS_EXT |
| 165 | # |
| 166 | # TODO: Assuming a naming pattern and using a regex is what the |
| 167 | # old glenumsgen script did. But the registry XML knows which enums are |
| 168 | # parts of bitmask groups, so we should just use that. I'm not sure how |
| 169 | # to get the information out though, and it's not critical right now, |
| 170 | # so leaving for later. |
| 171 | if re.search('_BIT($|\d*_)', name): |
| 172 | return |
| 173 | |
| 174 | # Skip non-hex values (GL_TRUE, GL_FALSE, header guard junk) |
| 175 | if not re.search('0x[0-9A-Fa-f]+', value): |
| 176 | return |
| 177 | |
| 178 | # Append 'u' or 'ull' type suffix if present |
| 179 | type = enuminfo.elem.get('type') |
| 180 | if type and type != 'i': |
| 181 | value += type |
| 182 | |
| 183 | if value not in self.enums: |
| 184 | self.enums[value] = name |
| 185 | |
| 186 | def finish(self): |
| 187 | # sort by function name, remove duplicates |
| 188 | self.cmds.sort(key=itemgetter('name')) |
| 189 | cmds = [] |
| 190 | for cmd in self.cmds: |
| 191 | if len(cmds) == 0 or cmd != cmds[-1]: |
| 192 | cmds.append(cmd) |
| 193 | self.cmds = cmds |
| 194 | |
| 195 | # Write entries.in |
| 196 | def writeEntries(self, outfile): |
| 197 | for cmd in self.cmds: |
| 198 | print('GL_ENTRY(%s, %s, %s)' |
| 199 | % (cmd['rtype'], cmd['name'], fmtParams(cmd['params'])), |
| 200 | file=outfile) |
| 201 | |
| 202 | # Write traces.in |
| 203 | def writeTrace(self, outfile): |
| 204 | for cmd in self.cmds: |
| 205 | if cmd['rtype'] == 'void': |
| 206 | ret = '_VOID(' |
| 207 | else: |
| 208 | ret = '(%s, ' % cmd['rtype'] |
| 209 | |
| 210 | params = cmd['params'] |
| 211 | if len(params) > 0: |
| 212 | typeNameList = ', ' + fmtTypeNameList(params) |
| 213 | else: |
| 214 | typeNameList = '' |
| 215 | |
| 216 | print('TRACE_GL%s%s, (%s), (%s), %d%s)' |
| 217 | % (ret, cmd['name'], |
| 218 | fmtParams(params), fmtArgs(params), |
| 219 | len(params), typeNameList), |
| 220 | file=outfile) |
| 221 | |
| 222 | # Write enums.in |
| 223 | def writeEnums(self, outfile): |
| 224 | for enum in self.enums.iteritems(): |
| 225 | print('GL_ENUM(%s,%s)' % (enum[0], enum[1]), file=outfile) |
| 226 | |
| 227 | |
Jesse Hall | 16f0392 | 2014-05-19 15:12:22 -0700 | [diff] [blame] | 228 | # Generate .spec entries for use by legacy 'gen' script |
| 229 | class SpecGenerator(reg.OutputGenerator): |
| 230 | def __init__(self): |
| 231 | reg.OutputGenerator.__init__(self, sys.stderr, sys.stderr, None) |
| 232 | |
| 233 | def genCmd(self, cmd, name): |
| 234 | reg.OutputGenerator.genCmd(self, cmd, name) |
| 235 | rtype, fname = parseTypedName(cmd.elem.find('proto')) |
| 236 | params = [parseTypedName(p) for p in cmd.elem.findall('param')] |
| 237 | |
| 238 | print('%s %s ( %s )' % (rtype, fname, fmtParams(params)), |
| 239 | file=self.outFile) |
| 240 | |
| 241 | |
Jesse Hall | 4a73962 | 2014-05-13 21:21:46 -0700 | [diff] [blame] | 242 | if __name__ == '__main__': |
| 243 | registry = reg.Registry() |
| 244 | registry.loadFile('registry/gl.xml') |
| 245 | |
| 246 | registry.setGenerator(TrampolineGen()) |
| 247 | TRAMPOLINE_OPTIONS = [ |
| 248 | reg.GeneratorOptions( |
| 249 | apiname = 'gles1', |
| 250 | profile = 'common', |
| 251 | filename = '../../libs/GLES_CM/gl_api.in'), |
| 252 | reg.GeneratorOptions( |
| 253 | apiname = 'gles1', |
| 254 | profile = 'common', |
| 255 | emitversions = None, |
| 256 | defaultExtensions = 'gles1', |
| 257 | filename = '../../libs/GLES_CM/glext_api.in'), |
| 258 | reg.GeneratorOptions( |
| 259 | apiname = 'gles2', |
Jesse Hall | 4a73962 | 2014-05-13 21:21:46 -0700 | [diff] [blame] | 260 | profile = 'common', |
| 261 | filename = '../../libs/GLES2/gl2_api.in'), |
| 262 | reg.GeneratorOptions( |
| 263 | apiname = 'gles2', |
Jesse Hall | 4a73962 | 2014-05-13 21:21:46 -0700 | [diff] [blame] | 264 | profile = 'common', |
| 265 | emitversions = None, |
| 266 | defaultExtensions = 'gles2', |
| 267 | filename = '../../libs/GLES2/gl2ext_api.in')] |
| 268 | for opts in TRAMPOLINE_OPTIONS: |
| 269 | registry.apiGen(opts) |
| 270 | |
| 271 | apigen = ApiGenerator() |
| 272 | registry.setGenerator(apigen) |
| 273 | API_OPTIONS = [ |
| 274 | # Generate non-extension versions of each API first, then extensions, |
| 275 | # so that if an extension enum was later standardized, we see the non- |
| 276 | # suffixed version first. |
| 277 | reg.GeneratorOptions( |
| 278 | apiname = 'gles1', |
| 279 | profile = 'common'), |
| 280 | reg.GeneratorOptions( |
| 281 | apiname = 'gles2', |
Jesse Hall | 4a73962 | 2014-05-13 21:21:46 -0700 | [diff] [blame] | 282 | profile = 'common'), |
| 283 | reg.GeneratorOptions( |
| 284 | apiname = 'gles1', |
| 285 | profile = 'common', |
| 286 | emitversions = None, |
| 287 | defaultExtensions = 'gles1'), |
| 288 | reg.GeneratorOptions( |
| 289 | apiname = 'gles2', |
Jesse Hall | 4a73962 | 2014-05-13 21:21:46 -0700 | [diff] [blame] | 290 | profile = 'common', |
| 291 | emitversions = None, |
| 292 | defaultExtensions = 'gles2')] |
| 293 | for opts in API_OPTIONS: |
| 294 | registry.apiGen(opts) |
| 295 | apigen.finish() |
| 296 | with open('../../libs/entries.in', 'w') as f: |
| 297 | apigen.writeEntries(f) |
| 298 | with open('../../libs/trace.in', 'w') as f: |
| 299 | apigen.writeTrace(f) |
| 300 | with open('../../libs/enums.in', 'w') as f: |
| 301 | apigen.writeEnums(f) |
Jesse Hall | 16f0392 | 2014-05-19 15:12:22 -0700 | [diff] [blame] | 302 | |
| 303 | registry.setGenerator(SpecGenerator()) |
| 304 | SPEC_OPTIONS = [ |
| 305 | reg.GeneratorOptions( |
| 306 | apiname = 'gles2', |
| 307 | profile = 'common', |
| 308 | versions = '3\.1', |
| 309 | filename = '../glgen/specs/gles11/GLES31.spec'), |
| 310 | reg.GeneratorOptions( |
| 311 | apiname = 'gles2', |
| 312 | profile = 'common', |
| 313 | emitversions = None, |
| 314 | defaultExtensions = None, |
| 315 | addExtensions = '^({})$'.format('|'.join(AEP_EXTENSIONS)), |
| 316 | filename = '../glgen/specs/gles11/GLES31Ext.spec')] |
| 317 | # SpecGenerator creates a good starting point, but the CFunc.java parser is |
| 318 | # so terrible that the .spec file needs a lot of manual massaging before |
| 319 | # it works. Commenting this out to avoid accidentally overwriting all the |
| 320 | # manual modifications. |
| 321 | # |
| 322 | # Eventually this script should generate the Java and JNI code directly, |
| 323 | # skipping the intermediate .spec step, and obsoleting the existing |
| 324 | # ../glgen system. |
| 325 | # |
| 326 | # for opts in SPEC_OPTIONS: |
| 327 | # registry.apiGen(opts) |
| 328 | |