21738f4da9027bb7266706c06c6710f7bec87516
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',
63 # We generate a static hash table for entry point lookup
64 # (vkGetProcAddress). We use a linear congruential generator for our hash
65 # function and a power-of-two size table. The prime numbers are determined
68 TEMPLATE_H
= Template(textwrap
.dedent("""\
69 /* This file generated from ${filename}, don't edit directly. */
71 struct radv_dispatch_table {
73 void *entrypoints[${len(entrypoints)}];
75 % for _, name, _, _, _, guard in entrypoints:
76 % if guard is not None:
78 PFN_vk${name} ${name};
83 PFN_vk${name} ${name};
90 % for type_, name, args, num, h, guard in entrypoints:
91 % if guard is not None:
94 ${type_} radv_${name}(${args});
95 % if guard is not None:
99 """), output_encoding
='utf-8')
101 TEMPLATE_C
= Template(textwrap
.dedent(u
"""\
103 * Copyright © 2015 Intel Corporation
105 * Permission is hereby granted, free of charge, to any person obtaining a
106 * copy of this software and associated documentation files (the "Software"),
107 * to deal in the Software without restriction, including without limitation
108 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
109 * and/or sell copies of the Software, and to permit persons to whom the
110 * Software is furnished to do so, subject to the following conditions:
112 * The above copyright notice and this permission notice (including the next
113 * paragraph) shall be included in all copies or substantial portions of the
116 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
117 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
118 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
119 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
120 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
121 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
125 /* This file generated from ${filename}, don't edit directly. */
127 #include "radv_private.h"
129 struct radv_entrypoint {
134 /* We use a big string constant to avoid lots of reloctions from the entry
135 * point table to lots of little strings. The entries in the entry point table
136 * store the index into this big string.
139 static const char strings[] =
140 % for _, name, _, _, _, _ in entrypoints:
145 static const struct radv_entrypoint entrypoints[] = {
146 % for _, _, _, num, h, _ in entrypoints:
147 { ${offsets[num]}, ${'{:0=#8x}'.format(h)} },
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 ['radv']:
157 % for type_, name, args, _, _, guard in entrypoints:
158 % if guard is not None:
161 ${type_} ${layer}_${name}(${args}) __attribute__ ((weak));
162 % if guard is not None:
167 const struct radv_dispatch_table ${layer}_layer = {
168 % for _, name, args, _, _, guard in entrypoints:
169 % if guard is not None:
172 .${name} = ${layer}_${name},
173 % if guard is not None:
180 static void * __attribute__ ((noinline))
181 radv_resolve_entrypoint(uint32_t index)
183 return radv_layer.entrypoints[index];
187 * size ${hash_size} entries
188 * collisions entries:
189 % for i in xrange(10):
190 * ${i}${'+' if i == 9 else ''} ${collisions[i]}
194 #define none ${'{:#x}'.format(none)}
195 static const uint16_t map[] = {
196 % for i in xrange(0, hash_size, 8):
197 % for j in xrange(i, i + 8):
198 ## This is 6 because the 0x is counted in the length
199 % if mapping[j] & 0xffff == 0xffff:
202 ${'{:0=#6x}'.format(mapping[j] & 0xffff)},
209 radv_lookup_entrypoint(const char *name)
211 static const uint32_t prime_factor = ${prime_factor};
212 static const uint32_t prime_step = ${prime_step};
213 const struct radv_entrypoint *e;
218 for (p = name; *p; p++)
219 hash = hash * prime_factor + *p;
223 i = map[h & ${hash_mask}];
228 } while (e->hash != hash);
230 if (strcmp(name, strings + e->name) != 0)
233 return radv_resolve_entrypoint(i);
234 }"""), output_encoding
='utf-8')
239 HASH_MASK
= HASH_SIZE
- 1
241 PRIME_FACTOR
= 5024183
246 """Calculate the same hash value that Mesa will calculate in C."""
247 return functools
.reduce(
248 lambda h
, c
: (h
* PRIME_FACTOR
+ ord(c
)) & U32_MASK
, name
, 0)
251 def get_entrypoints(doc
, entrypoints_to_defines
):
252 """Extract the entry points from the registry."""
255 enabled_commands
= set()
256 for feature
in doc
.findall('./feature'):
257 assert feature
.attrib
['api'] == 'vulkan'
258 if float(feature
.attrib
['number']) > MAX_API_VERSION
:
261 for command
in feature
.findall('./require/command'):
262 enabled_commands
.add(command
.attrib
['name'])
264 for extension
in doc
.findall('.extensions/extension'):
265 if extension
.attrib
['name'] not in SUPPORTED_EXTENSIONS
:
268 assert extension
.attrib
['supported'] == 'vulkan'
269 for command
in extension
.findall('./require/command'):
270 enabled_commands
.add(command
.attrib
['name'])
273 for command
in doc
.findall('./commands/command'):
274 type = command
.find('./proto/type').text
275 fullname
= command
.find('./proto/name').text
277 if fullname
not in enabled_commands
:
280 shortname
= fullname
[2:]
281 params
= (''.join(p
.itertext()) for p
in command
.findall('./param'))
282 params
= ', '.join(params
)
283 guard
= entrypoints_to_defines
.get(fullname
)
284 entrypoints
.append((type, shortname
, params
, index
, cal_hash(fullname
), guard
))
290 def get_entrypoints_defines(doc
):
291 """Maps entry points to extension defines."""
292 entrypoints_to_defines
= {}
294 for extension
in doc
.findall('./extensions/extension[@protect]'):
295 define
= extension
.attrib
['protect']
297 for entrypoint
in extension
.findall('./require/command'):
298 fullname
= entrypoint
.attrib
['name']
299 entrypoints_to_defines
[fullname
] = define
301 return entrypoints_to_defines
304 def gen_code(entrypoints
):
305 """Generate the C code."""
308 for _
, name
, _
, _
, _
, _
in entrypoints
:
310 i
+= 2 + len(name
) + 1
312 mapping
= [NONE
] * HASH_SIZE
313 collisions
= [0] * 10
314 for _
, name
, _
, num
, h
, _
in entrypoints
:
316 while mapping
[h
& HASH_MASK
] != NONE
:
322 collisions
[level
] += 1
323 mapping
[h
& HASH_MASK
] = num
325 return TEMPLATE_C
.render(entrypoints
=entrypoints
,
327 collisions
=collisions
,
330 prime_step
=PRIME_STEP
,
331 prime_factor
=PRIME_FACTOR
,
334 filename
=os
.path
.basename(__file__
))
338 parser
= argparse
.ArgumentParser()
339 parser
.add_argument('--outdir', help='Where to write the files.',
341 parser
.add_argument('--xml', help='Vulkan API XML file.', required
=True)
342 args
= parser
.parse_args()
344 doc
= et
.parse(args
.xml
)
345 entrypoints
= get_entrypoints(doc
, get_entrypoints_defines(doc
))
347 with
open(os
.path
.join(args
.outdir
, 'radv_entrypoints.h'), 'wb') as f
:
348 f
.write(TEMPLATE_H
.render(entrypoints
=entrypoints
,
349 filename
=os
.path
.basename(__file__
)))
350 with
open(os
.path
.join(args
.outdir
, 'radv_entrypoints.c'), 'wb') as f
:
351 f
.write(gen_code(entrypoints
))
354 if __name__
== '__main__':