From 7fd87c704a7be0ed4b9929979f43f0e5874eb857 Mon Sep 17 00:00:00 2001 From: Jesse Natalie Date: Thu, 10 Sep 2020 16:25:58 -0700 Subject: [PATCH] Use power-of-two alignment values for allocating pixel data (#827) --- test_common/harness/imageHelpers.cpp | 35 +++++++++++++++++++++------- test_common/harness/imageHelpers.h | 10 ++++---- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/test_common/harness/imageHelpers.cpp b/test_common/harness/imageHelpers.cpp index d5cdc7a1..807f4338 100644 --- a/test_common/harness/imageHelpers.cpp +++ b/test_common/harness/imageHelpers.cpp @@ -72,12 +72,12 @@ sRGBunmap(float fc) } -size_t get_format_type_size( const cl_image_format *format ) +uint32_t get_format_type_size(const cl_image_format *format) { return get_channel_data_type_size( format->image_channel_data_type ); } -size_t get_channel_data_type_size( cl_channel_type channelType ) +uint32_t get_channel_data_type_size(cl_channel_type channelType) { switch( channelType ) { @@ -129,12 +129,12 @@ size_t get_channel_data_type_size( cl_channel_type channelType ) } } -size_t get_format_channel_count( const cl_image_format *format ) +uint32_t get_format_channel_count(const cl_image_format *format) { return get_channel_order_channel_count( format->image_channel_order ); } -size_t get_channel_order_channel_count( cl_channel_order order ) +uint32_t get_channel_order_channel_count(cl_channel_order order) { switch( order ) { @@ -281,7 +281,7 @@ int is_format_signed( const cl_image_format *format ) } } -size_t get_pixel_size( cl_image_format *format ) +uint32_t get_pixel_size(cl_image_format *format) { switch( format->image_channel_data_type ) { @@ -333,6 +333,23 @@ size_t get_pixel_size( cl_image_format *format ) } } +uint32_t next_power_of_two(uint32_t v) +{ + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v++; + return v; +} + +uint32_t get_pixel_alignment(cl_image_format *format) +{ + return next_power_of_two(get_pixel_size(format)); +} + int get_8_bit_image_format( cl_context context, cl_mem_object_type objType, cl_mem_flags flags, size_t channelCount, cl_image_format *outFormat ) { cl_image_format formatList[ 128 ]; @@ -1019,7 +1036,8 @@ char * generate_random_image_data( image_descriptor *imageInfo, BufferOwningPtr< } #else P.reset( NULL ); // Free already allocated memory first, then try to allocate new block. - char *data = (char *)align_malloc(allocSize, get_pixel_size(imageInfo->format)); + char *data = + (char *)align_malloc(allocSize, get_pixel_alignment(imageInfo->format)); P.reset(data,NULL,0,allocSize, true); #endif @@ -2942,8 +2960,9 @@ char *create_random_image_data( ExplicitType dataType, image_descriptor *imageIn P.reset(data); } #else - char *data = (char *)align_malloc(allocSize, get_pixel_size(imageInfo->format)); - P.reset(data,NULL,0,allocSize,true); + char *data = + (char *)align_malloc(allocSize, get_pixel_alignment(imageInfo->format)); + P.reset(data, NULL, 0, allocSize, true); #endif if (data == NULL) { diff --git a/test_common/harness/imageHelpers.h b/test_common/harness/imageHelpers.h index a50e85f1..a3feb72b 100644 --- a/test_common/harness/imageHelpers.h +++ b/test_common/harness/imageHelpers.h @@ -83,14 +83,14 @@ extern void build_required_image_formats(cl_mem_flags flags, cl_device_id device, std::vector& formatsToSupport); -extern size_t get_format_type_size( const cl_image_format *format ); -extern size_t get_channel_data_type_size( cl_channel_type channelType ); -extern size_t get_format_channel_count( const cl_image_format *format ); -extern size_t get_channel_order_channel_count( cl_channel_order order ); +extern uint32_t get_format_type_size(const cl_image_format *format); +extern uint32_t get_channel_data_type_size(cl_channel_type channelType); +extern uint32_t get_format_channel_count(const cl_image_format *format); +extern uint32_t get_channel_order_channel_count(cl_channel_order order); cl_channel_type get_channel_type_from_name( const char *name ); cl_channel_order get_channel_order_from_name( const char *name ); extern int is_format_signed( const cl_image_format *format ); -extern size_t get_pixel_size( cl_image_format *format ); +extern uint32_t get_pixel_size(cl_image_format *format); /* Helper to get any ol image format as long as it is 8-bits-per-channel */ extern int get_8_bit_image_format( cl_context context, cl_mem_object_type objType, cl_mem_flags flags, size_t channelCount, cl_image_format *outFormat );