7dd98f960cea07a058d94516e8523bf2952fd6e3
[mesa.git] / src / intel / vulkan / anv_entrypoints_gen.py
1 # coding=utf-8
2 #
3 # Copyright © 2015, 2017 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 argparse
26 import os
27 import textwrap
28 import xml.etree.ElementTree as et
29
30 from mako.template import Template
31
32 MAX_API_VERSION = 1.0
33
34 SUPPORTED_EXTENSIONS = [
35 'VK_KHR_descriptor_update_template',
36 'VK_KHR_get_physical_device_properties2',
37 'VK_KHR_maintenance1',
38 'VK_KHR_push_descriptor',
39 'VK_KHR_sampler_mirror_clamp_to_edge',
40 'VK_KHR_shader_draw_parameters',
41 'VK_KHR_surface',
42 'VK_KHR_swapchain',
43 'VK_KHR_wayland_surface',
44 'VK_KHR_xcb_surface',
45 'VK_KHR_xlib_surface',
46 ]
47
48 # We generate a static hash table for entry point lookup
49 # (vkGetProcAddress). We use a linear congruential generator for our hash
50 # function and a power-of-two size table. The prime numbers are determined
51 # experimentally.
52
53 TEMPLATE_H = Template(textwrap.dedent("""\
54 /* This file generated from ${filename}, don't edit directly. */
55
56 struct anv_dispatch_table {
57 union {
58 void *entrypoints[${len(entrypoints)}];
59 struct {
60 % for _, name, _, _, _, guard in entrypoints:
61 % if guard is not None:
62 #ifdef ${guard}
63 PFN_vk${name} ${name};
64 #else
65 void *${name};
66 # endif
67 % else:
68 PFN_vk${name} ${name};
69 % endif
70 % endfor
71 };
72 };
73 };
74
75 void anv_set_dispatch_devinfo(const struct gen_device_info *info);
76 % for type_, name, args, num, h, guard in entrypoints:
77 % if guard is not None:
78 #ifdef ${guard}
79 % endif
80 ${type_} anv_${name}(${args});
81 ${type_} gen7_${name}(${args});
82 ${type_} gen75_${name}(${args});
83 ${type_} gen8_${name}(${args});
84 ${type_} gen9_${name}(${args});
85 % if guard is not None:
86 #endif // ${guard}
87 % endif
88 % endfor
89 """))
90
91 NONE = 0xffff
92 HASH_SIZE = 256
93 U32_MASK = 2**32 - 1
94 HASH_MASK = HASH_SIZE - 1
95
96 PRIME_FACTOR = 5024183
97 PRIME_STEP = 19
98
99 opt_header = False
100 opt_code = False
101
102
103 def hash(name):
104 h = 0
105 for c in name:
106 h = (h * PRIME_FACTOR + ord(c)) & U32_MASK
107
108 return h
109
110
111 def print_guard_start(guard):
112 if guard is not None:
113 print "#ifdef {0}".format(guard)
114
115
116 def print_guard_end(guard):
117 if guard is not None:
118 print "#endif // {0}".format(guard)
119
120
121 def get_entrypoints(doc, entrypoints_to_defines):
122 """Extract the entry points from the registry."""
123 entrypoints = []
124
125 enabled_commands = set()
126 for feature in doc.findall('./feature'):
127 assert feature.attrib['api'] == 'vulkan'
128 if float(feature.attrib['number']) > MAX_API_VERSION:
129 continue
130
131 for command in feature.findall('./require/command'):
132 enabled_commands.add(command.attrib['name'])
133
134 for extension in doc.findall('.extensions/extension'):
135 if extension.attrib['name'] not in SUPPORTED_EXTENSIONS:
136 continue
137
138 assert extension.attrib['supported'] == 'vulkan'
139 for command in extension.findall('./require/command'):
140 enabled_commands.add(command.attrib['name'])
141
142 index = 0
143 for command in doc.findall('./commands/command'):
144 type = command.find('./proto/type').text
145 fullname = command.find('./proto/name').text
146
147 if fullname not in enabled_commands:
148 continue
149
150 shortname = fullname[2:]
151 params = (''.join(p.itertext()) for p in command.findall('./param'))
152 params = ', '.join(params)
153 if fullname in entrypoints_to_defines:
154 guard = entrypoints_to_defines[fullname]
155 else:
156 guard = None
157 entrypoints.append((type, shortname, params, index, hash(fullname), guard))
158 index += 1
159
160 return entrypoints
161
162
163 def get_entrypoints_defines(doc):
164 """Maps entry points to extension defines."""
165 entrypoints_to_defines = {}
166 extensions = doc.findall('./extensions/extension')
167 for extension in extensions:
168 define = extension.get('protect')
169 entrypoints = extension.findall('./require/command')
170 for entrypoint in entrypoints:
171 fullname = entrypoint.get('name')
172 entrypoints_to_defines[fullname] = define
173 return entrypoints_to_defines
174
175
176 def gen_code(entrypoints):
177 print textwrap.dedent("""\
178 /*
179 * Copyright © 2015 Intel Corporation
180 *
181 * Permission is hereby granted, free of charge, to any person obtaining a
182 * copy of this software and associated documentation files (the "Software"),
183 * to deal in the Software without restriction, including without limitation
184 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
185 * and/or sell copies of the Software, and to permit persons to whom the
186 * Software is furnished to do so, subject to the following conditions:
187 *
188 * The above copyright notice and this permission notice (including the next
189 * paragraph) shall be included in all copies or substantial portions of the
190 * Software.
191 *
192 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
193 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
194 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
195 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
196 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
197 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
198 * IN THE SOFTWARE.
199 */
200
201 /* This file generated from {}, don't edit directly. */
202
203 #include "anv_private.h"
204
205 struct anv_entrypoint {{
206 uint32_t name;
207 uint32_t hash;
208 }};
209
210 /* We use a big string constant to avoid lots of reloctions from the entry
211 * point table to lots of little strings. The entries in the entry point table
212 * store the index into this big string.
213 */
214
215 static const char strings[] =""".format(os.path.basename(__file__)))
216
217 offsets = []
218 i = 0
219 for type, name, args, num, h, guard in entrypoints:
220 print " \"vk%s\\0\"" % name
221 offsets.append(i)
222 i += 2 + len(name) + 1
223 print " ;"
224
225 # Now generate the table of all entry points
226
227 print "\nstatic const struct anv_entrypoint entrypoints[] = {"
228 for type, name, args, num, h, guard in entrypoints:
229 print " { %5d, 0x%08x }," % (offsets[num], h)
230 print "};\n"
231
232 print textwrap.dedent("""
233
234 /* Weak aliases for all potential implementations. These will resolve to
235 * NULL if they're not defined, which lets the resolve_entrypoint() function
236 * either pick the correct entry point.
237 */
238 """)
239
240 for layer in ["anv", "gen7", "gen75", "gen8", "gen9"]:
241 for type, name, args, num, h, guard in entrypoints:
242 print_guard_start(guard)
243 print "%s %s_%s(%s) __attribute__ ((weak));" % (type, layer, name, args)
244 print_guard_end(guard)
245 print "\nconst struct anv_dispatch_table %s_layer = {" % layer
246 for type, name, args, num, h, guard in entrypoints:
247 print_guard_start(guard)
248 print " .%s = %s_%s," % (name, layer, name)
249 print_guard_end(guard)
250 print "};\n"
251
252 print textwrap.dedent("""
253 static void * __attribute__ ((noinline))
254 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
255 {
256 if (devinfo == NULL) {
257 return anv_layer.entrypoints[index];
258 }
259
260 switch (devinfo->gen) {
261 case 9:
262 if (gen9_layer.entrypoints[index])
263 return gen9_layer.entrypoints[index];
264 /* fall through */
265 case 8:
266 if (gen8_layer.entrypoints[index])
267 return gen8_layer.entrypoints[index];
268 /* fall through */
269 case 7:
270 if (devinfo->is_haswell && gen75_layer.entrypoints[index])
271 return gen75_layer.entrypoints[index];
272
273 if (gen7_layer.entrypoints[index])
274 return gen7_layer.entrypoints[index];
275 /* fall through */
276 case 0:
277 return anv_layer.entrypoints[index];
278 default:
279 unreachable("unsupported gen\\n");
280 }
281 }
282 """)
283
284 # Now generate the hash table used for entry point look up. This is a
285 # uint16_t table of entry point indices. We use 0xffff to indicate an entry
286 # in the hash table is empty.
287
288 map = [NONE] * HASH_SIZE
289 collisions = [0] * 10
290 for type, name, args, num, h, guard in entrypoints:
291 level = 0
292 while map[h & HASH_MASK] != NONE:
293 h = h + PRIME_STEP
294 level = level + 1
295 if level > 9:
296 collisions[9] += 1
297 else:
298 collisions[level] += 1
299 map[h & HASH_MASK] = num
300
301 print "/* Hash table stats:"
302 print " * size %d entries" % HASH_SIZE
303 print " * collisions entries"
304 for i in xrange(10):
305 if i == 9:
306 plus = "+"
307 else:
308 plus = " "
309
310 print " * %2d%s %4d" % (i, plus, collisions[i])
311 print " */\n"
312
313 print "#define none 0x%04x\n" % NONE
314
315 print "static const uint16_t map[] = {"
316 for i in xrange(0, HASH_SIZE, 8):
317 print " ",
318 for j in xrange(i, i + 8):
319 if map[j] & 0xffff == 0xffff:
320 print " none,",
321 else:
322 print "0x%04x," % (map[j] & 0xffff),
323 print
324
325 print "};"
326
327 # Finally we generate the hash table lookup function. The hash function and
328 # linear probing algorithm matches the hash table generated above.
329
330 print textwrap.dedent("""
331 void *
332 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
333 {
334 static const uint32_t prime_factor = %d;
335 static const uint32_t prime_step = %d;
336 const struct anv_entrypoint *e;
337 uint32_t hash, h, i;
338 const char *p;
339
340 hash = 0;
341 for (p = name; *p; p++)
342 hash = hash * prime_factor + *p;
343
344 h = hash;
345 do {
346 i = map[h & %d];
347 if (i == none)
348 return NULL;
349 e = &entrypoints[i];
350 h += prime_step;
351 } while (e->hash != hash);
352
353 if (strcmp(name, strings + e->name) != 0)
354 return NULL;
355
356 return anv_resolve_entrypoint(devinfo, i);
357 }
358 """) % (PRIME_FACTOR, PRIME_STEP, HASH_MASK)
359
360
361 def main():
362 parser = argparse.ArgumentParser()
363 parser.add_argument('target', choices=['header', 'code'],
364 help='Which file to generate.')
365 parser.add_argument('--xml', help='Vulkan API XML file.')
366 args = parser.parse_args()
367
368 doc = et.parse(args.xml)
369 entrypoints = get_entrypoints(doc, get_entrypoints_defines(doc))
370
371 # Manually add CreateDmaBufImageINTEL for which we don't have an extension
372 # defined.
373 entrypoints.append(('VkResult', 'CreateDmaBufImageINTEL',
374 'VkDevice device, ' +
375 'const VkDmaBufImageCreateInfo* pCreateInfo, ' +
376 'const VkAllocationCallbacks* pAllocator,' +
377 'VkDeviceMemory* pMem,' +
378 'VkImage* pImage', len(entrypoints),
379 hash('vkCreateDmaBufImageINTEL'), None))
380
381 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
382 # per entry point.
383 if args.target == 'header':
384 print TEMPLATE_H.render(entrypoints=entrypoints,
385 filename=os.path.basename(__file__))
386 else:
387 gen_code(entrypoints)
388
389
390 if __name__ == '__main__':
391 main()