Path: blob/21.2-virgl/src/util/format/u_format_pack.py
7111 views
1'''2/**************************************************************************3*4* Copyright 2009-2010 VMware, Inc.5* All Rights Reserved.6*7* Permission is hereby granted, free of charge, to any person obtaining a8* copy of this software and associated documentation files (the9* "Software"), to deal in the Software without restriction, including10* without limitation the rights to use, copy, modify, merge, publish,11* distribute, sub license, and/or sell copies of the Software, and to12* permit persons to whom the Software is furnished to do so, subject to13* the following conditions:14*15* The above copyright notice and this permission notice (including the16* next paragraph) shall be included in all copies or substantial portions17* of the Software.18*19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS20* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.22* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR23* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,24* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE25* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.26*27**************************************************************************/2829/**30* @file31* Pixel format packing and unpacking functions.32*33* @author Jose Fonseca <[email protected]>34*/35'''363738from __future__ import division, print_function3940import sys4142from u_format_parse import *434445if sys.version_info < (3, 0):46integer_types = (int, long)4748else:49integer_types = (int, )5051def inv_swizzles(swizzles):52'''Return an array[4] of inverse swizzle terms'''53'''Only pick the first matching value to avoid l8 getting blue and i8 getting alpha'''54inv_swizzle = [None]*455for i in range(4):56swizzle = swizzles[i]57if swizzle < 4 and inv_swizzle[swizzle] == None:58inv_swizzle[swizzle] = i59return inv_swizzle6061def print_channels(format, func):62if format.nr_channels() <= 1:63func(format.le_channels, format.le_swizzles)64else:65if (format.le_channels == format.be_channels and66[c.shift for c in format.le_channels] ==67[c.shift for c in format.be_channels] and68format.le_swizzles == format.be_swizzles):69func(format.le_channels, format.le_swizzles)70else:71print('#if UTIL_ARCH_BIG_ENDIAN')72func(format.be_channels, format.be_swizzles)73print('#else')74func(format.le_channels, format.le_swizzles)75print('#endif')7677def generate_format_type(format):78'''Generate a structure that describes the format.'''7980assert format.layout == PLAIN8182def generate_bitfields(channels, swizzles):83for channel in channels:84if channel.type == VOID:85if channel.size:86print(' unsigned %s:%u;' % (channel.name, channel.size))87elif channel.type == UNSIGNED:88print(' unsigned %s:%u;' % (channel.name, channel.size))89elif channel.type in (SIGNED, FIXED):90print(' int %s:%u;' % (channel.name, channel.size))91elif channel.type == FLOAT:92if channel.size == 64:93print(' double %s;' % (channel.name))94elif channel.size == 32:95print(' float %s;' % (channel.name))96else:97print(' unsigned %s:%u;' % (channel.name, channel.size))98else:99assert 0100101def generate_full_fields(channels, swizzles):102for channel in channels:103assert channel.size % 8 == 0 and is_pot(channel.size)104if channel.type == VOID:105if channel.size:106print(' uint%u_t %s;' % (channel.size, channel.name))107elif channel.type == UNSIGNED:108print(' uint%u_t %s;' % (channel.size, channel.name))109elif channel.type in (SIGNED, FIXED):110print(' int%u_t %s;' % (channel.size, channel.name))111elif channel.type == FLOAT:112if channel.size == 64:113print(' double %s;' % (channel.name))114elif channel.size == 32:115print(' float %s;' % (channel.name))116elif channel.size == 16:117print(' uint16_t %s;' % (channel.name))118else:119assert 0120else:121assert 0122123use_bitfields = False124for channel in format.le_channels:125if channel.size % 8 or not is_pot(channel.size):126use_bitfields = True127128print('struct util_format_%s {' % format.short_name())129if use_bitfields:130print_channels(format, generate_bitfields)131else:132print_channels(format, generate_full_fields)133print('};')134print()135136137def is_format_supported(format):138'''Determines whether we actually have the plumbing necessary to generate the139to read/write to/from this format.'''140141# FIXME: Ideally we would support any format combination here.142143if format.layout != PLAIN:144return False145146for i in range(4):147channel = format.le_channels[i]148if channel.type not in (VOID, UNSIGNED, SIGNED, FLOAT, FIXED):149return False150if channel.type == FLOAT and channel.size not in (16, 32, 64):151return False152153return True154155def native_type(format):156'''Get the native appropriate for a format.'''157158if format.name == 'PIPE_FORMAT_R11G11B10_FLOAT':159return 'uint32_t'160if format.name == 'PIPE_FORMAT_R9G9B9E5_FLOAT':161return 'uint32_t'162163if format.layout == PLAIN:164if not format.is_array():165# For arithmetic pixel formats return the integer type that matches the whole pixel166return 'uint%u_t' % format.block_size()167else:168# For array pixel formats return the integer type that matches the color channel169channel = format.array_element()170if channel.type in (UNSIGNED, VOID):171return 'uint%u_t' % channel.size172elif channel.type in (SIGNED, FIXED):173return 'int%u_t' % channel.size174elif channel.type == FLOAT:175if channel.size == 16:176return 'uint16_t'177elif channel.size == 32:178return 'float'179elif channel.size == 64:180return 'double'181else:182assert False183else:184assert False185else:186assert False187188189def intermediate_native_type(bits, sign):190'''Find a native type adequate to hold intermediate results of the request bit size.'''191192bytes = 4 # don't use anything smaller than 32bits193while bytes * 8 < bits:194bytes *= 2195bits = bytes*8196197if sign:198return 'int%u_t' % bits199else:200return 'uint%u_t' % bits201202203def get_one_shift(type):204'''Get the number of the bit that matches unity for this type.'''205if type.type == 'FLOAT':206assert False207if not type.norm:208return 0209if type.type == UNSIGNED:210return type.size211if type.type == SIGNED:212return type.size - 1213if type.type == FIXED:214return type.size / 2215assert False216217218def truncate_mantissa(x, bits):219'''Truncate an integer so it can be represented exactly with a floating220point mantissa'''221222assert isinstance(x, integer_types)223224s = 1225if x < 0:226s = -1227x = -x228229# We can represent integers up to mantissa + 1 bits exactly230mask = (1 << (bits + 1)) - 1231232# Slide the mask until the MSB matches233shift = 0234while (x >> shift) & ~mask:235shift += 1236237x &= mask << shift238x *= s239return x240241242def value_to_native(type, value):243'''Get the value of unity for this type.'''244if type.type == FLOAT:245if type.size <= 32 \246and isinstance(value, integer_types):247return truncate_mantissa(value, 23)248return value249if type.type == FIXED:250return int(value * (1 << (type.size // 2)))251if not type.norm:252return int(value)253if type.type == UNSIGNED:254return int(value * ((1 << type.size) - 1))255if type.type == SIGNED:256return int(value * ((1 << (type.size - 1)) - 1))257assert False258259260def native_to_constant(type, value):261'''Get the value of unity for this type.'''262if type.type == FLOAT:263if type.size <= 32:264return "%.1ff" % float(value)265else:266return "%.1f" % float(value)267else:268return str(int(value))269270271def get_one(type):272'''Get the value of unity for this type.'''273return value_to_native(type, 1)274275276def clamp_expr(src_channel, dst_channel, dst_native_type, value):277'''Generate the expression to clamp the value in the source type to the278destination type range.'''279280if src_channel == dst_channel:281return value282283src_min = src_channel.min()284src_max = src_channel.max()285dst_min = dst_channel.min()286dst_max = dst_channel.max()287288# Translate the destination range to the src native value289dst_min_native = native_to_constant(src_channel, value_to_native(src_channel, dst_min))290dst_max_native = native_to_constant(src_channel, value_to_native(src_channel, dst_max))291292if src_min < dst_min and src_max > dst_max:293return 'CLAMP(%s, %s, %s)' % (value, dst_min_native, dst_max_native)294295if src_max > dst_max:296return 'MIN2(%s, %s)' % (value, dst_max_native)297298if src_min < dst_min:299return 'MAX2(%s, %s)' % (value, dst_min_native)300301return value302303304def conversion_expr(src_channel,305dst_channel, dst_native_type,306value,307clamp=True,308src_colorspace = RGB,309dst_colorspace = RGB):310'''Generate the expression to convert a value between two types.'''311312if src_colorspace != dst_colorspace:313if src_colorspace == SRGB:314assert src_channel.type == UNSIGNED315assert src_channel.norm316assert src_channel.size <= 8317assert src_channel.size >= 4318assert dst_colorspace == RGB319if src_channel.size < 8:320value = '%s << %x | %s >> %x' % (value, 8 - src_channel.size, value, 2 * src_channel.size - 8)321if dst_channel.type == FLOAT:322return 'util_format_srgb_8unorm_to_linear_float(%s)' % value323else:324assert dst_channel.type == UNSIGNED325assert dst_channel.norm326assert dst_channel.size == 8327return 'util_format_srgb_to_linear_8unorm(%s)' % value328elif dst_colorspace == SRGB:329assert dst_channel.type == UNSIGNED330assert dst_channel.norm331assert dst_channel.size <= 8332assert src_colorspace == RGB333if src_channel.type == FLOAT:334value = 'util_format_linear_float_to_srgb_8unorm(%s)' % value335else:336assert src_channel.type == UNSIGNED337assert src_channel.norm338assert src_channel.size == 8339value = 'util_format_linear_to_srgb_8unorm(%s)' % value340# XXX rounding is all wrong.341if dst_channel.size < 8:342return '%s >> %x' % (value, 8 - dst_channel.size)343else:344return value345elif src_colorspace == ZS:346pass347elif dst_colorspace == ZS:348pass349else:350assert 0351352if src_channel == dst_channel:353return value354355src_type = src_channel.type356src_size = src_channel.size357src_norm = src_channel.norm358src_pure = src_channel.pure359360# Promote half to float361if src_type == FLOAT and src_size == 16:362value = '_mesa_half_to_float(%s)' % value363src_size = 32364365# Special case for float <-> ubytes for more accurate results366# Done before clamping since these functions already take care of that367if src_type == UNSIGNED and src_norm and src_size == 8 and dst_channel.type == FLOAT and dst_channel.size == 32:368return 'ubyte_to_float(%s)' % value369if src_type == FLOAT and src_size == 32 and dst_channel.type == UNSIGNED and dst_channel.norm and dst_channel.size == 8:370return 'float_to_ubyte(%s)' % value371372if clamp:373if dst_channel.type != FLOAT or src_type != FLOAT:374value = clamp_expr(src_channel, dst_channel, dst_native_type, value)375376if src_type in (SIGNED, UNSIGNED) and dst_channel.type in (SIGNED, UNSIGNED):377if not src_norm and not dst_channel.norm:378# neither is normalized -- just cast379return '(%s)%s' % (dst_native_type, value)380381if src_norm and dst_channel.norm:382return "_mesa_%snorm_to_%snorm(%s, %d, %d)" % ("s" if src_type == SIGNED else "u",383"s" if dst_channel.type == SIGNED else "u",384value, src_channel.size, dst_channel.size)385else:386# We need to rescale using an intermediate type big enough to hold the multiplication of both387src_one = get_one(src_channel)388dst_one = get_one(dst_channel)389tmp_native_type = intermediate_native_type(src_size + dst_channel.size, src_channel.sign and dst_channel.sign)390value = '((%s)%s)' % (tmp_native_type, value)391value = '(%s)(%s * 0x%x / 0x%x)' % (dst_native_type, value, dst_one, src_one)392return value393394395# Promote to either float or double396if src_type != FLOAT:397if src_norm or src_type == FIXED:398one = get_one(src_channel)399if src_size <= 23:400value = '(%s * (1.0f/0x%x))' % (value, one)401if dst_channel.size <= 32:402value = '(float)%s' % value403src_size = 32404else:405# bigger than single precision mantissa, use double406value = '(%s * (1.0/0x%x))' % (value, one)407src_size = 64408src_norm = False409else:410if src_size <= 23 or dst_channel.size <= 32:411value = '(float)%s' % value412src_size = 32413else:414# bigger than single precision mantissa, use double415value = '(double)%s' % value416src_size = 64417src_type = FLOAT418419# Convert double or float to non-float420if dst_channel.type != FLOAT:421if dst_channel.norm or dst_channel.type == FIXED:422dst_one = get_one(dst_channel)423if dst_channel.size <= 23:424value = 'util_iround(%s * 0x%x)' % (value, dst_one)425else:426# bigger than single precision mantissa, use double427value = '(%s * (double)0x%x)' % (value, dst_one)428value = '(%s)%s' % (dst_native_type, value)429else:430# Cast double to float when converting to either half or float431if dst_channel.size <= 32 and src_size > 32:432value = '(float)%s' % value433src_size = 32434435if dst_channel.size == 16:436value = '_mesa_float_to_float16_rtz(%s)' % value437elif dst_channel.size == 64 and src_size < 64:438value = '(double)%s' % value439440return value441442443def generate_unpack_kernel(format, dst_channel, dst_native_type):444445if not is_format_supported(format):446return447448assert format.layout == PLAIN449450def unpack_from_bitmask(channels, swizzles):451depth = format.block_size()452print(' uint%u_t value = *(const uint%u_t *)src;' % (depth, depth))453454# Compute the intermediate unshifted values455for i in range(format.nr_channels()):456src_channel = channels[i]457value = 'value'458shift = src_channel.shift459if src_channel.type == UNSIGNED:460if shift:461value = '%s >> %u' % (value, shift)462if shift + src_channel.size < depth:463value = '(%s) & 0x%x' % (value, (1 << src_channel.size) - 1)464print(' uint%u_t %s = %s;' % (depth, src_channel.name, value))465elif src_channel.type == SIGNED:466if shift + src_channel.size < depth:467# Align the sign bit468lshift = depth - (shift + src_channel.size)469value = '%s << %u' % (value, lshift)470# Cast to signed471value = '(int%u_t)(%s) ' % (depth, value)472if src_channel.size < depth:473# Align the LSB bit474rshift = depth - src_channel.size475value = '(%s) >> %u' % (value, rshift)476print(' int%u_t %s = %s;' % (depth, src_channel.name, value))477else:478value = None479480# Convert, swizzle, and store final values481for i in range(4):482swizzle = swizzles[i]483if swizzle < 4:484src_channel = channels[swizzle]485src_colorspace = format.colorspace486if src_colorspace == SRGB and i == 3:487# Alpha channel is linear488src_colorspace = RGB489value = src_channel.name490value = conversion_expr(src_channel,491dst_channel, dst_native_type,492value,493src_colorspace = src_colorspace)494elif swizzle == SWIZZLE_0:495value = '0'496elif swizzle == SWIZZLE_1:497value = get_one(dst_channel)498elif swizzle == SWIZZLE_NONE:499value = '0'500else:501assert False502print(' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]))503504def unpack_from_struct(channels, swizzles):505print(' struct util_format_%s pixel;' % format.short_name())506print(' memcpy(&pixel, src, sizeof pixel);')507508for i in range(4):509swizzle = swizzles[i]510if swizzle < 4:511src_channel = channels[swizzle]512src_colorspace = format.colorspace513if src_colorspace == SRGB and i == 3:514# Alpha channel is linear515src_colorspace = RGB516value = 'pixel.%s' % src_channel.name517value = conversion_expr(src_channel,518dst_channel, dst_native_type,519value,520src_colorspace = src_colorspace)521elif swizzle == SWIZZLE_0:522value = '0'523elif swizzle == SWIZZLE_1:524value = get_one(dst_channel)525elif swizzle == SWIZZLE_NONE:526value = '0'527else:528assert False529print(' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]))530531if format.is_bitmask():532print_channels(format, unpack_from_bitmask)533else:534print_channels(format, unpack_from_struct)535536537def generate_pack_kernel(format, src_channel, src_native_type):538539if not is_format_supported(format):540return541542dst_native_type = native_type(format)543544assert format.layout == PLAIN545546def pack_into_bitmask(channels, swizzles):547inv_swizzle = inv_swizzles(swizzles)548549depth = format.block_size()550print(' uint%u_t value = 0;' % depth)551552for i in range(4):553dst_channel = channels[i]554shift = dst_channel.shift555if inv_swizzle[i] is not None:556value ='src[%u]' % inv_swizzle[i]557dst_colorspace = format.colorspace558if dst_colorspace == SRGB and inv_swizzle[i] == 3:559# Alpha channel is linear560dst_colorspace = RGB561value = conversion_expr(src_channel,562dst_channel, dst_native_type,563value,564dst_colorspace = dst_colorspace)565if dst_channel.type in (UNSIGNED, SIGNED):566if shift + dst_channel.size < depth:567value = '(%s) & 0x%x' % (value, (1 << dst_channel.size) - 1)568if shift:569value = '(uint32_t)(%s) << %u' % (value, shift)570if dst_channel.type == SIGNED:571# Cast to unsigned572value = '(uint%u_t)(%s) ' % (depth, value)573else:574value = None575if value is not None:576print(' value |= %s;' % (value))577578print(' *(uint%u_t *)dst = value;' % depth)579580def pack_into_struct(channels, swizzles):581inv_swizzle = inv_swizzles(swizzles)582583print(' struct util_format_%s pixel = {0};' % format.short_name())584585for i in range(4):586dst_channel = channels[i]587width = dst_channel.size588if inv_swizzle[i] is None:589continue590dst_colorspace = format.colorspace591if dst_colorspace == SRGB and inv_swizzle[i] == 3:592# Alpha channel is linear593dst_colorspace = RGB594value ='src[%u]' % inv_swizzle[i]595value = conversion_expr(src_channel,596dst_channel, dst_native_type,597value,598dst_colorspace = dst_colorspace)599print(' pixel.%s = %s;' % (dst_channel.name, value))600601print(' memcpy(dst, &pixel, sizeof pixel);')602603if format.is_bitmask():604print_channels(format, pack_into_bitmask)605else:606print_channels(format, pack_into_struct)607608609def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix):610'''Generate the function to unpack pixels from a particular format'''611612name = format.short_name()613614if "8unorm" in dst_suffix:615dst_proto_type = dst_native_type616else:617dst_proto_type = 'void'618619proto = 'util_format_%s_unpack_%s(%s *restrict dst_row, const uint8_t *restrict src, unsigned width)' % (620name, dst_suffix, dst_proto_type)621print('void %s;' % proto, file=sys.stdout2)622623print('void')624print(proto)625print('{')626627if is_format_supported(format):628print(' %s *dst = dst_row;' % (dst_native_type))629print(630' for (unsigned x = 0; x < width; x += %u) {' % (format.block_width,))631632generate_unpack_kernel(format, dst_channel, dst_native_type)633634print(' src += %u;' % (format.block_size() / 8,))635print(' dst += 4;')636print(' }')637638print('}')639print()640641642def generate_format_pack(format, src_channel, src_native_type, src_suffix):643'''Generate the function to pack pixels to a particular format'''644645name = format.short_name()646647print('void')648print('util_format_%s_pack_%s(uint8_t *restrict dst_row, unsigned dst_stride, const %s *restrict src_row, unsigned src_stride, unsigned width, unsigned height)' %649(name, src_suffix, src_native_type))650print('{')651652print('void util_format_%s_pack_%s(uint8_t *restrict dst_row, unsigned dst_stride, const %s *restrict src_row, unsigned src_stride, unsigned width, unsigned height);' %653(name, src_suffix, src_native_type), file=sys.stdout2)654655if is_format_supported(format):656print(' unsigned x, y;')657print(' for(y = 0; y < height; y += %u) {' % (format.block_height,))658print(' const %s *src = src_row;' % (src_native_type))659print(' uint8_t *dst = dst_row;')660print(' for(x = 0; x < width; x += %u) {' % (format.block_width,))661662generate_pack_kernel(format, src_channel, src_native_type)663664print(' src += 4;')665print(' dst += %u;' % (format.block_size() / 8,))666print(' }')667print(' dst_row += dst_stride;')668print(' src_row += src_stride/sizeof(*src_row);')669print(' }')670671print('}')672print()673674675def generate_format_fetch(format, dst_channel, dst_native_type):676'''Generate the function to unpack pixels from a particular format'''677678name = format.short_name()679680proto = 'util_format_%s_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src, UNUSED unsigned i, UNUSED unsigned j)' % (name)681print('void %s;' % proto, file=sys.stdout2)682683print('void')684print(proto)685686print('{')687print(' %s *dst = in_dst;' % dst_native_type)688689if is_format_supported(format):690generate_unpack_kernel(format, dst_channel, dst_native_type)691692print('}')693print()694695696def is_format_hand_written(format):697return format.layout != PLAIN or format.colorspace == ZS698699700def generate(formats):701print()702print('#include "pipe/p_compiler.h"')703print('#include "util/u_math.h"')704print('#include "util/half_float.h"')705print('#include "u_format.h"')706print('#include "u_format_other.h"')707print('#include "util/format_srgb.h"')708print('#include "format_utils.h"')709print('#include "u_format_yuv.h"')710print('#include "u_format_zs.h"')711print('#include "u_format_pack.h"')712print()713714for format in formats:715if not is_format_hand_written(format):716717if is_format_supported(format) and not format.is_bitmask():718generate_format_type(format)719720if format.is_pure_unsigned():721native_type = 'unsigned'722suffix = 'unsigned'723channel = Channel(UNSIGNED, False, True, 32)724725generate_format_unpack(format, channel, native_type, suffix)726generate_format_pack(format, channel, native_type, suffix)727generate_format_fetch(format, channel, native_type)728729channel = Channel(SIGNED, False, True, 32)730native_type = 'int'731suffix = 'signed'732generate_format_pack(format, channel, native_type, suffix)733elif format.is_pure_signed():734native_type = 'int'735suffix = 'signed'736channel = Channel(SIGNED, False, True, 32)737738generate_format_unpack(format, channel, native_type, suffix)739generate_format_pack(format, channel, native_type, suffix)740generate_format_fetch(format, channel, native_type)741742native_type = 'unsigned'743suffix = 'unsigned'744channel = Channel(UNSIGNED, False, True, 32)745generate_format_pack(format, channel, native_type, suffix)746else:747channel = Channel(FLOAT, False, False, 32)748native_type = 'float'749suffix = 'rgba_float'750751generate_format_unpack(format, channel, native_type, suffix)752generate_format_pack(format, channel, native_type, suffix)753generate_format_fetch(format, channel, native_type)754755channel = Channel(UNSIGNED, True, False, 8)756native_type = 'uint8_t'757suffix = 'rgba_8unorm'758759generate_format_unpack(format, channel, native_type, suffix)760generate_format_pack(format, channel, native_type, suffix)761762763