3 # Copyright © 2015 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
26 import xml
.etree
.ElementTree
as ET
30 supported_extensions
= [
31 'VK_AMD_draw_indirect_count',
32 'VK_NV_dedicated_allocation',
33 'VK_KHR_descriptor_update_template',
34 'VK_KHR_get_physical_device_properties2',
35 'VK_KHR_incremental_present',
36 'VK_KHR_maintenance1',
37 'VK_KHR_push_descriptor',
38 'VK_KHR_sampler_mirror_clamp_to_edge',
39 'VK_KHR_shader_draw_parameters',
42 'VK_KHR_wayland_surface',
44 'VK_KHR_xlib_surface',
47 # We generate a static hash table for entry point lookup
48 # (vkGetProcAddress). We use a linear congruential generator for our hash
49 # function and a power-of-two size table. The prime numbers are determined
55 hash_mask
= hash_size
- 1
57 prime_factor
= 5024183
63 h
= (h
* prime_factor
+ ord(c
)) & u32_mask
67 def print_guard_start(guard
):
69 print "#ifdef {0}".format(guard
)
71 def print_guard_end(guard
):
73 print "#endif // {0}".format(guard
)
78 if (sys
.argv
[1] == "header"):
81 elif (sys
.argv
[1] == "code"):
85 # Extract the entry points from the registry
86 def get_entrypoints(doc
, entrypoints_to_defines
):
89 enabled_commands
= set()
90 for feature
in doc
.findall('./feature'):
91 assert feature
.attrib
['api'] == 'vulkan'
92 if float(feature
.attrib
['number']) > max_api_version
:
95 for command
in feature
.findall('./require/command'):
96 enabled_commands
.add(command
.attrib
['name'])
98 for extension
in doc
.findall('.extensions/extension'):
99 if extension
.attrib
['name'] not in supported_extensions
:
102 assert extension
.attrib
['supported'] == 'vulkan'
103 for command
in extension
.findall('./require/command'):
104 enabled_commands
.add(command
.attrib
['name'])
107 for command
in doc
.findall('./commands/command'):
108 type = command
.find('./proto/type').text
109 fullname
= command
.find('./proto/name').text
111 if fullname
not in enabled_commands
:
114 shortname
= fullname
[2:]
115 params
= map(lambda p
: "".join(p
.itertext()), command
.findall('./param'))
116 params
= ', '.join(params
)
117 if fullname
in entrypoints_to_defines
:
118 guard
= entrypoints_to_defines
[fullname
]
121 entrypoints
.append((type, shortname
, params
, index
, hash(fullname
), guard
))
126 # Maps entry points to extension defines
127 def get_entrypoints_defines(doc
):
128 entrypoints_to_defines
= {}
129 extensions
= doc
.findall('./extensions/extension')
130 for extension
in extensions
:
131 define
= extension
.get('protect')
132 entrypoints
= extension
.findall('./require/command')
133 for entrypoint
in entrypoints
:
134 fullname
= entrypoint
.get('name')
135 entrypoints_to_defines
[fullname
] = define
136 return entrypoints_to_defines
138 doc
= ET
.parse(sys
.stdin
)
139 entrypoints
= get_entrypoints(doc
, get_entrypoints_defines(doc
))
141 # For outputting entrypoints.h we generate a radv_EntryPoint() prototype
145 print "/* This file generated from vk_gen.py, don't edit directly. */\n"
147 print "struct radv_dispatch_table {"
149 print " void *entrypoints[%d];" % len(entrypoints
)
152 for type, name
, args
, num
, h
, guard
in entrypoints
:
153 if guard
is not None:
154 print "#ifdef {0}".format(guard
)
155 print " PFN_vk{0} {0};".format(name
)
157 print " void *{0};".format(name
)
160 print " PFN_vk{0} {0};".format(name
)
165 for type, name
, args
, num
, h
, guard
in entrypoints
:
166 print_guard_start(guard
)
167 print "%s radv_%s(%s);" % (type, name
, args
)
168 print_guard_end(guard
)
174 * Copyright © 2015 Intel Corporation
176 * Permission is hereby granted, free of charge, to any person obtaining a
177 * copy of this software and associated documentation files (the "Software"),
178 * to deal in the Software without restriction, including without limitation
179 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
180 * and/or sell copies of the Software, and to permit persons to whom the
181 * Software is furnished to do so, subject to the following conditions:
183 * The above copyright notice and this permission notice (including the next
184 * paragraph) shall be included in all copies or substantial portions of the
187 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
188 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
189 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
190 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
192 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
196 /* DO NOT EDIT! This is a generated file. */
198 #include "radv_private.h"
200 struct radv_entrypoint {
205 /* We use a big string constant to avoid lots of reloctions from the entry
206 * point table to lots of little strings. The entries in the entry point table
207 * store the index into this big string.
210 static const char strings[] ="""
214 for type, name
, args
, num
, h
, guard
in entrypoints
:
215 print " \"vk%s\\0\"" % name
217 i
+= 2 + len(name
) + 1
220 # Now generate the table of all entry points
222 print "\nstatic const struct radv_entrypoint entrypoints[] = {"
223 for type, name
, args
, num
, h
, guard
in entrypoints
:
224 print " { %5d, 0x%08x }," % (offsets
[num
], h
)
229 /* Weak aliases for all potential implementations. These will resolve to
230 * NULL if they're not defined, which lets the resolve_entrypoint() function
231 * either pick the correct entry point.
235 for layer
in [ "radv" ]:
236 for type, name
, args
, num
, h
, guard
in entrypoints
:
237 print_guard_start(guard
)
238 print "%s %s_%s(%s) __attribute__ ((weak));" % (type, layer
, name
, args
)
239 print_guard_end(guard
)
240 print "\nconst struct radv_dispatch_table %s_layer = {" % layer
241 for type, name
, args
, num
, h
, guard
in entrypoints
:
242 print_guard_start(guard
)
243 print " .%s = %s_%s," % (name
, layer
, name
)
244 print_guard_end(guard
)
249 static void * __attribute__ ((noinline))
250 radv_resolve_entrypoint(uint32_t index)
252 return radv_layer.entrypoints[index];
256 # Now generate the hash table used for entry point look up. This is a
257 # uint16_t table of entry point indices. We use 0xffff to indicate an entry
258 # in the hash table is empty.
260 map = [none
for f
in xrange(hash_size
)]
261 collisions
= [0 for f
in xrange(10)]
262 for type, name
, args
, num
, h
, guard
in entrypoints
:
264 while map[h
& hash_mask
] != none
:
270 collisions
[level
] += 1
271 map[h
& hash_mask
] = num
273 print "/* Hash table stats:"
274 print " * size %d entries" % hash_size
275 print " * collisions entries"
282 print " * %2d%s %4d" % (i
, plus
, collisions
[i
])
285 print "#define none 0x%04x\n" % none
287 print "static const uint16_t map[] = {"
288 for i
in xrange(0, hash_size
, 8):
290 for j
in xrange(i
, i
+ 8):
291 if map[j
] & 0xffff == 0xffff:
294 print "0x%04x," % (map[j
] & 0xffff),
299 # Finally we generate the hash table lookup function. The hash function and
300 # linear probing algorithm matches the hash table generated above.
304 radv_lookup_entrypoint(const char *name)
306 static const uint32_t prime_factor = %d;
307 static const uint32_t prime_step = %d;
308 const struct radv_entrypoint *e;
313 for (p = name; *p; p++)
314 hash = hash * prime_factor + *p;
323 } while (e->hash != hash);
325 if (strcmp(name, strings + e->name) != 0)
328 return radv_resolve_entrypoint(i);
330 """ % (prime_factor
, prime_step
, hash_mask
)