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_dedicated_allocation',
37 'VK_KHR_descriptor_update_template',
38 'VK_KHR_get_memory_requirements2',
39 'VK_KHR_get_physical_device_properties2',
40 'VK_KHR_get_surface_capabilities2',
41 'VK_KHR_incremental_present',
42 'VK_KHR_maintenance1',
43 'VK_KHR_push_descriptor',
44 'VK_KHR_sampler_mirror_clamp_to_edge',
45 'VK_KHR_shader_draw_parameters',
48 'VK_KHR_wayland_surface',
50 'VK_KHR_xlib_surface',
54 # We generate a static hash table for entry point lookup
55 # (vkGetProcAddress). We use a linear congruential generator for our hash
56 # function and a power-of-two size table. The prime numbers are determined
59 TEMPLATE_H
= Template(textwrap
.dedent("""\
60 /* This file generated from ${filename}, don't edit directly. */
62 struct anv_dispatch_table {
64 void *entrypoints[${len(entrypoints)}];
66 % for _, name, _, _, _, guard in entrypoints:
67 % if guard is not None:
69 PFN_vk${name} ${name};
74 PFN_vk${name} ${name};
81 % for type_, name, args, num, h, guard in entrypoints:
82 % if guard is not None:
85 ${type_} anv_${name}(${args});
86 ${type_} gen7_${name}(${args});
87 ${type_} gen75_${name}(${args});
88 ${type_} gen8_${name}(${args});
89 ${type_} gen9_${name}(${args});
90 ${type_} gen10_${name}(${args});
91 % if guard is not None:
95 """), output_encoding
='utf-8')
97 TEMPLATE_C
= Template(textwrap
.dedent(u
"""\
99 * Copyright © 2015 Intel Corporation
101 * Permission is hereby granted, free of charge, to any person obtaining a
102 * copy of this software and associated documentation files (the "Software"),
103 * to deal in the Software without restriction, including without limitation
104 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
105 * and/or sell copies of the Software, and to permit persons to whom the
106 * Software is furnished to do so, subject to the following conditions:
108 * The above copyright notice and this permission notice (including the next
109 * paragraph) shall be included in all copies or substantial portions of the
112 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
113 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
114 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
115 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
116 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
117 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
121 /* This file generated from ${filename}, don't edit directly. */
123 #include "anv_private.h"
125 struct anv_entrypoint {
130 /* We use a big string constant to avoid lots of reloctions from the entry
131 * point table to lots of little strings. The entries in the entry point table
132 * store the index into this big string.
135 static const char strings[] =
136 % for _, name, _, _, _, _ in entrypoints:
141 static const struct anv_entrypoint entrypoints[] = {
142 % for _, _, _, num, h, _ in entrypoints:
143 { ${offsets[num]}, ${'{:0=#8x}'.format(h)} },
147 /* Weak aliases for all potential implementations. These will resolve to
148 * NULL if they're not defined, which lets the resolve_entrypoint() function
149 * either pick the correct entry point.
152 % for layer in ['anv', 'gen7', 'gen75', 'gen8', 'gen9', 'gen10']:
153 % for type_, name, args, _, _, guard in entrypoints:
154 % if guard is not None:
157 ${type_} ${layer}_${name}(${args}) __attribute__ ((weak));
158 % if guard is not None:
163 const struct anv_dispatch_table ${layer}_layer = {
164 % for _, name, args, _, _, guard in entrypoints:
165 % if guard is not None:
168 .${name} = ${layer}_${name},
169 % if guard is not None:
176 static void * __attribute__ ((noinline))
177 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
179 if (devinfo == NULL) {
180 return anv_layer.entrypoints[index];
183 switch (devinfo->gen) {
185 if (gen10_layer.entrypoints[index])
186 return gen10_layer.entrypoints[index];
189 if (gen9_layer.entrypoints[index])
190 return gen9_layer.entrypoints[index];
193 if (gen8_layer.entrypoints[index])
194 return gen8_layer.entrypoints[index];
197 if (devinfo->is_haswell && gen75_layer.entrypoints[index])
198 return gen75_layer.entrypoints[index];
200 if (gen7_layer.entrypoints[index])
201 return gen7_layer.entrypoints[index];
204 return anv_layer.entrypoints[index];
206 unreachable("unsupported gen\\n");
211 * size ${hash_size} entries
212 * collisions entries:
213 % for i in xrange(10):
214 * ${i}${'+' if i == 9 else ''} ${collisions[i]}
218 #define none ${'{:#x}'.format(none)}
219 static const uint16_t map[] = {
220 % for i in xrange(0, hash_size, 8):
221 % for j in xrange(i, i + 8):
222 ## This is 6 because the 0x is counted in the length
223 % if mapping[j] & 0xffff == 0xffff:
226 ${'{:0=#6x}'.format(mapping[j] & 0xffff)},
233 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
235 static const uint32_t prime_factor = ${prime_factor};
236 static const uint32_t prime_step = ${prime_step};
237 const struct anv_entrypoint *e;
242 for (p = name; *p; p++)
243 hash = hash * prime_factor + *p;
247 i = map[h & ${hash_mask}];
252 } while (e->hash != hash);
254 if (strcmp(name, strings + e->name) != 0)
257 return anv_resolve_entrypoint(devinfo, i);
258 }"""), output_encoding
='utf-8')
263 HASH_MASK
= HASH_SIZE
- 1
265 PRIME_FACTOR
= 5024183
270 """Calculate the same hash value that Mesa will calculate in C."""
271 return functools
.reduce(
272 lambda h
, c
: (h
* PRIME_FACTOR
+ ord(c
)) & U32_MASK
, name
, 0)
275 def get_entrypoints(doc
, entrypoints_to_defines
):
276 """Extract the entry points from the registry."""
279 enabled_commands
= set()
280 for feature
in doc
.findall('./feature'):
281 assert feature
.attrib
['api'] == 'vulkan'
282 if float(feature
.attrib
['number']) > MAX_API_VERSION
:
285 for command
in feature
.findall('./require/command'):
286 enabled_commands
.add(command
.attrib
['name'])
288 for extension
in doc
.findall('.extensions/extension'):
289 if extension
.attrib
['name'] not in SUPPORTED_EXTENSIONS
:
292 assert extension
.attrib
['supported'] == 'vulkan'
293 for command
in extension
.findall('./require/command'):
294 enabled_commands
.add(command
.attrib
['name'])
297 for command
in doc
.findall('./commands/command'):
298 type = command
.find('./proto/type').text
299 fullname
= command
.find('./proto/name').text
301 if fullname
not in enabled_commands
:
304 shortname
= fullname
[2:]
305 params
= (''.join(p
.itertext()) for p
in command
.findall('./param'))
306 params
= ', '.join(params
)
307 guard
= entrypoints_to_defines
.get(fullname
)
308 entrypoints
.append((type, shortname
, params
, index
, cal_hash(fullname
), guard
))
314 def get_entrypoints_defines(doc
):
315 """Maps entry points to extension defines."""
316 entrypoints_to_defines
= {}
318 for extension
in doc
.findall('./extensions/extension[@protect]'):
319 define
= extension
.attrib
['protect']
321 for entrypoint
in extension
.findall('./require/command'):
322 fullname
= entrypoint
.attrib
['name']
323 entrypoints_to_defines
[fullname
] = define
325 return entrypoints_to_defines
328 def gen_code(entrypoints
):
329 """Generate the C code."""
332 for _
, name
, _
, _
, _
, _
in entrypoints
:
334 i
+= 2 + len(name
) + 1
336 mapping
= [NONE
] * HASH_SIZE
337 collisions
= [0] * 10
338 for _
, name
, _
, num
, h
, _
in entrypoints
:
340 while mapping
[h
& HASH_MASK
] != NONE
:
346 collisions
[level
] += 1
347 mapping
[h
& HASH_MASK
] = num
349 return TEMPLATE_C
.render(entrypoints
=entrypoints
,
351 collisions
=collisions
,
354 prime_step
=PRIME_STEP
,
355 prime_factor
=PRIME_FACTOR
,
358 filename
=os
.path
.basename(__file__
))
362 parser
= argparse
.ArgumentParser()
363 parser
.add_argument('--outdir', help='Where to write the files.',
365 parser
.add_argument('--xml', help='Vulkan API XML file.', required
=True)
366 args
= parser
.parse_args()
368 doc
= et
.parse(args
.xml
)
369 entrypoints
= get_entrypoints(doc
, get_entrypoints_defines(doc
))
371 # Manually add CreateDmaBufImageINTEL for which we don't have an extension
373 entrypoints
.append(('VkResult', 'CreateDmaBufImageINTEL',
374 'VkDevice device, ' +
375 'const VkDmaBufImageCreateInfo* pCreateInfo, ' +
376 'const VkAllocationCallbacks* pAllocator,' +
377 'VkDeviceMemory* pMem,' +
378 'VkImage* pImage', len(entrypoints
),
379 cal_hash('vkCreateDmaBufImageINTEL'), None))
381 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
383 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.h'), 'wb') as f
:
384 f
.write(TEMPLATE_H
.render(entrypoints
=entrypoints
,
385 filename
=os
.path
.basename(__file__
)))
386 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.c'), 'wb') as f
:
387 f
.write(gen_code(entrypoints
))
390 if __name__
== '__main__':