C Specification

An image object may be created using the function

// Provided by CL_VERSION_1_2
cl_mem clCreateImage(
    cl_context context,
    cl_mem_flags flags,
    const cl_image_format* image_format,
    const cl_image_desc* image_desc,
    void* host_ptr,
    cl_int* errcode_ret);
clCreateImage is missing before version 1.2.

An image object may also be created with additional properties using the function

// Provided by CL_VERSION_3_0
cl_mem clCreateImageWithProperties(
    cl_context context,
    const cl_mem_properties* properties,
    cl_mem_flags flags,
    const cl_image_format* image_format,
    const cl_image_desc* image_desc,
    void* host_ptr,
    cl_int* errcode_ret);
clCreateImageWithProperties is missing before version 3.0.

Parameters

  • context is a valid OpenCL context used to create the image object.

  • properties is an optional list of properties for the image object and their corresponding values. The list is terminated with the special property 0. If no properties are required, properties may be NULL. OpenCL 3.0 does not define any optional properties for images, but extensions may define properties as described in the https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#external-image-memory-properties-table.

  • flags is a bit-field that is used to specify allocation and usage information about the image memory object being created and is described in the supported memory flag values table.

  • image_format is a pointer to a structure that describes format properties of the image to be allocated. A 1D image buffer can be created from a buffer by specifying a buffer object in image_descmem_object. If the cl_khr_image2d_from_buffer extension is supported, a 2D image can be created from a buffer by specifying a buffer object in image_descmem_object. If the cl_ext_image_from_buffer extension is supported, an image of any type can be created from a buffer by specifying a buffer object in image_descmem_object. A 2D image can be created from another 2D image object by specifying an image object in the image_descmem_object. Refer to the Image Format Descriptor section for a detailed description of the image format descriptor.

  • image_desc is a pointer to a structure that describes type and dimensions of the image to be allocated. Refer to the Image Descriptor section for a detailed description of the image descriptor.

  • host_ptr is a pointer to the image data that may already be allocated by the application. Refer to the table below for a description of how large the buffer that host_ptr points to must be.

  • errcode_ret will return an appropriate error code. If errcode_ret is NULL, no error code is returned.

Description

The alignment requirements for data stored in image objects are described in Alignment of Application Data Types.

For all image types except CL_MEM_OBJECT_IMAGE1D_BUFFER, if the value specified for flags is 0, the default is used which is CL_MEM_READ_WRITE.

For CL_MEM_OBJECT_IMAGE1D_BUFFER image type, or an image created from another memory object (image or buffer), if the CL_MEM_READ_WRITE, CL_MEM_READ_ONLY or CL_MEM_WRITE_ONLY values are not specified in flags, they are inherited from the corresponding memory access qualifiers associated with mem_object. The CL_MEM_USE_HOST_PTR, CL_MEM_ALLOC_HOST_PTR and CL_MEM_COPY_HOST_PTR values cannot be specified in flags but are inherited from the corresponding memory access qualifiers associated with mem_object. If CL_MEM_COPY_HOST_PTR is specified in the memory access qualifier values associated with mem_object it does not imply any additional copies when the image is created from mem_object. If the CL_MEM_HOST_WRITE_ONLY, CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_NO_ACCESS values are not specified in flags, they are inherited from the corresponding memory access qualifiers associated with mem_object.

Mipmap Images

A mipmapped 1D image, 1D image array, 2D image, 2D image array or 3D image is created by specifying num_mip_levels to be a value greater than one in image_desc. The dimensions of a mipmapped image can be a power of two or a non-power of two. Each successively smaller mipmap level is half the size of the previous level, rounded down to the nearest integer.

The following restrictions apply when mipmapped images are created with clCreateImage:

  • CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR cannot be specified if a mipmapped image is created.

  • The host_ptr argument to clCreateImage must be a NULL value.

  • Mip-mapped images cannot be created for CL_MEM_OBJECT_IMAGE1D_BUFFER images, depth images or multi-sampled (i.e. msaa) images.

Image Data in Host Memory

For a 3D image or 2D image array, the image data specified by host_ptr is stored as a linear sequence of adjacent 2D image slices or 2D images respectively. Each 2D image is a linear sequence of adjacent scanlines. Each scanline is a linear sequence of image elements.

For a 2D image, the image data specified by host_ptr is stored as a linear sequence of adjacent scanlines. Each scanline is a linear sequence of image elements.

For a 1D image array, the image data specified by host_ptr is stored as a linear sequence of adjacent 1D images. Each 1D image is stored as a single scanline which is a linear sequence of adjacent elements.

For 1D image or 1D image buffer, the image data specified by host_ptr is stored as a single scanline which is a linear sequence of adjacent elements.

Image elements are stored according to their image format as described in the Image Format Descriptor section.

Table 1. List of supported image creation properties
Property Property Value Description

CL_MEM_DEVICE_HANDLE_LIST_KHR

provided by the cl_khr_external_memory extension.

cl_device_id[]

Specifies the list of OpenCL devices (terminated with CL_MEM_DEVICE_HANDLE_LIST_END_KHR) to associate with the external memory handle.

If CL_MEM_DEVICE_HANDLE_LIST_KHR is not specified as part of properties, the memory object created by clCreateBufferWithProperties or clCreateImageWithProperties is by default associated with all devices in the context.

The properties used to create an image from an external memory handle are described for the corresponding extensions. When an image is created from an external memory handle, the flags used to specify usage information for the image must not include CL_MEM_USE_HOST_PTR, CL_MEM_ALLOC_HOST_PTR, or CL_MEM_COPY_HOST_PTR, and the host_ptr argument must be NULL. When images are created from an external memory handle, implementations may acquire information about image attributes such as format and layout at the time of creation. When such information is acquired at image creation time, it is used for the lifetime of the image object.

If CL_MEM_IMMUTABLE_EXT is set in flags, the image created is an immutable memory object that cannot be modified by the implementation after creation, and one and only one of the following conditions must be met:

  • Either CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR must be set in flags.

  • An external memory handle must be included in properties.

If the image being created is created from another memory object and that memory object is an immutable memory object, then CL_MEM_IMMUTABLE_EXT must be set in flags.

clCreateImage and clCreateImageWithProperties returns a valid non-zero image object and errcode_ret is set to CL_SUCCESS if the image object is created successfully. Otherwise, they return a NULL value with one of the following error values returned in errcode_ret:

  • CL_INVALID_CONTEXT

    • if context is not a valid context

  • CL_INVALID_OPERATION

    • if there are no devices in context that support images (i.e. CL_DEVICE_IMAGE_SUPPORT specified in the Device Queries table is CL_FALSE)

  • CL_INVALID_PROPERTY

    • if a property name in properties is not a supported property name

    • if the value specified for a supported property name is not valid

    • if the same property name is specified more than once

    • if properties does not include a supported external memory handle and CL_MEM_DEVICE_HANDLE_LIST_KHR is specified as part of properties

    • if properties includes more than one external memory handle

  • CL_INVALID_DEVICE

    • if a device identified by the property CL_MEM_DEVICE_HANDLE_LIST_KHR is not a valid device or is not associated with context

    • if a device identified by property CL_MEM_DEVICE_HANDLE_LIST_KHR cannot import the requested external memory object type

    • if CL_MEM_DEVICE_HANDLE_LIST_KHR is not specified as part of properties and one or more devices in context cannot import the requested external memory object type

  • CL_INVALID_VALUE

    • if values specified in flags are not valid as defined in the Memory Flags table

    • if an image is being created from another memory object (buffer or image) under one of the following circumstances:

      • mem_object was created with CL_MEM_WRITE_ONLY and flags specifies CL_MEM_READ_WRITE or CL_MEM_READ_ONLY

      • mem_object was created with CL_MEM_READ_ONLY and flags specifies CL_MEM_READ_WRITE or CL_MEM_WRITE_ONLY

      • flags specifies CL_MEM_USE_HOST_PTR or CL_MEM_ALLOC_HOST_PTR or CL_MEM_COPY_HOST_PTR

    • if an image is being created from another memory object (buffer or image) and mem_object was created with CL_MEM_HOST_WRITE_ONLY and flags specifies CL_MEM_HOST_READ_ONLY

    • if mem_object was created with CL_MEM_HOST_READ_ONLY and flags specifies CL_MEM_HOST_WRITE_ONLY

    • if mem_object was created with CL_MEM_HOST_NO_ACCESS and_flags_ specifies CL_MEM_HOST_READ_ONLY or CL_MEM_HOST_WRITE_ONLY

    • if properties includes a supported external memory handle and flags includes CL_MEM_USE_HOST_PTR, CL_MEM_ALLOC_HOST_PTR, or CL_MEM_COPY_HOST_PTR

    • if CL_MEM_IMMUTABLE_EXT is set in flags and CL_MEM_READ_WRITE, CL_MEM_WRITE_ONLY, or CL_MEM_HOST_WRITE_ONLY is set in flags

    • if CL_MEM_IMMUTABLE_EXT is not set in flags and mem_object was created with CL_MEM_IMMUTABLE_EXT

    • if CL_MEM_IMMUTABLE_EXT is set in flags and none of the following conditions are met:

      • CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR is set in flags

      • the image is being created from another memory object (buffer or image)

      • properties includes an external memory handle

  • CL_INVALID_IMAGE_FORMAT_DESCRIPTOR

    • if image_format is NULL

    • if values specified in image_format are not valid

    • if properties includes an AHardwareBuffer external memory handle and image_format is not NULL

    • if an image is created from a buffer and the row pitch, or slice pitch, if the cl_ext_image_from_buffer extension is supported, or base address alignment do not follow the rules described for creating an image from a buffer

  • CL_INVALID_IMAGE_DESCRIPTOR

    • if properties includes an AHardwareBuffer external memory handle and image_desc is not NULL

  • CL_INVALID_IMAGE_SIZE

    • if the image dimensions specified in image_desc are not valid or exceed the maximum image dimensions described in the Device Queries table for all devices in context

    • if the cl_ext_image_from_buffer extension is supported and an image is created from a buffer and the buffer passed in mem_object is too small to be used as a data store for the image, e.g. if its size is smaller than the value returned for CL_IMAGE_REQUIREMENTS_SIZE_EXT for the parameters used to create the image

  • CL_INVALID_HOST_PTR

    • if host_ptr is NULL and CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR are set in flags

    • if host_ptr is not NULL but CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR are not set in flags

    • if properties includes a supported external memory handle and host_ptr is not NULL

  • CL_IMAGE_FORMAT_NOT_SUPPORTED

    • if there are no devices in context that support image_format

    • if properties includes an AHardwareBuffer external memory handle and the AHardwareBuffer format is not supported

  • CL_MEM_OBJECT_ALLOCATION_FAILURE

    • if there is a failure to allocate memory for the image object

  • 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

Table 2. Required host_ptr buffer sizes for images
Image Type Size of buffer that host_ptr points to

CL_MEM_OBJECT_IMAGE1D

missing before version 1.2.

≥ image_row_pitch

CL_MEM_OBJECT_IMAGE1D_BUFFER

missing before version 1.2.

≥ image_row_pitch

CL_MEM_OBJECT_IMAGE2D

≥ image_row_pitch × image_height

CL_MEM_OBJECT_IMAGE3D

≥ image_slice_pitch × image_depth

CL_MEM_OBJECT_IMAGE1D_ARRAY

missing before version 1.2.

≥ image_slice_pitch × image_array_size

CL_MEM_OBJECT_IMAGE2D_ARRAY

missing before version 1.2.

≥ image_slice_pitch × image_array_size

See Also

Document Notes

For more information, see the OpenCL Specification

This page is extracted from the OpenCL Specification. Fixes and changes should be made to the Specification, not directly.

Copyright 2014-2025 The Khronos Group Inc.

SPDX-License-Identifier: CC-BY-4.0