util: Add callback to fetch a single pixel.
[mesa.git] / src / gallium / auxiliary / util / u_format_table.py
1 #!/usr/bin/env python
2
3 '''
4 /**************************************************************************
5 *
6 * Copyright 2009 VMware, Inc.
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sub license, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
16 *
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
19 * of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
25 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 *
29 **************************************************************************/
30 '''
31
32
33 import sys
34
35 from u_format_parse import *
36 import u_format_pack
37
38
39 def layout_map(layout):
40 return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper()
41
42
43 def colorspace_map(colorspace):
44 return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper()
45
46
47 colorspace_channels_map = {
48 'rgb': ['r', 'g', 'b', 'a'],
49 'srgb': ['sr', 'sg', 'sb', 'a'],
50 'zs': ['z', 's'],
51 'yuv': ['y', 'u', 'v'],
52 }
53
54
55 type_map = {
56 VOID: "UTIL_FORMAT_TYPE_VOID",
57 UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED",
58 SIGNED: "UTIL_FORMAT_TYPE_SIGNED",
59 FIXED: "UTIL_FORMAT_TYPE_FIXED",
60 FLOAT: "UTIL_FORMAT_TYPE_FLOAT",
61 }
62
63
64 def bool_map(value):
65 if value:
66 return "TRUE"
67 else:
68 return "FALSE"
69
70
71 swizzle_map = {
72 SWIZZLE_X: "UTIL_FORMAT_SWIZZLE_X",
73 SWIZZLE_Y: "UTIL_FORMAT_SWIZZLE_Y",
74 SWIZZLE_Z: "UTIL_FORMAT_SWIZZLE_Z",
75 SWIZZLE_W: "UTIL_FORMAT_SWIZZLE_W",
76 SWIZZLE_0: "UTIL_FORMAT_SWIZZLE_0",
77 SWIZZLE_1: "UTIL_FORMAT_SWIZZLE_1",
78 SWIZZLE_NONE: "UTIL_FORMAT_SWIZZLE_NONE",
79 }
80
81
82 def write_format_table(formats):
83 print '/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */'
84 print
85 # This will print the copyright message on the top of this file
86 print __doc__.strip()
87 print
88 print '#include "u_format.h"'
89 print
90 print '''
91 static void
92 util_format_none_unpack_8unorm(uint8_t *dst, const uint8_t *src, unsigned length)
93 {
94 }
95
96 static void
97 util_format_none_pack_8unorm(uint8_t *dst, const uint8_t *src, unsigned length)
98 {
99 }
100
101 static void
102 util_format_none_unpack_float(float *dst, const uint8_t *src, unsigned length)
103 {
104 }
105
106 static void
107 util_format_none_pack_float(uint8_t *dst, const float *src, unsigned length)
108 {
109 }
110
111 static void
112 util_format_none_fetch_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
113 {
114 }
115 '''
116 print 'const struct util_format_description'
117 print 'util_format_none_description = {'
118 print " PIPE_FORMAT_NONE,"
119 print " \"PIPE_FORMAT_NONE\","
120 print " \"none\","
121 print " {0, 0, 0},"
122 print " 0,"
123 print " 0,"
124 print " 0,"
125 print " 0,"
126 print " 0,"
127 print " {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},"
128 print " {0, 0, 0, 0},"
129 print " 0,"
130 print " &util_format_none_unpack_8unorm,"
131 print " &util_format_none_pack_8unorm,"
132 print " &util_format_none_unpack_float,"
133 print " &util_format_none_pack_float,"
134 print " &util_format_none_fetch_float"
135 print "};"
136 print
137
138 u_format_pack.generate(formats)
139
140 for format in formats:
141 print 'const struct util_format_description'
142 print 'util_format_%s_description = {' % (format.short_name(),)
143 print " %s," % (format.name,)
144 print " \"%s\"," % (format.name,)
145 print " \"%s\"," % (format.short_name(),)
146 print " {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size())
147 print " %s," % (layout_map(format.layout),)
148 print " %u,\t/* nr_channels */" % (format.nr_channels(),)
149 print " %s,\t/* is_array */" % (bool_map(format.is_array()),)
150 print " %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),)
151 print " %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),)
152 print " {"
153 for i in range(4):
154 channel = format.channels[i]
155 if i < 3:
156 sep = ","
157 else:
158 sep = ""
159 if channel.size:
160 print " {%s, %s, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), channel.size, sep, "xyzw"[i], channel.name)
161 else:
162 print " {0, 0, 0}%s" % (sep,)
163 print " },"
164 print " {"
165 for i in range(4):
166 swizzle = format.swizzles[i]
167 if i < 3:
168 sep = ","
169 else:
170 sep = ""
171 try:
172 comment = colorspace_channels_map[format.colorspace][i]
173 except (KeyError, IndexError):
174 comment = 'ignored'
175 print " %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment)
176 print " },"
177 print " %s," % (colorspace_map(format.colorspace),)
178 print " &util_format_%s_unpack_8unorm," % format.short_name()
179 print " &util_format_%s_pack_8unorm," % format.short_name()
180 print " &util_format_%s_unpack_float," % format.short_name()
181 print " &util_format_%s_pack_float," % format.short_name()
182 print " &util_format_%s_fetch_float" % format.short_name()
183 print "};"
184 print
185
186 print "const struct util_format_description *"
187 print "util_format_description(enum pipe_format format)"
188 print "{"
189 print " if (format >= PIPE_FORMAT_COUNT) {"
190 print " return NULL;"
191 print " }"
192 print
193 print " switch (format) {"
194 print " case PIPE_FORMAT_NONE:"
195 print " return &util_format_none_description;"
196 for format in formats:
197 print " case %s:" % format.name
198 print " return &util_format_%s_description;" % (format.short_name(),)
199 print " default:"
200 print " assert(0);"
201 print " return NULL;"
202 print " }"
203 print "}"
204 print
205
206
207 def main():
208
209 formats = []
210 for arg in sys.argv[1:]:
211 formats.extend(parse(arg))
212 write_format_table(formats)
213
214
215 if __name__ == '__main__':
216 main()