anv/icl: Generate gen11 entry point functions
[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 functools
27 import os
28 import xml.etree.cElementTree as et
29
30 from collections import OrderedDict, namedtuple
31 from mako.template import Template
32
33 from anv_extensions import *
34
35 # We generate a static hash table for entry point lookup
36 # (vkGetProcAddress). We use a linear congruential generator for our hash
37 # function and a power-of-two size table. The prime numbers are determined
38 # experimentally.
39
40 LAYERS = [
41 'anv',
42 'gen7',
43 'gen75',
44 'gen8',
45 'gen9',
46 'gen10',
47 'gen11',
48 ]
49
50 TEMPLATE_H = Template("""\
51 /* This file generated from ${filename}, don't edit directly. */
52
53 struct anv_dispatch_table {
54 union {
55 void *entrypoints[${len(entrypoints)}];
56 struct {
57 % for e in entrypoints:
58 % if e.guard is not None:
59 #ifdef ${e.guard}
60 PFN_${e.name} ${e.name};
61 #else
62 void *${e.name};
63 # endif
64 % else:
65 PFN_${e.name} ${e.name};
66 % endif
67 % endfor
68 };
69 };
70 };
71
72 %for layer in LAYERS:
73 extern const struct anv_dispatch_table ${layer}_dispatch_table;
74 %endfor
75 extern const struct anv_dispatch_table anv_tramp_dispatch_table;
76
77 % for e in entrypoints:
78 % if e.guard is not None:
79 #ifdef ${e.guard}
80 % endif
81 % for layer in LAYERS:
82 ${e.return_type} ${e.prefixed_name(layer)}(${e.decl_params()});
83 % endfor
84 % if e.guard is not None:
85 #endif // ${e.guard}
86 % endif
87 % endfor
88 """, output_encoding='utf-8')
89
90 TEMPLATE_C = Template(u"""\
91 /*
92 * Copyright © 2015 Intel Corporation
93 *
94 * Permission is hereby granted, free of charge, to any person obtaining a
95 * copy of this software and associated documentation files (the "Software"),
96 * to deal in the Software without restriction, including without limitation
97 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
98 * and/or sell copies of the Software, and to permit persons to whom the
99 * Software is furnished to do so, subject to the following conditions:
100 *
101 * The above copyright notice and this permission notice (including the next
102 * paragraph) shall be included in all copies or substantial portions of the
103 * Software.
104 *
105 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
106 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
107 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
108 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
109 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
110 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
111 * IN THE SOFTWARE.
112 */
113
114 /* This file generated from ${filename}, don't edit directly. */
115
116 #include "anv_private.h"
117
118 struct anv_entrypoint {
119 uint32_t name;
120 uint32_t hash;
121 };
122
123 /* We use a big string constant to avoid lots of reloctions from the entry
124 * point table to lots of little strings. The entries in the entry point table
125 * store the index into this big string.
126 */
127
128 static const char strings[] =
129 % for e in entrypoints:
130 "${e.name}\\0"
131 % endfor
132 ;
133
134 static const struct anv_entrypoint entrypoints[] = {
135 % for e in entrypoints:
136 [${e.num}] = { ${offsets[e.num]}, ${'{:0=#8x}'.format(e.get_c_hash())} }, /* ${e.name} */
137 % endfor
138 };
139
140 /* Weak aliases for all potential implementations. These will resolve to
141 * NULL if they're not defined, which lets the resolve_entrypoint() function
142 * either pick the correct entry point.
143 */
144
145 % for layer in LAYERS:
146 % for e in entrypoints:
147 % if e.guard is not None:
148 #ifdef ${e.guard}
149 % endif
150 ${e.return_type} ${e.prefixed_name(layer)}(${e.decl_params()}) __attribute__ ((weak));
151 % if e.guard is not None:
152 #endif // ${e.guard}
153 % endif
154 % endfor
155
156 const struct anv_dispatch_table ${layer}_dispatch_table = {
157 % for e in entrypoints:
158 % if e.guard is not None:
159 #ifdef ${e.guard}
160 % endif
161 .${e.name} = ${e.prefixed_name(layer)},
162 % if e.guard is not None:
163 #endif // ${e.guard}
164 % endif
165 % endfor
166 };
167 % endfor
168
169
170 /** Trampoline entrypoints for all device functions */
171
172 % for e in entrypoints:
173 % if e.params[0].type not in ('VkDevice', 'VkCommandBuffer'):
174 <% continue %>
175 % endif
176 % if e.guard is not None:
177 #ifdef ${e.guard}
178 % endif
179 static ${e.return_type}
180 ${e.prefixed_name('anv_tramp')}(${e.decl_params()})
181 {
182 % if e.params[0].type == 'VkDevice':
183 ANV_FROM_HANDLE(anv_device, anv_device, ${e.params[0].name});
184 return anv_device->dispatch.${e.name}(${e.call_params()});
185 % else:
186 ANV_FROM_HANDLE(anv_cmd_buffer, anv_cmd_buffer, ${e.params[0].name});
187 return anv_cmd_buffer->device->dispatch.${e.name}(${e.call_params()});
188 % endif
189 }
190 % if e.guard is not None:
191 #endif // ${e.guard}
192 % endif
193 % endfor
194
195 const struct anv_dispatch_table anv_tramp_dispatch_table = {
196 % for e in entrypoints:
197 % if e.params[0].type not in ('VkDevice', 'VkCommandBuffer'):
198 <% continue %>
199 % endif
200 % if e.guard is not None:
201 #ifdef ${e.guard}
202 % endif
203 .${e.name} = ${e.prefixed_name('anv_tramp')},
204 % if e.guard is not None:
205 #endif // ${e.guard}
206 % endif
207 % endfor
208 };
209
210
211 /** Return true if the core version or extension in which the given entrypoint
212 * is defined is enabled.
213 *
214 * If device is NULL, all device extensions are considered enabled.
215 */
216 bool
217 anv_entrypoint_is_enabled(int index, uint32_t core_version,
218 const struct anv_instance_extension_table *instance,
219 const struct anv_device_extension_table *device)
220 {
221 switch (index) {
222 % for e in entrypoints:
223 case ${e.num}:
224 % if e.core_version:
225 return ${e.core_version.c_vk_version()} <= core_version;
226 % elif e.extension:
227 % if e.extension.type == 'instance':
228 return instance->${e.extension.name[3:]};
229 % else:
230 return !device || device->${e.extension.name[3:]};
231 % endif
232 % else:
233 return true;
234 % endif
235 % endfor
236 default:
237 return false;
238 }
239 }
240
241 static void * __attribute__ ((noinline))
242 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
243 {
244 if (devinfo == NULL) {
245 return anv_dispatch_table.entrypoints[index];
246 }
247
248 const struct anv_dispatch_table *genX_table;
249 switch (devinfo->gen) {
250 case 11:
251 genX_table = &gen11_dispatch_table;
252 break;
253 case 10:
254 genX_table = &gen10_dispatch_table;
255 break;
256 case 9:
257 genX_table = &gen9_dispatch_table;
258 break;
259 case 8:
260 genX_table = &gen8_dispatch_table;
261 break;
262 case 7:
263 if (devinfo->is_haswell)
264 genX_table = &gen75_dispatch_table;
265 else
266 genX_table = &gen7_dispatch_table;
267 break;
268 default:
269 unreachable("unsupported gen\\n");
270 }
271
272 if (genX_table->entrypoints[index])
273 return genX_table->entrypoints[index];
274 else
275 return anv_dispatch_table.entrypoints[index];
276 }
277
278 /* Hash table stats:
279 * size ${hash_size} entries
280 * collisions entries:
281 % for i in xrange(10):
282 * ${i}${'+' if i == 9 else ''} ${collisions[i]}
283 % endfor
284 */
285
286 #define none ${'{:#x}'.format(none)}
287 static const uint16_t map[] = {
288 % for i in xrange(0, hash_size, 8):
289 % for j in xrange(i, i + 8):
290 ## This is 6 because the 0x is counted in the length
291 % if mapping[j] & 0xffff == 0xffff:
292 none,
293 % else:
294 ${'{:0=#6x}'.format(mapping[j] & 0xffff)},
295 % endif
296 % endfor
297 % endfor
298 };
299
300 int
301 anv_get_entrypoint_index(const char *name)
302 {
303 static const uint32_t prime_factor = ${prime_factor};
304 static const uint32_t prime_step = ${prime_step};
305 const struct anv_entrypoint *e;
306 uint32_t hash, h, i;
307 const char *p;
308
309 hash = 0;
310 for (p = name; *p; p++)
311 hash = hash * prime_factor + *p;
312
313 h = hash;
314 do {
315 i = map[h & ${hash_mask}];
316 if (i == none)
317 return -1;
318 e = &entrypoints[i];
319 h += prime_step;
320 } while (e->hash != hash);
321
322 if (strcmp(name, strings + e->name) != 0)
323 return -1;
324
325 return i;
326 }
327
328 void *
329 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
330 {
331 int idx = anv_get_entrypoint_index(name);
332 if (idx < 0)
333 return NULL;
334 return anv_resolve_entrypoint(devinfo, idx);
335 }""", output_encoding='utf-8')
336
337 NONE = 0xffff
338 HASH_SIZE = 256
339 U32_MASK = 2**32 - 1
340 HASH_MASK = HASH_SIZE - 1
341
342 PRIME_FACTOR = 5024183
343 PRIME_STEP = 19
344
345
346 def cal_hash(name):
347 """Calculate the same hash value that Mesa will calculate in C."""
348 return functools.reduce(
349 lambda h, c: (h * PRIME_FACTOR + ord(c)) & U32_MASK, name, 0)
350
351 EntrypointParam = namedtuple('EntrypointParam', 'type name decl')
352
353 class Entrypoint(object):
354 def __init__(self, name, return_type, params, guard = None):
355 self.name = name
356 self.return_type = return_type
357 self.params = params
358 self.guard = guard
359 self.enabled = False
360 self.num = None
361 # Extensions which require this entrypoint
362 self.core_version = None
363 self.extension = None
364
365 def prefixed_name(self, prefix):
366 assert self.name.startswith('vk')
367 return prefix + '_' + self.name[2:]
368
369 def decl_params(self):
370 return ', '.join(p.decl for p in self.params)
371
372 def call_params(self):
373 return ', '.join(p.name for p in self.params)
374
375 def get_c_hash(self):
376 return cal_hash(self.name)
377
378 def get_entrypoints(doc, entrypoints_to_defines, start_index):
379 """Extract the entry points from the registry."""
380 entrypoints = OrderedDict()
381
382 for command in doc.findall('./commands/command'):
383 ret_type = command.find('./proto/type').text
384 fullname = command.find('./proto/name').text
385 params = [EntrypointParam(
386 type = p.find('./type').text,
387 name = p.find('./name').text,
388 decl = ''.join(p.itertext())
389 ) for p in command.findall('./param')]
390 guard = entrypoints_to_defines.get(fullname)
391 # They really need to be unique
392 assert fullname not in entrypoints
393 entrypoints[fullname] = Entrypoint(fullname, ret_type, params, guard)
394
395 enabled_commands = set()
396 for feature in doc.findall('./feature'):
397 assert feature.attrib['api'] == 'vulkan'
398 version = VkVersion(feature.attrib['number'])
399 if version > MAX_API_VERSION:
400 continue
401
402 for command in feature.findall('./require/command'):
403 e = entrypoints[command.attrib['name']]
404 e.enabled = True
405 assert e.core_version is None
406 e.core_version = version
407
408 supported_exts = dict((ext.name, ext) for ext in EXTENSIONS)
409 for extension in doc.findall('.extensions/extension'):
410 ext_name = extension.attrib['name']
411 if ext_name not in supported_exts:
412 continue
413
414 if extension.attrib['supported'] != 'vulkan':
415 continue
416
417 ext = supported_exts[ext_name]
418 ext.type = extension.attrib['type']
419
420 for command in extension.findall('./require/command'):
421 e = entrypoints[command.attrib['name']]
422 e.enabled = True
423 assert e.core_version is None
424 assert e.extension is None
425 e.extension = ext
426
427 return [e for e in entrypoints.itervalues() if e.enabled]
428
429
430 def get_entrypoints_defines(doc):
431 """Maps entry points to extension defines."""
432 entrypoints_to_defines = {}
433
434 for extension in doc.findall('./extensions/extension[@protect]'):
435 define = extension.attrib['protect']
436
437 for entrypoint in extension.findall('./require/command'):
438 fullname = entrypoint.attrib['name']
439 entrypoints_to_defines[fullname] = define
440
441 return entrypoints_to_defines
442
443
444 def gen_code(entrypoints):
445 """Generate the C code."""
446 i = 0
447 offsets = []
448 for e in entrypoints:
449 offsets.append(i)
450 i += len(e.name) + 1
451
452 mapping = [NONE] * HASH_SIZE
453 collisions = [0] * 10
454 for e in entrypoints:
455 level = 0
456 h = e.get_c_hash()
457 while mapping[h & HASH_MASK] != NONE:
458 h = h + PRIME_STEP
459 level = level + 1
460 if level > 9:
461 collisions[9] += 1
462 else:
463 collisions[level] += 1
464 mapping[h & HASH_MASK] = e.num
465
466 return TEMPLATE_C.render(entrypoints=entrypoints,
467 LAYERS=LAYERS,
468 offsets=offsets,
469 collisions=collisions,
470 mapping=mapping,
471 hash_mask=HASH_MASK,
472 prime_step=PRIME_STEP,
473 prime_factor=PRIME_FACTOR,
474 none=NONE,
475 hash_size=HASH_SIZE,
476 filename=os.path.basename(__file__))
477
478
479 def main():
480 parser = argparse.ArgumentParser()
481 parser.add_argument('--outdir', help='Where to write the files.',
482 required=True)
483 parser.add_argument('--xml',
484 help='Vulkan API XML file.',
485 required=True,
486 action='append',
487 dest='xml_files')
488 args = parser.parse_args()
489
490 entrypoints = []
491
492 for filename in args.xml_files:
493 doc = et.parse(filename)
494 entrypoints += get_entrypoints(doc, get_entrypoints_defines(doc),
495 start_index=len(entrypoints))
496
497 # Manually add CreateDmaBufImageINTEL for which we don't have an extension
498 # defined.
499 entrypoints.append(Entrypoint('vkCreateDmaBufImageINTEL', 'VkResult', [
500 EntrypointParam('VkDevice', 'device', 'VkDevice device'),
501 EntrypointParam('VkDmaBufImageCreateInfo', 'pCreateInfo',
502 'const VkDmaBufImageCreateInfo* pCreateInfo'),
503 EntrypointParam('VkAllocationCallbacks', 'pAllocator',
504 'const VkAllocationCallbacks* pAllocator'),
505 EntrypointParam('VkDeviceMemory', 'pMem', 'VkDeviceMemory* pMem'),
506 EntrypointParam('VkImage', 'pImage', 'VkImage* pImage')
507 ]))
508
509 for num, e in enumerate(entrypoints):
510 e.num = num
511
512 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
513 # per entry point.
514 try:
515 with open(os.path.join(args.outdir, 'anv_entrypoints.h'), 'wb') as f:
516 f.write(TEMPLATE_H.render(entrypoints=entrypoints,
517 LAYERS=LAYERS,
518 filename=os.path.basename(__file__)))
519 with open(os.path.join(args.outdir, 'anv_entrypoints.c'), 'wb') as f:
520 f.write(gen_code(entrypoints))
521 except Exception:
522 # In the even there's an error this imports some helpers from mako
523 # to print a useful stack trace and prints it, then exits with
524 # status 1, if python is run with debug; otherwise it just raises
525 # the exception
526 if __debug__:
527 import sys
528 from mako import exceptions
529 sys.stderr.write(exceptions.text_error_template().render() + '\n')
530 sys.exit(1)
531 raise
532
533
534 if __name__ == '__main__':
535 main()