xref: /aosp_15_r20/external/angle/src/common/gen_uniform_type_table.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1#!/usr/bin/python3
2# Copyright 2017 The ANGLE Project Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5#
6# gen_uniform_type_table.py:
7#  Code generation for OpenGL uniform type info tables.
8#  NOTE: don't run this script directly. Run scripts/run_code_generation.py.
9
10import os
11import sys
12
13all_uniform_types = [
14    "GL_NONE", "GL_BOOL", "GL_BOOL_VEC2", "GL_BOOL_VEC3", "GL_BOOL_VEC4", "GL_FLOAT",
15    "GL_FLOAT_MAT2", "GL_FLOAT_MAT2x3", "GL_FLOAT_MAT2x4", "GL_FLOAT_MAT3", "GL_FLOAT_MAT3x2",
16    "GL_FLOAT_MAT3x4", "GL_FLOAT_MAT4", "GL_FLOAT_MAT4x2", "GL_FLOAT_MAT4x3", "GL_FLOAT_VEC2",
17    "GL_FLOAT_VEC3", "GL_FLOAT_VEC4", "GL_IMAGE_2D", "GL_IMAGE_2D_ARRAY", "GL_IMAGE_3D",
18    "GL_IMAGE_CUBE", "GL_IMAGE_CUBE_MAP_ARRAY", "GL_IMAGE_BUFFER", "GL_INT", "GL_INT_IMAGE_2D",
19    "GL_INT_IMAGE_2D_ARRAY", "GL_INT_IMAGE_3D", "GL_INT_IMAGE_CUBE", "GL_INT_IMAGE_CUBE_MAP_ARRAY",
20    "GL_INT_IMAGE_BUFFER", "GL_INT_SAMPLER_2D", "GL_INT_SAMPLER_2D_ARRAY",
21    "GL_INT_SAMPLER_2D_MULTISAMPLE", "GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY", "GL_INT_SAMPLER_3D",
22    "GL_INT_SAMPLER_CUBE", "GL_INT_SAMPLER_CUBE_MAP_ARRAY", "GL_INT_SAMPLER_BUFFER", "GL_INT_VEC2",
23    "GL_INT_VEC3", "GL_INT_VEC4", "GL_SAMPLER_2D", "GL_SAMPLER_2D_ARRAY",
24    "GL_SAMPLER_2D_ARRAY_SHADOW", "GL_SAMPLER_2D_MULTISAMPLE", "GL_SAMPLER_2D_MULTISAMPLE_ARRAY",
25    "GL_SAMPLER_2D_RECT_ANGLE", "GL_SAMPLER_2D_SHADOW", "GL_SAMPLER_3D", "GL_SAMPLER_CUBE",
26    "GL_SAMPLER_CUBE_MAP_ARRAY", "GL_SAMPLER_BUFFER", "GL_SAMPLER_CUBE_SHADOW",
27    "GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW", "GL_SAMPLER_EXTERNAL_OES", "GL_UNSIGNED_INT",
28    "GL_UNSIGNED_INT_ATOMIC_COUNTER", "GL_UNSIGNED_INT_IMAGE_2D", "GL_UNSIGNED_INT_IMAGE_2D_ARRAY",
29    "GL_UNSIGNED_INT_IMAGE_3D", "GL_UNSIGNED_INT_IMAGE_CUBE",
30    "GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY", "GL_UNSIGNED_INT_IMAGE_BUFFER",
31    "GL_UNSIGNED_INT_SAMPLER_2D", "GL_UNSIGNED_INT_SAMPLER_2D_ARRAY",
32    "GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE", "GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY",
33    "GL_UNSIGNED_INT_SAMPLER_3D", "GL_UNSIGNED_INT_SAMPLER_CUBE",
34    "GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY", "GL_UNSIGNED_INT_SAMPLER_BUFFER",
35    "GL_UNSIGNED_INT_VEC2", "GL_UNSIGNED_INT_VEC3", "GL_UNSIGNED_INT_VEC4",
36    "GL_SAMPLER_VIDEO_IMAGE_WEBGL", "GL_SAMPLER_EXTERNAL_2D_Y2Y_EXT"
37]
38
39# Uniform texture types. Be wary of substrings finding the wrong types.
40# e.g. with 2D_MULTISAMPLE/2D_ARRAY and 2D.
41texture_types = {
42    "2D": "2D",
43    "2D_ARRAY": "2D_ARRAY",
44    "2D_ARRAY_SHADOW": "2D_ARRAY",
45    "2D_MULTISAMPLE": "2D_MULTISAMPLE",
46    "2D_MULTISAMPLE_ARRAY": "2D_MULTISAMPLE_ARRAY",
47    "2D_RECT_ANGLE": "2D",
48    "2D_SHADOW": "2D",
49    "3D": "3D",
50    "CUBE": "CUBE_MAP",
51    "CUBE_SHADOW": "CUBE_MAP",
52    "EXTERNAL_OES": "EXTERNAL_OES",
53    "RECT": "RECTANGLE",
54    "CUBE_MAP_ARRAY": "CUBE_MAP_ARRAY",
55    "BUFFER": "BUFFER",
56    "VIDEO_IMAGE_WEBGL": "VIDEO_IMAGE_WEBGL",
57}
58
59template_cpp = """// GENERATED FILE - DO NOT EDIT.
60// Generated by {script_name}.
61//
62// Copyright 2017 The ANGLE Project Authors. All rights reserved.
63// Use of this source code is governed by a BSD-style license that can be
64// found in the LICENSE file.
65//
66// Uniform type info table:
67//   Metadata about a particular uniform format, indexed by GL type.
68
69#include <array>
70#include "common/utilities.h"
71
72using namespace angle;
73
74namespace gl
75{{
76
77namespace
78{{
79constexpr std::array<UniformTypeInfo, {total_count}> kInfoTable =
80{{{{
81{uniform_type_info_data}
82}}}};
83
84uint16_t GetIndex(GLenum uniformType)
85{{
86    switch (uniformType)
87    {{
88{uniform_type_index_cases}
89        default:
90            UNREACHABLE();
91            return 0;
92    }}
93}}
94}}  // anonymous namespace
95
96UniformTypeIndex GetUniformTypeIndex(GLenum uniformType)
97{{
98    return UniformTypeIndex{{GetIndex(uniformType)}};
99}}
100
101const UniformTypeInfo &GetUniformTypeInfo(GLenum uniformType)
102{{
103    ASSERT(kInfoTable[GetIndex(uniformType)].type == uniformType);
104    return kInfoTable[GetIndex(uniformType)];
105}}
106
107const UniformTypeInfo &GetUniformTypeInfoFromIndex(UniformTypeIndex index)
108{{
109    ASSERT(index.value >= 0 && index.value < {total_count});
110    return kInfoTable[index.value];
111}}
112
113}}  // namespace gl
114"""
115
116template_h = """// GENERATED FILE - DO NOT EDIT.
117// Generated by {script_name}.
118//
119// Copyright 2024 The ANGLE Project Authors. All rights reserved.
120// Use of this source code is governed by a BSD-style license that can be
121// found in the LICENSE file.
122//
123// Uniform type info table:
124//   Metadata about a particular uniform format, indexed by GL type.
125
126#ifndef COMMON_UNIFORM_TYPE_INFO_AUTOGEN_H_
127#define COMMON_UNIFORM_TYPE_INFO_AUTOGEN_H_
128
129#include "angle_gl.h"
130#include "utilities.h"
131
132namespace gl
133{{
134
135inline GLint GetUniformElementComponents(UniformTypeIndex index)
136{{
137    static constexpr std::array<uint8_t, {total_count}> kElementComponents = {{ {uniform_element_components} }};
138    ASSERT(index.value >= 0 && index.value < {total_count});
139    return kElementComponents[index.value];
140}}
141
142}}  // namespace gl
143#endif  // COMMON_UNIFORM_TYPE_INFO_AUTOGEN_H_
144"""
145
146type_info_data_template = """{{{type}, {component_type}, {texture_type}, {transposed_type}, {bool_type}, {sampler_format}, {rows}, {columns}, {components}, {component_size}, {internal_size}, {external_size}, {is_sampler}, {is_matrix}, {is_image} }}"""
147
148
149def cpp_bool(value):
150    return "true" if value else "false"
151
152
153def get_component_type(uniform_type):
154    if uniform_type.find("GL_BOOL") == 0:
155        return "GL_BOOL"
156    elif uniform_type.find("GL_FLOAT") == 0:
157        return "GL_FLOAT"
158    elif uniform_type.find("GL_INT") == 0:
159        return "GL_INT"
160    elif uniform_type.find("GL_UNSIGNED_INT") == 0:
161        return "GL_UNSIGNED_INT"
162    elif uniform_type == "GL_NONE":
163        return "GL_NONE"
164    else:
165        return "GL_INT"
166
167
168def get_texture_type(uniform_type):
169    for sampler_type, tex_type in texture_types.items():
170        if uniform_type.endswith(sampler_type):
171            return "GL_TEXTURE_" + tex_type
172    return "GL_NONE"
173
174
175def get_transposed_type(uniform_type):
176    if "_MAT" in uniform_type:
177        if "x" in uniform_type:
178            return "GL_FLOAT_MAT" + uniform_type[-1] + "x" + uniform_type[uniform_type.find("_MAT")
179                                                                          + 4]
180        else:
181            return uniform_type
182    else:
183        return "GL_NONE"
184
185
186def get_bool_type(uniform_type):
187    if uniform_type == "GL_INT" or uniform_type == "GL_UNSIGNED_INT" or uniform_type == "GL_FLOAT":
188        return "GL_BOOL"
189    elif "_VEC" in uniform_type:
190        return "GL_BOOL_VEC" + uniform_type[-1]
191    else:
192        return "GL_NONE"
193
194
195def get_sampler_format(uniform_type):
196    if not "_SAMPLER_" in uniform_type:
197        return "SamplerFormat::InvalidEnum"
198    elif "_SHADOW" in uniform_type:
199        return "SamplerFormat::Shadow"
200    elif "GL_UNSIGNED_INT_SAMPLER_" in uniform_type:
201        return "SamplerFormat::Unsigned"
202    elif "GL_INT_SAMPLER_" in uniform_type:
203        return "SamplerFormat::Signed"
204    else:
205        return "SamplerFormat::Float"
206
207
208def get_rows(uniform_type):
209    if uniform_type == "GL_NONE":
210        return "0"
211    elif "_MAT" in uniform_type:
212        return uniform_type[-1]
213    else:
214        return "1"
215
216
217def get_columns(uniform_type):
218    if uniform_type == "GL_NONE":
219        return "0"
220    elif "_VEC" in uniform_type:
221        return uniform_type[-1]
222    elif "_MAT" in uniform_type:
223        return uniform_type[uniform_type.find("_MAT") + 4]
224    else:
225        return "1"
226
227
228def get_components(uniform_type):
229    return str(int(get_rows(uniform_type)) * int(get_columns(uniform_type)))
230
231
232def get_component_size(uniform_type):
233    component_type = get_component_type(uniform_type)
234    if component_type == "GL_BOOL":
235        return "sizeof(GLint)"
236    elif component_type == "GL_FLOAT":
237        return "sizeof(GLfloat)"
238    elif component_type == "GL_INT":
239        return "sizeof(GLint)"
240    elif component_type == "GL_UNSIGNED_INT":
241        return "sizeof(GLuint)"
242    elif component_type == "GL_NONE":
243        return "0"
244    else:
245        raise "Invalid component type: " + component_type
246
247
248def get_internal_size(uniform_type):
249    return get_component_size(uniform_type) + " * " + str(int(get_rows(uniform_type)) * 4)
250
251
252def get_external_size(uniform_type):
253    return get_component_size(uniform_type) + " * " + get_components(uniform_type)
254
255
256def get_is_sampler(uniform_type):
257    return cpp_bool("_SAMPLER_" in uniform_type)
258
259
260def get_is_matrix(uniform_type):
261    return cpp_bool("_MAT" in uniform_type)
262
263
264def get_is_image(uniform_type):
265    return cpp_bool("_VIDEO_" not in uniform_type and "_IMAGE_" in uniform_type)
266
267
268def gen_type_info(uniform_type):
269    return type_info_data_template.format(
270        type=uniform_type,
271        component_type=get_component_type(uniform_type),
272        texture_type=get_texture_type(uniform_type),
273        transposed_type=get_transposed_type(uniform_type),
274        bool_type=get_bool_type(uniform_type),
275        sampler_format=get_sampler_format(uniform_type),
276        rows=get_rows(uniform_type),
277        columns=get_columns(uniform_type),
278        components=get_components(uniform_type),
279        component_size=get_component_size(uniform_type),
280        internal_size=get_internal_size(uniform_type),
281        external_size=get_external_size(uniform_type),
282        is_sampler=get_is_sampler(uniform_type),
283        is_matrix=get_is_matrix(uniform_type),
284        is_image=get_is_image(uniform_type))
285
286
287def gen_type_index_case(index, uniform_type):
288    return "case " + uniform_type + ": return " + str(index) + ";"
289
290
291def main():
292
293    # auto_script parameters.
294    if len(sys.argv) > 1:
295        inputs = []
296        outputs = ['uniform_type_info_autogen.cpp', 'uniform_type_info_autogen.h']
297
298        if sys.argv[1] == 'inputs':
299            print(','.join(inputs))
300        elif sys.argv[1] == 'outputs':
301            print(','.join(outputs))
302        else:
303            print('Invalid script parameters')
304            return 1
305        return 0
306
307    uniform_type_info_data = ",\n".join(
308        gen_type_info(uniform_type) for uniform_type in all_uniform_types)
309    uniform_type_index_cases = "\n".join(
310        gen_type_index_case(index, uniform_type)
311        for index, uniform_type in enumerate(all_uniform_types))
312
313    uniform_element_components = ", ".join(
314        get_components(uniform_type) for uniform_type in all_uniform_types)
315
316    with open('uniform_type_info_autogen.cpp', 'wt') as out_file:
317        output_cpp = template_cpp.format(
318            script_name=os.path.basename(sys.argv[0]),
319            total_count=len(all_uniform_types),
320            uniform_type_info_data=uniform_type_info_data,
321            uniform_type_index_cases=uniform_type_index_cases)
322        out_file.write(output_cpp)
323
324    with open('uniform_type_info_autogen.h', 'wt') as out_file:
325        output_h = template_h.format(
326            script_name=os.path.basename(sys.argv[0]),
327            total_count=len(all_uniform_types),
328            uniform_element_components=uniform_element_components)
329        out_file.write(output_h)
330        out_file.close()
331
332    return 0
333
334
335if __name__ == '__main__':
336    sys.exit(main())
337