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
37 # We generate a static hash table for entry point lookup
38 # (vkGetProcAddress). We use a linear congruential generator for our hash
39 # function and a power-of-two size table. The prime numbers are determined
42 TEMPLATE_H
= Template(textwrap
.dedent("""\
43 /* This file generated from ${filename}, don't edit directly. */
45 struct anv_dispatch_table {
47 void *entrypoints[${len(entrypoints)}];
49 % for _, name, _, _, _, guard in entrypoints:
50 % if guard is not None:
52 PFN_vk${name} ${name};
57 PFN_vk${name} ${name};
64 % for type_, name, args, num, h, guard in entrypoints:
65 % if guard is not None:
68 ${type_} anv_${name}(${args});
69 ${type_} gen7_${name}(${args});
70 ${type_} gen75_${name}(${args});
71 ${type_} gen8_${name}(${args});
72 ${type_} gen9_${name}(${args});
73 ${type_} gen10_${name}(${args});
74 % if guard is not None:
78 """), output_encoding
='utf-8')
80 TEMPLATE_C
= Template(textwrap
.dedent(u
"""\
82 * Copyright © 2015 Intel Corporation
84 * Permission is hereby granted, free of charge, to any person obtaining a
85 * copy of this software and associated documentation files (the "Software"),
86 * to deal in the Software without restriction, including without limitation
87 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88 * and/or sell copies of the Software, and to permit persons to whom the
89 * Software is furnished to do so, subject to the following conditions:
91 * The above copyright notice and this permission notice (including the next
92 * paragraph) shall be included in all copies or substantial portions of the
95 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
96 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
97 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
98 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
99 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
100 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
104 /* This file generated from ${filename}, don't edit directly. */
106 #include "anv_private.h"
108 struct anv_entrypoint {
113 /* We use a big string constant to avoid lots of reloctions from the entry
114 * point table to lots of little strings. The entries in the entry point table
115 * store the index into this big string.
118 static const char strings[] =
119 % for _, name, _, _, _, _ in entrypoints:
124 static const struct anv_entrypoint entrypoints[] = {
125 % for _, _, _, num, h, _ in entrypoints:
126 { ${offsets[num]}, ${'{:0=#8x}'.format(h)} },
130 /* Weak aliases for all potential implementations. These will resolve to
131 * NULL if they're not defined, which lets the resolve_entrypoint() function
132 * either pick the correct entry point.
135 % for layer in ['anv', 'gen7', 'gen75', 'gen8', 'gen9', 'gen10']:
136 % for type_, name, args, _, _, guard in entrypoints:
137 % if guard is not None:
140 ${type_} ${layer}_${name}(${args}) __attribute__ ((weak));
141 % if guard is not None:
146 const struct anv_dispatch_table ${layer}_layer = {
147 % for _, name, args, _, _, guard in entrypoints:
148 % if guard is not None:
151 .${name} = ${layer}_${name},
152 % if guard is not None:
159 static void * __attribute__ ((noinline))
160 anv_resolve_entrypoint(const struct gen_device_info *devinfo, uint32_t index)
162 if (devinfo == NULL) {
163 return anv_layer.entrypoints[index];
166 switch (devinfo->gen) {
168 if (gen10_layer.entrypoints[index])
169 return gen10_layer.entrypoints[index];
172 if (gen9_layer.entrypoints[index])
173 return gen9_layer.entrypoints[index];
176 if (gen8_layer.entrypoints[index])
177 return gen8_layer.entrypoints[index];
180 if (devinfo->is_haswell && gen75_layer.entrypoints[index])
181 return gen75_layer.entrypoints[index];
183 if (gen7_layer.entrypoints[index])
184 return gen7_layer.entrypoints[index];
187 return anv_layer.entrypoints[index];
189 unreachable("unsupported gen\\n");
194 * size ${hash_size} entries
195 * collisions entries:
196 % for i in xrange(10):
197 * ${i}${'+' if i == 9 else ''} ${collisions[i]}
201 #define none ${'{:#x}'.format(none)}
202 static const uint16_t map[] = {
203 % for i in xrange(0, hash_size, 8):
204 % for j in xrange(i, i + 8):
205 ## This is 6 because the 0x is counted in the length
206 % if mapping[j] & 0xffff == 0xffff:
209 ${'{:0=#6x}'.format(mapping[j] & 0xffff)},
216 anv_lookup_entrypoint(const struct gen_device_info *devinfo, const char *name)
218 static const uint32_t prime_factor = ${prime_factor};
219 static const uint32_t prime_step = ${prime_step};
220 const struct anv_entrypoint *e;
225 for (p = name; *p; p++)
226 hash = hash * prime_factor + *p;
230 i = map[h & ${hash_mask}];
235 } while (e->hash != hash);
237 if (strcmp(name, strings + e->name) != 0)
240 return anv_resolve_entrypoint(devinfo, i);
241 }"""), output_encoding
='utf-8')
246 HASH_MASK
= HASH_SIZE
- 1
248 PRIME_FACTOR
= 5024183
253 """Calculate the same hash value that Mesa will calculate in C."""
254 return functools
.reduce(
255 lambda h
, c
: (h
* PRIME_FACTOR
+ ord(c
)) & U32_MASK
, name
, 0)
258 def get_entrypoints(doc
, entrypoints_to_defines
):
259 """Extract the entry points from the registry."""
262 enabled_commands
= set()
263 for feature
in doc
.findall('./feature'):
264 assert feature
.attrib
['api'] == 'vulkan'
265 if float(feature
.attrib
['number']) > MAX_API_VERSION
:
268 for command
in feature
.findall('./require/command'):
269 enabled_commands
.add(command
.attrib
['name'])
271 supported
= set(ext
.name
for ext
in anv_extensions
.EXTENSIONS
)
272 for extension
in doc
.findall('.extensions/extension'):
273 if extension
.attrib
['name'] not in supported
:
276 assert extension
.attrib
['supported'] == 'vulkan'
277 for command
in extension
.findall('./require/command'):
278 enabled_commands
.add(command
.attrib
['name'])
281 for command
in doc
.findall('./commands/command'):
282 type = command
.find('./proto/type').text
283 fullname
= command
.find('./proto/name').text
285 if fullname
not in enabled_commands
:
288 shortname
= fullname
[2:]
289 params
= (''.join(p
.itertext()) for p
in command
.findall('./param'))
290 params
= ', '.join(params
)
291 guard
= entrypoints_to_defines
.get(fullname
)
292 entrypoints
.append((type, shortname
, params
, index
, cal_hash(fullname
), guard
))
298 def get_entrypoints_defines(doc
):
299 """Maps entry points to extension defines."""
300 entrypoints_to_defines
= {}
302 for extension
in doc
.findall('./extensions/extension[@protect]'):
303 define
= extension
.attrib
['protect']
305 for entrypoint
in extension
.findall('./require/command'):
306 fullname
= entrypoint
.attrib
['name']
307 entrypoints_to_defines
[fullname
] = define
309 return entrypoints_to_defines
312 def gen_code(entrypoints
):
313 """Generate the C code."""
316 for _
, name
, _
, _
, _
, _
in entrypoints
:
318 i
+= 2 + len(name
) + 1
320 mapping
= [NONE
] * HASH_SIZE
321 collisions
= [0] * 10
322 for _
, name
, _
, num
, h
, _
in entrypoints
:
324 while mapping
[h
& HASH_MASK
] != NONE
:
330 collisions
[level
] += 1
331 mapping
[h
& HASH_MASK
] = num
333 return TEMPLATE_C
.render(entrypoints
=entrypoints
,
335 collisions
=collisions
,
338 prime_step
=PRIME_STEP
,
339 prime_factor
=PRIME_FACTOR
,
342 filename
=os
.path
.basename(__file__
))
346 parser
= argparse
.ArgumentParser()
347 parser
.add_argument('--outdir', help='Where to write the files.',
349 parser
.add_argument('--xml', help='Vulkan API XML file.', required
=True)
350 args
= parser
.parse_args()
352 doc
= et
.parse(args
.xml
)
353 entrypoints
= get_entrypoints(doc
, get_entrypoints_defines(doc
))
355 # Manually add CreateDmaBufImageINTEL for which we don't have an extension
357 entrypoints
.append(('VkResult', 'CreateDmaBufImageINTEL',
358 'VkDevice device, ' +
359 'const VkDmaBufImageCreateInfo* pCreateInfo, ' +
360 'const VkAllocationCallbacks* pAllocator,' +
361 'VkDeviceMemory* pMem,' +
362 'VkImage* pImage', len(entrypoints
),
363 cal_hash('vkCreateDmaBufImageINTEL'), None))
365 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
367 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.h'), 'wb') as f
:
368 f
.write(TEMPLATE_H
.render(entrypoints
=entrypoints
,
369 filename
=os
.path
.basename(__file__
)))
370 with
open(os
.path
.join(args
.outdir
, 'anv_entrypoints.c'), 'wb') as f
:
371 f
.write(gen_code(entrypoints
))
374 if __name__
== '__main__':