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 brw_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 "%s anv_validate_%s%s;" % (type, name, args)
138 print_guard_end(name)
144 * Copyright © 2015 Intel Corporation
146 * Permission is hereby granted, free of charge, to any person obtaining a
147 * copy of this software and associated documentation files (the "Software"),
148 * to deal in the Software without restriction, including without limitation
149 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
150 * and/or sell copies of the Software, and to permit persons to whom the
151 * Software is furnished to do so, subject to the following conditions:
153 * The above copyright notice and this permission notice (including the next
154 * paragraph) shall be included in all copies or substantial portions of the
157 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
159 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
160 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
161 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
162 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
166 /* DO NOT EDIT! This is a generated file. */
168 #include "anv_private.h"
170 struct anv_entrypoint {
175 /* We use a big string constant to avoid lots of reloctions from the entry
176 * point table to lots of little strings. The entries in the entry point table
177 * store the index into this big string.
180 static const char strings[] ="""
184 for type, name, args, num, h in entrypoints:
185 print " \"vk%s\\0\"" % name
187 i += 2 + len(name) + 1
190 /* Weak aliases for all potential validate functions. These will resolve to
191 * NULL if they're not defined, which lets the resolve_entrypoint() function
192 * either pick a validate wrapper if available or just plug in the actual
197 # Now generate the table of all entry points and their validation functions
199 print "\nstatic const struct anv_entrypoint entrypoints[] = {"
200 for type, name, args, num, h in entrypoints:
201 print " { %5d, 0x%08x }," % (offsets[num], h)
204 for layer in [ "anv", "validate", "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)
218 static bool enable_validate = true;
220 static bool enable_validate = false;
223 /* We can't use symbols that need resolving (like, oh, getenv) in the resolve
224 * function. This means that we have to determine whether or not to use the
225 * validation layer sometime before that. The constructor function attribute asks
226 * the dynamic linker to invoke determine_validate() at dlopen() time which
229 static void __attribute__ ((constructor))
230 determine_validate(void)
232 const char *s = getenv("ANV_VALIDATE");
235 enable_validate = atoi(s);
238 static const struct brw_device_info *dispatch_devinfo;
241 anv_set_dispatch_devinfo(const struct brw_device_info *devinfo)
243 dispatch_devinfo = devinfo;
246 void * __attribute__ ((noinline))
247 anv_resolve_entrypoint(uint32_t index)
249 if (enable_validate && validate_layer.entrypoints[index])
250 return validate_layer.entrypoints[index];
252 if (dispatch_devinfo == NULL) {
253 return anv_layer.entrypoints[index];
256 switch (dispatch_devinfo->gen) {
258 if (gen9_layer.entrypoints[index])
259 return gen9_layer.entrypoints[index];
262 if (gen8_layer.entrypoints[index])
263 return gen8_layer.entrypoints[index];
266 if (dispatch_devinfo->is_haswell && gen75_layer.entrypoints[index])
267 return gen75_layer.entrypoints[index];
269 if (gen7_layer.entrypoints[index])
270 return gen7_layer.entrypoints[index];
273 return anv_layer.entrypoints[index];
275 unreachable("unsupported gen\\n");
280 # Now output ifuncs and their resolve helpers for all entry points. The
281 # resolve helper calls resolve_entrypoint() with the entry point index, which
282 # lets the resolver look it up in the table.
284 for type, name, args, num, h in entrypoints:
285 print_guard_start(name)
286 print "static void *resolve_%s(void) { return anv_resolve_entrypoint(%d); }" % (name, num)
287 print "%s vk%s%s\n __attribute__ ((ifunc (\"resolve_%s\"), visibility (\"default\")));\n" % (type, name, args, name)
288 print_guard_end(name)
291 # Now generate the hash table used for entry point look up. This is a
292 # uint16_t table of entry point indices. We use 0xffff to indicate an entry
293 # in the hash table is empty.
295 map = [none for f in xrange(hash_size)]
296 collisions = [0 for f in xrange(10)]
297 for type, name, args, num, h in entrypoints:
299 while map[h & hash_mask] != none:
305 collisions[level] += 1
306 map[h & hash_mask] = num
308 print "/* Hash table stats:"
309 print " * size %d entries" % hash_size
310 print " * collisions entries"
317 print " * %2d%s %4d" % (i, plus, collisions[i])
320 print "#define none 0x%04x\n" % none
322 print "static const uint16_t map[] = {"
323 for i in xrange(0, hash_size, 8):
325 for j in xrange(i, i + 8):
326 if map[j] & 0xffff == 0xffff:
329 print "0x%04x," % (map[j] & 0xffff),
334 # Finally we generate the hash table lookup function. The hash function and
335 # linear probing algorithm matches the hash table generated above.
339 anv_lookup_entrypoint(const char *name)
341 static const uint32_t prime_factor = %d;
342 static const uint32_t prime_step = %d;
343 const struct anv_entrypoint *e;
348 for (p = name; *p; p++)
349 hash = hash * prime_factor + *p;
358 } while (e->hash != hash);
360 if (strcmp(name, strings + e->name) != 0)
363 return anv_resolve_entrypoint(i);
365 """ % (prime_factor, prime_step, hash_mask)