f1b4b0c95e14b4bb8bb99f505a0cf1ff2a4d29e5
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_physical_device_properties2',
38 'VK_KHR_get_surface_capabilities2',
39 'VK_KHR_incremental_present',
40 'VK_KHR_maintenance1',
41 'VK_KHR_push_descriptor',
42 'VK_KHR_sampler_mirror_clamp_to_edge',
43 'VK_KHR_shader_draw_parameters',
46 'VK_KHR_wayland_surface',
48 'VK_KHR_xlib_surface',
49 'VK_KHX_external_memory',
50 'VK_KHX_external_memory_capabilities',
51 'VK_KHX_external_memory_fd',
52 'VK_KHX_external_semaphore',
53 'VK_KHX_external_semaphore_capabilities',
54 'VK_KHX_external_semaphore_fd',
58 # We generate a static hash table for entry point lookup
59 # (vkGetProcAddress). We use a linear congruential generator for our hash
60 # function and a power-of-two size table. The prime numbers are determined
63 TEMPLATE_H
= Template(textwrap
.dedent("""\
64 /* This file generated from ${filename}, don't edit directly. */
66 struct anv_dispatch_table {
68 void *entrypoints[${len(entrypoints)}];
70 % for _, name, _, _, _, guard in entrypoints:
71 % if guard is not None:
73 PFN_vk${name} ${name};
78 PFN_vk${name} ${name};
85 % for type_, name, args, num, h, guard in entrypoints:
86 % if guard is not None:
89 ${type_} anv_${name}(${args});
90 ${type_} gen7_${name}(${args});
91 ${type_} gen75_${name}(${args});
92 ${type_} gen8_${name}(${args});
93 ${type_} gen9_${name}(${args});
94 ${type_} gen10_${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 "anv_private.h"
129 struct anv_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 anv_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 ['anv', 'gen7', 'gen75', 'gen8', 'gen9', 'gen10']:
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 anv_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 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
183 if (devinfo == NULL) {
184 return anv_layer.entrypoints[index];
187 switch (devinfo->gen) {
189 if (gen10_layer.entrypoints[index])
190 return gen10_layer.entrypoints[index];
193 if (gen9_layer.entrypoints[index])
194 return gen9_layer.entrypoints[index];
197 if (gen8_layer.entrypoints[index])
198 return gen8_layer.entrypoints[index];
201 if (devinfo->is_haswell && gen75_layer.entrypoints[index])
202 return gen75_layer.entrypoints[index];
204 if (gen7_layer.entrypoints[index])
205 return gen7_layer.entrypoints[index];
208 return anv_layer.entrypoints[index];
210 unreachable("unsupported gen\\n");
215 * size ${hash_size} entries
216 * collisions entries:
217 % for i in xrange(10):
218 * ${i}${'+' if i == 9 else ''} ${collisions[i]}
222 #define none ${'{:#x}'.format(none)}
223 static const uint16_t map[] = {
224 % for i in xrange(0, hash_size, 8):
225 % for j in xrange(i, i + 8):
226 ## This is 6 because the 0x is counted in the length
227 % if mapping[j] & 0xffff == 0xffff:
230 ${'{:0=#6x}'.format(mapping[j] & 0xffff)},
237 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
239 static const uint32_t prime_factor = ${prime_factor};
240 static const uint32_t prime_step = ${prime_step};
241 const struct anv_entrypoint *e;
246 for (p = name; *p; p++)
247 hash = hash * prime_factor + *p;
251 i = map[h & ${hash_mask}];
256 } while (e->hash != hash);
258 if (strcmp(name, strings + e->name) != 0)
261 return anv_resolve_entrypoint(devinfo, i);
262 }"""), output_encoding
='utf-8')
267 HASH_MASK
= HASH_SIZE
- 1
269 PRIME_FACTOR
= 5024183
274 """Calculate the same hash value that Mesa will calculate in C."""
275 return functools
.reduce(
276 lambda h
, c
: (h
* PRIME_FACTOR
+ ord(c
)) & U32_MASK
, name
, 0)
279 def get_entrypoints(doc
, entrypoints_to_defines
):
280 """Extract the entry points from the registry."""
283 enabled_commands
= set()
284 for feature
in doc
.findall('./feature'):
285 assert feature
.attrib
['api'] == 'vulkan'
286 if float(feature
.attrib
['number']) > MAX_API_VERSION
:
289 for command
in feature
.findall('./require/command'):
290 enabled_commands
.add(command
.attrib
['name'])
292 for extension
in doc
.findall('.extensions/extension'):
293 if extension
.attrib
['name'] not in SUPPORTED_EXTENSIONS
:
296 assert extension
.attrib
['supported'] == 'vulkan'
297 for command
in extension
.findall('./require/command'):
298 enabled_commands
.add(command
.attrib
['name'])
301 for command
in doc
.findall('./commands/command'):
302 type = command
.find('./proto/type').text
303 fullname
= command
.find('./proto/name').text
305 if fullname
not in enabled_commands
:
308 shortname
= fullname
[2:]
309 params
= (''.join(p
.itertext()) for p
in command
.findall('./param'))
310 params
= ', '.join(params
)
311 guard
= entrypoints_to_defines
.get(fullname
)
312 entrypoints
.append((type, shortname
, params
, index
, cal_hash(fullname
), guard
))
318 def get_entrypoints_defines(doc
):
319 """Maps entry points to extension defines."""
320 entrypoints_to_defines
= {}
322 for extension
in doc
.findall('./extensions/extension[@protect]'):
323 define
= extension
.attrib
['protect']
325 for entrypoint
in extension
.findall('./require/command'):
326 fullname
= entrypoint
.attrib
['name']
327 entrypoints_to_defines
[fullname
] = define
329 return entrypoints_to_defines
332 def gen_code(entrypoints
):
333 """Generate the C code."""
336 for _
, name
, _
, _
, _
, _
in entrypoints
:
338 i
+= 2 + len(name
) + 1
340 mapping
= [NONE
] * HASH_SIZE
341 collisions
= [0] * 10
342 for _
, name
, _
, num
, h
, _
in entrypoints
:
344 while mapping
[h
& HASH_MASK
] != NONE
:
350 collisions
[level
] += 1
351 mapping
[h
& HASH_MASK
] = num
353 return TEMPLATE_C
.render(entrypoints
=entrypoints
,
355 collisions
=collisions
,
358 prime_step
=PRIME_STEP
,
359 prime_factor
=PRIME_FACTOR
,
362 filename
=os
.path
.basename(__file__
))
366 parser
= argparse
.ArgumentParser()
367 parser
.add_argument('--outdir', help='Where to write the files.',
369 parser
.add_argument('--xml', help='Vulkan API XML file.', required
=True)
370 args
= parser
.parse_args()
372 doc
= et
.parse(args
.xml
)
373 entrypoints
= get_entrypoints(doc
, get_entrypoints_defines(doc
))
375 # Manually add CreateDmaBufImageINTEL for which we don't have an extension
377 entrypoints
.append(('VkResult', 'CreateDmaBufImageINTEL',
378 'VkDevice device, ' +
379 'const VkDmaBufImageCreateInfo* pCreateInfo, ' +
380 'const VkAllocationCallbacks* pAllocator,' +
381 'VkDeviceMemory* pMem,' +
382 'VkImage* pImage', len(entrypoints
),
383 cal_hash('vkCreateDmaBufImageINTEL'), None))
385 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
387 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.h'), 'wb') as f
:
388 f
.write(TEMPLATE_H
.render(entrypoints
=entrypoints
,
389 filename
=os
.path
.basename(__file__
)))
390 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.c'), 'wb') as f
:
391 f
.write(gen_code(entrypoints
))
394 if __name__
== '__main__':