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