xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/api/test_mem_object_info.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 #include "harness/typeWrappers.h"
18 #include "harness/testHarness.h"
19 
20 
21 #define TEST_MEM_OBJECT_PARAM( mem, paramName, val, expected, name, type, cast )    \
22 error = clGetMemObjectInfo( mem, paramName, sizeof( val ), &val, &size );   \
23 test_error( error, "Unable to get mem object " name );  \
24 if( val != expected )   \
25 {   \
26 log_error( "ERROR: Mem object " name " did not validate! (expected " type ", got " type " from %s:%d)\n",   \
27 expected, (cast)val, __FILE__, __LINE__ );   \
28 return -1;  \
29 }   \
30 if( size != sizeof( val ) ) \
31 {   \
32 log_error( "ERROR: Returned size of mem object " name " does not validate! (expected %d, got %d from %s:%d)\n", \
33 (int)sizeof( val ), (int)size , __FILE__, __LINE__ );   \
34 return -1;  \
35 }
36 
mem_obj_destructor_callback(cl_mem,void * data)37 static void CL_CALLBACK mem_obj_destructor_callback( cl_mem, void * data )
38 {
39     free( data );
40 }
41 
42 static unsigned int
get_image_dim(MTdata * d,unsigned int mod)43 get_image_dim(MTdata *d, unsigned int mod)
44 {
45     unsigned int val = 0;
46 
47     do
48     {
49         val = (unsigned int)genrand_int32(*d) % mod;
50     } while (val == 0);
51 
52     return val;
53 }
54 
55 
test_get_buffer_info(cl_device_id deviceID,cl_context context,cl_command_queue ignoreQueue,int num_elements)56 int test_get_buffer_info( cl_device_id deviceID, cl_context context, cl_command_queue ignoreQueue, int num_elements )
57 {
58     int error;
59     size_t size;
60     void * buffer = NULL;
61 
62     clMemWrapper bufferObject;
63     clMemWrapper subBufferObject;
64 
65     cl_mem_flags bufferFlags[] = {
66         CL_MEM_READ_WRITE,
67         CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
68         CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
69         CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
70         CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
71         CL_MEM_READ_ONLY,
72         CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
73         CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
74         CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
75         CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
76         CL_MEM_WRITE_ONLY,
77         CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
78         CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
79         CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
80         CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
81         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE,
82         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
83         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
84         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
85         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
86         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY,
87         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
88         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
89         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
90         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
91         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY,
92         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
93         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
94         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
95         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
96         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE,
97         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
98         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
99         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
100         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
101         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY,
102         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
103         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
104         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
105         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
106         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY,
107         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
108         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
109         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
110         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
111         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE,
112         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
113         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
114         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
115         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
116         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY,
117         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
118         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
119         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
120         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
121         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY,
122         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
123         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
124         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
125         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
126     };
127 
128     cl_mem_flags subBufferFlags[] = {
129         CL_MEM_READ_WRITE,
130         CL_MEM_READ_ONLY,
131         CL_MEM_WRITE_ONLY,
132         0,
133         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE,
134         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY,
135         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY,
136         CL_MEM_HOST_READ_ONLY | 0,
137         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE,
138         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY,
139         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY,
140         CL_MEM_HOST_WRITE_ONLY | 0,
141         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE,
142         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY,
143         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY,
144         CL_MEM_HOST_NO_ACCESS | 0,
145     };
146 
147 
148     // Get the address alignment, so we can make sure the sub-buffer test later works properly.
149     cl_uint addressAlignBits;
150     error = clGetDeviceInfo( deviceID, CL_DEVICE_MEM_BASE_ADDR_ALIGN, sizeof(addressAlignBits), &addressAlignBits, NULL );
151 
152     size_t addressAlign = addressAlignBits/8;
153     if ( addressAlign < 128 )
154     {
155         addressAlign = 128;
156     }
157 
158     for ( unsigned int i = 0; i < sizeof(bufferFlags) / sizeof(cl_mem_flags); ++i )
159     {
160         //printf("@@@ bufferFlags[%u]=0x%x\n", i, bufferFlags[ i ]);
161         if ( bufferFlags[ i ] & CL_MEM_USE_HOST_PTR )
162         {
163             // Create a buffer object to test against.
164             buffer = malloc( addressAlign * 4 );
165             bufferObject = clCreateBuffer( context, bufferFlags[ i ], addressAlign * 4, buffer, &error );
166             if ( error )
167             {
168                 free( buffer );
169                 test_error( error, "Unable to create buffer (CL_MEM_USE_HOST_PTR) to test with" );
170             }
171 
172             // Make sure buffer is cleaned up appropriately if we encounter an error in the rest of the calls.
173             error = clSetMemObjectDestructorCallback( bufferObject, mem_obj_destructor_callback, buffer );
174             test_error( error, "Unable to set mem object destructor callback" );
175 
176             void * ptr;
177             TEST_MEM_OBJECT_PARAM( bufferObject, CL_MEM_HOST_PTR, ptr, buffer, "host pointer", "%p", void * )
178         }
179         else if ( (bufferFlags[ i ] & CL_MEM_ALLOC_HOST_PTR) && (bufferFlags[ i ] & CL_MEM_COPY_HOST_PTR) )
180         {
181             // Create a buffer object to test against.
182             buffer = malloc( addressAlign * 4 );
183             bufferObject = clCreateBuffer( context, bufferFlags[ i ], addressAlign * 4, buffer, &error );
184             if ( error )
185             {
186                 free( buffer );
187                 test_error( error, "Unable to create buffer (CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR) to test with" );
188             }
189 
190             // Make sure buffer is cleaned up appropriately if we encounter an error in the rest of the calls.
191             error = clSetMemObjectDestructorCallback( bufferObject, mem_obj_destructor_callback, buffer );
192             test_error( error, "Unable to set mem object destructor callback" );
193         }
194         else if ( bufferFlags[ i ] & CL_MEM_ALLOC_HOST_PTR )
195         {
196             // Create a buffer object to test against.
197             bufferObject = clCreateBuffer( context, bufferFlags[ i ], addressAlign * 4, NULL, &error );
198             test_error( error, "Unable to create buffer (CL_MEM_ALLOC_HOST_PTR) to test with" );
199         }
200         else if ( bufferFlags[ i ] & CL_MEM_COPY_HOST_PTR )
201         {
202             // Create a buffer object to test against.
203             buffer = malloc( addressAlign * 4 );
204             bufferObject = clCreateBuffer( context, bufferFlags[ i ], addressAlign * 4, buffer, &error );
205             if ( error )
206             {
207                 free( buffer );
208                 test_error( error, "Unable to create buffer (CL_MEM_COPY_HOST_PTR) to test with" );
209             }
210 
211             // Make sure buffer is cleaned up appropriately if we encounter an error in the rest of the calls.
212             error = clSetMemObjectDestructorCallback( bufferObject, mem_obj_destructor_callback, buffer );
213             test_error( error, "Unable to set mem object destructor callback" );
214         }
215         else
216         {
217             // Create a buffer object to test against.
218             bufferObject = clCreateBuffer( context, bufferFlags[ i ], addressAlign * 4, NULL, &error );
219             test_error( error, "Unable to create buffer to test with" );
220             void *ptr;
221             TEST_MEM_OBJECT_PARAM(bufferObject, CL_MEM_HOST_PTR, ptr, NULL,
222                                   "host pointer", "%p", void *)
223         }
224 
225         // Perform buffer object queries.
226         cl_mem_object_type type;
227         TEST_MEM_OBJECT_PARAM( bufferObject, CL_MEM_TYPE, type, CL_MEM_OBJECT_BUFFER, "type", "%d", int )
228 
229         cl_mem_flags flags;
230         TEST_MEM_OBJECT_PARAM( bufferObject, CL_MEM_FLAGS, flags, (unsigned int)bufferFlags[ i ], "flags", "%d", unsigned int )
231 
232         size_t sz;
233         TEST_MEM_OBJECT_PARAM( bufferObject, CL_MEM_SIZE, sz, (size_t)( addressAlign * 4 ), "size", "%ld", size_t )
234 
235         cl_uint mapCount;
236         error = clGetMemObjectInfo( bufferObject, CL_MEM_MAP_COUNT, sizeof( mapCount ), &mapCount, &size );
237         test_error( error, "Unable to get mem object map count" );
238         if( size != sizeof( mapCount ) )
239         {
240             log_error( "ERROR: Returned size of mem object map count does not validate! (expected %d, got %d from %s:%d)\n",
241                       (int)sizeof( mapCount ), (int)size, __FILE__, __LINE__ );
242             return -1;
243         }
244 
245         cl_uint refCount;
246         error = clGetMemObjectInfo( bufferObject, CL_MEM_REFERENCE_COUNT, sizeof( refCount ), &refCount, &size );
247         test_error( error, "Unable to get mem object reference count" );
248         if( size != sizeof( refCount ) )
249         {
250             log_error( "ERROR: Returned size of mem object reference count does not validate! (expected %d, got %d from %s:%d)\n",
251                       (int)sizeof( refCount ), (int)size, __FILE__, __LINE__ );
252             return -1;
253         }
254 
255         cl_context otherCtx;
256         TEST_MEM_OBJECT_PARAM( bufferObject, CL_MEM_CONTEXT, otherCtx, context, "context", "%p", cl_context )
257 
258         cl_mem origObj;
259         TEST_MEM_OBJECT_PARAM( bufferObject, CL_MEM_ASSOCIATED_MEMOBJECT, origObj, (void *)NULL, "associated mem object", "%p", void * )
260 
261         size_t offset;
262         TEST_MEM_OBJECT_PARAM( bufferObject, CL_MEM_OFFSET, offset, 0L, "offset", "%ld", size_t )
263 
264         cl_buffer_region region;
265         region.origin = addressAlign;
266         region.size = addressAlign;
267 
268         // Loop over possible sub-buffer objects to create.
269         for ( unsigned int j = 0; j < sizeof(subBufferFlags) / sizeof(cl_mem_flags); ++j )
270         {
271             if ( subBufferFlags[ j ] & CL_MEM_READ_WRITE )
272             {
273                 if ( !(bufferFlags[ i ] & CL_MEM_READ_WRITE) )
274                     continue; // Buffer must be read_write for sub-buffer to be read_write.
275             }
276             if ( subBufferFlags[ j ] & CL_MEM_READ_ONLY )
277             {
278                 if ( !(bufferFlags[ i ] & CL_MEM_READ_WRITE) && !(bufferFlags[ i ] & CL_MEM_READ_ONLY) )
279                     continue; // Buffer must be read_write or read_only for sub-buffer to be read_only
280             }
281             if ( subBufferFlags[ j ] & CL_MEM_WRITE_ONLY )
282             {
283                 if ( !(bufferFlags[ i ] & CL_MEM_READ_WRITE) && !(bufferFlags[ i ] & CL_MEM_WRITE_ONLY) )
284                     continue; // Buffer must be read_write or write_only for sub-buffer to be write_only
285             }
286             if ( subBufferFlags[ j ] & CL_MEM_HOST_READ_ONLY )
287             {
288                 if ( (bufferFlags[ i ] & CL_MEM_HOST_NO_ACCESS) || (bufferFlags[ i ] & CL_MEM_HOST_WRITE_ONLY) )
289                     continue; // Buffer must be host all access or host read_only for sub-buffer to be host read_only
290             }
291             if ( subBufferFlags[ j ] & CL_MEM_HOST_WRITE_ONLY )
292             {
293                 if ( (bufferFlags[ i ] & CL_MEM_HOST_NO_ACCESS) || (bufferFlags[ i ] & CL_MEM_HOST_READ_ONLY) )
294                     continue; // Buffer must be host all access or host write_only for sub-buffer to be host write_only
295             }
296             //printf("@@@ bufferFlags[%u]=0x%x subBufferFlags[%u]=0x%x\n", i, bufferFlags[ i ], j, subBufferFlags[ j ]);
297 
298             subBufferObject = clCreateSubBuffer( bufferObject, subBufferFlags[ j ], CL_BUFFER_CREATE_TYPE_REGION, &region, &error );
299             test_error( error, "Unable to create sub-buffer to test against" );
300 
301             // Perform sub-buffer object queries.
302             cl_mem_object_type type;
303             TEST_MEM_OBJECT_PARAM( subBufferObject, CL_MEM_TYPE, type, CL_MEM_OBJECT_BUFFER, "type", "%d", int )
304 
305             cl_mem_flags flags;
306             cl_mem_flags inheritedFlags = subBufferFlags[ j ];
307             if ( (subBufferFlags[ j ] & (CL_MEM_READ_WRITE | CL_MEM_READ_ONLY | CL_MEM_WRITE_ONLY)) == 0 )
308             {
309               inheritedFlags |= bufferFlags[ i ] & (CL_MEM_READ_WRITE | CL_MEM_READ_ONLY | CL_MEM_WRITE_ONLY);
310             }
311             inheritedFlags |= bufferFlags[ i ] & (CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR);
312             if ( (subBufferFlags[ j ] & (CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_NO_ACCESS)) == 0)
313             {
314               inheritedFlags |= bufferFlags[ i ] & (CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_NO_ACCESS);
315             }
316             TEST_MEM_OBJECT_PARAM( subBufferObject, CL_MEM_FLAGS, flags, (unsigned int)inheritedFlags, "flags", "%d", unsigned int )
317 
318             TEST_MEM_OBJECT_PARAM( subBufferObject, CL_MEM_SIZE, sz, (size_t)( addressAlign ), "size", "%ld", size_t )
319 
320             if ( bufferFlags[ i ] & CL_MEM_USE_HOST_PTR )
321             {
322                 void * ptr;
323                 void * offsetInBuffer = (char *)buffer + addressAlign;
324 
325                 TEST_MEM_OBJECT_PARAM( subBufferObject, CL_MEM_HOST_PTR, ptr, offsetInBuffer, "host pointer", "%p", void * )
326             }
327 
328             cl_uint mapCount;
329             error = clGetMemObjectInfo( subBufferObject, CL_MEM_MAP_COUNT, sizeof( mapCount ), &mapCount, &size );
330             test_error( error, "Unable to get mem object map count" );
331             if( size != sizeof( mapCount ) )
332             {
333                 log_error( "ERROR: Returned size of mem object map count does not validate! (expected %d, got %d from %s:%d)\n",
334                           (int)sizeof( mapCount ), (int)size, __FILE__, __LINE__ );
335                 return -1;
336             }
337 
338             cl_uint refCount;
339             error = clGetMemObjectInfo( subBufferObject, CL_MEM_REFERENCE_COUNT, sizeof( refCount ), &refCount, &size );
340             test_error( error, "Unable to get mem object reference count" );
341             if( size != sizeof( refCount ) )
342             {
343                 log_error( "ERROR: Returned size of mem object reference count does not validate! (expected %d, got %d from %s:%d)\n",
344                           (int)sizeof( refCount ), (int)size, __FILE__, __LINE__ );
345                 return -1;
346             }
347 
348             cl_context otherCtx;
349             TEST_MEM_OBJECT_PARAM( subBufferObject, CL_MEM_CONTEXT, otherCtx, context, "context", "%p", cl_context )
350 
351             TEST_MEM_OBJECT_PARAM( subBufferObject, CL_MEM_ASSOCIATED_MEMOBJECT, origObj, (cl_mem)bufferObject, "associated mem object", "%p", void * )
352 
353             TEST_MEM_OBJECT_PARAM( subBufferObject, CL_MEM_OFFSET, offset, (size_t)( addressAlign ), "offset", "%ld", size_t )
354         }
355     }
356 
357     return CL_SUCCESS;
358 }
359 
360 
test_get_imageObject_info(cl_mem * image,cl_mem_flags objectFlags,cl_image_desc * imageInfo,cl_image_format * imageFormat,size_t pixelSize,cl_context context)361 int test_get_imageObject_info( cl_mem * image, cl_mem_flags objectFlags, cl_image_desc *imageInfo, cl_image_format *imageFormat, size_t pixelSize, cl_context context )
362 {
363     int error;
364     size_t size;
365     cl_mem_object_type type;
366     cl_mem_flags flags;
367     cl_uint mapCount;
368     cl_uint refCount;
369     cl_context otherCtx;
370     size_t offset;
371     size_t sz;
372 
373     TEST_MEM_OBJECT_PARAM( *image, CL_MEM_TYPE, type, imageInfo->image_type, "type", "%d", int )
374 
375     TEST_MEM_OBJECT_PARAM( *image, CL_MEM_FLAGS, flags, (unsigned int)objectFlags, "flags", "%d", unsigned int )
376 
377     error = clGetMemObjectInfo( *image, CL_MEM_SIZE, sizeof( sz ), &sz, NULL );
378     test_error( error, "Unable to get mem size" );
379 
380     // The size returned is not constrained by the spec.
381 
382     error = clGetMemObjectInfo( *image, CL_MEM_MAP_COUNT, sizeof( mapCount ), &mapCount, &size );
383     test_error( error, "Unable to get mem object map count" );
384     if( size != sizeof( mapCount ) )
385     {
386         log_error( "ERROR: Returned size of mem object map count does not validate! (expected %d, got %d from %s:%d)\n",
387                   (int)sizeof( mapCount ), (int)size, __FILE__, __LINE__ );
388         return -1;
389     }
390 
391     error = clGetMemObjectInfo( *image, CL_MEM_REFERENCE_COUNT, sizeof( refCount ), &refCount, &size );
392     test_error( error, "Unable to get mem object reference count" );
393     if( size != sizeof( refCount ) )
394     {
395         log_error( "ERROR: Returned size of mem object reference count does not validate! (expected %d, got %d from %s:%d)\n",
396                   (int)sizeof( refCount ), (int)size, __FILE__, __LINE__ );
397         return -1;
398     }
399 
400     TEST_MEM_OBJECT_PARAM( *image, CL_MEM_CONTEXT, otherCtx, context, "context", "%p", cl_context )
401 
402     TEST_MEM_OBJECT_PARAM( *image, CL_MEM_OFFSET, offset, 0L, "offset", "%ld", size_t )
403 
404     return CL_SUCCESS;
405 }
406 
407 
test_get_image_info(cl_device_id deviceID,cl_context context,cl_mem_object_type type)408 int test_get_image_info( cl_device_id deviceID, cl_context context, cl_mem_object_type type )
409 {
410     int error;
411     size_t size;
412     void * image = NULL;
413 
414     cl_mem imageObject;
415     cl_image_desc imageInfo;
416 
417     cl_mem_flags imageFlags[] = {
418         CL_MEM_READ_WRITE,
419         CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
420         CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
421         CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
422         CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
423         CL_MEM_READ_ONLY,
424         CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
425         CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
426         CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
427         CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
428         CL_MEM_WRITE_ONLY,
429         CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
430         CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
431         CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
432         CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
433         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE,
434         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
435         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
436         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
437         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
438         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY,
439         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
440         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
441         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
442         CL_MEM_HOST_READ_ONLY | CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
443         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY,
444         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
445         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
446         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
447         CL_MEM_HOST_READ_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
448         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE,
449         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
450         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
451         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
452         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
453         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY,
454         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
455         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
456         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
457         CL_MEM_HOST_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
458         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY,
459         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
460         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
461         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
462         CL_MEM_HOST_WRITE_ONLY | CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
463         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE,
464         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
465         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
466         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
467         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
468         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY,
469         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
470         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR,
471         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
472         CL_MEM_HOST_NO_ACCESS | CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
473         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY,
474         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
475         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR,
476         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR,
477         CL_MEM_HOST_NO_ACCESS | CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
478     };
479     MTdata d;
480 
481     PASSIVE_REQUIRE_IMAGE_SUPPORT( deviceID )
482 
483     cl_image_format imageFormat;
484     size_t pixelSize = 4;
485 
486     imageFormat.image_channel_order = CL_RGBA;
487     imageFormat.image_channel_data_type = CL_UNORM_INT8;
488 
489     imageInfo.image_width = imageInfo.image_height = imageInfo.image_depth = 1;
490     imageInfo.image_array_size = 0;
491     imageInfo.num_mip_levels = imageInfo.num_samples = 0;
492 #ifdef CL_VERSION_2_0
493     imageInfo.mem_object = NULL;
494 #else
495     imageInfo.buffer = NULL;
496 #endif
497 
498     d = init_genrand( gRandomSeed );
499 
500     for ( unsigned int i = 0; i < sizeof(imageFlags) / sizeof(cl_mem_flags); ++i )
501     {
502         imageInfo.image_row_pitch = 0;
503         imageInfo.image_slice_pitch = 0;
504 
505         switch (type)
506         {
507             case CL_MEM_OBJECT_IMAGE1D:
508                 imageInfo.image_width = get_image_dim(&d, 1023);
509                 imageInfo.image_type = CL_MEM_OBJECT_IMAGE1D;
510                 break;
511 
512             case CL_MEM_OBJECT_IMAGE2D:
513                 imageInfo.image_width = get_image_dim(&d, 1023);
514                 imageInfo.image_height = get_image_dim(&d, 1023);
515                 imageInfo.image_type = CL_MEM_OBJECT_IMAGE2D;
516                 break;
517 
518             case CL_MEM_OBJECT_IMAGE3D:
519                 error = checkFor3DImageSupport(deviceID);
520                 if (error == CL_IMAGE_FORMAT_NOT_SUPPORTED)
521                 {
522                     log_info("Device doesn't support 3D images. Skipping test.\n");
523                     return CL_SUCCESS;
524                 }
525                 imageInfo.image_width = get_image_dim(&d, 127);
526                 imageInfo.image_height = get_image_dim(&d, 127);
527                 imageInfo.image_depth = get_image_dim(&d, 127);
528                 imageInfo.image_type = CL_MEM_OBJECT_IMAGE3D;
529                 break;
530 
531             case CL_MEM_OBJECT_IMAGE1D_ARRAY:
532                 imageInfo.image_width = get_image_dim(&d, 1023);
533                 imageInfo.image_array_size = get_image_dim(&d, 1023);
534                 imageInfo.image_type = CL_MEM_OBJECT_IMAGE1D_ARRAY;
535                 break;
536 
537             case CL_MEM_OBJECT_IMAGE2D_ARRAY:
538                 imageInfo.image_width = get_image_dim(&d, 255);
539                 imageInfo.image_height = get_image_dim(&d, 255);
540                 imageInfo.image_array_size = get_image_dim(&d, 255);
541                 imageInfo.image_type = CL_MEM_OBJECT_IMAGE2D_ARRAY;
542                 break;
543         }
544 
545         if ( imageFlags[i] & CL_MEM_USE_HOST_PTR )
546         {
547             // Create an image object to test against.
548             image = malloc( imageInfo.image_width * imageInfo.image_height * imageInfo.image_depth * pixelSize *
549                            ((imageInfo.image_array_size == 0) ? 1 : imageInfo.image_array_size) );
550             imageObject = clCreateImage( context, imageFlags[i], &imageFormat, &imageInfo, image, &error );
551             if ( error )
552             {
553                 free( image );
554                 test_error( error, "Unable to create image with (CL_MEM_USE_HOST_PTR) to test with" );
555             }
556 
557             // Make sure image is cleaned up appropriately if we encounter an error in the rest of the calls.
558             error = clSetMemObjectDestructorCallback( imageObject, mem_obj_destructor_callback, image );
559             test_error( error, "Unable to set mem object destructor callback" );
560 
561             void * ptr;
562             TEST_MEM_OBJECT_PARAM( imageObject, CL_MEM_HOST_PTR, ptr, image, "host pointer", "%p", void * )
563             int ret = test_get_imageObject_info( &imageObject, imageFlags[i], &imageInfo, &imageFormat, pixelSize, context );
564             if (ret)
565                 return ret;
566 
567             // release image object
568             clReleaseMemObject(imageObject);
569 
570             // Try again with non-zero rowPitch.
571             imageInfo.image_row_pitch = imageInfo.image_width * pixelSize;
572             switch (type)
573             {
574                 case CL_MEM_OBJECT_IMAGE1D_ARRAY:
575                 case CL_MEM_OBJECT_IMAGE2D_ARRAY:
576                 case CL_MEM_OBJECT_IMAGE3D:
577                     imageInfo.image_slice_pitch = imageInfo.image_row_pitch * imageInfo.image_height;
578                     break;
579             }
580 
581             image = malloc( imageInfo.image_width * imageInfo.image_height * imageInfo.image_depth * pixelSize *
582                            ((imageInfo.image_array_size == 0) ? 1 : imageInfo.image_array_size) );
583             imageObject = clCreateImage( context, imageFlags[i], &imageFormat, &imageInfo, image, &error );
584             if ( error )
585             {
586                 free( image );
587                 test_error( error, "Unable to create image2d (CL_MEM_USE_HOST_PTR) to test with" );
588             }
589 
590             // Make sure image2d is cleaned up appropriately if we encounter an error in the rest of the calls.
591             error = clSetMemObjectDestructorCallback( imageObject, mem_obj_destructor_callback, image );
592             test_error( error, "Unable to set mem object destructor callback" );
593 
594             TEST_MEM_OBJECT_PARAM( imageObject, CL_MEM_HOST_PTR, ptr, image, "host pointer", "%p", void * )
595             ret = test_get_imageObject_info( &imageObject, imageFlags[i], &imageInfo, &imageFormat, pixelSize, context );
596             if (ret)
597                 return ret;
598 
599         }
600         else if ( (imageFlags[i] & CL_MEM_ALLOC_HOST_PTR) && (imageFlags[i] & CL_MEM_COPY_HOST_PTR) )
601         {
602             // Create an image object to test against.
603             image = malloc( imageInfo.image_width * imageInfo.image_height * imageInfo.image_depth * pixelSize *
604                            ((imageInfo.image_array_size == 0) ? 1 : imageInfo.image_array_size) );
605             imageObject = clCreateImage( context, imageFlags[i], &imageFormat, &imageInfo, image, &error );
606             if ( error )
607             {
608                 free( image );
609                 test_error( error, "Unable to create image with (CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR) to test with" );
610             }
611 
612             // Make sure image is cleaned up appropriately if we encounter an error in the rest of the calls.
613             error = clSetMemObjectDestructorCallback( imageObject, mem_obj_destructor_callback, image );
614             test_error( error, "Unable to set mem object destructor callback" );
615             int ret = test_get_imageObject_info( &imageObject, imageFlags[ i ], &imageInfo, &imageFormat, pixelSize, context );
616             if (ret)
617                 return ret;
618 
619             // release image object
620             clReleaseMemObject(imageObject);
621 
622             // Try again with non-zero rowPitch.
623             imageInfo.image_row_pitch = imageInfo.image_width * pixelSize;
624             switch (type)
625             {
626                 case CL_MEM_OBJECT_IMAGE1D_ARRAY:
627                 case CL_MEM_OBJECT_IMAGE2D_ARRAY:
628                 case CL_MEM_OBJECT_IMAGE3D:
629                     imageInfo.image_slice_pitch = imageInfo.image_row_pitch * imageInfo.image_height;
630                     break;
631             }
632 
633             image = malloc( imageInfo.image_width * imageInfo.image_height * imageInfo.image_depth * pixelSize *
634                            ((imageInfo.image_array_size == 0) ? 1 : imageInfo.image_array_size) );
635             imageObject = clCreateImage( context, imageFlags[i], &imageFormat, &imageInfo, image, &error );
636             if ( error )
637             {
638                 free( image );
639                 test_error( error, "Unable to create image with (CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR) to test with" );
640             }
641 
642             // Make sure image is cleaned up appropriately if we encounter an error in the rest of the calls.
643             error = clSetMemObjectDestructorCallback( imageObject, mem_obj_destructor_callback, image );
644             test_error( error, "Unable to set mem object destructor callback" );
645             ret = test_get_imageObject_info( &imageObject, imageFlags[i], &imageInfo, &imageFormat, pixelSize, context );
646             if (ret)
647                 return ret;
648 
649         }
650         else if ( imageFlags[i] & CL_MEM_ALLOC_HOST_PTR )
651         {
652             // Create an image object to test against.
653             imageObject = clCreateImage( context, imageFlags[i], &imageFormat, &imageInfo, NULL, &error );
654             test_error( error, "Unable to create image with (CL_MEM_ALLOC_HOST_PTR) to test with" );
655             int ret = test_get_imageObject_info( &imageObject, imageFlags[i], &imageInfo, &imageFormat, pixelSize, context );
656             if (ret)
657                 return ret;
658 
659         }
660         else if ( imageFlags[i] & CL_MEM_COPY_HOST_PTR )
661         {
662             // Create an image object to test against.
663             image = malloc( imageInfo.image_width * imageInfo.image_height * imageInfo.image_depth * pixelSize *
664                            ((imageInfo.image_array_size == 0) ? 1 : imageInfo.image_array_size) );
665             imageObject = clCreateImage( context, imageFlags[i], &imageFormat, &imageInfo, image, &error );
666             if ( error )
667             {
668                 free( image );
669                 test_error( error, "Unable to create image with (CL_MEM_COPY_HOST_PTR) to test with" );
670             }
671 
672             // Make sure image is cleaned up appropriately if we encounter an error in the rest of the calls.
673             error = clSetMemObjectDestructorCallback( imageObject, mem_obj_destructor_callback, image );
674             test_error( error, "Unable to set mem object destructor callback" );
675             int ret = test_get_imageObject_info( &imageObject, imageFlags[i], &imageInfo, &imageFormat, pixelSize, context );
676             if (ret)
677                 return ret;
678 
679             clReleaseMemObject(imageObject);
680 
681             // Try again with non-zero rowPitch.
682             imageInfo.image_row_pitch = imageInfo.image_width * pixelSize;
683             switch (type)
684             {
685                 case CL_MEM_OBJECT_IMAGE1D_ARRAY:
686                 case CL_MEM_OBJECT_IMAGE2D_ARRAY:
687                 case CL_MEM_OBJECT_IMAGE3D:
688                     imageInfo.image_slice_pitch = imageInfo.image_row_pitch * imageInfo.image_height;
689                     break;
690             }
691 
692             image = malloc( imageInfo.image_width * imageInfo.image_height * imageInfo.image_depth * pixelSize *
693                            ((imageInfo.image_array_size == 0) ? 1 : imageInfo.image_array_size) );
694             imageObject = clCreateImage( context, imageFlags[i], &imageFormat, &imageInfo, image, &error );
695             if ( error )
696             {
697                 free( image );
698                 test_error( error, "Unable to create image with (CL_MEM_COPY_HOST_PTR) to test with" );
699             }
700 
701             // Make sure image is cleaned up appropriately if we encounter an error in the rest of the calls.
702             error = clSetMemObjectDestructorCallback( imageObject, mem_obj_destructor_callback, image );
703             test_error( error, "Unable to set mem object destructor callback" );
704             ret = test_get_imageObject_info( &imageObject, imageFlags[i], &imageInfo, &imageFormat, pixelSize, context );
705             if (ret)
706                 return ret;
707 
708         }
709         else
710         {
711             // Create an image object to test against.
712             imageObject = clCreateImage( context, imageFlags[i], &imageFormat, &imageInfo, NULL, &error );
713             test_error( error, "Unable to create image to test with" );
714             int ret = test_get_imageObject_info( &imageObject, imageFlags[i], &imageInfo, &imageFormat, pixelSize, context );
715             if (ret)
716                 return ret;
717 
718         }
719 
720         clReleaseMemObject( imageObject );
721     }
722 
723     return CL_SUCCESS;
724 }
725 
726 
test_get_image2d_info(cl_device_id deviceID,cl_context context,cl_command_queue ignoreQueue,int num_elements)727 int test_get_image2d_info( cl_device_id deviceID, cl_context context, cl_command_queue ignoreQueue, int num_elements )
728 {
729     return test_get_image_info(deviceID, context, CL_MEM_OBJECT_IMAGE2D);
730 }
731 
test_get_image3d_info(cl_device_id deviceID,cl_context context,cl_command_queue ignoreQueue,int num_elements)732 int test_get_image3d_info( cl_device_id deviceID, cl_context context, cl_command_queue ignoreQueue, int num_elements )
733 {
734     return test_get_image_info(deviceID, context, CL_MEM_OBJECT_IMAGE3D);
735 }
736 
test_get_image1d_info(cl_device_id deviceID,cl_context context,cl_command_queue ignoreQueue,int num_elements)737 int test_get_image1d_info( cl_device_id deviceID, cl_context context, cl_command_queue ignoreQueue, int num_elements )
738 {
739     return test_get_image_info(deviceID, context, CL_MEM_OBJECT_IMAGE1D);
740 }
741 
test_get_image1d_array_info(cl_device_id deviceID,cl_context context,cl_command_queue ignoreQueue,int num_elements)742 int test_get_image1d_array_info( cl_device_id deviceID, cl_context context, cl_command_queue ignoreQueue, int num_elements )
743 {
744     return test_get_image_info(deviceID, context, CL_MEM_OBJECT_IMAGE1D_ARRAY);
745 }
746 
test_get_image2d_array_info(cl_device_id deviceID,cl_context context,cl_command_queue ignoreQueue,int num_elements)747 int test_get_image2d_array_info( cl_device_id deviceID, cl_context context, cl_command_queue ignoreQueue, int num_elements )
748 {
749     return test_get_image_info(deviceID, context, CL_MEM_OBJECT_IMAGE2D_ARRAY);
750 }
751 
752 
753