xref: /aosp_15_r20/external/OpenCL-Headers/scripts/cl_ext.h.mako (revision 3cc257528f335c5672e46865a03b8ee020e5fe2d)
1<%
2# Extensions to skip by default because they are in dedicated headers:
3skipExtensions = {
4    'cl_khr_d3d10_sharing',
5    'cl_khr_d3d11_sharing',
6    'cl_khr_dx9_media_sharing',
7    'cl_khr_egl_event',
8    'cl_khr_egl_image',
9    'cl_khr_gl_depth_images',
10    'cl_khr_gl_event',
11    'cl_khr_gl_msaa_sharing',
12    'cl_khr_gl_sharing',
13    'cl_loader_layers',
14    'cl_intel_dx9_media_sharing',
15    'cl_intel_va_api_media_sharing',
16    'cl_intel_sharing_format_query_d3d10',
17    'cl_intel_sharing_format_query_d3d11',
18    'cl_intel_sharing_format_query_dx9',
19    'cl_intel_sharing_format_query_gl',
20    'cl_intel_sharing_format_query_va_api',
21    }
22
23# Extensions to include in this header:
24def shouldGenerate(extension):
25    if extension in genExtensions:
26        return True
27    elif not genExtensions and not extension in skipExtensions:
28        return True
29    return False
30
31# XML blocks to include in the headers:
32def shouldEmit(block):
33    for type in block.findall('type'):
34        if type.get('name') in typedefs:
35            return True
36        elif type.get('name') in macros:
37            return True
38        elif type.get('name') in structs:
39            return True
40    for enum in block.findall('enum'):
41        #if enum.get('name') in enums:
42            return True
43    for func in block.findall('command'):
44        return True
45    return False
46
47# Initially, keep the same extension order as the original headers:
48orderedExtensions = [
49    # cl_ext.h:
50    'cl_khr_command_buffer',
51    'cl_khr_command_buffer_multi_device',
52    'cl_khr_command_buffer_mutable_dispatch',
53    'cl_khr_fp64',
54    'cl_khr_fp16',
55    'cl_APPLE_SetMemObjectDestructor',
56    'cl_APPLE_ContextLoggingFunctions',
57    'cl_khr_icd',
58    'cl_khr_il_program',
59    'cl_khr_image2D_from_buffer',   # incorrect name
60    'cl_khr_image2d_from_buffer',
61    'cl_khr_initialize_memory',
62    'cl_khr_terminate_context',
63    'cl_khr_spir',
64    'cl_khr_create_command_queue',
65    'cl_nv_device_attribute_query',
66    'cl_amd_device_attribute_query',
67    'cl_arm_printf',
68    'cl_ext_device_fission',
69    'cl_ext_migrate_memobject',
70    'cl_ext_cxx_for_opencl',
71    'cl_qcom_ext_host_ptr',
72    'cl_qcom_ext_host_ptr_iocoherent',
73    'cl_qcom_ion_host_ptr',
74    'cl_qcom_android_native_buffer_host_ptr',
75    'cl_img_yuv_image',
76    'cl_img_cached_allocations',
77    'cl_img_use_gralloc_ptr',
78    'cl_img_generate_mipmap',
79    'cl_img_mem_properties',
80    'cl_khr_subgroups',
81    'cl_khr_mipmap_image',
82    'cl_khr_priority_hints',
83    'cl_khr_throttle_hints',
84    'cl_khr_subgroup_named_barrier',
85    'cl_khr_extended_versioning',
86    'cl_khr_device_uuid',
87    'cl_khr_pci_bus_info',
88    'cl_khr_suggested_local_work_size',
89    'cl_khr_integer_dot_product',
90    'cl_khr_external_memory',
91    'cl_khr_external_memory_dma_buf',
92    'cl_khr_external_memory_dx',
93    'cl_khr_external_memory_opaque_fd',
94    'cl_khr_external_memory_win32',
95    'cl_khr_external_semaphore',
96    'cl_khr_external_semaphore_dx_fence',
97    'cl_khr_external_semaphore_opaque_fd',
98    'cl_khr_external_semaphore_sync_fd',
99    'cl_khr_external_semaphore_win32',
100    'cl_khr_semaphore',
101    'cl_arm_import_memory',
102    'cl_arm_shared_virtual_memory',
103    'cl_arm_get_core_id',
104    'cl_arm_job_slot_selection',
105    'cl_arm_scheduling_controls',
106    'cl_arm_controlled_kernel_termination',
107    'cl_arm_protected_memory_allocation',
108    'cl_intel_exec_by_local_thread',
109    'cl_intel_device_attribute_query',
110    'cl_intel_device_partition_by_names',
111    'cl_intel_accelerator',
112    'cl_intel_motion_estimation',
113    'cl_intel_advanced_motion_estimation',
114    'cl_intel_simultaneous_sharing',
115    'cl_intel_egl_image_yuv',
116    'cl_intel_packed_yuv',
117    'cl_intel_required_subgroup_size',
118    'cl_intel_driver_diagnostics',
119    'cl_intel_planar_yuv',
120    'cl_intel_device_side_avc_motion_estimation',
121    'cl_intel_unified_shared_memory',
122    'cl_intel_mem_alloc_buffer_location',
123    'cl_intel_create_buffer_with_properties',
124    'cl_intel_program_scope_host_pipe',
125    'cl_intel_mem_channel_property',
126    'cl_intel_mem_force_host_memory',
127    'cl_intel_command_queue_families',
128    'cl_intel_queue_no_sync_operations',
129    'cl_intel_sharing_format_query',
130    'cl_ext_image_requirements_info',
131    'cl_ext_image_from_buffer',
132    'cl_loader_info',
133    'cl_khr_depth_images',
134    'cl_ext_float_atomics',
135    'cl_intel_create_mem_object_properties',
136    'cl_pocl_content_size',
137    'cl_ext_image_raw10_raw12',
138    # cl_d3d10.h
139    'cl_khr_d3d10_sharing',
140    'cl_intel_sharing_format_query_d3d10',
141    # cl_d3d11.h
142    'cl_khr_d3d11_sharing',
143    'cl_intel_sharing_format_query_d3d11',
144    # cl_dx9_media_sharing.h
145    'cl_khr_dx9_media_sharing',
146    'cl_intel_dx9_media_sharing',
147    'cl_intel_sharing_format_query_dx9',
148    # cl_egl.h
149    'cl_khr_egl_image',
150    'cl_khr_egl_event',
151    # cl_gl.h
152    'cl_khr_gl_sharing',
153    'cl_khr_gl_event',
154    'cl_khr_gl_depth_images',
155    'cl_khr_gl_msaa_sharing',
156    'cl_intel_sharing_format_query_gl',
157    # cl_layer.h
158    'cl_loader_layers',
159    # cl_va_api_media_sharing_intel.h
160    'cl_intel_sharing_format_query_va_api',
161    'cl_intel_va_api_media_sharing',
162    ]
163
164def getExtensionSortKey(item):
165    name = item.get('name')
166    try:
167        index = orderedExtensions.index(name)
168    except ValueError:
169        if name.startswith('cl_khr'):
170            index = 10000
171        elif name.startswith('cl_ext'):
172            index = 10001
173        else:
174            index = 10002
175    return index, name
176
177# Order the extensions should be emitted in the headers.
178# KHR -> EXT -> Vendor Extensions
179def getExtensionSortKey_ideal(item):
180    name = item.get('name')
181    if name.startswith('cl_khr'):
182        return 0, name
183    if name.startswith('cl_ext'):
184        return 1, name
185    return 99, name
186
187# Order enums should be emitted in the headers.
188# Enums Without Bits -> Ordered Bit Enums
189def getEnumSortKey(item):
190    name = item.get('name')
191    if name in enums:
192        if enums[name].Value:
193            return -1
194        if enums[name].Bitpos:
195            return int(enums[name].Bitpos)
196    return 99
197
198# Gets C function parameter strings for the specified API params:
199def getCParameterStrings(params):
200    strings = []
201    if len(params) == 0:
202        strings.append("void")
203    else:
204        for param in params:
205            paramstr = param.Type + ' ' + param.Name + param.TypeEnd
206            strings.append(paramstr)
207    return strings
208
209# Gets a bit string for the specified bit position:
210def getBitPosString(bitpos):
211    ret = '('
212    ret += '(cl_bitfield)' if int(bitpos) >= 31 else ''
213    ret += '1 << '
214    ret += bitpos
215    ret += ')'
216    return ret
217
218# Extensions that are included in the ICD dispatch table for historical reasons.
219# This should not be required for new extensions!
220loaderExtensions = {
221    'cl_ext_device_fission',
222    'cl_khr_d3d10_sharing',
223    'cl_khr_d3d11_sharing',
224    'cl_khr_dx9_media_sharing',
225    'cl_khr_egl_event',
226    'cl_khr_egl_image',
227    'cl_khr_gl_event',
228    'cl_khr_gl_sharing',
229    'cl_khr_subgroups'
230    }
231
232# Gets the preprocessor guard for the given extension:
233def getPrototypeIfGuard(name):
234    if name in loaderExtensions:
235        guard = '!defined(CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES)'
236    else:
237        guard = '!defined(CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES)'
238    return guard
239
240# Names that have been generated already, since some may be shared by multiple
241# extensions:
242generatedNames = set()
243
244def isDuplicateName(name):
245    if name in generatedNames:
246        return True
247    generatedNames.add(name)
248    return False
249
250%>/*******************************************************************************
251 * Copyright (c) 2008-2023 The Khronos Group Inc.
252 *
253 * Licensed under the Apache License, Version 2.0 (the "License");
254 * you may not use this file except in compliance with the License.
255 * You may obtain a copy of the License at
256 *
257 *    http://www.apache.org/licenses/LICENSE-2.0
258 *
259 * Unless required by applicable law or agreed to in writing, software
260 * distributed under the License is distributed on an "AS IS" BASIS,
261 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
262 * See the License for the specific language governing permissions and
263 * limitations under the License.
264 ******************************************************************************/
265
266#ifndef ${guard}
267#define ${guard}
268
269/*
270** This header is generated from the Khronos OpenCL XML API Registry.
271*/
272
273%if includes:
274${includes}
275
276%endif
277#include <CL/cl.h>
278
279/* CL_NO_PROTOTYPES implies CL_NO_EXTENSION_PROTOTYPES: */
280#if defined(CL_NO_PROTOTYPES) && !defined(CL_NO_EXTENSION_PROTOTYPES)
281#define CL_NO_EXTENSION_PROTOTYPES
282#endif
283
284/* CL_NO_EXTENSION_PROTOTYPES implies
285   CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES and
286   CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES: */
287#if defined(CL_NO_EXTENSION_PROTOTYPES) && ${"\\"}
288    !defined(CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES)
289#define CL_NO_ICD_DISPATCH_EXTENSION_PROTOTYPES
290#endif
291#if defined(CL_NO_EXTENSION_PROTOTYPES) && ${"\\"}
292    !defined(CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES)
293#define CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES
294#endif
295
296#ifdef __cplusplus
297extern "C" {
298#endif
299
300%for extension in sorted(spec.findall('extensions/extension'), key=getExtensionSortKey):
301%  if shouldGenerate(extension.get('name')):
302<%
303    name = extension.get('name')
304%>/***************************************************************
305* ${name}
306***************************************************************/
307%if extension.get('condition'):
308#if ${extension.get('condition')}
309
310%endif
311#define ${name} 1
312#define ${name.upper()}_EXTENSION_NAME ${"\\"}
313    "${name}"
314
315%for block in extension.findall('require'):
316%  if shouldEmit(block):
317%    if block.get('condition'):
318#if ${block.get('condition')}
319%    endif
320%    if block.get('comment'):
321/* ${block.get('comment')} */
322%    endif
323%    for type in block.findall('type'):
324%      if isDuplicateName(type.get('name')):
325/* type ${type.get('name')} */
326%      else:
327%        if type.get('name') in typedefs:
328${typedefs[type.get('name')].Typedef.ljust(27)} ${type.get('name')};
329%        elif type.get('name') in macros:
330#define ${type.get('name')}${macros[type.get('name')].Macro}
331%        elif type.get('name') in structs:
332<%
333    struct = structs[type.get('name')]
334%>typedef struct _${struct.Name} {
335%          for i, member in enumerate(struct.Members):
336    ${member.Type} ${member.Name}${member.TypeEnd};
337%          endfor
338} ${struct.Name};
339%        else:
340// type ${type.get('name')} not found!
341%        endif
342%      endif
343%    endfor
344%    for enum in sorted(block.findall('enum'), key=getEnumSortKey):
345%      if isDuplicateName(enum.get('name')):
346/* enum ${enum.get('name')} */
347%      else:
348%        if enum.get('name') in enums:
349%          if enums[enum.get('name')].Value:
350#define ${enum.get('name').ljust(51)} ${enums[enum.get('name')].Value}
351%          elif enums[enum.get('name')].Bitpos:
352#define ${enum.get('name').ljust(51)} ${getBitPosString(enums[enum.get('name')].Bitpos)}
353%          else:
354// enum ${enum.get('name')} is unassigned!
355%          endif
356%        else:
357// enum ${enum.get('name')} not found!
358%        endif
359%      endif
360%    endfor
361%    if block.findall('command'):
362%      for func in block.findall('command'):
363<%
364    api = apisigs[func.get('name')]
365%>
366typedef ${api.RetType} CL_API_CALL
367${api.Name}_t(
368%        for i, paramStr in enumerate(getCParameterStrings(api.Params)):
369%          if i < len(api.Params)-1:
370    ${paramStr},
371%          else:
372    ${paramStr});
373%          endif
374%        endfor
375
376typedef ${api.Name}_t *
377${api.Name}_fn ${api.Suffix};
378%      endfor
379%      if generate_pfn_typedefs:
380
381/*
382** The function pointer typedefs prefixed with "pfn_" are provided for
383** compatibility with earlier versions of the headers.  New code is
384** encouraged to use the function pointer typedefs that are suffixed with
385** "_fn" instead, for consistency.
386*/
387%        for func in block.findall('command'):
388<%
389    api = apisigs[func.get('name')]
390%>
391typedef ${api.Name}_t *
392pfn_${api.Name} ${api.Suffix};
393%        endfor
394%      endif
395
396#if ${getPrototypeIfGuard(name)}
397%      for func in block.findall('command'):
398<%
399    api = apisigs[func.get('name')]
400%>
401extern CL_API_ENTRY ${api.RetType} CL_API_CALL
402${api.Name}(
403%        for i, paramStr in enumerate(getCParameterStrings(api.Params)):
404%          if i < len(api.Params)-1:
405    ${paramStr},
406%          else:
407    ${paramStr}) ${api.Suffix};
408%          endif
409%        endfor
410%      endfor
411
412#endif /* ${getPrototypeIfGuard(name)} */
413%    endif
414%    if block.get('condition'):
415
416#endif /* ${block.get('condition')} */
417%    endif
418
419%  endif
420%endfor
421%if extension.get('condition'):
422#endif /* ${extension.get('condition')} */
423
424%endif
425%  endif
426%endfor
427#ifdef __cplusplus
428}
429#endif
430
431#endif /* ${guard} */
432