Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/call_all.c.mako
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ skipExtensions = {
defaultValueForType = {
# Handle Types
'cl_accelerator_intel' : 'NULL',
'cl_command_buffer_khr' : 'NULL',
'cl_command_queue' : 'NULL',
'cl_context' : 'NULL',
'cl_device_id' : 'NULL',
Expand All @@ -38,6 +39,7 @@ defaultValueForType = {
'cl_accelerator_info_intel' : 'CL_ACCELERATOR_DESCRIPTOR_INTEL',
'cl_accelerator_type_intel' : '0',
'cl_bool' : 'CL_FALSE',
'cl_command_buffer_info_khr' : 'CL_COMMAND_BUFFER_REFERENCE_COUNT_KHR',
'cl_d3d10_device_source_khr' : 'CL_D3D10_DEVICE_KHR',
'cl_d3d10_device_set_khr' : 'CL_ALL_DEVICES_FOR_D3D10_KHR',
'cl_d3d11_device_source_khr' : 'CL_D3D11_DEVICE_KHR',
Expand Down
169 changes: 133 additions & 36 deletions scripts/openclext.cpp.mako
Original file line number Diff line number Diff line change
Expand Up @@ -273,42 +273,6 @@ static inline cl_platform_id _get_platform(cl_mem memobj)
return _get_platform(context);
}

#if defined(cl_khr_semaphore)

static inline cl_platform_id _get_platform(cl_semaphore_khr semaphore)
{
if (semaphore == NULL) return NULL;

cl_context context = NULL;
clGetSemaphoreInfoKHR(
semaphore,
CL_SEMAPHORE_CONTEXT_KHR,
sizeof(context),
&context,
NULL);
return _get_platform(context);
}

#endif // defined(cl_khr_semaphore)

#if defined(cl_intel_accelerator)

static inline cl_platform_id _get_platform(cl_accelerator_intel accelerator)
{
if (accelerator == NULL) return NULL;

cl_context context = NULL;
clGetAcceleratorInfoINTEL(
accelerator,
CL_ACCELERATOR_CONTEXT_INTEL,
sizeof(context),
&context,
NULL);
return _get_platform(context);
}

#endif // defined(cl_intel_accelerator)

/***************************************************************
* Function Pointer Typedefs
***************************************************************/
Expand Down Expand Up @@ -437,20 +401,54 @@ static void _init(cl_platform_id platform, openclext_dispatch_table* dispatch_pt
}

#if defined(CLEXT_SINGLE_PLATFORM_ONLY)

static openclext_dispatch_table _dispatch = {0};
static openclext_dispatch_table* _dispatch_ptr = NULL;

template<typename T>
static inline openclext_dispatch_table* _get_dispatch(T object)
{
if (object == NULL) return NULL;

if (_dispatch_ptr == NULL) {
cl_platform_id platform = _get_platform(object);
_init(platform, &_dispatch);
_dispatch_ptr = &_dispatch;
}

return _dispatch_ptr;
}

// For some extension objects we cannot reliably query a platform ID without
// infinitely recursing. For these objects we cannot initialize the dispatch
// table if it is not already initialized.

#if defined(cl_khr_semaphore)
template<>
inline openclext_dispatch_table* _get_dispatch<cl_semaphore_khr>(cl_semaphore_khr)
{
return _dispatch_ptr;
}
#endif // defined(cl_khr_semaphore)

#if defined(cl_khr_command_buffer)
template<>
inline openclext_dispatch_table* _get_dispatch<cl_command_buffer_khr>(cl_command_buffer_khr)
{
return _dispatch_ptr;
}
#endif // defined(cl_khr_command_buffer)

#if defined(cl_intel_accelerator)
template<>
inline openclext_dispatch_table* _get_dispatch<cl_accelerator_intel>(cl_accelerator_intel)
{
return _dispatch_ptr;
}
#endif // defined(cl_intel_accelerator)

#else // defined(CLEXT_SINGLE_PLATFORM_ONLY)

static size_t _num_platforms = 0;
static openclext_dispatch_table* _dispatch_array = NULL;

Expand All @@ -460,6 +458,9 @@ static openclext_dispatch_table* _get_dispatch(T object)
if (_num_platforms == 0 && _dispatch_array == NULL) {
cl_uint numPlatforms = 0;
clGetPlatformIDs(0, NULL, &numPlatforms);
if (numPlatforms == 0) {
return NULL;
}

openclext_dispatch_table* dispatch =
(openclext_dispatch_table*)malloc(
Expand Down Expand Up @@ -490,6 +491,96 @@ static openclext_dispatch_table* _get_dispatch(T object)

return NULL;
}

// For some extension objects we cannot reliably query a platform ID without
// infinitely recursing. For these objects we cannot initialize the dispatch
// table if it is not already initialized, and we need to use other methods
// to find the right dispatch table.

#if defined(cl_khr_semaphore)
template<>
inline openclext_dispatch_table* _get_dispatch<cl_semaphore_khr>(cl_semaphore_khr semaphore)
{
if (semaphore == NULL) return NULL;
if (_num_platforms <= 1) return _dispatch_array;

for (size_t i = 0; i < _num_platforms; i++) {
openclext_dispatch_table* dispatch_ptr =
_dispatch_array + i;
if (dispatch_ptr->clGetSemaphoreInfoKHR) {
cl_uint refCount = 0;
cl_int errorCode = dispatch_ptr->clGetSemaphoreInfoKHR(
semaphore,
CL_SEMAPHORE_REFERENCE_COUNT_KHR,
sizeof(refCount),
&refCount,
NULL);
if (errorCode == CL_SUCCESS) {
return dispatch_ptr;
}
}
}

return NULL;
}
#endif // defined(cl_khr_semaphore)

#if defined(cl_khr_command_buffer)
template<>
inline openclext_dispatch_table* _get_dispatch<cl_command_buffer_khr>(cl_command_buffer_khr cmdbuf)
{
if (cmdbuf == NULL) return NULL;
if (_num_platforms <= 1) return _dispatch_array;

for (size_t i = 0; i < _num_platforms; i++) {
openclext_dispatch_table* dispatch_ptr =
_dispatch_array + i;
if (dispatch_ptr->clGetCommandBufferInfoKHR) {
cl_uint refCount = 0;
cl_int errorCode = dispatch_ptr->clGetCommandBufferInfoKHR(
cmdbuf,
CL_COMMAND_BUFFER_REFERENCE_COUNT_KHR,
sizeof(refCount),
&refCount,
NULL);
if (errorCode == CL_SUCCESS) {
return dispatch_ptr;
}
}
}

return NULL;
}
#endif // defined(cl_khr_command_buffer)

#if defined(cl_intel_accelerator)
template<>
inline openclext_dispatch_table* _get_dispatch<cl_accelerator_intel>(cl_accelerator_intel accelerator)
{
if (accelerator == NULL) return NULL;
if (_num_platforms <= 1) return _dispatch_array;

for (size_t i = 0; i < _num_platforms; i++) {
openclext_dispatch_table* dispatch_ptr =
_dispatch_array + i;
if (dispatch_ptr->clGetAcceleratorInfoINTEL) {
cl_uint refCount = 0;
cl_int errorCode = dispatch_ptr->clGetAcceleratorInfoINTEL(
accelerator,
CL_ACCELERATOR_REFERENCE_COUNT_INTEL,
sizeof(refCount),
&refCount,
NULL);
if (errorCode == CL_SUCCESS) {
return dispatch_ptr;
}
}
}

return NULL;
}
#endif // defined(cl_intel_accelerator)

#endif // defined(CLEXT_SINGLE_PLATFORM_ONLY)

#ifdef __cplusplus
Expand Down Expand Up @@ -525,7 +616,13 @@ ${api.RetType} CL_API_CALL ${api.Name}(
% endif
% endfor
{
% if api.Name == "clCreateCommandBufferKHR":
struct openclext_dispatch_table* dispatch_ptr = _get_dispatch(${api.Params[0].Name} > 0 && ${api.Params[1].Name} ? ${api.Params[1].Name}[0] : NULL);
% elif api.Name == "clEnqueueCommandBufferKHR":
struct openclext_dispatch_table* dispatch_ptr = _get_dispatch(${api.Params[2].Name});
% else:
struct openclext_dispatch_table* dispatch_ptr = _get_dispatch(${api.Params[0].Name});
% endif
if (dispatch_ptr == NULL || dispatch_ptr->${api.Name} == NULL) {
% if api.RetType == "cl_int":
return CL_INVALID_OPERATION;
Expand Down
Loading