Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Chia-I Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 2 | # |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 3 | # VK |
Chia-I Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 4 | # |
| 5 | # Copyright (C) 2014 LunarG, Inc. |
| 6 | # |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | # copy of this software and associated documentation files (the "Software"), |
| 9 | # to deal in the Software without restriction, including without limitation |
| 10 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | # and/or sell copies of the Software, and to permit persons to whom the |
| 12 | # Software is furnished to do so, subject to the following conditions: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice shall be included |
| 15 | # in all copies or substantial portions of the Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | # DEALINGS IN THE SOFTWARE. |
| 24 | # |
| 25 | # Authors: |
| 26 | # Chia-I Wu <olv@lunarg.com> |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 27 | |
| 28 | import sys |
| 29 | |
Courtney Goeltzenleuchter | a8c0628 | 2015-04-14 14:55:44 -0600 | [diff] [blame] | 30 | import vulkan |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 31 | |
Chia-I Wu | f7d6d3c | 2015-01-05 12:55:13 +0800 | [diff] [blame] | 32 | def generate_get_proc_addr_check(name): |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 33 | return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \ |
| 34 | " return NULL;" % ((name,) * 3) |
Chia-I Wu | f7d6d3c | 2015-01-05 12:55:13 +0800 | [diff] [blame] | 35 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame^] | 36 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 37 | class Subcommand(object): |
| 38 | def __init__(self, argv): |
| 39 | self.argv = argv |
Courtney Goeltzenleuchter | a8c0628 | 2015-04-14 14:55:44 -0600 | [diff] [blame] | 40 | self.headers = vulkan.headers |
| 41 | self.protos = vulkan.protos |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 42 | |
| 43 | def run(self): |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 44 | print(self.generate()) |
| 45 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame^] | 46 | def is_dispatchable_object_first_param(self, proto): |
| 47 | in_objs = proto.object_in_params() |
| 48 | non_dispatch_objs = ["VkInstance"] |
| 49 | param0 = proto.params[0] |
| 50 | return (len(in_objs) > 0) and (in_objs[0].ty == param0.ty) and (param0.ty not in non_dispatch_objs) |
| 51 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 52 | def generate(self): |
| 53 | copyright = self.generate_copyright() |
| 54 | header = self.generate_header() |
| 55 | body = self.generate_body() |
| 56 | footer = self.generate_footer() |
| 57 | |
| 58 | contents = [] |
| 59 | if copyright: |
| 60 | contents.append(copyright) |
| 61 | if header: |
| 62 | contents.append(header) |
| 63 | if body: |
| 64 | contents.append(body) |
| 65 | if footer: |
| 66 | contents.append(footer) |
| 67 | |
| 68 | return "\n\n".join(contents) |
| 69 | |
| 70 | def generate_copyright(self): |
| 71 | return """/* THIS FILE IS GENERATED. DO NOT EDIT. */ |
| 72 | |
| 73 | /* |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 74 | * Vulkan |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 75 | * |
| 76 | * Copyright (C) 2014 LunarG, Inc. |
| 77 | * |
| 78 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 79 | * copy of this software and associated documentation files (the "Software"), |
| 80 | * to deal in the Software without restriction, including without limitation |
| 81 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 82 | * and/or sell copies of the Software, and to permit persons to whom the |
| 83 | * Software is furnished to do so, subject to the following conditions: |
| 84 | * |
| 85 | * The above copyright notice and this permission notice shall be included |
| 86 | * in all copies or substantial portions of the Software. |
| 87 | * |
| 88 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 89 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 90 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 91 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 92 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 93 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 94 | * DEALINGS IN THE SOFTWARE. |
| 95 | */""" |
| 96 | |
| 97 | def generate_header(self): |
Chia-I Wu | 6ae460f | 2014-09-13 13:36:06 +0800 | [diff] [blame] | 98 | return "\n".join(["#include <" + h + ">" for h in self.headers]) |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 99 | |
| 100 | def generate_body(self): |
| 101 | pass |
| 102 | |
| 103 | def generate_footer(self): |
| 104 | pass |
| 105 | |
Chia-I Wu | d4a76ae | 2015-01-04 10:15:48 +0800 | [diff] [blame] | 106 | class LoaderEntrypointsSubcommand(Subcommand): |
Chia-I Wu | 2686435 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 107 | def generate_header(self): |
Chia-I Wu | 1d6731b | 2015-04-11 15:34:07 +0800 | [diff] [blame] | 108 | return "#include \"loader.h\"" |
Chia-I Wu | 2686435 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 109 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame^] | 110 | def _is_loader_special_case(self, proto): |
| 111 | if proto.name in ["GetProcAddr", "EnumerateGpus", "EnumerateLayers"]: |
| 112 | return True |
Chia-I Wu | 565734c | 2015-01-04 15:32:52 +0800 | [diff] [blame] | 113 | |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame^] | 114 | return not self.is_dispatchable_object_first_param(proto) |
Chia-I Wu | 565734c | 2015-01-04 15:32:52 +0800 | [diff] [blame] | 115 | |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 116 | def _generate_object_setup(self, proto): |
| 117 | method = "loader_init_data" |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 118 | cond = "res == VK_SUCCESS" |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 119 | |
| 120 | if "Get" in proto.name: |
| 121 | method = "loader_set_data" |
| 122 | |
| 123 | setup = [] |
| 124 | |
| 125 | if proto.name == "AllocDescriptorSets": |
| 126 | psets = proto.params[-2].name |
| 127 | pcount = proto.params[-1].name |
| 128 | setup.append("uint32_t i;") |
| 129 | setup.append("for (i = 0; i < *%s; i++)" % pcount) |
| 130 | setup.append(" %s(%s[i], disp);" % (method, psets)) |
| 131 | else: |
| 132 | obj_params = proto.object_out_params() |
| 133 | for param in obj_params: |
| 134 | setup.append("%s(*%s, disp);" % (method, param.name)) |
| 135 | |
| 136 | if setup: |
| 137 | joined = "\n ".join(setup) |
| 138 | setup = [] |
| 139 | setup.append(" if (%s) {" % cond) |
| 140 | setup.append(" " + joined) |
| 141 | setup.append(" }") |
| 142 | |
| 143 | return "\n".join(setup) |
| 144 | |
Chia-I Wu | 2686435 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 145 | def _generate_loader_dispatch_entrypoints(self, qual=""): |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 146 | if qual: |
| 147 | qual += " " |
| 148 | |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 149 | funcs = [] |
| 150 | for proto in self.protos: |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame^] | 151 | if self._is_loader_special_case(proto): |
Tobin Ehlis | 66bbbf5 | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 152 | continue |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 153 | func = [] |
| 154 | |
| 155 | obj_setup = self._generate_object_setup(proto) |
| 156 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 157 | func.append(qual + proto.c_func(prefix="vk", attr="VKAPI")) |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 158 | func.append("{") |
| 159 | |
| 160 | # declare local variables |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 161 | func.append(" const VkLayerDispatchTable *disp;") |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 162 | if proto.ret != 'void' and obj_setup: |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 163 | func.append(" VkResult res;") |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 164 | func.append("") |
| 165 | |
Jon Ashburn | 630e44f | 2015-04-08 21:33:34 -0600 | [diff] [blame] | 166 | # get dispatch table |
| 167 | func.append(" disp = loader_get_data(%s);" % |
| 168 | proto.params[0].name) |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 169 | func.append("") |
| 170 | |
| 171 | # dispatch! |
| 172 | dispatch = "disp->%s;" % proto.c_call() |
| 173 | if proto.ret == 'void': |
| 174 | func.append(" " + dispatch) |
| 175 | elif not obj_setup: |
| 176 | func.append(" return " + dispatch) |
| 177 | else: |
| 178 | func.append(" res = " + dispatch) |
| 179 | func.append(obj_setup) |
| 180 | func.append("") |
| 181 | func.append(" return res;") |
| 182 | |
| 183 | func.append("}") |
| 184 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 185 | if 'WsiX11AssociateConnection' == proto.name: |
Ian Elliott | e977a6c | 2015-02-26 14:34:52 -0700 | [diff] [blame] | 186 | funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)") |
Chia-I Wu | 67a81c8 | 2015-04-11 16:07:13 +0800 | [diff] [blame] | 187 | |
| 188 | funcs.append("\n".join(func)) |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 189 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 190 | funcs.append("#endif") |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 191 | return "\n\n".join(funcs) |
| 192 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 193 | def generate_body(self): |
Chia-I Wu | 2686435 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 194 | body = [self._generate_loader_dispatch_entrypoints("LOADER_EXPORT")] |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 195 | |
| 196 | return "\n\n".join(body) |
| 197 | |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 198 | class DispatchTableOpsSubcommand(Subcommand): |
| 199 | def run(self): |
| 200 | if len(self.argv) != 1: |
| 201 | print("DispatchTableOpsSubcommand: <prefix> unspecified") |
| 202 | return |
| 203 | |
| 204 | self.prefix = self.argv[0] |
| 205 | super().run() |
| 206 | |
| 207 | def generate_header(self): |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 208 | return "\n".join(["#include <vulkan.h>", |
| 209 | "#include <vkLayer.h>", |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 210 | "#include <string.h>", |
| 211 | "#include \"loader_platform.h\""]) |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 212 | |
| 213 | def _generate_init(self): |
| 214 | stmts = [] |
| 215 | for proto in self.protos: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 216 | if 'WsiX11AssociateConnection' == proto.name: |
Ian Elliott | e977a6c | 2015-02-26 14:34:52 -0700 | [diff] [blame] | 217 | stmts.append("#if defined(__linux__) || defined(XCB_NVIDIA)") |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 218 | if proto.name == "GetProcAddr": |
| 219 | stmts.append("table->%s = gpa; /* direct assignment */" % |
| 220 | proto.name) |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame^] | 221 | elif self.is_dispatchable_object_first_param(proto): |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 222 | stmts.append("table->%s = (PFN_vk%s) gpa(gpu, \"vk%s\");" % |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 223 | (proto.name, proto.name, proto.name)) |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 224 | stmts.append("#endif") |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 225 | |
| 226 | func = [] |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 227 | func.append("static inline void %s_initialize_dispatch_table(VkLayerDispatchTable *table," |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 228 | % self.prefix) |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 229 | func.append("%s PFN_vkGetProcAddr gpa," |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 230 | % (" " * len(self.prefix))) |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 231 | func.append("%s VkPhysicalGpu gpu)" |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 232 | % (" " * len(self.prefix))) |
| 233 | func.append("{") |
| 234 | func.append(" %s" % "\n ".join(stmts)) |
| 235 | func.append("}") |
| 236 | |
| 237 | return "\n".join(func) |
| 238 | |
| 239 | def _generate_lookup(self): |
| 240 | lookups = [] |
| 241 | for proto in self.protos: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 242 | if 'WsiX11AssociateConnection' == proto.name: |
Ian Elliott | e977a6c | 2015-02-26 14:34:52 -0700 | [diff] [blame] | 243 | lookups.append("#if defined(__linux__) || defined(XCB_NVIDIA)") |
Jon Ashburn | eb2728b | 2015-04-10 14:33:07 -0600 | [diff] [blame^] | 244 | if self.is_dispatchable_object_first_param(proto): |
| 245 | lookups.append("if (!strcmp(name, \"%s\"))" % (proto.name)) |
| 246 | lookups.append(" return (void *) table->%s;" |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 247 | % (proto.name)) |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 248 | lookups.append("#endif") |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 249 | |
| 250 | func = [] |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 251 | func.append("static inline void *%s_lookup_dispatch_table(const VkLayerDispatchTable *table," |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 252 | % self.prefix) |
| 253 | func.append("%s const char *name)" |
| 254 | % (" " * len(self.prefix))) |
| 255 | func.append("{") |
Chia-I Wu | f7d6d3c | 2015-01-05 12:55:13 +0800 | [diff] [blame] | 256 | func.append(generate_get_proc_addr_check("name")) |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 257 | func.append("") |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 258 | func.append(" name += 2;") |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 259 | func.append(" %s" % "\n ".join(lookups)) |
| 260 | func.append("") |
| 261 | func.append(" return NULL;") |
| 262 | func.append("}") |
| 263 | |
| 264 | return "\n".join(func) |
| 265 | |
| 266 | def generate_body(self): |
| 267 | body = [self._generate_init(), |
| 268 | self._generate_lookup()] |
| 269 | |
| 270 | return "\n\n".join(body) |
| 271 | |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 272 | class IcdDummyEntrypointsSubcommand(Subcommand): |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 273 | def run(self): |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 274 | if len(self.argv) == 1: |
| 275 | self.prefix = self.argv[0] |
| 276 | self.qual = "static" |
| 277 | else: |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 278 | self.prefix = "vk" |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 279 | self.qual = "ICD_EXPORT" |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 280 | |
| 281 | super().run() |
| 282 | |
| 283 | def generate_header(self): |
| 284 | return "#include \"icd.h\"" |
| 285 | |
| 286 | def _generate_stub_decl(self, proto): |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 287 | return proto.c_pretty_decl(self.prefix + proto.name, attr="VKAPI") |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 288 | |
| 289 | def _generate_stubs(self): |
| 290 | stubs = [] |
| 291 | for proto in self.protos: |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 292 | decl = self._generate_stub_decl(proto) |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 293 | if proto.ret != "void": |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 294 | stmt = " return VK_ERROR_UNKNOWN;\n" |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 295 | else: |
| 296 | stmt = "" |
| 297 | |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 298 | stubs.append("%s %s\n{\n%s}" % (self.qual, decl, stmt)) |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 299 | |
| 300 | return "\n\n".join(stubs) |
| 301 | |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 302 | def generate_body(self): |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 303 | return self._generate_stubs() |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 304 | |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 305 | class IcdGetProcAddrSubcommand(IcdDummyEntrypointsSubcommand): |
| 306 | def generate_header(self): |
| 307 | return "\n".join(["#include <string.h>", "#include \"icd.h\""]) |
| 308 | |
| 309 | def generate_body(self): |
| 310 | for proto in self.protos: |
| 311 | if proto.name == "GetProcAddr": |
| 312 | gpa_proto = proto |
| 313 | |
| 314 | gpa_decl = self._generate_stub_decl(gpa_proto) |
| 315 | gpa_pname = gpa_proto.params[-1].name |
| 316 | |
| 317 | lookups = [] |
| 318 | for proto in self.protos: |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 319 | if 'WsiX11AssociateConnection' == proto.name: |
Ian Elliott | e977a6c | 2015-02-26 14:34:52 -0700 | [diff] [blame] | 320 | lookups.append("#if defined(__linux__) || defined(XCB_NVIDIA)") |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 321 | lookups.append("if (!strcmp(%s, \"%s\"))" % |
| 322 | (gpa_pname, proto.name)) |
| 323 | lookups.append(" return (%s) %s%s;" % |
| 324 | (gpa_proto.ret, self.prefix, proto.name)) |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 325 | lookups.append("#endif") |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 326 | |
| 327 | body = [] |
| 328 | body.append("%s %s" % (self.qual, gpa_decl)) |
| 329 | body.append("{") |
Chia-I Wu | f7d6d3c | 2015-01-05 12:55:13 +0800 | [diff] [blame] | 330 | body.append(generate_get_proc_addr_check(gpa_pname)) |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 331 | body.append("") |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 332 | body.append(" %s += 2;" % gpa_pname) |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 333 | body.append(" %s" % "\n ".join(lookups)) |
| 334 | body.append("") |
| 335 | body.append(" return NULL;") |
| 336 | body.append("}") |
| 337 | |
| 338 | return "\n".join(body) |
| 339 | |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 340 | class LayerInterceptProcSubcommand(Subcommand): |
| 341 | def run(self): |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 342 | self.prefix = "vk" |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 343 | |
| 344 | # we could get the list from argv if wanted |
| 345 | self.intercepted = [proto.name for proto in self.protos |
Jon Ashburn | 457c5d1 | 2015-01-29 16:54:38 -0700 | [diff] [blame] | 346 | if proto.name not in ["EnumerateGpus"]] |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 347 | |
| 348 | for proto in self.protos: |
| 349 | if proto.name == "GetProcAddr": |
| 350 | self.gpa = proto |
| 351 | |
| 352 | super().run() |
| 353 | |
| 354 | def generate_header(self): |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 355 | return "\n".join(["#include <string.h>", "#include \"vkLayer.h\""]) |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 356 | |
| 357 | def generate_body(self): |
| 358 | lookups = [] |
| 359 | for proto in self.protos: |
| 360 | if proto.name not in self.intercepted: |
| 361 | lookups.append("/* no %s%s */" % (self.prefix, proto.name)) |
| 362 | continue |
| 363 | |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 364 | if 'WsiX11AssociateConnection' == proto.name: |
Ian Elliott | e977a6c | 2015-02-26 14:34:52 -0700 | [diff] [blame] | 365 | lookups.append("#if defined(__linux__) || defined(XCB_NVIDIA)") |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 366 | lookups.append("if (!strcmp(name, \"%s\"))" % proto.name) |
| 367 | lookups.append(" return (%s) %s%s;" % |
| 368 | (self.gpa.ret, self.prefix, proto.name)) |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 369 | lookups.append("#endif") |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 370 | |
| 371 | body = [] |
Ian Elliott | a742a62 | 2015-02-18 12:38:04 -0700 | [diff] [blame] | 372 | body.append("static inline %s layer_intercept_proc(const char *name)" % |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 373 | self.gpa.ret) |
| 374 | body.append("{") |
| 375 | body.append(generate_get_proc_addr_check("name")) |
| 376 | body.append("") |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 377 | body.append(" name += 2;") |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 378 | body.append(" %s" % "\n ".join(lookups)) |
| 379 | body.append("") |
| 380 | body.append(" return NULL;") |
| 381 | body.append("}") |
| 382 | |
| 383 | return "\n".join(body) |
| 384 | |
Chia-I Wu | d98a23c | 2015-04-11 10:56:50 +0800 | [diff] [blame] | 385 | class WinDefFileSubcommand(Subcommand): |
| 386 | def run(self): |
| 387 | library_exports = { |
| 388 | "all": [], |
| 389 | "icd": [ |
| 390 | "EnumerateGpus", |
| 391 | "CreateInstance", |
| 392 | "DestroyInstance", |
| 393 | "GetProcAddr", |
| 394 | ], |
| 395 | "layer": [ |
| 396 | "GetProcAddr", |
| 397 | "EnumerateLayers", |
| 398 | ], |
| 399 | } |
| 400 | |
| 401 | if len(self.argv) != 2 or self.argv[1] not in library_exports: |
| 402 | print("WinDefFileSubcommand: <library-name> {%s}" % |
| 403 | "|".join(library_exports.keys())) |
| 404 | return |
| 405 | |
| 406 | self.library = self.argv[0] |
| 407 | self.exports = library_exports[self.argv[1]] |
| 408 | |
| 409 | super().run() |
| 410 | |
| 411 | def generate_copyright(self): |
| 412 | return """; THIS FILE IS GENERATED. DO NOT EDIT. |
| 413 | |
| 414 | ;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 415 | ; Vulkan |
Chia-I Wu | d98a23c | 2015-04-11 10:56:50 +0800 | [diff] [blame] | 416 | ; |
| 417 | ; Copyright (C) 2015 LunarG, Inc. |
| 418 | ; |
| 419 | ; Permission is hereby granted, free of charge, to any person obtaining a |
| 420 | ; copy of this software and associated documentation files (the "Software"), |
| 421 | ; to deal in the Software without restriction, including without limitation |
| 422 | ; the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 423 | ; and/or sell copies of the Software, and to permit persons to whom the |
| 424 | ; Software is furnished to do so, subject to the following conditions: |
| 425 | ; |
| 426 | ; The above copyright notice and this permission notice shall be included |
| 427 | ; in all copies or substantial portions of the Software. |
| 428 | ; |
| 429 | ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 430 | ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 431 | ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 432 | ; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 433 | ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 434 | ; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 435 | ; DEALINGS IN THE SOFTWARE. |
| 436 | ;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;""" |
| 437 | |
| 438 | def generate_header(self): |
| 439 | return "; The following is required on Windows, for exporting symbols from the DLL" |
| 440 | |
| 441 | def generate_body(self): |
| 442 | body = [] |
| 443 | |
| 444 | body.append("LIBRARY " + self.library) |
| 445 | body.append("EXPORTS") |
| 446 | |
| 447 | for proto in self.protos: |
| 448 | if self.exports and proto.name not in self.exports: |
| 449 | continue |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 450 | body.append(" vk" + proto.name) |
Chia-I Wu | d98a23c | 2015-04-11 10:56:50 +0800 | [diff] [blame] | 451 | |
| 452 | return "\n".join(body) |
| 453 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 454 | def main(): |
| 455 | subcommands = { |
Chia-I Wu | d4a76ae | 2015-01-04 10:15:48 +0800 | [diff] [blame] | 456 | "loader-entrypoints": LoaderEntrypointsSubcommand, |
Chia-I Wu | 28b8d8c | 2015-01-04 10:19:50 +0800 | [diff] [blame] | 457 | "dispatch-table-ops": DispatchTableOpsSubcommand, |
Chia-I Wu | 731517d | 2015-01-03 23:48:15 +0800 | [diff] [blame] | 458 | "icd-dummy-entrypoints": IcdDummyEntrypointsSubcommand, |
Chia-I Wu | 58f2085 | 2015-01-04 00:11:17 +0800 | [diff] [blame] | 459 | "icd-get-proc-addr": IcdGetProcAddrSubcommand, |
Chia-I Wu | f4bd2e4 | 2015-01-05 12:56:13 +0800 | [diff] [blame] | 460 | "layer-intercept-proc": LayerInterceptProcSubcommand, |
Chia-I Wu | d98a23c | 2015-04-11 10:56:50 +0800 | [diff] [blame] | 461 | "win-def-file": WinDefFileSubcommand, |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | if len(sys.argv) < 2 or sys.argv[1] not in subcommands: |
| 465 | print("Usage: %s <subcommand> [options]" % sys.argv[0]) |
| 466 | print |
| 467 | print("Available sucommands are: %s" % " ".join(subcommands)) |
| 468 | exit(1) |
| 469 | |
| 470 | subcmd = subcommands[sys.argv[1]](sys.argv[2:]) |
| 471 | subcmd.run() |
| 472 | |
| 473 | if __name__ == "__main__": |
| 474 | main() |