anv/entrypoints: Rework #if guards
[mesa.git] / src / intel / vulkan / anv_entrypoints_gen.py
1 # coding=utf-8
2 #
3 # Copyright © 2015 Intel Corporation
4 #
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:
11 #
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
14 # Software.
15 #
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
22 # IN THE SOFTWARE.
23 #
24
25 import fileinput, re, sys
26
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.
29
30 p = re.compile('typedef ([^ ]*) *\((?:VKAPI_PTR)? *\*PFN_vk([^(]*)\)(.*);')
31
32 entrypoints = []
33
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
37 # experimentally.
38
39 none = 0xffff
40 hash_size = 256
41 u32_mask = 2**32 - 1
42 hash_mask = hash_size - 1
43
44 prime_factor = 5024183
45 prime_step = 19
46
47 def hash(name):
48 h = 0;
49 for c in name:
50 h = (h * prime_factor + ord(c)) & u32_mask
51
52 return h
53
54 def get_platform_guard_macro(name):
55 if "Xlib" in name:
56 return "VK_USE_PLATFORM_XLIB_KHR"
57 elif "Xcb" in name:
58 return "VK_USE_PLATFORM_XCB_KHR"
59 elif "Wayland" in name:
60 return "VK_USE_PLATFORM_WAYLAND_KHR"
61 elif "Mir" in name:
62 return "VK_USE_PLATFORM_MIR_KHR"
63 elif "Android" in name:
64 return "VK_USE_PLATFORM_ANDROID_KHR"
65 elif "Win32" in name:
66 return "VK_USE_PLATFORM_WIN32_KHR"
67 else:
68 return None
69
70 def print_guard_start(name):
71 guard = get_platform_guard_macro(name)
72 if guard is not None:
73 print "#ifdef {0}".format(guard)
74
75 def print_guard_end(name):
76 guard = get_platform_guard_macro(name)
77 if guard is not None:
78 print "#endif // {0}".format(guard)
79
80 opt_header = False
81 opt_code = False
82
83 if (sys.argv[1] == "header"):
84 opt_header = True
85 sys.argv.pop()
86 elif (sys.argv[1] == "code"):
87 opt_code = True
88 sys.argv.pop()
89
90 # Parse the entry points in the header
91
92 i = 0
93 for line in fileinput.input():
94 m = p.match(line)
95 if (m):
96 if m.group(2) == 'VoidFunction':
97 continue
98 fullname = "vk" + m.group(2)
99 h = hash(fullname)
100 entrypoints.append((m.group(1), m.group(2), m.group(3), i, h))
101 i = i + 1
102
103 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
104 # per entry point.
105
106 if opt_header:
107 print "/* This file generated from vk_gen.py, don't edit directly. */\n"
108
109 print "struct anv_dispatch_table {"
110 print " union {"
111 print " void *entrypoints[%d];" % len(entrypoints)
112 print " struct {"
113
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)
119 print "#else"
120 print " void *{0};".format(name)
121 print "#endif"
122 else:
123 print " PFN_vk{0} {0};".format(name)
124 print " };\n"
125 print " };\n"
126 print "};\n"
127
128 print "void anv_set_dispatch_devinfo(const struct brw_device_info *info);\n"
129
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)
139 exit()
140
141
142
143 print """/*
144 * Copyright © 2015 Intel Corporation
145 *
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:
152 *
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
155 * Software.
156 *
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
163 * IN THE SOFTWARE.
164 */
165
166 /* DO NOT EDIT! This is a generated file. */
167
168 #include "anv_private.h"
169
170 struct anv_entrypoint {
171 uint32_t name;
172 uint32_t hash;
173 };
174
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.
178 */
179
180 static const char strings[] ="""
181
182 offsets = []
183 i = 0;
184 for type, name, args, num, h in entrypoints:
185 print " \"vk%s\\0\"" % name
186 offsets.append(i)
187 i += 2 + len(name) + 1
188 print """ ;
189
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
193 * entry point.
194 */
195 """
196
197 # Now generate the table of all entry points and their validation functions
198
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)
202 print "};\n"
203
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)
214 print "};\n"
215
216 print """
217 #ifdef DEBUG
218 static bool enable_validate = true;
219 #else
220 static bool enable_validate = false;
221 #endif
222
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
227 * works.
228 */
229 static void __attribute__ ((constructor))
230 determine_validate(void)
231 {
232 const char *s = getenv("ANV_VALIDATE");
233
234 if (s)
235 enable_validate = atoi(s);
236 }
237
238 static const struct brw_device_info *dispatch_devinfo;
239
240 void
241 anv_set_dispatch_devinfo(const struct brw_device_info *devinfo)
242 {
243 dispatch_devinfo = devinfo;
244 }
245
246 void * __attribute__ ((noinline))
247 anv_resolve_entrypoint(uint32_t index)
248 {
249 if (enable_validate && validate_layer.entrypoints[index])
250 return validate_layer.entrypoints[index];
251
252 if (dispatch_devinfo == NULL) {
253 return anv_layer.entrypoints[index];
254 }
255
256 switch (dispatch_devinfo->gen) {
257 case 9:
258 if (gen9_layer.entrypoints[index])
259 return gen9_layer.entrypoints[index];
260 /* fall through */
261 case 8:
262 if (gen8_layer.entrypoints[index])
263 return gen8_layer.entrypoints[index];
264 /* fall through */
265 case 7:
266 if (dispatch_devinfo->is_haswell && gen75_layer.entrypoints[index])
267 return gen75_layer.entrypoints[index];
268
269 if (gen7_layer.entrypoints[index])
270 return gen7_layer.entrypoints[index];
271 /* fall through */
272 case 0:
273 return anv_layer.entrypoints[index];
274 default:
275 unreachable("unsupported gen\\n");
276 }
277 }
278 """
279
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.
283
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)
289
290
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.
294
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:
298 level = 0
299 while map[h & hash_mask] != none:
300 h = h + prime_step
301 level = level + 1
302 if level > 9:
303 collisions[9] += 1
304 else:
305 collisions[level] += 1
306 map[h & hash_mask] = num
307
308 print "/* Hash table stats:"
309 print " * size %d entries" % hash_size
310 print " * collisions entries"
311 for i in xrange(10):
312 if (i == 9):
313 plus = "+"
314 else:
315 plus = " "
316
317 print " * %2d%s %4d" % (i, plus, collisions[i])
318 print " */\n"
319
320 print "#define none 0x%04x\n" % none
321
322 print "static const uint16_t map[] = {"
323 for i in xrange(0, hash_size, 8):
324 print " ",
325 for j in xrange(i, i + 8):
326 if map[j] & 0xffff == 0xffff:
327 print " none,",
328 else:
329 print "0x%04x," % (map[j] & 0xffff),
330 print
331
332 print "};"
333
334 # Finally we generate the hash table lookup function. The hash function and
335 # linear probing algorithm matches the hash table generated above.
336
337 print """
338 void *
339 anv_lookup_entrypoint(const char *name)
340 {
341 static const uint32_t prime_factor = %d;
342 static const uint32_t prime_step = %d;
343 const struct anv_entrypoint *e;
344 uint32_t hash, h, i;
345 const char *p;
346
347 hash = 0;
348 for (p = name; *p; p++)
349 hash = hash * prime_factor + *p;
350
351 h = hash;
352 do {
353 i = map[h & %d];
354 if (i == none)
355 return NULL;
356 e = &entrypoints[i];
357 h += prime_step;
358 } while (e->hash != hash);
359
360 if (strcmp(name, strings + e->name) != 0)
361 return NULL;
362
363 return anv_resolve_entrypoint(i);
364 }
365 """ % (prime_factor, prime_step, hash_mask)