anv: let anv_entrypoints_gen.py generate proper Wayland/Xcb 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 print_guard_start(name):
55 if "Wayland" in name:
56 print "#ifdef VK_USE_PLATFORM_WAYLAND_KHR"
57 if "Xcb" in name:
58 print "#ifdef VK_USE_PLATFORM_XCB_KHR"
59 return
60
61 def print_guard_end(name):
62 if "Wayland" in name:
63 print "#endif // VK_USE_PLATFORM_WAYLAND_KHR"
64 if "Xcb" in name:
65 print "#endif // VK_USE_PLATFORM_XCB_KHR"
66 return
67
68 opt_header = False
69 opt_code = False
70
71 if (sys.argv[1] == "header"):
72 opt_header = True
73 sys.argv.pop()
74 elif (sys.argv[1] == "code"):
75 opt_code = True
76 sys.argv.pop()
77
78 # Parse the entry points in the header
79
80 i = 0
81 for line in fileinput.input():
82 m = p.match(line)
83 if (m):
84 if m.group(2) == 'VoidFunction':
85 continue
86 fullname = "vk" + m.group(2)
87 h = hash(fullname)
88 entrypoints.append((m.group(1), m.group(2), m.group(3), i, h))
89 i = i + 1
90
91 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
92 # per entry point.
93
94 if opt_header:
95 print "/* This file generated from vk_gen.py, don't edit directly. */\n"
96
97 print "struct anv_dispatch_table {"
98 print " union {"
99 print " void *entrypoints[%d];" % len(entrypoints)
100 print " struct {"
101
102 for type, name, args, num, h in entrypoints:
103 print_guard_start(name)
104 print " %s (*%s)%s;" % (type, name, args)
105 print_guard_end(name)
106 print " };\n"
107 print " };\n"
108 print "};\n"
109
110 print "void anv_set_dispatch_devinfo(const struct brw_device_info *info);\n"
111
112 for type, name, args, num, h in entrypoints:
113 print_guard_start(name)
114 print "%s anv_%s%s;" % (type, name, args)
115 print "%s gen7_%s%s;" % (type, name, args)
116 print "%s gen75_%s%s;" % (type, name, args)
117 print "%s gen8_%s%s;" % (type, name, args)
118 print "%s gen9_%s%s;" % (type, name, args)
119 print "%s anv_validate_%s%s;" % (type, name, args)
120 print_guard_end(name)
121 exit()
122
123
124
125 print """/*
126 * Copyright © 2015 Intel Corporation
127 *
128 * Permission is hereby granted, free of charge, to any person obtaining a
129 * copy of this software and associated documentation files (the "Software"),
130 * to deal in the Software without restriction, including without limitation
131 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
132 * and/or sell copies of the Software, and to permit persons to whom the
133 * Software is furnished to do so, subject to the following conditions:
134 *
135 * The above copyright notice and this permission notice (including the next
136 * paragraph) shall be included in all copies or substantial portions of the
137 * Software.
138 *
139 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
141 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
142 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
143 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
144 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
145 * IN THE SOFTWARE.
146 */
147
148 /* DO NOT EDIT! This is a generated file. */
149
150 #include "anv_private.h"
151
152 struct anv_entrypoint {
153 uint32_t name;
154 uint32_t hash;
155 };
156
157 /* We use a big string constant to avoid lots of reloctions from the entry
158 * point table to lots of little strings. The entries in the entry point table
159 * store the index into this big string.
160 */
161
162 static const char strings[] ="""
163
164 offsets = []
165 i = 0;
166 for type, name, args, num, h in entrypoints:
167 print_guard_start(name)
168 print " \"vk%s\\0\"" % name
169 offsets.append(i)
170 i += 2 + len(name) + 1
171 print_guard_end(name)
172 print """ ;
173
174 /* Weak aliases for all potential validate functions. These will resolve to
175 * NULL if they're not defined, which lets the resolve_entrypoint() function
176 * either pick a validate wrapper if available or just plug in the actual
177 * entry point.
178 */
179 """
180
181 # Now generate the table of all entry points and their validation functions
182
183 print "\nstatic const struct anv_entrypoint entrypoints[] = {"
184 for type, name, args, num, h in entrypoints:
185 print_guard_start(name)
186 print " { %5d, 0x%08x }," % (offsets[num], h)
187 print_guard_end(name)
188 print "};\n"
189
190 for layer in [ "anv", "validate", "gen7", "gen75", "gen8", "gen9" ]:
191 for type, name, args, num, h in entrypoints:
192 print_guard_start(name)
193 print "%s %s_%s%s __attribute__ ((weak));" % (type, layer, name, args)
194 print_guard_end(name)
195 print "\nconst struct anv_dispatch_table %s_layer = {" % layer
196 for type, name, args, num, h in entrypoints:
197 print_guard_start(name)
198 print " .%s = %s_%s," % (name, layer, name)
199 print_guard_end(name)
200 print "};\n"
201
202 print """
203 #ifdef DEBUG
204 static bool enable_validate = true;
205 #else
206 static bool enable_validate = false;
207 #endif
208
209 /* We can't use symbols that need resolving (like, oh, getenv) in the resolve
210 * function. This means that we have to determine whether or not to use the
211 * validation layer sometime before that. The constructor function attribute asks
212 * the dynamic linker to invoke determine_validate() at dlopen() time which
213 * works.
214 */
215 static void __attribute__ ((constructor))
216 determine_validate(void)
217 {
218 const char *s = getenv("ANV_VALIDATE");
219
220 if (s)
221 enable_validate = atoi(s);
222 }
223
224 static const struct brw_device_info *dispatch_devinfo;
225
226 void
227 anv_set_dispatch_devinfo(const struct brw_device_info *devinfo)
228 {
229 dispatch_devinfo = devinfo;
230 }
231
232 void * __attribute__ ((noinline))
233 anv_resolve_entrypoint(uint32_t index)
234 {
235 if (enable_validate && validate_layer.entrypoints[index])
236 return validate_layer.entrypoints[index];
237
238 if (dispatch_devinfo == NULL) {
239 return anv_layer.entrypoints[index];
240 }
241
242 switch (dispatch_devinfo->gen) {
243 case 9:
244 if (gen9_layer.entrypoints[index])
245 return gen9_layer.entrypoints[index];
246 /* fall through */
247 case 8:
248 if (gen8_layer.entrypoints[index])
249 return gen8_layer.entrypoints[index];
250 /* fall through */
251 case 7:
252 if (dispatch_devinfo->is_haswell && gen75_layer.entrypoints[index])
253 return gen75_layer.entrypoints[index];
254
255 if (gen7_layer.entrypoints[index])
256 return gen7_layer.entrypoints[index];
257 /* fall through */
258 case 0:
259 return anv_layer.entrypoints[index];
260 default:
261 unreachable("unsupported gen\\n");
262 }
263 }
264 """
265
266 # Now output ifuncs and their resolve helpers for all entry points. The
267 # resolve helper calls resolve_entrypoint() with the entry point index, which
268 # lets the resolver look it up in the table.
269
270 for type, name, args, num, h in entrypoints:
271 print_guard_start(name)
272 print "static void *resolve_%s(void) { return anv_resolve_entrypoint(%d); }" % (name, num)
273 print "%s vk%s%s\n __attribute__ ((ifunc (\"resolve_%s\"), visibility (\"default\")));\n" % (type, name, args, name)
274 print_guard_end(name)
275
276
277 # Now generate the hash table used for entry point look up. This is a
278 # uint16_t table of entry point indices. We use 0xffff to indicate an entry
279 # in the hash table is empty.
280
281 map = [none for f in xrange(hash_size)]
282 collisions = [0 for f in xrange(10)]
283 for type, name, args, num, h in entrypoints:
284 level = 0
285 while map[h & hash_mask] != none:
286 h = h + prime_step
287 level = level + 1
288 if level > 9:
289 collisions[9] += 1
290 else:
291 collisions[level] += 1
292 map[h & hash_mask] = num
293
294 print "/* Hash table stats:"
295 print " * size %d entries" % hash_size
296 print " * collisions entries"
297 for i in xrange(10):
298 if (i == 9):
299 plus = "+"
300 else:
301 plus = " "
302
303 print " * %2d%s %4d" % (i, plus, collisions[i])
304 print " */\n"
305
306 print "#define none 0x%04x\n" % none
307
308 print "static const uint16_t map[] = {"
309 for i in xrange(0, hash_size, 8):
310 print " ",
311 for j in xrange(i, i + 8):
312 if map[j] & 0xffff == 0xffff:
313 print " none,",
314 else:
315 print "0x%04x," % (map[j] & 0xffff),
316 print
317
318 print "};"
319
320 # Finally we generate the hash table lookup function. The hash function and
321 # linear probing algorithm matches the hash table generated above.
322
323 print """
324 void *
325 anv_lookup_entrypoint(const char *name)
326 {
327 static const uint32_t prime_factor = %d;
328 static const uint32_t prime_step = %d;
329 const struct anv_entrypoint *e;
330 uint32_t hash, h, i;
331 const char *p;
332
333 hash = 0;
334 for (p = name; *p; p++)
335 hash = hash * prime_factor + *p;
336
337 h = hash;
338 do {
339 i = map[h & %d];
340 if (i == none)
341 return NULL;
342 e = &entrypoints[i];
343 h += prime_step;
344 } while (e->hash != hash);
345
346 if (strcmp(name, strings + e->name) != 0)
347 return NULL;
348
349 return anv_resolve_entrypoint(i);
350 }
351 """ % (prime_factor, prime_step, hash_mask)