• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1COPYRIGHT=u"""
2/* Copyright © 2015-2021 Intel Corporation
3 * Copyright © 2021 Collabora, Ltd.
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
26import argparse
27import os
28import re
29from collections import namedtuple
30import xml.etree.ElementTree as et
31
32from mako.template import Template
33
34# Mesa-local imports must be declared in meson variable
35# '{file_without_suffix}_depend_files'.
36from vk_entrypoints import get_entrypoints_from_xml, EntrypointParam
37
38# These have hand-typed implementations in vk_cmd_enqueue.c
39MANUAL_COMMANDS = [
40    # This script doesn't know how to copy arrays in structs in arrays
41    'CmdPushDescriptorSetKHR',
42
43    # The size of the elements is specified in a stride param
44    'CmdDrawMultiEXT',
45    'CmdDrawMultiIndexedEXT',
46
47    # The VkPipelineLayout object could be released before the command is
48    # executed
49    'CmdBindDescriptorSets',
50]
51
52NO_ENQUEUE_COMMANDS = [
53    # pData's size cannot be calculated from the xml
54    'CmdPushDescriptorSetWithTemplateKHR',
55
56    # These don't return void
57    'CmdSetPerformanceMarkerINTEL',
58    'CmdSetPerformanceStreamMarkerINTEL',
59    'CmdSetPerformanceOverrideINTEL',
60]
61
62TEMPLATE_H = Template(COPYRIGHT + """\
63/* This file generated from ${filename}, don't edit directly. */
64
65#pragma once
66
67#include "util/list.h"
68
69#define VK_PROTOTYPES
70#include <vulkan/vulkan.h>
71
72#ifdef __cplusplus
73extern "C" {
74#endif
75
76struct vk_device_dispatch_table;
77
78struct vk_cmd_queue {
79   const VkAllocationCallbacks *alloc;
80   struct list_head cmds;
81   VkResult error;
82};
83
84enum vk_cmd_type {
85% for c in commands:
86% if c.guard is not None:
87#ifdef ${c.guard}
88% endif
89   ${to_enum_name(c.name)},
90% if c.guard is not None:
91#endif // ${c.guard}
92% endif
93% endfor
94};
95
96extern const char *vk_cmd_queue_type_names[];
97
98% for c in commands:
99% if len(c.params) <= 1:             # Avoid "error C2016: C requires that a struct or union have at least one member"
100<% continue %>
101% endif
102% if c.guard is not None:
103#ifdef ${c.guard}
104% endif
105struct ${to_struct_name(c.name)} {
106% for p in c.params[1:]:
107   ${to_field_decl(p.decl)};
108% endfor
109};
110% if c.guard is not None:
111#endif // ${c.guard}
112% endif
113% endfor
114
115struct vk_cmd_queue_entry {
116   struct list_head cmd_link;
117   enum vk_cmd_type type;
118   union {
119% for c in commands:
120% if len(c.params) <= 1:
121<% continue %>
122% endif
123% if c.guard is not None:
124#ifdef ${c.guard}
125% endif
126      struct ${to_struct_name(c.name)} ${to_struct_field_name(c.name)};
127% if c.guard is not None:
128#endif // ${c.guard}
129% endif
130% endfor
131   } u;
132   void *driver_data;
133   void (*driver_free_cb)(struct vk_cmd_queue *queue,
134                          struct vk_cmd_queue_entry *cmd);
135};
136
137% for c in commands:
138% if c.name in manual_commands or c.name in no_enqueue_commands:
139<% continue %>
140% endif
141% if c.guard is not None:
142#ifdef ${c.guard}
143% endif
144  void vk_enqueue_${to_underscore(c.name)}(struct vk_cmd_queue *queue
145% for p in c.params[1:]:
146   , ${p.decl}
147% endfor
148  );
149% if c.guard is not None:
150#endif // ${c.guard}
151% endif
152
153% endfor
154
155void vk_free_queue(struct vk_cmd_queue *queue);
156
157static inline void
158vk_cmd_queue_init(struct vk_cmd_queue *queue, VkAllocationCallbacks *alloc)
159{
160   queue->alloc = alloc;
161   list_inithead(&queue->cmds);
162   queue->error = VK_SUCCESS;
163}
164
165static inline void
166vk_cmd_queue_reset(struct vk_cmd_queue *queue)
167{
168   vk_free_queue(queue);
169   list_inithead(&queue->cmds);
170   queue->error = VK_SUCCESS;
171}
172
173static inline void
174vk_cmd_queue_finish(struct vk_cmd_queue *queue)
175{
176   vk_free_queue(queue);
177   list_inithead(&queue->cmds);
178}
179
180void vk_cmd_queue_execute(struct vk_cmd_queue *queue,
181                          VkCommandBuffer commandBuffer,
182                          const struct vk_device_dispatch_table *disp);
183
184#ifdef __cplusplus
185}
186#endif
187""", output_encoding='utf-8')
188
189TEMPLATE_C = Template(COPYRIGHT + """
190/* This file generated from ${filename}, don't edit directly. */
191
192#include "${header}"
193
194#define VK_PROTOTYPES
195#include <vulkan/vulkan.h>
196
197#include "vk_alloc.h"
198#include "vk_cmd_enqueue_entrypoints.h"
199#include "vk_command_buffer.h"
200#include "vk_dispatch_table.h"
201#include "vk_device.h"
202
203const char *vk_cmd_queue_type_names[] = {
204% for c in commands:
205% if c.guard is not None:
206#ifdef ${c.guard}
207% endif
208   "${to_enum_name(c.name)}",
209% if c.guard is not None:
210#endif // ${c.guard}
211% endif
212% endfor
213};
214
215% for c in commands:
216% if c.guard is not None:
217#ifdef ${c.guard}
218% endif
219static void
220vk_free_${to_underscore(c.name)}(struct vk_cmd_queue *queue,
221${' ' * len('vk_free_' + to_underscore(c.name) + '(')}\\
222struct vk_cmd_queue_entry *cmd)
223{
224   if (cmd->driver_free_cb)
225      cmd->driver_free_cb(queue, cmd);
226   else
227      vk_free(queue->alloc, cmd->driver_data);
228% for p in c.params[1:]:
229% if p.len:
230   vk_free(queue->alloc, (${remove_suffix(p.decl.replace("const", ""), p.name)})cmd->u.${to_struct_field_name(c.name)}.${to_field_name(p.name)});
231% elif '*' in p.decl:
232   ${get_struct_free(c, p, types)}
233% endif
234% endfor
235   vk_free(queue->alloc, cmd);
236}
237
238% if c.name not in manual_commands and c.name not in no_enqueue_commands:
239void vk_enqueue_${to_underscore(c.name)}(struct vk_cmd_queue *queue
240% for p in c.params[1:]:
241, ${p.decl}
242% endfor
243)
244{
245   if (queue->error)
246      return;
247
248   struct vk_cmd_queue_entry *cmd = vk_zalloc(queue->alloc,
249                                              sizeof(*cmd), 8,
250                                              VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
251   if (!cmd) goto err;
252
253   cmd->type = ${to_enum_name(c.name)};
254
255% for p in c.params[1:]:
256% if p.len:
257   if (${p.name}) {
258      ${get_array_copy(c, p)}
259   }
260% elif '[' in p.decl:
261   memcpy(cmd->u.${to_struct_field_name(c.name)}.${to_field_name(p.name)}, ${p.name},
262          sizeof(*${p.name}) * ${get_array_len(p)});
263% elif p.type == "void":
264   cmd->u.${to_struct_field_name(c.name)}.${to_field_name(p.name)} = (${remove_suffix(p.decl.replace("const", ""), p.name)}) ${p.name};
265% elif '*' in p.decl:
266   ${get_struct_copy("cmd->u.%s.%s" % (to_struct_field_name(c.name), to_field_name(p.name)), p.name, p.type, 'sizeof(%s)' % p.type, types)}
267% else:
268   cmd->u.${to_struct_field_name(c.name)}.${to_field_name(p.name)} = ${p.name};
269% endif
270% endfor
271
272   list_addtail(&cmd->cmd_link, &queue->cmds);
273   return;
274
275err:
276   queue->error = VK_ERROR_OUT_OF_HOST_MEMORY;
277   if (cmd)
278      vk_free_${to_underscore(c.name)}(queue, cmd);
279}
280% endif
281% if c.guard is not None:
282#endif // ${c.guard}
283% endif
284
285% endfor
286
287void
288vk_free_queue(struct vk_cmd_queue *queue)
289{
290   struct vk_cmd_queue_entry *tmp, *cmd;
291   LIST_FOR_EACH_ENTRY_SAFE(cmd, tmp, &queue->cmds, cmd_link) {
292      switch(cmd->type) {
293% for c in commands:
294% if c.guard is not None:
295#ifdef ${c.guard}
296% endif
297      case ${to_enum_name(c.name)}:
298         vk_free_${to_underscore(c.name)}(queue, cmd);
299         break;
300% if c.guard is not None:
301#endif // ${c.guard}
302% endif
303% endfor
304      }
305   }
306}
307
308void
309vk_cmd_queue_execute(struct vk_cmd_queue *queue,
310                     VkCommandBuffer commandBuffer,
311                     const struct vk_device_dispatch_table *disp)
312{
313   list_for_each_entry(struct vk_cmd_queue_entry, cmd, &queue->cmds, cmd_link) {
314      switch (cmd->type) {
315% for c in commands:
316% if c.guard is not None:
317#ifdef ${c.guard}
318% endif
319      case ${to_enum_name(c.name)}:
320          disp->${c.name}(commandBuffer
321% for p in c.params[1:]:
322             , cmd->u.${to_struct_field_name(c.name)}.${to_field_name(p.name)}\\
323% endfor
324          );
325          break;
326% if c.guard is not None:
327#endif // ${c.guard}
328% endif
329% endfor
330      default: unreachable("Unsupported command");
331      }
332   }
333}
334
335% for c in commands:
336% if c.name in no_enqueue_commands:
337/* TODO: Generate vk_cmd_enqueue_${c.name}() */
338<% continue %>
339% endif
340
341% if c.guard is not None:
342#ifdef ${c.guard}
343% endif
344<% assert c.return_type == 'void' %>
345
346% if c.name in manual_commands:
347/* vk_cmd_enqueue_${c.name}() is hand-typed in vk_cmd_enqueue.c */
348% else:
349VKAPI_ATTR void VKAPI_CALL
350vk_cmd_enqueue_${c.name}(${c.decl_params()})
351{
352   VK_FROM_HANDLE(vk_command_buffer, cmd_buffer, commandBuffer);
353
354% if len(c.params) == 1:
355   vk_enqueue_${to_underscore(c.name)}(&cmd_buffer->cmd_queue);
356% else:
357   vk_enqueue_${to_underscore(c.name)}(&cmd_buffer->cmd_queue,
358                                       ${c.call_params(1)});
359% endif
360}
361% endif
362
363VKAPI_ATTR void VKAPI_CALL
364vk_cmd_enqueue_unless_primary_${c.name}(${c.decl_params()})
365{
366    VK_FROM_HANDLE(vk_command_buffer, cmd_buffer, commandBuffer);
367
368   if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
369      const struct vk_device_dispatch_table *disp =
370         cmd_buffer->base.device->command_dispatch_table;
371
372      disp->${c.name}(${c.call_params()});
373   } else {
374      vk_cmd_enqueue_${c.name}(${c.call_params()});
375   }
376}
377% if c.guard is not None:
378#endif // ${c.guard}
379% endif
380% endfor
381""", output_encoding='utf-8')
382
383def remove_prefix(text, prefix):
384    if text.startswith(prefix):
385        return text[len(prefix):]
386    return text
387
388def remove_suffix(text, suffix):
389    if text.endswith(suffix):
390        return text[:-len(suffix)]
391    return text
392
393def to_underscore(name):
394    return remove_prefix(re.sub('([A-Z]+)', r'_\1', name).lower(), '_')
395
396def to_struct_field_name(name):
397    return to_underscore(name).replace('cmd_', '')
398
399def to_field_name(name):
400    return remove_prefix(to_underscore(name).replace('cmd_', ''), 'p_')
401
402def to_field_decl(decl):
403    if 'const*' in decl:
404        decl = decl.replace('const*', '*')
405    else:
406        decl = decl.replace('const ', '')
407    [decl, name] = decl.rsplit(' ', 1)
408    return decl + ' ' + to_field_name(name)
409
410def to_enum_name(name):
411    return "VK_%s" % to_underscore(name).upper()
412
413def to_struct_name(name):
414    return "vk_%s" % to_underscore(name)
415
416def get_array_len(param):
417    return param.decl[param.decl.find("[") + 1:param.decl.find("]")]
418
419def get_array_copy(command, param):
420    field_name = "cmd->u.%s.%s" % (to_struct_field_name(command.name), to_field_name(param.name))
421    if param.type == "void":
422        field_size = "1"
423    else:
424        field_size = "sizeof(*%s)" % field_name
425    allocation = "%s = vk_zalloc(queue->alloc, %s * %s, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);\n   if (%s == NULL) goto err;\n" % (field_name, field_size, param.len, field_name)
426    const_cast = remove_suffix(param.decl.replace("const", ""), param.name)
427    copy = "memcpy((%s)%s, %s, %s * %s);" % (const_cast, field_name, param.name, field_size, param.len)
428    return "%s\n   %s" % (allocation, copy)
429
430def get_array_member_copy(struct, src_name, member):
431    field_name = "%s->%s" % (struct, member.name)
432    if member.len == "struct-ptr":
433        field_size = "sizeof(*%s)" % (field_name)
434    else:
435        field_size = "sizeof(*%s) * %s->%s" % (field_name, struct, member.len)
436    allocation = "%s = vk_zalloc(queue->alloc, %s, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);\n   if (%s == NULL) goto err;\n" % (field_name, field_size, field_name)
437    const_cast = remove_suffix(member.decl.replace("const", ""), member.name)
438    copy = "memcpy((%s)%s, %s->%s, %s);" % (const_cast, field_name, src_name, member.name, field_size)
439    return "if (%s->%s) {\n   %s\n   %s\n}\n" % (src_name, member.name, allocation, copy)
440
441def get_pnext_member_copy(struct, src_type, member, types, level):
442    if not types[src_type].extended_by:
443        return ""
444    field_name = "%s->%s" % (struct, member.name)
445    pnext_decl = "const VkBaseInStructure *pnext = %s;" % field_name
446    case_stmts = ""
447    for type in types[src_type].extended_by:
448        case_stmts += """
449      case %s:
450         %s
451         break;
452      """ % (type.enum, get_struct_copy(field_name, "pnext", type.name, "sizeof(%s)" % type.name, types, level))
453    return """
454      %s
455      if (pnext) {
456         switch ((int32_t)pnext->sType) {
457         %s
458         }
459      }
460      """ % (pnext_decl, case_stmts)
461
462def get_struct_copy(dst, src_name, src_type, size, types, level=0):
463    global tmp_dst_idx
464    global tmp_src_idx
465
466    allocation = "%s = vk_zalloc(queue->alloc, %s, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);\n      if (%s == NULL) goto err;\n" % (dst, size, dst)
467    copy = "memcpy((void*)%s, %s, %s);" % (dst, src_name, size)
468
469    level += 1
470    tmp_dst = "%s *tmp_dst%d = (void *) %s; (void) tmp_dst%d;" % (src_type, level, dst, level)
471    tmp_src = "%s *tmp_src%d = (void *) %s; (void) tmp_src%d;" % (src_type, level, src_name, level)
472
473    member_copies = ""
474    if src_type in types:
475        for member in types[src_type].members:
476            if member.len and member.len != 'null-terminated':
477                member_copies += get_array_member_copy("tmp_dst%d" % level, "tmp_src%d" % level, member)
478            elif member.name == 'pNext':
479                member_copies += get_pnext_member_copy("tmp_dst%d" % level, src_type, member, types, level)
480
481    null_assignment = "%s = NULL;" % dst
482    if_stmt = "if (%s) {" % src_name
483    return "%s\n      %s\n      %s\n   %s\n   %s   \n   %s   } else {\n      %s\n   }" % (if_stmt, allocation, copy, tmp_dst, tmp_src, member_copies, null_assignment)
484
485def get_struct_free(command, param, types):
486    field_name = "cmd->u.%s.%s" % (to_struct_field_name(command.name), to_field_name(param.name))
487    const_cast = remove_suffix(param.decl.replace("const", ""), param.name)
488    struct_free = "vk_free(queue->alloc, (%s)%s);" % (const_cast, field_name)
489    member_frees = ""
490    if (param.type in types):
491        for member in types[param.type].members:
492            if member.len and member.len != 'null-terminated':
493                member_name = "cmd->u.%s.%s->%s" % (to_struct_field_name(command.name), to_field_name(param.name), member.name)
494                const_cast = remove_suffix(member.decl.replace("const", ""), member.name)
495                member_frees += "vk_free(queue->alloc, (%s)%s);\n" % (const_cast, member_name)
496    return "%s      %s\n" % (member_frees, struct_free)
497
498EntrypointType = namedtuple('EntrypointType', 'name enum members extended_by')
499
500def get_types(doc):
501    """Extract the types from the registry."""
502    types = {}
503
504    for _type in doc.findall('./types/type'):
505        if _type.attrib.get('category') != 'struct':
506            continue
507        members = []
508        type_enum = None
509        for p in _type.findall('./member'):
510            mem_type = p.find('./type').text
511            mem_name = p.find('./name').text
512            mem_decl = ''.join(p.itertext())
513            mem_len = p.attrib.get('len', None)
514            if mem_len is None and '*' in mem_decl and mem_name != 'pNext':
515                mem_len = "struct-ptr"
516
517            member = EntrypointParam(type=mem_type,
518                                     name=mem_name,
519                                     decl=mem_decl,
520                                     len=mem_len)
521            members.append(member)
522
523            if mem_name == 'sType':
524                type_enum = p.attrib.get('values')
525        types[_type.attrib['name']] = EntrypointType(name=_type.attrib['name'], enum=type_enum, members=members, extended_by=[])
526
527    for _type in doc.findall('./types/type'):
528        if _type.attrib.get('category') != 'struct':
529            continue
530        if _type.attrib.get('structextends') is None:
531            continue
532        for extended in _type.attrib.get('structextends').split(','):
533            types[extended].extended_by.append(types[_type.attrib['name']])
534
535    return types
536
537def get_types_from_xml(xml_files):
538    types = {}
539
540    for filename in xml_files:
541        doc = et.parse(filename)
542        types.update(get_types(doc))
543
544    return types
545
546def main():
547    parser = argparse.ArgumentParser()
548    parser.add_argument('--out-c', required=True, help='Output C file.')
549    parser.add_argument('--out-h', required=True, help='Output H file.')
550    parser.add_argument('--xml',
551                        help='Vulkan API XML file.',
552                        required=True, action='append', dest='xml_files')
553    args = parser.parse_args()
554
555    commands = []
556    for e in get_entrypoints_from_xml(args.xml_files):
557        if e.name.startswith('Cmd') and \
558           not e.alias:
559            commands.append(e)
560
561    types = get_types_from_xml(args.xml_files)
562
563    assert os.path.dirname(args.out_c) == os.path.dirname(args.out_h)
564
565    environment = {
566        'header': os.path.basename(args.out_h),
567        'commands': commands,
568        'filename': os.path.basename(__file__),
569        'to_underscore': to_underscore,
570        'get_array_len': get_array_len,
571        'to_struct_field_name': to_struct_field_name,
572        'to_field_name': to_field_name,
573        'to_field_decl': to_field_decl,
574        'to_enum_name': to_enum_name,
575        'to_struct_name': to_struct_name,
576        'get_array_copy': get_array_copy,
577        'get_struct_copy': get_struct_copy,
578        'get_struct_free': get_struct_free,
579        'types': types,
580        'manual_commands': MANUAL_COMMANDS,
581        'no_enqueue_commands': NO_ENQUEUE_COMMANDS,
582        'remove_suffix': remove_suffix,
583    }
584
585    try:
586        with open(args.out_h, 'wb') as f:
587            guard = os.path.basename(args.out_h).replace('.', '_').upper()
588            f.write(TEMPLATE_H.render(guard=guard, **environment))
589        with open(args.out_c, 'wb') as f:
590            f.write(TEMPLATE_C.render(**environment))
591    except Exception:
592        # In the event there's an error, this imports some helpers from mako
593        # to print a useful stack trace and prints it, then exits with
594        # status 1, if python is run with debug; otherwise it just raises
595        # the exception
596        import sys
597        from mako import exceptions
598        print(exceptions.text_error_template().render(), file=sys.stderr)
599        sys.exit(1)
600
601if __name__ == '__main__':
602    main()
603