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