a63badb4770f7d771606c05b9c93a38c738aa8f4
[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 """), output_encoding='utf-8')
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 cal_hash(name):
260 """Calculate the same hash value that Mesa will calculate in C."""
261 h = 0
262 for c in name:
263 h = (h * PRIME_FACTOR + ord(c)) & U32_MASK
264
265 return h
266
267
268 def get_entrypoints(doc, entrypoints_to_defines):
269 """Extract the entry points from the registry."""
270 entrypoints = []
271
272 enabled_commands = set()
273 for feature in doc.findall('./feature'):
274 assert feature.attrib['api'] == 'vulkan'
275 if float(feature.attrib['number']) > MAX_API_VERSION:
276 continue
277
278 for command in feature.findall('./require/command'):
279 enabled_commands.add(command.attrib['name'])
280
281 for extension in doc.findall('.extensions/extension'):
282 if extension.attrib['name'] not in SUPPORTED_EXTENSIONS:
283 continue
284
285 assert extension.attrib['supported'] == 'vulkan'
286 for command in extension.findall('./require/command'):
287 enabled_commands.add(command.attrib['name'])
288
289 index = 0
290 for command in doc.findall('./commands/command'):
291 type = command.find('./proto/type').text
292 fullname = command.find('./proto/name').text
293
294 if fullname not in enabled_commands:
295 continue
296
297 shortname = fullname[2:]
298 params = (''.join(p.itertext()) for p in command.findall('./param'))
299 params = ', '.join(params)
300 if fullname in entrypoints_to_defines:
301 guard = entrypoints_to_defines[fullname]
302 else:
303 guard = None
304 entrypoints.append((type, shortname, params, index, cal_hash(fullname), guard))
305 index += 1
306
307 return entrypoints
308
309
310 def get_entrypoints_defines(doc):
311 """Maps entry points to extension defines."""
312 entrypoints_to_defines = {}
313 extensions = doc.findall('./extensions/extension')
314 for extension in extensions:
315 define = extension.get('protect')
316 entrypoints = extension.findall('./require/command')
317 for entrypoint in entrypoints:
318 fullname = entrypoint.get('name')
319 entrypoints_to_defines[fullname] = define
320 return entrypoints_to_defines
321
322
323 def gen_code(entrypoints):
324 """Generate the C code."""
325 i = 0
326 offsets = []
327 for _, name, _, _, _, _ in entrypoints:
328 offsets.append(i)
329 i += 2 + len(name) + 1
330
331 mapping = [NONE] * HASH_SIZE
332 collisions = [0] * 10
333 for _, name, _, num, h, _ in entrypoints:
334 level = 0
335 while mapping[h & HASH_MASK] != NONE:
336 h = h + PRIME_STEP
337 level = level + 1
338 if level > 9:
339 collisions[9] += 1
340 else:
341 collisions[level] += 1
342 mapping[h & HASH_MASK] = num
343
344 return TEMPLATE_C.render(entrypoints=entrypoints,
345 offsets=offsets,
346 collisions=collisions,
347 mapping=mapping,
348 hash_mask=HASH_MASK,
349 prime_step=PRIME_STEP,
350 prime_factor=PRIME_FACTOR,
351 none=NONE,
352 hash_size=HASH_SIZE,
353 filename=os.path.basename(__file__))
354
355
356 def main():
357 parser = argparse.ArgumentParser()
358 parser.add_argument('--outdir', help='Where to write the files.',
359 required=True)
360 parser.add_argument('--xml', help='Vulkan API XML file.', required=True)
361 args = parser.parse_args()
362
363 doc = et.parse(args.xml)
364 entrypoints = get_entrypoints(doc, get_entrypoints_defines(doc))
365
366 # Manually add CreateDmaBufImageINTEL for which we don't have an extension
367 # defined.
368 entrypoints.append(('VkResult', 'CreateDmaBufImageINTEL',
369 'VkDevice device, ' +
370 'const VkDmaBufImageCreateInfo* pCreateInfo, ' +
371 'const VkAllocationCallbacks* pAllocator,' +
372 'VkDeviceMemory* pMem,' +
373 'VkImage* pImage', len(entrypoints),
374 cal_hash('vkCreateDmaBufImageINTEL'), None))
375
376 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
377 # per entry point.
378 with open(os.path.join(args.outdir, 'anv_entrypoints.h'), 'wb') as f:
379 f.write(TEMPLATE_H.render(entrypoints=entrypoints,
380 filename=os.path.basename(__file__)))
381 with open(os.path.join(args.outdir, 'anv_entrypoints.c'), 'wb') as f:
382 f.write(gen_code(entrypoints))
383
384
385 if __name__ == '__main__':
386 main()