cl: cl_mem_flags has CL_MEM_KERNEL_READ_AND_WRITE since 2.0
[piglit.git] / tests / util / piglit-util-cl.h
blob820e7e062912f3f5d212673d30a52d5f7e79834b
1 /*
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
13 * Software.
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.
24 #pragma once
25 #ifndef PIGLIT_UTIL_CL_H
26 #define PIGLIT_UTIL_CL_H
28 #include "piglit-util.h"
29 #include "piglit-util-cl-enum.h"
32 /* Define with which version of OpenCL Piglit was compiled */
34 #if defined(CL_VERSION_3_0)
35 # define PIGLIT_CL_VERSION 30
36 #elif defined(CL_VERSION_2_0)
37 # define PIGLIT_CL_VERSION 20
38 #elif defined(CL_VERSION_1_2)
39 # define PIGLIT_CL_VERSION 12
40 #elif defined(CL_VERSION_1_1)
41 # define PIGLIT_CL_VERSION 11
42 #else
43 # define PIGLIT_CL_VERSION 10
44 #endif
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
51 #define DIRECT_CONVERT(to, from) \
52 static inline from convert_##to(from in) \
53 { \
54 return (to)in; \
57 DIRECT_CONVERT(cl_char, int64_t);
58 DIRECT_CONVERT(cl_uchar, uint64_t);
59 DIRECT_CONVERT(cl_short, int64_t);
60 DIRECT_CONVERT(cl_ushort, uint64_t);
61 DIRECT_CONVERT(cl_int, int64_t);
62 DIRECT_CONVERT(cl_uint, uint64_t);
63 DIRECT_CONVERT(cl_long, int64_t);
64 DIRECT_CONVERT(cl_ulong, uint64_t);
65 DIRECT_CONVERT(cl_float, double);
66 DIRECT_CONVERT(cl_double, double);
68 cl_half convert_cl_half(double in);
71 /* Runtime independent */
73 /**
74 * \brief Probe integer \c value if it compares equal to \c expect with
75 * tolerance \c tolerance.
77 bool piglit_cl_probe_integer(int64_t value, int64_t expect, uint64_t tolerance);
79 /**
80 * \brief Probe unsigned integer \c value if it compares equal to \c expect with
81 * tolerance \c tolerance.
83 bool piglit_cl_probe_uinteger(uint64_t value,
84 uint64_t expect,
85 uint64_t tolerance);
87 /**
88 * \brief Probe half-floating-point \c value if it compares equal to \c expect with
89 * tolerance \c ulp.
91 bool piglit_cl_probe_half(cl_half value, cl_half expect, uint32_t ulp);
93 /**
94 * \brief Probe floating-point \c value if it compares equal to \c expect with
95 * tolerance \c ulp.
97 bool piglit_cl_probe_floating(float value, float expect, uint32_t ulp);
99 /**
100 * \brief Probe double \c value if it compares equal to \c expect with
101 * tolerance \c ulp.
103 bool piglit_cl_probe_double(double value, double expect, uint64_t ulp);
106 * \brief Check for unexpected GL error and report it.
108 * If \c error is other than \c expected_error, then print a diagnostic.
110 * If you expect no error, then set \code expected_error = CL_SUCCESS \endcode.
112 * Returns true if \c error and \c expected_error are the same,
113 * else it returns false.
115 bool piglit_cl_check_error(cl_int error, cl_int expected_error);
118 * \brief Check for unexpected GL error and possibly terminate the test.
120 * If \c error is other than \c expected_error, then print a diagnostic
121 * and terminate the test with the given \c result.
123 * If you expect no error, then set \code expected_error = CL_SUCCESS \endcode.
125 void piglit_cl_expect_error(cl_int error,
126 cl_int expected_error,
127 enum piglit_result result);
129 /* Runtime dependent */
131 /* Info functions */
134 * \brief Get version of OpenCL API for \c platform.
136 * Returned version is multiplied by 10 to make it an integer. For
137 * example, if the CL version is 1.1, the returned value is 11.
139 int piglit_cl_get_platform_version(cl_platform_id platform);
142 * \brief Check for required OpenCL version and possibly terminate the test.
144 * \c required_version_times_10 must be an OpenCL version multiplied by 10.
145 * For example, if the required CL version is 1.1, then the
146 * \c required_version_times_10 should be 11.
148 void piglit_cl_require_platform_version(cl_platform_id platform,
149 int required_version_times_10);
152 * \brief Get version of OpenCL API for \c device.
154 * Returned version is multiplied by 10 to make it an integer. For
155 * example, if the CL version is 1.1, the returned value is 11.
157 int piglit_cl_get_device_version(cl_device_id platform);
160 * \brief Check for required OpenCL version and possibly terminate the test.
162 * \c required_version_times_10 must be an OpenCL version multiplied by 10.
163 * For example, if the required CL version is 1.1, then the
164 * \c required_version_times_10 should be 11.
166 void piglit_cl_require_device_version(cl_device_id device,
167 int required_version_times_10);
170 * \brief Get version of OpenCL C for \c device.
172 * Returned version is multiplied by 10 to make it an integer. For
173 * example, if the CL C version is 1.1, the returned value is 11.
175 int piglit_cl_get_device_cl_c_version(cl_device_id device);
178 * \brief Check for required OpenCL C version and possibly terminate the test.
180 * \c required_version_times_10 must be an OpenCL C version multiplied by 10.
181 * For example, if the required CL C version is 1.1, then the
182 * \c required_version_times_10 should be 11.
184 void piglit_cl_require_device_cl_c_version(cl_device_id device,
185 int required_version_times_10);
188 * \brief Get platform information.
190 * \warning Returned data must be freed by the caller.
192 * \note
193 * Although the returned types of all params are of type char all through
194 * the last version of OpenCL (1.2 as of time of writing), the return type
195 * of this function is void* for future compatibility.
197 void* piglit_cl_get_platform_info(cl_platform_id platform,
198 cl_platform_info param);
201 * \brief Get device information.
203 * \warning Returned data must be freed by the caller.
205 void* piglit_cl_get_device_info(cl_device_id device, cl_device_info param);
208 * \brief Get context information.
210 * \warning Returned data must be freed by the caller.
212 void* piglit_cl_get_context_info(cl_context context, cl_context_info param);
215 * \brief Get command queue information.
217 * \warning Returned data must be freed by the caller.
219 void* piglit_cl_get_command_queue_info(cl_command_queue command_queue,
220 cl_command_queue_info param);
223 * \brief Get memory object information.
225 * \warning Returned data must be freed by the caller.
227 void* piglit_cl_get_mem_object_info(cl_mem mem_obj, cl_mem_info param);
230 * \brief Get image information.
232 * \warning Returned data must be freed by the caller.
234 void* piglit_cl_get_image_info(cl_mem image, cl_image_info param);
237 * \brief Get sampler information.
239 * \warning Returned data must be freed by the caller.
241 void* piglit_cl_get_sampler_info(cl_sampler sampler, cl_sampler_info param);
244 * \brief Get program information.
246 * \warning Returned data must be freed by the caller.
248 void* piglit_cl_get_program_info(cl_program program, cl_program_info param);
251 * \brief Get program build information.
253 * \warning Returned data must be freed by the caller.
255 void* piglit_cl_get_program_build_info(cl_program program,
256 cl_device_id device,
257 cl_program_build_info param);
260 * \brief Get kernel information.
262 * \warning Returned data must be freed by the caller.
264 void* piglit_cl_get_kernel_info(cl_kernel kernel, cl_mem_info param);
267 * \brief Get kernel work group information.
269 * \warning Returned data must be freed by the caller.
271 void* piglit_cl_get_kernel_work_group_info(cl_kernel kernel,
272 cl_device_id device,
273 cl_mem_info param);
276 * \brief Get event information.
278 * \warning Returned data must be freed by the caller.
280 void* piglit_cl_get_event_info(cl_event event, cl_event_info param);
283 * \brief Get profiling information.
285 * \warning Returned data must be freed by the caller.
287 void* piglit_cl_get_event_profiling_info(cl_event event,
288 cl_profiling_info param);
290 /* Extensions */
293 * \brief Check if platform extension is supported
295 * \pre name is not null
297 bool piglit_cl_is_platform_extension_supported(cl_platform_id platform,
298 const char *name);
301 * \brief Check for required OpenCL platform extension and possibly
302 * terminate the test.
304 void piglit_cl_require_platform_extension(cl_platform_id platform,
305 const char *name);
308 * \brief Check for not required OpenCL platform extension and possibly
309 * terminate the test.
311 void piglit_cl_require_not_platform_extension(cl_platform_id platform,
312 const char *name);
315 * \brief Check if device extension is supported
317 * \pre name is not null
319 bool piglit_cl_is_device_extension_supported(cl_device_id device,
320 const char *name);
323 * \brief Check for required OpenCL device extension and possibly
324 * terminate the test.
326 void piglit_cl_require_device_extension(cl_device_id device, const char *name);
329 * \brief Check for not required OpenCL device extension and possibly
330 * terminate the test.
332 void piglit_cl_require_not_device_extension(cl_device_id device,
333 const char *name);
335 /* Helper functions */
338 * \brief Get all available platforms.
340 * \warning Caller must free the allocated platform array.
342 * @param platform_ids Address to store a pointer to platform ids list.
343 * @return Number of stored platform ids.
345 unsigned int piglit_cl_get_platform_ids(cl_platform_id** platform_ids);
348 * \brief Get all available devices on platform \c platform_id.
350 * \warning Caller must free the allocated device array.
352 * @param platform_id Platform from which to get platforms.
353 * @param device_type A bitfield to filter device types.
354 * @param device_ids Address to store a pointer to device ids list.
355 * @return Number of stored device ids.
357 unsigned int piglit_cl_get_device_ids(cl_platform_id platform_id,
358 cl_device_type device_type,
359 cl_device_id** device_ids);
362 * \brief Helper context.
364 * Helper context struct for easier OpenCL context manipulation.
366 struct _piglit_cl_context {
367 cl_platform_id platform_id; /**< Platform used to create context. */
368 cl_context cl_ctx; /**< OpenCL context. */
370 unsigned int num_devices; /**< Number of members in \c device_ids and \c
371 command_queues */
373 cl_device_id* device_ids; /**< Device ids available in the context. */
374 cl_command_queue* command_queues; /**< Command queues available in the
375 context. Each command queue is
376 assigned to device id in \c
377 device_ids with the same index. */
379 typedef struct _piglit_cl_context* piglit_cl_context;
382 * \brief Create \c piglit_cl_context
384 * Create a helper context from platform id \c platform_id and
385 * device ids \c device_ids.
387 * @param context Context struct to fill.
388 * @param platform_id Platform from which to create context.
389 * @param device_ids Device ids to add to context.
390 * @param num_devices Number of members in \c device_ids.
391 * @return Return \c true on success.
393 piglit_cl_context
394 piglit_cl_create_context(cl_platform_id platform_id,
395 const cl_device_id device_ids[],
396 unsigned int num_devices);
399 * \brief Release \c piglit_cl_context
401 * Free memory used by \c context and release the generated context
402 * and memory queues.
404 * @param context Context to release.
406 void
407 piglit_cl_release_context(piglit_cl_context context);
410 * \brief Create and build a program with source.
412 * Create and build a program with source for all devices in
413 * \c piglit_cl_context.
415 * @param context Context on which to create and build program.
416 * @param count Number of strings in \c strings.
417 * @param string Array of pointers to NULL-terminated source strings.
418 * @param options NULL-terminated string that describes build options.
419 * @return Built program or NULL on fail.
421 cl_program
422 piglit_cl_build_program_with_source(piglit_cl_context context,
423 cl_uint count,
424 char** strings,
425 const char* options);
428 * \brief Create and try to build a program with invalid source.
430 * Create and try to build a program with invalid source for all devices
431 * in \c piglit_cl_context.
433 * @param context Context on which to create and build program.
434 * @param count Number of strings in \c strings.
435 * @param string Array of pointers to NULL-terminated source strings.
436 * @param options NULL-terminated string that describes build options.
437 * @return Unsuccessfully built program or NULL on fail.
439 cl_program
440 piglit_cl_fail_build_program_with_source(piglit_cl_context context,
441 cl_uint count,
442 char** strings,
443 const char* options);
446 * \brief Create and build a program with binary.
448 * Create and build aprogram with binary for all devices in
449 * \c piglit_cl_context.
451 * @param context Context on which to create and build program.
452 * @param lengths Lengths of binaries in \c binaries.
453 * @param binaries Array of pointers to binaries.
454 * @param options NULL-terminated string that describes build options.
455 * @return Built program or NULL on fail.
457 cl_program
458 piglit_cl_build_program_with_binary(piglit_cl_context context,
459 size_t* lengths,
460 unsigned char** binaries,
461 const char* options);
464 * \brief Create and try to build a program with invalid binary.
466 * Create and try to build a program with invalid binary for all devices
467 * in \c piglit_cl_context.
469 * @param context Context on which to create and build program.
470 * @param lengths Lengths of binaries in \c binaries.
471 * @param binaries Array of pointers to binaries.
472 * @param options NULL-terminated string that describes build options.
473 * @return Unsuccessfully built program or NULL on fail.
475 cl_program
476 piglit_cl_fail_build_program_with_binary(piglit_cl_context context,
477 size_t* lengths,
478 unsigned char** binaries,
479 const char* options);
482 * \brief Create a buffer.
484 * @param context Context on which to create buffer.
485 * @param flags Memory flags.
486 * @param size Size of created buffer.
487 * @return Created buffer or NULL on fail.
489 cl_mem
490 piglit_cl_create_buffer(piglit_cl_context context,
491 cl_mem_flags flags,
492 size_t size);
495 * \brief Blocking write to a buffer.
497 * @param command_queue Command queue to enqueue operation on.
498 * @param buffer Memory buffer to write to.
499 * @param offset Offset in buffer.
500 * @param cb Size of data in bytes.
501 * @param ptr Pointer to data to be written to buffer.
502 * @return \c true on success, \c false otherwise.
504 bool
505 piglit_cl_write_buffer(cl_command_queue command_queue,
506 cl_mem buffer,
507 size_t offset,
508 size_t cb,
509 const void *ptr);
512 * \brief Blocking write to a whole buffer.
514 * \warning \c ptr must point to memory space which is equal or larger
515 * in size than \c buffer.
517 * @param command_queue Command queue to enqueue operation on.
518 * @param buffer Memory buffer to write to.
519 * @param ptr Pointer to data to be written to buffer.
520 * @return \c true on success, \c false otherwise.
522 bool
523 piglit_cl_write_whole_buffer(cl_command_queue command_queue,
524 cl_mem buffer,
525 const void *ptr);
528 * \brief Blocking read from a buffer.
530 * @param command_queue Command queue to enqueue operation on.
531 * @param buffer Memory buffer to read from.
532 * @param offset Offset in buffer.
533 * @param cb Size of data in bytes.
534 * @param ptr Pointer to data to be written from buffer.
535 * @return \c true on success, \c false otherwise.
537 bool
538 piglit_cl_read_buffer(cl_command_queue command_queue,
539 cl_mem buffer,
540 size_t offset,
541 size_t cb,
542 void *ptr);
545 * \brief Blocking read from a whole buffer.
547 * \warning \c ptr must point to memory space which is equal or larger
548 * in size than \c buffer.
550 * @param command_queue Command queue to enqueue operation on.
551 * @param buffer Memory buffer to read from.
552 * @param ptr Pointer to data to be written from buffer.
553 * @return \c true on success, \c false otherwise.
555 bool
556 piglit_cl_read_whole_buffer(cl_command_queue command_queue,
557 cl_mem buffer,
558 void *ptr);
559 #ifdef CL_VERSION_1_2
560 typedef cl_image_desc piglit_image_desc;
561 #else
562 /** Taken from OpenCL 1.2 specs 5.3.1.2 */
563 typedef struct {
564 cl_mem_object_type image_type;
565 size_t image_width;
566 size_t image_height;
567 size_t image_depth;
568 size_t image_array_size;
569 size_t image_row_pitch;
570 size_t image_slice_pitch;
571 cl_uint num_mip_levels;
572 cl_uint num_samples;
573 cl_mem buffer;
574 } piglit_image_desc;
575 #endif
578 * \brief Get context image support.
580 * @param context Context on which to create image.
581 * @return True if there is one device with image support.
583 bool
584 piglit_cl_get_context_image_support(const piglit_cl_context context);
587 * \brief Get device image support.
589 * @param device Context on which to create image.
590 * @return True if there the device has image support.
592 bool
593 piglit_cl_get_device_image_support(cl_device_id device);
597 * \brief Create an image.
599 * @param context Context on which to create image.
600 * @param flags Memory flags.
601 * @param format Image format.
602 * @param desc Image descriptor.
603 * @return Created image or NULL on fail.
605 cl_mem
606 piglit_cl_create_image(piglit_cl_context context,
607 cl_mem_flags flags,
608 const cl_image_format *format,
609 const piglit_image_desc *desc);
612 * \brief Blocking write to an image.
614 * @param command_queue Command queue to enqueue operation on.
615 * @param image Image to write to.
616 * @param origin (x, y, z) offset in pixels.
617 * @param region (width, height, depht) size in pixels.
618 * @param ptr Pointer to data to be written to image.
619 * @return \c true on success, \c false otherwise.
621 bool
622 piglit_cl_write_image(cl_command_queue command_queue,
623 cl_mem image,
624 const size_t *origin,
625 const size_t *region,
626 const void *ptr);
629 * \brief Blocking write to the entire area of an image.
631 * \warning \c ptr must point to memory space which is equal or larger
632 * in size than \c image.
634 * @param command_queue Command queue to enqueue operation on.
635 * @param image Image to write to.
636 * @param ptr Pointer to data to be written to image.
637 * @return \c true on success, \c false otherwise.
639 bool
640 piglit_cl_write_whole_image(cl_command_queue command_queue,
641 cl_mem image,
642 const void *ptr);
645 * \brief Blocking read from an image.
647 * @param command_queue Command queue to enqueue operation on.
648 * @param image Image to read from.
649 * @param origin (x, y, z) offset in pixels.
650 * @param region (width, height, depht) size in pixels.
651 * @param ptr Pointer to data read from image.
652 * @return \c true on success, \c false otherwise.
654 bool
655 piglit_cl_read_image(cl_command_queue command_queue,
656 cl_mem image,
657 const size_t *origin,
658 const size_t *region,
659 void *ptr);
662 * \brief Blocking read of the full contents of an image.
664 * \warning \c ptr must point to memory space which is equal or larger
665 * in size than \c image.
667 * @param command_queue Command queue to enqueue operation on.
668 * @param image Image to read from.
669 * @param ptr Pointer to data read from image.
670 * @return \c true on success, \c false otherwise.
672 bool
673 piglit_cl_read_whole_image(cl_command_queue command_queue,
674 cl_mem image,
675 void *ptr);
678 * \brief Create a sampler.
680 * @param context Context on which to create image.
681 * @param normalized_coords Use normalized coords if true.
682 * @param addressing_mode Addressing mode.
683 * @param filter_mode Filter mode.
684 * @return Created sampler or NULL on fail.
686 cl_sampler
687 piglit_cl_create_sampler(piglit_cl_context context,
688 cl_bool normalized_coords,
689 cl_addressing_mode addressing_mode,
690 cl_filter_mode filter_mode);
693 * \brief Create a kernel.
695 * @param context Program on which to create a kernel.
696 * @param kernel_name Kernel name.
697 * @return Created kernel or NULL on fail.
699 cl_kernel
700 piglit_cl_create_kernel(cl_program program, const char* kernel_name);
703 * \brief Set kernel argument.
705 * @param kernel Kernel for which to set an argument.
706 * @param arg_index Argument index.
707 * @param size Size of argument.
708 * @param arg_value Pointer to argument value.
709 * @return \c true on success, \c false otherwise.
711 bool
712 piglit_cl_set_kernel_arg(cl_kernel kernel,
713 cl_uint arg_index,
714 size_t size,
715 const void* arg_value);
718 * \brief Set kernel buffer argument.
720 * @param kernel Kernel for which to set an buffer argument.
721 * @param arg_index Argument index.
722 * @param buffer Buffer to be set as argument.
723 * @return \c true on success, \c false otherwise.
725 bool
726 piglit_cl_set_kernel_buffer_arg(cl_kernel kernel,
727 cl_uint arg_index,
728 cl_mem *buffer);
731 * \brief Enqueue ND-range kernel.
733 * @param command_queue Command queue to enqueue operation on.
734 * @param kernel Kernel to be enqueued.
735 * @param work_dim Work dimensions.
736 * @param global_offset Global offset.
737 * @param global_work_size Global work sizes.
738 * @param local_work_size Local work sizes.
739 * @param ev Location to store execution event.
740 * @return \c true on success, \c false otherwise.
742 bool
743 piglit_cl_enqueue_ND_range_kernel(cl_command_queue command_queue,
744 cl_kernel kernel,
745 cl_uint work_dim,
746 const size_t* global_offset,
747 const size_t* global_work_size,
748 const size_t* local_work_size,
749 cl_event *ev);
752 * \brief Enqueue ND-range kernel and wait it to complete.
754 * @param command_queue Command queue to enqueue operation on.
755 * @param kernel Kernel to be enqueued.
756 * @param work_dim Work dimensions.
757 * @param global_offset Global offset.
758 * @param global_work_size Global work sizes.
759 * @param local_work_size Local work sizes.
760 * @return \c true on success, \c false otherwise.
762 bool
763 piglit_cl_execute_ND_range_kernel(cl_command_queue command_queue,
764 cl_kernel kernel,
765 cl_uint work_dim,
766 const size_t* global_offset,
767 const size_t* global_work_size,
768 const size_t* local_work_size);
771 * \brief Enqueue kernel task.
773 * @param command_queue Command queue to enqueue operation on.
774 * @param kernel Kernel to be enqueued.
775 * @return \c true on success, \c false otherwise.
777 bool
778 piglit_cl_enqueue_task(cl_command_queue command_queue, cl_kernel kernel);
781 * \brief Enqueue kernel task and wait it to complete.
783 * @param command_queue Command queue to enqueue operation on.
784 * @param kernel Kernel to be enqueued.
785 * @return \c true on success, \c false otherwise.
787 bool
788 piglit_cl_execute_task(cl_command_queue command_queue, cl_kernel kernel);
790 #ifdef __cplusplus
791 } /* end extern "C" */
792 #endif
794 #endif //PIGLIT_UTIL_CL_H