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