2 * Copyright © 2012 Blaž Tomažič <blaz.tomazic@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 #include "piglit-util-cl.h"
29 piglit_cl_probe_integer(int64_t value
, int64_t expect
, uint64_t tolerance
)
31 int64_t diff
= value
> expect
? value
-expect
: expect
-value
;
33 if(diff
> tolerance
) {
34 printf("Expecting %"PRId64
" (0x%"PRIx64
") with tolerance %"PRIu64
35 ", but got %"PRId64
" (0x%"PRIx64
")\n",
36 expect
, (uint64_t)expect
, tolerance
, value
, (uint64_t)value
);
44 piglit_cl_probe_uinteger(uint64_t value
, uint64_t expect
, uint64_t tolerance
)
46 uint64_t diff
= value
> expect
? value
-expect
: expect
-value
;
48 if(diff
> tolerance
) {
49 printf("Expecting %"PRIu64
" (0x%"PRIx64
") with tolerance %"PRIu64
50 ", but got %"PRIu64
" (0x%"PRIx64
")\n",
51 expect
, expect
, tolerance
, value
, value
);
58 # define probe_float_check_nan_inf(value, expect) \
59 ((isnan(value) && isnan(expect)) || \
60 (isinf(value) && isinf(expect) && ((value > 0) == (expect > 0))))
62 static float float_from_cl_half(uint32_t in
)
68 uint32_t exponent
= ((in
>> 10) & 0x1f);
69 convert
.bits
= (((in
>> 15) & 0x1) << 31) | //sign
70 ((exponent
? (exponent
!= 0x1f ? exponent
+ 112 : 0xff)
71 : 0) << 23) | //exponent (0 and 1f are special cases)
72 (((in
>> 0) & 0x3ff) << 13); // mantissa
76 bool piglit_cl_probe_half(cl_half value
, cl_half expect
, uint32_t ulp
)
78 // after conversion to float the last 13 digits are 0, adjust ulp
79 return piglit_cl_probe_floating(float_from_cl_half(value
),
80 float_from_cl_half(expect
),
84 bool piglit_cl_probe_floating(float value
, float expect
, uint32_t ulp
)
95 /* Treat infinity and nan separately */
96 if (probe_float_check_nan_inf(value
, expect
)) {
100 /* Check "any value" */
101 if (ulp
>= (1u << 24)) {
105 /* expect is correctly rounded, 1 ULP is the distance to next
106 * representable value */
107 float direction
= signbit(expect
) ? -INFINITY
: INFINITY
;
108 float one_ulp
= nextafterf(expect
, direction
) - expect
;
109 float tolerance
= fabsf(ulp
* one_ulp
);
111 diff
= fabsf(value
- expect
);
113 if (diff
> tolerance
|| isnan(value
)) {
114 printf("Expecting %f (0x%x) with tolerance %f (%u ulps), but got %f (0x%x)\n",
115 e
.f
, e
.u
, tolerance
, ulp
, v
.f
, v
.u
);
122 bool piglit_cl_probe_double(double value
, double expect
, uint64_t ulp
)
133 /* Treat infinity and nan separately */
134 if (probe_float_check_nan_inf(value
, expect
)) {
137 /* Check "any value" */
138 if (ulp
>= (1ul << 53)) {
142 double direction
= signbit(expect
) ? -INFINITY
: INFINITY
;
143 double one_ulp
= nextafter(expect
, direction
) - expect
;
144 double tolerance
= fabs(ulp
* one_ulp
);
146 diff
= fabs(value
- expect
);
148 if(diff
> tolerance
|| isnan(value
)) {
149 printf("Expecting %f (0x%" PRIx64
") with tolerance %f (%" PRIu64
"), but got %f (0x%" PRIx64
")\n",
150 e
.f
, e
.u
, tolerance
, ulp
, v
.f
, v
.u
);
158 cl_half
convert_cl_half(double in
)
163 } convert
= { .val
= in
};
164 /* Bit 63 is sign bit */
165 cl_half sign
= (cl_half
)((convert
.bits
>> 63) << 15);
166 /* Get only the top 10 bits of mantissa */
167 cl_half mantissa
= (cl_half
)((convert
.bits
>> 42) & 0x3ff);
168 /* 11 bits of exponent */
169 uint64_t exp
= (convert
.bits
>> 52) & 0x7ff;
170 /* 0 and ~0 are special cases that are not adjusted,
171 * half bias is 15, double bias is 1023 */
172 cl_half exponent
= ((exp
== 0 || exp
== 0x7ff) ? (exp
& 0x3ff) :
174 return (sign
| exponent
| mantissa
);
178 piglit_cl_check_error(cl_int error
, cl_int expected_error
)
180 if (error
== expected_error
) {
185 * If the lookup of the error's name is successful, then print
186 * Unexpected CL error: NAME DEC
188 * Unexpected CL error: DEC
190 printf("Unexpected CL error: %s %d\n",
191 piglit_cl_get_error_name(error
), error
);
193 /* Print the expected error, but only if an error was really expected. */
194 if (expected_error
!= CL_SUCCESS
) {
195 printf("Expected CL error: %s %d\n",
196 piglit_cl_get_error_name(expected_error
),
204 piglit_cl_expect_error(cl_int error
,
205 cl_int expected_error
,
206 enum piglit_result result
)
208 if(!piglit_cl_check_error(error
, expected_error
)) {
209 piglit_report_result(result
);
214 piglit_cl_get_platform_version(cl_platform_id platform
)
216 char* version_string
;
217 const char *version_number_string
;
224 * OpenCL<space><major_version.minor_version><space><platform-specific information>
226 version_string
= piglit_cl_get_platform_info(platform
, CL_PLATFORM_VERSION
);
228 /* skip to version number */
229 version_number_string
= version_string
+ 6;
231 /* Interpret version number */
232 scanf_count
= sscanf(version_number_string
, "%i.%i", &major
, &minor
);
233 if (scanf_count
!= 2) {
234 printf("Unable to interpret CL_PLATFORM_VERSION string: %s\n",
236 free(version_string
);
237 piglit_report_result(PIGLIT_FAIL
);
239 free(version_string
);
241 return 10*major
+minor
;
245 piglit_cl_require_platform_version(cl_platform_id platform
,
246 int required_version_times_10
)
248 if (piglit_cl_get_platform_version(platform
) < required_version_times_10
) {
249 printf("Test requires OpenCL version %g\n",
250 required_version_times_10
/ 10.0);
251 piglit_report_result(PIGLIT_SKIP
);
256 piglit_cl_get_device_version(cl_device_id device
)
258 char* version_string
;
259 const char *version_number_string
;
266 * OpenCL<space><major_version.minor_version><space><platform-specific information>
268 version_string
= piglit_cl_get_device_info(device
, CL_DEVICE_VERSION
);
270 /* skip to version number */
271 version_number_string
= version_string
+ 6;
273 /* Interpret version number */
274 scanf_count
= sscanf(version_number_string
, "%i.%i", &major
, &minor
);
275 if (scanf_count
!= 2) {
276 printf("Unable to interpret CL_DEVICE_VERSION string: %s\n",
278 free(version_string
);
279 piglit_report_result(PIGLIT_FAIL
);
281 free(version_string
);
283 return 10*major
+minor
;
287 piglit_cl_require_device_version(cl_device_id device
,
288 int required_version_times_10
)
290 if (piglit_cl_get_device_version(device
) < required_version_times_10
) {
291 printf("Test requires OpenCL version %g\n",
292 required_version_times_10
/ 10.0);
293 piglit_report_result(PIGLIT_SKIP
);
298 piglit_cl_get_device_cl_c_version(cl_device_id device
)
300 char* version_string
;
301 const char *version_number_string
;
306 /* OpenCL 1.0 does not have enum CL_DEVICE_OPENCL_C_VERSION */
307 if(piglit_cl_get_device_version(device
) == 10) {
313 * OpenCL<space>C<space><major_version.minor_version><space><vendor-specific information>
315 version_string
= piglit_cl_get_device_info(device
,
316 CL_DEVICE_OPENCL_C_VERSION
);
318 /* skip to version number */
319 version_number_string
= version_string
+ 8;
321 /* Interpret version number */
322 scanf_count
= sscanf(version_number_string
, "%i.%i", &major
, &minor
);
323 if (scanf_count
!= 2) {
324 printf("Unable to interpret CL_DEVICE_OPENCL_C_VERSION string: %s\n",
326 free(version_string
);
327 piglit_report_result(PIGLIT_FAIL
);
329 free(version_string
);
331 return 10*major
+minor
;
335 piglit_cl_require_device_cl_c_version(cl_device_id device
,
336 int required_version_times_10
)
338 if (piglit_cl_get_device_cl_c_version(device
) < required_version_times_10
) {
339 printf("Test requires OpenCL C version %g\n",
340 required_version_times_10
/ 10.0);
341 piglit_report_result(PIGLIT_SKIP
);
345 struct _program_build_info_args
{
349 struct _kernel_work_group_info_args
{
355 piglit_cl_get_info(void* fn_ptr
, void* obj
, cl_uint param
)
359 void* param_ptr
= NULL
;
362 if(fn_ptr
== clGetPlatformInfo
) {
363 errNo
= clGetPlatformInfo(*(cl_platform_id
*)obj
, param
, 0, NULL
,
365 } else if(fn_ptr
== clGetDeviceInfo
) {
366 errNo
= clGetDeviceInfo(*(cl_device_id
*)obj
, param
, 0, NULL
,
368 } else if(fn_ptr
== clGetContextInfo
) {
369 errNo
= clGetContextInfo(*(cl_context
*)obj
, param
, 0, NULL
,
371 } else if(fn_ptr
== clGetCommandQueueInfo
) {
372 errNo
= clGetCommandQueueInfo(*(cl_command_queue
*)obj
, param
, 0, NULL
,
374 } else if(fn_ptr
== clGetMemObjectInfo
) {
375 errNo
= clGetMemObjectInfo(*(cl_mem
*)obj
, param
, 0, NULL
,
377 } else if(fn_ptr
== clGetImageInfo
) {
378 errNo
= clGetImageInfo(*(cl_mem
*)obj
, param
, 0, NULL
,
380 } else if(fn_ptr
== clGetSamplerInfo
) {
381 errNo
= clGetSamplerInfo(*(cl_sampler
*)obj
, param
, 0, NULL
,
383 } else if(fn_ptr
== clGetProgramInfo
) {
384 errNo
= clGetProgramInfo(*(cl_program
*)obj
, param
, 0, NULL
,
386 } else if(fn_ptr
== clGetProgramBuildInfo
) {
387 errNo
= clGetProgramBuildInfo(((struct _program_build_info_args
*)obj
)->program
,
388 ((struct _program_build_info_args
*)obj
)->device
,
389 param
, 0, NULL
, ¶m_size
);
390 } else if(fn_ptr
== clGetKernelInfo
) {
391 errNo
= clGetKernelInfo(*(cl_kernel
*)obj
, param
, 0, NULL
,
393 } else if(fn_ptr
== clGetKernelWorkGroupInfo
) {
394 errNo
= clGetKernelWorkGroupInfo(((struct _kernel_work_group_info_args
*)obj
)->kernel
,
395 ((struct _kernel_work_group_info_args
*)obj
)->device
,
396 param
, 0, NULL
, ¶m_size
);
397 } else if(fn_ptr
== clGetEventInfo
) {
398 errNo
= clGetEventInfo(*(cl_event
*)obj
, param
, 0, NULL
,
400 } else if(fn_ptr
== clGetEventProfilingInfo
) {
401 errNo
= clGetEventProfilingInfo(*(cl_event
*)obj
, param
, 0, NULL
,
405 "Trying to get %s information from undefined function.\n",
406 piglit_cl_get_enum_name(param
));
407 piglit_report_result(PIGLIT_FAIL
);
410 if(errNo
== CL_SUCCESS
) {
411 param_ptr
= calloc(param_size
, sizeof(char));
414 if(fn_ptr
== clGetPlatformInfo
) {
415 errNo
= clGetPlatformInfo(*(cl_platform_id
*)obj
, param
,
416 param_size
, param_ptr
, NULL
);
417 } else if(fn_ptr
== clGetDeviceInfo
) {
418 errNo
= clGetDeviceInfo(*(cl_device_id
*)obj
, param
,
419 param_size
, param_ptr
, NULL
);
420 } else if(fn_ptr
== clGetContextInfo
) {
421 errNo
= clGetContextInfo(*(cl_context
*)obj
, param
,
422 param_size
, param_ptr
, NULL
);
423 } else if(fn_ptr
== clGetCommandQueueInfo
) {
424 errNo
= clGetCommandQueueInfo(*(cl_command_queue
*)obj
,
425 param
, param_size
, param_ptr
, NULL
);
426 } else if(fn_ptr
== clGetMemObjectInfo
) {
427 errNo
= clGetMemObjectInfo(*(cl_mem
*)obj
, param
,
428 param_size
, param_ptr
, NULL
);
429 } else if(fn_ptr
== clGetImageInfo
) {
430 errNo
= clGetImageInfo(*(cl_mem
*)obj
, param
,
431 param_size
, param_ptr
, NULL
);
432 } else if(fn_ptr
== clGetSamplerInfo
) {
433 errNo
= clGetSamplerInfo(*(cl_sampler
*)obj
, param
,
434 param_size
, param_ptr
, NULL
);
435 } else if(fn_ptr
== clGetProgramInfo
) {
436 errNo
= clGetProgramInfo(*(cl_program
*)obj
, param
,
437 param_size
, param_ptr
, NULL
);
438 } else if(fn_ptr
== clGetProgramBuildInfo
) {
439 errNo
= clGetProgramBuildInfo(((struct _program_build_info_args
*)obj
)->program
,
440 ((struct _program_build_info_args
*)obj
)->device
,
441 param
, param_size
, param_ptr
, NULL
);
442 } else if(fn_ptr
== clGetKernelInfo
) {
443 errNo
= clGetKernelInfo(*(cl_kernel
*)obj
, param
,
444 param_size
, param_ptr
, NULL
);
445 } else if(fn_ptr
== clGetKernelWorkGroupInfo
) {
446 errNo
= clGetKernelWorkGroupInfo(((struct _kernel_work_group_info_args
*)obj
)->kernel
,
447 ((struct _kernel_work_group_info_args
*)obj
)->device
,
448 param
, param_size
, param_ptr
, NULL
);
449 } else if(fn_ptr
== clGetEventInfo
) {
450 errNo
= clGetEventInfo(*(cl_event
*)obj
, param
,
451 param_size
, param_ptr
, NULL
);
452 } else if(fn_ptr
== clGetEventProfilingInfo
) {
453 errNo
= clGetEventProfilingInfo(*(cl_event
*)obj
, param
,
454 param_size
, param_ptr
, NULL
);
457 if(errNo
!= CL_SUCCESS
) {
463 if(param_ptr
== NULL
) {
465 "Unable to get %s information (Error: %s)\n",
466 piglit_cl_get_enum_name(param
),
467 piglit_cl_get_error_name(errNo
));
468 piglit_report_result(PIGLIT_FAIL
);
475 piglit_cl_get_platform_info(cl_platform_id platform
, cl_platform_info param
) {
476 return piglit_cl_get_info(clGetPlatformInfo
, &platform
, param
);
480 piglit_cl_get_device_info(cl_device_id device
, cl_device_info param
) {
481 return piglit_cl_get_info(clGetDeviceInfo
, &device
, param
);
485 piglit_cl_get_context_info(cl_context context
, cl_context_info param
) {
486 return piglit_cl_get_info(clGetContextInfo
, &context
, param
);
490 piglit_cl_get_command_queue_info(cl_command_queue command_queue
,
491 cl_command_queue_info param
) {
492 return piglit_cl_get_info(clGetCommandQueueInfo
, &command_queue
, param
);
496 piglit_cl_get_mem_object_info(cl_mem mem_obj
, cl_mem_info param
) {
497 return piglit_cl_get_info(clGetMemObjectInfo
, &mem_obj
, param
);
501 piglit_cl_get_image_info(cl_mem image
, cl_image_info param
) {
502 return piglit_cl_get_info(clGetImageInfo
, &image
, param
);
506 piglit_cl_get_sampler_info(cl_sampler sampler
, cl_sampler_info param
) {
507 return piglit_cl_get_info(clGetSamplerInfo
, &sampler
, param
);
511 piglit_cl_get_program_info(cl_program program
, cl_program_info param
) {
512 return piglit_cl_get_info(clGetProgramInfo
, &program
, param
);
516 piglit_cl_get_program_build_info(cl_program program
, cl_device_id device
,
517 cl_program_build_info param
) {
518 struct _program_build_info_args args
= {
523 return piglit_cl_get_info(clGetProgramBuildInfo
, &args
, param
);
527 piglit_cl_get_kernel_info(cl_kernel kernel
, cl_mem_info param
) {
528 return piglit_cl_get_info(clGetKernelInfo
, &kernel
, param
);
532 piglit_cl_get_kernel_work_group_info(cl_kernel kernel
, cl_device_id device
,
534 struct _kernel_work_group_info_args args
= {
539 return piglit_cl_get_info(clGetKernelWorkGroupInfo
, &args
, param
);
543 piglit_cl_get_event_info(cl_event event
, cl_event_info param
) {
544 return piglit_cl_get_info(clGetEventInfo
, &event
, param
);
548 piglit_cl_get_event_profiling_info(cl_event event
, cl_profiling_info param
) {
549 return piglit_cl_get_info(clGetEventProfilingInfo
, &event
, param
);
553 piglit_cl_is_platform_extension_supported(cl_platform_id platform
,
556 char* extensions
= piglit_cl_get_platform_info(platform
,
557 CL_PLATFORM_EXTENSIONS
);
558 bool supported
= piglit_is_extension_in_string(extensions
, name
);
566 piglit_cl_require_platform_extension(cl_platform_id platform
, const char *name
)
568 if (!piglit_cl_is_platform_extension_supported(platform
, name
)) {
569 printf("Test requires %s platform extension\n", name
);
570 piglit_report_result(PIGLIT_SKIP
);
575 piglit_cl_require_not_platform_extension(cl_platform_id platform
,
578 if (piglit_cl_is_platform_extension_supported(platform
, name
)) {
579 printf("Test requires absence of %s\n platform extension\n", name
);
580 piglit_report_result(PIGLIT_SKIP
);
585 piglit_cl_is_device_extension_supported(cl_device_id device
, const char *name
)
587 char* extensions
= piglit_cl_get_device_info(device
, CL_DEVICE_EXTENSIONS
);
588 bool supported
= piglit_is_extension_in_string(extensions
, name
);
596 piglit_cl_require_device_extension(cl_device_id device
, const char *name
)
598 if (!piglit_cl_is_device_extension_supported(device
, name
)) {
599 printf("Test requires %s device extension\n", name
);
600 piglit_report_result(PIGLIT_SKIP
);
605 piglit_cl_require_not_device_extension(cl_device_id device
, const char *name
)
607 if (piglit_cl_is_device_extension_supported(device
, name
)) {
608 printf("Test requires absence of %s device extension\n", name
);
609 piglit_report_result(PIGLIT_SKIP
);
614 piglit_cl_get_platform_ids(cl_platform_id
** platform_ids
)
617 cl_uint num_platform_ids
;
619 /* get number of platforms */
620 errNo
= clGetPlatformIDs(0, NULL
, &num_platform_ids
);
621 if(errNo
!= CL_SUCCESS
) {
623 "Could not get number of platforms: %s\n",
624 piglit_cl_get_error_name(errNo
));
628 /* get platform list */
629 if(platform_ids
!= NULL
&& num_platform_ids
> 0) {
630 *platform_ids
= malloc(num_platform_ids
* sizeof(cl_platform_id
));
631 errNo
= clGetPlatformIDs(num_platform_ids
, *platform_ids
, NULL
);
632 if(errNo
!= CL_SUCCESS
) {
634 *platform_ids
= malloc(0);
636 "Could not get get platform list: %s\n",
637 piglit_cl_get_error_name(errNo
));
642 return num_platform_ids
;
646 piglit_cl_get_device_ids(cl_platform_id platform_id
, cl_device_type device_type
,
647 cl_device_id
** device_ids
)
650 cl_uint num_device_ids
;
651 cl_uint num_platform_ids
;
652 cl_platform_id
*platform_ids
;
655 /* get platform_ids */
656 num_platform_ids
= piglit_cl_get_platform_ids(&platform_ids
);
658 /* find the right platform */
659 for(i
= 0; i
< num_platform_ids
; i
++) {
660 if(platform_ids
[i
] == platform_id
) {
661 /* get number of devices */
662 errNo
= clGetDeviceIDs(platform_id
,
667 if(errNo
== CL_DEVICE_NOT_FOUND
) {
668 *device_ids
= malloc(0);
671 if(errNo
!= CL_SUCCESS
) {
672 *device_ids
= malloc(0);
674 "Could not get number of devices: %s\n",
675 piglit_cl_get_error_name(errNo
));
679 /* get device list */
680 if(device_ids
!= NULL
&& num_device_ids
> 0) {
681 *device_ids
= malloc(num_device_ids
* sizeof(cl_device_id
));
682 errNo
= clGetDeviceIDs(platform_id
,
687 if(errNo
!= CL_SUCCESS
) {
689 *device_ids
= malloc(0);
691 "Could not get get device list: %s\n",
692 piglit_cl_get_error_name(errNo
));
699 return num_device_ids
;
703 /* received invalid platform_id */
704 fprintf(stderr
, "Trying to get a device from invalid platform_id\n");
706 *device_ids
= malloc(0);
714 piglit_cl_create_context(cl_platform_id platform_id
,
715 const cl_device_id device_ids
[],
716 unsigned int num_devices
)
718 piglit_cl_context context
= malloc(sizeof(struct _piglit_cl_context
));
722 cl_context_properties cl_ctx_properties
[] = {
723 CL_CONTEXT_PLATFORM
, (cl_context_properties
)platform_id
,
727 /* assign platform */
728 context
->platform_id
= platform_id
;
731 context
->num_devices
= num_devices
;
732 context
->device_ids
= malloc(num_devices
* sizeof(cl_device_id
));
733 memcpy(context
->device_ids
, device_ids
, num_devices
* sizeof(cl_device_id
));
735 /* create and assign context */
736 context
->cl_ctx
= clCreateContext(cl_ctx_properties
,
737 context
->num_devices
,
742 if(errNo
!= CL_SUCCESS
) {
743 free(context
->device_ids
);
746 "Could not create context: %s\n",
747 piglit_cl_get_error_name(errNo
));
751 /* create and assign command queues */
752 context
->command_queues
= malloc(num_devices
* sizeof(cl_command_queue
));
753 for(i
= 0; i
< num_devices
; i
++) {
754 context
->command_queues
[i
] = clCreateCommandQueue(context
->cl_ctx
,
755 context
->device_ids
[i
],
758 if(errNo
!= CL_SUCCESS
) {
759 clReleaseContext(context
->cl_ctx
);
760 free(context
->device_ids
);
761 free(context
->command_queues
);
764 "Could not create command queue: %s\n",
765 piglit_cl_get_error_name(errNo
));
774 piglit_cl_release_context(piglit_cl_context context
)
778 if(context
== NULL
) {
782 /* release command queues */
783 for(i
= 0; i
< context
->num_devices
; i
++) {
784 if(clReleaseCommandQueue(context
->command_queues
[i
]) != CL_SUCCESS
) {
785 printf("Command queue already released\n");
789 /* release context */
790 if(clReleaseContext(context
->cl_ctx
) != CL_SUCCESS
) {
791 printf("Context already released\n");
795 free(context
->device_ids
);
796 free(context
->command_queues
);
801 piglit_cl_build_program_with_source_extended(piglit_cl_context context
,
802 cl_uint count
, char** strings
,
803 const char* options
, bool fail
)
808 program
= clCreateProgramWithSource(context
->cl_ctx
,
810 (const char**)strings
,
813 if(errNo
!= CL_SUCCESS
) {
815 "Could not create program with source: %s\n",
816 piglit_cl_get_error_name(errNo
));
820 errNo
= clBuildProgram(program
,
821 context
->num_devices
,
826 if( (!fail
&& errNo
!= CL_SUCCESS
)
827 || ( fail
&& errNo
== CL_SUCCESS
)) {
831 !fail
? "Could not build program: %s\n"
832 : "Program built when it should have failed: %s\n",
833 piglit_cl_get_error_name(errNo
));
835 /*printf("Build log for source:\n");
836 for(i = 0; i < count; i++) {
837 printf("%s\n", strings[i]);
840 for(i
= 0; i
< context
->num_devices
; i
++) {
841 char* device_name
= piglit_cl_get_device_info(context
->device_ids
[i
],
843 char* log
= piglit_cl_get_program_build_info(program
,
844 context
->device_ids
[i
],
845 CL_PROGRAM_BUILD_LOG
);
847 printf("Build log for device %s:\n -------- \n%s\n -------- \n",
855 clReleaseProgram(program
);
863 piglit_cl_build_program_with_source(piglit_cl_context context
, cl_uint count
,
864 char** strings
, const char* options
)
866 return piglit_cl_build_program_with_source_extended(context
, count
, strings
, options
, false);
870 piglit_cl_fail_build_program_with_source(piglit_cl_context context
,
871 cl_uint count
, char** strings
,
874 return piglit_cl_build_program_with_source_extended(context
, count
, strings
, options
, true);
878 piglit_cl_build_program_with_binary_extended(piglit_cl_context context
,
880 unsigned char** binaries
,
881 const char* options
, bool fail
)
886 cl_int
* binary_status
= malloc(sizeof(cl_int
) * context
->num_devices
);
888 program
= clCreateProgramWithBinary(context
->cl_ctx
,
889 context
->num_devices
,
892 (const unsigned char**)binaries
,
895 if(errNo
!= CL_SUCCESS
) {
899 "Could not create program with binary: %s\n",
900 piglit_cl_get_error_name(errNo
));
902 printf("Create error with binaries:\n");
903 for(i
= 0; i
< context
->num_devices
; i
++) {
904 char* device_name
= piglit_cl_get_device_info(context
->device_ids
[i
],
907 printf("Error for %s: %s\n",
909 piglit_cl_get_error_name(binary_status
[i
]));
919 errNo
= clBuildProgram(program
,
920 context
->num_devices
,
925 if( (!fail
&& errNo
!= CL_SUCCESS
)
926 || ( fail
&& errNo
== CL_SUCCESS
)) {
930 !fail
? "Could not build program: %s\n"
931 : "Program built when it should have failed: %s\n",
932 piglit_cl_get_error_name(errNo
));
934 printf("Build log for binaries.\n");
936 for(i
= 0; i
< context
->num_devices
; i
++) {
937 char* device_name
= piglit_cl_get_device_info(context
->device_ids
[i
],
939 char* log
= piglit_cl_get_program_build_info(program
,
940 context
->device_ids
[i
],
941 CL_PROGRAM_BUILD_LOG
);
943 printf("Build log for device %s:\n -------- \n%s\n -------- \n",
951 clReleaseProgram(program
);
959 piglit_cl_build_program_with_binary(piglit_cl_context context
, size_t* lengths
,
960 unsigned char** binaries
,
963 return piglit_cl_build_program_with_binary_extended(context
, lengths
,
969 piglit_cl_fail_build_program_with_binary(piglit_cl_context context
,
971 unsigned char** binaries
,
974 return piglit_cl_build_program_with_binary_extended(context
, lengths
,
980 piglit_cl_create_buffer(piglit_cl_context context
, cl_mem_flags flags
,
986 buffer
= clCreateBuffer(context
->cl_ctx
, flags
, size
, NULL
, &errNo
);
987 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
989 "Could not create buffer: %s\n",
990 piglit_cl_get_error_name(errNo
));
997 piglit_cl_write_buffer(cl_command_queue command_queue
, cl_mem buffer
,
998 size_t offset
, size_t cb
, const void *ptr
)
1002 errNo
= clEnqueueWriteBuffer(command_queue
, buffer
, CL_TRUE
, offset
, cb
,
1003 ptr
, 0, NULL
, NULL
);
1004 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1006 "Could not enqueue buffer write: %s\n",
1007 piglit_cl_get_error_name(errNo
));
1015 piglit_cl_write_whole_buffer(cl_command_queue command_queue
, cl_mem buffer
,
1019 size_t* buffer_size
;
1021 buffer_size
= piglit_cl_get_mem_object_info(buffer
, CL_MEM_SIZE
);
1022 success
= piglit_cl_write_buffer(command_queue
, buffer
, 0, *buffer_size
,
1030 piglit_cl_read_buffer(cl_command_queue command_queue
, cl_mem buffer
,
1031 size_t offset
, size_t cb
, void *ptr
)
1035 errNo
= clEnqueueReadBuffer(command_queue
, buffer
, CL_TRUE
, offset
, cb
, ptr
,
1037 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1039 "Could not enqueue buffer read: %s\n",
1040 piglit_cl_get_error_name(errNo
));
1048 piglit_cl_read_whole_buffer(cl_command_queue command_queue
, cl_mem buffer
,
1052 size_t* buffer_size
;
1054 buffer_size
= piglit_cl_get_mem_object_info(buffer
, CL_MEM_SIZE
);
1055 success
= piglit_cl_read_buffer(command_queue
, buffer
, 0, *buffer_size
,
1063 piglit_cl_get_context_image_support(const piglit_cl_context context
)
1068 for(i
= 0; i
< context
->num_devices
; i
++)
1069 ret
|= piglit_cl_get_device_image_support(context
->device_ids
[i
]);
1075 piglit_cl_get_device_image_support(cl_device_id device
)
1079 cl_bool
*image_support
=
1080 piglit_cl_get_device_info(device
, CL_DEVICE_IMAGE_SUPPORT
);
1083 ret
= *image_support
;
1085 free(image_support
);
1091 piglit_cl_create_image(piglit_cl_context context
, cl_mem_flags flags
,
1092 const cl_image_format
*format
,
1093 const piglit_image_desc
*desc
)
1096 cl_mem image
= NULL
;
1098 #ifdef CL_VERSION_1_2
1099 if (piglit_cl_get_platform_version(context
->platform_id
) >= 12) {
1100 image
= clCreateImage(context
->cl_ctx
, flags
, format
, desc
, NULL
, &errNo
);
1103 if (desc
->image_type
== CL_MEM_OBJECT_IMAGE2D
) {
1104 image
= clCreateImage2D(context
->cl_ctx
, flags
, format
,
1105 desc
->image_width
, desc
->image_height
, 0,
1107 } else if (desc
->image_type
== CL_MEM_OBJECT_IMAGE3D
) {
1108 image
= clCreateImage3D(context
->cl_ctx
, flags
, format
,
1109 desc
->image_width
, desc
->image_height
,
1110 desc
->image_depth
, 0, 0,
1113 errNo
= CL_INVALID_OPERATION
;
1115 "Invalid image mem object type: %s\n",
1116 piglit_cl_get_enum_name(desc
->image_type
));
1118 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1120 "Could not create image: %s\n",
1121 piglit_cl_get_error_name(errNo
));
1128 piglit_cl_write_image(cl_command_queue command_queue
, cl_mem image
,
1129 const size_t *origin
, const size_t *region
,
1134 errNo
= clEnqueueWriteImage(command_queue
, image
, CL_TRUE
, origin
, region
,
1135 0, 0, ptr
, 0, NULL
, NULL
);
1136 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1138 "Could not enqueue image write: %s\n",
1139 piglit_cl_get_error_name(errNo
));
1147 piglit_get_image_region(cl_mem image
, size_t *region
)
1150 cl_mem_object_type
*type
;
1152 type
= piglit_cl_get_mem_object_info(image
, CL_MEM_TYPE
);
1154 p
= piglit_cl_get_image_info(image
, CL_IMAGE_WIDTH
);
1159 #ifdef CL_VERSION_1_2
1160 case CL_MEM_OBJECT_IMAGE1D_ARRAY
:
1161 p
= piglit_cl_get_image_info(image
, CL_IMAGE_ARRAY_SIZE
);
1166 case CL_MEM_OBJECT_IMAGE1D
:
1167 case CL_MEM_OBJECT_IMAGE1D_BUFFER
:
1171 case CL_MEM_OBJECT_IMAGE2D_ARRAY
:
1172 p
= piglit_cl_get_image_info(image
, CL_IMAGE_HEIGHT
);
1175 p
= piglit_cl_get_image_info(image
, CL_IMAGE_ARRAY_SIZE
);
1180 case CL_MEM_OBJECT_IMAGE2D
:
1181 p
= piglit_cl_get_image_info(image
, CL_IMAGE_HEIGHT
);
1186 case CL_MEM_OBJECT_IMAGE3D
:
1187 p
= piglit_cl_get_image_info(image
, CL_IMAGE_HEIGHT
);
1190 p
= piglit_cl_get_image_info(image
, CL_IMAGE_DEPTH
);
1200 piglit_cl_write_whole_image(cl_command_queue command_queue
, cl_mem image
,
1204 size_t origin
[3], region
[3];
1206 memset(origin
, 0, sizeof(origin
));
1207 piglit_get_image_region(image
, region
);
1208 success
= piglit_cl_write_image(command_queue
, image
, origin
, region
, ptr
);
1214 piglit_cl_read_image(cl_command_queue command_queue
, cl_mem image
,
1215 const size_t *origin
, const size_t *region
,
1220 errNo
= clEnqueueReadImage(command_queue
, image
, CL_TRUE
, origin
, region
,
1221 0, 0, ptr
, 0, NULL
, NULL
);
1222 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1224 "Could not enqueue image read: %s\n",
1225 piglit_cl_get_error_name(errNo
));
1233 piglit_cl_read_whole_image(cl_command_queue command_queue
, cl_mem image
,
1237 size_t origin
[3], region
[3];
1239 memset(origin
, 0, sizeof(origin
));
1240 piglit_get_image_region(image
, region
);
1241 success
= piglit_cl_read_image(command_queue
, image
, origin
, region
, ptr
);
1247 piglit_cl_create_kernel(cl_program program
, const char* kernel_name
)
1252 kernel
= clCreateKernel(program
, kernel_name
, &errNo
);
1253 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1255 "Could not create kernel %s: %s\n",
1257 piglit_cl_get_error_name(errNo
));
1264 piglit_cl_create_sampler(piglit_cl_context context
,
1265 cl_bool normalized_coords
,
1266 cl_addressing_mode addressing_mode
,
1267 cl_filter_mode filter_mode
)
1272 sampler
= clCreateSampler(context
->cl_ctx
, normalized_coords
,
1273 addressing_mode
, filter_mode
, &errNo
);
1274 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1276 "Could not create sampler: %s\n",
1277 piglit_cl_get_error_name(errNo
));
1284 piglit_cl_set_kernel_arg(cl_kernel kernel
, cl_uint arg_index
, size_t size
,
1285 const void* arg_value
)
1289 errNo
= clSetKernelArg(kernel
, arg_index
, size
, arg_value
);
1290 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1292 "Could not set kernel argument %u: %s\n",
1294 piglit_cl_get_error_name(errNo
));
1302 piglit_cl_set_kernel_buffer_arg(cl_kernel kernel
, cl_uint arg_index
,
1307 success
= piglit_cl_set_kernel_arg(kernel
, arg_index
, sizeof(cl_mem
),
1311 "Could not set kernel buffer argument %u\n",
1320 piglit_cl_enqueue_ND_range_kernel(cl_command_queue command_queue
,
1321 cl_kernel kernel
, cl_uint work_dim
,
1322 const size_t* global_offset
,
1323 const size_t* global_work_size
,
1324 const size_t* local_work_size
,
1329 errNo
= clEnqueueNDRangeKernel(command_queue
, kernel
, work_dim
,
1330 global_offset
, global_work_size
,
1331 local_work_size
, 0, NULL
, ev
);
1332 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1334 "Could not enqueue ND range kernel: %s\n",
1335 piglit_cl_get_error_name(errNo
));
1343 piglit_cl_execute_ND_range_kernel(cl_command_queue command_queue
,
1344 cl_kernel kernel
, cl_uint work_dim
,
1345 const size_t* global_offset
,
1346 const size_t* global_work_size
,
1347 const size_t* local_work_size
)
1352 if(!piglit_cl_enqueue_ND_range_kernel(command_queue
,
1362 errNo
= clWaitForEvents(1, &ev
);
1365 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1367 "Could not wait for kernel to finish: %s\n",
1368 piglit_cl_get_error_name(errNo
));
1376 piglit_cl_enqueue_task(cl_command_queue command_queue
, cl_kernel kernel
)
1380 errNo
= clEnqueueTask(command_queue
, kernel
,
1382 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1384 "Could not enqueue task: %s\n",
1385 piglit_cl_get_error_name(errNo
));
1393 piglit_cl_execute_task(cl_command_queue command_queue
, cl_kernel kernel
)
1397 if(!piglit_cl_enqueue_task(command_queue
,
1402 errNo
= clFinish(command_queue
);
1403 if(!piglit_cl_check_error(errNo
, CL_SUCCESS
)) {
1405 "Could not wait for kernel to finish: %s\n",
1406 piglit_cl_get_error_name(errNo
));