summaryrefslogtreecommitdiff
path: root/src/vulkan/runtime/vk_format_info_gen.py
blob: 898ba8c4a1f710cc84372a4e26901ba5296f52dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
COPYRIGHT=u"""
/* Copyright © 2022 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */
"""

import argparse
import os
import re
from collections import namedtuple
import xml.etree.ElementTree as et

from mako.template import Template

TEMPLATE_H = Template(COPYRIGHT + """\
/* This file generated from ${filename}, don't edit directly. */

#ifndef VK_FORMAT_INFO_H
#define VK_FORMAT_INFO_H

#include <vulkan/vulkan_core.h>

#ifdef __cplusplus
extern "C" {
#endif

enum vk_format_class {
   MESA_VK_FORMAT_CLASS_UNKNOWN,
% for name in format_classes:
   ${to_enum_name('MESA_VK_FORMAT_CLASS_', name)},
% endfor
};

struct vk_format_class_info {
   const VkFormat *formats;
   uint32_t format_count;
};

const struct vk_format_class_info *
vk_format_class_get_info(enum vk_format_class class);

const struct vk_format_class_info *
vk_format_get_class_info(VkFormat format);

#ifdef __cplusplus
}
#endif

#endif
""", output_encoding='utf-8')

TEMPLATE_C = Template(COPYRIGHT + """
/* This file generated from ${filename}, don't edit directly. */

#include "${header}"

#include "util/macros.h"

#include "vk_format.h"

struct vk_format_info {
   enum vk_format_class class;
};

% for id, ext in extensions.items():
static const struct vk_format_info ext${id}_format_infos[] = {
%   for name, format in ext.formats.items():
   [${format.offset}] = {
      .class = ${to_enum_name('MESA_VK_FORMAT_CLASS_', format.cls)},
   },
%   endfor
};

% endfor
static const struct vk_format_info *
vk_format_get_info(VkFormat format)
{
   uint32_t extnumber =
      format < 1000000000 ? 0 : (((format % 1000000000) / 1000) + 1);
   uint32_t offset = format % 1000;

   switch (extnumber) {
% for id, ext in extensions.items():
   case ${id}:
      assert(offset < ARRAY_SIZE(ext${id}_format_infos));
      return &ext${id}_format_infos[offset];
% endfor
   default:
      unreachable("Invalid extension");
   }
}

% for clsname, cls in format_classes.items():
%   if len(cls.formats) > 0:
static const VkFormat ${to_enum_name('MESA_VK_FORMAT_CLASS_', clsname).lower() + '_formats'}[] = {
%     for fname in cls.formats:
   ${fname},
%     endfor
%   endif
};

% endfor
static const struct vk_format_class_info class_infos[] = {
% for clsname, cls in format_classes.items():
   [${to_enum_name('MESA_VK_FORMAT_CLASS_', clsname)}] = {
%   if len(cls.formats) > 0:
      .formats = ${to_enum_name('MESA_VK_FORMAT_CLASS_', clsname).lower() + '_formats'},
      .format_count = ARRAY_SIZE(${to_enum_name('MESA_VK_FORMAT_CLASS_', clsname).lower() + '_formats'}),
%   else:
      0
%   endif
   },
% endfor
};

const struct vk_format_class_info *
vk_format_class_get_info(enum vk_format_class class)
{
   assert(class < ARRAY_SIZE(class_infos));
   return &class_infos[class];
}

const struct vk_format_class_info *
vk_format_get_class_info(VkFormat format)
{
    const struct vk_format_info *format_info = vk_format_get_info(format);
    return &class_infos[format_info->class];
}
""", output_encoding='utf-8')

def to_enum_name(prefix, name):
    return "%s" % prefix + re.sub('([^A-Za-z0-9_])', '_', name).upper()

Format = namedtuple('Format', ['name', 'cls', 'ext', 'offset'])
FormatClass = namedtuple('FormatClass', ['name', 'formats'])
Extension = namedtuple('Extension', ['id', 'formats'])

def get_formats(doc):
    """Extract the formats from the registry."""
    formats = {}

    for fmt in doc.findall('./formats/format'):
        xpath = './/enum[@name="{}"]'.format(fmt.attrib['name'])
        enum = doc.find(xpath)
        ext = None
        if 'extends' in enum.attrib:
            assert(enum.attrib['extends'] == 'VkFormat')
            if 'extnumber' in enum.attrib:
                ext = int(enum.attrib['extnumber'])
            else:
                xpath = xpath + '/..'
                parent = doc.find(xpath)
                while parent != None and ext == None:
                    if parent.tag == 'extension':
                        assert('number' in parent.attrib)
                        ext = parent.attrib['number']
                    xpath = xpath + '/..'
                    parent = doc.find(xpath)
            offset = int(enum.attrib['offset'])
        else:
            ext = 0
            offset = int(enum.attrib['value'])

        assert(ext != None)
        format = Format(fmt.attrib['name'], fmt.attrib['class'], ext, offset)
        formats[format.name] = format

    return formats

def get_formats_from_xml(xml_files):
    formats = {}

    for filename in xml_files:
        doc = et.parse(filename)
        formats.update(get_formats(doc))

    return formats

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--out-c', required=True, help='Output C file.')
    parser.add_argument('--out-h', required=True, help='Output H file.')
    parser.add_argument('--xml',
                        help='Vulkan API XML file.',
                        required=True, action='append', dest='xml_files')
    args = parser.parse_args()

    formats = get_formats_from_xml(args.xml_files)
    classes = {}
    extensions = {}
    for n, f in formats.items():
        if f.cls not in classes:
            classes[f.cls] = FormatClass(f.cls, {})
        classes[f.cls].formats[f.name] = f
        if f.ext not in extensions:
            extensions[f.ext] = Extension(f.cls, {})
        extensions[f.ext].formats[f.name] = f

    assert os.path.dirname(args.out_c) == os.path.dirname(args.out_h)

    environment = {
        'header': os.path.basename(args.out_h),
        'formats': formats,
        'format_classes': classes,
        'extensions': extensions,
        'filename': os.path.basename(__file__),
        'to_enum_name': to_enum_name,
    }

    try:
        with open(args.out_h, 'wb') as f:
            guard = os.path.basename(args.out_h).replace('.', '_').upper()
            f.write(TEMPLATE_H.render(guard=guard, **environment))
        with open(args.out_c, 'wb') as f:
            f.write(TEMPLATE_C.render(**environment))
    except Exception:
        # In the event there's an error, this imports some helpers from mako
        # to print a useful stack trace and prints it, then exits with
        # status 1, if python is run with debug; otherwise it just raises
        # the exception
        import sys
        from mako import exceptions
        print(exceptions.text_error_template().render(), file=sys.stderr)
        sys.exit(1)

if __name__ == '__main__':
    main()