Simple futurize python srcipts

macos/master
Ondřej Súkup 7 years ago committed by Dave Airlie
parent 7f30999e06
commit eaa6499cc8
  1. 159
      src/gallium/auxiliary/util/u_format_pack.py
  2. 85
      src/gallium/auxiliary/util/u_format_table.py

@ -35,6 +35,7 @@
* @author Jose Fonseca <jfonseca@vmware.com> * @author Jose Fonseca <jfonseca@vmware.com>
*/ */
''' '''
from __future__ import print_function
from u_format_parse import * from u_format_parse import *
@ -54,11 +55,11 @@ def print_channels(format, func):
if format.nr_channels() <= 1: if format.nr_channels() <= 1:
func(format.le_channels, format.le_swizzles) func(format.le_channels, format.le_swizzles)
else: else:
print '#ifdef PIPE_ARCH_BIG_ENDIAN' print('#ifdef PIPE_ARCH_BIG_ENDIAN')
func(format.be_channels, format.be_swizzles) func(format.be_channels, format.be_swizzles)
print '#else' print('#else')
func(format.le_channels, format.le_swizzles) func(format.le_channels, format.le_swizzles)
print '#endif' print('#endif')
def generate_format_type(format): def generate_format_type(format):
'''Generate a structure that describes the format.''' '''Generate a structure that describes the format.'''
@ -69,18 +70,18 @@ def generate_format_type(format):
for channel in channels: for channel in channels:
if channel.type == VOID: if channel.type == VOID:
if channel.size: if channel.size:
print ' unsigned %s:%u;' % (channel.name, channel.size) print(' unsigned %s:%u;' % (channel.name, channel.size))
elif channel.type == UNSIGNED: elif channel.type == UNSIGNED:
print ' unsigned %s:%u;' % (channel.name, channel.size) print(' unsigned %s:%u;' % (channel.name, channel.size))
elif channel.type in (SIGNED, FIXED): elif channel.type in (SIGNED, FIXED):
print ' int %s:%u;' % (channel.name, channel.size) print(' int %s:%u;' % (channel.name, channel.size))
elif channel.type == FLOAT: elif channel.type == FLOAT:
if channel.size == 64: if channel.size == 64:
print ' double %s;' % (channel.name) print(' double %s;' % (channel.name))
elif channel.size == 32: elif channel.size == 32:
print ' float %s;' % (channel.name) print(' float %s;' % (channel.name))
else: else:
print ' unsigned %s:%u;' % (channel.name, channel.size) print(' unsigned %s:%u;' % (channel.name, channel.size))
else: else:
assert 0 assert 0
@ -89,41 +90,41 @@ def generate_format_type(format):
assert channel.size % 8 == 0 and is_pot(channel.size) assert channel.size % 8 == 0 and is_pot(channel.size)
if channel.type == VOID: if channel.type == VOID:
if channel.size: if channel.size:
print ' uint%u_t %s;' % (channel.size, channel.name) print(' uint%u_t %s;' % (channel.size, channel.name))
elif channel.type == UNSIGNED: elif channel.type == UNSIGNED:
print ' uint%u_t %s;' % (channel.size, channel.name) print(' uint%u_t %s;' % (channel.size, channel.name))
elif channel.type in (SIGNED, FIXED): elif channel.type in (SIGNED, FIXED):
print ' int%u_t %s;' % (channel.size, channel.name) print(' int%u_t %s;' % (channel.size, channel.name))
elif channel.type == FLOAT: elif channel.type == FLOAT:
if channel.size == 64: if channel.size == 64:
print ' double %s;' % (channel.name) print(' double %s;' % (channel.name))
elif channel.size == 32: elif channel.size == 32:
print ' float %s;' % (channel.name) print(' float %s;' % (channel.name))
elif channel.size == 16: elif channel.size == 16:
print ' uint16_t %s;' % (channel.name) print(' uint16_t %s;' % (channel.name))
else: else:
assert 0 assert 0
else: else:
assert 0 assert 0
print 'union util_format_%s {' % format.short_name() print('union util_format_%s {' % format.short_name())
if format.block_size() in (8, 16, 32, 64): if format.block_size() in (8, 16, 32, 64):
print ' uint%u_t value;' % (format.block_size(),) print(' uint%u_t value;' % (format.block_size(),))
use_bitfields = False use_bitfields = False
for channel in format.le_channels: for channel in format.le_channels:
if channel.size % 8 or not is_pot(channel.size): if channel.size % 8 or not is_pot(channel.size):
use_bitfields = True use_bitfields = True
print ' struct {' print(' struct {')
if use_bitfields: if use_bitfields:
print_channels(format, generate_bitfields) print_channels(format, generate_bitfields)
else: else:
print_channels(format, generate_full_fields) print_channels(format, generate_full_fields)
print ' } chan;' print(' } chan;')
print '};' print('};')
print print()
def is_format_supported(format): def is_format_supported(format):
@ -445,15 +446,15 @@ def generate_unpack_kernel(format, dst_channel, dst_native_type):
def unpack_from_bitmask(channels, swizzles): def unpack_from_bitmask(channels, swizzles):
depth = format.block_size() depth = format.block_size()
print ' uint%u_t value = *(const uint%u_t *)src;' % (depth, depth) print(' uint%u_t value = *(const uint%u_t *)src;' % (depth, depth))
# Declare the intermediate variables # Declare the intermediate variables
for i in range(format.nr_channels()): for i in range(format.nr_channels()):
src_channel = channels[i] src_channel = channels[i]
if src_channel.type == UNSIGNED: if src_channel.type == UNSIGNED:
print ' uint%u_t %s;' % (depth, src_channel.name) print(' uint%u_t %s;' % (depth, src_channel.name))
elif src_channel.type == SIGNED: elif src_channel.type == SIGNED:
print ' int%u_t %s;' % (depth, src_channel.name) print(' int%u_t %s;' % (depth, src_channel.name))
# Compute the intermediate unshifted values # Compute the intermediate unshifted values
for i in range(format.nr_channels()): for i in range(format.nr_channels()):
@ -480,7 +481,7 @@ def generate_unpack_kernel(format, dst_channel, dst_native_type):
value = None value = None
if value is not None: if value is not None:
print ' %s = %s;' % (src_channel.name, value) print(' %s = %s;' % (src_channel.name, value))
# Convert, swizzle, and store final values # Convert, swizzle, and store final values
for i in range(4): for i in range(4):
@ -504,11 +505,11 @@ def generate_unpack_kernel(format, dst_channel, dst_native_type):
value = '0' value = '0'
else: else:
assert False assert False
print ' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]) print(' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]))
def unpack_from_union(channels, swizzles): def unpack_from_union(channels, swizzles):
print ' union util_format_%s pixel;' % format.short_name() print(' union util_format_%s pixel;' % format.short_name())
print ' memcpy(&pixel, src, sizeof pixel);' print(' memcpy(&pixel, src, sizeof pixel);')
for i in range(4): for i in range(4):
swizzle = swizzles[i] swizzle = swizzles[i]
@ -531,7 +532,7 @@ def generate_unpack_kernel(format, dst_channel, dst_native_type):
value = '0' value = '0'
else: else:
assert False assert False
print ' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]) print(' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]))
if format.is_bitmask(): if format.is_bitmask():
print_channels(format, unpack_from_bitmask) print_channels(format, unpack_from_bitmask)
@ -552,7 +553,7 @@ def generate_pack_kernel(format, src_channel, src_native_type):
inv_swizzle = inv_swizzles(swizzles) inv_swizzle = inv_swizzles(swizzles)
depth = format.block_size() depth = format.block_size()
print ' uint%u_t value = 0;' % depth print(' uint%u_t value = 0;' % depth)
for i in range(4): for i in range(4):
dst_channel = channels[i] dst_channel = channels[i]
@ -578,14 +579,14 @@ def generate_pack_kernel(format, src_channel, src_native_type):
else: else:
value = None value = None
if value is not None: if value is not None:
print ' value |= %s;' % (value) print(' value |= %s;' % (value))
print ' *(uint%u_t *)dst = value;' % depth print(' *(uint%u_t *)dst = value;' % depth)
def pack_into_union(channels, swizzles): def pack_into_union(channels, swizzles):
inv_swizzle = inv_swizzles(swizzles) inv_swizzle = inv_swizzles(swizzles)
print ' union util_format_%s pixel;' % format.short_name() print(' union util_format_%s pixel;' % format.short_name())
for i in range(4): for i in range(4):
dst_channel = channels[i] dst_channel = channels[i]
@ -601,9 +602,9 @@ def generate_pack_kernel(format, src_channel, src_native_type):
dst_channel, dst_native_type, dst_channel, dst_native_type,
value, value,
dst_colorspace = dst_colorspace) dst_colorspace = dst_colorspace)
print ' pixel.chan.%s = %s;' % (dst_channel.name, value) print(' pixel.chan.%s = %s;' % (dst_channel.name, value))
print ' memcpy(dst, &pixel, sizeof pixel);' print(' memcpy(dst, &pixel, sizeof pixel);')
if format.is_bitmask(): if format.is_bitmask():
print_channels(format, pack_into_bitmask) print_channels(format, pack_into_bitmask)
@ -616,28 +617,28 @@ def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix):
name = format.short_name() name = format.short_name()
print 'static inline void' print('static inline void')
print 'util_format_%s_unpack_%s(%s *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, dst_suffix, dst_native_type) print('util_format_%s_unpack_%s(%s *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, dst_suffix, dst_native_type))
print '{' print('{')
if is_format_supported(format): if is_format_supported(format):
print ' unsigned x, y;' print(' unsigned x, y;')
print ' for(y = 0; y < height; y += %u) {' % (format.block_height,) print(' for(y = 0; y < height; y += %u) {' % (format.block_height,))
print ' %s *dst = dst_row;' % (dst_native_type) print(' %s *dst = dst_row;' % (dst_native_type))
print ' const uint8_t *src = src_row;' print(' const uint8_t *src = src_row;')
print ' for(x = 0; x < width; x += %u) {' % (format.block_width,) print(' for(x = 0; x < width; x += %u) {' % (format.block_width,))
generate_unpack_kernel(format, dst_channel, dst_native_type) generate_unpack_kernel(format, dst_channel, dst_native_type)
print ' src += %u;' % (format.block_size() / 8,) print(' src += %u;' % (format.block_size() / 8,))
print ' dst += 4;' print(' dst += 4;')
print ' }' print(' }')
print ' src_row += src_stride;' print(' src_row += src_stride;')
print ' dst_row += dst_stride/sizeof(*dst_row);' print(' dst_row += dst_stride/sizeof(*dst_row);')
print ' }' print(' }')
print '}' print('}')
print print()
def generate_format_pack(format, src_channel, src_native_type, src_suffix): def generate_format_pack(format, src_channel, src_native_type, src_suffix):
@ -645,28 +646,28 @@ def generate_format_pack(format, src_channel, src_native_type, src_suffix):
name = format.short_name() name = format.short_name()
print 'static inline void' print('static inline void')
print 'util_format_%s_pack_%s(uint8_t *dst_row, unsigned dst_stride, const %s *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, src_suffix, src_native_type) print('util_format_%s_pack_%s(uint8_t *dst_row, unsigned dst_stride, const %s *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, src_suffix, src_native_type))
print '{' print('{')
if is_format_supported(format): if is_format_supported(format):
print ' unsigned x, y;' print(' unsigned x, y;')
print ' for(y = 0; y < height; y += %u) {' % (format.block_height,) print(' for(y = 0; y < height; y += %u) {' % (format.block_height,))
print ' const %s *src = src_row;' % (src_native_type) print(' const %s *src = src_row;' % (src_native_type))
print ' uint8_t *dst = dst_row;' print(' uint8_t *dst = dst_row;')
print ' for(x = 0; x < width; x += %u) {' % (format.block_width,) print(' for(x = 0; x < width; x += %u) {' % (format.block_width,))
generate_pack_kernel(format, src_channel, src_native_type) generate_pack_kernel(format, src_channel, src_native_type)
print ' src += 4;' print(' src += 4;')
print ' dst += %u;' % (format.block_size() / 8,) print(' dst += %u;' % (format.block_size() / 8,))
print ' }' print(' }')
print ' dst_row += dst_stride;' print(' dst_row += dst_stride;')
print ' src_row += src_stride/sizeof(*src_row);' print(' src_row += src_stride/sizeof(*src_row);')
print ' }' print(' }')
print '}' print('}')
print print()
def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix): def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix):
@ -674,15 +675,15 @@ def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix):
name = format.short_name() name = format.short_name()
print 'static inline void' print('static inline void')
print 'util_format_%s_fetch_%s(%s *dst, const uint8_t *src, unsigned i, unsigned j)' % (name, dst_suffix, dst_native_type) print('util_format_%s_fetch_%s(%s *dst, const uint8_t *src, unsigned i, unsigned j)' % (name, dst_suffix, dst_native_type))
print '{' print('{')
if is_format_supported(format): if is_format_supported(format):
generate_unpack_kernel(format, dst_channel, dst_native_type) generate_unpack_kernel(format, dst_channel, dst_native_type)
print '}' print('}')
print print()
def is_format_hand_written(format): def is_format_hand_written(format):
@ -690,10 +691,10 @@ def is_format_hand_written(format):
def generate(formats): def generate(formats):
print print()
print '#include "pipe/p_compiler.h"' print('#include "pipe/p_compiler.h"')
print '#include "u_math.h"' print('#include "u_math.h"')
print '#include "u_half.h"' print('#include "u_half.h"')
print '#include "u_format.h"' print('#include "u_format.h"')
print print()

@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
CopyRight = ''' CopyRight = '''
/************************************************************************** /**************************************************************************
* *
@ -80,18 +81,18 @@ swizzle_map = {
def write_format_table(formats): def write_format_table(formats):
print '/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */' print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */')
print print()
# This will print the copyright message on the top of this file # This will print the copyright message on the top of this file
print CopyRight.strip() print(CopyRight.strip())
print print()
print '#include "u_format.h"' print('#include "u_format.h"')
print print()
u_format_pack.generate(formats) u_format_pack.generate(formats)
def do_channel_array(channels, swizzles): def do_channel_array(channels, swizzles):
print " {" print(" {")
for i in range(4): for i in range(4):
channel = channels[i] channel = channels[i]
if i < 3: if i < 3:
@ -99,13 +100,13 @@ def write_format_table(formats):
else: else:
sep = "" sep = ""
if channel.size: if channel.size:
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) 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))
else: else:
print " {0, 0, 0, 0, 0}%s" % (sep,) print(" {0, 0, 0, 0, 0}%s" % (sep,))
print " }," print(" },")
def do_swizzle_array(channels, swizzles): def do_swizzle_array(channels, swizzles):
print " {" print(" {")
for i in range(4): for i in range(4):
swizzle = swizzles[i] swizzle = swizzles[i]
if i < 3: if i < 3:
@ -116,43 +117,43 @@ def write_format_table(formats):
comment = colorspace_channels_map[format.colorspace][i] comment = colorspace_channels_map[format.colorspace][i]
except (KeyError, IndexError): except (KeyError, IndexError):
comment = 'ignored' comment = 'ignored'
print " %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment) print(" %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment))
print " }," print(" },")
for format in formats: for format in formats:
print 'const struct util_format_description' print('const struct util_format_description')
print 'util_format_%s_description = {' % (format.short_name(),) print('util_format_%s_description = {' % (format.short_name(),))
print " %s," % (format.name,) print(" %s," % (format.name,))
print " \"%s\"," % (format.name,) print(" \"%s\"," % (format.name,))
print " \"%s\"," % (format.short_name(),) print(" \"%s\"," % (format.short_name(),))
print " {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size()) print(" {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size()))
print " %s," % (layout_map(format.layout),) print(" %s," % (layout_map(format.layout),))
print " %u,\t/* nr_channels */" % (format.nr_channels(),) print(" %u,\t/* nr_channels */" % (format.nr_channels(),))
print " %s,\t/* is_array */" % (bool_map(format.is_array()),) print(" %s,\t/* is_array */" % (bool_map(format.is_array()),))
print " %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),) print(" %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),))
print " %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),) print(" %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),))
u_format_pack.print_channels(format, do_channel_array) u_format_pack.print_channels(format, do_channel_array)
u_format_pack.print_channels(format, do_swizzle_array) u_format_pack.print_channels(format, do_swizzle_array)
print " %s," % (colorspace_map(format.colorspace),) print(" %s," % (colorspace_map(format.colorspace),))
print "};" print("};")
print print()
print "const struct util_format_description *" print("const struct util_format_description *")
print "util_format_description(enum pipe_format format)" print("util_format_description(enum pipe_format format)")
print "{" print("{")
print " if (format >= PIPE_FORMAT_COUNT) {" print(" if (format >= PIPE_FORMAT_COUNT) {")
print " return NULL;" print(" return NULL;")
print " }" print(" }")
print print()
print " switch (format) {" print(" switch (format) {")
for format in formats: for format in formats:
print " case %s:" % format.name print(" case %s:" % format.name)
print " return &util_format_%s_description;" % (format.short_name(),) print(" return &util_format_%s_description;" % (format.short_name(),))
print " default:" print(" default:")
print " return NULL;" print(" return NULL;")
print " }" print(" }")
print "}" print("}")
print print()
def main(): def main():

Loading…
Cancel
Save