Deduplicate write_image (#1536)

* Deduplicate write_image

Merge writeimage,writeimage_int16,writeimage_fp32
as they share a lot of common code.

Signed-off-by: John Kesapides <john.kesapides@arm.com>

* Test for CL_MEM_WRITE_ONLY and CL_MEM_READ_WRITE.

Signed-off-by: John Kesapides <john.kesapides@arm.com>

* Rename test_flags to img_flags

Signed-off-by: John Kesapides <john.kesapides@arm.com>

Signed-off-by: John Kesapides <john.kesapides@arm.com>
This commit is contained in:
John Kesapides
2022-11-15 17:11:37 +00:00
committed by GitHub
parent 524fcab7f3
commit 2110e45cce
4 changed files with 216 additions and 695 deletions

View File

@@ -7,7 +7,7 @@ set(${MODULE_NAME}_SOURCES
test_hiloeo.cpp test_local.cpp test_pointercast.cpp
test_if.cpp test_loop.cpp
test_readimage.cpp
test_writeimage.cpp test_writeimage_int16.cpp test_writeimage_fp32.cpp
test_writeimage.cpp
test_multireadimageonefmt.cpp test_multireadimagemultifmt.cpp
test_imagedim.cpp
test_vloadstore.cpp

View File

@@ -1,6 +1,6 @@
//
// Copyright (c) 2017 The Khronos Group Inc.
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
@@ -14,6 +14,7 @@
// limitations under the License.
//
#include "harness/compat.h"
#include "harness/imageHelpers.h"
#include <stdio.h>
#include <stdlib.h>
@@ -24,331 +25,237 @@
#include "procs.h"
static const char *bgra8888_write_kernel_code =
"\n"
"__kernel void test_bgra8888_write(__global unsigned char *src, write_only image2d_t dstimg)\n"
"{\n"
" int tid_x = get_global_id(0);\n"
" int tid_y = get_global_id(1);\n"
" int indx = tid_y * get_image_width(dstimg) + tid_x;\n"
" float4 color;\n"
"\n"
" indx *= 4;\n"
" color = (float4)((float)src[indx+2], (float)src[indx+1], (float)src[indx+0], (float)src[indx+3]);\n"
" color /= (float4)(255.0f, 255.0f, 255.0f, 255.0f);\n"
" write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n"
"\n"
"}\n";
#include <algorithm>
#include <string>
#include <vector>
#include "procs.h"
static const char *rgba8888_write_kernel_code =
"\n"
"__kernel void test_rgba8888_write(__global unsigned char *src, write_only image2d_t dstimg)\n"
"{\n"
" int tid_x = get_global_id(0);\n"
" int tid_y = get_global_id(1);\n"
" int indx = tid_y * get_image_width(dstimg) + tid_x;\n"
" float4 color;\n"
"\n"
" indx *= 4;\n"
" color = (float4)((float)src[indx+0], (float)src[indx+1], (float)src[indx+2], (float)src[indx+3]);\n"
" color /= (float4)(255.0f, 255.0f, 255.0f, 255.0f);\n"
" write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n"
"\n"
"}\n";
static unsigned char *
generate_8888_image(int w, int h, MTdata d)
namespace {
const char *kernel_source = R"(
__kernel void test_CL_BGRACL_UNORM_INT8(__global unsigned char *src, write_only image2d_t dstimg)
{
cl_uchar *ptr = (cl_uchar *)malloc(w * h * 4);
int i;
int tid_x = get_global_id(0);
int tid_y = get_global_id(1);
int indx = tid_y * get_image_width(dstimg) + tid_x;
float4 color;
for (i=0; i<w*h*4; i++)
ptr[i] = (cl_uchar)genrand_int32(d);
return ptr;
indx *= 4;
color = (float4)((float)src[indx+2], (float)src[indx+1], (float)src[indx+0], (float)src[indx+3]);
color /= (float4)(255.0f, 255.0f, 255.0f, 255.0f);
write_imagef(dstimg, (int2)(tid_x, tid_y), color);
}
static int
verify_bgra8888_image(unsigned char *image, unsigned char *outptr, int w, int h)
__kernel void test_CL_RGBACL_UNORM_INT8(__global unsigned char *src, write_only image2d_t dstimg)
{
int i;
int tid_x = get_global_id(0);
int tid_y = get_global_id(1);
int indx = tid_y * get_image_width(dstimg) + tid_x;
float4 color;
for (i=0; i<w*h*4; i++)
indx *= 4;
color = (float4)((float)src[indx+0], (float)src[indx+1], (float)src[indx+2], (float)src[indx+3]);
color /= (float4)(255.0f, 255.0f, 255.0f, 255.0f);
write_imagef(dstimg, (int2)(tid_x, tid_y), color);
}
__kernel void test_CL_RGBACL_UNORM_INT16(__global unsigned short *src, write_only image2d_t dstimg)
{
int tid_x = get_global_id(0);
int tid_y = get_global_id(1);
int indx = tid_y * get_image_width(dstimg) + tid_x;
float4 color;
indx *= 4;
color = (float4)((float)src[indx+0], (float)src[indx+1], (float)src[indx+2], (float)src[indx+3]);
color /= 65535.0f;
write_imagef(dstimg, (int2)(tid_x, tid_y), color);
}
__kernel void test_CL_RGBACL_FLOAT(__global float *src, write_only image2d_t dstimg)
{
int tid_x = get_global_id(0);
int tid_y = get_global_id(1);
int indx = tid_y * get_image_width(dstimg) + tid_x;
float4 color;
indx *= 4;
color = (float4)(src[indx+0], src[indx+1], src[indx+2], src[indx+3]);
write_imagef(dstimg, (int2)(tid_x, tid_y), color);
}
)";
template <typename T> void generate_random_inputs(std::vector<T> &v)
{
RandomSeed seed(gRandomSeed);
auto random_generator = [&seed]() {
return static_cast<T>(genrand_int32(seed));
};
std::generate(v.begin(), v.end(), random_generator);
}
template <> void generate_random_inputs<float>(std::vector<float> &v)
{
RandomSeed seed(gRandomSeed);
auto random_generator = [&seed]() {
return get_random_float(-0x40000000, 0x40000000, seed);
};
std::generate(v.begin(), v.end(), random_generator);
}
const char *get_mem_flag_name(cl_mem_flags flags)
{
switch (flags)
{
if (outptr[i] != image[i])
{
log_error("WRITE_IMAGE_BGRA_UNORM_INT8 test failed\n");
return -1;
}
case CL_MEM_READ_WRITE: return "CL_MEM_READ_WRITE";
case CL_MEM_WRITE_ONLY: return "CL_MEM_WRITE_ONLY";
default: return "Unsupported cl_mem_flags value";
}
log_info("WRITE_IMAGE_BGRA_UNORM_INT8 test passed\n");
return 0;
}
static int
verify_rgba8888_image(unsigned char *image, unsigned char *outptr, int w, int h)
template <typename T>
int test_writeimage(cl_device_id device, cl_context context,
cl_command_queue queue, const cl_image_format *img_format,
cl_mem_flags img_flags)
{
int i;
clMemWrapper streams[2];
clProgramWrapper program;
clKernelWrapper kernel;
for (i=0; i<w*h*4; i++)
std::string kernel_name("test_");
size_t img_width = 512;
size_t img_height = 512;
int err;
const size_t origin[3] = { 0, 0, 0 };
const size_t region[3] = { img_width, img_height, 1 };
const size_t num_elements = img_width * img_height * 4;
const size_t length = num_elements * sizeof(T);
PASSIVE_REQUIRE_IMAGE_SUPPORT(device)
std::vector<T> input(num_elements);
std::vector<T> output(num_elements);
generate_random_inputs(input);
streams[0] = create_image_2d(context, img_flags, img_format, img_width,
img_height, 0, nullptr, &err);
test_error(err, "create_image failed.");
streams[1] =
clCreateBuffer(context, CL_MEM_READ_WRITE, length, nullptr, &err);
test_error(err, "clCreateBuffer failed.");
err = clEnqueueWriteBuffer(queue, streams[1], CL_TRUE, 0, length,
input.data(), 0, nullptr, nullptr);
test_error(err, "clEnqueueWriteImage failed.");
kernel_name += GetChannelOrderName(img_format->image_channel_order);
kernel_name += GetChannelTypeName(img_format->image_channel_data_type);
err = create_single_kernel_helper(context, &program, &kernel, 1,
&kernel_source, kernel_name.c_str());
test_error(err, "create_single_kernel_helper failed.");
err |= clSetKernelArg(kernel, 0, sizeof(streams[1]), &streams[1]);
err |= clSetKernelArg(kernel, 1, sizeof(streams[0]), &streams[0]);
test_error(err, "clSetKernelArgs failed\n");
size_t threads[] = { img_width, img_height };
err = clEnqueueNDRangeKernel(queue, kernel, 2, nullptr, threads, nullptr, 0,
nullptr, nullptr);
test_error(err, "clEnqueueNDRangeKernel failed\n");
err = clEnqueueReadImage(queue, streams[0], CL_TRUE, origin, region, 0, 0,
output.data(), 0, nullptr, nullptr);
if (0 != memcmp(input.data(), output.data(), length))
{
if (outptr[i] != image[i])
{
log_error("WRITE_IMAGE_RGBA_UNORM_INT8 test failed\n");
return -1;
}
}
log_info("WRITE_IMAGE_RGBA_UNORM_INT8 test passed\n");
return 0;
}
int test_writeimage(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
{
cl_mem streams[6];
cl_program program[2];
cl_kernel kernel[4];
unsigned char *input_ptr[2], *output_ptr;
cl_image_format img_format;
cl_image_format *supported_formats;
size_t threads[2];
int img_width = 512;
int img_height = 512;
int i, err, any_err = 0;
size_t origin[3] = {0, 0, 0};
size_t region[3] = {img_width, img_height, 1};
size_t length = img_width * img_height * 4 * sizeof(unsigned char);
int supportsBGRA = 0;
cl_uint numFormats = 0;
PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
MTdata d = init_genrand( gRandomSeed );
input_ptr[0] = generate_8888_image(img_width, img_height, d);
input_ptr[1] = generate_8888_image(img_width, img_height, d);
free_mtdata(d); d = NULL;
output_ptr = (unsigned char*)malloc(length);
if(gIsEmbedded)
{
/* Get the supported image formats to see if BGRA is supported */
clGetSupportedImageFormats (context, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 0, NULL, &numFormats);
supported_formats = (cl_image_format *) malloc(sizeof(cl_image_format) * numFormats);
clGetSupportedImageFormats (context, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, numFormats, supported_formats, NULL);
for(i = 0; i < numFormats; i++)
{
if(supported_formats[i].image_channel_order == CL_BGRA)
{
supportsBGRA = 1;
break;
}
}
log_error("WRITE_IMAGE_%s_%s with %s test failed\n",
GetChannelOrderName(img_format->image_channel_order),
GetChannelTypeName(img_format->image_channel_data_type),
get_mem_flag_name(img_flags));
err = -1;
}
else
{
supportsBGRA = 1;
log_info("WRITE_IMAGE_%s_%s with %s test passed\n",
GetChannelOrderName(img_format->image_channel_order),
GetChannelTypeName(img_format->image_channel_data_type),
get_mem_flag_name(img_flags));
}
if(supportsBGRA)
{
img_format.image_channel_order = CL_BGRA;
img_format.image_channel_data_type = CL_UNORM_INT8;
streams[0] = clCreateImage2D(context, CL_MEM_READ_WRITE, &img_format, img_width, img_height, 0, NULL, NULL);
if (!streams[0])
{
log_error("clCreateImage2D failed\n");
return -1;
}
}
img_format.image_channel_order = CL_RGBA;
img_format.image_channel_data_type = CL_UNORM_INT8;
streams[1] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, img_width, img_height, 0, NULL, NULL);
if (!streams[1])
{
log_error("create_image_2d failed\n");
return -1;
}
if(supportsBGRA)
{
img_format.image_channel_order = CL_BGRA;
img_format.image_channel_data_type = CL_UNORM_INT8;
streams[2] = clCreateImage2D(context, CL_MEM_WRITE_ONLY, &img_format, img_width, img_height, 0, NULL, NULL);
if (!streams[2])
{
log_error("clCreateImage2D failed\n");
return -1;
}
}
img_format.image_channel_order = CL_RGBA;
img_format.image_channel_data_type = CL_UNORM_INT8;
streams[3] = create_image_2d(context, CL_MEM_WRITE_ONLY, &img_format, img_width, img_height, 0, NULL, NULL);
if (!streams[3])
{
log_error("create_image_2d failed\n");
return -1;
}
streams[4] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
if (!streams[4])
{
log_error("clCreateBuffer failed\n");
return -1;
}
streams[5] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
if (!streams[5])
{
log_error("clCreateBuffer failed\n");
return -1;
}
err = clEnqueueWriteBuffer(queue, streams[4], CL_TRUE, 0, length, input_ptr[0], 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clEnqueueWriteBuffer failed\n");
return -1;
}
err = clEnqueueWriteBuffer(queue, streams[5], CL_TRUE, 0, length, input_ptr[1], 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clEnqueueWriteBuffer failed\n");
return -1;
}
if(supportsBGRA)
{
err = create_single_kernel_helper(context, &program[0], &kernel[0], 1, &bgra8888_write_kernel_code, "test_bgra8888_write" );
if (err)
return -1;
kernel[2] = clCreateKernel(program[0], "test_bgra8888_write", NULL);
if (!kernel[2])
{
log_error("clCreateKernel failed\n");
return -1;
}
}
err = create_single_kernel_helper(context, &program[1], &kernel[1], 1, &rgba8888_write_kernel_code, "test_rgba8888_write" );
if (err)
return -1;
kernel[3] = clCreateKernel(program[1], "test_rgba8888_write", NULL);
if (!kernel[3])
{
log_error("clCreateKernel failed\n");
return -1;
}
if(supportsBGRA)
{
err = clSetKernelArg(kernel[0], 0, sizeof streams[4], &streams[4]);
err |= clSetKernelArg(kernel[0], 1, sizeof streams[0], &streams[0]);
if (err != CL_SUCCESS)
{
log_error("clSetKernelArgs failed\n");
return -1;
}
}
err = clSetKernelArg(kernel[1], 0, sizeof streams[5], &streams[5]);
err |= clSetKernelArg(kernel[1], 1, sizeof streams[1], &streams[1]);
if (err != CL_SUCCESS)
{
log_error("clSetKernelArgs failed\n");
return -1;
}
if(supportsBGRA)
{
err = clSetKernelArg(kernel[2], 0, sizeof streams[4], &streams[4]);
err |= clSetKernelArg(kernel[2], 1, sizeof streams[2], &streams[2]);
if (err != CL_SUCCESS)
{
log_error("clSetKernelArgs failed\n");
return -1;
}
}
err = clSetKernelArg(kernel[3], 0, sizeof streams[5], &streams[5]);
err |= clSetKernelArg(kernel[3], 1, sizeof streams[3], &streams[3]);
if (err != CL_SUCCESS)
{
log_error("clSetKernelArgs failed\n");
return -1;
}
threads[0] = (unsigned int)img_width;
threads[1] = (unsigned int)img_height;
for (i=0; i<4; i++)
{
if(!supportsBGRA && (i == 0 || i == 2))
continue;
err = clEnqueueNDRangeKernel(queue, kernel[i], 2, NULL, threads, NULL, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clEnqueueNDRangeKernel failed\n");
return -1;
}
err = clEnqueueReadImage(queue, streams[i], CL_TRUE, origin, region, 0, 0, output_ptr, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clReadImage failed\n");
return -1;
}
switch (i)
{
case 0:
case 2:
err = verify_bgra8888_image(input_ptr[i&0x01], output_ptr, img_width, img_height);
break;
case 1:
case 3:
err = verify_rgba8888_image(input_ptr[i&0x01], output_ptr, img_width, img_height);
break;
}
//if (err)
//break;
any_err |= err;
}
// cleanup
if(supportsBGRA)
clReleaseMemObject(streams[0]);
clReleaseMemObject(streams[1]);
if(supportsBGRA)
clReleaseMemObject(streams[2]);
clReleaseMemObject(streams[3]);
clReleaseMemObject(streams[4]);
clReleaseMemObject(streams[5]);
for (i=0; i<2; i++)
{
if(i == 0 && !supportsBGRA)
continue;
clReleaseKernel(kernel[i]);
clReleaseKernel(kernel[i+2]);
clReleaseProgram(program[i]);
}
free(input_ptr[0]);
free(input_ptr[1]);
free(output_ptr);
return any_err;
return err;
}
bool check_format(cl_device_id device, cl_context context,
cl_mem_object_type image_type,
const cl_image_format img_format, cl_mem_flags test_flags)
{
return is_image_format_required(img_format, test_flags, image_type, device)
|| is_image_format_supported(context, test_flags, image_type,
&img_format);
}
}
int test_writeimage(cl_device_id device, cl_context context,
cl_command_queue queue, int num_elements)
{
int err = 0;
const cl_image_format format[] = { { CL_RGBA, CL_UNORM_INT8 },
{ CL_BGRA, CL_UNORM_INT8 } };
const cl_mem_flags test_flags[] = { CL_MEM_WRITE_ONLY, CL_MEM_READ_WRITE };
for (size_t i = 0; i < ARRAY_SIZE(test_flags) && !err; i++)
{
err = test_writeimage<cl_uchar>(device, context, queue, &format[0],
test_flags[i]);
if (check_format(device, context, CL_MEM_OBJECT_IMAGE2D, format[1],
test_flags[i]))
{
err |= test_writeimage<cl_uchar>(device, context, queue, &format[1],
test_flags[i]);
}
}
return err;
}
int test_writeimage_int16(cl_device_id device, cl_context context,
cl_command_queue queue, int num_elements)
{
int err = 0;
const cl_image_format format = { CL_RGBA, CL_UNORM_INT16 };
const cl_mem_flags test_flags[] = { CL_MEM_WRITE_ONLY, CL_MEM_READ_WRITE };
for (size_t i = 0; i < ARRAY_SIZE(test_flags) && !err; i++)
{
err = test_writeimage<cl_ushort>(device, context, queue, &format,
test_flags[i]);
}
return err;
}
int test_writeimage_fp32(cl_device_id device, cl_context context,
cl_command_queue queue, int num_elements)
{
int err = 0;
const cl_image_format format = { CL_RGBA, CL_FLOAT };
const cl_mem_flags test_flags[] = { CL_MEM_WRITE_ONLY, CL_MEM_READ_WRITE };
for (size_t i = 0; i < ARRAY_SIZE(test_flags) && !err; i++)
{
err = test_writeimage<cl_float>(device, context, queue, &format,
test_flags[i]);
}
return err;
}

View File

@@ -1,190 +0,0 @@
//
// Copyright (c) 2017 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "harness/compat.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "procs.h"
static const char *rgbaFFFF_write_kernel_code =
"__kernel void test_rgbaFFFF_write(__global float *src, write_only image2d_t dstimg)\n"
"{\n"
" int tid_x = get_global_id(0);\n"
" int tid_y = get_global_id(1);\n"
" int indx = tid_y * get_image_width(dstimg) + tid_x;\n"
" float4 color;\n"
"\n"
" indx *= 4;\n"
" color = (float4)(src[indx+0], src[indx+1], src[indx+2], src[indx+3]);\n"
" write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n"
"\n"
"}\n";
static float *
generate_float_image(int w, int h, MTdata d)
{
float *ptr = (float*)malloc(w * h * 4 * sizeof(float));
int i;
for (i=0; i<w*h*4; i++)
ptr[i] = get_random_float(-0x40000000, 0x40000000, d);
return ptr;
}
static int
verify_float_image(const char *string, float *image, float *outptr, int w, int h)
{
int i;
for (i=0; i<w*h*4; i++)
{
if (outptr[i] != image[i])
{
log_error("%s failed\n", string);
return -1;
}
}
log_info("%s passed\n", string);
return 0;
}
int test_writeimage_fp32(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
{
cl_mem streams[3];
cl_program program;
cl_kernel kernel[2];
cl_image_format img_format;
float *input_ptr, *output_ptr;
size_t threads[2];
int img_width = 512;
int img_height = 512;
int i, err, any_err = 0;
size_t origin[3] = {0, 0, 0};
size_t region[3] = {img_width, img_height, 1};
size_t length = img_width * img_height * 4 * sizeof(float);
MTdata d;
PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
d = init_genrand( gRandomSeed );
input_ptr = generate_float_image(img_width, img_height, d);
free_mtdata(d); d = NULL;
output_ptr = (float*)malloc(length);
img_format.image_channel_order = CL_RGBA;
img_format.image_channel_data_type = CL_FLOAT;
streams[0] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, img_width, img_height, 0, NULL, NULL);
if (!streams[0])
{
log_error("create_image_2d failed\n");
return -1;
}
streams[1] = create_image_2d(context, CL_MEM_WRITE_ONLY, &img_format, img_width, img_height, 0, NULL, NULL);
if (!streams[1])
{
log_error("create_image_2d failed\n");
return -1;
}
streams[2] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
if (!streams[2])
{
log_error("clCreateArray failed\n");
return -1;
}
err = clEnqueueWriteBuffer(queue, streams[2], CL_TRUE, 0, length, input_ptr, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clEnqueueWriteBuffer failed\n");
return -1;
}
err = create_single_kernel_helper(context, &program, &kernel[0], 1,
&rgbaFFFF_write_kernel_code,
"test_rgbaFFFF_write");
if (err) return -1;
kernel[1] = clCreateKernel(program, "test_rgbaFFFF_write", NULL);
if (!kernel[1])
{
log_error("clCreateKernel failed\n");
return -1;
}
err = clSetKernelArg(kernel[0], 0, sizeof streams[2], &streams[2]);
err |= clSetKernelArg(kernel[0], 1, sizeof streams[0], &streams[0]);
if (err != CL_SUCCESS)
{
log_error("clSetKernelArgs failed\n");
return -1;
}
err = clSetKernelArg(kernel[1], 0, sizeof streams[2], &streams[2]);
err |= clSetKernelArg(kernel[1], 1, sizeof streams[1], &streams[1]);
if (err != CL_SUCCESS)
{
log_error("clSetKernelArgs failed\n");
return -1;
}
threads[0] = (unsigned int)img_width;
threads[1] = (unsigned int)img_height;
for (i=0; i<2; i++)
{
err = clEnqueueNDRangeKernel(queue, kernel[i], 2, NULL, threads, NULL, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clExecuteKernel failed\n");
return -1;
}
err = clEnqueueReadImage(queue, streams[i], CL_TRUE, origin, region, 0, 0, output_ptr, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clReadImage failed\n");
return -1;
}
err = verify_float_image((i == 0) ? "WRITE_IMAGE_RGBA_FLOAT test with memflags = CL_MEM_READ_WRITE" :
"WRITE_IMAGE_RGBA_FLOAT test with memflags = CL_MEM_WRITE_ONLY",
input_ptr, output_ptr, img_width, img_height);
any_err |= err;
}
// cleanup
clReleaseMemObject(streams[0]);
clReleaseMemObject(streams[1]);
clReleaseMemObject(streams[2]);
clReleaseKernel(kernel[0]);
clReleaseKernel(kernel[1]);
clReleaseProgram(program);
free(input_ptr);
free(output_ptr);
return any_err;
}

View File

@@ -1,196 +0,0 @@
//
// Copyright (c) 2017 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "harness/compat.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "procs.h"
static const char *rgba16_write_kernel_code =
"__kernel void test_rgba16_write(__global unsigned short *src, write_only image2d_t dstimg)\n"
"{\n"
" int tid_x = get_global_id(0);\n"
" int tid_y = get_global_id(1);\n"
" int indx = tid_y * get_image_width(dstimg) + tid_x;\n"
" float4 color;\n"
"\n"
" indx *= 4;\n"
" color = (float4)((float)src[indx+0], (float)src[indx+1], (float)src[indx+2], (float)src[indx+3]);\n"
" color /= 65535.0f;\n"
" write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n"
"\n"
"}\n";
static unsigned short *
generate_16bit_image(int w, int h, MTdata d)
{
cl_ushort *ptr = (cl_ushort*)malloc(w * h * 4 * sizeof(cl_ushort));
int i;
for (i=0; i<w*h*4; i++)
ptr[i] = (cl_ushort)genrand_int32(d);
return ptr;
}
// normalized 16bit ints ... get dived by 64k then muled by 64k...
// give the poor things some tolerance
#define MAX_ERR 1
static int
verify_16bit_image(const char *string, cl_ushort *image, cl_ushort *outptr, int w, int h)
{
int i;
for (i=0; i<w*h*4; i++)
{
if (abs(outptr[i] - image[i]) > MAX_ERR)
{
log_error("%s failed\n", string);
return -1;
}
}
log_info("%s passed\n", string);
return 0;
}
int test_writeimage_int16(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
{
cl_mem streams[3];
cl_program program;
cl_kernel kernel[2];
cl_image_format img_format;
cl_ushort *input_ptr, *output_ptr;
size_t threads[2];
int img_width = 512;
int img_height = 512;
int i, err, any_err = 0;
size_t origin[3] = {0, 0, 0};
size_t region[3] = {img_width, img_height, 1};
size_t length = img_width * img_height * 4 * sizeof(cl_ushort);
PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
MTdata d = init_genrand( gRandomSeed );
input_ptr = generate_16bit_image(img_width, img_height, d);
free_mtdata(d); d = NULL;
output_ptr = (cl_ushort*)malloc(length);
img_format.image_channel_order = CL_RGBA;
img_format.image_channel_data_type = CL_UNORM_INT16;
streams[0] = create_image_2d(context, CL_MEM_READ_WRITE, &img_format, img_width, img_height, 0, NULL, NULL);
if (!streams[0])
{
log_error("create_image_2d failed\n");
return -1;
}
img_format.image_channel_order = CL_RGBA;
img_format.image_channel_data_type = CL_UNORM_INT16;
streams[1] = create_image_2d(context, CL_MEM_WRITE_ONLY, &img_format, img_width, img_height, 0, NULL, NULL);
if (!streams[1])
{
log_error("create_image_2d failed\n");
return -1;
}
streams[2] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
if (!streams[2])
{
log_error("clCreateArray failed\n");
return -1;
}
err = clEnqueueWriteBuffer(queue, streams[2], CL_TRUE, 0, length, input_ptr, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clEnqueueWriteBuffer failed\n");
return -1;
}
err = create_single_kernel_helper(context, &program, &kernel[0], 1,
&rgba16_write_kernel_code,
"test_rgba16_write");
if (err) return -1;
kernel[1] = clCreateKernel(program, "test_rgba16_write", NULL);
if (!kernel[1])
{
log_error("clCreateKernel failed\n");
return -1;
}
err = clSetKernelArg(kernel[0], 0, sizeof streams[2], &streams[2]);
err |= clSetKernelArg(kernel[0], 1, sizeof streams[0], &streams[0]);
if (err != CL_SUCCESS)
{
log_error("clSetKernelArgs failed\n");
return -1;
}
err = clSetKernelArg(kernel[1], 0, sizeof streams[2], &streams[2]);
err |= clSetKernelArg(kernel[1], 1, sizeof streams[1], &streams[1]);
if (err != CL_SUCCESS)
{
log_error("clSetKernelArgs failed\n");
return -1;
}
threads[0] = (unsigned int)img_width;
threads[1] = (unsigned int)img_height;
for (i=0; i<2; i++)
{
err = clEnqueueNDRangeKernel(queue, kernel[i], 2, NULL, threads, NULL, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clExecuteKernel failed\n");
return -1;
}
err = clEnqueueReadImage(queue, streams[i], CL_TRUE, origin, region, 0, 0, output_ptr, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clReadImage failed\n");
return -1;
}
err = verify_16bit_image((i == 0) ? "WRITE_IMAGE_RGBA_UNORM_INT16 test with memflags = CL_MEM_READ_WRITE" :
"WRITE_IMAGE_RGBA_UNORM_INT16 test with memflags = CL_MEM_WRITE_ONLY",
input_ptr, output_ptr, img_width, img_height);
any_err |= err;
}
// cleanup
clReleaseMemObject(streams[0]);
clReleaseMemObject(streams[1]);
clReleaseMemObject(streams[2]);
clReleaseKernel(kernel[0]);
clReleaseKernel(kernel[1]);
clReleaseProgram(program);
free(input_ptr);
free(output_ptr);
return any_err;
}