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