3 # Copyright © 2015, 2017 Intel Corporation
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:
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
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
28 import xml
.etree
.ElementTree
as et
30 from mako
.template
import Template
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',
43 'VK_KHR_wayland_surface',
45 'VK_KHR_xlib_surface',
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
53 TEMPLATE_H
= Template(textwrap
.dedent("""\
54 /* This file generated from ${filename}, don't edit directly. */
56 struct anv_dispatch_table {
58 void *entrypoints[${len(entrypoints)}];
60 % for _, name, _, _, _, guard in entrypoints:
61 % if guard is not None:
63 PFN_vk${name} ${name};
68 PFN_vk${name} ${name};
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:
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:
89 """), output_encoding
='utf-8')
91 TEMPLATE_C
= Template(textwrap
.dedent(u
"""\
93 * Copyright © 2015 Intel Corporation
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:
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
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
115 /* This file generated from ${filename}, don't edit directly. */
117 #include "anv_private.h"
119 struct anv_entrypoint {
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.
129 static const char strings[] =
130 % for _, name, _, _, _, _ in entrypoints:
135 static const struct anv_entrypoint entrypoints[] = {
136 % for _, _, _, num, h, _ in entrypoints:
137 { ${offsets[num]}, ${'{:0=#8x}'.format(h)} },
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.
146 % for layer in ['anv', 'gen7', 'gen75', 'gen8', 'gen9']:
147 % for type_, name, args, _, _, guard in entrypoints:
148 % if guard is not None:
151 ${type_} ${layer}_${name}(${args}) __attribute__ ((weak));
152 % if guard is not None:
157 const struct anv_dispatch_table ${layer}_layer = {
158 % for _, name, args, _, _, guard in entrypoints:
159 % if guard is not None:
162 .${name} = ${layer}_${name},
163 % if guard is not None:
170 static void * __attribute__ ((noinline))
171 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
173 if (devinfo == NULL) {
174 return anv_layer.entrypoints[index];
177 switch (devinfo->gen) {
179 if (gen9_layer.entrypoints[index])
180 return gen9_layer.entrypoints[index];
183 if (gen8_layer.entrypoints[index])
184 return gen8_layer.entrypoints[index];
187 if (devinfo->is_haswell && gen75_layer.entrypoints[index])
188 return gen75_layer.entrypoints[index];
190 if (gen7_layer.entrypoints[index])
191 return gen7_layer.entrypoints[index];
194 return anv_layer.entrypoints[index];
196 unreachable("unsupported gen\\n");
201 * size ${hash_size} entries
202 * collisions entries:
203 % for i in xrange(10):
204 * ${i}${'+' if i == 9 else ''} ${collisions[i]}
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:
216 ${'{:0=#6x}'.format(mapping[j] & 0xffff)},
223 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
225 static const uint32_t prime_factor = ${prime_factor};
226 static const uint32_t prime_step = ${prime_step};
227 const struct anv_entrypoint *e;
232 for (p = name; *p; p++)
233 hash = hash * prime_factor + *p;
237 i = map[h & ${hash_mask}];
242 } while (e->hash != hash);
244 if (strcmp(name, strings + e->name) != 0)
247 return anv_resolve_entrypoint(devinfo, i);
248 }"""), output_encoding
='utf-8')
253 HASH_MASK
= HASH_SIZE
- 1
255 PRIME_FACTOR
= 5024183
262 h
= (h
* PRIME_FACTOR
+ ord(c
)) & U32_MASK
267 def get_entrypoints(doc
, entrypoints_to_defines
):
268 """Extract the entry points from the registry."""
271 enabled_commands
= set()
272 for feature
in doc
.findall('./feature'):
273 assert feature
.attrib
['api'] == 'vulkan'
274 if float(feature
.attrib
['number']) > MAX_API_VERSION
:
277 for command
in feature
.findall('./require/command'):
278 enabled_commands
.add(command
.attrib
['name'])
280 for extension
in doc
.findall('.extensions/extension'):
281 if extension
.attrib
['name'] not in SUPPORTED_EXTENSIONS
:
284 assert extension
.attrib
['supported'] == 'vulkan'
285 for command
in extension
.findall('./require/command'):
286 enabled_commands
.add(command
.attrib
['name'])
289 for command
in doc
.findall('./commands/command'):
290 type = command
.find('./proto/type').text
291 fullname
= command
.find('./proto/name').text
293 if fullname
not in enabled_commands
:
296 shortname
= fullname
[2:]
297 params
= (''.join(p
.itertext()) for p
in command
.findall('./param'))
298 params
= ', '.join(params
)
299 if fullname
in entrypoints_to_defines
:
300 guard
= entrypoints_to_defines
[fullname
]
303 entrypoints
.append((type, shortname
, params
, index
, hash(fullname
), guard
))
309 def get_entrypoints_defines(doc
):
310 """Maps entry points to extension defines."""
311 entrypoints_to_defines
= {}
312 extensions
= doc
.findall('./extensions/extension')
313 for extension
in extensions
:
314 define
= extension
.get('protect')
315 entrypoints
= extension
.findall('./require/command')
316 for entrypoint
in entrypoints
:
317 fullname
= entrypoint
.get('name')
318 entrypoints_to_defines
[fullname
] = define
319 return entrypoints_to_defines
322 def gen_code(entrypoints
):
323 """Generate the C code."""
326 for _
, name
, _
, _
, _
, _
in entrypoints
:
328 i
+= 2 + len(name
) + 1
330 mapping
= [NONE
] * HASH_SIZE
331 collisions
= [0] * 10
332 for _
, name
, _
, num
, h
, _
in entrypoints
:
334 while mapping
[h
& HASH_MASK
] != NONE
:
340 collisions
[level
] += 1
341 mapping
[h
& HASH_MASK
] = num
343 return TEMPLATE_C
.render(entrypoints
=entrypoints
,
345 collisions
=collisions
,
348 prime_step
=PRIME_STEP
,
349 prime_factor
=PRIME_FACTOR
,
352 filename
=os
.path
.basename(__file__
))
356 parser
= argparse
.ArgumentParser()
357 parser
.add_argument('--outdir', help='Where to write the files.',
359 parser
.add_argument('--xml', help='Vulkan API XML file.', required
=True)
360 args
= parser
.parse_args()
362 doc
= et
.parse(args
.xml
)
363 entrypoints
= get_entrypoints(doc
, get_entrypoints_defines(doc
))
365 # Manually add CreateDmaBufImageINTEL for which we don't have an extension
367 entrypoints
.append(('VkResult', 'CreateDmaBufImageINTEL',
368 'VkDevice device, ' +
369 'const VkDmaBufImageCreateInfo* pCreateInfo, ' +
370 'const VkAllocationCallbacks* pAllocator,' +
371 'VkDeviceMemory* pMem,' +
372 'VkImage* pImage', len(entrypoints
),
373 hash('vkCreateDmaBufImageINTEL'), None))
375 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
377 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.h'), 'wb') as f
:
378 f
.write(TEMPLATE_H
.render(entrypoints
=entrypoints
,
379 filename
=os
.path
.basename(__file__
)))
380 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.c'), 'wb') as f
:
381 f
.write(gen_code(entrypoints
))
384 if __name__
== '__main__':