@@ -65,6 +65,153 @@ clGetICDLoaderInfoOCLICD(
6565 return CL_SUCCESS ;
6666}
6767
68+ #if KHR_LOADER_MANAGED_DISPATCH
69+
70+ static clCreateInstanceKHR_t clCreateInstanceKHR ;
71+ cl_instance CL_API_CALL
72+ clCreateInstanceKHR (
73+ const cl_instance_properties * properties ,
74+ cl_int * errcode_ret )
75+ {
76+ #define KHR_ICD_ALLOC_AND_ZERO (var , num , type ) \
77+ do \
78+ { \
79+ var = (type *)malloc(num * sizeof(type)); \
80+ if (!var) \
81+ { \
82+ err = CL_OUT_OF_HOST_MEMORY; \
83+ goto error; \
84+ } \
85+ memset(var, 0, num * sizeof(type)); \
86+ } while (0)
87+
88+ (void ) properties ;
89+ cl_int err = CL_SUCCESS ;
90+ cl_instance instance = NULL ;
91+ cl_uint num_platforms = 0 ;
92+
93+ if (!khrIcdVendors )
94+ {
95+ err = CL_PLATFORM_NOT_FOUND_KHR ;
96+ goto error ;
97+ }
98+
99+ for (KHRicdVendor * vendor = khrIcdVendors ; vendor ; vendor = vendor -> next )
100+ {
101+ if (vendor -> clIcdCreateInstancePlatform )
102+ num_platforms ++ ;
103+ }
104+
105+ KHR_ICD_ALLOC_AND_ZERO (instance , 1 , struct _cl_instance );
106+ KHR_ICD_ALLOC_AND_ZERO (instance -> platforms , num_platforms , cl_platform_id );
107+ KHR_ICD_ALLOC_AND_ZERO (instance -> vendors , num_platforms , KHRicdVendor * );
108+ KHR_ICD_ALLOC_AND_ZERO (instance -> dispDatas , num_platforms , struct KHRDisp );
109+
110+ num_platforms = 0 ;
111+ for (KHRicdVendor * vendor = khrIcdVendors ; vendor ; vendor = vendor -> next )
112+ {
113+ if (vendor -> clIcdCreateInstancePlatform )
114+ {
115+ cl_platform_id platform ;
116+ platform = vendor -> clIcdCreateInstancePlatform (vendor -> platform , & err );
117+ if (CL_SUCCESS != err )
118+ continue ;
119+ vendor -> clIcdSetPlatformDispatchData (platform , instance -> dispDatas + num_platforms );
120+ instance -> platforms [num_platforms ] = platform ;
121+ instance -> vendors [num_platforms ] = vendor ;
122+ memcpy (instance -> dispDatas + num_platforms , & vendor -> dispData , sizeof (struct KHRDisp ));
123+ num_platforms ++ ;
124+ }
125+ }
126+
127+ if (!num_platforms )
128+ {
129+ err = CL_PLATFORM_NOT_FOUND_KHR ;
130+ goto error ;
131+ }
132+
133+ instance -> num_platforms = num_platforms ;
134+
135+ if (errcode_ret )
136+ * errcode_ret = CL_SUCCESS ;
137+ return instance ;
138+ error :
139+ if (instance )
140+ {
141+ free (instance -> dispDatas );
142+ free (instance -> vendors );
143+ free (instance -> platforms );
144+ free (instance );
145+ }
146+ if (errcode_ret )
147+ * errcode_ret = err ;
148+ #undef KHR_ICD_ALLOC_AND_ZERO
149+ return NULL ;
150+ }
151+
152+ static clDestroyInstanceKHR_t clDestroyInstanceKHR ;
153+ cl_int CL_API_CALL
154+ clDestroyInstanceKHR (
155+ cl_instance instance )
156+ {
157+ KHR_ICD_VALIDATE_HANDLE_RETURN_ERROR (instance , CL_INVALID_INSTANCE_KHR );
158+ for (cl_uint i = 0 ; i < instance -> num_platforms ; i ++ )
159+ instance -> vendors [i ]-> clIcdDestroyInstancePlatform (instance -> platforms [i ]);
160+ free (instance -> dispDatas );
161+ free (instance -> vendors );
162+ free (instance -> platforms );
163+ free (instance );
164+ return CL_SUCCESS ;
165+ }
166+
167+ static clGetPlatformIDsForInstanceKHR_t clGetPlatformIDsForInstanceKHR ;
168+ cl_int CL_API_CALL
169+ clGetPlatformIDsForInstanceKHR (
170+ cl_instance instance ,
171+ cl_uint num_entries ,
172+ cl_platform_id * platforms ,
173+ cl_uint * num_platforms )
174+ {
175+ cl_uint i ;
176+
177+ KHR_ICD_VALIDATE_HANDLE_RETURN_ERROR (instance , CL_INVALID_INSTANCE_KHR );
178+ // should be impossible since create would have refused to
179+ // create an empty instance
180+ if (!instance -> num_platforms )
181+ {
182+ return CL_INVALID_INSTANCE_KHR ;
183+ }
184+ if (!num_entries && platforms )
185+ {
186+ return CL_INVALID_VALUE ;
187+ }
188+ if (!platforms && !num_platforms )
189+ {
190+ return CL_INVALID_VALUE ;
191+ }
192+ if (num_platforms )
193+ {
194+ * num_platforms = instance -> num_platforms ;
195+ }
196+ if (platforms )
197+ {
198+ for (i = 0 ; i < num_entries ; ++ i )
199+ {
200+ platforms [i ] = NULL ;
201+ }
202+ if (instance -> num_platforms < num_entries )
203+ {
204+ num_entries = instance -> num_platforms ;
205+ }
206+ for (i = 0 ; i < num_entries ; ++ i )
207+ {
208+ platforms [i ] = instance -> platforms [i ];
209+ }
210+ }
211+ return CL_SUCCESS ;
212+ }
213+ #endif //KHR_LOADER_MANAGED_DISPATCH
214+
68215static void * khrIcdGetExtensionFunctionAddress (const char * function_name )
69216{
70217// Most extensions, including multi-vendor KHR and EXT extensions,
@@ -139,6 +286,13 @@ static void* khrIcdGetExtensionFunctionAddress(const char* function_name)
139286 // cl_icdl
140287 KHR_ICD_CHECK_EXTENSION_FUNCTION (clGetICDLoaderInfoOCLICD );
141288
289+ #if KHR_LOADER_MANAGED_DISPATCH
290+ // cl_khr_instances
291+ KHR_ICD_CHECK_EXTENSION_FUNCTION (clCreateInstanceKHR );
292+ KHR_ICD_CHECK_EXTENSION_FUNCTION (clDestroyInstanceKHR );
293+ KHR_ICD_CHECK_EXTENSION_FUNCTION (clGetPlatformIDsForInstanceKHR );
294+ #endif
295+
142296#undef KHR_ICD_CHECK_EXTENSION_FUNCTION
143297
144298 return NULL ;
0 commit comments