radv: remove the validation layer and some related bits.
[mesa.git] / src / amd / vulkan / radv_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 radv_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 radv_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 for type, name, args, num, h in entrypoints:
129 print_guard_start(name)
130 print "%s radv_%s%s;" % (type, name, args)
131 print_guard_end(name)
132 exit()
133
134
135
136 print """/*
137 * Copyright © 2015 Intel Corporation
138 *
139 * Permission is hereby granted, free of charge, to any person obtaining a
140 * copy of this software and associated documentation files (the "Software"),
141 * to deal in the Software without restriction, including without limitation
142 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
143 * and/or sell copies of the Software, and to permit persons to whom the
144 * Software is furnished to do so, subject to the following conditions:
145 *
146 * The above copyright notice and this permission notice (including the next
147 * paragraph) shall be included in all copies or substantial portions of the
148 * Software.
149 *
150 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
152 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
153 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
154 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
155 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
156 * IN THE SOFTWARE.
157 */
158
159 /* DO NOT EDIT! This is a generated file. */
160
161 #include "radv_private.h"
162
163 struct radv_entrypoint {
164 uint32_t name;
165 uint32_t hash;
166 };
167
168 /* We use a big string constant to avoid lots of reloctions from the entry
169 * point table to lots of little strings. The entries in the entry point table
170 * store the index into this big string.
171 */
172
173 static const char strings[] ="""
174
175 offsets = []
176 i = 0;
177 for type, name, args, num, h in entrypoints:
178 print " \"vk%s\\0\"" % name
179 offsets.append(i)
180 i += 2 + len(name) + 1
181 print " ;"
182
183 # Now generate the table of all entry points
184
185 print "\nstatic const struct radv_entrypoint entrypoints[] = {"
186 for type, name, args, num, h in entrypoints:
187 print " { %5d, 0x%08x }," % (offsets[num], h)
188 print "};\n"
189
190 print """
191
192 /* Weak aliases for all potential implementations. These will resolve to
193 * NULL if they're not defined, which lets the resolve_entrypoint() function
194 * either pick the correct entry point.
195 */
196 """
197
198 for layer in [ "radv" ]:
199 for type, name, args, num, h in entrypoints:
200 print_guard_start(name)
201 print "%s %s_%s%s __attribute__ ((weak));" % (type, layer, name, args)
202 print_guard_end(name)
203 print "\nconst struct radv_dispatch_table %s_layer = {" % layer
204 for type, name, args, num, h in entrypoints:
205 print_guard_start(name)
206 print " .%s = %s_%s," % (name, layer, name)
207 print_guard_end(name)
208 print "};\n"
209
210 print """
211
212 void * __attribute__ ((noinline))
213 radv_resolve_entrypoint(uint32_t index)
214 {
215 return radv_layer.entrypoints[index];
216 }
217 """
218
219 # Now generate the hash table used for entry point look up. This is a
220 # uint16_t table of entry point indices. We use 0xffff to indicate an entry
221 # in the hash table is empty.
222
223 map = [none for f in xrange(hash_size)]
224 collisions = [0 for f in xrange(10)]
225 for type, name, args, num, h in entrypoints:
226 level = 0
227 while map[h & hash_mask] != none:
228 h = h + prime_step
229 level = level + 1
230 if level > 9:
231 collisions[9] += 1
232 else:
233 collisions[level] += 1
234 map[h & hash_mask] = num
235
236 print "/* Hash table stats:"
237 print " * size %d entries" % hash_size
238 print " * collisions entries"
239 for i in xrange(10):
240 if (i == 9):
241 plus = "+"
242 else:
243 plus = " "
244
245 print " * %2d%s %4d" % (i, plus, collisions[i])
246 print " */\n"
247
248 print "#define none 0x%04x\n" % none
249
250 print "static const uint16_t map[] = {"
251 for i in xrange(0, hash_size, 8):
252 print " ",
253 for j in xrange(i, i + 8):
254 if map[j] & 0xffff == 0xffff:
255 print " none,",
256 else:
257 print "0x%04x," % (map[j] & 0xffff),
258 print
259
260 print "};"
261
262 # Finally we generate the hash table lookup function. The hash function and
263 # linear probing algorithm matches the hash table generated above.
264
265 print """
266 void *
267 radv_lookup_entrypoint(const char *name)
268 {
269 static const uint32_t prime_factor = %d;
270 static const uint32_t prime_step = %d;
271 const struct radv_entrypoint *e;
272 uint32_t hash, h, i;
273 const char *p;
274
275 hash = 0;
276 for (p = name; *p; p++)
277 hash = hash * prime_factor + *p;
278
279 h = hash;
280 do {
281 i = map[h & %d];
282 if (i == none)
283 return NULL;
284 e = &entrypoints[i];
285 h += prime_step;
286 } while (e->hash != hash);
287
288 if (strcmp(name, strings + e->name) != 0)
289 return NULL;
290
291 return radv_resolve_entrypoint(i);
292 }
293 """ % (prime_factor, prime_step, hash_mask)