Creates an OpenCL context.
cl_context clCreateContext(const cl_context_properties *properties,
cl_uint num_devices,
const cl_device_id *devices,
void ( CL_CALLBACK *pfn_notify) const char *errinfo, const void *private_info, size_t cb, void *user_data,
void *user_data,
cl_int *errcode_ret)An OpenCL context is created with one or more devices. Contexts are used by the OpenCL runtime for managing objects such as command-queues, memory, program and kernel objects and for executing kernels on one or more devices specified in the context.
- properties
-
Specifies a list of context property names and their corresponding values. Each property name is immediately followed by the corresponding desired value. The list is terminated with 0.
propertiescan be NULL in which case the platform that is selected is implementation-defined. The list of supportedpropertiesis described in the table below.If the extension
cl_khr_dx9_media_sharingis enabled, thenpropertiesspecifies a list of context property names and their corresponding values. Each property is followed immediately by the corresponding desired value. The list is terminated with zero. If a property is not specified inproperties, then its default value (listed in the table below) is used (it is said to be specified implicitly). Ifpropertiesis NULL or empty (points to a list whose first value is zero), all attributes take on their default values.If the extension
cl_khr_d3d10_sharingis enabled, thenpropertiesspecifies a list of context property names and their corresponding values. Each property is followed immediately by the corresponding desired value. The list is terminated with zero. If a property is not specified inproperties, then its default value is used (it is said to be specified implicitly). Ifpropertiesis NULL or empty (points to a list whose first value is zero), all attributes take on their default value.If the extension
cl_khr_d3d11_sharingis enabled, thenpropertiesspecifies a list of context property names and their corresponding values. Each property is followed immediately by the corresponding desired value. The list is terminated with zero. If a property is not specified inproperties, then its default value is used (it is said to be specified implicitly). Ifpropertiesis NULL or empty (points to a list whose first value is zero), all attributes take on their default value.If the extension
cl_khr_gl_sharingis enabled, thenpropertiespoints to an attribute list, which is a array of ordered <attribute name, value> pairs terminated with zero. If an attribute is not specified inproperties, then its default value is used (it is said to be specified implicitly). Ifpropertiesis NULL or empty (points to a list whose first value is zero), all attributes take on their default values. num_devices-
The number of devices specified in the
devicesargument. devices-
A pointer to a list of unique devices returned by
clGetDeviceIDsor sub-devices created byclCreateSubDevicesfor a platform. Duplicate devices specified indevicesare ignored. pfn_notify-
A callback function that can be registered by the application. This callback function will be used by the OpenCL implementation to report information on errors during context creation as well as errors that occur at runtime in this context. This callback function may be called asynchronously by the OpenCL implementation. It is the application’s responsibility to ensure that the callback function is thread-safe. If
pfn_notifyis NULL, no callback function is registered. The parameters to this callback function are:errinfois a pointer to an error string.private_infoandcbrepresent a pointer to binary data that is returned by the OpenCL implementation that can be used to log additional information helpful in debugging the error.user_datais a pointer to user supplied data.NoteThere are a number of cases where error notifications need to be delivered due to an error that occurs outside a context. Such notifications may not be delivered through the pfn_notifycallback. Where these notifications go is implementation-defined. user_data-
Passed as the
user_dataargument whenpfn_notifyis called.user_datacan be NULL. errcode_ret-
Returns an appropriate error code. If
errcode_retis NULL, no error code is returned.
clCreateContext and clCreateContextFromType perform an implicit retain.
This is very helpful for 3rd party libraries, which typically get a context passed to them by the application.
However, it is possible that the application may delete the context without informing the library.
Allowing functions to attach to (i.e.
retain) and release a context solves the problem of a context being used by a library no longer being valid.
If the cl_khr_terminate_context extension is enabled, CL_CONTEXT_TERMINATE_KHR can be specified in the context properties only if all devices associated with the context support the ability to support context termination (i.e.
CL_DEVICE_TERMINATE_CAPABILITY_CONTEXT_KHR is set for CL_DEVICE_TERMINATE_CAPABILITY_KHR).
Otherwise, context creation fails with error code of CL_INVALID_PROPERTY.
If the cl_khr_gl_sharing extension is enabled: Attributes control sharing of OpenCL memory objects with OpenGL buffer, texture, and renderbuffer objects as described in section 9.7.
Depending on the platform-specific API used to bind OpenGL contexts to the window system, the following attributes may be set to identify an OpenGL context:
-
When the CGL binding API is supported, the attribute
CL_CGL_SHAREGROUP_KHRshould be set to aCGLShareGrouphandle to a CGL share group object. -
When the EGL binding API is supported, the attribute
CL_GL_CONTEXT_KHRshould be set to anEGLContext`handle to an OpenGL ES or OpenGL context, and the attribute `CL_EGL_DISPLAY_KHRshould be set to the EGLDisplay handle of the display used to create the OpenGL ES or OpenGL context. -
When the GLX binding API is supported, the attribute
CL_GL_CONTEXT_KHRshould be set to aGLXContexthandle to an OpenGL context, and the attributeCL_GLX_DISPLAY_KHRshould be set to the Display handle of the X Window System display used to create the OpenGL context. -
When the WGL binding API is supported, the attribute
CL_GL_CONTEXT_KHRshould be set to an HGLRC handle to an OpenGL context, and the attributeCL_WGL_HDC_KHRshould be set to the HDC handle of the display used to create the OpenGL context.
If the cl_khr_gl_sharing extension is enabled: Memory objects created in the context so specified may be shared with the specified OpenGL or OpenGL ES context (as well as with any other OpenGL contexts on the share list of that context, according to the description of sharing in the GLX 1.4 and EGL 1.4 specifications, and the WGL documentation for OpenGL implementations on Microsoft Windows), or with the explicitly identified OpenGL share group for CGL.
If no OpenGL or OpenGL ES context or share group is specified in the attribute list, then memory objects may not be shared, and calling any of the commands in section 9.7 will result in a CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR error.
clCreateContext returns a valid non-zero context and errcode_ret is set to CL_SUCCESS if the context is created successfully.
Otherwise, it returns a NULL value with the following error values returned in errcode_ret:
-
CL_INVALID_PLATFORMifpropertiesis NULL and no platform could be selected or if platform value specified inpropertiesis not a valid platform. (If the extensioncl_khr_gl_sharingis enabled, then this error is replaced withCL_INVALID_GL_SHAREGROUP_REFERENCE_KHR; see below.) -
CL_INVALID_PROPERTYif context property name inpropertiesis not a supported property name, if the value specified for a supported property name is not valid, or if the same property name is specified more than once. However if the extensioncl_khr_gl_sharingis enabled, thenCL_INVALID_PROPERTYis returned if an attribute name other than those listed in the table forpropertiesabove or ifCL_CONTEXT_INTEROP_USER_SYNCis specified inproperties. -
CL_INVALID_PROPERTYif thecl_khr_gl_sharingextension is enabled and an attribute name other than those specified in the table above (table 4.5) or ifCL_CONTEXT_INTEROP_USER_SYNCis specified inproperties. -
CL_INVALID_VALUEifdevicesis NULL; ifnum_devicesis equal to zero; or ifpfn_notifyis NULL butuser_datais not NULL. -
CL_INVALID_DEVICEifdevicescontains an invalid device. -
CL_INVALID_OPERATIONif Direct3D 10 interoperability is specified by settingCL_INVALID_D3D10_DEVICE_KHRto a non-NULL value, and interoperability with another graphics API is also specified (if thecl_khr_d3d10_sharingextension is enabled). -
CL_DEVICE_NOT_AVAILABLEif a device indevicesis currently not available even though the device was returned byclGetDeviceIDs. -
CL_OUT_OF_RESOURCESif there is a failure to allocate resources required by the OpenCL implementation on the device. -
CL_OUT_OF_HOST_MEMORYif there is a failure to allocate resources required by the OpenCL implementation on the host. -
CL_INVALID_D3D10_DEVICE_KHRif the Direct3D 10 device specified for interoperability is not compatible with the devices against which the context is to be created (if thecl_khr_d3d10_sharingextension is enabled). -
CL_INVALID_D3D10_DEVICE_KHRif the value of the propertyCL_CONTEXT_D3D10_DEVICE_KHRis non-NULL and does not specify a valid Direct3D 10 device with which thecl_device_idsagainst which this context is to be created may interoperate (if thecl_khr_d3d10_sharingextension is enabled). -
CL_INVALID_GL_SHAREGROUP_REFERENCE_KHRwhen an invalid OpenGL context or share group object handle is specified inproperties(only if thecl_khr_gl_sharingextension is enabled). -
CL_INVALID_GL_SHAREGROUP_REFERENCE_KHRif no OpenGL or OpenGL ES context or share group is specified in the attribute list given toclCreateContextand any of the commands in section 9.7 are called. (if thecl_khr_gl_sharingextension is enabled) -
CL_INVALID_GL_SHAREGROUP_REFERENCE_KHRif thecl_khr_gl_sharingextension is enabled and if a context was specified by any of the following means:-
A context specified for an EGL-based OpenGL ES or OpenGL implementation by setting the attributes
CL_GL_CONTEXT_KHRandCL_EGL_DISPLAY_KHR. -
A context was specified for a GLX-based OpenGL implementation by setting the attributes
CL_GL_CONTEXT_KHRandCL_GLX_DISPLAY_KHR. -
A context was specified for a WGL-based OpenGL implementation by setting the attributes
CL_GL_CONTEXT_KHRandCL_WGL_HDC_KHR.
and any of the following conditions hold:
-
The specified display and context attributes do not identify a valid OpenGL or OpenGL ES context.
-
The specified context does not support buffer and renderbuffer objects.
-
The specified context is not compatible with the OpenCL context being created (for example, it exists in a physically distinct address space, such as another hardware device, or does not support sharing data with OpenCL due to implementation restrictions).
-
-
CL_INVALID_GL_SHAREGROUP_REFERENCE_KHRif a share group was specified for a CGL-based OpenGL implementation by setting the attributeCL_CGL_SHAREGROUP_KHR, and the specified share group does not identify a valid CGL share group object (if thecl_khr_gl_sharingextension is enabled). -
CL_INVALID_OPERATIONif thecl_khr_gl_sharingextension is enabled and if a context was specified as described above and any of the following conditions hold:-
A context or share group object was specified for one of CGL, EGL, GLX, or WGL and the OpenGL implementation does not support that window-system binding API.
-
More than one of the attributes
CL_CGL_SHAREGROUP_KHR,CL_EGL_DISPLAY_KHR,CL_GLX_DISPLAY_KHR, andCL_WGL_HDC_KHRis set to a non-default value. -
Both of the attributes
CL_CGL_SHAREGROUP_KHRandCL_GL_CONTEXT_KHRare set to non-default values. -
Any of the devices specified in the
devicesargument cannot support OpenCL objects which share the data store of an OpenGL object, as described in section 9.7.
-
-
CL_INVALID_DX9_MEDIA_ADAPTER_KHRif the media adapter specified for interoperability is not compatible with the devices against which the context is to be created (only if thecl_khr_dx9_media_sharingextension is supported). -
CL_INVALID_ADAPTER_KHRif any of the values of the propertiesCL_CONTEXT_ADAPTER_D3D9_KHR,CL_CONTEXT_ADAPTER_D3D9EX_KHRorCL_CONTEXT_ADAPTER_DXVA_KHRis non-NULL and does not specify a valid media adapter with which thecl_device_idsagainst which this context is to be created may interoperate (only if thecl_khr_dx9_media_sharingextension is supported). -
CL_INVALID_OPERATIONif interoperability is specified by settingCL_CONTEXT_ADAPTER_D3D9_KHR,CL_CONTEXT_ADAPTER_D3D9EX_KHRorCL_CONTEXT_ADAPTER_DXVA_KHRto a non-NULL value, and interoperability with another graphics API is also specified (only if thecl_khr_dx9_media_sharingextension is supported). -
CL_INVALID_OPERATIONif Direct3D 11 interoperability is specified by settingCL_INVALID_D3D11_DEVICE_KHRto a non-NULL value, and interoperability with another graphics API is also specified (only if thecl_khr_d3d11_sharingextension is supported). -
CL_INVALID_D3D11_DEVICE_KHRif the value of the propertyCL_CONTEXT_D3D11_DEVICE_KHRis non-NULL and does not specify a valid Direct3D 11 device with which thecl_device_idsagainst which this context is to be created may interoperate (only if thecl_khr_d3d11_sharingextension is supported). -
CL_INVALID_D3D11_DEVICE_KHRif the Direct3D 11 device specified for interoperability is not compatible with the devices against which the context is to be created (only if thecl_khr_d3d11_sharingextension is supported).