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