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