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 "../testBase.h"
17 #include "../common.h"
18 
19 extern int gTypesToTest;
20 extern bool gTestReadWrite;
21 
22 extern int test_read_image_set_1D(cl_device_id device, cl_context context,
23                                   cl_command_queue queue,
24                                   const cl_image_format *format,
25                                   image_sampler_data *imageSampler,
26                                   ExplicitType outputType);
27 extern int test_read_image_set_1D_buffer(cl_device_id device,
28                                          cl_context context,
29                                          cl_command_queue queue,
30                                          const cl_image_format *format,
31                                          image_sampler_data *imageSampler,
32                                          ExplicitType outputType);
33 extern int test_read_image_set_2D(cl_device_id device, cl_context context,
34                                   cl_command_queue queue,
35                                   const cl_image_format *format,
36                                   image_sampler_data *imageSampler,
37                                   ExplicitType outputType);
38 extern int test_read_image_set_3D(cl_device_id device, cl_context context,
39                                   cl_command_queue queue,
40                                   const cl_image_format *format,
41                                   image_sampler_data *imageSampler,
42                                   ExplicitType outputType);
43 extern int test_read_image_set_1D_array(cl_device_id device, cl_context context,
44                                         cl_command_queue queue,
45                                         const cl_image_format *format,
46                                         image_sampler_data *imageSampler,
47                                         ExplicitType outputType);
48 extern int test_read_image_set_2D_array(cl_device_id device, cl_context context,
49                                         cl_command_queue queue,
50                                         const cl_image_format *format,
51                                         image_sampler_data *imageSampler,
52                                         ExplicitType outputType);
53 
test_read_image_type(cl_device_id device,cl_context context,cl_command_queue queue,const cl_image_format * format,image_sampler_data * imageSampler,ExplicitType outputType,cl_mem_object_type imageType)54 int test_read_image_type(cl_device_id device, cl_context context,
55                          cl_command_queue queue, const cl_image_format *format,
56                          image_sampler_data *imageSampler,
57                          ExplicitType outputType, cl_mem_object_type imageType)
58 {
59     int ret = 0;
60     imageSampler->addressing_mode = CL_ADDRESS_NONE;
61 
62     print_read_header( format, imageSampler, false );
63 
64     gTestCount++;
65 
66     switch (imageType)
67     {
68         case CL_MEM_OBJECT_IMAGE1D:
69             ret = test_read_image_set_1D( device, context, queue, format, imageSampler, outputType );
70             break;
71         case CL_MEM_OBJECT_IMAGE1D_BUFFER:
72             ret += test_read_image_set_1D_buffer( device, context, queue, format, imageSampler, outputType );
73             break;
74         case CL_MEM_OBJECT_IMAGE2D:
75             ret = test_read_image_set_2D( device, context, queue, format, imageSampler, outputType );
76             break;
77         case CL_MEM_OBJECT_IMAGE3D:
78             ret = test_read_image_set_3D( device, context, queue, format, imageSampler, outputType );
79             break;
80         case CL_MEM_OBJECT_IMAGE1D_ARRAY:
81             ret = test_read_image_set_1D_array( device, context, queue, format, imageSampler, outputType );
82             break;
83         case CL_MEM_OBJECT_IMAGE2D_ARRAY:
84             ret = test_read_image_set_2D_array( device, context, queue, format, imageSampler, outputType );
85             break;
86     }
87 
88     if ( ret != 0 )
89     {
90         gFailCount++;
91         log_error( "FAILED: " );
92         print_read_header( format, imageSampler, true );
93         log_info( "\n" );
94     }
95     return ret;
96 }
97 
test_read_image_formats(cl_device_id device,cl_context context,cl_command_queue queue,const std::vector<cl_image_format> & formatList,const std::vector<bool> & filterFlags,image_sampler_data * imageSampler,ExplicitType outputType,cl_mem_object_type imageType)98 int test_read_image_formats(cl_device_id device, cl_context context,
99                             cl_command_queue queue,
100                             const std::vector<cl_image_format> &formatList,
101                             const std::vector<bool> &filterFlags,
102                             image_sampler_data *imageSampler,
103                             ExplicitType outputType,
104                             cl_mem_object_type imageType)
105 {
106     int ret = 0;
107     imageSampler->normalized_coords = false;
108     log_info( "read_image (%s coords, %s results) *****************************\n",
109               "integer", get_explicit_type_name( outputType ) );
110 
111     for (unsigned int i = 0; i < formatList.size(); i++)
112     {
113         if ( filterFlags[i] )
114             continue;
115 
116         const cl_image_format &imageFormat = formatList[i];
117 
118         ret |= test_read_image_type( device, context, queue, &imageFormat, imageSampler, outputType, imageType );
119     }
120     return ret;
121 }
122 
123 
test_image_set(cl_device_id device,cl_context context,cl_command_queue queue,cl_mem_object_type imageType)124 int test_image_set( cl_device_id device, cl_context context, cl_command_queue queue, cl_mem_object_type imageType )
125 {
126     int ret = 0;
127     static int printedFormatList = -1;
128 
129     // Grab the list of supported image formats
130     std::vector<cl_image_format> formatList;
131 
132     if (gTestReadWrite && checkForReadWriteImageSupport(device))
133     {
134         return TEST_SKIPPED_ITSELF;
135     }
136 
137     std::vector<cl_image_format> readOnlyFormats;
138     if (get_format_list(context, imageType, readOnlyFormats, CL_MEM_READ_ONLY))
139         return -1;
140 
141     if (gTestReadWrite)
142     {
143         std::vector<cl_image_format> readWriteFormats;
144         if (get_format_list(context, imageType, readWriteFormats,
145                             CL_MEM_KERNEL_READ_AND_WRITE))
146             return -1;
147 
148         // Keep only intersecting formats with read only and read write flags
149         for (unsigned int i = 0; i < readOnlyFormats.size(); i++)
150         {
151             for (unsigned int j = 0; j < readWriteFormats.size(); j++)
152             {
153                 if (readOnlyFormats[i].image_channel_data_type
154                         == readWriteFormats[j].image_channel_data_type
155                     && readOnlyFormats[i].image_channel_order
156                         == readWriteFormats[j].image_channel_order)
157                 {
158                     formatList.push_back(readOnlyFormats[i]);
159                     break;
160                 }
161             }
162         }
163     }
164     else
165     {
166         formatList = readOnlyFormats;
167     }
168 
169     // First time through, we'll go ahead and print the formats supported, regardless of type
170     if ( printedFormatList != (int)imageType )
171     {
172         log_info( "---- Supported %s read formats for this device ---- \n", convert_image_type_to_string(imageType) );
173         for (unsigned int f = 0; f < formatList.size(); f++)
174             log_info( "  %-7s %-24s %d\n", GetChannelOrderName( formatList[ f ].image_channel_order ),
175                       GetChannelTypeName( formatList[ f ].image_channel_data_type ),
176                       (int)get_format_channel_count( &formatList[ f ] ) );
177         log_info( "------------------------------------------- \n" );
178         printedFormatList = imageType;
179     }
180 
181     image_sampler_data imageSampler;
182 
183     for (auto test : imageTestTypes)
184     {
185         if (gTypesToTest & test.type)
186         {
187             std::vector<bool> filterFlags(formatList.size(), false);
188             if (filter_formats(formatList, filterFlags, test.channelTypes) == 0)
189             {
190                 log_info("No formats supported for %s type\n", test.name);
191             }
192             else
193             {
194                 imageSampler.filter_mode = CL_FILTER_NEAREST;
195                 ret += test_read_image_formats(
196                     device, context, queue, formatList, filterFlags,
197                     &imageSampler, test.explicitType, imageType);
198             }
199         }
200     }
201 
202     return ret;
203 }
204