Use power-of-two alignment values for allocating pixel data (#827)

This commit is contained in:
Jesse Natalie
2020-09-10 16:25:58 -07:00
committed by GitHub
parent 172322e155
commit 7fd87c704a
2 changed files with 32 additions and 13 deletions

View File

@@ -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 ); 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 ) 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 ); 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 ) 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 ) 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 ) 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 ]; cl_image_format formatList[ 128 ];
@@ -1019,7 +1036,8 @@ char * generate_random_image_data( image_descriptor *imageInfo, BufferOwningPtr<
} }
#else #else
P.reset( NULL ); // Free already allocated memory first, then try to allocate new block. 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); P.reset(data,NULL,0,allocSize, true);
#endif #endif
@@ -2942,8 +2960,9 @@ char *create_random_image_data( ExplicitType dataType, image_descriptor *imageIn
P.reset(data); P.reset(data);
} }
#else #else
char *data = (char *)align_malloc(allocSize, get_pixel_size(imageInfo->format)); char *data =
P.reset(data,NULL,0,allocSize,true); (char *)align_malloc(allocSize, get_pixel_alignment(imageInfo->format));
P.reset(data, NULL, 0, allocSize, true);
#endif #endif
if (data == NULL) { if (data == NULL) {

View File

@@ -83,14 +83,14 @@ extern void build_required_image_formats(cl_mem_flags flags,
cl_device_id device, cl_device_id device,
std::vector<cl_image_format>& formatsToSupport); std::vector<cl_image_format>& formatsToSupport);
extern size_t get_format_type_size( const cl_image_format *format ); extern uint32_t get_format_type_size(const cl_image_format *format);
extern size_t get_channel_data_type_size( cl_channel_type channelType ); extern uint32_t get_channel_data_type_size(cl_channel_type channelType);
extern size_t get_format_channel_count( const cl_image_format *format ); extern uint32_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_channel_order_channel_count(cl_channel_order order);
cl_channel_type get_channel_type_from_name( const char *name ); cl_channel_type get_channel_type_from_name( const char *name );
cl_channel_order get_channel_order_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 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 */ /* 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 ); 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 );