1#!/usr/bin/python3
2# Copyright 2021 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_load_texture_border_functions_table.py:
7#  Code generation for the load texture border function tables used for texture formats. These mappings are
8#  not renderer specific. The mappings are done from the GL internal format, to the ANGLE format ID.
9#  NOTE: don't run this script directly. Run scripts/run_code_generation.py.
10#
11
12import json, sys
13
14sys.path.append('../..')
15import angle_format
16
17template = """// GENERATED FILE - DO NOT EDIT.
18// Generated by gen_load_texture_border_functions_table.py using data from load_texture_border_functions_data.json
19//
20// Copyright 2021 The ANGLE Project Authors. All rights reserved.
21// Use of this source code is governed by a BSD-style license that can be
22// found in the LICENSE file.
23//
24// load_texture_border_functions_table":
25//   Contains the LoadTextureBorderFunctionMap for texture_format_util.h
26//
27
28#include "libANGLE/renderer/load_texture_border_functions_table.h"
29#include "image_util/loadtextureborder.h"
30
31using namespace rx;
32
33namespace angle
34{{
35
36namespace
37{{
38
39// ES3 texture border color loading functions vary based on:
40//    - the GL internal format (supplied to glTex*Image*D)
41//    - the target DXGI_FORMAT that the image will be loaded into (which is chosen based on the D3D
42//    device's capabilities)
43// This map type determines which loading function to use, based on these two parameters.
44// This map only contains formats which need to reorder border color channel explictly.
45
46{load_functions_data}}}  // namespace
47
48LoadTextureBorderFunctionMap GetLoadTextureBorderFunctionsMap(GLenum {internal_format}, FormatID {angle_format})
49{{
50    switch ({internal_format})
51    {{
52{switch_data}
53        default:
54            // Use LoadToNative for any format that doesn't reorder channels.
55            return DefaultLoadFunction;
56    }}
57
58}}  // GetLoadTextureBorderFunctionsMap
59
60}}  // namespace angle
61"""
62
63internal_format_param = 'internalFormat'
64angle_format_param = 'angleFormat'
65angle_format_unknown = 'NONE'
66
67
68def load_functions_name(internal_format, angle_format):
69    return internal_format[3:] + "_to_" + angle_format
70
71
72def get_load_func(func_name, load_function):
73    snippet = "LoadTextureBorderFunctionInfo " + func_name + "()\n"
74    snippet += "{\n"
75    snippet += "    return LoadTextureBorderFunctionInfo(" + load_function + ");\n"
76    snippet += "}\n"
77    snippet += "\n"
78
79    return snippet
80
81
82def parse_json(json_data):
83    table_data = ''
84    load_functions_data = ''
85    load_functions_data += get_load_func('DefaultLoadFunction', 'LoadToNative')
86    for internal_format, angle_format_func in sorted(json_data["map"].items()):
87
88        s = '        '
89
90        table_data += s + 'case ' + internal_format + ':\n'
91
92        do_switch = len(angle_format_func) > 1 or list(
93            angle_format_func)[0] != angle_format_unknown
94
95        if do_switch:
96            table_data += s + '{\n'
97            s += '    '
98            table_data += s + 'switch (' + angle_format_param + ')\n'
99            table_data += s + '{\n'
100            s += '    '
101
102            for angle_format, load_function in sorted(angle_format_func.items()):
103                func_name = load_functions_name(internal_format, angle_format)
104                # Main case statements
105                table_data += s + 'case FormatID::' + angle_format + ':\n'
106                table_data += s + '    return ' + func_name + ';\n'
107
108                load_functions_data += get_load_func(func_name, load_function)
109
110        if do_switch:
111            table_data += s + 'default:\n'
112            table_data += s + '    return DefaultLoadFunction;\n'
113
114        if do_switch:
115            s = s[4:]
116            table_data += s + '}\n'
117            s = s[4:]
118            table_data += s + '}\n'
119
120    return table_data, load_functions_data
121
122
123def main():
124
125    # auto_script parameters.
126    if len(sys.argv) > 1:
127        inputs = ['angle_format.py', 'load_texture_border_functions_data.json']
128        outputs = ['load_texture_border_functions_table_autogen.cpp']
129
130        if sys.argv[1] == 'inputs':
131            print(','.join(inputs))
132        elif sys.argv[1] == 'outputs':
133            print(','.join(outputs))
134        else:
135            print('Invalid script parameters')
136            return 1
137        return 0
138
139    json_data = angle_format.load_json('load_texture_border_functions_data.json')
140
141    switch_data, load_functions_data = parse_json(json_data)
142    output = template.format(
143        internal_format=internal_format_param,
144        angle_format=angle_format_param,
145        switch_data=switch_data,
146        load_functions_data=load_functions_data)
147
148    with open('load_texture_border_functions_table_autogen.cpp', 'wt') as out_file:
149        out_file.write(output)
150        out_file.close()
151    return 0
152
153
154if __name__ == '__main__':
155    sys.exit(main())
156