C Specification

To create an OpenCL buffer object from an OpenGL buffer object, call the function

// Provided by cl_khr_gl_sharing
cl_mem clCreateFromGLBuffer(
    cl_context context,
    cl_mem_flags flags,
    cl_GLuint bufobj,
    cl_int* errcode_ret);
clCreateFromGLBuffer 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.

  • bufobj is the name of an OpenGL buffer object. The data store of the OpenGL buffer object must have been previously created by calling glBufferData, although its contents need not be initialized. The size of the data store will be used to determine the size of the OpenCL buffer object.

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

Description

The size of the OpenGL buffer object data store at the time clCreateFromGLBuffer is called will be used as the size of buffer object returned by clCreateFromGLBuffer. If the state of an OpenGL buffer object is modified through the OpenGL API (e.g. glBufferData) while there exists a corresponding OpenCL buffer object, subsequent use of the OpenCL buffer object will result in undefined behavior.

The clRetainMemObject and clReleaseMemObject functions can be used to retain and release the buffer object.

The OpenCL buffer object created using clCreateFromGLBuffer can also be used to create an OpenCL 1D image buffer object.

clCreateFromGLBuffer returns a valid non-zero OpenCL buffer object and errcode_ret is set to CL_SUCCESS if the buffer 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 bufobj is not an OpenGL buffer object or is a OpenGL buffer object but does not have an existing data store or the size of the buffer is 0.

  • 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