summaryrefslogtreecommitdiffstats
path: root/gfx/angle/src/libANGLE/gen_format_map.py
blob: b67f42784bd581a8d4934b18a9f869b603874325 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python
# Copyright 2016 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# gen_format_map.py:
#  Code generation for GL format map. The format map matches between
#  {format,type} and internal format.

from datetime import date
import sys

sys.path.append('renderer')
import angle_format

template_cpp = """// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {data_source_name}.
// ES3 format info from {es3_data_source_name}.
//
// Copyright {copyright_year} The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// format_map:
//   Determining the sized internal format from a (format,type) pair.
//   Also check es3 format combinations for validity.

#include "angle_gl.h"
#include "common/debug.h"

namespace gl
{{

GLenum GetSizedFormatInternal(GLenum format, GLenum type)
{{
    switch (format)
    {{
{format_cases}        case GL_NONE:
            return GL_NONE;

        default:
            break;
    }}

    return GL_NONE;
}}

bool ValidES3Format(GLenum format)
{{
    switch (format)
    {{
{es3_format_cases}            return true;

        default:
            return false;
    }}
}}

bool ValidES3Type(GLenum type)
{{
    switch (type)
    {{
{es3_type_cases}            return true;

        default:
            return false;
    }}
}}

bool ValidES3FormatCombination(GLenum format, GLenum type, GLenum internalFormat)
{{
    ASSERT(ValidES3Format(format) && ValidES3Type(type));

    switch (format)
    {{
{es3_combo_cases}        default:
            UNREACHABLE();
            break;
    }}

    return false;
}}

}}  // namespace gl
"""

template_format_case = """        case {format}:
            switch (type)
            {{
{type_cases}                default:
                    break;
            }}
            break;

"""

template_simple_case = """                case {key}:
                    return {result};
"""

template_es3_combo_type_case = """                case {type}:
                {{
                    switch (internalFormat)
                    {{
{internal_format_cases}                            return true;
                        default:
                            break;
                    }}
                    break;
                }}
"""

def parse_type_case(type, result):
    return template_simple_case.format(
        key = type, result = result)

def parse_format_case(format, type_map):
    type_cases = ""
    for type, internal_format in sorted(type_map.iteritems()):
        type_cases += parse_type_case(type, internal_format)
    return template_format_case.format(
        format = format, type_cases = type_cases)

input_script = 'format_map_data.json'

format_map = angle_format.load_json(input_script)

format_cases = ""

for format, type_map in sorted(format_map.iteritems()):
    format_cases += parse_format_case(format, type_map)

combo_data_file = 'es3_format_type_combinations.json'
es3_combo_data = angle_format.load_json(combo_data_file)
combo_data = [combo for sublist in es3_combo_data.values() for combo in sublist]

types = set()
formats = set()
combos = {}

for internal_format, format, type in combo_data:
    types.update([type])
    formats.update([format])
    if format not in combos:
        combos[format] = {}
    if type not in combos[format]:
        combos[format][type] = [internal_format]
    else:
        combos[format][type] += [internal_format]

es3_format_cases = ""

for format in sorted(formats):
    es3_format_cases += "        case " + format + ":\n"

es3_type_cases = ""

for type in sorted(types):
    es3_type_cases += "        case " + type + ":\n"

es3_combo_cases = ""

for format, type_combos in combos.iteritems():
    this_type_cases = ""
    for type, combos in type_combos.iteritems():
        internal_format_cases = ""
        for internal_format in combos:
            internal_format_cases += "                        case " + internal_format + ":\n"

        this_type_cases += template_es3_combo_type_case.format(
            type = type, internal_format_cases = internal_format_cases)

    es3_combo_cases += template_format_case.format(
        format = format, type_cases = this_type_cases)

with open('format_map_autogen.cpp', 'wt') as out_file:
    output_cpp = template_cpp.format(
        script_name = sys.argv[0],
        data_source_name = input_script,
        es3_data_source_name = combo_data_file,
        copyright_year = date.today().year,
        format_cases = format_cases,
        es3_format_cases = es3_format_cases,
        es3_type_cases = es3_type_cases,
        es3_combo_cases = es3_combo_cases)
    out_file.write(output_cpp)
    out_file.close()