1CopyRight = ''' 2/************************************************************************** 3 * 4 * Copyright 2010 VMware, Inc. 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28''' 29 30 31import sys, os 32 33from u_format_parse import * 34import u_format_pack 35 36 37def layout_map(layout): 38 return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper() 39 40 41def colorspace_map(colorspace): 42 return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper() 43 44 45colorspace_channels_map = { 46 'rgb': ['r', 'g', 'b', 'a'], 47 'srgb': ['sr', 'sg', 'sb', 'a'], 48 'zs': ['z', 's'], 49 'yuv': ['y', 'u', 'v'], 50} 51 52 53type_map = { 54 VOID: "UTIL_FORMAT_TYPE_VOID", 55 UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED", 56 SIGNED: "UTIL_FORMAT_TYPE_SIGNED", 57 FIXED: "UTIL_FORMAT_TYPE_FIXED", 58 FLOAT: "UTIL_FORMAT_TYPE_FLOAT", 59} 60 61 62def bool_map(value): 63 if value: 64 return "true" 65 else: 66 return "false" 67 68 69swizzle_map = { 70 SWIZZLE_X: "PIPE_SWIZZLE_X", 71 SWIZZLE_Y: "PIPE_SWIZZLE_Y", 72 SWIZZLE_Z: "PIPE_SWIZZLE_Z", 73 SWIZZLE_W: "PIPE_SWIZZLE_W", 74 SWIZZLE_0: "PIPE_SWIZZLE_0", 75 SWIZZLE_1: "PIPE_SWIZZLE_1", 76 SWIZZLE_NONE: "PIPE_SWIZZLE_NONE", 77} 78 79def has_access(format): 80 # We don't generate code for YUV formats, and many of the new ones lack 81 # pack/unpack functions for softpipe/llvmpipe. 82 noaccess_formats = [ 83 'r1_unorm', 84 'yv12', 85 'yv16', 86 'iyuv', 87 'nv12', 88 'nv16', 89 'nv21', 90 'p010', 91 'p012', 92 'p016', 93 'p030', 94 'y210', 95 'y212', 96 'y216', 97 'y410', 98 'y412', 99 'y416', 100 'xyuv', 101 'ayuv', 102 'r8g8_r8b8_unorm', 103 'r8b8_r8g8_unorm', 104 'g8r8_b8r8_unorm', 105 'b8r8_g8r8_unorm', 106 'g8r8_g8b8_unorm', 107 'g8b8_g8r8_unorm', 108 'b8g8_r8g8_unorm', 109 'y8_400_unorm', 110 'y8_u8_v8_422_unorm', 111 'y8_u8v8_422_unorm', 112 'y8_u8_v8_444_unorm', 113 'y16_u16_v16_420_unorm', 114 'y16_u16_v16_422_unorm', 115 'y16_u16v16_422_unorm', 116 'y16_u16_v16_444_unorm', 117 'r8_g8b8_420_unorm', 118 'r8_b8g8_420_unorm', 119 'g8_b8r8_420_unorm', 120 'r8_g8_b8_420_unorm', 121 'r8_b8_g8_420_unorm', 122 'g8_b8_r8_420_unorm', 123 'r8_g8_b8_unorm', 124 'y8_unorm', 125 ] 126 if format.short_name() in noaccess_formats: 127 return False 128 if format.layout in ('astc', 'atc'): 129 return False 130 if format.layout == 'etc' and format.short_name() != 'etc1_rgb8': 131 return False 132 return True 133 134def write_format_table_header(file): 135 print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */', file=file) 136 print(file=file) 137 # This will print the copyright message on the top of this file 138 print(CopyRight.strip(), file=file) 139 print(file=file) 140 print('#include "util/format/u_format.h"', file=file) 141 142def write_format_table(formats): 143 write_format_table_header(sys.stdout) 144 print('#include "u_format_bptc.h"') 145 print('#include "u_format_fxt1.h"') 146 print('#include "u_format_s3tc.h"') 147 print('#include "u_format_rgtc.h"') 148 print('#include "u_format_latc.h"') 149 print('#include "u_format_etc.h"') 150 print() 151 152 write_format_table_header(sys.stdout2) 153 154 print('#ifdef __cplusplus', file=sys.stdout2) 155 print('extern "C" {', file=sys.stdout2) 156 print('#endif', file=sys.stdout2) 157 print(file=sys.stdout2) 158 159 u_format_pack.generate(formats) 160 161 print('#ifdef __cplusplus', file=sys.stdout2) 162 print('} /* extern "C" */', file=sys.stdout2) 163 print('#endif', file=sys.stdout2) 164 165 def do_channel_array(channels, swizzles): 166 print(" {") 167 for i in range(4): 168 channel = channels[i] 169 if i < 3: 170 sep = "," 171 else: 172 sep = "" 173 if channel.size: 174 print(" {%s, %s, %s, %u, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), channel.size, channel.shift, sep, "xyzw"[i], channel.name)) 175 else: 176 print(" {0, 0, 0, 0, 0}%s" % (sep,)) 177 print(" },") 178 179 def do_swizzle_array(channels, swizzles): 180 print(" {") 181 for i in range(4): 182 swizzle = swizzles[i] 183 if i < 3: 184 sep = "," 185 else: 186 sep = "" 187 try: 188 comment = colorspace_channels_map[format.colorspace][i] 189 except (KeyError, IndexError): 190 comment = 'ignored' 191 print(" %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment)) 192 print(" },") 193 194 def generate_table_getter(type): 195 suffix = "" 196 if type == "unpack_": 197 suffix = "_generic" 198 print("ATTRIBUTE_RETURNS_NONNULL const struct util_format_%sdescription *" % type) 199 print("util_format_%sdescription%s(enum pipe_format format)" % (type, suffix)) 200 print("{") 201 print(" assert(format < PIPE_FORMAT_COUNT);") 202 print(" return &util_format_%sdescriptions[format];" % (type)) 203 print("}") 204 print() 205 206 def generate_function_getter(func): 207 print("util_format_%s_func_ptr" % func) 208 print("util_format_%s_func(enum pipe_format format)" % (func)) 209 print("{") 210 print(" assert(format < PIPE_FORMAT_COUNT);") 211 print(" return util_format_%s_table[format];" % (func)) 212 print("}") 213 print() 214 215 print('static const struct util_format_description') 216 print('util_format_descriptions[PIPE_FORMAT_COUNT] = {') 217 for format in formats: 218 sn = format.short_name() 219 220 print(" [%s] = {" % (format.name,)) 221 print(" %s," % (format.name,)) 222 print(" \"%s\"," % (format.name,)) 223 print(" \"%s\"," % (sn,)) 224 print(" {%u, %u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_depth, format.block_size())) 225 print(" %s," % (layout_map(format.layout),)) 226 print(" %u,\t/* nr_channels */" % (format.nr_channels(),)) 227 print(" %s,\t/* is_array */" % (bool_map(format.is_array()),)) 228 print(" %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),)) 229 print(" %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),)) 230 print(" %s,\t/* is_unorm */" % (bool_map(format.is_unorm()),)) 231 print(" %s,\t/* is_snorm */" % (bool_map(format.is_snorm()),)) 232 u_format_pack.print_channels(format, do_channel_array) 233 u_format_pack.print_channels(format, do_swizzle_array) 234 print(" %s," % (colorspace_map(format.colorspace),)) 235 print(" },") 236 print() 237 print("};") 238 print() 239 generate_table_getter("") 240 241 print('static const struct util_format_pack_description') 242 print('util_format_pack_descriptions[PIPE_FORMAT_COUNT] = {') 243 for format in formats: 244 sn = format.short_name() 245 246 if not has_access(format): 247 print(" [%s] = { 0 }," % (format.name,)) 248 continue 249 250 print(" [%s] = {" % (format.name,)) 251 if format.colorspace != ZS and not format.is_pure_color(): 252 print(" .pack_rgba_8unorm = &util_format_%s_pack_rgba_8unorm," % sn) 253 print(" .pack_rgba_float = &util_format_%s_pack_rgba_float," % sn) 254 255 if format.has_depth(): 256 print(" .pack_z_32unorm = &util_format_%s_pack_z_32unorm," % sn) 257 print(" .pack_z_float = &util_format_%s_pack_z_float," % sn) 258 259 if format.has_stencil(): 260 print(" .pack_s_8uint = &util_format_%s_pack_s_8uint," % sn) 261 262 if format.is_pure_unsigned() or format.is_pure_signed(): 263 print(" .pack_rgba_uint = &util_format_%s_pack_unsigned," % sn) 264 print(" .pack_rgba_sint = &util_format_%s_pack_signed," % sn) 265 print(" },") 266 print() 267 print("};") 268 print() 269 generate_table_getter("pack_") 270 print('static const struct util_format_unpack_description') 271 print('util_format_unpack_descriptions[PIPE_FORMAT_COUNT] = {') 272 for format in formats: 273 sn = format.short_name() 274 275 if not has_access(format): 276 print(" [%s] = { 0 }," % (format.name,)) 277 continue 278 279 print(" [%s] = {" % (format.name,)) 280 281 if format.colorspace != ZS and not format.is_pure_color(): 282 if format.layout == 's3tc' or format.layout == 'rgtc': 283 print(" .fetch_rgba_8unorm = &util_format_%s_fetch_rgba_8unorm," % sn) 284 if format.block_width > 1: 285 print( 286 " .unpack_rgba_8unorm_rect = &util_format_%s_unpack_rgba_8unorm," % sn) 287 print( 288 " .unpack_rgba_rect = &util_format_%s_unpack_rgba_float," % sn) 289 else: 290 print( 291 " .unpack_rgba_8unorm = &util_format_%s_unpack_rgba_8unorm," % sn) 292 print(" .unpack_rgba = &util_format_%s_unpack_rgba_float," % sn) 293 294 if format.has_depth(): 295 print(" .unpack_z_32unorm = &util_format_%s_unpack_z_32unorm," % sn) 296 print(" .unpack_z_float = &util_format_%s_unpack_z_float," % sn) 297 298 if format.has_stencil(): 299 print(" .unpack_s_8uint = &util_format_%s_unpack_s_8uint," % sn) 300 301 if format.is_pure_unsigned(): 302 print(" .unpack_rgba = &util_format_%s_unpack_unsigned," % sn) 303 elif format.is_pure_signed(): 304 print(" .unpack_rgba = &util_format_%s_unpack_signed," % sn) 305 print(" },") 306 print("};") 307 print() 308 309 generate_table_getter("unpack_") 310 311 print('static const util_format_fetch_rgba_func_ptr util_format_fetch_rgba_table[PIPE_FORMAT_COUNT] = {') 312 for format in formats: 313 sn = format.short_name() 314 315 if format.colorspace != ZS and has_access(format): 316 print(" [%s] = &util_format_%s_fetch_rgba," % (format.name, sn)) 317 else: 318 print(" [%s] = NULL," % format.name) 319 320 print("};") 321 print() 322 323 generate_function_getter("fetch_rgba") 324 325def main(): 326 formats = [] 327 328 sys.stdout2 = open(os.devnull, "w") 329 330 for arg in sys.argv[1:]: 331 if arg == '--header': 332 sys.stdout2 = sys.stdout 333 sys.stdout = open(os.devnull, "w") 334 continue 335 336 formats.extend(parse(arg)) 337 338 write_format_table(formats) 339 340if __name__ == '__main__': 341 main() 342