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