xref: /aosp_15_r20/external/angle/scripts/gen_proc_table.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker#!/usr/bin/python3
2*8975f5c5SAndroid Build Coastguard Worker# Copyright 2017 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker#
6*8975f5c5SAndroid Build Coastguard Worker# gen_proc_table.py:
7*8975f5c5SAndroid Build Coastguard Worker#  Code generation for entry point loading tables.
8*8975f5c5SAndroid Build Coastguard Worker#  NOTE: don't run this script directly. Run scripts/run_code_generation.py.
9*8975f5c5SAndroid Build Coastguard Worker
10*8975f5c5SAndroid Build Coastguard Workerimport os
11*8975f5c5SAndroid Build Coastguard Workerimport sys
12*8975f5c5SAndroid Build Coastguard Workerimport registry_xml
13*8975f5c5SAndroid Build Coastguard Worker
14*8975f5c5SAndroid Build Coastguard Workerout_file_name_gles = "../src/libGLESv2/proc_table_egl_autogen.cpp"
15*8975f5c5SAndroid Build Coastguard Workerout_file_name_cl = "../src/libGLESv2/proc_table_cl_autogen.cpp"
16*8975f5c5SAndroid Build Coastguard Workerout_file_name_cl_map = "../src/libOpenCL/libOpenCL_autogen.map"
17*8975f5c5SAndroid Build Coastguard Worker
18*8975f5c5SAndroid Build Coastguard Workerstrip_suffixes = ["ANGLE", "EXT", "KHR", "OES", "CHROMIUM", "OVR"]
19*8975f5c5SAndroid Build Coastguard Worker
20*8975f5c5SAndroid Build Coastguard Workertemplate_cpp = """// GENERATED FILE - DO NOT EDIT.
21*8975f5c5SAndroid Build Coastguard Worker// Generated by {script_name} using data from {data_source_name}.
22*8975f5c5SAndroid Build Coastguard Worker//
23*8975f5c5SAndroid Build Coastguard Worker// Copyright 2019 The ANGLE Project Authors. All rights reserved.
24*8975f5c5SAndroid Build Coastguard Worker// Use of this source code is governed by a BSD-style license that can be
25*8975f5c5SAndroid Build Coastguard Worker// found in the LICENSE file.
26*8975f5c5SAndroid Build Coastguard Worker//
27*8975f5c5SAndroid Build Coastguard Worker// getProcAddress loader table:
28*8975f5c5SAndroid Build Coastguard Worker//   Mapping from a string entry point name to function address.
29*8975f5c5SAndroid Build Coastguard Worker//
30*8975f5c5SAndroid Build Coastguard Worker
31*8975f5c5SAndroid Build Coastguard Worker{includes}
32*8975f5c5SAndroid Build Coastguard Worker#define P(FUNC) reinterpret_cast<{cast}>(FUNC)
33*8975f5c5SAndroid Build Coastguard Worker
34*8975f5c5SAndroid Build Coastguard Workernamespace {namespace}
35*8975f5c5SAndroid Build Coastguard Worker{{
36*8975f5c5SAndroid Build Coastguard Worker// clang-format off
37*8975f5c5SAndroid Build Coastguard Workerconst ProcEntry g_procTable[] = {{
38*8975f5c5SAndroid Build Coastguard Worker{proc_data}
39*8975f5c5SAndroid Build Coastguard Worker}};
40*8975f5c5SAndroid Build Coastguard Worker// clang-format on
41*8975f5c5SAndroid Build Coastguard Workerconst size_t g_numProcs = {num_procs};
42*8975f5c5SAndroid Build Coastguard Worker}}  // namespace {namespace}
43*8975f5c5SAndroid Build Coastguard Worker"""
44*8975f5c5SAndroid Build Coastguard Worker
45*8975f5c5SAndroid Build Coastguard Worker# FOR OPENCL
46*8975f5c5SAndroid Build Coastguard Workertemplate_map_cpp = """// GENERATED FILE - DO NOT EDIT.
47*8975f5c5SAndroid Build Coastguard Worker// Generated by {script_name} using data from {data_source_name}.
48*8975f5c5SAndroid Build Coastguard Worker//
49*8975f5c5SAndroid Build Coastguard Worker// Copyright 2021 The ANGLE Project Authors. All rights reserved.
50*8975f5c5SAndroid Build Coastguard Worker// Use of this source code is governed by a BSD-style license that can be
51*8975f5c5SAndroid Build Coastguard Worker// found in the LICENSE file.
52*8975f5c5SAndroid Build Coastguard Worker//
53*8975f5c5SAndroid Build Coastguard Worker// proc_table:
54*8975f5c5SAndroid Build Coastguard Worker//   Mapping from a string entry point name to function address.
55*8975f5c5SAndroid Build Coastguard Worker//
56*8975f5c5SAndroid Build Coastguard Worker
57*8975f5c5SAndroid Build Coastguard Worker{includes}
58*8975f5c5SAndroid Build Coastguard Worker#define P(FUNC) reinterpret_cast<{cast}>(FUNC)
59*8975f5c5SAndroid Build Coastguard Worker
60*8975f5c5SAndroid Build Coastguard Workernamespace {namespace}
61*8975f5c5SAndroid Build Coastguard Worker{{
62*8975f5c5SAndroid Build Coastguard Worker
63*8975f5c5SAndroid Build Coastguard Workerconst ProcTable &GetProcTable()
64*8975f5c5SAndroid Build Coastguard Worker{{
65*8975f5c5SAndroid Build Coastguard Worker    static angle::base::NoDestructor<ProcTable> sProcTable(
66*8975f5c5SAndroid Build Coastguard Worker        {{{proc_data}}});
67*8975f5c5SAndroid Build Coastguard Worker    return *sProcTable;
68*8975f5c5SAndroid Build Coastguard Worker}}
69*8975f5c5SAndroid Build Coastguard Worker
70*8975f5c5SAndroid Build Coastguard Worker}}  // namespace {namespace}
71*8975f5c5SAndroid Build Coastguard Worker"""
72*8975f5c5SAndroid Build Coastguard Worker
73*8975f5c5SAndroid Build Coastguard Worker# FOR OPENCL
74*8975f5c5SAndroid Build Coastguard Workertemplate_map = """/* GENERATED FILE - DO NOT EDIT.
75*8975f5c5SAndroid Build Coastguard Worker * Generated by {script_name} using data from {data_source_name}.
76*8975f5c5SAndroid Build Coastguard Worker *
77*8975f5c5SAndroid Build Coastguard Worker * Copyright 2021 The ANGLE Project Authors. All rights reserved.
78*8975f5c5SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
79*8975f5c5SAndroid Build Coastguard Worker * found in the LICENSE file.
80*8975f5c5SAndroid Build Coastguard Worker *
81*8975f5c5SAndroid Build Coastguard Worker * symbol version map: Maps versions to entry point names for a shared library.
82*8975f5c5SAndroid Build Coastguard Worker */
83*8975f5c5SAndroid Build Coastguard Worker{symbol_maps}
84*8975f5c5SAndroid Build Coastguard Worker"""
85*8975f5c5SAndroid Build Coastguard Worker
86*8975f5c5SAndroid Build Coastguard Workerincludes_gles = """#include "libGLESv2/proc_table_egl.h"
87*8975f5c5SAndroid Build Coastguard Worker
88*8975f5c5SAndroid Build Coastguard Worker#include "libGLESv2/entry_points_egl_autogen.h"
89*8975f5c5SAndroid Build Coastguard Worker#include "libGLESv2/entry_points_egl_ext_autogen.h"
90*8975f5c5SAndroid Build Coastguard Worker#include "libGLESv2/entry_points_gles_1_0_autogen.h"
91*8975f5c5SAndroid Build Coastguard Worker#include "libGLESv2/entry_points_gles_2_0_autogen.h"
92*8975f5c5SAndroid Build Coastguard Worker#include "libGLESv2/entry_points_gles_3_0_autogen.h"
93*8975f5c5SAndroid Build Coastguard Worker#include "libGLESv2/entry_points_gles_3_1_autogen.h"
94*8975f5c5SAndroid Build Coastguard Worker#include "libGLESv2/entry_points_gles_3_2_autogen.h"
95*8975f5c5SAndroid Build Coastguard Worker#include "libGLESv2/entry_points_gles_ext_autogen.h"
96*8975f5c5SAndroid Build Coastguard Worker#include "platform/PlatformMethods.h"
97*8975f5c5SAndroid Build Coastguard Worker
98*8975f5c5SAndroid Build Coastguard Worker#include <iterator>
99*8975f5c5SAndroid Build Coastguard Worker"""
100*8975f5c5SAndroid Build Coastguard Worker
101*8975f5c5SAndroid Build Coastguard Workerincludes_cl = """#include "libGLESv2/proc_table_cl.h"
102*8975f5c5SAndroid Build Coastguard Worker
103*8975f5c5SAndroid Build Coastguard Worker#include "libGLESv2/entry_points_cl_autogen.h"
104*8975f5c5SAndroid Build Coastguard Worker
105*8975f5c5SAndroid Build Coastguard Worker#include "anglebase/no_destructor.h"
106*8975f5c5SAndroid Build Coastguard Worker
107*8975f5c5SAndroid Build Coastguard Worker// Using fully qualified entry point identifiers to make sure that missing entry points would not
108*8975f5c5SAndroid Build Coastguard Worker// pick up the global declarations of OpenCL
109*8975f5c5SAndroid Build Coastguard Worker"""
110*8975f5c5SAndroid Build Coastguard Worker
111*8975f5c5SAndroid Build Coastguard Workersys.path.append('../src/libANGLE/renderer')
112*8975f5c5SAndroid Build Coastguard Workerimport angle_format
113*8975f5c5SAndroid Build Coastguard Worker
114*8975f5c5SAndroid Build Coastguard Worker
115*8975f5c5SAndroid Build Coastguard Workerdef _get_annotations(versions):
116*8975f5c5SAndroid Build Coastguard Worker    return ["%d_%d" % version for version in versions]
117*8975f5c5SAndroid Build Coastguard Worker
118*8975f5c5SAndroid Build Coastguard Worker
119*8975f5c5SAndroid Build Coastguard Workerdef main():
120*8975f5c5SAndroid Build Coastguard Worker
121*8975f5c5SAndroid Build Coastguard Worker    # auto_script parameters.
122*8975f5c5SAndroid Build Coastguard Worker    if len(sys.argv) > 1:
123*8975f5c5SAndroid Build Coastguard Worker        inputs = [source for source in registry_xml.xml_inputs]
124*8975f5c5SAndroid Build Coastguard Worker        outputs = [out_file_name_gles, out_file_name_cl, out_file_name_cl_map]
125*8975f5c5SAndroid Build Coastguard Worker        if sys.argv[1] == 'inputs':
126*8975f5c5SAndroid Build Coastguard Worker            print(','.join(inputs))
127*8975f5c5SAndroid Build Coastguard Worker        elif sys.argv[1] == 'outputs':
128*8975f5c5SAndroid Build Coastguard Worker            print(','.join(outputs))
129*8975f5c5SAndroid Build Coastguard Worker        else:
130*8975f5c5SAndroid Build Coastguard Worker            print('Invalid script parameters')
131*8975f5c5SAndroid Build Coastguard Worker            return 1
132*8975f5c5SAndroid Build Coastguard Worker        return 0
133*8975f5c5SAndroid Build Coastguard Worker
134*8975f5c5SAndroid Build Coastguard Worker    glesxml = registry_xml.RegistryXML('gl.xml', 'gl_angle_ext.xml')
135*8975f5c5SAndroid Build Coastguard Worker
136*8975f5c5SAndroid Build Coastguard Worker    for annotation in _get_annotations(registry_xml.GLES_VERSIONS):
137*8975f5c5SAndroid Build Coastguard Worker        name_prefix = "GL_ES_VERSION_"
138*8975f5c5SAndroid Build Coastguard Worker        if annotation[0] == '1':
139*8975f5c5SAndroid Build Coastguard Worker            name_prefix = "GL_VERSION_ES_CM_"
140*8975f5c5SAndroid Build Coastguard Worker        feature_name = "{}{}".format(name_prefix, annotation)
141*8975f5c5SAndroid Build Coastguard Worker        glesxml.AddCommands(feature_name, annotation)
142*8975f5c5SAndroid Build Coastguard Worker
143*8975f5c5SAndroid Build Coastguard Worker    glesxml.AddExtensionCommands(registry_xml.supported_extensions, ['gles2', 'gles1'])
144*8975f5c5SAndroid Build Coastguard Worker
145*8975f5c5SAndroid Build Coastguard Worker    # Also don't add GLES extension commands to libGL proc table
146*8975f5c5SAndroid Build Coastguard Worker    extension_commands = []
147*8975f5c5SAndroid Build Coastguard Worker    for extension_name, ext_cmd_names in sorted(glesxml.ext_data.items()):
148*8975f5c5SAndroid Build Coastguard Worker        extension_commands.extend(glesxml.ext_data[extension_name])
149*8975f5c5SAndroid Build Coastguard Worker    for name in extension_commands:
150*8975f5c5SAndroid Build Coastguard Worker        name_no_suffix = name
151*8975f5c5SAndroid Build Coastguard Worker        for suffix in strip_suffixes:
152*8975f5c5SAndroid Build Coastguard Worker            if name_no_suffix.endswith(suffix):
153*8975f5c5SAndroid Build Coastguard Worker                name_no_suffix = name_no_suffix[0:-len(suffix)]
154*8975f5c5SAndroid Build Coastguard Worker
155*8975f5c5SAndroid Build Coastguard Worker    gles_data = glesxml.all_cmd_names.get_all_commands()
156*8975f5c5SAndroid Build Coastguard Worker    eglxml = registry_xml.RegistryXML('egl.xml', 'egl_angle_ext.xml')
157*8975f5c5SAndroid Build Coastguard Worker
158*8975f5c5SAndroid Build Coastguard Worker    for annotation in _get_annotations(registry_xml.EGL_VERSIONS):
159*8975f5c5SAndroid Build Coastguard Worker        name_prefix = "EGL_VERSION_"
160*8975f5c5SAndroid Build Coastguard Worker        feature_name = "{}{}".format(name_prefix, annotation)
161*8975f5c5SAndroid Build Coastguard Worker        eglxml.AddCommands(feature_name, annotation)
162*8975f5c5SAndroid Build Coastguard Worker
163*8975f5c5SAndroid Build Coastguard Worker    eglxml.AddExtensionCommands(registry_xml.supported_egl_extensions, ['gles2', 'gles1'])
164*8975f5c5SAndroid Build Coastguard Worker
165*8975f5c5SAndroid Build Coastguard Worker    gles_data.extend(eglxml.all_cmd_names.get_all_commands())
166*8975f5c5SAndroid Build Coastguard Worker
167*8975f5c5SAndroid Build Coastguard Worker    gles_data.append("ANGLEGetDisplayPlatform")
168*8975f5c5SAndroid Build Coastguard Worker    gles_data.append("ANGLEResetDisplayPlatform")
169*8975f5c5SAndroid Build Coastguard Worker    gles_data = set(gles_data)
170*8975f5c5SAndroid Build Coastguard Worker
171*8975f5c5SAndroid Build Coastguard Worker    all_functions = {}
172*8975f5c5SAndroid Build Coastguard Worker    for function in gles_data:
173*8975f5c5SAndroid Build Coastguard Worker        if function.startswith("gl"):
174*8975f5c5SAndroid Build Coastguard Worker            all_functions[function] = "GL_" + function[2:]
175*8975f5c5SAndroid Build Coastguard Worker        elif function.startswith("egl"):
176*8975f5c5SAndroid Build Coastguard Worker            all_functions[function] = "EGL_" + function[3:]
177*8975f5c5SAndroid Build Coastguard Worker        else:
178*8975f5c5SAndroid Build Coastguard Worker            all_functions[function] = function
179*8975f5c5SAndroid Build Coastguard Worker
180*8975f5c5SAndroid Build Coastguard Worker    proc_data = []
181*8975f5c5SAndroid Build Coastguard Worker    for func, angle_func in sorted(all_functions.items()):
182*8975f5c5SAndroid Build Coastguard Worker        proc_data.append('    {"%s", P(%s)},' % (func, angle_func))
183*8975f5c5SAndroid Build Coastguard Worker
184*8975f5c5SAndroid Build Coastguard Worker    with open(out_file_name_gles, 'w') as out_file:
185*8975f5c5SAndroid Build Coastguard Worker        output_cpp = template_cpp.format(
186*8975f5c5SAndroid Build Coastguard Worker            script_name=os.path.basename(sys.argv[0]),
187*8975f5c5SAndroid Build Coastguard Worker            data_source_name="gl.xml, gl_angle_ext.xml, egl.xml, egl_angle_ext.xml",
188*8975f5c5SAndroid Build Coastguard Worker            includes=includes_gles,
189*8975f5c5SAndroid Build Coastguard Worker            cast="__eglMustCastToProperFunctionPointerType",
190*8975f5c5SAndroid Build Coastguard Worker            namespace="egl",
191*8975f5c5SAndroid Build Coastguard Worker            proc_data="\n".join(proc_data),
192*8975f5c5SAndroid Build Coastguard Worker            num_procs="std::size(g_procTable)")
193*8975f5c5SAndroid Build Coastguard Worker        out_file.write(output_cpp)
194*8975f5c5SAndroid Build Coastguard Worker        out_file.close()
195*8975f5c5SAndroid Build Coastguard Worker
196*8975f5c5SAndroid Build Coastguard Worker    # libCL proc table
197*8975f5c5SAndroid Build Coastguard Worker    clxml = registry_xml.RegistryXML('cl.xml')
198*8975f5c5SAndroid Build Coastguard Worker    symbol_maps = []
199*8975f5c5SAndroid Build Coastguard Worker    symbol_map_dependency = ""
200*8975f5c5SAndroid Build Coastguard Worker
201*8975f5c5SAndroid Build Coastguard Worker    for major_version, minor_version in registry_xml.CL_VERSIONS:
202*8975f5c5SAndroid Build Coastguard Worker        name_prefix = "CL_VERSION_"
203*8975f5c5SAndroid Build Coastguard Worker        annotation = "%d_%d" % (major_version, minor_version)
204*8975f5c5SAndroid Build Coastguard Worker        feature_name = "%s%s" % (name_prefix, annotation)
205*8975f5c5SAndroid Build Coastguard Worker        clxml.AddCommands(feature_name, annotation)
206*8975f5c5SAndroid Build Coastguard Worker        symbol_version = "OPENCL_%d.%d" % (major_version, minor_version)
207*8975f5c5SAndroid Build Coastguard Worker        symbol_maps += ["\n%s {\n    global:" % symbol_version]
208*8975f5c5SAndroid Build Coastguard Worker        symbol_maps += ['        %s;' % cmd for cmd in clxml.commands[annotation]]
209*8975f5c5SAndroid Build Coastguard Worker        if not symbol_map_dependency:
210*8975f5c5SAndroid Build Coastguard Worker            symbol_maps += ["    local:\n        *;\n};"]
211*8975f5c5SAndroid Build Coastguard Worker        else:
212*8975f5c5SAndroid Build Coastguard Worker            symbol_maps += ["} %s;" % symbol_map_dependency]
213*8975f5c5SAndroid Build Coastguard Worker        symbol_map_dependency = symbol_version
214*8975f5c5SAndroid Build Coastguard Worker
215*8975f5c5SAndroid Build Coastguard Worker    clxml.AddExtensionCommands(registry_xml.supported_cl_extensions, ['cl'])
216*8975f5c5SAndroid Build Coastguard Worker    cl_commands = clxml.all_cmd_names.get_all_commands()
217*8975f5c5SAndroid Build Coastguard Worker    proc_data = ['{"%s", P(::cl::%s)}' % (cmd, cmd) for cmd in cl_commands]
218*8975f5c5SAndroid Build Coastguard Worker
219*8975f5c5SAndroid Build Coastguard Worker    with open(out_file_name_cl, 'w') as out_file:
220*8975f5c5SAndroid Build Coastguard Worker        output_cpp = template_map_cpp.format(
221*8975f5c5SAndroid Build Coastguard Worker            script_name=os.path.basename(sys.argv[0]),
222*8975f5c5SAndroid Build Coastguard Worker            data_source_name="cl.xml",
223*8975f5c5SAndroid Build Coastguard Worker            includes=includes_cl,
224*8975f5c5SAndroid Build Coastguard Worker            cast="void *",
225*8975f5c5SAndroid Build Coastguard Worker            namespace="cl",
226*8975f5c5SAndroid Build Coastguard Worker            proc_data=",\n         ".join(proc_data))
227*8975f5c5SAndroid Build Coastguard Worker        out_file.write(output_cpp)
228*8975f5c5SAndroid Build Coastguard Worker        out_file.close()
229*8975f5c5SAndroid Build Coastguard Worker
230*8975f5c5SAndroid Build Coastguard Worker    with open(out_file_name_cl_map, 'w') as out_file:
231*8975f5c5SAndroid Build Coastguard Worker        output_map = template_map.format(
232*8975f5c5SAndroid Build Coastguard Worker            script_name=os.path.basename(sys.argv[0]),
233*8975f5c5SAndroid Build Coastguard Worker            data_source_name="cl.xml",
234*8975f5c5SAndroid Build Coastguard Worker            symbol_maps="\n".join(symbol_maps))
235*8975f5c5SAndroid Build Coastguard Worker        out_file.write(output_map)
236*8975f5c5SAndroid Build Coastguard Worker        out_file.close()
237*8975f5c5SAndroid Build Coastguard Worker
238*8975f5c5SAndroid Build Coastguard Worker    return 0
239*8975f5c5SAndroid Build Coastguard Worker
240*8975f5c5SAndroid Build Coastguard Worker
241*8975f5c5SAndroid Build Coastguard Workerif __name__ == '__main__':
242*8975f5c5SAndroid Build Coastguard Worker    sys.exit(main())
243