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_external_memory',
39 'VK_KHR_external_memory_capabilities',
40 'VK_KHR_external_memory_fd',
41 'VK_KHR_get_memory_requirements2',
42 'VK_KHR_get_physical_device_properties2',
43 'VK_KHR_get_surface_capabilities2',
44 'VK_KHR_incremental_present',
45 'VK_KHR_maintenance1',
46 'VK_KHR_push_descriptor',
47 'VK_KHR_sampler_mirror_clamp_to_edge',
48 'VK_KHR_shader_draw_parameters',
51 'VK_KHR_wayland_surface',
53 'VK_KHR_xlib_surface',
57 # We generate a static hash table for entry point lookup
58 # (vkGetProcAddress). We use a linear congruential generator for our hash
59 # function and a power-of-two size table. The prime numbers are determined
62 TEMPLATE_H
= Template(textwrap
.dedent("""\
63 /* This file generated from ${filename}, don't edit directly. */
65 struct anv_dispatch_table {
67 void *entrypoints[${len(entrypoints)}];
69 % for _, name, _, _, _, guard in entrypoints:
70 % if guard is not None:
72 PFN_vk${name} ${name};
77 PFN_vk${name} ${name};
84 % for type_, name, args, num, h, guard in entrypoints:
85 % if guard is not None:
88 ${type_} anv_${name}(${args});
89 ${type_} gen7_${name}(${args});
90 ${type_} gen75_${name}(${args});
91 ${type_} gen8_${name}(${args});
92 ${type_} gen9_${name}(${args});
93 ${type_} gen10_${name}(${args});
94 % if guard is not None:
98 """), output_encoding
='utf-8')
100 TEMPLATE_C
= Template(textwrap
.dedent(u
"""\
102 * Copyright © 2015 Intel Corporation
104 * Permission is hereby granted, free of charge, to any person obtaining a
105 * copy of this software and associated documentation files (the "Software"),
106 * to deal in the Software without restriction, including without limitation
107 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
108 * and/or sell copies of the Software, and to permit persons to whom the
109 * Software is furnished to do so, subject to the following conditions:
111 * The above copyright notice and this permission notice (including the next
112 * paragraph) shall be included in all copies or substantial portions of the
115 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
116 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
117 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
118 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
119 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
120 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
124 /* This file generated from ${filename}, don't edit directly. */
126 #include "anv_private.h"
128 struct anv_entrypoint {
133 /* We use a big string constant to avoid lots of reloctions from the entry
134 * point table to lots of little strings. The entries in the entry point table
135 * store the index into this big string.
138 static const char strings[] =
139 % for _, name, _, _, _, _ in entrypoints:
144 static const struct anv_entrypoint entrypoints[] = {
145 % for _, _, _, num, h, _ in entrypoints:
146 { ${offsets[num]}, ${'{:0=#8x}'.format(h)} },
150 /* Weak aliases for all potential implementations. These will resolve to
151 * NULL if they're not defined, which lets the resolve_entrypoint() function
152 * either pick the correct entry point.
155 % for layer in ['anv', 'gen7', 'gen75', 'gen8', 'gen9', 'gen10']:
156 % for type_, name, args, _, _, guard in entrypoints:
157 % if guard is not None:
160 ${type_} ${layer}_${name}(${args}) __attribute__ ((weak));
161 % if guard is not None:
166 const struct anv_dispatch_table ${layer}_layer = {
167 % for _, name, args, _, _, guard in entrypoints:
168 % if guard is not None:
171 .${name} = ${layer}_${name},
172 % if guard is not None:
179 static void * __attribute__ ((noinline))
180 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
182 if (devinfo == NULL) {
183 return anv_layer.entrypoints[index];
186 switch (devinfo->gen) {
188 if (gen10_layer.entrypoints[index])
189 return gen10_layer.entrypoints[index];
192 if (gen9_layer.entrypoints[index])
193 return gen9_layer.entrypoints[index];
196 if (gen8_layer.entrypoints[index])
197 return gen8_layer.entrypoints[index];
200 if (devinfo->is_haswell && gen75_layer.entrypoints[index])
201 return gen75_layer.entrypoints[index];
203 if (gen7_layer.entrypoints[index])
204 return gen7_layer.entrypoints[index];
207 return anv_layer.entrypoints[index];
209 unreachable("unsupported gen\\n");
214 * size ${hash_size} entries
215 * collisions entries:
216 % for i in xrange(10):
217 * ${i}${'+' if i == 9 else ''} ${collisions[i]}
221 #define none ${'{:#x}'.format(none)}
222 static const uint16_t map[] = {
223 % for i in xrange(0, hash_size, 8):
224 % for j in xrange(i, i + 8):
225 ## This is 6 because the 0x is counted in the length
226 % if mapping[j] & 0xffff == 0xffff:
229 ${'{:0=#6x}'.format(mapping[j] & 0xffff)},
236 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
238 static const uint32_t prime_factor = ${prime_factor};
239 static const uint32_t prime_step = ${prime_step};
240 const struct anv_entrypoint *e;
245 for (p = name; *p; p++)
246 hash = hash * prime_factor + *p;
250 i = map[h & ${hash_mask}];
255 } while (e->hash != hash);
257 if (strcmp(name, strings + e->name) != 0)
260 return anv_resolve_entrypoint(devinfo, i);
261 }"""), output_encoding
='utf-8')
266 HASH_MASK
= HASH_SIZE
- 1
268 PRIME_FACTOR
= 5024183
273 """Calculate the same hash value that Mesa will calculate in C."""
274 return functools
.reduce(
275 lambda h
, c
: (h
* PRIME_FACTOR
+ ord(c
)) & U32_MASK
, name
, 0)
278 def get_entrypoints(doc
, entrypoints_to_defines
):
279 """Extract the entry points from the registry."""
282 enabled_commands
= set()
283 for feature
in doc
.findall('./feature'):
284 assert feature
.attrib
['api'] == 'vulkan'
285 if float(feature
.attrib
['number']) > MAX_API_VERSION
:
288 for command
in feature
.findall('./require/command'):
289 enabled_commands
.add(command
.attrib
['name'])
291 for extension
in doc
.findall('.extensions/extension'):
292 if extension
.attrib
['name'] not in SUPPORTED_EXTENSIONS
:
295 assert extension
.attrib
['supported'] == 'vulkan'
296 for command
in extension
.findall('./require/command'):
297 enabled_commands
.add(command
.attrib
['name'])
300 for command
in doc
.findall('./commands/command'):
301 type = command
.find('./proto/type').text
302 fullname
= command
.find('./proto/name').text
304 if fullname
not in enabled_commands
:
307 shortname
= fullname
[2:]
308 params
= (''.join(p
.itertext()) for p
in command
.findall('./param'))
309 params
= ', '.join(params
)
310 guard
= entrypoints_to_defines
.get(fullname
)
311 entrypoints
.append((type, shortname
, params
, index
, cal_hash(fullname
), guard
))
317 def get_entrypoints_defines(doc
):
318 """Maps entry points to extension defines."""
319 entrypoints_to_defines
= {}
321 for extension
in doc
.findall('./extensions/extension[@protect]'):
322 define
= extension
.attrib
['protect']
324 for entrypoint
in extension
.findall('./require/command'):
325 fullname
= entrypoint
.attrib
['name']
326 entrypoints_to_defines
[fullname
] = define
328 return entrypoints_to_defines
331 def gen_code(entrypoints
):
332 """Generate the C code."""
335 for _
, name
, _
, _
, _
, _
in entrypoints
:
337 i
+= 2 + len(name
) + 1
339 mapping
= [NONE
] * HASH_SIZE
340 collisions
= [0] * 10
341 for _
, name
, _
, num
, h
, _
in entrypoints
:
343 while mapping
[h
& HASH_MASK
] != NONE
:
349 collisions
[level
] += 1
350 mapping
[h
& HASH_MASK
] = num
352 return TEMPLATE_C
.render(entrypoints
=entrypoints
,
354 collisions
=collisions
,
357 prime_step
=PRIME_STEP
,
358 prime_factor
=PRIME_FACTOR
,
361 filename
=os
.path
.basename(__file__
))
365 parser
= argparse
.ArgumentParser()
366 parser
.add_argument('--outdir', help='Where to write the files.',
368 parser
.add_argument('--xml', help='Vulkan API XML file.', required
=True)
369 args
= parser
.parse_args()
371 doc
= et
.parse(args
.xml
)
372 entrypoints
= get_entrypoints(doc
, get_entrypoints_defines(doc
))
374 # Manually add CreateDmaBufImageINTEL for which we don't have an extension
376 entrypoints
.append(('VkResult', 'CreateDmaBufImageINTEL',
377 'VkDevice device, ' +
378 'const VkDmaBufImageCreateInfo* pCreateInfo, ' +
379 'const VkAllocationCallbacks* pAllocator,' +
380 'VkDeviceMemory* pMem,' +
381 'VkImage* pImage', len(entrypoints
),
382 cal_hash('vkCreateDmaBufImageINTEL'), None))
384 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
386 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.h'), 'wb') as f
:
387 f
.write(TEMPLATE_H
.render(entrypoints
=entrypoints
,
388 filename
=os
.path
.basename(__file__
)))
389 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.c'), 'wb') as f
:
390 f
.write(gen_code(entrypoints
))
393 if __name__
== '__main__':