• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3
2# Copyright 2016 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_dxgi_format_table.py:
7#  Code generation for DXGI format map.
8#  NOTE: don't run this script directly. Run scripts/run_code_generation.py.
9
10import sys
11import angle_format
12
13from functools import reduce
14
15template_cpp = """// GENERATED FILE - DO NOT EDIT.
16// Generated by {script_name} using data from {data_source_name}.
17//
18// Copyright 2020 The ANGLE Project Authors. All rights reserved.
19// Use of this source code is governed by a BSD-style license that can be
20// found in the LICENSE file.
21//
22// DXGI format info:
23//   Determining metadata about a DXGI format.
24
25#include "libANGLE/renderer/Format.h"
26
27using namespace angle;
28
29namespace rx
30{{
31
32namespace d3d11
33{{
34
35GLenum GetComponentType(DXGI_FORMAT dxgiFormat)
36{{
37    switch (dxgiFormat)
38    {{
39{component_type_cases}        default:
40            break;
41    }}
42
43    UNREACHABLE();
44    return GL_NONE;
45}}
46
47}}  // namespace d3d11
48
49namespace d3d11_angle
50{{
51
52const Format &GetFormat(DXGI_FORMAT dxgiFormat)
53{{
54    switch (dxgiFormat)
55    {{
56{format_cases}        default:
57            break;
58    }}
59
60    UNREACHABLE();
61    return Format::Get(FormatID::NONE);
62}}
63
64}}  // namespace d3d11_angle
65
66}}  // namespace rx
67"""
68
69template_format_case = """        case DXGI_FORMAT_{dxgi_format}:
70            return {result};
71"""
72
73template_undefined_case = """        case DXGI_FORMAT_{dxgi_format}:
74            break;
75"""
76
77
78def format_case(dxgi_format, result):
79    return template_format_case.format(dxgi_format=dxgi_format, result=result)
80
81
82def undefined_case(dxgi_format):
83    return template_undefined_case.format(dxgi_format=dxgi_format)
84
85
86def main():
87
88    # auto_script parameters.
89    if len(sys.argv) > 1:
90        inputs = [
91            'angle_format.py',
92            'angle_format_map.json',
93            'dxgi_format_data.json',
94        ]
95        outputs = ['dxgi_format_map_autogen.cpp']
96
97        if sys.argv[1] == 'inputs':
98            print(','.join(inputs))
99        elif sys.argv[1] == 'outputs':
100            print(','.join(outputs))
101        else:
102            print('Invalid script parameters')
103            return 1
104        return 0
105
106    component_cases = ""
107    format_cases = ""
108
109    input_data = 'dxgi_format_data.json'
110
111    dxgi_map = angle_format.load_json(input_data)
112
113    types = {
114        'SNORM': 'GL_SIGNED_NORMALIZED',
115        'UNORM': 'GL_UNSIGNED_NORMALIZED',
116        'SINT': 'GL_INT',
117        'UINT': 'GL_UNSIGNED_INT',
118        'FLOAT': 'GL_FLOAT',
119        'SHAREDEXP': 'GL_FLOAT'
120    }
121
122    all_angle = angle_format.get_all_angle_formats()
123
124    for dxgi_format, a_format in sorted(dxgi_map.items()):
125
126        found = [ctype in dxgi_format for ctype in types.keys()]
127        count = reduce((lambda a, b: int(a) + int(b)), found)
128
129        component_type = 'GL_NONE'
130
131        if count == 1:
132            gltype = next(
133                gltype for ctype, gltype in sorted(types.items()) if ctype in dxgi_format)
134            component_cases += format_case(dxgi_format, gltype)
135        else:
136            component_cases += undefined_case(dxgi_format)
137
138        if a_format == "":
139            a_format = dxgi_format
140
141        if a_format in all_angle:
142            a_format = "Format::Get(FormatID::" + a_format + ")"
143            format_cases += format_case(dxgi_format, a_format)
144        else:
145            format_cases += undefined_case(dxgi_format)
146
147    with open('dxgi_format_map_autogen.cpp', 'wt') as out_file:
148        output_cpp = template_cpp.format(
149            script_name=sys.argv[0],
150            data_source_name=input_data,
151            component_type_cases=component_cases,
152            format_cases=format_cases)
153        out_file.write(output_cpp)
154        out_file.close()
155    return 0
156
157
158if __name__ == '__main__':
159    sys.exit(main())
160