C Specification

To create an OpenCL 2D image object from an OpenGL renderbuffer object, call the function

// Provided by cl_khr_gl_sharing
cl_mem clCreateFromGLRenderbuffer(
    cl_context context,
    cl_mem_flags flags,
    cl_GLuint renderbuffer,
    cl_int* errcode_ret);
clCreateFromGLRenderbuffer is provided by the cl_khr_gl_sharing extension.

Parameters

  • context is a valid OpenCL context created from an OpenGL context.

  • flags is a bit-field that is used to specify usage information. Refer to the Memory Flags table for a description of flags. Only the CL_MEM_READ_ONLY, CL_MEM_WRITE_ONLY and CL_MEM_READ_WRITE flags specified in that table can be used.

  • renderbuffer is the name of an OpenGL renderbuffer object. The renderbuffer storage must be specified before the image object can be created. The renderbuffer format and dimensions defined by OpenGL will be used to create the 2D image object. Only OpenGL renderbuffers with an internal format that maps to an appropriate Image Channel Order and Image Channel Data Type may be used to create the 2D image object.

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

Description

If the state of an OpenGL renderbuffer object is modified through the OpenGL API (i.e. changes to the dimensions or format used to represent pixels of the OpenGL renderbuffer using appropriate OpenGL API calls such as glRenderbufferStorage) while there exists a corresponding OpenCL image object, subsequent use of the OpenCL image object will result in undefined behavior.

The clRetainMemObject and clReleaseMemObject functions can be used to retain and release the image objects.

The OpenGL Internal Formats and Corresponding OpenCL Internal Formats table describes the list of OpenGL renderbuffer internal formats and the Corresponding OpenCL Image Formats. If an OpenGL renderbuffer object with an internal format from the table is successfully created by OpenGL, then there is guaranteed to be a mapping to one of the corresponding OpenCL image format(s) in that table. Renderbuffer objects created with other OpenGL internal formats may (but are not guaranteed to) have a mapping to an OpenCL image format; if such mappings exist, they are guaranteed to preserve all color components, data types, and at least the number of bits/component actually allocated by OpenGL for that format.

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

  • CL_INVALID_CONTEXT if context is not a valid context or was not created from an OpenGL context.

  • CL_INVALID_VALUE if values specified in flags are not valid.

  • CL_INVALID_GL_OBJECT if renderbuffer is not an OpenGL renderbuffer object, or if the width or height of renderbuffer is zero.

  • CL_INVALID_IMAGE_FORMAT_DESCRIPTOR if the internal format of renderbuffer is not listed in the OpenGL Internal Formats and Corresponding OpenCL Internal Formats table.

  • CL_INVALID_OPERATION if renderbuffer is a multi-sample OpenGL renderbuffer 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.

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