3 # Copyright (c) 2015-2016 The Khronos Group Inc.
4 # Copyright (c) 2015-2016 Valve Corporation
5 # Copyright (c) 2015-2016 LunarG, Inc.
6 # Copyright (c) 2015-2016 Google Inc.
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
20 # Author: Chia-I Wu <olv@lunarg.com>
21 # Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
22 # Author: Jon Ashburn <jon@lunarg.com>
23 # Author: Gwan-gyeong Mun <kk.moon@samsung.com>
29 def generate_get_proc_addr_check(name):
30 return " if (!%s || %s[0] != 'v' || %s[1] != 'k')\n" \
31 " return NULL;" % ((name,) * 3)
33 class Subcommand(object):
34 def __init__(self, argv):
36 self.headers = vulkan.headers
37 self.protos = vulkan.protos
42 with open(self.outfile, "w") as outfile:
43 outfile.write(self.generate())
45 print(self.generate())
48 copyright = self.generate_copyright()
49 header = self.generate_header()
50 body = self.generate_body()
51 footer = self.generate_footer()
55 contents.append(copyright)
57 contents.append(header)
61 contents.append(footer)
63 return "\n\n".join(contents)
65 def generate_copyright(self):
66 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
69 * Copyright (c) 2015-2016 The Khronos Group Inc.
70 * Copyright (c) 2015-2016 Valve Corporation
71 * Copyright (c) 2015-2016 LunarG, Inc.
73 * Licensed under the Apache License, Version 2.0 (the "License");
74 * you may not use this file except in compliance with the License.
75 * You may obtain a copy of the License at
77 * http://www.apache.org/licenses/LICENSE-2.0
79 * Unless required by applicable law or agreed to in writing, software
80 * distributed under the License is distributed on an "AS IS" BASIS,
81 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
82 * See the License for the specific language governing permissions and
83 * limitations under the License.
85 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
88 def generate_header(self):
89 return "\n".join(["#include <" + h + ">" for h in self.headers])
91 def generate_body(self):
94 def generate_footer(self):
97 class DispatchTableOpsSubcommand(Subcommand):
98 def __init__(self, argv):
100 self.headers = vulkan.headers_all
101 self.protos = vulkan.protos_all
105 if len(self.argv) < 1:
106 print("DispatchTableOpsSubcommand: <prefix> unspecified")
109 self.prefix = self.argv[0]
111 if len(self.argv) > 2:
112 print("DispatchTableOpsSubcommand: <prefix> [outfile]")
115 if len(self.argv) == 2:
116 self.outfile = self.argv[1]
118 super(DispatchTableOpsSubcommand, self).run()
120 def generate_header(self):
121 return "\n".join(["#include <vulkan/vulkan.h>",
122 "#include <vulkan/vk_layer.h>",
123 "#include <string.h>"])
125 def _generate_init_dispatch(self, type):
129 # GPA has to be first one and uses wrapped object
130 stmts.append(" memset(table, 0, sizeof(*table));")
131 stmts.append(" // Core device function pointers")
132 stmts.append(" table->GetDeviceProcAddr = (PFN_vkGetDeviceProcAddr) gpa(device, \"vkGetDeviceProcAddr\");")
134 for proto in self.protos:
135 if proto.name == "CreateInstance" or proto.name == "EnumerateInstanceExtensionProperties" or \
136 proto.name == "EnumerateInstanceLayerProperties" or proto.params[0].ty == "VkInstance" or \
137 proto.params[0].ty == "VkPhysicalDevice" or proto.name == "GetDeviceProcAddr":
139 if proto.name == "GetMemoryWin32HandleNV":
140 stmts.append("#ifdef VK_USE_PLATFORM_WIN32_KHR")
141 stmts.append(" table->%s = (PFN_vk%s) gpa(device, \"vk%s\");" %
142 (proto.name, proto.name, proto.name))
143 stmts.append("#endif // VK_USE_PLATFORM_WIN32_KHR")
145 stmts.append(" table->%s = (PFN_vk%s) gpa(device, \"vk%s\");" %
146 (proto.name, proto.name, proto.name))
147 func.append("static inline void %s_init_device_dispatch_table(VkDevice device,"
149 func.append("%s VkLayerDispatchTable *table,"
150 % (" " * len(self.prefix)))
151 func.append("%s PFN_vkGetDeviceProcAddr gpa)"
152 % (" " * len(self.prefix)))
154 stmts.append(" memset(table, 0, sizeof(*table));")
155 stmts.append(" // Core instance function pointers")
156 stmts.append(" table->GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr) gpa(instance, \"vkGetInstanceProcAddr\");")
160 Win32_printed = False
165 Android_printed = False
166 for proto in self.protos:
167 if proto.params[0].ty != "VkInstance" and proto.params[0].ty != "VkPhysicalDevice" or \
168 proto.name == "CreateDevice" or proto.name == "GetInstanceProcAddr":
170 if Win32_printed and 'Win32' not in proto.name:
171 stmts.append("#endif // VK_USE_PLATFORM_WIN32_KHR")
172 Win32_printed = False
173 if XLIB_printed and 'Xlib' not in proto.name:
174 stmts.append("#endif // VK_USE_PLATFORM_XLIB_KHR")
176 if XCB_printed and 'Xcb' not in proto.name:
177 stmts.append("#endif // VK_USE_PLATFORM_XCB_KHR")
179 if MIR_printed and 'Mir' not in proto.name:
180 stmts.append("#endif // VK_USE_PLATFORM_MIR_KHR")
182 if WAY_printed and 'Wayland' not in proto.name:
183 stmts.append("#endif // VK_USE_PLATFORM_WAYLAND_KHR")
185 if Android_printed and 'Android' not in proto.name:
186 stmts.append("#endif // VK_USE_PLATFORM_ANDROID_KHR")
187 Android_printed = False
188 if 'KHR' in proto.name and 'Win32' in proto.name:
189 if not Win32_printed:
190 stmts.append("#ifdef VK_USE_PLATFORM_WIN32_KHR")
192 if 'KHR' in proto.name and 'Xlib' in proto.name:
194 stmts.append("#ifdef VK_USE_PLATFORM_XLIB_KHR")
196 if 'KHR' in proto.name and 'Xcb' in proto.name:
198 stmts.append("#ifdef VK_USE_PLATFORM_XCB_KHR")
200 if 'KHR' in proto.name and 'Mir' in proto.name:
202 stmts.append("#ifdef VK_USE_PLATFORM_MIR_KHR")
204 if 'KHR' in proto.name and 'Wayland' in proto.name:
206 stmts.append("#ifdef VK_USE_PLATFORM_WAYLAND_KHR")
208 if 'KHR' in proto.name and 'Android' in proto.name:
209 if not Android_printed:
210 stmts.append("#ifdef VK_USE_PLATFORM_ANDROID_KHR")
211 Android_printed = True
212 if 'KHR' in proto.name and not KHR_printed:
213 stmts.append(" // KHR instance extension function pointers")
215 if 'EXT' in proto.name and not EXT_printed:
216 stmts.append(" // EXT instance extension function pointers")
218 stmts.append(" table->%s = (PFN_vk%s) gpa(instance, \"vk%s\");" %
219 (proto.name, proto.name, proto.name))
220 func.append("static inline void %s_init_instance_dispatch_table(" % self.prefix)
221 func.append("%s VkInstance instance," % (" " * len(self.prefix)))
222 func.append("%s VkLayerInstanceDispatchTable *table," % (" " * len(self.prefix)))
223 func.append("%s PFN_vkGetInstanceProcAddr gpa)" % (" " * len(self.prefix)))
225 func.append("%s" % "\n".join(stmts))
228 return "\n".join(func)
230 def generate_body(self):
231 body = [self._generate_init_dispatch("device"),
232 self._generate_init_dispatch("instance")]
234 return "\n\n".join(body)
236 class WinDefFileSubcommand(Subcommand):
241 "vk_icdGetInstanceProcAddr",
244 "vkGetInstanceProcAddr",
245 "vkGetDeviceProcAddr",
246 "vkEnumerateInstanceLayerProperties",
247 "vkEnumerateInstanceExtensionProperties"
250 "multi2GetInstanceProcAddr",
251 "multi1GetDeviceProcAddr"
255 if len(self.argv) < 2 or len(self.argv) > 3 or self.argv[1] not in library_exports:
256 print("WinDefFileSubcommand: <library-name> {%s} [outfile]" %
257 "|".join(library_exports.keys()))
260 self.library = self.argv[0]
261 if self.library == "VkLayer_multi":
262 self.exports = library_exports["layer_multi"]
264 self.exports = library_exports[self.argv[1]]
266 if len(self.argv) == 3:
267 self.outfile = self.argv[2]
269 super(WinDefFileSubcommand, self).run()
271 def generate_copyright(self):
272 return """; THIS FILE IS GENERATED. DO NOT EDIT.
274 ;;;; Begin Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277 ; Copyright (c) 2015-2016 The Khronos Group Inc.
278 ; Copyright (c) 2015-2016 Valve Corporation
279 ; Copyright (c) 2015-2016 LunarG, Inc.
281 ; Licensed under the Apache License, Version 2.0 (the "License");
282 ; you may not use this file except in compliance with the License.
283 ; You may obtain a copy of the License at
285 ; http://www.apache.org/licenses/LICENSE-2.0
287 ; Unless required by applicable law or agreed to in writing, software
288 ; distributed under the License is distributed on an "AS IS" BASIS,
289 ; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
290 ; See the License for the specific language governing permissions and
291 ; limitations under the License.
293 ; Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
294 ;;;; End Copyright Notice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"""
296 def generate_header(self):
297 return "; The following is required on Windows, for exporting symbols from the DLL"
299 def generate_body(self):
302 body.append("LIBRARY " + self.library)
303 body.append("EXPORTS")
305 for proto in self.exports:
306 if self.library != "VkLayerSwapchain" or proto != "vkEnumerateInstanceExtensionProperties" and proto != "vkEnumerateInstanceLayerProperties":
309 return "\n".join(body)
323 "dispatch-table-ops": DispatchTableOpsSubcommand,
324 "win-def-file": WinDefFileSubcommand,
327 if len(sys.argv) < 3 or sys.argv[1] not in wsi or sys.argv[2] not in subcommands:
328 print("Usage: %s <wsi> <subcommand> [options]" % sys.argv[0])
330 print("Available sucommands are: %s" % " ".join(subcommands))
333 subcmd = subcommands[sys.argv[2]](sys.argv[3:])
336 if __name__ == "__main__":