1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "harness/compat.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #include "procs.h"
25
test_imagearraycopy3d_single_format(cl_device_id device,cl_context context,cl_command_queue queue,cl_image_format * format)26 int test_imagearraycopy3d_single_format(cl_device_id device, cl_context context, cl_command_queue queue, cl_image_format *format)
27 {
28 cl_uchar *imgptr, *bufptr;
29 clMemWrapper image, buffer;
30 int img_width = 128;
31 int img_height = 128;
32 int img_depth = 32;
33 size_t elem_size;
34 size_t buffer_size;
35 int i;
36 cl_int err;
37 MTdata d;
38
39 log_info("Testing %s %s\n", GetChannelOrderName(format->image_channel_order), GetChannelTypeName(format->image_channel_data_type));
40
41 image = create_image_3d(context, CL_MEM_READ_ONLY, format, img_width,
42 img_height, img_depth, 0, 0, NULL, &err);
43 test_error(err, "create_image_3d failed");
44
45 err = clGetImageInfo(image, CL_IMAGE_ELEMENT_SIZE, sizeof(size_t), &elem_size, NULL);
46 test_error(err, "clGetImageInfo failed");
47
48 buffer_size = sizeof(cl_uchar) * elem_size * img_width * img_height * img_depth;
49
50 buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, buffer_size, NULL, &err);
51 test_error(err, "clCreateBuffer failed");
52
53 d = init_genrand( gRandomSeed );
54 imgptr = (cl_uchar*)malloc(buffer_size);
55 for (i=0; i<(int)buffer_size; i++) {
56 imgptr[i] = (cl_uchar)genrand_int32(d);
57 }
58 free_mtdata(d); d = NULL;
59
60 size_t origin[3]={0,0,0}, region[3]={img_width,img_height,img_depth};
61 err = clEnqueueWriteImage( queue, image, CL_TRUE, origin, region, 0, 0, imgptr, 0, NULL, NULL );
62 test_error(err, "clEnqueueWriteBuffer failed");
63
64 err = clEnqueueCopyImageToBuffer( queue, image, buffer, origin, region, 0, 0, NULL, NULL );
65 test_error(err, "clEnqueueCopyImageToBuffer failed");
66
67 bufptr = (cl_uchar*)malloc(buffer_size);
68
69 err = clEnqueueReadBuffer( queue, buffer, CL_TRUE, 0, buffer_size, bufptr, 0, NULL, NULL);
70 test_error(err, "clEnqueueReadBuffer failed");
71
72 if (memcmp(imgptr, bufptr, buffer_size) != 0) {
73 log_error( "ERROR: Results did not validate!\n" );
74 unsigned char * inchar = (unsigned char*)imgptr;
75 unsigned char * outchar = (unsigned char*)bufptr;
76 int failuresPrinted = 0;
77 int i;
78 for (i=0; i< (int)buffer_size; i+=(int)elem_size) {
79 int failed = 0;
80 int j;
81 for (j=0; j<(int)elem_size; j++)
82 if (inchar[i+j] != outchar[i+j])
83 failed = 1;
84 char values[4096];
85 values[0] = 0;
86 if (failed) {
87 sprintf(values + strlen(values), "%d(0x%x) -> actual [", i, i);
88 int j;
89 for (j=0; j<(int)elem_size; j++)
90 sprintf(values + strlen( values), "0x%02x ", inchar[i+j]);
91 sprintf(values + strlen(values), "] != expected [");
92 for (j=0; j<(int)elem_size; j++)
93 sprintf(values + strlen( values), "0x%02x ", outchar[i+j]);
94 sprintf(values + strlen(values), "]");
95 log_error("%s\n", values);
96 failuresPrinted++;
97 }
98 if (failuresPrinted > 5) {
99 log_error("Not printing further failures...\n");
100 break;
101 }
102 }
103 err = -1;
104 }
105
106 free(imgptr);
107 free(bufptr);
108
109 if (err)
110 log_error("IMAGE3D to ARRAY copy test failed for image_channel_order=0x%lx and image_channel_data_type=0x%lx\n",
111 (unsigned long)format->image_channel_order, (unsigned long)format->image_channel_data_type);
112
113 return err;
114 }
115
test_imagearraycopy3d(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)116 int test_imagearraycopy3d(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
117 {
118 cl_int err;
119 cl_image_format *formats;
120 cl_uint num_formats;
121 cl_uint i;
122
123 PASSIVE_REQUIRE_3D_IMAGE_SUPPORT( device )
124
125 err = clGetSupportedImageFormats(
126 context, CL_MEM_READ_ONLY, CL_MEM_OBJECT_IMAGE3D, 0, NULL, &num_formats);
127 test_error(err, "clGetSupportedImageFormats failed");
128
129 formats = (cl_image_format *)malloc(num_formats * sizeof(cl_image_format));
130
131 err = clGetSupportedImageFormats(context, CL_MEM_READ_ONLY,
132 CL_MEM_OBJECT_IMAGE3D, num_formats, formats,
133 NULL);
134 test_error(err, "clGetSupportedImageFormats failed");
135
136 for (i = 0; i < num_formats; i++) {
137 err |= test_imagearraycopy3d_single_format(device, context, queue, &formats[i]);
138 }
139
140 free(formats);
141 if (err)
142 log_error("IMAGE3D to ARRAY copy test failed\n");
143 else
144 log_info("IMAGE3D to ARRAY copy test passed\n");
145
146 return err;
147 }
148