diff --git a/src/command_queue.rs b/src/command_queue.rs index 06ca79f..34e7ef9 100644 --- a/src/command_queue.rs +++ b/src/command_queue.rs @@ -14,6 +14,7 @@ //! `OpenCL` Command Queue API. +#![allow(unused_unsafe)] #![allow(non_camel_case_types, deprecated)] #![allow( clippy::too_many_arguments, @@ -31,26 +32,6 @@ pub use opencl_sys::{ CL_SUCCESS, }; -use opencl_sys::{ - clCreateCommandQueue, clEnqueueBarrierWithWaitList, clEnqueueCopyBuffer, - clEnqueueCopyBufferRect, clEnqueueCopyBufferToImage, clEnqueueCopyImage, - clEnqueueCopyImageToBuffer, clEnqueueFillBuffer, clEnqueueFillImage, clEnqueueMapBuffer, - clEnqueueMapImage, clEnqueueMarkerWithWaitList, clEnqueueMigrateMemObjects, - clEnqueueNDRangeKernel, clEnqueueNativeKernel, clEnqueueReadBuffer, clEnqueueReadBufferRect, - clEnqueueReadImage, clEnqueueTask, clEnqueueUnmapMemObject, clEnqueueWriteBuffer, - clEnqueueWriteBufferRect, clEnqueueWriteImage, clFinish, clFlush, clGetCommandQueueInfo, - clReleaseCommandQueue, clRetainCommandQueue, -}; - -#[cfg(feature = "CL_VERSION_2_0")] -use opencl_sys::{ - clCreateCommandQueueWithProperties, clEnqueueSVMFree, clEnqueueSVMMap, clEnqueueSVMMemFill, - clEnqueueSVMMemcpy, clEnqueueSVMUnmap, -}; - -#[cfg(feature = "CL_VERSION_2_1")] -use opencl_sys::clEnqueueSVMMigrateMem; - use super::info_type::InfoType; use super::{api_info_size, api_info_value, api_info_vector}; use libc::{c_void, intptr_t, size_t}; @@ -59,7 +40,7 @@ use std::ptr; /// Create an `OpenCL` host or device command-queue on a specific device. /// -/// Calls `clCreateCommandQueue` to create an `OpenCL` context. +/// Calls `clCreateCommandQueue` to create an `OpenCL` context. /// Deprecated in `CL_VERSION_2_0` by `create_command_queue_with_properties`. /// /// * `context` - a valid `OpenCL` context. @@ -92,7 +73,12 @@ pub unsafe fn create_command_queue( properties: cl_command_queue_properties, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let queue: cl_command_queue = clCreateCommandQueue(context, device, properties, &mut status); + let queue: cl_command_queue = cl_call!(clCreateCommandQueue( + context, + device, + properties, + &mut status + )); if CL_SUCCESS == status { Ok(queue) } else { @@ -102,7 +88,7 @@ pub unsafe fn create_command_queue( /// Create an `OpenCL` host or device command-queue on a specific device. /// -/// Calls clCreateCommandQueueWithProperties to create an `OpenCL` context. +/// Calls clCreateCommandQueueWithProperties to create an `OpenCL` context. /// `CL_VERSION_2_0` onwards. /// /// * `context` - a valid `OpenCL` context. @@ -124,8 +110,12 @@ pub unsafe fn create_command_queue_with_properties( properties: *const cl_queue_properties, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let queue: cl_command_queue = - clCreateCommandQueueWithProperties(context, device, properties, &mut status); + let queue: cl_command_queue = cl_call!(clCreateCommandQueueWithProperties( + context, + device, + properties, + &mut status + )); if CL_SUCCESS == status { Ok(queue) } else { @@ -133,7 +123,7 @@ pub unsafe fn create_command_queue_with_properties( } } -/// Retain an `OpenCL` command-queue. +/// Retain an `OpenCL` command-queue. /// Calls clRetainCommandQueue to increment the command-queue reference count. /// /// * `command_queue` - the `OpenCL` command-queue. @@ -145,7 +135,7 @@ pub unsafe fn create_command_queue_with_properties( /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn retain_command_queue(command_queue: cl_command_queue) -> Result<(), cl_int> { - let status: cl_int = clRetainCommandQueue(command_queue); + let status: cl_int = cl_call!(clRetainCommandQueue(command_queue)); if CL_SUCCESS == status { Ok(()) } else { @@ -153,7 +143,7 @@ pub unsafe fn retain_command_queue(command_queue: cl_command_queue) -> Result<() } } -/// Release an `OpenCL` command-queue. +/// Release an `OpenCL` command-queue. /// Calls clReleaseCommandQueue to decrement the command-queue reference count. /// /// * `command_queue` - the `OpenCL` command-queue. @@ -165,7 +155,7 @@ pub unsafe fn retain_command_queue(command_queue: cl_command_queue) -> Result<() /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn release_command_queue(command_queue: cl_command_queue) -> Result<(), cl_int> { - let status: cl_int = clReleaseCommandQueue(command_queue); + let status: cl_int = cl_call!(clReleaseCommandQueue(command_queue)); if CL_SUCCESS == status { Ok(()) } else { @@ -185,7 +175,7 @@ pub fn get_command_queue_data( get_vector(command_queue, param_name, size) } -/// Get specific information about an `OpenCL` command-queue. +/// Get specific information about an `OpenCL` command-queue. /// Calls `clGetCommandQueueInfo` to get the desired information about the command-queue. /// /// * `command_queue` - the `OpenCL` command-queue. @@ -237,15 +227,15 @@ pub fn get_command_queue_info( } } -/// Flush commands to a device. -/// Calls clFlush to flush an `OpenCL` command-queue. +/// Flush commands to a device. +/// Calls clFlush to flush an `OpenCL` command-queue. /// /// * `command_queue` - the `OpenCL` command-queue. /// /// returns an empty Result or the error code from the `OpenCL` C API function. #[inline] pub fn flush(command_queue: cl_command_queue) -> Result<(), cl_int> { - let status: cl_int = unsafe { clFlush(command_queue) }; + let status: cl_int = unsafe { cl_call!(clFlush(command_queue)) }; if CL_SUCCESS == status { Ok(()) } else { @@ -253,7 +243,7 @@ pub fn flush(command_queue: cl_command_queue) -> Result<(), cl_int> { } } -/// Wait for completion of commands on a device. +/// Wait for completion of commands on a device. /// Calls clFinish and blocks until all previously queued commands have completed. /// /// * `command_queue` - the `OpenCL` command-queue. @@ -261,7 +251,7 @@ pub fn flush(command_queue: cl_command_queue) -> Result<(), cl_int> { /// returns an empty Result or the error code from the `OpenCL` C API function. #[inline] pub fn finish(command_queue: cl_command_queue) -> Result<(), cl_int> { - let status: cl_int = unsafe { clFinish(command_queue) }; + let status: cl_int = unsafe { cl_call!(clFinish(command_queue)) }; if CL_SUCCESS == status { Ok(()) } else { @@ -283,7 +273,7 @@ pub unsafe fn enqueue_read_buffer( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueReadBuffer( + let status: cl_int = cl_call!(clEnqueueReadBuffer( command_queue, buffer, blocking_read, @@ -293,7 +283,7 @@ pub unsafe fn enqueue_read_buffer( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -318,7 +308,7 @@ pub unsafe fn enqueue_read_buffer_rect( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueReadBufferRect( + let status: cl_int = cl_call!(clEnqueueReadBufferRect( command_queue, buffer, blocking_read, @@ -333,7 +323,7 @@ pub unsafe fn enqueue_read_buffer_rect( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -353,7 +343,7 @@ pub unsafe fn enqueue_write_buffer( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueWriteBuffer( + let status: cl_int = cl_call!(clEnqueueWriteBuffer( command_queue, buffer, blocking_write, @@ -363,7 +353,7 @@ pub unsafe fn enqueue_write_buffer( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -388,7 +378,7 @@ pub unsafe fn enqueue_write_buffer_rect( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueWriteBufferRect( + let status: cl_int = cl_call!(clEnqueueWriteBufferRect( command_queue, buffer, blocking_write, @@ -403,7 +393,7 @@ pub unsafe fn enqueue_write_buffer_rect( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -424,7 +414,7 @@ pub unsafe fn enqueue_fill_buffer( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueFillBuffer( + let status: cl_int = cl_call!(clEnqueueFillBuffer( command_queue, buffer, pattern, @@ -434,7 +424,7 @@ pub unsafe fn enqueue_fill_buffer( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -454,7 +444,7 @@ pub unsafe fn enqueue_copy_buffer( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueCopyBuffer( + let status: cl_int = cl_call!(clEnqueueCopyBuffer( command_queue, src_buffer, dst_buffer, @@ -464,7 +454,7 @@ pub unsafe fn enqueue_copy_buffer( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -488,7 +478,7 @@ pub unsafe fn enqueue_copy_buffer_rect( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueCopyBufferRect( + let status: cl_int = cl_call!(clEnqueueCopyBufferRect( command_queue, src_buffer, dst_buffer, @@ -502,7 +492,7 @@ pub unsafe fn enqueue_copy_buffer_rect( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -524,7 +514,7 @@ pub unsafe fn enqueue_read_image( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueReadImage( + let status: cl_int = cl_call!(clEnqueueReadImage( command_queue, image, blocking_read, @@ -536,7 +526,7 @@ pub unsafe fn enqueue_read_image( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -558,7 +548,7 @@ pub unsafe fn enqueue_write_image( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueWriteImage( + let status: cl_int = cl_call!(clEnqueueWriteImage( command_queue, image, blocking_write, @@ -570,7 +560,7 @@ pub unsafe fn enqueue_write_image( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -590,7 +580,7 @@ pub unsafe fn enqueue_fill_image( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueFillImage( + let status: cl_int = cl_call!(clEnqueueFillImage( command_queue, image, fill_color, @@ -599,7 +589,7 @@ pub unsafe fn enqueue_fill_image( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -619,7 +609,7 @@ pub unsafe fn enqueue_copy_image( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueCopyImage( + let status: cl_int = cl_call!(clEnqueueCopyImage( command_queue, src_image, dst_image, @@ -629,7 +619,7 @@ pub unsafe fn enqueue_copy_image( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -649,7 +639,7 @@ pub unsafe fn enqueue_copy_image_to_buffer( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueCopyImageToBuffer( + let status: cl_int = cl_call!(clEnqueueCopyImageToBuffer( command_queue, src_image, dst_buffer, @@ -659,7 +649,7 @@ pub unsafe fn enqueue_copy_image_to_buffer( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -679,7 +669,7 @@ pub unsafe fn enqueue_copy_buffer_to_image( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueCopyBufferToImage( + let status: cl_int = cl_call!(clEnqueueCopyBufferToImage( command_queue, src_buffer, dst_image, @@ -689,7 +679,7 @@ pub unsafe fn enqueue_copy_buffer_to_image( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -697,7 +687,7 @@ pub unsafe fn enqueue_copy_buffer_to_image( } } -/// Note: returns event NOT pointer for consistency with other enqueue functions. +/// Note: returns event NOT pointer for consistency with other enqueue functions. /// The buffer pointer is returned in the `buffer_ptr` mutable reference. #[inline] pub unsafe fn enqueue_map_buffer( @@ -713,7 +703,7 @@ pub unsafe fn enqueue_map_buffer( ) -> Result { let mut event: cl_event = ptr::null_mut(); let mut status: cl_int = CL_INVALID_VALUE; - *buffer_ptr = clEnqueueMapBuffer( + *buffer_ptr = cl_call!(clEnqueueMapBuffer( command_queue, buffer, blocking_map, @@ -724,7 +714,7 @@ pub unsafe fn enqueue_map_buffer( event_wait_list, &mut event, &mut status, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -732,7 +722,7 @@ pub unsafe fn enqueue_map_buffer( } } -/// Note: returns event NOT pointer for consistency with other enqueue functions. +/// Note: returns event NOT pointer for consistency with other enqueue functions. /// The image pointer is returned in the `image_ptr` mutable reference. #[inline] pub unsafe fn enqueue_map_image( @@ -750,7 +740,7 @@ pub unsafe fn enqueue_map_image( ) -> Result<*mut c_void, cl_int> { let mut event: cl_event = ptr::null_mut(); let mut status: cl_int = CL_INVALID_VALUE; - *image_ptr = clEnqueueMapImage( + *image_ptr = cl_call!(clEnqueueMapImage( command_queue, image, blocking_map, @@ -763,7 +753,7 @@ pub unsafe fn enqueue_map_image( event_wait_list, &mut event, &mut status, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -780,14 +770,14 @@ pub unsafe fn enqueue_unmap_mem_object( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueUnmapMemObject( + let status: cl_int = cl_call!(clEnqueueUnmapMemObject( command_queue, memobj, mapped_ptr, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -806,7 +796,7 @@ pub unsafe fn enqueue_migrate_mem_object( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueMigrateMemObjects( + let status: cl_int = cl_call!(clEnqueueMigrateMemObjects( command_queue, num_mem_objects, mem_objects, @@ -814,7 +804,7 @@ pub unsafe fn enqueue_migrate_mem_object( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -834,7 +824,7 @@ pub unsafe fn enqueue_nd_range_kernel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueNDRangeKernel( + let status: cl_int = cl_call!(clEnqueueNDRangeKernel( command_queue, kernel, work_dim, @@ -844,7 +834,7 @@ pub unsafe fn enqueue_nd_range_kernel( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -873,13 +863,13 @@ pub unsafe fn enqueue_task( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueTask( + let status: cl_int = cl_call!(clEnqueueTask( command_queue, kernel, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -900,7 +890,7 @@ pub unsafe fn enqueue_native_kernel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueNativeKernel( + let status: cl_int = cl_call!(clEnqueueNativeKernel( command_queue, user_func, args, @@ -911,7 +901,7 @@ pub unsafe fn enqueue_native_kernel( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -927,12 +917,12 @@ pub unsafe fn enqueue_marker_with_wait_list( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueMarkerWithWaitList( + let status: cl_int = cl_call!(clEnqueueMarkerWithWaitList( command_queue, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -948,12 +938,12 @@ pub unsafe fn enqueue_barrier_with_wait_list( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueBarrierWithWaitList( + let status: cl_int = cl_call!(clEnqueueBarrierWithWaitList( command_queue, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -980,7 +970,7 @@ pub unsafe fn enqueue_svm_free( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMFree( + let status: cl_int = cl_call!(clEnqueueSVMFree( command_queue, num_svm_pointers, svm_pointers, @@ -989,7 +979,7 @@ pub unsafe fn enqueue_svm_free( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1009,7 +999,7 @@ pub unsafe fn enqueue_svm_mem_cpy( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMMemcpy( + let status: cl_int = cl_call!(clEnqueueSVMMemcpy( command_queue, blocking_copy, dst_ptr, @@ -1018,7 +1008,7 @@ pub unsafe fn enqueue_svm_mem_cpy( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1038,7 +1028,7 @@ pub unsafe fn enqueue_svm_mem_fill( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMMemFill( + let status: cl_int = cl_call!(clEnqueueSVMMemFill( command_queue, svm_ptr, pattern, @@ -1047,7 +1037,7 @@ pub unsafe fn enqueue_svm_mem_fill( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1067,7 +1057,7 @@ pub unsafe fn enqueue_svm_map( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMMap( + let status: cl_int = cl_call!(clEnqueueSVMMap( command_queue, blocking_map, flags, @@ -1076,7 +1066,7 @@ pub unsafe fn enqueue_svm_map( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1093,13 +1083,13 @@ pub unsafe fn enqueue_svm_unmap( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMUnmap( + let status: cl_int = cl_call!(clEnqueueSVMUnmap( command_queue, svm_ptr, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1119,7 +1109,7 @@ pub unsafe fn enqueue_svm_migrate_mem( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMMigrateMem( + let status: cl_int = cl_call!(clEnqueueSVMMigrateMem( command_queue, num_svm_pointers, svm_pointers, @@ -1128,7 +1118,7 @@ pub unsafe fn enqueue_svm_migrate_mem( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { diff --git a/src/context.rs b/src/context.rs index 1b758b5..25c9767 100644 --- a/src/context.rs +++ b/src/context.rs @@ -14,6 +14,7 @@ //! `OpenCL` Context API. +#![allow(unused_unsafe)] #![allow(non_camel_case_types)] #![allow(clippy::not_unsafe_ptr_arg_deref)] @@ -24,20 +25,13 @@ pub use opencl_sys::{ CL_SUCCESS, }; -use opencl_sys::{ - clCreateContext, clCreateContextFromType, clGetContextInfo, clReleaseContext, clRetainContext, -}; - -#[cfg(feature = "CL_VERSION_3_0")] -use opencl_sys::clSetContextDestructorCallback; - use super::info_type::InfoType; use super::{api_info_size, api_info_value, api_info_vector}; use libc::{c_char, c_void, intptr_t, size_t}; use std::mem; use std::ptr; -/// Create an `OpenCL` context. +/// Create an `OpenCL` context. /// Calls clCreateContext to create an `OpenCL` context. /// /// * `devices` - a slice of unique devices for an `OpenCL` platform. @@ -48,6 +42,7 @@ use std::ptr; /// /// returns a Result containing the new `OpenCL` context /// or the error code from the `OpenCL` C API function. +#[allow(unused_unsafe)] #[allow(clippy::cast_possible_truncation)] #[inline] pub fn create_context( @@ -58,14 +53,14 @@ pub fn create_context( ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; let context = unsafe { - clCreateContext( + cl_call!(clCreateContext( properties, devices.len() as cl_uint, devices.as_ptr(), pfn_notify, user_data, &mut status, - ) + )) }; if CL_SUCCESS == status { Ok(context) @@ -74,7 +69,7 @@ pub fn create_context( } } -/// Create an `OpenCL` context from a specific device type. +/// Create an `OpenCL` context from a specific device type. /// Calls `clCreateContextFromType` to create an `OpenCL` context. /// /// * `device_type` - the type of `OpenCL` device, see: @@ -95,7 +90,13 @@ pub fn create_context_from_type( ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; let context = unsafe { - clCreateContextFromType(properties, device_type, pfn_notify, user_data, &mut status) + cl_call!(clCreateContextFromType( + properties, + device_type, + pfn_notify, + user_data, + &mut status + )) }; if CL_SUCCESS == status { Ok(context) @@ -104,7 +105,7 @@ pub fn create_context_from_type( } } -/// Retain an `OpenCL` context. +/// Retain an `OpenCL` context. /// Calls clRetainContext to increment the context reference count. /// /// * `context` - the `cl_context` of the `OpenCL` context. @@ -116,7 +117,7 @@ pub fn create_context_from_type( /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn retain_context(context: cl_context) -> Result<(), cl_int> { - let status: cl_int = clRetainContext(context); + let status: cl_int = cl_call!(clRetainContext(context)); if CL_SUCCESS == status { Ok(()) } else { @@ -124,7 +125,7 @@ pub unsafe fn retain_context(context: cl_context) -> Result<(), cl_int> { } } -/// Release an `OpenCL` context. +/// Release an `OpenCL` context. /// Calls clReleaseContext to decrement the context reference count. /// /// * `context` - the `cl_context` of the `OpenCL` context. @@ -136,7 +137,7 @@ pub unsafe fn retain_context(context: cl_context) -> Result<(), cl_int> { /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn release_context(context: cl_context) -> Result<(), cl_int> { - let status: cl_int = clReleaseContext(context); + let status: cl_int = cl_call!(clReleaseContext(context)); if CL_SUCCESS == status { Ok(()) } else { @@ -156,7 +157,7 @@ pub fn get_context_data( get_vector(context, param_name, size) } -/// Get specific information about an `OpenCL` context. +/// Get specific information about an `OpenCL` context. /// Calls `clGetContextInfo` to get the desired information about the context. /// /// * `context` - the `cl_context` of the `OpenCL` context. @@ -187,8 +188,8 @@ pub fn get_context_info( } } -/// Register a callback function with a context that is called when the `context` is destroyed. -/// Calls `clSetContextDestructorCallback`. +/// Register a callback function with a context that is called when the `context` is destroyed. +/// Calls `clSetContextDestructorCallback`. /// `CL_VERSION_3_0` /// /// * `context` - the `cl_context` of the `OpenCL` context. @@ -203,7 +204,11 @@ pub fn set_context_destructor_callback( pfn_notify: Option, user_data: *mut c_void, ) -> Result<(), cl_int> { - let status: cl_int = unsafe { clSetContextDestructorCallback(context, pfn_notify, user_data) }; + let status: cl_int = unsafe { + cl_call!(clSetContextDestructorCallback( + context, pfn_notify, user_data + )) + }; if CL_SUCCESS == status { Ok(()) } else { @@ -253,8 +258,6 @@ mod tests { println!("CL_CONTEXT_NUM_DEVICES: {}", value); assert!(0 < value); - unsafe { - release_context(context).unwrap(); - } + unsafe { release_context(context).unwrap() }; } } diff --git a/src/d3d10.rs b/src/d3d10.rs index 8824938..8701077 100644 --- a/src/d3d10.rs +++ b/src/d3d10.rs @@ -36,28 +36,28 @@ pub unsafe fn get_supported_d3d10_texture_formats_intel( image_type: cl_mem_object_type, ) -> Result, cl_int> { let mut count: cl_uint = 0; - let status: cl_int = clGetSupportedD3D10TextureFormatsINTEL( + let status: cl_int = cl_call!(cl_icd::clGetSupportedD3D10TextureFormatsINTEL( context, flags, image_type, 0, ptr::null_mut(), &mut count, - ); + )); if CL_SUCCESS != status { Err(status) } else if 0 < count { // Get the d3d11_formats. let len = count as usize; let mut ids: Vec = Vec::with_capacity(len); - let status: cl_int = clGetSupportedD3D10TextureFormatsINTEL( + let status: cl_int = cl_call!(cl_d3d10::clGetSupportedD3D10TextureFormatsINTEL( context, flags, image_type, count, ids.as_mut_ptr(), ptr::null_mut(), - ); + )); if CL_SUCCESS == status { Ok(ids) } else { diff --git a/src/d3d11.rs b/src/d3d11.rs index 9cdeef6..6ec271f 100644 --- a/src/d3d11.rs +++ b/src/d3d11.rs @@ -14,7 +14,7 @@ //! FFI bindings for `cl_d3d11.h` //! -//! `cl_d3d11.h` contains `OpenCL` extensions that provide interoperability with `Direct3D` 11. +//! `cl_d3d11.h` contains `OpenCL` extensions that provide interoperability with `Direct3D` 11. //! `OpenCL` extensions are documented in the [OpenCL-Registry](https://github.com/KhronosGroup/OpenCL-Registry) #![allow(clippy::missing_safety_doc)] @@ -37,7 +37,7 @@ pub unsafe fn get_supported_d3d11_texture_formats_intel( plane: cl_uint, ) -> Result, cl_int> { let mut count: cl_uint = 0; - let status: cl_int = clGetSupportedD3D11TextureFormatsINTEL( + let status: cl_int = cl_call!(cl_d3d11::clGetSupportedD3D11TextureFormatsINTEL( context, flags, image_type, @@ -45,14 +45,14 @@ pub unsafe fn get_supported_d3d11_texture_formats_intel( 0, ptr::null_mut(), &mut count, - ); + )); if CL_SUCCESS != status { Err(status) } else if 0 < count { // Get the d3d11_formats. let len = count as usize; let mut ids: Vec = Vec::with_capacity(len); - let status: cl_int = clGetSupportedD3D11TextureFormatsINTEL( + let status: cl_int = cl_call!(cl_d3d11::clGetSupportedD3D11TextureFormatsINTEL( context, flags, image_type, @@ -60,7 +60,7 @@ pub unsafe fn get_supported_d3d11_texture_formats_intel( count, ids.as_mut_ptr(), ptr::null_mut(), - ); + )); if CL_SUCCESS == status { Ok(ids) } else { diff --git a/src/device.rs b/src/device.rs index 92a3c31..e40174e 100644 --- a/src/device.rs +++ b/src/device.rs @@ -14,6 +14,7 @@ //! `OpenCL` Device API. +#![allow(unused_unsafe)] #![allow(non_camel_case_types, non_upper_case_globals)] #![allow( clippy::not_unsafe_ptr_arg_deref, @@ -110,20 +111,13 @@ pub use opencl_sys::{ CL_VERSION_MINOR_MASK, CL_VERSION_PATCH_BITS, CL_VERSION_PATCH_MASK, }; -use opencl_sys::{ - clCreateSubDevices, clGetDeviceIDs, clGetDeviceInfo, clReleaseDevice, clRetainDevice, -}; - -#[cfg(feature = "CL_VERSION_2_1")] -use opencl_sys::{clGetDeviceAndHostTimer, clGetHostTimer, clSetDefaultDeviceCommandQueue}; - use super::info_type::InfoType; use super::{api_info_size, api_info_value, api_info_vector}; use libc::{c_void, intptr_t, size_t}; use std::mem; use std::ptr; -/// Get the list of available devices of the given type on a platform. +/// Get the list of available devices of the given type on a platform. /// Calls clGetDeviceIDs to get the available device ids on the platform. /// # Examples /// ``` @@ -152,8 +146,15 @@ pub fn get_device_ids( ) -> Result, cl_int> { // Get the number of devices of device_type let mut count: cl_uint = 0; - let mut status = - unsafe { clGetDeviceIDs(platform, device_type, 0, ptr::null_mut(), &mut count) }; + let mut status = unsafe { + cl_call!(clGetDeviceIDs( + platform, + device_type, + 0, + ptr::null_mut(), + &mut count + )) + }; if (CL_SUCCESS != status) && (CL_DEVICE_NOT_FOUND != status) { Err(status) @@ -162,13 +163,13 @@ pub fn get_device_ids( let len = count as size_t; let mut ids: Vec = Vec::with_capacity(len); unsafe { - status = clGetDeviceIDs( + status = cl_call!(clGetDeviceIDs( platform, device_type, count, ids.as_mut_ptr(), ptr::null_mut(), - ); + )); ids.set_len(len); }; @@ -194,13 +195,13 @@ pub fn get_device_data( get_vector(device, param_name, size) } -/// Get specific information about an `OpenCL` device. +/// Get specific information about an `OpenCL` device. /// Calls clGetDeviceInfo to get the desired information about the device. /// # Examples /// ``` /// use cl3::platform::get_platform_ids; /// use cl3::device::{get_device_ids, get_device_info, CL_DEVICE_TYPE, CL_DEVICE_TYPE_GPU, CL_DEVICE_VENDOR, CL_DEVICE_VERSION}; -/// use opencl_sys::cl_ulong; +/// use cl3::types::cl_ulong; /// /// let platform_ids = get_platform_ids().unwrap(); /// assert!(0 < platform_ids.len()); @@ -441,12 +442,12 @@ pub fn get_device_info( => { let mut value: [u8; CL_UUID_SIZE_KHR] = [0; CL_UUID_SIZE_KHR]; let status = unsafe { - clGetDeviceInfo( + cl_call!(clGetDeviceInfo( device, param_name, CL_UUID_SIZE_KHR, value.as_mut_ptr().cast::(), - ptr::null_mut(),) + ptr::null_mut(),)) }; if CL_SUCCESS == status { Ok(InfoType::Uuid(value)) @@ -459,12 +460,12 @@ pub fn get_device_info( => { let mut value: [u8; CL_LUID_SIZE_KHR] = [0; CL_LUID_SIZE_KHR]; let status = unsafe { - clGetDeviceInfo( + cl_call!(clGetDeviceInfo( device, param_name, CL_LUID_SIZE_KHR, value.as_mut_ptr().cast::(), - ptr::null_mut(),) + ptr::null_mut(),)) }; if CL_SUCCESS == status { Ok(InfoType::Luid(value)) @@ -566,13 +567,13 @@ fn count_sub_devices( ) -> Result { let mut count: cl_uint = 0; let status: cl_int = unsafe { - clCreateSubDevices( + cl_call!(clCreateSubDevices( in_device, properties.as_ptr(), 0, ptr::null_mut(), &mut count, - ) + )) }; if CL_SUCCESS == status { Ok(count) @@ -604,13 +605,13 @@ pub fn create_sub_devices( let mut ids: Vec = Vec::with_capacity(num_devices as size_t); let status: cl_int = unsafe { ids.set_len(num_devices as size_t); - clCreateSubDevices( + cl_call!(clCreateSubDevices( in_device, properties.as_ptr(), num_devices * mem::size_of::() as cl_uint, ids.as_mut_ptr(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { @@ -620,7 +621,7 @@ pub fn create_sub_devices( } } -/// Retain an `OpenCL` device. +/// Retain an `OpenCL` device. /// Calls `clRetainDevice` to increment the device reference count /// if device is a valid sub-device created by a call to clCreateSubDevices. /// @@ -634,7 +635,7 @@ pub fn create_sub_devices( #[cfg(feature = "CL_VERSION_1_2")] #[inline] pub unsafe fn retain_device(device: cl_device_id) -> Result<(), cl_int> { - let status: cl_int = clRetainDevice(device); + let status: cl_int = cl_call!(clRetainDevice(device)); if CL_SUCCESS == status { Ok(()) } else { @@ -642,7 +643,7 @@ pub unsafe fn retain_device(device: cl_device_id) -> Result<(), cl_int> { } } -/// Release an `OpenCL` device. +/// Release an `OpenCL` device. /// Calls `clReleaseDevice` to decrement the device reference count /// if device is a valid sub-device created by a call to clCreateSubDevices. /// @@ -656,7 +657,7 @@ pub unsafe fn retain_device(device: cl_device_id) -> Result<(), cl_int> { #[cfg(feature = "CL_VERSION_1_2")] #[inline] pub unsafe fn release_device(device: cl_device_id) -> Result<(), cl_int> { - let status: cl_int = clReleaseDevice(device); + let status: cl_int = cl_call!(clReleaseDevice(device)); if CL_SUCCESS == status { Ok(()) } else { @@ -664,8 +665,8 @@ pub unsafe fn release_device(device: cl_device_id) -> Result<(), cl_int> { } } -/// Replace the default command queue on an `OpenCL` device. -/// Calls `clSetDefaultDeviceCommandQueue` to replace the default command queue +/// Replace the default command queue on an `OpenCL` device. +/// Calls `clSetDefaultDeviceCommandQueue` to replace the default command queue /// `CL_VERSION_2_1` /// /// * `context` - the `OpenCL` context used to create `command_queue`. @@ -681,7 +682,13 @@ pub fn set_default_device_command_queue( device: cl_device_id, command_queue: cl_command_queue, ) -> Result<(), cl_int> { - let status: cl_int = unsafe { clSetDefaultDeviceCommandQueue(context, device, command_queue) }; + let status: cl_int = unsafe { + cl_call!(clSetDefaultDeviceCommandQueue( + context, + device, + command_queue + )) + }; if CL_SUCCESS == status { Ok(()) } else { @@ -689,8 +696,8 @@ pub fn set_default_device_command_queue( } } -/// Query device and host timestamps. -/// Calls `clGetDeviceAndHostTimer` +/// Query device and host timestamps. +/// Calls `clGetDeviceAndHostTimer` /// `CL_VERSION_2_1` /// /// * `device` - a valid `OpenCL` device. @@ -702,8 +709,13 @@ pub fn set_default_device_command_queue( pub fn get_device_and_host_timer(device: cl_device_id) -> Result<[cl_ulong; 2], cl_int> { let mut device_timestamp: cl_ulong = 0; let mut host_timestamp: cl_ulong = 0; - let status: cl_int = - unsafe { clGetDeviceAndHostTimer(device, &mut device_timestamp, &mut host_timestamp) }; + let status: cl_int = unsafe { + cl_call!(clGetDeviceAndHostTimer( + device, + &mut device_timestamp, + &mut host_timestamp + )) + }; if CL_SUCCESS == status { Ok([device_timestamp, host_timestamp]) } else { @@ -711,8 +723,8 @@ pub fn get_device_and_host_timer(device: cl_device_id) -> Result<[cl_ulong; 2], } } -/// The current value of the host clock as seen by device. -/// Calls `clGetHostTimer` +/// The current value of the host clock as seen by device. +/// Calls `clGetHostTimer` /// `CL_VERSION_2_1` /// /// * `device` - a valid `OpenCL` `device`. @@ -723,7 +735,7 @@ pub fn get_device_and_host_timer(device: cl_device_id) -> Result<[cl_ulong; 2], #[inline] pub fn get_host_timer(device: cl_device_id) -> Result { let mut host_timestamp: cl_ulong = 0; - let status: cl_int = unsafe { clGetHostTimer(device, &mut host_timestamp) }; + let status: cl_int = unsafe { cl_call!(clGetHostTimer(device, &mut host_timestamp)) }; if CL_SUCCESS == status { Ok(host_timestamp) } else { @@ -1845,7 +1857,11 @@ mod tests { let platform_ids = get_platform_ids().unwrap(); // Choose the platform with the most compliant GPU - let platform_id = platform_ids[1]; + let platform_id = if platform_ids.len() > 1 { + platform_ids[1] + } else { + platform_ids[0] + }; let device_ids = get_device_ids(platform_id, CL_DEVICE_TYPE_GPU).unwrap(); println!("CL_DEVICE_TYPE_GPU count: {}", device_ids.len()); @@ -1854,7 +1870,13 @@ mod tests { let device_id = device_ids[0]; // CL_VERSION_3_0 - let value = get_device_info(device_id, CL_DEVICE_NUMERIC_VERSION).unwrap(); + let value = if let Ok(value) = get_device_info(device_id, CL_DEVICE_NUMERIC_VERSION) { + value + } else { + println!("OpenCL device doesn't support OpenCL 3.0 API"); + return; + }; + let value = cl_uint::from(value); println!("CL_DEVICE_NUMERIC_VERSION: {}", value); assert!(0 < value); diff --git a/src/dx9_media_sharing.rs b/src/dx9_media_sharing.rs index c9992ce..fb23612 100644 --- a/src/dx9_media_sharing.rs +++ b/src/dx9_media_sharing.rs @@ -12,9 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! FFI bindings for `cl_dx9_media_sharing.h` +//! FFI bindings for `cl_dx9_media_sharing.h` //! -//! `cl_ecl_dx9_media_sharing.h` contains `OpenCL` extensions that provide interoperability with `Direct3D` 9. +//! `cl_ecl_dx9_media_sharing.h` contains `OpenCL` extensions that provide interoperability with `Direct3D` 9. //! `OpenCL` extensions are documented in the [OpenCL-Registry](https://github.com/KhronosGroup/OpenCL-Registry) #![allow(non_camel_case_types)] @@ -38,7 +38,7 @@ pub unsafe fn get_device_ids_from_dx9_intel( dx9_device_set: cl_dx9_device_set_intel, ) -> Result, cl_int> { let mut count: cl_uint = 0; - let status: cl_int = clGetDeviceIDsFromDX9INTEL( + let status: cl_int = cl_call!(cl_dx9_media_sharing::clGetDeviceIDsFromDX9INTEL( platform, dx9_device_source, dx9_object, @@ -46,14 +46,14 @@ pub unsafe fn get_device_ids_from_dx9_intel( 0, ptr::null_mut(), &mut count, - ); + )); if CL_SUCCESS != status { Err(status) } else if 0 < count { // Get the device ids. let len = count as usize; let mut ids: Vec = Vec::with_capacity(len); - let status: cl_int = clGetDeviceIDsFromDX9INTEL( + let status: cl_int = cl_call!(cl_dx9_media_sharing::clGetDeviceIDsFromDX9INTEL( platform, dx9_device_source, dx9_object, @@ -61,7 +61,7 @@ pub unsafe fn get_device_ids_from_dx9_intel( count, ids.as_mut_ptr(), ptr::null_mut(), - ); + )); if CL_SUCCESS == status { Ok(ids) } else { @@ -81,14 +81,14 @@ pub unsafe fn create_from_dx9_media_surface_intel( plane: cl_uint, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem = clCreateFromDX9MediaSurfaceINTEL( + let mem = cl_call!(cl_dx9_media_sharing::clCreateFromDX9MediaSurfaceINTEL( context, flags, resource, shared_handle, plane, &mut status, - ); + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -105,14 +105,14 @@ pub unsafe fn enqueue_acquire_dx9_objects_intel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueAcquireDX9ObjectsINTEL( + let status: cl_int = cl_call!(cl_dx9_media_sharing::clEnqueueAcquireDX9ObjectsINTEL( command_queue, num_objects, mem_objects, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -129,14 +129,14 @@ pub unsafe fn enqueue_release_dx9_objects_intel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueReleaseDX9ObjectsINTEL( + let status: cl_int = cl_call!(cl_dx9_media_sharing::clEnqueueReleaseDX9ObjectsINTEL( command_queue, num_objects, mem_objects, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -155,14 +155,16 @@ pub unsafe fn get_supported_dx9_media_surface_formats_intel( plane: cl_uint, ) -> Result, cl_int> { let mut count: cl_uint = 0; - let status: cl_int = clGetSupportedDX9MediaSurfaceFormatsINTEL( - context, - flags, - image_type, - plane, - 0, - ptr::null_mut(), - &mut count, + let status: cl_int = cl_call!( + cl_dx9_media_sharing::clGetSupportedDX9MediaSurfaceFormatsINTEL( + context, + flags, + image_type, + plane, + 0, + ptr::null_mut(), + &mut count, + ) ); if CL_SUCCESS != status { Err(status) @@ -170,14 +172,16 @@ pub unsafe fn get_supported_dx9_media_surface_formats_intel( // Get the dx9 formats. let len = count as usize; let mut ids: Vec = Vec::with_capacity(len); - let status: cl_int = clGetSupportedDX9MediaSurfaceFormatsINTEL( - context, - flags, - image_type, - plane, - count, - ids.as_mut_ptr(), - ptr::null_mut(), + let status: cl_int = cl_call!( + cl_dx9_media_sharing::clGetSupportedDX9MediaSurfaceFormatsINTEL( + context, + flags, + image_type, + plane, + count, + ids.as_mut_ptr(), + ptr::null_mut(), + ) ); if CL_SUCCESS == status { Ok(ids) diff --git a/src/dynamic_library.rs b/src/dynamic_library.rs index 160b72a..fcddf5b 100644 --- a/src/dynamic_library.rs +++ b/src/dynamic_library.rs @@ -14,8 +14,7 @@ //! `OpenCL` dynamic library function call. -#[allow(unused_imports)] -use crate::error_codes::{DLOPEN_FUNCTION_NOT_AVAILABLE, DLOPEN_RUNTIME_LOAD_FAILED}; +use crate::error_codes::DLOPEN_RUNTIME_LOAD_FAILED; use crate::runtime::{load_library, OpenClRuntime}; pub fn load_dynamic_runtime() -> Result<&'static OpenClRuntime, i32> { @@ -26,10 +25,10 @@ pub fn load_dynamic_runtime() -> Result<&'static OpenClRuntime, i32> { macro_rules! cl_call { ($func:ident($($arg:expr),* $(,)?)) => {{ - if let Some(result) = load_dynamic_runtime()?.$func($($arg),*) { + if let Some(result) = $crate::dynamic_library::load_dynamic_runtime()?.$func($($arg),*) { result } else { - return Err(DLOPEN_FUNCTION_NOT_AVAILABLE) + return Err($crate::error_codes::DLOPEN_FUNCTION_NOT_AVAILABLE) } }}; ($namespace:ident::$func:ident($($arg:expr),* $(,)?)) => {{ diff --git a/src/egl.rs b/src/egl.rs index 513f69a..6ea0aab 100644 --- a/src/egl.rs +++ b/src/egl.rs @@ -24,16 +24,16 @@ pub use opencl_sys::{ #[allow(unused_imports)] use std::ptr; -/// Create an `OpenCL` image object, from the `EGLImage` source provided as image. -/// Requires the `cl_khr_egl_image` extension. -/// Calls `clCreateFromEGLImageKHR` to create an `OpenCL` memory object. +/// Create an `OpenCL` image object, from the `EGLImage` source provided as image. +/// Requires the `cl_khr_egl_image` extension. +/// Calls `clCreateFromEGLImageKHR` to create an `OpenCL` memory object. /// /// * `context` - a valid `OpenCL` context created from an `OpenGL` context. /// * `display` - should be of type `EGLDisplay`, cast into the type `CLeglDisplayKHR` -/// * `image` - should be of type `EGLImageKHR`, cast into the type `CLeglImageKHR`. -/// * `flags` - usage information about the memory object being created. +/// * `image` - should be of type `EGLImageKHR`, cast into the type `CLeglImageKHR`. +/// * `flags` - usage information about the memory object being created. /// * `properties` - a null terminated list of property names and their -/// corresponding values. +/// corresponding values. /// /// returns a Result containing the new `OpenCL` image object /// or the error code from the `OpenCL` C API function. @@ -51,7 +51,14 @@ pub unsafe fn create_from_egl_image( properties: *const cl_egl_image_properties_khr, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem = clCreateFromEGLImageKHR(context, display, image, flags, properties, &mut status); + let mem = cl_call!(cl_egl::clCreateFromEGLImageKHR( + context, + display, + image, + flags, + properties, + &mut status + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -59,9 +66,9 @@ pub unsafe fn create_from_egl_image( } } -/// Acquire `OpenCL` memory objects that have been created from EGL resources. -/// Requires the `cl_khr_egl_image` extension. -/// Calls `clEnqueueAcquireEGLObjectsKHR`. +/// Acquire `OpenCL` memory objects that have been created from EGL resources. +/// Requires the `cl_khr_egl_image` extension. +/// Calls `clEnqueueAcquireEGLObjectsKHR`. /// /// * `command_queue` - a valid `OpenCL` `command_queue`. /// * `num_objects` - the number of memory objects to acquire. @@ -85,14 +92,14 @@ pub unsafe fn enqueue_acquire_egl_objects( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueAcquireEGLObjectsKHR( + let status: cl_int = cl_call!(cl_egl::clEnqueueAcquireEGLObjectsKHR( command_queue, num_objects, mem_objects, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -100,9 +107,9 @@ pub unsafe fn enqueue_acquire_egl_objects( } } -/// Release `OpenCL` memory objects that have been created from EGL resources. -/// Requires the `cl_khr_egl_image` extension. -/// Calls `clEnqueueReleaseEGLObjectsKHR`. +/// Release `OpenCL` memory objects that have been created from EGL resources. +/// Requires the `cl_khr_egl_image` extension. +/// Calls `clEnqueueReleaseEGLObjectsKHR`. /// /// * `command_queue` - a valid `OpenCL` `command_queue`. /// * `num_objects` - the number of memory objects to acquire. @@ -126,14 +133,14 @@ pub unsafe fn enqueue_release_egl_objects( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueReleaseEGLObjectsKHR( + let status: cl_int = cl_call!(cl_egl::clEnqueueReleaseEGLObjectsKHR( command_queue, num_objects, mem_objects, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -141,13 +148,13 @@ pub unsafe fn enqueue_release_egl_objects( } } -/// Create an event object linked to an EGL fence sync object. +/// Create an event object linked to an EGL fence sync object. /// Requires the `cl_khr_egl_event` extension -/// Calls `clCreateEventFromEGLSyncKHR`. +/// Calls `clCreateEventFromEGLSyncKHR`. /// /// * `context` - a valid `OpenCL` context. -/// * `sync` - the handle to an `EGLSync` object. -/// * `display` - the handle to an `EGLDisplay`. +/// * `sync` - the handle to an `EGLSync` object. +/// * `display` - the handle to an `EGLDisplay`. /// /// returns a Result containing the new `OpenCL` event /// or the error code from the `OpenCL` C API function. @@ -163,7 +170,12 @@ pub unsafe fn create_event_from_egl_sync_khr( display: CLeglDisplayKHR, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let event: cl_event = clCreateEventFromEGLSyncKHR(context, sync, display, &mut status); + let event: cl_event = cl_call!(cl_egl::clCreateEventFromEGLSyncKHR( + context, + sync, + display, + &mut status + )); if CL_SUCCESS == status { Ok(event) } else { diff --git a/src/event.rs b/src/event.rs index 92350dc..1b37914 100644 --- a/src/event.rs +++ b/src/event.rs @@ -14,6 +14,7 @@ //! `OpenCL` Event Object API. +#![allow(unused_unsafe)] #![allow(non_camel_case_types)] #![allow(clippy::not_unsafe_ptr_arg_deref)] @@ -41,11 +42,6 @@ pub use opencl_sys::cl_egl::{ CL_COMMAND_RELEASE_EGL_OBJECTS_KHR, }; -use opencl_sys::{ - clCreateUserEvent, clGetEventInfo, clGetEventProfilingInfo, clReleaseEvent, clRetainEvent, - clSetEventCallback, clSetUserEventStatus, clWaitForEvents, -}; - use super::info_type::InfoType; use super::{api_info_size, api_info_value, api_info_vector}; use libc::{c_void, intptr_t, size_t}; @@ -53,7 +49,7 @@ use std::fmt; use std::mem; use std::ptr; -/// Wait for `OpenCL` events to complete. +/// Wait for `OpenCL` events to complete. /// Calls `clWaitForEvents`. /// /// * `events` - a slice of `OpenCL` events. @@ -62,7 +58,8 @@ use std::ptr; #[inline] #[allow(clippy::cast_possible_truncation)] pub fn wait_for_events(events: &[cl_event]) -> Result<(), cl_int> { - let status: cl_int = unsafe { clWaitForEvents(events.len() as cl_uint, events.as_ptr()) }; + let status: cl_int = + unsafe { cl_call!(clWaitForEvents(events.len() as cl_uint, events.as_ptr())) }; if CL_SUCCESS == status { Ok(()) } else { @@ -79,7 +76,7 @@ pub fn get_event_data(event: cl_event, param_name: cl_event_info) -> Result Result Result Result { let mut status: cl_int = CL_INVALID_VALUE; - let event: cl_event = unsafe { clCreateUserEvent(context, &mut status) }; + let event: cl_event = unsafe { cl_call!(clCreateUserEvent(context, &mut status)) }; if CL_SUCCESS == status { Ok(event) } else { @@ -127,7 +124,7 @@ pub fn create_user_event(context: cl_context) -> Result { } } -/// Retain an `OpenCL` event. +/// Retain an `OpenCL` event. /// Calls clRetainEvent to increment the event reference count. /// /// * `event` - the `OpenCL` event. @@ -139,7 +136,7 @@ pub fn create_user_event(context: cl_context) -> Result { /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn retain_event(event: cl_event) -> Result<(), cl_int> { - let status: cl_int = clRetainEvent(event); + let status: cl_int = cl_call!(clRetainEvent(event)); if CL_SUCCESS == status { Ok(()) } else { @@ -147,7 +144,7 @@ pub unsafe fn retain_event(event: cl_event) -> Result<(), cl_int> { } } -/// Release an `OpenCL` event. +/// Release an `OpenCL` event. /// Calls `clReleaseEvent` to decrement the event reference count. /// /// * `event` - the `OpenCL` event. @@ -159,7 +156,7 @@ pub unsafe fn retain_event(event: cl_event) -> Result<(), cl_int> { /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn release_event(event: cl_event) -> Result<(), cl_int> { - let status: cl_int = clReleaseEvent(event); + let status: cl_int = cl_call!(clReleaseEvent(event)); if CL_SUCCESS == status { Ok(()) } else { @@ -167,7 +164,7 @@ pub unsafe fn release_event(event: cl_event) -> Result<(), cl_int> { } } -/// Set the execution status of a user event object. +/// Set the execution status of a user event object. /// Calls `clSetUserEventStatus` to set the execution status. /// /// * `event` - the `OpenCL` event. @@ -176,7 +173,7 @@ pub unsafe fn release_event(event: cl_event) -> Result<(), cl_int> { /// returns an empty Result or the error code from the `OpenCL` C API function. #[inline] pub fn set_user_event_status(event: cl_event, execution_status: cl_int) -> Result<(), cl_int> { - let status: cl_int = unsafe { clSetUserEventStatus(event, execution_status) }; + let status: cl_int = unsafe { cl_call!(clSetUserEventStatus(event, execution_status)) }; if CL_SUCCESS == status { Ok(()) } else { @@ -185,7 +182,7 @@ pub fn set_user_event_status(event: cl_event, execution_status: cl_int) -> Resul } /// Register a user callback function for a specific command execution status, -/// Calls `clSetEventCallback` to register a callback function. +/// Calls `clSetEventCallback` to register a callback function. /// /// * `event` - the `OpenCL` event. /// * `pfn_notify` - function pointer to the callback function. @@ -200,12 +197,12 @@ pub fn set_event_callback( user_data: *mut c_void, ) -> Result<(), cl_int> { let status: cl_int = unsafe { - clSetEventCallback( + cl_call!(clSetEventCallback( event, command_exec_callback_type, Some(pfn_notify), user_data, - ) + )) }; if CL_SUCCESS == status { Ok(()) @@ -227,7 +224,7 @@ pub fn get_event_profiling_data( } /// Get profiling information for a command associated with an event when -/// profiling is enabled. +/// profiling is enabled. /// Calls clGetEventProfilingInfo to get the desired information. /// /// * `event` - the `OpenCL` event. diff --git a/src/ext.rs b/src/ext.rs index d1f89ef..89ba1de 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -15,6 +15,7 @@ //! `OpenCL` extensions that don't have external (OpenGL, D3D) dependencies. //! See: [OpenCL Extension Specification](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_Ext.html) +#![allow(unused_unsafe)] #![allow(non_camel_case_types)] #![allow( clippy::not_unsafe_ptr_arg_deref, @@ -44,12 +45,12 @@ pub fn create_command_buffer_khr( ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; let buffer = unsafe { - clCreateCommandBufferKHR( + cl_call!(clCreateCommandBufferKHR( queues.len() as cl_uint, queues.as_ptr(), properties, &mut status, - ) + )) }; if CL_SUCCESS == status { Ok(buffer) @@ -60,7 +61,7 @@ pub fn create_command_buffer_khr( #[cfg(feature = "cl_khr_command_buffer")] pub fn finalize_command_buffer_khr(command_buffer: cl_command_buffer_khr) -> Result<(), cl_int> { - let status: cl_int = unsafe { clFinalizeCommandBufferKHR(command_buffer) }; + let status: cl_int = unsafe { cl_call!(clFinalizeCommandBufferKHR(command_buffer)) }; if CL_SUCCESS == status { Ok(()) } else { @@ -72,7 +73,7 @@ pub fn finalize_command_buffer_khr(command_buffer: cl_command_buffer_khr) -> Res pub unsafe fn retain_command_buffer_khr( command_buffer: cl_command_buffer_khr, ) -> Result<(), cl_int> { - let status: cl_int = clRetainCommandBufferKHR(command_buffer); + let status: cl_int = cl_call!(clRetainCommandBufferKHR(command_buffer)); if CL_SUCCESS == status { Ok(()) } else { @@ -84,7 +85,7 @@ pub unsafe fn retain_command_buffer_khr( pub unsafe fn release_command_buffer_khr( command_buffer: cl_command_buffer_khr, ) -> Result<(), cl_int> { - let status: cl_int = clReleaseCommandBufferKHR(command_buffer); + let status: cl_int = cl_call!(clReleaseCommandBufferKHR(command_buffer)); if CL_SUCCESS == status { Ok(()) } else { @@ -101,14 +102,14 @@ pub unsafe fn enqueue_command_buffer_khr( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueCommandBufferKHR( + let status: cl_int = cl_call!(clEnqueueCommandBufferKHR( num_queues, queues, command_buffer, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -125,14 +126,14 @@ pub unsafe fn command_barrier_with_wait_list_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandBarrierWithWaitListKHR( + let status: cl_int = cl_call!(clCommandBarrierWithWaitListKHR( command_buffer, command_queue, sync_point_wait_list.len() as cl_uint, sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -154,7 +155,7 @@ pub unsafe fn command_copy_buffer_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandCopyBufferKHR( + let status: cl_int = cl_call!(clCommandCopyBufferKHR( command_buffer, command_queue, src_buffer, @@ -166,7 +167,7 @@ pub unsafe fn command_copy_buffer_khr( sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -192,7 +193,7 @@ pub unsafe fn command_copy_buffer_rect_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandCopyBufferRectKHR( + let status: cl_int = cl_call!(clCommandCopyBufferRectKHR( command_buffer, command_queue, src_buffer, @@ -208,7 +209,7 @@ pub unsafe fn command_copy_buffer_rect_khr( sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -230,7 +231,7 @@ pub unsafe fn command_copy_buffer_to_image_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandCopyBufferToImageKHR( + let status: cl_int = cl_call!(clCommandCopyBufferToImageKHR( command_buffer, command_queue, src_buffer, @@ -242,7 +243,7 @@ pub unsafe fn command_copy_buffer_to_image_khr( sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -264,7 +265,7 @@ pub unsafe fn command_copy_image_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandCopyImageKHR( + let status: cl_int = cl_call!(clCommandCopyImageKHR( command_buffer, command_queue, src_image, @@ -276,7 +277,7 @@ pub unsafe fn command_copy_image_khr( sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -298,7 +299,7 @@ pub unsafe fn command_copy_image_to_buffer_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandCopyImageToBufferKHR( + let status: cl_int = cl_call!(clCommandCopyImageToBufferKHR( command_buffer, command_queue, src_image, @@ -310,7 +311,7 @@ pub unsafe fn command_copy_image_to_buffer_khr( sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -332,7 +333,7 @@ pub unsafe fn command_fill_buffer_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandFillBufferKHR( + let status: cl_int = cl_call!(clCommandFillBufferKHR( command_buffer, command_queue, buffer, @@ -344,7 +345,7 @@ pub unsafe fn command_fill_buffer_khr( sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -365,7 +366,7 @@ pub unsafe fn command_fill_image_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandFillImageKHR( + let status: cl_int = cl_call!(clCommandFillImageKHR( command_buffer, command_queue, image, @@ -376,7 +377,7 @@ pub unsafe fn command_fill_image_khr( sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -399,7 +400,7 @@ pub unsafe fn command_nd_range_kernel_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandNDRangeKernelKHR( + let status: cl_int = cl_call!(clCommandNDRangeKernelKHR( command_buffer, command_queue, properties, @@ -412,7 +413,7 @@ pub unsafe fn command_nd_range_kernel_khr( sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -432,7 +433,7 @@ pub unsafe fn command_svm_memcpy_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandSVMMemcpyKHR( + let status: cl_int = cl_call!(clCommandSVMMemcpyKHR( command_buffer, command_queue, dst_ptr, @@ -442,7 +443,7 @@ pub unsafe fn command_svm_memcpy_khr( sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -463,7 +464,7 @@ pub unsafe fn command_svm_mem_fill_khr( sync_point: *mut cl_sync_point_khr, mutable_handle: *mut cl_mutable_command_khr, ) -> Result<(), cl_int> { - let status: cl_int = clCommandSVMMemFillKHR( + let status: cl_int = cl_call!(clCommandSVMMemFillKHR( command_buffer, command_queue, svm_ptr, @@ -474,7 +475,7 @@ pub unsafe fn command_svm_mem_fill_khr( sync_point_wait_list.as_ptr(), sync_point, mutable_handle, - ); + )); if CL_SUCCESS == status { Ok(()) } else { @@ -550,7 +551,7 @@ pub unsafe fn remap_command_buffer_khr( handles_ret: *mut cl_mutable_command_khr, ) -> Result { let mut errcode_ret: cl_int = CL_INVALID_VALUE; - let cmd_buffer = clRemapCommandBufferKHR( + let cmd_buffer = cl_call!(clRemapCommandBufferKHR( command_buffer, automatic, num_queues, @@ -559,7 +560,7 @@ pub unsafe fn remap_command_buffer_khr( handles, handles_ret, &mut errcode_ret, - ); + )); if CL_SUCCESS == errcode_ret { Ok(cmd_buffer) } else { @@ -572,7 +573,7 @@ pub unsafe fn update_mutable_commands_khr( command_buffer: cl_command_buffer_khr, mutable_config: *const cl_mutable_base_config_khr, ) -> Result<(), cl_int> { - let status: cl_int = clUpdateMutableCommandsKHR(command_buffer, mutable_config); + let status: cl_int = cl_call!(clUpdateMutableCommandsKHR(command_buffer, mutable_config)); if CL_SUCCESS == status { Ok(()) } else { @@ -597,7 +598,7 @@ pub unsafe fn set_mem_object_destructor_apple( pfn_notify: Option, user_data: *mut c_void, ) -> Result<(), cl_int> { - let status: cl_int = clSetMemObjectDestructorAPPLE(memobj, pfn_notify, user_data); + let status: cl_int = cl_call!(clSetMemObjectDestructorAPPLE(memobj, pfn_notify, user_data)); if CL_SUCCESS == status { Ok(()) } else { @@ -610,7 +611,7 @@ pub unsafe fn set_mem_object_destructor_apple( pub fn icd_get_platform_ids_khr() -> Result, cl_int> { // Get the number of platforms let mut count: cl_uint = 0; - let mut status = unsafe { clIcdGetPlatformIDsKHR(0, ptr::null_mut(), &mut count) }; + let mut status = unsafe { cl_call!(clIcdGetPlatformIDsKHR(0, ptr::null_mut(), &mut count)) }; if CL_SUCCESS != status { Err(status) @@ -620,7 +621,11 @@ pub fn icd_get_platform_ids_khr() -> Result, cl_int> { let mut ids: Vec = Vec::with_capacity(len); unsafe { ids.set_len(len); - status = clIcdGetPlatformIDsKHR(count, ids.as_mut_ptr(), ptr::null_mut()); + status = cl_call!(clIcdGetPlatformIDsKHR( + count, + ids.as_mut_ptr(), + ptr::null_mut() + )); }; if CL_SUCCESS == status { @@ -637,12 +642,12 @@ pub fn icd_get_platform_ids_khr() -> Result, cl_int> { pub fn create_program_with_il_khr(context: cl_context, il: &[u8]) -> Result { let mut status: cl_int = CL_INVALID_VALUE; let program = unsafe { - clCreateProgramWithILKHR( + cl_call!(clCreateProgramWithILKHR( context, il.as_ptr().cast::(), il.len() as size_t, &mut status, - ) + )) }; if CL_SUCCESS == status { Ok(program) @@ -653,7 +658,7 @@ pub fn create_program_with_il_khr(context: cl_context, il: &[u8]) -> Result Result<(), cl_int> { - let status = clTerminateContextKHR(context); + let status = cl_call!(clTerminateContextKHR(context)); if CL_SUCCESS == status { Ok(()) } else { @@ -668,8 +673,14 @@ pub fn create_command_queue_with_properties_khr( properties: *const cl_queue_properties_khr, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let queue: cl_command_queue = - unsafe { clCreateCommandQueueWithPropertiesKHR(context, device, properties, &mut status) }; + let queue: cl_command_queue = unsafe { + cl_call!(clCreateCommandQueueWithPropertiesKHR( + context, + device, + properties, + &mut status + )) + }; if CL_SUCCESS == status { Ok(queue) } else { @@ -679,7 +690,7 @@ pub fn create_command_queue_with_properties_khr( #[cfg(feature = "cl_ext_device_fission")] pub unsafe fn release_device_ext(device: cl_device_id) -> Result<(), cl_int> { - let status = clReleaseDeviceEXT(device); + let status = cl_call!(clReleaseDeviceEXT(device)); if CL_SUCCESS == status { Ok(()) } else { @@ -689,7 +700,7 @@ pub unsafe fn release_device_ext(device: cl_device_id) -> Result<(), cl_int> { #[cfg(feature = "cl_ext_device_fission")] pub unsafe fn retain_device_ext(device: cl_device_id) -> Result<(), cl_int> { - let status = clRetainDeviceEXT(device); + let status = cl_call!(clRetainDeviceEXT(device)); if CL_SUCCESS == status { Ok(()) } else { @@ -705,13 +716,13 @@ fn count_sub_devices_ext( ) -> Result { let mut count: cl_uint = 0; let status: cl_int = unsafe { - clCreateSubDevicesEXT( + cl_call!(clCreateSubDevicesEXT( in_device, properties.as_ptr(), 0, ptr::null_mut(), &mut count, - ) + )) }; if CL_SUCCESS == status { Ok(count) @@ -733,13 +744,13 @@ pub fn create_sub_devices_ext( let mut ids: Vec = Vec::with_capacity(num_devices as usize); let status: cl_int = unsafe { ids.set_len(num_devices as usize); - clCreateSubDevicesEXT( + cl_call!(clCreateSubDevicesEXT( in_device, properties.as_ptr(), num_devices * mem::size_of::() as cl_uint, ids.as_mut_ptr(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { @@ -759,7 +770,7 @@ pub unsafe fn enqueue_migrate_mem_object_ext( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueMigrateMemObjectEXT( + let status: cl_int = cl_call!(clEnqueueMigrateMemObjectEXT( command_queue, num_mem_objects, mem_objects, @@ -767,7 +778,7 @@ pub unsafe fn enqueue_migrate_mem_object_ext( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -786,7 +797,7 @@ pub fn get_device_image_info_qcom( let mut data: cl_uint = 0; let data_ptr: *mut cl_uint = &mut data; let status = unsafe { - clGetDeviceImageInfoQCOM( + cl_call!(clGetDeviceImageInfoQCOM( device, image_width, image_height, @@ -795,7 +806,7 @@ pub fn get_device_image_info_qcom( mem::size_of::(), data_ptr.cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(data) @@ -813,14 +824,14 @@ pub unsafe fn enqueue_acquire_gralloc_objects_img( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueAcquireGrallocObjectsIMG( + let status: cl_int = cl_call!(clEnqueueAcquireGrallocObjectsIMG( command_queue, num_objects, mem_objects, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -837,14 +848,14 @@ pub unsafe fn enqueue_release_gralloc_objects_img( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueReleaseGrallocObjectsIMG( + let status: cl_int = cl_call!(clEnqueueReleaseGrallocObjectsIMG( command_queue, num_objects, mem_objects, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -864,7 +875,7 @@ pub unsafe fn enqueue_generate_mipmap_img( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueGenerateMipmapIMG( + let status: cl_int = cl_call!(clEnqueueGenerateMipmapIMG( command_queue, src_image, dst_image, @@ -874,7 +885,7 @@ pub unsafe fn enqueue_generate_mipmap_img( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -899,7 +910,7 @@ pub fn get_kernel_sub_group_info_khr( let mut data: size_t = 0; let data_ptr: *mut size_t = &mut data; let status = unsafe { - clGetKernelSubGroupInfoKHR( + cl_call!(clGetKernelSubGroupInfoKHR( kernel, device, param_name, @@ -908,7 +919,7 @@ pub fn get_kernel_sub_group_info_khr( mem::size_of::(), data_ptr.cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(data) @@ -929,14 +940,14 @@ pub fn get_kernel_suggested_local_work_size_khr( ) -> Result { let mut suggested_local_work_size: size_t = 0; let status: cl_int = unsafe { - clGetKernelSuggestedLocalWorkSizeKHR( + cl_call!(clGetKernelSuggestedLocalWorkSizeKHR( command_queue, kernel, work_dim, global_work_offset, global_work_size, &mut suggested_local_work_size, - ) + )) }; if CL_SUCCESS == status { Ok(suggested_local_work_size) @@ -954,14 +965,14 @@ pub unsafe fn enqueue_acquire_external_mem_objects_khr( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueAcquireExternalMemObjectsKHR( + let status: cl_int = cl_call!(clEnqueueAcquireExternalMemObjectsKHR( command_queue, num_mem_objects, mem_objects, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -978,14 +989,14 @@ pub unsafe fn enqueue_release_external_mem_objects_khr( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueReleaseExternalMemObjectsKHR( + let status: cl_int = cl_call!(clEnqueueReleaseExternalMemObjectsKHR( command_queue, num_mem_objects, mem_objects, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1002,27 +1013,27 @@ pub fn get_semaphore_handle_for_type_khr( // Get the size of the information. let mut size: size_t = 0; let status: cl_int = unsafe { - clGetSemaphoreHandleForTypeKHR( + cl_call!(clGetSemaphoreHandleForTypeKHR( sema_object, device, handle_type, 0, ptr::null_mut(), &mut size, - ) + )) }; if CL_SUCCESS == status { let mut data: cl_semaphore_khr = ptr::null_mut(); let data_ptr: *mut cl_semaphore_khr = &mut data; let status: cl_int = unsafe { - clGetSemaphoreHandleForTypeKHR( + cl_call!(clGetSemaphoreHandleForTypeKHR( sema_object, device, handle_type, size, data_ptr.cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(data) @@ -1040,7 +1051,11 @@ pub unsafe fn reimport_semaphore_sync_fd( reimport_props: *mut cl_semaphore_reimport_properties_khr, fd: c_int, ) -> Result<(), cl_int> { - let status: cl_int = clReImportSemaphoreSyncFdKHR(sema_object, reimport_props, fd); + let status: cl_int = cl_call!(clReImportSemaphoreSyncFdKHR( + sema_object, + reimport_props, + fd + )); if CL_SUCCESS == status { Ok(()) } else { @@ -1054,8 +1069,13 @@ pub fn create_semaphore_with_properties_khr( sema_props: *const cl_semaphore_properties_khr, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let semaphore: cl_semaphore_khr = - unsafe { clCreateSemaphoreWithPropertiesKHR(context, sema_props, &mut status) }; + let semaphore: cl_semaphore_khr = unsafe { + cl_call!(clCreateSemaphoreWithPropertiesKHR( + context, + sema_props, + &mut status + )) + }; if CL_SUCCESS == status { Ok(semaphore) } else { @@ -1073,7 +1093,7 @@ pub unsafe fn enqueue_wait_semaphores_khr( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueWaitSemaphoresKHR( + let status: cl_int = cl_call!(clEnqueueWaitSemaphoresKHR( command_queue, num_sema_objects, sema_objects, @@ -1081,7 +1101,7 @@ pub unsafe fn enqueue_wait_semaphores_khr( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1099,7 +1119,7 @@ pub unsafe fn enqueue_signal_semaphores_khr( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSignalSemaphoresKHR( + let status: cl_int = cl_call!(clEnqueueSignalSemaphoresKHR( command_queue, num_sema_objects, sema_objects, @@ -1107,7 +1127,7 @@ pub unsafe fn enqueue_signal_semaphores_khr( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1128,7 +1148,7 @@ pub fn get_semaphore_info_khr( #[cfg(feature = "cl_khr_semaphore")] pub unsafe fn release_semaphore_khr(sema_object: cl_semaphore_khr) -> Result<(), cl_int> { - let status: cl_int = clReleaseSemaphoreKHR(sema_object); + let status: cl_int = cl_call!(clReleaseSemaphoreKHR(sema_object)); if CL_SUCCESS == status { Ok(()) } else { @@ -1138,7 +1158,7 @@ pub unsafe fn release_semaphore_khr(sema_object: cl_semaphore_khr) -> Result<(), #[cfg(feature = "cl_khr_semaphore")] pub unsafe fn retain_semaphore_khr(sema_object: cl_semaphore_khr) -> Result<(), cl_int> { - let status: cl_int = clRetainSemaphoreKHR(sema_object); + let status: cl_int = cl_call!(clRetainSemaphoreKHR(sema_object)); if CL_SUCCESS == status { Ok(()) } else { @@ -1155,7 +1175,14 @@ pub unsafe fn import_memory_arm( size: size_t, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem: cl_mem = clImportMemoryARM(context, flags, properties, memory, size, &mut status); + let mem: cl_mem = cl_call!(clImportMemoryARM( + context, + flags, + properties, + memory, + size, + &mut status + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -1170,7 +1197,7 @@ pub unsafe fn svm_alloc_arm( size: size_t, alignment: cl_uint, ) -> Result<*mut c_void, cl_int> { - let ptr = clSVMAllocARM(context, flags, size, alignment); + let ptr = cl_call!(clSVMAllocARM(context, flags, size, alignment)); if ptr.is_null() { Err(CL_INVALID_VALUE) } else { @@ -1179,8 +1206,9 @@ pub unsafe fn svm_alloc_arm( } #[cfg(feature = "cl_arm_shared_virtual_memory")] -pub unsafe fn svm_free_arm(context: cl_context, svm_pointer: *mut c_void) { - clSVMFreeARM(context, svm_pointer); +pub unsafe fn svm_free_arm(context: cl_context, svm_pointer: *mut c_void) -> Result<(), cl_int> { + cl_call!(clSVMFreeARM(context, svm_pointer)); + Ok(()) } #[cfg(feature = "cl_arm_shared_virtual_memory")] @@ -1201,7 +1229,7 @@ pub unsafe fn enqueue_svm_free_arm( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMFreeARM( + let status: cl_int = cl_call!(clEnqueueSVMFreeARM( command_queue, num_svm_pointers, svm_pointers, @@ -1210,7 +1238,7 @@ pub unsafe fn enqueue_svm_free_arm( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1229,7 +1257,7 @@ pub unsafe fn enqueue_svm_mem_cpy_arm( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMMemcpyARM( + let status: cl_int = cl_call!(clEnqueueSVMMemcpyARM( command_queue, blocking_copy, dst_ptr, @@ -1238,7 +1266,7 @@ pub unsafe fn enqueue_svm_mem_cpy_arm( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1257,7 +1285,7 @@ pub unsafe fn enqueue_svm_mem_fill_arm( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMMemFillARM( + let status: cl_int = cl_call!(clEnqueueSVMMemFillARM( command_queue, svm_ptr, pattern, @@ -1266,7 +1294,7 @@ pub unsafe fn enqueue_svm_mem_fill_arm( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1285,7 +1313,7 @@ pub unsafe fn enqueue_svm_map_arm( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMMapARM( + let status: cl_int = cl_call!(clEnqueueSVMMapARM( command_queue, blocking_map, flags, @@ -1294,7 +1322,7 @@ pub unsafe fn enqueue_svm_map_arm( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1310,13 +1338,13 @@ pub unsafe fn enqueue_svm_unmap_arm( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueSVMUnmapARM( + let status: cl_int = cl_call!(clEnqueueSVMUnmapARM( command_queue, svm_ptr, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1330,7 +1358,8 @@ pub fn set_kernel_arg_svm_pointer( arg_index: cl_uint, arg_ptr: *const c_void, ) -> Result<(), cl_int> { - let status: cl_int = unsafe { clSetKernelArgSVMPointerARM(kernel, arg_index, arg_ptr) }; + let status: cl_int = + unsafe { cl_call!(clSetKernelArgSVMPointerARM(kernel, arg_index, arg_ptr)) }; if CL_SUCCESS == status { Ok(()) } else { @@ -1345,8 +1374,14 @@ pub fn set_kernel_exec_info_arm( param_value_size: size_t, param_value: *const c_void, ) -> Result<(), cl_int> { - let status: cl_int = - unsafe { clSetKernelExecInfoARM(kernel, param_name, param_value_size, param_value) }; + let status: cl_int = unsafe { + cl_call!(clSetKernelExecInfoARM( + kernel, + param_name, + param_value_size, + param_value + )) + }; if CL_SUCCESS == status { Ok(()) } else { @@ -1363,13 +1398,13 @@ pub fn create_accelerator_intel( ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; let ptr = unsafe { - clCreateAcceleratorINTEL( + cl_call!(clCreateAcceleratorINTEL( context, accelerator_type, descriptor_size, descriptor, &mut status, - ) + )) }; if CL_SUCCESS == status { Ok(ptr) @@ -1417,7 +1452,7 @@ pub fn get_accelerator_info_intel( #[cfg(feature = "cl_intel_accelerator")] pub unsafe fn retain_accelerator_intel(accelerator: cl_accelerator_intel) -> Result<(), cl_int> { - let status = clRetainAcceleratorINTEL(accelerator); + let status = cl_call!(clRetainAcceleratorINTEL(accelerator)); if CL_SUCCESS == status { Ok(()) } else { @@ -1427,7 +1462,7 @@ pub unsafe fn retain_accelerator_intel(accelerator: cl_accelerator_intel) -> Res #[cfg(feature = "cl_intel_accelerator")] pub unsafe fn release_accelerator_intel(accelerator: cl_accelerator_intel) -> Result<(), cl_int> { - let status = clReleaseAcceleratorINTEL(accelerator); + let status = cl_call!(clReleaseAcceleratorINTEL(accelerator)); if CL_SUCCESS == status { Ok(()) } else { @@ -1443,7 +1478,13 @@ pub unsafe fn host_mem_alloc_intel( alignment: cl_uint, ) -> Result<(), cl_int> { let mut status: cl_int = CL_INVALID_VALUE; - clHostMemAllocINTEL(context, properties, size, alignment, &mut status); + cl_call!(clHostMemAllocINTEL( + context, + properties, + size, + alignment, + &mut status + )); if CL_SUCCESS == status { Ok(()) } else { @@ -1460,7 +1501,14 @@ pub unsafe fn device_mem_alloc_intel( alignment: cl_uint, ) -> Result<(), cl_int> { let mut status: cl_int = CL_INVALID_VALUE; - clDeviceMemAllocINTEL(context, device, properties, size, alignment, &mut status); + cl_call!(clDeviceMemAllocINTEL( + context, + device, + properties, + size, + alignment, + &mut status + )); if CL_SUCCESS == status { Ok(()) } else { @@ -1477,7 +1525,14 @@ pub unsafe fn shared_mem_alloc_intel( alignment: cl_uint, ) -> Result<(), cl_int> { let mut status: cl_int = CL_INVALID_VALUE; - clSharedMemAllocINTEL(context, device, properties, size, alignment, &mut status); + cl_call!(clSharedMemAllocINTEL( + context, + device, + properties, + size, + alignment, + &mut status + )); if CL_SUCCESS == status { Ok(()) } else { @@ -1487,7 +1542,7 @@ pub unsafe fn shared_mem_alloc_intel( #[cfg(feature = "cl_intel_unified_shared_memory")] pub unsafe fn mem_free_intel(context: cl_context, ptr: *mut c_void) -> Result<(), cl_int> { - let status = clMemFreeINTEL(context, ptr); + let status = cl_call!(clMemFreeINTEL(context, ptr)); if CL_SUCCESS == status { Ok(()) } else { @@ -1497,7 +1552,7 @@ pub unsafe fn mem_free_intel(context: cl_context, ptr: *mut c_void) -> Result<() #[cfg(feature = "cl_intel_unified_shared_memory")] pub unsafe fn mem_blocking_free_intel(context: cl_context, ptr: *mut c_void) -> Result<(), cl_int> { - let status = clMemBlockingFreeINTEL(context, ptr); + let status = cl_call!(clMemBlockingFreeINTEL(context, ptr)); if CL_SUCCESS == status { Ok(()) } else { @@ -1514,14 +1569,14 @@ fn mem_alloc_info_intel( let mut data: T = T::default(); let data_ptr: *mut T = &mut data; let status = unsafe { - clGetMemAllocInfoINTEL( + cl_call!(clGetMemAllocInfoINTEL( context, ptr, param_id, mem::size_of::(), data_ptr.cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(data) @@ -1554,7 +1609,14 @@ pub fn get_mem_alloc_info_intel( // get the size let mut size: size_t = 0; let status = unsafe { - clGetMemAllocInfoINTEL(context, ptr, param_name, 0, ptr::null_mut(), &mut size) + cl_call!(clGetMemAllocInfoINTEL( + context, + ptr, + param_name, + 0, + ptr::null_mut(), + &mut size + )) }; if CL_SUCCESS != status { Err(status) @@ -1562,14 +1624,14 @@ pub fn get_mem_alloc_info_intel( // Get the data. let mut data: Vec = Vec::with_capacity(size); let status = unsafe { - clGetMemAllocInfoINTEL( + cl_call!(clGetMemAllocInfoINTEL( context, ptr, param_name, size, data.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(InfoType::VecUchar(data)) @@ -1589,7 +1651,7 @@ pub unsafe fn set_kernel_arg_mem_pointer_intel( arg_index: cl_uint, arg_value: *const c_void, ) -> Result<(), cl_int> { - let status = clSetKernelArgMemPointerINTEL(kernel, arg_index, arg_value); + let status = cl_call!(clSetKernelArgMemPointerINTEL(kernel, arg_index, arg_value)); if CL_SUCCESS == status { Ok(()) } else { @@ -1607,7 +1669,7 @@ pub unsafe fn enqueue_mem_set_intel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueMemsetINTEL( + let status: cl_int = cl_call!(clEnqueueMemsetINTEL( command_queue, dst_ptr, value, @@ -1615,7 +1677,7 @@ pub unsafe fn enqueue_mem_set_intel( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1634,7 +1696,7 @@ pub unsafe fn enqueue_mem_fill_intel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueMemFillINTEL( + let status: cl_int = cl_call!(clEnqueueMemFillINTEL( command_queue, dst_ptr, pattern, @@ -1643,7 +1705,7 @@ pub unsafe fn enqueue_mem_fill_intel( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1662,7 +1724,7 @@ pub unsafe fn enqueue_mem_copy_intel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueMemcpyINTEL( + let status: cl_int = cl_call!(clEnqueueMemcpyINTEL( command_queue, blocking, dst_ptr, @@ -1671,7 +1733,7 @@ pub unsafe fn enqueue_mem_copy_intel( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1689,7 +1751,7 @@ pub unsafe fn enqueue_migrate_mem_intel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueMigrateMemINTEL( + let status: cl_int = cl_call!(clEnqueueMigrateMemINTEL( command_queue, ptr, size, @@ -1697,7 +1759,7 @@ pub unsafe fn enqueue_migrate_mem_intel( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1715,7 +1777,7 @@ pub unsafe fn enqueue_mem_advise_intel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueMemAdviseINTEL( + let status: cl_int = cl_call!(clEnqueueMemAdviseINTEL( command_queue, ptr, size, @@ -1723,7 +1785,7 @@ pub unsafe fn enqueue_mem_advise_intel( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1740,8 +1802,14 @@ pub unsafe fn create_buffer_with_properties_intel( host_ptr: *mut c_void, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem: cl_mem = - clCreateBufferWithPropertiesINTEL(context, properties, flags, size, host_ptr, &mut status); + let mem: cl_mem = cl_call!(clCreateBufferWithPropertiesINTEL( + context, + properties, + flags, + size, + host_ptr, + &mut status + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -1761,7 +1829,7 @@ pub unsafe fn enqueue_read_host_pipe_intel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueReadHostPipeINTEL( + let status: cl_int = cl_call!(clEnqueueReadHostPipeINTEL( command_queue, program, pipe_symbol, @@ -1771,7 +1839,7 @@ pub unsafe fn enqueue_read_host_pipe_intel( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1791,7 +1859,7 @@ pub unsafe fn enqueue_write_host_pipe_intel( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueWriteHostPipeINTEL( + let status: cl_int = cl_call!(clEnqueueWriteHostPipeINTEL( command_queue, program, pipe_symbol, @@ -1801,7 +1869,7 @@ pub unsafe fn enqueue_write_host_pipe_intel( num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -1821,7 +1889,7 @@ pub fn get_image_requirements_info_ext( // get the size let mut size: size_t = mem::size_of::(); let status: cl_int = unsafe { - clGetImageRequirementsInfoEXT( + cl_call!(clGetImageRequirementsInfoEXT( context, properties, flags, @@ -1831,14 +1899,14 @@ pub fn get_image_requirements_info_ext( 0, ptr::null_mut(), &mut size, - ) + )) }; if CL_SUCCESS == status { // Get the data. let mut data: Vec = Vec::with_capacity(size); let status = unsafe { data.set_len(size); - clGetImageRequirementsInfoEXT( + cl_call!(clGetImageRequirementsInfoEXT( context, properties, flags, @@ -1848,7 +1916,7 @@ pub fn get_image_requirements_info_ext( size, data.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(data) @@ -1864,17 +1932,24 @@ pub fn get_image_requirements_info_ext( pub fn get_icd_loader_info_oclicd(param_name: cl_icdl_info) -> Result, cl_int> { // get the size let mut size: size_t = 0; - let status = unsafe { clGetICDLoaderInfoOCLICD(param_name, 0, ptr::null_mut(), &mut size) }; + let status = unsafe { + cl_call!(clGetICDLoaderInfoOCLICD( + param_name, + 0, + ptr::null_mut(), + &mut size + )) + }; if CL_SUCCESS == status { // Get the data. let mut data: Vec = Vec::with_capacity(size); let status = unsafe { - clGetICDLoaderInfoOCLICD( + cl_call!(clGetICDLoaderInfoOCLICD( param_name, size, data.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(data) @@ -1891,7 +1966,7 @@ pub fn set_content_size_buffer_pocl( buffer: cl_mem, content_size_buffer: cl_mem, ) -> Result<(), cl_int> { - let status = unsafe { clSetContentSizeBufferPoCL(buffer, content_size_buffer) }; + let status = unsafe { cl_call!(clSetContentSizeBufferPoCL(buffer, content_size_buffer)) }; if CL_SUCCESS == status { Ok(()) } else { diff --git a/src/gl.rs b/src/gl.rs index 29ecb43..0a4fd2d 100644 --- a/src/gl.rs +++ b/src/gl.rs @@ -14,6 +14,7 @@ //! `OpenCL` `OpenGL` Interoperability API. +#![allow(unused_unsafe)] #![allow(non_camel_case_types, deprecated)] #![allow(clippy::not_unsafe_ptr_arg_deref, clippy::missing_safety_doc)] @@ -29,14 +30,6 @@ pub use opencl_sys::{ CL_GL_TEXTURE_TARGET, CL_INVALID_VALUE, CL_KHR_GL_SHARING, CL_SUCCESS, CL_WGL_HDC_KHR, }; -#[allow(unused_imports)] -use opencl_sys::{ - clCreateEventFromGLsyncKHR, clCreateFromGLBuffer, clCreateFromGLRenderbuffer, - clCreateFromGLTexture, clCreateFromGLTexture2D, clCreateFromGLTexture3D, - clEnqueueAcquireGLObjects, clEnqueueReleaseGLObjects, clGetGLContextInfoKHR, clGetGLObjectInfo, - clGetGLTextureInfo, -}; - use super::info_type::InfoType; use super::{api_info_size, api_info_value, api_info_vector}; #[allow(unused_imports)] @@ -44,14 +37,14 @@ use libc::{c_void, intptr_t, size_t}; use std::mem; use std::ptr; -/// Create an `OpenCL` buffer object for a context from an OpenGL buffer. -/// Calls clCreateFromGLBuffer to create an `OpenCL` buffer object. +/// Create an `OpenCL` buffer object for a context from an OpenGL buffer. +/// Calls clCreateFromGLBuffer to create an `OpenCL` buffer object. /// /// * `context` - a valid `OpenCL` context created from an OpenGL context. /// * `flags` - a bit-field used to specify allocation and usage information /// about the image memory object being created, see: /// [Memory Flags](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#memory-flags-table). -/// * `bufobj` - the OpenGL buffer. +/// * `bufobj` - the OpenGL buffer. /// /// returns a Result containing the new `OpenCL` buffer object /// or the error code from the `OpenCL` C API function. @@ -62,7 +55,7 @@ pub unsafe fn create_from_gl_buffer( bufobj: cl_GLuint, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem = clCreateFromGLBuffer(context, flags, bufobj, &mut status); + let mem = cl_call!(clCreateFromGLBuffer(context, flags, bufobj, &mut status)); if CL_SUCCESS == status { Ok(mem) } else { @@ -73,16 +66,16 @@ pub unsafe fn create_from_gl_buffer( /// Create an `OpenCL` image object, image array object, or image buffer object. /// /// For a context from an OpenGL texture object, texture array object, -/// texture buffer object, or a single face of an OpenGL cubemap texture object. -/// Calls clCreateFromGLTexture to create an `OpenCL` memory object. +/// texture buffer object, or a single face of an OpenGL cubemap texture object. +/// Calls clCreateFromGLTexture to create an `OpenCL` memory object. /// /// * `context` - a valid `OpenCL` context created from an OpenGL context. /// * `flags` - a bit-field used to specify allocation and usage information /// about the image memory object being created, see: /// [Memory Flags](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#memory-flags-table). -/// * `texture_target` - used to define the image type of texture. -/// * `miplevel ` - used to define the mipmap level. -/// * `texture ` - the name of a GL buffer texture object. +/// * `texture_target` - used to define the image type of texture. +/// * `miplevel ` - used to define the mipmap level. +/// * `texture ` - the name of a GL buffer texture object. /// /// returns a Result containing the new `OpenCL` image object /// or the error code from the `OpenCL` C API function. @@ -95,14 +88,14 @@ pub unsafe fn create_from_gl_texture( texture: cl_GLuint, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem = clCreateFromGLTexture( + let mem = cl_call!(clCreateFromGLTexture( context, flags, texture_target, miplevel, texture, &mut status, - ); + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -112,13 +105,13 @@ pub unsafe fn create_from_gl_texture( /// Create an `OpenCL` 2D image object from an OpenGL renderbuffer object. /// -/// Calls clCreateFromGLRenderbuffer to create an `OpenCL` buffer object. +/// Calls clCreateFromGLRenderbuffer to create an `OpenCL` buffer object. /// /// * `context` - a valid `OpenCL` context created from an OpenGL context. /// * `flags` - a bit-field used to specify allocation and usage information /// about the image memory object being created, see: /// [Memory Flags](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#memory-flags-table). -/// * `renderbuffer` - a GL renderbuffer object. +/// * `renderbuffer` - a GL renderbuffer object. /// /// returns a Result containing the new `OpenCL` image object /// or the error code from the `OpenCL` C API function. @@ -129,7 +122,12 @@ pub unsafe fn create_from_gl_render_buffer( renderbuffer: cl_GLuint, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem = clCreateFromGLRenderbuffer(context, flags, renderbuffer, &mut status); + let mem = cl_call!(clCreateFromGLRenderbuffer( + context, + flags, + renderbuffer, + &mut status + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -137,9 +135,9 @@ pub unsafe fn create_from_gl_render_buffer( } } -/// Query an OpenGL object used to create an `OpenCL` memory object. +/// Query an OpenGL object used to create an `OpenCL` memory object. /// -/// Calls clGetGLObjectInfo to get the object type and name. +/// Calls clGetGLObjectInfo to get the object type and name. /// /// * `memobj` - a valid `OpenCL` memory object handle. /// @@ -149,7 +147,13 @@ pub unsafe fn create_from_gl_render_buffer( pub fn get_gl_object_info(memobj: cl_mem) -> Result<(cl_GLuint, cl_GLuint), cl_int> { let mut object_type: cl_uint = CL_GL_OBJECT_BUFFER; let mut object_name: cl_uint = 0; - let status = unsafe { clGetGLObjectInfo(memobj, &mut object_type, &mut object_name) }; + let status = unsafe { + cl_call!(clGetGLObjectInfo( + memobj, + &mut object_type, + &mut object_name + )) + }; if CL_SUCCESS == status { Ok((object_type, object_name)) } else { @@ -199,8 +203,8 @@ pub fn get_gl_texture_info( } } -/// Acquire `OpenCL` memory objects that have been created from `OpenGL` objects. -/// Calls `clEnqueueAcquireGLObjects`. +/// Acquire `OpenCL` memory objects that have been created from `OpenGL` objects. +/// Calls `clEnqueueAcquireGLObjects`. /// /// * `command_queue` - a valid `OpenCL` `command_queue`. /// * `num_objects` - the number of memory objects to acquire. @@ -219,14 +223,14 @@ pub unsafe fn enqueue_acquire_gl_objects( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueAcquireGLObjects( + let status: cl_int = cl_call!(clEnqueueAcquireGLObjects( command_queue, num_objects, mem_objects, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -234,8 +238,8 @@ pub unsafe fn enqueue_acquire_gl_objects( } } -/// Release `OpenCL` memory objects that have been created from `OpenGL` objects. -/// Calls `clEnqueueReleaseGLObjects`. +/// Release `OpenCL` memory objects that have been created from `OpenGL` objects. +/// Calls `clEnqueueReleaseGLObjects`. /// /// * `command_queue` - a valid `OpenCL` `command_queue`. /// * `num_objects` - the number of memory objects to acquire. @@ -254,14 +258,14 @@ pub unsafe fn enqueue_release_gl_objects( event_wait_list: *const cl_event, ) -> Result { let mut event: cl_event = ptr::null_mut(); - let status: cl_int = clEnqueueReleaseGLObjects( + let status: cl_int = cl_call!(clEnqueueReleaseGLObjects( command_queue, num_objects, mem_objects, num_events_in_wait_list, event_wait_list, &mut event, - ); + )); if CL_SUCCESS == status { Ok(event) } else { @@ -272,16 +276,16 @@ pub unsafe fn enqueue_release_gl_objects( /// Create an `OpenCL` 2D image object from an `OpenGL` 2D texture object, /// or a single face of an OpenGL cubemap texture object. /// -/// Calls clCreateFromGLTexture2D to create an `OpenCL` memory object. +/// Calls clCreateFromGLTexture2D to create an `OpenCL` memory object. /// Deprecated in `CL_VERSION_1_2`, use `create_from_gl_texture`. /// /// * `context` - a valid `OpenCL` context created from an OpenGL context. /// * `flags` - a bit-field used to specify allocation and usage information /// about the image memory object being created, see: /// [Memory Flags](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#memory-flags-table). -/// * `texture_target` - used to define the image type of texture. -/// * `miplevel ` - used to define the mipmap level. -/// * `texture ` - the name of a GL 2D, cubemap or rectangle texture object. +/// * `texture_target` - used to define the image type of texture. +/// * `miplevel ` - used to define the mipmap level. +/// * `texture ` - the name of a GL 2D, cubemap or rectangle texture object. /// /// returns a Result containing the new `OpenCL` image object /// or the error code from the `OpenCL` C API function. @@ -307,14 +311,14 @@ pub unsafe fn create_from_gl_texture_2d( texture: cl_GLuint, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem = clCreateFromGLTexture2D( + let mem = cl_call!(clCreateFromGLTexture2D( context, flags, texture_target, miplevel, texture, &mut status, - ); + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -322,18 +326,18 @@ pub unsafe fn create_from_gl_texture_2d( } } -/// Create an `OpenCL` 3D image object from an OpenGL 3D texture object. +/// Create an `OpenCL` 3D image object from an OpenGL 3D texture object. /// -/// Calls `clCreateFromGLTexture3D` to create an `OpenCL` memory object. +/// Calls `clCreateFromGLTexture3D` to create an `OpenCL` memory object. /// Deprecated in `CL_VERSION_1_2`, use `create_from_gl_texture`. /// /// * `context` - a valid `OpenCL` context created from an OpenGL context. /// * `flags` - a bit-field used to specify allocation and usage information /// about the image memory object being created, see: /// [Memory Flags](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#memory-flags-table). -/// * `texture_target` - used to define the image type of texture. -/// * `miplevel ` - used to define the mipmap level. -/// * `texture ` - the name of a GL 2D, cubemap or rectangle texture object. +/// * `texture_target` - used to define the image type of texture. +/// * `miplevel ` - used to define the mipmap level. +/// * `texture ` - the name of a GL 2D, cubemap or rectangle texture object. /// /// returns a Result containing the new `OpenCL` image object /// or the error code from the `OpenCL` C API function. @@ -359,14 +363,14 @@ pub unsafe fn create_from_gl_texture_3d( texture: cl_GLuint, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem = clCreateFromGLTexture3D( + let mem = cl_call!(clCreateFromGLTexture3D( context, flags, texture_target, miplevel, texture, &mut status, - ); + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -393,13 +397,13 @@ pub fn get_gl_context_info_khr( let mut data: intptr_t = 0; let data_ptr: *mut intptr_t = &mut data; let status = unsafe { - clGetGLContextInfoKHR( + cl_call!(clGetGLContextInfoKHR( properties, param_name, mem::size_of::(), data_ptr.cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(InfoType::Ptr(data)) @@ -412,7 +416,13 @@ pub fn get_gl_context_info_khr( // Get the size let mut size: size_t = 0; let status = unsafe { - clGetGLContextInfoKHR(properties, param_name, 0, ptr::null_mut(), &mut size) + cl_call!(clGetGLContextInfoKHR( + properties, + param_name, + 0, + ptr::null_mut(), + &mut size + )) }; if CL_SUCCESS != status { Err(status) @@ -421,13 +431,13 @@ pub fn get_gl_context_info_khr( let count = size / mem::size_of::(); let mut data: Vec = Vec::with_capacity(count); let status = unsafe { - clGetGLContextInfoKHR( + cl_call!(clGetGLContextInfoKHR( properties, param_name, size, data.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(InfoType::VecIntPtr(data)) @@ -443,7 +453,13 @@ pub fn get_gl_context_info_khr( // Get the size let mut size: size_t = 0; let status = unsafe { - clGetGLContextInfoKHR(properties, param_name, 0, ptr::null_mut(), &mut size) + cl_call!(clGetGLContextInfoKHR( + properties, + param_name, + 0, + ptr::null_mut(), + &mut size + )) }; if CL_SUCCESS != status { Err(status) @@ -451,13 +467,13 @@ pub fn get_gl_context_info_khr( // Get the data let mut data: Vec = Vec::with_capacity(size); let status = unsafe { - clGetGLContextInfoKHR( + cl_call!(clGetGLContextInfoKHR( properties, param_name, size, data.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(InfoType::VecUchar(data)) @@ -471,12 +487,12 @@ pub fn get_gl_context_info_khr( } } -/// Create an event object linked to an OpenGL sync object. +/// Create an event object linked to an OpenGL sync object. /// Requires the `cl_khr_gl_event` extension -/// Calls `clCreateEventFromGLsyncKHR`. +/// Calls `clCreateEventFromGLsyncKHR`. /// /// * `context` - a valid `OpenCL` context. -/// * `sync` - the sync object in the GL share group associated with context. +/// * `sync` - the sync object in the GL share group associated with context. /// /// returns a Result containing the new `OpenCL` event /// or the error code from the `OpenCL` C API function. @@ -487,7 +503,8 @@ pub fn create_event_from_gl_sync_khr( sync: cl_GLsync, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let event: cl_event = unsafe { clCreateEventFromGLsyncKHR(context, sync, &mut status) }; + let event: cl_event = + unsafe { cl_call!(clCreateEventFromGLsyncKHR(context, sync, &mut status)) }; if CL_SUCCESS == status { Ok(event) } else { diff --git a/src/info_type.rs b/src/info_type.rs index d5c7784..424a19c 100644 --- a/src/info_type.rs +++ b/src/info_type.rs @@ -19,9 +19,9 @@ use opencl_sys::{ }; use std::fmt; -/// A Rust enum to handle `OpenCL` API "Info" function return types. +/// A Rust enum to handle `OpenCL` API "Info" function return types. /// Each of the data types may be extracted from the enum using its associated -/// From trait or `to_*` function. +/// From trait or `to_*` function. /// /// # Panics /// diff --git a/src/kernel.rs b/src/kernel.rs index ff40b8c..52cfd04 100644 --- a/src/kernel.rs +++ b/src/kernel.rs @@ -14,6 +14,7 @@ //! `OpenCL` Kernel Object API. +#![allow(unused_unsafe)] #![allow(non_camel_case_types)] #![allow( clippy::not_unsafe_ptr_arg_deref, @@ -42,17 +43,6 @@ pub use opencl_sys::{ CL_SUCCESS, }; -use opencl_sys::{ - clCreateKernel, clCreateKernelsInProgram, clGetKernelArgInfo, clGetKernelInfo, - clGetKernelWorkGroupInfo, clReleaseKernel, clRetainKernel, clSetKernelArg, -}; - -#[cfg(feature = "CL_VERSION_2_0")] -use opencl_sys::{clSetKernelArgSVMPointer, clSetKernelExecInfo}; - -#[cfg(feature = "CL_VERSION_2_1")] -use opencl_sys::{clCloneKernel, clGetKernelSubGroupInfo}; - use super::info_type::InfoType; use super::{ api2_info_size, api2_info_value, api2_info_vector, api_info_size, api_info_value, @@ -63,8 +53,8 @@ use std::ffi::CStr; use std::mem; use std::ptr; -/// Create an `OpenCL` kernel object for a program with a successfully built executable. -/// Calls clCreateKernel to create an `OpenCL` kernel object. +/// Create an `OpenCL` kernel object for a program with a successfully built executable. +/// Calls clCreateKernel to create an `OpenCL` kernel object. /// /// * `program` - a valid `OpenCL` program. /// * `kernel_name` - a kernel function name in the program. @@ -74,7 +64,8 @@ use std::ptr; #[inline] pub fn create_kernel(program: cl_program, kernel_name: &CStr) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let kernel: cl_kernel = unsafe { clCreateKernel(program, kernel_name.as_ptr(), &mut status) }; + let kernel: cl_kernel = + unsafe { cl_call!(clCreateKernel(program, kernel_name.as_ptr(), &mut status)) }; if CL_SUCCESS == status { Ok(kernel) } else { @@ -84,8 +75,14 @@ pub fn create_kernel(program: cl_program, kernel_name: &CStr) -> Result Result { let mut count: cl_uint = 0; - let status: cl_int = - unsafe { clCreateKernelsInProgram(program, 0, ptr::null_mut(), &mut count) }; + let status: cl_int = unsafe { + cl_call!(clCreateKernelsInProgram( + program, + 0, + ptr::null_mut(), + &mut count + )) + }; if CL_SUCCESS == status { Ok(count) } else { @@ -93,8 +90,8 @@ fn count_kernels_in_program(program: cl_program) -> Result { } } -/// Create `OpenCL` kernel objects for all kernel functions in a program. -/// Calls clCreateKernelsInProgram to create `OpenCL` kernel objects. +/// Create `OpenCL` kernel objects for all kernel functions in a program. +/// Calls clCreateKernelsInProgram to create `OpenCL` kernel objects. /// /// * `program` - a valid `OpenCL` program. /// @@ -106,12 +103,12 @@ pub fn create_kernels_in_program(program: cl_program) -> Result, let mut kernels: Vec = Vec::with_capacity(count as size_t); let status: cl_int = unsafe { kernels.set_len(count as size_t); - clCreateKernelsInProgram( + cl_call!(clCreateKernelsInProgram( program, count, kernels.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(kernels) @@ -120,8 +117,8 @@ pub fn create_kernels_in_program(program: cl_program) -> Result, } } -/// Clone an `OpenCL` kernel object. -/// Calls clCloneKernel to clone an `OpenCL` kernel object. +/// Clone an `OpenCL` kernel object. +/// Calls clCloneKernel to clone an `OpenCL` kernel object. /// `CL_VERSION_2_1` /// /// * `source_kernel` - a valid `OpenCL` `cl_kernel` object that will be copied. @@ -132,7 +129,7 @@ pub fn create_kernels_in_program(program: cl_program) -> Result, #[inline] pub fn clone_kernel(source_kernel: cl_kernel) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let kernel: cl_kernel = unsafe { clCloneKernel(source_kernel, &mut status) }; + let kernel: cl_kernel = unsafe { cl_call!(clCloneKernel(source_kernel, &mut status)) }; if CL_SUCCESS == status { Ok(kernel) } else { @@ -140,7 +137,7 @@ pub fn clone_kernel(source_kernel: cl_kernel) -> Result { } } -/// Retain an `OpenCL` kernel. +/// Retain an `OpenCL` kernel. /// Calls clRetainKernel to increment the kernel reference count. /// /// * `program` - the `OpenCL` kernel. @@ -152,7 +149,7 @@ pub fn clone_kernel(source_kernel: cl_kernel) -> Result { /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn retain_kernel(kernel: cl_kernel) -> Result<(), cl_int> { - let status: cl_int = clRetainKernel(kernel); + let status: cl_int = cl_call!(clRetainKernel(kernel)); if CL_SUCCESS == status { Ok(()) } else { @@ -160,7 +157,7 @@ pub unsafe fn retain_kernel(kernel: cl_kernel) -> Result<(), cl_int> { } } -/// Release an `OpenCL` kernel. +/// Release an `OpenCL` kernel. /// Calls clReleaseKernel to decrement the kernel reference count. /// /// * `kernel` - the `OpenCL` kernel. @@ -172,7 +169,7 @@ pub unsafe fn retain_kernel(kernel: cl_kernel) -> Result<(), cl_int> { /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn release_kernel(kernel: cl_kernel) -> Result<(), cl_int> { - let status: cl_int = clReleaseKernel(kernel); + let status: cl_int = cl_call!(clReleaseKernel(kernel)); if CL_SUCCESS == status { Ok(()) } else { @@ -180,8 +177,8 @@ pub unsafe fn release_kernel(kernel: cl_kernel) -> Result<(), cl_int> { } } -/// Set the argument value for a specific argument of a kernel. -/// Calls clSetKernelArg. +/// Set the argument value for a specific argument of a kernel. +/// Calls clSetKernelArg. /// /// * `kernel` - the `OpenCL` kernel. /// * `arg_index` - the kernel argument index. @@ -199,7 +196,7 @@ pub unsafe fn set_kernel_arg( arg_size: size_t, arg_value: *const c_void, ) -> Result<(), cl_int> { - let status: cl_int = clSetKernelArg(kernel, arg_index, arg_size, arg_value); + let status: cl_int = cl_call!(clSetKernelArg(kernel, arg_index, arg_size, arg_value)); if CL_SUCCESS == status { Ok(()) } else { @@ -207,8 +204,8 @@ pub unsafe fn set_kernel_arg( } } -/// Set set a SVM pointer as the argument value for a specific argument of a kernel. -/// Calls clSetKernelArgSVMPointer. +/// Set set a SVM pointer as the argument value for a specific argument of a kernel. +/// Calls clSetKernelArgSVMPointer. /// /// * `kernel` - the `OpenCL` kernel. /// * `arg_index` - the kernel argument index. @@ -226,7 +223,7 @@ pub unsafe fn set_kernel_arg_svm_pointer( arg_index: cl_uint, arg_ptr: *const c_void, ) -> Result<(), cl_int> { - let status: cl_int = clSetKernelArgSVMPointer(kernel, arg_index, arg_ptr); + let status: cl_int = cl_call!(clSetKernelArgSVMPointer(kernel, arg_index, arg_ptr)); if CL_SUCCESS == status { Ok(()) } else { @@ -234,8 +231,8 @@ pub unsafe fn set_kernel_arg_svm_pointer( } } -/// Pass additional information other than argument values to a kernel. -/// Calls clSetKernelExecInfo. +/// Pass additional information other than argument values to a kernel. +/// Calls clSetKernelExecInfo. /// /// * `kernel` - the `OpenCL` kernel. /// * `param_name` - the information to be passed to kernel, see: @@ -255,7 +252,12 @@ pub unsafe fn set_kernel_exec_info( param_value_size: size_t, param_value: *const c_void, ) -> Result<(), cl_int> { - let status: cl_int = clSetKernelExecInfo(kernel, param_name, param_value_size, param_value); + let status: cl_int = cl_call!(clSetKernelExecInfo( + kernel, + param_name, + param_value_size, + param_value + )); if CL_SUCCESS == status { Ok(()) } else { @@ -272,7 +274,7 @@ pub fn get_kernel_data(kernel: cl_kernel, param_name: cl_kernel_info) -> Result< get_vector(kernel, param_name, size) } -/// Get specific information about an `OpenCL` kernel. +/// Get specific information about an `OpenCL` kernel. /// Calls clGetKernelInfo to get the desired information about the kernel. /// /// * `kernel` - the `OpenCL` kernel. @@ -312,7 +314,7 @@ pub fn get_kernel_arg_data( get_vector(kernel, arg_indx, param_name, size) } -/// Get specific information about arguments of an `OpenCL` kernel. +/// Get specific information about arguments of an `OpenCL` kernel. /// Calls clGetKernelArgInfo to get the desired information about the kernel. /// /// * `kernel` - the `OpenCL` kernel. @@ -362,7 +364,7 @@ pub fn get_kernel_work_group_data( get_vector(kernel, device, param_name, size) } -/// Get specific information about work groups of an `OpenCL` kernel. +/// Get specific information about work groups of an `OpenCL` kernel. /// Calls clGetKernelWorkGroupInfo to get the desired information about the kernel. /// /// * `kernel` - the `OpenCL` kernel. @@ -420,8 +422,8 @@ pub fn get_kernel_work_group_info( } } -/// Get specific information about sub groups of an `OpenCL` kernel. -/// Calls clGetKernelSubGroupInfo to get the desired information about the kernel. +/// Get specific information about sub groups of an `OpenCL` kernel. +/// Calls clGetKernelSubGroupInfo to get the desired information about the kernel. /// `CL_VERSION_2_1` /// /// * `kernel` - the `OpenCL` kernel. @@ -452,7 +454,7 @@ pub fn get_kernel_sub_group_info( let mut data: size_t = 0; let data_ptr: *mut size_t = &mut data; let status = unsafe { - clGetKernelSubGroupInfo( + cl_call!(clGetKernelSubGroupInfo( kernel, device, param_name, @@ -461,7 +463,7 @@ pub fn get_kernel_sub_group_info( size, data_ptr.cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(InfoType::Size(data)) @@ -473,7 +475,7 @@ pub fn get_kernel_sub_group_info( CL_KERNEL_LOCAL_SIZE_FOR_SUB_GROUP_COUNT => { // get the size let status: cl_int = unsafe { - clGetKernelSubGroupInfo( + cl_call!(clGetKernelSubGroupInfo( kernel, device, param_name, @@ -482,7 +484,7 @@ pub fn get_kernel_sub_group_info( 0, ptr::null_mut(), &mut size, - ) + )) }; if CL_SUCCESS == status { // Get the information. @@ -490,7 +492,7 @@ pub fn get_kernel_sub_group_info( let mut data: Vec = Vec::with_capacity(count); let status = unsafe { data.set_len(count); - clGetKernelSubGroupInfo( + cl_call!(clGetKernelSubGroupInfo( kernel, device, param_name, @@ -499,7 +501,7 @@ pub fn get_kernel_sub_group_info( size, data.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(InfoType::VecSize(data)) @@ -514,7 +516,7 @@ pub fn get_kernel_sub_group_info( _ => { // get the size let status: cl_int = unsafe { - clGetKernelSubGroupInfo( + cl_call!(clGetKernelSubGroupInfo( kernel, device, param_name, @@ -523,7 +525,7 @@ pub fn get_kernel_sub_group_info( 0, ptr::null_mut(), &mut size, - ) + )) }; if CL_SUCCESS == status { // Get the information. @@ -531,7 +533,7 @@ pub fn get_kernel_sub_group_info( let mut data: Vec = Vec::with_capacity(count); let status = unsafe { data.set_len(count); - clGetKernelSubGroupInfo( + cl_call!(clGetKernelSubGroupInfo( kernel, device, param_name, @@ -540,7 +542,7 @@ pub fn get_kernel_sub_group_info( size, data.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(InfoType::VecUchar(data)) diff --git a/src/layer.rs b/src/layer.rs index a1bcbb4..7d0f8d0 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -14,6 +14,8 @@ //! `OpenCL` layer extensions +#![allow(unused_unsafe)] + pub use opencl_sys::cl_layer::*; pub use opencl_sys::*; @@ -26,16 +28,23 @@ use std::ptr; /// Calls `clGetLayerInfo`. pub fn get_layer_data(param_name: cl_layer_info) -> Result, cl_int> { let mut size: size_t = 0; - let status = unsafe { clGetLayerInfo(param_name, 0, ptr::null_mut(), &mut size) }; + let status = unsafe { + cl_call!(cl_layer::clGetLayerInfo( + param_name, + 0, + ptr::null_mut(), + &mut size + )) + }; if CL_SUCCESS == status { let mut data: Vec = Vec::with_capacity(size); let status = unsafe { - clGetLayerInfo( + cl_call!(cl_layer::clGetLayerInfo( param_name, size, data.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(data) @@ -59,12 +68,12 @@ pub unsafe fn init_layer( ) -> Result<&[cl_icd_dispatch], cl_int> { let mut num_entries_ret: cl_uint = 0; let mut layer_dispatch_ret: *const cl_icd_dispatch = ptr::null(); - let status = clInitLayer( + let status = cl_call!(cl_layer::clInitLayer( target_dispatch.len() as cl_uint, target_dispatch.as_ptr(), &mut num_entries_ret, &mut layer_dispatch_ret, - ); + )); if CL_SUCCESS == status { let slice = std::slice::from_raw_parts(layer_dispatch_ret, num_entries_ret as usize); Ok(slice) diff --git a/src/lib.rs b/src/lib.rs index 847c8cf..8b2baaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ //! based upon the [opencl-sys](https://crates.io/crates/opencl-sys) `OpenCL` FFI bindings. //! //! [OpenCL 3.0](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html) -//! is a unified specification that adds little new functionality to previous `OpenCL` versions. +//! is a unified specification that adds little new functionality to previous `OpenCL` versions. //! It specifies that all `OpenCL 1.2` features are **mandatory**, while all //! `OpenCL 2.x` and `OpenCL 3.0` features are now optional. //! @@ -70,11 +70,11 @@ //! //! # Use //! -//! See [cl3](https://crates.io/crates/cl3). +//! See [cl3](https://crates.io/crates/cl3). //! //! ## License //! -//! Licensed under the Apache License, Version 2.0, as per Khronos Group `OpenCL`. +//! Licensed under the Apache License, Version 2.0, as per Khronos Group `OpenCL`. //! You may obtain a copy of the License at: //! //! `OpenCL` and the `OpenCL` logo are trademarks of Apple Inc. used under license by Khronos. @@ -114,4 +114,5 @@ pub mod memory; pub mod platform; pub mod program; pub mod sampler; + pub mod types; diff --git a/src/macros.rs b/src/macros.rs index ca063dc..9d27828 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -20,7 +20,8 @@ macro_rules! api_info_size { fn $func(object: *mut c_void, param_name: cl_uint) -> Result { // Get the size of the information. let mut size: size_t = 0; - let status = unsafe { $api(object, param_name, 0, ptr::null_mut(), &mut size) }; + let status = + unsafe { cl_call!($api(object, param_name, 0, ptr::null_mut(), &mut size)) }; if CL_SUCCESS != status { Err(status) } else { @@ -39,13 +40,13 @@ macro_rules! api_info_value { let mut data: $ty = $ty::default(); let data_ptr: *mut $ty = &mut data; let status = unsafe { - $api( + cl_call!($api( object, param_name, size, data_ptr.cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS != status { Err(status) @@ -70,13 +71,13 @@ macro_rules! api_info_vector { let mut data: Vec<$ty> = Vec::with_capacity(count); let status = unsafe { data.set_len(count); - $api( + cl_call!($api( object, param_name, size, data.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS != status { Err(status) @@ -96,7 +97,8 @@ macro_rules! api2_info_size { fn $func(object: *mut c_void, idx: $type, param_name: cl_uint) -> Result { // Get the size of the information. let mut size: size_t = 0; - let status = unsafe { $api(object, idx, param_name, 0, ptr::null_mut(), &mut size) }; + let status = + unsafe { cl_call!($api(object, idx, param_name, 0, ptr::null_mut(), &mut size)) }; if CL_SUCCESS != status { Err(status) } else { @@ -115,14 +117,14 @@ macro_rules! api2_info_value { let mut data: $ty = $ty::default(); let data_ptr: *mut $ty = &mut data; let status = unsafe { - $api( + cl_call!($api( object, idx, param_name, size, data_ptr.cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS != status { Err(status) @@ -148,14 +150,14 @@ macro_rules! api2_info_vector { let mut data: Vec<$ty> = Vec::with_capacity(count); let status = unsafe { data.set_len(count); - $api( + cl_call!($api( object, idx, param_name, size, data.as_mut_ptr().cast::(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS != status { Err(status) diff --git a/src/memory.rs b/src/memory.rs index a0d9e24..d7c9e74 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -14,6 +14,7 @@ //! `OpenCL` Memory Object API. +#![allow(unused_unsafe)] #![allow(non_camel_case_types)] #![allow(clippy::not_unsafe_ptr_arg_deref)] @@ -43,26 +44,14 @@ pub use opencl_sys::{ CL_UNORM_SHORT_565, CL_UNSIGNED_INT16, CL_UNSIGNED_INT32, CL_UNSIGNED_INT8, }; -use opencl_sys::{ - clCreateBuffer, clCreateImage, clCreateSubBuffer, clGetImageInfo, clGetMemObjectInfo, - clGetSupportedImageFormats, clReleaseMemObject, clRetainMemObject, - clSetMemObjectDestructorCallback, -}; - -#[cfg(feature = "CL_VERSION_2_0")] -use opencl_sys::{clCreatePipe, clGetPipeInfo, clSVMAlloc, clSVMFree}; - -#[cfg(feature = "CL_VERSION_3_0")] -use opencl_sys::{clCreateBufferWithProperties, clCreateImageWithProperties}; - use super::info_type::InfoType; use super::{api_info_size, api_info_value, api_info_vector}; use libc::{c_void, intptr_t, size_t}; use std::mem; use std::ptr; -/// Create an `OpenCL` buffer object for a `context`. -/// Calls `clCreateBuffer` to create an `OpenCL` buffer object. +/// Create an `OpenCL` buffer object for a `context`. +/// Calls `clCreateBuffer` to create an `OpenCL` buffer object. /// /// * `context` - a valid `OpenCL` context. /// * `flags` - a bit-field used to specify allocation and usage information @@ -86,7 +75,7 @@ pub unsafe fn create_buffer( host_ptr: *mut c_void, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem: cl_mem = clCreateBuffer(context, flags, size, host_ptr, &mut status); + let mem: cl_mem = cl_call!(clCreateBuffer(context, flags, size, host_ptr, &mut status)); if CL_SUCCESS == status { Ok(mem) } else { @@ -94,8 +83,8 @@ pub unsafe fn create_buffer( } } -/// Create an new `OpenCL` buffer object from an existing buffer object. -/// Calls `clCreateSubBuffer` to create an `OpenCL` sub-buffer object. +/// Create an new `OpenCL` buffer object from an existing buffer object. +/// Calls `clCreateSubBuffer` to create an `OpenCL` sub-buffer object. /// /// * `buffer` - a valid `OpenCL` buffer. /// * `flags` - a bit-field used to specify allocation and usage information @@ -119,13 +108,13 @@ pub unsafe fn create_sub_buffer( buffer_create_info: *const c_void, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem: cl_mem = clCreateSubBuffer( + let mem: cl_mem = cl_call!(clCreateSubBuffer( buffer, flags, buffer_create_type, buffer_create_info, &mut status, - ); + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -133,8 +122,8 @@ pub unsafe fn create_sub_buffer( } } -/// Create an `OpenCL` image object for a `context`. -/// Calls `clCreateImage` to create an `OpenCL` image object. +/// Create an `OpenCL` image object for a `context`. +/// Calls `clCreateImage` to create an `OpenCL` image object. /// /// * `context` - a valid `OpenCL` context. /// * `flags` - a bit-field used to specify allocation and usage information @@ -163,14 +152,14 @@ pub unsafe fn create_image( host_ptr: *mut c_void, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem: cl_mem = clCreateImage( + let mem: cl_mem = cl_call!(clCreateImage( context, flags, image_format, image_desc, host_ptr, &mut status, - ); + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -178,8 +167,8 @@ pub unsafe fn create_image( } } -/// Create an `OpenCL` pipe object for a context. -/// Calls `clCreatePipe` to create an `OpenCL` pipe object. +/// Create an `OpenCL` pipe object for a context. +/// Calls `clCreatePipe` to create an `OpenCL` pipe object. /// `CL_VERSION_2_0` /// /// * `context` - a valid `OpenCL` context. @@ -206,14 +195,14 @@ pub unsafe fn create_pipe( // properties: *const cl_pipe_properties, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem: cl_mem = clCreatePipe( + let mem: cl_mem = cl_call!(clCreatePipe( context, flags, pipe_packet_size, pipe_max_packets, ptr::null(), &mut status, - ); + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -221,8 +210,8 @@ pub unsafe fn create_pipe( } } -/// Create an `OpenCL` buffer object for a context. -/// Calls `clCreateBufferWithProperties` to create an `OpenCL` buffer object. +/// Create an `OpenCL` buffer object for a context. +/// Calls `clCreateBufferWithProperties` to create an `OpenCL` buffer object. /// `CL_VERSION_3_0` /// /// * `context` - a valid `OpenCL` context. @@ -250,8 +239,14 @@ pub unsafe fn create_buffer_with_properties( host_ptr: *mut c_void, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem: cl_mem = - clCreateBufferWithProperties(context, properties, flags, size, host_ptr, &mut status); + let mem: cl_mem = cl_call!(clCreateBufferWithProperties( + context, + properties, + flags, + size, + host_ptr, + &mut status + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -259,8 +254,8 @@ pub unsafe fn create_buffer_with_properties( } } -/// Create an `OpenCL` image object for a context. -/// Calls `clCreateImage` to create an `OpenCL` image object. +/// Create an `OpenCL` image object for a context. +/// Calls `clCreateImage` to create an `OpenCL` image object. /// `CL_VERSION_3_0` /// /// * `context` - a valid `OpenCL` context. @@ -292,7 +287,7 @@ pub unsafe fn create_image_with_properties( host_ptr: *mut c_void, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let mem: cl_mem = clCreateImageWithProperties( + let mem: cl_mem = cl_call!(clCreateImageWithProperties( context, properties, flags, @@ -300,7 +295,7 @@ pub unsafe fn create_image_with_properties( image_desc, host_ptr, &mut status, - ); + )); if CL_SUCCESS == status { Ok(mem) } else { @@ -308,7 +303,7 @@ pub unsafe fn create_image_with_properties( } } -/// Retain an `OpenCL` memory object. +/// Retain an `OpenCL` memory object. /// Calls `clRetainMemObject` to increment the memory object reference count. /// /// * `memobj` - the `OpenCL` memory object. @@ -320,7 +315,7 @@ pub unsafe fn create_image_with_properties( /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn retain_mem_object(memobj: cl_mem) -> Result<(), cl_int> { - let status: cl_int = clRetainMemObject(memobj); + let status: cl_int = cl_call!(clRetainMemObject(memobj)); if CL_SUCCESS == status { Ok(()) } else { @@ -328,7 +323,7 @@ pub unsafe fn retain_mem_object(memobj: cl_mem) -> Result<(), cl_int> { } } -/// Release an `OpenCL` memory object. +/// Release an `OpenCL` memory object. /// Calls `clReleaseMemObject` to decrement the memory object reference count. /// /// * `memobj` - the `OpenCL` memory object. @@ -340,7 +335,7 @@ pub unsafe fn retain_mem_object(memobj: cl_mem) -> Result<(), cl_int> { /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn release_mem_object(memobj: cl_mem) -> Result<(), cl_int> { - let status: cl_int = clReleaseMemObject(memobj); + let status: cl_int = cl_call!(clReleaseMemObject(memobj)); if CL_SUCCESS == status { Ok(()) } else { @@ -355,7 +350,14 @@ fn count_supported_image_formats( ) -> Result { let mut count: cl_uint = 0; let status: cl_int = unsafe { - clGetSupportedImageFormats(context, flags, image_type, 0, ptr::null_mut(), &mut count) + cl_call!(clGetSupportedImageFormats( + context, + flags, + image_type, + 0, + ptr::null_mut(), + &mut count + )) }; if CL_SUCCESS == status { Ok(count) @@ -387,14 +389,14 @@ pub fn get_supported_image_formats( let mut image_formats: Vec = Vec::with_capacity(count as usize); let status: cl_int = unsafe { image_formats.set_len(count as usize); - clGetSupportedImageFormats( + cl_call!(clGetSupportedImageFormats( context, flags, image_type, count, image_formats.as_mut_ptr(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(image_formats) @@ -412,7 +414,7 @@ pub fn get_mem_object_data(memobj: cl_mem, param_name: cl_mem_info) -> Result Result Result, get_vector(pipe, param_name, size) } -/// Get information specific to an `OpenCL` pipe object. +/// Get information specific to an `OpenCL` pipe object. /// Calls `clGetPipeInfo` to get the desired information about the pipe object. /// `CL_VERSION_2_0` /// @@ -551,8 +553,8 @@ pub fn get_pipe_info(pipe: cl_mem, param_name: cl_pipe_info) -> Result Result<(), cl_int> { - let status: cl_int = clSetMemObjectDestructorCallback(memobj, Some(pfn_notify), user_data); + let status: cl_int = cl_call!(clSetMemObjectDestructorCallback( + memobj, + Some(pfn_notify), + user_data + )); if CL_SUCCESS == status { Ok(()) } else { @@ -578,8 +584,8 @@ pub unsafe fn set_mem_object_destructor_callback( } /// Allocate a shared virtual memory (SVM) buffer that can be shared by the -/// host and all devices in an `OpenCL` context. -/// Calls `clSVMAlloc`. +/// host and all devices in an `OpenCL` context. +/// Calls `clSVMAlloc`. /// `CL_VERSION_2_0` /// /// * `context` - a valid `OpenCL` context. @@ -603,7 +609,7 @@ pub unsafe fn svm_alloc( size: size_t, alignment: cl_uint, ) -> Result<*mut c_void, cl_int> { - let ptr = clSVMAlloc(context, flags, size, alignment); + let ptr = cl_call!(clSVMAlloc(context, flags, size, alignment)); if ptr.is_null() { Err(CL_INVALID_VALUE) } else { @@ -611,8 +617,8 @@ pub unsafe fn svm_alloc( } } -/// Free a shared virtual memory (SVM) buffer allocated using `clSVMAlloc`. -/// Calls `clSVMFree`. +/// Free a shared virtual memory (SVM) buffer allocated using `clSVMAlloc`. +/// Calls `clSVMFree`. /// `CL_VERSION_2_0` /// /// * `context` - the valid `OpenCL` context used to create the SVM buffer. @@ -623,6 +629,7 @@ pub unsafe fn svm_alloc( /// This function is unsafe because `svm_pointer` is no longer valid after it is called. #[cfg(feature = "CL_VERSION_2_0")] #[inline] -pub unsafe fn svm_free(context: cl_context, svm_pointer: *mut c_void) { - clSVMFree(context, svm_pointer); +pub unsafe fn svm_free(context: cl_context, svm_pointer: *mut c_void) -> Result<(), cl_int> { + cl_call!(clSVMFree(context, svm_pointer)); + Ok(()) } diff --git a/src/platform.rs b/src/platform.rs index 7780ba4..82e1167 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -14,7 +14,9 @@ //! `OpenCL` Platform API. -#![allow(non_camel_case_types, clippy::wildcard_in_or_patterns)] +#![allow(unused_unsafe)] +#![allow(non_camel_case_types)] +#![allow(clippy::wildcard_in_or_patterns)] pub use opencl_sys::{ cl_int, cl_name_version, cl_platform_id, cl_platform_info, cl_uint, cl_ulong, cl_version, @@ -25,11 +27,6 @@ pub use opencl_sys::{ CL_PLATFORM_SEMAPHORE_TYPES_KHR, CL_PLATFORM_VENDOR, CL_PLATFORM_VERSION, CL_SUCCESS, }; -#[allow(unused_imports)] -use opencl_sys::{clGetPlatformIDs, clGetPlatformInfo}; - -#[cfg(feature = "dynamic")] -use super::dynamic_library::load_dynamic_runtime; #[allow(unused_imports)] use super::error_codes::DLOPEN_FUNCTION_NOT_AVAILABLE; use super::info_type::InfoType; @@ -39,7 +36,7 @@ use libc::{c_void, size_t}; use std::mem; use std::ptr; -/// Get the available platforms. +/// Get the available platforms. /// Calls clGetPlatformIDs to get the available platform ids. /// # Examples /// ``` diff --git a/src/program.rs b/src/program.rs index a86fd3e..7b12df7 100644 --- a/src/program.rs +++ b/src/program.rs @@ -14,6 +14,7 @@ //! `OpenCL` Program Object API. +#![allow(unused_unsafe)] #![allow(non_camel_case_types)] #![allow(clippy::not_unsafe_ptr_arg_deref, clippy::wildcard_in_or_patterns)] @@ -29,18 +30,6 @@ pub use opencl_sys::{ CL_PROGRAM_REFERENCE_COUNT, CL_PROGRAM_SOURCE, CL_SUCCESS, CL_TRUE, }; -use opencl_sys::{ - clBuildProgram, clCompileProgram, clCreateProgramWithBinary, clCreateProgramWithBuiltInKernels, - clCreateProgramWithSource, clGetProgramBuildInfo, clGetProgramInfo, clLinkProgram, - clReleaseProgram, clRetainProgram, clUnloadPlatformCompiler, -}; - -#[cfg(feature = "CL_VERSION_2_1")] -use opencl_sys::clCreateProgramWithIL; - -#[cfg(feature = "CL_VERSION_2_2")] -use opencl_sys::clSetProgramSpecializationConstant; - use super::info_type::InfoType; use super::{ api2_info_size, api2_info_value, api2_info_vector, api_info_size, api_info_value, @@ -55,8 +44,8 @@ use std::ptr; pub const CL_PROGRAM_SCOPE_GLOBAL_CTORS_PRESENT: cl_program_info = 0x116A; pub const CL_PROGRAM_SCOPE_GLOBAL_DTORS_PRESENT: cl_program_info = 0x116B; -/// Create an `OpenCL` program object for a context and load source code into that object. -/// Calls `clCreateProgramWithSource` to create an `OpenCL` program object. +/// Create an `OpenCL` program object for a context and load source code into that object. +/// Calls `clCreateProgramWithSource` to create an `OpenCL` program object. /// /// * `context` - a valid `OpenCL` context. /// * `sources` - an array of slices of source code strings. @@ -72,13 +61,13 @@ pub fn create_program_with_source( let lengths: Vec = sources.iter().map(|src| src.len()).collect(); let mut status: cl_int = CL_INVALID_VALUE; let program: cl_program = unsafe { - clCreateProgramWithSource( + cl_call!(clCreateProgramWithSource( context, sources.len() as cl_uint, sources.as_ptr().cast::<*const c_char>(), lengths.as_ptr(), &mut status, - ) + )) }; if CL_SUCCESS == status { @@ -88,8 +77,8 @@ pub fn create_program_with_source( } } -/// Create an `OpenCL` program object for a context and load binary bits into that object. -/// Calls `clCreateProgramWithBinary` to create an `OpenCL` program object. +/// Create an `OpenCL` program object for a context and load binary bits into that object. +/// Calls `clCreateProgramWithBinary` to create an `OpenCL` program object. /// /// * `context` - a valid `OpenCL` context. /// * `devices` - a slice of devices that are in context. @@ -111,7 +100,7 @@ pub unsafe fn create_program_with_binary( let lengths: Vec = binaries.iter().map(|bin| bin.len()).collect(); let mut binary_status: Vec = Vec::with_capacity(binaries_length); let mut status: cl_int = CL_INVALID_VALUE; - let program: cl_program = clCreateProgramWithBinary( + let program: cl_program = cl_call!(clCreateProgramWithBinary( context, devices.len() as cl_uint, devices.as_ptr(), @@ -119,8 +108,7 @@ pub unsafe fn create_program_with_binary( binaries.as_ptr().cast::<*const c_uchar>(), binary_status.as_mut_ptr(), &mut status, - ); - binary_status.set_len(binaries_length); + )); if CL_SUCCESS == status { Ok(program) } else { @@ -131,7 +119,7 @@ pub unsafe fn create_program_with_binary( /// Create an `OpenCL` program object for a context and loads the information /// related to the built-in kernels into that object. /// -/// Calls `clCreateProgramWithBuiltInKernels` to create an `OpenCL` program object. +/// Calls `clCreateProgramWithBuiltInKernels` to create an `OpenCL` program object. /// /// * `context` - a valid `OpenCL` context. /// * `devices` - a slice of devices that are in context. @@ -152,13 +140,13 @@ pub unsafe fn create_program_with_builtin_kernels( kernel_names: &CStr, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let program: cl_program = clCreateProgramWithBuiltInKernels( + let program: cl_program = cl_call!(clCreateProgramWithBuiltInKernels( context, devices.len() as cl_uint, devices.as_ptr(), kernel_names.as_ptr(), &mut status, - ); + )); if CL_SUCCESS == status { Ok(program) } else { @@ -167,8 +155,8 @@ pub unsafe fn create_program_with_builtin_kernels( } /// Create an `OpenCL` program object for a context and load code in an intermediate -/// language into that object. -/// Calls `clCreateProgramWithIL` to create an `OpenCL` program object. +/// language into that object. +/// Calls `clCreateProgramWithIL` to create an `OpenCL` program object. /// `CL_VERSION_2_1` /// /// * `context` - a valid `OpenCL` context. @@ -181,12 +169,12 @@ pub unsafe fn create_program_with_builtin_kernels( pub fn create_program_with_il(context: cl_context, il: &[u8]) -> Result { let mut status: cl_int = CL_INVALID_VALUE; let program: cl_program = unsafe { - clCreateProgramWithIL( + cl_call!(clCreateProgramWithIL( context, il.as_ptr().cast::(), il.len() as size_t, &mut status, - ) + )) }; if CL_SUCCESS == status { Ok(program) @@ -195,7 +183,7 @@ pub fn create_program_with_il(context: cl_context, il: &[u8]) -> Result Result Result<(), cl_int> { - let status: cl_int = clRetainProgram(program); + let status: cl_int = cl_call!(clRetainProgram(program)); if CL_SUCCESS == status { Ok(()) } else { @@ -215,7 +203,7 @@ pub unsafe fn retain_program(program: cl_program) -> Result<(), cl_int> { } } -/// Release an `OpenCL` program. +/// Release an `OpenCL` program. /// Calls `clReleaseProgram` to decrement the program reference count. /// /// * `program` - the `OpenCL` program. @@ -227,7 +215,7 @@ pub unsafe fn retain_program(program: cl_program) -> Result<(), cl_int> { /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn release_program(program: cl_program) -> Result<(), cl_int> { - let status: cl_int = clReleaseProgram(program); + let status: cl_int = cl_call!(clReleaseProgram(program)); if CL_SUCCESS == status { Ok(()) } else { @@ -235,8 +223,8 @@ pub unsafe fn release_program(program: cl_program) -> Result<(), cl_int> { } } -/// Build (compile & link) a program executable. -/// Calls `clBuildProgram` to build an `OpenCL` program object. +/// Build (compile & link) a program executable. +/// Calls `clBuildProgram` to build an `OpenCL` program object. /// /// * `program` - a valid `OpenCL` program. /// * `devices` - a slice of devices that are in context. @@ -256,14 +244,14 @@ pub fn build_program( user_data: *mut c_void, ) -> Result<(), cl_int> { let status: cl_int = unsafe { - clBuildProgram( + cl_call!(clBuildProgram( program, devices.len() as cl_uint, devices.as_ptr(), options.as_ptr(), pfn_notify, user_data, - ) + )) }; if CL_SUCCESS == status { Ok(()) @@ -273,8 +261,8 @@ pub fn build_program( } /// Compile a program’s source for the devices the `OpenCL` context associated -/// with the program. -/// Calls clCompileProgram to compile an `OpenCL` program object. +/// with the program. +/// Calls clCompileProgram to compile an `OpenCL` program object. /// /// * `program` - a valid `OpenCL` program. /// * `devices` - a slice of devices that are in context. @@ -315,7 +303,7 @@ pub fn compile_program( } else { header_include_names.as_ptr() }; - clCompileProgram( + cl_call!(clCompileProgram( program, devices.len() as cl_uint, devices.as_ptr(), @@ -325,7 +313,7 @@ pub fn compile_program( header_include_names_ptr.cast::<*const c_char>(), pfn_notify, user_data, - ) + )) }; if CL_SUCCESS == status { Ok(()) @@ -335,8 +323,8 @@ pub fn compile_program( } /// Link a set of compiled program objects and libraries for the devices in the -/// `OpenCL` context associated with the program. -/// Calls clLinkProgram to link an `OpenCL` program object. +/// `OpenCL` context associated with the program. +/// Calls clLinkProgram to link an `OpenCL` program object. /// /// * `context` - a valid `OpenCL` context. /// * `devices` - a slice of devices that are in context. @@ -368,7 +356,7 @@ pub unsafe fn link_program( ) -> Result { assert!(!input_programs.is_empty()); let mut status: cl_int = CL_INVALID_VALUE; - let programme: cl_program = clLinkProgram( + let programme: cl_program = cl_call!(clLinkProgram( context, devices.len() as cl_uint, devices.as_ptr(), @@ -378,7 +366,7 @@ pub unsafe fn link_program( pfn_notify, user_data, &mut status, - ); + )); if CL_SUCCESS == status { Ok(programme) } else { @@ -386,9 +374,9 @@ pub unsafe fn link_program( } } -/// Set the value of a specialization constant. -/// Calls `clSetProgramSpecializationConstant`. -/// `CL_VERSION_2_2` +/// Set the value of a specialization constant. +/// Calls `clSetProgramSpecializationConstant`. +/// `CL_VERSION_2_2` /// /// * `program` - the program. /// * `spec_id` - the specialization constant whose value will be set. @@ -409,8 +397,9 @@ pub unsafe fn set_program_specialization_constant( spec_size: size_t, spec_value: *const c_void, ) -> Result<(), cl_int> { - let status: cl_int = - clSetProgramSpecializationConstant(program, spec_id, spec_size, spec_value); + let status: cl_int = cl_call!(clSetProgramSpecializationConstant( + program, spec_id, spec_size, spec_value + )); if CL_SUCCESS == status { Ok(()) } else { @@ -418,8 +407,8 @@ pub unsafe fn set_program_specialization_constant( } } -/// Release the resources allocated by the `OpenCL` compiler for platform. -/// Calls clUnloadPlatformCompiler. +/// Release the resources allocated by the `OpenCL` compiler for platform. +/// Calls clUnloadPlatformCompiler. /// /// * `platform` - the platform. /// @@ -431,7 +420,7 @@ pub unsafe fn set_program_specialization_constant( #[cfg(feature = "CL_VERSION_1_2")] #[inline] pub unsafe fn unload_platform_compiler(platform: cl_platform_id) -> Result<(), cl_int> { - let status: cl_int = clUnloadPlatformCompiler(platform); + let status: cl_int = cl_call!(clUnloadPlatformCompiler(platform)); if CL_SUCCESS == status { Ok(()) } else { @@ -451,7 +440,7 @@ pub fn get_program_data( get_vector(program, param_name, size) } -/// Get specific information about an `OpenCL` program. +/// Get specific information about an `OpenCL` program. /// Calls clGetProgramInfo to get the desired information about the program. /// /// * `program` - the `OpenCL` program. @@ -512,13 +501,13 @@ pub fn get_program_info( }).collect::>(); let status = unsafe { - clGetProgramInfo( + cl_call!(clGetProgramInfo( program, param_name, binary_ptrs.len() * mem::size_of::<*mut c_void>(), binary_ptrs.as_mut_ptr().cast(), ptr::null_mut(), - ) + )) }; if CL_SUCCESS == status { Ok(InfoType::VecVecUchar(binaries)) @@ -554,7 +543,7 @@ pub fn get_program_build_data( get_vector(program, device, param_name, size) } -/// Get specific information about an `OpenCL` program build. +/// Get specific information about an `OpenCL` program build. /// Calls clGetProgramBuildInfo to get the desired information about the program build. /// /// * `program` - the `OpenCL` program. @@ -787,8 +776,8 @@ mod tests { unsafe { release_program(program).unwrap(); - release_context(context).unwrap() - }; + release_context(context).unwrap(); + } } #[test] diff --git a/src/sampler.rs b/src/sampler.rs index a9d7cde..384834d 100644 --- a/src/sampler.rs +++ b/src/sampler.rs @@ -14,6 +14,7 @@ //! `OpenCL` Sampler API. +#![allow(unused_unsafe)] #![allow(non_camel_case_types, deprecated)] #![allow(clippy::not_unsafe_ptr_arg_deref, clippy::wildcard_in_or_patterns)] @@ -25,19 +26,14 @@ pub use opencl_sys::{ CL_SAMPLER_REFERENCE_COUNT, CL_SUCCESS, }; -use opencl_sys::{clCreateSampler, clGetSamplerInfo, clReleaseSampler, clRetainSampler}; - -#[cfg(feature = "CL_VERSION_2_0")] -use opencl_sys::clCreateSamplerWithProperties; - use super::info_type::InfoType; use super::{api_info_size, api_info_value, api_info_vector}; use libc::{c_void, intptr_t, size_t}; use std::mem; use std::ptr; -/// Create an `OpenCL` buffer `sampler` for a context. -/// Calls `clCreateSampler` to create an `OpenCL` `sampler` object. +/// Create an `OpenCL` buffer `sampler` for a context. +/// Calls `clCreateSampler` to create an `OpenCL` `sampler` object. /// Deprecated in `CL_VERSION_2_0` by `create_sampler_with_properties`. /// /// * `context` - a valid `OpenCL` context. @@ -46,7 +42,7 @@ use std::ptr; /// * `filter_mode` - same interpretation as `CL_SAMPLER_FILTER_MODE`. /// /// `CL_SAMPLER_NORMALIZED_COORDS`, `CL_SAMPLER_ADDRESSING_MODE` and `CL_SAMPLER_FILTER_MODE` -/// are described in: [Sampler Properties](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#sampler-properties-table) table. +/// are described in: [Sampler Properties](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#sampler-properties-table) table. /// returns a Result containing the new `OpenCL` `sampler` object /// or the error code from the `OpenCL` C API function. #[cfg_attr( @@ -70,13 +66,13 @@ pub fn create_sampler( ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; let sampler: cl_sampler = unsafe { - clCreateSampler( + cl_call!(clCreateSampler( context, normalize_coords, addressing_mode, filter_mode, &mut status, - ) + )) }; if CL_SUCCESS == status { Ok(sampler) @@ -85,8 +81,8 @@ pub fn create_sampler( } } -/// Create an `OpenCL` buffer sampler for a context. -/// Calls `clCreateSamplerWithProperties` to create an `OpenCL` `sampler` object. +/// Create an `OpenCL` buffer sampler for a context. +/// Calls `clCreateSamplerWithProperties` to create an `OpenCL` `sampler` object. /// `CL_VERSION_2_0` /// /// * `context` - a valid `OpenCL` context. @@ -102,8 +98,13 @@ pub fn create_sampler_with_properties( properties: *const cl_sampler_properties, ) -> Result { let mut status: cl_int = CL_INVALID_VALUE; - let sampler: cl_sampler = - unsafe { clCreateSamplerWithProperties(context, properties, &mut status) }; + let sampler: cl_sampler = unsafe { + cl_call!(clCreateSamplerWithProperties( + context, + properties, + &mut status + )) + }; if CL_SUCCESS == status { Ok(sampler) } else { @@ -111,7 +112,7 @@ pub fn create_sampler_with_properties( } } -/// Retain an `OpenCL` sampler. +/// Retain an `OpenCL` sampler. /// Calls `clRetainSampler` to increment the `sampler` reference count. /// /// * `sampler` - the `OpenCL` sampler. @@ -123,7 +124,7 @@ pub fn create_sampler_with_properties( /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn retain_sampler(sampler: cl_sampler) -> Result<(), cl_int> { - let status: cl_int = clRetainSampler(sampler); + let status: cl_int = cl_call!(clRetainSampler(sampler)); if CL_SUCCESS == status { Ok(()) } else { @@ -131,7 +132,7 @@ pub unsafe fn retain_sampler(sampler: cl_sampler) -> Result<(), cl_int> { } } -/// Release an `OpenCL` sampler. +/// Release an `OpenCL` sampler. /// Calls `clReleaseMemObject` to decrement the `sampler` reference count. /// /// * `sampler` - the `OpenCL` sampler. @@ -143,7 +144,7 @@ pub unsafe fn retain_sampler(sampler: cl_sampler) -> Result<(), cl_int> { /// This function is unsafe because it changes the `OpenCL` object reference count. #[inline] pub unsafe fn release_sampler(sampler: cl_sampler) -> Result<(), cl_int> { - let status: cl_int = clReleaseSampler(sampler); + let status: cl_int = cl_call!(clReleaseSampler(sampler)); if CL_SUCCESS == status { Ok(()) } else { @@ -163,7 +164,7 @@ pub fn get_sampler_data( get_vector(sampler, param_name, size) } -/// Get information specific to an `OpenCL` sampler object. +/// Get information specific to an `OpenCL` sampler object. /// Calls `clGetImageInfo` to get the desired information about the sampler object. /// /// * `sampler` - the `OpenCL` sampler object.