mirror of
https://github.com/KhronosGroup/OpenCL-CTS.git
synced 2026-03-19 14:09:03 +00:00
Refactor imagecopy and imagereadwrite tests (#2362)
Refactor the following tests: 1. `test_imagecopy` 2. `test_imagecopy3d` 3. `test_imagereadwrite` 4. `test_imagereadwrite3d` The change does the following: 1. Use RAII to manage allocated resources 2. For `imagecopy` and `imagecopy3d`, the change allows for a custom src image memory flags and adjusts how the source image is created according to the input flags. Signed-off-by: Michael Rizkalla <michael.rizkalla@arm.com>
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
//
|
||||
#include "harness/compat.h"
|
||||
|
||||
#include <memory>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -23,16 +24,15 @@
|
||||
|
||||
#include "testBase.h"
|
||||
|
||||
static unsigned char *
|
||||
generate_rgba8_image(int w, int h, MTdata d)
|
||||
static std::unique_ptr<unsigned char[]> generate_rgba8_image(int w, int h,
|
||||
MTdata d)
|
||||
{
|
||||
unsigned char *ptr = (unsigned char*)malloc(w * h * 4);
|
||||
int i;
|
||||
std::unique_ptr<unsigned char[]> ptr{ new unsigned char[w * h * 4] };
|
||||
|
||||
for (i=0; i<w*h*4; i++)
|
||||
ptr[i] = (unsigned char)genrand_int32(d);
|
||||
for (int i = 0; i < w * h * 4; i++)
|
||||
ptr[i] = (unsigned char)genrand_int32(d);
|
||||
|
||||
return ptr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -73,8 +73,8 @@ update_image_from_image(void *out, void *in, int x, int y, int w, int h, int img
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
verify_rgba8_image(unsigned char *image, unsigned char *outptr, int w, int h)
|
||||
static int verify_rgba8_image(const unsigned char *image,
|
||||
const unsigned char *outptr, int w, int h)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -91,16 +91,15 @@ verify_rgba8_image(unsigned char *image, unsigned char *outptr, int w, int h)
|
||||
}
|
||||
|
||||
|
||||
static unsigned short *
|
||||
generate_rgba16_image(int w, int h, MTdata d)
|
||||
static std::unique_ptr<unsigned short[]> generate_rgba16_image(int w, int h,
|
||||
MTdata d)
|
||||
{
|
||||
unsigned short *ptr = (unsigned short*)malloc(w * h * 4 * sizeof(unsigned short));
|
||||
int i;
|
||||
std::unique_ptr<unsigned short[]> ptr{ new unsigned short[w * h * 4] };
|
||||
|
||||
for (i=0; i<w*h*4; i++)
|
||||
ptr[i] = (unsigned short)genrand_int32(d);
|
||||
for (int i = 0; i < w * h * 4; i++)
|
||||
ptr[i] = (unsigned short)genrand_int32(d);
|
||||
|
||||
return ptr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -121,8 +120,8 @@ update_rgba16_image(unsigned short *p, int x, int y, int w, int h, int img_width
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
verify_rgba16_image(unsigned short *image, unsigned short *outptr, int w, int h)
|
||||
static int verify_rgba16_image(const unsigned short *image,
|
||||
const unsigned short *outptr, int w, int h)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -139,16 +138,14 @@ verify_rgba16_image(unsigned short *image, unsigned short *outptr, int w, int h)
|
||||
}
|
||||
|
||||
|
||||
static float *
|
||||
generate_rgbafp_image(int w, int h, MTdata d)
|
||||
static std::unique_ptr<float[]> generate_rgbafp_image(int w, int h, MTdata d)
|
||||
{
|
||||
float *ptr = (float*)malloc(w * h * 4 * sizeof(float));
|
||||
int i;
|
||||
std::unique_ptr<float[]> ptr{ new float[w * h * 4] };
|
||||
|
||||
for (i=0; i<w*h*4; i++)
|
||||
ptr[i] = get_random_float(-0x40000000, 0x40000000, d);
|
||||
for (int i = 0; i < w * h * 4; i++)
|
||||
ptr[i] = get_random_float(-0x40000000, 0x40000000, d);
|
||||
|
||||
return ptr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -169,8 +166,8 @@ update_rgbafp_image(float *p, int x, int y, int w, int h, int img_width, MTdata
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
verify_rgbafp_image(float *image, float *outptr, int w, int h)
|
||||
static int verify_rgbafp_image(const float *image, const float *outptr, int w,
|
||||
int h)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -186,59 +183,52 @@ verify_rgbafp_image(float *image, float *outptr, int w, int h)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static constexpr cl_image_format image_formats[] = { { CL_RGBA, CL_UNORM_INT8 },
|
||||
{ CL_RGBA,
|
||||
CL_UNORM_INT16 },
|
||||
{ CL_RGBA, CL_FLOAT } };
|
||||
|
||||
REGISTER_TEST(imagereadwrite)
|
||||
{
|
||||
cl_image_format img_format;
|
||||
unsigned char *rgba8_inptr, *rgba8_outptr;
|
||||
unsigned short *rgba16_inptr, *rgba16_outptr;
|
||||
float *rgbafp_inptr, *rgbafp_outptr;
|
||||
constexpr size_t image_formats_count = ARRAY_SIZE(image_formats);
|
||||
std::unique_ptr<unsigned char[]> rgba8_inptr, rgba8_outptr;
|
||||
std::unique_ptr<unsigned short[]> rgba16_inptr, rgba16_outptr;
|
||||
std::unique_ptr<float[]> rgbafp_inptr, rgbafp_outptr;
|
||||
clMemWrapper streams[3];
|
||||
int img_width = 512;
|
||||
int img_height = 512;
|
||||
int num_tries = 200;
|
||||
int i, j, err;
|
||||
MTdata d;
|
||||
MTdataHolder d(gRandomSeed);
|
||||
|
||||
PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
|
||||
|
||||
d = init_genrand( gRandomSeed );
|
||||
rgba8_inptr = (unsigned char *)generate_rgba8_image(img_width, img_height, d);
|
||||
rgba16_inptr = (unsigned short *)generate_rgba16_image(img_width, img_height, d);
|
||||
rgbafp_inptr = (float *)generate_rgbafp_image(img_width, img_height, d);
|
||||
rgba8_inptr = generate_rgba8_image(img_width, img_height, d);
|
||||
rgba16_inptr = generate_rgba16_image(img_width, img_height, d);
|
||||
rgbafp_inptr = generate_rgbafp_image(img_width, img_height, d);
|
||||
|
||||
rgba8_outptr = (unsigned char*)malloc(sizeof(unsigned char) * 4 * img_width * img_height);
|
||||
rgba16_outptr = (unsigned short*)malloc(sizeof(unsigned short) * 4 * img_width * img_height);
|
||||
rgbafp_outptr = (float*)malloc(sizeof(float) * 4 * img_width * img_height);
|
||||
rgba8_outptr.reset(new unsigned char[4 * img_width * img_height]);
|
||||
rgba16_outptr.reset(new unsigned short[4 * img_width * img_height]);
|
||||
rgbafp_outptr.reset(new float[4 * img_width * img_height]);
|
||||
|
||||
img_format.image_channel_order = CL_RGBA;
|
||||
img_format.image_channel_data_type = CL_UNORM_INT8;
|
||||
streams[0] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
|
||||
img_width, img_height, 0, NULL, &err);
|
||||
test_error(err, "create_image_2d failed");
|
||||
|
||||
img_format.image_channel_order = CL_RGBA;
|
||||
img_format.image_channel_data_type = CL_UNORM_INT16;
|
||||
streams[1] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
|
||||
img_width, img_height, 0, NULL, &err);
|
||||
test_error(err, "create_image_2d failed");
|
||||
|
||||
img_format.image_channel_order = CL_RGBA;
|
||||
img_format.image_channel_data_type = CL_FLOAT;
|
||||
streams[2] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format,
|
||||
img_width, img_height, 0, NULL, &err);
|
||||
test_error(err, "create_image_2d failed");
|
||||
for (size_t index = 0; index < image_formats_count; ++index)
|
||||
{
|
||||
streams[index] =
|
||||
create_image_2d(context, CL_MEM_READ_WRITE, &image_formats[index],
|
||||
img_width, img_height, 0, NULL, &err);
|
||||
test_error(err, "create_image_2d failed");
|
||||
}
|
||||
|
||||
for (i=0; i<3; i++)
|
||||
{
|
||||
void *p;
|
||||
|
||||
if (i == 0)
|
||||
p = (void *)rgba8_inptr;
|
||||
p = rgba8_inptr.get();
|
||||
else if (i == 1)
|
||||
p = (void *)rgba16_inptr;
|
||||
p = rgba16_inptr.get();
|
||||
else
|
||||
p = (void *)rgbafp_inptr;
|
||||
p = rgbafp_inptr.get();
|
||||
size_t origin[3] = {0,0,0}, region[3] = {img_width, img_height, 1};
|
||||
err = clEnqueueWriteImage(queue, streams[i], CL_TRUE,
|
||||
origin, region, 0, 0,
|
||||
@@ -250,7 +240,7 @@ REGISTER_TEST(imagereadwrite)
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0,j=0; i<num_tries*3; i++,j++)
|
||||
for (i = 0, j = 0; i < num_tries * image_formats_count; i++, j++)
|
||||
{
|
||||
int x = (int)get_random_float(0, img_width, d);
|
||||
int y = (int)get_random_float(0, img_height, d);
|
||||
@@ -260,10 +250,12 @@ REGISTER_TEST(imagereadwrite)
|
||||
int set_input_pitch = (int)(genrand_int32(d) & 0x01);
|
||||
int packed_update = (int)(genrand_int32(d) & 0x01);
|
||||
void *p, *outp;
|
||||
std::unique_ptr<unsigned char[]> p_rgba8;
|
||||
std::unique_ptr<unsigned short[]> p_rgba16;
|
||||
std::unique_ptr<float[]> p_rgbaf;
|
||||
int elem_size;
|
||||
|
||||
if (j == 3)
|
||||
j = 0;
|
||||
if (j == image_formats_count) j = 0;
|
||||
|
||||
switch (j)
|
||||
{
|
||||
@@ -272,45 +264,57 @@ REGISTER_TEST(imagereadwrite)
|
||||
elem_size = 4;
|
||||
if(packed_update)
|
||||
{
|
||||
p = generate_rgba8_image(w, h, d);
|
||||
update_image_from_image(rgba8_inptr, p, x, y, w, h, img_width, elem_size);
|
||||
p_rgba8 = generate_rgba8_image(w, h, d);
|
||||
p = p_rgba8.get();
|
||||
update_image_from_image(rgba8_inptr.get(), p, x, y, w, h,
|
||||
img_width, elem_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
update_rgba8_image(rgba8_inptr, x, y, w, h, img_width, d);
|
||||
p = (void *)(rgba8_inptr + ((y * img_width + x) * 4));
|
||||
update_rgba8_image(rgba8_inptr.get(), x, y, w, h, img_width,
|
||||
d);
|
||||
p = static_cast<void *>(rgba8_inptr.get()
|
||||
+ ((y * img_width + x) * 4));
|
||||
}
|
||||
outp = (void *)rgba8_outptr;
|
||||
outp = static_cast<void *>(rgba8_outptr.get());
|
||||
break;
|
||||
case 1:
|
||||
//if ((w<=8) || (h<=8)) continue;
|
||||
elem_size = 2*4;
|
||||
if(packed_update)
|
||||
{
|
||||
p = generate_rgba16_image(w, h, d);
|
||||
update_image_from_image(rgba16_inptr, p, x, y, w, h, img_width, elem_size);
|
||||
p_rgba16 = generate_rgba16_image(w, h, d);
|
||||
p = p_rgba16.get();
|
||||
update_image_from_image(rgba16_inptr.get(), p, x, y, w, h,
|
||||
img_width, elem_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
update_rgba16_image(rgba16_inptr, x, y, w, h, img_width, d);
|
||||
p = (void *)(rgba16_inptr + ((y * img_width + x) * 4));
|
||||
update_rgba16_image(rgba16_inptr.get(), x, y, w, h,
|
||||
img_width, d);
|
||||
p = static_cast<void *>(rgba16_inptr.get()
|
||||
+ ((y * img_width + x) * 4));
|
||||
}
|
||||
outp = (void *)rgba16_outptr;
|
||||
outp = static_cast<void *>(rgba16_outptr.get());
|
||||
break;
|
||||
case 2:
|
||||
//if ((w<=8) || (h<=8)) continue;
|
||||
elem_size = 4*4;
|
||||
if(packed_update)
|
||||
{
|
||||
p = generate_rgbafp_image(w, h, d);
|
||||
update_image_from_image(rgbafp_inptr, p, x, y, w, h, img_width, elem_size);
|
||||
p_rgbaf = generate_rgbafp_image(w, h, d);
|
||||
p = p_rgbaf.get();
|
||||
update_image_from_image(rgbafp_inptr.get(), p, x, y, w, h,
|
||||
img_width, elem_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
update_rgbafp_image(rgbafp_inptr, x, y, w, h, img_width, d);
|
||||
p = (void *)(rgbafp_inptr + ((y * img_width + x) * 4));
|
||||
update_rgbafp_image(rgbafp_inptr.get(), x, y, w, h,
|
||||
img_width, d);
|
||||
p = static_cast<void *>(rgbafp_inptr.get()
|
||||
+ ((y * img_width + x) * 4));
|
||||
}
|
||||
outp = (void *)rgbafp_outptr;
|
||||
outp = static_cast<void *>(rgbafp_outptr.get());
|
||||
break;
|
||||
default:
|
||||
log_error("ERROR Invalid j = %d\n", j);
|
||||
@@ -358,8 +362,7 @@ REGISTER_TEST(imagereadwrite)
|
||||
|
||||
if(packed_update)
|
||||
{
|
||||
free(p);
|
||||
p = NULL;
|
||||
p = nullptr;
|
||||
}
|
||||
|
||||
memset(outp, 0x7, img_width*img_height*elem_size);
|
||||
@@ -379,7 +382,8 @@ REGISTER_TEST(imagereadwrite)
|
||||
switch (j)
|
||||
{
|
||||
case 0:
|
||||
err = verify_rgba8_image(rgba8_inptr, rgba8_outptr, img_width, img_height);
|
||||
err = verify_rgba8_image(rgba8_inptr.get(), rgba8_outptr.get(),
|
||||
img_width, img_height);
|
||||
if (err)
|
||||
{
|
||||
log_error("x=%d y=%d w=%d h=%d, pitch=%d, try=%d\n", x, y, w, h, (int)input_pitch, (int)i);
|
||||
@@ -387,7 +391,9 @@ REGISTER_TEST(imagereadwrite)
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
err = verify_rgba16_image(rgba16_inptr, rgba16_outptr, img_width, img_height);
|
||||
err =
|
||||
verify_rgba16_image(rgba16_inptr.get(), rgba16_outptr.get(),
|
||||
img_width, img_height);
|
||||
if (err)
|
||||
{
|
||||
log_error("x=%d y=%d w=%d h=%d, pitch=%d, try=%d\n", x, y, w, h, (int)input_pitch, (int)i);
|
||||
@@ -395,7 +401,9 @@ REGISTER_TEST(imagereadwrite)
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
err = verify_rgbafp_image(rgbafp_inptr, rgbafp_outptr, img_width, img_height);
|
||||
err =
|
||||
verify_rgbafp_image(rgbafp_inptr.get(), rgbafp_outptr.get(),
|
||||
img_width, img_height);
|
||||
if (err)
|
||||
{
|
||||
log_error("x=%d y=%d w=%d h=%d, pitch=%d, try=%d\n", x, y, w, h, (int)input_pitch, (int)i);
|
||||
@@ -407,14 +415,6 @@ REGISTER_TEST(imagereadwrite)
|
||||
if (err) break;
|
||||
}
|
||||
|
||||
free_mtdata(d);
|
||||
free(rgba8_inptr);
|
||||
free(rgba16_inptr);
|
||||
free(rgbafp_inptr);
|
||||
free(rgba8_outptr);
|
||||
free(rgba16_outptr);
|
||||
free(rgbafp_outptr);
|
||||
|
||||
if (!err)
|
||||
log_info("IMAGE read, write test passed\n");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user