blob: bbc09cfa40e2e7777e55d696de0abe0700924fdd [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
Jamie Madill2e29b132017-08-28 17:22:11 -0400159def default_return_value(cmd_name, return_type):
Jamie Madillee769dd2017-05-04 11:38:30 -0400160 if return_type == "void":
161 return ""
Jamie Madill2e29b132017-08-28 17:22:11 -0400162 return "GetDefaultReturnValue<EntryPoint::" + cmd_name[2:] + ", " + return_type + ">()"
Jamie Madillee769dd2017-05-04 11:38:30 -0400163
Geoff Langcae72d62017-06-01 11:53:45 -0400164def get_context_getter_function(cmd_name):
165 if cmd_name == "glGetError":
166 return "GetGlobalContext"
167 else:
168 return "GetValidGlobalContext"
169
Jamie Madillee769dd2017-05-04 11:38:30 -0400170def format_entry_point_def(cmd_name, proto, params):
171 pass_params = [just_the_name(param) for param in params]
172 format_params = [param_format_string(param) for param in params]
173 return_type = proto[:-len(cmd_name)]
Jamie Madill2e29b132017-08-28 17:22:11 -0400174 default_return = default_return_value(cmd_name, return_type.strip())
Jamie Madillee769dd2017-05-04 11:38:30 -0400175 return template_entry_point_def.format(
176 name = cmd_name[2:],
177 name_lower = cmd_name[2:3].lower() + cmd_name[3:],
178 return_type = return_type,
179 params = ", ".join(params),
180 pass_params = ", ".join(pass_params),
181 comma_if_needed = ", " if len(params) > 0 else "",
182 validate_params = ", ".join(["context"] + pass_params),
183 format_params = ", ".join(format_params),
Jamie Madillee769dd2017-05-04 11:38:30 -0400184 return_if_needed = "" if default_return == "" else "return ",
Geoff Langcae72d62017-06-01 11:53:45 -0400185 default_return_if_needed = "" if default_return == "" else "\n return " + default_return + ";\n",
186 context_getter = get_context_getter_function(cmd_name))
Jamie Madillee769dd2017-05-04 11:38:30 -0400187
Jamie Madill2e16d962017-04-19 14:06:36 -0400188for cmd_name in gles2_commands:
189 command_xpath = "command/proto[name='" + cmd_name + "']/.."
190 command = commands.find(command_xpath)
191 params = ["".join(param.itertext()) for param in command.findall("./param")]
192 proto = "".join(command.find("./proto").itertext())
Jamie Madillee769dd2017-05-04 11:38:30 -0400193 cmd_names += [cmd_name]
Jamie Madill2e16d962017-04-19 14:06:36 -0400194 entry_point_decls_gles_2_0 += [format_entry_point_decl(cmd_name, proto, params)]
Jamie Madillee769dd2017-05-04 11:38:30 -0400195 entry_point_defs_gles_2_0 += [format_entry_point_def(cmd_name, proto, params)]
Jamie Madill2e16d962017-04-19 14:06:36 -0400196
Jamie Madill16daadb2017-08-26 23:34:31 -0400197for cmd_name in gles3_commands:
198 command_xpath = "command/proto[name='" + cmd_name + "']/.."
199 command = commands.find(command_xpath)
200 params = ["".join(param.itertext()) for param in command.findall("./param")]
201 proto = "".join(command.find("./proto").itertext())
202 cmd_names += [cmd_name]
203 entry_point_decls_gles_3_0 += [format_entry_point_decl(cmd_name, proto, params)]
204 entry_point_defs_gles_3_0 += [format_entry_point_def(cmd_name, proto, params)]
205
Jamie Madill2e16d962017-04-19 14:06:36 -0400206gles_2_0_header = template_entry_point_header.format(
207 script_name = os.path.basename(sys.argv[0]),
208 data_source_name = "gl.xml",
209 year = date.today().year,
210 major_version = 2,
211 minor_version = 0,
212 entry_points = "\n".join(entry_point_decls_gles_2_0))
213
Jamie Madillee769dd2017-05-04 11:38:30 -0400214gles_2_0_source = template_entry_point_source.format(
215 script_name = os.path.basename(sys.argv[0]),
216 data_source_name = "gl.xml",
217 year = date.today().year,
218 major_version = 2,
219 minor_version = 0,
220 entry_points = "\n".join(entry_point_defs_gles_2_0))
221
Jamie Madill16daadb2017-08-26 23:34:31 -0400222gles_3_0_header = template_entry_point_header.format(
223 script_name = os.path.basename(sys.argv[0]),
224 data_source_name = "gl.xml",
225 year = date.today().year,
226 major_version = 3,
227 minor_version = 0,
228 entry_points = "\n".join(entry_point_decls_gles_3_0))
229
230# TODO(jmadill): Remove manually added entry points once we auto-gen them.
231manual_cmd_names = ["Invalid"] + [cmd[2:] for cmd in cmd_names] + ["DrawElementsInstancedANGLE"]
Jamie Madillee769dd2017-05-04 11:38:30 -0400232entry_points_enum = template_entry_points_enum_header.format(
233 script_name = os.path.basename(sys.argv[0]),
234 data_source_name = "gl.xml",
235 year = date.today().year,
236 entry_points_list = ",\n".join([" " + cmd for cmd in manual_cmd_names]))
237
Jamie Madill2e16d962017-04-19 14:06:36 -0400238def path_to(folder, file):
239 return os.path.join(script_relative(".."), "src", folder, file)
240
241gles_2_0_header_path = path_to("libGLESv2", "entry_points_gles_2_0_autogen.h")
Jamie Madillee769dd2017-05-04 11:38:30 -0400242gles_2_0_source_path = path_to("libGLESv2", "entry_points_gles_2_0_autogen.cpp")
Jamie Madill16daadb2017-08-26 23:34:31 -0400243gles_3_0_header_path = path_to("libGLESv2", "entry_points_gles_3_0_autogen.h")
244gles_3_0_source_path = path_to("libGLESv2", "entry_points_gles_3_0_autogen.cpp")
Jamie Madillee769dd2017-05-04 11:38:30 -0400245entry_points_enum_header_path = path_to("libANGLE", "entry_points_enum_autogen.h")
Jamie Madill2e16d962017-04-19 14:06:36 -0400246
247with open(gles_2_0_header_path, "w") as out:
248 out.write(gles_2_0_header)
249 out.close()
Jamie Madillee769dd2017-05-04 11:38:30 -0400250
251with open(gles_2_0_source_path, "w") as out:
252 out.write(gles_2_0_source)
253 out.close()
254
Jamie Madill16daadb2017-08-26 23:34:31 -0400255with open(gles_3_0_header_path, "w") as out:
256 out.write(gles_3_0_header)
257 out.close()
258
Jamie Madillee769dd2017-05-04 11:38:30 -0400259with open(entry_points_enum_header_path, "w") as out:
260 out.write(entry_points_enum)
Geoff Langcae72d62017-06-01 11:53:45 -0400261 out.close()