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_arrayimagecopy3d_single_format(cl_device_id device,cl_context context,cl_command_queue queue,cl_image_format * format)26 int test_arrayimagecopy3d_single_format(cl_device_id device, cl_context context, cl_command_queue queue, cl_image_format *format)
27 {
28 cl_uchar *bufptr, *imgptr;
29 clMemWrapper buffer, image;
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 cl_event copyevent;
39
40 log_info("Testing %s %s\n", GetChannelOrderName(format->image_channel_order), GetChannelTypeName(format->image_channel_data_type));
41
42 image = create_image_3d(context, CL_MEM_READ_ONLY, format, img_width,
43 img_height, img_depth, 0, 0, NULL, &err);
44 test_error(err, "create_image_3d failed");
45
46 err = clGetImageInfo(image, CL_IMAGE_ELEMENT_SIZE, sizeof(size_t), &elem_size, NULL);
47 test_error(err, "clGetImageInfo failed");
48
49 buffer_size = sizeof(cl_uchar) * elem_size * img_width * img_height * img_depth;
50
51 buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, buffer_size, NULL, &err);
52 test_error(err, "clCreateBuffer failed");
53
54 d = init_genrand( gRandomSeed );
55 bufptr = (cl_uchar*)malloc(buffer_size);
56 for (i=0; i<(int)buffer_size; i++) {
57 bufptr[i] = (cl_uchar)genrand_int32(d);
58 }
59 free_mtdata(d); d = NULL;
60
61 size_t origin[3]={0,0,0}, region[3]={img_width,img_height,img_depth};
62 err = clEnqueueWriteBuffer( queue, buffer, CL_TRUE, 0, buffer_size, bufptr, 0, NULL, NULL);
63 test_error(err, "clEnqueueWriteBuffer failed");
64
65 err = clEnqueueCopyBufferToImage( queue, buffer, image, 0, origin, region, 0, NULL, ©event );
66 test_error(err, "clEnqueueCopyImageToBuffer failed");
67
68 imgptr = (cl_uchar*)malloc(buffer_size);
69
70 err = clEnqueueReadImage( queue, image, CL_TRUE, origin, region, 0, 0, imgptr, 1, ©event, NULL );
71 test_error(err, "clEnqueueReadBuffer failed");
72
73 err = clReleaseEvent(copyevent);
74 test_error(err, "clReleaseEvent failed");
75
76 if (memcmp(bufptr, imgptr, buffer_size) != 0) {
77 log_error( "ERROR: Results did not validate!\n" );
78 unsigned char * inchar = (unsigned char*)bufptr;
79 unsigned char * outchar = (unsigned char*)imgptr;
80 int failuresPrinted = 0;
81 int i;
82 for (i=0; i< (int)buffer_size; i+=(int)elem_size) {
83 int failed = 0;
84 int j;
85 for (j=0; j<(int)elem_size; j++)
86 if (inchar[i+j] != outchar[i+j])
87 failed = 1;
88 char values[4096];
89 values[0] = 0;
90 if (failed) {
91 sprintf(values + strlen(values), "%d(0x%x) -> actual [", i, i);
92 int j;
93 for (j=0; j<(int)elem_size; j++)
94 sprintf(values + strlen( values), "0x%02x ", inchar[i+j]);
95 sprintf(values + strlen(values), "] != expected [");
96 for (j=0; j<(int)elem_size; j++)
97 sprintf(values + strlen( values), "0x%02x ", outchar[i+j]);
98 sprintf(values + strlen(values), "]");
99 log_error("%s\n", values);
100 failuresPrinted++;
101 }
102 if (failuresPrinted > 5) {
103 log_error("Not printing further failures...\n");
104 break;
105 }
106 }
107 err = -1;
108 }
109
110 free(bufptr);
111 free(imgptr);
112
113 if (err)
114 log_error("ARRAY to IMAGE3D copy test failed for image_channel_order=0x%lx and image_channel_data_type=0x%lx\n",
115 (unsigned long)format->image_channel_order, (unsigned long)format->image_channel_data_type);
116
117 return err;
118 }
119
test_arrayimagecopy3d(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)120 int test_arrayimagecopy3d(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
121 {
122 cl_int err;
123 cl_image_format *formats;
124 cl_uint num_formats;
125 cl_uint i;
126
127 PASSIVE_REQUIRE_3D_IMAGE_SUPPORT( device )
128
129 err = clGetSupportedImageFormats(
130 context, CL_MEM_READ_ONLY, CL_MEM_OBJECT_IMAGE3D, 0, NULL, &num_formats);
131 test_error(err, "clGetSupportedImageFormats failed");
132
133 formats = (cl_image_format *)malloc(num_formats * sizeof(cl_image_format));
134
135 err = clGetSupportedImageFormats(context, CL_MEM_READ_ONLY,
136 CL_MEM_OBJECT_IMAGE3D, num_formats, formats,
137 NULL);
138 test_error(err, "clGetSupportedImageFormats failed");
139
140 for (i = 0; i < num_formats; i++) {
141 err |= test_arrayimagecopy3d_single_format(device, context, queue, &formats[i]);
142 }
143
144 free(formats);
145 if (err)
146 log_error("ARRAY to IMAGE3D copy test failed\n");
147 else
148 log_info("ARRAY to IMAGE3D copy test passed\n");
149
150 return err;
151 }
152