summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/format_fallback.py
blob: 4a2b85cecb3e262ae1ca4c2ea72fc50a9ed22201 (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
COPYRIGHT = """\
/*
 * Copyright 2017 Google
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
"""

# stdlib
import argparse
from sys import stdout
from mako.template import Template

# local
import format_parser

def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument("csv")
    p.add_argument("out")
    return p.parse_args()

def get_unorm_to_srgb_map(formats):
    names = set(fmt.name for fmt in formats)

    for fmt in formats:
        if fmt.colorspace != 'srgb':
            continue;

        replacements = [
            ('SRGB', 'RGB'),
            ('SRGB', 'UNORM'),
            ('SRGB8_ALPHA8', 'RGBA'),
            ('SRGB8_ALPHA8', 'RGBA8'),
            ('SRGB_ALPHA_UNORM', 'RGBA_UNORM'),
        ]
        found_unorm_name = False
        for rep in replacements:
            if fmt.name.find(rep[0]) == -1:
                continue

            unorm_name = fmt.name.replace(rep[0], rep[1])
            if unorm_name in names:
                yield unorm_name, fmt.name
                found_unorm_name = True
                break

        # Every sRGB format MUST have a UNORM equivalent
        assert found_unorm_name

def get_rgbx_to_rgba_map(formats):
    names = set(fmt.name for fmt in formats)

    for fmt in formats:
        if not fmt.has_channel('r') or not fmt.has_channel('x'):
            continue

        # The condition above will still let MESA_FORMAT_R9G9B9E5_FLOAT
        # through.  We need to ensure it actually has an X in the name.
        if not 'X' in fmt.name:
            continue

        rgbx_name = fmt.name
        rgba_name = rgbx_name.replace("X", "A")
        if rgba_name not in names:
            continue;

        yield rgbx_name, rgba_name

TEMPLATE = Template(COPYRIGHT + """
#include "formats.h"
#include "util/macros.h"

/**
 * For an sRGB format, return the corresponding linear color space format.
 * For non-sRGB formats, return the format as-is.
 */
mesa_format
_mesa_get_srgb_format_linear(mesa_format format)
{
   switch (format) {
%for unorm, srgb in unorm_to_srgb_map:
   case ${srgb}:
      return ${unorm};
%endfor
   default:
      return format;
   }
}

/**
 * For a linear format, return the corresponding sRGB color space format.
 * For an sRGB format, return the format as-is.
 * Assert-fails if the format is not sRGB and does not have an sRGB equivalent.
 */
mesa_format
_mesa_get_linear_format_srgb(mesa_format format)
{
   switch (format) {
%for unorm, srgb in unorm_to_srgb_map:
   case ${unorm}:
      return ${srgb};
%endfor
%for unorm, srgb in unorm_to_srgb_map:
   case ${srgb}:
%endfor
      return format;
   default:
      unreachable("Given format does not have an sRGB equivalent");
   }
}

/**
 * If the format has an alpha channel, and there exists a non-alpha
 * variant of the format with an identical bit layout, then return
 * the non-alpha format. Otherwise return the original format.
 *
 * Examples:
 *    Fallback exists:
 *       MESA_FORMAT_R8G8B8X8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
 *       MESA_FORMAT_RGBX_UNORM16 -> MESA_FORMAT_RGBA_UNORM16
 *
 *    No fallback:
 *       MESA_FORMAT_R8G8B8A8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
 *       MESA_FORMAT_Z_FLOAT32 -> MESA_FORMAT_Z_FLOAT32
 */
mesa_format
_mesa_format_fallback_rgbx_to_rgba(mesa_format format)
{
   switch (format) {
%for rgbx, rgba in rgbx_to_rgba_map:
   case ${rgbx}:
      return ${rgba};
%endfor
   default:
      return format;
   }
}
""");

def main():
    pargs = parse_args()

    formats = list(format_parser.parse(pargs.csv))

    template_env = {
        'unorm_to_srgb_map': list(get_unorm_to_srgb_map(formats)),
        'rgbx_to_rgba_map': list(get_rgbx_to_rgba_map(formats)),
    }

    with open(pargs.out, 'w') as f:
        f.write(TEMPLATE.render(**template_env))

if __name__ == "__main__":
    main()