## C Specification

The following functions enqueue commands to read from an image or image array object to host memory or write to an image or image array object from host memory.

cl_int clEnqueueReadImage(
cl_command_queue command_queue,
cl_mem image,
const size_t* origin,
const size_t* region,
size_t row_pitch,
size_t slice_pitch,
void* ptr,
cl_uint num_events_in_wait_list,
const cl_event* event_wait_list,
cl_event* event);
cl_int clEnqueueWriteImage(
cl_command_queue command_queue,
cl_mem image,
cl_bool blocking_write,
const size_t* origin,
const size_t* region,
size_t input_row_pitch,
size_t input_slice_pitch,
const void* ptr,
cl_uint num_events_in_wait_list,
const cl_event* event_wait_list,
cl_event* event);

## Parameters

• command_queue refers to the host command-queue in which the read / write command will be queued. command_queue and image must be created with the same OpenCL context.

• image refers to a valid image or image array object.

• blocking_read and blocking_write indicate if the read and write operations are blocking or non-blocking.

• origin defines the (x, y, z) offset in pixels in the 1D, 2D or 3D image, the (x, y) offset and the image index in the 2D image array or the (x) offset and the image index in the 1D image array. If image is a 2D image object, origin[2] must be 0. If image is a 1D image or 1D image buffer object, origin[1] and origin[2] must be 0. If image is a 1D image array object, origin[2] must be 0. If image is a 1D image array object, origin[1] describes the image index in the 1D image array. If image is a 2D image array object, origin[2] describes the image index in the 2D image array.

• region defines the (width, height, depth) in pixels of the 1D, 2D or 3D rectangle, the (width, height) in pixels of the 2D rectangle and the number of images of a 2D image array or the (width) in pixels of the 1D rectangle and the number of images of a 1D image array. If image is a 2D image object, region[2] must be 1. If image is a 1D image or 1D image buffer object, region[1] and region[2] must be 1. If image is a 1D image array object, region[2] must be 1. The values in region cannot be 0.

• row_pitch in clEnqueueReadImage and input_row_pitch in clEnqueueWriteImage is the length of each row in bytes. This value must be greater than or equal to the element size in bytes × width. If row_pitch (or input_row_pitch) is set to 0, the appropriate row pitch is calculated based on the size of each element in bytes multiplied by width.

• slice_pitch in clEnqueueReadImage and input_slice_pitch in clEnqueueWriteImage is the size in bytes of the 2D slice of the 3D region of a 3D image or each image of a 1D or 2D image array being read or written respectively. This must be 0 if image is a 1D or 2D image. Otherwise this value must be greater than or equal to row_pitch × height. If slice_pitch (or input_slice_pitch) is set to 0, the appropriate slice pitch is calculated based on the row_pitch × height.

• ptr is the pointer to a buffer in host memory where image data is to be read from or to be written to. The alignment requirements for ptr are specified in Alignment of Application Data Types.

• event_wait_list and num_events_in_wait_list specify events that need to complete before this particular command can be executed. If event_wait_list is NULL, then this particular command does not wait on any event to complete. If event_wait_list is NULL, num_events_in_wait_list must be 0. If event_wait_list is not NULL, the list of events pointed to by event_wait_list must be valid and num_events_in_wait_list must be greater than 0. The events specified in event_wait_list act as synchronization points. The context associated with events in event_wait_list and command_queue must be the same. The memory associated with event_wait_list can be reused or freed after the function returns.

• event returns an event object that identifies this particular read / write command and can be used to query or queue a wait for this particular command to complete. event can be NULL in which case it will not be possible for the application to query the status of this command or queue a wait for this command to complete. If the event_wait_list and the event arguments are not NULL, the event argument should not refer to an element of the event_wait_list array.

## Description

If blocking_read is CL_​TRUE i.e. the read command is blocking, clEnqueueReadImage does not return until the buffer data has been read and copied into memory pointed to by ptr.

If blocking_read is CL_​FALSE i.e. the read command is non-blocking, clEnqueueReadImage queues a non-blocking read command and returns. The contents of the buffer that ptr points to cannot be used until the read command has completed. The event argument returns an event object which can be used to query the execution status of the read command. When the read command has completed, the contents of the buffer that ptr points to can be used by the application.

If blocking_write is CL_​TRUE, the write command is blocking and does not return until the command is complete, including transfer of the data. The memory pointed to by ptr can be reused by the application after the clEnqueueWriteImage call returns.

If blocking_write is CL_​FALSE, the OpenCL implementation will use ptr to perform a non-blocking write. As the write is non-blocking the implementation can return immediately. The memory pointed to by ptr cannot be reused by the application after the call returns. The event argument returns an event object which can be used to query the execution status of the write command. When the write command has completed, the memory pointed to by ptr can then be reused by the application.

clEnqueueReadImage and clEnqueueWriteImage return CL_​SUCCESS if the function is executed successfully. Otherwise, it returns one of the following errors:

• CL_​INVALID_​COMMAND_​QUEUE if command_queue is not a valid host command-queue.

• CL_​INVALID_​CONTEXT if the context associated with command_queue and image are not the same or if the context associated with command_queue and events in event_wait_list are not the same.

• CL_​INVALID_​MEM_​OBJECT if i_mage_ is not a valid image object.

• CL_​INVALID_​VALUE if the region being read or written specified by origin and region is out of bounds or if ptr is a NULL value.

• CL_​INVALID_​VALUE if values in origin and region do not follow rules described in the argument description for origin and region.

• CL_​INVALID_​EVENT_​WAIT_​LIST if event_wait_list is NULL and num_events_in_wait_list > 0, or event_wait_list is not NULL and num_events_in_wait_list is 0, or if event objects in event_wait_list are not valid events.

• CL_​INVALID_​IMAGE_​SIZE if image dimensions (image width, height, specified or compute row and/or slice pitch) for image are not supported by device associated with queue.

• CL_​IMAGE_​FORMAT_​NOT_​SUPPORTED if image format (image channel order and data type) for image are not supported by device associated with queue.

• CL_​MEM_​OBJECT_​ALLOCATION_​FAILURE if there is a failure to allocate memory for data store associated with image.

• CL_​INVALID_​OPERATION if the device associated with command_queue does not support images (i.e. CL_​DEVICE_​IMAGE_​SUPPORT specified in the Device Queries table is CL_​FALSE).

• CL_​INVALID_​OPERATION if clEnqueueReadImage is called on image which has been created with CL_​MEM_​HOST_​WRITE_​ONLY or CL_​MEM_​HOST_​NO_​ACCESS.

• CL_​INVALID_​OPERATION if clEnqueueWriteImage is called on image which has been created with CL_​MEM_​HOST_​READ_​ONLY or CL_​MEM_​HOST_​NO_​ACCESS.

• CL_​EXEC_​STATUS_​ERROR_​FOR_​EVENTS_​IN_​WAIT_​LIST if the read and write operations are blocking and the execution status of any of the events in event_wait_list is a negative integer value. This error code is missing before version 1.1.

• CL_​OUT_​OF_​RESOURCES if there is a failure to allocate resources required by the OpenCL implementation on the device.

• CL_​OUT_​OF_​HOST_​MEMORY if there is a failure to allocate resources required by the OpenCL implementation on the host.

 Calling clEnqueueReadImage to read a region of the image with the ptr argument value set to host_ptr + (origin[2] × image slice pitch + origin[1] × image row pitch + origin[0] × bytes per pixel), where host_ptr is a pointer to the memory region specified when the image being read is created with CL_​MEM_​USE_​HOST_​PTR, must meet the following requirements in order to avoid undefined behavior: All commands that use this image object have finished execution before the read command begins execution. The row_pitch and slice_pitch argument values in clEnqueueReadImage must be set to the image row pitch and slice pitch. The image object is not mapped. The image object is not used by any command-queue until the read command has finished execution. Calling clEnqueueWriteImage to update the latest bits in a region of the image with the ptr argument value set to host_ptr + (origin[2] × image slice pitch + origin[1] × image row pitch + origin[0] × bytes per pixel), where host_ptr is a pointer to the memory region specified when the image being written is created with CL_​MEM_​USE_​HOST_​PTR, must meet the following requirements in order to avoid undefined behavior: The host memory region being written contains the latest bits when the enqueued write command begins execution. The input_row_pitch and input_slice_pitch argument values in clEnqueueWriteImage must be set to the image row pitch and slice pitch. The image object is not mapped. The image object is not used by any command-queue until the write command has finished execution.