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
18 #define MAX_ERR 0.005f
19 #define MAX_HALF_LINEAR_ERR 0.3f
20
21 extern bool gDebugTrace, gTestSmallImages, gTestMaxImages;
22
23
test_get_image_info_single(cl_context context,image_descriptor * imageInfo,MTdata d,cl_mem_flags flags,size_t row_pitch,size_t slice_pitch)24 int test_get_image_info_single( cl_context context, image_descriptor *imageInfo, MTdata d, cl_mem_flags flags, size_t row_pitch, size_t slice_pitch )
25 {
26 int error;
27 clMemWrapper image;
28 cl_image_desc imageDesc;
29 void *host_ptr = NULL;
30
31 // Generate some data to test against
32 BufferOwningPtr<char> imageValues;
33 generate_random_image_data( imageInfo, imageValues, d );
34
35 if (flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR)) {
36 host_ptr = (char *)imageValues;
37 }
38
39 memset(&imageDesc, 0x0, sizeof(cl_image_desc));
40 imageDesc.image_type = imageInfo->type;
41 imageDesc.image_width = imageInfo->width;
42 imageDesc.image_height = imageInfo->height;
43 imageDesc.image_depth = imageInfo->depth;
44 imageDesc.image_array_size = imageInfo->arraySize;
45 imageDesc.image_row_pitch = row_pitch;
46 imageDesc.image_slice_pitch = slice_pitch;
47
48 // Construct testing source
49 // Note: for now, just reset the pitches, since they only can actually be different
50 // if we use CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR
51 imageInfo->rowPitch = imageInfo->width * get_pixel_size( imageInfo->format );
52 imageInfo->slicePitch = 0;
53 switch (imageInfo->type)
54 {
55 case CL_MEM_OBJECT_IMAGE1D:
56 if ( gDebugTrace )
57 log_info( " - Creating 1D image %d with flags=0x%lx row_pitch=%d slice_pitch=%d host_ptr=%p...\n", (int)imageInfo->width, (unsigned long)flags, (int)row_pitch, (int)slice_pitch, host_ptr );
58 break;
59 case CL_MEM_OBJECT_IMAGE2D:
60 if ( gDebugTrace )
61 log_info( " - Creating 2D image %d by %d with flags=0x%lx row_pitch=%d slice_pitch=%d host_ptr=%p...\n", (int)imageInfo->width, (int)imageInfo->height, (unsigned long)flags, (int)row_pitch, (int)slice_pitch, host_ptr );
62 break;
63 case CL_MEM_OBJECT_IMAGE3D:
64 imageInfo->slicePitch = imageInfo->rowPitch * imageInfo->height;
65 if ( gDebugTrace )
66 log_info( " - Creating 3D image %d by %d by %d with flags=0x%lx row_pitch=%d slice_pitch=%d host_ptr=%p...\n", (int)imageInfo->width, (int)imageInfo->height, (int)imageInfo->depth, (unsigned long)flags, (int)row_pitch, (int)slice_pitch, host_ptr );
67 break;
68 case CL_MEM_OBJECT_IMAGE1D_ARRAY:
69 imageInfo->slicePitch = imageInfo->rowPitch;
70 if ( gDebugTrace )
71 log_info( " - Creating 1D image array %d by %d with flags=0x%lx row_pitch=%d slice_pitch=%d host_ptr=%p...\n", (int)imageInfo->width, (int)imageInfo->arraySize, (unsigned long)flags, (int)row_pitch, (int)slice_pitch, host_ptr );
72 break;
73 case CL_MEM_OBJECT_IMAGE2D_ARRAY:
74 imageInfo->slicePitch = imageInfo->rowPitch * imageInfo->height;
75 if ( gDebugTrace )
76 log_info( " - Creating 2D image array %d by %d by %d with flags=0x%lx row_pitch=%d slice_pitch=%d host_ptr=%p...\n", (int)imageInfo->width, (int)imageInfo->height, (int)imageInfo->arraySize, (unsigned long)flags, (int)row_pitch, (int)slice_pitch, host_ptr );
77 break;
78 }
79
80 image = clCreateImage(context, flags, imageInfo->format, &imageDesc, host_ptr, &error);
81 if( image == NULL )
82 {
83 switch (imageInfo->type)
84 {
85 case CL_MEM_OBJECT_IMAGE1D:
86 log_error( "ERROR: Unable to create 1D image of size %d (%s)", (int)imageInfo->width, IGetErrorString( error ) );
87 break;
88 case CL_MEM_OBJECT_IMAGE2D:
89 log_error( "ERROR: Unable to create 2D image of size %d x %d (%s)", (int)imageInfo->width, (int)imageInfo->height, IGetErrorString( error ) );
90 break;
91 case CL_MEM_OBJECT_IMAGE3D:
92 log_error( "ERROR: Unable to create 3D image of size %d x %d x %d (%s)", (int)imageInfo->width, (int)imageInfo->height, (int)imageInfo->depth, IGetErrorString( error ) );
93 break;
94 case CL_MEM_OBJECT_IMAGE1D_ARRAY:
95 log_error( "ERROR: Unable to create 1D image array of size %d x %d (%s)", (int)imageInfo->width, (int)imageInfo->arraySize, IGetErrorString( error ) );
96 break;
97 break;
98 case CL_MEM_OBJECT_IMAGE2D_ARRAY:
99 log_error( "ERROR: Unable to create 2D image array of size %d x %d x %d (%s)", (int)imageInfo->width, (int)imageInfo->height, (int)imageInfo->arraySize, IGetErrorString( error ) );
100 break;
101 }
102 return -1;
103 }
104
105 // Get info of the image and verify each item is correct
106 cl_image_format outFormat;
107 error = clGetImageInfo( image, CL_IMAGE_FORMAT, sizeof( outFormat ), &outFormat, NULL );
108 test_error( error, "Unable to get image info (format)" );
109 if( outFormat.image_channel_order != imageInfo->format->image_channel_order ||
110 outFormat.image_channel_data_type != imageInfo->format->image_channel_data_type )
111 {
112 log_error( "ERROR: image format returned is invalid! (expected %s:%s, got %s:%s (%d:%d))\n",
113 GetChannelOrderName( imageInfo->format->image_channel_order ), GetChannelTypeName( imageInfo->format->image_channel_data_type ),
114 GetChannelOrderName( outFormat.image_channel_order ), GetChannelTypeName( outFormat.image_channel_data_type ),
115 (int)outFormat.image_channel_order, (int)outFormat.image_channel_data_type );
116 return 1;
117 }
118
119 size_t outElementSize;
120 error = clGetImageInfo( image, CL_IMAGE_ELEMENT_SIZE, sizeof( outElementSize ), &outElementSize, NULL );
121 test_error( error, "Unable to get image info (element size)" );
122 if( outElementSize != get_pixel_size( imageInfo->format ) )
123 {
124 log_error( "ERROR: image element size returned is invalid! (expected %d, got %d)\n",
125 (int)get_pixel_size( imageInfo->format ), (int)outElementSize );
126 return 1;
127 }
128
129 size_t outRowPitch;
130 error = clGetImageInfo( image, CL_IMAGE_ROW_PITCH, sizeof( outRowPitch ), &outRowPitch, NULL );
131 test_error( error, "Unable to get image info (row pitch)" );
132
133 size_t outSlicePitch;
134 error = clGetImageInfo( image, CL_IMAGE_SLICE_PITCH, sizeof( outSlicePitch ), &outSlicePitch, NULL );
135 test_error( error, "Unable to get image info (slice pitch)" );
136 if( imageInfo->type == CL_MEM_OBJECT_IMAGE1D && outSlicePitch != 0 )
137 {
138 log_error( "ERROR: slice pitch returned is invalid! (expected %d, got %d)\n",
139 (int)0, (int)outSlicePitch );
140 return 1;
141 }
142
143 size_t outWidth;
144 error = clGetImageInfo( image, CL_IMAGE_WIDTH, sizeof( outWidth ), &outWidth, NULL );
145 test_error( error, "Unable to get image info (width)" );
146 if( outWidth != imageInfo->width )
147 {
148 log_error( "ERROR: image width returned is invalid! (expected %d, got %d)\n",
149 (int)imageInfo->width, (int)outWidth );
150 return 1;
151 }
152
153 size_t required_height;
154 switch (imageInfo->type)
155 {
156 case CL_MEM_OBJECT_IMAGE1D:
157 case CL_MEM_OBJECT_IMAGE1D_ARRAY:
158 required_height = 0;
159 break;
160 case CL_MEM_OBJECT_IMAGE2D:
161 case CL_MEM_OBJECT_IMAGE2D_ARRAY:
162 case CL_MEM_OBJECT_IMAGE3D:
163 required_height = imageInfo->height;
164 break;
165 }
166
167 size_t outHeight;
168 error = clGetImageInfo( image, CL_IMAGE_HEIGHT, sizeof( outHeight ), &outHeight, NULL );
169 test_error( error, "Unable to get image info (height)" );
170 if( outHeight != required_height )
171 {
172 log_error( "ERROR: image height returned is invalid! (expected %d, got %d)\n",
173 (int)required_height, (int)outHeight );
174 return 1;
175 }
176
177 size_t required_depth;
178 switch (imageInfo->type)
179 {
180 case CL_MEM_OBJECT_IMAGE1D:
181 case CL_MEM_OBJECT_IMAGE2D:
182 case CL_MEM_OBJECT_IMAGE1D_ARRAY:
183 case CL_MEM_OBJECT_IMAGE2D_ARRAY:
184 required_depth = 0;
185 break;
186 case CL_MEM_OBJECT_IMAGE3D:
187 required_depth = imageInfo->depth;
188 break;
189 }
190
191 size_t outDepth;
192 error = clGetImageInfo( image, CL_IMAGE_DEPTH, sizeof( outDepth ), &outDepth, NULL );
193 test_error( error, "Unable to get image info (depth)" );
194 if( outDepth != required_depth )
195 {
196 log_error( "ERROR: image depth returned is invalid! (expected %d, got %d)\n",
197 (int)required_depth, (int)outDepth );
198 return 1;
199 }
200
201 size_t required_array_size;
202 switch (imageInfo->type)
203 {
204 case CL_MEM_OBJECT_IMAGE1D:
205 case CL_MEM_OBJECT_IMAGE2D:
206 case CL_MEM_OBJECT_IMAGE3D:
207 required_array_size = 0;
208 break;
209 case CL_MEM_OBJECT_IMAGE1D_ARRAY:
210 case CL_MEM_OBJECT_IMAGE2D_ARRAY:
211 required_array_size = imageInfo->arraySize;
212 break;
213 }
214
215 size_t outArraySize;
216 error = clGetImageInfo( image, CL_IMAGE_ARRAY_SIZE, sizeof( outArraySize ), &outArraySize, NULL );
217 test_error( error, "Unable to get image info (array size)" );
218 if( outArraySize != required_array_size )
219 {
220 log_error( "ERROR: image array size returned is invalid! (expected %d, got %d)\n",
221 (int)required_array_size, (int)outArraySize );
222 return 1;
223 }
224
225 cl_mem outBuffer;
226 error = clGetImageInfo( image, CL_IMAGE_BUFFER, sizeof( outBuffer ), &outBuffer, NULL );
227 test_error( error, "Unable to get image info (buffer)" );
228 if (imageInfo->type == CL_MEM_OBJECT_IMAGE1D_BUFFER) {
229 if (outBuffer != imageInfo->buffer) {
230 log_error( "ERROR: cl_mem returned is invalid! (expected %p, got %p)\n",
231 imageInfo->buffer, outBuffer );
232 return 1;
233 }
234 } else {
235 if (outBuffer != (cl_mem)NULL) {
236 log_error( "ERROR: cl_mem returned is invalid! (expected %p, got %p)\n",
237 (cl_mem)NULL, outBuffer );
238 return 1;
239 }
240 }
241
242 cl_uint numMipLevels;
243 error = clGetImageInfo( image, CL_IMAGE_NUM_MIP_LEVELS, sizeof( numMipLevels ), &numMipLevels, NULL );
244 test_error( error, "Unable to get image info (num mip levels)" );
245 if( numMipLevels != 0 )
246 {
247 log_error( "ERROR: image num_mip_levels returned is invalid! (expected %d, got %d)\n",
248 (int)0, (int)numMipLevels );
249 return 1;
250 }
251
252 cl_uint numSamples;
253 error = clGetImageInfo( image, CL_IMAGE_NUM_SAMPLES, sizeof( numSamples ), &numSamples, NULL );
254 test_error( error, "Unable to get image info (num samples)" );
255 if( numSamples != 0 )
256 {
257 log_error( "ERROR: image num_samples returned is invalid! (expected %d, got %d)\n",
258 (int)0, (int)numSamples );
259 return 1;
260 }
261
262 return 0;
263 }
264
test_get_image_info_2D(cl_device_id device,cl_context context,cl_image_format * format,cl_mem_flags flags)265 int test_get_image_info_2D( cl_device_id device, cl_context context, cl_image_format *format, cl_mem_flags flags )
266 {
267 size_t maxWidth, maxHeight;
268 cl_ulong maxAllocSize, memSize;
269 image_descriptor imageInfo = { 0 };
270 RandomSeed seed( gRandomSeed );
271 size_t pixelSize;
272
273 cl_mem_flags all_host_ptr_flags[5] = {
274 flags,
275 CL_MEM_ALLOC_HOST_PTR | flags,
276 CL_MEM_COPY_HOST_PTR | flags,
277 CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR | flags,
278 CL_MEM_USE_HOST_PTR | flags
279 };
280
281 memset(&imageInfo, 0x0, sizeof(image_descriptor));
282 imageInfo.format = format;
283 imageInfo.type = CL_MEM_OBJECT_IMAGE2D;
284 pixelSize = get_pixel_size( imageInfo.format );
285
286 int error = clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
287 error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof( maxHeight ), &maxHeight, NULL );
288 error |= clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
289 error |= clGetDeviceInfo( device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof( memSize ), &memSize, NULL );
290 test_error( error, "Unable to get max image 2D width or max image 3D height or max memory allocation size or global memory size from device" );
291
292 if (memSize > (cl_ulong)SIZE_MAX) {
293 memSize = (cl_ulong)SIZE_MAX;
294 }
295
296 if( gTestSmallImages )
297 {
298 for( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
299 {
300 imageInfo.rowPitch = imageInfo.width * pixelSize;
301 for( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
302 {
303 for (unsigned int j=0; j < sizeof(all_host_ptr_flags)/sizeof(cl_mem_flags); j++)
304 {
305 if( gDebugTrace )
306 log_info( " at size %d,%d (flags[%u] 0x%x pitch %d)\n", (int)imageInfo.width, (int)imageInfo.height, j, (unsigned int) all_host_ptr_flags[j], (int)imageInfo.rowPitch );
307 if ( test_get_image_info_single( context, &imageInfo, seed, all_host_ptr_flags[j], 0, 0 ) )
308 return -1;
309 if (all_host_ptr_flags[j] & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR)) { // skip test when host_ptr is NULL
310 if ( test_get_image_info_single( context, &imageInfo, seed, all_host_ptr_flags[j], imageInfo.rowPitch, 0 ) )
311 return -1;
312 }
313 }
314 }
315 }
316 }
317 else if( gTestMaxImages )
318 {
319 // Try a specific set of maximum sizes
320 size_t numbeOfSizes;
321 size_t sizes[100][3];
322
323 get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, 1, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE2D, imageInfo.format);
324
325 for( size_t idx = 0; idx < numbeOfSizes; idx++ )
326 {
327 imageInfo.width = sizes[ idx ][ 0 ];
328 imageInfo.height = sizes[ idx ][ 1 ];
329 imageInfo.rowPitch = imageInfo.width * pixelSize;
330 log_info( "Testing %d x %d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
331 for (unsigned int j=0; j < sizeof(all_host_ptr_flags)/sizeof(cl_mem_flags); j++)
332 {
333 if( gDebugTrace )
334 log_info( " at max size %d,%d (flags[%u] 0x%x pitch %d)\n", (int)imageInfo.width, (int)imageInfo.height, j, (unsigned int) all_host_ptr_flags[j], (int)imageInfo.rowPitch );
335 if( test_get_image_info_single( context, &imageInfo, seed, all_host_ptr_flags[j], 0, 0 ) )
336 return -1;
337 if (all_host_ptr_flags[j] & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR)) { // skip test when host_ptr is NULL
338 if( test_get_image_info_single( context, &imageInfo, seed, all_host_ptr_flags[j], imageInfo.rowPitch, 0 ) )
339 return -1;
340 }
341 }
342 }
343 }
344 else
345 {
346 for( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
347 {
348 cl_ulong size;
349 // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
350 // image, the result array, plus offset arrays, will fit in the global ram space
351 do
352 {
353 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
354 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed );
355
356 imageInfo.rowPitch = imageInfo.width * pixelSize;
357 size_t extraWidth = (int)random_log_in_range( 0, 64, seed );
358 imageInfo.rowPitch += extraWidth;
359
360 do {
361 extraWidth++;
362 imageInfo.rowPitch += extraWidth;
363 } while ((imageInfo.rowPitch % pixelSize) != 0);
364
365 size = (cl_ulong)imageInfo.rowPitch * (cl_ulong)imageInfo.height * 4;
366 } while( size > maxAllocSize || ( size * 3 ) > memSize );
367
368 for (unsigned int j=0; j < sizeof(all_host_ptr_flags)/sizeof(cl_mem_flags); j++)
369 {
370 if( gDebugTrace )
371 log_info( " at size %d,%d (flags[%u] 0x%x pitch %d) out of %d,%d\n", (int)imageInfo.width, (int)imageInfo.height, j, (unsigned int) all_host_ptr_flags[j], (int)imageInfo.rowPitch, (int)maxWidth, (int)maxHeight );
372 if ( test_get_image_info_single( context, &imageInfo, seed, all_host_ptr_flags[j], 0, 0 ) )
373 return -1;
374 if (all_host_ptr_flags[j] & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR)) { // skip test when host_ptr is NULL
375 if ( test_get_image_info_single( context, &imageInfo, seed, all_host_ptr_flags[j], imageInfo.rowPitch, 0 ) )
376 return -1;
377 }
378 }
379 }
380 }
381
382 return 0;
383 }
384