summaryrefslogtreecommitdiff
path: root/scripts/cl_ext.h.mako
blob: c1f89260422c53a3093845a75344d3c7b3078718 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<%
# Extensions to skip by default because they are in dedicated headers:
skipExtensions = {
    'cl_khr_d3d10_sharing',
    'cl_khr_d3d11_sharing',
    'cl_khr_dx9_media_sharing',
    'cl_khr_egl_event',
    'cl_khr_egl_image',
    'cl_khr_gl_depth_images',
    'cl_khr_gl_event',
    'cl_khr_gl_msaa_sharing',
    'cl_khr_gl_sharing',
    'cl_loader_layers',
    'cl_intel_dx9_media_sharing',
    'cl_intel_va_api_media_sharing',
    'cl_intel_sharing_format_query_d3d10',
    'cl_intel_sharing_format_query_d3d11',
    'cl_intel_sharing_format_query_dx9',
    'cl_intel_sharing_format_query_gl',
    'cl_intel_sharing_format_query_va_api',
    }

# Extensions to include in this header:
def shouldGenerate(extension):
    if extension in genExtensions:
        return True
    elif not genExtensions and not extension in skipExtensions:
        return True
    return False

# XML blocks to include in the headers:
def shouldEmit(block):
    for type in block.findall('type'):
        if type.get('name') in typedefs:
            return True
        elif type.get('name') in macros:
            return True
        elif type.get('name') in structs:
            return True
    for enum in block.findall('enum'):
        #if enum.get('name') in enums:
            return True
    for func in block.findall('command'):
        return True
    return False

# Initially, keep the same extension order as the original headers:
orderedExtensions = [
    # cl_ext.h:
    'cl_khr_command_buffer',
    'cl_khr_command_buffer_multi_device',
    'cl_khr_command_buffer_mutable_dispatch',
    'cl_khr_fp64',
    'cl_khr_fp16',
    'cl_APPLE_SetMemObjectDestructor',
    'cl_APPLE_ContextLoggingFunctions',
    'cl_khr_icd',
    'cl_khr_il_program',
    'cl_khr_image2D_from_buffer',   # incorrect name
    'cl_khr_image2d_from_buffer',
    'cl_khr_initialize_memory',
    'cl_khr_terminate_context',
    'cl_khr_spir',
    'cl_khr_create_command_queue',
    'cl_nv_device_attribute_query',
    'cl_amd_device_attribute_query',
    'cl_arm_printf',
    'cl_ext_device_fission',
    'cl_ext_migrate_memobject',
    'cl_ext_cxx_for_opencl',
    'cl_qcom_ext_host_ptr',
    'cl_qcom_ext_host_ptr_iocoherent',
    'cl_qcom_ion_host_ptr',
    'cl_qcom_android_native_buffer_host_ptr',
    'cl_img_yuv_image',
    'cl_img_cached_allocations',
    'cl_img_use_gralloc_ptr',
    'cl_img_generate_mipmap',
    'cl_img_mem_properties',
    'cl_khr_subgroups',
    'cl_khr_mipmap_image',
    'cl_khr_priority_hints',
    'cl_khr_throttle_hints',
    'cl_khr_subgroup_named_barrier',
    'cl_khr_extended_versioning',
    'cl_khr_device_uuid',
    'cl_khr_pci_bus_info',
    'cl_khr_suggested_local_work_size',
    'cl_khr_integer_dot_product',
    'cl_khr_external_memory',
    'cl_khr_external_memory_dma_buf',
    'cl_khr_external_memory_dx',
    'cl_khr_external_memory_opaque_fd',
    'cl_khr_external_memory_win32',
    'cl_khr_external_semaphore',
    'cl_khr_external_semaphore_dx_fence',
    'cl_khr_external_semaphore_opaque_fd',
    'cl_khr_external_semaphore_sync_fd',
    'cl_khr_external_semaphore_win32',
    'cl_khr_semaphore',
    'cl_arm_import_memory',
    'cl_arm_shared_virtual_memory',
    'cl_arm_get_core_id',
    'cl_arm_job_slot_selection',
    'cl_arm_scheduling_controls',
    'cl_arm_controlled_kernel_termination',
    'cl_arm_protected_memory_allocation',
    'cl_intel_exec_by_local_thread',
    'cl_intel_device_attribute_query',
    'cl_intel_device_partition_by_names',
    'cl_intel_accelerator',
    'cl_intel_motion_estimation',
    'cl_intel_advanced_motion_estimation',
    'cl_intel_simultaneous_sharing',
    'cl_intel_egl_image_yuv',
    'cl_intel_packed_yuv',
    'cl_intel_required_subgroup_size',
    'cl_intel_driver_diagnostics',
    'cl_intel_planar_yuv',
    'cl_intel_device_side_avc_motion_estimation',
    'cl_intel_unified_shared_memory',
    'cl_intel_mem_alloc_buffer_location',
    'cl_intel_create_buffer_with_properties',
    'cl_intel_program_scope_host_pipe',
    'cl_intel_mem_channel_property',
    'cl_intel_mem_force_host_memory',
    'cl_intel_command_queue_families',
    'cl_intel_queue_no_sync_operations',
    'cl_intel_sharing_format_query',
    'cl_ext_image_requirements_info',
    'cl_ext_image_from_buffer',
    'cl_loader_info',
    'cl_khr_depth_images',
    'cl_ext_float_atomics',
    'cl_intel_create_mem_object_properties',
    'cl_pocl_content_size',
    'cl_ext_image_raw10_raw12',
    # cl_d3d10.h
    'cl_khr_d3d10_sharing',
    'cl_intel_sharing_format_query_d3d10',
    # cl_d3d11.h
    'cl_khr_d3d11_sharing',
    'cl_intel_sharing_format_query_d3d11',
    # cl_dx9_media_sharing.h
    'cl_khr_dx9_media_sharing',
    'cl_intel_dx9_media_sharing',
    'cl_intel_sharing_format_query_dx9',
    # cl_egl.h
    'cl_khr_egl_image',
    'cl_khr_egl_event',
    # cl_gl.h
    'cl_khr_gl_sharing',
    'cl_khr_gl_event',
    'cl_khr_gl_depth_images',
    'cl_khr_gl_msaa_sharing',
    'cl_intel_sharing_format_query_gl',
    # cl_layer.h
    'cl_loader_layers',
    # cl_va_api_media_sharing_intel.h
    'cl_intel_sharing_format_query_va_api',
    'cl_intel_va_api_media_sharing',
    ]

def getExtensionSortKey(item):
    name = item.get('name')
    try:
        index = orderedExtensions.index(name)
    except ValueError:
        if name.startswith('cl_khr'):
            index = 10000
        elif name.startswith('cl_ext'):
            index = 10001
        else:
            index = 10002
    return index, name

# Order the extensions should be emitted in the headers.
# KHR -> EXT -> Vendor Extensions
def getExtensionSortKey_ideal(item):
    name = item.get('name')
    if name.startswith('cl_khr'):
        return 0, name
    if name.startswith('cl_ext'):
        return 1, name
    return 99, name

# Order enums should be emitted in the headers.
# Enums Without Bits -> Ordered Bit Enums
def getEnumSortKey(item):
    name = item.get('name')
    if name in enums:
        if enums[name].Value:
            return -1
        if enums[name].Bitpos:
            return int(enums[name].Bitpos)
    return 99

# Gets C function parameter strings for the specified API params:
def getCParameterStrings(params):
    strings = []
    if len(params) == 0:
        strings.append("void")
    else:
        for param in params:
            paramstr = param.Type + ' ' + param.Name + param.TypeEnd
            strings.append(paramstr)
    return strings

# Gets a bit string for the specified bit position:
def getBitPosString(bitpos):
    ret = '('
    ret += '(cl_bitfield)' if int(bitpos) >= 31 else ''
    ret += '1 << '
    ret += bitpos
    ret += ')'
    return ret

# Extensions that are included in the ICD dispatch table for historical reasons.
# This should not be required for new extensions!
loaderExtensions = {
    'cl_ext_device_fission',
    'cl_khr_d3d10_sharing',
    'cl_khr_d3d11_sharing',
    'cl_khr_dx9_media_sharing',
    'cl_khr_egl_event',
    'cl_khr_egl_image',
    'cl_khr_gl_event',
    'cl_khr_gl_sharing',
    'cl_khr_subgroups'
    }

# Gets the preprocessor guard for the given extension:
def getPrototypeIfGuard(name):
    if name in loaderExtensions:
        guard = '!defined(CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES)'
    else:
        guard = '!defined(CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES)'
    return guard

# Names that have been generated already, since some may be shared by multiple
# extensions:
generatedNames = set()

def isDuplicateName(name):
    if name in generatedNames:
        return True
    generatedNames.add(name)
    return False

%>/*******************************************************************************
 * Copyright (c) 2008-2023 The Khronos Group Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

#ifndef ${guard}
#define ${guard}

/*
** This header is generated from the Khronos OpenCL XML API Registry.
*/

%if includes:
${includes}

%endif
#include <CL/cl.h>

/* CL_NO_PROTOTYPES implies CL_NO_EXTENSION_PROTOTYPES: */
#if defined(CL_NO_PROTOTYPES) && !defined(CL_NO_EXTENSION_PROTOTYPES)
#define CL_NO_EXTENSION_PROTOTYPES
#endif

/* CL_NO_EXTENSION_PROTOTYPES implies
   CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES and
   CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES: */
#if defined(CL_NO_EXTENSION_PROTOTYPES) && ${"\\"}
    !defined(CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES)
#define CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES
#endif
#if defined(CL_NO_EXTENSION_PROTOTYPES) && ${"\\"}
    !defined(CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES)
#define CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES
#endif

#ifdef __cplusplus
extern "C" {
#endif

%for extension in sorted(spec.findall('extensions/extension'), key=getExtensionSortKey):
%  if shouldGenerate(extension.get('name')):
<%
    name = extension.get('name')
%>/***************************************************************
* ${name}
***************************************************************/
%if extension.get('condition'):
#if ${extension.get('condition')}

%endif
#define ${name} 1
#define ${name.upper()}_EXTENSION_NAME ${"\\"}
    "${name}"

%for block in extension.findall('require'):
%  if shouldEmit(block):
%    if block.get('condition'):
#if ${block.get('condition')}
%    endif
%    if block.get('comment'):
/* ${block.get('comment')} */
%    endif
%    for type in block.findall('type'):
%      if isDuplicateName(type.get('name')):
/* type ${type.get('name')} */
%      else:
%        if type.get('name') in typedefs:
${typedefs[type.get('name')].Typedef.ljust(27)} ${type.get('name')};
%        elif type.get('name') in macros:
#define ${type.get('name')}${macros[type.get('name')].Macro}
%        elif type.get('name') in structs:
<%
    struct = structs[type.get('name')]
%>typedef struct _${struct.Name} {
%          for i, member in enumerate(struct.Members):
    ${member.Type} ${member.Name}${member.TypeEnd};
%          endfor
} ${struct.Name};
%        else:
// type ${type.get('name')} not found!
%        endif
%      endif
%    endfor
%    for enum in sorted(block.findall('enum'), key=getEnumSortKey):
%      if isDuplicateName(enum.get('name')):
/* enum ${enum.get('name')} */
%      else:
%        if enum.get('name') in enums:
%          if enums[enum.get('name')].Value:
#define ${enum.get('name').ljust(51)} ${enums[enum.get('name')].Value}
%          elif enums[enum.get('name')].Bitpos:
#define ${enum.get('name').ljust(51)} ${getBitPosString(enums[enum.get('name')].Bitpos)}
%          else:
// enum ${enum.get('name')} is unassigned!
%          endif
%        else:
// enum ${enum.get('name')} not found!
%        endif
%      endif
%    endfor
%    if block.findall('command'):
%      for func in block.findall('command'):
<%
    api = apisigs[func.get('name')]
%>
typedef ${api.RetType} CL_API_CALL
${api.Name}_t(
%        for i, paramStr in enumerate(getCParameterStrings(api.Params)):
%          if i < len(api.Params)-1:
    ${paramStr},
%          else:
    ${paramStr});
%          endif
%        endfor

typedef ${api.Name}_t *
${api.Name}_fn ${api.Suffix};
%      endfor
%      if generate_pfn_typedefs:

/*
** The function pointer typedefs prefixed with "pfn_" are provided for
** compatibility with earlier versions of the headers.  New code is
** encouraged to use the function pointer typedefs that are suffixed with
** "_fn" instead, for consistency.
*/
%        for func in block.findall('command'):
<%
    api = apisigs[func.get('name')]
%>
typedef ${api.Name}_t *
pfn_${api.Name} ${api.Suffix};
%        endfor
%      endif

#if ${getPrototypeIfGuard(name)}
%      for func in block.findall('command'):
<%
    api = apisigs[func.get('name')]
%>
extern CL_API_ENTRY ${api.RetType} CL_API_CALL
${api.Name}(
%        for i, paramStr in enumerate(getCParameterStrings(api.Params)):
%          if i < len(api.Params)-1:
    ${paramStr},
%          else:
    ${paramStr}) ${api.Suffix};
%          endif
%        endfor
%      endfor

#endif /* ${getPrototypeIfGuard(name)} */
%    endif
%    if block.get('condition'):

#endif /* ${block.get('condition')} */
%    endif

%  endif
%endfor
%if extension.get('condition'):
#endif /* ${extension.get('condition')} */

%endif
%  endif
%endfor
#ifdef __cplusplus
}
#endif

#endif /* ${guard} */