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 mako
.template
import Template
35 SUPPORTED_EXTENSIONS
= [
36 'VK_AMD_draw_indirect_count',
37 'VK_NV_dedicated_allocation',
38 'VK_KHR_descriptor_update_template',
39 'VK_KHR_get_physical_device_properties2',
40 'VK_KHR_incremental_present',
41 'VK_KHR_maintenance1',
42 'VK_KHR_push_descriptor',
43 'VK_KHR_sampler_mirror_clamp_to_edge',
44 'VK_KHR_shader_draw_parameters',
47 'VK_KHR_wayland_surface',
49 'VK_KHR_xlib_surface',
50 'VK_KHR_get_memory_requirements2',
51 'VK_KHR_dedicated_allocation',
52 'VK_KHR_external_memory_capabilities',
53 'VK_KHR_external_memory',
54 'VK_KHR_external_memory_fd',
55 'VK_KHR_storage_buffer_storage_class',
56 'VK_KHR_variable_pointers',
57 'VK_KHR_external_semaphore_capabilities',
58 'VK_KHR_external_semaphore',
59 'VK_KHR_external_semaphore_fd',
60 'VK_KHR_bind_memory2',
61 'VK_KHR_maintenance2',
64 # We generate a static hash table for entry point lookup
65 # (vkGetProcAddress). We use a linear congruential generator for our hash
66 # function and a power-of-two size table. The prime numbers are determined
69 TEMPLATE_H
= Template(textwrap
.dedent("""\
70 /* This file generated from ${filename}, don't edit directly. */
72 struct radv_dispatch_table {
74 void *entrypoints[${len(entrypoints)}];
76 % for _, name, _, _, _, guard in entrypoints:
77 % if guard is not None:
79 PFN_vk${name} ${name};
84 PFN_vk${name} ${name};
91 % for type_, name, args, num, h, guard in entrypoints:
92 % if guard is not None:
95 ${type_} radv_${name}(${args});
96 % if guard is not None:
100 """), output_encoding
='utf-8')
102 TEMPLATE_C
= Template(textwrap
.dedent(u
"""\
104 * Copyright © 2015 Intel Corporation
106 * Permission is hereby granted, free of charge, to any person obtaining a
107 * copy of this software and associated documentation files (the "Software"),
108 * to deal in the Software without restriction, including without limitation
109 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
110 * and/or sell copies of the Software, and to permit persons to whom the
111 * Software is furnished to do so, subject to the following conditions:
113 * The above copyright notice and this permission notice (including the next
114 * paragraph) shall be included in all copies or substantial portions of the
117 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
118 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
119 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
120 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
121 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
122 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
126 /* This file generated from ${filename}, don't edit directly. */
128 #include "radv_private.h"
130 struct radv_entrypoint {
135 /* We use a big string constant to avoid lots of reloctions from the entry
136 * point table to lots of little strings. The entries in the entry point table
137 * store the index into this big string.
140 static const char strings[] =
141 % for _, name, _, _, _, _ in entrypoints:
146 static const struct radv_entrypoint entrypoints[] = {
147 % for _, _, _, num, h, _ in entrypoints:
148 { ${offsets[num]}, ${'{:0=#8x}'.format(h)} },
152 /* Weak aliases for all potential implementations. These will resolve to
153 * NULL if they're not defined, which lets the resolve_entrypoint() function
154 * either pick the correct entry point.
157 % for layer in ['radv']:
158 % for type_, name, args, _, _, guard in entrypoints:
159 % if guard is not None:
162 ${type_} ${layer}_${name}(${args}) __attribute__ ((weak));
163 % if guard is not None:
168 const struct radv_dispatch_table ${layer}_layer = {
169 % for _, name, args, _, _, guard in entrypoints:
170 % if guard is not None:
173 .${name} = ${layer}_${name},
174 % if guard is not None:
181 static void * __attribute__ ((noinline))
182 radv_resolve_entrypoint(uint32_t index)
184 return radv_layer.entrypoints[index];
188 * size ${hash_size} entries
189 * collisions entries:
190 % for i in xrange(10):
191 * ${i}${'+' if i == 9 else ''} ${collisions[i]}
195 #define none ${'{:#x}'.format(none)}
196 static const uint16_t map[] = {
197 % for i in xrange(0, hash_size, 8):
198 % for j in xrange(i, i + 8):
199 ## This is 6 because the 0x is counted in the length
200 % if mapping[j] & 0xffff == 0xffff:
203 ${'{:0=#6x}'.format(mapping[j] & 0xffff)},
210 radv_lookup_entrypoint(const char *name)
212 static const uint32_t prime_factor = ${prime_factor};
213 static const uint32_t prime_step = ${prime_step};
214 const struct radv_entrypoint *e;
219 for (p = name; *p; p++)
220 hash = hash * prime_factor + *p;
224 i = map[h & ${hash_mask}];
229 } while (e->hash != hash);
231 if (strcmp(name, strings + e->name) != 0)
234 return radv_resolve_entrypoint(i);
235 }"""), output_encoding
='utf-8')
240 HASH_MASK
= HASH_SIZE
- 1
242 PRIME_FACTOR
= 5024183
247 """Calculate the same hash value that Mesa will calculate in C."""
248 return functools
.reduce(
249 lambda h
, c
: (h
* PRIME_FACTOR
+ ord(c
)) & U32_MASK
, name
, 0)
252 def get_entrypoints(doc
, entrypoints_to_defines
):
253 """Extract the entry points from the registry."""
256 enabled_commands
= set()
257 for feature
in doc
.findall('./feature'):
258 assert feature
.attrib
['api'] == 'vulkan'
259 if float(feature
.attrib
['number']) > MAX_API_VERSION
:
262 for command
in feature
.findall('./require/command'):
263 enabled_commands
.add(command
.attrib
['name'])
265 for extension
in doc
.findall('.extensions/extension'):
266 if extension
.attrib
['name'] not in SUPPORTED_EXTENSIONS
:
269 assert extension
.attrib
['supported'] == 'vulkan'
270 for command
in extension
.findall('./require/command'):
271 enabled_commands
.add(command
.attrib
['name'])
274 for command
in doc
.findall('./commands/command'):
275 type = command
.find('./proto/type').text
276 fullname
= command
.find('./proto/name').text
278 if fullname
not in enabled_commands
:
281 shortname
= fullname
[2:]
282 params
= (''.join(p
.itertext()) for p
in command
.findall('./param'))
283 params
= ', '.join(params
)
284 guard
= entrypoints_to_defines
.get(fullname
)
285 entrypoints
.append((type, shortname
, params
, index
, cal_hash(fullname
), guard
))
291 def get_entrypoints_defines(doc
):
292 """Maps entry points to extension defines."""
293 entrypoints_to_defines
= {}
295 for extension
in doc
.findall('./extensions/extension[@protect]'):
296 define
= extension
.attrib
['protect']
298 for entrypoint
in extension
.findall('./require/command'):
299 fullname
= entrypoint
.attrib
['name']
300 entrypoints_to_defines
[fullname
] = define
302 return entrypoints_to_defines
305 def gen_code(entrypoints
):
306 """Generate the C code."""
309 for _
, name
, _
, _
, _
, _
in entrypoints
:
311 i
+= 2 + len(name
) + 1
313 mapping
= [NONE
] * HASH_SIZE
314 collisions
= [0] * 10
315 for _
, name
, _
, num
, h
, _
in entrypoints
:
317 while mapping
[h
& HASH_MASK
] != NONE
:
323 collisions
[level
] += 1
324 mapping
[h
& HASH_MASK
] = num
326 return TEMPLATE_C
.render(entrypoints
=entrypoints
,
328 collisions
=collisions
,
331 prime_step
=PRIME_STEP
,
332 prime_factor
=PRIME_FACTOR
,
335 filename
=os
.path
.basename(__file__
))
339 parser
= argparse
.ArgumentParser()
340 parser
.add_argument('--outdir', help='Where to write the files.',
342 parser
.add_argument('--xml', help='Vulkan API XML file.', required
=True)
343 args
= parser
.parse_args()
345 doc
= et
.parse(args
.xml
)
346 entrypoints
= get_entrypoints(doc
, get_entrypoints_defines(doc
))
348 with
open(os
.path
.join(args
.outdir
, 'radv_entrypoints.h'), 'wb') as f
:
349 f
.write(TEMPLATE_H
.render(entrypoints
=entrypoints
,
350 filename
=os
.path
.basename(__file__
)))
351 with
open(os
.path
.join(args
.outdir
, 'radv_entrypoints.c'), 'wb') as f
:
352 f
.write(gen_code(entrypoints
))
355 if __name__
== '__main__':