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
57 if (sys
.argv
[1] == "header"):
60 elif (sys
.argv
[1] == "code"):
64 # Parse the entry points in the header
67 for line
in fileinput
.input():
70 if m
.group(2) == 'VoidFunction':
72 fullname
= "vk" + m
.group(2)
74 entrypoints
.append((m
.group(1), m
.group(2), m
.group(3), i
, h
))
77 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
81 print "/* This file generated from vk_gen.py, don't edit directly. */\n"
83 print "struct anv_dispatch_table {"
85 print " void *entrypoints[%d];" % len(entrypoints
)
88 for type, name
, args
, num
, h
in entrypoints
:
89 print " %s (*%s)%s;" % (type, name
, args
)
94 print "void anv_set_dispatch_devinfo(const struct brw_device_info *info);\n"
96 for type, name
, args
, num
, h
in entrypoints
:
97 print "%s anv_%s%s;" % (type, name
, args
)
98 print "%s gen7_%s%s;" % (type, name
, args
)
99 print "%s gen75_%s%s;" % (type, name
, args
)
100 print "%s gen8_%s%s;" % (type, name
, args
)
101 print "%s gen9_%s%s;" % (type, name
, args
)
102 print "%s anv_validate_%s%s;" % (type, name
, args
)
108 * Copyright © 2015 Intel Corporation
110 * Permission is hereby granted, free of charge, to any person obtaining a
111 * copy of this software and associated documentation files (the "Software"),
112 * to deal in the Software without restriction, including without limitation
113 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
114 * and/or sell copies of the Software, and to permit persons to whom the
115 * Software is furnished to do so, subject to the following conditions:
117 * The above copyright notice and this permission notice (including the next
118 * paragraph) shall be included in all copies or substantial portions of the
121 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
122 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
123 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
124 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
125 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
126 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
130 /* DO NOT EDIT! This is a generated file. */
132 #include "anv_private.h"
134 struct anv_entrypoint {
139 /* We use a big string constant to avoid lots of reloctions from the entry
140 * point table to lots of little strings. The entries in the entry point table
141 * store the index into this big string.
144 static const char strings[] ="""
148 for type, name
, args
, num
, h
in entrypoints
:
149 print " \"vk%s\\0\"" % name
151 i
+= 2 + len(name
) + 1
154 /* Weak aliases for all potential validate functions. These will resolve to
155 * NULL if they're not defined, which lets the resolve_entrypoint() function
156 * either pick a validate wrapper if available or just plug in the actual
161 # Now generate the table of all entry points and their validation functions
163 print "\nstatic const struct anv_entrypoint entrypoints[] = {"
164 for type, name
, args
, num
, h
in entrypoints
:
165 print " { %5d, 0x%08x }," % (offsets
[num
], h
)
168 for layer
in [ "anv", "validate", "gen7", "gen75", "gen8", "gen9" ]:
169 for type, name
, args
, num
, h
in entrypoints
:
170 print "%s %s_%s%s __attribute__ ((weak));" % (type, layer
, name
, args
)
171 print "\nconst struct anv_dispatch_table %s_layer = {" % layer
172 for type, name
, args
, num
, h
in entrypoints
:
173 print " .%s = %s_%s," % (name
, layer
, name
)
178 static bool enable_validate = true;
180 static bool enable_validate = false;
183 /* We can't use symbols that need resolving (like, oh, getenv) in the resolve
184 * function. This means that we have to determine whether or not to use the
185 * validation layer sometime before that. The constructor function attribute asks
186 * the dynamic linker to invoke determine_validate() at dlopen() time which
189 static void __attribute__ ((constructor))
190 determine_validate(void)
192 const char *s = getenv("ANV_VALIDATE");
195 enable_validate = atoi(s);
198 static const struct brw_device_info *dispatch_devinfo;
201 anv_set_dispatch_devinfo(const struct brw_device_info *devinfo)
203 dispatch_devinfo = devinfo;
206 void * __attribute__ ((noinline))
207 anv_resolve_entrypoint(uint32_t index)
209 if (enable_validate && validate_layer.entrypoints[index])
210 return validate_layer.entrypoints[index];
212 if (dispatch_devinfo == NULL) {
213 assert(anv_layer.entrypoints[index]);
214 return anv_layer.entrypoints[index];
217 switch (dispatch_devinfo->gen) {
219 if (gen9_layer.entrypoints[index])
220 return gen9_layer.entrypoints[index];
223 if (gen8_layer.entrypoints[index])
224 return gen8_layer.entrypoints[index];
227 if (dispatch_devinfo->is_haswell && gen75_layer.entrypoints[index])
228 return gen75_layer.entrypoints[index];
230 if (gen7_layer.entrypoints[index])
231 return gen7_layer.entrypoints[index];
234 return anv_layer.entrypoints[index];
236 unreachable("unsupported gen\\n");
241 # Now output ifuncs and their resolve helpers for all entry points. The
242 # resolve helper calls resolve_entrypoint() with the entry point index, which
243 # lets the resolver look it up in the table.
245 for type, name
, args
, num
, h
in entrypoints
:
246 print "static void *resolve_%s(void) { return anv_resolve_entrypoint(%d); }" % (name
, num
)
247 print "%s vk%s%s\n __attribute__ ((ifunc (\"resolve_%s\"), visibility (\"default\")));\n" % (type, name
, args
, name
)
250 # Now generate the hash table used for entry point look up. This is a
251 # uint16_t table of entry point indices. We use 0xffff to indicate an entry
252 # in the hash table is empty.
254 map = [none
for f
in xrange(hash_size
)]
255 collisions
= [0 for f
in xrange(10)]
256 for type, name
, args
, num
, h
in entrypoints
:
258 while map[h
& hash_mask
] != none
:
264 collisions
[level
] += 1
265 map[h
& hash_mask
] = num
267 print "/* Hash table stats:"
268 print " * size %d entries" % hash_size
269 print " * collisions entries"
276 print " * %2d%s %4d" % (i
, plus
, collisions
[i
])
279 print "#define none 0x%04x\n" % none
281 print "static const uint16_t map[] = {"
282 for i
in xrange(0, hash_size
, 8):
284 for j
in xrange(i
, i
+ 8):
285 if map[j
] & 0xffff == 0xffff:
288 print "0x%04x," % (map[j
] & 0xffff),
293 # Finally we generate the hash table lookup function. The hash function and
294 # linear probing algorithm matches the hash table generated above.
298 anv_lookup_entrypoint(const char *name)
300 static const uint32_t prime_factor = %d;
301 static const uint32_t prime_step = %d;
302 const struct anv_entrypoint *e;
307 for (p = name; *p; p++)
308 hash = hash * prime_factor + *p;
317 } while (e->hash != hash);
319 if (strcmp(name, strings + e->name) != 0)
322 return anv_resolve_entrypoint(i);
324 """ % (prime_factor
, prime_step
, hash_mask
)