4a2b85cecb3e262ae1ca4c2ea72fc50a9ed22201
3 * Copyright 2017 Google
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 from sys
import stdout
30 from mako
.template
import Template
36 p
= argparse
.ArgumentParser()
41 def get_unorm_to_srgb_map(formats
):
42 names
= set(fmt
.name
for fmt
in formats
)
45 if fmt
.colorspace
!= 'srgb':
51 ('SRGB8_ALPHA8', 'RGBA'),
52 ('SRGB8_ALPHA8', 'RGBA8'),
53 ('SRGB_ALPHA_UNORM', 'RGBA_UNORM'),
55 found_unorm_name
= False
56 for rep
in replacements
:
57 if fmt
.name
.find(rep
[0]) == -1:
60 unorm_name
= fmt
.name
.replace(rep
[0], rep
[1])
61 if unorm_name
in names
:
62 yield unorm_name
, fmt
.name
63 found_unorm_name
= True
66 # Every sRGB format MUST have a UNORM equivalent
67 assert found_unorm_name
69 def get_rgbx_to_rgba_map(formats
):
70 names
= set(fmt
.name
for fmt
in formats
)
73 if not fmt
.has_channel('r') or not fmt
.has_channel('x'):
76 # The condition above will still let MESA_FORMAT_R9G9B9E5_FLOAT
77 # through. We need to ensure it actually has an X in the name.
78 if not 'X' in fmt
.name
:
82 rgba_name
= rgbx_name
.replace("X", "A")
83 if rgba_name
not in names
:
86 yield rgbx_name
, rgba_name
88 TEMPLATE
= Template(COPYRIGHT
+ """
90 #include "util/macros.h"
93 * For an sRGB format, return the corresponding linear color space format.
94 * For non-sRGB formats, return the format as-is.
97 _mesa_get_srgb_format_linear(mesa_format format)
100 %for unorm, srgb in unorm_to_srgb_map:
110 * For a linear format, return the corresponding sRGB color space format.
111 * For an sRGB format, return the format as-is.
112 * Assert-fails if the format is not sRGB and does not have an sRGB equivalent.
115 _mesa_get_linear_format_srgb(mesa_format format)
118 %for unorm, srgb in unorm_to_srgb_map:
122 %for unorm, srgb in unorm_to_srgb_map:
127 unreachable("Given format does not have an sRGB equivalent");
132 * If the format has an alpha channel, and there exists a non-alpha
133 * variant of the format with an identical bit layout, then return
134 * the non-alpha format. Otherwise return the original format.
138 * MESA_FORMAT_R8G8B8X8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
139 * MESA_FORMAT_RGBX_UNORM16 -> MESA_FORMAT_RGBA_UNORM16
142 * MESA_FORMAT_R8G8B8A8_UNORM -> MESA_FORMAT_R8G8B8A8_UNORM
143 * MESA_FORMAT_Z_FLOAT32 -> MESA_FORMAT_Z_FLOAT32
146 _mesa_format_fallback_rgbx_to_rgba(mesa_format format)
149 %for rgbx, rgba in rgbx_to_rgba_map:
162 formats
= list(format_parser
.parse(pargs
.csv
))
165 'unorm_to_srgb_map': list(get_unorm_to_srgb_map(formats
)),
166 'rgbx_to_rgba_map': list(get_rgbx_to_rgba_map(formats
)),
169 with
open(pargs
.out
, 'w') as f
:
170 f
.write(TEMPLATE
.render(**template_env
))
172 if __name__
== "__main__":