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