Path: blob/21.2-virgl/src/util/format/u_format_table.py
7099 views
from __future__ import print_function12CopyRight = '''3/**************************************************************************4*5* Copyright 2010 VMware, Inc.6* All Rights Reserved.7*8* Permission is hereby granted, free of charge, to any person obtaining a9* copy of this software and associated documentation files (the10* "Software"), to deal in the Software without restriction, including11* without limitation the rights to use, copy, modify, merge, publish,12* distribute, sub license, and/or sell copies of the Software, and to13* permit persons to whom the Software is furnished to do so, subject to14* the following conditions:15*16* The above copyright notice and this permission notice (including the17* next paragraph) shall be included in all copies or substantial portions18* of the Software.19*20* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS21* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF22* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.23* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR24* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,25* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE26* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.27*28**************************************************************************/29'''303132import sys, os3334from u_format_parse import *35import u_format_pack363738def layout_map(layout):39return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper()404142def colorspace_map(colorspace):43return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper()444546colorspace_channels_map = {47'rgb': ['r', 'g', 'b', 'a'],48'srgb': ['sr', 'sg', 'sb', 'a'],49'zs': ['z', 's'],50'yuv': ['y', 'u', 'v'],51}525354type_map = {55VOID: "UTIL_FORMAT_TYPE_VOID",56UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED",57SIGNED: "UTIL_FORMAT_TYPE_SIGNED",58FIXED: "UTIL_FORMAT_TYPE_FIXED",59FLOAT: "UTIL_FORMAT_TYPE_FLOAT",60}616263def bool_map(value):64if value:65return "TRUE"66else:67return "FALSE"686970swizzle_map = {71SWIZZLE_X: "PIPE_SWIZZLE_X",72SWIZZLE_Y: "PIPE_SWIZZLE_Y",73SWIZZLE_Z: "PIPE_SWIZZLE_Z",74SWIZZLE_W: "PIPE_SWIZZLE_W",75SWIZZLE_0: "PIPE_SWIZZLE_0",76SWIZZLE_1: "PIPE_SWIZZLE_1",77SWIZZLE_NONE: "PIPE_SWIZZLE_NONE",78}7980def has_access(format):81# We don't generate code for YUV formats, and many of the new ones lack82# pack/unpack functions for softpipe/llvmpipe.83noaccess_formats = [84'r1_unorm',85'yv12',86'yv16',87'iyuv',88'nv12',89'nv16',90'nv21',91'p010',92'p012',93'p016',94'y210',95'y212',96'y216',97'y410',98'y412',99'y416',100'xyuv',101'ayuv',102'r8g8_r8b8_unorm',103'g8r8_b8r8_unorm',104'g8r8_g8b8_unorm',105'y8_u8_v8_422_unorm',106'y8_u8v8_422_unorm',107'y8_u8_v8_444_unorm',108'y16_u16_v16_420_unorm',109'y16_u16_v16_422_unorm',110'y16_u16v16_422_unorm',111'y16_u16_v16_444_unorm',112'r8_g8b8_420_unorm',113]114if format.short_name() in noaccess_formats:115return False116if format.layout in ('astc', 'atc'):117return False118if format.layout == 'etc' and format.short_name() != 'etc1_rgb8':119return False120return True121122def write_format_table_header(file):123print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */', file=file)124print(file=file)125# This will print the copyright message on the top of this file126print(CopyRight.strip(), file=file)127print(file=file)128print('#include "util/format/u_format.h"', file=file)129130def write_format_table(formats):131write_format_table_header(sys.stdout)132print('#include "u_format_bptc.h"')133print('#include "u_format_fxt1.h"')134print('#include "u_format_s3tc.h"')135print('#include "u_format_rgtc.h"')136print('#include "u_format_latc.h"')137print('#include "u_format_etc.h"')138print()139140write_format_table_header(sys.stdout2)141142u_format_pack.generate(formats)143144def do_channel_array(channels, swizzles):145print(" {")146for i in range(4):147channel = channels[i]148if i < 3:149sep = ","150else:151sep = ""152if channel.size:153print(" {%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))154else:155print(" {0, 0, 0, 0, 0}%s" % (sep,))156print(" },")157158def do_swizzle_array(channels, swizzles):159print(" {")160for i in range(4):161swizzle = swizzles[i]162if i < 3:163sep = ","164else:165sep = ""166try:167comment = colorspace_channels_map[format.colorspace][i]168except (KeyError, IndexError):169comment = 'ignored'170print(" %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment))171print(" },")172173def generate_table_getter(type):174suffix = ""175if type == "unpack_":176suffix = "_generic"177print("const struct util_format_%sdescription *" % type)178print("util_format_%sdescription%s(enum pipe_format format)" % (type, suffix))179print("{")180print(" if (format >= ARRAY_SIZE(util_format_%sdescriptions))" % (type))181print(" return NULL;")182print()183print(" return &util_format_%sdescriptions[format];" % (type))184print("}")185print()186187def generate_function_getter(func):188print("util_format_%s_func_ptr" % func)189print("util_format_%s_func(enum pipe_format format)" % (func))190print("{")191print(" if (format >= ARRAY_SIZE(util_format_%s_table))" % (func))192print(" return NULL;")193print()194print(" return util_format_%s_table[format];" % (func))195print("}")196print()197198print('static const struct util_format_description')199print('util_format_descriptions[] = {')200for format in formats:201sn = format.short_name()202203print(" [%s] = {" % (format.name,))204print(" %s," % (format.name,))205print(" \"%s\"," % (format.name,))206print(" \"%s\"," % (sn,))207print(" {%u, %u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_depth, format.block_size()))208print(" %s," % (layout_map(format.layout),))209print(" %u,\t/* nr_channels */" % (format.nr_channels(),))210print(" %s,\t/* is_array */" % (bool_map(format.is_array()),))211print(" %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),))212print(" %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),))213print(" %s,\t/* is_unorm */" % (bool_map(format.is_unorm()),))214print(" %s,\t/* is_snorm */" % (bool_map(format.is_snorm()),))215u_format_pack.print_channels(format, do_channel_array)216u_format_pack.print_channels(format, do_swizzle_array)217print(" %s," % (colorspace_map(format.colorspace),))218print(" },")219print()220print("};")221print()222generate_table_getter("")223224print('static const struct util_format_pack_description')225print('util_format_pack_descriptions[] = {')226for format in formats:227sn = format.short_name()228229if not has_access(format):230print(" [%s] = { 0 }," % (format.name,))231continue232233print(" [%s] = {" % (format.name,))234if format.colorspace != ZS and not format.is_pure_color():235print(" .pack_rgba_8unorm = &util_format_%s_pack_rgba_8unorm," % sn)236print(" .pack_rgba_float = &util_format_%s_pack_rgba_float," % sn)237238if format.has_depth():239print(" .pack_z_32unorm = &util_format_%s_pack_z_32unorm," % sn)240print(" .pack_z_float = &util_format_%s_pack_z_float," % sn)241242if format.has_stencil():243print(" .pack_s_8uint = &util_format_%s_pack_s_8uint," % sn)244245if format.is_pure_unsigned() or format.is_pure_signed():246print(" .pack_rgba_uint = &util_format_%s_pack_unsigned," % sn)247print(" .pack_rgba_sint = &util_format_%s_pack_signed," % sn)248print(" },")249print()250print("};")251print()252generate_table_getter("pack_")253print('static const struct util_format_unpack_description')254print('util_format_unpack_descriptions[] = {')255for format in formats:256sn = format.short_name()257258if not has_access(format):259print(" [%s] = { 0 }," % (format.name,))260continue261262print(" [%s] = {" % (format.name,))263264if format.colorspace != ZS and not format.is_pure_color():265if format.layout == 's3tc' or format.layout == 'rgtc':266print(" .fetch_rgba_8unorm = &util_format_%s_fetch_rgba_8unorm," % sn)267if format.block_width > 1:268print(269" .unpack_rgba_8unorm_rect = &util_format_%s_unpack_rgba_8unorm," % sn)270print(271" .unpack_rgba_rect = &util_format_%s_unpack_rgba_float," % sn)272else:273print(274" .unpack_rgba_8unorm = &util_format_%s_unpack_rgba_8unorm," % sn)275print(" .unpack_rgba = &util_format_%s_unpack_rgba_float," % sn)276277if format.has_depth():278print(" .unpack_z_32unorm = &util_format_%s_unpack_z_32unorm," % sn)279print(" .unpack_z_float = &util_format_%s_unpack_z_float," % sn)280281if format.has_stencil():282print(" .unpack_s_8uint = &util_format_%s_unpack_s_8uint," % sn)283284if format.is_pure_unsigned():285print(" .unpack_rgba = &util_format_%s_unpack_unsigned," % sn)286elif format.is_pure_signed():287print(" .unpack_rgba = &util_format_%s_unpack_signed," % sn)288print(" },")289print("};")290print()291292generate_table_getter("unpack_")293294print('static const util_format_fetch_rgba_func_ptr util_format_fetch_rgba_table[] = {')295for format in formats:296sn = format.short_name()297298if format.colorspace != ZS and has_access(format):299print(" [%s] = &util_format_%s_fetch_rgba," % (format.name, sn))300else:301print(" [%s] = NULL," % format.name)302303print("};")304print()305306generate_function_getter("fetch_rgba")307308def main():309formats = []310311sys.stdout2 = open(os.devnull, "w")312313for arg in sys.argv[1:]:314if arg == '--header':315sys.stdout2 = sys.stdout316sys.stdout = open(os.devnull, "w")317continue318319formats.extend(parse(arg))320321write_format_table(formats)322323if __name__ == '__main__':324main()325326327