3 # Copyright © 2015 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
25 import fileinput
, re
, sys
27 # Each function typedef in the vulkan.h header is all on one line and matches
28 # this regepx. We hope that won't change.
30 p
= re
.compile('typedef ([^ ]*) *\((?:VKAPI_PTR)? *\*PFN_vk([^(]*)\)(.*);')
34 # We generate a static hash table for entry point lookup
35 # (vkGetProcAddress). We use a linear congruential generator for our hash
36 # function and a power-of-two size table. The prime numbers are determined
42 hash_mask
= hash_size
- 1
44 prime_factor
= 5024183
50 h
= (h
* prime_factor
+ ord(c
)) & u32_mask
54 def get_platform_guard_macro(name
):
56 return "VK_USE_PLATFORM_XLIB_KHR"
58 return "VK_USE_PLATFORM_XCB_KHR"
59 elif "Wayland" in name
:
60 return "VK_USE_PLATFORM_WAYLAND_KHR"
62 return "VK_USE_PLATFORM_MIR_KHR"
63 elif "Android" in name
:
64 return "VK_USE_PLATFORM_ANDROID_KHR"
66 return "VK_USE_PLATFORM_WIN32_KHR"
70 def print_guard_start(name
):
71 guard
= get_platform_guard_macro(name
)
73 print "#ifdef {0}".format(guard
)
75 def print_guard_end(name
):
76 guard
= get_platform_guard_macro(name
)
78 print "#endif // {0}".format(guard
)
83 if (sys
.argv
[1] == "header"):
86 elif (sys
.argv
[1] == "code"):
90 # Parse the entry points in the header
93 for line
in fileinput
.input():
96 if m
.group(2) == 'VoidFunction':
98 fullname
= "vk" + m
.group(2)
100 entrypoints
.append((m
.group(1), m
.group(2), m
.group(3), i
, h
))
103 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
107 print "/* This file generated from vk_gen.py, don't edit directly. */\n"
109 print "struct anv_dispatch_table {"
111 print " void *entrypoints[%d];" % len(entrypoints
)
114 for type, name
, args
, num
, h
in entrypoints
:
115 guard
= get_platform_guard_macro(name
)
116 if guard
is not None:
117 print "#ifdef {0}".format(guard
)
118 print " PFN_vk{0} {0};".format(name
)
120 print " void *{0};".format(name
)
123 print " PFN_vk{0} {0};".format(name
)
128 print "void anv_set_dispatch_devinfo(const struct gen_device_info *info);\n"
130 for type, name
, args
, num
, h
in entrypoints
:
131 print_guard_start(name
)
132 print "%s anv_%s%s;" % (type, name
, args
)
133 print "%s gen7_%s%s;" % (type, name
, args
)
134 print "%s gen75_%s%s;" % (type, name
, args
)
135 print "%s gen8_%s%s;" % (type, name
, args
)
136 print "%s gen9_%s%s;" % (type, name
, args
)
137 print_guard_end(name
)
143 * Copyright © 2015 Intel Corporation
145 * Permission is hereby granted, free of charge, to any person obtaining a
146 * copy of this software and associated documentation files (the "Software"),
147 * to deal in the Software without restriction, including without limitation
148 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
149 * and/or sell copies of the Software, and to permit persons to whom the
150 * Software is furnished to do so, subject to the following conditions:
152 * The above copyright notice and this permission notice (including the next
153 * paragraph) shall be included in all copies or substantial portions of the
156 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
157 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
158 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
159 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
160 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
161 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
165 /* DO NOT EDIT! This is a generated file. */
167 #include "anv_private.h"
169 struct anv_entrypoint {
174 /* We use a big string constant to avoid lots of reloctions from the entry
175 * point table to lots of little strings. The entries in the entry point table
176 * store the index into this big string.
179 static const char strings[] ="""
183 for type, name
, args
, num
, h
in entrypoints
:
184 print " \"vk%s\\0\"" % name
186 i
+= 2 + len(name
) + 1
189 # Now generate the table of all entry points
191 print "\nstatic const struct anv_entrypoint entrypoints[] = {"
192 for type, name
, args
, num
, h
in entrypoints
:
193 print " { %5d, 0x%08x }," % (offsets
[num
], h
)
198 /* Weak aliases for all potential implementations. These will resolve to
199 * NULL if they're not defined, which lets the resolve_entrypoint() function
200 * either pick the correct entry point.
204 for layer
in [ "anv", "gen7", "gen75", "gen8", "gen9" ]:
205 for type, name
, args
, num
, h
in entrypoints
:
206 print_guard_start(name
)
207 print "%s %s_%s%s __attribute__ ((weak));" % (type, layer
, name
, args
)
208 print_guard_end(name
)
209 print "\nconst struct anv_dispatch_table %s_layer = {" % layer
210 for type, name
, args
, num
, h
in entrypoints
:
211 print_guard_start(name
)
212 print " .%s = %s_%s," % (name
, layer
, name
)
213 print_guard_end(name
)
217 static void * __attribute__ ((noinline))
218 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
220 if (devinfo == NULL) {
221 return anv_layer.entrypoints[index];
224 switch (devinfo->gen) {
226 if (gen9_layer.entrypoints[index])
227 return gen9_layer.entrypoints[index];
230 if (gen8_layer.entrypoints[index])
231 return gen8_layer.entrypoints[index];
234 if (devinfo->is_haswell && gen75_layer.entrypoints[index])
235 return gen75_layer.entrypoints[index];
237 if (gen7_layer.entrypoints[index])
238 return gen7_layer.entrypoints[index];
241 return anv_layer.entrypoints[index];
243 unreachable("unsupported gen\\n");
248 # Now generate the hash table used for entry point look up. This is a
249 # uint16_t table of entry point indices. We use 0xffff to indicate an entry
250 # in the hash table is empty.
252 map = [none
for f
in xrange(hash_size
)]
253 collisions
= [0 for f
in xrange(10)]
254 for type, name
, args
, num
, h
in entrypoints
:
256 while map[h
& hash_mask
] != none
:
262 collisions
[level
] += 1
263 map[h
& hash_mask
] = num
265 print "/* Hash table stats:"
266 print " * size %d entries" % hash_size
267 print " * collisions entries"
274 print " * %2d%s %4d" % (i
, plus
, collisions
[i
])
277 print "#define none 0x%04x\n" % none
279 print "static const uint16_t map[] = {"
280 for i
in xrange(0, hash_size
, 8):
282 for j
in xrange(i
, i
+ 8):
283 if map[j
] & 0xffff == 0xffff:
286 print "0x%04x," % (map[j
] & 0xffff),
291 # Finally we generate the hash table lookup function. The hash function and
292 # linear probing algorithm matches the hash table generated above.
296 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
298 static const uint32_t prime_factor = %d;
299 static const uint32_t prime_step = %d;
300 const struct anv_entrypoint *e;
305 for (p = name; *p; p++)
306 hash = hash * prime_factor + *p;
315 } while (e->hash != hash);
317 if (strcmp(name, strings + e->name) != 0)
320 return anv_resolve_entrypoint(devinfo, i);
322 """ % (prime_factor
, prime_step
, hash_mask
)