glgen.py 11 KB

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