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 'y210', 94 'y212', 95 'y216', 96 'y410', 97 'y412', 98 'y416', 99 'xyuv', 100 'ayuv', 101 'r8g8_r8b8_unorm', 102 'g8r8_b8r8_unorm', 103 'g8r8_g8b8_unorm', 104 'y8_u8_v8_422_unorm', 105 'y8_u8v8_422_unorm', 106 'y8_u8_v8_444_unorm', 107 'y16_u16_v16_420_unorm', 108 'y16_u16_v16_422_unorm', 109 'y16_u16v16_422_unorm', 110 'y16_u16_v16_444_unorm', 111 'r8_g8b8_420_unorm', 112 'r8_g8_b8_420_unorm', 113 ] 114 if format.short_name() in noaccess_formats: 115 return False 116 if format.layout in ('astc', 'atc'): 117 return False 118 if format.layout == 'etc' and format.short_name() != 'etc1_rgb8': 119 return False 120 return True 121 122def write_format_table_header(file): 123 print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */', file=file) 124 print(file=file) 125 # This will print the copyright message on the top of this file 126 print(CopyRight.strip(), file=file) 127 print(file=file) 128 print('#include "util/format/u_format.h"', file=file) 129 130def write_format_table(formats): 131 write_format_table_header(sys.stdout) 132 print('#include "u_format_bptc.h"') 133 print('#include "u_format_fxt1.h"') 134 print('#include "u_format_s3tc.h"') 135 print('#include "u_format_rgtc.h"') 136 print('#include "u_format_latc.h"') 137 print('#include "u_format_etc.h"') 138 print() 139 140 write_format_table_header(sys.stdout2) 141 142 u_format_pack.generate(formats) 143 144 def do_channel_array(channels, swizzles): 145 print(" {") 146 for i in range(4): 147 channel = channels[i] 148 if i < 3: 149 sep = "," 150 else: 151 sep = "" 152 if channel.size: 153 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)) 154 else: 155 print(" {0, 0, 0, 0, 0}%s" % (sep,)) 156 print(" },") 157 158 def do_swizzle_array(channels, swizzles): 159 print(" {") 160 for i in range(4): 161 swizzle = swizzles[i] 162 if i < 3: 163 sep = "," 164 else: 165 sep = "" 166 try: 167 comment = colorspace_channels_map[format.colorspace][i] 168 except (KeyError, IndexError): 169 comment = 'ignored' 170 print(" %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment)) 171 print(" },") 172 173 def generate_table_getter(type): 174 suffix = "" 175 if type == "unpack_": 176 suffix = "_generic" 177 print("const struct util_format_%sdescription *" % type) 178 print("util_format_%sdescription%s(enum pipe_format format)" % (type, suffix)) 179 print("{") 180 print(" if (format >= ARRAY_SIZE(util_format_%sdescriptions))" % (type)) 181 print(" return NULL;") 182 print() 183 print(" return &util_format_%sdescriptions[format];" % (type)) 184 print("}") 185 print() 186 187 def generate_function_getter(func): 188 print("util_format_%s_func_ptr" % func) 189 print("util_format_%s_func(enum pipe_format format)" % (func)) 190 print("{") 191 print(" if (format >= ARRAY_SIZE(util_format_%s_table))" % (func)) 192 print(" return NULL;") 193 print() 194 print(" return util_format_%s_table[format];" % (func)) 195 print("}") 196 print() 197 198 print('static const struct util_format_description') 199 print('util_format_descriptions[] = {') 200 for format in formats: 201 sn = format.short_name() 202 203 print(" [%s] = {" % (format.name,)) 204 print(" %s," % (format.name,)) 205 print(" \"%s\"," % (format.name,)) 206 print(" \"%s\"," % (sn,)) 207 print(" {%u, %u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_depth, format.block_size())) 208 print(" %s," % (layout_map(format.layout),)) 209 print(" %u,\t/* nr_channels */" % (format.nr_channels(),)) 210 print(" %s,\t/* is_array */" % (bool_map(format.is_array()),)) 211 print(" %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),)) 212 print(" %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),)) 213 print(" %s,\t/* is_unorm */" % (bool_map(format.is_unorm()),)) 214 print(" %s,\t/* is_snorm */" % (bool_map(format.is_snorm()),)) 215 u_format_pack.print_channels(format, do_channel_array) 216 u_format_pack.print_channels(format, do_swizzle_array) 217 print(" %s," % (colorspace_map(format.colorspace),)) 218 print(" },") 219 print() 220 print("};") 221 print() 222 generate_table_getter("") 223 224 print('static const struct util_format_pack_description') 225 print('util_format_pack_descriptions[] = {') 226 for format in formats: 227 sn = format.short_name() 228 229 if not has_access(format): 230 print(" [%s] = { 0 }," % (format.name,)) 231 continue 232 233 print(" [%s] = {" % (format.name,)) 234 if format.colorspace != ZS and not format.is_pure_color(): 235 print(" .pack_rgba_8unorm = &util_format_%s_pack_rgba_8unorm," % sn) 236 print(" .pack_rgba_float = &util_format_%s_pack_rgba_float," % sn) 237 238 if format.has_depth(): 239 print(" .pack_z_32unorm = &util_format_%s_pack_z_32unorm," % sn) 240 print(" .pack_z_float = &util_format_%s_pack_z_float," % sn) 241 242 if format.has_stencil(): 243 print(" .pack_s_8uint = &util_format_%s_pack_s_8uint," % sn) 244 245 if format.is_pure_unsigned() or format.is_pure_signed(): 246 print(" .pack_rgba_uint = &util_format_%s_pack_unsigned," % sn) 247 print(" .pack_rgba_sint = &util_format_%s_pack_signed," % sn) 248 print(" },") 249 print() 250 print("};") 251 print() 252 generate_table_getter("pack_") 253 print('static const struct util_format_unpack_description') 254 print('util_format_unpack_descriptions[] = {') 255 for format in formats: 256 sn = format.short_name() 257 258 if not has_access(format): 259 print(" [%s] = { 0 }," % (format.name,)) 260 continue 261 262 print(" [%s] = {" % (format.name,)) 263 264 if format.colorspace != ZS and not format.is_pure_color(): 265 if format.layout == 's3tc' or format.layout == 'rgtc': 266 print(" .fetch_rgba_8unorm = &util_format_%s_fetch_rgba_8unorm," % sn) 267 if format.block_width > 1: 268 print( 269 " .unpack_rgba_8unorm_rect = &util_format_%s_unpack_rgba_8unorm," % sn) 270 print( 271 " .unpack_rgba_rect = &util_format_%s_unpack_rgba_float," % sn) 272 else: 273 print( 274 " .unpack_rgba_8unorm = &util_format_%s_unpack_rgba_8unorm," % sn) 275 print(" .unpack_rgba = &util_format_%s_unpack_rgba_float," % sn) 276 277 if format.has_depth(): 278 print(" .unpack_z_32unorm = &util_format_%s_unpack_z_32unorm," % sn) 279 print(" .unpack_z_float = &util_format_%s_unpack_z_float," % sn) 280 281 if format.has_stencil(): 282 print(" .unpack_s_8uint = &util_format_%s_unpack_s_8uint," % sn) 283 284 if format.is_pure_unsigned(): 285 print(" .unpack_rgba = &util_format_%s_unpack_unsigned," % sn) 286 elif format.is_pure_signed(): 287 print(" .unpack_rgba = &util_format_%s_unpack_signed," % sn) 288 print(" },") 289 print("};") 290 print() 291 292 generate_table_getter("unpack_") 293 294 print('static const util_format_fetch_rgba_func_ptr util_format_fetch_rgba_table[] = {') 295 for format in formats: 296 sn = format.short_name() 297 298 if format.colorspace != ZS and has_access(format): 299 print(" [%s] = &util_format_%s_fetch_rgba," % (format.name, sn)) 300 else: 301 print(" [%s] = NULL," % format.name) 302 303 print("};") 304 print() 305 306 generate_function_getter("fetch_rgba") 307 308def main(): 309 formats = [] 310 311 sys.stdout2 = open(os.devnull, "w") 312 313 for arg in sys.argv[1:]: 314 if arg == '--header': 315 sys.stdout2 = sys.stdout 316 sys.stdout = open(os.devnull, "w") 317 continue 318 319 formats.extend(parse(arg)) 320 321 write_format_table(formats) 322 323if __name__ == '__main__': 324 main() 325