blob: 6dc9a13eb1b7513a2ace302d1b85b57bf2a1dde9 [file] [log] [blame]
Jeff Gilbert1b605ee2017-10-30 18:41:46 -07001#!/usr/bin/python2
Jamie Madill2e16d962017-04-19 14:06:36 -04002#
3# Copyright 2017 The ANGLE Project Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# generate_entry_points.py:
8# Generates the OpenGL bindings and entry point layers for ANGLE.
9
Corentin Wallez2e568cf2017-09-18 17:05:22 -040010import sys, os, pprint, json
Jamie Madill2e16d962017-04-19 14:06:36 -040011import xml.etree.ElementTree as etree
12from datetime import date
13
Jamie Madillfa920eb2018-01-04 11:45:50 -050014# List of supported extensions. Add to this list to enable new extensions
15# available in gl.xml.
Brandon Jones416aaf92018-04-10 08:10:16 -070016
17angle_extensions = [
18 # ANGLE extensions
19 "GL_CHROMIUM_bind_uniform_location",
20 "GL_CHROMIUM_framebuffer_mixed_samples",
21 "GL_CHROMIUM_path_rendering",
22 "GL_CHROMIUM_copy_texture",
23 "GL_CHROMIUM_copy_compressed_texture",
24 "GL_ANGLE_request_extension",
25 "GL_ANGLE_robust_client_memory",
26 "GL_ANGLE_multiview",
27]
28
Lingfeng Yanga0648782018-03-12 14:45:25 -070029gles1_extensions = [
30 # ES1 (Possibly the min set of extensions needed by Android)
31 "GL_OES_draw_texture",
32 "GL_OES_framebuffer_object",
33 "GL_OES_matrix_palette",
Geoff Lang2aaa7b42018-01-12 17:17:27 -050034 "GL_OES_point_size_array",
35 "GL_OES_query_matrix",
Lingfeng Yanga0648782018-03-12 14:45:25 -070036 "GL_OES_texture_cube_map",
37]
38
39# List of GLES1 extensions for which we don't need to add Context.h decls.
40gles1_no_context_decl_extensions = [
41 "GL_OES_framebuffer_object",
42]
43
44# List of GLES1 API calls that have had their semantics changed in later GLES versions, but the
45# name was kept the same
46gles1_overloaded = [
47 "glGetPointerv",
48]
49
Brandon Jones416aaf92018-04-10 08:10:16 -070050supported_extensions = sorted(angle_extensions + gles1_extensions + [
Geoff Lang2aaa7b42018-01-12 17:17:27 -050051 # ES2+
Jamie Madillfa920eb2018-01-04 11:45:50 -050052 "GL_ANGLE_framebuffer_blit",
53 "GL_ANGLE_framebuffer_multisample",
54 "GL_ANGLE_instanced_arrays",
55 "GL_ANGLE_translated_shader_source",
56 "GL_EXT_debug_marker",
57 "GL_EXT_discard_framebuffer",
58 "GL_EXT_disjoint_timer_query",
59 "GL_EXT_draw_buffers",
60 "GL_EXT_map_buffer_range",
61 "GL_EXT_occlusion_query_boolean",
62 "GL_EXT_robustness",
63 "GL_EXT_texture_storage",
64 "GL_KHR_debug",
65 "GL_NV_fence",
66 "GL_OES_EGL_image",
67 "GL_OES_get_program_binary",
68 "GL_OES_mapbuffer",
69 "GL_OES_vertex_array_object",
70])
71
72# This is a list of exceptions for entry points which don't want to have
73# the EVENT macro. This is required for some debug marker entry points.
74no_event_marker_exceptions_list = sorted([
75 "glPushGroupMarkerEXT",
76 "glPopGroupMarkerEXT",
77 "glInsertEventMarkerEXT",
78])
79
80# Strip these suffixes from Context entry point names. NV is excluded (for now).
Brandon Jones416aaf92018-04-10 08:10:16 -070081strip_suffixes = ["ANGLE", "EXT", "KHR", "OES", "CHROMIUM"]
Jamie Madillfa920eb2018-01-04 11:45:50 -050082
Jamie Madill2e16d962017-04-19 14:06:36 -040083template_entry_point_header = """// GENERATED FILE - DO NOT EDIT.
84// Generated by {script_name} using data from {data_source_name}.
85//
86// Copyright {year} The ANGLE Project Authors. All rights reserved.
87// Use of this source code is governed by a BSD-style license that can be
88// found in the LICENSE file.
89//
Jamie Madillc8c9a242018-01-02 13:39:00 -050090// entry_points_gles_{annotation_lower}_autogen.h:
91// Defines the GLES {comment} entry points.
Jamie Madill2e16d962017-04-19 14:06:36 -040092
Jamie Madillc8c9a242018-01-02 13:39:00 -050093#ifndef LIBGLESV2_ENTRY_POINTS_GLES_{annotation_upper}_AUTOGEN_H_
94#define LIBGLESV2_ENTRY_POINTS_GLES_{annotation_upper}_AUTOGEN_H_
Jamie Madill2e16d962017-04-19 14:06:36 -040095
Jamie Madillc8c9a242018-01-02 13:39:00 -050096{includes}
97
Jamie Madill2e16d962017-04-19 14:06:36 -040098namespace gl
99{{
100{entry_points}
101}} // namespace gl
102
Jamie Madillc8c9a242018-01-02 13:39:00 -0500103#endif // LIBGLESV2_ENTRY_POINTS_GLES_{annotation_upper}_AUTOGEN_H_
Jamie Madill2e16d962017-04-19 14:06:36 -0400104"""
105
Jamie Madillee769dd2017-05-04 11:38:30 -0400106template_entry_point_source = """// GENERATED FILE - DO NOT EDIT.
107// Generated by {script_name} using data from {data_source_name}.
108//
109// Copyright {year} The ANGLE Project Authors. All rights reserved.
110// Use of this source code is governed by a BSD-style license that can be
111// found in the LICENSE file.
112//
Jamie Madillc8c9a242018-01-02 13:39:00 -0500113// entry_points_gles_{annotation_lower}_autogen.cpp:
114// Defines the GLES {comment} entry points.
Jamie Madillee769dd2017-05-04 11:38:30 -0400115
Jamie Madillc8c9a242018-01-02 13:39:00 -0500116{includes}
Jamie Madillee769dd2017-05-04 11:38:30 -0400117
118namespace gl
119{{
120{entry_points}}} // namespace gl
121"""
122
123template_entry_points_enum_header = """// GENERATED FILE - DO NOT EDIT.
124// Generated by {script_name} using data from {data_source_name}.
125//
126// Copyright {year} The ANGLE Project Authors. All rights reserved.
127// Use of this source code is governed by a BSD-style license that can be
128// found in the LICENSE file.
129//
130// entry_points_enum_autogen.h:
131// Defines the GLES entry points enumeration.
132
133#ifndef LIBGLESV2_ENTRYPOINTSENUM_AUTOGEN_H_
134#define LIBGLESV2_ENTRYPOINTSENUM_AUTOGEN_H_
135
136namespace gl
137{{
138enum class EntryPoint
139{{
140{entry_points_list}
141}};
142}} // namespace gl
143#endif // LIBGLESV2_ENTRY_POINTS_ENUM_AUTOGEN_H_
144"""
145
Brandon Jones41e59f52018-05-02 12:45:28 -0700146template_libgles_entry_point_source = """// GENERATED FILE - DO NOT EDIT.
147// Generated by {script_name} using data from {data_source_name}.
148//
149// Copyright {year} The ANGLE Project Authors. All rights reserved.
150// Use of this source code is governed by a BSD-style license that can be
151// found in the LICENSE file.
152//
153// libGLESv2.cpp: Implements the exported OpenGL ES functions.
154
155{includes}
156extern "C" {{
157{entry_points}
158}} // extern "C"
159"""
160
161template_libgles_entry_point_export = """; GENERATED FILE - DO NOT EDIT.
162; Generated by {script_name} using data from {data_source_name}.
163;
164; Copyright {year} The ANGLE Project Authors. All rights reserved.
165; Use of this source code is governed by a BSD-style license that can be
166; found in the LICENSE file.
167LIBRARY libGLESv2
168EXPORTS
169{exports}
170"""
171
Jamie Madill2e16d962017-04-19 14:06:36 -0400172template_entry_point_decl = """ANGLE_EXPORT {return_type}GL_APIENTRY {name}({params});"""
173
Jamie Madillee769dd2017-05-04 11:38:30 -0400174template_entry_point_def = """{return_type}GL_APIENTRY {name}({params})
175{{
Jamie Madillfa920eb2018-01-04 11:45:50 -0500176 {event_comment}EVENT("({format_params})"{comma_if_needed}{pass_params});
Jamie Madillee769dd2017-05-04 11:38:30 -0400177
Geoff Langcae72d62017-06-01 11:53:45 -0400178 Context *context = {context_getter}();
Jamie Madillee769dd2017-05-04 11:38:30 -0400179 if (context)
Corentin Wallez2e568cf2017-09-18 17:05:22 -0400180 {{{packed_gl_enum_conversions}
181 context->gatherParams<EntryPoint::{name}>({internal_params});
Jamie Madillee769dd2017-05-04 11:38:30 -0400182
Jamie Madill53d38412017-04-20 11:33:00 -0400183 if (context->skipValidation() || Validate{name}({validate_params}))
Jamie Madillee769dd2017-05-04 11:38:30 -0400184 {{
Jamie Madillc8c9a242018-01-02 13:39:00 -0500185 {return_if_needed}context->{name_lower_no_suffix}({internal_params});
Jamie Madillee769dd2017-05-04 11:38:30 -0400186 }}
Jamie Madillee769dd2017-05-04 11:38:30 -0400187 }}
188{default_return_if_needed}}}
189"""
190
Lingfeng Yanga0648782018-03-12 14:45:25 -0700191context_gles_header = """// GENERATED FILE - DO NOT EDIT.
192// Generated by {script_name} using data from {data_source_name}.
193//
194// Copyright {year} The ANGLE Project Authors. All rights reserved.
195// Use of this source code is governed by a BSD-style license that can be
196// found in the LICENSE file.
197//
198// Context_gles_{annotation_lower}_autogen.h: Creates a macro for interfaces in Context.
199
200#ifndef ANGLE_CONTEXT_GLES_{annotation_upper}_AUTOGEN_H_
201#define ANGLE_CONTEXT_GLES_{annotation_upper}_AUTOGEN_H_
202
203#define ANGLE_GLES1_CONTEXT_API \\
204{interface}
205
206#endif // ANGLE_CONTEXT_API_{annotation_upper}_AUTOGEN_H_
207"""
208
209context_gles_decl = """ {return_type} {name_lower_no_suffix}({internal_params}); \\"""
210
Brandon Jones41e59f52018-05-02 12:45:28 -0700211libgles_entry_point_def = """{return_type}GL_APIENTRY gl{name}({params})
212{{
213 return gl::{name}({internal_params});
214}}
215"""
216
217libgles_entry_point_export = """ {name}{spaces}@{ordinal}"""
218
Jamie Madill16daadb2017-08-26 23:34:31 -0400219def script_relative(path):
220 return os.path.join(os.path.dirname(sys.argv[0]), path)
221
222tree = etree.parse(script_relative('gl.xml'))
223root = tree.getroot()
Jamie Madill2e16d962017-04-19 14:06:36 -0400224commands = root.find(".//commands[@namespace='GL']")
Jamie Madill2e16d962017-04-19 14:06:36 -0400225
Corentin Wallez2e568cf2017-09-18 17:05:22 -0400226with open(script_relative('entry_point_packed_gl_enums.json')) as f:
227 cmd_packed_gl_enums = json.loads(f.read())
228
Jamie Madill2e16d962017-04-19 14:06:36 -0400229def format_entry_point_decl(cmd_name, proto, params):
230 return template_entry_point_decl.format(
231 name = cmd_name[2:],
232 return_type = proto[:-len(cmd_name)],
233 params = ", ".join(params))
234
Jamie Madillee769dd2017-05-04 11:38:30 -0400235def type_name_sep_index(param):
236 space = param.rfind(" ")
237 pointer = param.rfind("*")
238 return max(space, pointer)
239
240def just_the_type(param):
Lingfeng Yanga0648782018-03-12 14:45:25 -0700241 if "*" in param:
242 return param[:type_name_sep_index(param) + 1]
Jamie Madillee769dd2017-05-04 11:38:30 -0400243 return param[:type_name_sep_index(param)]
244
245def just_the_name(param):
246 return param[type_name_sep_index(param)+1:]
247
Lingfeng Yanga0648782018-03-12 14:45:25 -0700248def make_param(param_type, param_name):
249 return param_type + " " + param_name
250
251def just_the_type_packed(param, entry):
252 name = just_the_name(param)
253 if entry.has_key(name):
254 return entry[name]
255 else:
256 return just_the_type(param)
257
Corentin Wallez2e568cf2017-09-18 17:05:22 -0400258def just_the_name_packed(param, reserved_set):
259 name = just_the_name(param)
260 if name in reserved_set:
261 return name + 'Packed'
262 else:
263 return name
264
Jamie Madillee769dd2017-05-04 11:38:30 -0400265format_dict = {
266 "GLbitfield": "0x%X",
267 "GLboolean": "%u",
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500268 "GLclampx": "0x%X",
Jamie Madillee769dd2017-05-04 11:38:30 -0400269 "GLenum": "0x%X",
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500270 "GLfixed": "0x%X",
Jamie Madillee769dd2017-05-04 11:38:30 -0400271 "GLfloat": "%f",
272 "GLint": "%d",
273 "GLintptr": "%d",
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500274 "GLshort": "%d",
Jamie Madillee769dd2017-05-04 11:38:30 -0400275 "GLsizei": "%d",
276 "GLsizeiptr": "%d",
Jamie Madill16daadb2017-08-26 23:34:31 -0400277 "GLsync": "0x%0.8p",
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500278 "GLubyte": "%d",
Jamie Madillff161f82017-08-26 23:49:10 -0400279 "GLuint": "%u",
Jamie Madillfa920eb2018-01-04 11:45:50 -0500280 "GLuint64": "%llu",
281 "GLDEBUGPROC": "0x%0.8p",
282 "GLDEBUGPROCKHR": "0x%0.8p",
283 "GLeglImageOES": "0x%0.8p",
Jamie Madillee769dd2017-05-04 11:38:30 -0400284}
285
286def param_format_string(param):
287 if "*" in param:
288 return param + " = 0x%0.8p"
289 else:
Jamie Madill16daadb2017-08-26 23:34:31 -0400290 type_only = just_the_type(param)
291 if type_only not in format_dict:
292 raise Exception(type_only + " is not a known type in 'format_dict'")
293
294 return param + " = " + format_dict[type_only]
Jamie Madillee769dd2017-05-04 11:38:30 -0400295
Jamie Madill2e29b132017-08-28 17:22:11 -0400296def default_return_value(cmd_name, return_type):
Jamie Madillee769dd2017-05-04 11:38:30 -0400297 if return_type == "void":
298 return ""
Jamie Madill2e29b132017-08-28 17:22:11 -0400299 return "GetDefaultReturnValue<EntryPoint::" + cmd_name[2:] + ", " + return_type + ">()"
Jamie Madillee769dd2017-05-04 11:38:30 -0400300
Geoff Langcae72d62017-06-01 11:53:45 -0400301def get_context_getter_function(cmd_name):
302 if cmd_name == "glGetError":
303 return "GetGlobalContext"
304 else:
305 return "GetValidGlobalContext"
306
Jamie Madillfa920eb2018-01-04 11:45:50 -0500307template_event_comment = """// Don't run an EVENT() macro on the EXT_debug_marker entry points.
308 // It can interfere with the debug events being set by the caller.
309 // """
310
Jamie Madillee769dd2017-05-04 11:38:30 -0400311def format_entry_point_def(cmd_name, proto, params):
Corentin Wallez2e568cf2017-09-18 17:05:22 -0400312 packed_gl_enums = cmd_packed_gl_enums.get(cmd_name, {})
313 internal_params = [just_the_name_packed(param, packed_gl_enums) for param in params]
314 packed_gl_enum_conversions = []
315 for param in params:
316 name = just_the_name(param)
317 if name in packed_gl_enums:
318 internal_name = name + "Packed"
319 internal_type = packed_gl_enums[name]
320 packed_gl_enum_conversions += ["\n " + internal_type + " " + internal_name +" = FromGLenum<" +
321 internal_type + ">(" + name + ");"]
322
Jamie Madillee769dd2017-05-04 11:38:30 -0400323 pass_params = [just_the_name(param) for param in params]
324 format_params = [param_format_string(param) for param in params]
325 return_type = proto[:-len(cmd_name)]
Jamie Madill2e29b132017-08-28 17:22:11 -0400326 default_return = default_return_value(cmd_name, return_type.strip())
Jamie Madillfa920eb2018-01-04 11:45:50 -0500327 event_comment = template_event_comment if cmd_name in no_event_marker_exceptions_list else ""
Jamie Madillc8c9a242018-01-02 13:39:00 -0500328 name_lower_no_suffix = cmd_name[2:3].lower() + cmd_name[3:]
329
Jamie Madillfa920eb2018-01-04 11:45:50 -0500330 for suffix in strip_suffixes:
331 if name_lower_no_suffix.endswith(suffix):
332 name_lower_no_suffix = name_lower_no_suffix[0:-len(suffix)]
333
Jamie Madillee769dd2017-05-04 11:38:30 -0400334 return template_entry_point_def.format(
335 name = cmd_name[2:],
Jamie Madillc8c9a242018-01-02 13:39:00 -0500336 name_lower_no_suffix = name_lower_no_suffix,
Jamie Madillee769dd2017-05-04 11:38:30 -0400337 return_type = return_type,
338 params = ", ".join(params),
Corentin Wallez2e568cf2017-09-18 17:05:22 -0400339 internal_params = ", ".join(internal_params),
340 packed_gl_enum_conversions = "".join(packed_gl_enum_conversions),
Jamie Madillee769dd2017-05-04 11:38:30 -0400341 pass_params = ", ".join(pass_params),
342 comma_if_needed = ", " if len(params) > 0 else "",
Corentin Wallez2e568cf2017-09-18 17:05:22 -0400343 validate_params = ", ".join(["context"] + internal_params),
Jamie Madillee769dd2017-05-04 11:38:30 -0400344 format_params = ", ".join(format_params),
Jamie Madillee769dd2017-05-04 11:38:30 -0400345 return_if_needed = "" if default_return == "" else "return ",
Geoff Langcae72d62017-06-01 11:53:45 -0400346 default_return_if_needed = "" if default_return == "" else "\n return " + default_return + ";\n",
Jamie Madillfa920eb2018-01-04 11:45:50 -0500347 context_getter = get_context_getter_function(cmd_name),
348 event_comment = event_comment)
Jamie Madillee769dd2017-05-04 11:38:30 -0400349
Lingfeng Yanga0648782018-03-12 14:45:25 -0700350def format_context_gles_decl(cmd_name, proto, params):
351 packed_gl_enums = cmd_packed_gl_enums.get(cmd_name, {})
352 internal_params = ", ".join([make_param(just_the_type_packed(param, packed_gl_enums),
353 just_the_name_packed(param, packed_gl_enums)) for param in params])
354
355 return_type = proto[:-len(cmd_name)]
356 name_lower_no_suffix = cmd_name[2:3].lower() + cmd_name[3:]
357
358 for suffix in strip_suffixes:
359 if name_lower_no_suffix.endswith(suffix):
360 name_lower_no_suffix = name_lower_no_suffix[0:-len(suffix)]
361
362 return context_gles_decl.format(
363 return_type = return_type,
364 name_lower_no_suffix = name_lower_no_suffix,
365 internal_params = internal_params)
366
Brandon Jones41e59f52018-05-02 12:45:28 -0700367def format_libgles_entry_point_def(cmd_name, proto, params):
368 internal_params = [just_the_name(param) for param in params]
369 return_type = proto[:-len(cmd_name)]
370
371 return libgles_entry_point_def.format(
372 name = cmd_name[2:],
373 return_type = return_type,
374 params = ", ".join(params),
375 internal_params = ", ".join(internal_params)
376 )
377
378def format_libgles_entry_point_export(cmd_name, ordinal):
379 return libgles_entry_point_export.format(
380 name = cmd_name,
381 ordinal = ordinal,
382 spaces = " "*(50 - len(cmd_name))
383 )
384
Jiajia Qincb59a902017-11-22 13:03:42 +0800385def path_to(folder, file):
386 return os.path.join(script_relative(".."), "src", folder, file)
Jamie Madill2e16d962017-04-19 14:06:36 -0400387
Brandon Jones41e59f52018-05-02 12:45:28 -0700388def get_entry_points(all_commands, gles_commands, ordinal):
Jamie Madillc8c9a242018-01-02 13:39:00 -0500389 decls = []
390 defs = []
Brandon Jones41e59f52018-05-02 12:45:28 -0700391 export_defs = []
392 exports = []
393
Jamie Madillffa2cd02017-12-28 14:57:53 -0500394 for command in all_commands:
395 proto = command.find('proto')
396 cmd_name = proto.find('name').text
397
398 if cmd_name not in gles_commands:
399 continue
400
401 param_text = ["".join(param.itertext()) for param in command.findall('param')]
402 proto_text = "".join(proto.itertext())
Brandon Jones41e59f52018-05-02 12:45:28 -0700403
Jamie Madillc8c9a242018-01-02 13:39:00 -0500404 decls.append(format_entry_point_decl(cmd_name, proto_text, param_text))
405 defs.append(format_entry_point_def(cmd_name, proto_text, param_text))
Jamie Madillee769dd2017-05-04 11:38:30 -0400406
Brandon Jones41e59f52018-05-02 12:45:28 -0700407 export_defs.append(format_libgles_entry_point_def(cmd_name, proto_text, param_text))
408 exports.append(format_libgles_entry_point_export(cmd_name, ordinal))
409 ordinal = ordinal + 1
410
411 return decls, defs, export_defs, exports
Jamie Madill16daadb2017-08-26 23:34:31 -0400412
Lingfeng Yanga0648782018-03-12 14:45:25 -0700413def get_gles1_decls(all_commands, gles_commands):
414 decls = []
415 for command in all_commands:
416 proto = command.find('proto')
417 cmd_name = proto.find('name').text
418
419 if cmd_name not in gles_commands:
420 continue
421
422 if cmd_name in gles1_overloaded:
423 continue
424
425 param_text = ["".join(param.itertext()) for param in command.findall('param')]
426 proto_text = "".join(proto.itertext())
427 decls.append(format_context_gles_decl(cmd_name, proto_text, param_text))
428
429 return decls
430
Brandon Jones416aaf92018-04-10 08:10:16 -0700431def write_file(annotation, comment, template, entry_points, suffix, includes, file):
Jiajia Qincb59a902017-11-22 13:03:42 +0800432
Jamie Madillc8c9a242018-01-02 13:39:00 -0500433 content = template.format(
434 script_name = os.path.basename(sys.argv[0]),
Brandon Jones416aaf92018-04-10 08:10:16 -0700435 data_source_name = file,
Jamie Madillc8c9a242018-01-02 13:39:00 -0500436 year = date.today().year,
437 annotation_lower = annotation.lower(),
438 annotation_upper = annotation.upper(),
439 comment = comment,
440 includes = includes,
441 entry_points = entry_points)
Jiajia Qincb59a902017-11-22 13:03:42 +0800442
Jamie Madillc8c9a242018-01-02 13:39:00 -0500443 path = path_to("libGLESv2", "entry_points_gles_{}_autogen.{}".format(
444 annotation.lower(), suffix))
445
446 with open(path, "w") as out:
447 out.write(content)
448 out.close()
449
Brandon Jones41e59f52018-05-02 12:45:28 -0700450def write_export_files(entry_points, includes, exports):
451
452 content = template_libgles_entry_point_source.format(
453 script_name = os.path.basename(sys.argv[0]),
454 data_source_name = "gl.xml and gl_angle_ext.xml",
455 year = date.today().year,
456 includes = includes,
457 entry_points = entry_points)
458
459 path = path_to("libGLESv2", "libGLESv2_autogen.cpp")
460
461 with open(path, "w") as out:
462 out.write(content)
463 out.close()
464
465 content = template_libgles_entry_point_export.format(
466 script_name = os.path.basename(sys.argv[0]),
467 data_source_name = "gl.xml and gl_angle_ext.xml",
468 exports = exports,
469 year = date.today().year)
470
471 path = path_to("libGLESv2", "libGLESv2_autogen.def")
472
473 with open(path, "w") as out:
474 out.write(content)
475 out.close()
476
Lingfeng Yanga0648782018-03-12 14:45:25 -0700477def write_context_api_decls(annotation, template, decls):
478
479 interface_lines = []
480
481 for i in decls['core']:
482 interface_lines.append(i)
483
484 for extname in sorted(decls['exts'].keys()):
485 interface_lines.append(" /* " + extname + " */ \\")
486 interface_lines.extend(decls['exts'][extname])
487
488 content = template.format(
489 annotation_lower = annotation.lower(),
490 annotation_upper = annotation.upper(),
491 script_name = os.path.basename(sys.argv[0]),
492 data_source_name = "gl.xml",
493 year = date.today().year,
494 interface = "\n".join(interface_lines))
495
496 path = path_to("libANGLE", "Context_gles_%s_autogen.h" % annotation.lower())
497
498 with open(path, "w") as out:
499 out.write(content)
500 out.close()
501
Brandon Jones416aaf92018-04-10 08:10:16 -0700502def append_angle_extensions(base_root):
503 angle_ext_tree = etree.parse(script_relative('gl_angle_ext.xml'))
504 angle_ext_root = angle_ext_tree.getroot()
505
506 insertion_point = base_root.findall("./commands")[0]
507 for command in angle_ext_root.iter('commands'):
508 insertion_point.extend(command)
509
510 insertion_point = base_root.findall("./extensions")[0]
511 for extension in angle_ext_root.iter('extensions'):
512 insertion_point.extend(extension)
513 return base_root
514
515root = append_angle_extensions(root)
516
Jamie Madillc8c9a242018-01-02 13:39:00 -0500517all_commands = root.findall('commands/command')
518all_cmd_names = []
519
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500520template_header_includes = """#include <GLES{major}/gl{major}{minor}.h>
Jamie Madillc8c9a242018-01-02 13:39:00 -0500521#include <export.h>"""
522
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500523template_sources_includes = """#include "libGLESv2/entry_points_gles_{}_autogen.h"
524
525#include "libANGLE/Context.h"
Jamie Madillc8c9a242018-01-02 13:39:00 -0500526#include "libANGLE/validationES{}{}.h"
527#include "libGLESv2/global_state.h"
528"""
529
Lingfeng Yanga0648782018-03-12 14:45:25 -0700530gles1decls = {}
531
532gles1decls['core'] = []
533gles1decls['exts'] = {}
534
Brandon Jones41e59f52018-05-02 12:45:28 -0700535libgles_ep_defs = []
536libgles_ep_exports = []
537
538ordinal_start = 1
539
Lingfeng Yanga0648782018-03-12 14:45:25 -0700540# First run through the main GLES entry points. Since ES2+ is the primary use
541# case, we go through those first and then add ES1-only APIs at the end.
542for major_version, minor_version in [[2, 0], [3, 0], [3, 1], [1, 0]]:
Jamie Madillc8c9a242018-01-02 13:39:00 -0500543 annotation = "{}_{}".format(major_version, minor_version)
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500544 name_prefix = "GL_ES_VERSION_"
Lingfeng Yanga0648782018-03-12 14:45:25 -0700545
546 is_gles1 = major_version == 1
547 if is_gles1:
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500548 name_prefix = "GL_VERSION_ES_CM_"
Lingfeng Yanga0648782018-03-12 14:45:25 -0700549
Jamie Madillc8c9a242018-01-02 13:39:00 -0500550 comment = annotation.replace("_", ".")
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500551 gles_xpath = ".//feature[@name='{}{}']//command".format(name_prefix, annotation)
Jamie Madillc8c9a242018-01-02 13:39:00 -0500552 gles_commands = [cmd.attrib['name'] for cmd in root.findall(gles_xpath)]
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500553
554 # Remove commands that have already been processed
555 gles_commands = [cmd for cmd in gles_commands if cmd not in all_cmd_names]
556
Jamie Madillc8c9a242018-01-02 13:39:00 -0500557 all_cmd_names += gles_commands
558
Brandon Jones41e59f52018-05-02 12:45:28 -0700559 decls, defs, libgles_defs, libgles_exports = get_entry_points(
560 all_commands, gles_commands, ordinal_start)
561
562 # Increment the ordinal before inserting the version comment
563 ordinal_start += len(libgles_exports)
564
565 # Write the version as a comment before the first EP.
566 libgles_defs.insert(0, "\n// OpenGL ES {}.{}".format(major_version, minor_version))
567 libgles_exports.insert(0, "\n ; OpenGL ES {}.{}".format(major_version, minor_version))
568
569 libgles_ep_defs += libgles_defs
570 libgles_ep_exports += libgles_exports
Jamie Madillc8c9a242018-01-02 13:39:00 -0500571
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500572 major_if_not_one = major_version if major_version != 1 else ""
Jamie Madillc8c9a242018-01-02 13:39:00 -0500573 minor_if_not_zero = minor_version if minor_version != 0 else ""
574
575 header_includes = template_header_includes.format(
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500576 major=major_if_not_one, minor=minor_if_not_zero)
Jamie Madillc8c9a242018-01-02 13:39:00 -0500577
578 # We include the platform.h header since it undefines the conflicting MemoryBarrier macro.
579 if major_version == 3 and minor_version == 1:
580 header_includes += "\n#include \"common/platform.h\"\n"
581
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500582 source_includes = template_sources_includes.format(
583 annotation.lower(), major_version,minor_if_not_zero)
Jamie Madillc8c9a242018-01-02 13:39:00 -0500584
585 write_file(annotation, comment, template_entry_point_header,
Brandon Jones416aaf92018-04-10 08:10:16 -0700586 "\n".join(decls), "h", header_includes, "gl.xml")
Jamie Madillc8c9a242018-01-02 13:39:00 -0500587 write_file(annotation, comment, template_entry_point_source,
Brandon Jones416aaf92018-04-10 08:10:16 -0700588 "\n".join(defs), "cpp", source_includes, "gl.xml")
Lingfeng Yanga0648782018-03-12 14:45:25 -0700589 if is_gles1:
590 gles1decls['core'] = get_gles1_decls(all_commands, gles_commands)
591
Jamie Madill57ae8c12017-08-30 12:14:29 -0400592
Jamie Madillfa920eb2018-01-04 11:45:50 -0500593# After we finish with the main entry points, we process the extensions.
594extension_defs = []
595extension_decls = []
596
597# Use a first step to run through the extensions so we can generate them
598# in sorted order.
599ext_data = {}
600
Lingfeng Yanga0648782018-03-12 14:45:25 -0700601for gles1ext in gles1_extensions:
602 gles1decls['exts'][gles1ext] = []
603
Jamie Madillfa920eb2018-01-04 11:45:50 -0500604for extension in root.findall("extensions/extension"):
605 extension_name = extension.attrib['name']
606 if not extension_name in supported_extensions:
607 continue
608
609 ext_cmd_names = []
610
611 # There's an extra step here to filter out 'api=gl' extensions. This
612 # is necessary for handling KHR extensions, which have separate entry
613 # point signatures (without the suffix) for desktop GL. Note that this
614 # extra step is necessary because of Etree's limited Xpath support.
615 for require in extension.findall('require'):
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500616 if 'api' in require.attrib and require.attrib['api'] != 'gles2' and require.attrib['api'] != 'gles1':
Jamie Madillfa920eb2018-01-04 11:45:50 -0500617 continue
618
619 # Another special case for EXT_texture_storage
620 filter_out_comment = "Supported only if GL_EXT_direct_state_access is supported"
621 if 'comment' in require.attrib and require.attrib['comment'] == filter_out_comment:
622 continue
623
624 extension_commands = require.findall('command')
625 ext_cmd_names += [command.attrib['name'] for command in extension_commands]
626
627 ext_data[extension_name] = sorted(ext_cmd_names)
628
629for extension_name, ext_cmd_names in sorted(ext_data.iteritems()):
630
631 # Detect and filter duplicate extensions.
632 dupes = []
633 for ext_cmd in ext_cmd_names:
634 if ext_cmd in all_cmd_names:
635 dupes.append(ext_cmd)
636
637 for dupe in dupes:
638 ext_cmd_names.remove(dupe)
639
640 all_cmd_names += ext_cmd_names
641
Brandon Jones41e59f52018-05-02 12:45:28 -0700642 decls, defs, libgles_defs, libgles_exports = get_entry_points(
643 all_commands, ext_cmd_names, ordinal_start)
Jamie Madillfa920eb2018-01-04 11:45:50 -0500644
645 # Avoid writing out entry points defined by a prior extension.
646 for dupe in dupes:
647 msg = "// {} is already defined.\n".format(dupe[2:])
648 defs.append(msg)
649
Brandon Jones41e59f52018-05-02 12:45:28 -0700650 # Increment starting ordinal before adding extension comment
651 ordinal_start += len(libgles_exports)
652
Jamie Madillfa920eb2018-01-04 11:45:50 -0500653 # Write the extension name as a comment before the first EP.
654 comment = "\n// {}".format(extension_name)
655 defs.insert(0, comment)
656 decls.insert(0, comment)
Brandon Jones41e59f52018-05-02 12:45:28 -0700657 libgles_defs.insert(0, comment)
658 libgles_exports.insert(0, "\n ; {}".format(extension_name))
Jamie Madillfa920eb2018-01-04 11:45:50 -0500659
660 extension_defs += defs
661 extension_decls += decls
662
Brandon Jones41e59f52018-05-02 12:45:28 -0700663 libgles_ep_defs += libgles_defs
664 libgles_ep_exports += libgles_exports
665
Lingfeng Yanga0648782018-03-12 14:45:25 -0700666 if extension_name in gles1_extensions:
667 if extension_name not in gles1_no_context_decl_extensions:
668 gles1decls['exts'][extension_name] = get_gles1_decls(all_commands, ext_cmd_names)
669
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500670header_includes = template_header_includes.format(
671 major="", minor="")
672header_includes += """
673#include <GLES/gl.h>
674#include <GLES/glext.h>
675#include <GLES2/gl2.h>
676#include <GLES2/gl2ext.h>
677"""
Jamie Madillfa920eb2018-01-04 11:45:50 -0500678
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500679source_includes = template_sources_includes.format("ext", "", "")
680source_includes += """
681#include "libANGLE/validationES.h"
682#include "libANGLE/validationES1.h"
Jamie Madillfa920eb2018-01-04 11:45:50 -0500683#include "libANGLE/validationES3.h"
684#include "libANGLE/validationES31.h"
685"""
686
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500687write_file("ext", "extension", template_entry_point_header,
Brandon Jones416aaf92018-04-10 08:10:16 -0700688 "\n".join([item for item in extension_decls]), "h", header_includes,
689 "gl.xml and gl_angle_ext.xml")
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500690write_file("ext", "extension", template_entry_point_source,
Brandon Jones416aaf92018-04-10 08:10:16 -0700691 "\n".join([item for item in extension_defs]), "cpp", source_includes,
692 "gl.xml and gl_angle_ext.xml")
Jamie Madillc8c9a242018-01-02 13:39:00 -0500693
Lingfeng Yanga0648782018-03-12 14:45:25 -0700694write_context_api_decls("1_0", context_gles_header, gles1decls)
695
Jamie Madillc8c9a242018-01-02 13:39:00 -0500696sorted_cmd_names = ["Invalid"] + [cmd[2:] for cmd in sorted(all_cmd_names)]
697
Jamie Madillee769dd2017-05-04 11:38:30 -0400698entry_points_enum = template_entry_points_enum_header.format(
699 script_name = os.path.basename(sys.argv[0]),
Brandon Jones416aaf92018-04-10 08:10:16 -0700700 data_source_name = "gl.xml and gl_angle_ext.xml",
Jamie Madillee769dd2017-05-04 11:38:30 -0400701 year = date.today().year,
Jamie Madillc8c9a242018-01-02 13:39:00 -0500702 entry_points_list = ",\n".join([" " + cmd for cmd in sorted_cmd_names]))
Jamie Madillee769dd2017-05-04 11:38:30 -0400703
Jamie Madillee769dd2017-05-04 11:38:30 -0400704entry_points_enum_header_path = path_to("libANGLE", "entry_points_enum_autogen.h")
Jamie Madillee769dd2017-05-04 11:38:30 -0400705with open(entry_points_enum_header_path, "w") as out:
706 out.write(entry_points_enum)
Geoff Langcae72d62017-06-01 11:53:45 -0400707 out.close()
Brandon Jones41e59f52018-05-02 12:45:28 -0700708
709source_includes = """
710#include "angle_gl.h"
711
712#include "libGLESv2/entry_points_gles_1_0_autogen.h"
713#include "libGLESv2/entry_points_gles_2_0_autogen.h"
714#include "libGLESv2/entry_points_gles_3_0_autogen.h"
715#include "libGLESv2/entry_points_gles_3_1_autogen.h"
716#include "libGLESv2/entry_points_gles_ext_autogen.h"
717
718#include "common/event_tracer.h"
719"""
720
721write_export_files("\n".join([item for item in libgles_ep_defs]), source_includes, "\n".join([item for item in libgles_ep_exports]))