Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Chia-I Wu | 701f3f6 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 2 | # |
| 3 | # XGL |
| 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 | |
| 30 | import xgl |
| 31 | |
| 32 | class Subcommand(object): |
| 33 | def __init__(self, argv): |
| 34 | self.argv = argv |
Chia-I Wu | c4f24e8 | 2015-01-01 08:46:31 +0800 | [diff] [blame] | 35 | self.headers = xgl.headers |
| 36 | self.protos = xgl.protos |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 37 | |
| 38 | def run(self): |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 39 | print(self.generate()) |
| 40 | |
| 41 | def generate(self): |
| 42 | copyright = self.generate_copyright() |
| 43 | header = self.generate_header() |
| 44 | body = self.generate_body() |
| 45 | footer = self.generate_footer() |
| 46 | |
| 47 | contents = [] |
| 48 | if copyright: |
| 49 | contents.append(copyright) |
| 50 | if header: |
| 51 | contents.append(header) |
| 52 | if body: |
| 53 | contents.append(body) |
| 54 | if footer: |
| 55 | contents.append(footer) |
| 56 | |
| 57 | return "\n\n".join(contents) |
| 58 | |
| 59 | def generate_copyright(self): |
| 60 | return """/* THIS FILE IS GENERATED. DO NOT EDIT. */ |
| 61 | |
| 62 | /* |
| 63 | * XGL |
| 64 | * |
| 65 | * Copyright (C) 2014 LunarG, Inc. |
| 66 | * |
| 67 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 68 | * copy of this software and associated documentation files (the "Software"), |
| 69 | * to deal in the Software without restriction, including without limitation |
| 70 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 71 | * and/or sell copies of the Software, and to permit persons to whom the |
| 72 | * Software is furnished to do so, subject to the following conditions: |
| 73 | * |
| 74 | * The above copyright notice and this permission notice shall be included |
| 75 | * in all copies or substantial portions of the Software. |
| 76 | * |
| 77 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 78 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 79 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 80 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 81 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 82 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 83 | * DEALINGS IN THE SOFTWARE. |
| 84 | */""" |
| 85 | |
| 86 | def generate_header(self): |
Chia-I Wu | 6bdf019 | 2014-09-13 13:36:06 +0800 | [diff] [blame] | 87 | return "\n".join(["#include <" + h + ">" for h in self.headers]) |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 88 | |
| 89 | def generate_body(self): |
| 90 | pass |
| 91 | |
| 92 | def generate_footer(self): |
| 93 | pass |
| 94 | |
Chia-I Wu | 2e4f530 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 95 | class LoaderSubcommand(Subcommand): |
| 96 | def generate_header(self): |
| 97 | return "#include \"loader.h\"" |
| 98 | |
| 99 | def _generate_loader_dispatch_entrypoints(self, qual=""): |
Chia-I Wu | 1930060 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 100 | if qual: |
| 101 | qual += " " |
| 102 | |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 103 | funcs = [] |
| 104 | for proto in self.protos: |
Tobin Ehlis | ea7fe0b | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 105 | if not xgl.is_dispatchable(proto): |
| 106 | continue |
| 107 | decl = proto.c_func(prefix="xgl", attr="XGLAPI") |
| 108 | stmt = "(*disp)->%s" % proto.c_call() |
Chia-I Wu | 2e4f530 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 109 | if proto.name == "CreateDevice": |
Tobin Ehlis | ea7fe0b | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 110 | funcs.append("%s%s\n" |
| 111 | "{\n" |
Jon Ashburn | 613b0c1 | 2014-11-17 10:17:37 -0700 | [diff] [blame] | 112 | " loader_activate_layers(%s, %s);\n" |
Tobin Ehlis | ea7fe0b | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 113 | " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n" |
Jon Ashburn | e05c8c6 | 2014-12-03 14:30:48 -0700 | [diff] [blame] | 114 | " const XGL_LAYER_DISPATCH_TABLE **disp =\n" |
| 115 | " (const XGL_LAYER_DISPATCH_TABLE **) wrapped_obj->baseObject;\n" |
Tobin Ehlis | ea7fe0b | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 116 | " %s = wrapped_obj->nextObject;\n" |
Jon Ashburn | e05c8c6 | 2014-12-03 14:30:48 -0700 | [diff] [blame] | 117 | " XGL_RESULT res = %s;\n" |
| 118 | " *(const XGL_LAYER_DISPATCH_TABLE **) (*%s) = *disp;\n" |
| 119 | " return res;\n" |
| 120 | "}" % (qual, decl, proto.params[0].name, proto.params[1].name, |
| 121 | proto.params[0].name, proto.params[0].name, stmt, |
| 122 | proto.params[-1].name)) |
| 123 | elif xgl.does_function_create_object(proto.name) and qual == "LOADER_EXPORT ": |
| 124 | funcs.append("%s%s\n" |
| 125 | "{\n" |
| 126 | " const XGL_LAYER_DISPATCH_TABLE **disp =\n" |
| 127 | " (const XGL_LAYER_DISPATCH_TABLE **) %s;\n" |
| 128 | " XGL_RESULT res = %s;\n" |
| 129 | " *(const XGL_LAYER_DISPATCH_TABLE **) (*%s) = *disp;\n" |
| 130 | " return res;\n" |
| 131 | "}" % (qual, decl, proto.params[0].name, stmt, proto.params[-1].name)) |
Chia-I Wu | 2e4f530 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 132 | elif proto.name == "GetMultiGpuCompatibility": |
Jon Ashburn | e05c8c6 | 2014-12-03 14:30:48 -0700 | [diff] [blame] | 133 | funcs.append("%s%s\n" |
| 134 | "{\n" |
| 135 | " XGL_BASE_LAYER_OBJECT* wrapped_obj0 = (XGL_BASE_LAYER_OBJECT*)%s;\n" |
| 136 | " XGL_BASE_LAYER_OBJECT* wrapped_obj1 = (XGL_BASE_LAYER_OBJECT*)%s;\n" |
| 137 | " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n" |
| 138 | " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj0->baseObject;\n" |
| 139 | " %s = wrapped_obj0->nextObject;\n" |
| 140 | " %s = wrapped_obj1->nextObject;\n" |
| 141 | " return %s;\n" |
| 142 | "}" % (qual, decl, proto.params[0].name, proto.params[1].name, |
| 143 | proto.params[0].name, proto.params[1].name, stmt)) |
Chia-I Wu | 2e4f530 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 144 | elif proto.params[0].ty != "XGL_PHYSICAL_GPU": |
Jon Ashburn | e05c8c6 | 2014-12-03 14:30:48 -0700 | [diff] [blame] | 145 | if proto.ret != "XGL_VOID": |
| 146 | stmt = "return " + stmt |
Tobin Ehlis | ea7fe0b | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 147 | funcs.append("%s%s\n" |
| 148 | "{\n" |
| 149 | " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n" |
| 150 | " (const XGL_LAYER_DISPATCH_TABLE * const *) %s;\n" |
| 151 | " %s;\n" |
| 152 | "}" % (qual, decl, proto.params[0].name, stmt)) |
| 153 | else: |
Jon Ashburn | e05c8c6 | 2014-12-03 14:30:48 -0700 | [diff] [blame] | 154 | if proto.ret != "XGL_VOID": |
| 155 | stmt = "return " + stmt |
Tobin Ehlis | ea7fe0b | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 156 | funcs.append("%s%s\n" |
| 157 | "{\n" |
| 158 | " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n" |
| 159 | " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n" |
| 160 | " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj->baseObject;\n" |
| 161 | " %s = wrapped_obj->nextObject;\n" |
| 162 | " %s;\n" |
| 163 | "}" % (qual, decl, proto.params[0].name, proto.params[0].name, stmt)) |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 164 | |
| 165 | return "\n\n".join(funcs) |
| 166 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 167 | def generate_body(self): |
Chia-I Wu | 2e4f530 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 168 | body = [self._generate_loader_dispatch_entrypoints("LOADER_EXPORT")] |
Jon Ashburn | d38bfb1 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 169 | |
| 170 | return "\n\n".join(body) |
| 171 | |
Chia-I Wu | 03537f7 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 172 | class IcdDispatchEntrypointsSubcommand(Subcommand): |
Chia-I Wu | 03537f7 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 173 | def generate_header(self): |
Chia-I Wu | 1930060 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 174 | return "#include \"icd.h\"" |
Chia-I Wu | 03537f7 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 175 | |
Chia-I Wu | 2e4f530 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 176 | def _generate_icd_dispatch_entrypoints(self, qual=""): |
| 177 | if qual: |
| 178 | qual += " " |
| 179 | |
| 180 | funcs = [] |
| 181 | for proto in self.protos: |
| 182 | if not xgl.is_dispatchable(proto): |
| 183 | continue |
| 184 | |
| 185 | decl = proto.c_func(prefix="xgl", attr="XGLAPI") |
| 186 | stmt = "(*disp)->%s" % proto.c_call() |
| 187 | if proto.ret != "XGL_VOID": |
| 188 | stmt = "return " + stmt |
| 189 | |
| 190 | funcs.append("%s%s\n" |
| 191 | "{\n" |
| 192 | " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n" |
| 193 | " (const XGL_LAYER_DISPATCH_TABLE * const *) %s;\n" |
| 194 | " %s;\n" |
| 195 | "}" % (qual, decl, proto.params[0].name, stmt)) |
| 196 | |
| 197 | return "\n\n".join(funcs) |
| 198 | |
Chia-I Wu | 03537f7 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 199 | def generate_body(self): |
Chia-I Wu | 2e4f530 | 2015-01-02 00:21:24 +0800 | [diff] [blame] | 200 | return self._generate_icd_dispatch_entrypoints("ICD_EXPORT") |
Chia-I Wu | 03537f7 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 201 | |
Chia-I Wu | df3c432 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 202 | class IcdDispatchDummyImplSubcommand(Subcommand): |
| 203 | def run(self): |
| 204 | if len(self.argv) != 1: |
| 205 | print("IcdDispatchDummyImplSubcommand: <prefix> unspecified") |
| 206 | return |
| 207 | |
| 208 | self.prefix = self.argv[0] |
| 209 | |
| 210 | super().run() |
| 211 | |
| 212 | def generate_header(self): |
| 213 | return "#include \"icd.h\"" |
| 214 | |
| 215 | def _generate_stub_decl(self, proto): |
| 216 | plist = [] |
| 217 | for param in proto.params: |
| 218 | idx = param.ty.find("[") |
| 219 | if idx < 0: |
| 220 | idx = len(param.ty) |
| 221 | |
| 222 | pad = 44 - idx |
| 223 | if pad <= 0: |
| 224 | pad = 1 |
| 225 | |
| 226 | plist.append(" %s%s%s%s" % (param.ty[:idx], |
| 227 | " " * pad, param.name, param.ty[idx:])) |
| 228 | |
| 229 | return "%s XGLAPI %s%s(\n%s)" % (proto.ret, self.prefix, |
| 230 | proto.name, ",\n".join(plist)) |
| 231 | |
| 232 | def _generate_stubs(self): |
| 233 | stubs = [] |
| 234 | for proto in self.protos: |
| 235 | if not xgl.is_dispatchable(proto): |
| 236 | continue |
| 237 | |
| 238 | decl = self._generate_stub_decl(proto) |
| 239 | if proto.ret != "XGL_VOID": |
| 240 | stmt = " return XGL_ERROR_UNAVAILABLE;\n" |
| 241 | else: |
| 242 | stmt = "" |
| 243 | |
| 244 | stubs.append("static %s\n{\n%s}" % (decl, stmt)) |
| 245 | |
| 246 | return "\n\n".join(stubs) |
| 247 | |
| 248 | |
| 249 | def _generate_tables(self): |
| 250 | initializer = [] |
| 251 | for proto in self.protos: |
| 252 | prefix = self.prefix if xgl.is_dispatchable(proto) else "xgl" |
| 253 | initializer.append(".%s = %s%s" % |
| 254 | (proto.name, prefix, proto.name)) |
| 255 | |
Jon Ashburn | 8c05b7a | 2014-12-03 11:13:40 -0700 | [diff] [blame] | 256 | return """const XGL_LAYER_DISPATCH_TABLE %s_normal_dispatch_table = { |
Chia-I Wu | df3c432 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 257 | %s, |
| 258 | }; |
| 259 | |
Jon Ashburn | 8c05b7a | 2014-12-03 11:13:40 -0700 | [diff] [blame] | 260 | const XGL_LAYER_DISPATCH_TABLE %s_debug_dispatch_table = { |
Chia-I Wu | df3c432 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 261 | %s, |
| 262 | };""" % (self.prefix, ",\n ".join(initializer), |
| 263 | self.prefix, ",\n ".join(initializer)) |
| 264 | |
| 265 | def generate_body(self): |
| 266 | body = [self._generate_stubs(), |
| 267 | self._generate_tables()] |
| 268 | |
| 269 | return "\n\n".join(body) |
| 270 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 271 | def main(): |
| 272 | subcommands = { |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 273 | "loader": LoaderSubcommand, |
Chia-I Wu | 03537f7 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 274 | "icd-dispatch-entrypoints": IcdDispatchEntrypointsSubcommand, |
Chia-I Wu | df3c432 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 275 | "icd-dispatch-dummy-impl": IcdDispatchDummyImplSubcommand, |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if len(sys.argv) < 2 or sys.argv[1] not in subcommands: |
| 279 | print("Usage: %s <subcommand> [options]" % sys.argv[0]) |
| 280 | print |
| 281 | print("Available sucommands are: %s" % " ".join(subcommands)) |
| 282 | exit(1) |
| 283 | |
| 284 | subcmd = subcommands[sys.argv[1]](sys.argv[2:]) |
| 285 | subcmd.run() |
| 286 | |
| 287 | if __name__ == "__main__": |
| 288 | main() |