OSDN Git Service

anv: do not export the Vulkan API
[android-x86/external-mesa.git] / src / intel / vulkan / anv_entrypoints_gen.py
1 # coding=utf-8
2 #
3 # Copyright © 2015 Intel Corporation
4 #
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:
11 #
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
14 # Software.
15 #
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
22 # IN THE SOFTWARE.
23 #
24
25 import fileinput, re, sys
26
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.
29
30 p = re.compile('typedef ([^ ]*) *\((?:VKAPI_PTR)? *\*PFN_vk([^(]*)\)(.*);')
31
32 entrypoints = []
33
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
37 # experimentally.
38
39 none = 0xffff
40 hash_size = 256
41 u32_mask = 2**32 - 1
42 hash_mask = hash_size - 1
43
44 prime_factor = 5024183
45 prime_step = 19
46
47 def hash(name):
48     h = 0;
49     for c in name:
50         h = (h * prime_factor + ord(c)) & u32_mask
51
52     return h
53
54 def get_platform_guard_macro(name):
55     if "Xlib" in name:
56         return "VK_USE_PLATFORM_XLIB_KHR"
57     elif "Xcb" in name:
58         return "VK_USE_PLATFORM_XCB_KHR"
59     elif "Wayland" in name:
60         return "VK_USE_PLATFORM_WAYLAND_KHR"
61     elif "Mir" in name:
62         return "VK_USE_PLATFORM_MIR_KHR"
63     elif "Android" in name:
64         return "VK_USE_PLATFORM_ANDROID_KHR"
65     elif "Win32" in name:
66         return "VK_USE_PLATFORM_WIN32_KHR"
67     else:
68         return None
69
70 def print_guard_start(name):
71     guard = get_platform_guard_macro(name)
72     if guard is not None:
73         print "#ifdef {0}".format(guard)
74
75 def print_guard_end(name):
76     guard = get_platform_guard_macro(name)
77     if guard is not None:
78         print "#endif // {0}".format(guard)
79
80 opt_header = False
81 opt_code = False
82
83 if (sys.argv[1] == "header"):
84     opt_header = True
85     sys.argv.pop()
86 elif (sys.argv[1] == "code"):
87     opt_code = True
88     sys.argv.pop()
89
90 # Parse the entry points in the header
91
92 i = 0
93 for line in fileinput.input():
94     m  = p.match(line)
95     if (m):
96         if m.group(2) == 'VoidFunction':
97             continue
98         fullname = "vk" + m.group(2)
99         h = hash(fullname)
100         entrypoints.append((m.group(1), m.group(2), m.group(3), i, h))
101         i = i + 1
102
103 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
104 # per entry point.
105
106 if opt_header:
107     print "/* This file generated from vk_gen.py, don't edit directly. */\n"
108
109     print "struct anv_dispatch_table {"
110     print "   union {"
111     print "      void *entrypoints[%d];" % len(entrypoints)
112     print "      struct {"
113
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)
119             print "#else"
120             print "         void *{0};".format(name)
121             print "#endif"
122         else:
123             print "         PFN_vk{0} {0};".format(name)
124     print "      };\n"
125     print "   };\n"
126     print "};\n"
127
128     print "void anv_set_dispatch_devinfo(const struct brw_device_info *info);\n"
129
130     for type, name, args, num, h in entrypoints:
131         print_guard_start(name)
132         print "%s anv_%s%s;" % (type, name, args)
133         print "%s gen7_%s%s;" % (type, name, args)
134         print "%s gen75_%s%s;" % (type, name, args)
135         print "%s gen8_%s%s;" % (type, name, args)
136         print "%s gen9_%s%s;" % (type, name, args)
137         print_guard_end(name)
138     exit()
139
140
141
142 print """/*
143  * Copyright © 2015 Intel Corporation
144  *
145  * Permission is hereby granted, free of charge, to any person obtaining a
146  * copy of this software and associated documentation files (the "Software"),
147  * to deal in the Software without restriction, including without limitation
148  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
149  * and/or sell copies of the Software, and to permit persons to whom the
150  * Software is furnished to do so, subject to the following conditions:
151  *
152  * The above copyright notice and this permission notice (including the next
153  * paragraph) shall be included in all copies or substantial portions of the
154  * Software.
155  *
156  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
157  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
158  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
159  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
160  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
161  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
162  * IN THE SOFTWARE.
163  */
164
165 /* DO NOT EDIT! This is a generated file. */
166
167 #include "anv_private.h"
168
169 struct anv_entrypoint {
170    uint32_t name;
171    uint32_t hash;
172 };
173
174 /* We use a big string constant to avoid lots of reloctions from the entry
175  * point table to lots of little strings. The entries in the entry point table
176  * store the index into this big string.
177  */
178
179 static const char strings[] ="""
180
181 offsets = []
182 i = 0;
183 for type, name, args, num, h in entrypoints:
184     print "   \"vk%s\\0\"" % name
185     offsets.append(i)
186     i += 2 + len(name) + 1
187 print "   ;"
188
189 # Now generate the table of all entry points
190
191 print "\nstatic const struct anv_entrypoint entrypoints[] = {"
192 for type, name, args, num, h in entrypoints:
193     print "   { %5d, 0x%08x }," % (offsets[num], h)
194 print "};\n"
195
196 print """
197
198 /* Weak aliases for all potential implementations. These will resolve to
199  * NULL if they're not defined, which lets the resolve_entrypoint() function
200  * either pick the correct entry point.
201  */
202 """
203
204 for layer in [ "anv", "gen7", "gen75", "gen8", "gen9" ]:
205     for type, name, args, num, h in entrypoints:
206         print_guard_start(name)
207         print "%s %s_%s%s __attribute__ ((weak));" % (type, layer, name, args)
208         print_guard_end(name)
209     print "\nconst struct anv_dispatch_table %s_layer = {" % layer
210     for type, name, args, num, h in entrypoints:
211         print_guard_start(name)
212         print "   .%s = %s_%s," % (name, layer, name)
213         print_guard_end(name)
214     print "};\n"
215
216 print """
217 static const struct brw_device_info *dispatch_devinfo;
218
219 void
220 anv_set_dispatch_devinfo(const struct brw_device_info *devinfo)
221 {
222    dispatch_devinfo = devinfo;
223 }
224
225 void * __attribute__ ((noinline))
226 anv_resolve_entrypoint(uint32_t index)
227 {
228    if (dispatch_devinfo == NULL) {
229       return anv_layer.entrypoints[index];
230    }
231
232    switch (dispatch_devinfo->gen) {
233    case 9:
234       if (gen9_layer.entrypoints[index])
235          return gen9_layer.entrypoints[index];
236       /* fall through */
237    case 8:
238       if (gen8_layer.entrypoints[index])
239          return gen8_layer.entrypoints[index];
240       /* fall through */
241    case 7:
242       if (dispatch_devinfo->is_haswell && gen75_layer.entrypoints[index])
243          return gen75_layer.entrypoints[index];
244
245       if (gen7_layer.entrypoints[index])
246          return gen7_layer.entrypoints[index];
247       /* fall through */
248    case 0:
249       return anv_layer.entrypoints[index];
250    default:
251       unreachable("unsupported gen\\n");
252    }
253 }
254 """
255
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.
259
260 map = [none for f in xrange(hash_size)]
261 collisions = [0 for f in xrange(10)]
262 for type, name, args, num, h in entrypoints:
263     level = 0
264     while map[h & hash_mask] != none:
265         h = h + prime_step
266         level = level + 1
267     if level > 9:
268         collisions[9] += 1
269     else:
270         collisions[level] += 1
271     map[h & hash_mask] = num
272
273 print "/* Hash table stats:"
274 print " * size %d entries" % hash_size
275 print " * collisions  entries"
276 for i in xrange(10):
277     if (i == 9):
278         plus = "+"
279     else:
280         plus = " "
281
282     print " *     %2d%s     %4d" % (i, plus, collisions[i])
283 print " */\n"
284
285 print "#define none 0x%04x\n" % none
286
287 print "static const uint16_t map[] = {"
288 for i in xrange(0, hash_size, 8):
289     print "   ",
290     for j in xrange(i, i + 8):
291         if map[j] & 0xffff == 0xffff:
292             print "  none,",
293         else:
294             print "0x%04x," % (map[j] & 0xffff),
295     print
296
297 print "};"    
298
299 # Finally we generate the hash table lookup function.  The hash function and
300 # linear probing algorithm matches the hash table generated above.
301
302 print """
303 void *
304 anv_lookup_entrypoint(const char *name)
305 {
306    static const uint32_t prime_factor = %d;
307    static const uint32_t prime_step = %d;
308    const struct anv_entrypoint *e;
309    uint32_t hash, h, i;
310    const char *p;
311
312    hash = 0;
313    for (p = name; *p; p++)
314       hash = hash * prime_factor + *p;
315
316    h = hash;
317    do {
318       i = map[h & %d];
319       if (i == none)
320          return NULL;
321       e = &entrypoints[i];
322       h += prime_step;
323    } while (e->hash != hash);
324
325    if (strcmp(name, strings + e->name) != 0)
326       return NULL;
327
328    return anv_resolve_entrypoint(i);
329 }
330 """ % (prime_factor, prime_step, hash_mask)