util: Add callback to fetch a single pixel.

This commit is contained in:
José Fonseca
2010-03-31 20:15:17 +01:00
parent 3200e70858
commit 69895725cf
4 changed files with 249 additions and 175 deletions

View File

@@ -33,6 +33,37 @@
#include "util/u_format_tests.h" #include "util/u_format_tests.h"
static boolean
test_format_fetch_float(const struct util_format_description *format_desc,
const struct util_format_test_case *test)
{
float unpacked[4];
unsigned i;
boolean success;
/*
* TODO: test block formats too.
*/
if (format_desc->block.width != 1 && format_desc->block.height != 1) {
return TRUE;
}
format_desc->fetch_float(unpacked, test->packed, 0, 0);
success = TRUE;
for (i = 0; i < 4; ++i)
if (test->unpacked[i] != unpacked[i])
success = FALSE;
if (!success) {
printf("FAILED: (%f %f %f %f) obtained\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
printf(" (%f %f %f %f) expected\n", test->unpacked[0], test->unpacked[1], test->unpacked[2], test->unpacked[3]);
}
return success;
}
static boolean static boolean
test_format_unpack_float(const struct util_format_description *format_desc, test_format_unpack_float(const struct util_format_description *format_desc,
const struct util_format_test_case *test) const struct util_format_test_case *test)
@@ -225,6 +256,9 @@ test_all(void)
{ {
bool success = TRUE; bool success = TRUE;
if (!test_one(&test_format_fetch_float, "fetch_float"))
success = FALSE;
if (!test_one(&test_format_pack_float, "pack_float")) if (!test_one(&test_format_pack_float, "pack_float"))
success = FALSE; success = FALSE;

View File

@@ -191,21 +191,34 @@ struct util_format_description
enum util_format_colorspace colorspace; enum util_format_colorspace colorspace;
/** /**
* Accessor functions. * Unpack a span of pixel blocks to R8G8B8A8_UNORM.
*/ */
void void
(*unpack_8unorm)(uint8_t *dst, const uint8_t *src, unsigned nr_blocks); (*unpack_8unorm)(uint8_t *dst, const uint8_t *src, unsigned nr_blocks);
/**
* Pack a span of pixel blocks from R8G8B8A8_UNORM.
*/
void void
(*pack_8unorm)(uint8_t *dst, const uint8_t *src, unsigned nr_blocks); (*pack_8unorm)(uint8_t *dst, const uint8_t *src, unsigned nr_blocks);
/**
* Unpack a span of pixel blocks to R32G32B32A32_FLOAT.
*/
void void
(*unpack_float)(float *dst, const uint8_t *src, unsigned nr_blocks); (*unpack_float)(float *dst, const uint8_t *src, unsigned nr_blocks);
/**
* Pack a span of pixel blocks from R32G32B32A32_FLOAT.
*/
void void
(*pack_float)(uint8_t *dst, const float *src, unsigned nr_blocks); (*pack_float)(uint8_t *dst, const float *src, unsigned nr_blocks);
/**
* Fetch a single pixel (i, j) from a block.
*/
void
(*fetch_float)(float *dst, const uint8_t *src, unsigned i, unsigned j);
}; };

View File

@@ -324,23 +324,15 @@ def conversion_expr(src_channel, dst_channel, dst_native_type, value, clamp=True
assert False assert False
def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix): def generate_unpack_kernel(format, dst_channel, dst_native_type):
'''Generate the function to unpack pixels from a particular format'''
name = format.short_name() if not is_format_supported(format):
return
print 'static INLINE void'
print 'util_format_%s_unpack_%s(%s *dst, const uint8_t *src, unsigned length)' % (name, dst_suffix, dst_native_type)
print '{'
if is_format_supported(format):
assert format.layout == PLAIN assert format.layout == PLAIN
src_native_type = native_type(format) src_native_type = native_type(format)
print ' while(length--) {'
if format.is_bitmask(): if format.is_bitmask():
depth = format.block_size() depth = format.block_size()
print ' uint%u_t value = *(uint%u_t *)src;' % (depth, depth) print ' uint%u_t value = *(uint%u_t *)src;' % (depth, depth)
@@ -434,32 +426,18 @@ def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix):
value = 'dst[0]' value = 'dst[0]'
print ' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]) print ' dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i])
print ' src += %u;' % (format.block_size() / 8,)
print ' dst += 4;'
print ' }'
print '}' def generate_pack_kernel(format, src_channel, src_native_type):
print
if not is_format_supported(format):
return
def generate_format_pack(format, src_channel, src_native_type, src_suffix):
'''Generate the function to pack pixels to a particular format'''
name = format.short_name()
print 'static INLINE void'
print 'util_format_%s_pack_%s(uint8_t *dst, const %s *src, unsigned length)' % (name, src_suffix, src_native_type)
print '{'
if is_format_supported(format):
dst_native_type = native_type(format) dst_native_type = native_type(format)
assert format.layout == PLAIN assert format.layout == PLAIN
inv_swizzle = format.inv_swizzles() inv_swizzle = format.inv_swizzles()
print ' while(length--) {'
if format.is_bitmask(): if format.is_bitmask():
depth = format.block_size() depth = format.block_size()
print ' uint%u_t value = 0;' % depth print ' uint%u_t value = 0;' % depth
@@ -516,6 +494,43 @@ def generate_format_pack(format, src_channel, src_native_type, src_suffix):
bswap_format(format) bswap_format(format)
print ' memcpy(dst, &pixel, sizeof pixel);' print ' memcpy(dst, &pixel, sizeof pixel);'
def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix):
'''Generate the function to unpack pixels from a particular format'''
name = format.short_name()
print 'static INLINE void'
print 'util_format_%s_unpack_%s(%s *dst, const uint8_t *src, unsigned length)' % (name, dst_suffix, dst_native_type)
print '{'
if is_format_supported(format):
print ' while(length--) {'
generate_unpack_kernel(format, dst_channel, dst_native_type)
print ' src += %u;' % (format.block_size() / 8,)
print ' dst += 4;'
print ' }'
print '}'
print
def generate_format_pack(format, src_channel, src_native_type, src_suffix):
'''Generate the function to pack pixels to a particular format'''
name = format.short_name()
print 'static INLINE void'
print 'util_format_%s_pack_%s(uint8_t *dst, const %s *src, unsigned length)' % (name, src_suffix, src_native_type)
print '{'
if is_format_supported(format):
print ' while(length--) {'
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 ' }'
@@ -524,18 +539,20 @@ def generate_format_pack(format, src_channel, src_native_type, src_suffix):
print print
def generate_unpack(formats, dst_channel, dst_native_type, dst_suffix): def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix):
'''Generate the dispatch function to unpack pixels from any format''' '''Generate the function to unpack pixels from a particular format'''
for format in formats: name = format.short_name()
generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix)
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 '{'
def generate_pack(formats, src_channel, src_native_type, src_suffix): if is_format_supported(format):
'''Generate the dispatch function to pack pixels to any format''' generate_unpack_kernel(format, dst_channel, dst_native_type)
for format in formats: print '}'
generate_format_pack(format, src_channel, src_native_type, src_suffix) print
def generate(formats): def generate(formats):
@@ -555,13 +572,16 @@ def generate(formats):
native_type = 'float' native_type = 'float'
suffix = 'float' suffix = 'float'
generate_unpack(formats, channel, native_type, suffix) for format in formats:
generate_pack(formats, channel, native_type, suffix) generate_format_unpack(format, channel, native_type, suffix)
generate_format_pack(format, channel, native_type, suffix)
generate_format_fetch(format, channel, native_type, suffix)
channel = Channel(UNSIGNED, True, 8) channel = Channel(UNSIGNED, True, 8)
native_type = 'uint8_t' native_type = 'uint8_t'
suffix = '8unorm' suffix = '8unorm'
generate_unpack(formats, channel, native_type, suffix) for format in formats:
generate_pack(formats, channel, native_type, suffix) generate_format_unpack(format, channel, native_type, suffix)
generate_format_pack(format, channel, native_type, suffix)

View File

@@ -106,6 +106,11 @@ util_format_none_unpack_float(float *dst, const uint8_t *src, unsigned length)
static void static void
util_format_none_pack_float(uint8_t *dst, const float *src, unsigned length) util_format_none_pack_float(uint8_t *dst, const float *src, unsigned length)
{ {
}
static void
util_format_none_fetch_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
{
} }
''' '''
print 'const struct util_format_description' print 'const struct util_format_description'
@@ -125,7 +130,8 @@ util_format_none_pack_float(uint8_t *dst, const float *src, unsigned length)
print " &util_format_none_unpack_8unorm," print " &util_format_none_unpack_8unorm,"
print " &util_format_none_pack_8unorm," print " &util_format_none_pack_8unorm,"
print " &util_format_none_unpack_float," print " &util_format_none_unpack_float,"
print " &util_format_none_pack_float" print " &util_format_none_pack_float,"
print " &util_format_none_fetch_float"
print "};" print "};"
print print
@@ -172,7 +178,8 @@ util_format_none_pack_float(uint8_t *dst, const float *src, unsigned length)
print " &util_format_%s_unpack_8unorm," % format.short_name() print " &util_format_%s_unpack_8unorm," % format.short_name()
print " &util_format_%s_pack_8unorm," % format.short_name() print " &util_format_%s_pack_8unorm," % format.short_name()
print " &util_format_%s_unpack_float," % format.short_name() print " &util_format_%s_unpack_float," % format.short_name()
print " &util_format_%s_pack_float" % format.short_name() print " &util_format_%s_pack_float," % format.short_name()
print " &util_format_%s_fetch_float" % format.short_name()
print "};" print "};"
print print