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