blob: fa981a89a73d9bac3ca4a593cd933ec5d920ca6e [file] [log] [blame]
Jesse Hall4a739622014-05-13 21:21:46 -07001#!/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
17from __future__ import print_function
18from operator import itemgetter
19import collections
20import os.path
21import re
22import sys
23
24
25# Avoid endlessly adding to the path if this module is imported multiple
26# times, e.g. in an interactive session
27regpath = os.path.join(sys.path[0], "registry")
28if sys.path[1] != regpath:
29 sys.path.insert(1, regpath)
30import reg
31
32
Jesse Hall16f03922014-05-19 15:12:22 -070033AEP_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 Hall4a739622014-05-13 21:21:46 -070056def nonestr(s):
57 return s if s else ""
58
Pablo Ceballos46907542015-10-27 14:00:42 -070059def parseProto(elem):
60 type = nonestr(elem.text)
Jesse Hall4a739622014-05-13 21:21:46 -070061 name = None
62 for subelem in elem:
63 text = nonestr(subelem.text)
Jesse Hall4a739622014-05-13 21:21:46 -070064 if subelem.tag == 'name':
65 name = text
Jesse Hall4a739622014-05-13 21:21:46 -070066 else:
Pablo Ceballos46907542015-10-27 14:00:42 -070067 type += text
68 type += nonestr(subelem.tail)
69 return (type.strip(), name)
Jesse Hall4a739622014-05-13 21:21:46 -070070
Pablo Ceballos46907542015-10-27 14:00:42 -070071def parseParam(elem):
72 name = elem.find('name').text
73 declaration = ''.join(elem.itertext())
74 return (name, declaration)
Jesse Hall4a739622014-05-13 21:21:46 -070075
Pablo Ceballos46907542015-10-27 14:00:42 -070076# Format a list of (type, declaration) tuples as a C-style parameter list
Jesse Hall4a739622014-05-13 21:21:46 -070077def fmtParams(params):
78 if not params:
79 return 'void'
Jesse Hall4a739622014-05-13 21:21:46 -070080 return ', '.join(p[1] for p in params)
81
Pablo Ceballos46907542015-10-27 14:00:42 -070082# Format a list of (type, declaration) tuples as a C-style argument list
83def fmtArgs(params):
84 return ', '.join(p[0] for p in params)
Jesse Hall4a739622014-05-13 21:21:46 -070085
Alistair Strachanedfe72e2015-05-22 14:10:09 -070086def overrideSymbolName(sym, apiname):
87 # The wrapper intercepts various glGet and glGetString functions and
88 # (sometimes) calls the generated thunk which dispatches to the
89 # driver's implementation
90 wrapped_get_syms = {
91 'gles1' : [
92 'glGetString'
93 ],
94 'gles2' : [
95 'glGetString',
96 'glGetStringi',
97 'glGetBooleanv',
98 'glGetFloatv',
99 'glGetIntegerv',
100 'glGetInteger64v',
101 ],
102 }
103 if sym in wrapped_get_syms.get(apiname):
104 return '__' + sym
Jesse Hall4a739622014-05-13 21:21:46 -0700105 else:
106 return sym
107
108
109# Generate API trampoline templates:
110# <rtype> API_ENTRY(<name>)(<params>) {
111# CALL_GL_API(<name>, <args>);
112# // or
113# CALL_GL_API_RETURN(<name>, <args>);
114# }
115class TrampolineGen(reg.OutputGenerator):
116 def __init__(self):
117 reg.OutputGenerator.__init__(self, sys.stderr, sys.stderr, None)
118
119 def genCmd(self, cmd, name):
Krzysztof Kosiński1381b182018-04-01 20:53:27 -0700120 if re.search('Win32', name):
121 return
Jesse Hall4a739622014-05-13 21:21:46 -0700122 reg.OutputGenerator.genCmd(self, cmd, name)
123
Pablo Ceballos46907542015-10-27 14:00:42 -0700124 rtype, fname = parseProto(cmd.elem.find('proto'))
125 params = [parseParam(p) for p in cmd.elem.findall('param')]
Jesse Hall4a739622014-05-13 21:21:46 -0700126 call = 'CALL_GL_API' if rtype == 'void' else 'CALL_GL_API_RETURN'
127 print('%s API_ENTRY(%s)(%s) {\n'
128 ' %s(%s%s%s);\n'
129 '}'
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700130 % (rtype, overrideSymbolName(fname, self.genOpts.apiname),
131 fmtParams(params), call, fname,
Jesse Hall4a739622014-05-13 21:21:46 -0700132 ', ' if len(params) > 0 else '',
133 fmtArgs(params)),
134 file=self.outFile)
135
136
137
138# Collect all API prototypes across all families, remove duplicates,
Pablo Ceballos46907542015-10-27 14:00:42 -0700139# emit to entries.in and enums.in files.
Jesse Hall4a739622014-05-13 21:21:46 -0700140class ApiGenerator(reg.OutputGenerator):
141 def __init__(self):
142 reg.OutputGenerator.__init__(self, sys.stderr, sys.stderr, None)
143 self.cmds = []
144 self.enums = collections.OrderedDict()
145
146 def genCmd(self, cmd, name):
Krzysztof Kosiński1381b182018-04-01 20:53:27 -0700147 if re.search('Win32', name):
148 return
Jesse Hall4a739622014-05-13 21:21:46 -0700149 reg.OutputGenerator.genCmd(self, cmd, name)
Pablo Ceballos46907542015-10-27 14:00:42 -0700150 rtype, fname = parseProto(cmd.elem.find('proto'))
151 params = [parseParam(p) for p in cmd.elem.findall('param')]
Jesse Hall4a739622014-05-13 21:21:46 -0700152 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
Krzysztof Kosiński1381b182018-04-01 20:53:27 -0700173 if re.search('D3D|WIN32', name):
174 return
Jesse Hall4a739622014-05-13 21:21:46 -0700175
176 # Skip non-hex values (GL_TRUE, GL_FALSE, header guard junk)
177 if not re.search('0x[0-9A-Fa-f]+', value):
178 return
179
180 # Append 'u' or 'ull' type suffix if present
181 type = enuminfo.elem.get('type')
182 if type and type != 'i':
183 value += type
184
185 if value not in self.enums:
186 self.enums[value] = name
187
188 def finish(self):
189 # sort by function name, remove duplicates
190 self.cmds.sort(key=itemgetter('name'))
191 cmds = []
192 for cmd in self.cmds:
193 if len(cmds) == 0 or cmd != cmds[-1]:
194 cmds.append(cmd)
195 self.cmds = cmds
196
197 # Write entries.in
198 def writeEntries(self, outfile):
199 for cmd in self.cmds:
200 print('GL_ENTRY(%s, %s, %s)'
201 % (cmd['rtype'], cmd['name'], fmtParams(cmd['params'])),
202 file=outfile)
203
Jesse Hall4a739622014-05-13 21:21:46 -0700204 # Write enums.in
205 def writeEnums(self, outfile):
206 for enum in self.enums.iteritems():
207 print('GL_ENUM(%s,%s)' % (enum[0], enum[1]), file=outfile)
208
209
Jesse Hall16f03922014-05-19 15:12:22 -0700210# Generate .spec entries for use by legacy 'gen' script
211class SpecGenerator(reg.OutputGenerator):
212 def __init__(self):
213 reg.OutputGenerator.__init__(self, sys.stderr, sys.stderr, None)
214
215 def genCmd(self, cmd, name):
216 reg.OutputGenerator.genCmd(self, cmd, name)
Pablo Ceballos46907542015-10-27 14:00:42 -0700217 rtype, fname = parseProto(cmd.elem.find('proto'))
218 params = [parseParam(p) for p in cmd.elem.findall('param')]
Jesse Hall16f03922014-05-19 15:12:22 -0700219
220 print('%s %s ( %s )' % (rtype, fname, fmtParams(params)),
221 file=self.outFile)
222
223
Jesse Hall4a739622014-05-13 21:21:46 -0700224if __name__ == '__main__':
225 registry = reg.Registry()
226 registry.loadFile('registry/gl.xml')
227
228 registry.setGenerator(TrampolineGen())
229 TRAMPOLINE_OPTIONS = [
230 reg.GeneratorOptions(
231 apiname = 'gles1',
232 profile = 'common',
233 filename = '../../libs/GLES_CM/gl_api.in'),
234 reg.GeneratorOptions(
235 apiname = 'gles1',
236 profile = 'common',
237 emitversions = None,
238 defaultExtensions = 'gles1',
239 filename = '../../libs/GLES_CM/glext_api.in'),
240 reg.GeneratorOptions(
241 apiname = 'gles2',
Jesse Hall4a739622014-05-13 21:21:46 -0700242 profile = 'common',
243 filename = '../../libs/GLES2/gl2_api.in'),
244 reg.GeneratorOptions(
245 apiname = 'gles2',
Jesse Hall4a739622014-05-13 21:21:46 -0700246 profile = 'common',
247 emitversions = None,
248 defaultExtensions = 'gles2',
249 filename = '../../libs/GLES2/gl2ext_api.in')]
250 for opts in TRAMPOLINE_OPTIONS:
251 registry.apiGen(opts)
252
253 apigen = ApiGenerator()
254 registry.setGenerator(apigen)
255 API_OPTIONS = [
256 # Generate non-extension versions of each API first, then extensions,
257 # so that if an extension enum was later standardized, we see the non-
258 # suffixed version first.
259 reg.GeneratorOptions(
260 apiname = 'gles1',
261 profile = 'common'),
262 reg.GeneratorOptions(
263 apiname = 'gles2',
Jesse Hall4a739622014-05-13 21:21:46 -0700264 profile = 'common'),
265 reg.GeneratorOptions(
266 apiname = 'gles1',
267 profile = 'common',
268 emitversions = None,
269 defaultExtensions = 'gles1'),
270 reg.GeneratorOptions(
271 apiname = 'gles2',
Jesse Hall4a739622014-05-13 21:21:46 -0700272 profile = 'common',
273 emitversions = None,
274 defaultExtensions = 'gles2')]
275 for opts in API_OPTIONS:
276 registry.apiGen(opts)
277 apigen.finish()
278 with open('../../libs/entries.in', 'w') as f:
279 apigen.writeEntries(f)
Jesse Hall4a739622014-05-13 21:21:46 -0700280 with open('../../libs/enums.in', 'w') as f:
281 apigen.writeEnums(f)
Jesse Hall16f03922014-05-19 15:12:22 -0700282
283 registry.setGenerator(SpecGenerator())
284 SPEC_OPTIONS = [
285 reg.GeneratorOptions(
286 apiname = 'gles2',
287 profile = 'common',
288 versions = '3\.1',
289 filename = '../glgen/specs/gles11/GLES31.spec'),
290 reg.GeneratorOptions(
291 apiname = 'gles2',
292 profile = 'common',
293 emitversions = None,
294 defaultExtensions = None,
295 addExtensions = '^({})$'.format('|'.join(AEP_EXTENSIONS)),
Pablo Ceballos8a59ca72015-10-30 10:31:22 -0700296 filename = '../glgen/specs/gles11/GLES31Ext.spec'),
297 reg.GeneratorOptions(
298 apiname = 'gles2',
299 profile = 'common',
300 versions = '3\.2',
301 filename = '../glgen/specs/gles11/GLES32.spec')]
Jesse Hall16f03922014-05-19 15:12:22 -0700302 # SpecGenerator creates a good starting point, but the CFunc.java parser is
303 # so terrible that the .spec file needs a lot of manual massaging before
304 # it works. Commenting this out to avoid accidentally overwriting all the
305 # manual modifications.
306 #
307 # Eventually this script should generate the Java and JNI code directly,
308 # skipping the intermediate .spec step, and obsoleting the existing
309 # ../glgen system.
310 #
311 # for opts in SPEC_OPTIONS:
312 # registry.apiGen(opts)
313