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
25 import fileinput
, re
, sys
27 # Each function typedef in the vulkan.h header is all on one line and matches
28 # this regepx. We hope that won't change.
30 p
= re
.compile('typedef ([^ ]*) *\((?:VKAPI_PTR)? *\*PFN_vk([^(]*)\)(.*);')
34 # We generate a static hash table for entry point lookup
35 # (vkGetProcAddress). We use a linear congruential generator for our hash
36 # function and a power-of-two size table. The prime numbers are determined
42 hash_mask
= hash_size
- 1
44 prime_factor
= 5024183
50 h
= (h
* prime_factor
+ ord(c
)) & u32_mask
54 def get_platform_guard_macro(name
):
56 return "VK_USE_PLATFORM_XLIB_KHR"
58 return "VK_USE_PLATFORM_XCB_KHR"
59 elif "Wayland" in name
:
60 return "VK_USE_PLATFORM_WAYLAND_KHR"
62 return "VK_USE_PLATFORM_MIR_KHR"
63 elif "Android" in name
:
64 return "VK_USE_PLATFORM_ANDROID_KHR"
66 return "VK_USE_PLATFORM_WIN32_KHR"
70 def print_guard_start(name
):
71 guard
= get_platform_guard_macro(name
)
73 print "#ifdef {0}".format(guard
)
75 def print_guard_end(name
):
76 guard
= get_platform_guard_macro(name
)
78 print "#endif // {0}".format(guard
)
83 if (sys
.argv
[1] == "header"):
86 elif (sys
.argv
[1] == "code"):
90 # Parse the entry points in the header
93 for line
in fileinput
.input():
96 if m
.group(2) == 'VoidFunction':
98 fullname
= "vk" + m
.group(2)
100 entrypoints
.append((m
.group(1), m
.group(2), m
.group(3), i
, h
))
103 # For outputting entrypoints.h we generate a radv_EntryPoint() prototype
107 print "/* This file generated from vk_gen.py, don't edit directly. */\n"
109 print "struct radv_dispatch_table {"
111 print " void *entrypoints[%d];" % len(entrypoints
)
114 for type, name
, args
, num
, h
in entrypoints
:
115 guard
= get_platform_guard_macro(name
)
116 if guard
is not None:
117 print "#ifdef {0}".format(guard
)
118 print " PFN_vk{0} {0};".format(name
)
120 print " void *{0};".format(name
)
123 print " PFN_vk{0} {0};".format(name
)
128 for type, name
, args
, num
, h
in entrypoints
:
129 print_guard_start(name
)
130 print "%s radv_%s%s;" % (type, name
, args
)
131 print_guard_end(name
)
137 * Copyright © 2015 Intel Corporation
139 * Permission is hereby granted, free of charge, to any person obtaining a
140 * copy of this software and associated documentation files (the "Software"),
141 * to deal in the Software without restriction, including without limitation
142 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
143 * and/or sell copies of the Software, and to permit persons to whom the
144 * Software is furnished to do so, subject to the following conditions:
146 * The above copyright notice and this permission notice (including the next
147 * paragraph) shall be included in all copies or substantial portions of the
150 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
152 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
153 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
154 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
155 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
159 /* DO NOT EDIT! This is a generated file. */
161 #include "radv_private.h"
163 struct radv_entrypoint {
168 /* We use a big string constant to avoid lots of reloctions from the entry
169 * point table to lots of little strings. The entries in the entry point table
170 * store the index into this big string.
173 static const char strings[] ="""
177 for type, name
, args
, num
, h
in entrypoints
:
178 print " \"vk%s\\0\"" % name
180 i
+= 2 + len(name
) + 1
183 # Now generate the table of all entry points
185 print "\nstatic const struct radv_entrypoint entrypoints[] = {"
186 for type, name
, args
, num
, h
in entrypoints
:
187 print " { %5d, 0x%08x }," % (offsets
[num
], h
)
192 /* Weak aliases for all potential implementations. These will resolve to
193 * NULL if they're not defined, which lets the resolve_entrypoint() function
194 * either pick the correct entry point.
198 for layer
in [ "radv" ]:
199 for type, name
, args
, num
, h
in entrypoints
:
200 print_guard_start(name
)
201 print "%s %s_%s%s __attribute__ ((weak));" % (type, layer
, name
, args
)
202 print_guard_end(name
)
203 print "\nconst struct radv_dispatch_table %s_layer = {" % layer
204 for type, name
, args
, num
, h
in entrypoints
:
205 print_guard_start(name
)
206 print " .%s = %s_%s," % (name
, layer
, name
)
207 print_guard_end(name
)
212 void * __attribute__ ((noinline))
213 radv_resolve_entrypoint(uint32_t index)
215 return radv_layer.entrypoints[index];
219 # Now generate the hash table used for entry point look up. This is a
220 # uint16_t table of entry point indices. We use 0xffff to indicate an entry
221 # in the hash table is empty.
223 map = [none
for f
in xrange(hash_size
)]
224 collisions
= [0 for f
in xrange(10)]
225 for type, name
, args
, num
, h
in entrypoints
:
227 while map[h
& hash_mask
] != none
:
233 collisions
[level
] += 1
234 map[h
& hash_mask
] = num
236 print "/* Hash table stats:"
237 print " * size %d entries" % hash_size
238 print " * collisions entries"
245 print " * %2d%s %4d" % (i
, plus
, collisions
[i
])
248 print "#define none 0x%04x\n" % none
250 print "static const uint16_t map[] = {"
251 for i
in xrange(0, hash_size
, 8):
253 for j
in xrange(i
, i
+ 8):
254 if map[j
] & 0xffff == 0xffff:
257 print "0x%04x," % (map[j
] & 0xffff),
262 # Finally we generate the hash table lookup function. The hash function and
263 # linear probing algorithm matches the hash table generated above.
267 radv_lookup_entrypoint(const char *name)
269 static const uint32_t prime_factor = %d;
270 static const uint32_t prime_step = %d;
271 const struct radv_entrypoint *e;
276 for (p = name; *p; p++)
277 hash = hash * prime_factor + *p;
286 } while (e->hash != hash);
288 if (strcmp(name, strings + e->name) != 0)
291 return radv_resolve_entrypoint(i);
293 """ % (prime_factor
, prime_step
, hash_mask
)