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