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_KHR_descriptor_update_template',
37 'VK_KHR_get_memory_requirements2',
38 'VK_KHR_get_physical_device_properties2',
39 'VK_KHR_get_surface_capabilities2',
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',
53 # We generate a static hash table for entry point lookup
54 # (vkGetProcAddress). We use a linear congruential generator for our hash
55 # function and a power-of-two size table. The prime numbers are determined
58 TEMPLATE_H
= Template(textwrap
.dedent("""\
59 /* This file generated from ${filename}, don't edit directly. */
61 struct anv_dispatch_table {
63 void *entrypoints[${len(entrypoints)}];
65 % for _, name, _, _, _, guard in entrypoints:
66 % if guard is not None:
68 PFN_vk${name} ${name};
73 PFN_vk${name} ${name};
80 % for type_, name, args, num, h, guard in entrypoints:
81 % if guard is not None:
84 ${type_} anv_${name}(${args});
85 ${type_} gen7_${name}(${args});
86 ${type_} gen75_${name}(${args});
87 ${type_} gen8_${name}(${args});
88 ${type_} gen9_${name}(${args});
89 ${type_} gen10_${name}(${args});
90 % if guard is not None:
94 """), output_encoding
='utf-8')
96 TEMPLATE_C
= Template(textwrap
.dedent(u
"""\
98 * Copyright © 2015 Intel Corporation
100 * Permission is hereby granted, free of charge, to any person obtaining a
101 * copy of this software and associated documentation files (the "Software"),
102 * to deal in the Software without restriction, including without limitation
103 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
104 * and/or sell copies of the Software, and to permit persons to whom the
105 * Software is furnished to do so, subject to the following conditions:
107 * The above copyright notice and this permission notice (including the next
108 * paragraph) shall be included in all copies or substantial portions of the
111 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
112 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
113 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
114 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
115 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
116 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
120 /* This file generated from ${filename}, don't edit directly. */
122 #include "anv_private.h"
124 struct anv_entrypoint {
129 /* We use a big string constant to avoid lots of reloctions from the entry
130 * point table to lots of little strings. The entries in the entry point table
131 * store the index into this big string.
134 static const char strings[] =
135 % for _, name, _, _, _, _ in entrypoints:
140 static const struct anv_entrypoint entrypoints[] = {
141 % for _, _, _, num, h, _ in entrypoints:
142 { ${offsets[num]}, ${'{:0=#8x}'.format(h)} },
146 /* Weak aliases for all potential implementations. These will resolve to
147 * NULL if they're not defined, which lets the resolve_entrypoint() function
148 * either pick the correct entry point.
151 % for layer in ['anv', 'gen7', 'gen75', 'gen8', 'gen9', 'gen10']:
152 % for type_, name, args, _, _, guard in entrypoints:
153 % if guard is not None:
156 ${type_} ${layer}_${name}(${args}) __attribute__ ((weak));
157 % if guard is not None:
162 const struct anv_dispatch_table ${layer}_layer = {
163 % for _, name, args, _, _, guard in entrypoints:
164 % if guard is not None:
167 .${name} = ${layer}_${name},
168 % if guard is not None:
175 static void * __attribute__ ((noinline))
176 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
178 if (devinfo == NULL) {
179 return anv_layer.entrypoints[index];
182 switch (devinfo->gen) {
184 if (gen10_layer.entrypoints[index])
185 return gen10_layer.entrypoints[index];
188 if (gen9_layer.entrypoints[index])
189 return gen9_layer.entrypoints[index];
192 if (gen8_layer.entrypoints[index])
193 return gen8_layer.entrypoints[index];
196 if (devinfo->is_haswell && gen75_layer.entrypoints[index])
197 return gen75_layer.entrypoints[index];
199 if (gen7_layer.entrypoints[index])
200 return gen7_layer.entrypoints[index];
203 return anv_layer.entrypoints[index];
205 unreachable("unsupported gen\\n");
210 * size ${hash_size} entries
211 * collisions entries:
212 % for i in xrange(10):
213 * ${i}${'+' if i == 9 else ''} ${collisions[i]}
217 #define none ${'{:#x}'.format(none)}
218 static const uint16_t map[] = {
219 % for i in xrange(0, hash_size, 8):
220 % for j in xrange(i, i + 8):
221 ## This is 6 because the 0x is counted in the length
222 % if mapping[j] & 0xffff == 0xffff:
225 ${'{:0=#6x}'.format(mapping[j] & 0xffff)},
232 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
234 static const uint32_t prime_factor = ${prime_factor};
235 static const uint32_t prime_step = ${prime_step};
236 const struct anv_entrypoint *e;
241 for (p = name; *p; p++)
242 hash = hash * prime_factor + *p;
246 i = map[h & ${hash_mask}];
251 } while (e->hash != hash);
253 if (strcmp(name, strings + e->name) != 0)
256 return anv_resolve_entrypoint(devinfo, i);
257 }"""), output_encoding
='utf-8')
262 HASH_MASK
= HASH_SIZE
- 1
264 PRIME_FACTOR
= 5024183
269 """Calculate the same hash value that Mesa will calculate in C."""
270 return functools
.reduce(
271 lambda h
, c
: (h
* PRIME_FACTOR
+ ord(c
)) & U32_MASK
, name
, 0)
274 def get_entrypoints(doc
, entrypoints_to_defines
):
275 """Extract the entry points from the registry."""
278 enabled_commands
= set()
279 for feature
in doc
.findall('./feature'):
280 assert feature
.attrib
['api'] == 'vulkan'
281 if float(feature
.attrib
['number']) > MAX_API_VERSION
:
284 for command
in feature
.findall('./require/command'):
285 enabled_commands
.add(command
.attrib
['name'])
287 for extension
in doc
.findall('.extensions/extension'):
288 if extension
.attrib
['name'] not in SUPPORTED_EXTENSIONS
:
291 assert extension
.attrib
['supported'] == 'vulkan'
292 for command
in extension
.findall('./require/command'):
293 enabled_commands
.add(command
.attrib
['name'])
296 for command
in doc
.findall('./commands/command'):
297 type = command
.find('./proto/type').text
298 fullname
= command
.find('./proto/name').text
300 if fullname
not in enabled_commands
:
303 shortname
= fullname
[2:]
304 params
= (''.join(p
.itertext()) for p
in command
.findall('./param'))
305 params
= ', '.join(params
)
306 guard
= entrypoints_to_defines
.get(fullname
)
307 entrypoints
.append((type, shortname
, params
, index
, cal_hash(fullname
), guard
))
313 def get_entrypoints_defines(doc
):
314 """Maps entry points to extension defines."""
315 entrypoints_to_defines
= {}
317 for extension
in doc
.findall('./extensions/extension[@protect]'):
318 define
= extension
.attrib
['protect']
320 for entrypoint
in extension
.findall('./require/command'):
321 fullname
= entrypoint
.attrib
['name']
322 entrypoints_to_defines
[fullname
] = define
324 return entrypoints_to_defines
327 def gen_code(entrypoints
):
328 """Generate the C code."""
331 for _
, name
, _
, _
, _
, _
in entrypoints
:
333 i
+= 2 + len(name
) + 1
335 mapping
= [NONE
] * HASH_SIZE
336 collisions
= [0] * 10
337 for _
, name
, _
, num
, h
, _
in entrypoints
:
339 while mapping
[h
& HASH_MASK
] != NONE
:
345 collisions
[level
] += 1
346 mapping
[h
& HASH_MASK
] = num
348 return TEMPLATE_C
.render(entrypoints
=entrypoints
,
350 collisions
=collisions
,
353 prime_step
=PRIME_STEP
,
354 prime_factor
=PRIME_FACTOR
,
357 filename
=os
.path
.basename(__file__
))
361 parser
= argparse
.ArgumentParser()
362 parser
.add_argument('--outdir', help='Where to write the files.',
364 parser
.add_argument('--xml', help='Vulkan API XML file.', required
=True)
365 args
= parser
.parse_args()
367 doc
= et
.parse(args
.xml
)
368 entrypoints
= get_entrypoints(doc
, get_entrypoints_defines(doc
))
370 # Manually add CreateDmaBufImageINTEL for which we don't have an extension
372 entrypoints
.append(('VkResult', 'CreateDmaBufImageINTEL',
373 'VkDevice device, ' +
374 'const VkDmaBufImageCreateInfo* pCreateInfo, ' +
375 'const VkAllocationCallbacks* pAllocator,' +
376 'VkDeviceMemory* pMem,' +
377 'VkImage* pImage', len(entrypoints
),
378 cal_hash('vkCreateDmaBufImageINTEL'), None))
380 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
382 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.h'), 'wb') as f
:
383 f
.write(TEMPLATE_H
.render(entrypoints
=entrypoints
,
384 filename
=os
.path
.basename(__file__
)))
385 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.c'), 'wb') as f
:
386 f
.write(gen_code(entrypoints
))
389 if __name__
== '__main__':