3 # Copyright © 2015, 2017 Intel Corporation
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 import xml
.etree
.cElementTree
as et
31 from collections
import OrderedDict
, namedtuple
32 from mako
.template
import Template
34 from radv_extensions
import *
36 # We generate a static hash table for entry point lookup
37 # (vkGetProcAddress). We use a linear congruential generator for our hash
38 # function and a power-of-two size table. The prime numbers are determined
41 # We currently don't use layers in radv, but keeping the ability for anv
42 # anyways, so we can use it for device groups.
47 TEMPLATE_H
= Template("""\
48 /* This file generated from ${filename}, don't edit directly. */
50 struct radv_dispatch_table {
52 void *entrypoints[${len(entrypoints)}];
54 % for e in entrypoints:
55 % if e.guard is not None:
57 PFN_${e.name} ${e.name};
62 PFN_${e.name} ${e.name};
69 % for e in entrypoints:
73 % if e.guard is not None:
76 % for layer in LAYERS:
77 ${e.return_type} ${e.prefixed_name(layer)}(${e.decl_params()});
79 % if e.guard is not None:
83 """, output_encoding
='utf-8')
85 TEMPLATE_C
= Template(u
"""\
87 * Copyright © 2015 Intel Corporation
89 * Permission is hereby granted, free of charge, to any person obtaining a
90 * copy of this software and associated documentation files (the "Software"),
91 * to deal in the Software without restriction, including without limitation
92 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
93 * and/or sell copies of the Software, and to permit persons to whom the
94 * Software is furnished to do so, subject to the following conditions:
96 * The above copyright notice and this permission notice (including the next
97 * paragraph) shall be included in all copies or substantial portions of the
100 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
101 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
102 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
103 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
104 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
105 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
109 /* This file generated from ${filename}, don't edit directly. */
111 #include "radv_private.h"
113 struct string_map_entry {
119 /* We use a big string constant to avoid lots of relocations from the entry
120 * point table to lots of little strings. The entries in the entry point table
121 * store the index into this big string.
124 static const char strings[] =
125 % for s in strmap.sorted_strings:
130 static const struct string_map_entry string_map_entries[] = {
131 % for s in strmap.sorted_strings:
132 { ${s.offset}, ${'{:0=#8x}'.format(s.hash)}, ${s.num} }, /* ${s.string} */
137 * size ${len(strmap.sorted_strings)} entries
138 * collisions entries:
139 % for i in range(10):
140 * ${i}${'+' if i == 9 else ' '} ${strmap.collisions[i]}
145 static const uint16_t string_map[${strmap.hash_size}] = {
146 % for e in strmap.mapping:
147 ${ '{:0=#6x}'.format(e) if e >= 0 else 'none' },
151 /* Weak aliases for all potential implementations. These will resolve to
152 * NULL if they're not defined, which lets the resolve_entrypoint() function
153 * either pick the correct entry point.
156 % for layer in LAYERS:
157 % for e in entrypoints:
161 % if e.guard is not None:
164 ${e.return_type} ${e.prefixed_name(layer)}(${e.decl_params()}) __attribute__ ((weak));
165 % if e.guard is not None:
170 const struct radv_dispatch_table ${layer}_layer = {
171 % for e in entrypoints:
172 % if e.guard is not None:
175 .${e.name} = ${e.prefixed_name(layer)},
176 % if e.guard is not None:
183 static void * __attribute__ ((noinline))
184 radv_resolve_entrypoint(uint32_t index)
186 return radv_layer.entrypoints[index];
189 /** Return true if the core version or extension in which the given entrypoint
190 * is defined is enabled.
192 * If instance is NULL, we only allow the 3 commands explicitly allowed by the vk
195 * If device is NULL, all device extensions are considered enabled.
198 radv_entrypoint_is_enabled(int index, uint32_t core_version,
199 const struct radv_instance_extension_table *instance,
200 const struct radv_device_extension_table *device)
203 % for e in entrypoints:
205 % if not e.device_command:
206 if (device) return false;
208 % if e.name == 'vkCreateInstance' or e.name == 'vkEnumerateInstanceExtensionProperties' or e.name == 'vkEnumerateInstanceLayerProperties' or e.name == 'vkEnumerateInstanceVersion':
210 % elif e.core_version:
211 return instance && ${e.core_version.c_vk_version()} <= core_version;
213 % for ext in e.extensions:
214 % if ext.type == 'instance':
215 if (instance && instance->${ext.name[3:]}) return true;
217 if (instance && (!device || device->${ext.name[3:]})) return true;
231 radv_entrypoint_is_enabled_physical_device(int index, uint32_t core_version,
232 const struct radv_instance_extension_table *instance)
235 % for e in entrypoints:
236 %if e.physical_device_command:
239 return instance && ${e.core_version.c_vk_version()} <= core_version;
241 % for ext in e.extensions:
242 % if ext.type == 'instance':
243 if (instance && instance->${ext.name[3:]}) return true;
260 radv_lookup_entrypoint(const char *name)
262 static const uint32_t prime_factor = ${strmap.prime_factor};
263 static const uint32_t prime_step = ${strmap.prime_step};
264 const struct string_map_entry *e;
270 for (p = name; *p; p++)
271 hash = hash * prime_factor + *p;
275 i = string_map[h & ${strmap.hash_mask}];
278 e = &string_map_entries[i];
279 if (e->hash == hash && strcmp(name, strings + e->name) == 0)
288 radv_lookup_entrypoint_unchecked(const char *name)
290 int index = radv_lookup_entrypoint(name);
293 return radv_resolve_entrypoint(index);
297 radv_lookup_entrypoint_checked(const char *name,
298 uint32_t core_version,
299 const struct radv_instance_extension_table *instance,
300 const struct radv_device_extension_table *device)
302 int index = radv_lookup_entrypoint(name);
303 if (index < 0 || !radv_entrypoint_is_enabled(index, core_version, instance, device))
305 return radv_resolve_entrypoint(index);
309 radv_lookup_physical_device_entrypoint_checked(const char *name,
310 uint32_t core_version,
311 const struct radv_instance_extension_table *instance)
313 int index = radv_lookup_entrypoint(name);
314 if (index < 0 || !radv_entrypoint_is_enabled_physical_device(index, core_version, instance))
316 return radv_resolve_entrypoint(index);
319 """, output_encoding
='utf-8')
323 PRIME_FACTOR
= 5024183
326 def round_to_pow2(x
):
327 return 2**int(math
.ceil(math
.log(x
, 2)))
329 class StringIntMapEntry(object):
330 def __init__(self
, string
, num
):
334 # Calculate the same hash value that we will calculate in C.
337 h
= ((h
* PRIME_FACTOR
) + ord(c
)) & U32_MASK
342 class StringIntMap(object):
345 self
.strings
= dict()
347 def add_string(self
, string
, num
):
348 assert not self
.baked
349 assert string
not in self
.strings
350 assert num
>= 0 and num
< 2**31
351 self
.strings
[string
] = StringIntMapEntry(string
, num
)
354 self
.sorted_strings
= \
355 sorted(self
.strings
.values(), key
=lambda x
: x
.string
)
357 for entry
in self
.sorted_strings
:
358 entry
.offset
= offset
359 offset
+= len(entry
.string
) + 1
361 # Save off some values that we'll need in C
362 self
.hash_size
= round_to_pow2(len(self
.strings
) * 1.25)
363 self
.hash_mask
= self
.hash_size
- 1
364 self
.prime_factor
= PRIME_FACTOR
365 self
.prime_step
= PRIME_STEP
367 self
.mapping
= [-1] * self
.hash_size
368 self
.collisions
= [0] * 10
369 for idx
, s
in enumerate(self
.sorted_strings
):
372 while self
.mapping
[h
& self
.hash_mask
] >= 0:
375 self
.collisions
[min(level
, 9)] += 1
376 self
.mapping
[h
& self
.hash_mask
] = idx
378 EntrypointParam
= namedtuple('EntrypointParam', 'type name decl')
380 class EntrypointBase(object):
381 def __init__(self
, name
):
387 # Extensions which require this entrypoint
388 self
.core_version
= None
391 class Entrypoint(EntrypointBase
):
392 def __init__(self
, name
, return_type
, params
, guard
= None):
393 super(Entrypoint
, self
).__init
__(name
)
394 self
.return_type
= return_type
397 self
.device_command
= len(params
) > 0 and (params
[0].type == 'VkDevice' or params
[0].type == 'VkQueue' or params
[0].type == 'VkCommandBuffer')
398 self
.physical_device_command
= len(params
) > 0 and params
[0].type == 'VkPhysicalDevice'
400 def prefixed_name(self
, prefix
):
401 assert self
.name
.startswith('vk')
402 return prefix
+ '_' + self
.name
[2:]
404 def decl_params(self
):
405 return ', '.join(p
.decl
for p
in self
.params
)
407 def call_params(self
):
408 return ', '.join(p
.name
for p
in self
.params
)
410 class EntrypointAlias(EntrypointBase
):
411 def __init__(self
, name
, entrypoint
):
412 super(EntrypointAlias
, self
).__init
__(name
)
413 self
.alias
= entrypoint
414 self
.device_command
= entrypoint
.device_command
415 self
.physical_device_command
= entrypoint
.physical_device_command
417 def prefixed_name(self
, prefix
):
418 return self
.alias
.prefixed_name(prefix
)
420 def get_entrypoints(doc
, entrypoints_to_defines
, start_index
):
421 """Extract the entry points from the registry."""
422 entrypoints
= OrderedDict()
424 for command
in doc
.findall('./commands/command'):
425 if 'alias' in command
.attrib
:
426 alias
= command
.attrib
['name']
427 target
= command
.attrib
['alias']
428 entrypoints
[alias
] = EntrypointAlias(alias
, entrypoints
[target
])
430 name
= command
.find('./proto/name').text
431 ret_type
= command
.find('./proto/type').text
432 params
= [EntrypointParam(
433 type = p
.find('./type').text
,
434 name
= p
.find('./name').text
,
435 decl
= ''.join(p
.itertext())
436 ) for p
in command
.findall('./param')]
437 guard
= entrypoints_to_defines
.get(name
)
438 # They really need to be unique
439 assert name
not in entrypoints
440 entrypoints
[name
] = Entrypoint(name
, ret_type
, params
, guard
)
442 for feature
in doc
.findall('./feature'):
443 assert feature
.attrib
['api'] == 'vulkan'
444 version
= VkVersion(feature
.attrib
['number'])
445 if version
> MAX_API_VERSION
:
448 for command
in feature
.findall('./require/command'):
449 e
= entrypoints
[command
.attrib
['name']]
451 assert e
.core_version
is None
452 e
.core_version
= version
454 supported_exts
= dict((ext
.name
, ext
) for ext
in EXTENSIONS
)
455 for extension
in doc
.findall('.extensions/extension'):
456 ext_name
= extension
.attrib
['name']
457 if ext_name
not in supported_exts
:
460 ext
= supported_exts
[ext_name
]
461 ext
.type = extension
.attrib
['type']
463 for command
in extension
.findall('./require/command'):
464 e
= entrypoints
[command
.attrib
['name']]
466 assert e
.core_version
is None
467 e
.extensions
.append(ext
)
469 # if the base command is not supported by the driver yet, don't alias aliases
470 for e
in entrypoints
.values():
471 if e
.alias
and not e
.alias
.enabled
:
472 e_clone
= copy
.deepcopy(e
.alias
)
473 e_clone
.enabled
= True
474 e_clone
.name
= e
.name
475 entrypoints
[e
.name
] = e_clone
477 return [e
for e
in entrypoints
.values() if e
.enabled
]
480 def get_entrypoints_defines(doc
):
481 """Maps entry points to extension defines."""
482 entrypoints_to_defines
= {}
484 for extension
in doc
.findall('./extensions/extension[@protect]'):
485 define
= extension
.attrib
['protect']
487 for entrypoint
in extension
.findall('./require/command'):
488 fullname
= entrypoint
.attrib
['name']
489 entrypoints_to_defines
[fullname
] = define
492 for platform
in doc
.findall('./platforms/platform'):
493 name
= platform
.attrib
['name']
494 define
= platform
.attrib
['protect']
495 platform_define
[name
] = define
497 for extension
in doc
.findall('./extensions/extension[@platform]'):
498 platform
= extension
.attrib
['platform']
499 define
= platform_define
[platform
]
501 for entrypoint
in extension
.findall('./require/command'):
502 fullname
= entrypoint
.attrib
['name']
503 entrypoints_to_defines
[fullname
] = define
505 return entrypoints_to_defines
508 def gen_code(entrypoints
):
509 """Generate the C code."""
510 strmap
= StringIntMap()
511 for e
in entrypoints
:
512 strmap
.add_string(e
.name
, e
.num
)
515 return TEMPLATE_C
.render(entrypoints
=entrypoints
,
518 filename
=os
.path
.basename(__file__
))
522 parser
= argparse
.ArgumentParser()
523 parser
.add_argument('--outdir', help='Where to write the files.',
525 parser
.add_argument('--xml',
526 help='Vulkan API XML file.',
530 args
= parser
.parse_args()
534 for filename
in args
.xml_files
:
535 doc
= et
.parse(filename
)
536 entrypoints
+= get_entrypoints(doc
, get_entrypoints_defines(doc
),
537 start_index
=len(entrypoints
))
539 for num
, e
in enumerate(entrypoints
):
542 # For outputting entrypoints.h we generate a radv_EntryPoint() prototype
544 with
open(os
.path
.join(args
.outdir
, 'radv_entrypoints.h'), 'wb') as f
:
545 f
.write(TEMPLATE_H
.render(entrypoints
=entrypoints
,
547 filename
=os
.path
.basename(__file__
)))
548 with
open(os
.path
.join(args
.outdir
, 'radv_entrypoints.c'), 'wb') as f
:
549 f
.write(gen_code(entrypoints
))
552 if __name__
== '__main__':