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