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