cd1af6226a4cb30fcbaf63cc4cc300509d9baeff
[mesa.git] / src / amd / vulkan / vk_format_table.py
1
2 CopyRight = '''
3 /**************************************************************************
4 *
5 * Copyright 2010 VMware, Inc.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29 '''
30
31
32 import sys
33
34 from vk_format_parse import *
35
36 def layout_map(layout):
37 return 'VK_FORMAT_LAYOUT_' + str(layout).upper()
38
39
40 def colorspace_map(colorspace):
41 return 'VK_FORMAT_COLORSPACE_' + str(colorspace).upper()
42
43
44 colorspace_channels_map = {
45 'rgb': ['r', 'g', 'b', 'a'],
46 'srgb': ['sr', 'sg', 'sb', 'a'],
47 'zs': ['z', 's'],
48 'yuv': ['y', 'u', 'v'],
49 }
50
51
52 type_map = {
53 VOID: "VK_FORMAT_TYPE_VOID",
54 UNSIGNED: "VK_FORMAT_TYPE_UNSIGNED",
55 SIGNED: "VK_FORMAT_TYPE_SIGNED",
56 FIXED: "VK_FORMAT_TYPE_FIXED",
57 FLOAT: "VK_FORMAT_TYPE_FLOAT",
58 }
59
60
61 def bool_map(value):
62 if value:
63 return "true"
64 else:
65 return "false"
66
67
68 swizzle_map = {
69 SWIZZLE_X: "VK_SWIZZLE_X",
70 SWIZZLE_Y: "VK_SWIZZLE_Y",
71 SWIZZLE_Z: "VK_SWIZZLE_Z",
72 SWIZZLE_W: "VK_SWIZZLE_W",
73 SWIZZLE_0: "VK_SWIZZLE_0",
74 SWIZZLE_1: "VK_SWIZZLE_1",
75 SWIZZLE_NONE: "VK_SWIZZLE_NONE",
76 }
77
78 def print_channels(format, func):
79 if format.nr_channels() <= 1:
80 func(format.le_channels, format.le_swizzles)
81 else:
82 print '#ifdef PIPE_ARCH_BIG_ENDIAN'
83 func(format.be_channels, format.be_swizzles)
84 print '#else'
85 func(format.le_channels, format.le_swizzles)
86 print '#endif'
87
88 def write_format_table(formats):
89 print '/* This file is autogenerated by vk_format_table.py from vk_format_layout.csv. Do not edit directly. */'
90 print
91 # This will print the copyright message on the top of this file
92 print CopyRight.strip()
93 print
94 print '#include "stdbool.h"'
95 print '#include "vk_format.h"'
96 print
97
98 def do_channel_array(channels, swizzles):
99 print " {"
100 for i in range(4):
101 channel = channels[i]
102 if i < 3:
103 sep = ","
104 else:
105 sep = ""
106 if channel.size:
107 print " {%s, %s, %s, %s, %u, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), bool_map(channel.scaled), channel.size, channel.shift, sep, "xyzw"[i], channel.name)
108 else:
109 print " {0, 0, 0, 0, 0}%s" % (sep,)
110 print " },"
111
112 def do_swizzle_array(channels, swizzles):
113 print " {"
114 for i in range(4):
115 swizzle = swizzles[i]
116 if i < 3:
117 sep = ","
118 else:
119 sep = ""
120 try:
121 comment = colorspace_channels_map[format.colorspace][i]
122 except (KeyError, IndexError):
123 comment = 'ignored'
124 print " %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment)
125 print " },"
126
127 for format in formats:
128 print 'static const struct vk_format_description'
129 print 'vk_format_%s_description = {' % (format.short_name(),)
130 print " %s," % (format.name,)
131 print " \"%s\"," % (format.name,)
132 print " \"%s\"," % (format.short_name(),)
133 print " {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size())
134 print " %s," % (layout_map(format.layout),)
135 print " %u,\t/* nr_channels */" % (format.nr_channels(),)
136 print " %s,\t/* is_array */" % (bool_map(format.is_array()),)
137 print " %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),)
138 print " %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),)
139 print_channels(format, do_channel_array)
140 print_channels(format, do_swizzle_array)
141 print " %s," % (colorspace_map(format.colorspace),)
142 print "};"
143 print
144
145 print "const struct vk_format_description *"
146 print "vk_format_description(VkFormat format)"
147 print "{"
148 print " if (format > VK_FORMAT_END_RANGE) {"
149 print " return NULL;"
150 print " }"
151 print
152 print " switch (format) {"
153 for format in formats:
154 print " case %s:" % format.name
155 print " return &vk_format_%s_description;" % (format.short_name(),)
156 print " default:"
157 print " return NULL;"
158 print " }"
159 print "}"
160 print
161
162
163 def main():
164
165 formats = []
166 for arg in sys.argv[1:]:
167 formats.extend(parse(arg))
168 write_format_table(formats)
169
170
171 if __name__ == '__main__':
172 main()