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
29 import xml
.etree
.cElementTree
as et
31 from mako
.template
import Template
33 from anv_extensions
import *
35 # We generate a static hash table for entry point lookup
36 # (vkGetProcAddress). We use a linear congruential generator for our hash
37 # function and a power-of-two size table. The prime numbers are determined
40 TEMPLATE_H
= Template(textwrap
.dedent("""\
41 /* This file generated from ${filename}, don't edit directly. */
43 struct anv_dispatch_table {
45 void *entrypoints[${len(entrypoints)}];
47 % for _, name, _, _, _, guard in entrypoints:
48 % if guard is not None:
50 PFN_vk${name} ${name};
55 PFN_vk${name} ${name};
62 % for type_, name, args, num, h, guard in entrypoints:
63 % if guard is not None:
66 ${type_} anv_${name}(${args});
67 ${type_} gen7_${name}(${args});
68 ${type_} gen75_${name}(${args});
69 ${type_} gen8_${name}(${args});
70 ${type_} gen9_${name}(${args});
71 ${type_} gen10_${name}(${args});
72 % if guard is not None:
76 """), output_encoding
='utf-8')
78 TEMPLATE_C
= Template(textwrap
.dedent(u
"""\
80 * Copyright © 2015 Intel Corporation
82 * Permission is hereby granted, free of charge, to any person obtaining a
83 * copy of this software and associated documentation files (the "Software"),
84 * to deal in the Software without restriction, including without limitation
85 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
86 * and/or sell copies of the Software, and to permit persons to whom the
87 * Software is furnished to do so, subject to the following conditions:
89 * The above copyright notice and this permission notice (including the next
90 * paragraph) shall be included in all copies or substantial portions of the
93 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
94 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
95 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
96 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
97 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
98 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
102 /* This file generated from ${filename}, don't edit directly. */
104 #include "anv_private.h"
106 struct anv_entrypoint {
111 /* We use a big string constant to avoid lots of reloctions from the entry
112 * point table to lots of little strings. The entries in the entry point table
113 * store the index into this big string.
116 static const char strings[] =
117 % for _, name, _, _, _, _ in entrypoints:
122 static const struct anv_entrypoint entrypoints[] = {
123 % for _, _, _, num, h, _ in entrypoints:
124 { ${offsets[num]}, ${'{:0=#8x}'.format(h)} },
128 /* Weak aliases for all potential implementations. These will resolve to
129 * NULL if they're not defined, which lets the resolve_entrypoint() function
130 * either pick the correct entry point.
133 % for layer in ['anv', 'gen7', 'gen75', 'gen8', 'gen9', 'gen10']:
134 % for type_, name, args, _, _, guard in entrypoints:
135 % if guard is not None:
138 ${type_} ${layer}_${name}(${args}) __attribute__ ((weak));
139 % if guard is not None:
144 const struct anv_dispatch_table ${layer}_layer = {
145 % for _, name, args, _, _, guard in entrypoints:
146 % if guard is not None:
149 .${name} = ${layer}_${name},
150 % if guard is not None:
157 static void * __attribute__ ((noinline))
158 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
160 if (devinfo == NULL) {
161 return anv_layer.entrypoints[index];
164 switch (devinfo->gen) {
166 if (gen10_layer.entrypoints[index])
167 return gen10_layer.entrypoints[index];
170 if (gen9_layer.entrypoints[index])
171 return gen9_layer.entrypoints[index];
174 if (gen8_layer.entrypoints[index])
175 return gen8_layer.entrypoints[index];
178 if (devinfo->is_haswell && gen75_layer.entrypoints[index])
179 return gen75_layer.entrypoints[index];
181 if (gen7_layer.entrypoints[index])
182 return gen7_layer.entrypoints[index];
185 return anv_layer.entrypoints[index];
187 unreachable("unsupported gen\\n");
192 * size ${hash_size} entries
193 * collisions entries:
194 % for i in xrange(10):
195 * ${i}${'+' if i == 9 else ''} ${collisions[i]}
199 #define none ${'{:#x}'.format(none)}
200 static const uint16_t map[] = {
201 % for i in xrange(0, hash_size, 8):
202 % for j in xrange(i, i + 8):
203 ## This is 6 because the 0x is counted in the length
204 % if mapping[j] & 0xffff == 0xffff:
207 ${'{:0=#6x}'.format(mapping[j] & 0xffff)},
214 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
216 static const uint32_t prime_factor = ${prime_factor};
217 static const uint32_t prime_step = ${prime_step};
218 const struct anv_entrypoint *e;
223 for (p = name; *p; p++)
224 hash = hash * prime_factor + *p;
228 i = map[h & ${hash_mask}];
233 } while (e->hash != hash);
235 if (strcmp(name, strings + e->name) != 0)
238 return anv_resolve_entrypoint(devinfo, i);
239 }"""), output_encoding
='utf-8')
244 HASH_MASK
= HASH_SIZE
- 1
246 PRIME_FACTOR
= 5024183
251 """Calculate the same hash value that Mesa will calculate in C."""
252 return functools
.reduce(
253 lambda h
, c
: (h
* PRIME_FACTOR
+ ord(c
)) & U32_MASK
, name
, 0)
256 def get_entrypoints(doc
, entrypoints_to_defines
):
257 """Extract the entry points from the registry."""
260 enabled_commands
= set()
261 for feature
in doc
.findall('./feature'):
262 assert feature
.attrib
['api'] == 'vulkan'
263 if VkVersion(feature
.attrib
['number']) > MAX_API_VERSION
:
266 for command
in feature
.findall('./require/command'):
267 enabled_commands
.add(command
.attrib
['name'])
269 supported
= set(ext
.name
for ext
in EXTENSIONS
)
270 for extension
in doc
.findall('.extensions/extension'):
271 if extension
.attrib
['name'] not in supported
:
274 assert extension
.attrib
['supported'] == 'vulkan'
275 for command
in extension
.findall('./require/command'):
276 enabled_commands
.add(command
.attrib
['name'])
279 for command
in doc
.findall('./commands/command'):
280 type = command
.find('./proto/type').text
281 fullname
= command
.find('./proto/name').text
283 if fullname
not in enabled_commands
:
286 shortname
= fullname
[2:]
287 params
= (''.join(p
.itertext()) for p
in command
.findall('./param'))
288 params
= ', '.join(params
)
289 guard
= entrypoints_to_defines
.get(fullname
)
290 entrypoints
.append((type, shortname
, params
, index
, cal_hash(fullname
), guard
))
296 def get_entrypoints_defines(doc
):
297 """Maps entry points to extension defines."""
298 entrypoints_to_defines
= {}
300 for extension
in doc
.findall('./extensions/extension[@protect]'):
301 define
= extension
.attrib
['protect']
303 for entrypoint
in extension
.findall('./require/command'):
304 fullname
= entrypoint
.attrib
['name']
305 entrypoints_to_defines
[fullname
] = define
307 return entrypoints_to_defines
310 def gen_code(entrypoints
):
311 """Generate the C code."""
314 for _
, name
, _
, _
, _
, _
in entrypoints
:
316 i
+= 2 + len(name
) + 1
318 mapping
= [NONE
] * HASH_SIZE
319 collisions
= [0] * 10
320 for _
, name
, _
, num
, h
, _
in entrypoints
:
322 while mapping
[h
& HASH_MASK
] != NONE
:
328 collisions
[level
] += 1
329 mapping
[h
& HASH_MASK
] = num
331 return TEMPLATE_C
.render(entrypoints
=entrypoints
,
333 collisions
=collisions
,
336 prime_step
=PRIME_STEP
,
337 prime_factor
=PRIME_FACTOR
,
340 filename
=os
.path
.basename(__file__
))
344 parser
= argparse
.ArgumentParser()
345 parser
.add_argument('--outdir', help='Where to write the files.',
347 parser
.add_argument('--xml', help='Vulkan API XML file.', required
=True)
348 args
= parser
.parse_args()
350 doc
= et
.parse(args
.xml
)
351 entrypoints
= get_entrypoints(doc
, get_entrypoints_defines(doc
))
353 # Manually add CreateDmaBufImageINTEL for which we don't have an extension
355 entrypoints
.append(('VkResult', 'CreateDmaBufImageINTEL',
356 'VkDevice device, ' +
357 'const VkDmaBufImageCreateInfo* pCreateInfo, ' +
358 'const VkAllocationCallbacks* pAllocator,' +
359 'VkDeviceMemory* pMem,' +
360 'VkImage* pImage', len(entrypoints
),
361 cal_hash('vkCreateDmaBufImageINTEL'), None))
363 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
365 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.h'), 'wb') as f
:
366 f
.write(TEMPLATE_H
.render(entrypoints
=entrypoints
,
367 filename
=os
.path
.basename(__file__
)))
368 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.c'), 'wb') as f
:
369 f
.write(gen_code(entrypoints
))
372 if __name__
== '__main__':