7dd98f960cea07a058d94516e8523bf2952fd6e3
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
28 import xml
.etree
.ElementTree
as et
30 from mako
.template
import Template
34 SUPPORTED_EXTENSIONS
= [
35 'VK_KHR_descriptor_update_template',
36 'VK_KHR_get_physical_device_properties2',
37 'VK_KHR_maintenance1',
38 'VK_KHR_push_descriptor',
39 'VK_KHR_sampler_mirror_clamp_to_edge',
40 'VK_KHR_shader_draw_parameters',
43 'VK_KHR_wayland_surface',
45 'VK_KHR_xlib_surface',
48 # We generate a static hash table for entry point lookup
49 # (vkGetProcAddress). We use a linear congruential generator for our hash
50 # function and a power-of-two size table. The prime numbers are determined
53 TEMPLATE_H
= Template(textwrap
.dedent("""\
54 /* This file generated from ${filename}, don't edit directly. */
56 struct anv_dispatch_table {
58 void *entrypoints[${len(entrypoints)}];
60 % for _, name, _, _, _, guard in entrypoints:
61 % if guard is not None:
63 PFN_vk${name} ${name};
68 PFN_vk${name} ${name};
75 void anv_set_dispatch_devinfo(const struct gen_device_info *info);
76 % for type_, name, args, num, h, guard in entrypoints:
77 % if guard is not None:
80 ${type_} anv_${name}(${args});
81 ${type_} gen7_${name}(${args});
82 ${type_} gen75_${name}(${args});
83 ${type_} gen8_${name}(${args});
84 ${type_} gen9_${name}(${args});
85 % if guard is not None:
94 HASH_MASK
= HASH_SIZE
- 1
96 PRIME_FACTOR
= 5024183
106 h
= (h
* PRIME_FACTOR
+ ord(c
)) & U32_MASK
111 def print_guard_start(guard
):
112 if guard
is not None:
113 print "#ifdef {0}".format(guard
)
116 def print_guard_end(guard
):
117 if guard
is not None:
118 print "#endif // {0}".format(guard
)
121 def get_entrypoints(doc
, entrypoints_to_defines
):
122 """Extract the entry points from the registry."""
125 enabled_commands
= set()
126 for feature
in doc
.findall('./feature'):
127 assert feature
.attrib
['api'] == 'vulkan'
128 if float(feature
.attrib
['number']) > MAX_API_VERSION
:
131 for command
in feature
.findall('./require/command'):
132 enabled_commands
.add(command
.attrib
['name'])
134 for extension
in doc
.findall('.extensions/extension'):
135 if extension
.attrib
['name'] not in SUPPORTED_EXTENSIONS
:
138 assert extension
.attrib
['supported'] == 'vulkan'
139 for command
in extension
.findall('./require/command'):
140 enabled_commands
.add(command
.attrib
['name'])
143 for command
in doc
.findall('./commands/command'):
144 type = command
.find('./proto/type').text
145 fullname
= command
.find('./proto/name').text
147 if fullname
not in enabled_commands
:
150 shortname
= fullname
[2:]
151 params
= (''.join(p
.itertext()) for p
in command
.findall('./param'))
152 params
= ', '.join(params
)
153 if fullname
in entrypoints_to_defines
:
154 guard
= entrypoints_to_defines
[fullname
]
157 entrypoints
.append((type, shortname
, params
, index
, hash(fullname
), guard
))
163 def get_entrypoints_defines(doc
):
164 """Maps entry points to extension defines."""
165 entrypoints_to_defines
= {}
166 extensions
= doc
.findall('./extensions/extension')
167 for extension
in extensions
:
168 define
= extension
.get('protect')
169 entrypoints
= extension
.findall('./require/command')
170 for entrypoint
in entrypoints
:
171 fullname
= entrypoint
.get('name')
172 entrypoints_to_defines
[fullname
] = define
173 return entrypoints_to_defines
176 def gen_code(entrypoints
):
177 print textwrap
.dedent("""\
179 * Copyright © 2015 Intel Corporation
181 * Permission is hereby granted, free of charge, to any person obtaining a
182 * copy of this software and associated documentation files (the "Software"),
183 * to deal in the Software without restriction, including without limitation
184 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
185 * and/or sell copies of the Software, and to permit persons to whom the
186 * Software is furnished to do so, subject to the following conditions:
188 * The above copyright notice and this permission notice (including the next
189 * paragraph) shall be included in all copies or substantial portions of the
192 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
193 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
194 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
195 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
196 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
197 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
201 /* This file generated from {}, don't edit directly. */
203 #include "anv_private.h"
205 struct anv_entrypoint {{
210 /* We use a big string constant to avoid lots of reloctions from the entry
211 * point table to lots of little strings. The entries in the entry point table
212 * store the index into this big string.
215 static const char strings[] =""".format(os
.path
.basename(__file__
)))
219 for type, name
, args
, num
, h
, guard
in entrypoints
:
220 print " \"vk%s\\0\"" % name
222 i
+= 2 + len(name
) + 1
225 # Now generate the table of all entry points
227 print "\nstatic const struct anv_entrypoint entrypoints[] = {"
228 for type, name
, args
, num
, h
, guard
in entrypoints
:
229 print " { %5d, 0x%08x }," % (offsets
[num
], h
)
232 print textwrap
.dedent("""
234 /* Weak aliases for all potential implementations. These will resolve to
235 * NULL if they're not defined, which lets the resolve_entrypoint() function
236 * either pick the correct entry point.
240 for layer
in ["anv", "gen7", "gen75", "gen8", "gen9"]:
241 for type, name
, args
, num
, h
, guard
in entrypoints
:
242 print_guard_start(guard
)
243 print "%s %s_%s(%s) __attribute__ ((weak));" % (type, layer
, name
, args
)
244 print_guard_end(guard
)
245 print "\nconst struct anv_dispatch_table %s_layer = {" % layer
246 for type, name
, args
, num
, h
, guard
in entrypoints
:
247 print_guard_start(guard
)
248 print " .%s = %s_%s," % (name
, layer
, name
)
249 print_guard_end(guard
)
252 print textwrap
.dedent("""
253 static void * __attribute__ ((noinline))
254 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
256 if (devinfo == NULL) {
257 return anv_layer.entrypoints[index];
260 switch (devinfo->gen) {
262 if (gen9_layer.entrypoints[index])
263 return gen9_layer.entrypoints[index];
266 if (gen8_layer.entrypoints[index])
267 return gen8_layer.entrypoints[index];
270 if (devinfo->is_haswell && gen75_layer.entrypoints[index])
271 return gen75_layer.entrypoints[index];
273 if (gen7_layer.entrypoints[index])
274 return gen7_layer.entrypoints[index];
277 return anv_layer.entrypoints[index];
279 unreachable("unsupported gen\\n");
284 # Now generate the hash table used for entry point look up. This is a
285 # uint16_t table of entry point indices. We use 0xffff to indicate an entry
286 # in the hash table is empty.
288 map = [NONE
] * HASH_SIZE
289 collisions
= [0] * 10
290 for type, name
, args
, num
, h
, guard
in entrypoints
:
292 while map[h
& HASH_MASK
] != NONE
:
298 collisions
[level
] += 1
299 map[h
& HASH_MASK
] = num
301 print "/* Hash table stats:"
302 print " * size %d entries" % HASH_SIZE
303 print " * collisions entries"
310 print " * %2d%s %4d" % (i
, plus
, collisions
[i
])
313 print "#define none 0x%04x\n" % NONE
315 print "static const uint16_t map[] = {"
316 for i
in xrange(0, HASH_SIZE
, 8):
318 for j
in xrange(i
, i
+ 8):
319 if map[j
] & 0xffff == 0xffff:
322 print "0x%04x," % (map[j
] & 0xffff),
327 # Finally we generate the hash table lookup function. The hash function and
328 # linear probing algorithm matches the hash table generated above.
330 print textwrap
.dedent("""
332 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
334 static const uint32_t prime_factor = %d;
335 static const uint32_t prime_step = %d;
336 const struct anv_entrypoint *e;
341 for (p = name; *p; p++)
342 hash = hash * prime_factor + *p;
351 } while (e->hash != hash);
353 if (strcmp(name, strings + e->name) != 0)
356 return anv_resolve_entrypoint(devinfo, i);
358 """) % (PRIME_FACTOR
, PRIME_STEP
, HASH_MASK
)
362 parser
= argparse
.ArgumentParser()
363 parser
.add_argument('target', choices
=['header', 'code'],
364 help='Which file to generate.')
365 parser
.add_argument('--xml', help='Vulkan API XML file.')
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 hash('vkCreateDmaBufImageINTEL'), None))
381 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
383 if args
.target
== 'header':
384 print TEMPLATE_H
.render(entrypoints
=entrypoints
,
385 filename
=os
.path
.basename(__file__
))
387 gen_code(entrypoints
)
390 if __name__
== '__main__':