• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_arrayimagecopy_single_format(cl_device_id device,cl_context context,cl_command_queue queue,cl_image_format * format)26 int test_arrayimagecopy_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 = 512;
31   int        img_height = 512;
32   size_t    elem_size;
33   size_t    buffer_size;
34   int        i;
35   cl_int          err;
36   MTdata          d;
37   cl_event  copyevent;
38 
39   log_info("Testing %s %s\n", GetChannelOrderName(format->image_channel_order), GetChannelTypeName(format->image_channel_data_type));
40 
41   image = create_image_2d(context, CL_MEM_READ_WRITE, format, img_width,
42                           img_height, 0, NULL, &err);
43   test_error(err, "create_image_2d 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;
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   bufptr = (cl_uchar*)malloc(buffer_size);
55   for (i=0; i<(int)buffer_size; i++) {
56      bufptr[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,1};
61   err = clEnqueueWriteBuffer( queue, buffer, CL_TRUE, 0, buffer_size, bufptr, 0, NULL, NULL);
62   test_error(err, "clEnqueueWriteBuffer failed");
63 
64   err = clEnqueueCopyBufferToImage( queue, buffer, image, 0, origin, region, 0, NULL, &copyevent );
65   test_error(err, "clEnqueueCopyImageToBuffer failed");
66 
67   imgptr = (cl_uchar*)malloc(buffer_size);
68 
69   err = clEnqueueReadImage( queue, image, CL_TRUE, origin, region, 0, 0, imgptr, 1, &copyevent, NULL );
70   test_error(err, "clEnqueueReadBuffer failed");
71 
72   err = clReleaseEvent(copyevent);
73   test_error(err, "clReleaseEvent failed");
74 
75   if (memcmp(bufptr, imgptr, buffer_size) != 0) {
76     log_error( "ERROR: Results did not validate!\n" );
77     unsigned char * inchar = (unsigned char*)bufptr;
78     unsigned char * outchar = (unsigned char*)imgptr;
79     int failuresPrinted = 0;
80     int i;
81     for (i=0; i< (int)buffer_size; i+=(int)elem_size) {
82         int failed = 0;
83         int j;
84         for (j=0; j<(int)elem_size; j++)
85             if (inchar[i+j] != outchar[i+j])
86                 failed = 1;
87         char values[4096];
88         values[0] = 0;
89         if (failed) {
90             sprintf(values + strlen(values), "%d(0x%x) -> actual [", i, i);
91             int j;
92             for (j=0; j<(int)elem_size; j++)
93                 sprintf(values + strlen( values), "0x%02x ", inchar[i+j]);
94             sprintf(values + strlen(values), "] != expected [");
95             for (j=0; j<(int)elem_size; j++)
96                 sprintf(values + strlen( values), "0x%02x ", outchar[i+j]);
97             sprintf(values + strlen(values), "]");
98             log_error("%s\n", values);
99             failuresPrinted++;
100         }
101         if (failuresPrinted > 5) {
102             log_error("Not printing further failures...\n");
103             break;
104         }
105     }
106     err = -1;
107   }
108 
109   free(bufptr);
110   free(imgptr);
111 
112   if (err)
113     log_error("ARRAY to IMAGE copy test failed for image_channel_order=0x%lx and image_channel_data_type=0x%lx\n",
114               (unsigned long)format->image_channel_order, (unsigned long)format->image_channel_data_type);
115 
116   return err;
117 }
118 
test_arrayimagecopy(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)119 int test_arrayimagecopy(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
120 {
121   cl_int          err;
122   cl_image_format *formats;
123   cl_uint         num_formats;
124   cl_uint         i;
125 
126   PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
127 
128   err = clGetSupportedImageFormats(context, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 0, NULL, &num_formats);
129   test_error(err, "clGetSupportedImageFormats failed");
130 
131   formats = (cl_image_format *)malloc(num_formats * sizeof(cl_image_format));
132 
133   err = clGetSupportedImageFormats(context, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, num_formats, formats, NULL);
134   test_error(err, "clGetSupportedImageFormats failed");
135 
136   for (i = 0; i < num_formats; i++) {
137     err |= test_arrayimagecopy_single_format(device, context, queue, &formats[i]);
138   }
139 
140   free(formats);
141   if (err)
142     log_error("ARRAY to IMAGE copy test failed\n");
143   else
144     log_info("ARRAY to IMAGE copy test passed\n");
145 
146   return err;
147 }
148