blob: 4b49de9d662c31a8a9efa7490d6a8557b89f8b6a [file] [log] [blame]
Jamie Madill2e16d962017-04-19 14:06:36 -04001#!/usr/bin/python
2#
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
10import sys, os, pprint
11import xml.etree.ElementTree as etree
12from datetime import date
13
Jamie Madill2e16d962017-04-19 14:06:36 -040014template_entry_point_header = """// GENERATED FILE - DO NOT EDIT.
15// Generated by {script_name} using data from {data_source_name}.
16//
17// Copyright {year} The ANGLE Project Authors. All rights reserved.
18// Use of this source code is governed by a BSD-style license that can be
19// found in the LICENSE file.
20//
21// entry_points_gles_{major_version}_{minor_version}_autogen.h:
22// Defines the GLES {major_version}.{minor_version} entry points.
23
24#ifndef LIBGLESV2_ENTRYPOINTSGLES{major_version}{minor_version}_AUTOGEN_H_
25#define LIBGLESV2_ENTRYPOINTSGLES{major_version}{minor_version}_AUTOGEN_H_
26
Jamie Madill16daadb2017-08-26 23:34:31 -040027#include <GLES{major_version}/gl{major_version}.h>
Jamie Madill2e16d962017-04-19 14:06:36 -040028#include <export.h>
29
30namespace gl
31{{
32{entry_points}
33}} // namespace gl
34
35#endif // LIBGLESV2_ENTRYPOINTSGLES{major_version}{minor_version}_AUTOGEN_H_
36"""
37
Jamie Madillee769dd2017-05-04 11:38:30 -040038template_entry_point_source = """// GENERATED FILE - DO NOT EDIT.
39// Generated by {script_name} using data from {data_source_name}.
40//
41// Copyright {year} The ANGLE Project Authors. All rights reserved.
42// Use of this source code is governed by a BSD-style license that can be
43// found in the LICENSE file.
44//
45// entry_points_gles_{major_version}_{minor_version}_autogen.cpp:
46// Defines the GLES {major_version}.{minor_version} entry points.
47
Jamie Madillee769dd2017-05-04 11:38:30 -040048#include "libANGLE/Context.h"
49#include "libANGLE/validationES2.h"
50#include "libGLESv2/global_state.h"
51
52namespace gl
53{{
54{entry_points}}} // namespace gl
55"""
56
57template_entry_points_enum_header = """// GENERATED FILE - DO NOT EDIT.
58// Generated by {script_name} using data from {data_source_name}.
59//
60// Copyright {year} The ANGLE Project Authors. All rights reserved.
61// Use of this source code is governed by a BSD-style license that can be
62// found in the LICENSE file.
63//
64// entry_points_enum_autogen.h:
65// Defines the GLES entry points enumeration.
66
67#ifndef LIBGLESV2_ENTRYPOINTSENUM_AUTOGEN_H_
68#define LIBGLESV2_ENTRYPOINTSENUM_AUTOGEN_H_
69
70namespace gl
71{{
72enum class EntryPoint
73{{
74{entry_points_list}
75}};
76}} // namespace gl
77#endif // LIBGLESV2_ENTRY_POINTS_ENUM_AUTOGEN_H_
78"""
79
Jamie Madill2e16d962017-04-19 14:06:36 -040080template_entry_point_decl = """ANGLE_EXPORT {return_type}GL_APIENTRY {name}({params});"""
81
Jamie Madillee769dd2017-05-04 11:38:30 -040082template_entry_point_def = """{return_type}GL_APIENTRY {name}({params})
83{{
84 EVENT("({format_params})"{comma_if_needed}{pass_params});
85
Geoff Langcae72d62017-06-01 11:53:45 -040086 Context *context = {context_getter}();
Jamie Madillee769dd2017-05-04 11:38:30 -040087 if (context)
88 {{
89 context->gatherParams<EntryPoint::{name}>({pass_params});
90
Jamie Madill53d38412017-04-20 11:33:00 -040091 if (context->skipValidation() || Validate{name}({validate_params}))
Jamie Madillee769dd2017-05-04 11:38:30 -040092 {{
Jamie Madill53d38412017-04-20 11:33:00 -040093 {return_if_needed}context->{name_lower}({pass_params});
Jamie Madillee769dd2017-05-04 11:38:30 -040094 }}
Jamie Madillee769dd2017-05-04 11:38:30 -040095 }}
96{default_return_if_needed}}}
97"""
98
Jamie Madill16daadb2017-08-26 23:34:31 -040099def script_relative(path):
100 return os.path.join(os.path.dirname(sys.argv[0]), path)
101
102tree = etree.parse(script_relative('gl.xml'))
103root = tree.getroot()
104
105gles2_xpath = ".//feature[@name='GL_ES_VERSION_2_0']//command"
106gles2_commands = [cmd.attrib['name'] for cmd in root.findall(gles2_xpath)]
107
108gles3_xpath = ".//feature[@name='GL_ES_VERSION_3_0']//command"
109gles3_commands = [cmd.attrib['name'] for cmd in root.findall(gles3_xpath)]
110
Jamie Madill2e16d962017-04-19 14:06:36 -0400111commands = root.find(".//commands[@namespace='GL']")
112entry_point_decls_gles_2_0 = []
Jamie Madillee769dd2017-05-04 11:38:30 -0400113entry_point_defs_gles_2_0 = []
Jamie Madill16daadb2017-08-26 23:34:31 -0400114entry_point_decls_gles_3_0 = []
115entry_point_defs_gles_3_0 = []
Jamie Madillee769dd2017-05-04 11:38:30 -0400116cmd_names = []
Jamie Madill2e16d962017-04-19 14:06:36 -0400117
118def format_entry_point_decl(cmd_name, proto, params):
119 return template_entry_point_decl.format(
120 name = cmd_name[2:],
121 return_type = proto[:-len(cmd_name)],
122 params = ", ".join(params))
123
Jamie Madillee769dd2017-05-04 11:38:30 -0400124def type_name_sep_index(param):
125 space = param.rfind(" ")
126 pointer = param.rfind("*")
127 return max(space, pointer)
128
129def just_the_type(param):
130 return param[:type_name_sep_index(param)]
131
132def just_the_name(param):
133 return param[type_name_sep_index(param)+1:]
134
135format_dict = {
136 "GLbitfield": "0x%X",
137 "GLboolean": "%u",
138 "GLenum": "0x%X",
139 "GLfloat": "%f",
140 "GLint": "%d",
141 "GLintptr": "%d",
142 "GLsizei": "%d",
143 "GLsizeiptr": "%d",
Jamie Madill16daadb2017-08-26 23:34:31 -0400144 "GLsync": "0x%0.8p",
Jamie Madillff161f82017-08-26 23:49:10 -0400145 "GLuint": "%u",
Jamie Madill16daadb2017-08-26 23:34:31 -0400146 "GLuint64": "%llu"
Jamie Madillee769dd2017-05-04 11:38:30 -0400147}
148
149def param_format_string(param):
150 if "*" in param:
151 return param + " = 0x%0.8p"
152 else:
Jamie Madill16daadb2017-08-26 23:34:31 -0400153 type_only = just_the_type(param)
154 if type_only not in format_dict:
155 raise Exception(type_only + " is not a known type in 'format_dict'")
156
157 return param + " = " + format_dict[type_only]
Jamie Madillee769dd2017-05-04 11:38:30 -0400158
159def default_return_value(return_type):
160 if return_type == "void":
161 return ""
162 elif return_type == "GLenum" or return_type == "GLint" or return_type == "GLuint":
163 return "0"
164 elif return_type == "GLboolean":
165 return "GL_FALSE"
Jamie Madill16daadb2017-08-26 23:34:31 -0400166 elif "*" in return_type or return_type == "GLsync":
Jamie Madillee769dd2017-05-04 11:38:30 -0400167 return "nullptr"
168 else:
Jamie Madill16daadb2017-08-26 23:34:31 -0400169 raise Exception("Don't know default return type for " + return_type)
Jamie Madillee769dd2017-05-04 11:38:30 -0400170
Geoff Langcae72d62017-06-01 11:53:45 -0400171def get_context_getter_function(cmd_name):
172 if cmd_name == "glGetError":
173 return "GetGlobalContext"
174 else:
175 return "GetValidGlobalContext"
176
Jamie Madillee769dd2017-05-04 11:38:30 -0400177def format_entry_point_def(cmd_name, proto, params):
178 pass_params = [just_the_name(param) for param in params]
179 format_params = [param_format_string(param) for param in params]
180 return_type = proto[:-len(cmd_name)]
181 default_return = default_return_value(return_type.strip())
182 return template_entry_point_def.format(
183 name = cmd_name[2:],
184 name_lower = cmd_name[2:3].lower() + cmd_name[3:],
185 return_type = return_type,
186 params = ", ".join(params),
187 pass_params = ", ".join(pass_params),
188 comma_if_needed = ", " if len(params) > 0 else "",
189 validate_params = ", ".join(["context"] + pass_params),
190 format_params = ", ".join(format_params),
Jamie Madillee769dd2017-05-04 11:38:30 -0400191 return_if_needed = "" if default_return == "" else "return ",
Geoff Langcae72d62017-06-01 11:53:45 -0400192 default_return_if_needed = "" if default_return == "" else "\n return " + default_return + ";\n",
193 context_getter = get_context_getter_function(cmd_name))
Jamie Madillee769dd2017-05-04 11:38:30 -0400194
Jamie Madill2e16d962017-04-19 14:06:36 -0400195for cmd_name in gles2_commands:
196 command_xpath = "command/proto[name='" + cmd_name + "']/.."
197 command = commands.find(command_xpath)
198 params = ["".join(param.itertext()) for param in command.findall("./param")]
199 proto = "".join(command.find("./proto").itertext())
Jamie Madillee769dd2017-05-04 11:38:30 -0400200 cmd_names += [cmd_name]
Jamie Madill2e16d962017-04-19 14:06:36 -0400201 entry_point_decls_gles_2_0 += [format_entry_point_decl(cmd_name, proto, params)]
Jamie Madillee769dd2017-05-04 11:38:30 -0400202 entry_point_defs_gles_2_0 += [format_entry_point_def(cmd_name, proto, params)]
Jamie Madill2e16d962017-04-19 14:06:36 -0400203
Jamie Madill16daadb2017-08-26 23:34:31 -0400204for cmd_name in gles3_commands:
205 command_xpath = "command/proto[name='" + cmd_name + "']/.."
206 command = commands.find(command_xpath)
207 params = ["".join(param.itertext()) for param in command.findall("./param")]
208 proto = "".join(command.find("./proto").itertext())
209 cmd_names += [cmd_name]
210 entry_point_decls_gles_3_0 += [format_entry_point_decl(cmd_name, proto, params)]
211 entry_point_defs_gles_3_0 += [format_entry_point_def(cmd_name, proto, params)]
212
Jamie Madill2e16d962017-04-19 14:06:36 -0400213gles_2_0_header = template_entry_point_header.format(
214 script_name = os.path.basename(sys.argv[0]),
215 data_source_name = "gl.xml",
216 year = date.today().year,
217 major_version = 2,
218 minor_version = 0,
219 entry_points = "\n".join(entry_point_decls_gles_2_0))
220
Jamie Madillee769dd2017-05-04 11:38:30 -0400221gles_2_0_source = template_entry_point_source.format(
222 script_name = os.path.basename(sys.argv[0]),
223 data_source_name = "gl.xml",
224 year = date.today().year,
225 major_version = 2,
226 minor_version = 0,
227 entry_points = "\n".join(entry_point_defs_gles_2_0))
228
Jamie Madill16daadb2017-08-26 23:34:31 -0400229gles_3_0_header = template_entry_point_header.format(
230 script_name = os.path.basename(sys.argv[0]),
231 data_source_name = "gl.xml",
232 year = date.today().year,
233 major_version = 3,
234 minor_version = 0,
235 entry_points = "\n".join(entry_point_decls_gles_3_0))
236
237# TODO(jmadill): Remove manually added entry points once we auto-gen them.
238manual_cmd_names = ["Invalid"] + [cmd[2:] for cmd in cmd_names] + ["DrawElementsInstancedANGLE"]
Jamie Madillee769dd2017-05-04 11:38:30 -0400239entry_points_enum = template_entry_points_enum_header.format(
240 script_name = os.path.basename(sys.argv[0]),
241 data_source_name = "gl.xml",
242 year = date.today().year,
243 entry_points_list = ",\n".join([" " + cmd for cmd in manual_cmd_names]))
244
Jamie Madill2e16d962017-04-19 14:06:36 -0400245def path_to(folder, file):
246 return os.path.join(script_relative(".."), "src", folder, file)
247
248gles_2_0_header_path = path_to("libGLESv2", "entry_points_gles_2_0_autogen.h")
Jamie Madillee769dd2017-05-04 11:38:30 -0400249gles_2_0_source_path = path_to("libGLESv2", "entry_points_gles_2_0_autogen.cpp")
Jamie Madill16daadb2017-08-26 23:34:31 -0400250gles_3_0_header_path = path_to("libGLESv2", "entry_points_gles_3_0_autogen.h")
251gles_3_0_source_path = path_to("libGLESv2", "entry_points_gles_3_0_autogen.cpp")
Jamie Madillee769dd2017-05-04 11:38:30 -0400252entry_points_enum_header_path = path_to("libANGLE", "entry_points_enum_autogen.h")
Jamie Madill2e16d962017-04-19 14:06:36 -0400253
254with open(gles_2_0_header_path, "w") as out:
255 out.write(gles_2_0_header)
256 out.close()
Jamie Madillee769dd2017-05-04 11:38:30 -0400257
258with open(gles_2_0_source_path, "w") as out:
259 out.write(gles_2_0_source)
260 out.close()
261
Jamie Madill16daadb2017-08-26 23:34:31 -0400262with open(gles_3_0_header_path, "w") as out:
263 out.write(gles_3_0_header)
264 out.close()
265
Jamie Madillee769dd2017-05-04 11:38:30 -0400266with open(entry_points_enum_header_path, "w") as out:
267 out.write(entry_points_enum)
Geoff Langcae72d62017-06-01 11:53:45 -0400268 out.close()